WASM
A concise, high-level cheat sheet covering the Wasm Component Model, WASI, memory architecture, and compiler pipelines.
1. Core & Component Model Glossary
Section titled “1. Core & Component Model Glossary”- WebAssembly (Wasm): A portable bytecode format and sandboxed virtual machine; “Wasm” and “WebAssembly” are exact synonyms.
- Core Wasm Module: The foundational binary unit that works with numeric primitives and raw byte arrays (linear memory).
- Wasm Runtime: The host engine (e.g., Wasmtime, WasmEdge, V8) that acts like a virtual OS—loading, validating, sandboxing, and compiling Wasm bytecode to native CPU instructions.
- Wasm Component: The “executable/library binary” for a Wasm runtime; wraps Core Wasm modules with high-level WIT interface metadata.
- Wasm Component Model: The standard architecture allowing independently compiled components (across different languages) to link and interoperate safely.
- WIT (Wasm Interface Types): A language-neutral IDL (similar to Protobuf or Java/Rust interfaces) defining data types, functions, and contracts.
- WIT World: A component’s complete two-sided contract bundling all interfaces it imports (needs) and exports (provides).
- Canonical ABI: Rules that “lift” and “lower” rich WIT types (strings, lists, records) across isolated component memories without manual pointers.
- Component Composition: Wiring components together (e.g., via
wac) so one component’s exports satisfy another’s imports. wit-bindgen: Generates native language bindings (Rust traits, Java classes) directly from.witdefinitions.
2. WASI & Runtime Contracts
Section titled “2. WASI & Runtime Contracts”- WASI (WebAssembly System Interface): Standardized WIT interfaces (
wasi:filesystem,wasi:http,wasi:cli) giving components portable OS/cloud capabilities (currently at WASI 0.3 with native async). - Executable vs. Library Components:
- Executable (
wasi:cli/commandorwasi:http/proxy): Exports a host-invoked entry point (run()orhandle()), analogous to C’smain(). - Library Component: Exports custom WIT interfaces without a
main()entry point; meant to be composed into other components or called by a host.
- Executable (
- Static Link Verification & Optional Capabilities: Host runtimes validate all required WIT imports at load time and reject missing links immediately. Optional features are handled via build-time
--target-worldstubs, polyfill shim components, or runtimeerror-coderesults. - Containers vs. Wasm Sandboxes: Docker/OCI containers isolate full OS processes using Linux namespaces and cgroups; Wasm packs many isolated components inside a single OS process with microsecond startup and zero direct OS syscalls.
3. WIT Building Blocks & Large Data
Section titled “3. WIT Building Blocks & Large Data”- Data Types: Primitives (
u32,string, etc.),record(struct),variant(tagged union),enum,flags, and containers (list<T>,option<T>,result<T, E>). - Stateless Functions (
func): Independent functions carrying no hidden state across calls. - Stateful Resources (
resource): Opaque integer handles representing live objects with constructors and methods (also used to simulate callbacks/closures across languages). - Streams & Futures (
stream<T>,future<T>): Native WASI 0.3 primitives for non-blocking async values and chunked, backpressure-aware data flows. - Passing Large Data:
- Small/Medium: Value types (
list<u8>,record) are deep-copied across component memories via Canonical ABI. - Zero-Copy Read/Query: Keep data inside one component and pass a
resourcehandle. - Large Transfers: Stream chunks incrementally using
stream<T>to keep memory flat.
- Small/Medium: Value types (
4. Memory Architecture & Wasm 3.0
Section titled “4. Memory Architecture & Wasm 3.0”- Linear Memory: A private, zero-based contiguous byte array (
u8[]) per component; call stacks and executable code live outside it for security. - Hardware Guard Pages (
wasm32): Runtimes reserve an ~8–10 GB virtual address region per 4 GBwasm32memory usingPROT_NONEguard pages, letting the CPU’s MMU catch out-of-bounds accesses in hardware with zero branch overhead. - Memory64 (
wasm64): Expands linear memory past 4 GB using 64-bit pointers; incurs ~5–15% overhead unless bounded by a smaller max size (e.g., 16–64 GB) that still fits inside hardware guard-page reservations. - Multiple Memories (Wasm 3.0): Allows one module to hold multiple independent
wasm32memories (memory 0,memory 1…), scaling past 4 GB and isolating sensitive data while keeping full guard-page speed. - WasmGC (Wasm 3.0): A separate, host-managed garbage-collected heap (
structandarrayreferences) built into the Wasm engine so languages like Kotlin, Dart, Java (TeaVM/GraalVM), OCaml, and Scala.js don’t bundle their own GC.
5. Compiler Pipelines & IR Hierarchy
Section titled “5. Compiler Pipelines & IR Hierarchy”- IR Abstraction Ranking (High to Low):
- Java Bytecode: Full OOP classes, methods, and managed references.
- Scala Native NIR: Hybrid OO classes + low-level C pointers.
- Wasm / WasmGC: Structured control flow + either opaque GC types (WasmGC) or raw linear memory offsets.
- LLVM IR: Low-level, hardware-agnostic SSA assembly with raw pointers (
ptr) and byte offsets.
- Lowering vs. Lifting: Compiling high-to-low is straightforward; going low-to-high loses object/type context. Because LLVM lowers objects to raw pointers, LLVM does not target WasmGC—GC languages compile from high-level IRs (like Scala.js
SJSIR) directly to WasmGC and optimize via Binaryen (wasm-opt). - Two-Stage Compilation: Systems languages (Rust/C++) use LLVM #1 at build time to emit portable
.wasm, and runtimes (Wasmer/WasmEdge) use LLVM #2 (or fast compilers like Cranelift in Wasmtime) at deploy/run time to generate native CPU machine code.