Skip to content

Commit

Permalink
Merge pull request #492 from WebAssembly/fix.490
Browse files Browse the repository at this point in the history
[spec] Sync prose for module instantiation
  • Loading branch information
rossberg authored Dec 13, 2023
2 parents bc887cd + 5ba1334 commit 93f81bb
Showing 1 changed file with 12 additions and 20 deletions.
32 changes: 12 additions & 20 deletions document/core/exec/modules.rst
Original file line number Diff line number Diff line change
Expand Up @@ -517,19 +517,17 @@ It is up to the :ref:`embedder <embedder>` to define how such conditions are rep

.. _exec-initvals:

5. Let :math:`\moduleinst_{\F{init}}` be the auxiliary module :ref:`instance <syntax-moduleinst>` :math:`\{\MIGLOBALS~\evglobals(\externval^n), \MIFUNCS~\moduleinst.\MIFUNCS\}` that only consists of the imported globals and the imported and allocated functions from the final module instance :math:`\moduleinst`, defined below.
6. Let :math:`F` be the auxiliary :ref:`frame <syntax-frame>` :math:`\{ \AMODULE~\moduleinst, \ALOCALS~\epsilon \}`, that consists of the final module instance :math:`\moduleinst`, defined below.

6. Let :math:`F_{\F{init}}` be the auxiliary :ref:`frame <syntax-frame>` :math:`\{ \AMODULE~\moduleinst_{\F{init}}, \ALOCALS~\epsilon \}`.

7. Push the frame :math:`F_{\F{init}}` to the stack.
7. Push the frame :math:`F` to the stack.

8. Let :math:`\val_{\F{g}}^\ast` be the vector of :ref:`global <syntax-global>` initialization :ref:`values <syntax-val>` determined by :math:`\module` and :math:`\externval^n`. These may be calculated as follows.

a. For each :ref:`global <syntax-global>` :math:`\global_i` in :math:`\module.\MGLOBALS`, do:

i. Let :math:`\val_{\F{g}i}` be the result of :ref:`evaluating <exec-expr>` the initializer expression :math:`\global_i.\GINIT`.

b. Assert: due to :ref:`validation <valid-module>`, the frame :math:`F_{\F{init}}` is now on the top of the stack.
b. Assert: due to :ref:`validation <valid-module>`, the frame :math:`F` is now on the top of the stack.

c. Let :math:`\val_{\F{g}}^\ast` be the concatenation of :math:`\val_{\F{g}i}` in index order.

Expand All @@ -543,7 +541,7 @@ It is up to the :ref:`embedder <embedder>` to define how such conditions are rep

iii. Let :math:`\reff_{\F{t}i}` be the reference :math:`\val_{\F{t}i}`.

b. Assert: due to :ref:`validation <valid-module>`, the frame :math:`F_{\F{init}}` is now on the top of the stack.
b. Assert: due to :ref:`validation <valid-module>`, the frame :math:`F` is now on the top of the stack.

c. Let :math:`\reff_{\F{t}}^\ast` be the concatenation of :math:`\reff_{ti}` in index order.

Expand All @@ -557,15 +555,9 @@ It is up to the :ref:`embedder <embedder>` to define how such conditions are rep

c. Let :math:`(\reff_{\F{e}}^\ast)^\ast` be the concatenation of function element vectors :math:`\reff^\ast_i` in order of index :math:`i`.

11. Pop the frame :math:`F_{\F{init}}` from the stack.

12. Let :math:`\moduleinst` be a new module instance :ref:`allocated <alloc-module>` from :math:`\module` in store :math:`S` with imports :math:`\externval^n`, global initializer values :math:`\val_{\F{g}}^\ast`, table initializer values :math:`\reff_{\F{t}}^\ast`, and element segment contents :math:`(\reff_{\F{e}}^\ast)^\ast`, and let :math:`S'` be the extended store produced by module allocation.

13. Let :math:`F` be the auxiliary :ref:`frame <syntax-frame>` :math:`\{ \AMODULE~\moduleinst, \ALOCALS~\epsilon \}`.

14. Push the frame :math:`F` to the stack.
11. Let :math:`\moduleinst` be a new module instance :ref:`allocated <alloc-module>` from :math:`\module` in store :math:`S` with imports :math:`\externval^n`, global initializer values :math:`\val_{\F{g}}^\ast`, table initializer values :math:`\reff_{\F{t}}^\ast`, and element segment contents :math:`(\reff_{\F{e}}^\ast)^\ast`, and let :math:`S'` be the extended store produced by module allocation.

