Modules

The binary encoding of modules is organized into sections. Most sections correspond to one component of a module record, except that function definitions are split into two sections, separating their type declarations in the function section from their bodies in the code section.

Note

This separation enables parallel and streaming compilation of the functions in a module.

Indices

All indices are encoded with their respective value.

typeidx::=x:u32xfuncidx::=x:u32xtableidx::=x:u32xmemidx::=x:u32xglobalidx::=x:u32xelemidx::=x:u32xdataidx::=x:u32xlocalidx::=x:u32xlabelidx::=l:u32l

Sections

Each section consists of

  • a one-byte section id,

  • the u32 size of the contents, in bytes,

  • the actual contents, whose structure is dependent on the section id.

Every section is optional; an omitted section is equivalent to the section being present with empty contents.

The following parameterized grammar rule defines the generic structure of a section with id N and contents described by the grammar B.

sectionN(B)::=N:byte  size:u32  cont:Bcont(ifsize=||B||)|ϵϵ

For most sections, the contents B encodes a vector. In these cases, the empty result ϵ is interpreted as the empty vector.

Note

Other than for unknown custom sections, the size is not required for decoding, but can be used to skip sections when navigating through a binary. The module is malformed if the size does not match the length of the binary contents B.

The following section ids are used:

Id

Section

0

custom section

1

type section

2

import section

3

function section

4

table section

5

memory section

6

global section

7

export section

8

start section

9

element section

10

code section

11

data section

12

data count section

Custom Section

Custom sections have the id 0. They are intended to be used for debugging information or third-party extensions, and are ignored by the WebAssembly semantics. Their contents consist of a name further identifying the custom section, followed by an uninterpreted sequence of bytes for custom use.

customsec::=section0(custom)custom::=name  byte

Note

If an implementation interprets the data of a custom section, then errors in that data, or the placement of the section, must not invalidate the module.

Type Section

The type section has the id 1. It decodes into a vector of function types that represent the types component of a module.

typesec::=ft:section1(vec(functype))ft

Import Section

The import section has the id 2. It decodes into a vector of imports that represent the imports component of a module.

importsec::=im:section2(vec(import))imimport::=mod:name  nm:name  d:importdesc{module mod,name nm,desc d}importdesc::=0x00  x:typeidxfunc x|0x01  tt:tabletypetable tt|0x02  mt:memtypemem mt|0x03  gt:globaltypeglobal gt

Function Section

The function section has the id 3. It decodes into a vector of type indices that represent the type fields of the functions in the funcs component of a module. The locals and body fields of the respective functions are encoded separately in the code section.

funcsec::=x:section3(vec(typeidx))x

Table Section

The table section has the id 4. It decodes into a vector of tables that represent the tables component of a module.

tablesec::=tab:section4(vec(table))tabtable::=tt:tabletype{type tt}

Memory Section

The memory section has the id 5. It decodes into a vector of memories that represent the mems component of a module.

memsec::=mem:section5(vec(mem))memmem::=mt:memtype{type mt}

Global Section

The global section has the id 6. It decodes into a vector of globals that represent the globals component of a module.

globalsec::=glob:section6(vec(global))globglobal::=gt:globaltype  e:expr{type gt,init e}

Export Section

The export section has the id 7. It decodes into a vector of exports that represent the exports component of a module.

exportsec::=ex:section7(vec(export))exexport::=nm:name  d:exportdesc{name nm,desc d}exportdesc::=0x00  x:funcidxfunc x|0x01  x:tableidxtable x|0x02  x:memidxmem x|0x03  x:globalidxglobal x

Start Section

The start section has the id 8. It decodes into an optional start function that represents the start component of a module.

startsec::=st?:section8(start)st?start::=x:funcidx{func x}

Element Section

The element section has the id 9. It decodes into a vector of element segments that represent the elems component of a module.

elemsec::=seg:section9(vec(elem))segelem::=0:u32  e:expr  y:vec(funcidx){type funcref,init ((ref.func y) end),mode active {table 0,offset e}}|1:u32  et:elemkind  y:vec(funcidx){type et,init ((ref.func y) end),mode passive}|2:u32  x:tableidx  e:expr  et:elemkind  y:vec(funcidx){type et,init ((ref.func y) end),mode active {table x,offset e}}|3:u32  et:elemkind  y:vec(funcidx){type et,init ((ref.func y) end),mode declarative}|4:u32  e:expr  el:vec(expr){type funcref,init el,mode active {table 0,offset e}}|5:u32  et:reftype  el:vec(expr){type et,init el,mode passive}|6:u32  x:tableidx  e:expr  et:reftype  el:vec(expr){type et,init el,mode active {table x,offset e}}|7:u32  et:reftype  el:vec(expr){type et,init el,mode declarative}elemkind::=0x00funcref

