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
- In the browser, a module's imports come from JavaScript. Here they are 13 functions from
wasi_snapshot_preview1, among themargs_get,fd_read,fd_write,path_open,fd_prestat_getandproc_exit. The runtime provides them. terrace-room.rgbais the 1200 × 800 photo as raw RGBA bytes, 3,840,000 bytes.- This target used to be called
wasm32-wasi. Rust 1.78 added the namewasm32-wasip1, and the old name was removed in Rust 1.84.
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
wasmtime file.wasm arguments...runs_start, which is Rust'smain(). The arguments after the file name are passed to the program.- Without
--dir, the program has not a single folder. Thephotosfolder is on the disk, but to the module the file is "No such file or directory".os error 44is WASI's error number fornoent. --dir photosopens the host'sphotosfolder and gives it to the module under the same name. This is called a preopen.--dir photos::/datagives the same folder under another name. The module sees/data, not the real path on the disk.- The result matches the earlier lessons' JavaScript grayscale byte for byte.
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
preopensis the same as--dir: on the left the name inside the module, on the right the folder on the host.wasi.start()returns the exit code (returnOnExitdefaults totrue), so pass it on toprocess.exitCode.- The path
photos/../private/guests.csvis refused here too, with a different message:Capabilities insufficient (os error 76)(notcapable). - Its status is still Stability: 1 - Experimental in the Node 22, 24 and 26 docs. The docs also say
node:wasidoes not yet give the full file-system security of other WASI runtimes: do not use it to run code you do not trust.
Preview 1, WASI 0.2, WASI 0.3
- Preview 1 (
wasi_snapshot_preview1, also called WASI 0.1): a list of C-style functions whose data travels as pointers and lengths in linear memory. It has the broadest support: Wasmtime, Node, edge runtimes, and Go (GOOS=wasip1) and TinyGo (-target=wasip1) too. - WASI 0.2 (preview 2, official on 25 January 2024): built on the Component Model. Its interfaces are written in WIT (
wasi:cli,wasi:filesystem,wasi:sockets,wasi:clocks,wasi:random,wasi:io,wasi:http), and the output is a component, not a plain module. Rust:--target wasm32-wasip2(tier 2 since Rust 1.82). This version ofgray-imgis 89,702 bytes, and Wasmtime runs it with the same command. - WASI 0.3 (released 11 June 2026, patch 0.3.1 in August 2026, the one used in this lesson): async built into the Component Model (
stream,future,async func), andwasi:iois removed. wasi.dev calls it the latest release; WASI 0.2 stays widely supported. In Rust 1.99,wasm32-wasip3is still tier 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 |
- Most of that time is the cost of starting a process. Inside a runtime that is already running, creating a new instance of an already compiled module is far cheaper: in Node 22, 0.4 to 0.6 ms for this lesson's WASI modules, that is, hundreds of microseconds.
- That is why Wasm-based serverless platforms create a fresh instance per request: it starts fast, and every request is kept apart from the others.
- Compared with a container, a Wasm module carries no Linux file system: one 63 KB file, with every permission refused until it is granted.
⚠️ Why the program can't read a file, or fails without a sound
- Forgetting
--dir. The folder is on the disk, but the module seesNo such file or directory (os error 44). - Thinking
photos/..can get out. Paths open relative to the permitted folder, and leaving it is refused (os error 63in Wasmtime,76in Node). - Using
node:wasifor other people's code. It is still experimental, and its own docs say not to use it for untrusted code. Use Wasmtime. - Running a WASI 0.2 component on a preview 1 runtime.
expected version 01 00 00 00, found 0d 00 01 00. Pick the target that matches the runtime. - Not passing the exit code on.
wasi.start()returns the number; withoutprocess.exitCode, Node exits with 0 even when the program failed.
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.