WASI 0.2
View SourceWASI 0.2 (wasm32-wasip2) is the version of WASI for the Component Model. The
guest is a component: a binary that holds core WebAssembly modules and
describes typed interfaces (see Components). It imports
interfaces such as wasi:cli, wasi:io, wasi:filesystem, wasi:clocks,
wasi:sockets, wasi:random and wasi:http. Read this page when your
toolchain targets wasm32-wasip2 or produces a component. It shows how to run
the component, how to decide what it may reach, and which interfaces the host
provides. For a core module built for wasm32-wasip1, read
WASI preview 1 instead.
The host is wasi_preview2. It answers every import from Erlang, with the
same code as Preview 1: paths go through wasi_fs, address checks through
wasi_net, and random bytes come from crypto:strong_rand_bytes/1. The
sandbox and grant rules in WASI preview 1 apply here without change.
Run a command component
Pass the component bytes, the bytes to feed its stdin, and an options map. You get back the exit code and everything it wrote:
{ok, Bin} = file:read_file("app.component.wasm"),
{ok, #{exit_code := Code, stdout := Out, stderr := Err}} =
wasi_preview2:run_command(Bin, <<"input bytes">>,
#{args => [~"app", ~"--flag"],
env => [{~"MODE", ~"prod"}],
preopen => "/srv/app/data",
writable => false}).run_command/3 instantiates the component with command/1, calls its
wasi:cli/run export and collects stdout and stderr. It always destroys the
instance, also on errors. A component without a wasi:cli/run export returns
{error, no_run_export}. Pass compile => true to build its core modules in
the calling process instead of through the module cache, which limits how
many modules it loads a second. Without compile => true, run_command/3
always uses the cache: if the wasm application is not started, it answers
{error, #{kind := cache_unavailable}}.
The exit code is 0 when run returns ok and 1 when it returns err.
wasi:cli/exit.exit works the same way. exit-with-code reports the code
it was given. A trap that is not an exit is {error, Reason}.
Grant capabilities
The options are grants, plus three options for outgoing HTTP. If you leave a grant out, the component does not get that capability.
| key | grants | left out |
|---|---|---|
args | the command line, argv[0] included | zero arguments |
env | variables, as {Name, Value} binaries | none, never the node's |
initial_cwd | the initial-cwd answer | none |
preopen | one host directory, at guest / | no filesystem |
writable | true makes preopen writable | read-only |
preopens | mounts, as {Name, HostDir, Writable} | preopen, if any |
network | wasi:sockets, and outbound wasi:http unless http is given | no network |
http | outbound wasi:http alone, by host name; see HTTP | network, for wasi:http |
http_authorize | a fun(Request) -> allow | deny asked for each outbound request, by host name and then by address | every request the grant admits |
http_transport | the module that performs outbound HTTP | wasi_http_h1 |
http_tls | ssl client options for https | verify against system CAs |
http_max_body_bytes | the largest HTTP body the host holds: a response an outbound request reads, and a body the guest writes | 8 MiB |
nn | wasi:nn: a backend and the model names the guest may load; see wasi-nn | no wasi:nn |
args is returned exactly as you pass it. Nothing is added in front.
run_command/3 supplies stdin from its second argument and collects stdout and
stderr itself. When you build the imports with command/1 yourself, stdin is
a binary, and stdout and stderr are funs of type fun((binary()) -> ok)
that receive the output. By default, output is thrown away.
Mount directories
One directory at the root:
#{preopen => "/srv/app/data", writable => true}Several, each with its own writability:
#{preopens => [{~"/data", "/srv/app/data", false},
{~"/scratch", "/tmp/app-1234", true}]}When preopens is present, preopen and writable are ignored. An attempt
to write on a read-only mount fails with the read-only error code. The guest
path goes to wasi_fs:open/3, the same call Preview 1 uses, so the escapes
listed in WASI preview 1 are refused here too.
Make writes durable
A guest makes what it wrote survive a power loss with sync or sync-data,
on a file or on a directory. Each does what fd_sync and fd_datasync do
in preview 1, including the full flush on macOS; see
Make writes durable.
open-atrefusesfile-integrity-sync,data-integrity-syncandrequested-write-syncwithunsupported, as wasmtime does.- A write to a file stream writes every byte it is given, or fails.
- A flush of a file stream calls
fsync(2). Callsyncwhen the write must survive a power loss. - Nothing locks a file, so two writers on one database corrupt it; see Two writers on one file.
Grant a network
network takes the same grant as the Preview 1 net key: connect and
listen rules of {Proto, Addr, Port}, resolve, max_sockets and timeout.
Two more keys, tcp and udp, are booleans. false forbids creating any
socket of that kind. Both default to true.
#{network => #{connect => [{tcp, ~"10.0.0.0/8", {8000, 8099}}],
listen => [{tcp, ~"127.0.0.1", 8080}],
resolve => allow,
udp => false}}Without network, the component gets no wasi:sockets imports. A component
that does not import them still links. ip-name-lookup resolves names only
when the grant says resolve => allow. That flag also lets wasi:http reach
a host by name, so to allow HTTP by name without giving the guest sockets or
name lookups, grant http instead: see
HTTP. Sockets go through wasi_sock2, which
uses OTP's socket module. So a TCP socket can be bound before it connects or
listens, and can report the port it was given.
Options from preview 1
The 0.2 and 0.3 hosts grant the same capabilities as preview 1, with other
names and shapes. wasi_preview3:run_command/3 takes the same options as
wasi_preview2:run_command/3. Translate a preview 1 map like this:
| capability | preview 1: wasi:run/2, wasi:imports/1 | 0.2 and 0.3: run_command/3, command/1 |
|---|---|---|
| arguments | args => [Arg] | args => [Arg], the same |
| environment | env => #{Name => Value}, a map | env => [{Name, Value}], a list of pairs |
| directories | dirs => [{GuestPath, HostDir, read | write}] | preopen => HostDir and writable => true | false for one mount at guest /, or preopens => [{GuestPath, HostDir, true | false}] |
| working directory | none | initial_cwd => Path |
| network | net => Grant | network => Grant: the same grant, plus tcp and udp booleans |
| clocks | clocks => [monotonic, realtime], both by default | no option: both clocks, always |
| random bytes | random => strong | {seed, N} | none, strong by default | no option: always strong |
| stdin | stdin => Binary | Fun | the second argument of run_command/3; stdin => Binary for command/1 |
| stdout, stderr | wasi:run/2 collects them; for imports/1, a pid, fun or io device | run_command/3 collects them; for command/1, fun((binary()) -> ok) |
| fuel and limits | none on wasi:run/2: call wasm:instantiate/3 with wasi:imports/1 and a limits map | fuel, max_depth, max_memory_pages, max_heap_words, max_host_calls in the same map, and resource_limit |
| how core modules are built | your choice before the call: wasm:load_file/1 (the cache) or wasm:compile/1 | compile => true builds them in the calling process; without it, the module cache |
| directory listing | readdir_batch_bytes, readdir_fallback | none |
| outgoing HTTP | none | http, http_authorize, http_transport, http_tls, http_max_body_bytes |
wasm_component:instantiate/3 names the same choice differently:
loader => compile or loader => load, and without it, it picks load
while the wasm application runs and compile when it does not. The
run_command/3 and run_serve/3 runners take compile => true and never
pick by themselves. So with the runners, pass compile => true whenever the
application may not be started; with instantiate/3, leave loader out.
Which interfaces are provided
| interface | built by |
|---|---|
wasi:random (random, insecure, insecure-seed) | random/0 |
wasi:clocks (monotonic-clock, wall-clock) | clocks/0 |
wasi:cli/environment | environment/0, environment/3 |
wasi:io streams, error and poll | io/0, io/1 |
wasi:cli stdio, exit, terminals (no tty) | command/1 |
wasi:filesystem, read-only or writable per mount | filesystem/1, in wasi_p2_filesystem |
wasi:sockets: TCP, UDP, name lookup | sockets/1, in wasi_p2_sockets |
wasi:http types and outgoing-handler | wasi_http:http/1 |
one wasi:http/incoming-handler request | run_serve/3 |
wasi:nn, when the nn option is given | wasi_nn:imports/1 |
command/1 puts all of these in one import map, limited by the grants in its
options. Pass that map to wasm_component:instantiate/2 when you want to
manage the instance yourself:
Imports = wasi_preview2:command(#{args => [~"app"],
preopen => "/srv/app/data"}),
{ok, Inst} = wasm_component:instantiate(Bin, Imports).imports/0 is a smaller map with only the interfaces that keep no state
(random, clocks, environment, io). Use it for a component that needs nothing
else.
wasi:filesystem and wasi:http are always present in command/1, so a
component that imports them links with no mount and no network. It then sees
no directories, and its HTTP requests are denied.
Versioned imports
A component imports names with a version, such as wasi:io/streams@0.2.0. The
host uses the name without the version (wasi:io/streams) as its key.
wasm_component removes the @version suffix when it looks up an import. So
one host answers every 0.2.x component. In your own import map, also write
keys without the version. The lookup uses the name without the version, so a
key with a version never matches.
WASI 0.3 is the one exception. Its interfaces reuse 0.2's names with other
functions, and one component can import both. So an import at 0.3.x is looked
up as Interface@0.3, for example wasi:cli/stdout@0.3, and never as the bare
0.2 key.
Call a typed export
A component that exports functions, not a command, is instantiated and then called by function name, with the function's type (see Pass data in and out):
{ok, Inst} = wasm_component:instantiate(Bin),
{ok, Result} = wasm_component:call(Inst, ~"run",
{[{list, u8}], {list, u8}}, [~"hi"]),
ok = wasm_component:destroy(Inst).Check it against the conformance suites
rebar3 ct --suite=test/wasm_wasi2_conformance_SUITE
rebar3 ct --suite=test/wasm_wasi2_testsuite_SUITE
The first suite runs unmodified wasm32-wasip2 Rust programs. When
wasmtime is on the path, it also compares their output with wasmtime's
output. The second suite runs the official wasi-testsuite cases through the
adapter that turns a preview 1 module into a preview 2 component. It skips if
the checkout, wasm-tools or the adapter is missing. Its baseline lists how
many cases are known to fail in each directory. The suite header explains each
one.
WASI 0.3
A component built for WASI 0.3 uses stream and future from the async
Canonical ABI (see the glossary). Run it with
wasi_preview3, which hosts 0.3 beside this 0.2 host; see
WASI 0.3.
Short notes
stdoutandstderrnever reach the node's own standard output unless you pass a sink that writes there.- The environment is only what you pass. The node's real environment is never read.
wasi:cli/terminal-*report no terminal, so a guest writes plain text without terminal codes.
Read this next
Run a WASI 0.2 component, the runnable
version of this page, or HTTP for wasi:http.