Skip to content

Commit 228e910

Browse files
authored
[spec] Narrow definition of null ref values (#2093)
1 parent 8a4f0d0 commit 228e910

33 files changed

Lines changed: 809 additions & 856 deletions

document/core/exec/instructions.rst

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -687,19 +687,9 @@ Reference Instructions
687687
:math:`\REFNULL~x`
688688
.......................
689689

690-
1. Let :math:`F` be the :ref:`current <exec-notation-textual>` :ref:`frame <syntax-frame>`.
691-
692-
2. Assert: due to :ref:`validation <valid-ref.null>`, the :ref:`defined type <syntax-deftype>` :math:`F.\AMODULE.\MITYPES[x]` exists.
693-
694-
3. Let :math:`\deftype` be the :ref:`defined type <syntax-deftype>` :math:`F.\AMODULE.\MITYPES[x]`.
690+
$${rule-prose: Step_read/ref.null}
695691

696-
4. Push the value :math:`\REFNULL~\deftype` to the stack.
697-
698-
$${rule: {Step_read/ref.null-*}}
699-
700-
.. note::
701-
No formal reduction rule is required for the case |REFNULL| |ABSHEAPTYPE|,
702-
since the instruction form is already a :ref:`value <syntax-val>`.
692+
$${rule: {Step_read/ref.null}}
703693

704694

705695
.. _exec-ref.func:

document/core/exec/runtime.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ Runtime Structure
1212
.. _syntax-num:
1313
.. _syntax-vec:
1414
.. _syntax-ref:
15-
.. _syntax-addrref:
1615
.. _syntax-ref.i31num:
1716
.. _syntax-ref.struct:
1817
.. _syntax-ref.array:
@@ -34,14 +33,15 @@ It is convenient to reuse the same notation as for the ${:CONST} :ref:`instructi
3433

3534
References other than null are represented with additional :ref:`administrative instructions <syntax-instr-admin>`.
3635
They either are *scalar references*, containing a 31-bit :ref:`integer <syntax-int>`,
36+
*null references*,
3737
*structure references*, pointing to a specific :ref:`structure address <syntax-structaddr>`,
3838
*array references*, pointing to a specific :ref:`array address <syntax-arrayaddr>`,
3939
*function references*, pointing to a specific :ref:`function address <syntax-funcaddr>`,
4040
*exception references*, pointing to a specific :ref:`exception address <syntax-exnaddr>`,
4141
or *host references* pointing to an uninterpreted form of :ref:`host address <syntax-hostaddr>` defined by the :ref:`embedder <embedder>`.
4242
Any of the aformentioned references can furthermore be wrapped up as an *external reference*.
4343

44-
$${syntax: val num vec ref addrref}
44+
$${syntax: val num vec ref}
4545

4646
.. note::
4747
Future versions of WebAssembly may add additional forms of values.
@@ -590,7 +590,7 @@ In order to express the reduction of :ref:`traps <trap>`, :ref:`calls <syntax-ca
590590

591591
$${syntax: {instr/admin}}
592592

593-
An :ref:`address reference <syntax-addrref>` represents an allocated :ref:`reference <syntax-ref>` value of respective form :ref:`"on the stack" <exec-notation>`.
593+
A :ref:`reference <syntax-ref>` represents a :ref:`reference <syntax-ref>` value of respective form :ref:`"on the stack" <exec-notation>`.
594594

595595
The ${:LABEL}, ${:FRAME}, and ${:HANDLER} instructions model :ref:`labels <syntax-label>`, :ref:`frames <syntax-frame>`, and active :ref:`exception handlers <syntax-handler>`, respectively, :ref:`"on the stack" <exec-notation>`.
596596
Moreover, the administrative syntax maintains the nesting structure of the original :ref:`structured control instruction <syntax-instr-control>` or :ref:`function body <syntax-func>` and their :ref:`instruction sequences <syntax-instrs>`.

document/core/exec/values.rst

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,6 @@ $${rule-prose: Ref_ok/null}
4646

4747
$${rule: Ref_ok/null}
4848

49-
.. note::
50-
A null reference can be typed with any smaller type.
51-
In particular, that allows it to be typed with the least type in its respective hierarchy.
52-
That ensures that the value is compatible with any nullable type in that hierarchy.
53-
5449

5550
.. _valid-ref.i31num:
5651

document/core/util/macros.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1531,6 +1531,7 @@
15311531
.. Administrative Instructions, terminals
15321532

15331533
.. |REFI31NUM| mathdef:: \xref{exec/runtime}{syntax-ref}{\K{ref{.}i\scriptstyle31}}
1534+
.. |REFNULLADDR| mathdef:: \xref{exec/runtime}{syntax-ref}{\K{ref{.}null}}
15341535
.. |REFFUNCADDR| mathdef:: \xref{exec/runtime}{syntax-ref}{\K{ref{.}func}}
15351536
.. |REFSTRUCTADDR| mathdef:: \xref{exec/runtime}{syntax-ref}{\K{ref{.}struct}}
15361537
.. |REFARRAYADDR| mathdef:: \xref{exec/runtime}{syntax-ref}{\K{ref{.}array}}
@@ -1545,7 +1546,6 @@
15451546
.. |num| mathdef:: \xref{exec/runtime}{syntax-num}{\X{num}}
15461547
.. |vec| mathdef:: \xref{exec/runtime}{syntax-vec}{\X{vec}}
15471548
.. |reff| mathdef:: \xref{exec/runtime}{syntax-ref}{\X{ref}}
1548-
.. |addrref| mathdef:: \xref{exec/runtime}{syntax-addrref}{\X{addrref}}
15491549
.. |val| mathdef:: \xref{exec/runtime}{syntax-val}{\X{val}}
15501550
.. |result| mathdef:: \xref{exec/runtime}{syntax-result}{\X{result}}
15511551

interpreter/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ action:
453453
const:
454454
( <num_type>.const <num> ) ;; number value
455455
( <vec_type> <vec_shape> <num>+ ) ;; vector value
456-
( ref.null <ref_kind> ) ;; null reference
456+
( ref.null <ref_kind>? ) ;; null reference
457457
( ref.host <nat> ) ;; host reference
458458
( ref.extern <nat> ) ;; external host reference
459459

interpreter/exec/eval.ml

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ let any_ref (inst : moduleinst) x i at =
126126
let func_ref (inst : moduleinst) x i at =
127127
match any_ref inst x i at with
128128
| FuncRef f -> f
129-
| NullRef _ -> Trap.error at ("uninitialized element " ^ Int64.to_string i)
129+
| NullRef -> Trap.error at ("uninitialized element " ^ Int64.to_string i)
130130
| _ -> Crash.error at ("type mismatch for element " ^ Int64.to_string i)
131131

132132
let blocktype (inst : moduleinst) bt at =
@@ -231,13 +231,13 @@ let rec step (c : config) : config =
231231
else
232232
vs', [Plain (Br (Lib.List32.nth xs i)) @@ e.at]
233233

234-
| BrOnNull x, Ref (NullRef _) :: vs' ->
234+
| BrOnNull x, Ref NullRef :: vs' ->
235235
vs', [Plain (Br x) @@ e.at]
236236

237237
| BrOnNull x, Ref r :: vs' ->
238238
Ref r :: vs', []
239239

240-
| BrOnNonNull x, Ref (NullRef _) :: vs' ->
240+
| BrOnNonNull x, Ref NullRef :: vs' ->
241241
vs', []
242242

243243
| BrOnNonNull x, Ref r :: vs' ->
@@ -263,7 +263,7 @@ let rec step (c : config) : config =
263263
| Call x, vs ->
264264
vs, [Invoke (func c.frame.inst x) @@ e.at]
265265

266-
| CallRef _x, Ref (NullRef _) :: vs ->
266+
| CallRef _x, Ref NullRef :: vs ->
267267
vs, [Trapping "null function reference" @@ e.at]
268268

269269
| CallRef _x, Ref (FuncRef f) :: vs ->
@@ -285,7 +285,7 @@ let rec step (c : config) : config =
285285
| _ -> assert false
286286
)
287287

