Don't try to handle non-wasmtime segfaults (#1577)
This commit fixes an issue in Wasmtime where Wasmtime would accidentally "handle" non-wasm segfaults while executing host imports of wasm modules. If a host import segfaulted then Wasmtime would recognize that wasm code is on the stack, so it'd longjmp out of the wasm code. This papers over real bugs though in host code and erroneously classified segfaults as wasm traps. The fix here was to add a check to our wasm signal handler for if the faulting address falls in JIT code itself. Actually threading through all the right information for that check to happen is a bit tricky, though, so this involved some refactoring: * A closure parameter to `catch_traps` was added. This closure is responsible for classifying addresses as whether or not they fall in JIT code. Anything returning `false` means that the trap won't get handled and we'll forward to the next signal handler. * To avoid passing tons of context all over the place, the start function is now no longer automatically invoked by `InstanceHandle`. This avoids the need for passing all sorts of trap-handling contextual information like the maximum stack size and "is this a jit address" closure. Instead creators of `InstanceHandle` (like wasmtime) are now responsible for invoking the start function. * To avoid excessive use of `transmute` with lifetimes since the traphandler state now has a lifetime the per-instance custom signal handler is now replaced with a per-store custom signal handler. I'm not entirely certain the purpose of the custom signal handler, though, so I'd look for feedback on this part. A new test has been added which ensures that if a host function segfaults we don't accidentally try to handle it, and instead we correctly report the segfault.
This commit is contained in:
@@ -178,7 +178,6 @@ macro_rules! getters {
|
||||
// of the closure. Pass the export in so that we can call it.
|
||||
let instance = self.instance.clone();
|
||||
let export = self.export.clone();
|
||||
let max_wasm_stack = self.store().engine().config().max_wasm_stack;
|
||||
|
||||
// ... and then once we've passed the typechecks we can hand out our
|
||||
// object since our `transmute` below should be safe!
|
||||
@@ -194,13 +193,9 @@ macro_rules! getters {
|
||||
>(export.address);
|
||||
let mut ret = None;
|
||||
$(let $args = $args.into_abi();)*
|
||||
wasmtime_runtime::catch_traps(export.vmctx, max_wasm_stack, || {
|
||||
catch_traps(export.vmctx, &instance.store, || {
|
||||
ret = Some(fnptr(export.vmctx, ptr::null_mut(), $($args,)*));
|
||||
}).map_err(Trap::from_jit)?;
|
||||
|
||||
// We're holding this handle just to ensure that the instance stays
|
||||
// live while we call into it.
|
||||
drop(&instance);
|
||||
})?;
|
||||
|
||||
Ok(ret.unwrap())
|
||||
}
|
||||
@@ -562,22 +557,14 @@ impl Func {
|
||||
}
|
||||
|
||||
// Call the trampoline.
|
||||
if let Err(error) = unsafe {
|
||||
wasmtime_runtime::catch_traps(
|
||||
catch_traps(self.export.vmctx, &self.instance.store, || unsafe {
|
||||
(self.trampoline)(
|
||||
self.export.vmctx,
|
||||
self.instance.store.engine().config().max_wasm_stack,
|
||||
|| {
|
||||
(self.trampoline)(
|
||||
self.export.vmctx,
|
||||
ptr::null_mut(),
|
||||
self.export.address,
|
||||
values_vec.as_mut_ptr(),
|
||||
)
|
||||
},
|
||||
ptr::null_mut(),
|
||||
self.export.address,
|
||||
values_vec.as_mut_ptr(),
|
||||
)
|
||||
} {
|
||||
return Err(Trap::from_jit(error).into());
|
||||
}
|
||||
})?;
|
||||
|
||||
// Load the return values out of `values_vec`.
|
||||
let mut results = Vec::with_capacity(my_ty.results().len());
|
||||
@@ -746,6 +733,24 @@ impl fmt::Debug for Func {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn catch_traps(
|
||||
vmctx: *mut VMContext,
|
||||
store: &Store,
|
||||
closure: impl FnMut(),
|
||||
) -> Result<(), Trap> {
|
||||
let signalhandler = store.signal_handler();
|
||||
unsafe {
|
||||
wasmtime_runtime::catch_traps(
|
||||
vmctx,
|
||||
store.engine().config().max_wasm_stack,
|
||||
|addr| store.compiler().is_in_jit_code(addr),
|
||||
signalhandler.as_deref(),
|
||||
closure,
|
||||
)
|
||||
.map_err(Trap::from_jit)
|
||||
}
|
||||
}
|
||||
|
||||
/// A trait implemented for types which can be arguments to closures passed to
|
||||
/// [`Func::wrap`] and friends.
|
||||
///
|
||||
|
||||
Reference in New Issue
Block a user