Note

The initial integer can be interpreted as a bitfield. Bit 0 indicates a passive or declarative segment, bit 1 indicates the presence of an explicit table index for an active segment and otherwise distinguishes passive from declarative segments, bit 2 indicates the use of element type and element expressions instead of element kind and element indices.

Additional element kinds may be added in future versions of WebAssembly.

Code Section

The code section has the id 10. It decodes into a vector of code entries that are pairs of value type vectors and expressions. They represent the locals and body field of the functions in the funcs component of a module. The type fields of the respective functions are encoded separately in the function section.

The encoding of each code entry consists of

  • the u32 size of the function code in bytes,

  • the actual function code, which in turn consists of

    • the declaration of locals,

    • the function body as an expression.

Local declarations are compressed into a vector whose entries consist of

denoting count locals of the same value type.

codesec::=code:section10(vec(code))codecode::=size:u32  code:funccode(ifsize=||func||)func::=(t):vec(locals)  e:exprconcat((t)),e(if|concat((t))|<232)locals::=n:u32  t:valtypetn

Here, code ranges over pairs (valtype,expr). The meta function concat((t)) concatenates all sequences ti in (t). Any code for which the length of the resulting sequence is out of bounds of the maximum size of a vector is malformed.

Note

Like with sections, the code size is not needed for decoding, but can be used to skip functions when navigating through a binary. The module is malformed if a size does not match the length of the respective function code.

Data Section

The data section has the id 11. It decodes into a vector of data segments that represent the datas component of a module.

datasec::=seg:section11(vec(data))segdata::=0:u32  e:expr  b:vec(byte){init b,mode active {memory 0,offset e}}|1:u32  b:vec(byte){init b,mode passive}|2:u32  x:memidx  e:expr  b:vec(byte){init b,mode active {memory x,offset e}}

Note

The initial integer can be interpreted as a bitfield. Bit 0 indicates a passive segment, bit 1 indicates the presence of an explicit memory index for an active segment.

In the current version of WebAssembly, at most one memory may be defined or imported in a single module, so all valid active data segments have a memory value of 0.

Data Count Section

The data count section has the id 12. It decodes into an optional u32 that represents the number of data segments in the data section. If this count does not match the length of the data segment vector, the module is malformed.

datacountsec::=n?:section12(u32)n?

Note

The data count section is used to simplify single-pass validation. Since the data section occurs after the code section, the memory.init and data.drop instructions would not be able to check whether the data segment index is valid until the data section is read. The data count section occurs before the code section, so a single-pass validator can use this count instead of deferring validation.

Modules

The encoding of a module starts with a preamble containing a 4-byte magic number (the string 0asm) and a version field. The current version of the WebAssembly binary format is 1.

The preamble is followed by a sequence of sections. Custom sections may be inserted at any place in this sequence, while other sections must occur at most once and in the prescribed order. All sections can be empty.

The lengths of vectors produced by the (possibly empty) function and code section must match up.

Similarly, the optional data count must match the length of the data segment vector. Furthermore, it must be present if any data index occurs in the code section.

magic::=0x00 0x61 0x73 0x6Dversion::=0x01 0x00 0x00 0x00module::=magicversioncustomsecfunctype:typeseccustomsecimport:importseccustomsectypeidxn:funcseccustomsectable:tableseccustomsecmem:memseccustomsecglobal:globalseccustomsecexport:exportseccustomsecstart?:startseccustomsecelem:elemseccustomsecm?:datacountseccustomseccoden:codeseccustomsecdatam:dataseccustomsec{ types functype,funcs funcn,tables table,mems mem,globals global,elems elem,datas datam,start start?,imports import,exports export }(ifm?ϵdataidx(coden)=)

where for each ti,ei in coden,

funcn[i]={type typeidxn[i],locals ti,body ei}

Note

The version of the WebAssembly binary format may increase in the future if backward-incompatible changes have to be made to the format. However, such changes are expected to occur very infrequently, if ever. The binary format is intended to be forward-compatible, such that future extensions can be made without incrementing its version.