From c07e667eacbb1306ed475a3ce303f70424a791d8 Mon Sep 17 00:00:00 2001 From: Pavel Grigorenko Date: Sat, 24 Feb 2024 23:10:13 +0300 Subject: [PATCH] Remove many `as`-casts of pointers --- crates/line-tables-only/src/lib.rs | 4 ++-- src/backtrace/dbghelp.rs | 4 ++-- src/backtrace/libunwind.rs | 8 ++++---- src/backtrace/miri.rs | 11 ++++++++--- src/backtrace/noop.rs | 7 ++++--- src/capture.rs | 8 +++----- src/dbghelp.rs | 6 +++--- src/lib.rs | 3 ++- src/print/fuchsia.rs | 2 +- src/symbolize/dbghelp.rs | 12 ++++++------ src/symbolize/gimli.rs | 2 +- src/symbolize/gimli/libs_aix.rs | 8 +++++--- src/symbolize/gimli/libs_dl_iterate_phdr.rs | 4 ++-- src/symbolize/gimli/libs_illumos.rs | 2 +- src/symbolize/gimli/libs_macos.rs | 15 +++++++++++---- src/symbolize/gimli/mmap_windows.rs | 4 ++-- src/symbolize/mod.rs | 2 +- 17 files changed, 58 insertions(+), 44 deletions(-) diff --git a/crates/line-tables-only/src/lib.rs b/crates/line-tables-only/src/lib.rs index 3c71a73c3..860d3db59 100644 --- a/crates/line-tables-only/src/lib.rs +++ b/crates/line-tables-only/src/lib.rs @@ -13,7 +13,7 @@ mod tests { extern "C" fn store_backtrace(data: *mut c_void) { let bt = backtrace::Backtrace::new(); - unsafe { *(data as *mut Option) = Some(bt) }; + unsafe { *data.cast::>() = Some(bt) }; } fn assert_contains( @@ -50,7 +50,7 @@ mod tests { #[cfg_attr(windows, ignore)] fn backtrace_works_with_line_tables_only() { let mut backtrace: Option = None; - unsafe { foo(store_backtrace, addr_of_mut!(backtrace) as *mut c_void) }; + unsafe { foo(store_backtrace, addr_of_mut!(backtrace).cast::()) }; let backtrace = backtrace.expect("backtrace"); assert_contains(&backtrace, "foo", "src/callback.c", 13); assert_contains(&backtrace, "bar", "src/callback.c", 9); diff --git a/src/backtrace/dbghelp.rs b/src/backtrace/dbghelp.rs index ecc126c70..229e4b142 100644 --- a/src/backtrace/dbghelp.rs +++ b/src/backtrace/dbghelp.rs @@ -141,7 +141,7 @@ pub unsafe fn trace(cb: &mut dyn FnMut(&super::Frame) -> bool) { let frame = super::Frame { inner: Frame { - base_address: fn_entry as *mut c_void, + base_address: fn_entry, ip: context.ip() as *mut c_void, sp: context.sp() as *mut c_void, #[cfg(not(target_env = "gnu"))] @@ -162,7 +162,7 @@ pub unsafe fn trace(cb: &mut dyn FnMut(&super::Frame) -> bool) { context.ip(), fn_entry, &mut context.0, - ptr::addr_of_mut!(handler_data) as *mut PVOID, + ptr::addr_of_mut!(handler_data).cast::(), &mut establisher_frame, ptr::null_mut(), ); diff --git a/src/backtrace/libunwind.rs b/src/backtrace/libunwind.rs index 9861d7d32..fb02c4cd1 100644 --- a/src/backtrace/libunwind.rs +++ b/src/backtrace/libunwind.rs @@ -102,13 +102,13 @@ impl Clone for Frame { #[inline(always)] pub unsafe fn trace(mut cb: &mut dyn FnMut(&super::Frame) -> bool) { - uw::_Unwind_Backtrace(trace_fn, addr_of_mut!(cb) as *mut _); + uw::_Unwind_Backtrace(trace_fn, addr_of_mut!(cb).cast()); extern "C" fn trace_fn( ctx: *mut uw::_Unwind_Context, arg: *mut c_void, ) -> uw::_Unwind_Reason_Code { - let cb = unsafe { &mut *(arg as *mut &mut dyn FnMut(&super::Frame) -> bool) }; + let cb = unsafe { &mut *arg.cast::<&mut dyn FnMut(&super::Frame) -> bool>() }; let cx = super::Frame { inner: Frame::Raw(ctx), }; @@ -251,7 +251,7 @@ mod uw { _Unwind_VRS_RegClass::_UVRSC_CORE, 15, _Unwind_VRS_DataRepresentation::_UVRSD_UINT32, - ptr as *mut c_void, + ptr.cast::(), ); (val & !1) as libc::uintptr_t } @@ -267,7 +267,7 @@ mod uw { _Unwind_VRS_RegClass::_UVRSC_CORE, SP, _Unwind_VRS_DataRepresentation::_UVRSD_UINT32, - ptr as *mut c_void, + ptr.cast::(), ); val as libc::uintptr_t } diff --git a/src/backtrace/miri.rs b/src/backtrace/miri.rs index f8c496428..ce06ed6bd 100644 --- a/src/backtrace/miri.rs +++ b/src/backtrace/miri.rs @@ -65,7 +65,7 @@ pub fn trace bool>(cb: F) { pub fn resolve_addr(ptr: *mut c_void) -> Frame { // SAFETY: Miri will stop execution with an error if this pointer // is invalid. - let frame = unsafe { miri_resolve_frame(ptr as *mut (), 1) }; + let frame = unsafe { miri_resolve_frame(ptr.cast::<()>(), 1) }; let mut name = Vec::with_capacity(frame.name_len); let mut filename = Vec::with_capacity(frame.filename_len); @@ -73,7 +73,12 @@ pub fn resolve_addr(ptr: *mut c_void) -> Frame { // SAFETY: name and filename have been allocated with the amount // of memory miri has asked for, and miri guarantees it will initialize it unsafe { - miri_resolve_frame_names(ptr as *mut (), 0, name.as_mut_ptr(), filename.as_mut_ptr()); + miri_resolve_frame_names( + ptr.cast::<()>(), + 0, + name.as_mut_ptr(), + filename.as_mut_ptr(), + ); name.set_len(frame.name_len); filename.set_len(frame.filename_len); @@ -101,7 +106,7 @@ unsafe fn trace_unsynchronized bool>(mut cb: F) { frames.set_len(len); for ptr in frames.iter() { - let frame = resolve_addr(*ptr as *mut c_void); + let frame = resolve_addr((*ptr).cast::()); if !cb(&super::Frame { inner: frame }) { return; } diff --git a/src/backtrace/noop.rs b/src/backtrace/noop.rs index 7bcea67aa..cae53df14 100644 --- a/src/backtrace/noop.rs +++ b/src/backtrace/noop.rs @@ -2,6 +2,7 @@ //! appropriate. use core::ffi::c_void; +use core::ptr::null_mut; #[inline(always)] pub fn trace(_cb: &mut dyn FnMut(&super::Frame) -> bool) {} @@ -11,15 +12,15 @@ pub struct Frame; impl Frame { pub fn ip(&self) -> *mut c_void { - 0 as *mut _ + null_mut() } pub fn sp(&self) -> *mut c_void { - 0 as *mut _ + null_mut() } pub fn symbol_address(&self) -> *mut c_void { - 0 as *mut _ + null_mut() } pub fn module_base_address(&self) -> Option<*mut c_void> { diff --git a/src/capture.rs b/src/capture.rs index e0dd9c474..a7ee93834 100644 --- a/src/capture.rs +++ b/src/capture.rs @@ -272,7 +272,7 @@ impl BacktraceFrame { /// This function requires the `std` feature of the `backtrace` crate to be /// enabled, and the `std` feature is enabled by default. pub fn ip(&self) -> *mut c_void { - self.frame.ip() as *mut c_void + self.frame.ip() } /// Same as `Frame::symbol_address` @@ -282,7 +282,7 @@ impl BacktraceFrame { /// This function requires the `std` feature of the `backtrace` crate to be /// enabled, and the `std` feature is enabled by default. pub fn symbol_address(&self) -> *mut c_void { - self.frame.symbol_address() as *mut c_void + self.frame.symbol_address() } /// Same as `Frame::module_base_address` @@ -292,9 +292,7 @@ impl BacktraceFrame { /// This function requires the `std` feature of the `backtrace` crate to be /// enabled, and the `std` feature is enabled by default. pub fn module_base_address(&self) -> Option<*mut c_void> { - self.frame - .module_base_address() - .map(|addr| addr as *mut c_void) + self.frame.module_base_address() } /// Returns the list of symbols that this frame corresponds to. diff --git a/src/dbghelp.rs b/src/dbghelp.rs index c524d85a9..54d320bce 100644 --- a/src/dbghelp.rs +++ b/src/dbghelp.rs @@ -88,7 +88,7 @@ macro_rules! dbghelp { static mut DBGHELP: Dbghelp = Dbghelp { // Initially we haven't loaded the DLL - dll: 0 as *mut _, + dll: ptr::null_mut(), // Initially all functions are set to zero to say they need to be // dynamically loaded. $($name: 0,)* @@ -108,7 +108,7 @@ macro_rules! dbghelp { } let lib = b"dbghelp.dll\0"; unsafe { - self.dll = LoadLibraryA(lib.as_ptr() as *const i8); + self.dll = LoadLibraryA(lib.as_ptr().cast::()); if self.dll.is_null() { Err(()) } else { @@ -135,7 +135,7 @@ macro_rules! dbghelp { fn symbol(&self, symbol: &[u8]) -> Option { unsafe { - match GetProcAddress(self.dll, symbol.as_ptr() as *const _) as usize { + match GetProcAddress(self.dll, symbol.as_ptr().cast()) as usize { 0 => None, n => Some(n), } diff --git a/src/lib.rs b/src/lib.rs index 44a0bc64e..5b97281a7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -159,11 +159,12 @@ impl Drop for Bomb { mod lock { use std::boxed::Box; use std::cell::Cell; + use std::ptr; use std::sync::{Mutex, MutexGuard, Once}; pub struct LockGuard(Option>); - static mut LOCK: *mut Mutex<()> = 0 as *mut _; + static mut LOCK: *mut Mutex<()> = ptr::null_mut(); static INIT: Once = Once::new(); thread_local!(static LOCK_HELD: Cell = Cell::new(false)); diff --git a/src/print/fuchsia.rs b/src/print/fuchsia.rs index 6e0e7ddd0..9bae98dd0 100644 --- a/src/print/fuchsia.rs +++ b/src/print/fuchsia.rs @@ -359,7 +359,7 @@ fn for_each_dso(mut visitor: &mut DsoPrinter<'_, '_>) { // location. let name_len = unsafe { libc::strlen(info.name) }; let name_slice: &[u8] = - unsafe { core::slice::from_raw_parts(info.name as *const u8, name_len) }; + unsafe { core::slice::from_raw_parts(info.name.cast::(), name_len) }; let name = match core::str::from_utf8(name_slice) { Ok(name) => name, Err(_) => { diff --git a/src/symbolize/dbghelp.rs b/src/symbolize/dbghelp.rs index 1e6295f3c..57cc0a07d 100644 --- a/src/symbolize/dbghelp.rs +++ b/src/symbolize/dbghelp.rs @@ -23,6 +23,7 @@ use core::char; use core::ffi::c_void; use core::marker; use core::mem; +use core::ptr; use core::slice; // Store an OsString on std so we can provide the symbol name and filename. @@ -44,7 +45,7 @@ impl Symbol<'_> { } pub fn addr(&self) -> Option<*mut c_void> { - Some(self.addr as *mut _) + Some(self.addr) } pub fn filename_raw(&self) -> Option> { @@ -184,8 +185,7 @@ unsafe fn do_resolve( ) { const SIZE: usize = 2 * MAX_SYM_NAME + mem::size_of::(); let mut data = Aligned8([0u8; SIZE]); - let data = &mut data.0; - let info = &mut *(data.as_mut_ptr() as *mut SYMBOL_INFOW); + let info = &mut *ptr::addr_of_mut!(data.0).cast::(); info.MaxNameLen = MAX_SYM_NAME as ULONG; // the struct size in C. the value is different to // `size_of::() - MAX_SYM_NAME + 1` (== 81) @@ -200,7 +200,7 @@ unsafe fn do_resolve( // give a buffer of (MaxNameLen - 1) characters and set NameLen to // the real value. let name_len = ::core::cmp::min(info.NameLen as usize, info.MaxNameLen as usize - 1); - let name_ptr = info.Name.as_ptr() as *const u16; + let name_ptr = info.Name.as_ptr().cast::(); let name = slice::from_raw_parts(name_ptr, name_len); // Reencode the utf-16 symbol to utf-8 so we can use `SymbolName::new` like @@ -222,7 +222,7 @@ unsafe fn do_resolve( } } } - let name = core::ptr::addr_of!(name_buffer[..name_len]); + let name = ptr::addr_of!(name_buffer[..name_len]); let mut line = mem::zeroed::(); line.SizeOfStruct = mem::size_of::() as DWORD; @@ -240,7 +240,7 @@ unsafe fn do_resolve( let len = len as usize; - filename = Some(slice::from_raw_parts(base, len) as *const [u16]); + filename = Some(ptr::from_ref(slice::from_raw_parts(base, len))); } cb(&super::Symbol { diff --git a/src/symbolize/gimli.rs b/src/symbolize/gimli.rs index 3b28bf741..fa8e59723 100644 --- a/src/symbolize/gimli.rs +++ b/src/symbolize/gimli.rs @@ -430,7 +430,7 @@ pub unsafe fn resolve(what: ResolveWhat<'_>, cb: &mut dyn FnMut(&super::Symbol)) }; Cache::with_global(|cache| { - let (lib, addr) = match cache.avma_to_svma(addr as *const u8) { + let (lib, addr) = match cache.avma_to_svma(addr.cast_const().cast::()) { Some(pair) => pair, None => return, }; diff --git a/src/symbolize/gimli/libs_aix.rs b/src/symbolize/gimli/libs_aix.rs index 8cac11d4d..aa4eaff89 100644 --- a/src/symbolize/gimli/libs_aix.rs +++ b/src/symbolize/gimli/libs_aix.rs @@ -20,7 +20,7 @@ pub(super) fn native_libraries() -> Vec { loop { if libc::loadquery( libc::L_GETINFO, - buffer.as_mut_ptr() as *mut libc::c_char, + buffer.as_mut_ptr().cast::(), (mem::size_of::() * buffer.len()) as u32, ) != -1 { @@ -66,8 +66,10 @@ pub(super) fn native_libraries() -> Vec { if (*current).ldinfo_next == 0 { break; } - current = (current as *mut libc::c_char).offset((*current).ldinfo_next as isize) - as *mut libc::ld_info; + current = current + .cast::() + .offset((*current).ldinfo_next as isize) + .cast::(); } } return ret; diff --git a/src/symbolize/gimli/libs_dl_iterate_phdr.rs b/src/symbolize/gimli/libs_dl_iterate_phdr.rs index 9fcc8161a..c8558e626 100644 --- a/src/symbolize/gimli/libs_dl_iterate_phdr.rs +++ b/src/symbolize/gimli/libs_dl_iterate_phdr.rs @@ -12,7 +12,7 @@ use core::slice; pub(super) fn native_libraries() -> Vec { let mut ret = Vec::new(); unsafe { - libc::dl_iterate_phdr(Some(callback), core::ptr::addr_of_mut!(ret) as *mut _); + libc::dl_iterate_phdr(Some(callback), core::ptr::addr_of_mut!(ret).cast()); } return ret; } @@ -43,7 +43,7 @@ unsafe extern "C" fn callback( vec: *mut libc::c_void, ) -> libc::c_int { let info = &*info; - let libs = &mut *(vec as *mut Vec); + let libs = &mut *vec.cast::>(); let is_main_prog = info.dlpi_name.is_null() || *info.dlpi_name == 0; let name = if is_main_prog { // The man page for dl_iterate_phdr says that the first object visited by diff --git a/src/symbolize/gimli/libs_illumos.rs b/src/symbolize/gimli/libs_illumos.rs index 7da16c6d0..4fc510cf5 100644 --- a/src/symbolize/gimli/libs_illumos.rs +++ b/src/symbolize/gimli/libs_illumos.rs @@ -41,7 +41,7 @@ pub(super) fn native_libraries() -> Vec { if dlinfo( RTLD_SELF, RTLD_DI_LINKMAP, - core::ptr::addr_of_mut!(map) as *mut libc::c_void, + core::ptr::addr_of_mut!(map).cast::(), ) != 0 { return libs; diff --git a/src/symbolize/gimli/libs_macos.rs b/src/symbolize/gimli/libs_macos.rs index 438bbff6f..671505a13 100644 --- a/src/symbolize/gimli/libs_macos.rs +++ b/src/symbolize/gimli/libs_macos.rs @@ -7,6 +7,13 @@ use super::{Library, LibrarySegment}; use core::convert::TryInto; use core::mem; +// FIXME: replace with ptr::from_ref once MSRV is high enough +#[inline(always)] +#[must_use] +const fn ptr_from_ref(r: &T) -> *const T { + r +} + pub(super) fn native_libraries() -> Vec { let mut ret = Vec::new(); let images = unsafe { libc::_dyld_image_count() }; @@ -42,18 +49,18 @@ fn native_library(i: u32) -> Option { match (*header).magic { macho::MH_MAGIC => { let endian = NativeEndian; - let header = &*(header as *const macho::MachHeader32); + let header = &*header.cast::>(); let data = core::slice::from_raw_parts( - header as *const _ as *const u8, + ptr_from_ref(header).cast::(), mem::size_of_val(header) + header.sizeofcmds.get(endian) as usize, ); (header.load_commands(endian, data, 0).ok()?, endian) } macho::MH_MAGIC_64 => { let endian = NativeEndian; - let header = &*(header as *const macho::MachHeader64); + let header = &*header.cast::>(); let data = core::slice::from_raw_parts( - header as *const _ as *const u8, + ptr_from_ref(header).cast::(), mem::size_of_val(header) + header.sizeofcmds.get(endian) as usize, ); (header.load_commands(endian, data, 0).ok()?, endian) diff --git a/src/symbolize/gimli/mmap_windows.rs b/src/symbolize/gimli/mmap_windows.rs index b39509d8c..37c2a4066 100644 --- a/src/symbolize/gimli/mmap_windows.rs +++ b/src/symbolize/gimli/mmap_windows.rs @@ -17,7 +17,7 @@ impl Mmap { pub unsafe fn map(file: &File, len: usize) -> Option { let file = file.try_clone().ok()?; let mapping = CreateFileMappingA( - file.as_raw_handle() as *mut _, + file.as_raw_handle(), ptr::null_mut(), PAGE_READONLY, 0, @@ -43,7 +43,7 @@ impl Deref for Mmap { type Target = [u8]; fn deref(&self) -> &[u8] { - unsafe { slice::from_raw_parts(self.ptr as *const u8, self.len) } + unsafe { slice::from_raw_parts(self.ptr.cast_const().cast::(), self.len) } } } diff --git a/src/symbolize/mod.rs b/src/symbolize/mod.rs index a7c199506..f8571d7be 100644 --- a/src/symbolize/mod.rs +++ b/src/symbolize/mod.rs @@ -210,7 +210,7 @@ impl Symbol { /// Returns the starting address of this function. pub fn addr(&self) -> Option<*mut c_void> { - self.inner.addr().map(|p| p as *mut _) + self.inner.addr() } /// Returns the raw filename as a slice. This is mainly useful for `no_std`