Skip to content
This repository was archived by the owner on Nov 3, 2021. It is now read-only.

Commit e27b4ea

Browse files
authored
[spec] DataCount section, syntax, and validation (#80)
The data count section has a count that must match the number of data segments. If the data count section isn't present, then `memory.init` and `data.drop` cannot be used. Fixes issue #73.
1 parent d346a8f commit e27b4ea

File tree

3 files changed

+63
-21
lines changed

3 files changed

+63
-21
lines changed

document/core/binary/modules.rst

Lines changed: 61 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -84,22 +84,23 @@ In these cases, the empty result :math:`\epsilon` is interpreted as the empty ve
8484

8585
The following section ids are used:
8686

87-
== ========================================
88-
Id Section
89-
== ========================================
90-
0 :ref:`custom section <binary-customsec>`
91-
1 :ref:`type section <binary-typesec>`
92-
2 :ref:`import section <binary-importsec>`
93-
3 :ref:`function section <binary-funcsec>`
94-
4 :ref:`table section <binary-tablesec>`
95-
5 :ref:`memory section <binary-memsec>`
96-
6 :ref:`global section <binary-globalsec>`
97-
7 :ref:`export section <binary-exportsec>`
98-
8 :ref:`start section <binary-startsec>`
99-
9 :ref:`element section <binary-elemsec>`
100-
10 :ref:`code section <binary-codesec>`
101-
11 :ref:`data section <binary-datasec>`
102-
== ========================================
87+
== ===============================================
88+
Id Section
89+
== ===============================================
90+
0 :ref:`custom section <binary-customsec>`
91+
1 :ref:`type section <binary-typesec>`
92+
2 :ref:`import section <binary-importsec>`
93+
3 :ref:`function section <binary-funcsec>`
94+
4 :ref:`table section <binary-tablesec>`
95+
5 :ref:`memory section <binary-memsec>`
96+
6 :ref:`global section <binary-globalsec>`
97+
7 :ref:`export section <binary-exportsec>`
98+
8 :ref:`start section <binary-startsec>`
99+
9 :ref:`element section <binary-elemsec>`
100+
10 :ref:`code section <binary-codesec>`
101+
11 :ref:`data section <binary-datasec>`
102+
12 :ref:`data count section <binary-datacountsec>`
103+
== ===============================================
103104

104105

105106
.. index:: ! custom section
@@ -433,6 +434,32 @@ It decodes into a vector of :ref:`data segments <syntax-data>` that represent th
433434
segments have a |DMEM| value of :math:`0`.
434435

435436

437+
.. index:: ! data count section, data count, data segment
438+
pair: binary format; data count
439+
pair: section; data count
440+
.. _binary-datacountsec:
441+
442+
Data Count Section
443+
~~~~~~~~~~~~~~~~~~
444+
445+
The *data count section* has the id 12.
446+
It decodes into an optional :ref:`u32 <syntax-uint>` that represents the number of :ref:`data segments <syntax-data>` in the :ref:`data section <binary-datasec>`. If this count does not match the length of the data segment vector, the module is malformed.
447+
448+
.. math::
449+
\begin{array}{llclll}
450+
\production{data count section} & \Bdatacountsec &::=&
451+
\X{n}^?{:}\Bsection_{12}(\Bu32) &\Rightarrow& \X{n}^? \\
452+
\end{array}
453+
454+
.. note::
455+
The data count section is used to simplify single-pass validation. Since the
456+
data section occurs after the code section, the :math:`\MEMORYINIT` and
457+
:math:`\DATADROP` instructions would not be able to check whether the data
458+
segment index is valid until the data section is read. The data count section
459+
occurs before the code section, so a single-pass validator can use this count
460+
instead of deferring validation.
461+
462+
436463
.. index:: module, section, type definition, function type, function, table, memory, global, element, data, start function, import, export, context, version
437464
pair: binary format; module
438465
.. _binary-magic:
@@ -450,6 +477,8 @@ The preamble is followed by a sequence of :ref:`sections <binary-section>`.
450477
while other sections must occur at most once and in the prescribed order.
451478
All sections can be empty.
452479
The lengths of vectors produced by the (possibly empty) :ref:`function <binary-funcsec>` and :ref:`code <binary-codesec>` section must match up.
480+
Similarly, the data count must match the length of the :ref:`data segment <binary-datasec>` vector.
481+
The :math:`\MEMORYINIT` and :math:`\DATADROP` instructions can only be used if the data count section is present.
453482

454483
.. math::
455484
\begin{array}{llcllll}
@@ -479,9 +508,11 @@ The lengths of vectors produced by the (possibly empty) :ref:`function <binary-f
479508
\Bcustomsec^\ast \\ &&&
480509
\elem^\ast{:\,}\Belemsec \\ &&&
481510
\Bcustomsec^\ast \\ &&&
511+
m^?{:\,}\Bdatacountsec \\ &&&
512+
\Bcustomsec^\ast \\ &&&
482513
\X{code}^n{:\,}\Bcodesec \\ &&&
483514
\Bcustomsec^\ast \\ &&&
484-
\data^\ast{:\,}\Bdatasec \\ &&&
515+
\data^{\X{m'}}{:\,}\Bdatasec \\ &&&
485516
\Bcustomsec^\ast
486517
\quad\Rightarrow\quad \{~
487518
\begin{array}[t]{@{}l@{}}
@@ -491,10 +522,14 @@ The lengths of vectors produced by the (possibly empty) :ref:`function <binary-f
491522
\MMEMS~\mem^\ast, \\
492523
\MGLOBALS~\global^\ast, \\
493524
\MELEM~\elem^\ast, \\
494-
\MDATA~\data^\ast, \\
525+
\MDATA~\data^{\X{m'}}, \\
495526
\MSTART~\start^?, \\
496527
\MIMPORTS~\import^\ast, \\
497528
\MEXPORTS~\export^\ast ~\} \\
529+
\end{array} \\ &&&
530+
(\begin{array}[t]{@{}c@{~}l@{}}
531+
\iff & m^? \neq \epsilon \\
532+
\vee & \forall (t^\ast, e) \in \X{code}^n, \MEMORYINIT \notin e \wedge \DATADROP \notin e) \\
498533
\end{array} \\
499534
\end{array}
500535
@@ -503,6 +538,14 @@ where for each :math:`t_i^\ast, e_i` in :math:`\X{code}^n`,
503538
.. math::
504539
\func^n[i] = \{ \FTYPE~\typeidx^n[i], \FLOCALS~t_i^\ast, \FBODY~e_i \} ) \\
505540
541+
and where,
542+
543+
.. math::
544+
\begin{array}{lcl@{\qquad}l}
545+
\X{m'} &=& m & (\iff m^? \neq \epsilon) \\
546+
\X{m'} &=& 0 & (\otherwise)
547+
\end{array}
548+
506549
.. note::
507550
The version of the WebAssembly binary format may increase in the future
508551
if backward-incompatible changes have to be made to the format.

document/core/util/macros.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,7 @@
503503
.. |Belemsec| mathdef:: \xref{binary/modules}{binary-elemsec}{\B{elemsec}}
504504
.. |Bdatasec| mathdef:: \xref{binary/modules}{binary-datasec}{\B{datasec}}
505505
.. |Bstartsec| mathdef:: \xref{binary/modules}{binary-startsec}{\B{startsec}}
506+
.. |Bdatacountsec| mathdef:: \xref{binary/modules}{binary-datacountsec}{\B{datacountsec}}
506507

507508
.. |Bcustom| mathdef:: \xref{binary/modules}{binary-customsec}{\B{custom}}
508509
.. |Btype| mathdef:: \xref{binary/modules}{binary-typedef}{\B{type}}

document/core/valid/instructions.rst

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ Table Instructions
357357
\frac{
358358
C.\CELEM[x] = \segtype
359359
}{
360-
C \vdashinstr \DATADROP~x : [] \to []
360+
C \vdashinstr \ELEMDROP~x : [] \to []
361361
}
362362
363363
@@ -548,8 +548,6 @@ Memory Instructions
548548

549549
* The data segment :math:`C.\CDATA[x]` must be defined in the context.
550550

551-
* The :ref:`segment type <syntax-segtype>` :math:`C.\CDATA[x]` must be |SPASSIVE|.
552-
553551
* Then the instruction is valid with type :math:`[] \to []`.
554552

555553
.. math::

0 commit comments

Comments
 (0)