Type 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]

Contexts

In order to check rolled up recursive types, the context is locally extended with an additional component that records the sub type corresponding to each recursive type index within the current recursive type:

\[\begin{split}\begin{array}{llll} \def\mathdef1384#1{{}}\mathdef1384{context} & C &::=& \{~ \dots, \href{../appendix/properties.html#context-ext}{\mathsf{recs}} ~ \href{../syntax/types.html#syntax-subtype}{\mathit{subtype}}^\ast ~\} \\ \end{array}\end{split}\]

Types

Well-formedness for extended type forms is defined as follows.

Heap Type \(\href{../valid/conventions.html#syntax-heaptype-ext}{\mathsf{bot}}\)

  • The heap type is valid.

\[\frac{ }{ C \href{../valid/types.html#valid-heaptype}{\vdash} \href{../valid/conventions.html#syntax-heaptype-ext}{\mathsf{bot}} \mathrel{\mbox{ok}} }\]

Heap Type \(\href{../syntax/types.html#syntax-heaptype}{\mathsf{rec}}~i\)

  • The recursive type index \(i\) must exist in \(C.\href{../appendix/properties.html#context-ext}{\mathsf{recs}}\).

  • Then the heap type is valid.

\[\frac{ C.\href{../appendix/properties.html#context-ext}{\mathsf{recs}}[i] = \href{../syntax/types.html#syntax-subtype}{\mathit{subtype}} }{ C \href{../valid/types.html#valid-heaptype}{\vdash} \href{../syntax/types.html#syntax-heaptype}{\mathsf{rec}}~i \mathrel{\mbox{ok}} }\]

Value Type \(\href{../valid/conventions.html#syntax-valtype-ext}{\mathsf{bot}}\)

  • The value type is valid.

\[\frac{ }{ C \href{../valid/types.html#valid-valtype}{\vdash} \href{../valid/conventions.html#syntax-valtype-ext}{\mathsf{bot}} \mathrel{\mbox{ok}} }\]

Recursive Types \(\href{../syntax/types.html#syntax-rectype}{\mathsf{rec}}~\href{../syntax/types.html#syntax-subtype}{\mathit{subtype}}^\ast\)

  • Let \(C'\) be the current context \(C\), but where \(\href{../appendix/properties.html#context-ext}{\mathsf{recs}}\) is \(\href{../syntax/types.html#syntax-subtype}{\mathit{subtype}}^\ast\).

  • There must be a type index \(x\), such that for each sub type \(\href{../syntax/types.html#syntax-subtype}{\mathit{subtype}}_i\) in \(\href{../syntax/types.html#syntax-subtype}{\mathit{subtype}}^\ast\):

  • Then the recursive type is valid for the type index \(x\).

\[\frac{ C,\href{../appendix/properties.html#context-ext}{\mathsf{recs}}~\href{../syntax/types.html#syntax-subtype}{\mathit{subtype}}^\ast \href{../valid/types.html#valid-rectype}{\vdash} \href{../syntax/types.html#syntax-rectype}{\mathsf{rec}}~\href{../syntax/types.html#syntax-subtype}{\mathit{subtype}}^\ast ~{\mathrel{\mbox{ok}}}(x,0) }{ C \href{../valid/types.html#valid-rectype}{\vdash} \href{../syntax/types.html#syntax-rectype}{\mathsf{rec}}~\href{../syntax/types.html#syntax-subtype}{\mathit{subtype}}^\ast ~{\mathrel{\mbox{ok}}}(x) }\]
\[\frac{ }{ C \href{../valid/types.html#valid-rectype}{\vdash} \href{../syntax/types.html#syntax-rectype}{\mathsf{rec}}~\epsilon ~{\mathrel{\mbox{ok}}}(x,i) } \qquad \frac{ C \href{../valid/types.html#valid-subtype}{\vdash} \href{../syntax/types.html#syntax-subtype}{\mathit{subtype}} ~{\mathrel{\mbox{ok}}}(x,i) \qquad C \href{../valid/types.html#valid-rectype}{\vdash} \href{../syntax/types.html#syntax-rectype}{\mathsf{rec}}~{\href{../syntax/types.html#syntax-subtype}{\mathit{subtype}}'}^\ast ~{\mathrel{\mbox{ok}}}(x+1,i+1) }{ C \href{../valid/types.html#valid-rectype}{\vdash} \href{../syntax/types.html#syntax-rectype}{\mathsf{rec}}~\href{../syntax/types.html#syntax-subtype}{\mathit{subtype}}~{\href{../syntax/types.html#syntax-subtype}{\mathit{subtype}}'}^\ast ~{\mathrel{\mbox{ok}}}(x,i) }\]

Note

These rules are a generalisation of the ones previously given.

Sub types \(\href{../syntax/types.html#syntax-subtype}{\mathsf{sub}}~\href{../syntax/types.html#syntax-subtype}{\mathsf{final}}^?~\mathit{ht}^\ast~\href{../syntax/types.html#syntax-comptype}{\mathit{comptype}}\)

  • The composite type \(\href{../syntax/types.html#syntax-comptype}{\mathit{comptype}}\) must be valid.

  • The sequence \(\mathit{ht}^\ast\) may be no longer than \(1\).

  • For every heap type \(\mathit{ht}_k\) in \(\mathit{ht}^\ast\):

    • The heap type \(\mathit{ht}_k\) must be ordered before a type index \(x\) and recursive type index a \(i\), meaning:

      • Either \(\mathit{ht}_k\) is a defined type.

      • Or \(\mathit{ht}_k\) is a type index \(y_k\) that is smaller than \(x\).

      • Or \(\mathit{ht}_k\) is a recursive type index \(\href{../syntax/types.html#syntax-heaptype}{\mathsf{rec}}~j_k\) where \(j_k\) is smaller than \(i\).

    • Let sub type \(\href{../syntax/types.html#syntax-subtype}{\mathit{subtype}}_k\) be the unrolling of the heap type \(\mathit{ht}_k\), meaning:

      • Either \(\mathit{ht}_k\) is a defined type \(\href{../valid/conventions.html#syntax-deftype}{\mathit{deftype}}_k\), then \(\href{../syntax/types.html#syntax-subtype}{\mathit{subtype}}_k\) must be the unrolling of \(\href{../valid/conventions.html#syntax-deftype}{\mathit{deftype}}_k\).

      • Or \(\mathit{ht}_k\) is a type index \(y_k\), then \(\href{../syntax/types.html#syntax-subtype}{\mathit{subtype}}_k\) must be the unrolling of the defined type \(C.\href{../valid/conventions.html#context}{\mathsf{types}}[y_k]\).

      • Or \(\mathit{ht}_k\) is a recursive type index \(\href{../syntax/types.html#syntax-heaptype}{\mathsf{rec}}~j_k\), then \(\href{../syntax/types.html#syntax-subtype}{\mathit{subtype}}_k\) must be \(C.\href{../appendix/properties.html#context-ext}{\mathsf{recs}}[j_k]\).

    • The sub type \(\href{../syntax/types.html#syntax-subtype}{\mathit{subtype}}_k\) must not contain \(\href{../syntax/types.html#syntax-subtype}{\mathsf{final}}\).

    • Let \(\href{../syntax/types.html#syntax-comptype}{\mathit{comptype}}'_k\) be the composite type in \(\href{../syntax/types.html#syntax-subtype}{\mathit{subtype}}_k\).

    • The composite type \(\href{../syntax/types.html#syntax-comptype}{\mathit{comptype}}\) must match \(\href{../syntax/types.html#syntax-comptype}{\mathit{comptype}}'_k\).

  • Then the sub type is valid for the type index \(x\) and recursive type index \(i\).

\[\begin{split}\frac{ \begin{array}{@{}c@{}} |\mathit{ht}^\ast| \leq 1 \qquad (\mathit{ht} \prec x,i)^\ast \qquad (\href{../appendix/properties.html#aux-unroll-heaptype}{\mathrm{unroll}}_{C}(\mathit{ht}) = \href{../syntax/types.html#syntax-subtype}{\mathsf{sub}}~{\mathit{ht}'}^\ast~\href{../syntax/types.html#syntax-comptype}{\mathit{comptype}}')^\ast \\ C \href{../valid/types.html#valid-comptype}{\vdash} \href{../syntax/types.html#syntax-comptype}{\mathit{comptype}} \mathrel{\mbox{ok}} \qquad (C \href{../valid/matching.html#match-comptype}{\vdash} \href{../syntax/types.html#syntax-comptype}{\mathit{comptype}} \href{../valid/matching.html#match-comptype}{\leq} \href{../syntax/types.html#syntax-comptype}{\mathit{comptype}}')^\ast \end{array} }{ C \href{../valid/types.html#valid-subtype}{\vdash} \href{../syntax/types.html#syntax-subtype}{\mathsf{sub}}~\href{../syntax/types.html#syntax-subtype}{\mathsf{final}}^?~\mathit{ht}^\ast~\href{../syntax/types.html#syntax-comptype}{\mathit{comptype}} ~{\mathrel{\mbox{ok}}}(x,i) }\end{split}\]

where:

\[\begin{split}\begin{array}{@{}lll@{}} (\href{../valid/conventions.html#syntax-deftype}{\mathit{deftype}} \prec x,i) &=& {\mathrm{true}} \\ (y \prec x,i) &=& y < x \\ (\href{../syntax/types.html#syntax-heaptype}{\mathsf{rec}}~j \prec x,i) &=& j < i \\ [2ex] \href{../appendix/properties.html#aux-unroll-heaptype}{\mathrm{unroll}}_{C}(\href{../valid/conventions.html#syntax-deftype}{\mathit{deftype}}) &=& \href{../valid/conventions.html#aux-unroll-deftype}{\mathrm{unroll}}(\href{../valid/conventions.html#syntax-deftype}{\mathit{deftype}}) \\ \href{../appendix/properties.html#aux-unroll-heaptype}{\mathrm{unroll}}_{C}(y) &=& \href{../valid/conventions.html#aux-unroll-deftype}{\mathrm{unroll}}(C.\href{../valid/conventions.html#context}{\mathsf{types}}[y]) \\ \href{../appendix/properties.html#aux-unroll-heaptype}{\mathrm{unroll}}_{C}(\href{../syntax/types.html#syntax-heaptype}{\mathsf{rec}}~j) &=& C.\href{../appendix/properties.html#context-ext}{\mathsf{recs}}[j] \\ \end{array}\end{split}\]

Note

This rule is a generalisation of the ones previously given, which only allowed type indices as supertypes.

Subtyping

In a rolled-up recursive type, a recursive type indices \(\href{../syntax/types.html#syntax-heaptype}{\mathsf{rec}}~i\) matches another heap type \(\mathit{ht}\) if:

  • Let \(\href{../syntax/types.html#syntax-subtype}{\mathsf{sub}}~\href{../syntax/types.html#syntax-subtype}{\mathsf{final}}^?~{\mathit{ht}'}^\ast~\href{../syntax/types.html#syntax-comptype}{\mathit{comptype}}\) be the sub type \(C.\href{../appendix/properties.html#context-ext}{\mathsf{recs}}[i]\).

  • The heap type \(\mathit{ht}\) is contained in \({\mathit{ht}'}^\ast\).

\[\frac{ C.\href{../appendix/properties.html#context-ext}{\mathsf{recs}}[i] = \href{../syntax/types.html#syntax-subtype}{\mathsf{sub}}~\href{../syntax/types.html#syntax-subtype}{\mathsf{final}}^?~(\mathit{ht}_1^\ast~\mathit{ht}~\mathit{ht}_2^\ast)~\href{../syntax/types.html#syntax-comptype}{\mathit{comptype}} }{ C \href{../valid/matching.html#match-heaptype}{\vdash} \href{../syntax/types.html#syntax-heaptype}{\mathsf{rec}}~i \href{../valid/matching.html#match-heaptype}{\leq} \mathit{ht} }\]

Note

This rule is only invoked when checking validity of rolled-up recursive types.

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]\).

\[\frac{ (S \href{../exec/values.html#valid-val}{\vdash} \href{../exec/runtime.html#syntax-val}{\mathit{val}} : t)^\ast }{ S \href{../appendix/properties.html#valid-result}{\vdash} \href{../exec/runtime.html#syntax-val}{\mathit{val}}^\ast : [t^\ast] }\]

Results \(\href{../exec/runtime.html#syntax-trap}{\mathsf{trap}}\)

\[\frac{ \href{../valid/types.html#valid-resulttype}{\vdash} [t^\ast] \mathrel{\mbox{ok}} }{ S \href{../appendix/properties.html#valid-result}{\vdash} \href{../exec/runtime.html#syntax-trap}{\mathsf{trap}} : [t^\ast] }\]

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.

  • Each structure instance \(\href{../exec/runtime.html#syntax-structinst}{\mathit{structinst}}_i\) in \(S.\href{../exec/runtime.html#syntax-store}{\mathsf{structs}}\) must be valid.

  • Each array instance \(\href{../exec/runtime.html#syntax-arrayinst}{\mathit{arrayinst}}_i\) in \(S.\href{../exec/runtime.html#syntax-store}{\mathsf{arrays}}\) must be valid.

  • No reference to a bound structure address must be reachable from itself through a path consisting only of indirections through immutable structure or array fields.

  • No reference to a bound array address must be reachable from itself through a path consisting only of indirections through immutable structure or array fields.

  • Then the store is valid.

\[\begin{split}~\\[-1ex] \frac{ \begin{array}{@{}c@{}} (S \href{../appendix/properties.html#valid-funcinst}{\vdash} \href{../exec/runtime.html#syntax-funcinst}{\mathit{funcinst}} : \href{../valid/conventions.html#syntax-deftype}{\mathit{deftype}})^\ast \qquad (S \href{../appendix/properties.html#valid-tableinst}{\vdash} \href{../exec/runtime.html#syntax-tableinst}{\mathit{tableinst}} : \href{../syntax/types.html#syntax-tabletype}{\mathit{tabletype}})^\ast \\ (S \href{../appendix/properties.html#valid-meminst}{\vdash} \href{../exec/runtime.html#syntax-meminst}{\mathit{meminst}} : \href{../syntax/types.html#syntax-memtype}{\mathit{memtype}})^\ast \qquad (S \href{../appendix/properties.html#valid-globalinst}{\vdash} \href{../exec/runtime.html#syntax-globalinst}{\mathit{globalinst}} : \href{../syntax/types.html#syntax-globaltype}{\mathit{globaltype}})^\ast \\ (S \href{../appendix/properties.html#valid-eleminst}{\vdash} \href{../exec/runtime.html#syntax-eleminst}{\mathit{eleminst}} : \href{../syntax/types.html#syntax-reftype}{\mathit{reftype}})^\ast \qquad (S \href{../appendix/properties.html#valid-datainst}{\vdash} \href{../exec/runtime.html#syntax-datainst}{\mathit{datainst}} \mathrel{\mbox{ok}})^\ast \\ (S \href{../appendix/properties.html#valid-structinst}{\vdash} \href{../exec/runtime.html#syntax-structinst}{\mathit{structinst}} \mathrel{\mbox{ok}})^\ast \qquad (S \href{../appendix/properties.html#valid-arrayinst}{\vdash} \href{../exec/runtime.html#syntax-arrayinst}{\mathit{arrayinst}} \mathrel{\mbox{ok}})^\ast \\ S = \{ \begin{array}[t]{@{}l@{}} \href{../exec/runtime.html#syntax-store}{\mathsf{funcs}}~\href{../exec/runtime.html#syntax-funcinst}{\mathit{funcinst}}^\ast, \href{../exec/runtime.html#syntax-store}{\mathsf{globals}}~\href{../exec/runtime.html#syntax-globalinst}{\mathit{globalinst}}^\ast, \href{../exec/runtime.html#syntax-store}{\mathsf{tables}}~\href{../exec/runtime.html#syntax-tableinst}{\mathit{tableinst}}^\ast, \href{../exec/runtime.html#syntax-store}{\mathsf{mems}}~\href{../exec/runtime.html#syntax-meminst}{\mathit{meminst}}^\ast, \\ \href{../exec/runtime.html#syntax-store}{\mathsf{elems}}~\href{../exec/runtime.html#syntax-eleminst}{\mathit{eleminst}}^\ast, \href{../exec/runtime.html#syntax-store}{\mathsf{datas}}~\href{../exec/runtime.html#syntax-datainst}{\mathit{datainst}}^\ast, \href{../exec/runtime.html#syntax-store}{\mathsf{structs}}~\href{../exec/runtime.html#syntax-structinst}{\mathit{structinst}}^\ast, \href{../exec/runtime.html#syntax-store}{\mathsf{arrays}}~\href{../exec/runtime.html#syntax-arrayinst}{\mathit{arrayinst}}^\ast \} \end{array} \\ (S.\href{../exec/runtime.html#syntax-store}{\mathsf{structs}}[a_{\mathrm{s}}] = \href{../exec/runtime.html#syntax-structinst}{\mathit{structinst}})^\ast \qquad ((\href{../exec/runtime.html#syntax-ref}{\mathsf{ref{.}struct}}~a_{\mathrm{s}}) \not\gg^+_S (\href{../exec/runtime.html#syntax-ref}{\mathsf{ref{.}struct}}~a_{\mathrm{s}}))^\ast \\ (S.\href{../exec/runtime.html#syntax-store}{\mathsf{arrays}}[a_{\mathrm{a}}] = \href{../exec/runtime.html#syntax-arrayinst}{\mathit{arrayinst}})^\ast \qquad ((\href{../exec/runtime.html#syntax-ref}{\mathsf{ref{.}array}}~a_{\mathrm{a}}) \not\gg^+_S (\href{../exec/runtime.html#syntax-ref}{\mathsf{ref{.}array}}~a_{\mathrm{a}}))^\ast \end{array} }{ \href{../appendix/properties.html#valid-store}{\vdash} S \mathrel{\mbox{ok}} }\end{split}\]

where \(\href{../exec/runtime.html#syntax-val}{\mathit{val}}_1 \gg^+_S \href{../exec/runtime.html#syntax-val}{\mathit{val}}_2\) denotes the transitive closure of the following reachability relation on values:

\[\begin{split}\begin{array}{@{}lcll@{}} (\href{../exec/runtime.html#syntax-ref}{\mathsf{ref{.}struct}}~a) &\gg_S& S.\href{../exec/runtime.html#syntax-store}{\mathsf{structs}}[a].\href{../exec/runtime.html#syntax-structinst}{\mathsf{fields}}[i] & \mathrel{\mbox{if}} \href{../valid/conventions.html#aux-expand-deftype}{\mathrm{expand}}(S.\href{../exec/runtime.html#syntax-store}{\mathsf{structs}}[a].\href{../exec/runtime.html#syntax-structinst}{\mathsf{type}}) = \href{../syntax/types.html#syntax-comptype}{\mathsf{struct}}~\mathit{ft}_1^i~(\href{../syntax/types.html#syntax-mut}{\mathsf{const}}~\mathit{st})~\mathit{ft}_2^\ast \\ (\href{../exec/runtime.html#syntax-ref}{\mathsf{ref{.}array}}~a) &\gg_S& S.\href{../exec/runtime.html#syntax-store}{\mathsf{arrays}}[a].\href{../exec/runtime.html#syntax-arrayinst}{\mathsf{fields}}[i] & \mathrel{\mbox{if}} \href{../valid/conventions.html#aux-expand-deftype}{\mathrm{expand}}(S.\href{../exec/runtime.html#syntax-store}{\mathsf{arrays}}[a].\href{../exec/runtime.html#syntax-arrayinst}{\mathsf{type}}) = \href{../syntax/types.html#syntax-comptype}{\mathsf{array}}~(\href{../syntax/types.html#syntax-mut}{\mathsf{const}}~\mathit{st}) \\ (\href{../exec/runtime.html#syntax-ref}{\mathsf{ref{.}extern}}~\href{../exec/runtime.html#syntax-ref}{\mathit{ref}}) &\gg_S& \href{../exec/runtime.html#syntax-ref}{\mathit{ref}} \\ \end{array}\end{split}\]

Note

The constraint on reachability through immutable fields prevents the presence of cyclic data structures that can not be constructed in the language. Cycles can only be formed using mutation.

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 under an empty context.

  • 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 some function type \(\href{../syntax/types.html#syntax-functype}{\mathit{functype}}'\).

    • The function type \(\href{../syntax/types.html#syntax-functype}{\mathit{functype}}'\) must match \(\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}}\).

\[\begin{split}\frac{ \begin{array}{@{}c@{}} \href{../valid/types.html#valid-functype}{\vdash} \href{../syntax/types.html#syntax-functype}{\mathit{functype}} \mathrel{\mbox{ok}} \qquad S \href{../appendix/properties.html#valid-moduleinst}{\vdash} \href{../exec/runtime.html#syntax-moduleinst}{\mathit{moduleinst}} : C \\ C \href{../valid/modules.html#valid-func}{\vdash} \href{../syntax/modules.html#syntax-func}{\mathit{func}} : \href{../syntax/types.html#syntax-functype}{\mathit{functype}}' \qquad C \href{../valid/matching.html#match-functype}{\vdash} \href{../syntax/types.html#syntax-functype}{\mathit{functype}}' \href{../valid/matching.html#match-functype}{\leq} \href{../syntax/types.html#syntax-functype}{\mathit{functype}} \end{array} }{ S \href{../appendix/properties.html#valid-funcinst}{\vdash} \{\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}}\} : \href{../syntax/types.html#syntax-functype}{\mathit{functype}} }\end{split}\]

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 under an empty context.

  • Let \([t_1^\ast] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t_2^\ast]\) be the function type \(\href{../syntax/types.html#syntax-functype}{\mathit{functype}}\).

  • For every valid store \(S_1\) extending \(S\) and every sequence \(\href{../exec/runtime.html#syntax-val}{\mathit{val}}^\ast\) of values whose types coincide with \(t_1^\ast\):

    • Executing \(\mathit{hf}\) in store \(S_1\) with arguments \(\href{../exec/runtime.html#syntax-val}{\mathit{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 valid store \(S_2\) extending \(S_1\) and a result \(\href{../exec/runtime.html#syntax-result}{\mathit{result}}\) whose type coincides with \([t_2^\ast]\).

  • Then the function instance is valid with function type \(\href{../syntax/types.html#syntax-functype}{\mathit{functype}}\).

\[\begin{split}\frac{ \begin{array}[b]{@{}l@{}} \href{../valid/types.html#valid-functype}{\vdash} [t_1^\ast] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t_2^\ast] \mathrel{\mbox{ok}} \\ \end{array} \quad \begin{array}[b]{@{}l@{}} \forall S_1, \href{../exec/runtime.html#syntax-val}{\mathit{val}}^\ast,~ {\href{../appendix/properties.html#valid-store}{\vdash} S_1 \mathrel{\mbox{ok}}} \wedge {\href{../appendix/properties.html#extend-store}{\vdash} S \href{../appendix/properties.html#extend}{\preceq} S_1} \wedge {S_1 \href{../appendix/properties.html#valid-result}{\vdash} \href{../exec/runtime.html#syntax-val}{\mathit{val}}^\ast : [t_1^\ast]} \Longrightarrow {} \\ \qquad \mathit{hf}(S_1; \href{../exec/runtime.html#syntax-val}{\mathit{val}}^\ast) \supset \emptyset \wedge {} \\ \qquad \forall R \in \mathit{hf}(S_1; \href{../exec/runtime.html#syntax-val}{\mathit{val}}^\ast),~ R = \bot \vee {} \\ \qquad\qquad \exists S_2, \href{../exec/runtime.html#syntax-result}{\mathit{result}},~ {\href{../appendix/properties.html#valid-store}{\vdash} S_2 \mathrel{\mbox{ok}}} \wedge {\href{../appendix/properties.html#extend-store}{\vdash} S_1 \href{../appendix/properties.html#extend}{\preceq} S_2} \wedge {S_2 \href{../appendix/properties.html#valid-result}{\vdash} \href{../exec/runtime.html#syntax-result}{\mathit{result}} : [t_2^\ast]} \wedge R = (S_2; \href{../exec/runtime.html#syntax-result}{\mathit{result}}) \end{array} }{ S \href{../appendix/properties.html#valid-funcinst}{\vdash} \{\href{../exec/runtime.html#syntax-funcinst}{\mathsf{type}}~[t_1^\ast] \mathrel{\href{../valid/conventions.html#syntax-instrtype}{\rightarrow}} [t_2^\ast], \href{../exec/runtime.html#syntax-funcinst}{\mathsf{hostcode}}~\mathit{hf}\} : [t_1^\ast] \mathrel{\href{../valid/conventions.html#syntax-instrtype}{\rightarrow}} [t_2^\ast] }\end{split}\]

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 execution rule 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.

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 under the empty context.

  • 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\):

  • Then the table instance is valid with table type \(\href{../syntax/types.html#syntax-limits}{\mathit{limits}}~t\).

\[\frac{ \href{../valid/types.html#valid-tabletype}{\vdash} \href{../syntax/types.html#syntax-limits}{\mathit{limits}}~t \mathrel{\mbox{ok}} \qquad n = \href{../syntax/types.html#syntax-limits}{\mathit{limits}}.\href{../syntax/types.html#syntax-limits}{\mathsf{min}} \qquad (S \vdash \href{../exec/runtime.html#syntax-ref}{\mathit{ref}} : t')^n \qquad (\href{../valid/matching.html#match-reftype}{\vdash} t' \href{../valid/matching.html#match-valtype}{\leq} t)^n }{ S \href{../appendix/properties.html#valid-tableinst}{\vdash} \{ \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}}^n \} : \href{../syntax/types.html#syntax-limits}{\mathit{limits}}~t }\]

Memory Instances \(\{ \href{../exec/runtime.html#syntax-meminst}{\mathsf{type}}~\href{../syntax/types.html#syntax-limits}{\mathit{limits}}, \href{../exec/runtime.html#syntax-meminst}{\mathsf{data}}~b^\ast \}\)

  • The memory type \(\href{../syntax/types.html#syntax-limits}{\mathit{limits}}\) must be valid under the empty context.

  • The length of \(b^\ast\) must equal \(\href{../syntax/types.html#syntax-limits}{\mathit{limits}}.\href{../syntax/types.html#syntax-limits}{\mathsf{min}}\) multiplied by the page size \(64\,\mathrm{Ki}\).

  • Then the memory instance is valid with memory type \(\href{../syntax/types.html#syntax-limits}{\mathit{limits}}\).

\[\frac{ \href{../valid/types.html#valid-memtype}{\vdash} \href{../syntax/types.html#syntax-limits}{\mathit{limits}} \mathrel{\mbox{ok}} \qquad n = \href{../syntax/types.html#syntax-limits}{\mathit{limits}}.\href{../syntax/types.html#syntax-limits}{\mathsf{min}} \cdot 64\,\mathrm{Ki} }{ S \href{../appendix/properties.html#valid-meminst}{\vdash} \{ \href{../exec/runtime.html#syntax-meminst}{\mathsf{type}}~\href{../syntax/types.html#syntax-limits}{\mathit{limits}}, \href{../exec/runtime.html#syntax-meminst}{\mathsf{data}}~b^n \} : \href{../syntax/types.html#syntax-limits}{\mathit{limits}} }\]

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}} \}\)

\[\frac{ \href{../valid/types.html#valid-globaltype}{\vdash} \href{../syntax/types.html#syntax-mut}{\mathit{mut}}~t \mathrel{\mbox{ok}} \qquad S \href{../exec/values.html#valid-val}{\vdash} \href{../exec/runtime.html#syntax-val}{\mathit{val}} : t' \qquad \href{../valid/matching.html#match-valtype}{\vdash} t' \href{../valid/matching.html#match-valtype}{\leq} t }{ S \href{../appendix/properties.html#valid-globalinst}{\vdash} \{ \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}} \} : \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 \}\)

\[\frac{ \href{../valid/types.html#valid-reftype}{\vdash} t \mathrel{\mbox{ok}} \qquad (S \href{../exec/values.html#valid-val}{\vdash} \href{../exec/runtime.html#syntax-ref}{\mathit{ref}} : t')^\ast \qquad (\href{../valid/matching.html#match-reftype}{\vdash} t' \href{../valid/matching.html#match-valtype}{\leq} t)^\ast }{ S \href{../appendix/properties.html#valid-eleminst}{\vdash} \{ \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 \} : t }\]

Data Instances \(\{ \href{../exec/runtime.html#syntax-datainst}{\mathsf{data}}~b^\ast \}\)

  • The data instance is valid.

\[\frac{ }{ S \href{../appendix/properties.html#valid-datainst}{\vdash} \{ \href{../exec/runtime.html#syntax-datainst}{\mathsf{data}}~b^\ast \} \mathrel{\mbox{ok}} }\]

Structure Instances \(\{ \href{../exec/runtime.html#syntax-structinst}{\mathsf{type}}~\href{../valid/conventions.html#syntax-deftype}{\mathit{deftype}}, \href{../exec/runtime.html#syntax-structinst}{\mathsf{fields}}~\href{../exec/runtime.html#syntax-fieldval}{\mathit{fieldval}}^\ast \}\)

  • The defined type \(\href{../valid/conventions.html#syntax-deftype}{\mathit{deftype}}\) must be valid.

  • The expansion of \(\href{../valid/conventions.html#syntax-deftype}{\mathit{deftype}}\) must be a structure type \(\href{../syntax/types.html#syntax-comptype}{\mathsf{struct}}~\href{../syntax/types.html#syntax-fieldtype}{\mathit{fieldtype}}^\ast\).

  • The length of the sequence of field values \(\href{../exec/runtime.html#syntax-fieldval}{\mathit{fieldval}}^\ast\) must be the same as the length of the sequence of field types \(\href{../syntax/types.html#syntax-fieldtype}{\mathit{fieldtype}}^\ast\).

  • For each field value \(\href{../exec/runtime.html#syntax-fieldval}{\mathit{fieldval}}_i\) in \(\href{../exec/runtime.html#syntax-fieldval}{\mathit{fieldval}}^\ast\) and corresponding field type \(\href{../syntax/types.html#syntax-fieldtype}{\mathit{fieldtype}}_i\) in \(\href{../syntax/types.html#syntax-fieldtype}{\mathit{fieldtype}}^\ast\):

    • Let \(\href{../syntax/types.html#syntax-fieldtype}{\mathit{fieldtype}}_i\) be \(\href{../syntax/types.html#syntax-mut}{\mathit{mut}}~\href{../syntax/types.html#syntax-storagetype}{\mathit{storagetype}}_i\).

    • The field value \(\href{../exec/runtime.html#syntax-fieldval}{\mathit{fieldval}}_i\) must be valid with storage type \(\href{../syntax/types.html#syntax-storagetype}{\mathit{storagetype}}_i\).

  • Then the structure instance is valid.

\[\frac{ \href{../valid/types.html#valid-deftype}{\vdash} \mathit{dt} \mathrel{\mbox{ok}} \qquad \href{../valid/conventions.html#aux-expand-deftype}{\mathrm{expand}}(\mathit{dt}) = \href{../syntax/types.html#syntax-comptype}{\mathsf{struct}}~(\href{../syntax/types.html#syntax-mut}{\mathit{mut}}~\mathit{st})^\ast \qquad (S \href{../appendix/properties.html#valid-fieldval}{\vdash} \mathit{fv} : \mathit{st})^\ast }{ S \href{../appendix/properties.html#valid-structinst}{\vdash} \{ \href{../exec/runtime.html#syntax-structinst}{\mathsf{type}}~\mathit{dt}, \href{../exec/runtime.html#syntax-structinst}{\mathsf{fields}}~\mathit{fv}^\ast \} \mathrel{\mbox{ok}} }\]

Array Instances \(\{ \href{../exec/runtime.html#syntax-arrayinst}{\mathsf{type}}~\href{../valid/conventions.html#syntax-deftype}{\mathit{deftype}}, \href{../exec/runtime.html#syntax-arrayinst}{\mathsf{fields}}~\href{../exec/runtime.html#syntax-fieldval}{\mathit{fieldval}}^\ast \}\)

  • The defined type \(\href{../valid/conventions.html#syntax-deftype}{\mathit{deftype}}\) must be valid.

  • The expansion of \(\href{../valid/conventions.html#syntax-deftype}{\mathit{deftype}}\) must be an array type \(\href{../syntax/types.html#syntax-comptype}{\mathsf{array}}~\href{../syntax/types.html#syntax-fieldtype}{\mathit{fieldtype}}\).

  • Let \(\href{../syntax/types.html#syntax-fieldtype}{\mathit{fieldtype}}\) be \(\href{../syntax/types.html#syntax-mut}{\mathit{mut}}~\href{../syntax/types.html#syntax-storagetype}{\mathit{storagetype}}\).

  • For each field value \(\href{../exec/runtime.html#syntax-fieldval}{\mathit{fieldval}}_i\) in \(\href{../exec/runtime.html#syntax-fieldval}{\mathit{fieldval}}^\ast\):

    • The field value \(\href{../exec/runtime.html#syntax-fieldval}{\mathit{fieldval}}_i\) must be valid with storage type \(\href{../syntax/types.html#syntax-storagetype}{\mathit{storagetype}}\).

  • Then the array instance is valid.

\[\frac{ \href{../valid/types.html#valid-deftype}{\vdash} \mathit{dt} \mathrel{\mbox{ok}} \qquad \href{../valid/conventions.html#aux-expand-deftype}{\mathrm{expand}}(\mathit{dt}) = \href{../syntax/types.html#syntax-comptype}{\mathsf{array}}~(\href{../syntax/types.html#syntax-mut}{\mathit{mut}}~\mathit{st}) \qquad (S \href{../appendix/properties.html#valid-fieldval}{\vdash} \mathit{fv} : \mathit{st})^\ast }{ S \href{../appendix/properties.html#valid-arrayinst}{\vdash} \{ \href{../exec/runtime.html#syntax-arrayinst}{\mathsf{type}}~\mathit{dt}, \href{../exec/runtime.html#syntax-arrayinst}{\mathsf{fields}}~\mathit{fv}^\ast \} \mathrel{\mbox{ok}} }\]

Field Values \(\href{../exec/runtime.html#syntax-fieldval}{\mathit{fieldval}}\)

  • If \(\href{../exec/runtime.html#syntax-fieldval}{\mathit{fieldval}}\) is a value \(\href{../exec/runtime.html#syntax-val}{\mathit{val}}\), then:

    • The value \(\href{../exec/runtime.html#syntax-val}{\mathit{val}}\) must be valid with value type \(t\).

    • Then the field value is valid with value type \(t\).

  • Else, \(\href{../exec/runtime.html#syntax-fieldval}{\mathit{fieldval}}\) is a packed value \(\href{../exec/runtime.html#syntax-packedval}{\mathit{packedval}}\):

    • Let \(\href{../syntax/types.html#syntax-packedtype}{\mathit{packedtype}}.\href{../exec/runtime.html#syntax-packedval}{\mathsf{pack}}~i\) be the field value \(\href{../exec/runtime.html#syntax-fieldval}{\mathit{fieldval}}\).

    • Then the field value is valid with packed type \(\href{../syntax/types.html#syntax-packedtype}{\mathit{packedtype}}\).

\[\frac{ }{ S \href{../appendix/properties.html#valid-packedval}{\vdash} \mathit{pt}.\href{../exec/runtime.html#syntax-packedval}{\mathsf{pack}}~i : \mathit{pt} }\]

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.

\[\frac{ S \href{../exec/values.html#valid-externval}{\vdash} \href{../exec/runtime.html#syntax-externval}{\mathit{externval}} : \href{../syntax/types.html#syntax-externtype}{\mathit{externtype}} }{ S \href{../appendix/properties.html#valid-exportinst}{\vdash} \{ \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}} \} \mathrel{\mbox{ok}} }\]

Module Instances \(\href{../exec/runtime.html#syntax-moduleinst}{\mathit{moduleinst}}\)

  • Each defined type \(\href{../valid/conventions.html#syntax-deftype}{\mathit{deftype}}_i\) in \(\href{../exec/runtime.html#syntax-moduleinst}{\mathit{moduleinst}}.\href{../exec/runtime.html#syntax-moduleinst}{\mathsf{types}}\) must be valid under the empty context.

  • 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{../valid/conventions.html#syntax-deftype}{\mathit{deftype}}^\ast\) be the concatenation of all \(\href{../valid/conventions.html#syntax-deftype}{\mathit{deftype}}_i\) in order.

  • 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 \(m\) be the length of \(\href{../exec/runtime.html#syntax-moduleinst}{\mathit{moduleinst}}.\href{../exec/runtime.html#syntax-moduleinst}{\mathsf{funcaddrs}}\).

  • Let \(n\) be the length of \(\href{../exec/runtime.html#syntax-moduleinst}{\mathit{moduleinst}}.\href{../exec/runtime.html#syntax-moduleinst}{\mathsf{dataaddrs}}\).

  • Let \(x^\ast\) be the sequence of function indices from \(0\) to \(m-1\).

  • Then the module instance is valid with context \(\{\href{../valid/conventions.html#context}{\mathsf{types}}~\href{../valid/conventions.html#syntax-deftype}{\mathit{deftype}}^\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,\) \(\href{../valid/conventions.html#context}{\mathsf{refs}}~x^\ast\}\).

\[\begin{split}~\\[-1ex] \frac{ \begin{array}{@{}c@{}} (\href{../valid/types.html#valid-deftype}{\vdash} \href{../valid/conventions.html#syntax-deftype}{\mathit{deftype}} \mathrel{\mbox{ok}})^\ast \\ (S \href{../exec/values.html#valid-externval}{\vdash} \href{../exec/runtime.html#syntax-externval}{\mathsf{func}}~\href{../exec/runtime.html#syntax-funcaddr}{\mathit{funcaddr}} : \href{../syntax/types.html#syntax-externtype}{\mathsf{func}}~\href{../syntax/types.html#syntax-functype}{\mathit{functype}})^\ast \qquad (S \href{../exec/values.html#valid-externval}{\vdash} \href{../exec/runtime.html#syntax-externval}{\mathsf{table}}~\href{../exec/runtime.html#syntax-tableaddr}{\mathit{tableaddr}} : \href{../syntax/types.html#syntax-externtype}{\mathsf{table}}~\href{../syntax/types.html#syntax-tabletype}{\mathit{tabletype}})^\ast \\ (S \href{../exec/values.html#valid-externval}{\vdash} \href{../exec/runtime.html#syntax-externval}{\mathsf{mem}}~\href{../exec/runtime.html#syntax-memaddr}{\mathit{memaddr}} : \href{../syntax/types.html#syntax-externtype}{\mathsf{mem}}~\href{../syntax/types.html#syntax-memtype}{\mathit{memtype}})^\ast \qquad (S \href{../exec/values.html#valid-externval}{\vdash} \href{../exec/runtime.html#syntax-externval}{\mathsf{global}}~\href{../exec/runtime.html#syntax-globaladdr}{\mathit{globaladdr}} : \href{../syntax/types.html#syntax-externtype}{\mathsf{global}}~\href{../syntax/types.html#syntax-globaltype}{\mathit{globaltype}})^\ast \\ (S \href{../appendix/properties.html#valid-eleminst}{\vdash} S.\href{../exec/runtime.html#syntax-store}{\mathsf{elems}}[\href{../exec/runtime.html#syntax-elemaddr}{\mathit{elemaddr}}] : \href{../syntax/types.html#syntax-reftype}{\mathit{reftype}})^\ast \qquad (S \href{../appendix/properties.html#valid-datainst}{\vdash} S.\href{../exec/runtime.html#syntax-store}{\mathsf{datas}}[\href{../exec/runtime.html#syntax-dataaddr}{\mathit{dataaddr}}] \mathrel{\mbox{ok}})^n \\ (S \href{../appendix/properties.html#valid-exportinst}{\vdash} \href{../exec/runtime.html#syntax-exportinst}{\mathit{exportinst}} \mathrel{\mbox{ok}})^\ast \qquad (\href{../exec/runtime.html#syntax-exportinst}{\mathit{exportinst}}.\href{../exec/runtime.html#syntax-exportinst}{\mathsf{name}})^\ast ~\mbox{disjoint} \end{array} }{ S \href{../appendix/properties.html#valid-moduleinst}{\vdash} \{ \begin{array}[t]{@{}l@{~}l@{}} \href{../exec/runtime.html#syntax-moduleinst}{\mathsf{types}} & \href{../valid/conventions.html#syntax-deftype}{\mathit{deftype}}^\ast, \\ \href{../exec/runtime.html#syntax-moduleinst}{\mathsf{funcaddrs}} & \href{../exec/runtime.html#syntax-funcaddr}{\mathit{funcaddr}}^\ast, \\ \href{../exec/runtime.html#syntax-moduleinst}{\mathsf{tableaddrs}} & \href{../exec/runtime.html#syntax-tableaddr}{\mathit{tableaddr}}^\ast, \\ \href{../exec/runtime.html#syntax-moduleinst}{\mathsf{memaddrs}} & \href{../exec/runtime.html#syntax-memaddr}{\mathit{memaddr}}^\ast, \\ \href{../exec/runtime.html#syntax-moduleinst}{\mathsf{globaladdrs}} & \href{../exec/runtime.html#syntax-globaladdr}{\mathit{globaladdr}}^\ast, \\ \href{../exec/runtime.html#syntax-moduleinst}{\mathsf{elemaddrs}} & \href{../exec/runtime.html#syntax-elemaddr}{\mathit{elemaddr}}^\ast, \\ \href{../exec/runtime.html#syntax-moduleinst}{\mathsf{dataaddrs}} & \href{../exec/runtime.html#syntax-dataaddr}{\mathit{dataaddr}}^n, \\ \href{../exec/runtime.html#syntax-moduleinst}{\mathsf{exports}} & \href{../exec/runtime.html#syntax-exportinst}{\mathit{exportinst}}^\ast ~\} : \{ \begin{array}[t]{@{}l@{~}l@{}} \href{../valid/conventions.html#context}{\mathsf{types}} & \href{../valid/conventions.html#syntax-deftype}{\mathit{deftype}}^\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, \\ \href{../valid/conventions.html#context}{\mathsf{refs}} & 0 \dots (|\href{../exec/runtime.html#syntax-funcaddr}{\mathit{funcaddr}}^\ast|-1) ~\} \end{array} \end{array} }\end{split}\]

Configuration Validity

To relate the WebAssembly type system to its execution semantics, the typing rules for instructions must be extended to configurations \(S;T\), which relates the store to execution threads.

Configurations and threads are classified by their result type. 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;T\)

\[\frac{ \href{../appendix/properties.html#valid-store}{\vdash} S \mathrel{\mbox{ok}} \qquad S; \epsilon \href{../appendix/properties.html#valid-thread}{\vdash} T : [t^\ast] }{ \href{../appendix/properties.html#valid-config}{\vdash} S; T : [t^\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.

  • The frame \(F\) must be valid with a context \(C\).

  • 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 \([] \mathrel{\href{../valid/conventions.html#syntax-instrtype}{\rightarrow}} [t^\ast]\).

  • Then the thread is valid with the result type \([t^\ast]\).

\[\frac{ S \href{../appendix/properties.html#valid-frame}{\vdash} F : C \qquad S; C,\href{../valid/conventions.html#context}{\mathsf{return}}~\href{../syntax/types.html#syntax-resulttype}{\mathit{resulttype}}^? \href{../valid/instructions.html#valid-instr-seq}{\vdash} \href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast : [] \mathrel{\href{../valid/conventions.html#syntax-instrtype}{\rightarrow}} [t^\ast] }{ S; \href{../syntax/types.html#syntax-resulttype}{\mathit{resulttype}}^? \href{../appendix/properties.html#valid-thread}{\vdash} F; \href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast : [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'\).

\[\frac{ S \href{../appendix/properties.html#valid-moduleinst}{\vdash} \href{../exec/runtime.html#syntax-moduleinst}{\mathit{moduleinst}} : C \qquad (S \href{../exec/values.html#valid-val}{\vdash} \href{../exec/runtime.html#syntax-val}{\mathit{val}} : t)^\ast }{ S \href{../appendix/properties.html#valid-frame}{\vdash} \{\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}}\} : (C, \href{../valid/conventions.html#context}{\mathsf{locals}}~t^\ast) }\]

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 any valid instruction type of the form \([t_1^\ast] \mathrel{\href{../valid/conventions.html#syntax-instrtype}{\rightarrow}} [t_2^\ast]\).

\[\frac{ C \href{../valid/types.html#valid-instrtype}{\vdash} [t_1^\ast] \mathrel{\href{../valid/conventions.html#syntax-instrtype}{\rightarrow}} [t_2^\ast] \mathrel{\mbox{ok}} }{ S; C \href{../appendix/properties.html#valid-instr-admin}{\vdash} \href{../exec/runtime.html#syntax-trap}{\mathsf{trap}} : [t_1^\ast] \mathrel{\href{../valid/conventions.html#syntax-instrtype}{\rightarrow}} [t_2^\ast] }\]

\(\href{../exec/runtime.html#syntax-val}{\mathit{val}}\)

  • The value \(\href{../exec/runtime.html#syntax-val}{\mathit{val}}\) must be valid with value type \(t\).

  • Then it is valid as an instruction with type \([] \mathrel{\href{../valid/conventions.html#syntax-instrtype}{\rightarrow}} [t]\).

\[\frac{ S \href{../exec/values.html#valid-val}{\vdash} \href{../exec/runtime.html#syntax-val}{\mathit{val}} : t }{ S; C \href{../appendix/properties.html#valid-instr-admin}{\vdash} \href{../exec/runtime.html#syntax-val}{\mathit{val}} : [] \mathrel{\href{../valid/conventions.html#syntax-instrtype}{\rightarrow}} [t] }\]

\(\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}} \href{../syntax/types.html#syntax-functype}{\mathit{functype}}'\).

  • Let \([t_1^\ast] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t_2^\ast])\) be the function type \(\href{../syntax/types.html#syntax-functype}{\mathit{functype}}\).

  • Then the instruction is valid with type \([t_1^\ast] \mathrel{\href{../valid/conventions.html#syntax-instrtype}{\rightarrow}} [t_2^\ast]\).

\[\frac{ S \href{../exec/values.html#valid-externval}{\vdash} \href{../exec/runtime.html#syntax-externval}{\mathsf{func}}~\href{../exec/runtime.html#syntax-funcaddr}{\mathit{funcaddr}} : \href{../syntax/types.html#syntax-externtype}{\mathsf{func}}~[t_1^\ast] \href{../syntax/types.html#syntax-functype}{\rightarrow} [t_2^\ast] }{ S; C \href{../appendix/properties.html#valid-instr-admin}{\vdash} \href{../exec/runtime.html#syntax-invoke}{\mathsf{invoke}}~\href{../exec/runtime.html#syntax-funcaddr}{\mathit{funcaddr}} : [t_1^\ast] \mathrel{\href{../valid/conventions.html#syntax-instrtype}{\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] \mathrel{\href{../valid/conventions.html#syntax-instrtype}{\rightarrow}}_{x^\ast} [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 \([] \mathrel{\href{../valid/conventions.html#syntax-instrtype}{\rightarrow}}_{{x'}^\ast} [t_2^*]\).

  • Then the compound instruction is valid with type \([] \mathrel{\href{../valid/conventions.html#syntax-instrtype}{\rightarrow}} [t_2^*]\).

\[\frac{ S; C \href{../valid/instructions.html#valid-instr-seq}{\vdash} \href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}_0^\ast : [t_1^n] \mathrel{\href{../valid/conventions.html#syntax-instrtype}{\rightarrow}}_{x^\ast} [t_2^*] \qquad S; C,\href{../valid/conventions.html#context}{\mathsf{labels}}\,[t_1^n] \href{../valid/instructions.html#valid-instr-seq}{\vdash} \href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast : [] \mathrel{\href{../valid/conventions.html#syntax-instrtype}{\rightarrow}}_{{x'}^\ast} [t_2^*] }{ S; C \href{../appendix/properties.html#valid-instr-admin}{\vdash} \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}} : [] \mathrel{\href{../valid/conventions.html#syntax-instrtype}{\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 valid 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 \([] \mathrel{\href{../valid/conventions.html#syntax-instrtype}{\rightarrow}} [t^n]\).

\[\frac{ C \href{../valid/types.html#valid-resulttype}{\vdash} [t^n] \mathrel{\mbox{ok}} \qquad S; [t^n] \href{../valid/instructions.html#valid-instr-seq}{\vdash} F; \href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast : [t^n] }{ S; C \href{../appendix/properties.html#valid-instr-admin}{\vdash} \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}} : [] \mathrel{\href{../valid/conventions.html#syntax-instrtype}{\rightarrow}} [t^n] }\]

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.

  • The length of \(S.\href{../exec/runtime.html#syntax-store}{\mathsf{structs}}\) must not shrink.

  • The length of \(S.\href{../exec/runtime.html#syntax-store}{\mathsf{arrays}}\) 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 element 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 data instance must be an extension of the old.

  • For each structure instance \(\href{../exec/runtime.html#syntax-structinst}{\mathit{structinst}}_i\) in the original \(S.\href{../exec/runtime.html#syntax-store}{\mathsf{structs}}\), the new structure instance must be an extension of the old.

  • For each array instance \(\href{../exec/runtime.html#syntax-arrayinst}{\mathit{arrayinst}}_i\) in the original \(S.\href{../exec/runtime.html#syntax-store}{\mathsf{arrays}}\), the new array instance must be an extension of the old.

\[\begin{split}\frac{ \begin{array}{@{}ccc@{}} S_1.\href{../exec/runtime.html#syntax-store}{\mathsf{funcs}} = \href{../exec/runtime.html#syntax-funcinst}{\mathit{funcinst}}_1^\ast & S_2.\href{../exec/runtime.html#syntax-store}{\mathsf{funcs}} = {\href{../exec/runtime.html#syntax-funcinst}{\mathit{funcinst}}'_1}^\ast~\href{../exec/runtime.html#syntax-funcinst}{\mathit{funcinst}}_2^\ast & (\href{../appendix/properties.html#extend-funcinst}{\vdash} \href{../exec/runtime.html#syntax-funcinst}{\mathit{funcinst}}_1 \href{../appendix/properties.html#extend}{\preceq} \href{../exec/runtime.html#syntax-funcinst}{\mathit{funcinst}}'_1)^\ast \\ S_1.\href{../exec/runtime.html#syntax-store}{\mathsf{tables}} = \href{../exec/runtime.html#syntax-tableinst}{\mathit{tableinst}}_1^\ast & S_2.\href{../exec/runtime.html#syntax-store}{\mathsf{tables}} = {\href{../exec/runtime.html#syntax-tableinst}{\mathit{tableinst}}'_1}^\ast~\href{../exec/runtime.html#syntax-tableinst}{\mathit{tableinst}}_2^\ast & (\href{../appendix/properties.html#extend-tableinst}{\vdash} \href{../exec/runtime.html#syntax-tableinst}{\mathit{tableinst}}_1 \href{../appendix/properties.html#extend}{\preceq} \href{../exec/runtime.html#syntax-tableinst}{\mathit{tableinst}}'_1)^\ast \\ S_1.\href{../exec/runtime.html#syntax-store}{\mathsf{mems}} = \href{../exec/runtime.html#syntax-meminst}{\mathit{meminst}}_1^\ast & S_2.\href{../exec/runtime.html#syntax-store}{\mathsf{mems}} = {\href{../exec/runtime.html#syntax-meminst}{\mathit{meminst}}'_1}^\ast~\href{../exec/runtime.html#syntax-meminst}{\mathit{meminst}}_2^\ast & (\href{../appendix/properties.html#extend-meminst}{\vdash} \href{../exec/runtime.html#syntax-meminst}{\mathit{meminst}}_1 \href{../appendix/properties.html#extend}{\preceq} \href{../exec/runtime.html#syntax-meminst}{\mathit{meminst}}'_1)^\ast \\ S_1.\href{../exec/runtime.html#syntax-store}{\mathsf{globals}} = \href{../exec/runtime.html#syntax-globalinst}{\mathit{globalinst}}_1^\ast & S_2.\href{../exec/runtime.html#syntax-store}{\mathsf{globals}} = {\href{../exec/runtime.html#syntax-globalinst}{\mathit{globalinst}}'_1}^\ast~\href{../exec/runtime.html#syntax-globalinst}{\mathit{globalinst}}_2^\ast & (\href{../appendix/properties.html#extend-globalinst}{\vdash} \href{../exec/runtime.html#syntax-globalinst}{\mathit{globalinst}}_1 \href{../appendix/properties.html#extend}{\preceq} \href{../exec/runtime.html#syntax-globalinst}{\mathit{globalinst}}'_1)^\ast \\ S_1.\href{../exec/runtime.html#syntax-store}{\mathsf{elems}} = \href{../exec/runtime.html#syntax-eleminst}{\mathit{eleminst}}_1^\ast & S_2.\href{../exec/runtime.html#syntax-store}{\mathsf{elems}} = {\href{../exec/runtime.html#syntax-eleminst}{\mathit{eleminst}}'_1}^\ast~\href{../exec/runtime.html#syntax-eleminst}{\mathit{eleminst}}_2^\ast & (\href{../appendix/properties.html#extend-eleminst}{\vdash} \href{../exec/runtime.html#syntax-eleminst}{\mathit{eleminst}}_1 \href{../appendix/properties.html#extend}{\preceq} \href{../exec/runtime.html#syntax-eleminst}{\mathit{eleminst}}'_1)^\ast \\ S_1.\href{../exec/runtime.html#syntax-store}{\mathsf{datas}} = \href{../exec/runtime.html#syntax-datainst}{\mathit{datainst}}_1^\ast & S_2.\href{../exec/runtime.html#syntax-store}{\mathsf{datas}} = {\href{../exec/runtime.html#syntax-datainst}{\mathit{datainst}}'_1}^\ast~\href{../exec/runtime.html#syntax-datainst}{\mathit{datainst}}_2^\ast & (\href{../appendix/properties.html#extend-datainst}{\vdash} \href{../exec/runtime.html#syntax-datainst}{\mathit{datainst}}_1 \href{../appendix/properties.html#extend}{\preceq} \href{../exec/runtime.html#syntax-datainst}{\mathit{datainst}}'_1)^\ast \\ S_1.\href{../exec/runtime.html#syntax-store}{\mathsf{structs}} = \href{../exec/runtime.html#syntax-structinst}{\mathit{structinst}}_1^\ast & S_2.\href{../exec/runtime.html#syntax-store}{\mathsf{structs}} = {\href{../exec/runtime.html#syntax-structinst}{\mathit{structinst}}'_1}^\ast~\href{../exec/runtime.html#syntax-structinst}{\mathit{structinst}}_2^\ast & (\href{../appendix/properties.html#extend-structinst}{\vdash} \href{../exec/runtime.html#syntax-structinst}{\mathit{structinst}}_1 \href{../appendix/properties.html#extend}{\preceq} \href{../exec/runtime.html#syntax-structinst}{\mathit{structinst}}'_1)^\ast \\ S_1.\href{../exec/runtime.html#syntax-store}{\mathsf{arrays}} = \href{../exec/runtime.html#syntax-arrayinst}{\mathit{arrayinst}}_1^\ast & S_2.\href{../exec/runtime.html#syntax-store}{\mathsf{arrays}} = {\href{../exec/runtime.html#syntax-arrayinst}{\mathit{arrayinst}}'_1}^\ast~\href{../exec/runtime.html#syntax-arrayinst}{\mathit{arrayinst}}_2^\ast & (\href{../appendix/properties.html#extend-arrayinst}{\vdash} \href{../exec/runtime.html#syntax-arrayinst}{\mathit{arrayinst}}_1 \href{../appendix/properties.html#extend}{\preceq} \href{../exec/runtime.html#syntax-arrayinst}{\mathit{arrayinst}}'_1)^\ast \\ \end{array} }{ \href{../appendix/properties.html#extend-store}{\vdash} S_1 \href{../appendix/properties.html#extend}{\preceq} S_2 }\end{split}\]

Function Instance \(\href{../exec/runtime.html#syntax-funcinst}{\mathit{funcinst}}\)

  • A function instance must remain unchanged.

\[\frac{ }{ \href{../appendix/properties.html#extend-funcinst}{\vdash} \href{../exec/runtime.html#syntax-funcinst}{\mathit{funcinst}} \href{../appendix/properties.html#extend}{\preceq} \href{../exec/runtime.html#syntax-funcinst}{\mathit{funcinst}} }\]

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.

\[\frac{ n_1 \leq n_2 }{ \href{../appendix/properties.html#extend-tableinst}{\vdash} \{\href{../exec/runtime.html#syntax-tableinst}{\mathsf{type}}~\mathit{tt}, \href{../exec/runtime.html#syntax-tableinst}{\mathsf{elem}}~(\mathit{fa}_1^?)^{n_1}\} \href{../appendix/properties.html#extend}{\preceq} \{\href{../exec/runtime.html#syntax-tableinst}{\mathsf{type}}~\mathit{tt}, \href{../exec/runtime.html#syntax-tableinst}{\mathsf{elem}}~(\mathit{fa}_2^?)^{n_2}\} }\]

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.

\[\frac{ n_1 \leq n_2 }{ \href{../appendix/properties.html#extend-meminst}{\vdash} \{\href{../exec/runtime.html#syntax-meminst}{\mathsf{type}}~\mathit{mt}, \href{../exec/runtime.html#syntax-meminst}{\mathsf{data}}~b_1^{n_1}\} \href{../appendix/properties.html#extend}{\preceq} \{\href{../exec/runtime.html#syntax-meminst}{\mathsf{type}}~\mathit{mt}, \href{../exec/runtime.html#syntax-meminst}{\mathsf{data}}~b_2^{n_2}\} }\]

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.

\[\frac{ \href{../syntax/types.html#syntax-mut}{\mathit{mut}} = \href{../syntax/types.html#syntax-mut}{\mathsf{var}} \vee \href{../exec/runtime.html#syntax-val}{\mathit{val}}_1 = \href{../exec/runtime.html#syntax-val}{\mathit{val}}_2 }{ \href{../appendix/properties.html#extend-globalinst}{\vdash} \{\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}}_1\} \href{../appendix/properties.html#extend}{\preceq} \{\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}}_2\} }\]

Element Instance \(\href{../exec/runtime.html#syntax-eleminst}{\mathit{eleminst}}\)

  • The reference type \(\href{../exec/runtime.html#syntax-eleminst}{\mathit{eleminst}}.\href{../exec/runtime.html#syntax-eleminst}{\mathsf{type}}\) must remain unchanged.

  • 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\).

\[\frac{ }{ \href{../appendix/properties.html#extend-eleminst}{\vdash} \{\href{../exec/runtime.html#syntax-eleminst}{\mathsf{type}}~t, \href{../exec/runtime.html#syntax-eleminst}{\mathsf{elem}}~a^\ast\} \href{../appendix/properties.html#extend}{\preceq} \{\href{../exec/runtime.html#syntax-eleminst}{\mathsf{type}}~t, \href{../exec/runtime.html#syntax-eleminst}{\mathsf{elem}}~a^\ast\} }\]
\[\frac{ }{ \href{../appendix/properties.html#extend-eleminst}{\vdash} \{\href{../exec/runtime.html#syntax-eleminst}{\mathsf{type}}~t, \href{../exec/runtime.html#syntax-eleminst}{\mathsf{elem}}~a^\ast\} \href{../appendix/properties.html#extend}{\preceq} \{\href{../exec/runtime.html#syntax-eleminst}{\mathsf{type}}~t, \href{../exec/runtime.html#syntax-eleminst}{\mathsf{elem}}~\epsilon\} }\]

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\).

\[\frac{ }{ \href{../appendix/properties.html#extend-datainst}{\vdash} \{\href{../exec/runtime.html#syntax-datainst}{\mathsf{data}}~b^\ast\} \href{../appendix/properties.html#extend}{\preceq} \{\href{../exec/runtime.html#syntax-datainst}{\mathsf{data}}~b^\ast\} }\]
\[\frac{ }{ \href{../appendix/properties.html#extend-datainst}{\vdash} \{\href{../exec/runtime.html#syntax-datainst}{\mathsf{data}}~b^\ast\} \href{../appendix/properties.html#extend}{\preceq} \{\href{../exec/runtime.html#syntax-datainst}{\mathsf{data}}~\epsilon\} }\]

Structure Instance \(\href{../exec/runtime.html#syntax-structinst}{\mathit{structinst}}\)

  • The defined type \(\href{../exec/runtime.html#syntax-structinst}{\mathit{structinst}}.\href{../exec/runtime.html#syntax-structinst}{\mathsf{type}}\) must remain unchanged.

  • Assert: due to store well-formedness, the expansion of \(\href{../exec/runtime.html#syntax-structinst}{\mathit{structinst}}.\href{../exec/runtime.html#syntax-structinst}{\mathsf{type}}\) is a structure type.

  • Let \(\href{../syntax/types.html#syntax-comptype}{\mathsf{struct}}~\href{../syntax/types.html#syntax-fieldtype}{\mathit{fieldtype}}^\ast\) be the expansion of \(\href{../exec/runtime.html#syntax-structinst}{\mathit{structinst}}.\href{../exec/runtime.html#syntax-structinst}{\mathsf{type}}\).

  • The length of the vector \(\href{../exec/runtime.html#syntax-structinst}{\mathit{structinst}}.\href{../exec/runtime.html#syntax-structinst}{\mathsf{fields}}\) must remain unchanged.

  • Assert: due to store well-formedness, the length of \(\href{../exec/runtime.html#syntax-structinst}{\mathit{structinst}}.\href{../exec/runtime.html#syntax-structinst}{\mathsf{fields}}\) is the same as the length of \(\href{../syntax/types.html#syntax-fieldtype}{\mathit{fieldtype}}^\ast\).

  • For each field value \(\href{../exec/runtime.html#syntax-fieldval}{\mathit{fieldval}}_i\) in \(\href{../exec/runtime.html#syntax-structinst}{\mathit{structinst}}.\href{../exec/runtime.html#syntax-structinst}{\mathsf{fields}}\) and corresponding field type \(\href{../syntax/types.html#syntax-fieldtype}{\mathit{fieldtype}}_i\) in \(\href{../syntax/types.html#syntax-fieldtype}{\mathit{fieldtype}}^\ast\):

    • Let \(\href{../syntax/types.html#syntax-mut}{\mathit{mut}}_i~\mathit{st}_i\) be the structure of \(\href{../syntax/types.html#syntax-fieldtype}{\mathit{fieldtype}}_i\).

    • If \(\href{../syntax/types.html#syntax-mut}{\mathit{mut}}_i\) is \(\href{../syntax/types.html#syntax-mut}{\mathsf{const}}\), then the field value \(\href{../exec/runtime.html#syntax-fieldval}{\mathit{fieldval}}_i\) must remain unchanged.

\[\frac{ (\href{../syntax/types.html#syntax-mut}{\mathit{mut}} = \href{../syntax/types.html#syntax-mut}{\mathsf{var}} \vee \href{../exec/runtime.html#syntax-fieldval}{\mathit{fieldval}}_1 = \href{../exec/runtime.html#syntax-fieldval}{\mathit{fieldval}}_2)^\ast }{ \href{../appendix/properties.html#extend-structinst}{\vdash} \{\href{../exec/runtime.html#syntax-structinst}{\mathsf{type}}~(\href{../syntax/types.html#syntax-mut}{\mathit{mut}}~\mathit{st})^\ast, \href{../exec/runtime.html#syntax-structinst}{\mathsf{fields}}~\href{../exec/runtime.html#syntax-fieldval}{\mathit{fieldval}}_1^\ast\} \href{../appendix/properties.html#extend}{\preceq} \{\href{../exec/runtime.html#syntax-structinst}{\mathsf{type}}~(\href{../syntax/types.html#syntax-mut}{\mathit{mut}}~\mathit{st})^\ast, \href{../exec/runtime.html#syntax-structinst}{\mathsf{fields}}~\href{../exec/runtime.html#syntax-fieldval}{\mathit{fieldval}}_2^\ast\} }\]

Array Instance \(\href{../exec/runtime.html#syntax-arrayinst}{\mathit{arrayinst}}\)

  • The defined type \(\href{../exec/runtime.html#syntax-arrayinst}{\mathit{arrayinst}}.\href{../exec/runtime.html#syntax-arrayinst}{\mathsf{type}}\) must remain unchanged.

  • Assert: due to store well-formedness, the expansion of \(\href{../exec/runtime.html#syntax-arrayinst}{\mathit{arrayinst}}.\href{../exec/runtime.html#syntax-arrayinst}{\mathsf{type}}\) is an array type.

  • Let \(\href{../syntax/types.html#syntax-comptype}{\mathsf{array}}~\href{../syntax/types.html#syntax-fieldtype}{\mathit{fieldtype}}\) be the expansion of \(\href{../exec/runtime.html#syntax-arrayinst}{\mathit{arrayinst}}.\href{../exec/runtime.html#syntax-arrayinst}{\mathsf{type}}\).

  • The length of the vector \(\href{../exec/runtime.html#syntax-arrayinst}{\mathit{arrayinst}}.\href{../exec/runtime.html#syntax-arrayinst}{\mathsf{fields}}\) must remain unchanged.

  • Let \(\href{../syntax/types.html#syntax-mut}{\mathit{mut}}~\mathit{st}\) be the structure of \(\href{../syntax/types.html#syntax-fieldtype}{\mathit{fieldtype}}\).

  • If \(\href{../syntax/types.html#syntax-mut}{\mathit{mut}}\) is \(\href{../syntax/types.html#syntax-mut}{\mathsf{const}}\), then the sequence of field values \(\href{../exec/runtime.html#syntax-arrayinst}{\mathit{arrayinst}}.\href{../exec/runtime.html#syntax-arrayinst}{\mathsf{fields}}\) must remain unchanged.

\[\frac{ \href{../syntax/types.html#syntax-mut}{\mathit{mut}} = \href{../syntax/types.html#syntax-mut}{\mathsf{var}} \vee \href{../exec/runtime.html#syntax-fieldval}{\mathit{fieldval}}_1^\ast = \href{../exec/runtime.html#syntax-fieldval}{\mathit{fieldval}}_2^\ast }{ \href{../appendix/properties.html#extend-arrayinst}{\vdash} \{\href{../exec/runtime.html#syntax-arrayinst}{\mathsf{type}}~(\href{../syntax/types.html#syntax-mut}{\mathit{mut}}~\mathit{st}), \href{../exec/runtime.html#syntax-arrayinst}{\mathsf{fields}}~\href{../exec/runtime.html#syntax-fieldval}{\mathit{fieldval}}_1^\ast\} \href{../appendix/properties.html#extend}{\preceq} \{\href{../exec/runtime.html#syntax-arrayinst}{\mathsf{type}}~(\href{../syntax/types.html#syntax-mut}{\mathit{mut}}~\mathit{st}), \href{../exec/runtime.html#syntax-arrayinst}{\mathsf{fields}}~\href{../exec/runtime.html#syntax-fieldval}{\mathit{fieldval}}_2^\ast\} }\]

Theorems

Given the definition of valid configurations, the standard soundness theorems hold. [2] [3]

Theorem (Preservation). If a configuration \(S;T\) is valid with result type \([t^\ast]\) (i.e., \(\href{../appendix/properties.html#valid-config}{\vdash} S;T : [t^\ast]\)), and steps to \(S';T'\) (i.e., \(S;T \href{../exec/conventions.html#exec-notation}{\hookrightarrow} S';T'\)), then \(S';T'\) is a valid configuration with the same result type (i.e., \(\href{../appendix/properties.html#valid-config}{\vdash} S';T' : [t^\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 thread is terminal.

Theorem (Progress). If a configuration \(S;T\) is valid (i.e., \(\href{../appendix/properties.html#valid-config}{\vdash} S;T : [t^\ast]\) for some result type \([t^\ast]\)), then either it is terminal, or it can step to some configuration \(S';T'\) (i.e., \(S;T \href{../exec/conventions.html#exec-notation}{\hookrightarrow} S';T'\)).

From Preservation and Progress the soundness of the WebAssembly type system follows directly.

Corollary (Soundness). If a configuration \(S;T\) is valid (i.e., \(\href{../appendix/properties.html#valid-config}{\vdash} S;T : [t^\ast]\) for some result type \([t^\ast]\)), then it either diverges or takes a finite number of steps to reach a terminal configuration \(S';T'\) (i.e., \(S;T \href{../exec/conventions.html#exec-notation}{\hookrightarrow}^\ast S';T'\)) that is valid with the same result type (i.e., \(\href{../appendix/properties.html#valid-config}{\vdash} S';T' : [t^\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.

Type System Properties

Principal Types

The type system of WebAssembly features both subtyping and simple forms of polymorphism for instruction types. That has the effect that every instruction or instruction sequence can be classified with multiple different instruction types.

However, the typing rules still allow deriving principal types for instruction sequences. That is, every valid instruction sequence has one particular type scheme, possibly containing some unconstrained place holder type variables, that is a subtype of all its valid instruction types, after substituting its type variables with suitable specific types.

Moreover, when deriving an instruction type in a “forward” manner, i.e., the input of the instruction sequence is already fixed to specific types, then it has a principal output type expressible without type variables, up to a possibly polymorphic stack bottom representable with one single variable. In other words, “forward” principal types are effectively closed.

Note

For example, in isolation, the instruction \(\href{../syntax/instructions.html#syntax-instr-ref}{\mathsf{ref.as\_non\_null}}\) has the type \([(\href{../syntax/types.html#syntax-reftype}{\mathsf{ref}}~\href{../syntax/types.html#syntax-reftype}{\mathsf{null}}~\mathit{ht})] \mathrel{\href{../valid/conventions.html#syntax-instrtype}{\rightarrow}} [(\href{../syntax/types.html#syntax-reftype}{\mathsf{ref}}~\mathit{ht})]\) for any choice of valid heap type \(\mathit{ht}\). Moreover, if the input type \([(\href{../syntax/types.html#syntax-reftype}{\mathsf{ref}}~\href{../syntax/types.html#syntax-reftype}{\mathsf{null}}~\mathit{ht})]\) is already determined, i.e., a specific \(\mathit{ht}\) is given, then the output type \([(\href{../syntax/types.html#syntax-reftype}{\mathsf{ref}}~\mathit{ht})]\) is fully determined as well.

The implication of the latter property is that a validator for complete instruction sequences (as they occur in valid modules) can be implemented with a simple left-to-right algorithm that does not require the introduction of type variables.

A typing algorithm capable of handling partial instruction sequences (as might be considered for program analysis or program manipulation) needs to introduce type variables and perform substitutions, but it does not need to perform backtracking or record any non-syntactic constraints on these type variables.

Technically, the syntax of heap, value, and result types can be enriched with type variables as follows:

\[\begin{split}\begin{array}{llll} \def\mathdef1384#1{{}}\mathdef1384{nullability} & \mathit{null} &::=& \href{../syntax/types.html#syntax-reftype}{\mathsf{null}}^? ~|~ \alpha_{\mathit{null}} \\ \def\mathdef1384#1{{}}\mathdef1384{heap type} & \href{../syntax/types.html#syntax-heaptype}{\mathit{heaptype}} &::=& \dots ~|~ \alpha_{\href{../syntax/types.html#syntax-heaptype}{\mathit{heaptype}}} \\ \def\mathdef1384#1{{}}\mathdef1384{reference type} & \href{../syntax/types.html#syntax-reftype}{\mathit{reftype}} &::=& \href{../syntax/types.html#syntax-reftype}{\mathsf{ref}}~\mathit{null}~\href{../syntax/types.html#syntax-heaptype}{\mathit{heaptype}} \\ \def\mathdef1384#1{{}}\mathdef1384{value type} & \href{../syntax/types.html#syntax-valtype}{\mathit{valtype}} &::=& \dots ~|~ \alpha_{\href{../syntax/types.html#syntax-valtype}{\mathit{valtype}}} ~|~ \alpha_{\mathit{numvectype}} \\ \def\mathdef1384#1{{}}\mathdef1384{result type} & \href{../syntax/types.html#syntax-resulttype}{\mathit{resulttype}} &::=& [\alpha_{\href{../syntax/types.html#syntax-valtype}{\mathit{valtype}}^\ast}^?~\href{../syntax/types.html#syntax-valtype}{\mathit{valtype}}^\ast] \\ \end{array}\end{split}\]

where each \(\alpha_{\mathit{xyz}}\) ranges over a set of type variables for syntactic class \(\mathit{xyz}\), respectively. The special class \(\mathit{numvectype}\) is defined as \(\href{../syntax/types.html#syntax-numtype}{\mathit{numtype}} ~|~ \href{../syntax/types.html#syntax-vectype}{\mathit{vectype}} ~|~ \href{../valid/conventions.html#syntax-valtype-ext}{\mathsf{bot}}\), and is only needed to handle unannotated \(\href{../syntax/instructions.html#syntax-instr-parametric}{\mathsf{select}}\) instructions.

A type is closed when it does not contain any type variables, and open otherwise. A type substitution \(\sigma\) is a finite mapping from type variables to closed types of the respective syntactic class. When applied to an open type, it replaces the type variables \(\alpha\) from its domain with the respective \(\sigma(\alpha)\).

Theorem (Principal Types). If an instruction sequence \(\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast\) is valid with some closed instruction type \(\href{../valid/conventions.html#syntax-instrtype}{\mathit{instrtype}}\) (i.e., \(C \href{../valid/instructions.html#valid-instr-seq}{\vdash} \href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast : \href{../valid/conventions.html#syntax-instrtype}{\mathit{instrtype}}\)), then it is also valid with a possibly open instruction type \(\href{../valid/conventions.html#syntax-instrtype}{\mathit{instrtype}}_{\min}\) (i.e., \(C \href{../valid/instructions.html#valid-instr-seq}{\vdash} \href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast : \href{../valid/conventions.html#syntax-instrtype}{\mathit{instrtype}}_{\min}\)), such that for every closed type \(\href{../valid/conventions.html#syntax-instrtype}{\mathit{instrtype}}'\) with which \(\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast\) is valid (i.e., for all \(C \href{../valid/instructions.html#valid-instr-seq}{\vdash} \href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast : \href{../valid/conventions.html#syntax-instrtype}{\mathit{instrtype}}'\)), there exists a substitution \(\sigma\), such that \(\sigma(\href{../valid/conventions.html#syntax-instrtype}{\mathit{instrtype}}_{\min})\) is a subtype of \(\href{../valid/conventions.html#syntax-instrtype}{\mathit{instrtype}}'\) (i.e., \(C \href{../valid/matching.html#match-instrtype}{\vdash} \sigma(\href{../valid/conventions.html#syntax-instrtype}{\mathit{instrtype}}_{\min}) \href{../valid/matching.html#match-instrtype}{\leq} \href{../valid/conventions.html#syntax-instrtype}{\mathit{instrtype}}'\)). Furthermore, \(\href{../valid/conventions.html#syntax-instrtype}{\mathit{instrtype}}_{\min}\) is unique up to the choice of type variables.

Theorem (Closed Principal Forward Types). If closed input type \([t_1^\ast]\) is given and the instruction sequence \(\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast\) is valid with instruction type \([t_1^\ast] \mathrel{\href{../valid/conventions.html#syntax-instrtype}{\rightarrow}}_{x^\ast} [t_2^\ast]\) (i.e., \(C \href{../valid/instructions.html#valid-instr-seq}{\vdash} \href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast : [t_1^\ast] \mathrel{\href{../valid/conventions.html#syntax-instrtype}{\rightarrow}}_{x^\ast} [t_2^\ast]\)), then it is also valid with instruction type \([t_1^\ast] \mathrel{\href{../valid/conventions.html#syntax-instrtype}{\rightarrow}}_{x^\ast} [\alpha_{\href{../syntax/types.html#syntax-valtype}{\mathit{valtype}}^\ast}~t^\ast]\) (i.e., \(C \href{../valid/instructions.html#valid-instr-seq}{\vdash} \href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast : [t_1^\ast] \mathrel{\href{../valid/conventions.html#syntax-instrtype}{\rightarrow}}_{x^\ast} [\alpha_{\href{../syntax/types.html#syntax-valtype}{\mathit{valtype}}^\ast}~t^\ast]\)), where all \(t^\ast\) are closed, such that for every closed result type \([{t'_2}^\ast]\) with which \(\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast\) is valid (i.e., for all \(C \href{../valid/instructions.html#valid-instr-seq}{\vdash} \href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast : [t_1^\ast] \mathrel{\href{../valid/conventions.html#syntax-instrtype}{\rightarrow}}_{x^\ast} [{t'_2}^\ast]\)), there exists a substitution \(\sigma\), such that \([{t'_2}^\ast] = [\sigma(\alpha_{\href{../syntax/types.html#syntax-valtype}{\mathit{valtype}}^\ast})~t^\ast]\).

Type Lattice

The Principal Types property depends on the existence of a greatest lower bound for any pair of types.

Theorem (Greatest Lower Bounds for Value Types). For any two value types \(t_1\) and \(t_2\) that are valid (i.e., \(C \href{../valid/types.html#valid-valtype}{\vdash} t_1 \mathrel{\mbox{ok}}\) and \(C \href{../valid/types.html#valid-valtype}{\vdash} t_2 \mathrel{\mbox{ok}}\)), there exists a valid value type \(t\) that is a subtype of both \(t_1\) and \(t_2\) (i.e., \(C \href{../valid/types.html#valid-valtype}{\vdash} t \mathrel{\mbox{ok}}\) and \(C \href{../valid/matching.html#match-valtype}{\vdash} t \href{../valid/matching.html#match-valtype}{\leq} t_1\) and \(C \href{../valid/matching.html#match-valtype}{\vdash} t \href{../valid/matching.html#match-valtype}{\leq} t_2\)), such that every valid value type \(t'\) that also is a subtype of both \(t_1\) and \(t_2\) (i.e., for all \(C \href{../valid/types.html#valid-valtype}{\vdash} t' \mathrel{\mbox{ok}}\) and \(C \href{../valid/matching.html#match-valtype}{\vdash} t' \href{../valid/matching.html#match-valtype}{\leq} t_1\) and \(C \href{../valid/matching.html#match-valtype}{\vdash} t' \href{../valid/matching.html#match-valtype}{\leq} t_2\)), is a subtype of \(t\) (i.e., \(C \href{../valid/matching.html#match-valtype}{\vdash} t' \href{../valid/matching.html#match-valtype}{\leq} t\)).

Note

The greatest lower bound of two types may be \(\href{../valid/conventions.html#syntax-valtype-ext}{\mathsf{bot}}\).

Theorem (Conditional Least Upper Bounds for Value Types). Any two value types \(t_1\) and \(t_2\) that are valid (i.e., \(C \href{../valid/types.html#valid-valtype}{\vdash} t_1 \mathrel{\mbox{ok}}\) and \(C \href{../valid/types.html#valid-valtype}{\vdash} t_2 \mathrel{\mbox{ok}}\)) either have no common supertype, or there exists a valid value type \(t\) that is a supertype of both \(t_1\) and \(t_2\) (i.e., \(C \href{../valid/types.html#valid-valtype}{\vdash} t \mathrel{\mbox{ok}}\) and \(C \href{../valid/matching.html#match-valtype}{\vdash} t_1 \href{../valid/matching.html#match-valtype}{\leq} t\) and \(C \href{../valid/matching.html#match-valtype}{\vdash} t_2 \href{../valid/matching.html#match-valtype}{\leq} t\)), such that every valid value type \(t'\) that also is a supertype of both \(t_1\) and \(t_2\) (i.e., for all \(C \href{../valid/types.html#valid-valtype}{\vdash} t' \mathrel{\mbox{ok}}\) and \(C \href{../valid/matching.html#match-valtype}{\vdash} t_1 \href{../valid/matching.html#match-valtype}{\leq} t'\) and \(C \href{../valid/matching.html#match-valtype}{\vdash} t_2 \href{../valid/matching.html#match-valtype}{\leq} t'\)), is a supertype of \(t\) (i.e., \(C \href{../valid/matching.html#match-valtype}{\vdash} t \href{../valid/matching.html#match-valtype}{\leq} t'\)).

Note

If a top type was added to the type system, a least upper bound would exist for any two types.

Corollary (Type Lattice). Assuming the addition of a provisional top type, value types form a lattice with respect to their subtype relation.

Finally, value types can be partitioned into multiple disjoint hierarchies that are not related by subtyping, except through \(\href{../valid/conventions.html#syntax-valtype-ext}{\mathsf{bot}}\).

Theorem (Disjoint Subtype Hierarchies). The greatest lower bound of two value types is \(\href{../valid/conventions.html#syntax-valtype-ext}{\mathsf{bot}}\) or \(\href{../syntax/types.html#syntax-reftype}{\mathsf{ref}}~\href{../valid/conventions.html#syntax-valtype-ext}{\mathsf{bot}}\) if and only if they do not have a least upper bound.

In other words, types that do not have common supertypes, do not have common subtypes either (other than \(\href{../valid/conventions.html#syntax-valtype-ext}{\mathsf{bot}}\) or \(\href{../syntax/types.html#syntax-reftype}{\mathsf{ref}}~\href{../valid/conventions.html#syntax-valtype-ext}{\mathsf{bot}}\)), and vice versa.

Note

Types from disjoint hierarchies can safely be represented in mutually incompatible ways in an implementation, because their values can never flow to the same place.

Compositionality

Valid instruction sequences can be freely composed, as long as their types match up.

Theorem (Composition). If two instruction sequences \(\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}_1^\ast\) and \(\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}_2^\ast\) are valid with types \([t_1^\ast] \mathrel{\href{../valid/conventions.html#syntax-instrtype}{\rightarrow}}_{x_1^\ast} [t^\ast]\) and \([t^\ast] \mathrel{\href{../valid/conventions.html#syntax-instrtype}{\rightarrow}}_{x_2^\ast} [t_2^\ast]\), respectively (i.e., \(C \href{../valid/instructions.html#valid-instr-seq}{\vdash} \href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}_1^\ast : [t_1^\ast] \mathrel{\href{../valid/conventions.html#syntax-instrtype}{\rightarrow}}_{x_1^\ast} [t^\ast]\) and \(C \href{../valid/instructions.html#valid-instr-seq}{\vdash} \href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}_1^\ast : [t^\ast] \mathrel{\href{../valid/conventions.html#syntax-instrtype}{\rightarrow}}_{x_2^\ast} [t_2^\ast]\)), then the concatenated instruction sequence \((\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}_1^\ast\;\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}_2^\ast)\) is valid with type \([t_1^\ast] \mathrel{\href{../valid/conventions.html#syntax-instrtype}{\rightarrow}}_{x_1^\ast\,x_2^\ast} [t_2^\ast]\) (i.e., \(C \href{../valid/instructions.html#valid-instr-seq}{\vdash} \href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}_1^\ast\;\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}_2^\ast : [t_1^\ast] \mathrel{\href{../valid/conventions.html#syntax-instrtype}{\rightarrow}}_{x_1^\ast\,x_2^\ast} [t_2^\ast]\)).

Note

More generally, instead of a shared type \([t^\ast]\), it suffices if the output type of \(\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}_1^\ast\) is a subtype of the input type of \(\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}_1^\ast\), since the subtype can always be weakened to its supertype by subsumption.

Inversely, valid instruction sequences can also freely be decomposed, that is, splitting them anywhere produces two instruction sequences that are both valid.

Theorem (Decomposition). If an instruction sequence \(\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast\) that is valid with type \([t_1^\ast] \mathrel{\href{../valid/conventions.html#syntax-instrtype}{\rightarrow}}_{x^\ast} [t_2^\ast]\) (i.e., \(C \href{../valid/instructions.html#valid-instr-seq}{\vdash} \href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast : [t_1^\ast] \mathrel{\href{../valid/conventions.html#syntax-instrtype}{\rightarrow}}_{x^\ast} [t_2^\ast]\)) is split into two instruction sequences \(\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}_1^\ast\) and \(\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}_2^\ast\) at any point (i.e., \(\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}^\ast = \href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}_1^\ast\;\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}_2^\ast\)), then these are separately valid with some types \([t_1^\ast] \mathrel{\href{../valid/conventions.html#syntax-instrtype}{\rightarrow}}_{x_1^\ast} [t^\ast]\) and \([t^\ast] \mathrel{\href{../valid/conventions.html#syntax-instrtype}{\rightarrow}}_{x_2^\ast} [t_2^\ast]\), respectively (i.e., \(C \href{../valid/instructions.html#valid-instr-seq}{\vdash} \href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}_1^\ast : [t_1^\ast] \mathrel{\href{../valid/conventions.html#syntax-instrtype}{\rightarrow}}_{x_1^\ast} [t^\ast]\) and \(C \href{../valid/instructions.html#valid-instr-seq}{\vdash} \href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}_1^\ast : [t^\ast] \mathrel{\href{../valid/conventions.html#syntax-instrtype}{\rightarrow}}_{x_2^\ast} [t_2^\ast]\)), where \(x^\ast = x_1^\ast\;x_2^\ast\).

Note

This property holds because validation is required even for unreachable code. Without that, \(\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}_2^\ast\) might not be valid in isolation.