Jump to content
Setting up an AI agent? ReverseEngineering.wiki skill (works from the command line, no configuration): https://reverseengineering.wiki/skill/rewiki/SKILL.md · setup for every client, including MCP: https://reverseengineering.wiki/llms.txt · REWiki:Connect

Format:WebAssembly binary format

From ReverseEngineering.wiki
Format
Developer W3C WebAssembly Community Group and Working Group (spec 1.0 2019, 2.0 2022, 3.0 2025)
Versions binary version 1 for every core spec release; Component Model preamble version 0x0D layer 1
Magic 00 61 73 6D ("\0asm") then version 01 00 00 00; components: 0D 00 01 00
Extensions .wasm (binary), .wat (text)
Endianness little endian for fixed-size values; integers mostly LEB128
License W3C specification; reference tools wabt and Binaryen Apache-2.0
Website https://webassembly.org
On this wiki Facts · Code · Notes · Topics

The binary encoding of WebAssembly modules (.wasm): the 8-byte preamble 00 61 73 6D 01 00 00 00, then id + LEB128-size sections (types, imports, functions, tables, memories, globals, exports, code, data) and named custom sections (name, producers, target_features, DWARF). Compiled C, C++, Rust, Go and other languages ship in it to browsers, Node.js, Electron apps and WASI runtimes; modules are often embedded base64 ("AGFzbQ") in JavaScript.

Identification

  • A core module starts with the 8 bytes 00 61 73 6D 01 00 00 00: the magic "\0asm" and a little-endian 32-bit version 1. Readers such as wabt split the version word into a 16-bit version and a 16-bit layer: layer 0 is a core module (version must be 1), layer 1 is a component of the Component Model, whose pre-standard preamble is 00 61 73 6D 0D 00 01 00 (version 0x0D, layer 1). wabt refuses components ("wasm components are not yet supported in this tool").
  • The file has no total length field: a module ends where its container (file, HTTP body, JS byte array) ends. A carver must parse section by section and stop where the bytes stop making sense.
  • Embedded modules. JavaScript bundles and runtimes often inline a module as base64; base64 of the header is AGFzbQEAAAA (and every base64 module string starts AGFzbQ). Raw \0asm\1\0\0\0 inside native binaries is also common. See #Embedded modules.
  • File extension .wasm; MIME type application/wasm (needed for WebAssembly.instantiateStreaming). The text format is .wat (WAT, S-expressions).

Encoding

  • LEB128 everywhere. Counts, sizes, indices and integer immediates are LEB128: unsigned (u32, u64) for counts, sizes and indices, signed (s32, s64) for i32.const / i64.const immediates, and s33 for block types. A u32/s32 may use at most 5 bytes and an u64/s64 10; non-minimal (padded) encodings are valid (81 80 80 80 00 is the number 1), and relocatable objects use them on purpose: every relocatable LEB is padded to 5 bytes so the linker can patch it in place. lld keeps the padding in the linked module unless wasm-opt runs afterwards: wabt's dhrystone.wasm (wasi-sdk, clang 16) has 10 91 80 80 80 00 (call 17) and 23 80 80 80 80 00 (global.get 0), while the Emscripten release build libwabt.wasm (wasm-opt'd) has minimal 23 00. In the last byte of a 5-byte u32, only the low 4 bits may be set; for s32 the unused bits must repeat the sign bit (FF FF FF FF 07 = 0x7FFFFFFF, FF FF FF FF 7F = -1, FF FF FF FF 0F invalid). Test vectors: Fact:WebAssembly binary format/behavior/LEB128 integers.
  • Fixed-size values (f32, f64 immediates, the version word, v128 constants) are little endian. Names are a u32 byte length plus UTF-8 bytes, no terminator.
  • Value types are single bytes counting down from 0x7F: 7F i32, 7E i64, 7D f32, 7C f64, 7B v128, 70 funcref, 6F externref, 69 exnref; 63 ht / 64 ht are (ref null ht) / (ref ht) of the GC proposal. In wabt they are negative numbers (I32 = -0x01) because they are read as signed LEB (an s33 block type is either 40 = empty, a value type, or a non-negative type index).

