Skip to content

Commit

Permalink
Simplify signature of catch_traps in Wasmtime (#9674)
Browse files Browse the repository at this point in the history
* Simplify signature of `catch_traps` in Wasmtime

Now that `wasmtime-runtime` and `wasmtime` are merged there's no need to
pass all arguments individually, it's possible to forward along the
whole `Store`.

* Fix miri build

* Run the full test suite

prtest:full

* Fix windows-specific compiler warning
  • Loading branch information
alexcrichton authored Nov 26, 2024
1 parent 0e3e863 commit 2b3fe80
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 37 deletions.
9 changes: 1 addition & 8 deletions crates/wasmtime/src/runtime/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1601,14 +1601,7 @@ pub(crate) fn invoke_wasm_and_catch_traps<T>(
exit_wasm(store, exit);
return Err(trap);
}
let result = crate::runtime::vm::catch_traps(
store.0.signal_handler(),
store.0.engine().config().wasm_backtrace,
store.0.engine().config().coredump_on_trap,
store.0.async_guard_range(),
store.0.default_caller(),
closure,
);
let result = crate::runtime::vm::catch_traps(store, closure);
exit_wasm(store, exit);
store.0.call_hook(CallHook::ReturningFromWasm)?;
result.map_err(|t| crate::trap::from_runtime_box(store.0, t))
Expand Down
46 changes: 17 additions & 29 deletions crates/wasmtime/src/runtime/vm/traphandlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ mod signals;
pub use self::signals::*;

use crate::prelude::*;
use crate::runtime::store::StoreOpaque;
use crate::runtime::vm::sys::traphandlers;
use crate::runtime::vm::{Instance, VMContext, VMRuntimeLimits};
use crate::WasmBacktrace;
use crate::{StoreContextMut, WasmBacktrace};
use core::cell::{Cell, UnsafeCell};
use core::mem::MaybeUninit;
use core::ops::Range;
Expand Down Expand Up @@ -164,27 +165,16 @@ impl From<wasmtime_environ::Trap> for TrapReason {
/// returning them as a `Result`.
///
/// Highly unsafe since `closure` won't have any dtors run.
pub unsafe fn catch_traps<F>(
signal_handler: Option<*const SignalHandler>,
capture_backtrace: bool,
capture_coredump: bool,
async_guard_range: Range<*mut u8>,
caller: *mut VMContext,
pub unsafe fn catch_traps<T, F>(
store: &mut StoreContextMut<'_, T>,
mut closure: F,
) -> Result<(), Box<Trap>>
where
F: FnMut(*mut VMContext),
{
let limits = Instance::from_vmctx(caller, |i| i.runtime_limits());

let result = CallThreadState::new(
signal_handler,
capture_backtrace,
capture_coredump,
*limits,
async_guard_range,
)
.with(|cx| {
let caller = store.0.default_caller();

let result = CallThreadState::new(store.0, caller).with(|cx| {
traphandlers::wasmtime_setjmp(
cx.jmp_buf.as_ptr(),
call_closure::<F>,
Expand Down Expand Up @@ -260,26 +250,24 @@ mod call_thread_state {

impl CallThreadState {
#[inline]
pub(super) fn new(
signal_handler: Option<*const SignalHandler>,
capture_backtrace: bool,
capture_coredump: bool,
limits: *const VMRuntimeLimits,
async_guard_range: Range<*mut u8>,
) -> CallThreadState {
let _ = (capture_coredump, signal_handler, &async_guard_range);
pub(super) fn new(store: &mut StoreOpaque, caller: *mut VMContext) -> CallThreadState {
let limits = unsafe { *Instance::from_vmctx(caller, |i| i.runtime_limits()) };

// Don't try to plumb #[cfg] everywhere for this field, just pretend
// we're using it on miri/windows to silence compiler warnings.
let _: Range<_> = store.async_guard_range();

CallThreadState {
unwind: UnsafeCell::new(MaybeUninit::uninit()),
jmp_buf: Cell::new(ptr::null()),
#[cfg(all(feature = "signals-based-traps", not(miri)))]
signal_handler,
capture_backtrace,
signal_handler: store.signal_handler(),
capture_backtrace: store.engine().config().wasm_backtrace,
#[cfg(feature = "coredump")]
capture_coredump,
capture_coredump: store.engine().config().coredump_on_trap,
limits,
#[cfg(all(feature = "signals-based-traps", unix, not(miri)))]
async_guard_range,
async_guard_range: store.async_guard_range(),
prev: Cell::new(ptr::null()),
old_last_wasm_exit_fp: Cell::new(unsafe { *(*limits).last_wasm_exit_fp.get() }),
old_last_wasm_exit_pc: Cell::new(unsafe { *(*limits).last_wasm_exit_pc.get() }),
Expand Down

0 comments on commit 2b3fe80

Please sign in to comment.