Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

maint: Avoid loading shared libraries more than once #165

Merged
merged 2 commits into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed

- Fixed a bug that prevented the `XShm` module from loading.
- Ensured that the linked libraries are cached and only loaded once.

## [2.20.1] - 2022-11-24
### Added
Expand Down
2 changes: 1 addition & 1 deletion x11-dl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ workspace = ".."
edition = "2021"

[dependencies]
lazy_static = "1"
libc = "0.2"
once_cell = "1.17.0"

[build-dependencies]
pkg-config = "0.3.24"
3 changes: 0 additions & 3 deletions x11-dl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
#![allow(deref_nullptr)]
#![allow(clippy::missing_safety_doc)]

#[macro_use]
extern crate lazy_static;

extern crate libc;

#[macro_use]
Expand Down
60 changes: 30 additions & 30 deletions x11-dl/src/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ macro_rules! x11_link {
globals:
$(pub static $var_name:ident : $var_type:ty,)*
} => {
#[non_exhaustive]
#[derive(Copy, Clone)]
pub struct $struct_name {
#[allow(dead_code)]
lib: crate::link::DynamicLibrary,
$(pub $fn_name: unsafe extern "C" fn ($($param_type),*) -> $ret_type,)*
$(pub $vfn_name: unsafe extern "C" fn ($($vparam_type),+, ...) -> $vret_type,)*
$(pub $var_name: *mut $var_type,)*
Expand All @@ -34,31 +34,28 @@ macro_rules! x11_link {
unsafe impl Sync for $struct_name {}

impl $struct_name {
unsafe fn init (this: *mut Self) -> Result<(), $crate::error::OpenError> {
lazy_static! {
static ref SYMS: [(&'static str, usize); $nsyms] = unsafe {[
$((stringify!($fn_name), &((*core::ptr::null::<$struct_name>()).$fn_name) as *const _ as usize),)*
$((stringify!($vfn_name), &((*core::ptr::null::<$struct_name>()).$vfn_name) as *const _ as usize),)*
$((stringify!($var_name), &((*core::ptr::null::<$struct_name>()).$var_name) as *const _ as usize),)*
Comment on lines -40 to -42

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also FYI, the old code that was removed here has Undefined Behavior -- offsetting a null pointer by a non-zero amount (such as to project to the field, as in this code) is not allowed. Lucky enough we have offset_of! these days which makes it much less likely that people write code like this. :)

]};
}
let offset = this as usize;
for &(name, sym_offset) in SYMS.iter() {
*((offset + sym_offset) as *mut *mut _) = (*this).lib.symbol(name)?;
}
Ok(())
}

pub fn open () -> Result<$struct_name, $crate::error::OpenError> {
unsafe {
let libdir = $crate::link::config::libdir::$pkg_name;
let lib = $crate::link::DynamicLibrary::open_multi(libdir, &[$($lib_name),*])?;
let mut this = ::std::mem::MaybeUninit::<$struct_name>::uninit();
let this_ptr = this.as_mut_ptr();
::std::ptr::write(&mut (*this_ptr).lib, lib);
Self::init(this_ptr)?;
Ok(this.assume_init())
}
/// Cached function pointers and global variables for X11 libraries.
static CACHED: once_cell::sync::OnceCell<($crate::link::DynamicLibrary, $struct_name)> = once_cell::sync::OnceCell::new();

// Use the cached library or open a new one.
let (_, funcs) = CACHED.get_or_try_init(|| {
unsafe {
let libdir = $crate::link::config::libdir::$pkg_name;
let lib = $crate::link::DynamicLibrary::open_multi(libdir, &[$($lib_name),*])?;

// Load every function pointer.
let funcs = $struct_name {
$($fn_name: ::std::mem::transmute(lib.symbol(stringify!($fn_name))?),)*
$($vfn_name: ::std::mem::transmute(lib.symbol(stringify!($vfn_name))?),)*
$($var_name: ::std::mem::transmute(lib.symbol(stringify!($var_name))?),)*
};

Ok((lib, funcs))
}
})?;

Ok(*funcs)
}
}
};
Expand Down Expand Up @@ -176,9 +173,12 @@ impl DynamicLibrary {
}

impl Drop for DynamicLibrary {
fn drop(&mut self) {
unsafe {
libc::dlclose(self.handle as *mut _);
}
fn drop (&mut self) {
unsafe {
libc::dlclose(self.handle as *mut _);
}
}
}

unsafe impl Send for DynamicLibrary {}
unsafe impl Sync for DynamicLibrary {}