|
| 1 | +#![feature(libc)] |
| 2 | + |
| 3 | +extern crate libc; |
| 4 | + |
| 5 | +use libc::{c_char, c_void}; |
| 6 | +use std::sync::atomic; |
| 7 | + |
| 8 | +#[link(name="dl")] |
| 9 | +extern { |
| 10 | + fn dlsym(handle: *const c_void, symbol: *const c_char) -> *const c_void; |
| 11 | +} |
| 12 | + |
| 13 | +const RTLD_NEXT: *const c_void = -1is as *const c_void; |
| 14 | + |
| 15 | +pub unsafe fn dlsym_next(symbol: &'static str) -> *const u8 { |
| 16 | + let ptr = dlsym(RTLD_NEXT, symbol.as_ptr() as *const c_char); |
| 17 | + if ptr.is_null() { |
| 18 | + panic!("redhook: Unable to find underlying function for {}", symbol); |
| 19 | + } |
| 20 | + ptr as *const u8 |
| 21 | +} |
| 22 | + |
| 23 | +/* Some Rust library functionality (e.g., jemalloc) initializes |
| 24 | + * lazily, after the hooking library has inserted itself into the call |
| 25 | + * path. If the initialization uses any hooked functions, this will lead |
| 26 | + * to an infinite loop. Work around this by running some initialization |
| 27 | + * code in a static constructor, and bypassing all hooks until it has |
| 28 | + * completed. */ |
| 29 | + |
| 30 | +static INIT_STATE: atomic::AtomicBool = atomic::ATOMIC_BOOL_INIT; |
| 31 | + |
| 32 | +pub fn initialized() -> bool { |
| 33 | + INIT_STATE.load(atomic::Ordering::SeqCst) |
| 34 | +} |
| 35 | + |
| 36 | +extern fn initialize() { |
| 37 | + Box::new(0u8); |
| 38 | + INIT_STATE.store(true, atomic::Ordering::SeqCst); |
| 39 | +} |
| 40 | + |
| 41 | +/* Rust doesn't directly expose __attribute__((constructor)), but this |
| 42 | + * is how GNU implements it. */ |
| 43 | +#[link_section=".init_array"] |
| 44 | +pub static INITIALIZE_CTOR: extern fn() = initialize; |
| 45 | + |
| 46 | +#[macro_export] |
| 47 | +macro_rules! hook { |
| 48 | + (fn $real_fn:ident ( $($v:ident : $t:ty),* ) -> $r:ty => $hook_fn:ident $body:block) => { |
| 49 | + #[allow(non_camel_case_types)] |
| 50 | + pub struct $real_fn {__private_field: ()} |
| 51 | + #[allow(non_upper_case_globals)] |
| 52 | + static $real_fn: $real_fn = $real_fn {__private_field: ()}; |
| 53 | + |
| 54 | + impl $real_fn { |
| 55 | + fn get(&self) -> unsafe extern fn ( $($v : $t),* ) -> $r { |
| 56 | + use std::sync::{Once, ONCE_INIT}; |
| 57 | + |
| 58 | + static mut REAL: *const u8 = 0 as *const u8; |
| 59 | + static mut ONCE: Once = ONCE_INIT; |
| 60 | + |
| 61 | + unsafe { |
| 62 | + ONCE.call_once(|| { |
| 63 | + REAL = $crate::dlsym_next(concat!(stringify!($real_fn), "\0")); |
| 64 | + }); |
| 65 | + std::mem::transmute(REAL) |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + #[no_mangle] |
| 70 | + pub unsafe fn $real_fn ( $($v : $t),* ) -> $r { |
| 71 | + if $crate::initialized() { |
| 72 | + $hook_fn ( $($v),* ) |
| 73 | + } else { |
| 74 | + $real_fn.get() ( $($v),* ) |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + pub fn $hook_fn ( $($v : $t),* ) -> $r { |
| 80 | + $body |
| 81 | + } |
| 82 | + }; |
| 83 | + |
| 84 | + (fn $real_fn:ident ( $($v:ident : $t:ty),* ) => $hook_fn:ident $body:block) => { |
| 85 | + hook! { fn $real_fn ( $($v : $t),* ) -> () => $hook_fn $body } |
| 86 | + }; |
| 87 | +} |
| 88 | + |
| 89 | +#[macro_export] |
| 90 | +macro_rules! real { |
| 91 | + ($real_fn:ident) => {$real_fn.get()} |
| 92 | +} |
0 commit comments