Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use addr_of! #585

Merged
merged 2 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
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
5 changes: 3 additions & 2 deletions crates/line-tables-only/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod tests {
use backtrace::Backtrace;
use libc::c_void;
use std::path::Path;
use std::ptr::addr_of_mut;

pub type Callback = extern "C" fn(data: *mut c_void);

Expand All @@ -12,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 @@ -49,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, &mut backtrace as *mut _ 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
10 changes: 5 additions & 5 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,
&mut handler_data as *mut usize as *mut PVOID,
ptr::addr_of_mut!(handler_data).cast::<PVOID>(),
&mut establisher_frame,
ptr::null_mut(),
);
Expand All @@ -172,7 +172,7 @@ pub unsafe fn trace(cb: &mut dyn FnMut(&super::Frame) -> bool) {
#[cfg(any(target_arch = "x86", target_arch = "arm"))]
#[inline(always)]
pub unsafe fn trace(cb: &mut dyn FnMut(&super::Frame) -> bool) {
use core::mem;
use core::{mem, ptr};

// Allocate necessary structures for doing the stack walk
let process = GetCurrentProcess();
Expand Down Expand Up @@ -215,7 +215,7 @@ pub unsafe fn trace(cb: &mut dyn FnMut(&super::Frame) -> bool) {
process,
thread,
&mut stack_frame_ex,
&mut context.0 as *mut CONTEXT as PVOID,
ptr::addr_of_mut!(context.0) as PVOID,
None,
Some(function_table_access),
Some(get_module_base),
Expand Down Expand Up @@ -253,7 +253,7 @@ pub unsafe fn trace(cb: &mut dyn FnMut(&super::Frame) -> bool) {
process,
thread,
&mut stack_frame64,
&mut context.0 as *mut CONTEXT as PVOID,
ptr::addr_of_mut!(context.0) as PVOID,
None,
Some(function_table_access),
Some(get_module_base),
Expand Down
15 changes: 9 additions & 6 deletions src/backtrace/libunwind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

use super::super::Bomb;
use core::ffi::c_void;
use core::ptr::addr_of_mut;

pub enum Frame {
Raw(*mut uw::_Unwind_Context),
Expand Down Expand Up @@ -101,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, &mut cb as *mut _ 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 @@ -198,6 +199,8 @@ mod uw {
_Unwind_GetGR(ctx, 15)
}
} else {
use core::ptr::addr_of_mut;

// On android and arm, the function `_Unwind_GetIP` and a bunch of
// others are macros, so we define functions containing the
// expansion of the macros.
Expand Down Expand Up @@ -242,13 +245,13 @@ mod uw {

pub unsafe fn _Unwind_GetIP(ctx: *mut _Unwind_Context) -> libc::uintptr_t {
let mut val: _Unwind_Word = 0;
let ptr = &mut val as *mut _Unwind_Word;
let ptr = addr_of_mut!(val);
let _ = _Unwind_VRS_Get(
ctx,
_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 @@ -258,13 +261,13 @@ mod uw {

pub unsafe fn get_sp(ctx: *mut _Unwind_Context) -> libc::uintptr_t {
let mut val: _Unwind_Word = 0;
let ptr = &mut val as *mut _Unwind_Word;
let ptr = addr_of_mut!(val);
let _ = _Unwind_VRS_Get(
ctx,
_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
8 changes: 4 additions & 4 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 All @@ -155,7 +155,7 @@ macro_rules! dbghelp {

pub fn dbghelp(&self) -> *mut Dbghelp {
unsafe {
&mut DBGHELP
ptr::addr_of_mut!(DBGHELP)
}
}
}
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 *data.0.as_mut_ptr().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 = &name_buffer[..name_len] as *const [u8];
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), &mut ret as *mut Vec<_> 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,
(&mut map) as *mut *const LinkMap as *mut libc::c_void,
core::ptr::addr_of_mut!(map).cast::<libc::c_void>(),
) != 0
{
return libs;
Expand Down
Loading
Loading