1
Fork 0

Try weak symbols for all linux syscall! wrappers

This commit is contained in:
Josh Stone 2020-11-05 14:08:42 -08:00
parent 7a15f026f2
commit a035626eb5
2 changed files with 8 additions and 17 deletions

View file

@ -29,7 +29,7 @@ mod imp {
// A weak symbol allows interposition, e.g. for perf measurements that want to // A weak symbol allows interposition, e.g. for perf measurements that want to
// disable randomness for consistency. Otherwise, we'll try a raw syscall. // 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) // (`getrandom` was added in glibc 2.25, musl 1.1.20, android API level 28)
weak_syscall! { syscall! {
fn getrandom( fn getrandom(
buffer: *mut libc::c_void, buffer: *mut libc::c_void,
length: libc::size_t, length: libc::size_t,

View file

@ -92,26 +92,17 @@ macro_rules! syscall {
// (not paths). // (not paths).
use libc::*; 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 } 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() { if let Some(fun) = $name.get() {
fun($($arg_name),*) fun($($arg_name),*)
} else { } else {
syscall! { fn $name($($arg_name:$t),*) -> $ret } syscall(
$name($($arg_name),*) concat_idents!(SYS_, $name),
$($arg_name as c_long),*
) as $ret
} }
} }
) )