Skip to content

Commit ca477b9

Browse files
Merge pull request #585 from GrigorenkoPV/addr_of
Use `addr_of!` A follow-up to rust-lang/rust#121556
2 parents ef39a7d + b31831e commit ca477b9

File tree

19 files changed

+71
-52
lines changed

19 files changed

+71
-52
lines changed

crates/line-tables-only/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ mod tests {
33
use backtrace::Backtrace;
44
use libc::c_void;
55
use std::path::Path;
6+
use std::ptr::addr_of_mut;
67

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

@@ -12,7 +13,7 @@ mod tests {
1213

1314
extern "C" fn store_backtrace(data: *mut c_void) {
1415
let bt = backtrace::Backtrace::new();
15-
unsafe { *(data as *mut Option<Backtrace>) = Some(bt) };
16+
unsafe { *data.cast::<Option<Backtrace>>() = Some(bt) };
1617
}
1718

1819
fn assert_contains(
@@ -49,7 +50,7 @@ mod tests {
4950
#[cfg_attr(windows, ignore)]
5051
fn backtrace_works_with_line_tables_only() {
5152
let mut backtrace: Option<Backtrace> = None;
52-
unsafe { foo(store_backtrace, &mut backtrace as *mut _ as *mut c_void) };
53+
unsafe { foo(store_backtrace, addr_of_mut!(backtrace).cast::<c_void>()) };
5354
let backtrace = backtrace.expect("backtrace");
5455
assert_contains(&backtrace, "foo", "src/callback.c", 13);
5556
assert_contains(&backtrace, "bar", "src/callback.c", 9);

src/backtrace/dbghelp.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ pub unsafe fn trace(cb: &mut dyn FnMut(&super::Frame) -> bool) {
145145

146146
let frame = super::Frame {
147147
inner: Frame {
148-
base_address: fn_entry as *mut c_void,
148+
base_address: fn_entry.cast::<c_void>(),
149149
ip: context.ip() as *mut c_void,
150150
sp: context.sp() as *mut c_void,
151151
#[cfg(not(target_env = "gnu"))]
@@ -166,7 +166,7 @@ pub unsafe fn trace(cb: &mut dyn FnMut(&super::Frame) -> bool) {
166166
context.ip(),
167167
fn_entry,
168168
&mut context.0,
169-
&mut handler_data as *mut usize as *mut PVOID,
169+
ptr::addr_of_mut!(handler_data).cast::<PVOID>(),
170170
&mut establisher_frame,
171171
ptr::null_mut(),
172172
);
@@ -176,7 +176,7 @@ pub unsafe fn trace(cb: &mut dyn FnMut(&super::Frame) -> bool) {
176176
#[cfg(any(target_arch = "x86", target_arch = "arm"))]
177177
#[inline(always)]
178178
pub unsafe fn trace(cb: &mut dyn FnMut(&super::Frame) -> bool) {
179-
use core::mem;
179+
use core::{mem, ptr};
180180

181181
// Allocate necessary structures for doing the stack walk
182182
let process = GetCurrentProcess();
@@ -219,7 +219,7 @@ pub unsafe fn trace(cb: &mut dyn FnMut(&super::Frame) -> bool) {
219219
process,
220220
thread,
221221
&mut stack_frame_ex,
222-
&mut context.0 as *mut CONTEXT as PVOID,
222+
ptr::addr_of_mut!(context.0) as PVOID,
223223
None,
224224
Some(function_table_access),
225225
Some(get_module_base),
@@ -257,7 +257,7 @@ pub unsafe fn trace(cb: &mut dyn FnMut(&super::Frame) -> bool) {
257257
process,
258258
thread,
259259
&mut stack_frame64,
260-
&mut context.0 as *mut CONTEXT as PVOID,
260+
ptr::addr_of_mut!(context.0) as PVOID,
261261
None,
262262
Some(function_table_access),
263263
Some(get_module_base),

src/backtrace/libunwind.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
1818
use super::super::Bomb;
1919
use core::ffi::c_void;
20+
use core::ptr::addr_of_mut;
2021

2122
pub enum Frame {
2223
Raw(*mut uw::_Unwind_Context),
@@ -101,13 +102,13 @@ impl Clone for Frame {
101102

102103
#[inline(always)]
103104
pub unsafe fn trace(mut cb: &mut dyn FnMut(&super::Frame) -> bool) {
104-
uw::_Unwind_Backtrace(trace_fn, &mut cb as *mut _ as *mut _);
105+
uw::_Unwind_Backtrace(trace_fn, addr_of_mut!(cb).cast());
105106

106107
extern "C" fn trace_fn(
107108
ctx: *mut uw::_Unwind_Context,
108109
arg: *mut c_void,
109110
) -> uw::_Unwind_Reason_Code {
110-
let cb = unsafe { &mut *(arg as *mut &mut dyn FnMut(&super::Frame) -> bool) };
111+
let cb = unsafe { &mut *arg.cast::<&mut dyn FnMut(&super::Frame) -> bool>() };
111112
let cx = super::Frame {
112113
inner: Frame::Raw(ctx),
113114
};
@@ -198,6 +199,8 @@ mod uw {
198199
_Unwind_GetGR(ctx, 15)
199200
}
200201
} else {
202+
use core::ptr::addr_of_mut;
203+
201204
// On android and arm, the function `_Unwind_GetIP` and a bunch of
202205
// others are macros, so we define functions containing the
203206
// expansion of the macros.
@@ -242,13 +245,13 @@ mod uw {
242245

243246
pub unsafe fn _Unwind_GetIP(ctx: *mut _Unwind_Context) -> libc::uintptr_t {
244247
let mut val: _Unwind_Word = 0;
245-
let ptr = &mut val as *mut _Unwind_Word;
248+
let ptr = addr_of_mut!(val);
246249
let _ = _Unwind_VRS_Get(
247250
ctx,
248251
_Unwind_VRS_RegClass::_UVRSC_CORE,
249252
15,
250253
_Unwind_VRS_DataRepresentation::_UVRSD_UINT32,
251-
ptr as *mut c_void,
254+
ptr.cast::<c_void>(),
252255
);
253256
(val & !1) as libc::uintptr_t
254257
}
@@ -258,13 +261,13 @@ mod uw {
258261

259262
pub unsafe fn get_sp(ctx: *mut _Unwind_Context) -> libc::uintptr_t {
260263
let mut val: _Unwind_Word = 0;
261-
let ptr = &mut val as *mut _Unwind_Word;
264+
let ptr = addr_of_mut!(val);
262265
let _ = _Unwind_VRS_Get(
263266
ctx,
264267
_Unwind_VRS_RegClass::_UVRSC_CORE,
265268
SP,
266269
_Unwind_VRS_DataRepresentation::_UVRSD_UINT32,
267-
ptr as *mut c_void,
270+
ptr.cast::<c_void>(),
268271
);
269272
val as libc::uintptr_t
270273
}

src/backtrace/miri.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,20 @@ pub fn trace<F: FnMut(&super::Frame) -> bool>(cb: F) {
6565
pub fn resolve_addr(ptr: *mut c_void) -> Frame {
6666
// SAFETY: Miri will stop execution with an error if this pointer
6767
// is invalid.
68-
let frame = unsafe { miri_resolve_frame(ptr as *mut (), 1) };
68+
let frame = unsafe { miri_resolve_frame(ptr.cast::<()>(), 1) };
6969

7070
let mut name = Vec::with_capacity(frame.name_len);
7171
let mut filename = Vec::with_capacity(frame.filename_len);
7272

7373
// SAFETY: name and filename have been allocated with the amount
7474
// of memory miri has asked for, and miri guarantees it will initialize it
7575
unsafe {
76-
miri_resolve_frame_names(ptr as *mut (), 0, name.as_mut_ptr(), filename.as_mut_ptr());
76+
miri_resolve_frame_names(
77+
ptr.cast::<()>(),
78+
0,
79+
name.as_mut_ptr(),
80+
filename.as_mut_ptr(),
81+
);
7782

7883
name.set_len(frame.name_len);
7984
filename.set_len(frame.filename_len);
@@ -101,7 +106,7 @@ unsafe fn trace_unsynchronized<F: FnMut(&super::Frame) -> bool>(mut cb: F) {
101106
frames.set_len(len);
102107

103108
for ptr in frames.iter() {
104-
let frame = resolve_addr(*ptr as *mut c_void);
109+
let frame = resolve_addr((*ptr).cast::<c_void>());
105110
if !cb(&super::Frame { inner: frame }) {
106111
return;
107112
}

src/backtrace/noop.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//! appropriate.
33
44
use core::ffi::c_void;
5+
use core::ptr::null_mut;
56

67
#[inline(always)]
78
pub fn trace(_cb: &mut dyn FnMut(&super::Frame) -> bool) {}
@@ -11,15 +12,15 @@ pub struct Frame;
1112

1213
impl Frame {
1314
pub fn ip(&self) -> *mut c_void {
14-
0 as *mut _
15+
null_mut()
1516
}
1617

1718
pub fn sp(&self) -> *mut c_void {
18-
0 as *mut _
19+
null_mut()
1920
}
2021

2122
pub fn symbol_address(&self) -> *mut c_void {
22-
0 as *mut _
23+
null_mut()
2324
}
2425

2526
pub fn module_base_address(&self) -> Option<*mut c_void> {

src/capture.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ impl BacktraceFrame {
272272
/// This function requires the `std` feature of the `backtrace` crate to be
273273
/// enabled, and the `std` feature is enabled by default.
274274
pub fn ip(&self) -> *mut c_void {
275-
self.frame.ip() as *mut c_void
275+
self.frame.ip()
276276
}
277277

278278
/// Same as `Frame::symbol_address`
@@ -282,7 +282,7 @@ impl BacktraceFrame {
282282
/// This function requires the `std` feature of the `backtrace` crate to be
283283
/// enabled, and the `std` feature is enabled by default.
284284
pub fn symbol_address(&self) -> *mut c_void {
285-
self.frame.symbol_address() as *mut c_void
285+
self.frame.symbol_address()
286286
}
287287

288288
/// Same as `Frame::module_base_address`
@@ -292,9 +292,7 @@ impl BacktraceFrame {
292292
/// This function requires the `std` feature of the `backtrace` crate to be
293293
/// enabled, and the `std` feature is enabled by default.
294294
pub fn module_base_address(&self) -> Option<*mut c_void> {
295-
self.frame
296-
.module_base_address()
297-
.map(|addr| addr as *mut c_void)
295+
self.frame.module_base_address()
298296
}
299297

300298
/// Returns the list of symbols that this frame corresponds to.

src/dbghelp.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ macro_rules! dbghelp {
102102

103103
static mut DBGHELP: Dbghelp = Dbghelp {
104104
// Initially we haven't loaded the DLL
105-
dll: 0 as *mut _,
105+
dll: ptr::null_mut(),
106106
// Initially all functions are set to zero to say they need to be
107107
// dynamically loaded.
108108
$($name: 0,)*
@@ -122,7 +122,7 @@ macro_rules! dbghelp {
122122
}
123123
let lib = b"dbghelp.dll\0";
124124
unsafe {
125-
self.dll = LoadLibraryA(lib.as_ptr() as *const i8);
125+
self.dll = LoadLibraryA(lib.as_ptr().cast::<i8>());
126126
if self.dll.is_null() {
127127
Err(())
128128
} else {
@@ -149,7 +149,7 @@ macro_rules! dbghelp {
149149

150150
fn symbol(&self, symbol: &[u8]) -> Option<usize> {
151151
unsafe {
152-
match GetProcAddress(self.dll, symbol.as_ptr() as *const _) as usize {
152+
match GetProcAddress(self.dll, symbol.as_ptr().cast()) as usize {
153153
0 => None,
154154
n => Some(n),
155155
}
@@ -169,7 +169,7 @@ macro_rules! dbghelp {
169169

170170
pub fn dbghelp(&self) -> *mut Dbghelp {
171171
unsafe {
172-
&mut DBGHELP
172+
ptr::addr_of_mut!(DBGHELP)
173173
}
174174
}
175175
}

src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,12 @@ impl Drop for Bomb {
159159
mod lock {
160160
use std::boxed::Box;
161161
use std::cell::Cell;
162+
use std::ptr;
162163
use std::sync::{Mutex, MutexGuard, Once};
163164

164165
pub struct LockGuard(Option<MutexGuard<'static, ()>>);
165166

166-
static mut LOCK: *mut Mutex<()> = 0 as *mut _;
167+
static mut LOCK: *mut Mutex<()> = ptr::null_mut();
167168
static INIT: Once = Once::new();
168169
thread_local!(static LOCK_HELD: Cell<bool> = Cell::new(false));
169170

src/print/fuchsia.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ fn for_each_dso(mut visitor: &mut DsoPrinter<'_, '_>) {
359359
// location.
360360
let name_len = unsafe { libc::strlen(info.name) };
361361
let name_slice: &[u8] =
362-
unsafe { core::slice::from_raw_parts(info.name as *const u8, name_len) };
362+
unsafe { core::slice::from_raw_parts(info.name.cast::<u8>(), name_len) };
363363
let name = match core::str::from_utf8(name_slice) {
364364
Ok(name) => name,
365365
Err(_) => {

src/symbolize/dbghelp.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use core::char;
2323
use core::ffi::c_void;
2424
use core::marker;
2525
use core::mem;
26+
use core::ptr;
2627
use core::slice;
2728

2829
// Store an OsString on std so we can provide the symbol name and filename.
@@ -44,7 +45,7 @@ impl Symbol<'_> {
4445
}
4546

4647
pub fn addr(&self) -> Option<*mut c_void> {
47-
Some(self.addr as *mut _)
48+
Some(self.addr)
4849
}
4950

5051
pub fn filename_raw(&self) -> Option<BytesOrWideString<'_>> {
@@ -184,8 +185,7 @@ unsafe fn do_resolve(
184185
) {
185186
const SIZE: usize = 2 * MAX_SYM_NAME + mem::size_of::<SYMBOL_INFOW>();
186187
let mut data = Aligned8([0u8; SIZE]);
187-
let data = &mut data.0;
188-
let info = &mut *(data.as_mut_ptr() as *mut SYMBOL_INFOW);
188+
let info = &mut *data.0.as_mut_ptr().cast::<SYMBOL_INFOW>();
189189
info.MaxNameLen = MAX_SYM_NAME as ULONG;
190190
// the struct size in C. the value is different to
191191
// `size_of::<SYMBOL_INFOW>() - MAX_SYM_NAME + 1` (== 81)
@@ -200,7 +200,7 @@ unsafe fn do_resolve(
200200
// give a buffer of (MaxNameLen - 1) characters and set NameLen to
201201
// the real value.
202202
let name_len = ::core::cmp::min(info.NameLen as usize, info.MaxNameLen as usize - 1);
203-
let name_ptr = info.Name.as_ptr() as *const u16;
203+
let name_ptr = info.Name.as_ptr().cast::<u16>();
204204
let name = slice::from_raw_parts(name_ptr, name_len);
205205

206206
// Reencode the utf-16 symbol to utf-8 so we can use `SymbolName::new` like
@@ -222,7 +222,7 @@ unsafe fn do_resolve(
222222
}
223223
}
224224
}
225-
let name = &name_buffer[..name_len] as *const [u8];
225+
let name = ptr::addr_of!(name_buffer[..name_len]);
226226

227227
let mut line = mem::zeroed::<IMAGEHLP_LINEW64>();
228228
line.SizeOfStruct = mem::size_of::<IMAGEHLP_LINEW64>() as DWORD;
@@ -240,7 +240,7 @@ unsafe fn do_resolve(
240240

241241
let len = len as usize;
242242

243-
filename = Some(slice::from_raw_parts(base, len) as *const [u16]);
243+
filename = Some(ptr::from_ref(slice::from_raw_parts(base, len)));
244244
}
245245

246246
cb(&super::Symbol {

src/symbolize/gimli.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ pub unsafe fn resolve(what: ResolveWhat<'_>, cb: &mut dyn FnMut(&super::Symbol))
430430
};
431431

432432
Cache::with_global(|cache| {
433-
let (lib, addr) = match cache.avma_to_svma(addr as *const u8) {
433+
let (lib, addr) = match cache.avma_to_svma(addr.cast_const().cast::<u8>()) {
434434
Some(pair) => pair,
435435
None => return,
436436
};

src/symbolize/gimli/libs_aix.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub(super) fn native_libraries() -> Vec<Library> {
2020
loop {
2121
if libc::loadquery(
2222
libc::L_GETINFO,
23-
buffer.as_mut_ptr() as *mut libc::c_char,
23+
buffer.as_mut_ptr().cast::<libc::c_char>(),
2424
(mem::size_of::<libc::ld_info>() * buffer.len()) as u32,
2525
) != -1
2626
{
@@ -66,8 +66,10 @@ pub(super) fn native_libraries() -> Vec<Library> {
6666
if (*current).ldinfo_next == 0 {
6767
break;
6868
}
69-
current = (current as *mut libc::c_char).offset((*current).ldinfo_next as isize)
70-
as *mut libc::ld_info;
69+
current = current
70+
.cast::<libc::c_char>()
71+
.offset((*current).ldinfo_next as isize)
72+
.cast::<libc::ld_info>();
7173
}
7274
}
7375
return ret;

src/symbolize/gimli/libs_dl_iterate_phdr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use core::slice;
1212
pub(super) fn native_libraries() -> Vec<Library> {
1313
let mut ret = Vec::new();
1414
unsafe {
15-
libc::dl_iterate_phdr(Some(callback), &mut ret as *mut Vec<_> as *mut _);
15+
libc::dl_iterate_phdr(Some(callback), core::ptr::addr_of_mut!(ret).cast());
1616
}
1717
return ret;
1818
}
@@ -43,7 +43,7 @@ unsafe extern "C" fn callback(
4343
vec: *mut libc::c_void,
4444
) -> libc::c_int {
4545
let info = &*info;
46-
let libs = &mut *(vec as *mut Vec<Library>);
46+
let libs = &mut *vec.cast::<Vec<Library>>();
4747
let is_main_prog = info.dlpi_name.is_null() || *info.dlpi_name == 0;
4848
let name = if is_main_prog {
4949
// The man page for dl_iterate_phdr says that the first object visited by

src/symbolize/gimli/libs_illumos.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pub(super) fn native_libraries() -> Vec<Library> {
4141
if dlinfo(
4242
RTLD_SELF,
4343
RTLD_DI_LINKMAP,
44-
(&mut map) as *mut *const LinkMap as *mut libc::c_void,
44+
core::ptr::addr_of_mut!(map).cast::<libc::c_void>(),
4545
) != 0
4646
{
4747
return libs;

0 commit comments

Comments
 (0)