288-
| ReturnCallRef _x, Ref (NullRef _) :: vs ->
288+
| ReturnCallRef _x, Ref NullRef :: vs ->
289289
vs, [Trapping "null function reference" @@ e.at]
290290

291291
| ReturnCallRef x, vs ->
@@ -313,7 +313,7 @@ let rec step (c : config) : config =
313313
let args, vs' = split n vs e.at in
314314
vs', [Throwing (t, args) @@ e.at]
315315

316-
| ThrowRef, Ref (NullRef _) :: vs ->
316+
| ThrowRef, Ref NullRef :: vs ->
317317
vs, [Trapping "null exception reference" @@ e.at]
318318

319319
| ThrowRef, Ref (Exn.(ExnRef (Exn (t, args)))) :: vs ->
@@ -627,19 +627,19 @@ let rec step (c : config) : config =
627627
vs, []
628628

629629
| RefNull t, vs' ->
630-
Ref (NullRef (subst_heaptype (subst_of c.frame.inst) t)) :: vs', []
630+
Ref NullRef :: vs', []
631631

632632
| RefFunc x, vs' ->
633633
let f = func c.frame.inst x in
634634
Ref (FuncRef f) :: vs', []
635635

636-
| RefIsNull, Ref (NullRef _) :: vs' ->
636+
| RefIsNull, Ref NullRef :: vs' ->
637637
value_of_bool true :: vs', []
638638

