Full name: WebAssembly: a name joining web and assembly, not an abbreviation; Wasm is its short form.
Definition
WebAssembly (Wasm) is a portable binary instruction format: compilers translate languages such as C, C++ and Rust into WebAssembly modules, which a runtime loads and runs. Validation is the runtime’s check, before a module runs, that every instruction is well typed and every branch targets a declared place. Linear memory is a resizable byte array §14.5 that a module reads and writes by address; any access beyond its end stops the module. Host imports are functions the runtime supplies to the module: the module’s only way to affect anything outside itself.
The idea
A web browser, a server or an embedded device often runs a program written by someone else, such as a plugin compiled from C. The host, the program that runs the module, must stop the module from reading the host’s secrets, overwriting the host’s memory or calling the operating system on its own. WebAssembly gives the host three controls. Validation fixes, before the module runs, where its code can jump and what each instruction may be applied to. Linear memory gives the module one array of bytes and checks every load and store against that array’s size. Host imports are the only doors to the outside, and the host decides which doors exist.
The confinement is drawn around the whole array, not around the objects inside it. A C compiler places the program’s arrays, structures, heap blocks and many local variables side by side in the one linear memory. The runtime knows only the size of the array. An overflow from one C array into its neighbor stays inside the array, so the runtime sees a legal store.
A tenant who rents a whole floor of a building is a fair picture. The building’s guards stop the tenant from entering other floors, but they do not know where the tenant’s rooms are, so nothing stops water from one room soaking the next. The picture stops being accurate in one respect: the building also keeps the tenant’s code, and the record of which function called which, in a locked room that the tenant cannot enter, so no flood on the floor reaches them.
How it works
The module. A module is a binary file that declares function types, functions, a table of function references, a memory, global variables, imports, and exports, the functions it offers to the host. Function bodies are bytecode §2.4: instructions encoded for an abstract machine instead of a real processor. The abstract machine is a stack machine §2.5: each instruction takes its operands from an operand stack and pushes its result back. Control flow is structured: the instructions
block,loopandifopen nested regions, and a branch names an enclosing region, never an address.Validation. Before the module runs, the runtime checks the types of every function in one pass. For each instruction it checks that the operand stack holds values of the types the instruction needs, that every branch names an enclosing region, that every call names an existing function with matching types, and that every memory instruction names an existing memory. The runtime refuses a module that fails any check.
Execution state outside linear memory. The runtime keeps the operand stack, the local and global variables, and the call stack with its return addresses in its own memory, which no load or store of the module can reach. When a C compiler needs an address for a local variable, as it does for an array, it keeps that variable on a second stack, often called the shadow stack, which the compiled code manages inside linear memory.
Bounds on every access. The size of linear memory is a whole number of pages of 65,536 bytes. The memory grows only when the module executes the instruction
memory.growor the host grows it, up to an optional declared maximum. A load or store computes its effective address, the address operand plus a constant offset written in the instruction. If any byte of the access lies beyond the current size, the instruction traps: execution stops with an error, and control returns to the host. Runtimes implement this runtime bounds check §2.27 either with compare instructions or by reserving a large range of virtual addresses whose unused part is left unmapped, so that the memory management unit §6.9, the hardware that checks every address, faults on an out-of-range access.Indirect calls. A C function pointer becomes an index into the module’s function table. The instruction
call_indirectchecks that the index lies inside the table and that the function found there has the expected type, and traps otherwise.Host imports. The module declares each import by a module name, a field name and a type, and the host supplies a matching function when it instantiates the module. Calling an import runs host code, usually native machine code, with all of the host’s authority. A host function usually receives an address and a length inside linear memory, and it must itself check that range against the current size. WASI (WebAssembly System Interface) is a standard set of such imports for files, clocks and similar services.
Translation. An interpreter §2.13 executes the bytecode directly; a just-in-time compiler §2.14, which translates while the program runs, or an ahead-of-time compiler §2.15, which translates beforehand, turns it into machine code and must emit every bounds check and type check correctly.
Worked example
Take a linear memory of one page: 65,536 bytes, with addresses 0 to 65,535. A C program keeps an array a of one 4-byte integer at addresses 8 to 11, and a 4-byte integer b, which holds a security setting, directly after it at addresses 12 to 15.
The program writes
a[1], one element past the end ofa. Its address is , and the 4-byte store covers addresses 12 to 15. The check is , so the store proceeds and overwritesb. The confinement held; the C program’s object bounds did not.A 4-byte store with address operand 65,530 and constant offset 4 has effective address and needs bytes 65,534 to . The last legal address is 65,535, and the check is false, so the store traps.
A 4-byte store at effective address 65,532 needs bytes 65,532 to 65,535. The check is true, so the store proceeds.
The program follows a null pointer §4.26 and reads 4 bytes at address 0. The check is true, so the load returns whatever bytes 0 to 3 hold. A native program would usually be stopped here, because operating systems leave address 0 unmapped; in linear memory, address 0 is ordinary memory.
After
memory.growadds one page, the size is bytes, and the store of step 2 now proceeds: . The bound follows the current size of the memory, not any object.
Stated precisely
Let be the current size of a linear memory in bytes, a multiple of 65,536. For a load or store of bytes with address operand , an unsigned 32-bit integer, and constant offset , the effective address is , computed as an exact integer that does not wrap around. The access proceeds if and traps otherwise. With 32-bit addresses, is at most bytes, which is 4 GiB.
The confinement guarantee has three parts. Every load and store that a validated module executes touches only bytes 0 to of its own linear memory. Every transfer of control goes to a target that validation allowed: a branch target, a function entry, a function of the expected type in the table, or a return point. The module affects anything outside its own memory, table and globals only by calling imports. The guarantee rests on three assumptions.
The validator, the interpreter or compiler, and the checks the compiler emits implement the WebAssembly specification: a question of compiler correctness §3.40.
Every host import keeps its contract: it checks every address range it receives, gives the module no access to host memory, and treats the module’s data as untrusted.
The hardware and operating-system features that the runtime relies on, such as unmapped guard regions, work as specified.
Object-level memory safety is a different property. Spatial memory safety §2.28 requires each access to stay inside the object its pointer was derived from; temporal memory safety §2.29 requires that object to be still allocated. Neither follows from the confinement guarantee, because the runtime does not know where any object begins or ends.
Limits and common mistakes
Inside linear memory, a C program keeps the weaknesses of C. An overflow from one object into the next, a use-after-free §4.6 through a dangling pointer §4.1, one that still points to freed memory, which the C allocator may have handed out again, and a read through a null pointer all proceed without a trap. Linear memory also has no read-only part, so data that a native program keeps in read-only memory, such as string constants, can be overwritten.
Return addresses are out of reach, but a corrupted function pointer can still call any function in the table that has the expected type.
The host is in the trusted computing base §1.4 (TCB) of every sandbox §2.30 claim. A host import that writes outside the range it was given, keeps a native pointer into linear memory across memory.grow, which may move the memory to new host addresses, or performs for the module an action the module should not be allowed breaks the sandbox although validation passed; the last case is a confused deputy §1.25.
Validation does not bound running time or memory use. A validated module can loop forever or grow its memory to the declared maximum, so availability §1.10 needs limits such as a cap on executed instructions or a timer.
Bounds checks constrain completed instructions only. A processor that guesses a branch outcome can briefly perform an out-of-bounds load before the check resolves, the side channel §1.29 that Spectre attacks exploit.
The version matters. WebAssembly 1.0 allows one memory per module, with 32-bit addresses; later versions allow several memories and 64-bit addresses, which change the bounds arithmetic. A claim about a sandbox must name the version, the runtime and its host imports.
Questions answered
Does linear-memory confinement imply object-level memory safety?
No. Linear-memory confinement means that every load and store of a WebAssembly module stays inside the module’s linear memory, the single byte array the module may address, because the runtime checks each access against the array’s current size. Object-level memory safety means that every access stays inside the particular object, such as a C array or structure, that its pointer refers to, and that the object is still allocated. A compiler places all of a C program’s objects side by side in the one linear memory, and the runtime does not know where any of them begins or ends. An overflow from one array into the next object, a use-after-free and a read through a null pointer, which is an ordinary read of address 0, therefore all proceed without a trap.
Confinement protects the host from the module; it does not protect the module’s objects from each other. Object-level safety needs a memory-safe source language, checks inserted by the compiler or a separate checking tool. Even confinement itself depends on correct validation and translation, and on host imports that check every address range they receive.
Related entries: Virtual machine (VM) (§2.3), Buffer overflow (§4.4), Object lifetime (§4.1), Trusted computing base (TCB) (§1.4), Sandbox (§2.30), Runtime bounds check (§2.27), Spatial memory safety (§2.28), Temporal memory safety (§2.29), Guest-host ABI (§2.37), Software fault isolation (SFI) (§2.41). Further reading: [155].