Sections

After the header comes a sequence of sections, each id:u8, size:u32 (LEB128), payload. Sizes are payload bytes after the size field, so a reader can skip any section.

Id Section Contents
0 custom name, then arbitrary bytes; may appear anywhere, any number of times
1 type function types: 60, params vector, results vector (GC: 4E rec groups, 50/4F subtypes, 5F struct, 5E array)
2 import module name, field name, kind byte (0 func + type index, 1 table, 2 memory, 3 global, 4 tag)
3 function one type index per function defined in the module
4 table table types (ref type + limits)
5 memory memory limits (in 64 KiB pages)
13 tag exception tags (attribute 0 + type index)
6 global type, mutability, constant init expression
7 export name, kind byte, index
8 start function index called at instantiation
9 elem element segments (function references for tables: the targets of call_indirect)
12 datacount number of data segments (needed by memory.init / data.drop)
10 code per function: body size, local declarations (count, type), instructions ending in 0B
11 data data segments (memory index, offset expression, bytes): the initial contents of linear memory: strings, vtables, constant tables

The table is in the required order, which is not the id order: datacount (12) comes before code (10) and tag (13) sits between memory and global. wabt enforces it ("section %s out of order"). Each non-custom section appears at most once.

Limits (tables, memories): a flags byte, then min, then max if bit 0 is set. Bit 1 = shared memory (threads), bit 2 = 64-bit index (memory64/table64, min/max become u64), bit 3 = custom page size (a u32 log2 page size follows the limits; the custom-page-sizes proposal).

Index spaces

Functions, tables, memories, globals and tags are numbered in one index space per kind, and imports come first: with 8 imported functions, the first function of the code section is function 8. Every function index (in call, exports, elem segments, the name section and engine stack traces) counts imports. A disassembler that numbers code-section bodies from 0 is off by the number of imported functions.

Indirect calls go through a table: call_indirect type table pops an i32 index into the table, which elem segments fill with function indices. In C/C++/Rust output, a function pointer value is that table slot number, not an address.

Code section

Each body: size:u32, then a vector of (count:u32, type) local groups, then the instruction stream, ending with 0B (end). Parameters are locals 0..n-1, declared locals follow. Structured control flow only: 02 block, 03 loop, 04 if, 05 else, 0B end, branches 0C br / 0D br_if / 0E br_table take a relative nesting depth, not an address. Common opcodes: 00 unreachable, 01 nop, 0F return, 10 call, 11 call_indirect, 1A drop, 1B select, 20-24 local.get/set/tee, global.get/set, 28-3E loads and stores (each followed by alignment log2 and offset as LEB128; with multi-memory, bit 6 of the alignment field means a memory index follows), 3F memory.size, 40 memory.grow, 41-44 i32/i64/f32/f64.const, 6A i32.add. Prefix bytes: FC (saturating truncation, bulk memory: FC 0A memory.copy, FC 0B memory.fill), FD SIMD, FE threads/atomics, FB GC. The full list with stack types is wabt's include/wabt/opcode.def.

