00 · Start Here & Course index
Start Here / 3-Minute Visual Primer
What Is WebAssembly (Wasm) & Bytecode Verification?
A 3-minute, zero-jargon guide to how browsers, Cloudflare Workers, and plugin sandboxes run compiled C++ and Rust safely at near-native speed.
How Do Browsers & Edge Clouds Run Untrusted C++ and Rust Safely?
When you open a Figma canvas, run code on Cloudflare Workers, or load a desktop app plugin, that code may have been compiled from C++ or Rust by a stranger. Running raw machine code directly on your CPU could read passwords out of neighboring memory or crash the app.
WebAssembly (Wasm) solves this in two steps: first, a fast bytecode verifier scans every instruction once before execution to guarantee stack types and jump targets are legal. Second, all memory reads and writes stay trapped inside one isolated 64 KiB memory wall (bytes 0..65535).
Click Each Step to Watch the Wasm Operand Stack Work
WebAssembly has no CPU register names like r1. Instead, values live on an operand stack (a push/pop tray where new items enter at the right). Try stepping through 10 + 20:
Why Byte 65,536 Traps Immediately
Every Wasm module gets its own byte array starting at 0. One page holds 65,536 bytes (indices 0..65535). Click below to test a 4-byte i32.store near the wall:
Six Terms You Will Use Across All 8 Chapters
Stack Types, Jumps & Unreachable Floors
See why Wasm accepts unreachable code behind br 0 while trapping bad writes, enforce i32/f32 stack types, and trace nested loop/block targets.
64 KB Memory Walls & Indirect Calls
Prevent 32-bit wraparound bugs from sneaking past page bounds, and verify host function signatures at indirect call tables.
Verifier Architecture & Audit Exam
Compare single-pass streaming validation against two-pass control-flow graphs, then audit and patch a real plugin before sign-off.
Lesson 01 / Stack & control
A skipped add and a four-byte write
Browsers and servers close to their users read WebAssembly instructions from strangers once before running them, so they can catch mistakes quickly. Here the program gets 65,536 bytes of its own memory (one 64 KiB page) to keep a bad write from changing anyone else’s data. Your app gives a plug-in that page, a row of numbered bytes. The plug-in wants to write four bytes. You get to check its instructions before it runs. Its jump, br 0, goes to the end of the innermost block of instructions, numbered 0. That jump skips an add. The add, i32.add, needs two whole numbers from a pile called the stack. That pile is empty. After the jump, the write starts three bytes from the end. Decide where the plug-in stops and how much memory it changes.
1 · The big idea
When a browser or plugin host loads a WebAssembly module, it runs two separate stages: first a verifier (type checker) scans every instruction once without executing anything; only if verification passes does the runner execute instructions inside an isolated linear memory page of 65,536 bytes (addresses 0 through 65535).
In WebAssembly, block ... end wraps a group of instructions, br 0 means “branch (jump) immediately to the end of the nearest group,” and drop discards the top value on the stack. Anything sitting between br 0 and end is unreachable dead code.
Here is the famous surprise: i32.add normally requires two 32-bit integers on the stack (the push/pop value tray), and every block records its entry height h (for example, h=0 means the stack height when the block opened). After br 0, the stack is empty—yet the verifier enters a polymorphic unreachable state where missing inputs match any required type, so the module loads cleanly! Then the runner skips the dead add, reaches i32.store (which writes 4 bytes starting at base + offset, where offset is added to the base address), checks whether all 4 bytes fit inside 0..65535, and traps before modifying a single byte.
2 · Worked step-by-step example
- Step 0–1 (Verifier): Enter
block, then hitbr 0. Verifier jumps its model to unreachable mode at stack height0. - Step 2–3 (Verifier): Sees
i32.addthendropon an empty unreachable stack → supplies placeholderi32types and accepts the program! - Step 1 (Runner): Executes
br 0and skips right overi32.addanddropstraight toend. - Step 4–6 (Runner): Pushes base
65532+ value42, addsoffset=1→ needs 4 bytes[65533, 65534, 65535, 65536]. Byte65536hits the page wall → trap! 0 bytes written.
Instruction path
Empty stack → jump over add → write
Inside: 65533 · 65534 · 65535 | Outside: 65536
3 · Bytecode stepper (explore before quizzing)
The verifier checks the kinds of values each instruction takes. A block groups instructions up to its end. Here br 0 jumps out of that group. drop removes the top value. i32.const puts a 32-bit whole number on top, and i32.store writes one to memory. After br, the rest of that block is unreachable. Its empty stack is polymorphic: a missing input can match any required type. The runner follows the jump. A failed memory check stops execution with a trap.
Bytecode is the list of machine instructions below. Step through Check types and Run values before answering the prediction below.
4 · Make a prediction
5 · Repair the program
Code workshop
Implement fits in my-rules.mjs. Check that every byte of a write fits. The first command makes your own copy of the starter if needed. Edit the named function, then run the second command.
cp -n workshop/starter.mjs my-rules.mjs
node workshop/check.mjs 1 ./my-rules.mjsOptional worked example in a separate file
In lab.mjs, write the first check for a one-page memory. Let end name the first byte after the write. Run your file with node lab.mjs.
function fits(start, width, size) {
const end = start + width
return start >= 0 && end <= size
}
console.log(fits(65533, 4, 65536))Expect false. Change 65533 to 65532. The last byte used is 65535, so an end equal to size must pass.
Key Takeaway & Next Up
WebAssembly accepts unreachable instructions with an empty polymorphic stack after br 0, then traps at runtime before any out-of-bounds write touches memory.
Next: Next, we inspect how the verifier checks reachable i32 and f32 types on the stack.
Edit the bytecode
Change the numbered inputs or instructions and run the checker again. Each run starts with a fresh page. This editor accepts a JSON object containing the decoded instructions.