Skip to content

Commit

Permalink
Try weak symbols for all linux syscall! wrappers
Browse files Browse the repository at this point in the history
  • Loading branch information
cuviper committed Nov 16, 2020
1 parent 7a15f02 commit a035626
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 17 deletions.
2 changes: 1 addition & 1 deletion library/std/src/sys/unix/rand.rs
Expand Up @@ -29,7 +29,7 @@ mod imp {
// A weak symbol allows interposition, e.g. for perf measurements that want to
// disable randomness for consistency. Otherwise, we'll try a raw syscall.
// (`getrandom` was added in glibc 2.25, musl 1.1.20, android API level 28)
weak_syscall! {
syscall! {
fn getrandom(
buffer: *mut libc::c_void,
length: libc::size_t,
Expand Down
23 changes: 7 additions & 16 deletions library/std/src/sys/unix/weak.rs
Expand Up @@ -92,26 +92,17 @@ macro_rules! syscall {
// (not paths).
use libc::*;

syscall(
concat_idents!(SYS_, $name),
$($arg_name as c_long),*
) as $ret
}
)
}

/// Use a weak symbol from libc when possible, allowing `LD_PRELOAD` interposition,
/// but if it's not found just use a raw syscall.
#[cfg(any(target_os = "linux", target_os = "android"))]
macro_rules! weak_syscall {
(fn $name:ident($($arg_name:ident: $t:ty),*) -> $ret:ty) => (
unsafe fn $name($($arg_name:$t),*) -> $ret {
weak! { fn $name($($t),*) -> $ret }

// Use a weak symbol from libc when possible, allowing `LD_PRELOAD`
// interposition, but if it's not found just use a raw syscall.
if let Some(fun) = $name.get() {
fun($($arg_name),*)
} else {
syscall! { fn $name($($arg_name:$t),*) -> $ret }
$name($($arg_name),*)
syscall(
concat_idents!(SYS_, $name),
$($arg_name as c_long),*
) as $ret
}
}
)
Expand Down

0 comments on commit a035626

Please sign in to comment.