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:
@@ -8,7 +8,7 @@ use crate::jit_int::GdbJitImageRegistration;
|
||||
use crate::memory::{DefaultMemoryCreator, RuntimeLinearMemory, RuntimeMemoryCreator};
|
||||
use crate::table::Table;
|
||||
use crate::traphandlers;
|
||||
use crate::traphandlers::{catch_traps, Trap};
|
||||
use crate::traphandlers::Trap;
|
||||
use crate::vmcontext::{
|
||||
VMBuiltinFunctionsArray, VMCallerCheckedAnyfunc, VMContext, VMFunctionBody, VMFunctionImport,
|
||||
VMGlobalDefinition, VMGlobalImport, VMInterrupts, VMMemoryDefinition, VMMemoryImport,
|
||||
@@ -19,7 +19,7 @@ use memoffset::offset_of;
|
||||
use more_asserts::assert_lt;
|
||||
use std::alloc::{self, Layout};
|
||||
use std::any::Any;
|
||||
use std::cell::{Cell, RefCell};
|
||||
use std::cell::RefCell;
|
||||
use std::collections::HashMap;
|
||||
use std::convert::TryFrom;
|
||||
use std::rc::Rc;
|
||||
@@ -33,34 +33,6 @@ use wasmtime_environ::wasm::{
|
||||
};
|
||||
use wasmtime_environ::{ir, DataInitializer, EntityIndex, Module, TableElements, VMOffsets};
|
||||
|
||||
cfg_if::cfg_if! {
|
||||
if #[cfg(unix)] {
|
||||
pub type SignalHandler = dyn Fn(libc::c_int, *const libc::siginfo_t, *const libc::c_void) -> bool;
|
||||
|
||||
impl InstanceHandle {
|
||||
/// Set a custom signal handler
|
||||
pub fn set_signal_handler<H>(&self, handler: H)
|
||||
where
|
||||
H: 'static + Fn(libc::c_int, *const libc::siginfo_t, *const libc::c_void) -> bool,
|
||||
{
|
||||
self.instance().signal_handler.set(Some(Box::new(handler)));
|
||||
}
|
||||
}
|
||||
} else if #[cfg(target_os = "windows")] {
|
||||
pub type SignalHandler = dyn Fn(winapi::um::winnt::PEXCEPTION_POINTERS) -> bool;
|
||||
|
||||
impl InstanceHandle {
|
||||
/// Set a custom signal handler
|
||||
pub fn set_signal_handler<H>(&self, handler: H)
|
||||
where
|
||||
H: 'static + Fn(winapi::um::winnt::PEXCEPTION_POINTERS) -> bool,
|
||||
{
|
||||
self.instance().signal_handler.set(Some(Box::new(handler)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A WebAssembly instance.
|
||||
///
|
||||
/// This is repr(C) to ensure that the vmctx field is last.
|
||||
@@ -99,9 +71,6 @@ pub(crate) struct Instance {
|
||||
/// Optional image of JIT'ed code for debugger registration.
|
||||
dbg_jit_registration: Option<Rc<GdbJitImageRegistration>>,
|
||||
|
||||
/// Handler run when `SIGBUS`, `SIGFPE`, `SIGILL`, or `SIGSEGV` are caught by the instance thread.
|
||||
pub(crate) signal_handler: Cell<Option<Box<SignalHandler>>>,
|
||||
|
||||
/// Externally allocated data indicating how this instance will be
|
||||
/// interrupted.
|
||||
pub(crate) interrupts: Arc<VMInterrupts>,
|
||||
@@ -377,58 +346,6 @@ impl Instance {
|
||||
&*self.host_state
|
||||
}
|
||||
|
||||
/// Invoke the WebAssembly start function of the instance, if one is present.
|
||||
fn invoke_start_function(&self, max_wasm_stack: usize) -> Result<(), InstantiationError> {
|
||||
let start_index = match self.module.start_func {
|
||||
Some(idx) => idx,
|
||||
None => return Ok(()),
|
||||
};
|
||||
|
||||
self.invoke_function_index(start_index, max_wasm_stack)
|
||||
.map_err(InstantiationError::StartTrap)
|
||||
}
|
||||
|
||||
fn invoke_function_index(
|
||||
&self,
|
||||
callee_index: FuncIndex,
|
||||
max_wasm_stack: usize,
|
||||
) -> Result<(), Trap> {
|
||||
let (callee_address, callee_vmctx) =
|
||||
match self.module.local.defined_func_index(callee_index) {
|
||||
Some(defined_index) => {
|
||||
let body = *self
|
||||
.finished_functions
|
||||
.get(defined_index)
|
||||
.expect("function index is out of bounds");
|
||||
(body as *const _, self.vmctx_ptr())
|
||||
}
|
||||
None => {
|
||||
assert_lt!(callee_index.index(), self.module.local.num_imported_funcs);
|
||||
let import = self.imported_function(callee_index);
|
||||
(import.body, import.vmctx)
|
||||
}
|
||||
};
|
||||
|
||||
self.invoke_function(callee_vmctx, callee_address, max_wasm_stack)
|
||||
}
|
||||
|
||||
fn invoke_function(
|
||||
&self,
|
||||
callee_vmctx: *mut VMContext,
|
||||
callee_address: *const VMFunctionBody,
|
||||
max_wasm_stack: usize,
|
||||
) -> Result<(), Trap> {
|
||||
// Make the call.
|
||||
unsafe {
|
||||
catch_traps(callee_vmctx, max_wasm_stack, || {
|
||||
mem::transmute::<
|
||||
*const VMFunctionBody,
|
||||
unsafe extern "C" fn(*mut VMContext, *mut VMContext),
|
||||
>(callee_address)(callee_vmctx, self.vmctx_ptr())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Return the offset from the vmctx pointer to its containing Instance.
|
||||
#[inline]
|
||||
pub(crate) fn vmctx_offset() -> isize {
|
||||
@@ -908,7 +825,6 @@ impl InstanceHandle {
|
||||
trampolines,
|
||||
dbg_jit_registration,
|
||||
host_state,
|
||||
signal_handler: Cell::new(None),
|
||||
interrupts,
|
||||
vmctx: VMContext {},
|
||||
};
|
||||
@@ -988,7 +904,6 @@ impl InstanceHandle {
|
||||
pub unsafe fn initialize(
|
||||
&self,
|
||||
is_bulk_memory: bool,
|
||||
max_wasm_stack: usize,
|
||||
data_initializers: &[DataInitializer<'_>],
|
||||
) -> Result<(), InstantiationError> {
|
||||
// Check initializer bounds before initializing anything. Only do this
|
||||
@@ -1005,8 +920,6 @@ impl InstanceHandle {
|
||||
initialize_tables(self.instance())?;
|
||||
initialize_memories(self.instance(), data_initializers)?;
|
||||
|
||||
// And finally, invoke the start function.
|
||||
self.instance().invoke_start_function(max_wasm_stack)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user