Skip to content

Commit

Permalink
Use &raw from Rust 1.82
Browse files Browse the repository at this point in the history
This commit leverages bytecodealliance#9956 to use the `&raw` syntax for creating raw
pointers instead of using the `ptr::addr_of!` macro.
  • Loading branch information
alexcrichton committed Jan 9, 2025
1 parent a8c767a commit 5169686
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 25 deletions.
2 changes: 1 addition & 1 deletion crates/wasi-preview1-component-adapter/src/descriptors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ impl Descriptors {
if len >= (*table).len() {
return Err(wasi::ERRNO_NOMEM);
}
core::ptr::addr_of_mut!((*table)[len]).write(desc);
(&raw mut (*table)[len]).write(desc);
self.table_len.set(u16::try_from(len + 1).trapping_unwrap());
Ok(Fd::from(u32::try_from(len).trapping_unwrap()))
}
Expand Down
4 changes: 2 additions & 2 deletions crates/wasmtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,11 +334,11 @@ macro_rules! map_maybe_uninit {
use $crate::MaybeUninitExt;

let m: &mut core::mem::MaybeUninit<_> = $maybe_uninit;
// Note the usage of `addr_of_mut!` here which is an attempt to "stay
// Note the usage of `&raw` here which is an attempt to "stay
// safe" here where we never accidentally create `&mut T` where `T` is
// actually uninitialized, hopefully appeasing the Rust unsafe
// guidelines gods.
m.map(|p| core::ptr::addr_of_mut!((*p)$($field)*))
m.map(|p| &raw mut (*p)$($field)*)
}
}
})
Expand Down
2 changes: 1 addition & 1 deletion crates/wasmtime/src/runtime/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1834,7 +1834,7 @@ impl StoreOpaque {

Some(AsyncCx {
current_suspend: self.async_state.current_suspend.get(),
current_poll_cx: unsafe { core::ptr::addr_of_mut!((*poll_cx_box_ptr).future_context) },
current_poll_cx: unsafe { &raw mut (*poll_cx_box_ptr).future_context },
track_pkey_context_switch: self.pkey.is_some(),
})
}
Expand Down
2 changes: 1 addition & 1 deletion crates/wasmtime/src/runtime/vm/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ impl ComponentInstance {
}

fn vmctx(&self) -> *mut VMComponentContext {
let addr = core::ptr::addr_of!(self.vmctx);
let addr = &raw const self.vmctx;
Strict::with_addr(self.vmctx_self_reference.as_ptr(), Strict::addr(addr))
}

Expand Down
10 changes: 5 additions & 5 deletions crates/wasmtime/src/runtime/vm/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ impl Instance {
// today so the `sptr` crate is used. This crate provides the extension
// trait `Strict` but the method names conflict with the nightly methods
// so a different syntax is used to invoke methods here.
let addr = ptr::addr_of!(self.vmctx);
let addr = &raw const self.vmctx;
Strict::with_addr(self.vmctx_self_reference.as_ptr(), Strict::addr(addr))
}

Expand Down Expand Up @@ -1067,7 +1067,7 @@ impl Instance {

/// Get a locally-defined memory.
pub fn get_defined_memory(&mut self, index: DefinedMemoryIndex) -> *mut Memory {
ptr::addr_of_mut!(self.memories[index].1)
&raw mut self.memories[index].1
}

/// Do a `memory.copy`
Expand Down Expand Up @@ -1287,20 +1287,20 @@ impl Instance {
}
}

ptr::addr_of_mut!(self.tables[idx].1)
&raw mut self.tables[idx].1
}

/// Get a table by index regardless of whether it is locally-defined or an
/// imported, foreign table.
pub(crate) fn get_table(&mut self, table_index: TableIndex) -> *mut Table {
self.with_defined_table_index_and_instance(table_index, |idx, instance| {
ptr::addr_of_mut!(instance.tables[idx].1)
&raw mut instance.tables[idx].1
})
}

/// Get a locally-defined table.
pub(crate) fn get_defined_table(&mut self, index: DefinedTableIndex) -> *mut Table {
ptr::addr_of_mut!(self.tables[index].1)
&raw mut self.tables[index].1
}

