Skip to content

Commit 2b3fe80

Browse files
authored
Simplify signature of catch_traps in Wasmtime (#9674)
* 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
1 parent 0e3e863 commit 2b3fe80

File tree

2 files changed

+18
-37
lines changed

2 files changed

+18
-37
lines changed

crates/wasmtime/src/runtime/func.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1601,14 +1601,7 @@ pub(crate) fn invoke_wasm_and_catch_traps<T>(
16011601
exit_wasm(store, exit);
16021602
return Err(trap);
16031603
}
1604-
let result = crate::runtime::vm::catch_traps(
1605-
store.0.signal_handler(),
1606-
store.0.engine().config().wasm_backtrace,
1607-
store.0.engine().config().coredump_on_trap,
1608-
store.0.async_guard_range(),
1609-
store.0.default_caller(),
1610-
closure,
1611-
);
1604+
let result = crate::runtime::vm::catch_traps(store, closure);
16121605
exit_wasm(store, exit);
16131606
store.0.call_hook(CallHook::ReturningFromWasm)?;
16141607
result.map_err(|t| crate::trap::from_runtime_box(store.0, t))

crates/wasmtime/src/runtime/vm/traphandlers.rs

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ mod signals;
1616
pub use self::signals::*;
1717

1818
use crate::prelude::*;
19+
use crate::runtime::store::StoreOpaque;
1920
use crate::runtime::vm::sys::traphandlers;
2021
use crate::runtime::vm::{Instance, VMContext, VMRuntimeLimits};
21-
use crate::WasmBacktrace;
22+
use crate::{StoreContextMut, WasmBacktrace};
2223
use core::cell::{Cell, UnsafeCell};
2324
use core::mem::MaybeUninit;
2425
use core::ops::Range;
@@ -164,27 +165,16 @@ impl From<wasmtime_environ::Trap> for TrapReason {
164165
/// returning them as a `Result`.
165166
///
166167
/// Highly unsafe since `closure` won't have any dtors run.
167-
pub unsafe fn catch_traps<F>(
168-
signal_handler: Option<*const SignalHandler>,
169-
capture_backtrace: bool,
170-
capture_coredump: bool,
171-
async_guard_range: Range<*mut u8>,
172-
caller: *mut VMContext,
168+
pub unsafe fn catch_traps<T, F>(
169+
store: &mut StoreContextMut<'_, T>,
173170
mut closure: F,
174171
) -> Result<(), Box<Trap>>
175172
where
176173
F: FnMut(*mut VMContext),
177174
{
178-
let limits = Instance::from_vmctx(caller, |i| i.runtime_limits());
179-
180-
let result = CallThreadState::new(
181-
signal_handler,
182-
capture_backtrace,
183-
capture_coredump,
184-
*limits,
185-
async_guard_range,
186-
)
187-
.with(|cx| {
175+
let caller = store.0.default_caller();
176+
177+
let result = CallThreadState::new(store.0, caller).with(|cx| {
188178
traphandlers::wasmtime_setjmp(
189179
cx.jmp_buf.as_ptr(),
190180
call_closure::<F>,
@@ -260,26 +250,24 @@ mod call_thread_state {
260250

261251
impl CallThreadState {
262252
#[inline]
263-
pub(super) fn new(
264-
signal_handler: Option<*const SignalHandler>,
265-
capture_backtrace: bool,
266-
capture_coredump: bool,
267-
limits: *const VMRuntimeLimits,
268-
async_guard_range: Range<*mut u8>,
269-
) -> CallThreadState {
270-
let _ = (capture_coredump, signal_handler, &async_guard_range);
253+
pub(super) fn new(store: &mut StoreOpaque, caller: *mut VMContext) -> CallThreadState {
254+
let limits = unsafe { *Instance::from_vmctx(caller, |i| i.runtime_limits()) };
255+
256+
// Don't try to plumb #[cfg] everywhere for this field, just pretend
257+
// we're using it on miri/windows to silence compiler warnings.
258+
let _: Range<_> = store.async_guard_range();
271259

272260
CallThreadState {
273261
unwind: UnsafeCell::new(MaybeUninit::uninit()),
274262
jmp_buf: Cell::new(ptr::null()),
275263
#[cfg(all(feature = "signals-based-traps", not(miri)))]
276-
signal_handler,
277-
capture_backtrace,
264+
signal_handler: store.signal_handler(),
265+
capture_backtrace: store.engine().config().wasm_backtrace,
278266
#[cfg(feature = "coredump")]
279-
capture_coredump,
267+
capture_coredump: store.engine().config().coredump_on_trap,
280268
limits,
281269
#[cfg(all(feature = "signals-based-traps", unix, not(miri)))]
282-
async_guard_range,
270+
async_guard_range: store.async_guard_range(),
283271
prev: Cell::new(ptr::null()),
284272
old_last_wasm_exit_fp: Cell::new(unsafe { *(*limits).last_wasm_exit_fp.get() }),
285273
old_last_wasm_exit_pc: Cell::new(unsafe { *(*limits).last_wasm_exit_pc.get() }),

0 commit comments

Comments
 (0)