1 // Copyright 2013-2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! Dynamic library facilities.
13 //! A simple wrapper over the platform's dynamic library facilities
15 #![unstable(feature = "dynamic_lib",
16 reason = "API has not been scrutinized and is highly likely to \
17 either disappear or change")]
18 #![allow(missing_docs)]
23 use ffi::{CString, OsString};
25 use path::{Path, PathBuf};
27 pub struct DynamicLibrary {
31 impl Drop for DynamicLibrary {
33 match dl::check_for_errors_in(|| {
35 dl::close(self.handle)
39 Err(str) => panic!("{}", str)
45 /// Lazily open a dynamic library. When passed None it gives a
46 /// handle to the calling process
47 pub fn open(filename: Option<&Path>) -> Result<DynamicLibrary, String> {
48 let maybe_library = dl::open(filename.map(|path| path.as_os_str()));
50 // The dynamic library must not be constructed if there is
51 // an error opening the library so the destructor does not
55 Ok(handle) => Ok(DynamicLibrary { handle: handle })
59 /// Prepends a path to this process's search path for dynamic libraries
60 pub fn prepend_search_path(path: &Path) {
61 let mut search_path = DynamicLibrary::search_path();
62 search_path.insert(0, path.to_path_buf());
63 env::set_var(DynamicLibrary::envvar(), &DynamicLibrary::create_path(&search_path));
66 /// From a slice of paths, create a new vector which is suitable to be an
67 /// environment variable for this platforms dylib search path.
68 pub fn create_path(path: &[PathBuf]) -> OsString {
69 let mut newvar = OsString::new();
70 for (i, path) in path.iter().enumerate() {
71 if i > 0 { newvar.push(DynamicLibrary::separator()); }
77 /// Returns the environment variable for this process's dynamic library
79 pub fn envvar() -> &'static str {
82 } else if cfg!(target_os = "macos") {
89 fn separator() -> &'static str {
90 if cfg!(windows) { ";" } else { ":" }
93 /// Returns the current search path for dynamic libraries being used by this
95 pub fn search_path() -> Vec<PathBuf> {
96 match env::var_os(DynamicLibrary::envvar()) {
97 Some(var) => env::split_paths(&var).collect(),
102 /// Accesses the value at the symbol of the dynamic library.
103 pub unsafe fn symbol<T>(&self, symbol: &str) -> Result<*mut T, String> {
104 // This function should have a lifetime constraint of 'a on
105 // T but that feature is still unimplemented
107 let raw_string = CString::new(symbol).unwrap();
108 let maybe_symbol_value = dl::check_for_errors_in(|| {
109 dl::symbol(self.handle, raw_string.as_ptr())
112 // The value must not be constructed if there is an error so
113 // the destructor does not run.
114 match maybe_symbol_value {
115 Err(err) => Err(err),
116 Ok(symbol_value) => Ok(mem::transmute(symbol_value))
121 #[cfg(all(test, not(target_os = "ios")))]
130 #[cfg_attr(any(windows,
131 target_os = "android", // FIXME #10379
132 target_env = "musl"), ignore)]
133 fn test_loading_cosine() {
134 // The math library does not need to be loaded since it is already
135 // statically linked in
136 let libm = match DynamicLibrary::open(None) {
137 Err(error) => panic!("Could not load self as module: {}", error),
141 let cosine: extern fn(libc::c_double) -> libc::c_double = unsafe {
142 match libm.symbol("cos") {
143 Err(error) => panic!("Could not load function cos: {}", error),
144 Ok(cosine) => mem::transmute::<*mut u8, _>(cosine)
149 let expected_result = 1.0;
150 let result = cosine(argument);
151 if result != expected_result {
152 panic!("cos({}) != {} but equaled {} instead", argument,
153 expected_result, result)
158 #[cfg(any(target_os = "linux",
160 target_os = "freebsd",
161 target_os = "dragonfly",
162 target_os = "bitrig",
163 target_os = "netbsd",
164 target_os = "openbsd"))]
165 fn test_errors_do_not_crash() {
166 // Open /dev/null as a library to get an error, and make sure
167 // that only causes an error, and not a crash.
168 let path = Path::new("/dev/null");
169 match DynamicLibrary::open(Some(&path)) {
171 Ok(_) => panic!("Successfully opened the empty library.")
176 #[cfg(any(target_os = "linux",
177 target_os = "android",
180 target_os = "freebsd",
181 target_os = "dragonfly",
182 target_os = "bitrig",
183 target_os = "netbsd",
184 target_os = "openbsd"))]
188 use ffi::{CStr, OsStr};
193 pub fn open(filename: Option<&OsStr>) -> Result<*mut u8, String> {
194 check_for_errors_in(|| {
197 Some(filename) => open_external(filename),
198 None => open_internal(),
204 const LAZY: libc::c_int = 1;
206 unsafe fn open_external(filename: &OsStr) -> *mut u8 {
207 let s = filename.to_cstring().unwrap();
208 dlopen(s.as_ptr(), LAZY) as *mut u8
211 unsafe fn open_internal() -> *mut u8 {
212 dlopen(ptr::null(), LAZY) as *mut u8
215 pub fn check_for_errors_in<T, F>(f: F) -> Result<T, String> where
218 use sync::StaticMutex;
219 static LOCK: StaticMutex = StaticMutex::new();
221 // dlerror isn't thread safe, so we need to lock around this entire
223 let _guard = LOCK.lock();
224 let _old_error = dlerror();
228 let last_error = dlerror() as *const _;
229 let ret = if ptr::null() == last_error {
232 let s = CStr::from_ptr(last_error).to_bytes();
233 Err(str::from_utf8(s).unwrap().to_string())
240 pub unsafe fn symbol(handle: *mut u8,
241 symbol: *const libc::c_char) -> *mut u8 {
242 dlsym(handle as *mut libc::c_void, symbol) as *mut u8
244 pub unsafe fn close(handle: *mut u8) {
245 dlclose(handle as *mut libc::c_void); ()
249 fn dlopen(filename: *const libc::c_char,
250 flag: libc::c_int) -> *mut libc::c_void;
251 fn dlerror() -> *mut libc::c_char;
252 fn dlsym(handle: *mut libc::c_void,
253 symbol: *const libc::c_char) -> *mut libc::c_void;
254 fn dlclose(handle: *mut libc::c_void) -> libc::c_int;
258 #[cfg(target_os = "windows")]
264 use libc::consts::os::extra::ERROR_CALL_NOT_IMPLEMENTED;
266 use os::windows::prelude::*;
268 use sys::c::SetThreadErrorMode;
270 pub fn open(filename: Option<&OsStr>) -> Result<*mut u8, String> {
271 // disable "dll load failed" error dialog.
272 let mut use_thread_mode = true;
273 let prev_error_mode = unsafe {
274 // SEM_FAILCRITICALERRORS 0x01
275 let new_error_mode = 1;
276 let mut prev_error_mode = 0;
277 // Windows >= 7 supports thread error mode.
278 let result = SetThreadErrorMode(new_error_mode, &mut prev_error_mode);
280 let err = os::errno();
281 if err as libc::c_int == ERROR_CALL_NOT_IMPLEMENTED {
282 use_thread_mode = false;
283 // SetThreadErrorMode not found. use fallback solution:
284 // SetErrorMode() Note that SetErrorMode is process-wide so
285 // this can cause race condition! However, since even
286 // Windows APIs do not care of such problem (#20650), we
287 // just assume SetErrorMode race is not a great deal.
288 prev_error_mode = SetErrorMode(new_error_mode);
298 let result = match filename {
300 let filename_str: Vec<_> =
301 filename.encode_wide().chain(Some(0)).collect();
302 let result = unsafe {
303 LoadLibraryW(filename_str.as_ptr() as *const libc::c_void)
305 // beware: Vec/String may change errno during drop!
306 // so we get error here.
307 if result == ptr::null_mut() {
308 let errno = os::errno();
309 Err(os::error_string(errno))
311 Ok(result as *mut u8)
315 let mut handle = ptr::null_mut();
316 let succeeded = unsafe {
317 GetModuleHandleExW(0 as libc::DWORD, ptr::null(), &mut handle)
319 if succeeded == libc::FALSE {
320 let errno = os::errno();
321 Err(os::error_string(errno))
323 Ok(handle as *mut u8)
330 SetThreadErrorMode(prev_error_mode, ptr::null_mut());
332 SetErrorMode(prev_error_mode);
339 pub fn check_for_errors_in<T, F>(f: F) -> Result<T, String> where
347 let error = os::errno();
351 Err(format!("Error code {}", error))
356 pub unsafe fn symbol(handle: *mut u8, symbol: *const libc::c_char) -> *mut u8 {
357 GetProcAddress(handle as *mut libc::c_void, symbol) as *mut u8
359 pub unsafe fn close(handle: *mut u8) {
360 FreeLibrary(handle as *mut libc::c_void); ()
363 #[allow(non_snake_case)]
365 fn SetLastError(error: libc::size_t);
366 fn LoadLibraryW(name: *const libc::c_void) -> *mut libc::c_void;
367 fn GetModuleHandleExW(dwFlags: libc::DWORD, name: *const u16,
368 handle: *mut *mut libc::c_void) -> libc::BOOL;
369 fn GetProcAddress(handle: *mut libc::c_void,
370 name: *const libc::c_char) -> *mut libc::c_void;
371 fn FreeLibrary(handle: *mut libc::c_void);
372 fn SetErrorMode(uMode: libc::c_uint) -> libc::c_uint;