pub(crate) fn with_defined_table_index_and_instance<R>(
Expand Down
7 changes: 3 additions & 4 deletions crates/wasmtime/src/runtime/vm/sys/unix/machports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ use mach2::thread_status::*;
use mach2::traps::*;
use std::io;
use std::mem;
use std::ptr::addr_of_mut;
use std::thread;
use wasmtime_environ::Trap;

Expand Down Expand Up @@ -85,7 +84,7 @@ impl TrapHandler {
// Allocate our WASMTIME_PORT and make sure that it can be sent to so we
// can receive exceptions.
let me = mach_task_self();
let kret = mach_port_allocate(me, MACH_PORT_RIGHT_RECEIVE, addr_of_mut!(WASMTIME_PORT));
let kret = mach_port_allocate(me, MACH_PORT_RIGHT_RECEIVE, &raw mut WASMTIME_PORT);
assert_eq!(kret, KERN_SUCCESS, "failed to allocate port");
let kret =
mach_port_insert_right(me, WASMTIME_PORT, WASMTIME_PORT, MACH_MSG_TYPE_MAKE_SEND);
Expand All @@ -103,7 +102,7 @@ impl TrapHandler {
handler.sa_flags = libc::SA_SIGINFO | libc::SA_ONSTACK;
handler.sa_sigaction = sigbus_handler as usize;
libc::sigemptyset(&mut handler.sa_mask);
if libc::sigaction(libc::SIGBUS, &handler, addr_of_mut!(PREV_SIGBUS)) != 0 {
if libc::sigaction(libc::SIGBUS, &handler, &raw mut PREV_SIGBUS) != 0 {
panic!(
"unable to install signal handler: {}",
io::Error::last_os_error(),
Expand Down Expand Up @@ -149,7 +148,7 @@ unsafe extern "C" fn sigbus_handler(
});

super::signals::delegate_signal_to_previous_handler(
addr_of_mut!(PREV_SIGBUS),
&raw mut PREV_SIGBUS,
signum,
siginfo,
context,
Expand Down
20 changes: 10 additions & 10 deletions crates/wasmtime/src/runtime/vm/sys/unix/signals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::runtime::vm::traphandlers::{tls, TrapRegisters, TrapTest};
use std::cell::RefCell;
use std::io;
use std::mem;
use std::ptr::{self, addr_of, addr_of_mut, null_mut};
use std::ptr::{self, null_mut};

/// Function which may handle custom signals while processing traps.
pub type SignalHandler =
Expand Down Expand Up @@ -67,22 +67,22 @@ impl TrapHandler {
}
}

unsafe fn foreach_handler(mut f: impl FnMut(*mut libc::sigaction, i32)) {
fn foreach_handler(mut f: impl FnMut(*mut libc::sigaction, i32)) {
// Allow handling OOB with signals on all architectures
f(addr_of_mut!(PREV_SIGSEGV), libc::SIGSEGV);
f(&raw mut PREV_SIGSEGV, libc::SIGSEGV);

// Handle `unreachable` instructions which execute `ud2` right now
f(addr_of_mut!(PREV_SIGILL), libc::SIGILL);
f(&raw mut PREV_SIGILL, libc::SIGILL);

// x86 and s390x use SIGFPE to report division by zero
if cfg!(target_arch = "x86_64") || cfg!(target_arch = "s390x") {
f(addr_of_mut!(PREV_SIGFPE), libc::SIGFPE);
f(&raw mut PREV_SIGFPE, libc::SIGFPE);
}

// Sometimes we need to handle SIGBUS too:
// - On Darwin, guard page accesses are raised as SIGBUS.
if cfg!(target_vendor = "apple") || cfg!(target_os = "freebsd") {
f(addr_of_mut!(PREV_SIGBUS), libc::SIGBUS);
f(&raw mut PREV_SIGBUS, libc::SIGBUS);
}

// TODO(#1980): x86-32, if we support it, will also need a SIGFPE handler.
Expand Down Expand Up @@ -132,10 +132,10 @@ unsafe extern "C" fn trap_handler(
context: *mut libc::c_void,
) {
let previous = match signum {
libc::SIGSEGV => addr_of!(PREV_SIGSEGV),
libc::SIGBUS => addr_of!(PREV_SIGBUS),
libc::SIGFPE => addr_of!(PREV_SIGFPE),
libc::SIGILL => addr_of!(PREV_SIGILL),
libc::SIGSEGV => &raw const PREV_SIGSEGV,
libc::SIGBUS => &raw const PREV_SIGBUS,
libc::SIGFPE => &raw const PREV_SIGFPE,
libc::SIGILL => &raw const PREV_SIGILL,
_ => panic!("unknown signal: {signum}"),
};
let handled = tls::with(|info| {
Expand Down
2 changes: 1 addition & 1 deletion examples/min-platform/embedding/src/allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ unsafe impl dlmalloc::Allocator for MyAllocator {
(ptr::null_mut(), 0, 0)
} else {
INITIAL_HEAP_ALLOCATED = true;
(ptr::addr_of_mut!(INITIAL_HEAP).cast(), INITIAL_HEAP_SIZE, 0)
((&raw mut INITIAL_HEAP).cast(), INITIAL_HEAP_SIZE, 0)
}
}
}
Expand Down

0 comments on commit 5169686

Please # to comment.