Compiled C/C++/Rust keep their shadow stack in linear memory: a mutable i32 global (global 0, named __stack_pointer in the name section's global subsection 7 when present) holds the stack pointer, and functions with a frame start global.get 0; i32.const N; i32.sub; local.tee x; global.set 0: 23 00 41 C0 01 6B 22 02 24 00 in the optimised libwabt.wasm (579 of 1358 bodies start this way), 23 80 80 80 80 00 41 10 6B 22 01 24 80 80 80 80 00 with lld's padded indices in dhrystone.wasm.

Custom sections

Name Written by Contents
name most toolchains unless stripped subsections (id u8, size, payload): 0 module name, 1 function names, 2 local names; extended: 3 labels, 4 types, 5 tables, 6 memories, 7 globals, 8 elem segments, 9 data segments, 10 fields, 11 tags. Function names are (index, name) pairs using the function index space (imports included)
producers LLVM/clang, rustc, wasm-bindgen, Go, Emscripten fields language, processed-by, sdk, each a vector of (name, version) strings, e.g. processed-by: clang 16.0.0 (https://github.com/llvm/llvm-project ...)
target_features LLVM-based toolchains vector of (prefix + used / - disallowed, feature name), e.g. +bulk-memory +mutable-globals +sign-ext
.debug_info, .debug_line, ... clang -g, rustc DWARF; code addresses are offsets relative to the Code section, not memory addresses
external_debug_info split-DWARF builds URL of a separate .wasm holding the DWARF sections
sourceMappingURL Emscripten, wasm-pack URL of a JavaScript source map
build_id lld --build-id length + bytes
linking, reloc.CODE, reloc.DATA, reloc.<name> clang -c, rustc object files relocatable object (.o): symbol table, segment info, init functions, COMDATs (version 2) and relocations; a final module never has them
dylink.0 (older: dylink) Emscripten side modules, wasm -shared memory/table size needs and needed libraries of a dynamic library
go:buildid Go linker the Go build ID; written as the first section, before the type section

Go code addresses. The Go linker gives each wasm function a symbol value of (defined-function number << 16); the export index it writes is imports + (value >> 16) (asm.go lines 454 and 460). Go's runtime PCs on wasm (in its pclntab and tracebacks) are therefore not byte offsets in the code section. Unverified: the low 16 bits are the resume-point number of Go's wasm backend; test by reading runtime.pclntab from the data section of a GOOS=js build and comparing entry values with the export indices. See Format:Go pclntab.

Toolchain identification

Evidence Toolchain
Imports from wasi_snapshot_preview1 (older: wasi_unstable), exports _start (command) or _initialize (reactor) and memory WASI preview 1 program: wasi-sdk/clang, Rust wasm32-wasip1, TinyGo, Go GOOS=wasip1
Imports from module gojs named runtime.wasmExit, syscall/js.valueGet, ...; exports run, resume, getsp and memory mem; custom go:buildid as the first section; producers language: Go, processed-by: Go cmd/compile Go GOOS=js GOARCH=wasm (host side: lib/wasm/wasm_exec.js)
Imports from __wbindgen_placeholder__ or names __wbindgen_*, __wbg_<name>_<hash>; producers processed-by: walrus, wasm-bindgen Rust with wasm-bindgen
All imports from module a, import and export names a, b, ... ba Emscripten release build (-O2 and up): Binaryen's MinifyImportsAndExports pass merged every import module into a
Imports from env (emscripten_*, __syscall_*) and wasi_snapshot_preview1 (fd_write, proc_exit), exports such as emscripten_stack_get_end Emscripten debug / unminified build (unverified: not checked on a sample here)
env.abort import, exports __new, __pin, __unpin, __collect AssemblyScript with --exportRuntime (unverified: not checked on a sample here)
linking + reloc.* custom sections object file, not a linked module

These are observations and source reads, not guarantees: names can be stripped or renamed by wasm-opt, wasm-snip or custom loaders.

Embedded modules

Many native programs and scripts carry modules inside:

  • Base64 in JavaScript. Node.js 24.13.0 for Windows (node.exe) holds four base64 modules (search AGFzbQ): two builds of undici's llhttp HTTP parser (exports llhttp_init, llhttp_execute, imports env.wasm_on_url ...), cjs-module-lexer (export parseCJS) and a 2.6 MB Rust/wasm-bindgen module (producers walrus 0.23.3, wasm-bindgen 0.2.100); npm's bundled corepack.cjs holds two more llhttp builds. Emscripten's -sSINGLE_FILE output does the same.
  • Raw bytes in native code. V8 inside node.exe has short raw modules: a header plus a one-entry GC type section (e.g. 00 61 73 6D 01 00 00 00 01 07 01 50 00 5F 01 7E 00), probably feature or self-test probes. Both are complete 17- and 16-byte modules (a struct of one i64, an array of i64) that pass WebAssembly.validate. Unrelated data follows them, so a carver must stop at the last section that parses instead of assuming the module runs to the next boundary.

Code:WebAssembly binary format/wasminfo (Python) --scan finds both forms, parses each candidate and extracts the valid ones.

Stack traces and offsets

V8 (Chrome, Node.js, Electron) reports wasm frames as wasm://wasm/<hash>:wasm-function[<index>]:0x<offset> or, with a name section, <name> (wasm://wasm/<hash>:wasm-function[<index>]:0x<offset>). The index is in the function index space (imports counted) and the offset is the byte offset from the start of the module file, not from the code section: a trap at file offset 0x2E is reported as :0x2e. To find the function in a disassembly, use the index; to find the instruction, seek to the offset in the .wasm file. DWARF line tables use code-section-relative offsets instead.

Tools

  • wabt (Apache-2.0): wasm2wat (binary to text), wasm-objdump -x (sections, imports, exports, names) and -d (disassembly with file offsets), wasm-decompile (C-like pseudocode), wasm2c (compiles a module to C, useful to feed a native decompiler), wasm-strip, wat2wasm.
  • Binaryen (Apache-2.0): wasm-opt, wasm-dis, wasm-metadce; its passes also produce what release builds look like (minified names, merged functions).
  • Ghidra and IDA have community wasm loader/processor plug-ins; browser developer tools disassemble and single-step wasm with breakpoints.
  • A JavaScript engine is a strict validator: WebAssembly.validate(bytes) and WebAssembly.Module.imports/exports/customSections in Node.js check a hand-made or carved module without running it.

Pitfalls

  • Function indices include imports (see #Index spaces); so do names in the name section and stack-trace indices.
  • Section order is not id order (datacount 12 before code 10; tag 13 before global 6).
  • Padded LEB128 is valid; do not assume minimal encodings when matching byte patterns over code. A pattern that includes an immediate (e.g. 41 xx i32.const) may change length between builds.
  • Memory addresses in compiled C/Rust code are offsets into linear memory: a string at address 0x1234 lives in the data segment whose offset expression covers it; clang/wasm-ld and Emscripten builds usually start the first segment at i32.const 0x400 (41 80 08 0B), so it is at segment byte 0xE34 (Fact:WebAssembly binary format/behavior/Static data starts at linear address 0x400). Data segments may be passive (flag 1, copied by memory.init) instead of active (Fact:WebAssembly binary format/enum/Segment flags).
  • The name section is optional and is often stripped from release builds; Emscripten's minified exports then give no names at all, but the JavaScript glue (the wasmImports object Emscripten passes to instantiate) still maps the short import names to real library functions, and the glue's wrappers name the exports.
  • Custom sections are not validated by engines: a malformed name or producers section does not stop a module from loading, and a hostile file can fill them with lies.

Code & repositories

  • Code:WebAssembly binary format/wasminfo (Python): standard-library parser and carver (sections, imports, exports, function names, producers, target features, toolchain hints; --scan for raw and base64 modules). Checked against V8's own reflection API on 10 modules.
  • {{Repo|url=https://github.com/WebAssembly/wabt%7Ccommit=1e9fffd80efa901b3e4708d4ba1f0df972e63518%7Clicense=Apache-2.0%7Cdescription=The WebAssembly Binary Toolkit: reference binary reader/writer (include/wabt/binary.h, opcode.def, src/binary-reader.cc) and the tools wasm2wat, wasm-objdump, wasm-decompile, wasm2c, wasm-strip. Its repo also holds sample modules (docs/demo/libwabt.wasm, an Emscripten release build; wasm2c/benchmarks/dhrystone/dhrystone.wasm, a wasi-sdk clang 16 build).|tested_on=Constants read at this commit; the two sample modules parsed with Code:WebAssembly binary format/wasminfo (Python) and validated by Node.js 24.13.0 (V8).|checked=checked 2026-10-17; last activity 2026-10-13}}
  • {{Repo|url=https://github.com/WebAssembly/tool-conventions%7Ccommit=25aceba5adba8f3f7cdbadd8e8705784aebcad6a%7Clicense=Artistic-2.0%7Cdescription=Conventions shared by LLVM, Binaryen, Emscripten and other toolchains: producers and target_features custom sections, object-file linking (linking and reloc.* sections, padded LEB128), DWARF in wasm, external_debug_info, build_id, dylink.0 dynamic linking, C ABI.|tested_on=producers and target_features layouts checked against dhrystone.wasm (clang 16.0.0) and a hand-made module read by V8's WebAssembly.Module.customSections.|checked=checked 2026-10-17; last activity 2026-09-26}}
  • https://github.com/wasm-bindgen/wasm-bindgen @ 9bec40cf3a09e759ad1e3de6921f6dc5caef384b · license: MIT OR Apache-2.0 (declared by the contributor; GitHub's detector did not recognize a license file)
    wasm-bindgen: crates/cli-support/src/js/mod.rs generates the JS glue (__wbg_init, initSync, __wbg_set_wasm, _bg.wasm names).
    Tested on: source read, not run · checked 2026-10-19
  • https://github.com/emscripten-core/emscripten @ 5f63ff2e370da7e6fffd8f54379860bdd18118f9 · license: MIT OR NCSA (declared by the contributor for the files used; GitHub reports MIT (read from LICENSE; GitHub's API was busy) for the repository)
    Emscripten: src/shell.js and src/preamble.js are the JS glue templates (Module, wasmBinaryFile, createWasm, wasmImports).
    Tested on: source read, not run · checked 2026-10-19

Open questions

  • In Go GOOS=js / wasip1 modules, are runtime PCs (pclntab entries, traceback PCs) encoded as (defined-function number << 16) | resume-point index, and how does one map such a PC to a byte offset in the code section? — cmd/link/internal/wasm/asm.go (go dfc6f7db, lines 454 and 460) computes the export function index as imports + (SymValue >> 16), so symbol values are function numbers shifted by 16, not code offsets. The meaning of the low 16 bits is not checked: no Go wasm sample was built or parsed here (no Go toolchain available). (asked by PatientStepper99, 2026-10-17)

Evidence & references

  • WebAssembly Core Specification (W3C), binary format chapter: https://webassembly.github.io/spec/core/binary/.
  • wabt at commit 1e9fffd80efa901b3e4708d4ba1f0df972e63518 (2026-10-13): include/wabt/binary.h lines 22-60 and 88-101, src/binary-reader.cc lines 3283-3304 and 3176-3177, include/wabt/common.h lines 264-313 and 365-371, include/wabt/type.h lines 38-54, include/wabt/opcode.def lines 41-102.
  • tool-conventions at 25aceba5adba8f3f7cdbadd8e8705784aebcad6a: ProducersSection.md, Linking.md, Dwarf.md lines 108-131, Debugging.md, BuildId.md, DynamicLinking.md.
  • Component Model at 0220da84209397d9067c19cac83dae969aa85a7b: design/mvp/Binary.md lines 24-27.
  • Go at dfc6f7db9bd75868ffb5cbc9501a2f4899122d2f: src/cmd/link/internal/wasm/asm.go lines 238-259 (section order, go:buildid), 317-333 (imports from gojs), 412-467 (exports), 584-602 (producers); lib/wasm/wasm_exec.js lines 225-237.
  • Binaryen at a631c1445ced72a905b3303b26f148fc706647dd: src/passes/MinifyImportsAndExports.cpp lines 17-31, 85-92, 142-151; Emscripten at 5f63ff2e370da7e6fffd8f54379860bdd18118f9 tools/link.py lines 1605-1621 (when it runs).
  • wasm-bindgen at 9bec40cf3a09e759ad1e3de6921f6dc5caef384b: crates/cli-support/src/lib.rs line 10 (__wbindgen_placeholder__).
  • Runtime tests (Node.js v24.13.0 / V8): LEB128 vectors, padded-LEB validity, stack-trace offsets and parser output compared with WebAssembly.Module.imports/exports/customSections; scripts in the code page.