Introduction
A .wasm file is a compiled WebAssembly module: compact bytecode for a
simple, portable virtual machine. Every major web browser runs it beside JavaScript,
and standalone runtimes run it outside the browser. Compilers for C, C++, Rust, Go and
other languages can produce it.
The same module can be written as text in the .wat format, which tools
convert to and from the binary.
Structure
A module starts with an 8-byte header: the magic bytes 00 61 73 6D
(\0asm) and the version, 1, as a little-endian 32-bit number. Then come
sections. Each is one byte of section ID, its size as an unsigned LEB128 number, and its
contents. The standard sections appear at most once each and in a fixed order: type,
import, function, table, memory, tag (WebAssembly 3.0), global, export, start,
element, data count, code and data. Custom sections (ID 0) can appear anywhere and
hold extras such as function names for debuggers.
Every section’s layout, with the value encodings and the name section, is on the module and sections page; every type code on types; and the full opcode table, including the garbage-collection and vector instructions, on instructions.
Real files also contain features outside the 3.0 specification, which the pages
cover too: the threads proposal (atomic instructions under the FE prefix
and shared memory limits), the legacy exception-handling opcodes, and the custom
sections defined by the WebAssembly tool conventions (producers,
target_features, linking, reloc.*,
dylink.0, sourceMappingURL, external_debug_info)
together with the extended name-section subsections. It also covers
the phase 4 proposals that are not yet in release 3.0 (threads, wide arithmetic with
i64.add128 and its relatives, compact imports) and the branch-hint
section.
A .wasm file is not always a module. A WebAssembly component
has the same magic number, but its next four bytes are 0D 00 01 00
(version 0x0d, layer 0x01) instead of
01 00 00 00. Components are a separate format with their own
pages, not described here.
0magic00 61 73 6D4 bytes4version01 00 00 004 bytes8sections, one after anothervaries+0id: 1 type, 2 import, 3 function … 10 code, 11 data1 byte+1size of the contents, LEB1281 to 5 bytes…contentssize bytes
Not described
Known, not yet covered
- Viewer: the viewer checks the structure of the file and validates the instructions as the specification's validation chapter and its validation algorithm say (operand and control stacks, polymorphic stacks, block types, branches, locals of non-defaultable types, references and the type hierarchy with recursive groups, exception handling, constant expressions); each failure is a problem at its instruction with the rule named, and a branch in the disassembly shows the block it goes to and the offset. What is not done: a function stops being checked after its first error only within a block (the rest of the block is treated as unreachable), so later errors in the same block are not listed; and V8 refuses modules for its own limits (a larger table or memory than it supports) that the viewer, following the specification, accepts. The reference pages were compared row by row with the raw WebAssembly 3.0 specification source (github.com/WebAssembly/spec, commit 943c5df, the binary-format grammar files for values, types, instructions and modules, and the name section appendix), including all 0xFD vector instructions.
- Viewer: the
.debug_*custom sections are labelled as DWARF and shown as bytes, not decoded; themetadata.code.trace_inst, coredump and compilation-hints sections are labelled as known and not decoded, and any other custom section is labelled unknown. A branch hint is checked to sit on aniforbr_ifbyte, not on an instruction boundary. - Viewer: a component (the same magic number with
0D 00 01 00after it) is a WebAssembly component of the component model and opens in the component viewer, not in this one; its reference pages and record are separate. A core module embedded in a component opens in this viewer. Limits flags08to0F(the custom-page-sizes proposal, not described on the reference pages) are read as a page size after the limits, as the proposal lays them out. - Reference: not covered (proposal at phase 3, not blocking): custom page sizes, which adds limits flags 08 to 0F (
proposals/custom-page-sizes/Overview.md). Mainstream tools do not write it by default. - Reference: not covered (inactive proposal, not blocking): the
metadata.code.trace_instcode-metadata section (Instrument and Tracing Technology, listed as inactive). - Reference: not covered (experimental or work in progress, not blocking): the coredump custom sections (
Coredump.md, “experimental”), module signatures (Signatures.md, “work in progress”) and the compilation-hints proposal (phase 2). - Reference: not covered (separate format, not blocking): WebAssembly components. A component starts with the same magic but has version
0D 00and layer01 00; it is a different format that shares the.wasmextension, so it has its own pages and record, and the explorer tells the two apart by those bytes. - Reference: not covered (the linker’s behaviour, not file contents): the rules for how a linker merges sections and processes relocations in
Linking.md, and the experimental threading conventions. - Viewer: the viewer’s validation was run against the specification’s test suite only in part: the spec’s text-format
assert_invalidmodules (about 1,300) were not run, because nowat2wasmis available to turn them into binaries; the 812 modules that the suite already holds in binary form were run, and the viewer’s verdicts were compared with V8’s.
Also used for
- WebAssembly component – WebAssembly component model binary: nested modules, typed imports and exports.
Links
Software
- Web browsers – Chrome, Firefox, Safari and Edge all run WebAssembly beside JavaScript
- Node.js – JavaScript runtime that loads WebAssembly modules (Windows, macOS, Linux)
- Wasmtime – standalone WebAssembly runtime from the Bytecode Alliance (Windows, macOS, Linux)
- Wasmer – standalone WebAssembly runtime (Windows, macOS, Linux)
- WABT – the WebAssembly Binary Toolkit, with
wasm2watandwasm-objdump(Windows, macOS, Linux) - Binaryen – compiler toolkit with the
wasm-optoptimiser (Windows, macOS, Linux) - Ghidra – reverse engineering suite, with a WebAssembly extension (Windows, macOS, Linux)
Developers
- wasm-tools – tools and the
wasmparserandwasm-encodercrates (Rust) - WABT – a library as well as the command-line tools (C++)
Reference
- WebAssembly core specification – the standard, including the binary format
- WebAssembly – guide and reference on MDN Web Docs