Skip to content

Commit 4bb213a

Browse files
authored
[spec/test] Fix scoping of non-imported globals (#1525)
1 parent f35463a commit 4bb213a

File tree

5 files changed

+67
-36
lines changed

5 files changed

+67
-36
lines changed

document/core/valid/instructions.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1641,7 +1641,7 @@ Constant Expressions
16411641
}
16421642
16431643
.. note::
1644-
Currently, constant expressions occurring as initializers of :ref:`globals <syntax-global>` are further constrained in that contained |GLOBALGET| instructions are only allowed to refer to *imported* globals.
1644+
Currently, constant expressions occurring in :ref:`globals <syntax-global>`, :ref:`element <syntax-elem>`, or :ref:`data <syntax-data>` segments are further constrained in that contained |GLOBALGET| instructions are only allowed to refer to *imported* globals.
16451645
This is enforced in the :ref:`validation rule for modules <valid-module>` by constraining the context :math:`C` accordingly.
16461646

16471647
The definition of constant expression may be extended in future versions of WebAssembly.

document/core/valid/modules.rst

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -546,40 +546,40 @@ Instead, the context :math:`C` for validation of the module's content is constru
546546

547547
* all other fields are empty.
548548

549-
* Under the context :math:`C`:
549+
* For each :math:`\functype_i` in :math:`\module.\MTYPES`,
550+
the :ref:`function type <syntax-functype>` :math:`\functype_i` must be :ref:`valid <valid-functype>`.
550551

551-
* For each :math:`\functype_i` in :math:`\module.\MTYPES`,
552-
the :ref:`function type <syntax-functype>` :math:`\functype_i` must be :ref:`valid <valid-functype>`.
552+
* Under the context :math:`C`:
553553

554554
* For each :math:`\func_i` in :math:`\module.\MFUNCS`,
555555
the definition :math:`\func_i` must be :ref:`valid <valid-func>` with a :ref:`function type <syntax-functype>` :math:`\X{ft}_i`.
556556

557+
* If :math:`\module.\MSTART` is non-empty,
558+
then :math:`\module.\MSTART` must be :ref:`valid <valid-start>`.
559+
560+
* For each :math:`\import_i` in :math:`\module.\MIMPORTS`,
561+
the segment :math:`\import_i` must be :ref:`valid <valid-import>` with an :ref:`external type <syntax-externtype>` :math:`\X{it}_i`.
562+
563+
* For each :math:`\export_i` in :math:`\module.\MEXPORTS`,
564+
the segment :math:`\export_i` must be :ref:`valid <valid-export>` with :ref:`external type <syntax-externtype>` :math:`\X{et}_i`.
565+
566+
* Under the context :math:`C'`:
567+
557568
* For each :math:`\table_i` in :math:`\module.\MTABLES`,
558569
the definition :math:`\table_i` must be :ref:`valid <valid-table>` with a :ref:`table type <syntax-tabletype>` :math:`\X{tt}_i`.
559570

560571
* For each :math:`\mem_i` in :math:`\module.\MMEMS`,
561572
the definition :math:`\mem_i` must be :ref:`valid <valid-mem>` with a :ref:`memory type <syntax-memtype>` :math:`\X{mt}_i`.
562573

563-
* For each :math:`\global_i` in :math:`\module.\MGLOBALS`:
564-
565-
* Under the context :math:`C'`,
566-
the definition :math:`\global_i` must be :ref:`valid <valid-global>` with a :ref:`global type <syntax-globaltype>` :math:`\X{gt}_i`.
574+
* For each :math:`\global_i` in :math:`\module.\MGLOBALS`,
575+
the definition :math:`\global_i` must be :ref:`valid <valid-global>` with a :ref:`global type <syntax-globaltype>` :math:`\X{gt}_i`.
567576

568577
* For each :math:`\elem_i` in :math:`\module.\MELEMS`,
569578
the segment :math:`\elem_i` must be :ref:`valid <valid-elem>` with :ref:`reference type <syntax-reftype>` :math:`\X{rt}_i`.
570579

571580
* For each :math:`\data_i` in :math:`\module.\MDATAS`,
572581
the segment :math:`\data_i` must be :ref:`valid <valid-data>`.
573582

574-
* If :math:`\module.\MSTART` is non-empty,
575-
then :math:`\module.\MSTART` must be :ref:`valid <valid-start>`.
576-
577-
* For each :math:`\import_i` in :math:`\module.\MIMPORTS`,
578-
the segment :math:`\import_i` must be :ref:`valid <valid-import>` with an :ref:`external type <syntax-externtype>` :math:`\X{it}_i`.
579-
580-
* For each :math:`\export_i` in :math:`\module.\MEXPORTS`,
581-
the segment :math:`\export_i` must be :ref:`valid <valid-export>` with :ref:`external type <syntax-externtype>` :math:`\X{et}_i`.
582-
583583
* The length of :math:`C.\CMEMS` must not be larger than :math:`1`.
584584

