alexcrichton opened PR #5149 from refactor-trap to main:
This commit refactors how errors are modeled when returned from host functions and additionally refactors how custom errors work with
Trap. At a high level functions in Wasmtime that previously worked withResult<T, Trap>now work withResult<T>instead where the error isanyhow::Error. This includes functions such as:
- Host-defined functions in a
Linker<T>TypedFunc::call- Host-related callbacks like call hooks
Errors are now modeled primarily as
anyhow::Errorthroughout Wasmtime. This subsequently removes the need forTrapto have the ability to represent all host-defined errors as it previously did. Consequently theFromimplementations for any error into aTraphave been removed here and the only embedder-defined way to create aTrapis to useTrap::newwith a custom string.After this commit the distinction between a
Trapand a host error is the wasm backtrace that it contains. Previously all errors in host functions would flow through aTrapand get a wasm backtrace attached to them, but now this only happens if aTrapitself is created meaning that arbitrary host-defined errors flowing from a host import to the other side won't get backtraces attached. Some internals of Wasmtime itself were updated or preserved to useTrap::newto capture a backtrace where it seemed useful, such as when fuel runs out.The main motivation for this commit is that it now enables hosts to thread a concrete error type from a host function all the way through to where a wasm function was invoked. Previously this could not be done since the host error was wrapped in a
Trapthat didn't provide the ability to get at the internals.A consequence of this commit is that when a host error is returned that isn't a
Trapwe'll capture a backtrace and then won't have aTrapto attach it to. To avoid losing the contextual information this commit uses theError::contextmethod to attach the backtrace as contextual information to ensure that the backtrace is itself not lost.This is a breaking change for likely all users of Wasmtime, but it's hoped to be a relatively minor change to workaround. Most use cases can likely change
-> Result<T, Trap>to-> Result<T>and otherwise explicit creation of aTrapis largely no longer necessary.<!--
Please ensure that the following steps are all taken care of before submitting
the PR.
[ ] This has been discussed in issue #..., or if not, please tell us why
here.[ ] A short description of what this does, why it is needed; if the
description becomes long, the matter should probably be discussed in an issue
first.[ ] This PR contains test cases, if meaningful.
- [ ] A reviewer from the core maintainer team has been assigned for this PR.
If you don't know who could review this, please indicate so. The list of
suggested reviewers on the right can help you.Please ensure all communication adheres to the code of conduct.
-->
alexcrichton requested pchickey for a review on PR #5149.
alexcrichton updated PR #5149 from refactor-trap to main.
alexcrichton updated PR #5149 from refactor-trap to main.
alexcrichton updated PR #5149 from refactor-trap to main.
alexcrichton updated PR #5149 from refactor-trap to main.
pchickey submitted PR review.
alexcrichton updated PR #5149 from refactor-trap to main.
alexcrichton updated PR #5149 from refactor-trap to main.
alexcrichton edited PR #5149 from refactor-trap to main:
This commit refactors how errors are modeled when returned from host functions and additionally refactors how custom errors work with
Trap. At a high level functions in Wasmtime that previously worked withResult<T, Trap>now work withResult<T>instead where the error isanyhow::Error. This includes functions such as:
- Host-defined functions in a
Linker<T>TypedFunc::call- Host-related callbacks like call hooks
Errors are now modeled primarily as
anyhow::Errorthroughout Wasmtime. This subsequently removes the need forTrapto have the ability to represent all host-defined errors as it previously did. Consequently theFromimplementations for any error into aTraphave been removed here and the only embedder-defined way to create aTrapis to useTrap::newwith a custom string.After this commit the distinction between a
Trapand a host error is the wasm backtrace that it contains. Previously all errors in host functions would flow through aTrapand get a wasm backtrace attached to them, but now this only happens if aTrapitself is created meaning that arbitrary host-defined errors flowing from a host import to the other side won't get backtraces attached. Some internals of Wasmtime itself were updated or preserved to useTrap::newto capture a backtrace where it seemed useful, such as when fuel runs out.The main motivation for this commit is that it now enables hosts to thread a concrete error type from a host function all the way through to where a wasm function was invoked. Previously this could not be done since the host error was wrapped in a
Trapthat didn't provide the ability to get at the internals.A consequence of this commit is that when a host error is returned that isn't a
Trapwe'll capture a backtrace and then won't have aTrapto attach it to. To avoid losing the contextual information this commit uses theError::contextmethod to attach the backtrace as contextual information to ensure that the backtrace is itself not lost.This is a breaking change for likely all users of Wasmtime, but it's hoped to be a relatively minor change to workaround. Most use cases can likely change
-> Result<T, Trap>to-> Result<T>and otherwise explicit creation of aTrapis largely no longer necessary.<!--
Please ensure that the following steps are all taken care of before submitting
the PR.
[ ] This has been discussed in issue #..., or if not, please tell us why
here.[ ] A short description of what this does, why it is needed; if the
description becomes long, the matter should probably be discussed in an issue
first.[ ] This PR contains test cases, if meaningful.
- [ ] A reviewer from the core maintainer team has been assigned for this PR.
If you don't know who could review this, please indicate so. The list of
suggested reviewers on the right can help you.Please ensure all communication adheres to the code of conduct.
-->
alexcrichton edited PR #5149 from refactor-trap to main:
This series of commits is a large refactoring of how
Trapworks and is used in Wasmtime. Individual commits have more details but at a high-level the changes implemented here are:
- All functions in Wasmtime now work with
Result<T>instead of some working withResult<T, Trap>. For exampleTypedFunc::callreutrnsResult<T>now, in addition to the host-provided closure toFunc::wrap.- Instances of
anyhow::Errorare threaded around as-is, so if the host returns a specific error that error pops out the other end.- The
Trap::i32_exitfunction has been removed in favor of a concretewasi_common::I32Exiterror type- The
Trap::newfunction has been removed in favor ofanyhow::Errorconstructors likebail!- The
Traptype has been removed and replaced withTrapCode. It's now just a simpleenum- Wasm backtraces are modeled through a new
WasmBacktracetype that's attached with.context(...)toanyhow::Errorerrors.This is a large refactoring of how traps work in Wasmtime where the main goal is to pare down the responsibility of
Trapand instead letanyhowdo all the heavy lifting. Embedders which handle various kinds of traps various ways will need to be adjusted accordingly.
pchickey submitted PR review.
alexcrichton updated PR #5149 from refactor-trap to main.
alexcrichton has enabled auto merge for PR #5149.
alexcrichton updated PR #5149 from refactor-trap to main.
alexcrichton merged PR #5149.
Last updated: Dec 06 2025 at 06:05 UTC