639639
| RefIsNull, Ref _ :: vs' ->
640640
value_of_bool false :: vs', []
641641

642-
| RefAsNonNull, Ref (NullRef _) :: vs' ->
642+
| RefAsNonNull, Ref NullRef :: vs' ->
643643
vs', [Trapping "null reference" @@ e.at]
644644

645645
| RefAsNonNull, Ref r :: vs' ->
@@ -664,7 +664,7 @@ let rec step (c : config) : config =
664664
| RefI31, Num (I32 i) :: vs' ->
665665
Ref (I31.I31Ref (I31.of_i32 i)) :: vs', []
666666

667-
| I31Get ext, Ref (NullRef _) :: vs' ->
667+
| I31Get ext, Ref NullRef :: vs' ->
668668
vs', [Trapping "null i31 reference" @@ e.at]
669669

670670
| I31Get ext, Ref (I31.I31Ref i) :: vs' ->
@@ -687,7 +687,7 @@ let rec step (c : config) : config =
687687
with Failure _ -> Crash.error e.at "type mismatch packing value"
688688
in Ref (Aggr.StructRef struct_) :: vs'', []
689689

690-
| StructGet (x, i, exto), Ref (NullRef _) :: vs' ->
690+
| StructGet (x, i, exto), Ref NullRef :: vs' ->
691691
vs', [Trapping "null structure reference" @@ e.at]
692692

693693
| StructGet (x, i, exto), Ref Aggr.(StructRef (Struct (_, fs))) :: vs' ->
@@ -698,7 +698,7 @@ let rec step (c : config) : config =
698698
(try Aggr.read_field f exto :: vs', []
699699
with Failure _ -> Crash.error e.at "type mismatch reading field")
700700

701-
| StructSet (x, i), v :: Ref (NullRef _) :: vs' ->
701+
| StructSet (x, i), v :: Ref NullRef :: vs' ->
702702
vs', [Trapping "null structure reference" @@ e.at]
703703

704704
| StructSet (x, i), v :: Ref Aggr.(StructRef (Struct (_, fs))) :: vs' ->
@@ -765,7 +765,7 @@ let rec step (c : config) : config =
765765
with Failure _ -> Crash.error e.at "type mismatch packing value"
766766
in Ref (Aggr.ArrayRef array) :: vs', []
767767

768-
| ArrayGet (x, exto), Num (I32 i) :: Ref (NullRef _) :: vs' ->
768+
| ArrayGet (x, exto), Num (I32 i) :: Ref NullRef :: vs' ->
769769
vs', [Trapping "null array reference" @@ e.at]
770770

