From 0a995f241c3198dcb7ebdba2809749577f8d471f Mon Sep 17 00:00:00 2001 From: Dominik Maier Date: Thu, 15 Feb 2024 16:31:18 +0100 Subject: [PATCH] Cleanup Pointer Clippy Lints (#1861) * Fix pointer clippy lints * More clippy * fix build * fix --- libafl/src/corpus/mod.rs | 8 ++------ libafl/src/executors/differential.rs | 6 +++--- libafl/src/executors/hooks/timer.rs | 4 ++-- libafl/src/executors/hooks/unix.rs | 4 ++-- libafl/src/executors/inprocess.rs | 12 ++++++------ libafl/src/executors/inprocess_fork.rs | 8 ++++---- libafl/src/executors/mod.rs | 8 ++------ libafl/src/feedbacks/mod.rs | 7 ++++--- libafl/src/monitors/mod.rs | 6 ++---- libafl/src/observers/mod.rs | 10 +++++----- libafl_frida/src/asan/asan_rt.rs | 8 ++++---- libafl_frida/src/cmplog_rt.rs | 2 +- .../libafl_qemu_sys/src/x86_64_stub_bindings.rs | 4 ++-- libafl_qemu/src/executor.rs | 6 ++++-- libafl_targets/src/cmps/mod.rs | 6 ++---- utils/noaslr/libnoaslr/src/lib.rs | 2 +- utils/noaslr/noaslr/src/main.rs | 2 +- 17 files changed, 47 insertions(+), 56 deletions(-) diff --git a/libafl/src/corpus/mod.rs b/libafl/src/corpus/mod.rs index 0cfcc39c27..1dee0655ca 100644 --- a/libafl/src/corpus/mod.rs +++ b/libafl/src/corpus/mod.rs @@ -368,17 +368,13 @@ pub mod pybind { #[inline] fn current(&self) -> &Option { - let ptr = unwrap_me!(self.wrapper, c, { - core::ptr::from_ref::>(c.current()) - }); + let ptr = unwrap_me!(self.wrapper, c, { core::ptr::from_ref(c.current()) }); unsafe { ptr.as_ref().unwrap() } } #[inline] fn current_mut(&mut self) -> &mut Option { - let ptr = unwrap_me_mut!(self.wrapper, c, { - core::ptr::from_mut::>(c.current_mut()) - }); + let ptr = unwrap_me_mut!(self.wrapper, c, { core::ptr::from_mut(c.current_mut()) }); unsafe { ptr.as_mut().unwrap() } } diff --git a/libafl/src/executors/differential.rs b/libafl/src/executors/differential.rs index 745c4ed1b7..e3695a6da2 100644 --- a/libafl/src/executors/differential.rs +++ b/libafl/src/executors/differential.rs @@ -2,7 +2,7 @@ //! It wraps two executors that will be run after each other with the same input. //! In comparison to the [`crate::executors::CombinedExecutor`] it also runs the secondary executor in `run_target`. //! -use core::{cell::UnsafeCell, fmt::Debug}; +use core::{cell::UnsafeCell, fmt::Debug, ptr}; use libafl_bolts::{ownedref::OwnedMutPtr, tuples::MatchName}; use serde::{Deserialize, Serialize}; @@ -205,8 +205,8 @@ where impl ProxyObserversTuple { fn set(&mut self, primary: &A, secondary: &B) { - self.primary = OwnedMutPtr::Ptr(core::ptr::from_ref::(primary) as *mut A); - self.secondary = OwnedMutPtr::Ptr(core::ptr::from_ref::(secondary) as *mut B); + self.primary = OwnedMutPtr::Ptr(ptr::from_ref(primary) as *mut A); + self.secondary = OwnedMutPtr::Ptr(ptr::from_ref(secondary) as *mut B); } } diff --git a/libafl/src/executors/hooks/timer.rs b/libafl/src/executors/hooks/timer.rs index 2f93f2192e..6f1f6991e0 100644 --- a/libafl/src/executors/hooks/timer.rs +++ b/libafl/src/executors/hooks/timer.rs @@ -8,7 +8,7 @@ use core::{ #[cfg(target_os = "linux")] use core::{ mem::zeroed, - ptr::{addr_of, null_mut}, + ptr::{self, addr_of, null_mut}, }; #[cfg(all(unix, not(target_os = "linux")))] @@ -299,7 +299,7 @@ impl TimerStruct { let data = addr_of_mut!(GLOBAL_STATE); write_volatile( addr_of_mut!((*data).executor_ptr), - core::ptr::from_mut(self) as *mut c_void, + ptr::from_mut(self) as *mut c_void, ); if self.executions == 0 { diff --git a/libafl/src/executors/hooks/unix.rs b/libafl/src/executors/hooks/unix.rs index a47fde335e..adb0de9f20 100644 --- a/libafl/src/executors/hooks/unix.rs +++ b/libafl/src/executors/hooks/unix.rs @@ -185,8 +185,8 @@ pub mod unix_signal_handler { { #[cfg(all(target_os = "android", target_arch = "aarch64"))] let _context = _context.map(|p| { - &mut *(((p as *mut _ as *mut libc::c_void as usize) + 128) as *mut libc::c_void - as *mut ucontext_t) + &mut *(((core::ptr::from_mut(p) as *mut libc::c_void as usize) + 128) + as *mut libc::c_void as *mut ucontext_t) }); log::error!("Crashed with {signal}"); diff --git a/libafl/src/executors/inprocess.rs b/libafl/src/executors/inprocess.rs index f405118507..883a405a8f 100644 --- a/libafl/src/executors/inprocess.rs +++ b/libafl/src/executors/inprocess.rs @@ -10,7 +10,7 @@ use core::{ ffi::c_void, fmt::{self, Debug, Formatter}, marker::PhantomData, - ptr::{addr_of_mut, null, write_volatile}, + ptr::{self, addr_of_mut, null, write_volatile}, sync::atomic::{compiler_fence, Ordering}, time::Duration, }; @@ -178,25 +178,25 @@ where let data = addr_of_mut!(GLOBAL_STATE); write_volatile( addr_of_mut!((*data).current_input_ptr), - core::ptr::from_ref(input) as *const c_void, + ptr::from_ref(input) as *const c_void, ); write_volatile( addr_of_mut!((*data).executor_ptr), - core::ptr::from_ref(self) as *const c_void, + ptr::from_ref(self) as *const c_void, ); // Direct raw pointers access /aliasing is pretty undefined behavior. // Since the state and event may have moved in memory, refresh them right before the signal may happen write_volatile( addr_of_mut!((*data).state_ptr), - core::ptr::from_mut(state) as *mut c_void, + ptr::from_mut(state) as *mut c_void, ); write_volatile( addr_of_mut!((*data).event_mgr_ptr), - core::ptr::from_mut(mgr) as *mut c_void, + ptr::from_mut(mgr) as *mut c_void, ); write_volatile( addr_of_mut!((*data).fuzzer_ptr), - core::ptr::from_mut(fuzzer) as *mut c_void, + ptr::from_mut(fuzzer) as *mut c_void, ); compiler_fence(Ordering::SeqCst); } diff --git a/libafl/src/executors/inprocess_fork.rs b/libafl/src/executors/inprocess_fork.rs index 51e03ee862..50225439c8 100644 --- a/libafl/src/executors/inprocess_fork.rs +++ b/libafl/src/executors/inprocess_fork.rs @@ -3,7 +3,7 @@ use core::{ ffi::c_void, fmt::{self, Debug, Formatter}, marker::PhantomData, - ptr::{addr_of_mut, null_mut, write_volatile}, + ptr::{self, addr_of_mut, null_mut, write_volatile}, sync::atomic::{compiler_fence, Ordering}, time::Duration, }; @@ -278,15 +278,15 @@ where let data = addr_of_mut!(FORK_EXECUTOR_GLOBAL_DATA); write_volatile( addr_of_mut!((*data).executor_ptr), - core::ptr::from_ref(self) as *const c_void, + ptr::from_ref(self) as *const c_void, ); write_volatile( addr_of_mut!((*data).current_input_ptr), - core::ptr::from_ref(input) as *const c_void, + ptr::from_ref(input) as *const c_void, ); write_volatile( addr_of_mut!((*data).state_ptr), - core::ptr::from_mut(state) as *mut c_void, + ptr::from_mut(state) as *mut c_void, ); compiler_fence(Ordering::SeqCst); } diff --git a/libafl/src/executors/mod.rs b/libafl/src/executors/mod.rs index 9fed40d22f..abb87814ac 100644 --- a/libafl/src/executors/mod.rs +++ b/libafl/src/executors/mod.rs @@ -497,17 +497,13 @@ pub mod pybind { impl HasObservers for PythonExecutor { #[inline] fn observers(&self) -> &PythonObserversTuple { - let ptr = unwrap_me!(self.wrapper, e, { - core::ptr::from_ref::(e.observers()) - }); + let ptr = unwrap_me!(self.wrapper, e, { core::ptr::from_ref(e.observers()) }); unsafe { ptr.as_ref().unwrap() } } #[inline] fn observers_mut(&mut self) -> &mut PythonObserversTuple { - let ptr = unwrap_me_mut!(self.wrapper, e, { - core::ptr::from_mut::(e.observers_mut()) - }); + let ptr = unwrap_me_mut!(self.wrapper, e, { core::ptr::from_mut(e.observers_mut()) }); unsafe { ptr.as_mut().unwrap() } } } diff --git a/libafl/src/feedbacks/mod.rs b/libafl/src/feedbacks/mod.rs index af57449d6d..d7ac970d7e 100644 --- a/libafl/src/feedbacks/mod.rs +++ b/libafl/src/feedbacks/mod.rs @@ -1097,6 +1097,7 @@ impl From for ConstFeedback { #[allow(clippy::unnecessary_fallible_conversions)] #[allow(missing_docs)] pub mod pybind { + use core::ptr; use std::cell::UnsafeCell; use libafl_bolts::Named; @@ -1187,9 +1188,9 @@ pub mod pybind { // # Safety // We use this observer in Python ony when the ObserverTuple is PythonObserversTuple let dont_look_at_this: &PythonObserversTuple = - unsafe { &*(core::ptr::from_ref::(observers) as *const PythonObserversTuple) }; + unsafe { &*(ptr::from_ref(observers) as *const PythonObserversTuple) }; let dont_look_at_this2: &PythonEventManager = - unsafe { &*(core::ptr::from_mut::(manager) as *const PythonEventManager) }; + unsafe { &*(ptr::from_mut(manager) as *const PythonEventManager) }; Ok(Python::with_gil(|py| -> PyResult { let r: bool = self .inner @@ -1221,7 +1222,7 @@ pub mod pybind { // # Safety // We use this observer in Python ony when the ObserverTuple is PythonObserversTuple let dont_look_at_this: &PythonObserversTuple = - unsafe { &*(core::ptr::from_ref::(observers) as *const PythonObserversTuple) }; + unsafe { &*(ptr::from_ref(observers) as *const PythonObserversTuple) }; Python::with_gil(|py| -> PyResult<()> { self.inner.call_method1( py, diff --git a/libafl/src/monitors/mod.rs b/libafl/src/monitors/mod.rs index e86ca7f8b8..4b0b142f30 100644 --- a/libafl/src/monitors/mod.rs +++ b/libafl/src/monitors/mod.rs @@ -1407,15 +1407,13 @@ pub mod pybind { impl Monitor for PythonMonitor { fn client_stats_mut(&mut self) -> &mut Vec { let ptr = unwrap_me_mut!(self.wrapper, m, { - core::ptr::from_mut::>(m.client_stats_mut()) + core::ptr::from_mut(m.client_stats_mut()) }); unsafe { ptr.as_mut().unwrap() } } fn client_stats(&self) -> &[ClientStats] { - let ptr = unwrap_me!(self.wrapper, m, { - core::ptr::from_ref::<[ClientStats]>(m.client_stats()) - }); + let ptr = unwrap_me!(self.wrapper, m, { core::ptr::from_ref(m.client_stats()) }); unsafe { ptr.as_ref().unwrap() } } diff --git a/libafl/src/observers/mod.rs b/libafl/src/observers/mod.rs index ec4a0e3974..69f73d7066 100644 --- a/libafl/src/observers/mod.rs +++ b/libafl/src/observers/mod.rs @@ -593,6 +593,7 @@ where #[cfg(feature = "python")] #[allow(missing_docs)] pub mod pybind { + use core::ptr; use std::cell::UnsafeCell; use libafl_bolts::{ @@ -1047,7 +1048,7 @@ pub mod pybind { impl Named for PythonObserver { fn name(&self) -> &str { - let ptr = unwrap_me!(self.wrapper, o, { core::ptr::from_ref::(o.name()) }); + let ptr = unwrap_me!(self.wrapper, o, { ptr::from_ref::(o.name()) }); unsafe { ptr.as_ref().unwrap() } } } @@ -1266,7 +1267,7 @@ pub mod pybind { } PythonObserverWrapper::Python(py_wrapper) => { if type_eq::() && py_wrapper.name() == name { - r = (core::ptr::from_ref(py_wrapper) as *const T).as_ref(); + r = (ptr::from_ref(py_wrapper) as *const T).as_ref(); } } } @@ -1352,14 +1353,13 @@ pub mod pybind { if type_eq::() && py_wrapper.borrow(py).name() == name { - r = (std::ptr::addr_of!(*(*py_wrapper).borrow_mut(py)) - as *mut T) + r = (ptr::addr_of!(*(*py_wrapper).borrow_mut(py)) as *mut T) .as_mut(); } } PythonObserverWrapper::Python(py_wrapper) => { if type_eq::() && py_wrapper.name() == name { - r = (core::ptr::from_mut(py_wrapper) as *mut T).as_mut(); + r = (ptr::from_mut(py_wrapper) as *mut T).as_mut(); } } } diff --git a/libafl_frida/src/asan/asan_rt.rs b/libafl_frida/src/asan/asan_rt.rs index 4d0328e815..62ab0ea91f 100644 --- a/libafl_frida/src/asan/asan_rt.rs +++ b/libafl_frida/src/asan/asan_rt.rs @@ -565,7 +565,7 @@ impl AsanRuntime { interceptor.replace( frida_gum::Module::find_export_by_name($lib, stringify!($name)).expect("Failed to find function"), NativePointer([] as *mut c_void), - NativePointer(self as *mut _ as *mut c_void) + NativePointer(core::ptr::from_mut(self) as *mut c_void) ).ok(); } } @@ -590,7 +590,7 @@ impl AsanRuntime { interceptor.replace( frida_gum::Module::find_export_by_name($lib, stringify!($name)).expect("Failed to find function"), NativePointer([] as *mut c_void), - NativePointer(self as *mut _ as *mut c_void) + NativePointer(core::ptr::from_mut(self) as *mut c_void) ).ok(); } } @@ -1942,7 +1942,7 @@ impl AsanRuntime { ;->accessed_address: ; .dword 0x0 ; self_addr: - ; .qword core::ptr::from_mut(self) as *mut c_void as i64 + ; .qword core::ptr::from_mut(self) as *mut c_void as i64 ; self_regs_addr: ; .qword addr_of_mut!(self.regs) as i64 ; trap_func: @@ -2041,7 +2041,7 @@ impl AsanRuntime { ; br x1 // go back to the 'return address' ; self_addr: - ; .qword self as *mut _ as *mut c_void as i64 + ; .qword core::ptr::from_mut(self) as *mut c_void as i64 ; self_regs_addr: ; .qword addr_of_mut!(self.regs) as i64 ; trap_func: diff --git a/libafl_frida/src/cmplog_rt.rs b/libafl_frida/src/cmplog_rt.rs index a9e91505bc..588f308400 100644 --- a/libafl_frida/src/cmplog_rt.rs +++ b/libafl_frida/src/cmplog_rt.rs @@ -247,7 +247,7 @@ impl CmpLogRuntime { ; ldp x2, x3, [sp], #0x10 ; b >done ; self_addr: - ; .qword self as *mut _ as *mut c_void as i64 + ; .qword core::ptr::from_mut(self) as *mut c_void as i64 ; populate_lists: ; .qword CmpLogRuntime::populate_lists as *mut c_void as i64 ; done: diff --git a/libafl_qemu/libafl_qemu_sys/src/x86_64_stub_bindings.rs b/libafl_qemu/libafl_qemu_sys/src/x86_64_stub_bindings.rs index f7f904f13e..280e0567c3 100644 --- a/libafl_qemu/libafl_qemu_sys/src/x86_64_stub_bindings.rs +++ b/libafl_qemu/libafl_qemu_sys/src/x86_64_stub_bindings.rs @@ -90,11 +90,11 @@ impl __IncompleteArrayField { } #[inline] pub fn as_ptr(&self) -> *const T { - self as *const _ as *const T + core::ptr::from_ref(self) as *const T } #[inline] pub fn as_mut_ptr(&mut self) -> *mut T { - self as *mut _ as *mut T + core::ptr::from_mut(self) as *mut T } #[inline] pub unsafe fn as_slice(&self, len: usize) -> &[T] { diff --git a/libafl_qemu/src/executor.rs b/libafl_qemu/src/executor.rs index f1eff488b6..6b91540cf1 100644 --- a/libafl_qemu/src/executor.rs +++ b/libafl_qemu/src/executor.rs @@ -1,4 +1,6 @@ //! A `QEMU`-based executor for binary-only instrumentation in `LibAFL` +#[cfg(emulation_mode = "usermode")] +use core::ptr; use core::{ ffi::c_void, fmt::{self, Debug, Formatter}, @@ -79,8 +81,8 @@ pub unsafe fn inproc_qemu_crash_handler( Z: HasObjective, { let puc = match &mut context { - Some(v) => core::ptr::from_mut::(*v) as *mut c_void, - None => core::ptr::null_mut(), + Some(v) => ptr::from_mut::(*v) as *mut c_void, + None => ptr::null_mut(), }; libafl_qemu_handle_crash(signal as i32, info, puc); } diff --git a/libafl_targets/src/cmps/mod.rs b/libafl_targets/src/cmps/mod.rs index 095103484d..3715396b96 100644 --- a/libafl_targets/src/cmps/mod.rs +++ b/libafl_targets/src/cmps/mod.rs @@ -8,6 +8,7 @@ use alloc::{alloc::alloc_zeroed, boxed::Box, vec::Vec}; use core::{ alloc::Layout, fmt::{self, Debug, Formatter}, + mem, ptr, slice, }; use libafl::{ @@ -480,10 +481,7 @@ impl Serialize for AFLppCmpLogMap { S: Serializer, { let slice = unsafe { - core::slice::from_raw_parts( - (core::ptr::from_ref::(self)) as *const u8, - core::mem::size_of::(), - ) + slice::from_raw_parts(ptr::from_ref(self) as *const u8, mem::size_of::()) }; serializer.serialize_bytes(slice) } diff --git a/utils/noaslr/libnoaslr/src/lib.rs b/utils/noaslr/libnoaslr/src/lib.rs index 2517166b4c..777396628d 100644 --- a/utils/noaslr/libnoaslr/src/lib.rs +++ b/utils/noaslr/libnoaslr/src/lib.rs @@ -65,7 +65,7 @@ fn libnoaslr() -> Result<()> { libc::P_PID, 0, libc::PROC_ASLR_CTL, - &mut status as *mut i32 as *mut libc::c_void, + &mut core::ptr::from_mut(status) as *mut libc::c_void, ) < 0 { return Err(anyhow!("Failed to set aslr control")); diff --git a/utils/noaslr/noaslr/src/main.rs b/utils/noaslr/noaslr/src/main.rs index a00be2982f..1b9f146b92 100644 --- a/utils/noaslr/noaslr/src/main.rs +++ b/utils/noaslr/noaslr/src/main.rs @@ -36,7 +36,7 @@ fn disable_aslr() -> Result<()> { libc::P_PID, 0, libc::PROC_ASLR_CTL, - &mut status as *mut i32 as *mut libc::c_void, + &mut core::ptr::from_mut(status) as *mut libc::c_void, ) }; if r < 0 {