Skip to content

Hackily fix thread safety issues in dynamic_lib #9713

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 5, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions src/libstd/unstable/dynamic_lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

Dynamic library facilities.

A simple wrapper over the platforms dynamic library facilities
A simple wrapper over the platform's dynamic library facilities

*/
use c_str::ToCStr;
Expand Down Expand Up @@ -80,7 +80,6 @@ impl DynamicLibrary {
}
}


#[cfg(test)]
mod test {
use super::*;
Expand All @@ -90,8 +89,7 @@ mod test {
use libc;

#[test]
// #[ignore(cfg(windows))] // FIXME #8818
#[ignore] // FIXME #9137 this library isn't thread-safe
#[ignore(cfg(windows))] // FIXME #8818
fn test_loading_cosine() {
// The math library does not need to be loaded since it is already
// statically linked in
Expand All @@ -100,8 +98,6 @@ mod test {
Ok(libm) => libm
};

// Unfortunately due to issue #6194 it is not possible to call
// this as a C function
let cosine: extern fn(libc::c_double) -> libc::c_double = unsafe {
match libm.symbol("cos") {
Err(error) => fail2!("Could not load function cos: {}", error),
Expand All @@ -114,15 +110,14 @@ mod test {
let result = cosine(argument);
if result != expected_result {
fail2!("cos({:?}) != {:?} but equaled {:?} instead", argument,
expected_result, result)
expected_result, result)
}
}

#[test]
#[cfg(target_os = "linux")]
#[cfg(target_os = "macos")]
#[cfg(target_os = "freebsd")]
#[ignore] // FIXME #9137 this library isn't thread-safe
fn test_errors_do_not_crash() {
// Open /dev/null as a library to get an error, and make sure
// that only causes an error, and not a crash.
Expand Down Expand Up @@ -164,17 +159,25 @@ pub mod dl {
#[fixed_stack_segment]; #[inline(never)];

unsafe {
// dlerror isn't thread safe, so we need to lock around this entire
// sequence. `atomically` asserts that we don't do anything that
// would cause this task to be descheduled, which could deadlock
// the scheduler if it happens while the lock is held.
// FIXME #9105 use a Rust mutex instead of C++ mutexes.
do atomically {
rust_take_dlerror_lock();
let _old_error = dlerror();

let result = f();

let last_error = dlerror();
if ptr::null() == last_error {
let ret = if ptr::null() == last_error {
Ok(result)
} else {
Err(str::raw::from_c_str(last_error))
}
};
rust_drop_dlerror_lock();
ret
}
}
}
Expand All @@ -197,6 +200,11 @@ pub mod dl {
Local = 0,
}

extern {
fn rust_take_dlerror_lock();
fn rust_drop_dlerror_lock();
}

#[link_name = "dl"]
extern {
fn dlopen(filename: *libc::c_char, flag: libc::c_int) -> *libc::c_void;
Expand Down Expand Up @@ -246,6 +254,7 @@ pub mod dl {
}
}
}

pub unsafe fn symbol(handle: *libc::c_void, symbol: *libc::c_char) -> *libc::c_void {
#[fixed_stack_segment]; #[inline(never)];
GetProcAddress(handle, symbol)
Expand Down
12 changes: 12 additions & 0 deletions src/rt/rust_builtin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,18 @@ rust_drop_linenoise_lock() {
linenoise_lock.unlock();
}

static lock_and_signal dlerror_lock;

extern "C" CDECL void
rust_take_dlerror_lock() {
dlerror_lock.lock();
}

extern "C" CDECL void
rust_drop_dlerror_lock() {
dlerror_lock.unlock();
}

extern "C" CDECL unsigned int
rust_valgrind_stack_register(void *start, void *end) {
return VALGRIND_STACK_REGISTER(start, end);
Expand Down
2 changes: 2 additions & 0 deletions src/rt/rustrt.def.in
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,5 @@ sd_markdown_render
sd_markdown_free
bufrelease
bufnew
rust_take_dlerror_lock
rust_drop_dlerror_lock