15. For each :ref:`element segment <syntax-elem>` :math:`\elem_i` in :math:`\module.\MELEMS` whose :ref:`mode <syntax-elemmode>` is of the form :math:`\EACTIVE~\{ \ETABLE~\tableidx_i, \EOFFSET~\X{einstr}^\ast_i~\END \}`, do:
12. For each :ref:`element segment <syntax-elem>` :math:`\elem_i` in :math:`\module.\MELEMS` whose :ref:`mode <syntax-elemmode>` is of the form :math:`\EACTIVE~\{ \ETABLE~\tableidx_i, \EOFFSET~\X{einstr}^\ast_i~\END \}`, do:

a. Let :math:`n` be the length of the vector :math:`\elem_i.\EINIT`.

Expand All @@ -579,11 +571,11 @@ It is up to the :ref:`embedder <embedder>` to define how such conditions are rep

f. :ref:`Execute <exec-elem.drop>` the instruction :math:`\ELEMDROP~i`.

16. For each :ref:`element segment <syntax-elem>` :math:`\elem_i` in :math:`\module.\MELEMS` whose :ref:`mode <syntax-elemmode>` is of the form :math:`\EDECLARATIVE`, do:
13. For each :ref:`element segment <syntax-elem>` :math:`\elem_i` in :math:`\module.\MELEMS` whose :ref:`mode <syntax-elemmode>` is of the form :math:`\EDECLARATIVE`, do:

a. :ref:`Execute <exec-elem.drop>` the instruction :math:`\ELEMDROP~i`.

17. For each :ref:`data segment <syntax-data>` :math:`\data_i` in :math:`\module.\MDATAS` whose :ref:`mode <syntax-datamode>` is of the form :math:`\DACTIVE~\{ \DMEM~\memidx_i, \DOFFSET~\X{dinstr}^\ast_i~\END \}`, do:
14. For each :ref:`data segment <syntax-data>` :math:`\data_i` in :math:`\module.\MDATAS` whose :ref:`mode <syntax-datamode>` is of the form :math:`\DACTIVE~\{ \DMEM~\memidx_i, \DOFFSET~\X{dinstr}^\ast_i~\END \}`, do:

a. Assert: :math:`\memidx_i` is :math:`0`.

Expand All @@ -599,15 +591,15 @@ It is up to the :ref:`embedder <embedder>` to define how such conditions are rep

g. :ref:`Execute <exec-data.drop>` the instruction :math:`\DATADROP~i`.

18. If the :ref:`start function <syntax-start>` :math:`\module.\MSTART` is not empty, then:
15. If the :ref:`start function <syntax-start>` :math:`\module.\MSTART` is not empty, then:

a. Let :math:`\start` be the :ref:`start function <syntax-start>` :math:`\module.\MSTART`.

b. :ref:`Execute <exec-call>` the instruction :math:`\CALL~\start.\SFUNC`.

19. Assert: due to :ref:`validation <valid-module>`, the frame :math:`F` is now on the top of the stack.
16. Assert: due to :ref:`validation <valid-module>`, the frame :math:`F` is now on the top of the stack.

20. Pop the frame :math:`F` from the stack.
17. Pop the frame :math:`F` from the stack.


.. math::
Expand Down Expand Up @@ -658,7 +650,7 @@ where:

Similarly, module :ref:`allocation <alloc-module>` and the :ref:`evaluation <exec-expr>` of :ref:`global <syntax-global>` and :ref:`table <syntax-table>` initializers as well as :ref:`element segments <syntax-elem>` are mutually recursive because the global initialization :ref:`values <syntax-val>` :math:`\val_{\F{g}}^\ast`, :math:`\reff_{\F{t}}`, and element segment contents :math:`(\reff^\ast)^\ast` are passed to the module allocator while depending on the module instance :math:`\moduleinst` and store :math:`S'` returned by allocation.
Again, this recursion is just a specification device.
In practice, the initialization values can :ref:`be determined <exec-initvals>` beforehand by staging module allocation further such that first, the module's own :ref:`function instances <syntax-funcinst>` are pre-allocated in the store, then the initializer expressions are evaluated in order, allocating globals on the way, then the rest of the module instance is allocated, and finally the new function instances' :math:`\AMODULE` fields are set to that module instance.
In practice, the initialization values can :ref:`be determined <exec-initvals>` beforehand by staging module allocation such that first, the module's own :ref:`function instances <syntax-funcinst>` are pre-allocated in the store, then the initializer expressions are evaluated in order, allocating globals on the way, then the rest of the module instance is allocated, and finally the new function instances' :math:`\AMODULE` fields are set to that module instance.
This is possible because :ref:`validation <valid-module>` ensures that initialization expressions cannot actually call a function, only take their reference.

All failure conditions are checked before any observable mutation of the store takes place.
Expand Down

0 comments on commit 93f81bb

Please sign in to comment.