Don't re-capture backtraces when propagating traps through host frames (#5049)
* Add a benchmark for traps with many Wasm<-->host calls on the stack * Add a test for expected Wasm stack traces with Wasm<--host calls on the stack when we trap * Don't re-capture backtraces when propagating traps through host frames This fixes some accidentally quadratic code where we would re-capture a Wasm stack trace (takes `O(n)` time) every time we propagated a trap through a host frame back to Wasm (can happen `O(n)` times). And `O(n) * O(n) = O(n^2)`, of course. Whoops. After this commit, it trapping with a call stack that is `n` frames deep of Wasm-to-host-to-Wasm calls just captures a single backtrace and is therefore just a proper `O(n)` time operation, as it is intended to be. Now we explicitly track whether we need to capture a Wasm backtrace or not when raising a trap. This unfortunately isn't as straightforward as one might hope, however, because of the split between `wasmtime::Trap` and `wasmtime_runtime::Trap`. We need to decide whether or not to capture a Wasm backtrace inside `wasmtime_runtime` but in order to determine whether to do that or not we need to reflect on the `anyhow::Error` and see if it is a `wasmtime::Trap` that already has a backtrace or not. This can't be done the straightforward way because it would introduce a cyclic dependency between the `wasmtime` and `wasmtime-runtime` crates. We can't merge those two `Trap` types-- at least not without effectively merging the whole `wasmtime` and `wasmtime-runtime` crates together, which would be a good idea in a perfect world but would be a *ton* of ocean boiling from where we currently are -- because `wasmtime::Trap` does symbolication of stack traces which relies on module registration information data that resides inside the `wasmtime` crate and therefore can't be moved into `wasmtime-runtime`. We resolve this problem by adding a boolean to `wasmtime_runtime::raise_user_trap` that controls whether we should capture a Wasm backtrace or not, and then determine whether we need a backtrace or not at each of that function's call sites, which are in `wasmtime` and therefore can do the reflection to determine whether the user trap already has a backtrace or not. Phew! Fixes #5037 * debug assert that we don't record unnecessary backtraces for traps * Add assertions around `needs_backtrace` Unfortunately we can't do debug_assert_eq!(needs_backtrace, trap.inner.backtrace.get().is_some()); because `needs_backtrace` doesn't consider whether Wasm backtraces have been disabled via config. * Consolidate `needs_backtrace` calculation followed by calling `raise_user_trap` into one place
This commit is contained in:
@@ -9,6 +9,7 @@ fn bench_traps(c: &mut Criterion) {
|
||||
bench_multi_threaded_traps(c);
|
||||
bench_many_modules_registered_traps(c);
|
||||
bench_many_stack_frames_traps(c);
|
||||
bench_host_wasm_frames_traps(c);
|
||||
}
|
||||
|
||||
fn bench_multi_threaded_traps(c: &mut Criterion) {
|
||||
@@ -159,6 +160,66 @@ fn bench_many_stack_frames_traps(c: &mut Criterion) {
|
||||
group.finish()
|
||||
}
|
||||
|
||||
fn bench_host_wasm_frames_traps(c: &mut Criterion) {
|
||||
let mut group = c.benchmark_group("host-wasm-frames-traps");
|
||||
|
||||
let wat = r#"
|
||||
(module
|
||||
(import "" "" (func $host_func (param i32)))
|
||||
(func (export "f") (param i32)
|
||||
local.get 0
|
||||
i32.eqz
|
||||
if
|
||||
unreachable
|
||||
end
|
||||
|
||||
local.get 0
|
||||
i32.const 1
|
||||
i32.sub
|
||||
call $host_func
|
||||
)
|
||||
)
|
||||
"#;
|
||||
|
||||
let engine = Engine::default();
|
||||
let module = Module::new(&engine, wat).unwrap();
|
||||
|
||||
for num_stack_frames in vec![20, 40, 60, 80, 100, 120, 140, 160, 180, 200] {
|
||||
group.throughput(Throughput::Elements(num_stack_frames));
|
||||
group.bench_with_input(
|
||||
BenchmarkId::from_parameter(num_stack_frames),
|
||||
&num_stack_frames,
|
||||
|b, &num_stack_frames| {
|
||||
b.iter_custom(|iters| {
|
||||
let mut store = Store::new(&engine, ());
|
||||
let host_func = Func::new(
|
||||
&mut store,
|
||||
FuncType::new(vec![ValType::I32], vec![]),
|
||||
|mut caller, args, _results| {
|
||||
let f = caller.get_export("f").unwrap();
|
||||
let f = f.into_func().unwrap();
|
||||
f.call(caller, args, &mut [])?;
|
||||
Ok(())
|
||||
},
|
||||
);
|
||||
let instance = Instance::new(&mut store, &module, &[host_func.into()]).unwrap();
|
||||
let f = instance
|
||||
.get_typed_func::<(i32,), (), _>(&mut store, "f")
|
||||
.unwrap();
|
||||
|
||||
let start = std::time::Instant::now();
|
||||
for _ in 0..iters {
|
||||
assert!(f.call(&mut store, (num_stack_frames as i32,)).is_err());
|
||||
}
|
||||
start.elapsed()
|
||||
});
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
group.finish()
|
||||
}
|
||||
|
||||
fn module(engine: &Engine, num_funcs: u64) -> Result<Module> {
|
||||
let mut wat = String::new();
|
||||
wat.push_str("(module\n");
|
||||
|
||||
Reference in New Issue
Block a user