Skip to content
This repository was archived by the owner on Apr 25, 2025. It is now read-only.

Commit a17e84f

Browse files
ioannadrossberg
andauthored
Add uncaught exceptions to the core formal spec. (#238)
I found several places that needed change by grepping for 'trap'. Also applied suggestions from code review Co-authored-by: Andreas Rossberg <[email protected]>
1 parent dbb53bf commit a17e84f

File tree

5 files changed

+21
-24
lines changed

5 files changed

+21
-24
lines changed

document/core/appendix/properties.rst

+6-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Soundness
77
The :ref:`type system <type-system>` of WebAssembly is *sound*, implying both *type safety* and *memory safety* with respect to the WebAssembly semantics. For example:
88

99
* All types declared and derived during validation are respected at run time;
10-
e.g., every :ref:`local <syntax-local>` or :ref:`global <syntax-global>` variable will only contain type-correct values, every :ref:`instruction <syntax-instr>` will only be applied to operands of the expected type, and every :ref:`function <syntax-func>` :ref:`invocation <exec-invocation>` always evaluates to a result of the right type (if it does not :ref:`trap <trap>` or diverge).
10+
e.g., every :ref:`local <syntax-local>` or :ref:`global <syntax-global>` variable will only contain type-correct values, every :ref:`instruction <syntax-instr>` will only be applied to operands of the expected type, and every :ref:`function <syntax-func>` :ref:`invocation <exec-invocation>` always evaluates to a result of the right type (if it does not :ref:`trap <trap>`, throw an exception, or diverge).
1111

1212
* No memory location will be read or written except those explicitly defined by the program, i.e., as a :ref:`local <syntax-local>`, a :ref:`global <syntax-global>`, an element in a :ref:`table <syntax-table>`, or a location within a linear :ref:`memory <syntax-mem>`.
1313

@@ -20,7 +20,7 @@ The typing rules defining WebAssembly :ref:`validation <valid>` only cover the *
2020
In order to state and prove soundness precisely, the typing rules must be extended to the *dynamic* components of the abstract :ref:`runtime <syntax-runtime>`, that is, the :ref:`store <syntax-store>`, :ref:`configurations <syntax-config>`, and :ref:`administrative instructions <syntax-instr-admin>`. [#cite-pldi2017]_
2121

2222

23-
.. index:: value, value type, result, result type, trap
23+
.. index:: value, value type, result, result type, trap, exception, throw
2424
.. _valid-result:
2525

2626
Results
@@ -58,6 +58,9 @@ Results
5858
S \vdashresult \TRAP : [t^\ast]
5959
}
6060
61+
.. todo::
62+
Add validation for exception results.
63+
6164

6265
.. _module-context:
6366
.. _valid-store:
@@ -979,7 +982,7 @@ If a :ref:`configuration <syntax-config>` :math:`S;T` is :ref:`valid <valid-conf
979982
then it either diverges or takes a finite number of steps to reach a terminal configuration :math:`S';T'` (i.e., :math:`S;T \stepto^\ast S';T'`) that is valid with the same result type (i.e., :math:`\vdashconfig S';T' : [t^\ast]`)
980983
and where :math:`S'` is an :ref:`extension <extend-store>` of :math:`S` (i.e., :math:`\vdashstoreextends S \extendsto S'`).
981984

982-
In other words, every thread in a valid configuration either runs forever, traps, or terminates with a result that has the expected type.
985+
In other words, every thread in a valid configuration either runs forever, traps, throws an exception, or terminates with a result that has the expected type.
983986
Consequently, given a :ref:`valid store <valid-store>`, no computation defined by :ref:`instantiation <exec-instantiation>` or :ref:`invocation <exec-invocation>` of a valid module can "crash" or otherwise (mis)behave in ways not covered by the :ref:`execution <exec>` semantics given in this specification.
984987

985988

document/core/exec/conventions.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ The following conventions are adopted in stating these rules.
4949

5050
* Execution can *enter* and *exit* :ref:`instruction sequences <syntax-instr-seq>` that form :ref:`blocks <syntax-instr-control>`.
5151

52-
* :ref:`Instruction sequences <syntax-instr-seq>` are implicitly executed in order, unless a trap or jump occurs.
52+
* :ref:`Instruction sequences <syntax-instr-seq>` are implicitly executed in order, unless a trap or jump occurs, or an exception is thrown.
5353

5454
* In various places the rules contain *assertions* expressing crucial invariants about the program state.
5555

@@ -105,7 +105,7 @@ The order of reduction is determined by the definition of an appropriate :ref:`e
105105

106106
Reduction *terminates* when no more reduction rules are applicable.
107107
:ref:`Soundness <soundness>` of the WebAssembly :ref:`type system <type-system>` guarantees that this is only the case when the original instruction sequence has either been reduced to a sequence of |CONST| instructions, which can be interpreted as the :ref:`values <syntax-val>` of the resulting operand stack,
108-
or if a :ref:`trap <syntax-trap>` occurred.
108+
or if a :ref:`trap <syntax-trap>` or an uncaught exception occurred.
109109

110110
.. note::
111111
For example, the following instruction sequence,

document/core/exec/instructions.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -3171,7 +3171,7 @@ Host Functions
31713171
..............
31723172

31733173
Invoking a :ref:`host function <syntax-hostfunc>` has non-deterministic behavior.
3174-
It may either terminate with a :ref:`trap <trap>` or return regularly.
3174+
It may either terminate with a :ref:`trap <trap>`, an exception, or return regularly.
31753175
However, in the latter case, it must consume and produce the right number and types of WebAssembly :ref:`values <syntax-val>` on the stack,
31763176
according to its :ref:`function type <syntax-functype>`.
31773177

document/core/exec/modules.rst

+5-5
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,7 @@ Moreover, if the dots :math:`\dots` are a sequence :math:`A^n` (as for globals o
648648
In an implementation, this recursion is easily unraveled by mutating one or the other in a secondary step.
649649

650650

651-
.. index:: ! instantiation, module, instance, store, trap
651+
.. index:: ! instantiation, module, instance, store, trap, exception
652652
.. _exec-module:
653653
.. _exec-instantiation:
654654

@@ -659,7 +659,7 @@ Given a :ref:`store <syntax-store>` :math:`S`, a :ref:`module <syntax-module>` :
659659

660660
Instantiation checks that the module is :ref:`valid <valid>` and the provided imports :ref:`match <match-externtype>` the declared types,
661661
and may *fail* with an error otherwise.
662-
Instantiation can also result in a :ref:`trap <trap>` from executing the start function.
662+
Instantiation can also result in a :ref:`trap <trap>` or an exception from executing the start function.
663663
It is up to the :ref:`embedder <embedder>` to define how such conditions are reported.
664664

665665
1. If :math:`\module` is not :ref:`valid <valid-module>`, then:
@@ -816,7 +816,7 @@ where:
816816
:ref:`Evaluation <exec-expr>` of :ref:`constant expressions <valid-constant>` does not affect the store.
817817

818818

819-
.. index:: ! invocation, module, module instance, function, export, function address, function instance, function type, value, stack, trap, store
819+
.. index:: ! invocation, module, module instance, function, export, function address, function instance, function type, value, stack, trap, exception, store
820820
.. _exec-invocation:
821821

822822
Invocation
@@ -825,11 +825,11 @@ Invocation
825825
Once a :ref:`module <syntax-module>` has been :ref:`instantiated <exec-instantiation>`, any exported function can be *invoked* externally via its :ref:`function address <syntax-funcaddr>` :math:`\funcaddr` in the :ref:`store <syntax-store>` :math:`S` and an appropriate list :math:`\val^\ast` of argument :ref:`values <syntax-val>`.
826826

827827
Invocation may *fail* with an error if the arguments do not fit the :ref:`function type <syntax-functype>`.
828-
Invocation can also result in a :ref:`trap <trap>`.
828+
Invocation can also result in a :ref:`trap <trap>` or an exception.
829829
It is up to the :ref:`embedder <embedder>` to define how such conditions are reported.
830830

831831
.. note::
832-
If the :ref:`embedder <embedder>` API performs type checks itself, either statically or dynamically, before performing an invocation, then no failure other than traps can occur.
832+
If the :ref:`embedder <embedder>` API performs type checks itself, either statically or dynamically, before performing an invocation, then no failure other than traps or exceptions can occur.
833833

834834
The following steps are performed:
835835

document/core/exec/runtime.rst

+7-13
Original file line numberDiff line numberDiff line change
@@ -66,26 +66,24 @@ Convention
6666
* The meta variable :math:`r` ranges over reference values where clear from context.
6767

6868

69-
.. index:: ! result, value, trap
69+
.. index:: ! result, value, trap, exception
7070
pair: abstract syntax; result
7171
.. _syntax-result:
7272

7373
Results
7474
~~~~~~~
7575

7676
A *result* is the outcome of a computation.
77-
It is either a sequence of :ref:`values <syntax-val>` or a :ref:`trap <syntax-trap>`.
77+
It is either a sequence of :ref:`values <syntax-val>`, a :ref:`trap <syntax-trap>`, or an uncaught exception wrapped in its throw context.
7878

7979
.. math::
8080
\begin{array}{llcl}
8181
\production{(result)} & \result &::=&
8282
\val^\ast \\&&|&
83-
\TRAP
83+
\TRAP \\&&|&
84+
\XT[\val^\ast~(\THROWadm~\tagaddr)]
8485
\end{array}
8586
86-
.. todo::
87-
Add a result value for an unhandled exception.
88-
8987
.. note::
9088
In the current version of WebAssembly, a result can consist of at most one value.
9189

@@ -714,13 +712,14 @@ the following syntax of *throw contexts* is defined, as well as associated struc
714712
.. math::
715713
\begin{array}{llll}
716714
\production{(throw contexts)} & \XT &::=&
717-
\val^\ast~[\_]~\instr^\ast \\ &&|&
715+
[\_] | \val^\ast~\XT~\instr^\ast \\ &&|&
718716
\LABEL_n\{\instr^\ast\}~\XT~\END \\ &&|&
719717
\CAUGHTadm\{\tagaddr~\val^\ast\}~\XT~\END \\ &&|&
720718
\FRAME_n\{F\}~\XT~\END \\
721719
\end{array}
722720
723721
Throw contexts allow matching the program context around a throw instruction up to the innermost enclosing |CATCHadm| or |DELEGATEadm|, thereby selecting the exception |handler| responsible for an exception, if one exists.
722+
If no exception :ref:`handler that catches the exception <syntax-handler>` is found, the computation :ref:`results <syntax-result>` in an uncaught exception result value, which contains the exception's entire throw context.
724723

725724
.. note::
726725
Contrary to block contexts, throw contexts don't skip over handlers.
@@ -763,8 +762,6 @@ Throw contexts allow matching the program context around a throw instruction up
763762

764763
In this particular case, the exception is caught by the exception handler :math:`H` and its values are returned.
765764

766-
.. todo::
767-
Add administrative values to describe unresolved throws.
768765

769766

770767
.. index:: ! configuration, ! thread, store, frame, instruction, module instruction
@@ -820,10 +817,7 @@ Finally, the following definition of *evaluation context* and associated structu
820817
\end{array}
821818
822819
Reduction terminates when a thread's instruction sequence has been reduced to a :ref:`result <syntax-result>`,
823-
that is, either a sequence of :ref:`values <syntax-val>` or to a |TRAP|.
824-
825-
.. todo::
826-
Add rules to deal with unresolved :math:`\THROWadm~\tagaddr`, and extend results to include such situations.
820+
that is, either a sequence of :ref:`values <syntax-val>`, to an uncaught exception, or to a |TRAP|.
827821

828822
.. note::
829823
The restriction on evaluation contexts rules out contexts like :math:`[\_]` and :math:`\epsilon~[\_]~\epsilon` for which :math:`E[\TRAP] = \TRAP`.

0 commit comments

Comments
 (0)