Stream: git-wasmtime

Topic: wasmtime / PR #5149 Return `anyhow::Error` from host func...


view this post on Zulip Wasmtime GitHub notifications bot (Oct 28 2022 at 16:21):

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 with Result<T, Trap> now work with Result<T> instead where the error is anyhow::Error. This includes functions such as:

Errors are now modeled primarily as anyhow::Error throughout Wasmtime. This subsequently removes the need for Trap to have the ability to represent all host-defined errors as it previously did. Consequently the From implementations for any error into a Trap have been removed here and the only embedder-defined way to create a Trap is to use Trap::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 a Trap and get a wasm backtrace attached to them, but now this only happens if a Trap 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 use Trap::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 a Trap to attach it to. To avoid losing the contextual information this commit uses the Error::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 a Trap is largely no longer necessary.

<!--

Please ensure that the following steps are all taken care of before submitting
the PR.

Please ensure all communication adheres to the code of conduct.
-->

view this post on Zulip Wasmtime GitHub notifications bot (Oct 28 2022 at 16:21):

alexcrichton requested pchickey for a review on PR #5149.

view this post on Zulip Wasmtime GitHub notifications bot (Oct 31 2022 at 15:19):

alexcrichton updated PR #5149 from refactor-trap to main.

view this post on Zulip Wasmtime GitHub notifications bot (Oct 31 2022 at 16:20):

alexcrichton updated PR #5149 from refactor-trap to main.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 01 2022 at 14:14):

alexcrichton updated PR #5149 from refactor-trap to main.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 01 2022 at 17:56):

alexcrichton updated PR #5149 from refactor-trap to main.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 01 2022 at 18:09):

pchickey submitted PR review.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 01 2022 at 19:15):

alexcrichton updated PR #5149 from refactor-trap to main.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 01 2022 at 19:18):

alexcrichton updated PR #5149 from refactor-trap to main.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 01 2022 at 19:22):

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 with Result<T, Trap> now work with Result<T> instead where the error is anyhow::Error. This includes functions such as:

Errors are now modeled primarily as anyhow::Error throughout Wasmtime. This subsequently removes the need for Trap to have the ability to represent all host-defined errors as it previously did. Consequently the From implementations for any error into a Trap have been removed here and the only embedder-defined way to create a Trap is to use Trap::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 a Trap and get a wasm backtrace attached to them, but now this only happens if a Trap 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 use Trap::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 a Trap to attach it to. To avoid losing the contextual information this commit uses the Error::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 a Trap is largely no longer necessary.

<!--

Please ensure that the following steps are all taken care of before submitting
the PR.

Please ensure all communication adheres to the code of conduct.
-->

view this post on Zulip Wasmtime GitHub notifications bot (Nov 01 2022 at 19:26):

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:

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 let anyhow do all the heavy lifting. Embedders which handle various kinds of traps various ways will need to be adjusted accordingly.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 01 2022 at 21:09):

pchickey submitted PR review.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 02 2022 at 14:29):

alexcrichton updated PR #5149 from refactor-trap to main.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 02 2022 at 14:30):

alexcrichton has enabled auto merge for PR #5149.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 02 2022 at 14:49):

alexcrichton updated PR #5149 from refactor-trap to main.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 02 2022 at 16:29):

alexcrichton merged PR #5149.


Last updated: Nov 22 2024 at 17:03 UTC