summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/wasm/gc/ref-restrict.js
blob: 8a2e8f37ca80f95b2868274ce057a06a6724aacb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// |jit-test| skip-if: !wasmGcEnabled()

// For the time being, we do not want to expose struct types outside of the
// module where they are defined, and so there are restrictions on how functions
// and global variables that traffic in struct types can be used.
//
// At the same time, intra-module uses of functions and globals that use struct
// types must be allowed to the greatest extent possible.
//
// Terminology: A function that takes a Ref parameter or returns a Ref result is
// "exposed for Ref", as is a global of Ref type.  EqRef is OK though, in all
// cases.
//
// To keep it simple we have the following restrictions that can all be checked
// statically.
//
//  - Exported and imported functions cannot be exposed for Ref.
//
//  - If the module has an exported or imported table then no function stored in
//    that table by the module (by means of an element segment) can be exposed
//    for Ref.
//
//  - If the module has an exported or imported table then no call_indirect via
//    that table may reference a type that is exposed for Ref.
//
//  - An exported or imported global cannot be exposed for Ref.
//
// Conversely,
//
//  - If a module has a private table then it can contain private functions that
//    are exposed for Ref and it is possible to call those functions via that
//    table.
//
//  - If a module has a private global then it can be exposed for ref.
//
// Note that
//
//  - code generators can work around the restrictions by instead using
//    functions and globals that use eqref, and by using downcasts to check
//    that the types are indeed correct.  (Though the meaning of downcast will
//    change as the GC feature evolves.)
//
//  - we could probably make the restrictions slightly softer but there's really
//    no advantage to doing so.

function wasmCompile(text) {
    return new WebAssembly.Module(wasmTextToBinary(text));
}

// Exported function can't take ref type parameter, but eqref is OK.

assertErrorMessage(() => wasmCompile(
    `(module
      (type $box (struct (field $x i32)))
      (func (export "f") (param (ref null $box)) (unreachable)))`),
                   WebAssembly.CompileError,
                   /cannot expose indexed reference type/);

assertEq(typeof wasmCompile(
    `(module
      (func (export "f") (param eqref) (unreachable)))`),
         "object");

// Exported function can't return ref result, but eqref is OK.

assertErrorMessage(() => wasmCompile(
    `(module
      (type $box (struct (field $x i32)))
      (func (export "f") (result (ref null $box)) (ref.null $box)))`),
                   WebAssembly.CompileError,
                   /cannot expose indexed reference type/);

assertEq(typeof wasmCompile(
    `(module
      (func (export "f") (result eqref) (ref.null eq)))`),
         "object");

// Imported function can't take ref parameter, but eqref is OK.

assertErrorMessage(() => wasmCompile(
    `(module
      (type $box (struct (field $x i32)))
      (import "m" "f" (func (param (ref null $box)))))`),
                   WebAssembly.CompileError,
                   /cannot expose indexed reference type/);

assertEq(typeof wasmCompile(
    `(module
      (import "m" "f" (func (param eqref))))`),
         "object");

// Imported function can't return ref type, but eqref is OK.

assertErrorMessage(() => wasmCompile(
    `(module
      (type $box (struct (field $x i32)))
      (import "m" "f" (func (param i32) (result (ref null $box)))))`),
                   WebAssembly.CompileError,
                   /cannot expose indexed reference type/);

assertEq(typeof wasmCompile(
    `(module
      (import "m" "f" (func (param i32) (result eqref))))`),
         "object");

// Imported global can't be of Ref type (irrespective of mutability), though eqref is OK.

assertErrorMessage(() => wasmCompile(
    `(module
      (type $box (struct (field $val i32)))
      (import "m" "g" (global (mut (ref null $box)))))`),
                   WebAssembly.CompileError,
                   /cannot expose indexed reference type/);

assertErrorMessage(() => wasmCompile(
    `(module
      (type $box (struct (field $val i32)))
      (import "m" "g" (global (ref null $box))))`),
                   WebAssembly.CompileError,
                   /cannot expose indexed reference type/);

assertEq(typeof wasmCompile(
    `(module
      (import "m" "g" (global (mut eqref))))`),
         "object");

assertEq(typeof wasmCompile(
    `(module
      (import "m" "g" (global eqref)))`),
         "object");

// Exported global can't be of Ref type (irrespective of mutability), though eqref is OK.

assertErrorMessage(() => wasmCompile(
    `(module
      (type $box (struct (field $val i32)))
      (global $boxg (export "box") (mut (ref null $box)) (ref.null $box)))`),
                   WebAssembly.CompileError,
                   /cannot expose indexed reference type/);

assertErrorMessage(() => wasmCompile(
    `(module
      (type $box (struct (field $val i32)))
      (global $boxg (export "box") (ref null $box) (ref.null $box)))`),
                   WebAssembly.CompileError,
                   /cannot expose indexed reference type/);

assertEq(typeof wasmCompile(
    `(module
      (global $boxg (export "box") (mut eqref) (ref.null eq)))`),
         "object");

assertEq(typeof wasmCompile(
    `(module
      (global $boxg (export "box") eqref (ref.null eq)))`),
         "object");

// Exported table cannot reference functions that are exposed for Ref, but eqref is OK.

assertErrorMessage(() => wasmCompile(
    `(module
      (type $box (struct (field $val i32)))
      (table (export "tbl") 1 funcref)
      (elem (i32.const 0) $f1)
      (func $f1 (param (ref null $box)) (unreachable)))`),
                   WebAssembly.CompileError,
                   /cannot expose indexed reference type/);

