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:
Alex Crichton
2020-04-29 14:24:54 -05:00
committed by GitHub
parent 8ee8c322ae
commit d719ec7e1c
12 changed files with 214 additions and 222 deletions

View File

@@ -21,6 +21,12 @@ impl CodeMemoryEntry {
let registry = ManuallyDrop::new(UnwindRegistry::new(mmap.as_ptr() as usize));
Ok(Self { mmap, registry })
}
fn contains(&self, addr: usize) -> bool {
let start = self.mmap.as_ptr() as usize;
let end = start + self.mmap.len();
start <= addr && addr < end
}
}
impl Drop for CodeMemoryEntry {
@@ -236,4 +242,12 @@ impl CodeMemory {
Ok(())
}
/// Returns whether any published segment of this code memory contains
/// `addr`.
pub fn published_contains(&self, addr: usize) -> bool {
self.entries[..self.published]
.iter()
.any(|entry| entry.contains(addr))
}
}