Soundness¶
The type system of WebAssembly is sound, implying both type safety and memory safety with respect to the WebAssembly semantics. For example:
All types declared and derived during validation are respected at run time; e.g., every local or global variable will only contain type-correct values, every instruction will only be applied to operands of the expected type, and every function invocation always evaluates to a result of the right type (if it does not trap or diverge).
No memory location will be read or written except those explicitly defined by the program, i.e., as a local, a global, an element in a table, or a location within a linear memory.
There is no undefined behavior, i.e., the execution rules cover all possible cases that can occur in a valid program, and the rules are mutually consistent.
Soundness also is instrumental in ensuring additional properties, most notably, encapsulation of function and module scopes: no locals can be accessed outside their own function and no module components can be accessed outside their own module unless they are explicitly exported or imported.
The typing rules defining WebAssembly validation only cover the static components of a WebAssembly program. In order to state and prove soundness precisely, the typing rules must be extended to the dynamic components of the abstract runtime, that is, the store, configurations, and administrative instructions. 1
Results¶
Results can be classified by result types as follows.
Results \(\href{../exec/runtime.html#syntax-val}{\mathit{val}}^\ast\)¶
For each value \(\href{../exec/runtime.html#syntax-val}{\mathit{val}}_i\) in \(\href{../exec/runtime.html#syntax-val}{\mathit{val}}^\ast\):
The value \(\href{../exec/runtime.html#syntax-val}{\mathit{val}}_i\) is valid with some value type \(t_i\).
Let \(t^\ast\) be the concatenation of all \(t_i\).
Then the result is valid with result type \([t^\ast]\).
Results \(\href{../exec/runtime.html#syntax-trap}{\mathsf{trap}}\)¶
The result is valid with result type \([t^\ast]\), for any sequence \(t^\ast\) of value types.
Store Validity¶
The following typing rules specify when a runtime store \(S\) is valid. A valid store must consist of function, table, memory, global, and module instances that are themselves valid, relative to \(S\).
To that end, each kind of instance is classified by a respective function, table, memory, or global type. Module instances are classified by module contexts, which are regular contexts repurposed as module types describing the index spaces defined by a module.
Store \(S\)¶
Each function instance \(\href{../exec/runtime.html#syntax-funcinst}{\mathit{funcinst}}_i\) in \(S.\href{../exec/runtime.html#syntax-store}{\mathsf{funcs}}\) must be valid with some function type \(\href{../syntax/types.html#syntax-functype}{\mathit{functype}}_i\).
Each table instance \(\href{../exec/runtime.html#syntax-tableinst}{\mathit{tableinst}}_i\) in \(S.\href{../exec/runtime.html#syntax-store}{\mathsf{tables}}\) must be valid with some table type \(\href{../syntax/types.html#syntax-tabletype}{\mathit{tabletype}}_i\).
Each memory instance \(\href{../exec/runtime.html#syntax-meminst}{\mathit{meminst}}_i\) in \(S.\href{../exec/runtime.html#syntax-store}{\mathsf{mems}}\) must be valid with some memory type \(\href{../syntax/types.html#syntax-memtype}{\mathit{memtype}}_i\).
Each global instance \(\href{../exec/runtime.html#syntax-globalinst}{\mathit{globalinst}}_i\) in \(S.\href{../exec/runtime.html#syntax-store}{\mathsf{globals}}\) must be valid with some global type \(\href{../syntax/types.html#syntax-globaltype}{\mathit{globaltype}}_i\).
Each element instance \(\href{../exec/runtime.html#syntax-eleminst}{\mathit{eleminst}}_i\) in \(S.\href{../exec/runtime.html#syntax-store}{\mathsf{elems}}\) must be valid with some reference type \(\href{../syntax/types.html#syntax-reftype}{\mathit{reftype}}_i\).
Each data instance \(\href{../exec/runtime.html#syntax-datainst}{\mathit{datainst}}_i\) in \(S.\href{../exec/runtime.html#syntax-store}{\mathsf{datas}}\) must be valid.
Then the store is valid.
Function Instances \(\{\href{../exec/runtime.html#syntax-funcinst}{\mathsf{type}}~\href{../syntax/types.html#syntax-functype}{\mathit{functype}}, \href{../exec/runtime.html#syntax-funcinst}{\mathsf{module}}~\href{../exec/runtime.html#syntax-moduleinst}{\mathit{moduleinst}}, \href{../exec/runtime.html#syntax-funcinst}{\mathsf{code}}~\href{../syntax/modules.html#syntax-func}{\mathit{func}}\}\)¶
The function type \(\href{../syntax/types.html#syntax-functype}{\mathit{functype}}\) must be valid.
The module instance \(\href{../exec/runtime.html#syntax-moduleinst}{\mathit{moduleinst}}\) must be valid with some context \(C\).
Under context \(C\), the function \(\href{../syntax/modules.html#syntax-func}{\mathit{func}}\) must be valid with function type \(\href{../syntax/types.html#syntax-functype}{\mathit{functype}}\).
Then the function instance is valid with function type \(\href{../syntax/types.html#syntax-functype}{\mathit{functype}}\).
Host Function Instances \(\{\href{../exec/runtime.html#syntax-funcinst}{\mathsf{type}}~\href{../syntax/types.html#syntax-functype}{\mathit{functype}}, \href{../exec/runtime.html#syntax-funcinst}{\mathsf{hostcode}}~\mathit{hf}\}\)¶
The function type \(\href{../syntax/types.html#syntax-functype}{\mathit{functype}}\) must be valid.
Then the function instance is valid with function type \(\href{../syntax/types.html#syntax-functype}{\mathit{functype}}\).
Table Instances \(\{ \href{../exec/runtime.html#syntax-tableinst}{\mathsf{type}}~(\href{../syntax/types.html#syntax-limits}{\mathit{limits}}~t), \href{../exec/runtime.html#syntax-tableinst}{\mathsf{elem}}~\href{../exec/runtime.html#syntax-ref}{\mathit{ref}}^\ast \}\)¶
The table type \(\href{../syntax/types.html#syntax-limits}{\mathit{limits}}~t\) must be valid.
The length of \(\href{../exec/runtime.html#syntax-ref}{\mathit{ref}}^\ast\) must equal \(\href{../syntax/types.html#syntax-limits}{\mathit{limits}}.\href{../syntax/types.html#syntax-limits}{\mathsf{min}}\).
For each reference \(\href{../exec/runtime.html#syntax-ref}{\mathit{ref}}_i\) in the table’s elements \(\href{../exec/runtime.html#syntax-ref}{\mathit{ref}}^n\):
The reference \(\href{../exec/runtime.html#syntax-ref}{\mathit{ref}}_i\) must be valid with reference type \(t\).
Then the table instance is valid with table type \(\href{../syntax/types.html#syntax-limits}{\mathit{limits}}~t\).
Global Instances \(\{ \href{../exec/runtime.html#syntax-globalinst}{\mathsf{type}}~(\href{../syntax/types.html#syntax-mut}{\mathit{mut}}~t), \href{../exec/runtime.html#syntax-globalinst}{\mathsf{value}}~\href{../exec/runtime.html#syntax-val}{\mathit{val}} \}\)¶
The global type \(\href{../syntax/types.html#syntax-mut}{\mathit{mut}}~t\) must be valid.
The value \(\href{../exec/runtime.html#syntax-val}{\mathit{val}}\) must be valid with value type \(t\).
Then the global instance is valid with global type \(\href{../syntax/types.html#syntax-mut}{\mathit{mut}}~t\).
Element Instances \(\{ \href{../exec/runtime.html#syntax-eleminst}{\mathsf{type}}~t, \href{../exec/runtime.html#syntax-eleminst}{\mathsf{elem}}~\href{../exec/runtime.html#syntax-ref}{\mathit{ref}}^\ast \}\)¶
For each reference \(\href{../exec/runtime.html#syntax-ref}{\mathit{ref}}_i\) in the elements \(\href{../exec/runtime.html#syntax-ref}{\mathit{ref}}^n\):
The reference \(\href{../exec/runtime.html#syntax-ref}{\mathit{ref}}_i\) must be valid with reference type \(t\).
Then the element instance is valid with reference type \(t\).
Data Instances \(\{ \href{../exec/runtime.html#syntax-datainst}{\mathsf{data}}~b^\ast \}\)¶
The data instance is valid.
Export Instances \(\{ \href{../exec/runtime.html#syntax-exportinst}{\mathsf{name}}~\href{../syntax/values.html#syntax-name}{\mathit{name}}, \href{../exec/runtime.html#syntax-exportinst}{\mathsf{value}}~\href{../exec/runtime.html#syntax-externval}{\mathit{externval}} \}\)¶
The external value \(\href{../exec/runtime.html#syntax-externval}{\mathit{externval}}\) must be valid with some external type \(\href{../syntax/types.html#syntax-externtype}{\mathit{externtype}}\).
Then the export instance is valid.
Module Instances \(\href{../exec/runtime.html#syntax-moduleinst}{\mathit{moduleinst}}\)¶
Each function type \(\href{../syntax/types.html#syntax-functype}{\mathit{functype}}_i\) in \(\href{../exec/runtime.html#syntax-moduleinst}{\mathit{moduleinst}}.\href{../exec/runtime.html#syntax-moduleinst}{\mathsf{types}}\) must be valid.
For each function address \(\href{../exec/runtime.html#syntax-funcaddr}{\mathit{funcaddr}}_i\) in \(\href{../exec/runtime.html#syntax-moduleinst}{\mathit{moduleinst}}.\href{../exec/runtime.html#syntax-moduleinst}{\mathsf{funcaddrs}}\), the external value \(\href{../exec/runtime.html#syntax-externval}{\mathsf{func}}~\href{../exec/runtime.html#syntax-funcaddr}{\mathit{funcaddr}}_i\) must be valid with some external type \(\href{../syntax/types.html#syntax-externtype}{\mathsf{func}}~\href{../syntax/types.html#syntax-functype}{\mathit{functype}}'_i\).
For each table address \(\href{../exec/runtime.html#syntax-tableaddr}{\mathit{tableaddr}}_i\) in \(\href{../exec/runtime.html#syntax-moduleinst}{\mathit{moduleinst}}.\href{../exec/runtime.html#syntax-moduleinst}{\mathsf{tableaddrs}}\), the external value \(\href{../exec/runtime.html#syntax-externval}{\mathsf{table}}~\href{../exec/runtime.html#syntax-tableaddr}{\mathit{tableaddr}}_i\) must be valid with some external type \(\href{../syntax/types.html#syntax-externtype}{\mathsf{table}}~\href{../syntax/types.html#syntax-tabletype}{\mathit{tabletype}}_i\).
For each memory address \(\href{../exec/runtime.html#syntax-memaddr}{\mathit{memaddr}}_i\) in \(\href{../exec/runtime.html#syntax-moduleinst}{\mathit{moduleinst}}.\href{../exec/runtime.html#syntax-moduleinst}{\mathsf{memaddrs}}\), the external value \(\href{../exec/runtime.html#syntax-externval}{\mathsf{mem}}~\href{../exec/runtime.html#syntax-memaddr}{\mathit{memaddr}}_i\) must be valid with some external type \(\href{../syntax/types.html#syntax-externtype}{\mathsf{mem}}~\href{../syntax/types.html#syntax-memtype}{\mathit{memtype}}_i\).
For each global address \(\href{../exec/runtime.html#syntax-globaladdr}{\mathit{globaladdr}}_i\) in \(\href{../exec/runtime.html#syntax-moduleinst}{\mathit{moduleinst}}.\href{../exec/runtime.html#syntax-moduleinst}{\mathsf{globaladdrs}}\), the external value \(\href{../exec/runtime.html#syntax-externval}{\mathsf{global}}~\href{../exec/runtime.html#syntax-globaladdr}{\mathit{globaladdr}}_i\) must be valid with some external type \(\href{../syntax/types.html#syntax-externtype}{\mathsf{global}}~\href{../syntax/types.html#syntax-globaltype}{\mathit{globaltype}}_i\).
For each element address \(\href{../exec/runtime.html#syntax-elemaddr}{\mathit{elemaddr}}_i\) in \(\href{../exec/runtime.html#syntax-moduleinst}{\mathit{moduleinst}}.\href{../exec/runtime.html#syntax-moduleinst}{\mathsf{elemaddrs}}\), the element instance \(S.\href{../exec/runtime.html#syntax-store}{\mathsf{elems}}[\href{../exec/runtime.html#syntax-elemaddr}{\mathit{elemaddr}}_i]\) must be valid with some reference type \(\href{../syntax/types.html#syntax-reftype}{\mathit{reftype}}_i\).
For each data address \(\href{../exec/runtime.html#syntax-dataaddr}{\mathit{dataaddr}}_i\) in \(\href{../exec/runtime.html#syntax-moduleinst}{\mathit{moduleinst}}.\href{../exec/runtime.html#syntax-moduleinst}{\mathsf{dataaddrs}}\), the data instance \(S.\href{../exec/runtime.html#syntax-store}{\mathsf{datas}}[\href{../exec/runtime.html#syntax-dataaddr}{\mathit{dataaddr}}_i]\) must be valid.
Each export instance \(\href{../exec/runtime.html#syntax-exportinst}{\mathit{exportinst}}_i\) in \(\href{../exec/runtime.html#syntax-moduleinst}{\mathit{moduleinst}}.\href{../exec/runtime.html#syntax-moduleinst}{\mathsf{exports}}\) must be valid.
For each export instance \(\href{../exec/runtime.html#syntax-exportinst}{\mathit{exportinst}}_i\) in \(\href{../exec/runtime.html#syntax-moduleinst}{\mathit{moduleinst}}.\href{../exec/runtime.html#syntax-moduleinst}{\mathsf{exports}}\), the name \(\href{../exec/runtime.html#syntax-exportinst}{\mathit{exportinst}}_i.\href{../exec/runtime.html#syntax-exportinst}{\mathsf{name}}\) must be different from any other name occurring in \(\href{../exec/runtime.html#syntax-moduleinst}{\mathit{moduleinst}}.\href{../exec/runtime.html#syntax-moduleinst}{\mathsf{exports}}\).
Let \({\href{../syntax/types.html#syntax-functype}{\mathit{functype}}'}^\ast\) be the concatenation of all \(\href{../syntax/types.html#syntax-functype}{\mathit{functype}}'_i\) in order.
Let \(\href{../syntax/types.html#syntax-tabletype}{\mathit{tabletype}}^\ast\) be the concatenation of all \(\href{../syntax/types.html#syntax-tabletype}{\mathit{tabletype}}_i\) in order.
Let \(\href{../syntax/types.html#syntax-memtype}{\mathit{memtype}}^\ast\) be the concatenation of all \(\href{../syntax/types.html#syntax-memtype}{\mathit{memtype}}_i\) in order.
Let \(\href{../syntax/types.html#syntax-globaltype}{\mathit{globaltype}}^\ast\) be the concatenation of all \(\href{../syntax/types.html#syntax-globaltype}{\mathit{globaltype}}_i\) in order.
Let \(\href{../syntax/types.html#syntax-reftype}{\mathit{reftype}}^\ast\) be the concatenation of all \(\href{../syntax/types.html#syntax-reftype}{\mathit{reftype}}_i\) in order.
Let \(n\) be the length of \(\href{../exec/runtime.html#syntax-moduleinst}{\mathit{moduleinst}}.\href{../exec/runtime.html#syntax-moduleinst}{\mathsf{dataaddrs}}\).
Then the module instance is valid with context \(\{\href{../valid/conventions.html#context}{\mathsf{types}}~\href{../syntax/types.html#syntax-functype}{\mathit{functype}}^\ast,\) \(\href{../valid/conventions.html#context}{\mathsf{funcs}}~{\href{../syntax/types.html#syntax-functype}{\mathit{functype}}'}^\ast,\) \(\href{../valid/conventions.html#context}{\mathsf{tables}}~\href{../syntax/types.html#syntax-tabletype}{\mathit{tabletype}}^\ast,\) \(\href{../valid/conventions.html#context}{\mathsf{mems}}~\href{../syntax/types.html#syntax-memtype}{\mathit{memtype}}^\ast,\) \(\href{../valid/conventions.html#context}{\mathsf{globals}}~\href{../syntax/types.html#syntax-globaltype}{\mathit{globaltype}}^\ast,\) \(\href{../valid/conventions.html#context}{\mathsf{elems}}~\href{../syntax/types.html#syntax-reftype}{\mathit{reftype}}^\ast,\) \(\href{../valid/conventions.html#context}{\mathsf{datas}}~{\mathrel{\mbox{ok}}}^n\}\).
Configuration Validity¶
To relate the WebAssembly type system to its execution semantics, the typing rules for instructions must be extended to configurations \(S;P^\ast\), which relates the store to execution threads.
Threads are classified by their result type, and configurations are classified by a list of their threads’ result types. In addition to the store \(S\), threads are typed under a return type \(\href{../syntax/types.html#syntax-resulttype}{\mathit{resulttype}}^?\), which controls whether and with which type a \(\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{return}}\) instruction is allowed. This type is absent (\(\epsilon\)) except for instruction sequences inside an administrative \(\href{../exec/runtime.html#syntax-frame}{\mathsf{frame}}\) instruction.
Finally, frames are classified with frame contexts, which extend the module contexts of a frame’s associated module instance with the locals that the frame contains.
Configurations \(S;P^\ast\)¶
Each thread \(P_i\) in \(P^\ast\), under no allowed return type, must be valid with some result type \([t_i^\ast]\).
Let \([t^\ast]^\ast\) be the concatenation of all \([t_i^\ast]\) in order.
Then the configuration is valid with the result type \([t^\ast]^\ast\).
Threads \(F;\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast\)¶
Let \(\href{../syntax/types.html#syntax-resulttype}{\mathit{resulttype}}^?\) be the current allowed return type.
Let \(C'\) be the same context as \(C\), but with \(\href{../valid/conventions.html#context}{\mathsf{return}}\) set to \(\href{../syntax/types.html#syntax-resulttype}{\mathit{resulttype}}^?\).
Under context \(C'\), the instruction sequence \(\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast\) must be valid with some type \([] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t^\ast]\).
Then the thread is valid with the result type \([t^\ast]\).
Frames \(\{\href{../exec/runtime.html#syntax-frame}{\mathsf{locals}}~\href{../exec/runtime.html#syntax-val}{\mathit{val}}^\ast, \href{../exec/runtime.html#syntax-frame}{\mathsf{module}}~\href{../exec/runtime.html#syntax-moduleinst}{\mathit{moduleinst}}\}\)¶
The module instance \(\href{../exec/runtime.html#syntax-moduleinst}{\mathit{moduleinst}}\) must be valid with some module context \(C\).
Each value \(\href{../exec/runtime.html#syntax-val}{\mathit{val}}_i\) in \(\href{../exec/runtime.html#syntax-val}{\mathit{val}}^\ast\) must be valid with some value type \(t_i\).
Let \(t^\ast\) be the concatenation of all \(t_i\) in order.
Let \(C'\) be the same context as \(C\), but with the value types \(t^\ast\) prepended to the \(\href{../valid/conventions.html#context}{\mathsf{locals}}\) vector.
Then the frame is valid with frame context \(C'\).
Administrative Instructions¶
Typing rules for administrative instructions are specified as follows. In addition to the context \(C\), typing of these instructions is defined under a given store \(S\). To that end, all previous typing judgements \(C \vdash \mathit{prop}\) are generalized to include the store, as in \(S; C \vdash \mathit{prop}\), by implicitly adding \(S\) to all rules – \(S\) is never modified by the pre-existing rules, but it is accessed in the extra rules for administrative instructions given below.
\(\href{../exec/runtime.html#syntax-trap}{\mathsf{trap}}\)¶
The instruction is valid with type \([t_1^\ast] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t_2^\ast]\), for any sequences of value types \(t_1^\ast\) and \(t_2^\ast\).
\(\href{../exec/runtime.html#syntax-ref.extern}{\mathsf{ref{.}extern}}~\href{../exec/runtime.html#syntax-externaddr}{\mathit{externaddr}}\)¶
The instruction is valid with type \([] \href{../syntax/types.html#syntax-functype}{\rightarrow} [\href{../syntax/types.html#syntax-reftype}{\mathsf{externref}}]\).
\(\href{../exec/runtime.html#syntax-ref}{\mathsf{ref}}~\href{../exec/runtime.html#syntax-funcaddr}{\mathit{funcaddr}}\)¶
The external function value \(\href{../exec/runtime.html#syntax-externval}{\mathsf{func}}~\href{../exec/runtime.html#syntax-funcaddr}{\mathit{funcaddr}}\) must be valid with external function type \(\href{../syntax/types.html#syntax-externtype}{\mathsf{func}}~\href{../syntax/types.html#syntax-functype}{\mathit{functype}}\).
Then the instruction is valid with type \([] \href{../syntax/types.html#syntax-functype}{\rightarrow} [\href{../syntax/types.html#syntax-reftype}{\mathsf{funcref}}]\).
\(\href{../exec/runtime.html#syntax-invoke}{\mathsf{invoke}}~\href{../exec/runtime.html#syntax-funcaddr}{\mathit{funcaddr}}\)¶
The external function value \(\href{../exec/runtime.html#syntax-externval}{\mathsf{func}}~\href{../exec/runtime.html#syntax-funcaddr}{\mathit{funcaddr}}\) must be valid with external function type \(\href{../syntax/types.html#syntax-externtype}{\mathsf{func}} ([t_1^\ast] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t_2^\ast])\).
Then the instruction is valid with type \([t_1^\ast] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t_2^\ast]\).
\(\href{../exec/runtime.html#syntax-label}{\mathsf{label}}_n\{\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}_0^\ast\}~\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast~\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{end}}\)¶
The instruction sequence \(\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}_0^\ast\) must be valid with some type \([t_1^n] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t_2^*]\).
Let \(C'\) be the same context as \(C\), but with the result type \([t_1^n]\) prepended to the \(\href{../valid/conventions.html#context}{\mathsf{labels}}\) vector.
Under context \(C'\), the instruction sequence \(\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast\) must be valid with type \([] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t_2^*]\).
Then the compound instruction is valid with type \([] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t_2^*]\).
\(\href{../exec/runtime.html#syntax-frame}{\mathsf{frame}}_n\{F\}~\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast~\href{../syntax/instructions.html#syntax-instr-control}{\mathsf{end}}\)¶
Under the return type \([t^n]\), the thread \(F; \href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast\) must be valid with result type \([t^n]\).
Then the compound instruction is valid with type \([] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t^n]\).
\(\href{../exec/runtime.html#syntax-instr-admin}{\mathsf{wait'}}~\href{../exec/runtime.html#syntax-memaddr}{\mathit{memaddr}}.\href{../text/conventions.html#text-context}{\mathsf{data}}[k]~n\)¶
The external memory value \(\href{../exec/runtime.html#syntax-externval}{\mathsf{mem}}~\href{../exec/runtime.html#syntax-memaddr}{\mathit{memaddr}}\) must be valid with external memory type \(\href{../syntax/types.html#syntax-externtype}{\mathsf{mem}}~(\href{../syntax/types.html#syntax-limits}{\mathit{limits}}~\href{../syntax/types.html#syntax-memtype}{\mathsf{shared}})\).
Then the instruction is valid with type \([] \href{../syntax/types.html#syntax-functype}{\rightarrow} []\).
\(\href{../exec/runtime.html#syntax-instr-admin}{\mathsf{perform}}~\href{../exec/runtime.html#syntax-act}{\mathit{act}}^\ast\)¶
The instruction is valid with type \([] \href{../syntax/types.html#syntax-functype}{\rightarrow} []\).
Store Extension¶
Programs can mutate the store and its contained instances. Any such modification must respect certain invariants, such as not removing allocated instances or changing immutable definitions. While these invariants are inherent to the execution semantics of WebAssembly instructions and modules, host functions do not automatically adhere to them. Consequently, the required invariants must be stated as explicit constraints on the invocation of host functions. Soundness only holds when the embedder ensures these constraints.
The necessary constraints are codified by the notion of store extension: a store state \(S'\) extends state \(S\), written \(S \href{../appendix/properties.html#extend}{\preceq} S'\), when the following rules hold.
Note
Extension does not imply that the new store is valid, which is defined separately above.
Store \(S\)¶
The length of \(S.\href{../exec/runtime.html#syntax-store}{\mathsf{funcs}}\) must not shrink.
The length of \(S.\href{../exec/runtime.html#syntax-store}{\mathsf{tables}}\) must not shrink.
The length of \(S.\href{../exec/runtime.html#syntax-store}{\mathsf{mems}}\) must not shrink.
The length of \(S.\href{../exec/runtime.html#syntax-store}{\mathsf{globals}}\) must not shrink.
The length of \(S.\href{../exec/runtime.html#syntax-store}{\mathsf{elems}}\) must not shrink.
The length of \(S.\href{../exec/runtime.html#syntax-store}{\mathsf{datas}}\) must not shrink.
For each function instance \(\href{../exec/runtime.html#syntax-funcinst}{\mathit{funcinst}}_i\) in the original \(S.\href{../exec/runtime.html#syntax-store}{\mathsf{funcs}}\), the new function instance must be an extension of the old.
For each table instance \(\href{../exec/runtime.html#syntax-tableinst}{\mathit{tableinst}}_i\) in the original \(S.\href{../exec/runtime.html#syntax-store}{\mathsf{tables}}\), the new table instance must be an extension of the old.
For each memory instance \(\href{../exec/runtime.html#syntax-meminst}{\mathit{meminst}}_i\) in the original \(S.\href{../exec/runtime.html#syntax-store}{\mathsf{mems}}\), the new memory instance must be an extension of the old.
For each global instance \(\href{../exec/runtime.html#syntax-globalinst}{\mathit{globalinst}}_i\) in the original \(S.\href{../exec/runtime.html#syntax-store}{\mathsf{globals}}\), the new global instance must be an extension of the old.
For each element instance \(\href{../exec/runtime.html#syntax-eleminst}{\mathit{eleminst}}_i\) in the original \(S.\href{../exec/runtime.html#syntax-store}{\mathsf{elems}}\), the new global instance must be an extension of the old.
For each data instance \(\href{../exec/runtime.html#syntax-datainst}{\mathit{datainst}}_i\) in the original \(S.\href{../exec/runtime.html#syntax-store}{\mathsf{datas}}\), the new global instance must be an extension of the old.
Function Instance \(\href{../exec/runtime.html#syntax-funcinst}{\mathit{funcinst}}\)¶
A function instance must remain unchanged.
Table Instance \(\href{../exec/runtime.html#syntax-tableinst}{\mathit{tableinst}}\)¶
The table type \(\href{../exec/runtime.html#syntax-tableinst}{\mathit{tableinst}}.\href{../exec/runtime.html#syntax-tableinst}{\mathsf{type}}\) must remain unchanged.
The length of \(\href{../exec/runtime.html#syntax-tableinst}{\mathit{tableinst}}.\href{../exec/runtime.html#syntax-tableinst}{\mathsf{elem}}\) must not shrink.
Memory Instance \(\href{../exec/runtime.html#syntax-meminst}{\mathit{meminst}}\)¶
The memory type \(\href{../exec/runtime.html#syntax-meminst}{\mathit{meminst}}.\href{../exec/runtime.html#syntax-meminst}{\mathsf{type}}\) must remain unchanged.
The length of \(\href{../exec/runtime.html#syntax-meminst}{\mathit{meminst}}.\href{../exec/runtime.html#syntax-meminst}{\mathsf{data}}\) must not shrink.
Global Instance \(\href{../exec/runtime.html#syntax-globalinst}{\mathit{globalinst}}\)¶
The global type \(\href{../exec/runtime.html#syntax-globalinst}{\mathit{globalinst}}.\href{../exec/runtime.html#syntax-globalinst}{\mathsf{type}}\) must remain unchanged.
Let \(\href{../syntax/types.html#syntax-mut}{\mathit{mut}}~t\) be the structure of \(\href{../exec/runtime.html#syntax-globalinst}{\mathit{globalinst}}.\href{../exec/runtime.html#syntax-globalinst}{\mathsf{type}}\).
If \(\href{../syntax/types.html#syntax-mut}{\mathit{mut}}\) is \(\href{../syntax/types.html#syntax-mut}{\mathsf{const}}\), then the value \(\href{../exec/runtime.html#syntax-globalinst}{\mathit{globalinst}}.\href{../exec/runtime.html#syntax-globalinst}{\mathsf{value}}\) must remain unchanged.
Element Instance \(\href{../exec/runtime.html#syntax-eleminst}{\mathit{eleminst}}\)¶
The vector \(\href{../exec/runtime.html#syntax-eleminst}{\mathit{eleminst}}.\href{../exec/runtime.html#syntax-eleminst}{\mathsf{elem}}\) must either remain unchanged or shrink to length \(0\).
Data Instance \(\href{../exec/runtime.html#syntax-datainst}{\mathit{datainst}}\)¶
The vector \(\href{../exec/runtime.html#syntax-datainst}{\mathit{datainst}}.\href{../exec/runtime.html#syntax-datainst}{\mathsf{data}}\) must either remain unchanged or shrink to length \(0\).
Theorems¶
Given the definition of valid configurations, the standard soundness theorems hold. 2 3
Theorem (Preservation). If a configuration \(S;P^\ast\) is valid with result type \([t^\ast]^\ast\) (i.e., \(\href{../appendix/properties.html#valid-config}{\vdash} S;P^\ast : [t^\ast]^\ast\)), and steps to \(S';P'^\ast\) (i.e., \(S;P^\ast \href{../exec/conventions.html#formal-notation}{\hookrightarrow}^{\href{../exec/runtime.html#syntax-evt}{\mathit{evt}}} S';P'^\ast\)), then \(S';P'^\ast\) is a valid configuration with the same result type (i.e., \(\href{../appendix/properties.html#valid-config}{\vdash} S';P'^\ast : [t^\ast]^\ast\)). Furthermore, \(S'\) is an extension of \(S\) (i.e., \(\href{../appendix/properties.html#extend-store}{\vdash} S \href{../appendix/properties.html#extend}{\preceq} S'\)).
A terminal thread is one whose sequence of instructions is a result. A terminal configuration is a configuration whose threads are all terminal.
Theorem (Progress). If a configuration \(S;P^\ast\) is valid (i.e., \(\href{../appendix/properties.html#valid-config}{\vdash} S;P^\ast : [t^\ast]^\ast\) for some result type \([t^\ast]^\ast\)), then either it is terminal, or it can step to some configuration \(S';P'^\ast\) (i.e., \(S;P^\ast \href{../exec/conventions.html#formal-notation}{\hookrightarrow}^{\href{../exec/runtime.html#syntax-evt}{\mathit{evt}}} S';P'^\ast\)).
From Preservation and Progress the soundness of the WebAssembly type system follows directly.
Corollary (Soundness). If a configuration \(S;P^\ast\) is valid (i.e., \(\href{../appendix/properties.html#valid-config}{\vdash} S;P^\ast : [t^\ast]^\ast\) for some result type \([t^\ast]^\ast\)), then it either diverges or takes a finite number of steps to reach a terminal configuration \(S';P'^\ast\) (i.e., \(S;P^\ast \href{../exec/conventions.html#formal-notation}{\hookrightarrow}^{\ast~(\href{../exec/runtime.html#syntax-evt}{\mathit{evt}}^\ast)} S';P'^\ast\)) that is valid with the same result type (i.e., \(\href{../appendix/properties.html#valid-config}{\vdash} S';P'^\ast : [t^\ast]^\ast\)) and where \(S'\) is an extension of \(S\) (i.e., \(\href{../appendix/properties.html#extend-store}{\vdash} S \href{../appendix/properties.html#extend}{\preceq} S'\)).
In other words, every thread in a valid configuration either runs forever, traps, or terminates with a result that has the expected type. Consequently, given a valid store, no computation defined by instantiation or invocation of a valid module can “crash” or otherwise (mis)behave in ways not covered by the execution semantics given in this specification.
- 1
The formalization and theorems are derived from the following article: Andreas Haas, Andreas Rossberg, Derek Schuff, Ben Titzer, Dan Gohman, Luke Wagner, Alon Zakai, JF Bastien, Michael Holman. Bringing the Web up to Speed with WebAssembly. Proceedings of the 38th ACM SIGPLAN Conference on Programming Language Design and Implementation (PLDI 2017). ACM 2017.
- 2
A machine-verified version of the formalization and soundness proof of the PLDI 2017 paper is described in the following article: Conrad Watt. Mechanising and Verifying the WebAssembly Specification. Proceedings of the 7th ACM SIGPLAN Conference on Certified Programs and Proofs (CPP 2018). ACM 2018.
- 3
Machine-verified formalizations and soundness proofs of the semantics from the official specification are described in the following article: Conrad Watt, Xiaojia Rao, Jean Pichon-Pharabod, Martin Bodin, Philippa Gardner. Two Mechanisations of WebAssembly 1.0. Proceedings of the 24th International Symposium on Formal Methods (FM 2021). Springer 2021.