WebAssembly lab: Wasmtime, WASI capabilities, WIT components and plugin limits
This lab gives each student a terminal with Wasmtime, the Bytecode Alliance's WebAssembly runtime, and small programs and components written in the WebAssembly text format, short enough to read in full: a first WASI program, a program that prints a file only from a folder it was granted, one that lists only the variables it was given, a component described by a WIT interface, two components composed into one, a component built against WASI 0.2 and one against WASI 0.3, and two plugins that misbehave.
Wasmtime is pinned to 50.0.1, so every copy prints the same. It is not an attack lab: every copy has no route to the internet and none to any other student, and nothing is downloaded into it.
What students learn
What WebAssembly is outside the browser and why: one portable file, a sandbox that reaches nothing but its imports, and a .wasm file read section by section, down to its LEB128 sizes. They grant a folder with --dir and watch a path that climbs out of it refused with WASI error 63, and grant one environment variable with --env. They read a WIT world, call a component's add, watch a component refused because nothing supplies its import, and run two components composed into one, which prints 42. They run one component built for WASI 0.2 and one for WASI 0.3, and turn 0.3 off to see the difference. Last, they stop a plugin that never ends with a timeout and with fuel, and cap its memory at 16 pages. It suits systems, cloud and security courses; check your own awarding body's unit for the exact wording, none is quoted here.
Run it in your browser
The lab starts two services that share one folder. tools has Python and a shell and writes the programs, the components and two folders, data and private, the first time it starts, with nothing downloaded. wasm runs Wasmtime and nothing else, so the terminal reaches it with docker exec wasm wasmtime, and Python with docker exec tools. There is no web page.
- Open the Container page. Open Build on the bar and choose Run a container. Anybody may open it, and the Git lab and the SQL lab start with no account. Every other lab needs a free account: create one, sign in, then press Start.
- Find the lab. The Sample labs card lists every sample. Find WebAssembly: Wasmtime, WASI and components.
- Press Start. Start runs it as it is, in one press. Edit the file first opens the compose file so you can change it before you start.
- Open it. On the lab's page, press Open terminal and type docker exec wasm wasmtime run hello.wat to see Hello from WebAssembly. Then type docker exec wasm wasmtime run --dir data cat.wat notes.txt to see a program read a file from the one folder it was granted.
What to ask students to hand in
A short write-up of one experiment of their own, such as a folder they granted and a path they could not reach, a function they added to a component and composed again, or the least fuel and memory a plugin needed, with the commands they typed and what Wasmtime printed. Then a sentence or two in their own words on what they found. Nothing is marked automatically: the sentence shows whether it was understood. For stronger work ask them to explain why a component with a missing import is refused before any of its code runs.
A basic lesson on this lab
WebAssembly Basics is a small lesson worked in this lab. In it a student runs a first program, asks for a file with nothing granted and then with one folder, grants one environment variable, calls a component, runs two components composed into one, and stops a program that never ends with a fuel budget.
Free personal accounts have it in the Basics module, which is on Home, and every college can copy it from the Library's Basics folder. It is deliberately small: it shows a teacher what a lesson worked in a lab is, and the lab does the rest. Copy it into a module of your own to change it. The undergraduate module WebAssembly components and WASI is worked in the same lab.
The code on GitHub
The compose file has no Dockerfile: it runs two public images, each as an unprivileged account with no capabilities. One is the official python:3.12.15-alpine, with 64 MB. The other is our own image of Wasmtime 50.0.1, pinned by digest, with 128 MB: the Bytecode Alliance publishes no image, so mitsurutech/wasmtime-image on GitHub builds one in the open from their release archives, checked by SHA-256, with the wasmtime program unchanged for both arm64 and amd64. Wasmtime is licensed Apache-2.0 with the LLVM exception, and its licence ships inside that image. The files are written from fixed text onto a volume the first time, so a student's edits survive a stop and a start. The limits are honest ones: no compiler or component tool is in the lab, so every module is hand-written text, and no component here is async.
Set it as work
Say in the task what to hand in.
- Make the lab. In your module add a task and choose the Container lab card, then In a GitHub repository. Give https://github.com/mitsurutech/lab-wasm-lab with the branch main. The compose file is the default, compose.yaml. Fork the repository first if you want to change it, for example to add a component of your own or another folder to grant.
- Publish and try it. Publish pins the commit you were shown, so a later change in the repository does not alter a lab you have already set. Start your own copy first.
- Hand it out. Assign it like any task, with a due date if you want one, and say in the task what to do and what to hand in.
- Mark what is handed in. Open a student’s copy and the work they handed in, mark it, and send it back with a reason if it needs more.
Try this lab, and what it costs
A free personal account can work through the basic lessons in this lab, with enough free credit each month to follow most lessons and no card. For a class, a school or college has its first 365 days free for up to 50 students, then pays per student a year, with every lab included and staff free.
Questions people ask
- Why does the terminal use docker exec wasm wasmtime?
- The lab's terminal carries a few clients and no Wasmtime. docker exec wasm runs a command inside the lab's wasm service, which has Wasmtime and no shell, so the command is always wasmtime followed by what to do. For a shell, use docker exec -it tools sh.
- Why are the programs written in text and not compiled from Rust or C?
- No pinned image carries a compiler or component tools beside Wasmtime, and a lab downloads nothing when it starts. Wasmtime reads the WebAssembly text format directly, so the programs and components are written in it by hand and can be read in full. The lessons explain how cargo-component, componentize-py and jco build the same kind of component from a language.
- Does the lab run WASI 0.3 async code?
- Wasmtime 50 has WASI 0.3 on by default, and the lab runs a component that imports a WASI 0.3 interface. No component in the lab is async, because one written by hand is not readable; async functions and streams are explained in the lessons.
- Where does the Wasmtime image come from?
- From mitsurutech/wasmtime-image, a public repository anyone can read: it downloads the Bytecode Alliance's release archive, checks its SHA-256 and copies wasmtime in unchanged. The lab pins the result by digest, so it cannot change under you. It runs as an unprivileged account with no capabilities and no way out to the internet, like every lab service.