fitzgen requested alexcrichton for a review on PR #5049.
fitzgen opened PR #5049 from keep-existing-trap-backtraces to main:
This fixes some accidentally quadratic code where we would re-capture a Wasm
stack trace (takesO(n)time) every time we propagated a trap through a host
frame back to Wasm (can happenO(n)times). AndO(n) * O(n) = O(n^2), of
course. Whoops. After this commit, it trapping with a call stack that isn
frames deep of Wasm-to-host-to-Wasm calls just captures a single backtrace and
is therefore just a properO(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 betweenwasmtime::Trapand
wasmtime_runtime::Trap. We need to decide whether or not to capture a Wasm
backtrace insidewasmtime_runtimebut in order to determine whether to do that
or not we need to reflect on theanyhow::Errorand see if it is a
wasmtime::Trapthat already has a backtrace or not. This can't be done the
straightforward way because it would introduce a cyclic dependency between the
wasmtimeandwasmtime-runtimecrates. We can't merge those twoTrap
types-- at least not without effectively merging the wholewasmtimeand
wasmtime-runtimecrates together, which would be a good idea in a perfect
world but would be a ton of ocean boiling from where we currently are --
becausewasmtime::Trapdoes symbolication of stack traces which relies on
module registration information data that resides inside thewasmtimecrate
and therefore can't be moved intowasmtime-runtime. We resolve this problem by
adding a boolean towasmtime_runtime::raise_user_trapthat 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 inwasmtime
and therefore can do the reflection to determine whether the user trap already
has a backtrace or not. Phew!Fixes #5037
alexcrichton submitted PR review.
alexcrichton created PR review comment:
IIRC this has a
OnceCellinitialized internally here which ignores insertion of a backtrace over a prior backtrace. Could some debug assertions and such be added around here? For example can this debug assert thatneeds_backtraceis the same asbacktrace.is_some()? Additionallyrecord_backtracecoulddebug_assert!that insertion is always successful to.I'm not sure if we can actually trip the assertions but it would probably be good to be aware of any cases if there are.
alexcrichton created PR review comment:
This snippet is shared amongst a few places below as well, so couldd a helper function be added to the
wasmtimecrate which is theraise_trap(e: Error)of old?
alexcrichton submitted PR review.
fitzgen updated PR #5049 from keep-existing-trap-backtraces to main.
fitzgen merged PR #5049.
Last updated: Dec 06 2025 at 06:05 UTC