771771
| ArrayGet (x, exto), Num (I32 i) :: Ref (Aggr.ArrayRef a) :: vs'
@@ -776,7 +776,7 @@ let rec step (c : config) : config =
776776
(try Aggr.read_field (Lib.List32.nth fs i) exto :: vs', []
777777
with Failure _ -> Crash.error e.at "type mismatch reading array")
778778

779-
| ArraySet x, v :: Num (I32 i) :: Ref (NullRef _) :: vs' ->
779+
| ArraySet x, v :: Num (I32 i) :: Ref NullRef :: vs' ->
780780
vs', [Trapping "null array reference" @@ e.at]
781781

782782
| ArraySet x, v :: Num (I32 i) :: Ref (Aggr.ArrayRef a) :: vs'
@@ -787,18 +787,18 @@ let rec step (c : config) : config =
787787
(try Aggr.write_field (Lib.List32.nth fs i) v; vs', []
788788
with Failure _ -> Crash.error e.at "type mismatch writing array")
789789

790-
| ArrayLen, Ref (NullRef _) :: vs' ->
790+
| ArrayLen, Ref NullRef :: vs' ->
791791
vs', [Trapping "null array reference" @@ e.at]
792792

793793
| ArrayLen, Ref Aggr.(ArrayRef (Array (_, fs))) :: vs' ->
794794
Num (I32 (Lib.List32.length fs)) :: vs', []
795795

796796
| ArrayCopy (x, y),
797-
Num _ :: Num _ :: Ref (NullRef _) :: Num _ :: Ref _ :: vs' ->
797+
Num _ :: Num _ :: Ref NullRef :: Num _ :: Ref _ :: vs' ->
798798
vs', [Trapping "null array reference" @@ e.at]
799799

800800
| ArrayCopy (x, y),
801-
Num _ :: Num _ :: Ref _ :: Num _ :: Ref (NullRef _) :: vs' ->
801+
Num _ :: Num _ :: Ref _ :: Num _ :: Ref NullRef :: vs' ->
802802
vs', [Trapping "null array reference" @@ e.at]
803803

804804
| ArrayCopy (x, y),
@@ -846,7 +846,7 @@ let rec step (c : config) : config =
846846
Plain (ArraySet x);
847847
]
848848

849-
| ArrayFill x, Num (I32 n) :: v :: Num (I32 i) :: Ref (NullRef _) :: vs' ->
849+
| ArrayFill x, Num (I32 n) :: v :: Num (I32 i) :: Ref NullRef :: vs' ->
850850
vs', [Trapping "null array reference" @@ e.at]
851851

852852
| ArrayFill x, Num (I32 n) :: v :: Num (I32 i) :: Ref (Aggr.ArrayRef a) :: vs' ->
@@ -868,7 +868,7 @@ let rec step (c : config) : config =
868868
]
869869

870870
| ArrayInitData (x, y),
871-
Num _ :: Num _ :: Num _ :: Ref (NullRef _) :: vs' ->
871+
Num _ :: Num _ :: Num _ :: Ref NullRef :: vs' ->
872872
vs', [Trapping "null array reference" @@ e.at]
873873

874874
| ArrayInitData (x, y),
@@ -899,7 +899,7 @@ let rec step (c : config) : config =
899899
]
900900

901901
| ArrayInitElem (x, y),
902-
Num _ :: Num _ :: Num _ :: Ref (NullRef _) :: vs' ->
902+
Num _ :: Num _ :: Num _ :: Ref NullRef :: vs' ->
903903
vs', [Trapping "null array reference" @@ e.at]
904904

905905
| ArrayInitElem (x, y),
@@ -926,14 +926,14 @@ let rec step (c : config) : config =
926926
Plain (ArrayInitElem (x, y));
927927
]
928928

929-
| ExternConvert Internalize, Ref (NullRef _) :: vs' ->
930-
Ref (NullRef NoneHT) :: vs', []
929+
| ExternConvert Internalize, Ref NullRef :: vs' ->
930+
Ref NullRef :: vs', []
931931