585585
* All export names :math:`\export_i.\ENAME` must be different.
@@ -607,15 +607,15 @@ Instead, the context :math:`C` for validation of the module's content is constru
607607
\quad
608608
(C \vdashfunc \func : \X{ft})^\ast
609609
\quad
610-
(C \vdashtable \table : \X{tt})^\ast
610+
(C' \vdashtable \table : \X{tt})^\ast
611611
\quad
612-
(C \vdashmem \mem : \X{mt})^\ast
612+
(C' \vdashmem \mem : \X{mt})^\ast
613613
\quad
614614
(C' \vdashglobal \global : \X{gt})^\ast
615615
\\
616-
(C \vdashelem \elem : \X{rt})^\ast
616+
(C' \vdashelem \elem : \X{rt})^\ast
617617
\quad
618-
(C \vdashdata \data \ok)^n
618+
(C' \vdashdata \data \ok)^n
619619
\quad
620620
(C \vdashstart \start \ok)^?
621621
\quad
@@ -667,8 +667,8 @@ Instead, the context :math:`C` for validation of the module's content is constru
667667
However, this recursion is just a specification device.
668668
All types needed to construct :math:`C` can easily be determined from a simple pre-pass over the module that does not perform any actual validation.
669669

670-
Globals, however, are not recursive.
671-
The effect of defining the limited context :math:`C'` for validating the module's globals is that their initialization expressions can only access functions and imported globals and nothing else.
670+
Globals, however, are not recursive and not accessible within :ref:`constant expressions <valid-const>` when they are defined locally.
671+
The effect of defining the limited context :math:`C'` for validating certain definitions is that they can only access functions and imported globals and nothing else.
672672

673673
.. note::
674674
The restriction on the number of memories may be lifted in future versions of WebAssembly.

test/core/data.wast

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,15 @@
8181
(data (global.get $g) "a")
8282
)
8383

84-
;; Use of internal globals in constant expressions is not allowed in MVP.
85-
;; (module (memory 1) (data (global.get 0) "a") (global i32 (i32.const 0)))
86-
;; (module (memory 1) (data (global.get $g) "a") (global $g i32 (i32.const 0)))
84+
(assert_invalid
85+
(module (memory 1) (global i32 (i32.const 0)) (data (global.get 0) "a"))
86+
"unknown global"
87+
)
88+
(assert_invalid
89+
(module (memory 1) (global $g i32 (i32.const 0)) (data (global.get $g) "a"))
90+
"unknown global"
91+
)
92+
8793

8894
;; Corner cases
8995

@@ -456,11 +462,14 @@
456462
"constant expression required"
457463
)
458464

459-
;; Use of internal globals in constant expressions is not allowed in MVP.
460-
;; (assert_invalid
461-
;; (module (memory 1) (data (global.get $g)) (global $g (mut i32) (i32.const 0)))
462-
;; "constant expression required"
463-
;; )
465+
(assert_invalid
466+
(module
467+
(global $g (import "test" "g") (mut i32))
468+
(memory 1)
469+
(data (global.get $g))
470+
)
471+
"constant expression required"
472+
)
464473

465474
(assert_invalid
466475
(module

test/core/elem.wast

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,16 @@
148148
(assert_return (invoke "call-7") (i32.const 65))
149149
(assert_return (invoke "call-9") (i32.const 66))
150150

151+
(assert_invalid
152+
(module (table 1 funcref) (global i32 (i32.const 0)) (elem (global.get 0) $f) (func $f))
153+
"unknown global"
154+
)
155+
(assert_invalid
156+
(module (table 1 funcref) (global $g i32 (i32.const 0)) (elem (global.get $g) $f) (func $f))
157+
"unknown global"
158+
)
159+
160+
151161
;; Corner cases
152162

153163
(module
@@ -425,11 +435,14 @@
425435
"constant expression required"
426436
)
427437

428-
;; Use of internal globals in constant expressions is not allowed in MVP.
429-
;; (assert_invalid
430-
;; (module (table 1 funcref) (elem (global.get $g)) (global $g i32 (i32.const 0)))
431-
;; "constant expression required"
432-
;; )
438+
(assert_invalid
439+
(module
440+
(global $g (import "test" "g") (mut i32))
441+
(table 1 funcref)
442+
(elem (global.get $g))
443+
)
444+
"constant expression required"
445+
)
433446

434447
(assert_invalid
435448
(module

test/core/global.wast

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,15 @@
348348
"unknown global"
349349
)
350350

351+
(assert_invalid
352+
(module (global i32 (i32.const 0)) (global i32 (global.get 0)))
353+
"unknown global"
354+
)
355+
(assert_invalid
356+
(module (global $g i32 (i32.const 0)) (global i32 (global.get $g)))
357+
"unknown global"
358+
)
359+
351360
(assert_invalid
352361
(module (global i32 (global.get 1)) (global i32 (i32.const 0)))
353362
"unknown global"

0 commit comments

Comments
 (0)