WASI

29.1 What is WASI?

The WebAssembly System Interface: standardized APIs that let Wasm programs interact with the outside world (files, clocks, randomness, networking, environment, HTTP) portably and securely, outside the browser.

Capability-based security: a module can only access resources the host explicitly grants (e.g., specific pre-opened directories). There's no ambient authority.

29.2 WASI Versions

Preview 1 (wasip1) wasi_snapshot_preview1 imports: POSIX-like (fd_read, path_open, clock_time_get, random_get...). Widely supported and still common.

WASI 0.2 (Preview 2) Released 2024. Built on the Component Model and WIT. Modular interfaces (worlds): wasi:cli, wasi:filesystem, wasi:sockets, wasi:http, wasi:io, wasi:clocks, wasi:random.

WASI 0.3 (Preview 3) Native async (futures and streams in the Component Model), simplifying I/O APIs (previews in 2025) Planned stable release

29.3 WASI Interfaces (0.2)

Command world: args, env, stdin/stdout/stderr, exit Pre-opened directories, files Streams, polling (pollables) Wall clock, monotonic clock TCP/UDP, name lookup

Incoming/outgoing HTTP handlers (wasi:http/proxy world) wasi:keyvalue, wasi:blobstore, wasi:config, wasi:messaging, wasi:logging, wasi:nn (neural networks), wasi:sql, wasi:observe, GPU (wasi:webgpu)

29.4 Running WASI Programs

wasmtime run hello.wasm                                  # stdout works by default
wasmtime run --dir=./data::/data app.wasm                # grant one directory
wasmtime run --env API_KEY=xyz app.wasm arg1             # env vars and args
wasmtime run -S inherit-network=y net.wasm               # allow sockets (0.2)
wasmtime serve http-handler.wasm                          # run a wasi:http/proxy component on :8080
wasmer run app.wasm --mapdir /data:./data
wasmedge --dir .:. app.wasm