WASI (Wasm on the Server) — WebAssembly

WASI is the standard interface between a Wasm module and the system: arguments, stdout, files, the clock, random numbers. An ordinary Rust program is built…

Mrs. Sri's photo program already runs in the browser. Now she wants the same program to process photos on a server and in a terminal, reading and writing files itself. In the browser, everything a module needs is supplied by JavaScript through imports. Outside the browser, it is supplied by WASI (WebAssembly System Interface), a standard list of functions a Wasm module may import to talk to the system: read its arguments, write to the terminal, open files, read the clock, get random numbers. With WASI, the same .wasm runs on a server and in a terminal, not only in a browser.

It works like Mrs. Sri hiring a craftsman from an agency to process the room photos. The craftsman works in one room, and every request goes to Mrs. Sri on a standard form. Mrs. Sri hands over the key to the photos cupboard only. The private cupboard, with the guest data, never gets its key handed over, so the craftsman does not even know it exists. The craftsman is the Wasm module, the form is the WASI functions, Mrs. Sri is the runtime (the host), and the cupboard key is a preopened folder.

The same photo program, now in a terminal

This is gray-img, an ordinary Rust program that uses std::fs. No wasm-bindgen, no JavaScript:

//! gray-img: turn an RGBA photo gray. Runs on a server through WASI.
use std::{env, fs, process};

fn main() {
    let args: Vec<String> = env::args().collect();
    if args.len() != 3 {
        eprintln!("usage: gray-img <input.rgba> <output.rgba>");
        process::exit(2);
    }
    let mut px = match fs::read(&args[1]) {
        Ok(data) => data,
        Err(e) => {
            eprintln!("error reading {}: {e}", args[1]);
            process::exit(1);
        }
    };
    for p in px.chunks_exact_mut(4) {
        let gray = ((p[0] as u32 * 77 + p[1] as u32 * 150 + p[2] as u32 * 29) >> 8) as u8;
        p[0] = gray;
        p[1] = gray;
        p[2] = gray;
    }
    if let Err(e) = fs::write(&args[2], &px) {
        eprintln!("error writing {}: {e}", args[2]);
        process::exit(1);
    }
    println!("{}: {} pixels are now gray", args[2], px.len() / 4);
}
rustup target add wasm32-wasip1
cargo build --release --target wasm32-wasip1   # target/wasm32-wasip1/release/gray-img.wasm, 63,172 bytes

Running it with Wasmtime

Wasmtime is the Bytecode Alliance's WASI runtime. This is its real output:

$ wasmtime --version
wasmtime 49.0.2 (3c8a3e79a 2026-10-02)
$ wasmtime gray-img.wasm photos/terrace-room.rgba photos/terrace-room-gray.rgba
error reading photos/terrace-room.rgba: No such file or directory (os error 44)
$ wasmtime --dir photos gray-img.wasm photos/terrace-room.rgba photos/terrace-room-gray.rgba
photos/terrace-room-gray.rgba: 960000 pixels are now gray
$ wasmtime --dir photos::/data gray-img.wasm /data/terrace-room.rgba /data/terrace-room-gray.rgba
/data/terrace-room-gray.rgba: 960000 pixels are now gray

Capabilities: permission per folder

WASI uses a capability model: a module can only use what the host explicitly gives it. By default: no files, no network, no environment variables. All it has is its arguments, stdin, stdout, stderr, the clock and random numbers.

Guess before you look at the output: the program runs with --dir photos, then is asked to read photos/../private/guests.csv. The path starts with photos/. Does it work or is it refused?

$ wasmtime --dir photos gray-img.wasm private/guests.csv photos/steal.rgba
error reading private/guests.csv: No such file or directory (os error 44)
$ wasmtime --dir photos gray-img.wasm photos/../private/guests.csv photos/steal.rgba
error reading photos/../private/guests.csv: Operation not permitted (os error 63)

The path through photos/.. is refused too. photos/../private/guests.csv is opened through the photos folder, and .. may not leave the permitted folder. Wasmtime refuses it with os error 63 (perm). Even when --dir private is given too, a path through photos/.. is still refused; only private/guests.csv directly is allowed. Compare that with a native program: it can read every file the user who runs it can read.

Node: node:wasi

Node has the node:wasi module for running WASI preview 1 modules:

// run.mjs
import { readFile } from 'node:fs/promises';
import { WASI } from 'node:wasi';

const wasi = new WASI({
  version: 'preview1', // required: 'preview1' (or 'unstable')
  args: ['gray-img', 'photos/terrace-room.rgba', 'photos/terrace-room-gray.rgba'],
  preopens: { photos: './photos' }, // the name inside the module: the folder on the host
});
const compiled = await WebAssembly.compile(await readFile('gray-img.wasm'));
const instance = await WebAssembly.instantiate(compiled, wasi.getImportObject());
process.exitCode = wasi.start(instance); // _start = Rust's main(); the result is the exit code
$ node run.mjs
(node:91328) ExperimentalWarning: WASI is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
photos/terrace-room-gray.rgba: 960000 pixels are now gray

