libloading/os/unix/
mod.rs

1use util::{ensure_compatible_types, cstr_cow_from_bytes};
2
3use std::ffi::{CStr, OsStr};
4use std::{fmt, marker, mem, ptr};
5use std::os::raw;
6use std::os::unix::ffi::OsStrExt;
7
8// dl* family of functions did not have enough thought put into it.
9//
10// Whole error handling scheme is done via setting and querying some global state, therefore it is
11// not safe to use dynamic library loading in MT-capable environment at all. Only in POSIX 2008+TC1
12// a thread-local state was allowed for `dlerror`, making the dl* family of functions MT-safe.
13//
14// In practice (as of 2020-04-01) most of the widely used targets use a thread-local for error
15// state and have been doing so for a long time. Regardless the comments in this function shall
16// remain as a documentation for the future generations.
17fn with_dlerror<T, F>(wrap: fn(crate::error::DlDescription) -> crate::Error, closure: F)
18-> Result<T, Option<crate::Error>>
19where F: FnOnce() -> Option<T> {
20    // We used to guard all uses of dl* functions with our own mutex. This made them safe to use in
21    // MT programs provided the only way a program used dl* was via this library. However, it also
22    // had a number of downsides or cases where it failed to handle the problems. For instance,
23    // if any other library called `dlerror` internally concurrently with `libloading` things would
24    // still go awry.
25    //
26    // On platforms where `dlerror` is still MT-unsafe, `dlsym` (`Library::get`) can spuriously
27    // succeed and return a null pointer for a symbol when the actual symbol look-up operation
28    // fails. Instances where the actual symbol _could_ be `NULL` are platform specific. For
29    // instance on GNU glibc based-systems (an excerpt from dlsym(3)):
30    //
31    // > The value of a symbol returned by dlsym() will never be NULL if the shared object is the
32    // > result of normal compilation,  since  a  global  symbol is never placed at the NULL
33    // > address. There are nevertheless cases where a lookup using dlsym() may return NULL as the
34    // > value of a symbol. For example, the symbol value may be  the  result of a GNU indirect
35    // > function (IFUNC) resolver function that returns NULL as the resolved value.
36
37    // While we could could call `dlerror` here to clear the previous error value, only the `dlsym`
38    // call depends on it being cleared beforehand and only in some cases too. We will instead
39    // clear the error inside the dlsym binding instead.
40    //
41    // In all the other cases, clearing the error here will only be hiding misuse of these bindings
42    // or a bug in implementation of dl* family of functions.
43    closure().ok_or_else(|| unsafe {
44        // This code will only get executed if the `closure` returns `None`.
45        let error = dlerror();
46        if error.is_null() {
47            // In non-dlsym case this may happen when there’re bugs in our bindings or there’s
48            // non-libloading user of libdl; possibly in another thread.
49            None
50        } else {
51            // You can’t even rely on error string being static here; call to subsequent dlerror
52            // may invalidate or overwrite the error message. Why couldn’t they simply give up the
53            // ownership over the message?
54            // TODO: should do locale-aware conversion here. OTOH Rust doesn’t seem to work well in
55            // any system that uses non-utf8 locale, so I doubt there’s a problem here.
56            let message = CStr::from_ptr(error).into();
57            Some(wrap(crate::error::DlDescription(message)))
58            // Since we do a copy of the error string above, maybe we should call dlerror again to
59            // let libdl know it may free its copy of the string now?
60        }
61    })
62}
63
64/// A platform-specific equivalent of the cross-platform `Library`.
65pub struct Library {
66    handle: *mut raw::c_void
67}
68
69unsafe impl Send for Library {}
70
71// That being said... this section in the volume 2 of POSIX.1-2008 states:
72//
73// > All functions defined by this volume of POSIX.1-2008 shall be thread-safe, except that the
74// > following functions need not be thread-safe.
75//
76// With notable absence of any dl* function other than dlerror in the list. By “this volume”
77// I suppose they refer precisely to the “volume 2”. dl* family of functions are specified
78// by this same volume, so the conclusion is indeed that dl* functions are required by POSIX
79// to be thread-safe. Great!
80//
81// See for more details:
82//
83//  * https://github.com/nagisa/rust_libloading/pull/17
84//  * http://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_09_01
85unsafe impl Sync for Library {}
86
87impl Library {
88    /// Find and load a shared library (module).
89    ///
90    /// Locations where library is searched for is platform specific and can’t be adjusted
91    /// portably.
92    ///
93    /// Corresponds to `dlopen(filename, RTLD_NOW)`.
94    #[inline]
95    pub fn new<P: AsRef<OsStr>>(filename: P) -> Result<Library, crate::Error> {
96        Library::open(Some(filename), RTLD_NOW)
97    }
98
99    /// Load the dynamic libraries linked into main program.
100    ///
101    /// This allows retrieving symbols from any **dynamic** library linked into the program,
102    /// without specifying the exact library.
103    ///
104    /// Corresponds to `dlopen(NULL, RTLD_NOW)`.
105    #[inline]
106    pub fn this() -> Library {
107        Library::open(None::<&OsStr>, RTLD_NOW).expect("this should never fail")
108    }
109
110    /// Find and load a shared library (module).
111    ///
112    /// Locations where library is searched for is platform specific and can’t be adjusted
113    /// portably.
114    ///
115    /// If the `filename` is None, null pointer is passed to `dlopen`.
116    ///
117    /// Corresponds to `dlopen(filename, flags)`.
118    pub fn open<P>(filename: Option<P>, flags: raw::c_int) -> Result<Library, crate::Error>
119    where P: AsRef<OsStr> {
120        let filename = match filename {
121            None => None,
122            Some(ref f) => Some(cstr_cow_from_bytes(f.as_ref().as_bytes())?),
123        };
124        with_dlerror(|desc| crate::Error::DlOpen { desc }, move || {
125            let result = unsafe {
126                let r = dlopen(match filename {
127                    None => ptr::null(),
128                    Some(ref f) => f.as_ptr()
129                }, flags);
130                // ensure filename lives until dlopen completes
131                drop(filename);
132                r
133            };
134            if result.is_null() {
135                None
136            } else {
137                Some(Library {
138                    handle: result
139                })
140            }
141        }).map_err(|e| e.unwrap_or(crate::Error::DlOpenUnknown))
142    }
143
144    unsafe fn get_impl<T, F>(&self, symbol: &[u8], on_null: F) -> Result<Symbol<T>, crate::Error>
145    where F: FnOnce() -> Result<Symbol<T>, crate::Error>
146    {
147        ensure_compatible_types::<T, *mut raw::c_void>()?;
148        let symbol = cstr_cow_from_bytes(symbol)?;
149        // `dlsym` may return nullptr in two cases: when a symbol genuinely points to a null
150        // pointer or the symbol cannot be found. In order to detect this case a double dlerror
151        // pattern must be used, which is, sadly, a little bit racy.
152        //
153        // We try to leave as little space as possible for this to occur, but we can’t exactly
154        // fully prevent it.
155        match with_dlerror(|desc| crate::Error::DlSym { desc }, || {
156            dlerror();
157            let symbol = dlsym(self.handle, symbol.as_ptr());
158            if symbol.is_null() {
159                None
160            } else {
161                Some(Symbol {
162                    pointer: symbol,
163                    pd: marker::PhantomData
164                })
165            }
166        }) {
167            Err(None) => on_null(),
168            Err(Some(e)) => Err(e),
169            Ok(x) => Ok(x)
170        }
171
172    }
173
174    /// Get a pointer to function or static variable by symbol name.
175    ///
176    /// The `symbol` may not contain any null bytes, with an exception of last byte. A null
177    /// terminated `symbol` may avoid a string allocation in some cases.
178    ///
179    /// Symbol is interpreted as-is; no mangling is done. This means that symbols like `x::y` are
180    /// most likely invalid.
181    ///
182    /// ## Unsafety
183    ///
184    /// This function does not validate the type `T`. It is up to the user of this function to
185    /// ensure that the loaded symbol is in fact a `T`. Using a value with a wrong type has no
186    /// definied behaviour.
187    ///
188    ///
189    ///
190    /// ## Platform-specific behaviour
191    ///
192    /// OS X uses some sort of lazy initialization scheme, which makes loading TLS variables
193    /// impossible. Using a TLS variable loaded this way on OS X is undefined behaviour.
194    ///
195    /// On POSIX implementations where the `dlerror` function is not confirmed to be MT-safe (such
196    /// as FreeBSD), this function will unconditionally return an error the underlying `dlsym` call
197    /// returns a null pointer. There are rare situations where `dlsym` returns a genuine null
198    /// pointer without it being an error. If loading a null pointer is something you care about,
199    /// consider using the [`Library::get_singlethreaded`] call.
200    #[inline(always)]
201    pub unsafe fn get<T>(&self, symbol: &[u8]) -> Result<Symbol<T>, crate::Error> {
202        #[cfg(mtsafe_dlerror)]
203        { return self.get_singlethreaded(symbol); }
204        #[cfg(not(mtsafe_dlerror))]
205        {
206            return self.get_impl(symbol, || Err(crate::Error::DlSymUnknown));
207        }
208    }
209
210    /// Get a pointer to function or static variable by symbol name.
211    ///
212    /// The `symbol` may not contain any null bytes, with an exception of last byte. A null
213    /// terminated `symbol` may avoid a string allocation in some cases.
214    ///
215    /// Symbol is interpreted as-is; no mangling is done. This means that symbols like `x::y` are
216    /// most likely invalid.
217    ///
218    /// ## Unsafety
219    ///
220    /// This function does not validate the type `T`. It is up to the user of this function to
221    /// ensure that the loaded symbol is in fact a `T`. Using a value with a wrong type has no
222    /// definied behaviour.
223    ///
224    /// It is up to the user of this library to ensure that no other calls to an MT-unsafe
225    /// implementation of `dlerror` occur while this function is executing. Failing that the
226    /// results of this function are not defined.
227    ///
228    /// ## Platform-specific behaviour
229    ///
230    /// OS X uses some sort of lazy initialization scheme, which makes loading TLS variables
231    /// impossible. Using a TLS variable loaded this way on OS X is undefined behaviour.
232    #[inline(always)]
233    pub unsafe fn get_singlethreaded<T>(&self, symbol: &[u8]) -> Result<Symbol<T>, crate::Error> {
234        self.get_impl(symbol, || Ok(Symbol {
235            pointer: ptr::null_mut(),
236            pd: marker::PhantomData
237        }))
238    }
239
240    /// Convert the `Library` to a raw handle.
241    ///
242    /// The handle returned by this function shall be usable with APIs which accept handles
243    /// as returned by `dlopen`.
244    pub fn into_raw(self) -> *mut raw::c_void {
245        let handle = self.handle;
246        mem::forget(self);
247        handle
248    }
249
250    /// Convert a raw handle returned by `dlopen`-family of calls to a `Library`.
251    ///
252    /// ## Unsafety
253    ///
254    /// The pointer shall be a result of a successful call of the `dlopen`-family of functions or a
255    /// pointer previously returned by `Library::into_raw` call. It must be valid to call `dlclose`
256    /// with this pointer as an argument.
257    pub unsafe fn from_raw(handle: *mut raw::c_void) -> Library {
258        Library {
259            handle: handle
260        }
261    }
262
263    /// Unload the library.
264    ///
265    /// This method might be a no-op, depending on the flags with which the `Library` was opened,
266    /// what library was opened or other platform specifics.
267    ///
268    /// You only need to call this if you are interested in handling any errors that may arise when
269    /// library is unloaded. Otherwise the implementation of `Drop` for `Library` will close the
270    /// library and ignore the errors were they arise.
271    pub fn close(self) -> Result<(), crate::Error> {
272        let result = with_dlerror(|desc| crate::Error::DlClose { desc }, || {
273            if unsafe { dlclose(self.handle) } == 0 {
274                Some(())
275            } else {
276                None
277            }
278        }).map_err(|e| e.unwrap_or(crate::Error::DlCloseUnknown));
279        std::mem::forget(self);
280        result
281    }
282}
283
284impl Drop for Library {
285    fn drop(&mut self) {
286        unsafe {
287            dlclose(self.handle);
288        }
289    }
290}
291
292impl fmt::Debug for Library {
293    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
294        f.write_str(&format!("Library@{:p}", self.handle))
295    }
296}
297
298/// Symbol from a library.
299///
300/// A major difference compared to the cross-platform `Symbol` is that this does not ensure the
301/// `Symbol` does not outlive `Library` it comes from.
302pub struct Symbol<T> {
303    pointer: *mut raw::c_void,
304    pd: marker::PhantomData<T>
305}
306
307impl<T> Symbol<T> {
308    /// Convert the loaded Symbol into a raw pointer.
309    pub fn into_raw(self) -> *mut raw::c_void {
310        let pointer = self.pointer;
311        mem::forget(self);
312        pointer
313    }
314}
315
316impl<T> Symbol<Option<T>> {
317    /// Lift Option out of the symbol.
318    pub fn lift_option(self) -> Option<Symbol<T>> {
319        if self.pointer.is_null() {
320            None
321        } else {
322            Some(Symbol {
323                pointer: self.pointer,
324                pd: marker::PhantomData,
325            })
326        }
327    }
328}
329
330unsafe impl<T: Send> Send for Symbol<T> {}
331unsafe impl<T: Sync> Sync for Symbol<T> {}
332
333impl<T> Clone for Symbol<T> {
334    fn clone(&self) -> Symbol<T> {
335        Symbol { ..*self }
336    }
337}
338
339impl<T> ::std::ops::Deref for Symbol<T> {
340    type Target = T;
341    fn deref(&self) -> &T {
342        unsafe {
343            // Additional reference level for a dereference on `deref` return value.
344            mem::transmute(&self.pointer)
345        }
346    }
347}
348
349impl<T> fmt::Debug for Symbol<T> {
350    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
351        unsafe {
352            let mut info = mem::MaybeUninit::<DlInfo>::uninit();
353            if dladdr(self.pointer, info.as_mut_ptr()) != 0 {
354                let info = info.assume_init();
355                if info.dli_sname.is_null() {
356                    f.write_str(&format!("Symbol@{:p} from {:?}",
357                                         self.pointer,
358                                         CStr::from_ptr(info.dli_fname)))
359                } else {
360                    f.write_str(&format!("Symbol {:?}@{:p} from {:?}",
361                                         CStr::from_ptr(info.dli_sname), self.pointer,
362                                         CStr::from_ptr(info.dli_fname)))
363                }
364            } else {
365                f.write_str(&format!("Symbol@{:p}", self.pointer))
366            }
367        }
368    }
369}
370
371// Platform specific things
372
373extern {
374    fn dlopen(filename: *const raw::c_char, flags: raw::c_int) -> *mut raw::c_void;
375    fn dlclose(handle: *mut raw::c_void) -> raw::c_int;
376    fn dlsym(handle: *mut raw::c_void, symbol: *const raw::c_char) -> *mut raw::c_void;
377    fn dlerror() -> *mut raw::c_char;
378    fn dladdr(addr: *mut raw::c_void, info: *mut DlInfo) -> raw::c_int;
379}
380
381#[cfg(not(target_os="android"))]
382const RTLD_NOW: raw::c_int = 2;
383#[cfg(target_os="android")]
384const RTLD_NOW: raw::c_int = 0;
385
386#[repr(C)]
387struct DlInfo {
388  dli_fname: *const raw::c_char,
389  dli_fbase: *mut raw::c_void,
390  dli_sname: *const raw::c_char,
391  dli_saddr: *mut raw::c_void
392}
393
394#[cfg(test)]
395mod tests {
396    #[test]
397    fn this() {
398        super::Library::this();
399    }
400}