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::Error
throughout Wasmtime. This subsequently removes the need forTrap
to have the ability to represent all host-defined errors as it previously did. Consequently theFrom
implementations for any error into aTrap
have been removed here and the only embedder-defined way to create aTrap
is to useTrap::new
with a custom string.After this commit the distinction between a
Trap
and a host error is the wasm backtrace that it contains. Previously all errors in host functions would flow through aTrap
and get a wasm backtrace attached to them, but now this only happens if aTrap
itself 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::new
to 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
Trap
that 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
Trap
we'll capture a backtrace and then won't have aTrap
to attach it to. To avoid losing the contextual information this commit uses theError::context
method 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 aTrap
is 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::Error
throughout Wasmtime. This subsequently removes the need forTrap
to have the ability to represent all host-defined errors as it previously did. Consequently theFrom
implementations for any error into aTrap
have been removed here and the only embedder-defined way to create aTrap
is to useTrap::new
with a custom string.After this commit the distinction between a
Trap
and a host error is the wasm backtrace that it contains. Previously all errors in host functions would flow through aTrap
and get a wasm backtrace attached to them, but now this only happens if aTrap
itself 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::new
to 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
Trap
that 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
Trap
we'll capture a backtrace and then won't have aTrap
to attach it to. To avoid losing the contextual information this commit uses theError::context
method 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 aTrap
is 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
Trap
works 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::call
reutrnsResult<T>
now, in addition to the host-provided closure toFunc::wrap
.- Instances of
anyhow::Error
are threaded around as-is, so if the host returns a specific error that error pops out the other end.- The
Trap::i32_exit
function has been removed in favor of a concretewasi_common::I32Exit
error type- The
Trap::new
function has been removed in favor ofanyhow::Error
constructors likebail!
- The
Trap
type has been removed and replaced withTrapCode
. It's now just a simpleenum
- Wasm backtraces are modeled through a new
WasmBacktrace
type that's attached with.context(...)
toanyhow::Error
errors.This is a large refactoring of how traps work in Wasmtime where the main goal is to pare down the responsibility of
Trap
and instead letanyhow
do 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: Nov 22 2024 at 17:03 UTC