Skip to content
Open
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
16 changes: 6 additions & 10 deletions src/ld_preload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,9 @@ pub static INITIALIZE_CTOR: extern "C" fn() = ::initialize;
#[macro_export]
macro_rules! hook {
(unsafe fn $real_fn:ident ( $($v:ident : $t:ty),* ) -> $r:ty => $hook_fn:ident $body:block) => {
#[allow(non_camel_case_types)]
pub struct $real_fn {__private_field: ()}
#[allow(non_upper_case_globals)]
static $real_fn: $real_fn = $real_fn {__private_field: ()};

impl $real_fn {
fn get(&self) -> unsafe extern fn ( $($v : $t),* ) -> $r {
mod $real_fn {
use super::*; //required for parameter types
pub fn get_dlsym_next() -> unsafe extern fn ( $($v : $t),* ) -> $r {
use ::std::sync::{Once, ONCE_INIT};

static mut REAL: *const u8 = 0 as *const u8;
Expand All @@ -46,10 +42,10 @@ macro_rules! hook {
#[no_mangle]
pub unsafe extern fn $real_fn ( $($v : $t),* ) -> $r {
if $crate::initialized() {
::std::panic::catch_unwind(|| $hook_fn ( $($v),* )).ok()
::std::panic::catch_unwind(|| super::$hook_fn ( $($v),* )).ok()
} else {
None
}.unwrap_or_else(|| $real_fn.get() ( $($v),* ))
}.unwrap_or_else(|| get_dlsym_next() ( $($v),* ))
}
}

Expand All @@ -66,6 +62,6 @@ macro_rules! hook {
#[macro_export]
macro_rules! real {
($real_fn:ident) => {
$real_fn.get()
$real_fn::get_dlsym_next()
};
}