wasm_canon (wasm v0.16.0)
View SourceThe Canonical ABI lift/lower for component-model values.
The Canonical ABI is how a component-model value crosses between the host and a
core module's linear memory. lower_params/3 turns Erlang terms into the flat
core arguments an exported function takes; lift_result/3 turns the core result
back into an Erlang term. Small values travel in registers ("flat"); aggregates
and anything a list/string points at travel through guest memory, placed with
the guest's cabi_realloc.
Value types and their Erlang shapes:
| descriptor | Erlang |
|---|---|
u8..u64, s8..s64, char | integer |
f32, f64 | float |
bool | true / false |
string, {list, u8} | binary (UTF-8 for a string) |
{list, D} | list of D |
{record, [{Name, D}]} | #{Name => value} |
{tuple, [D]} | tuple |
{enum, [Name]} | the name binary |
| {variant, [{Name, D | none}]} | {Name, Payload} (undefined when none) |
| {option, D} | none / {some, value} |
| {result, Ok, Err} | {ok, value} / {error, value} |
| {flags, [Name]} | list of the set name binaries |
| handle | integer (a resource own/borrow, an opaque i32) |
Names in records, variants, enums and flags come from the type descriptor the
host holds, not from guest bytes. A resource is an {own, T} or {borrow, T}
descriptor whose value is the handle wasm_resources minted. A parameter list
that flattens past MAX_FLAT_PARAMS is stored in memory as one tuple and passed
as a pointer, as the specification says.
Lowering trusts its values: check/2 runs first, on the arguments of a call,
and check_host_result/2 on what a host function returns. Each accepts
exactly the shapes above, which are the shapes lifting produces, with an
integer in the range of its width, a char that is a Unicode scalar value and a
string that is valid UTF-8. A case or a result arm with no type takes
undefined, or none. The check walks a value without building anything; only
a value that fails is walked again, by locate/3, to say where.
Where things are
| you want | look at |
|---|---|
a call's arguments as flat values, and the spill past MAX_FLAT_PARAMS | lower_params/3, params_spill/1, in %%% params |
| whether a value fits its descriptor, and where it does not | check/2, check_value/2, check_host_result/2, in %%% checks: valid/2 answers whether, locate/3 where |
| a call's result | lift_result/3, in %%% results |
| a host import, the other way round: the guest's arguments in, the host's result out | lift_params/3, lift_async_params/3, lower_value/3, store_value/4, load_value/3, result_via_memory/1, in %%% host imports (reverse) |
| the allocator and the string encoding a lowering uses | with_realloc/2, with_string_encoding/2, in the same banner |
| a value read from memory | load/3, in %%% memory |
| a value or a string written to memory | store/4, in %%% memory io |
| a type's size and alignment, and its flat core types | size_align/1 in %%% layout, flat_types/1 in %%% flat types |
| a call that must not leave its instance | no_leave/1, may_leave/0, in %%% helpers |
To add a type, follow "A canonical type" under "Where to add things" in
docs/component-internals.md: every layer has to agree on it, and the check
here refuses a descriptor it does not know.
Summary
Functions
Check each of Values against its descriptor in Descs, before anything is
lowered: ok, or {error, Error} for the first value that does not fit,
with class invalid and kind bad_argument.
Check what a host function returns before it is lowered into the guest. A
value that does not fit is the host's fault, not the guest's: it traps with
kind host_error, the ctx of check_value/2 and its message as reason.
As check/2, for one value, with a path from the value itself: [] when the
value is what does not fit.
The flat core value types a descriptor lowers to (i32|i64|f32|f64).
As lift_params/3, for an async-lowered call: its parameters are in memory
behind one pointer once they flatten past four values.
Lift the flat core arguments a guest passed into an imported function.
Lift the core result of a call by its descriptor.
Read a value of Desc from linear memory at Ptr (load, exported).
Lower each parameter to the flat core values its function takes.
Lower one value to its flat core representation (lower_flat, exported).
Whether the running call may leave its component instance.
Run Fun, a call that must not leave its component instance: a realloc or
a post-return. While it runs, may_leave/0 is false, and every async and
thread built-in traps.
Whether a result is returned through memory rather than flat.
The size and alignment, in bytes, of a value laid out in memory.
Write a value into linear memory at Ptr (store, exported).
Run Body with Realloc as the allocator for any result it lowers by memory.
Run Body with a string encoding in effect for the strings it lifts and lowers.
Types
-type desc() :: u8 | u16 | u32 | u64 | s8 | s16 | s32 | s64 | f32 | f64 | bool | char | string | {list, desc()} | {record, [{name(), desc()}]} | {tuple, [desc()]} | {enum, [name()]} | {variant, [{name(), desc() | none}]} | {option, desc()} | {result, desc() | none, desc() | none} | {flags, [name()]} | handle | {own, non_neg_integer()} | {borrow, non_neg_integer()} | {future, desc()} | {stream, desc()} | error_context.
-type name() :: binary().
Functions
-spec check([desc()], [term()]) -> ok | {error, wasm_error:error()}.
Check each of Values against its descriptor in Descs, before anything is
lowered: ok, or {error, Error} for the first value that does not fit,
with class invalid and kind bad_argument.
ctx holds:
path: where the value is, the parameter's position first (from 0), then a record field's name, a list or tuple index (from 0),{'case', Name}for a variant's payload, andsome,okorerror;expected: the descriptor the value should fit;got: the value when it is an integer, a float or an atom, else its type (binary,list,map,tupleand so on), so a large value is never copied into the error, andmissingfor a record field that is not there.
A name that is not one of the type's (a variant case, an enum case, a flag, a
record field) is in ctx under name, or field for a record. A list of
values of the wrong length has path => [], and the two lengths as
expected and got.
Check what a host function returns before it is lowered into the guest. A
value that does not fit is the host's fault, not the guest's: it traps with
kind host_error, the ctx of check_value/2 and its message as reason.
-spec check_value(desc(), term()) -> ok | {error, wasm_error:error()}.
As check/2, for one value, with a path from the value itself: [] when the
value is what does not fit.
-spec flat_types(desc()) -> [i32 | i64 | f32 | f64].
The flat core value types a descriptor lowers to (i32|i64|f32|f64).
-spec lift_async_params(wasm:instance(), [desc()], [term()]) -> {[term()], [term()]}.
As lift_params/3, for an async-lowered call: its parameters are in memory
behind one pointer once they flatten past four values.
-spec lift_params(wasm:instance(), [desc()], [term()]) -> {[term()], [term()]}.
Lift the flat core arguments a guest passed into an imported function.
The inverse of lower_params/3: for each parameter descriptor it consumes the
flat values the guest lowered and returns the Erlang term. Any flat values left
over -- a return-area pointer the guest passes for a by-memory result -- are
returned as the second element.
-spec lift_result(wasm:instance(), desc(), [term()]) -> term().
Lift the core result of a call by its descriptor.
A result that fits flat comes straight from CoreResults; a wider one is a single
pointer to a guest-allocated area, read from memory.
-spec load_value(wasm:instance(), desc(), non_neg_integer()) -> term().
Read a value of Desc from linear memory at Ptr (load, exported).
-spec lower_params(wasm:instance(), [desc()], [term()]) -> [term()].
Lower each parameter to the flat core values its function takes.
-spec lower_value(wasm:instance(), desc(), term()) -> [term()].
Lower one value to its flat core representation (lower_flat, exported).
-spec may_leave() -> boolean().
Whether the running call may leave its component instance.
-spec no_leave(fun(() -> T)) -> T.
Run Fun, a call that must not leave its component instance: a realloc or
a post-return. While it runs, may_leave/0 is false, and every async and
thread built-in traps.
Whether a result is returned through memory rather than flat.
-spec size_align(desc() | none) -> {non_neg_integer(), pos_integer()}.
The size and alignment, in bytes, of a value laid out in memory.
-spec store_value(wasm:instance(), desc(), non_neg_integer(), term()) -> ok.
Write a value into linear memory at Ptr (store, exported).
Run Body with Realloc as the allocator for any result it lowers by memory.
Realloc is a fun(Ctx, [0, 0, Align, Size]) -> {ok, [Ptr]} (a core function),
or undefined to keep the default (the guest's cabi_realloc export). A
multi-core component names a realloc that is not reachable by that name on the
instance calling the import, so the linker installs it here around the host
function; the override is scoped to Body and restored after.
-spec with_string_encoding(utf8 | utf16 | latin1_utf16, fun(() -> T)) -> T.
Run Body with a string encoding in effect for the strings it lifts and lowers.
The Canonical ABI lets a component choose how it stores strings: utf8 (the default),
utf16, or latin1+utf16 (latin1 when every code point fits, otherwise utf16, with the
high bit of the length disambiguating). The encoding is a canon option, so the caller sets
it around the lift/lower for the component whose option it is. utf8 installs no context,
so the common path is unchanged.