Preview 1, WASI 0.2, WASI 0.3

node:wasi only understands preview 1. A component is refused at its 4th byte:

$ node run-comp.mjs        # run.mjs, but reading gray-img-component.wasm
(node:91330) ExperimentalWarning: WASI is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
node:internal/modules/run_main:123
    triggerUncaughtException(
    ^

[CompileError: WebAssembly.compile(): expected version 01 00 00 00, found 0d 00 01 00 @+4]

Components are covered fully in the lesson "Wasm Component Model".

Other options for a WASI runtime:

Runtime Written in When to pick it
Wasmtime (this lesson) Rust The default here: preview 1 and WASI 0.2, strict folder permissions, from the Bytecode Alliance.
wazero Go, with no cgo A Go program that wants to run Wasm plugins inside itself. Preview 1 only, no components yet.
jco JavaScript and Rust Runs WASI 0.2 components in Node (jco run), or turns them into ES modules for Node and the browser (jco transpile). This is the way out for the component that node:wasi refused above.
Wasmer or WasmEdge Rust / C++ Other runtimes that run WASI modules from the CLI (wasmer run, wasmedge). Pick one when the platform you deploy to uses it.

📌 Size and start-up time

Measured on a busy MacBook M1 Pro, the time for one whole process (start, read the photo, process, write), the median of 11 interleaved rounds:

Way Size Time
Native Rust program (arm64) 302,656 bytes 5.9 ms
Wasmtime, compiled code cached (its default) 63,172 bytes 15.1 ms
Wasmtime, -C cache=n (compiled every time) 63,172 bytes 27.4 ms
Wasmtime, .cwasm from wasmtime compile 215,584 bytes 16.1 ms
Wasmtime, WASI 0.2 component 89,702 bytes 19.4 ms
Node 22, node run.mjs 63,172 bytes 40.3 ms (empty Node 29.4 ms)
TinyGo 0.43 (-target=wasip1) 302,365 bytes
Go 1.27.2 (GOOS=wasip1) 2,677,059 bytes

⚠️ Why the program can't read a file, or fails without a sound

TL;DR: WASI is the standard interface between a Wasm module and the system: arguments, stdout, files, the clock, random numbers. An ordinary Rust program is built with --target wasm32-wasip1 (63,172 bytes, 13 wasi_snapshot_preview1 imports) and run with wasmtime (49.0.2) or node:wasi (still Experimental, version: 'preview1' required). The security model is capabilities: without --dir photos (or preopens) the module sees no files at all (os error 44), and photos/../private is still refused (os error 63 in Wasmtime, 76 in Node). Preview 1 has the broadest support; WASI 0.2 (January 2024) and 0.3 (June 2026) use the Component Model, and their output is a component that Wasmtime runs but node:wasi refuses. One Wasmtime process takes about 15 ms with the cache or a .cwasm (27 ms when it compiles every time, native 6 ms), while a new instance inside a runtime that is already running takes under 1 ms.

⚡ An ordinary Rust program, a WASI target

Write:

rustup target add wasm32-wasip1
cargo build --release --target wasm32-wasip1   # target/wasm32-wasip1/release/gray-img.wasm, 63,172 bytes

Meaning: The program is ordinary Rust with std::fs, with no wasm-bindgen and no JavaScript. Its imports are 13 wasi_snapshot_preview1 functions (arguments, files, stdout, the clock), and a runtime such as Wasmtime provides them.

⚡ No permission, no files

Write:

$ wasmtime --dir photos gray-img.wasm photos/terrace-room.rgba photos/terrace-room-gray.rgba

Meaning: WASI uses a capability model: a module can only use what the host gives it. Without --dir photos, the file is No such file or directory (os error 44), even though it is on the disk. --dir photos opens that folder to the module (a preopen).

⚡ A shortcut through .. is still refused

Guess first: with --dir photos, the program is asked to read photos/../private/guests.csv. The path starts with photos/. Does it work or is it refused?

Refused, Operation not permitted (os error 63). The path is opened through the photos folder, and .. may not leave the permitted folder.

Meaning: A native program can read every file its user can read. A WASI module only sees the folders it is given.

⚡ Small, quick to start, tightly sandboxed

Meaning: Compared with a container, a Wasm module carries no Linux file system: one 63 KB file, with every permission refused until it is granted. A new instance inside a runtime that is already running takes under 1 ms, which is why Wasm-based serverless platforms create a fresh instance per request.

What you will learn