932932
| ExternConvert Internalize, Ref (Extern.ExternRef r) :: vs' ->
933933
Ref r :: vs', []
934934

935-
| ExternConvert Externalize, Ref (NullRef _) :: vs' ->
936-
Ref (NullRef NoExternHT) :: vs', []
935+
| ExternConvert Externalize, Ref NullRef :: vs' ->
936+
Ref NullRef :: vs', []
937937

938938
| ExternConvert Externalize, Ref r :: vs' ->
939939
Ref (Extern.ExternRef r) :: vs', []

interpreter/host/spectest.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@ let global (GlobalT (_, t) as gt) =
1515
| NumT F32T -> Num (F32 (F32.of_float 666.6))
1616
| NumT F64T -> Num (F64 (F64.of_float 666.6))
1717
| VecT V128T -> Vec (V128 (V128.I32x4.of_lanes [666l; 666l; 666l; 666l]))
18-
| RefT (_, t) -> Ref (NullRef t)
18+
| RefT _ -> Ref NullRef
1919
| BotT -> assert false
2020
in Some (ExternGlobal (Global.alloc gt v))
2121

2222
let table =
2323
let tt = TableT (I32AT, {min = 10L; max = Some 20L}, (Null, FuncHT)) in
24-
Some (ExternTable (Table.alloc tt (NullRef FuncHT)))
24+
Some (ExternTable (Table.alloc tt NullRef))
2525

2626
let table64 =
2727
let tt = TableT (I64AT, {min = 10L; max = Some 20L}, (Null, FuncHT)) in
28-
Some (ExternTable (Table.alloc tt (NullRef FuncHT)))
28+
Some (ExternTable (Table.alloc tt NullRef))
2929

3030
let memory =
3131
let mt = MemoryT (I32AT, {min = 1L; max = Some 2L}) in

interpreter/runtime/value.ml

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type ref_ = ..
1717
type value = Num of num | Vec of vec | Ref of ref_
1818
type t = value
1919

20-
type ref_ += NullRef of heaptype
20+
type ref_ += NullRef
2121

2222
type address = I64.t
2323

@@ -89,7 +89,7 @@ struct
8989
end
9090

9191
let is_null_ref = function
92-
| NullRef _ -> true
92+
| NullRef -> true
9393
| _ -> false
9494

9595

@@ -109,7 +109,7 @@ let type_of_vec = type_of_vecop
109109

110110
let type_of_ref' = ref (function _ -> assert false)
111111
let type_of_ref = function
112-
| NullRef t -> (Null, Match.bot_of_heaptype [] t)
112+
| NullRef -> (Null, BotHT)
113113
| r -> (NoNull, !type_of_ref' r)
114114

115115
let type_of_value = function
@@ -124,11 +124,7 @@ let eq_num n1 n2 = n1 = n2
124124

125125
let eq_vec v1 v2 = v1 = v2
126126

127-
let eq_ref' = ref (fun r1 r2 ->
128-
match r1, r2 with
129-
| NullRef _, NullRef _ -> true
130-
| _, _ -> r1 == r2
131-
)
127+
let eq_ref' = ref (==)
132128

133129
let eq_ref r1 r2 = !eq_ref' r1 r2
134130

@@ -152,7 +148,7 @@ let default_vec = function
152148
| V128T -> Some (Vec (V128 V128.zero))
153149

154150
let default_ref = function
155-
| (Null, t) -> Some (Ref (NullRef t))
151+
| (Null, _) -> Some (Ref NullRef)
156152
| (NoNull, _) -> None
157153

158154
let default_value = function
@@ -323,7 +319,7 @@ let hex_string_of_vec = function
323319

324320
let string_of_ref' = ref (function _ -> "ref")
325321
let string_of_ref = function
326-
| NullRef _ -> "null"
322+
| NullRef -> "null"
327323
| r -> !string_of_ref' r
328324

329325
let string_of_value = function

0 commit comments

Comments
 (0)