Implement interrupting wasm code, reimplement stack overflow (#1490)
* Implement interrupting wasm code, reimplement stack overflow This commit is a relatively large change for wasmtime with two main goals: * Primarily this enables interrupting executing wasm code with a trap, preventing infinite loops in wasm code. Note that resumption of the wasm code is not a goal of this commit. * Additionally this commit reimplements how we handle stack overflow to ensure that host functions always have a reasonable amount of stack to run on. This fixes an issue where we might longjmp out of a host function, skipping destructors. Lots of various odds and ends end up falling out in this commit once the two goals above were implemented. The strategy for implementing this was also lifted from Spidermonkey and existing functionality inside of Cranelift. I've tried to write up thorough documentation of how this all works in `crates/environ/src/cranelift.rs` where gnarly-ish bits are. A brief summary of how this works is that each function and each loop header now checks to see if they're interrupted. Interrupts and the stack overflow check are actually folded into one now, where function headers check to see if they've run out of stack and the sentinel value used to indicate an interrupt, checked in loop headers, tricks functions into thinking they're out of stack. An interrupt is basically just writing a value to a location which is read by JIT code. When interrupts are delivered and what triggers them has been left up to embedders of the `wasmtime` crate. The `wasmtime::Store` type has a method to acquire an `InterruptHandle`, where `InterruptHandle` is a `Send` and `Sync` type which can travel to other threads (or perhaps even a signal handler) to get notified from. It's intended that this provides a good degree of flexibility when interrupting wasm code. Note though that this does have a large caveat where interrupts don't work when you're interrupting host code, so if you've got a host import blocking for a long time an interrupt won't actually be received until the wasm starts running again. Some fallout included from this change is: * Unix signal handlers are no longer registered with `SA_ONSTACK`. Instead they run on the native stack the thread was already using. This is possible since stack overflow isn't handled by hitting the guard page, but rather it's explicitly checked for in wasm now. Native stack overflow will continue to abort the process as usual. * Unix sigaltstack management is now no longer necessary since we don't use it any more. * Windows no longer has any need to reset guard pages since we no longer try to recover from faults on guard pages. * On all targets probestack intrinsics are disabled since we use a different mechanism for catching stack overflow. * The C API has been updated with interrupts handles. An example has also been added which shows off how to interrupt a module. Closes #139 Closes #860 Closes #900 * Update comment about magical interrupt value * Store stack limit as a global value, not a closure * Run rustfmt * Handle review comments * Add a comment about SA_ONSTACK * Use `usize` for type of `INTERRUPTED` * Parse human-readable durations * Bring back sigaltstack handling Allows libstd to print out stack overflow on failure still. * Add parsing and emission of stack limit-via-preamble * Fix new example for new apis * Fix host segfault test in release mode * Fix new doc example
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
test compile
|
||||
set opt_level=speed_and_size
|
||||
set is_pic
|
||||
set enable_probestack=false
|
||||
target x86_64 haswell
|
||||
|
||||
; An empty function.
|
||||
@@ -244,7 +245,7 @@ block0(v0: i64):
|
||||
; nextln:
|
||||
; nextln: block0(v0: i64 [%rdi], v4: i64 [%rbp]):
|
||||
; nextln: v1 = copy v0
|
||||
; nextln: v2 = iadd_imm v1, 16
|
||||
; nextln: v2 = iadd_imm v1, 176
|
||||
; nextln: v3 = ifcmp_sp v2
|
||||
; nextln: trapif uge v3, stk_ovf
|
||||
; nextln: x86_push v4
|
||||
@@ -254,3 +255,60 @@ block0(v0: i64):
|
||||
; nextln: v5 = x86_pop.i64
|
||||
; nextln: return v5
|
||||
; nextln: }
|
||||
|
||||
function %big_stack_limit(i64 stack_limit) {
|
||||
ss0 = explicit_slot 40000
|
||||
block0(v0: i64):
|
||||
return
|
||||
}
|
||||
|
||||
; check: function %big_stack_limit(i64 stack_limit [%rdi], i64 fp [%rbp]) -> i64 fp [%rbp] fast {
|
||||
; nextln: ss0 = explicit_slot 40000, offset -40016
|
||||
; nextln: ss1 = incoming_arg 16, offset -16
|
||||
; nextln:
|
||||
; nextln: block0(v0: i64 [%rdi], v5: i64 [%rbp]):
|
||||
; nextln: v1 = copy v0
|
||||
; nextln: v2 = ifcmp_sp v1
|
||||
; nextln: trapif uge v2, stk_ovf
|
||||
; nextln: v3 = iadd_imm v1, 0x9c40
|
||||
; nextln: v4 = ifcmp_sp v3
|
||||
; nextln: trapif uge v4, stk_ovf
|
||||
; nextln: x86_push v5
|
||||
; nextln: copy_special %rsp -> %rbp
|
||||
; nextln: adjust_sp_down_imm 0x9c40
|
||||
; nextln: adjust_sp_up_imm 0x9c40
|
||||
; nextln: v6 = x86_pop.i64
|
||||
; nextln: return v6
|
||||
; nextln: }
|
||||
|
||||
function %limit_preamble(i64 vmctx) {
|
||||
gv0 = vmctx
|
||||
gv1 = load.i64 notrap aligned gv0
|
||||
gv2 = load.i64 notrap aligned gv1+4
|
||||
stack_limit = gv2
|
||||
ss0 = explicit_slot 20
|
||||
block0(v0: i64):
|
||||
return
|
||||
}
|
||||
|
||||
; check: function %limit_preamble(i64 vmctx [%rdi], i64 fp [%rbp]) -> i64 fp [%rbp] fast {
|
||||
; nextln: ss0 = explicit_slot 20, offset -36
|
||||
; nextln: ss1 = incoming_arg 16, offset -16
|
||||
; nextln: gv0 = vmctx
|
||||
; nextln: gv1 = load.i64 notrap aligned gv0
|
||||
; nextln: gv2 = load.i64 notrap aligned gv1+4
|
||||
; nextln: stack_limit = gv2
|
||||
; nextln:
|
||||
; nextln: block0(v0: i64 [%rdi], v5: i64 [%rbp]):
|
||||
; nextln: v1 = load.i64 notrap aligned v0
|
||||
; nextln: v2 = load.i64 notrap aligned v1+4
|
||||
; nextln: v3 = iadd_imm v2, 32
|
||||
; nextln: v4 = ifcmp_sp v3
|
||||
; nextln: trapif uge v4, stk_ovf
|
||||
; nextln: x86_push v5
|
||||
; nextln: copy_special %rsp -> %rbp
|
||||
; nextln: adjust_sp_down_imm 32
|
||||
; nextln: adjust_sp_up_imm 32
|
||||
; nextln: v6 = x86_pop.i64
|
||||
; nextln: return v6
|
||||
; nextln: }
|
||||
|
||||
Reference in New Issue
Block a user