1 use util::{ensure_compatible_types, cstr_cow_from_bytes};
2 
3 use std::ffi::{CStr, OsStr};
4 use std::{fmt, io, marker, mem, ptr};
5 use std::os::raw;
6 use std::os::unix::ffi::OsStrExt;
7 
8 extern "C" {
rust_libloading_dlerror_mutex_lock()9     fn rust_libloading_dlerror_mutex_lock();
rust_libloading_dlerror_mutex_unlock()10     fn rust_libloading_dlerror_mutex_unlock();
11 }
12 
13 struct DlerrorMutexGuard(());
14 
15 impl DlerrorMutexGuard {
new() -> DlerrorMutexGuard16     fn new() -> DlerrorMutexGuard {
17         unsafe {
18             rust_libloading_dlerror_mutex_lock();
19         }
20         DlerrorMutexGuard(())
21     }
22 }
23 
24 impl Drop for DlerrorMutexGuard {
drop(&mut self)25     fn drop(&mut self) {
26         unsafe {
27             rust_libloading_dlerror_mutex_unlock();
28         }
29     }
30 }
31 
32 // libdl is crazy.
33 //
34 // First of all, whole error handling scheme in libdl is done via setting and querying some global
35 // state, therefore it is not safe to use libdl in MT-capable environment at all. Only in POSIX
36 // 2008+TC1 a thread-local state was allowed, which for our purposes is way too late.
with_dlerror<T, F>(closure: F) -> Result<T, Option<io::Error>> where F: FnOnce() -> Option<T>37 fn with_dlerror<T, F>(closure: F) -> Result<T, Option<io::Error>>
38 where F: FnOnce() -> Option<T> {
39     // We will guard all uses of libdl library with our own mutex. This makes libdl
40     // safe to use in MT programs provided the only way a program uses libdl is via this library.
41     let _lock = DlerrorMutexGuard::new();
42     // While we could could call libdl here to clear the previous error value, only the dlsym
43     // depends on it being cleared beforehand and only in some cases too. We will instead clear the
44     // error inside the dlsym binding instead.
45     //
46     // In all the other cases, clearing the error here will only be hiding misuse of these bindings
47     // or the libdl.
48     closure().ok_or_else(|| unsafe {
49         // This code will only get executed if the `closure` returns `None`.
50         let error = dlerror();
51         if error.is_null() {
52             // In non-dlsym case this may happen when there’re bugs in our bindings or there’s
53             // non-libloading user of libdl; possibly in another thread.
54             None
55         } else {
56             // You can’t even rely on error string being static here; call to subsequent dlerror
57             // may invalidate or overwrite the error message. Why couldn’t they simply give up the
58             // ownership over the message?
59             // TODO: should do locale-aware conversion here. OTOH Rust doesn’t seem to work well in
60             // any system that uses non-utf8 locale, so I doubt there’s a problem here.
61             let message = CStr::from_ptr(error).to_string_lossy().into_owned();
62             Some(io::Error::new(io::ErrorKind::Other, message))
63             // Since we do a copy of the error string above, maybe we should call dlerror again to
64             // let libdl know it may free its copy of the string now?
65         }
66     })
67 }
68 
69 /// A platform-specific equivalent of the cross-platform `Library`.
70 pub struct Library {
71     handle: *mut raw::c_void
72 }
73 
74 unsafe impl Send for Library {}
75 
76 // That being said... this section in the volume 2 of POSIX.1-2008 states:
77 //
78 // > All functions defined by this volume of POSIX.1-2008 shall be thread-safe, except that the
79 // > following functions need not be thread-safe.
80 //
81 // With notable absence of any dl* function other than dlerror in the list. By “this volume”
82 // I suppose they refer precisely to the “volume 2”. dl* family of functions are specified
83 // by this same volume, so the conclusion is indeed that dl* functions are required by POSIX
84 // to be thread-safe. Great!
85 //
86 // See for more details:
87 //
88 //  * https://github.com/nagisa/rust_libloading/pull/17
89 //  * http://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_09_01
90 unsafe impl Sync for Library {}
91 
92 impl Library {
93     /// Find and load a shared library (module).
94     ///
95     /// Locations where library is searched for is platform specific and can’t be adjusted
96     /// portably.
97     ///
98     /// Corresponds to `dlopen(filename, RTLD_NOW)`.
99     #[inline]
new<P: AsRef<OsStr>>(filename: P) -> ::Result<Library>100     pub fn new<P: AsRef<OsStr>>(filename: P) -> ::Result<Library> {
101         Library::open(Some(filename), RTLD_NOW)
102     }
103 
104     /// Load the dynamic libraries linked into main program.
105     ///
106     /// This allows retrieving symbols from any **dynamic** library linked into the program,
107     /// without specifying the exact library.
108     ///
109     /// Corresponds to `dlopen(NULL, RTLD_NOW)`.
110     #[inline]
this() -> Library111     pub fn this() -> Library {
112         Library::open(None::<&OsStr>, RTLD_NOW).unwrap()
113     }
114 
115     /// Find and load a shared library (module).
116     ///
117     /// Locations where library is searched for is platform specific and can’t be adjusted
118     /// portably.
119     ///
120     /// If the `filename` is None, null pointer is passed to `dlopen`.
121     ///
122     /// Corresponds to `dlopen(filename, flags)`.
open<P>(filename: Option<P>, flags: raw::c_int) -> ::Result<Library> where P: AsRef<OsStr>123     pub fn open<P>(filename: Option<P>, flags: raw::c_int) -> ::Result<Library>
124     where P: AsRef<OsStr> {
125         let filename = match filename {
126             None => None,
127             Some(ref f) => Some(try!(cstr_cow_from_bytes(f.as_ref().as_bytes()))),
128         };
129         with_dlerror(move || {
130             let result = unsafe {
131                 let r = dlopen(match filename {
132                     None => ptr::null(),
133                     Some(ref f) => f.as_ptr()
134                 }, flags);
135                 // ensure filename lives until dlopen completes
136                 drop(filename);
137                 r
138             };
139             if result.is_null() {
140                 None
141             } else {
142                 Some(Library {
143                     handle: result
144                 })
145             }
146         }).map_err(|e| e.unwrap_or_else(||
147             panic!("dlopen failed but dlerror did not report anything")
148         ))
149     }
150 
151     /// Get a pointer to function or static variable by symbol name.
152     ///
153     /// The `symbol` may not contain any null bytes, with an exception of last byte. A null
154     /// terminated `symbol` may avoid a string allocation in some cases.
155     ///
156     /// Symbol is interpreted as-is; no mangling is done. This means that symbols like `x::y` are
157     /// most likely invalid.
158     ///
159     /// ## Unsafety
160     ///
161     /// Pointer to a value of arbitrary type is returned. Using a value with wrong type is
162     /// undefined.
163     ///
164     /// ## Platform-specific behaviour
165     ///
166     /// OS X uses some sort of lazy initialization scheme, which makes loading TLS variables
167     /// impossible. Using a TLS variable loaded this way on OS X is undefined behaviour.
get<T>(&self, symbol: &[u8]) -> ::Result<Symbol<T>>168     pub unsafe fn get<T>(&self, symbol: &[u8]) -> ::Result<Symbol<T>> {
169         ensure_compatible_types::<T, *mut raw::c_void>();
170         let symbol = try!(cstr_cow_from_bytes(symbol));
171         // `dlsym` may return nullptr in two cases: when a symbol genuinely points to a null
172         // pointer or the symbol cannot be found. In order to detect this case a double dlerror
173         // pattern must be used, which is, sadly, a little bit racy.
174         //
175         // We try to leave as little space as possible for this to occur, but we can’t exactly
176         // fully prevent it.
177         match with_dlerror(|| {
178             dlerror();
179             let symbol = dlsym(self.handle, symbol.as_ptr());
180             if symbol.is_null() {
181                 None
182             } else {
183                 Some(Symbol {
184                     pointer: symbol,
185                     pd: marker::PhantomData
186                 })
187             }
188         }) {
189             Err(None) => Ok(Symbol {
190                 pointer: ptr::null_mut(),
191                 pd: marker::PhantomData
192             }),
193             Err(Some(e)) => Err(e),
194             Ok(x) => Ok(x)
195         }
196     }
197 
198     /// Convert the `Library` to a raw handle.
199     ///
200     /// The handle returned by this function shall be usable with APIs which accept handles
201     /// as returned by `dlopen`.
into_raw(self) -> *mut raw::c_void202     pub fn into_raw(self) -> *mut raw::c_void {
203         let handle = self.handle;
204         mem::forget(self);
205         handle
206     }
207 
208     /// Convert a raw handle returned by `dlopen`-family of calls to a `Library`.
209     ///
210     /// ## Unsafety
211     ///
212     /// The pointer shall be a result of a successful call of the `dlopen`-family of functions or a
213     /// pointer previously returned by `Library::into_raw` call. It must be valid to call `dlclose`
214     /// with this pointer as an argument.
from_raw(handle: *mut raw::c_void) -> Library215     pub unsafe fn from_raw(handle: *mut raw::c_void) -> Library {
216         Library {
217             handle: handle
218         }
219     }
220 }
221 
222 impl Drop for Library {
drop(&mut self)223     fn drop(&mut self) {
224         with_dlerror(|| if unsafe { dlclose(self.handle) } == 0 {
225             Some(())
226         } else {
227             None
228         }).unwrap();
229     }
230 }
231 
232 impl fmt::Debug for Library {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result233     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
234         f.write_str(&format!("Library@{:p}", self.handle))
235     }
236 }
237 
238 /// Symbol from a library.
239 ///
240 /// A major difference compared to the cross-platform `Symbol` is that this does not ensure the
241 /// `Symbol` does not outlive `Library` it comes from.
242 pub struct Symbol<T> {
243     pointer: *mut raw::c_void,
244     pd: marker::PhantomData<T>
245 }
246 
247 impl<T> Symbol<T> {
248     /// Convert the loaded Symbol into a raw pointer.
into_raw(self) -> *mut raw::c_void249     pub fn into_raw(self) -> *mut raw::c_void {
250         let pointer = self.pointer;
251         mem::forget(self);
252         pointer
253     }
254 }
255 
256 impl<T> Symbol<Option<T>> {
257     /// Lift Option out of the symbol.
lift_option(self) -> Option<Symbol<T>>258     pub fn lift_option(self) -> Option<Symbol<T>> {
259         if self.pointer.is_null() {
260             None
261         } else {
262             Some(Symbol {
263                 pointer: self.pointer,
264                 pd: marker::PhantomData,
265             })
266         }
267     }
268 }
269 
270 unsafe impl<T: Send> Send for Symbol<T> {}
271 unsafe impl<T: Sync> Sync for Symbol<T> {}
272 
273 impl<T> Clone for Symbol<T> {
clone(&self) -> Symbol<T>274     fn clone(&self) -> Symbol<T> {
275         Symbol { ..*self }
276     }
277 }
278 
279 impl<T> ::std::ops::Deref for Symbol<T> {
280     type Target = T;
deref(&self) -> &T281     fn deref(&self) -> &T {
282         unsafe {
283             // Additional reference level for a dereference on `deref` return value.
284             mem::transmute(&self.pointer)
285         }
286     }
287 }
288 
289 impl<T> fmt::Debug for Symbol<T> {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result290     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
291         unsafe {
292             let mut info: DlInfo = mem::uninitialized();
293             if dladdr(self.pointer, &mut info) != 0 {
294                 if info.dli_sname.is_null() {
295                     f.write_str(&format!("Symbol@{:p} from {:?}",
296                                          self.pointer,
297                                          CStr::from_ptr(info.dli_fname)))
298                 } else {
299                     f.write_str(&format!("Symbol {:?}@{:p} from {:?}",
300                                          CStr::from_ptr(info.dli_sname), self.pointer,
301                                          CStr::from_ptr(info.dli_fname)))
302                 }
303             } else {
304                 f.write_str(&format!("Symbol@{:p}", self.pointer))
305             }
306         }
307     }
308 }
309 
310 // Platform specific things
311 
312 extern {
dlopen(filename: *const raw::c_char, flags: raw::c_int) -> *mut raw::c_void313     fn dlopen(filename: *const raw::c_char, flags: raw::c_int) -> *mut raw::c_void;
dlclose(handle: *mut raw::c_void) -> raw::c_int314     fn dlclose(handle: *mut raw::c_void) -> raw::c_int;
dlsym(handle: *mut raw::c_void, symbol: *const raw::c_char) -> *mut raw::c_void315     fn dlsym(handle: *mut raw::c_void, symbol: *const raw::c_char) -> *mut raw::c_void;
dlerror() -> *mut raw::c_char316     fn dlerror() -> *mut raw::c_char;
dladdr(addr: *mut raw::c_void, info: *mut DlInfo) -> raw::c_int317     fn dladdr(addr: *mut raw::c_void, info: *mut DlInfo) -> raw::c_int;
318 }
319 
320 #[cfg(not(target_os="android"))]
321 const RTLD_NOW: raw::c_int = 2;
322 #[cfg(target_os="android")]
323 const RTLD_NOW: raw::c_int = 0;
324 
325 #[repr(C)]
326 struct DlInfo {
327   dli_fname: *const raw::c_char,
328   dli_fbase: *mut raw::c_void,
329   dli_sname: *const raw::c_char,
330   dli_saddr: *mut raw::c_void
331 }
332 
333 #[test]
this()334 fn this() {
335     Library::this();
336 }
337