.. index:: ! soundness, type system
The :ref:`type system <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 :ref:`local <syntax-local>` or :ref:`global <syntax-global>` variable will only contain type-correct values, every :ref:`instruction <syntax-instr>` will only be applied to operands of the expected type, and every :ref:`function <syntax-func>` :ref:`invocation <exec-invocation>` always evaluates to a result of the right type (if it does not :ref:`trap <trap>` or diverge).
- No memory location will be read or written except those explicitly defined by the program, i.e., as a :ref:`local <syntax-local>`, a :ref:`global <syntax-global>`, an element in a :ref:`table <syntax-table>`, or a location within a linear :ref:`memory <syntax-mem>`.
- There is no undefined behavior, i.e., the :ref:`execution rules <exec>` cover all possible cases that can occur in a :ref:`valid <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 :ref:`locals <syntax-local>` can be accessed outside their own function and no :ref:`module <syntax-module>` components can be accessed outside their own module unless they are explicitly :ref:`exported <syntax-export>` or :ref:`imported <syntax-import>`.
The typing rules defining WebAssembly :ref:`validation <valid>` 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 :ref:`runtime <syntax-runtime>`, that is, the :ref:`store <syntax-store>`, :ref:`configurations <syntax-config>`, and :ref:`administrative instructions <syntax-instr-admin>`. [1]
.. index:: value, value type, result, result type, trap
:ref:`Results <syntax-result>` can be classified by :ref:`result types <syntax-resulttype>` as follows.
- For each :ref:`value <syntax-val>` \val_i in \val^\ast:
- The value \val_i is :ref:`valid <valid-val>` with some :ref:`value type <syntax-valtype>` t_i.
- Let t^\ast be the concatenation of all t_i.
- Then the result is valid with :ref:`result type <syntax-resulttype>` [t^\ast].
\frac{
(S \vdashval \val : t)^\ast
}{
S \vdashresult \val^\ast : [t^\ast]
}
- The result is valid with :ref:`result type <syntax-resulttype>` [t^\ast], for any sequence t^\ast of :ref:`value types <syntax-valtype>`.
\frac{
}{
S \vdashresult \TRAP : [t^\ast]
}
The following typing rules specify when a runtime :ref:`store <syntax-store>` S is valid. A valid store must consist of :ref:`function <syntax-funcinst>`, :ref:`table <syntax-tableinst>`, :ref:`memory <syntax-meminst>`, :ref:`global <syntax-globalinst>`, and :ref:`module <syntax-moduleinst>` instances that are themselves valid, relative to S.
To that end, each kind of instance is classified by a respective :ref:`function <syntax-functype>`, :ref:`table <syntax-tabletype>`, :ref:`memory <syntax-memtype>`, or :ref:`global <syntax-globaltype>` type. Module instances are classified by module contexts, which are regular :ref:`contexts <context>` repurposed as module types describing the :ref:`index spaces <syntax-index>` defined by a module.
.. index:: store, function instance, table instance, memory instance, global instance, function type, table type, memory type, global type
- Each :ref:`function instance <syntax-funcinst>` \funcinst_i in S.\SFUNCS must be :ref:`valid <valid-funcinst>` with some :ref:`function type <syntax-functype>` \functype_i.
- Each :ref:`table instance <syntax-tableinst>` \tableinst_i in S.\STABLES must be :ref:`valid <valid-tableinst>` with some :ref:`table type <syntax-tabletype>` \tabletype_i.
- Each :ref:`memory instance <syntax-meminst>` \meminst_i in S.\SMEMS must be :ref:`valid <valid-meminst>` with some :ref:`memory type <syntax-memtype>` \memtype_i.
- Each :ref:`global instance <syntax-globalinst>` \globalinst_i in S.\SGLOBALS must be :ref:`valid <valid-globalinst>` with some :ref:`global type <syntax-globaltype>` \globaltype_i.
- Each :ref:`element instance <syntax-eleminst>` \eleminst_i in S.\SELEMS must be :ref:`valid <valid-eleminst>`.
- Each :ref:`data instance <syntax-datainst>` \datainst_i in S.\SDATAS must be :ref:`valid <valid-datainst>`.
- Then the store is valid.
~\\[-1ex]
\frac{
\begin{array}{@{}c@{}}
(S \vdashfuncinst \funcinst : \functype)^\ast
\qquad
(S \vdashtableinst \tableinst : \tabletype)^\ast
\\
(S \vdashmeminst \meminst : \memtype)^\ast
\qquad
(S \vdashglobalinst \globalinst : \globaltype)^\ast
\\
(S \vdasheleminst \eleminst \ok)^\ast
\qquad
(S \vdashdatainst \datainst \ok)^\ast
\\
S = \{
\SFUNCS~\funcinst^\ast,
\STABLES~\tableinst^\ast,
\SMEMS~\meminst^\ast,
\SGLOBALS~\globalinst^\ast,
\SELEMS~\eleminst^\ast,
\SDATAS~\datainst^\ast \}
\end{array}
}{
\vdashstore S \ok
}
.. index:: function type, function instance
:ref:`Function Instances <syntax-funcinst>` \{\FITYPE~\functype, \FIMODULE~\moduleinst, \FICODE~\func\}
- The :ref:`function type <syntax-functype>` \functype must be :ref:`valid <valid-functype>`.
- The :ref:`module instance <syntax-moduleinst>` \moduleinst must be :ref:`valid <valid-moduleinst>` with some :ref:`context <context>` C.
- Under :ref:`context <context>` C, the :ref:`function <syntax-func>` \func must be :ref:`valid <valid-func>` with :ref:`function type <syntax-functype>` \functype.
- Then the function instance is valid with :ref:`function type <syntax-functype>` \functype.
\frac{
\vdashfunctype \functype \ok
\qquad
S \vdashmoduleinst \moduleinst : C
\qquad
C \vdashfunc \func : \functype
}{
S \vdashfuncinst \{\FITYPE~\functype, \FIMODULE~\moduleinst, \FICODE~\func\} : \functype
}
.. index:: function type, function instance, host function
- The :ref:`function type <syntax-functype>` \functype must be :ref:`valid <valid-functype>`.
- Let [t_1^\ast] \to [t_2^\ast] be the :ref:`function type <syntax-functype>` \functype.
- For every :ref:`valid <valid-store>` :ref:`store <syntax-store>` S_1 :ref:`extending <extend-store>` S and every sequence \val^\ast of :ref:`values <syntax-val>` whose :ref:`types <valid-val>` coincide with t_1^\ast:
- :ref:`Executing <exec-invoke-host>` \X{hf} in store S_1 with arguments \val^\ast has a non-empty set of possible outcomes.
- For every element R of this set:
- Either R must be \bot (i.e., divergence).
- Or R consists of a :ref:`valid <valid-store>` :ref:`store <syntax-store>` S_2 :ref:`extending <extend-store>` S_1 and a :ref:`result <syntax-result>` \result whose :ref:`type <valid-result>` coincides with [t_2^\ast].
- Then the function instance is valid with :ref:`function type <syntax-functype>` \functype.
\frac{
\begin{array}[b]{@{}l@{}}
\vdashfunctype [t_1^\ast] \to [t_2^\ast] \ok \\
\end{array}
\quad
\begin{array}[b]{@{}l@{}}
\forall S_1, \val^\ast,~
{\vdashstore S_1 \ok} \wedge
{\vdashstoreextends S \extendsto S_1} \wedge
{S_1 \vdashresult \val^\ast : [t_1^\ast]}
\Longrightarrow {} \\ \qquad
\X{hf}(S_1; \val^\ast) \supset \emptyset \wedge {} \\ \qquad
\forall R \in \X{hf}(S_1; \val^\ast),~
R = \bot \vee {} \\ \qquad\qquad
\exists S_2, \result,~
{\vdashstore S_2 \ok} \wedge
{\vdashstoreextends S_1 \extendsto S_2} \wedge
{S_2 \vdashresult \result : [t_2^\ast]} \wedge
R = (S_2; \result)
\end{array}
}{
S \vdashfuncinst \{\FITYPE~[t_1^\ast] \to [t_2^\ast], \FIHOSTCODE~\X{hf}\} : [t_1^\ast] \to [t_2^\ast]
}
Note
This rule states that, if appropriate pre-conditions about store and arguments are satisfied, then executing the host function must satisfy appropriate post-conditions about store and results. The post-conditions match the ones in the :ref:`execution rule <exec-invoke-host>` for invoking host functions.
Any store under which the function is invoked is assumed to be an extension of the current store. That way, the function itself is able to make sufficient assumptions about future stores.
.. index:: table type, table instance, limits, function address
- The :ref:`table type <syntax-tabletype>` \limits~t must be :ref:`valid <valid-tabletype>`.
- The length of \reff^\ast must equal \limits.\LMIN.
- For each :ref:`reference <syntax-ref>` \reff_i in the table's elements \reff^n:
- The :ref:`reference <syntax-ref>` \reff_i must be :ref:`valid <valid-ref>` with :ref:`reference type <syntax-reftype>` t.
- Then the table instance is valid with :ref:`table type <syntax-tabletype>` \limits~t.
\frac{
\vdashtabletype \limits~t \ok
\qquad
n = \limits.\LMIN
\qquad
(S \vdash \reff : t)^n
}{
S \vdashtableinst \{ \TITYPE~(\limits~t), \TIELEM~\reff^n \} : \limits~t
}
.. index:: memory type, memory instance, limits, byte
- The :ref:`memory type <syntax-memtype>` \{\LMIN~n, \LMAX~m^?\} must be :ref:`valid <valid-memtype>`.
- The length of b^\ast must equal \limits.\LMIN multiplied by the :ref:`page size <page-size>` 64\,\F{Ki}.
- Then the memory instance is valid with :ref:`memory type <syntax-memtype>` \limits.
\frac{
\vdashmemtype \limits \ok
\qquad
n = \limits.\LMIN \cdot 64\,\F{Ki}
}{
S \vdashmeminst \{ \MITYPE~\limits, \MIDATA~b^n \} : \limits
}
.. index:: global type, global instance, value, mutability
- The :ref:`global type <syntax-globaltype>` \mut~t must be :ref:`valid <valid-globaltype>`.
- The :ref:`value <syntax-val>` \val must be :ref:`valid <valid-val>` with :ref:`value type <syntax-valtype>` t.
- Then the global instance is valid with :ref:`global type <syntax-globaltype>` \mut~t.
\frac{
\vdashglobaltype \mut~t \ok
\qquad
S \vdashval \val : t
}{
S \vdashglobalinst \{ \GITYPE~(\mut~t), \GIVALUE~\val \} : \mut~t
}
.. index:: element instance, reference
- For each :ref:`reference <syntax-ref>` \reff_i in the elements \reff^n:
- The :ref:`reference <syntax-ref>` \reff_i must be :ref:`valid <valid-ref>` with :ref:`reference type <syntax-reftype>` t.
- Then the table instance is valid.
\frac{
(S \vdash \reff : t)^\ast
}{
S \vdasheleminst \{ \EITYPE~t, \EIELEM~\reff^\ast \} \ok
}
.. index:: data instance, byte
- The data instance is valid.
\frac{
}{
S \vdashdatainst \{ \DIDATA~b^\ast \} \ok
}
.. index:: external type, export instance, name, external value
- The :ref:`external value <syntax-externval>` \externval must be :ref:`valid <valid-externval>` with some :ref:`external type <syntax-externtype>` \externtype.
- Then the export instance is valid.
\frac{
S \vdashexternval \externval : \externtype
}{
S \vdashexportinst \{ \EINAME~\name, \EIVALUE~\externval \} \ok
}
.. index:: module instance, context
Each :ref:`function type <syntax-functype>` \functype_i in \moduleinst.\MITYPES must be :ref:`valid <valid-functype>`.
For each :ref:`function address <syntax-funcaddr>` \funcaddr_i in \moduleinst.\MIFUNCS, the :ref:`external value <syntax-externval>` \EVFUNC~\funcaddr_i must be :ref:`valid <valid-externval-func>` with some :ref:`external type <syntax-externtype>` \ETFUNC~\functype'_i.
For each :ref:`table address <syntax-tableaddr>` \tableaddr_i in \moduleinst.\MITABLES, the :ref:`external value <syntax-externval>` \EVTABLE~\tableaddr_i must be :ref:`valid <valid-externval-table>` with some :ref:`external type <syntax-externtype>` \ETTABLE~\tabletype_i.
For each :ref:`memory address <syntax-memaddr>` \memaddr_i in \moduleinst.\MIMEMS, the :ref:`external value <syntax-externval>` \EVMEM~\memaddr_i must be :ref:`valid <valid-externval-mem>` with some :ref:`external type <syntax-externtype>` \ETMEM~\memtype_i.
For each :ref:`global address <syntax-globaladdr>` \globaladdr_i in \moduleinst.\MIGLOBALS, the :ref:`external value <syntax-externval>` \EVGLOBAL~\globaladdr_i must be :ref:`valid <valid-externval-global>` with some :ref:`external type <syntax-externtype>` \ETGLOBAL~\globaltype_i.
For each :ref:`element address <syntax-elemaddr>` \elemaddr_i in \moduleinst.\MIELEMS, the :ref:`element instance <syntax-eleminst>` S.\SELEMS[\elemaddr_i] must be :ref:`valid <valid-eleminst>`.
For each :ref:`data address <syntax-dataaddr>` \dataaddr_i in \moduleinst.\MIDATAS, the :ref:`data instance <syntax-datainst>` S.\SDATAS[\dataaddr_i] must be :ref:`valid <valid-datainst>`.
Each :ref:`export instance <syntax-exportinst>` \exportinst_i in \moduleinst.\MIEXPORTS must be :ref:`valid <valid-exportinst>`.
For each :ref:`export instance <syntax-exportinst>` \exportinst_i in \moduleinst.\MIEXPORTS, the :ref:`name <syntax-name>` \exportinst_i.\EINAME must be different from any other name occurring in \moduleinst.\MIEXPORTS.
Let {\functype'}^\ast be the concatenation of all \functype'_i in order.
Let \tabletype^\ast be the concatenation of all \tabletype_i in order.
Let \memtype^\ast be the concatenation of all \memtype_i in order.
Let \globaltype^\ast be the concatenation of all \globaltype_i in order.
- Then the module instance is valid with :ref:`context <context>`\{\CTYPES~\functype^\ast, \CFUNCS~{\functype'}^\ast, \CTABLES~\tabletype^\ast, \CMEMS~\memtype^\ast, \CGLOBALS~\globaltype^\ast\}.
~\\[-1ex]
\frac{
\begin{array}{@{}c@{}}
(\vdashfunctype \functype \ok)^\ast
\\
(S \vdashexternval \EVFUNC~\funcaddr : \ETFUNC~\functype')^\ast
\qquad
(S \vdashexternval \EVTABLE~\tableaddr : \ETTABLE~\tabletype)^\ast
\\
(S \vdashexternval \EVMEM~\memaddr : \ETMEM~\memtype)^\ast
\qquad
(S \vdashexternval \EVGLOBAL~\globaladdr : \ETGLOBAL~\globaltype)^\ast
\\
(S \vdasheleminst S.\SELEMS[\elemaddr] \ok)^\ast
\qquad
(S \vdashdatainst S.\SDATAS[\dataaddr] \ok)^\ast
\\
(S \vdashexportinst \exportinst \ok)^\ast
\qquad
(\exportinst.\EINAME)^\ast ~\mbox{disjoint}
\end{array}
}{
S \vdashmoduleinst \{
\begin{array}[t]{@{}l@{~}l@{}}
\MITYPES & \functype^\ast, \\
\MIFUNCS & \funcaddr^\ast, \\
\MITABLES & \tableaddr^\ast, \\
\MIMEMS & \memaddr^\ast, \\
\MIGLOBALS & \globaladdr^\ast, \\
\MIELEMS & \elemaddr^\ast, \\
\MIDATAS & \dataaddr^\ast, \\
\MIEXPORTS & \exportinst^\ast ~\} : \{
\begin{array}[t]{@{}l@{~}l@{}}
\CTYPES & \functype^\ast, \\
\CFUNCS & {\functype'}^\ast, \\
\CTABLES & \tabletype^\ast, \\
\CMEMS & \memtype^\ast, \\
\CGLOBALS & \globaltype^\ast ~\}
\end{array}
\end{array}
}
.. index:: configuration, administrative instruction, store, frame
To relate the WebAssembly :ref:`type system <valid>` to its :ref:`execution semantics <exec>`, the :ref:`typing rules for instructions <valid-instr>` must be extended to :ref:`configurations <syntax-config>` S;T, which relates the :ref:`store <syntax-store>` to execution :ref:`threads <syntax-thread>`.
Configurations and threads are classified by their :ref:`result type <syntax-resulttype>`. In addition to the store S, threads are typed under a return type \resulttype^?, which controls whether and with which type a |return| instruction is allowed. This type is absent (\epsilon) except for instruction sequences inside an administrative |FRAME| instruction.
Finally, :ref:`frames <syntax-frame>` are classified with frame contexts, which extend the :ref:`module contexts <module-context>` of a frame's associated :ref:`module instance <syntax-moduleinst>` with the :ref:`locals <syntax-local>` that the frame contains.
.. index:: result type, thread
- The :ref:`store <syntax-store>` S must be :ref:`valid <valid-store>`.
- Under no allowed return type, the :ref:`thread <syntax-thread>` T must be :ref:`valid <valid-thread>` with some :ref:`result type <syntax-resulttype>` [t^\ast].
- Then the configuration is valid with the :ref:`result type <syntax-resulttype>` [t^\ast].
\frac{
\vdashstore S \ok
\qquad
S; \epsilon \vdashthread T : [t^\ast]
}{
\vdashconfig S; T : [t^\ast]
}
.. index:: thread, frame, instruction, result type, context
- Let \resulttype^? be the current allowed return type.
- The :ref:`frame <syntax-frame>` F must be :ref:`valid <valid-frame>` with a :ref:`context <context>` C.
- Let C' be the same :ref:`context <context>` as C, but with |CRETURN| set to \resulttype^?.
- Under context C', the instruction sequence \instr^\ast must be :ref:`valid <valid-instr-seq>` with some type [] \to [t^\ast].
- Then the thread is valid with the :ref:`result type <syntax-resulttype>` [t^\ast].
\frac{
S \vdashframe F : C
\qquad
S; C,\CRETURN~\resulttype^? \vdashinstrseq \instr^\ast : [] \to [t^\ast]
}{
S; \resulttype^? \vdashthread F; \instr^\ast : [t^\ast]
}
.. index:: frame, local, module instance, value, value type, context
- The :ref:`module instance <syntax-moduleinst>` \moduleinst must be :ref:`valid <valid-moduleinst>` with some :ref:`module context <module-context>` C.
- Each :ref:`value <syntax-val>` \val_i in \val^\ast must be :ref:`valid <valid-val>` with some :ref:`value type <syntax-valtype>` t_i.
- Let t^\ast the concatenation of all t_i in order.
- Let C' be the same :ref:`context <context>` as C, but with the :ref:`value types <syntax-valtype>` t^\ast prepended to the |CLOCALS| vector.
- Then the frame is valid with :ref:`frame context <frame-context>` C'.
\frac{
S \vdashmoduleinst \moduleinst : C
\qquad
(S \vdashval \val : t)^\ast
}{
S \vdashframe \{\ALOCALS~\val^\ast, \AMODULE~\moduleinst\} : (C, \CLOCALS~t^\ast)
}
.. index:: administrative instruction, value type, context, store
Typing rules for :ref:`administrative instructions <syntax-instr-admin>` are specified as follows. In addition to the :ref:`context <context>` C, typing of these instructions is defined under a given :ref:`store <syntax-store>` S. To that end, all previous typing judgements C \vdash \X{prop} are generalized to include the store, as in S; C \vdash \X{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 :ref:`administrative instructions <valid-instr-admin>` given below.
.. index:: trap
- The instruction is valid with type [t_1^\ast] \to [t_2^\ast], for any sequences of :ref:`value types <syntax-valtype>` t_1^\ast and t_2^\ast.
\frac{
}{
S; C \vdashadmininstr \TRAP : [t_1^\ast] \to [t_2^\ast]
}
.. index:: extern address
- The instruction is valid with type [] \to [\EXTERNREF].
\frac{
}{
S; C \vdashadmininstr \REFEXTERNADDR~\externaddr : [] \to [\EXTERNREF]
}
.. index:: function address, extern value, extern type, function type
- The :ref:`external function value <syntax-externval>` \EVFUNC~\funcaddr must be :ref:`valid <valid-externval-func>` with :ref:`external function type <syntax-externtype>` \ETFUNC \functype.
- Then the instruction is valid with type [] \to [\FUNCREF].
\frac{
S \vdashexternval \EVFUNC~\funcaddr : \ETFUNC~\functype
}{
S; C \vdashadmininstr \REFFUNCADDR~\funcaddr : [] \to [\FUNCREF]
}
.. index:: function address, extern value, extern type, function type
- The :ref:`external function value <syntax-externval>` \EVFUNC~\funcaddr must be :ref:`valid <valid-externval-func>` with :ref:`external function type <syntax-externtype>` \ETFUNC ([t_1^\ast] \to [t_2^\ast]).
- Then the instruction is valid with type [t_1^\ast] \to [t_2^\ast].
\frac{
S \vdashexternval \EVFUNC~\funcaddr : \ETFUNC~[t_1^\ast] \to [t_2^\ast]
}{
S; C \vdashadmininstr \INVOKE~\funcaddr : [t_1^\ast] \to [t_2^\ast]
}
.. index:: label, instruction, result type
- The instruction sequence \instr_0^\ast must be :ref:`valid <valid-instr-seq>` with some type [t_1^n] \to [t_2^*].
- Let C' be the same :ref:`context <context>` as C, but with the :ref:`result type <syntax-resulttype>` [t_1^n] prepended to the |CLABELS| vector.
- Under context C', the instruction sequence \instr^\ast must be :ref:`valid <valid-instr-seq>` with type [] \to [t_2^*].
- Then the compound instruction is valid with type [] \to [t_2^*].
\frac{
S; C \vdashinstrseq \instr_0^\ast : [t_1^n] \to [t_2^*]
\qquad
S; C,\CLABELS\,[t_1^n] \vdashinstrseq \instr^\ast : [] \to [t_2^*]
}{
S; C \vdashadmininstr \LABEL_n\{\instr_0^\ast\}~\instr^\ast~\END : [] \to [t_2^*]
}
.. index:: frame, instruction, result type
- Under the return type [t^n], the :ref:`thread <syntax-frame>` F; \instr^\ast must be :ref:`valid <valid-frame>` with :ref:`result type <syntax-resulttype>` [t^n].
- Then the compound instruction is valid with type [] \to [t^n].
\frac{
S; [t^n] \vdashinstrseq F; \instr^\ast : [t^n]
}{
S; C \vdashadmininstr \FRAME_n\{F\}~\instr^\ast~\END : [] \to [t^n]
}
.. index:: ! store extension, store
Programs can mutate the :ref:`store <syntax-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 :ref:`instructions <exec-instr>` and :ref:`modules <exec-instantiation>`, :ref:`host functions <syntax-hostfunc>` do not automatically adhere to them. Consequently, the required invariants must be stated as explicit constraints on the :ref:`invocation <exec-invoke-host>` of host functions. Soundness only holds when the :ref:`embedder <embedder>` ensures these constraints.
The necessary constraints are codified by the notion of store extension: a store state S' extends state S, written S \extendsto S', when the following rules hold.
Note
Extension does not imply that the new store is valid, which is defined separately :ref:`above <valid-store>`.
.. index:: store, function instance, table instance, memory instance, global instance
- The length of S.\SFUNCS must not shrink.
- The length of S.\STABLES must not shrink.
- The length of S.\SMEMS must not shrink.
- The length of S.\SGLOBALS must not shrink.
- The length of S.\SELEMS must not shrink.
- The length of S.\SDATAS must not shrink.
- For each :ref:`function instance <syntax-funcinst>` \funcinst_i in the original S.\SFUNCS, the new function instance must be an :ref:`extension <extend-funcinst>` of the old.
- For each :ref:`table instance <syntax-tableinst>` \tableinst_i in the original S.\STABLES, the new table instance must be an :ref:`extension <extend-tableinst>` of the old.
- For each :ref:`memory instance <syntax-meminst>` \meminst_i in the original S.\SMEMS, the new memory instance must be an :ref:`extension <extend-meminst>` of the old.
- For each :ref:`global instance <syntax-globalinst>` \globalinst_i in the original S.\SGLOBALS, the new global instance must be an :ref:`extension <extend-globalinst>` of the old.
- For each :ref:`element instance <syntax-eleminst>` \eleminst_i in the original S.\SELEMS, the new global instance must be an :ref:`extension <extend-eleminst>` of the old.
- For each :ref:`data instance <syntax-datainst>` \datainst_i in the original S.\SDATAS, the new global instance must be an :ref:`extension <extend-datainst>` of the old.
\frac{
\begin{array}{@{}ccc@{}}
S_1.\SFUNCS = \funcinst_1^\ast &
S_2.\SFUNCS = {\funcinst'_1}^\ast~\funcinst_2^\ast &
(\vdashfuncinstextends \funcinst_1 \extendsto \funcinst'_1)^\ast \\
S_1.\STABLES = \tableinst_1^\ast &
S_2.\STABLES = {\tableinst'_1}^\ast~\tableinst_2^\ast &
(\vdashtableinstextends \tableinst_1 \extendsto \tableinst'_1)^\ast \\
S_1.\SMEMS = \meminst_1^\ast &
S_2.\SMEMS = {\meminst'_1}^\ast~\meminst_2^\ast &
(\vdashmeminstextends \meminst_1 \extendsto \meminst'_1)^\ast \\
S_1.\SGLOBALS = \globalinst_1^\ast &
S_2.\SGLOBALS = {\globalinst'_1}^\ast~\globalinst_2^\ast &
(\vdashglobalinstextends \globalinst_1 \extendsto \globalinst'_1)^\ast \\
S_1.\SELEMS = \eleminst_1^\ast &
S_2.\SELEMS = {\eleminst'_1}^\ast~\eleminst_2^\ast &
(\vdasheleminstextends \eleminst_1 \extendsto \eleminst'_1)^\ast \\
S_1.\SDATAS = \datainst_1^\ast &
S_2.\SDATAS = {\datainst'_1}^\ast~\datainst_2^\ast &
(\vdashdatainstextends \datainst_1 \extendsto \datainst'_1)^\ast \\
\end{array}
}{
\vdashstoreextends S_1 \extendsto S_2
}
.. index:: function instance
- A function instance must remain unchanged.
\frac{
}{
\vdashfuncinstextends \funcinst \extendsto \funcinst
}
.. index:: table instance
- The :ref:`table type <syntax-tabletype>` \tableinst.\TITYPE must remain unchanged.
- The length of \tableinst.\TIELEM must not shrink.
\frac{
n_1 \leq n_2
}{
\vdashtableinstextends \{\TITYPE~\X{tt}, \TIELEM~(\X{fa}_1^?)^{n_1}\} \extendsto \{\TITYPE~\X{tt}, \TIELEM~(\X{fa}_2^?)^{n_2}\}
}
.. index:: memory instance
- The :ref:`memory type <syntax-memtype>` \meminst.\MITYPE must remain unchanged.
- The length of \meminst.\MIDATA must not shrink.
\frac{
n_1 \leq n_2
}{
\vdashmeminstextends \{\MITYPE~\X{mt}, \MIDATA~b_1^{n_1}\} \extendsto \{\MITYPE~\X{mt}, \MIDATA~b_2^{n_2}\}
}
.. index:: global instance, value, mutability
- The :ref:`global type <syntax-globaltype>` \globalinst.\GITYPE must remain unchanged.
- Let \mut~t be the structure of \globalinst.\GITYPE.
- If \mut is |MCONST|, then the :ref:`value <syntax-val>` \globalinst.\GIVALUE must remain unchanged.
\frac{
\mut = \MVAR \vee \val_1 = \val_2
}{
\vdashglobalinstextends \{\GITYPE~(\mut~t), \GIVALUE~\val_1\} \extendsto \{\GITYPE~(\mut~t), \GIVALUE~\val_2\}
}
.. index:: element instance
- The vector \eleminst.\EIELEM must either remain unchanged or shrink to length 0.
\frac{
\X{fa}_1^\ast = \X{fa}_2^\ast \vee \X{fa}_2^\ast = \epsilon
}{
\vdasheleminstextends \{\EIELEM~\X{fa}_1^\ast\} \extendsto \{\EIELEM~\X{fa}_2^\ast\}
}
.. index:: data instance
- The vector \datainst.\DIDATA must either remain unchanged or shrink to length 0.
\frac{
b_1^\ast = b_2^\ast \vee b_2^\ast = \epsilon
}{
\vdashdatainstextends \{\DIDATA~b_1^\ast\} \extendsto \{\DIDATA~b_2^\ast\}
}
.. index:: ! preservation, ! progress, soundness, configuration, thread, terminal configuration, instantiation, invocation, validity, module
Given the definition of :ref:`valid configurations <valid-config>`, the standard soundness theorems hold. [2]
Theorem (Preservation). If a :ref:`configuration <syntax-config>` S;T is :ref:`valid <valid-config>` with :ref:`result type <syntax-resulttype>` [t^\ast] (i.e., \vdashconfig S;T : [t^\ast]), and steps to S';T' (i.e., S;T \stepto S';T'), then S';T' is a valid configuration with the same result type (i.e., \vdashconfig S';T' : [t^\ast]). Furthermore, S' is an :ref:`extension <extend-store>` of S (i.e., \vdashstoreextends S \extendsto S').
A terminal :ref:`thread <syntax-thread>` is one whose sequence of :ref:`instructions <syntax-instr>` is a :ref:`result <syntax-result>`. A terminal configuration is a configuration whose thread is terminal.
Theorem (Progress). If a :ref:`configuration <syntax-config>` S;T is :ref:`valid <valid-config>` (i.e., \vdashconfig S;T : [t^\ast] for some :ref:`result type <syntax-resulttype>` [t^\ast]), then either it is terminal, or it can step to some configuration S';T' (i.e., S;T \stepto S';T').
From Preservation and Progress the soundness of the WebAssembly type system follows directly.
Corollary (Soundness). If a :ref:`configuration <syntax-config>` S;T is :ref:`valid <valid-config>` (i.e., \vdashconfig S;T : [t^\ast] for some :ref:`result type <syntax-resulttype>` [t^\ast]), then it either diverges or takes a finite number of steps to reach a terminal configuration S';T' (i.e., S;T \stepto^\ast S';T') that is valid with the same result type (i.e., \vdashconfig S';T' : [t^\ast]) and where S' is an :ref:`extension <extend-store>` of S (i.e., \vdashstoreextends S \extendsto 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 :ref:`valid store <valid-store>`, no computation defined by :ref:`instantiation <exec-instantiation>` or :ref:`invocation <exec-invocation>` of a valid module can "crash" or otherwise (mis)behave in ways not covered by the :ref:`execution <exec>` 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. |PLDI2017|_. 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 is described in the following article: Conrad Watt. |CPP2018|_. Proceedings of the 7th ACM SIGPLAN Conference on Certified Programs and Proofs (CPP 2018). ACM 2018. |