wasm_error (wasm v0.16.0)
View SourceStructured errors and traps.
Read this when you are deciding what to match on. Every failure you get back from the runtime is one of five classes, matching the distinctions the WebAssembly specification test suite draws:
malformed- the binary could not be decoded (assert_malformed)invalid- it decoded but failed validation (assert_invalid)link- instantiation failed (assert_unlinkable)trap- execution trapped (assert_trap)exhaustion- a resource limit was hit (assert_exhaustion) Match onclassandkind; both are atoms and neither is derived from module data.msgcarries the canonical specification message text, so the spec driver can compare it against the expected failure, andctxcarries enough to diagnose a module: which function, which byte offset, and the enclosing block path. Useformat/1for logs.
What it promises
Nothing a guest supplies makes a call raise: not the module bytes, not what
the guest does while it runs, not the strings it passes to WASI. Each of those
failures comes back as {error, Error}, with Error one of the maps above.
The promise covers the guest. A wrong Erlang type in your own call, such as an
atom for the module bytes, raises as any Erlang function does. Failures are
values at the guest
boundary
lists those cases, and the few failures that are values but not maps.
How the library uses it
Inside the library the decoder and validator signal by throw/1 rather than
threading {ok, _} | {error, _} through every combinator, which keeps the hot
paths free of result-tuple allocation. Three steps turn that into the promise:
- Raise. Code that finds a failure calls the constructor for its class:
malformed/2,3,invalid/2,3,link_error/2,3,trap/1,2orexhaustion/1,2. Each throws{wasm_error, Error}and does not return. - Capture. Every public entry point runs its work inside
capture/1orcapture/2, which turns that throw back into{error, Error}. Any other exception, a bug in this library, becomes{error, Error}too, with classmalformed, kindinternal, and the exception and its stacktrace inctx. A WebAssembly exception in flight ({wasm_exception, _}) is not caught: it is unwinding to a handler in an outer instance. - Add context.
capture/2merges a context map into the error it catches, andadd_context/2does the same to an error already in hand. A key the error already has wins, because it was recorded closer to the failure.
Result = wasm_error:capture(
fun() -> wasm_error:malformed(unexpected_end, ~"unexpected end") end,
#{offset => 12}),
{error, #{class := malformed, ctx := #{offset := 12}}} = Result.is_error/1 says whether a term is one of these errors, and format/1
renders one on a line.
Summary
Functions
Add fields to an error already caught, without losing what is there.
Run Fun, converting an internal throw into {error, Error}.
Throw: a limit stopped the guest, rather than the guest doing something wrong.
As exhaustion/1, carrying context for the report.
Render an error on one line, for your logs and test failures.
Throw: the module decodes but does not validate.
As invalid/2, carrying context for the report.
Whether this term is one of this module's errors.
Throw: instantiation cannot satisfy an import. Nothing has run yet.
As link_error/2, carrying context for the report.
Throw: the bytes are not a well-formed module.
As malformed/2, carrying context for the report.
Throw: the guest did something the specification says traps.
As trap/1, carrying context for the report.
Types
-type class() :: malformed | invalid | link | trap | exhaustion.
-type trap_reason() :: unreachable | integer_divide_by_zero | integer_overflow | invalid_conversion_to_integer | out_of_bounds_memory_access | out_of_bounds_table_access | undefined_element | uninitialized_element | indirect_call_type_mismatch | runtime_stopped | intra_component_copy | {host_error, term()}.
Functions
Add fields to an error already caught, without losing what is there.
-spec capture(fun(() -> Result)) -> Result | {error, error()}.
Run Fun, converting an internal throw into {error, Error}.
It also converts unexpected Erlang exceptions into a structured internal error
rather than letting them escape. A bug in this library still has to reach you as
a value, because the whole point of the runtime is that hostile input cannot
destabilise its caller.
Throw: a limit stopped the guest, rather than the guest doing something wrong.
Fuel, call depth, memory pages and host calls all arrive here. Distinct from a trap because the module is not at fault and the same call under a larger budget would succeed.
As exhaustion/1, carrying context for the report.
Render an error on one line, for your logs and test failures.
Throw: the module decodes but does not validate.
As invalid/2, carrying context for the report.
Whether this term is one of this module's errors.
Throw: instantiation cannot satisfy an import. Nothing has run yet.
As link_error/2, carrying context for the report.
Throw: the bytes are not a well-formed module.
Decoding only. A module that decodes but does not type check is invalid/2,
and the distinction is what the specification suite asserts on: assert_malformed
and assert_invalid are different directives.
As malformed/2, carrying context for the report.
-spec trap(trap_reason()) -> no_return().
Throw: the guest did something the specification says traps.
A trap is defined behaviour, not a defect. Every one of them is reachable from valid WebAssembly and an embedder should expect them.
-spec trap(trap_reason(), map()) -> no_return().
As trap/1, carrying context for the report.