Skip to content

Commit

Permalink
Remove many as-casts of pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
GrigorenkoPV committed Feb 24, 2024
1 parent de87ddf commit c54384d
Show file tree
Hide file tree
Showing 17 changed files with 58 additions and 44 deletions.
4 changes: 2 additions & 2 deletions crates/line-tables-only/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Backtrace>) = Some(bt) };
unsafe { *data.cast::<Option<Backtrace>>() = Some(bt) };
}

fn assert_contains(
Expand Down Expand Up @@ -50,7 +50,7 @@ mod tests {
#[cfg_attr(windows, ignore)]
fn backtrace_works_with_line_tables_only() {
let mut backtrace: Option<Backtrace> = None;
unsafe { foo(store_backtrace, addr_of_mut!(backtrace) as *mut c_void) };
unsafe { foo(store_backtrace, addr_of_mut!(backtrace).cast::<c_void>()) };
let backtrace = backtrace.expect("backtrace");
assert_contains(&backtrace, "foo", "src/callback.c", 13);
assert_contains(&backtrace, "bar", "src/callback.c", 9);
Expand Down
4 changes: 2 additions & 2 deletions src/backtrace/dbghelp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.cast::<c_void>(),
ip: context.ip() as *mut c_void,
sp: context.sp() as *mut c_void,
#[cfg(not(target_env = "gnu"))]
Expand All @@ -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::<PVOID>(),
&mut establisher_frame,
ptr::null_mut(),
);
Expand Down
8 changes: 4 additions & 4 deletions src/backtrace/libunwind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
};
Expand Down Expand Up @@ -251,7 +251,7 @@ mod uw {
_Unwind_VRS_RegClass::_UVRSC_CORE,
15,
_Unwind_VRS_DataRepresentation::_UVRSD_UINT32,
ptr as *mut c_void,
ptr.cast::<c_void>(),
);
(val & !1) as libc::uintptr_t
}
Expand All @@ -267,7 +267,7 @@ mod uw {
_Unwind_VRS_RegClass::_UVRSC_CORE,
SP,
_Unwind_VRS_DataRepresentation::_UVRSD_UINT32,
ptr as *mut c_void,
ptr.cast::<c_void>(),
);
val as libc::uintptr_t
}
Expand Down
11 changes: 8 additions & 3 deletions src/backtrace/miri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,20 @@ pub fn trace<F: FnMut(&super::Frame) -> 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);

// 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);
Expand Down Expand Up @@ -101,7 +106,7 @@ unsafe fn trace_unsynchronized<F: FnMut(&super::Frame) -> 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::<c_void>());
if !cb(&super::Frame { inner: frame }) {
return;
}
Expand Down
7 changes: 4 additions & 3 deletions src/backtrace/noop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}
Expand All @@ -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> {
Expand Down
8 changes: 3 additions & 5 deletions src/capture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -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`
Expand All @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions src/dbghelp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,)*
Expand All @@ -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::<i8>());
if self.dll.is_null() {
Err(())
} else {
Expand All @@ -135,7 +135,7 @@ macro_rules! dbghelp {

fn symbol(&self, symbol: &[u8]) -> Option<usize> {
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),
}
Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<MutexGuard<'static, ()>>);

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<bool> = Cell::new(false));

Expand Down
2 changes: 1 addition & 1 deletion src/print/fuchsia.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<u8>(), name_len) };
let name = match core::str::from_utf8(name_slice) {
Ok(name) => name,
Err(_) => {
Expand Down
12 changes: 6 additions & 6 deletions src/symbolize/dbghelp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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<BytesOrWideString<'_>> {
Expand Down Expand Up @@ -184,8 +185,7 @@ unsafe fn do_resolve(
) {
const SIZE: usize = 2 * MAX_SYM_NAME + mem::size_of::<SYMBOL_INFOW>();
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::<SYMBOL_INFOW>();
info.MaxNameLen = MAX_SYM_NAME as ULONG;
// the struct size in C. the value is different to
// `size_of::<SYMBOL_INFOW>() - MAX_SYM_NAME + 1` (== 81)
Expand All @@ -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::<u16>();
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
Expand All @@ -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::<IMAGEHLP_LINEW64>();
line.SizeOfStruct = mem::size_of::<IMAGEHLP_LINEW64>() as DWORD;
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/symbolize/gimli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<u8>()) {
Some(pair) => pair,
None => return,
};
Expand Down
8 changes: 5 additions & 3 deletions src/symbolize/gimli/libs_aix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub(super) fn native_libraries() -> Vec<Library> {
loop {
if libc::loadquery(
libc::L_GETINFO,
buffer.as_mut_ptr() as *mut libc::c_char,
buffer.as_mut_ptr().cast::<libc::c_char>(),
(mem::size_of::<libc::ld_info>() * buffer.len()) as u32,
) != -1
{
Expand Down Expand Up @@ -66,8 +66,10 @@ pub(super) fn native_libraries() -> Vec<Library> {
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::<libc::c_char>()
.offset((*current).ldinfo_next as isize)
.cast::<libc::ld_info>();
}
}
return ret;
Expand Down
4 changes: 2 additions & 2 deletions src/symbolize/gimli/libs_dl_iterate_phdr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use core::slice;
pub(super) fn native_libraries() -> Vec<Library> {
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;
}
Expand Down Expand Up @@ -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<Library>);
let libs = &mut *vec.cast::<Vec<Library>>();
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
Expand Down
2 changes: 1 addition & 1 deletion src/symbolize/gimli/libs_illumos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub(super) fn native_libraries() -> Vec<Library> {
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::<libc::c_void>(),
) != 0
{
return libs;
Expand Down
15 changes: 11 additions & 4 deletions src/symbolize/gimli/libs_macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T: ?Sized>(r: &T) -> *const T {
r
}

pub(super) fn native_libraries() -> Vec<Library> {
let mut ret = Vec::new();
let images = unsafe { libc::_dyld_image_count() };
Expand Down Expand Up @@ -42,18 +49,18 @@ fn native_library(i: u32) -> Option<Library> {
match (*header).magic {
macho::MH_MAGIC => {
let endian = NativeEndian;
let header = &*(header as *const macho::MachHeader32<NativeEndian>);
let header = &*header.cast::<macho::MachHeader32<NativeEndian>>();
let data = core::slice::from_raw_parts(
header as *const _ as *const u8,
ptr_from_ref(header).cast::<u8>(),
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<NativeEndian>);
let header = &*header.cast::<macho::MachHeader64<NativeEndian>>();
let data = core::slice::from_raw_parts(
header as *const _ as *const u8,
ptr_from_ref(header).cast::<u8>(),
mem::size_of_val(header) + header.sizeofcmds.get(endian) as usize,
);
(header.load_commands(endian, data, 0).ok()?, endian)
Expand Down
4 changes: 2 additions & 2 deletions src/symbolize/gimli/mmap_windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl Mmap {
pub unsafe fn map(file: &File, len: usize) -> Option<Mmap> {
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,
Expand All @@ -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::<u8>(), self.len) }
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/symbolize/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down

0 comments on commit c54384d

Please sign in to comment.