assertErrorMessage(() => wasmCompile(
    `(module
      (type $box (struct (field $val i32)))
      (table (export "tbl") 1 funcref)
      (elem (i32.const 0) $f1)
      (func $f1 (result (ref null $box)) (ref.null $box)))`),
                   WebAssembly.CompileError,
                   /cannot expose indexed reference type/);

assertEq(typeof wasmCompile(
    `(module
      (table (export "tbl") 1 funcref)
      (elem (i32.const 0) $f1)
      (func $f1 (param eqref) (unreachable)))`),
         "object");

assertEq(typeof wasmCompile(
    `(module
      (table (export "tbl") 1 funcref)
      (elem (i32.const 0) $f1)
      (func $f1 (result eqref) (ref.null eq)))`),
         "object");

// Imported table cannot reference functions that are exposed for Ref, though eqref is OK.

assertErrorMessage(() => wasmCompile(
    `(module
      (type $box (struct (field $val i32)))
      (import "m" "tbl" (table 1 funcref))
      (elem (i32.const 0) $f1)
      (func $f1 (param (ref null $box)) (unreachable)))`),
                   WebAssembly.CompileError,
                   /cannot expose indexed reference type/);

assertErrorMessage(() => wasmCompile(
    `(module
      (type $box (struct (field $val i32)))
      (import "m" "tbl" (table 1 funcref))
      (elem (i32.const 0) $f1)
      (func $f1 (result (ref null $box)) (ref.null $box)))`),
                   WebAssembly.CompileError,
                   /cannot expose indexed reference type/);

assertEq(typeof wasmCompile(
    `(module
      (import "m" "tbl" (table 1 funcref))
      (elem (i32.const 0) $f1)
      (func $f1 (param eqref) (unreachable)))`),
         "object");

assertEq(typeof wasmCompile(
    `(module
      (import "m" "tbl" (table 1 funcref))
      (elem (i32.const 0) $f1)
      (func $f1 (result eqref) (ref.null eq)))`),
         "object");

// Can't call via exported table with type that is exposed for Ref, though eqref is OK.

assertErrorMessage(() => wasmCompile(
    `(module
      (type $box (struct (field $val i32)))
      (type $fn (func (param (ref null $box))))
      (table (export "tbl") 1 funcref)
      (func (param i32)
       (call_indirect (type $fn) (ref.null $box) (local.get 0))))`),
                   WebAssembly.CompileError,
                   /cannot expose indexed reference type/);

assertErrorMessage(() => wasmCompile(
    `(module
      (type $box (struct (field $val i32)))
      (type $fn (func (result (ref null $box))))
      (table (export "tbl") 1 funcref)
      (func (param i32) (result (ref null $box))
       (call_indirect (type $fn) (local.get 0))))`),
                   WebAssembly.CompileError,
                   /cannot expose indexed reference type/);

assertEq(typeof wasmCompile(
    `(module
      (type $fn (func (param eqref)))
      (table (export "tbl") 1 funcref)
      (func (param i32)
       (call_indirect (type $fn) (ref.null eq) (local.get 0))))`),
         "object");

assertEq(typeof wasmCompile(
    `(module
      (type $fn (func (result eqref)))
      (table (export "tbl") 1 funcref)
      (func (param i32) (result eqref)
       (call_indirect (type $fn) (local.get 0))))`),
         "object");

// Can't call via imported table with type that is exposed for Ref, though eqref is OK.

assertErrorMessage(() => wasmCompile(
    `(module
      (type $box (struct (field $val i32)))
      (type $fn (func (param (ref null $box))))
      (import "m" "tbl" (table 1 funcref))
      (func (param i32)
       (call_indirect (type $fn) (ref.null $box) (local.get 0))))`),
                   WebAssembly.CompileError,
                   /cannot expose indexed reference type/);

assertErrorMessage(() => wasmCompile(
    `(module
      (type $box (struct (field $val i32)))
      (type $fn (func (result (ref null $box))))
      (import "m" "tbl" (table 1 funcref))
      (func (param i32) (result (ref null $box))
       (call_indirect (type $fn) (local.get 0))))`),
                   WebAssembly.CompileError,
                   /cannot expose indexed reference type/);

assertEq(typeof wasmCompile(
    `(module
      (type $fn (func (param eqref)))
      (import "m" "tbl" (table 1 funcref))
      (func (param i32)
       (call_indirect (type $fn) (ref.null eq) (local.get 0))))`),
         "object");

assertEq(typeof wasmCompile(
    `(module
      (type $fn (func (result eqref)))
      (import "m" "tbl" (table 1 funcref))
      (func (param i32) (result eqref)
       (call_indirect (type $fn) (local.get 0))))`),
         "object");

// We can call via a private table with a type that is exposed for Ref.

{
    let m = wasmCompile(
        `(module
          (type $box (struct (field $val i32)))
          (type $fn (func (param (ref null $box)) (result i32)))
          (table 1 funcref)
          (elem (i32.const 0) $f1)
          (func $f1 (param (ref null $box)) (result i32) (i32.const 37))
          (func (export "f") (param i32) (result i32)
           (call_indirect (type $fn) (ref.null $box) (local.get 0))))`);
    let i = new WebAssembly.Instance(m).exports;
    assertEq(i.f(0), 37);
}