libloading/os/unix/
mod.rs

1use core::{ffi::CStr, fmt, marker, mem, ptr, ptr::null};
2
3pub use self::consts::*;
4use crate::{as_filename::AsFilename, as_symbol_name::AsSymbolName, util::ensure_compatible_types};
5
6mod consts;
7
8/// Run code and handle errors reported by `dlerror`.
9///
10/// This function first executes the `closure` function containing calls to the functions that
11/// report their errors via `dlerror`. This closure may return either `None` or `Some(*)` to
12/// further affect operation of this function.
13///
14/// In case the `closure` returns `None`, `with_dlerror` inspects the `dlerror`. `dlerror` may
15/// decide to not provide any error description, in which case `Err(None)` is returned to the
16/// caller. Otherwise the `error` callback is invoked to allow inspection and conversion of the
17/// error message. The conversion result is returned as `Err(Some(Error))`.
18///
19/// If the operations that report their errors via `dlerror` were all successful, `closure` should
20/// return `Some(T)` instead. In this case `dlerror` is not inspected at all.
21///
22/// # Notes
23///
24/// The whole `dlerror` handling scheme is done via setting and querying some global state. For
25/// that reason it is not safe to use dynamic library loading in MT-capable environment at all.
26/// Only in POSIX 2008+TC1 a thread-local state was allowed for `dlerror`, making the dl* family of
27/// functions possibly MT-safe, depending on the implementation of `dlerror`.
28///
29/// In practice (as of 2020-04-01) most of the widely used targets use a thread-local for error
30/// state and have been doing so for a long time.
31pub fn with_dlerror<T, F, Error>(closure: F, error: fn(&CStr) -> Error) -> Result<T, Option<Error>>
32where
33    F: FnOnce() -> Option<T>,
34{
35    // We used to guard all uses of dl* functions with our own mutex. This made them safe to use in
36    // MT programs provided the only way a program used dl* was via this library. However, it also
37    // had a number of downsides or cases where it failed to handle the problems. For instance,
38    // if any other library called `dlerror` internally concurrently with `libloading` things would
39    // still go awry.
40    //
41    // On platforms where `dlerror` is still MT-unsafe, `dlsym` (`Library::get`) can spuriously
42    // succeed and return a null pointer for a symbol when the actual symbol look-up operation
43    // fails. Instances where the actual symbol _could_ be `NULL` are platform specific. For
44    // instance on GNU glibc based-systems (an excerpt from dlsym(3)):
45    //
46    // > The value of a symbol returned by dlsym() will never be NULL if the shared object is the
47    // > result of normal compilation,  since  a  global  symbol is never placed at the NULL
48    // > address. There are nevertheless cases where a lookup using dlsym() may return NULL as the
49    // > value of a symbol. For example, the symbol value may be  the  result of a GNU indirect
50    // > function (IFUNC) resolver function that returns NULL as the resolved value.
51
52    // While we could could call `dlerror` here to clear the previous error value, only the `dlsym`
53    // call depends on it being cleared beforehand and only in some cases too. We will instead
54    // clear the error inside the dlsym binding instead.
55    //
56    // In all the other cases, clearing the error here will only be hiding misuse of these bindings
57    // or a bug in implementation of dl* family of functions.
58    closure().ok_or_else(|| unsafe {
59        // This code will only get executed if the `closure` returns `None`.
60        let dlerror_str = dlerror();
61        if dlerror_str.is_null() {
62            // In non-dlsym case this may happen when there’re bugs in our bindings or there’s
63            // non-libloading user of libdl; possibly in another thread.
64            None
65        } else {
66            // You can’t even rely on error string being static here; call to subsequent dlerror
67            // may invalidate or overwrite the error message. Why couldn’t they simply give up the
68            // ownership over the message?
69            // TODO: should do locale-aware conversion here. OTOH Rust doesn’t seem to work well in
70            // any system that uses non-utf8 locale, so I doubt there’s a problem here.
71            Some(error(CStr::from_ptr(dlerror_str)))
72            // Since we do a copy of the error string above, maybe we should call dlerror again to
73            // let libdl know it may free its copy of the string now?
74        }
75    })
76}
77
78/// A platform-specific counterpart of the cross-platform [`Library`](crate::Library).
79pub struct Library {
80    handle: *mut core::ffi::c_void,
81}
82
83unsafe impl Send for Library {}
84
85// That being said... this section in the volume 2 of POSIX.1-2008 states:
86//
87// > All functions defined by this volume of POSIX.1-2008 shall be thread-safe, except that the
88// > following functions need not be thread-safe.
89//
90// With notable absence of any dl* function other than dlerror in the list. By “this volume”
91// I suppose they refer precisely to the “volume 2”. dl* family of functions are specified
92// by this same volume, so the conclusion is indeed that dl* functions are required by POSIX
93// to be thread-safe. Great!
94//
95// See for more details:
96//
97//  * https://github.com/nagisa/rust_libloading/pull/17
98//  * http://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_09_01
99unsafe impl Sync for Library {}
100
101impl Library {
102    /// Find and eagerly load a shared library (module).
103    ///
104    /// If the `filename` contains a [path separator], the `filename` is interpreted as a `path` to
105    /// a file. Otherwise, platform-specific algorithms are employed to find a library with a
106    /// matching file name.
107    ///
108    /// This is equivalent to <code>[Library::open](filename, [RTLD_LAZY] | [RTLD_LOCAL])</code>.
109    ///
110    /// [path separator]: std::path::MAIN_SEPARATOR
111    ///
112    /// # Safety
113    ///
114    /// When a library is loaded, initialisation routines contained within the library are executed.
115    /// For the purposes of safety, the execution of these routines is conceptually the same calling
116    /// an unknown foreign function and may impose arbitrary requirements on the caller for the
117    /// call to be sound.
118    ///
119    /// Additionally, the callers of this function must also ensure that execution of the
120    /// termination routines contained within the library is safe as well. These routines may be
121    /// executed when the library is unloaded.
122    #[inline]
123    pub unsafe fn new(filename: impl AsFilename) -> Result<Library, crate::Error> {
124        Library::open(Some(filename), RTLD_LAZY | RTLD_LOCAL)
125    }
126
127    /// Load the `Library` representing the current executable.
128    ///
129    /// [`Library::get`] calls of the returned `Library` will look for symbols in following
130    /// locations in order:
131    ///
132    /// 1. The original program image;
133    /// 2. Any executable object files (e.g. shared libraries) loaded at program startup;
134    /// 3. Any executable object files loaded at runtime (e.g. via other `Library::new` calls or via
135    ///    calls to the `dlopen` function).
136    ///
137    /// Note that the behaviour of a `Library` loaded with this method is different from that of
138    /// Libraries loaded with [`os::windows::Library::this`].
139    ///
140    /// This is equivalent to <code>[Library::open](None, [RTLD_LAZY] | [RTLD_LOCAL])</code>.
141    ///
142    /// [`os::windows::Library::this`]: crate::os::windows::Library::this
143    #[inline]
144    pub fn this() -> Library {
145        unsafe {
146            // SAFE: this does not load any new shared library images, no danger in it executing
147            // initialiser routines.
148            Library::open_char_ptr(null(), RTLD_LAZY | RTLD_LOCAL).expect("this should never fail")
149        }
150    }
151
152    /// Find and load an executable object file (shared library).
153    ///
154    /// See documentation for [`Library::this`] for further description of the behaviour
155    /// when the `filename` is `None`. Otherwise see [`Library::new`].
156    ///
157    /// Corresponds to `dlopen(filename, flags)`.
158    ///
159    /// # Safety
160    ///
161    /// When a library is loaded, initialisation routines contained within the library are executed.
162    /// For the purposes of safety, the execution of these routines is conceptually the same calling
163    /// an unknown foreign function and may impose arbitrary requirements on the caller for the
164    /// call to be sound.
165    ///
166    /// Additionally, the callers of this function must also ensure that execution of the
167    /// termination routines contained within the library is safe as well. These routines may be
168    /// executed when the library is unloaded.
169    pub unsafe fn open<P>(
170        filename: Option<P>,
171        flags: core::ffi::c_int,
172    ) -> Result<Library, crate::Error>
173    where
174        P: AsFilename,
175    {
176        let Some(filename) = filename else {
177            return Self::open_char_ptr(null(), flags);
178        };
179        filename.posix_filename(|posix_filename| Library::open_char_ptr(posix_filename, flags))
180    }
181
182    /// private helper to call dlopen+dlerror once we de-tangled the string into a raw pointer to a
183    /// 0 terminated utf-8 string. caller must ensure that the string is actually 0 terminated.
184    unsafe fn open_char_ptr(
185        filename: *const core::ffi::c_char,
186        flags: core::ffi::c_int,
187    ) -> Result<Library, crate::Error> {
188        with_dlerror(
189            move || {
190                let result = dlopen(filename, flags);
191
192                // ensure filename lives until dlopen completes
193                if result.is_null() {
194                    None
195                } else {
196                    Some(Library { handle: result })
197                }
198            },
199            |desc| crate::Error::DlOpen {
200                source: desc.into(),
201            },
202        )
203        .map_err(|e| e.unwrap_or(crate::Error::DlOpenUnknown))
204    }
205
206    unsafe fn get_impl<T, F>(
207        &self,
208        symbol: impl AsSymbolName,
209        on_null: F,
210    ) -> Result<Symbol<T>, crate::Error>
211    where
212        F: FnOnce() -> Result<Symbol<T>, crate::Error>,
213    {
214        ensure_compatible_types::<T, *mut core::ffi::c_void>()?;
215        // `dlsym` may return nullptr in two cases: when a symbol genuinely points to a null
216        // pointer or the symbol cannot be found. In order to detect this case a double dlerror
217        // pattern must be used, which is, sadly, a little bit racy.
218        //
219        // We try to leave as little space as possible for this to occur, but we can’t exactly
220        // fully prevent it.
221        symbol.symbol_name(|posix_symbol| {
222            let result = with_dlerror(
223                || {
224                    dlerror();
225                    let symbol = dlsym(self.handle, posix_symbol);
226                    if symbol.is_null() {
227                        None
228                    } else {
229                        Some(Symbol {
230                            pointer: symbol,
231                            pd: marker::PhantomData,
232                        })
233                    }
234                },
235                |desc| crate::Error::DlSym {
236                    source: desc.into(),
237                },
238            );
239            match result {
240                Err(None) => on_null(),
241                Err(Some(e)) => Err(e),
242                Ok(x) => Ok(x),
243            }
244        })
245    }
246
247    /// Get a pointer to a function or static variable by symbol name.
248    ///
249    /// The `symbol` may not contain any null bytes, with the exception of the last byte. Providing
250    /// a null terminated `symbol` may help to avoid an allocation.
251    ///
252    /// Symbol is interpreted as-is; no mangling is done. This means that symbols like `x::y` are
253    /// most likely invalid.
254    ///
255    /// # Safety
256    ///
257    /// Users of this API must specify the correct type of the function or variable loaded. Using a
258    /// `Symbol` with a wrong type is undefined.
259    ///
260    /// # Platform-specific behaviour
261    ///
262    /// Implementation of thread local variables is extremely platform specific and uses of such
263    /// variables that work on e.g. Linux may have unintended behaviour on other targets.
264    ///
265    /// On POSIX implementations where the `dlerror` function is not confirmed to be MT-safe (such
266    /// as FreeBSD), this function will unconditionally return an error when the underlying `dlsym`
267    /// call returns a null pointer. There are rare situations where `dlsym` returns a genuine null
268    /// pointer without it being an error. If loading a null pointer is something you care about,
269    /// consider using the [`Library::get_singlethreaded`] call.
270    #[inline(always)]
271    pub unsafe fn get<T>(&self, symbol: impl AsSymbolName) -> Result<Symbol<T>, crate::Error> {
272        #[cfg_attr(libloading_docs, allow(unused_extern_crates))]
273        #[cfg(libloading_docs)]
274        extern crate cfg_if;
275        cfg_if::cfg_if! {
276            // These targets are known to have MT-safe `dlerror`.
277            if #[cfg(any(
278                target_os = "linux",
279                target_os = "android",
280                target_os = "openbsd",
281                target_os = "macos",
282                target_os = "ios",
283                target_os = "solaris",
284                target_os = "illumos",
285                target_os = "redox",
286                target_os = "fuchsia",
287                target_os = "cygwin",
288                target_os = "twizzler",
289            ))] {
290                self.get_singlethreaded(symbol)
291            } else {
292                self.get_impl(symbol, || Err(crate::Error::DlSymUnknown))
293            }
294        }
295    }
296
297    /// Get a pointer to function or static variable by symbol name.
298    ///
299    /// The `symbol` may not contain any null bytes, with the exception of the last byte. Providing
300    /// a null terminated `symbol` may help to avoid an allocation.
301    ///
302    /// Symbol is interpreted as-is; no mangling is done. This means that symbols like `x::y` are
303    /// most likely invalid.
304    ///
305    /// # Safety
306    ///
307    /// Users of this API must specify the correct type of the function or variable loaded.
308    ///
309    /// It is up to the user of this library to ensure that no other calls to an MT-unsafe
310    /// implementation of `dlerror` occur during the execution of this function. Failing that, the
311    /// behaviour of this function is not defined.
312    ///
313    /// # Platform-specific behaviour
314    ///
315    /// The implementation of thread-local variables is extremely platform specific and uses of such
316    /// variables that work on e.g. Linux may have unintended behaviour on other targets.
317    #[inline(always)]
318    pub unsafe fn get_singlethreaded<T>(
319        &self,
320        symbol: impl AsSymbolName,
321    ) -> Result<Symbol<T>, crate::Error> {
322        self.get_impl(symbol, || {
323            Ok(Symbol {
324                pointer: ptr::null_mut(),
325                pd: marker::PhantomData,
326            })
327        })
328    }
329
330    /// Convert the `Library` to a raw handle.
331    ///
332    /// The handle returned by this function shall be usable with APIs which accept handles
333    /// as returned by `dlopen`.
334    pub fn into_raw(self) -> *mut core::ffi::c_void {
335        let handle = self.handle;
336        mem::forget(self);
337        handle
338    }
339
340    /// Convert a raw handle returned by `dlopen`-family of calls to a `Library`.
341    ///
342    /// # Safety
343    ///
344    /// The pointer shall be a result of a successful call of the `dlopen`-family of functions or a
345    /// pointer previously returned by `Library::into_raw` call. It must be valid to call `dlclose`
346    /// with this pointer as an argument.
347    pub unsafe fn from_raw(handle: *mut core::ffi::c_void) -> Library {
348        Library { handle }
349    }
350
351    /// Unload the library.
352    ///
353    /// This method might be a no-op, depending on the flags with which the `Library` was opened,
354    /// what library was opened or other platform specifics.
355    ///
356    /// You only need to call this if you are interested in handling any errors that may arise when
357    /// library is unloaded. Otherwise the implementation of `Drop` for `Library` will close the
358    /// library and ignore the errors were they arise.
359    ///
360    /// The underlying data structures may still get leaked if an error does occur.
361    pub fn close(self) -> Result<(), crate::Error> {
362        let result = with_dlerror(
363            || {
364                if unsafe { dlclose(self.handle) } == 0 {
365                    Some(())
366                } else {
367                    None
368                }
369            },
370            |desc| crate::Error::DlClose {
371                source: desc.into(),
372            },
373        )
374        .map_err(|e| e.unwrap_or(crate::Error::DlCloseUnknown));
375        // While the library is not free'd yet in case of an error, there is no reason to try
376        // dropping it again, because all that will do is try calling `dlclose` again. only
377        // this time it would ignore the return result, which we already seen failing…
378        mem::forget(self);
379        result
380    }
381}
382
383impl Drop for Library {
384    fn drop(&mut self) {
385        unsafe {
386            dlclose(self.handle);
387        }
388    }
389}
390
391impl fmt::Debug for Library {
392    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
393        f.write_fmt(format_args!("Library@{:p}", self.handle))
394    }
395}
396
397/// Symbol from a library.
398///
399/// A major difference compared to the cross-platform `Symbol` is that this does not ensure that the
400/// `Symbol` does not outlive the `Library` it comes from.
401pub struct Symbol<T> {
402    pointer: *mut core::ffi::c_void,
403    pd: marker::PhantomData<T>,
404}
405
406impl<T> Symbol<T> {
407    /// Convert the loaded `Symbol` into a raw pointer.
408    pub fn into_raw(self) -> *mut core::ffi::c_void {
409        self.pointer
410    }
411
412    /// Convert the loaded `Symbol` into a raw pointer.
413    /// For unix this does the same as into_raw.
414    pub fn as_raw_ptr(self) -> *mut core::ffi::c_void {
415        self.pointer
416    }
417}
418
419impl<T> Symbol<Option<T>> {
420    /// Lift Option out of the symbol.
421    pub fn lift_option(self) -> Option<Symbol<T>> {
422        if self.pointer.is_null() {
423            None
424        } else {
425            Some(Symbol {
426                pointer: self.pointer,
427                pd: marker::PhantomData,
428            })
429        }
430    }
431}
432
433unsafe impl<T: Send> Send for Symbol<T> {}
434unsafe impl<T: Sync> Sync for Symbol<T> {}
435
436impl<T> Clone for Symbol<T> {
437    fn clone(&self) -> Symbol<T> {
438        Symbol { ..*self }
439    }
440}
441
442impl<T> core::ops::Deref for Symbol<T> {
443    type Target = T;
444    fn deref(&self) -> &T {
445        unsafe {
446            // Additional reference level for a dereference on `deref` return value.
447            &*(&self.pointer as *const *mut _ as *const T)
448        }
449    }
450}
451
452impl<T> fmt::Debug for Symbol<T> {
453    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
454        unsafe {
455            let mut info = mem::MaybeUninit::<DlInfo>::uninit();
456            if dladdr(self.pointer, info.as_mut_ptr()) != 0 {
457                let info = info.assume_init();
458                if info.dli_sname.is_null() {
459                    f.write_fmt(format_args!(
460                        "Symbol@{:p} from {:?}",
461                        self.pointer,
462                        CStr::from_ptr(info.dli_fname)
463                    ))
464                } else {
465                    f.write_fmt(format_args!(
466                        "Symbol {:?}@{:p} from {:?}",
467                        CStr::from_ptr(info.dli_sname),
468                        self.pointer,
469                        CStr::from_ptr(info.dli_fname)
470                    ))
471                }
472            } else {
473                f.write_fmt(format_args!("Symbol@{:p}", self.pointer))
474            }
475        }
476    }
477}
478
479// Platform specific things
480#[cfg_attr(
481    any(target_os = "linux", target_os = "android", target_os = "twizzler"),
482    link(name = "dl")
483)]
484#[cfg_attr(any(target_os = "freebsd", target_os = "dragonfly"), link(name = "c"))]
485extern "C" {
486    fn dlopen(
487        filename: *const core::ffi::c_char,
488        flags: core::ffi::c_int,
489    ) -> *mut core::ffi::c_void;
490    fn dlclose(handle: *mut core::ffi::c_void) -> core::ffi::c_int;
491    fn dlsym(
492        handle: *mut core::ffi::c_void,
493        symbol: *const core::ffi::c_char,
494    ) -> *mut core::ffi::c_void;
495    fn dlerror() -> *mut core::ffi::c_char;
496    fn dladdr(addr: *mut core::ffi::c_void, info: *mut DlInfo) -> core::ffi::c_int;
497}
498
499#[repr(C)]
500struct DlInfo {
501    dli_fname: *const core::ffi::c_char,
502    dli_fbase: *mut core::ffi::c_void,
503    dli_sname: *const core::ffi::c_char,
504    dli_saddr: *mut core::ffi::c_void,
505}