wasm_tools_policy (wasm_tools v0.1.0)
View SourceCapabilities and limits: what a component may reach, and how much it may use.
This module is internal: you reach it through wasm_tools, and it can change
in any release. The README lists the supported API.
You give them when you load a component. This module checks them once, then
turns them into what erlang_wasm takes at each call: the WASI 0.2 imports
(wasi_preview2:command/1), the instance limits (wasm_component:instantiate/3),
and the deadline and heap bound the runner process enforces.
Nothing is granted unless you name it:
| capability | value | the guest gets |
|---|---|---|
filesystem | [{GuestPath, HostDir, read_only | read_write}] | those directories as WASI preopens, and nothing else |
env | [{Name, Value}] or #{Name => Value}, binaries | exactly those variables, never the node's |
network | an erlang_wasm network grant (wasi_net) | wasi:sockets and wasi:http to the addresses and ports it names |
http | [Host | {Host, Port}] | outbound wasi:http to those hosts, and no sockets and no name lookups |
An http host is a name (<<"api.example.com">>), a wildcard for its
subdomains (<<"*.example.org">>, which does not match example.org
itself), or a literal address (<<"127.0.0.1">>). Without a port it is
reachable on its scheme's default port, 80 or 443. A name that resolves to a
loopback, private, link-local, metadata or other non-public address is
refused; only a literal address you list reaches such an address. erlang_wasm
resolves the name once and connects to the address it checked, and does not
follow redirects: a request to a new location is checked again. authorize/2
says what a policy decides for a request; wasm_tools:authorize/2 asks it for a
loaded component.
Without network, the wasi:sockets interfaces are still given, under a grant
that refuses everything: no address, no name lookup, and no TCP or UDP socket
can even be created. A component built with a toolchain that links sockets by
default (CPython's, for one) then loads without being given any network, as
erlang_wasm already does for the filesystem and HTTP.
Clocks and random numbers are always available: erlang_wasm gives them to every WASI 0.2 component and has no switch for them. Standard output and error are discarded.
Limits use erlang_wasm's names (fuel, timeout, max_depth,
max_memory_pages, max_host_calls, max_heap_words). untrusted (the
default) and trusted are erlang_wasm's presets (wasm_limits:untrusted/0,
trusted/0: no fuel ceiling, no timeout, no memory bound); a map is merged over
untrusted. A timeout of infinity needs
the trusted preset. At a call, limits can only be lowered (clamp/2).
One limit is wasm_tools' own: min_heap_words, the heap a call's process
starts with, 16384 words (128 KiB) by default. A fresh process that
instantiates a component otherwise grows its heap step by step, and that
growth is much of the cost of a call: with the default a small call is about
a fifth faster, and calls per second rise too. A component that passes large
records gains more from a larger floor (65536 words made one 30% faster
again), at the price of memory each running call holds, which lowers
throughput under many concurrent calls. docs/benchmarks.md has the numbers;
0 turns the floor off.
Summary
Functions
What the http capability of Policy decides for one request: Request is
#{scheme, host, port}, as before a name is resolved, or the same with
address, as once an address has passed. deny for a policy without http.
The policy for one call, with the limits it asks for: each the smaller of the two, so a call can lower a limit and never raise one.
The heap bound of the runner process, in words.
The options for wasm_component:instantiate/3: the instance limits.
Every limit, the instance's and the runner's.
The heap the runner process starts with, in words; never above its bound.
Check capabilities and limits, once, at load.
Whether the component may reach the network, for MCP's openWorldHint.
The deadline of a call, in milliseconds.
The WASI 0.2 imports for one call, built in the process that will own the instance.
The WASI 0.2 imports for one call, as wasi_imports/1, with OnDeny called
with each wasi:http request the policy refuses: #{scheme, host, port}, with
address when the refusal is of the address a name resolved to. Without
http and network, every request is refused, and each is reported. With
network alone, its grant decides wasi:http and its refusals are not seen
here. undefined reports nothing.
Types
-type capabilities() :: #{filesystem => [{binary(), file:filename_all(), read_only | read_write}], env => [{binary(), binary()}] | #{binary() => binary()}, network => map(), http => [binary() | {binary(), inet:port_number()}]}.
-opaque policy()
Functions
What the http capability of Policy decides for one request: Request is
#{scheme, host, port}, as before a name is resolved, or the same with
address, as once an address has passed. deny for a policy without http.
The policy for one call, with the limits it asks for: each the smaller of the two, so a call can lower a limit and never raise one.
-spec heap_words(policy()) -> pos_integer() | infinity.
The heap bound of the runner process, in words.
The options for wasm_component:instantiate/3: the instance limits.
Every limit, the instance's and the runner's.
-spec min_heap_words(policy()) -> non_neg_integer().
The heap the runner process starts with, in words; never above its bound.
Check capabilities and limits, once, at load.
Anything can be passed, as they may come from configuration: what does not
have the shapes capabilities() and limits() describe is an error value.
Whether the component may reach the network, for MCP's openWorldHint.
The deadline of a call, in milliseconds.
The WASI 0.2 imports for one call, built in the process that will own the instance.
The WASI 0.2 imports for one call, as wasi_imports/1, with OnDeny called
with each wasi:http request the policy refuses: #{scheme, host, port}, with
address when the refusal is of the address a name resolved to. Without
http and network, every request is refused, and each is reported. With
network alone, its grant decides wasi:http and its refusals are not seen
here. undefined reports nothing.