2015-01-29 01:19:28 -06:00
|
|
|
// Copyright 2013-2015 The Rust Project Developers. See the COPYRIGHT
|
2013-06-09 01:29:32 -05:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2014-11-25 20:17:11 -06:00
|
|
|
//! Dynamic library facilities.
|
|
|
|
//!
|
|
|
|
//! A simple wrapper over the platform's dynamic library facilities
|
2014-04-29 13:38:51 -05:00
|
|
|
|
2016-03-07 17:42:29 -06:00
|
|
|
use std::env;
|
|
|
|
use std::ffi::{CString, OsString};
|
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
|
librustc: Make `Copy` opt-in.
This change makes the compiler no longer infer whether types (structures
and enumerations) implement the `Copy` trait (and thus are implicitly
copyable). Rather, you must implement `Copy` yourself via `impl Copy for
MyType {}`.
A new warning has been added, `missing_copy_implementations`, to warn
you if a non-generic public type has been added that could have
implemented `Copy` but didn't.
For convenience, you may *temporarily* opt out of this behavior by using
`#![feature(opt_out_copy)]`. Note though that this feature gate will never be
accepted and will be removed by the time that 1.0 is released, so you should
transition your code away from using it.
This breaks code like:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
Change this code to:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
impl Copy for Point2D {}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
This is the backwards-incompatible part of #13231.
Part of RFC #3.
[breaking-change]
2014-12-05 19:01:33 -06:00
|
|
|
pub struct DynamicLibrary {
|
|
|
|
handle: *mut u8
|
|
|
|
}
|
2013-06-09 01:29:32 -05:00
|
|
|
|
|
|
|
impl Drop for DynamicLibrary {
|
2013-09-16 20:18:07 -05:00
|
|
|
fn drop(&mut self) {
|
2016-03-07 17:42:29 -06:00
|
|
|
unsafe {
|
|
|
|
dl::close(self.handle)
|
2013-06-09 01:29:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DynamicLibrary {
|
|
|
|
/// Lazily open a dynamic library. When passed None it gives a
|
|
|
|
/// handle to the calling process
|
2014-11-25 15:28:35 -06:00
|
|
|
pub fn open(filename: Option<&Path>) -> Result<DynamicLibrary, String> {
|
2015-03-23 12:42:48 -05:00
|
|
|
let maybe_library = dl::open(filename.map(|path| path.as_os_str()));
|
2015-01-10 07:53:22 -06:00
|
|
|
|
|
|
|
// The dynamic library must not be constructed if there is
|
|
|
|
// an error opening the library so the destructor does not
|
|
|
|
// run.
|
|
|
|
match maybe_library {
|
|
|
|
Err(err) => Err(err),
|
|
|
|
Ok(handle) => Ok(DynamicLibrary { handle: handle })
|
2013-06-09 01:29:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-15 12:28:46 -05:00
|
|
|
/// Prepends a path to this process's search path for dynamic libraries
|
|
|
|
pub fn prepend_search_path(path: &Path) {
|
|
|
|
let mut search_path = DynamicLibrary::search_path();
|
2015-03-23 12:42:48 -05:00
|
|
|
search_path.insert(0, path.to_path_buf());
|
|
|
|
env::set_var(DynamicLibrary::envvar(), &DynamicLibrary::create_path(&search_path));
|
2014-05-15 12:28:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// From a slice of paths, create a new vector which is suitable to be an
|
|
|
|
/// environment variable for this platforms dylib search path.
|
2015-03-23 12:42:48 -05:00
|
|
|
pub fn create_path(path: &[PathBuf]) -> OsString {
|
|
|
|
let mut newvar = OsString::new();
|
2014-05-15 12:28:46 -05:00
|
|
|
for (i, path) in path.iter().enumerate() {
|
|
|
|
if i > 0 { newvar.push(DynamicLibrary::separator()); }
|
2015-03-23 12:42:48 -05:00
|
|
|
newvar.push(path);
|
2014-05-15 12:28:46 -05:00
|
|
|
}
|
|
|
|
return newvar;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the environment variable for this process's dynamic library
|
|
|
|
/// search path
|
|
|
|
pub fn envvar() -> &'static str {
|
|
|
|
if cfg!(windows) {
|
|
|
|
"PATH"
|
2014-04-29 13:38:51 -05:00
|
|
|
} else if cfg!(target_os = "macos") {
|
2014-05-15 12:28:46 -05:00
|
|
|
"DYLD_LIBRARY_PATH"
|
2014-04-29 13:38:51 -05:00
|
|
|
} else {
|
2014-05-15 12:28:46 -05:00
|
|
|
"LD_LIBRARY_PATH"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-23 12:42:48 -05:00
|
|
|
fn separator() -> &'static str {
|
|
|
|
if cfg!(windows) { ";" } else { ":" }
|
2014-05-15 12:28:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the current search path for dynamic libraries being used by this
|
|
|
|
/// process
|
2015-03-23 12:42:48 -05:00
|
|
|
pub fn search_path() -> Vec<PathBuf> {
|
2015-02-11 13:47:53 -06:00
|
|
|
match env::var_os(DynamicLibrary::envvar()) {
|
2015-03-23 12:42:48 -05:00
|
|
|
Some(var) => env::split_paths(&var).collect(),
|
2015-01-27 14:20:58 -06:00
|
|
|
None => Vec::new(),
|
2014-05-08 08:33:22 -05:00
|
|
|
}
|
2014-04-29 13:38:51 -05:00
|
|
|
}
|
|
|
|
|
2015-04-13 09:21:32 -05:00
|
|
|
/// Accesses the value at the symbol of the dynamic library.
|
2014-06-25 14:47:34 -05:00
|
|
|
pub unsafe fn symbol<T>(&self, symbol: &str) -> Result<*mut T, String> {
|
2013-12-10 01:16:18 -06:00
|
|
|
// This function should have a lifetime constraint of 'a on
|
2013-06-09 01:29:32 -05:00
|
|
|
// T but that feature is still unimplemented
|
|
|
|
|
2015-02-18 00:47:40 -06:00
|
|
|
let raw_string = CString::new(symbol).unwrap();
|
2016-03-07 17:42:29 -06:00
|
|
|
let maybe_symbol_value = dl::symbol(self.handle, raw_string.as_ptr());
|
2013-06-09 01:29:32 -05:00
|
|
|
|
2013-08-01 16:58:37 -05:00
|
|
|
// The value must not be constructed if there is an error so
|
|
|
|
// the destructor does not run.
|
|
|
|
match maybe_symbol_value {
|
|
|
|
Err(err) => Err(err),
|
2015-10-17 19:15:26 -05:00
|
|
|
Ok(symbol_value) => Ok(symbol_value as *mut T)
|
2013-06-09 01:29:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-07 17:42:29 -06:00
|
|
|
#[cfg(test)]
|
2015-04-24 10:30:41 -05:00
|
|
|
mod tests {
|
2013-08-01 16:58:37 -05:00
|
|
|
use super::*;
|
|
|
|
use libc;
|
2016-03-07 17:42:29 -06:00
|
|
|
use std::mem;
|
2013-08-01 16:58:37 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_loading_cosine() {
|
2016-03-07 17:42:29 -06:00
|
|
|
if cfg!(windows) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-08-01 16:58:37 -05:00
|
|
|
// The math library does not need to be loaded since it is already
|
|
|
|
// statically linked in
|
2015-03-17 15:33:26 -05:00
|
|
|
let libm = match DynamicLibrary::open(None) {
|
2014-10-09 14:17:22 -05:00
|
|
|
Err(error) => panic!("Could not load self as module: {}", error),
|
2013-08-01 16:58:37 -05:00
|
|
|
Ok(libm) => libm
|
|
|
|
};
|
|
|
|
|
|
|
|
let cosine: extern fn(libc::c_double) -> libc::c_double = unsafe {
|
|
|
|
match libm.symbol("cos") {
|
2014-10-09 14:17:22 -05:00
|
|
|
Err(error) => panic!("Could not load function cos: {}", error),
|
2014-06-25 14:47:34 -05:00
|
|
|
Ok(cosine) => mem::transmute::<*mut u8, _>(cosine)
|
2013-08-01 16:58:37 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let argument = 0.0;
|
|
|
|
let expected_result = 1.0;
|
|
|
|
let result = cosine(argument);
|
|
|
|
if result != expected_result {
|
2014-10-09 14:17:22 -05:00
|
|
|
panic!("cos({}) != {} but equaled {} instead", argument,
|
2013-10-03 23:34:35 -05:00
|
|
|
expected_result, result)
|
2013-08-01 16:58:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_errors_do_not_crash() {
|
2016-03-07 17:42:29 -06:00
|
|
|
use std::path::Path;
|
|
|
|
|
|
|
|
if !cfg!(unix) {
|
|
|
|
return
|
|
|
|
}
|
2016-01-23 01:49:57 -06:00
|
|
|
|
2013-08-01 16:58:37 -05:00
|
|
|
// Open /dev/null as a library to get an error, and make sure
|
|
|
|
// that only causes an error, and not a crash.
|
2014-05-05 16:53:57 -05:00
|
|
|
let path = Path::new("/dev/null");
|
2013-08-01 16:58:37 -05:00
|
|
|
match DynamicLibrary::open(Some(&path)) {
|
|
|
|
Err(_) => {}
|
2014-10-09 14:17:22 -05:00
|
|
|
Ok(_) => panic!("Successfully opened the empty library.")
|
2013-06-09 01:29:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-07 17:42:29 -06:00
|
|
|
#[cfg(unix)]
|
2015-01-10 07:53:22 -06:00
|
|
|
mod dl {
|
2013-06-09 01:29:32 -05:00
|
|
|
use libc;
|
2016-03-07 17:42:29 -06:00
|
|
|
use std::ffi::{CStr, OsStr, CString};
|
|
|
|
use std::os::unix::prelude::*;
|
|
|
|
use std::ptr;
|
|
|
|
use std::str;
|
2013-06-09 01:29:32 -05:00
|
|
|
|
2015-03-23 12:42:48 -05:00
|
|
|
pub fn open(filename: Option<&OsStr>) -> Result<*mut u8, String> {
|
2015-01-10 07:53:22 -06:00
|
|
|
check_for_errors_in(|| {
|
|
|
|
unsafe {
|
|
|
|
match filename {
|
|
|
|
Some(filename) => open_external(filename),
|
|
|
|
None => open_internal(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const LAZY: libc::c_int = 1;
|
|
|
|
|
2015-03-23 12:42:48 -05:00
|
|
|
unsafe fn open_external(filename: &OsStr) -> *mut u8 {
|
2016-03-07 17:42:29 -06:00
|
|
|
let s = CString::new(filename.as_bytes()).unwrap();
|
2015-11-02 18:23:22 -06:00
|
|
|
libc::dlopen(s.as_ptr(), LAZY) as *mut u8
|
2013-06-15 03:10:49 -05:00
|
|
|
}
|
|
|
|
|
2015-01-10 07:53:22 -06:00
|
|
|
unsafe fn open_internal() -> *mut u8 {
|
2015-11-02 18:23:22 -06:00
|
|
|
libc::dlopen(ptr::null(), LAZY) as *mut u8
|
2013-06-09 01:29:32 -05:00
|
|
|
}
|
|
|
|
|
2014-12-07 13:15:25 -06:00
|
|
|
pub fn check_for_errors_in<T, F>(f: F) -> Result<T, String> where
|
|
|
|
F: FnOnce() -> T,
|
|
|
|
{
|
2016-03-07 17:42:29 -06:00
|
|
|
use std::sync::StaticMutex;
|
2015-05-27 03:18:36 -05:00
|
|
|
static LOCK: StaticMutex = StaticMutex::new();
|
2013-06-09 01:29:32 -05:00
|
|
|
unsafe {
|
2013-10-03 23:34:35 -05:00
|
|
|
// dlerror isn't thread safe, so we need to lock around this entire
|
2013-12-12 19:27:37 -06:00
|
|
|
// sequence
|
2014-10-10 23:59:10 -05:00
|
|
|
let _guard = LOCK.lock();
|
2015-11-02 18:23:22 -06:00
|
|
|
let _old_error = libc::dlerror();
|
2013-12-05 19:34:37 -06:00
|
|
|
|
|
|
|
let result = f();
|
|
|
|
|
2015-11-02 18:23:22 -06:00
|
|
|
let last_error = libc::dlerror() as *const _;
|
2013-12-05 19:34:37 -06:00
|
|
|
let ret = if ptr::null() == last_error {
|
|
|
|
Ok(result)
|
|
|
|
} else {
|
2015-02-18 00:47:40 -06:00
|
|
|
let s = CStr::from_ptr(last_error).to_bytes();
|
2015-09-07 17:36:29 -05:00
|
|
|
Err(str::from_utf8(s).unwrap().to_owned())
|
2013-12-05 19:34:37 -06:00
|
|
|
};
|
2014-02-13 00:17:50 -06:00
|
|
|
|
2013-12-05 19:34:37 -06:00
|
|
|
ret
|
2013-06-09 01:29:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-25 14:47:34 -05:00
|
|
|
pub unsafe fn symbol(handle: *mut u8,
|
2016-03-07 17:42:29 -06:00
|
|
|
symbol: *const libc::c_char)
|
|
|
|
-> Result<*mut u8, String> {
|
|
|
|
check_for_errors_in(|| {
|
|
|
|
libc::dlsym(handle as *mut libc::c_void, symbol) as *mut u8
|
|
|
|
})
|
2013-06-09 01:29:32 -05:00
|
|
|
}
|
2014-06-25 14:47:34 -05:00
|
|
|
pub unsafe fn close(handle: *mut u8) {
|
2015-11-02 18:23:22 -06:00
|
|
|
libc::dlclose(handle as *mut libc::c_void); ()
|
2015-01-29 01:19:28 -06:00
|
|
|
}
|
2013-06-09 01:29:32 -05:00
|
|
|
}
|
|
|
|
|
2016-03-07 17:42:29 -06:00
|
|
|
#[cfg(windows)]
|
2015-01-10 07:53:22 -06:00
|
|
|
mod dl {
|
2016-03-07 17:42:29 -06:00
|
|
|
use std::ffi::OsStr;
|
|
|
|
use std::io;
|
|
|
|
use std::os::windows::prelude::*;
|
|
|
|
use std::ptr;
|
|
|
|
|
|
|
|
use libc::{c_uint, c_void, c_char};
|
|
|
|
|
|
|
|
type DWORD = u32;
|
|
|
|
type HMODULE = *mut u8;
|
|
|
|
type BOOL = i32;
|
|
|
|
type LPCWSTR = *const u16;
|
|
|
|
type LPCSTR = *const i8;
|
|
|
|
|
|
|
|
extern "system" {
|
|
|
|
fn SetThreadErrorMode(dwNewMode: DWORD,
|
|
|
|
lpOldMode: *mut DWORD) -> c_uint;
|
|
|
|
fn LoadLibraryW(name: LPCWSTR) -> HMODULE;
|
|
|
|
fn GetModuleHandleExW(dwFlags: DWORD,
|
|
|
|
name: LPCWSTR,
|
|
|
|
handle: *mut HMODULE) -> BOOL;
|
|
|
|
fn GetProcAddress(handle: HMODULE,
|
|
|
|
name: LPCSTR) -> *mut c_void;
|
|
|
|
fn FreeLibrary(handle: HMODULE) -> BOOL;
|
|
|
|
}
|
2015-01-10 07:53:22 -06:00
|
|
|
|
2015-03-23 12:42:48 -05:00
|
|
|
pub fn open(filename: Option<&OsStr>) -> Result<*mut u8, String> {
|
2015-01-10 07:53:22 -06:00
|
|
|
// disable "dll load failed" error dialog.
|
|
|
|
let prev_error_mode = unsafe {
|
|
|
|
// SEM_FAILCRITICALERRORS 0x01
|
|
|
|
let new_error_mode = 1;
|
|
|
|
let mut prev_error_mode = 0;
|
2016-03-07 17:42:29 -06:00
|
|
|
let result = SetThreadErrorMode(new_error_mode,
|
|
|
|
&mut prev_error_mode);
|
2015-01-10 07:53:22 -06:00
|
|
|
if result == 0 {
|
2016-03-07 17:42:29 -06:00
|
|
|
return Err(io::Error::last_os_error().to_string())
|
2015-01-10 07:53:22 -06:00
|
|
|
}
|
|
|
|
prev_error_mode
|
|
|
|
};
|
2013-06-09 01:29:32 -05:00
|
|
|
|
2015-01-10 07:53:22 -06:00
|
|
|
let result = match filename {
|
|
|
|
Some(filename) => {
|
2015-03-23 12:42:48 -05:00
|
|
|
let filename_str: Vec<_> =
|
2015-06-10 11:22:20 -05:00
|
|
|
filename.encode_wide().chain(Some(0)).collect();
|
2015-01-10 07:53:22 -06:00
|
|
|
let result = unsafe {
|
2016-03-07 17:42:29 -06:00
|
|
|
LoadLibraryW(filename_str.as_ptr())
|
2015-01-10 07:53:22 -06:00
|
|
|
};
|
2016-03-07 17:42:29 -06:00
|
|
|
ptr_result(result)
|
2015-01-10 07:53:22 -06:00
|
|
|
}
|
|
|
|
None => {
|
|
|
|
let mut handle = ptr::null_mut();
|
|
|
|
let succeeded = unsafe {
|
2016-03-07 17:42:29 -06:00
|
|
|
GetModuleHandleExW(0 as DWORD, ptr::null(), &mut handle)
|
2015-01-10 07:53:22 -06:00
|
|
|
};
|
2016-03-07 17:42:29 -06:00
|
|
|
if succeeded == 0 {
|
|
|
|
Err(io::Error::last_os_error().to_string())
|
2015-01-10 07:53:22 -06:00
|
|
|
} else {
|
|
|
|
Ok(handle as *mut u8)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
unsafe {
|
2016-03-07 17:42:29 -06:00
|
|
|
SetThreadErrorMode(prev_error_mode, ptr::null_mut());
|
2015-01-10 07:53:22 -06:00
|
|
|
}
|
2013-06-15 03:10:49 -05:00
|
|
|
|
2015-01-10 07:53:22 -06:00
|
|
|
result
|
2013-06-09 01:29:32 -05:00
|
|
|
}
|
|
|
|
|
2016-03-07 17:42:29 -06:00
|
|
|
pub unsafe fn symbol(handle: *mut u8,
|
|
|
|
symbol: *const c_char)
|
|
|
|
-> Result<*mut u8, String> {
|
|
|
|
let ptr = GetProcAddress(handle as HMODULE, symbol) as *mut u8;
|
|
|
|
ptr_result(ptr)
|
2013-06-09 01:29:32 -05:00
|
|
|
}
|
2013-10-03 23:34:35 -05:00
|
|
|
|
2014-06-25 14:47:34 -05:00
|
|
|
pub unsafe fn close(handle: *mut u8) {
|
2016-03-07 17:42:29 -06:00
|
|
|
FreeLibrary(handle as HMODULE);
|
2015-10-24 20:51:34 -05:00
|
|
|
}
|
|
|
|
|
2016-03-07 17:42:29 -06:00
|
|
|
fn ptr_result<T>(ptr: *mut T) -> Result<*mut T, String> {
|
|
|
|
if ptr.is_null() {
|
|
|
|
Err(io::Error::last_os_error().to_string())
|
|
|
|
} else {
|
|
|
|
Ok(ptr)
|
|
|
|
}
|
2015-10-24 20:51:34 -05:00
|
|
|
}
|
|
|
|
}
|