From a426b6fc3de5124ad80f20ad2cacbaae8a95ed6c Mon Sep 17 00:00:00 2001 From: Dominik Maier Date: Mon, 21 Aug 2023 13:35:59 +0200 Subject: [PATCH] Clippy for pthread_hook (#1435) * Clippy * doctest --- libafl_frida/src/pthread_hook.rs | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/libafl_frida/src/pthread_hook.rs b/libafl_frida/src/pthread_hook.rs index 337110dcee..de1e4ca421 100644 --- a/libafl_frida/src/pthread_hook.rs +++ b/libafl_frida/src/pthread_hook.rs @@ -20,7 +20,9 @@ type pthread_introspection_hook_t = extern "C" fn( ); extern "C" { - fn pthread_introspection_hook_install(hook: *const libc::c_void) -> *const libc::c_void; + fn pthread_introspection_hook_install( + hook: *const pthread_introspection_hook_t, + ) -> *const pthread_introspection_hook_t; } struct PreviousHook(*const pthread_introspection_hook_t); @@ -57,7 +59,7 @@ impl PreviousHook { } unsafe { self.0 = std::ptr::null(); - pthread_introspection_hook_install(inner as *const libc::c_void); + pthread_introspection_hook_install(inner); } } } @@ -66,7 +68,7 @@ impl PreviousHook { // Mark it as sync. unsafe impl Sync for PreviousHook {} -#[allow(non_upper_case_globals)] +// TODO: This could use a RwLock as well static mut PREVIOUS_HOOK: PreviousHook = PreviousHook(std::ptr::null()); static CURRENT_HOOK: RwLock> = RwLock::new(None); @@ -131,9 +133,11 @@ impl From for libc::c_uint { ///# use libafl_frida::pthread_hook; ///# use std::time::Duration; ///# use std::thread; -/// pthread_hook::install(|event, pthread, addr, size| { +/// unsafe { +/// pthread_hook::install(|event, pthread, addr, size| { /// log::trace!("thread id=0x{:x} event={:?} addr={:?} size={:x}", pthread, event, addr, size); -/// }); +/// }); +/// }; ///# thread::spawn(|| { ///# thread::sleep(Duration::from_millis(1)); ///# }); @@ -156,12 +160,10 @@ where let mut new_hook = CURRENT_HOOK.write().unwrap(); *new_hook = Some(Box::new(hook)); - let prev = unsafe { - pthread_introspection_hook_install(pthread_introspection_hook as *const libc::c_void) - }; + let prev = unsafe { pthread_introspection_hook_install(pthread_introspection_hook as _) }; // Allow because we're sure this isn't from a different code generation unit. - if !(prev).is_null() && prev != pthread_introspection_hook as *const libc::c_void { + if !(prev).is_null() && prev != pthread_introspection_hook as _ { unsafe { PREVIOUS_HOOK.set(prev as *const pthread_introspection_hook_t); } @@ -174,7 +176,7 @@ where ///# use libafl_frida::pthread_hook; ///# use std::time::Duration; ///# use std::thread; -/// pthread_hook::reset(); +/// unsafe { pthread_hook::reset() }; /// ``` /// /// # Safety @@ -220,7 +222,7 @@ mod test { let mut triggered = inner_triggered.lock().unwrap(); *triggered = true; } - }) + }); }; thread::spawn(|| { @@ -244,7 +246,7 @@ mod test { let mut triggered = inner_triggered.lock().unwrap(); *triggered = true; } - }) + }); }; thread::spawn(|| { @@ -268,7 +270,7 @@ mod test { let mut triggered = inner_triggered.lock().unwrap(); *triggered = true; } - }) + }); }; unsafe { super::reset() };