Delete historical interruptable support in Wasmtime (#3925)
* Delete historical interruptable support in Wasmtime This commit removes the `Config::interruptable` configuration along with the `InterruptHandle` type from the `wasmtime` crate. The original support for adding interruption to WebAssembly was added pretty early on in the history of Wasmtime when there was no other method to prevent an infinite loop from the host. Nowadays, however, there are alternative methods for interruption such as fuel or epoch-based interruption. One of the major downsides of `Config::interruptable` is that even when it's not enabled it forces an atomic swap to happen when entering WebAssembly code. This technically could be a non-atomic swap if the configuration option isn't enabled but that produces even more branch-y code on entry into WebAssembly which is already something we try to optimize. Calling into WebAssembly is on the order of a dozens of nanoseconds at this time and an atomic swap, even uncontended, can add up to 5ns on some platforms. The main goal of this PR is to remove this atomic swap on entry into WebAssembly. This is done by removing the `Config::interruptable` field entirely, moving all existing consumers to epochs instead which are suitable for the same purposes. This means that the stack overflow check is no longer entangled with the interruption check and perhaps one day we could continue to optimize that further as well. Some consequences of this change are: * Epochs are now the only method of remote-thread interruption. * There are no more Wasmtime traps that produces the `Interrupted` trap code, although we may wish to move future traps to this so I left it in place. * The C API support for interrupt handles was also removed and bindings for epoch methods were added. * Function-entry checks for interruption are a tiny bit less efficient since one check is performed for the stack limit and a second is performed for the epoch as opposed to the `Config::interruptable` style of bundling the stack limit and the interrupt check in one. It's expected though that this is likely to not really be measurable. * The old `VMInterrupts` structure is renamed to `VMRuntimeLimits`.
This commit is contained in:
@@ -1,14 +1,13 @@
|
||||
//! WebAssembly trap handling, which is built on top of the lower-level
|
||||
//! signalhandling mechanisms.
|
||||
|
||||
use crate::{VMContext, VMInterrupts};
|
||||
use crate::VMContext;
|
||||
use anyhow::Error;
|
||||
use backtrace::Backtrace;
|
||||
use std::any::Any;
|
||||
use std::cell::{Cell, UnsafeCell};
|
||||
use std::mem::MaybeUninit;
|
||||
use std::ptr;
|
||||
use std::sync::atomic::Ordering::SeqCst;
|
||||
use std::sync::Once;
|
||||
use wasmtime_environ::TrapCode;
|
||||
|
||||
@@ -122,10 +121,6 @@ pub enum Trap {
|
||||
pc: usize,
|
||||
/// Native stack backtrace at the time the trap occurred
|
||||
backtrace: Backtrace,
|
||||
/// An indicator for whether this may have been a trap generated from an
|
||||
/// interrupt, used for switching what would otherwise be a stack
|
||||
/// overflow trap to be an interrupt trap.
|
||||
maybe_interrupted: bool,
|
||||
},
|
||||
|
||||
/// A trap raised from a wasm libcall
|
||||
@@ -169,7 +164,6 @@ impl Trap {
|
||||
///
|
||||
/// Highly unsafe since `closure` won't have any dtors run.
|
||||
pub unsafe fn catch_traps<'a, F>(
|
||||
vminterrupts: *mut VMInterrupts,
|
||||
signal_handler: Option<*const SignalHandler<'static>>,
|
||||
callee: *mut VMContext,
|
||||
mut closure: F,
|
||||
@@ -177,7 +171,7 @@ pub unsafe fn catch_traps<'a, F>(
|
||||
where
|
||||
F: FnMut(*mut VMContext),
|
||||
{
|
||||
return CallThreadState::new(signal_handler).with(vminterrupts, |cx| {
|
||||
return CallThreadState::new(signal_handler).with(|cx| {
|
||||
wasmtime_setjmp(
|
||||
cx.jmp_buf.as_ptr(),
|
||||
call_closure::<F>,
|
||||
@@ -223,33 +217,21 @@ impl CallThreadState {
|
||||
}
|
||||
}
|
||||
|
||||
fn with(
|
||||
self,
|
||||
interrupts: *mut VMInterrupts,
|
||||
closure: impl FnOnce(&CallThreadState) -> i32,
|
||||
) -> Result<(), Box<Trap>> {
|
||||
fn with(self, closure: impl FnOnce(&CallThreadState) -> i32) -> Result<(), Box<Trap>> {
|
||||
let ret = tls::set(&self, || closure(&self))?;
|
||||
if ret != 0 {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(unsafe { self.read_trap(interrupts) })
|
||||
Err(unsafe { self.read_trap() })
|
||||
}
|
||||
}
|
||||
|
||||
#[cold]
|
||||
unsafe fn read_trap(&self, interrupts: *mut VMInterrupts) -> Box<Trap> {
|
||||
unsafe fn read_trap(&self) -> Box<Trap> {
|
||||
Box::new(match (*self.unwind.get()).as_ptr().read() {
|
||||
UnwindReason::UserTrap(data) => Trap::User(data),
|
||||
UnwindReason::LibTrap(trap) => trap,
|
||||
UnwindReason::JitTrap { backtrace, pc } => {
|
||||
let maybe_interrupted =
|
||||
(*interrupts).stack_limit.load(SeqCst) == wasmtime_environ::INTERRUPTED;
|
||||
Trap::Jit {
|
||||
pc,
|
||||
backtrace,
|
||||
maybe_interrupted,
|
||||
}
|
||||
}
|
||||
UnwindReason::JitTrap { backtrace, pc } => Trap::Jit { pc, backtrace },
|
||||
UnwindReason::Panic(panic) => std::panic::resume_unwind(panic),
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user