Debian Codegraph @

js/src/jit-test/tests/wasm/memory64/basic.js

raw
1 // |jit-test| heavy; allow-oom; test-also=--setpref=wasm_tail_calls=false
2
3 // Basic tests around creating and linking memories with i64 indices
4
5 const MaxMemory64PagesValidation = 0x1_0000_0000_0000n; // from spec
6 const MaxTable64ElemsValidation = 0xFFFF_FFFF_FFFF_FFFFn; // from spec
7 const MaxTableElemsRuntime = 10000000; // from WasmConstants.h
8 const MaxUint32 = 0xFFFF_FFFF;
9
10 // test the validity of different i64 memory types in validation, compilation,
11 // and the JS-API.
12 function memoryTypeModuleText(shared, initial, max) {
13 return `(module
14 (memory i64 ${initial} ${max ?? ''} ${shared ? `shared` : ''}))`;
15 }
16 function memoryTypeDescriptor(shared, initial, max) {
17 return {
18 address: 'i64',
19 initial,
20 maximum: max,
21 shared,
22 };
23 }
24 function validAndInstantiableMemoryType(shared, initial, max) {
25 wasmValidateText(memoryTypeModuleText(shared, initial, max));
26 wasmEvalText(memoryTypeModuleText(shared, initial, max));
27 new WebAssembly.Memory(memoryTypeDescriptor(shared, initial, max));
28 }
29 function validButNotInstantiableMemoryType(shared, initial, max, errorMessage) {
30 wasmValidateText(memoryTypeModuleText(shared, initial, max));
31 assertErrorMessage(() => wasmEvalText(memoryTypeModuleText(shared, initial, max)), WebAssembly.RuntimeError, errorMessage);
32 assertErrorMessage(() => new WebAssembly.Memory(memoryTypeDescriptor(shared, initial, max)), WebAssembly.RuntimeError, errorMessage);
33 }
34 function invalidMemoryType(shared, initial, max, compileMessage, jsMessage) {
35 wasmFailValidateText(memoryTypeModuleText(shared, initial, max), compileMessage);
36 assertErrorMessage(() => wasmEvalText(memoryTypeModuleText(shared, initial, max)), WebAssembly.CompileError, compileMessage);
37 assertErrorMessage(() => new WebAssembly.Memory(memoryTypeDescriptor(shared, initial, max)), Error, jsMessage);
38 }
39
40 function tableTypeModuleText(element, initial, max) {
41 return `(module
42 (table i64 ${initial} ${max ?? ''} ${element})
43 )`;
44 }
45 function tableTypeDescriptor(element, initial, max) {
46 return {
47 address: 'i64',
48 element,
49 initial,
50 maximum: max,
51 };
52 }
53 function validAndInstantiableTableType(element, initial, max) {
54 wasmValidateText(tableTypeModuleText(element, initial, max));
55 wasmEvalText(tableTypeModuleText(element, initial, max));
56 new WebAssembly.Table(tableTypeDescriptor(element, initial, max));
57 }
58 function validButNotInstantiableTableType(element, initial, max, errorMessage) {
59 wasmValidateText(tableTypeModuleText(element, initial, max));
60 assertErrorMessage(() => wasmEvalText(tableTypeModuleText(element, initial, max)), WebAssembly.RuntimeError, errorMessage);
61 assertErrorMessage(() => new WebAssembly.Table(tableTypeDescriptor(element, initial, max)), WebAssembly.RuntimeError, errorMessage);
62 }
63 function invalidTableType(element, initial, max, compileMessage, jsMessage) {
64 wasmFailValidateText(tableTypeModuleText(element, initial, max), compileMessage);
65 assertErrorMessage(() => wasmEvalText(tableTypeModuleText(element, initial, max)), WebAssembly.CompileError, compileMessage);
66 assertErrorMessage(() => new WebAssembly.Table(tableTypeDescriptor(element, initial, max)), Error, jsMessage);
67 }
68
69 // valid to define a memory with i64
70 validAndInstantiableMemoryType(false, 0n);
71 // valid to define max with i64
72 validAndInstantiableMemoryType(false, 0n, 1n);
73 // invalid for min to be greater than max with i64
74 invalidMemoryType(false, 2n, 1n, /minimum must not be greater than maximum/, /initial Memory size cannot be greater than maximum/);
75 // valid to define shared memory with max with i64
76 validAndInstantiableMemoryType(true, 1n, 2n);
77 // invalid to define shared memory without max with i64
78 invalidMemoryType(true, 1n, undefined, /maximum length required for shared memory/, /maximum is not specified/);
79
80 // valid to define a table with i64
81 validAndInstantiableTableType('funcref', 0n);
82 // valid to define table max with i64
83 validAndInstantiableTableType('funcref', 0n, 1n);
84 // invalid for table min to be greater than max with i64
85 invalidTableType('funcref', 2n, 1n, /minimum must not be greater than maximum/, /initial Table size cannot be greater than maximum/);
86
87 // test the validation limits of memory64 memories
88 validButNotInstantiableMemoryType(false, MaxMemory64PagesValidation, undefined, /too many memory pages/);
89 validButNotInstantiableMemoryType(false, MaxMemory64PagesValidation, MaxMemory64PagesValidation, /too many memory pages/);
90 validAndInstantiableMemoryType(false, 0n, MaxMemory64PagesValidation);
91 invalidMemoryType(false, 0n, MaxMemory64PagesValidation + 1n, /maximum memory size too big/, /bad Memory maximum/);
92 validAndInstantiableMemoryType(true, 0n, MaxMemory64PagesValidation);
93 invalidMemoryType(true, 0n, MaxMemory64PagesValidation + 1n, /maximum memory size too big/, /bad Memory maximum/);
94
95 // test the validation limits of memory64 tables
96 validButNotInstantiableTableType('funcref', MaxTable64ElemsValidation, undefined, /too many table elements/);
97 validButNotInstantiableTableType('funcref', MaxTable64ElemsValidation, MaxTable64ElemsValidation, /too many table elements/);
98 validAndInstantiableTableType('funcref', 0n, MaxTable64ElemsValidation);
99 // cannot create oversize table via either text or binary format since the full u64 range is valid
100 assertErrorMessage(() => new WebAssembly.Table(tableTypeDescriptor('funcref', 0n, MaxTable64ElemsValidation + 1n)), TypeError, /bad Table maximum/);
101
102 // further validation of memory64 descriptor params
103 assertErrorMessage(() => new WebAssembly.Memory(memoryTypeDescriptor(false, 0)), TypeError, /can't convert 0 to BigInt/);
104 assertErrorMessage(() => new WebAssembly.Memory(memoryTypeDescriptor(false, -1n)), TypeError, /bad Memory initial size/);
105 assertErrorMessage(() => new WebAssembly.Memory(memoryTypeDescriptor(false, 2n**64n)), TypeError, /bad Memory initial size/);
106 assertErrorMessage(() => new WebAssembly.Memory(memoryTypeDescriptor(false, 0n, 1)), TypeError, /can't convert 1 to BigInt/);
107 assertErrorMessage(() => new WebAssembly.Memory(memoryTypeDescriptor(false, 0n, -1n)), TypeError, /bad Memory maximum size/);
108 assertErrorMessage(() => new WebAssembly.Memory(memoryTypeDescriptor(false, 0n, 2n**64n)), TypeError, /bad Memory maximum size/);
109 assertErrorMessage(() => new WebAssembly.Table(tableTypeDescriptor('funcref', 0)), TypeError, /can't convert 0 to BigInt/);
110 assertErrorMessage(() => new WebAssembly.Table(tableTypeDescriptor('funcref', -1n)), TypeError, /bad Table initial size/);
111 assertErrorMessage(() => new WebAssembly.Table(tableTypeDescriptor('funcref', 2n**64n)), TypeError, /bad Table initial size/);
112 assertErrorMessage(() => new WebAssembly.Table(tableTypeDescriptor('funcref', 0n, 1)), TypeError, /can't convert 1 to BigInt/);
113 assertErrorMessage(() => new WebAssembly.Table(tableTypeDescriptor('funcref', 0n, -1n)), TypeError, /bad Table maximum size/);
114 assertErrorMessage(() => new WebAssembly.Table(tableTypeDescriptor('funcref', 0n, 2n**64n)), TypeError, /bad Table maximum size/);
115
116 // test that linking requires address types to be equal
117 function testLinkMemory(importedAddressType, importAddressType) {
118 let imported = new WebAssembly.Memory({
119 address: importedAddressType,
120 initial: importedAddressType === 'i64' ? 0n : 0,
121 });
122 let testModule =
123 `(module
124 (memory (import "" "imported") ${importAddressType} 0))`;
125 if (importedAddressType === importAddressType) {
126 wasmEvalText(testModule, {"": {imported}});
127 } else {
128 assertErrorMessage(() => wasmEvalText(testModule, {"": {imported}}), WebAssembly.LinkError, /address type/);
129 }
130 }
131 function testLinkTable(importedAddressType, importAddressType) {
132 const imported = new WebAssembly.Table({
133 element: 'funcref',
134 address: importedAddressType,
135 initial: importedAddressType === 'i64' ? 0n : 0,
136 });
137 const testModule =
138 `(module
139 (table (import "" "imported") ${importAddressType} 0 funcref))`;
140 if (importedAddressType === importAddressType) {
141 wasmEvalText(testModule, {"": {imported}});
142 } else {
143 assertErrorMessage(() => wasmEvalText(testModule, {"": {imported}}), WebAssembly.LinkError, /address type/);
144 }
145 }
146
147 var types = [
148 ['i64', 'i64'],
149 ['i32', 'i32'],
150 ['i64', 'i32'],
151 ['i32', 'i64']
152 ];
153
154 for ( let [a,b] of types ) {
155 testLinkMemory(a, b);
156 testLinkTable(a, b);
157 }
158
159 // Active data segments use the address type for the init expression
160
161 for ( let [memType, exprType] of types ) {
162 const moduleText = `
163 (module
164 (memory ${memType} 1)
165 (data (${exprType}.const 0) "abcde"))`;
166 if (memType == exprType) {
167 wasmEvalText(moduleText);
168 } else {
169 wasmFailValidateText(moduleText, new RegExp(`expression has type ${exprType} but expected ${memType}`));
170 }
171 }
172
173 // Active element segments use the address type for the init expression
174
175 for ( let [tableType, exprType] of types ) {
176 const moduleText = `
177 (module
178 (table ${tableType} 1 funcref)
179 (elem (table 0) (offset ${exprType}.const 0) funcref (ref.null func)))`;
180 if (tableType == exprType) {
181 wasmEvalText(moduleText);
182 } else {
183 wasmFailValidateText(moduleText, new RegExp(`expression has type ${exprType} but expected ${tableType}`));
184 }
185 }
186
187 // Validate instructions using 32/64-bit pointers in 32/64-bit memories.
188
189 var validOffsets = {i32: ['', 'offset=0x10000000'],
190 i64: ['', 'offset=0x10000000', 'offset=0x200000000']}
191
192 // Basic load/store
193 for (let [memType, ptrType] of types ) {
194 for (let offs of validOffsets[memType]) {
195 assertEq(WebAssembly.validate(wasmTextToBinary(`
196 (module
197 (memory ${memType} 1)
198 (func (param $p ${ptrType}) (param $i i32) (param $l i64) (param $f f32) (param $d f64)
199 (drop (i32.add (i32.const 1) (i32.load8_s ${offs} (local.get $p))))
200 (drop (i32.add (i32.const 1) (i32.load8_u ${offs} (local.get $p))))
201 (drop (i32.add (i32.const 1) (i32.load16_s ${offs} (local.get $p))))
202 (drop (i32.add (i32.const 1) (i32.load16_u ${offs} (local.get $p))))
203 (drop (i32.add (i32.const 1) (i32.load ${offs} (local.get $p))))
204 (i32.store8 ${offs} (local.get $p) (local.get $i))
205 (i32.store16 ${offs} (local.get $p) (local.get $i))
206 (i32.store ${offs} (local.get $p) (local.get $i))
207 (drop (i64.add (i64.const 1) (i64.load8_s ${offs} (local.get $p))))
208 (drop (i64.add (i64.const 1) (i64.load8_u ${offs} (local.get $p))))
209 (drop (i64.add (i64.const 1) (i64.load16_s ${offs} (local.get $p))))
210 (drop (i64.add (i64.const 1) (i64.load16_u ${offs} (local.get $p))))
211 (drop (i64.add (i64.const 1) (i64.load32_s ${offs} (local.get $p))))
212 (drop (i64.add (i64.const 1) (i64.load32_u ${offs} (local.get $p))))
213 (drop (i64.add (i64.const 1) (i64.load ${offs} (local.get $p))))
214 (i64.store8 ${offs} (local.get $p) (local.get $l))
215 (i64.store16 ${offs} (local.get $p) (local.get $l))
216 (i64.store32 ${offs} (local.get $p) (local.get $l))
217 (i64.store ${offs} (local.get $p) (local.get $l))
218 (drop (f32.add (f32.const 1) (f32.load ${offs} (local.get $p))))
219 (f32.store ${offs} (local.get $p) (local.get $f))
220 (drop (f64.add (f64.const 1) (f64.load ${offs} (local.get $p))))
221 (f64.store ${offs} (local.get $p) (local.get $d))
222 ))`)), memType == ptrType);
223 }
224 }
225
226 // Basic table get/set
227 for (const [tableType, ptrType] of types) {
228 function mod(ins) {
229 return `(module
230 (table ${tableType} 1 funcref)
231 (func (param $p ${ptrType})
232 ${ins}
233 )
234 )`;
235 }
236
237 if (tableType === ptrType) {
238 wasmValidateText(mod(`(drop (table.get (${ptrType}.const 0)))`));
239 wasmValidateText(mod(`(table.set (${ptrType}.const 0) (ref.null func))`));
240 } else {
241 wasmFailValidateText(
242 mod(`(drop (table.get (${ptrType}.const 0)))`),
243 new RegExp(`expression has type ${ptrType} but expected ${tableType}`),
244 );
245 wasmFailValidateText(
246 mod(`(table.set (${ptrType}.const 0) (ref.null func))`),
247 new RegExp(`expression has type ${ptrType} but expected ${tableType}`),
248 );
249 }
250 }
251
252 // Bulk memory operations
253 for (const [memType, ptrType] of types) {
254 function mod(ins) {
255 return `(module
256 (memory ${memType} 1)
257 (data $seg "0123456789abcdef")
258 (func (param $p ${ptrType})
259 ${ins}
260 )
261 )`;
262 }
263
264 if (memType === ptrType) {
265 wasmValidateText(mod(`(drop (${ptrType}.add (${ptrType}.const 1) (memory.size)))`));
266 wasmValidateText(mod(`(drop (${ptrType}.add (${ptrType}.const 1) (memory.grow (${ptrType}.const 1))))`));
267 wasmValidateText(mod(`(memory.copy (local.get $p) (${ptrType}.const 0) (${ptrType}.const 628))`));
268 wasmValidateText(mod(`(memory.fill (local.get $p) (i32.const 37) (${ptrType}.const 1024))`));
269 wasmValidateText(mod(`(memory.init $seg (local.get $p) (i32.const 3) (i32.const 5))`));
270 } else {
271 wasmFailValidateText(
272 mod(`(drop (${ptrType}.add (${ptrType}.const 1) (memory.size)))`),
273 new RegExp(`expression has type ${memType} but expected ${ptrType}`),
274 );
275 wasmFailValidateText(
276 mod(`(drop (memory.grow (${ptrType}.const 1)))`),
277 new RegExp(`expression has type ${ptrType} but expected ${memType}`),
278 );
279 wasmFailValidateText(
280 mod(`(drop (${ptrType}.add (${ptrType}.const 1) (memory.grow (${memType}.const 1))))`),
281 new RegExp(`expression has type ${memType} but expected ${ptrType}`),
282 );
283 wasmFailValidateText(
284 mod(`(memory.copy (local.get $p) (${ptrType}.const 0) (${ptrType}.const 628))`),
285 new RegExp(`expression has type ${ptrType} but expected ${memType}`),
286 );
287 wasmFailValidateText(
288 mod(`(memory.fill (local.get $p) (i32.const 37) (${ptrType}.const 1024))`),
289 new RegExp(`expression has type ${ptrType} but expected ${memType}`),
290 );
291 wasmFailValidateText(
292 mod(`(memory.init $seg (local.get $p) (i32.const 3) (i32.const 5))`),
293 new RegExp(`expression has type ${ptrType} but expected ${memType}`),
294 );
295 }
296 }
297
298 // Bulk table operations
299 for (const [tableType, ptrType] of types) {
300 function mod(ins) {
301 return `(module
302 (table ${tableType} 1 funcref)
303 (elem $seg funcref (ref.null func))
304 (func (param $p ${ptrType})
305 ${ins}
306 )
307 )`;
308 }
309
310 if (tableType === ptrType) {
311 wasmValidateText(mod(`(drop (${ptrType}.add (${ptrType}.const 1) (table.size)))`));
312 wasmValidateText(mod(`(drop (${ptrType}.add (${ptrType}.const 1) (table.grow (ref.null func) (${ptrType}.const 1))))`));
313 wasmValidateText(mod(`(table.copy (local.get $p) (${ptrType}.const 0) (${ptrType}.const 628))`));
314 wasmValidateText(mod(`(table.fill (local.get $p) (ref.null func) (${ptrType}.const 1024))`));
315 wasmValidateText(mod(`(table.init $seg (local.get $p) (i32.const 3) (i32.const 5))`));
316 } else {
317 wasmFailValidateText(
318 mod(`(drop (${ptrType}.add (${ptrType}.const 1) (table.size)))`),
319 new RegExp(`expression has type ${tableType} but expected ${ptrType}`),
320 );
321 wasmFailValidateText(
322 mod(`(drop (table.grow (ref.null func) (${ptrType}.const 1)))`),
323 new RegExp(`expression has type ${ptrType} but expected ${tableType}`),
324 );
325 wasmFailValidateText(
326 mod(`(drop (${ptrType}.add (${ptrType}.const 1) (table.grow (ref.null func) (${tableType}.const 1))))`),
327 new RegExp(`expression has type ${tableType} but expected ${ptrType}`),
328 );
329 wasmFailValidateText(
330 mod(`(table.copy (local.get $p) (${ptrType}.const 0) (${ptrType}.const 628))`),
331 new RegExp(`expression has type ${ptrType} but expected ${tableType}`),
332 );
333 wasmFailValidateText(
334 mod(`(table.fill (local.get $p) (ref.null func) (${ptrType}.const 1024))`),
335 new RegExp(`expression has type ${ptrType} but expected ${tableType}`),
336 );
337 wasmFailValidateText(
338 mod(`(table.init $seg (local.get $p) (i32.const 3) (i32.const 5))`),
339 new RegExp(`expression has type ${ptrType} but expected ${tableType}`),
340 );
341 }
342 }
343
344 // SIMD
345 if (wasmSimdEnabled()) {
346 for (let [memType, ptrType] of types ) {
347 for (let offs of validOffsets[memType]) {
348 assertEq(WebAssembly.validate(wasmTextToBinary(`
349 (module
350 (memory ${memType} 1)
351 (func (param $p ${ptrType}) (param $v v128) (param $w v128)
352 (drop (i8x16.add (local.get $w) (v128.load ${offs} (local.get $p))))
353 (v128.store ${offs} (local.get $p) (local.get $v))
354 (drop (i8x16.add (local.get $w) (v128.load8_splat ${offs} (local.get $p))))
355 (drop (i8x16.add (local.get $w) (v128.load16_splat ${offs} (local.get $p))))
356 (drop (i8x16.add (local.get $w) (v128.load32_splat ${offs} (local.get $p))))
357 (drop (i8x16.add (local.get $w) (v128.load64_splat ${offs} (local.get $p))))
358 (drop (i8x16.add (local.get $w) (v128.load32_zero ${offs} (local.get $p))))
359 (drop (i8x16.add (local.get $w) (v128.load64_zero ${offs} (local.get $p))))
360 (drop (i8x16.add (local.get $w) (v128.load8_lane ${offs} 1 (local.get $p) (local.get $v))))
361 (drop (i8x16.add (local.get $w) (v128.load16_lane ${offs} 1 (local.get $p) (local.get $v))))
362 (drop (i8x16.add (local.get $w) (v128.load32_lane ${offs} 1 (local.get $p) (local.get $v))))
363 (drop (i8x16.add (local.get $w) (v128.load64_lane ${offs} 1 (local.get $p) (local.get $v))))
364 (v128.store8_lane ${offs} 1 (local.get $p) (local.get $v))
365 (v128.store16_lane ${offs} 1 (local.get $p) (local.get $v))
366 (v128.store32_lane ${offs} 1 (local.get $p) (local.get $v))
367 (v128.store64_lane ${offs} 1 (local.get $p) (local.get $v))
368 (drop (i8x16.add (local.get $w) (v128.load8x8_s ${offs} (local.get $p))))
369 (drop (i8x16.add (local.get $w) (v128.load8x8_u ${offs} (local.get $p))))
370 (drop (i8x16.add (local.get $w) (v128.load16x4_s ${offs} (local.get $p))))
371 (drop (i8x16.add (local.get $w) (v128.load16x4_u ${offs} (local.get $p))))
372 (drop (i8x16.add (local.get $w) (v128.load32x2_s ${offs} (local.get $p))))
373 (drop (i8x16.add (local.get $w) (v128.load32x2_u ${offs} (local.get $p))))
374 ))`)), memType == ptrType);
375 }
376 }
377 }
378
379 // Threads
380 if (wasmThreadsEnabled()) {
381 for (let [memType, ptrType] of types ) {
382 for (let offs of validOffsets[memType]) {
383 assertEq(WebAssembly.validate(wasmTextToBinary(`
384 (module
385 (memory ${memType} 1 100 shared)
386 (func (param $p ${ptrType}) (param $i i32) (param $l i64)
387 (drop (i32.add (i32.const 1) (memory.atomic.wait32 ${offs} (local.get $p) (i32.const 0) (i64.const 37))))
388 (drop (i32.add (i32.const 1) (memory.atomic.wait64 ${offs} (local.get $p) (i64.const 0) (i64.const 37))))
389 (drop (i32.add (i32.const 1) (memory.atomic.notify ${offs} (local.get $p) (i32.const 1))))
390 ))`)), memType == ptrType);
391
392 for (let [ty,size,sx] of
393 [['i32','','','',],['i32','8','_u'],['i32','16','_u'],
394 ['i64','',''],['i64','8','_u'],['i64','16','_u'],['i64','32','_u']]) {
395 assertEq(WebAssembly.validate(wasmTextToBinary(`
396 (module
397 (memory ${memType} 1 100 shared)
398 (func (param $p ${ptrType}) (param $vi32 i32) (param $vi64 i64)
399 (drop (${ty}.add (${ty}.const 1) (${ty}.atomic.load${size}${sx} ${offs} (local.get $p))))
400 (${ty}.atomic.store${size} ${offs} (local.get $p) (local.get $v${ty}))
401 (drop (${ty}.add (${ty}.const 1) (${ty}.atomic.rmw${size}.add${sx} ${offs} (local.get $p) (local.get $v${ty}))))
402 (drop (${ty}.add (${ty}.const 1) (${ty}.atomic.rmw${size}.sub${sx} ${offs} (local.get $p) (local.get $v${ty}))))
403 (drop (${ty}.add (${ty}.const 1) (${ty}.atomic.rmw${size}.and${sx} ${offs} (local.get $p) (local.get $v${ty}))))
404 (drop (${ty}.add (${ty}.const 1) (${ty}.atomic.rmw${size}.or${sx} ${offs} (local.get $p) (local.get $v${ty}))))
405 (drop (${ty}.add (${ty}.const 1) (${ty}.atomic.rmw${size}.xor${sx} ${offs} (local.get $p) (local.get $v${ty}))))
406 (drop (${ty}.add (${ty}.const 1) (${ty}.atomic.rmw${size}.xchg${sx} ${offs} (local.get $p) (local.get $v${ty}))))
407 (drop (${ty}.add (${ty}.const 1) (${ty}.atomic.rmw${size}.cmpxchg${sx} ${offs} (local.get $p) (local.get $v${ty}) (${ty}.const 37))))
408 ))`)), memType == ptrType);
409 }
410
411 }
412 }
413 }
414
415 // Cursorily check that invalid offsets are rejected.
416
417 wasmFailValidateText(`
418 (module
419 (memory i32 1)
420 (func (param $p i32)
421 (drop (i32.load offset=0x100000000 (local.get $p)))))`, /offset too large for memory type/);
422
423
424 ///////////////////////////////////////////////////////////////////////////////
425 //
426 // EXECUTION
427
428 // Smoketest: Can we actually allocate a memory larger than 4GB?
429
430 if (getBuildConfiguration("pointer-byte-size") == 8) {
431 try {
432 new WebAssembly.Memory({address:"i64", initial:BigInt(65536 * 1.5), maximum:BigInt(65536 * 2)});
433 } catch (e) {
434 // OOM is OK.
435 if (!(e instanceof WebAssembly.RuntimeError) || !String(e).match(/too many memory pages/)) {
436 throw e;
437 }
438 }
439 }
440
441 // JS-API
442
443 if (WebAssembly.Function) {
444 const m64 = new WebAssembly.Memory({ address: "i64", initial:1n });
445 assertEq(m64.type().address, "i64");
446
447 const m32 = new WebAssembly.Memory({ initial:1 });
448 assertEq(m32.type().address, "i32");
449
450 const t64 = new WebAssembly.Table({ address: "i64", element: "funcref", initial: 1n });
451 assertEq(t64.type().address, "i64");
452
453 const t32 = new WebAssembly.Table({ initial: 1, element: "funcref" });
454 assertEq(t32.type().address, "i32");
455
456 const ins = wasmEvalText(`(module
457 (memory (export "mem") i64 1 0x100000000)
458 (table (export "table") i64 1 0x1000 funcref)
459 )`);
460 assertEq(ins.exports.mem.type().address, "i64");
461 assertEq(ins.exports.mem.type().minimum, 1n);
462 assertEq(ins.exports.mem.type().maximum, 0x100000000n);
463 assertEq(ins.exports.table.type().address, "i64");
464 assertEq(ins.exports.table.type().minimum, 1n);
465 assertEq(ins.exports.table.type().maximum, 0x1000n);
466 }
467
468 // Instructions
469
470 const SMALL = 64; // < offsetguard everywhere
471 const BIG = 131072; // > offsetguard on 32-bit
472 const HUGE = 2147483656; // > offsetguard on 64-bit
473 const VAST = 0x112001300; // > 4GB
474
475 function makeTest(LOC, INITIAL, MAXIMUM, SHARED) {
476 const v128Prefix =
477 ` (func $stash (param v128)
478 (v128.store (i64.const 0) (local.get 0)))
479
480 (func $unstash (result v128)
481 (v128.load (i64.const 0)))
482 `;
483
484 const readV128Code =
485 ` (func (export "readv128@0") (param $p i64)
486 (call $stash (v128.load (local.get $p))))
487
488 (func (export "readv128@small") (param $p i64)
489 (call $stash (v128.load offset=${SMALL} (local.get $p))))
490
491 (func (export "readv128@big") (param $p i64)
492 (call $stash (v128.load offset=${BIG} (local.get $p))))
493
494 (func (export "readv128@huge") (param $p i64)
495 (call $stash (v128.load offset=${HUGE} (local.get $p))))
496
497 (func (export "readv128/const@0")
498 (call $stash (v128.load (i64.const ${LOC}))))
499
500 (func (export "readv128/const@small")
501 (call $stash (v128.load offset=${SMALL} (i64.const ${LOC}))))
502
503 (func (export "readv128/const@big")
504 (call $stash (v128.load offset=${BIG} (i64.const ${LOC}))))
505
506 (func (export "v128.load_splat@small") (param $p i64)
507 (call $stash (v128.load32_splat offset=${SMALL} (local.get $p))))
508
509 (func (export "v128.load_zero@small") (param $p i64)
510 (call $stash (v128.load32_zero offset=${SMALL} (local.get $p))))
511
512 (func (export "v128.load_extend@small") (param $p i64)
513 (call $stash (v128.load32x2_u offset=${SMALL} (local.get $p))))
514
515 (func (export "v128.load_lane@small") (param $p i64)
516 (call $stash (v128.load32_lane offset=${SMALL} 2 (local.get $p) (call $unstash))))
517 `;
518
519 const writeV128Code =
520 ` (func (export "writev128@0") (param $p i64)
521 (v128.store (local.get $p) (call $unstash)))
522
523 (func (export "writev128@small") (param $p i64)
524 (v128.store offset=${SMALL} (local.get $p) (call $unstash)))
525
526 (func (export "writev128@big") (param $p i64)
527 (v128.store offset=${BIG} (local.get $p) (call $unstash)))
528
529 (func (export "writev128@huge") (param $p i64)
530 (v128.store offset=${HUGE} (local.get $p) (call $unstash)))
531
532 (func (export "writev128/const@0")
533 (v128.store (i64.const ${LOC}) (call $unstash)))
534
535 (func (export "writev128/const@small")
536 (v128.store offset=${SMALL} (i64.const ${LOC}) (call $unstash)))
537
538 (func (export "writev128/const@big")
539 (v128.store offset=${BIG} (i64.const ${LOC}) (call $unstash)))
540
541 (func (export "v128.store_lane@small") (param $p i64)
542 (v128.store32_lane offset=${SMALL} 2 (local.get $p) (call $unstash)))
543 `;
544
545 const ins = wasmEvalText(`
546 (module
547 (memory (export "mem") i64 ${INITIAL} ${MAXIMUM} ${SHARED})
548
549 ;; About the test cases: there are various optimizations in the engine
550 ;; for different shapes of a pointer+offset. Constant pointers are
551 ;; resolved early; large offsets are folded early using explicit code
552 ;; with an overflow check (but "large" depends on 32-bit vs 64-bit);
553 ;; wait/notify fold offsets early regardless; zero offsets lead to
554 ;; tighter code with variable pointers; and don't get me started on
555 ;; alignment checks. These test cases are not exhaustive but aim
556 ;; to test at least some things.
557
558 ;; TODO: more sizes for all operations, though this is not critical
559 ;; TODO: sign extending loads, again not critical
560
561 ${wasmSimdEnabled() ? v128Prefix : ""}
562
563 ;; Read i32
564 (func (export "readi32@0") (param $p i64) (result i32)
565 (i32.load (local.get $p)))
566
567 (func (export "readi32@small") (param $p i64) (result i32)
568 (i32.load offset=${SMALL} (local.get $p)))
569
570 (func (export "readi32@big") (param $p i64) (result i32)
571 (i32.load offset=${BIG} (local.get $p)))
572
573 (func (export "readi32@huge") (param $p i64) (result i32)
574 (i32.load offset=${HUGE} (local.get $p)))
575
576 (func (export "readi32@vast") (param $p i64) (result i32)
577 (i32.load offset=${VAST} (local.get $p)))
578
579 (func (export "readi32/const@0") (result i32)
580 (i32.load (i64.const ${LOC})))
581
582 (func (export "readi32/const@small") (result i32)
583 (i32.load offset=${SMALL} (i64.const ${LOC})))
584
585 (func (export "readi32/const@big") (result i32)
586 (i32.load offset=${BIG} (i64.const ${LOC})))
587
588 (func (export "readi32/const@vast") (result i32)
589 (i32.load offset=${VAST} (i64.const ${LOC})))
590
591 ;; Read i64
592 (func (export "readi64@0") (param $p i64) (result i64)
593 (i64.load (local.get $p)))
594
595 (func (export "readi64@small") (param $p i64) (result i64)
596 (i64.load offset=${SMALL} (local.get $p)))
597
598 (func (export "readi64@big") (param $p i64) (result i64)
599 (i64.load offset=${BIG} (local.get $p)))
600
601 (func (export "readi64@huge") (param $p i64) (result i64)
602 (i64.load offset=${HUGE} (local.get $p)))
603
604 (func (export "readi64@vast") (param $p i64) (result i64)
605 (i64.load offset=${VAST} (local.get $p)))
606
607 (func (export "readi64/const@0") (result i64)
608 (i64.load (i64.const ${LOC})))
609
610 (func (export "readi64/const@small") (result i64)
611 (i64.load offset=${SMALL} (i64.const ${LOC})))
612
613 (func (export "readi64/const@big") (result i64)
614 (i64.load offset=${BIG} (i64.const ${LOC})))
615
616 (func (export "readi64/const@vast") (result i64)
617 (i64.load offset=${VAST} (i64.const ${LOC})))
618
619 ;; Read v128
620 ${wasmSimdEnabled() ? readV128Code : ""}
621
622 ;; write i32
623 (func (export "writei32@0") (param $p i64) (param $v i32)
624 (i32.store (local.get $p) (local.get $v)))
625
626 (func (export "writei32@small") (param $p i64) (param $v i32)
627 (i32.store offset=${SMALL} (local.get $p) (local.get $v)))
628
629 (func (export "writei32@big") (param $p i64) (param $v i32)
630 (i32.store offset=${BIG} (local.get $p) (local.get $v)))
631
632 (func (export "writei32@huge") (param $p i64) (param $v i32)
633 (i32.store offset=${HUGE} (local.get $p) (local.get $v)))
634
635 (func (export "writei32@vast") (param $p i64) (param $v i32)
636 (i32.store offset=${VAST} (local.get $p) (local.get $v)))
637
638 (func (export "writei32/const@0") (param $v i32)
639 (i32.store (i64.const ${LOC}) (local.get $v)))
640
641 (func (export "writei32/const@small") (param $v i32)
642 (i32.store offset=${SMALL} (i64.const ${LOC}) (local.get $v)))
643
644 (func (export "writei32/const@big") (param $v i32)
645 (i32.store offset=${BIG} (i64.const ${LOC}) (local.get $v)))
646
647 (func (export "writei32/const@vast") (param $v i32)
648 (i32.store offset=${VAST} (i64.const ${LOC}) (local.get $v)))
649
650 ;; write i64
651 (func (export "writei64@0") (param $p i64) (param $v i64)
652 (i64.store (local.get $p) (local.get $v)))
653
654 (func (export "writei64@small") (param $p i64) (param $v i64)
655 (i64.store offset=${SMALL} (local.get $p) (local.get $v)))
656
657 (func (export "writei64@big") (param $p i64) (param $v i64)
658 (i64.store offset=${BIG} (local.get $p) (local.get $v)))
659
660 (func (export "writei64@huge") (param $p i64) (param $v i64)
661 (i64.store offset=${HUGE} (local.get $p) (local.get $v)))
662
663 (func (export "writei64@vast") (param $p i64) (param $v i64)
664 (i64.store offset=${VAST} (local.get $p) (local.get $v)))
665
666 (func (export "writei64/const@0") (param $v i64)
667 (i64.store (i64.const ${LOC}) (local.get $v)))
668
669 (func (export "writei64/const@small") (param $v i64)
670 (i64.store offset=${SMALL} (i64.const ${LOC}) (local.get $v)))
671
672 (func (export "writei64/const@big") (param $v i64)
673 (i64.store offset=${BIG} (i64.const ${LOC}) (local.get $v)))
674
675 (func (export "writei64/const@vast") (param $v i64)
676 (i64.store offset=${VAST} (i64.const ${LOC}) (local.get $v)))
677
678 ;; Read v128
679 ${wasmSimdEnabled() ? writeV128Code : ""}
680
681 ;; Atomic read i32
682
683 (func (export "areadi32@0") (param $p i64) (result i32)
684 (i32.atomic.load (local.get $p)))
685
686 (func (export "areadi32@small") (param $p i64) (result i32)
687 (i32.atomic.load offset=${SMALL} (local.get $p)))
688
689 (func (export "areadi32@big") (param $p i64) (result i32)
690 (i32.atomic.load offset=${BIG} (local.get $p)))
691
692 (func (export "areadi32@huge") (param $p i64) (result i32)
693 (i32.atomic.load offset=${HUGE} (local.get $p)))
694
695 (func (export "areadi32@vast") (param $p i64) (result i32)
696 (i32.atomic.load offset=${VAST} (local.get $p)))
697
698 (func (export "areadi32/const@0") (result i32)
699 (i32.atomic.load (i64.const ${LOC})))
700
701 (func (export "areadi32/const@small") (result i32)
702 (i32.atomic.load offset=${SMALL} (i64.const ${LOC})))
703
704 (func (export "areadi32/const@big") (result i32)
705 (i32.atomic.load offset=${BIG} (i64.const ${LOC})))
706
707 (func (export "areadi32/const@vast") (result i32)
708 (i32.atomic.load offset=${VAST} (i64.const ${LOC})))
709
710 ;; Atomic read i64
711
712 (func (export "areadi64@0") (param $p i64) (result i64)
713 (i64.atomic.load (local.get $p)))
714
715 (func (export "areadi64@small") (param $p i64) (result i64)
716 (i64.atomic.load offset=${SMALL} (local.get $p)))
717
718 (func (export "areadi64@big") (param $p i64) (result i64)
719 (i64.atomic.load offset=${BIG} (local.get $p)))
720
721 (func (export "areadi64@huge") (param $p i64) (result i64)
722 (i64.atomic.load offset=${HUGE} (local.get $p)))
723
724 (func (export "areadi64@vast") (param $p i64) (result i64)
725 (i64.atomic.load offset=${VAST} (local.get $p)))
726
727 (func (export "areadi64/const@0") (result i64)
728 (i64.atomic.load (i64.const ${LOC})))
729
730 (func (export "areadi64/const@small") (result i64)
731 (i64.atomic.load offset=${SMALL} (i64.const ${LOC})))
732
733 (func (export "areadi64/const@big") (result i64)
734 (i64.atomic.load offset=${BIG} (i64.const ${LOC})))
735
736 (func (export "areadi64/const@vast") (result i64)
737 (i64.atomic.load offset=${VAST} (i64.const ${LOC})))
738
739 ;; Atomic write i32
740
741 (func (export "awritei32@0") (param $p i64) (param $v i32)
742 (i32.atomic.store (local.get $p) (local.get $v)))
743
744 (func (export "awritei32@small") (param $p i64) (param $v i32)
745 (i32.atomic.store offset=${SMALL} (local.get $p) (local.get $v)))
746
747 (func (export "awritei32@big") (param $p i64) (param $v i32)
748 (i32.atomic.store offset=${BIG} (local.get $p) (local.get $v)))
749
750 (func (export "awritei32@huge") (param $p i64) (param $v i32)
751 (i32.atomic.store offset=${HUGE} (local.get $p) (local.get $v)))
752
753 (func (export "awritei32@vast") (param $p i64) (param $v i32)
754 (i32.atomic.store offset=${VAST} (local.get $p) (local.get $v)))
755
756 (func (export "awritei32/const@0") (param $v i32)
757 (i32.atomic.store (i64.const ${LOC}) (local.get $v)))
758
759 (func (export "awritei32/const@small") (param $v i32)
760 (i32.atomic.store offset=${SMALL} (i64.const ${LOC}) (local.get $v)))
761
762 (func (export "awritei32/const@big") (param $v i32)
763 (i32.atomic.store offset=${BIG} (i64.const ${LOC}) (local.get $v)))
764
765 (func (export "awritei32/const@vast") (param $v i32)
766 (i32.atomic.store offset=${VAST} (i64.const ${LOC}) (local.get $v)))
767
768 ;; Atomic write i64
769
770 (func (export "awritei64@0") (param $p i64) (param $v i64)
771 (i64.atomic.store (local.get $p) (local.get $v)))
772
773 (func (export "awritei64@small") (param $p i64) (param $v i64)
774 (i64.atomic.store offset=${SMALL} (local.get $p) (local.get $v)))
775
776 (func (export "awritei64@big") (param $p i64) (param $v i64)
777 (i64.atomic.store offset=${BIG} (local.get $p) (local.get $v)))
778
779 (func (export "awritei64@huge") (param $p i64) (param $v i64)
780 (i64.atomic.store offset=${HUGE} (local.get $p) (local.get $v)))
781
782 (func (export "awritei64@vast") (param $p i64) (param $v i64)
783 (i64.atomic.store offset=${VAST} (local.get $p) (local.get $v)))
784
785 (func (export "awritei64/const@0") (param $v i64)
786 (i64.atomic.store (i64.const ${LOC}) (local.get $v)))
787
788 (func (export "awritei64/const@small") (param $v i64)
789 (i64.atomic.store offset=${SMALL} (i64.const ${LOC}) (local.get $v)))
790
791 (func (export "awritei64/const@big") (param $v i64)
792 (i64.atomic.store offset=${BIG} (i64.const ${LOC}) (local.get $v)))
793
794 (func (export "awritei64/const@vast") (param $v i64)
795 (i64.atomic.store offset=${VAST} (i64.const ${LOC}) (local.get $v)))
796
797 ;; xchg i32
798
799 (func (export "xchgi32@0") (param $p i64) (param $v i32) (result i32)
800 (i32.atomic.rmw.xchg (local.get $p) (local.get $v)))
801
802 (func (export "xchgi32@small") (param $p i64) (param $v i32) (result i32)
803 (i32.atomic.rmw.xchg offset=${SMALL} (local.get $p) (local.get $v)))
804
805 (func (export "xchgi32@big") (param $p i64) (param $v i32) (result i32)
806 (i32.atomic.rmw.xchg offset=${BIG} (local.get $p) (local.get $v)))
807
808 (func (export "xchgi32@huge") (param $p i64) (param $v i32) (result i32)
809 (i32.atomic.rmw.xchg offset=${HUGE} (local.get $p) (local.get $v)))
810
811 (func (export "xchgi32/const@0") (param $v i32) (result i32)
812 (i32.atomic.rmw.xchg (i64.const ${LOC}) (local.get $v)))
813
814 (func (export "xchgi32/const@small") (param $v i32) (result i32)
815 (i32.atomic.rmw.xchg offset=${SMALL} (i64.const ${LOC}) (local.get $v)))
816
817 (func (export "xchgi32/const@big") (param $v i32) (result i32)
818 (i32.atomic.rmw.xchg offset=${BIG} (i64.const ${LOC}) (local.get $v)))
819
820 ;; xchg i64
821
822 (func (export "xchgi64@0") (param $p i64) (param $v i64) (result i64)
823 (i64.atomic.rmw.xchg (local.get $p) (local.get $v)))
824
825 (func (export "xchgi64@small") (param $p i64) (param $v i64) (result i64)
826 (i64.atomic.rmw.xchg offset=${SMALL} (local.get $p) (local.get $v)))
827
828 (func (export "xchgi64@big") (param $p i64) (param $v i64) (result i64)
829 (i64.atomic.rmw.xchg offset=${BIG} (local.get $p) (local.get $v)))
830
831 (func (export "xchgi64@huge") (param $p i64) (param $v i64) (result i64)
832 (i64.atomic.rmw.xchg offset=${HUGE} (local.get $p) (local.get $v)))
833
834 (func (export "xchgi64/const@0") (param $v i64) (result i64)
835 (i64.atomic.rmw.xchg (i64.const ${LOC}) (local.get $v)))
836
837 (func (export "xchgi64/const@small") (param $v i64) (result i64)
838 (i64.atomic.rmw.xchg offset=${SMALL} (i64.const ${LOC}) (local.get $v)))
839
840 (func (export "xchgi64/const@big") (param $v i64) (result i64)
841 (i64.atomic.rmw.xchg offset=${BIG} (i64.const ${LOC}) (local.get $v)))
842
843 ;; add i32
844
845 (func (export "addi32@0") (param $p i64) (param $v i32) (result i32)
846 (i32.atomic.rmw.add (local.get $p) (local.get $v)))
847
848 (func (export "addi32@small") (param $p i64) (param $v i32) (result i32)
849 (i32.atomic.rmw.add offset=${SMALL} (local.get $p) (local.get $v)))
850
851 (func (export "addi32@big") (param $p i64) (param $v i32) (result i32)
852 (i32.atomic.rmw.add offset=${BIG} (local.get $p) (local.get $v)))
853
854 (func (export "addi32@huge") (param $p i64) (param $v i32) (result i32)
855 (i32.atomic.rmw.add offset=${HUGE} (local.get $p) (local.get $v)))
856
857 (func (export "addi32/const@0") (param $v i32) (result i32)
858 (i32.atomic.rmw.add (i64.const ${LOC}) (local.get $v)))
859
860 (func (export "addi32/const@small") (param $v i32) (result i32)
861 (i32.atomic.rmw.add offset=${SMALL} (i64.const ${LOC}) (local.get $v)))
862
863 (func (export "addi32/const@big") (param $v i32) (result i32)
864 (i32.atomic.rmw.add offset=${BIG} (i64.const ${LOC}) (local.get $v)))
865
866 ;; add i64
867
868 (func (export "addi64@0") (param $p i64) (param $v i64) (result i64)
869 (i64.atomic.rmw.add (local.get $p) (local.get $v)))
870
871 (func (export "addi64@small") (param $p i64) (param $v i64) (result i64)
872 (i64.atomic.rmw.add offset=${SMALL} (local.get $p) (local.get $v)))
873
874 (func (export "addi64@big") (param $p i64) (param $v i64) (result i64)
875 (i64.atomic.rmw.add offset=${BIG} (local.get $p) (local.get $v)))
876
877 (func (export "addi64@huge") (param $p i64) (param $v i64) (result i64)
878 (i64.atomic.rmw.add offset=${HUGE} (local.get $p) (local.get $v)))
879
880 (func (export "addi64/const@0") (param $v i64) (result i64)
881 (i64.atomic.rmw.add (i64.const ${LOC}) (local.get $v)))
882
883 (func (export "addi64/const@small") (param $v i64) (result i64)
884 (i64.atomic.rmw.add offset=${SMALL} (i64.const ${LOC}) (local.get $v)))
885
886 (func (export "addi64/const@big") (param $v i64) (result i64)
887 (i64.atomic.rmw.add offset=${BIG} (i64.const ${LOC}) (local.get $v)))
888
889 ;; sub i32
890
891 (func (export "subi32@0") (param $p i64) (param $v i32) (result i32)
892 (i32.atomic.rmw.sub (local.get $p) (local.get $v)))
893
894 (func (export "subi32@small") (param $p i64) (param $v i32) (result i32)
895 (i32.atomic.rmw.sub offset=${SMALL} (local.get $p) (local.get $v)))
896
897 (func (export "subi32@big") (param $p i64) (param $v i32) (result i32)
898 (i32.atomic.rmw.sub offset=${BIG} (local.get $p) (local.get $v)))
899
900 (func (export "subi32@huge") (param $p i64) (param $v i32) (result i32)
901 (i32.atomic.rmw.sub offset=${HUGE} (local.get $p) (local.get $v)))
902
903 (func (export "subi32/const@0") (param $v i32) (result i32)
904 (i32.atomic.rmw.sub (i64.const ${LOC}) (local.get $v)))
905
906 (func (export "subi32/const@small") (param $v i32) (result i32)
907 (i32.atomic.rmw.sub offset=${SMALL} (i64.const ${LOC}) (local.get $v)))
908
909 (func (export "subi32/const@big") (param $v i32) (result i32)
910 (i32.atomic.rmw.sub offset=${BIG} (i64.const ${LOC}) (local.get $v)))
911
912 ;; sub i64
913
914 (func (export "subi64@0") (param $p i64) (param $v i64) (result i64)
915 (i64.atomic.rmw.sub (local.get $p) (local.get $v)))
916
917 (func (export "subi64@small") (param $p i64) (param $v i64) (result i64)
918 (i64.atomic.rmw.sub offset=${SMALL} (local.get $p) (local.get $v)))
919
920 (func (export "subi64@big") (param $p i64) (param $v i64) (result i64)
921 (i64.atomic.rmw.sub offset=${BIG} (local.get $p) (local.get $v)))
922
923 (func (export "subi64@huge") (param $p i64) (param $v i64) (result i64)
924 (i64.atomic.rmw.sub offset=${HUGE} (local.get $p) (local.get $v)))
925
926 (func (export "subi64/const@0") (param $v i64) (result i64)
927 (i64.atomic.rmw.sub (i64.const ${LOC}) (local.get $v)))
928
929 (func (export "subi64/const@small") (param $v i64) (result i64)
930 (i64.atomic.rmw.sub offset=${SMALL} (i64.const ${LOC}) (local.get $v)))
931
932 (func (export "subi64/const@big") (param $v i64) (result i64)
933 (i64.atomic.rmw.sub offset=${BIG} (i64.const ${LOC}) (local.get $v)))
934
935 ;; and i32
936
937 (func (export "andi32@0") (param $p i64) (param $v i32) (result i32)
938 (i32.atomic.rmw.and (local.get $p) (local.get $v)))
939
940 (func (export "andi32@small") (param $p i64) (param $v i32) (result i32)
941 (i32.atomic.rmw.and offset=${SMALL} (local.get $p) (local.get $v)))
942
943 (func (export "andi32@big") (param $p i64) (param $v i32) (result i32)
944 (i32.atomic.rmw.and offset=${BIG} (local.get $p) (local.get $v)))
945
946 (func (export "andi32@huge") (param $p i64) (param $v i32) (result i32)
947 (i32.atomic.rmw.and offset=${HUGE} (local.get $p) (local.get $v)))
948
949 (func (export "andi32/const@0") (param $v i32) (result i32)
950 (i32.atomic.rmw.and (i64.const ${LOC}) (local.get $v)))
951
952 (func (export "andi32/const@small") (param $v i32) (result i32)
953 (i32.atomic.rmw.and offset=${SMALL} (i64.const ${LOC}) (local.get $v)))
954
955 (func (export "andi32/const@big") (param $v i32) (result i32)
956 (i32.atomic.rmw.and offset=${BIG} (i64.const ${LOC}) (local.get $v)))
957
958 ;; and i64
959
960 (func (export "andi64@0") (param $p i64) (param $v i64) (result i64)
961 (i64.atomic.rmw.and (local.get $p) (local.get $v)))
962
963 (func (export "andi64@small") (param $p i64) (param $v i64) (result i64)
964 (i64.atomic.rmw.and offset=${SMALL} (local.get $p) (local.get $v)))
965
966 (func (export "andi64@big") (param $p i64) (param $v i64) (result i64)
967 (i64.atomic.rmw.and offset=${BIG} (local.get $p) (local.get $v)))
968
969 (func (export "andi64@huge") (param $p i64) (param $v i64) (result i64)
970 (i64.atomic.rmw.and offset=${HUGE} (local.get $p) (local.get $v)))
971
972 (func (export "andi64/const@0") (param $v i64) (result i64)
973 (i64.atomic.rmw.and (i64.const ${LOC}) (local.get $v)))
974
975 (func (export "andi64/const@small") (param $v i64) (result i64)
976 (i64.atomic.rmw.and offset=${SMALL} (i64.const ${LOC}) (local.get $v)))
977
978 (func (export "andi64/const@big") (param $v i64) (result i64)
979 (i64.atomic.rmw.and offset=${BIG} (i64.const ${LOC}) (local.get $v)))
980
981 ;; or i32
982
983 (func (export "ori32@0") (param $p i64) (param $v i32) (result i32)
984 (i32.atomic.rmw.or (local.get $p) (local.get $v)))
985
986 (func (export "ori32@small") (param $p i64) (param $v i32) (result i32)
987 (i32.atomic.rmw.or offset=${SMALL} (local.get $p) (local.get $v)))
988
989 (func (export "ori32@big") (param $p i64) (param $v i32) (result i32)
990 (i32.atomic.rmw.or offset=${BIG} (local.get $p) (local.get $v)))
991
992 (func (export "ori32@huge") (param $p i64) (param $v i32) (result i32)
993 (i32.atomic.rmw.or offset=${HUGE} (local.get $p) (local.get $v)))
994
995 (func (export "ori32/const@0") (param $v i32) (result i32)
996 (i32.atomic.rmw.or (i64.const ${LOC}) (local.get $v)))
997
998 (func (export "ori32/const@small") (param $v i32) (result i32)
999 (i32.atomic.rmw.or offset=${SMALL} (i64.const ${LOC}) (local.get $v)))
1000
1001 (func (export "ori32/const@big") (param $v i32) (result i32)
1002 (i32.atomic.rmw.or offset=${BIG} (i64.const ${LOC}) (local.get $v)))
1003
1004 ;; or i64
1005
1006 (func (export "ori64@0") (param $p i64) (param $v i64) (result i64)
1007 (i64.atomic.rmw.or (local.get $p) (local.get $v)))
1008
1009 (func (export "ori64@small") (param $p i64) (param $v i64) (result i64)
1010 (i64.atomic.rmw.or offset=${SMALL} (local.get $p) (local.get $v)))
1011
1012 (func (export "ori64@big") (param $p i64) (param $v i64) (result i64)
1013 (i64.atomic.rmw.or offset=${BIG} (local.get $p) (local.get $v)))
1014
1015 (func (export "ori64@huge") (param $p i64) (param $v i64) (result i64)
1016 (i64.atomic.rmw.or offset=${HUGE} (local.get $p) (local.get $v)))
1017
1018 (func (export "ori64/const@0") (param $v i64) (result i64)
1019 (i64.atomic.rmw.or (i64.const ${LOC}) (local.get $v)))
1020
1021 (func (export "ori64/const@small") (param $v i64) (result i64)
1022 (i64.atomic.rmw.or offset=${SMALL} (i64.const ${LOC}) (local.get $v)))
1023
1024 (func (export "ori64/const@big") (param $v i64) (result i64)
1025 (i64.atomic.rmw.or offset=${BIG} (i64.const ${LOC}) (local.get $v)))
1026
1027 ;; xor i32
1028
1029 (func (export "xori32@0") (param $p i64) (param $v i32) (result i32)
1030 (i32.atomic.rmw.xor (local.get $p) (local.get $v)))
1031
1032 (func (export "xori32@small") (param $p i64) (param $v i32) (result i32)
1033 (i32.atomic.rmw.xor offset=${SMALL} (local.get $p) (local.get $v)))
1034
1035 (func (export "xori32@big") (param $p i64) (param $v i32) (result i32)
1036 (i32.atomic.rmw.xor offset=${BIG} (local.get $p) (local.get $v)))
1037
1038 (func (export "xori32@huge") (param $p i64) (param $v i32) (result i32)
1039 (i32.atomic.rmw.xor offset=${HUGE} (local.get $p) (local.get $v)))
1040
1041 (func (export "xori32/const@0") (param $v i32) (result i32)
1042 (i32.atomic.rmw.xor (i64.const ${LOC}) (local.get $v)))
1043
1044 (func (export "xori32/const@small") (param $v i32) (result i32)
1045 (i32.atomic.rmw.xor offset=${SMALL} (i64.const ${LOC}) (local.get $v)))
1046
1047 (func (export "xori32/const@big") (param $v i32) (result i32)
1048 (i32.atomic.rmw.xor offset=${BIG} (i64.const ${LOC}) (local.get $v)))
1049
1050 ;; xor i64
1051
1052 (func (export "xori64@0") (param $p i64) (param $v i64) (result i64)
1053 (i64.atomic.rmw.xor (local.get $p) (local.get $v)))
1054
1055 (func (export "xori64@small") (param $p i64) (param $v i64) (result i64)
1056 (i64.atomic.rmw.xor offset=${SMALL} (local.get $p) (local.get $v)))
1057
1058 (func (export "xori64@big") (param $p i64) (param $v i64) (result i64)
1059 (i64.atomic.rmw.xor offset=${BIG} (local.get $p) (local.get $v)))
1060
1061 (func (export "xori64@huge") (param $p i64) (param $v i64) (result i64)
1062 (i64.atomic.rmw.xor offset=${HUGE} (local.get $p) (local.get $v)))
1063
1064 (func (export "xori64/const@0") (param $v i64) (result i64)
1065 (i64.atomic.rmw.xor (i64.const ${LOC}) (local.get $v)))
1066
1067 (func (export "xori64/const@small") (param $v i64) (result i64)
1068 (i64.atomic.rmw.xor offset=${SMALL} (i64.const ${LOC}) (local.get $v)))
1069
1070 (func (export "xori64/const@big") (param $v i64) (result i64)
1071 (i64.atomic.rmw.xor offset=${BIG} (i64.const ${LOC}) (local.get $v)))
1072
1073 ;; cmpxchg i32
1074
1075 (func (export "cmpxchgi32@0") (param $p i64) (param $expect i32) (param $new i32) (result i32)
1076 (i32.atomic.rmw.cmpxchg (local.get $p) (local.get $expect) (local.get $new)))
1077
1078 (func (export "cmpxchgi32@small") (param $p i64) (param $expect i32) (param $new i32) (result i32)
1079 (i32.atomic.rmw.cmpxchg offset=${SMALL} (local.get $p) (local.get $expect) (local.get $new)))
1080
1081 (func (export "cmpxchgi32@big") (param $p i64) (param $expect i32) (param $new i32) (result i32)
1082 (i32.atomic.rmw.cmpxchg offset=${BIG} (local.get $p) (local.get $expect) (local.get $new)))
1083
1084 (func (export "cmpxchgi32@huge") (param $p i64) (param $expect i32) (param $new i32) (result i32)
1085 (i32.atomic.rmw.cmpxchg offset=${HUGE} (local.get $p) (local.get $expect) (local.get $new)))
1086
1087 (func (export "cmpxchgi32/const@0") (param $expect i32) (param $new i32) (result i32)
1088 (i32.atomic.rmw.cmpxchg (i64.const ${LOC}) (local.get $expect) (local.get $new)))
1089
1090 (func (export "cmpxchgi32/const@small") (param $expect i32) (param $new i32) (result i32)
1091 (i32.atomic.rmw.cmpxchg offset=${SMALL} (i64.const ${LOC}) (local.get $expect) (local.get $new)))
1092
1093 (func (export "cmpxchgi32/const@big") (param $expect i32) (param $new i32) (result i32)
1094 (i32.atomic.rmw.cmpxchg offset=${BIG} (i64.const ${LOC}) (local.get $expect) (local.get $new)))
1095
1096 ;; cmpxchg i64
1097
1098 (func (export "cmpxchgi64@0") (param $p i64) (param $expect i64) (param $new i64) (result i64)
1099 (i64.atomic.rmw.cmpxchg (local.get $p) (local.get $expect) (local.get $new)))
1100
1101 (func (export "cmpxchgi64@small") (param $p i64) (param $expect i64) (param $new i64) (result i64)
1102 (i64.atomic.rmw.cmpxchg offset=${SMALL} (local.get $p) (local.get $expect) (local.get $new)))
1103
1104 (func (export "cmpxchgi64@big") (param $p i64) (param $expect i64) (param $new i64) (result i64)
1105 (i64.atomic.rmw.cmpxchg offset=${BIG} (local.get $p) (local.get $expect) (local.get $new)))
1106
1107 (func (export "cmpxchgi64@huge") (param $p i64) (param $expect i64) (param $new i64) (result i64)
1108 (i64.atomic.rmw.cmpxchg offset=${HUGE} (local.get $p) (local.get $expect) (local.get $new)))
1109
1110 (func (export "cmpxchgi64/const@0") (param $expect i64) (param $new i64) (result i64)
1111 (i64.atomic.rmw.cmpxchg (i64.const ${LOC}) (local.get $expect) (local.get $new)))
1112
1113 (func (export "cmpxchgi64/const@small") (param $expect i64) (param $new i64) (result i64)
1114 (i64.atomic.rmw.cmpxchg offset=${SMALL} (i64.const ${LOC}) (local.get $expect) (local.get $new)))
1115
1116 (func (export "cmpxchgi64/const@big") (param $expect i64) (param $new i64) (result i64)
1117 (i64.atomic.rmw.cmpxchg offset=${BIG} (i64.const ${LOC}) (local.get $expect) (local.get $new)))
1118
1119 ;; wait
1120
1121 (func (export "waiti32@small") (param $p i64) (result i32)
1122 (memory.atomic.wait32 offset=${SMALL} (local.get $p) (i32.const 1) (i64.const 0)))
1123
1124 (func (export "waiti32@huge") (param $p i64) (result i32)
1125 (memory.atomic.wait32 offset=${HUGE} (local.get $p) (i32.const 1) (i64.const 0)))
1126
1127 (func (export "waiti64@small") (param $p i64) (result i32)
1128 (memory.atomic.wait64 offset=${SMALL} (local.get $p) (i64.const 1) (i64.const 0)))
1129
1130 (func (export "waiti64@huge") (param $p i64) (result i32)
1131 (memory.atomic.wait64 offset=${HUGE} (local.get $p) (i64.const 1) (i64.const 0)))
1132
1133 ;; wake
1134
1135 (func (export "wake@0") (param $p i64) (result i32)
1136 (memory.atomic.notify (local.get $p) (i32.const 1)))
1137
1138 (func (export "wake@small") (param $p i64) (result i32)
1139 (memory.atomic.notify offset=${SMALL} (local.get $p) (i32.const 1)))
1140
1141 (func (export "wake@big") (param $p i64) (result i32)
1142 (memory.atomic.notify offset=${BIG} (local.get $p) (i32.const 1)))
1143
1144 (func (export "wake@huge") (param $p i64) (result i32)
1145 (memory.atomic.notify offset=${HUGE} (local.get $p) (i32.const 1)))
1146 )
1147 `);
1148 return ins;
1149 }
1150
1151 function i32Random() {
1152 // Limit this to small positive numbers to keep life simple.
1153 for (;;) {
1154 let r = (Math.random() * 0x3FFF_FFFF) | 0;
1155 if (r != 0)
1156 return r;
1157 }
1158 }
1159
1160 function i64Random() {
1161 return (BigInt(i32Random()) << 32n) | BigInt(i32Random());
1162 }
1163
1164 function Random(sz) {
1165 if (sz == 4)
1166 return i32Random();
1167 return i64Random();
1168 }
1169
1170 function Random2(sz) {
1171 return [Random(sz), Random(sz)];
1172 }
1173
1174 function Random4(sz) {
1175 return [Random(sz), Random(sz), Random(sz), Random(sz)];
1176 }
1177
1178 function Zero(sz) {
1179 if (sz == 4)
1180 return 0;
1181 return 0n;
1182 }
1183
1184 function testRead(ins, mem, LOC, prefix) {
1185 let r = 0;
1186 let SZ = mem.BYTES_PER_ELEMENT;
1187 let len = mem.length * SZ;
1188 let NM = prefix + "readi" + (SZ * 8);
1189
1190 // Read in-bounds
1191
1192 r = Random(SZ);
1193 mem[LOC / SZ] = r;
1194 assertEq(ins.exports[NM + "@0"](BigInt(LOC)), r);
1195 assertEq(ins.exports[NM + "/const@0"](), r);
1196
1197 mem[(len / SZ) - 1] = Zero(SZ);
1198 assertEq(ins.exports[NM + "@0"](BigInt(len - SZ)), Zero(SZ)); // Just barely in-bounds
1199
1200 r = Random(SZ);
1201 mem[(LOC + SMALL) / SZ] = r;
1202 assertEq(ins.exports[NM + "@small"](BigInt(LOC)), r);
1203 assertEq(ins.exports[NM + "/const@small"](), r);
1204
1205 if (len >= LOC + BIG + SZ) {
1206 r = Random(SZ);
1207 mem[(LOC + BIG) / SZ] = r;
1208 assertEq(ins.exports[NM + "@big"](BigInt(LOC)), r);
1209 assertEq(ins.exports[NM + "/const@big"](), r);
1210 }
1211
1212 if (len >= LOC + VAST + SZ) {
1213 r = Random(SZ);
1214 mem[(LOC + VAST) / SZ] = r;
1215 assertEq(ins.exports[NM + "@vast"](BigInt(LOC)), r);
1216 assertEq(ins.exports[NM + "/const@vast"](), r);
1217 }
1218
1219 // Read out-of-bounds
1220
1221 assertErrorMessage(() => ins.exports[NM + "@0"](BigInt(len)),
1222 WebAssembly.RuntimeError,
1223 /out of bounds/);
1224
1225 assertErrorMessage(() => ins.exports[NM + "@0"](BigInt(len-(SZ-1))),
1226 WebAssembly.RuntimeError,
1227 prefix == "" ? /out of bounds/ : /unaligned memory access/);
1228
1229 // This is OOB if we consider the whole pointer as we must, but if we
1230 // mistakenly only look at the low bits then it's in-bounds.
1231 if (len < 0x1_0000_0000) {
1232 assertErrorMessage(() => ins.exports[NM + "@0"](0x1_0000_0000n),
1233 WebAssembly.RuntimeError,
1234 /out of bounds/);
1235 }
1236
1237 if (len < HUGE) {
1238 assertErrorMessage(() => ins.exports[NM + "@huge"](0n),
1239 WebAssembly.RuntimeError,
1240 /out of bounds/);
1241 }
1242
1243 if (len < VAST) {
1244 assertErrorMessage(() => ins.exports[NM + "@vast"](0n),
1245 WebAssembly.RuntimeError,
1246 /out of bounds/);
1247 }
1248 }
1249
1250 function testReadV128(ins, mem, LOC) {
1251 let r = 0;
1252 let SZ = mem.BYTES_PER_ELEMENT;
1253 let len = mem.length * SZ;
1254 let NM = "readv128";
1255
1256 assertEq(SZ, 4);
1257
1258 // Read in-bounds
1259
1260 r = Random4(4);
1261 mem.set(r, LOC / SZ);
1262 ins.exports[NM + "@0"](BigInt(LOC))
1263 assertSame(mem.slice(0, 4), r);
1264 ins.exports[NM + "/const@0"]();
1265 assertSame(mem.slice(0, 4), r);
1266
1267 r = new Int32Array([0,0,0,0]);
1268 mem.set(r, (len / SZ) - 4);
1269 ins.exports[NM + "@0"](BigInt(len - (SZ * 4))); // Just barely in-bounds
1270 assertSame(mem.slice(0, 4), r);
1271
1272 r = Random4(SZ);
1273 mem.set(r, (LOC + SMALL) / SZ);
1274 ins.exports[NM + "@small"](BigInt(LOC))
1275 assertSame(mem.slice(0, 4), r);
1276 ins.exports[NM + "/const@small"]();
1277 assertSame(mem.slice(0, 4), r);
1278
1279 if (len >= LOC + BIG + SZ * 4) {
1280 r = Random4(SZ);
1281 mem.set(r, (LOC + BIG) / SZ);
1282 ins.exports[NM + "@big"](BigInt(LOC));
1283 assertSame(mem.slice(0, 4), r);
1284 ins.exports[NM + "/const@big"]();
1285 assertSame(mem.slice(0, 4), r);
1286 }
1287
1288 // Read out-of-bounds
1289
1290 assertErrorMessage(() => ins.exports[NM + "@0"](BigInt(len)),
1291 WebAssembly.RuntimeError,
1292 /out of bounds/);
1293
1294 assertErrorMessage(() => ins.exports[NM + "@0"](BigInt(len-((SZ*4)-1))),
1295 WebAssembly.RuntimeError,
1296 /out of bounds/);
1297
1298 // This is OOB if we consider the whole pointer as we must, but if we
1299 // mistakenly only look at the low bits then it's in-bounds.
1300 if (len < 0x1_0000_0000) {
1301 assertErrorMessage(() => ins.exports[NM + "@0"](0x1_0000_0000n),
1302 WebAssembly.RuntimeError,
1303 /out of bounds/);
1304 }
1305
1306 if (len < HUGE) {
1307 assertErrorMessage(() => ins.exports[NM + "@huge"](0n),
1308 WebAssembly.RuntimeError,
1309 /out of bounds/);
1310 }
1311
1312 // Superficial testing of other load operations
1313
1314 r = i32Random()
1315 mem[(LOC + SMALL) / SZ] = r;
1316 ins.exports["v128.load_splat@small"](BigInt(LOC));
1317 assertSame(mem.slice(0, 4), [r, r, r, r]);
1318
1319 r = i32Random()
1320 mem[(LOC + SMALL) / SZ] = r;
1321 ins.exports["v128.load_zero@small"](BigInt(LOC));
1322 assertSame(mem.slice(0, 4), [r, 0, 0, 0]);
1323
1324 r = Random2(SZ)
1325 mem.set(r, (LOC + SMALL) / SZ);
1326 ins.exports["v128.load_extend@small"](BigInt(LOC));
1327 assertSame(mem.slice(0, 4), [r[0], 0, r[1], 0]);
1328
1329 r = Random4(SZ)
1330 mem.set(r, 0);
1331 let s = i32Random()
1332 mem[(LOC + SMALL) / SZ] = s;
1333 ins.exports["v128.load_lane@small"](BigInt(LOC));
1334 assertSame(mem.slice(0, 4), [r[0], r[1], s, r[3]]);
1335 }
1336
1337 function testWrite(ins, mem, LOC, prefix) {
1338 let r = 0;
1339 let SZ = mem.BYTES_PER_ELEMENT;
1340 let len = mem.length * SZ;
1341 let WNM = prefix + "writei" + (SZ * 8);
1342 let RNM = prefix + "readi" + (SZ * 8);
1343
1344 // Write in-bounds
1345
1346 r = Random(SZ);
1347 ins.exports[WNM + "@0"](BigInt(LOC), r);
1348 assertEq(ins.exports[RNM + "@0"](BigInt(LOC)), r);
1349
1350 r = Random(SZ);
1351 ins.exports[WNM + "@small"](BigInt(LOC), r);
1352 assertEq(ins.exports[RNM + "@small"](BigInt(LOC)), r);
1353
1354 if (len >= LOC + BIG + SZ) {
1355 r = Random(SZ);
1356 ins.exports[WNM + "@big"](BigInt(LOC), r);
1357 assertEq(ins.exports[RNM + "@big"](BigInt(LOC)), r);
1358 }
1359
1360 if (len >= LOC + VAST + SZ) {
1361 r = Random(SZ);
1362 ins.exports[WNM + "@vast"](BigInt(LOC), r);
1363 assertEq(ins.exports[RNM + "@vast"](BigInt(LOC)), r);
1364 }
1365
1366 r = Random(SZ);
1367 ins.exports[WNM + "@0"](BigInt(len - SZ), r); // Just barely in-bounds
1368 assertEq(ins.exports[RNM + "@0"](BigInt(len - SZ)), r);
1369
1370 // Write out-of-bounds
1371
1372 assertErrorMessage(() => ins.exports[WNM + "@0"](BigInt(len), Random(SZ)),
1373 WebAssembly.RuntimeError,
1374 /out of bounds/);
1375
1376 assertErrorMessage(() => ins.exports[WNM + "@0"](BigInt(len - (SZ - 1)), Random(SZ)),
1377 WebAssembly.RuntimeError,
1378 prefix == "" ? /out of bounds/ : /unaligned memory access/);
1379
1380 if (len < 0x1_0000_0000) {
1381 let xs = ins.exports[RNM + "@0"](0n);
1382 assertErrorMessage(() => ins.exports[WNM + "@0"](0x1_0000_0000n, Random(SZ)),
1383 WebAssembly.RuntimeError,
1384 /out of bounds/);
1385 // Check for scribbling
1386 assertEq(ins.exports[RNM + "@0"](0n), xs);
1387 }
1388
1389 if (len < HUGE) {
1390 assertErrorMessage(() => ins.exports[WNM + "@huge"](0n, Random(SZ)),
1391 WebAssembly.RuntimeError,
1392 /out of bounds/);
1393 }
1394
1395 if (len < VAST) {
1396 assertErrorMessage(() => ins.exports[WNM + "@vast"](0n, Random(SZ)),
1397 WebAssembly.RuntimeError,
1398 /out of bounds/);
1399 }
1400 }
1401
1402 function testWriteV128(ins, mem, LOC) {
1403 let r = 0;
1404 let p = 0;
1405 let SZ = mem.BYTES_PER_ELEMENT;
1406 let len = mem.length * SZ;
1407 let WNM = "writev128";
1408 let RNM = "readv128";
1409
1410 assertEq(SZ, 4);
1411
1412 // Write in-bounds
1413
1414 r = Random4(SZ);
1415 mem.set(r, 0);
1416 p = LOC / SZ;
1417 ins.exports[WNM + "@0"](BigInt(LOC));
1418 assertSame(mem.slice(p, p + 4), r);
1419
1420 r = Random4(SZ);
1421 mem.set(r, 0);
1422 p = (LOC + SMALL) / SZ;
1423 ins.exports[WNM + "@small"](BigInt(LOC));
1424 assertSame(mem.slice(p, p + 4), r);
1425
1426 if (len >= LOC + BIG + SZ) {
1427 r = Random4(SZ);
1428 mem.set(r, 0);
1429 p = (LOC + BIG) / SZ;
1430 ins.exports[WNM + "@big"](BigInt(LOC));
1431 assertSame(mem.slice(p, p + 4), r);
1432 }
1433
1434 r = Random4(SZ);
1435 mem.set(r, 0);
1436 p = (len - (SZ * 4)) / SZ;
1437 ins.exports[WNM + "@0"](BigInt(len - (SZ * 4))); // Just barely in-bounds
1438 assertSame(mem.slice(p, p + 4), r);
1439
1440 // Write out-of-bounds
1441
1442 assertErrorMessage(() => ins.exports[WNM + "@0"](BigInt(len)),
1443 WebAssembly.RuntimeError,
1444 /out of bounds/);
1445
1446 assertErrorMessage(() => ins.exports[WNM + "@0"](BigInt(len - ((SZ * 4) - 1))),
1447 WebAssembly.RuntimeError,
1448 /out of bounds/);
1449
1450 if (len < HUGE) {
1451 assertErrorMessage(() => ins.exports[WNM + "@huge"](0n),
1452 WebAssembly.RuntimeError,
1453 /out of bounds/);
1454 }
1455
1456 // Superficial testing of other store operations
1457
1458 r = Random4(SZ);
1459 mem.set(r, 0);
1460 ins.exports["v128.store_lane@small"](BigInt(LOC));
1461 assertEq(mem[(LOC + SMALL) / SZ], r[2]);
1462 }
1463
1464 function testAtomicRMW(ins, mem, LOC, op, fn) {
1465 let r = 0, s = 0;
1466 let SZ = mem.BYTES_PER_ELEMENT;
1467 let len = mem.length * SZ;
1468 let NM = op + "i" + (SZ * 8);
1469
1470 [r,s] = Random2(SZ);
1471 mem[LOC / SZ] = r;
1472 assertEq(ins.exports[NM + "@0"](BigInt(LOC), s), r);
1473 assertEq(mem[LOC / SZ], fn(r, s));
1474
1475 [r,s] = Random2(SZ);
1476 mem[(LOC + SMALL) / SZ] = r;
1477 assertEq(ins.exports[NM + "@small"](BigInt(LOC), s), r);
1478 assertEq(mem[(LOC + SMALL) / SZ], fn(r, s));
1479
1480 if (len >= LOC + BIG + SZ) {
1481 [r,s] = Random2(SZ);
1482 mem[(LOC + BIG) / SZ] = r;
1483 assertEq(ins.exports[NM + "@big"](BigInt(LOC), s), r);
1484 assertEq(mem[(LOC + BIG) / SZ], fn(r, s));
1485 }
1486
1487
1488 assertErrorMessage(() => ins.exports[NM + "@0"](BigInt(len), Zero(SZ)),
1489 WebAssembly.RuntimeError,
1490 /out of bounds/);
1491
1492 assertErrorMessage(() => ins.exports[NM + "@0"](BigInt(len - (SZ - 1)), Zero(SZ)),
1493 WebAssembly.RuntimeError,
1494 /unaligned memory access/);
1495
1496 if (len < HUGE) {
1497 assertErrorMessage(() => ins.exports[NM + "@huge"](0n, Zero(SZ)),
1498 WebAssembly.RuntimeError,
1499 /out of bounds/);
1500 }
1501 }
1502
1503 function testAtomicCmpxchg(ins, mem, LOC) {
1504 let r = 0, s = 0;
1505 let SZ = mem.BYTES_PER_ELEMENT;
1506 let len = mem.length * SZ;
1507 let NM = "cmpxchgi" + (SZ * 8);
1508
1509 [r,s] = Random2(SZ);
1510 mem[LOC / SZ] = r;
1511 assertEq(ins.exports[NM + "@0"](BigInt(LOC), Zero(SZ), s), r);
1512 assertEq(ins.exports[NM + "@0"](BigInt(LOC), r, s), r);
1513 assertEq(mem[LOC / SZ], s);
1514
1515 [r,s] = Random2(SZ);
1516 mem[(LOC + SMALL) / SZ] = r;
1517 assertEq(ins.exports[NM + "@0"](BigInt(LOC + SMALL), Zero(SZ), s), r);
1518 assertEq(ins.exports[NM + "@0"](BigInt(LOC + SMALL), r, s), r);
1519 assertEq(mem[(LOC + SMALL) / SZ], s);
1520
1521 if (len >= LOC + BIG + SZ) {
1522 [r,s] = Random2(SZ);
1523 mem[(LOC + BIG) / SZ] = r;
1524 assertEq(ins.exports[NM + "@0"](BigInt(LOC + BIG), Zero(SZ), s), r);
1525 assertEq(ins.exports[NM + "@0"](BigInt(LOC + BIG), r, s), r);
1526 assertEq(mem[(LOC + BIG) / SZ], s);
1527 }
1528
1529 assertErrorMessage(() => ins.exports[NM + "@0"](BigInt(len), Zero(SZ), Zero(SZ)),
1530 WebAssembly.RuntimeError,
1531 /out of bounds/);
1532
1533 assertErrorMessage(() => ins.exports[NM + "@0"](BigInt(len - (SZ - 1)), Zero(SZ), Zero(SZ)),
1534 WebAssembly.RuntimeError,
1535 /unaligned memory access/);
1536
1537 if (len < HUGE) {
1538 assertErrorMessage(() => ins.exports[NM + "@huge"](0n, Zero(SZ), Zero(SZ)),
1539 WebAssembly.RuntimeError,
1540 /out of bounds/);
1541 }
1542 }
1543
1544 function testAtomicWake(ins, mem, LOC) {
1545 let SZ = mem.BYTES_PER_ELEMENT;
1546 let len = mem.length * SZ;
1547
1548 assertEq(ins.exports["wake@0"](BigInt(LOC)), 0);
1549 assertEq(ins.exports["wake@small"](BigInt(LOC)), 0);
1550 if (len >= LOC + BIG + SZ) {
1551 assertEq(ins.exports["wake@big"](BigInt(LOC)), 0);
1552 }
1553
1554 assertErrorMessage(() => ins.exports["wake@0"](BigInt(len)),
1555 WebAssembly.RuntimeError,
1556 /out of bounds/);
1557
1558 assertErrorMessage(() => ins.exports["wake@0"](BigInt(len - (SZ - 1))),
1559 WebAssembly.RuntimeError,
1560 /unaligned memory access/);
1561
1562 if (len < HUGE) {
1563 assertErrorMessage(() => ins.exports["wake@huge"](BigInt(LOC)),
1564 WebAssembly.RuntimeError,
1565 /out of bounds/);
1566 }
1567 }
1568
1569 // Sometimes we start memory at zero to disable certain bounds checking
1570 // optimizations. Other times we start memory at something beyond most of
1571 // our references to enable those optimizations.
1572 let configs = [[40, 0, 3], [40, 3, '']];
1573
1574 // On 64-bit systems, test beyond 2GB and also beyond 4GB
1575 if (getBuildConfiguration("pointer-byte-size") == 8) {
1576 configs.push([Math.pow(2, 31) + 40, 32771, '']);
1577 configs.push([Math.pow(2, 32) + 40, 65539, '']);
1578 configs.push([Math.pow(2, 31) + 40, 32771, 32773]);
1579 configs.push([Math.pow(2, 32) + 40, 65539, 65541]);
1580 }
1581
1582 for ( let shared of ['','shared'] ) {
1583 for (let [LOC, start, max] of configs) {
1584 if (shared != '' && max == '') {
1585 continue;
1586 }
1587 const ins = makeTest(LOC, start, max, shared);
1588 if (max != '') {
1589 // This can OOM legitimately; let it.
1590 let res = Number(ins.exports.mem.grow(BigInt(max - start)));
1591 if (res == -1) {
1592 print("SPURIOUS OOM");
1593 continue;
1594 }
1595 assertEq(res, start);
1596 }
1597
1598 const mem32 = new Int32Array(ins.exports.mem.buffer);
1599 const mem64 = new BigInt64Array(ins.exports.mem.buffer);
1600
1601 for ( let m of [mem32, mem64] ) {
1602 testRead(ins, m, LOC, "");
1603 testWrite(ins, m, LOC, "");
1604 testRead(ins, m, LOC, "a");
1605 testWrite(ins, m, LOC, "a");
1606 testAtomicRMW(ins, m, LOC, "add", (r,s) => r+s);
1607 testAtomicRMW(ins, m, LOC, "sub", (r,s) => r-s);
1608 testAtomicRMW(ins, m, LOC, "and", (r,s) => r&s);
1609 testAtomicRMW(ins, m, LOC, "or", (r,s) => r|s);
1610 testAtomicRMW(ins, m, LOC, "xor", (r,s) => r^s);
1611 testAtomicRMW(ins, m, LOC, "xchg", (r,s) => s);
1612 testAtomicCmpxchg(ins, m, LOC);
1613 testAtomicWake(ins, m, LOC);
1614 }
1615
1616 if (wasmSimdEnabled()) {
1617 testReadV128(ins, mem32, LOC);
1618 testWriteV128(ins, mem32, LOC);
1619 }
1620 }
1621 }
1622
1623 // Bulk memory operations
1624
1625 function makeModule(initial, maximum, shared) {
1626 return `
1627 (module
1628 (memory (export "mem") i64 ${initial} ${maximum} ${shared})
1629
1630 (data $seg "0123456789")
1631
1632 (func (export "size") (result i64)
1633 memory.size)
1634
1635 (func (export "grow") (param $delta i64) (result i64)
1636 (memory.grow (local.get $delta)))
1637
1638 (func (export "copy") (param $to i64) (param $from i64) (param $len i64)
1639 (memory.copy (local.get $to) (local.get $from) (local.get $len)))
1640
1641 (func (export "fill") (param $to i64) (param $val i32) (param $len i64)
1642 (memory.fill (local.get $to) (local.get $val) (local.get $len)))
1643
1644 (func (export "init") (param $to i64) (param $src i32) (param $count i32)
1645 (memory.init $seg (local.get $to) (local.get $src) (local.get $count)))
1646 )`;
1647 }
1648
1649 for ( let shared of ['','shared'] ) {
1650 let ins = wasmEvalText(makeModule(1, 3, shared));
1651
1652 assertEq(ins.exports.size(), 1n);
1653
1654 // OOM with very low probability will result in test failure
1655 assertEq(ins.exports.grow(2n), 1n);
1656 assertEq(ins.exports.size(), 3n);
1657
1658 // OOM with very low probability will result in test failure
1659 assertEq(ins.exports.grow(1n), -1n);
1660 assertEq(ins.exports.size(), 3n);
1661
1662 // More than max pages
1663 assertEq(ins.exports.grow(100000n), -1n);
1664 assertEq(ins.exports.size(), 3n);
1665
1666 // More than 2^48 pages
1667 assertEq(ins.exports.grow(0x1_0000_0000_0000n), -1n);
1668 assertEq(ins.exports.size(), 3n);
1669
1670 // More than 2^48 pages - interpreted as unsigned
1671 assertEq(ins.exports.grow(-1n), -1n);
1672 assertEq(ins.exports.size(), 3n);
1673
1674 var mem = new Uint8Array(ins.exports.mem.buffer);
1675 var val = [1,2,3,4,5];
1676 mem.set(val, 20);
1677 ins.exports.copy(40n, 20n, 5n);
1678 assertSame(val, mem.slice(40, 45));
1679
1680 ins.exports.fill(39n, 37, 8n);
1681 assertSame(iota(8).map(_ => 37), mem.slice(39, 47));
1682
1683 ins.exports.init(128n, 1, 5);
1684 assertSame(iota(5).map(x => x+49), mem.slice(128, 133));
1685 }
1686
1687 if (getBuildConfiguration("pointer-byte-size") == 8) {
1688 for ( let shared of ['','shared'] ) {
1689 let limit = wasmMaxMemoryPages('i64');
1690 let initial = 65537;
1691 let maximum = limit + 1;
1692 let pagesize = 65536n;
1693
1694 let ins = wasmEvalText(makeModule(initial, maximum, shared));
1695
1696 assertEq(ins.exports.size(), BigInt(initial));
1697
1698 // This can OOM legitimately; let it.
1699 {
1700 let res = ins.exports.grow(2n);
1701 if (res == -1) {
1702 print("SPURIOUS OOM");
1703 continue;
1704 }
1705 assertEq(res, BigInt(initial));
1706 }
1707 assertEq(ins.exports.size(), BigInt(initial + 2));
1708
1709 // This must fail
1710 assertEq(ins.exports.grow(BigInt(limit - (initial + 2) + 1)), -1n);
1711 assertEq(ins.exports.size(), BigInt(initial + 2));
1712
1713 // This can OOM legitimately; let it.
1714 {
1715 let res = ins.exports.grow(BigInt(limit - (initial + 2)));
1716 if (res == -1) {
1717 print("SPURIOUS OOM");
1718 continue;
1719 }
1720 assertEq(res, BigInt(initial + 2));
1721 }
1722 assertEq(ins.exports.size(), BigInt(limit));
1723
1724 let mem = new Uint8Array(ins.exports.mem.buffer);
1725
1726 let copyval = [1,2,3,4,5];
1727 let source = 20n;
1728 let target = BigInt(initial) * pagesize + 40n;
1729 let oobTarget = BigInt(limit) * pagesize - 1n;
1730
1731 // Copy from memory below 4GB to memory beyond 4GB
1732 mem.set(copyval, Number(source));
1733 ins.exports.copy(target, source, BigInt(copyval.length));
1734 assertSame(copyval, mem.slice(Number(target), Number(target) + copyval.length))
1735
1736 // Try to copy out of bounds
1737 // src and target are both in bounds but len brings it oob
1738 assertErrorMessage(() => ins.exports.copy(oobTarget, source, BigInt(copyval.length)),
1739 WebAssembly.RuntimeError,
1740 /out of bounds/);
1741 assertEq(mem[Number(oobTarget-1n)], 0);
1742 assertErrorMessage(() => ins.exports.copy(-1n, source, BigInt(copyval.length)),
1743 WebAssembly.RuntimeError,
1744 /out of bounds/);
1745 assertEq(mem[Number(oobTarget-1n)], 0);
1746
1747 // Fill above 4GB
1748 ins.exports.fill(target, 37, 30n);
1749 assertSame(iota(30).map(_ => 37), mem.slice(Number(target), Number(target) + 30));
1750
1751 // Try to fill out of bounds
1752 assertErrorMessage(() => ins.exports.fill(oobTarget, 37, 2n),
1753 WebAssembly.RuntimeError,
1754 /out of bounds/);
1755 assertEq(mem[Number(oobTarget-1n)], 0);
1756
1757 // Init above 4GB
1758 ins.exports.init(target, 1, 5);
1759 assertSame(iota(5).map(x => x+49), mem.slice(Number(target), Number(target)+5));
1760
1761 // Try to init out of bounds
1762 assertErrorMessage(() => ins.exports.init(oobTarget, 1, 5),
1763 WebAssembly.RuntimeError,
1764 /out of bounds/);
1765 assertEq(mem[Number(oobTarget-1n)], 0);
1766 }
1767 }
1768
1769 ////////////////////////////////////
1770 // Table64 execution
1771
1772 // get / set
1773 const table64Tests = [
1774 {
1775 elem: "anyref",
1776 jsval: "haha you cannot represent me",
1777 wasmval: `(struct.new_default $s)`,
1778 },
1779 ];
1780 if (WebAssembly.Function) {
1781 table64Tests.push({
1782 elem: "funcref",
1783 jsval: new WebAssembly.Function({ parameters: ["i32"], results: [] }, () => {}),
1784 wasmval: `(ref.func 0)`,
1785 });
1786 }
1787 for (const test of table64Tests) {
1788 const externalTable = new WebAssembly.Table({ address: "i64", element: test.elem, initial: 2n });
1789 externalTable.set(0n, test.jsval);
1790 externalTable.set(1n, null);
1791
1792 const {
1793 internalTable,
1794 extIsNull,
1795 swapExt,
1796 getInternal,
1797 getExternal,
1798 setInternal,
1799 setExternal,
1800 } = wasmEvalText(`(module
1801 (import "" "externalTable" (table $ext i64 2 ${test.elem}))
1802
1803 (table $int (export "internalTable") i64 2 ${test.elem})
1804 (elem declare func 0)
1805 (type $s (struct))
1806
1807 (func $start
1808 (table.set $int (i64.const 0) ${test.wasmval})
1809 )
1810 (start $start)
1811
1812 (func (export "extIsNull") (param $i i64) (result i32)
1813 (ref.is_null (table.get $ext (local.get $i)))
1814 )
1815 (func (export "swapExt")
1816 (local $tmp ${test.elem})
1817
1818 (local.set $tmp (table.get $ext (i64.const 0)))
1819 (table.set $ext (i64.const 0) (table.get $ext (i64.const 1)))
1820 (table.set $ext (i64.const 1) (local.get $tmp))
1821 )
1822
1823 (func (export "getInternal") (param i64) (result ${test.elem})
1824 (table.get $int (local.get 0))
1825 )
1826 (func (export "getExternal") (param i64) (result ${test.elem})
1827 (table.get $ext (local.get 0))
1828 )
1829 (func (export "setInternal") (param i64 ${test.elem})
1830 (table.set $int (local.get 0) (local.get 1))
1831 )
1832 (func (export "setExternal") (param i64 ${test.elem})
1833 (table.set $ext (local.get 0) (local.get 1))
1834 )
1835 )`, { "": { externalTable } }).exports;
1836
1837 assertEq(internalTable.get(0n) === null, false);
1838 assertEq(internalTable.get(1n) === null, true);
1839 assertEq(extIsNull(0n), 0);
1840 assertEq(extIsNull(1n), 1);
1841
1842 swapExt();
1843 const tmp = internalTable.get(0n);
1844 internalTable.set(0n, internalTable.get(1n));
1845 internalTable.set(1n, tmp);
1846
1847 assertEq(internalTable.get(0n) === null, true);
1848 assertEq(internalTable.get(1n) === null, false);
1849 assertEq(extIsNull(0n), 1);
1850 assertEq(extIsNull(1n), 0);
1851
1852 // Test bounds checks
1853 const indexes = [
1854 [-1n, false, TypeError],
1855 [2n, false, RangeError],
1856 [BigInt(0 + (MaxUint32 + 1)), false, RangeError],
1857 [BigInt(1 + (MaxUint32 + 1)), false, RangeError],
1858 [BigInt(2 + (MaxUint32 + 1)), false, RangeError],
1859 [BigInt(Number.MAX_SAFE_INTEGER), false, RangeError],
1860 [BigInt(Number.MAX_SAFE_INTEGER + 1), false, RangeError],
1861 [2n**64n - 1n, false, RangeError],
1862 [2n**64n, true, TypeError],
1863 ];
1864 for (const [index, jsOnly, jsError] of indexes) {
1865 if (!jsOnly) {
1866 assertErrorMessage(() => getInternal(index), WebAssembly.RuntimeError, /index out of bounds/);
1867 assertErrorMessage(() => setInternal(index, null), WebAssembly.RuntimeError, /index out of bounds/);
1868 }
1869 assertErrorMessage(() => internalTable.get(index), jsError, /bad Table get address/);
1870 assertErrorMessage(() => internalTable.set(index, null), jsError, /bad Table set address/);
1871
1872 if (!jsOnly) {
1873 assertErrorMessage(() => getExternal(index), WebAssembly.RuntimeError, /index out of bounds/);
1874 assertErrorMessage(() => setExternal(index, null), WebAssembly.RuntimeError, /index out of bounds/);
1875 }
1876 assertErrorMessage(() => externalTable.get(index), jsError, /bad Table get address/);
1877 assertErrorMessage(() => externalTable.set(index, null), jsError, /bad Table set address/);
1878 }
1879 }
1880
1881 // call_indirect / call_ref / return_call_indirect / return_call_ref
1882 {
1883 const {
1884 callIndirect,
1885 callRef,
1886 returnCallIndirect,
1887 returnCallRef,
1888 } = wasmEvalText(`(module
1889 (table $int (export "internalTable") i64 funcref
1890 (elem (ref.func 0) (ref.null func))
1891 )
1892
1893 (type $ft (func (param i32) (result i32)))
1894 (func (type $ft)
1895 (i32.add (local.get 0) (i32.const 10))
1896 )
1897 (func (export "callIndirect") (param i64 i32) (result i32)
1898 (call_indirect $int (type $ft) (local.get 1) (local.get 0))
1899 )
1900 (func (export "callRef") (param i64 i32) (result i32)
1901 (call_ref $ft (local.get 1) (ref.cast (ref null $ft) (table.get $int (local.get 0))))
1902 )
1903 (func (export "returnCallIndirect") (param i64 i32) (result i32)
1904 (return_call_indirect $int (type $ft) (local.get 1) (local.get 0))
1905 )
1906 (func (export "returnCallRef") (param i64 i32) (result i32)
1907 (return_call_ref $ft (local.get 1) (ref.cast (ref null $ft) (table.get $int (local.get 0))))
1908 )
1909 )`).exports;
1910
1911 assertEq(callIndirect(0n, 1), 11);
1912 assertEq(callRef(0n, 2), 12);
1913 assertErrorMessage(() => callIndirect(1n, 1), WebAssembly.RuntimeError, /indirect call to null/);
1914 assertErrorMessage(() => callRef(1n, 2), WebAssembly.RuntimeError, /dereferencing null pointer/);
1915 assertEq(returnCallIndirect(0n, 3), 13);
1916 assertEq(returnCallRef(0n, 4), 14);
1917 assertErrorMessage(() => returnCallIndirect(1n, 3), WebAssembly.RuntimeError, /indirect call to null/);
1918 assertErrorMessage(() => returnCallRef(1n, 4), WebAssembly.RuntimeError, /dereferencing null pointer/);
1919
1920 // Test bounds checks
1921 const indexes = [
1922 -1,
1923 2,
1924 0 + (MaxUint32 + 1),
1925 1 + (MaxUint32 + 1),
1926 2 + (MaxUint32 + 1),
1927 Number.MAX_SAFE_INTEGER,
1928 Number.MAX_SAFE_INTEGER + 1,
1929 ];
1930 for (const index of indexes) {
1931 assertErrorMessage(() => callIndirect(BigInt(index), 1), WebAssembly.RuntimeError, /index out of bounds/);
1932 assertErrorMessage(() => callRef(BigInt(index), 2), WebAssembly.RuntimeError, /table index out of bounds/);
1933 assertErrorMessage(() => returnCallIndirect(BigInt(index), 3), WebAssembly.RuntimeError, /index out of bounds/);
1934 assertErrorMessage(() => returnCallRef(BigInt(index), 4), WebAssembly.RuntimeError, /table index out of bounds/);
1935 }
1936 }
1937
1938 // Bulk table operations
1939 for (const [addrType1, addrType2] of types) {
1940 const lenType = addrType1 === "i64" && addrType2 === "i64" ? "i64" : "i32";
1941
1942 const {
1943 f1, f2, e1, e2,
1944 callF1, callF2,
1945 getE1, getE2,
1946 initF1, initE1,
1947 copyF, copyE,
1948 growF1, growE1,
1949 growF2, growE2,
1950 sizeF1, sizeE1,
1951 sizeF2, sizeE2,
1952 fillF1WithA,
1953 fillE1WithA,
1954 } = wasmEvalText(`(module
1955 (global $ea (import "" "ea") externref)
1956 (global $eb (import "" "eb") externref)
1957 (global $ec (import "" "ec") externref)
1958 (global $ed (import "" "ed") externref)
1959
1960 (table $f1 (export "f1") ${addrType1} 8 funcref)
1961 (table $f2 (export "f2") ${addrType2} 8 funcref)
1962 (table $e1 (export "e1") ${addrType1} 8 externref)
1963 (table $e2 (export "e2") ${addrType2} 8 externref)
1964
1965 (elem (table $f1) (offset ${addrType1}.const 2) funcref (ref.func $a) (ref.func $b))
1966 (elem $fcd funcref (ref.func $c) (ref.func $d))
1967 (elem (table $e1) (offset ${addrType1}.const 2) externref (global.get $ea) (global.get $eb))
1968 (elem $ecd externref (global.get $ec) (global.get $ed))
1969
1970 (type $ft (func (result i32)))
1971 (func $a (result i32) i32.const 97) ;; 'a'
1972 (func $b (result i32) i32.const 98) ;; 'b'
1973 (func $c (result i32) i32.const 99) ;; 'c'
1974 (func $d (result i32) i32.const 100) ;; 'd'
1975
1976 (func (export "callF1") (param ${addrType1}) (result i32)
1977 (if (ref.is_null (table.get $f1 (local.get 0)))
1978 (then
1979 (return (i32.const 0))
1980 )
1981 )
1982
1983 (table.get $f1 (local.get 0))
1984 ref.cast (ref $ft)
1985 call_ref $ft
1986 )
1987 (func (export "callF2") (param ${addrType2}) (result i32)
1988 (if (ref.is_null (table.get $f2 (local.get 0)))
1989 (then
1990 (return (i32.const 0))
1991 )
1992 )
1993
1994 (table.get $f2 (local.get 0))
1995 ref.cast (ref $ft)
1996 call_ref $ft
1997 )
1998
1999 (func (export "getE1") (param ${addrType1}) (result externref)
2000 (table.get $e1 (local.get 0))
2001 )
2002 (func (export "getE2") (param ${addrType2}) (result externref)
2003 (table.get $e2 (local.get 0))
2004 )
2005
2006 (func (export "sizeF1") (result ${addrType1}) table.size $f1)
2007 (func (export "sizeF2") (result ${addrType2}) table.size $f2)
2008 (func (export "sizeE1") (result ${addrType1}) table.size $e1)
2009 (func (export "sizeE2") (result ${addrType2}) table.size $e2)
2010
2011 (func (export "initF1") (param ${addrType1} i32 i32)
2012 (table.init $f1 $fcd (local.get 0) (local.get 1) (local.get 2))
2013 )
2014 (func (export "initE1") (param ${addrType1} i32 i32)
2015 (table.init $e1 $ecd (local.get 0) (local.get 1) (local.get 2))
2016 )
2017
2018 (func (export "copyF") (param ${addrType2} ${addrType1} ${lenType})
2019 (table.copy $f2 $f1 (local.get 0) (local.get 1) (local.get 2))
2020 )
2021 (func (export "copyE") (param ${addrType2} ${addrType1} ${lenType})
2022 (table.copy $e2 $e1 (local.get 0) (local.get 1) (local.get 2))
2023 )
2024
2025 (func (export "growF1") (param ${addrType1}) (result ${addrType1})
2026 (table.grow $f1 (ref.null func) (local.get 0))
2027 )
2028 (func (export "growE1") (param ${addrType1}) (result ${addrType1})
2029 (table.grow $e1 (ref.null extern) (local.get 0))
2030 )
2031
2032 (func (export "growF2") (param ${addrType2}) (result ${addrType2})
2033 (table.grow $f2 (ref.null func) (local.get 0))
2034 )
2035 (func (export "growE2") (param ${addrType2}) (result ${addrType2})
2036 (table.grow $e2 (ref.null extern) (local.get 0))
2037 )
2038
2039 (func (export "fillF1WithA") (param ${addrType1} ${addrType1})
2040 (table.fill $f1 (local.get 0) (ref.func $a) (local.get 1))
2041 )
2042 (func (export "fillE1WithA") (param ${addrType1} ${addrType1})
2043 (table.fill $e1 (local.get 0) (global.get $ea) (local.get 1))
2044 )
2045 )`, {
2046 "": {
2047 "ea": "a",
2048 "eb": "b",
2049 "ec": "c",
2050 "ed": "d",
2051 },
2052 }).exports;
2053
2054 function addr1(n) {
2055 return addrType1 === "i64" ? BigInt(n) : n;
2056 }
2057
2058 function addr2(n) {
2059 return addrType2 === "i64" ? BigInt(n) : n;
2060 }
2061
2062 function len(n) {
2063 return lenType === "i64" ? BigInt(n) : n;
2064 }
2065
2066 function tryGrow(growFunc) {
2067 const res = growFunc();
2068 if (res < 0) {
2069 throw new RangeError(`failed table grow inside wasm (result ${res})`);
2070 }
2071 }
2072
2073 function testFuncTable(table, vals) {
2074 const actual = (table === 1 ? sizeF1 : sizeF2)();
2075 const expected = table === 1 ? addr1(vals.length) : addr2(vals.length);
2076 assertEq(actual, expected, `table $f${table} had wrong size`);
2077
2078 for (let i = 0; i < vals.length; i++) {
2079 const addr = table === 1 ? addr1(i) : addr2(i);
2080 const expected = typeof vals[i] === "string" ? vals[i].charCodeAt(0) : vals[i];
2081 const actual = (table === 1 ? callF1 : callF2)(addr);
2082 assertEq(actual, expected, `table $e${table} index ${i}`);
2083 }
2084 }
2085
2086 // "extern" is "extn" here to line up with "func" for aesthetic reasons.
2087 // I will not apologize for this.
2088 function testExtnTable(table, vals) {
2089 const actual = (table === 1 ? sizeE1 : sizeE2)();
2090 const expected = table === 1 ? addr1(vals.length) : addr2(vals.length);
2091 assertEq(actual, expected, `table $e${table} had wrong size`);
2092
2093 for (let i = 0; i < vals.length; i++) {
2094 const addr = table === 1 ? addr1(i) : addr2(i);
2095 const expected = vals[i];
2096 const actual = (table === 1 ? getE1 : getE2)(addr);
2097 assertEq(actual, expected, `table $e${table} index ${i}`);
2098 }
2099 }
2100
2101 testFuncTable(1, [0, 0, "a", "b", 0, 0, 0, 0]);
2102 testFuncTable(2, [0, 0, 0, 0, 0, 0, 0, 0]);
2103 testExtnTable(1, [null, null, "a", "b", null, null, null, null]);
2104 testExtnTable(2, [null, null, null, null, null, null, null, null]);
2105
2106 initF1(addr1(4), 0, 2);
2107 initE1(addr1(4), 0, 2);
2108
2109 testFuncTable(1, [0, 0, "a", "b", "c", "d", 0, 0]);
2110 testFuncTable(2, [0, 0, 0, 0, 0, 0, 0, 0]);
2111 testExtnTable(1, [null, null, "a", "b", "c", "d", null, null]);
2112 testExtnTable(2, [null, null, null, null, null, null, null, null]);
2113
2114 copyF(addr2(4), addr1(2), len(4));
2115 copyE(addr2(4), addr1(2), len(4));
2116
2117 testFuncTable(1, [0, 0, "a", "b", "c", "d", 0, 0]);
2118 testFuncTable(2, [0, 0, 0, 0, "a", "b", "c", "d"]);
2119 testExtnTable(1, [null, null, "a", "b", "c", "d", null, null]);
2120 testExtnTable(2, [null, null, null, null, "a", "b", "c", "d"]);
2121
2122 tryGrow(() => growF1(addr1(4)));
2123 tryGrow(() => growF2(addr2(4)));
2124 tryGrow(() => growE1(addr1(4)));
2125 tryGrow(() => growE2(addr2(4)));
2126
2127 testFuncTable(1, [0, 0, "a", "b", "c", "d", 0, 0, 0, 0, 0, 0]);
2128 testFuncTable(2, [0, 0, 0, 0, "a", "b", "c", "d", 0, 0, 0, 0]);
2129 testExtnTable(1, [null, null, "a", "b", "c", "d", null, null, null, null, null, null]);
2130 testExtnTable(2, [null, null, null, null, "a", "b", "c", "d", null, null, null, null]);
2131
2132 fillF1WithA(addr1(8), addr1(4));
2133 fillE1WithA(addr1(8), addr1(4));
2134
2135 testFuncTable(1, [0, 0, "a", "b", "c", "d", 0, 0, "a", "a", "a", "a"]);
2136 testFuncTable(2, [0, 0, 0, 0, "a", "b", "c", "d", 0, 0, 0, 0]);
2137 testExtnTable(1, [null, null, "a", "b", "c", "d", null, null, "a", "a", "a", "a"]);
2138 testExtnTable(2, [null, null, null, null, "a", "b", "c", "d", null, null, null, null]);
2139
2140 // and now explode!
2141
2142 if (addrType1 === "i64") {
2143 // JS API must use BigInt for i64
2144 assertErrorMessage(() => f1.grow(1), TypeError, /can't convert 1 to BigInt/);
2145 assertErrorMessage(() => f1.get(0), TypeError, /can't convert 0 to BigInt/);
2146 assertErrorMessage(() => f1.set(0, null), TypeError, /can't convert 0 to BigInt/);
2147 assertEq(typeof f1.length, "bigint");
2148 }
2149
2150 const indexes = [
2151 -1,
2152 11, // length is 12, so test around the boundary
2153 12,
2154 13,
2155 0 + (MaxUint32 + 1),
2156 1 + (MaxUint32 + 1),
2157 2 + (MaxUint32 + 1),
2158 Number.MAX_SAFE_INTEGER,
2159 Number.MAX_SAFE_INTEGER + 1,
2160 ];
2161 if (addrType1 === "i64") {
2162 for (const i of indexes) {
2163 assertErrorMessage(
2164 () => fillF1WithA(addr1(i), addr1(2)),
2165 WebAssembly.RuntimeError, /index out of bounds/,
2166 `start index ${i}`,
2167 );
2168 assertErrorMessage(
2169 () => fillE1WithA(addr1(i), addr1(2)),
2170 WebAssembly.RuntimeError, /index out of bounds/,
2171 `start index ${i}`,
2172 );
2173
2174 assertErrorMessage(
2175 () => copyF(addr2(0), addr1(i), len(2)),
2176 WebAssembly.RuntimeError, /index out of bounds/,
2177 `src index ${i}`,
2178 );
2179 assertErrorMessage(
2180 () => copyE(addr2(0), addr1(i), len(2)),
2181 WebAssembly.RuntimeError, /index out of bounds/,
2182 `src index ${i}`,
2183 );
2184
2185 assertErrorMessage(
2186 () => initF1(addr1(i), 0, 2),
2187 WebAssembly.RuntimeError, /index out of bounds/,
2188 `dst index ${i}`,
2189 );
2190 assertErrorMessage(
2191 () => initE1(addr1(i), 0, 2),
2192 WebAssembly.RuntimeError, /index out of bounds/,
2193 `dst index ${i}`,
2194 );
2195 }
2196 }
2197 if (addrType2 === "i64") {
2198 for (const i of indexes) {
2199 assertErrorMessage(
2200 () => copyF(addr2(i), addr1(0), len(2)),
2201 WebAssembly.RuntimeError, /index out of bounds/,
2202 `dst index ${i}`,
2203 );
2204 assertErrorMessage(
2205 () => copyE(addr2(i), addr1(0), len(2)),
2206 WebAssembly.RuntimeError, /index out of bounds/,
2207 `dst index ${i}`,
2208 );
2209 }
2210 }
2211
2212 const maxDelta = MaxTableElemsRuntime - 12;
2213 assertEq(maxDelta % 2, 0, "maxDelta needs to be even for this test to work");
2214
2215 try {
2216 // Grow the tables up to max size using both the instruction and the JS API
2217 tryGrow(() => growF1(addr1(maxDelta / 2)));
2218 tryGrow(() => growF2(addr2(maxDelta / 2)));
2219 tryGrow(() => growE1(addr1(maxDelta / 2)));
2220 tryGrow(() => growE2(addr2(maxDelta / 2)));
2221 f1.grow(addr1(maxDelta / 2), null);
2222 f2.grow(addr2(maxDelta / 2), null);
2223 e1.grow(addr1(maxDelta / 2), null);
2224 e2.grow(addr2(maxDelta / 2), null);
2225
2226 assertEq(sizeF1(), addr1(MaxTableElemsRuntime));
2227 assertEq(sizeF2(), addr2(MaxTableElemsRuntime));
2228 assertEq(sizeE1(), addr1(MaxTableElemsRuntime));
2229 assertEq(sizeE2(), addr2(MaxTableElemsRuntime));
2230
2231 for (const delta of indexes) {
2232 if (addrType1 === "i64") {
2233 console.log(delta);
2234 assertEq(growF1(addr1(delta)), addr1(-1), `growing by ${delta}`);
2235 assertEq(growE1(addr1(delta)), addr1(-1), `growing by ${delta}`);
2236 assertErrorMessage(() => f1.grow(addr1(delta), null), Error, /grow/); // Loose to accept either TypeError or RangeError
2237 assertErrorMessage(() => e1.grow(addr1(delta), null), Error, /grow/);
2238 }
2239 if (addrType2 === "i64") {
2240 assertEq(growF2(addr2(delta)), addr2(-1), `growing by ${delta}`);
2241 assertEq(growE2(addr2(delta)), addr2(-1), `growing by ${delta}`);
2242 assertErrorMessage(() => f2.grow(addr2(delta), null), Error, /grow/);
2243 assertErrorMessage(() => e2.grow(addr2(delta), null), Error, /grow/);
2244 }
2245 }
2246
2247 assertEq(sizeF1(), addr1(MaxTableElemsRuntime));
2248 assertEq(sizeF2(), addr2(MaxTableElemsRuntime));
2249 assertEq(sizeE1(), addr1(MaxTableElemsRuntime));
2250 assertEq(sizeE2(), addr2(MaxTableElemsRuntime));
2251 } catch (e) {
2252 if (e instanceof RangeError) {
2253 // This can happen due to resource exhaustion on some platforms and is not worth
2254 // failing the whole test over.
2255 print("WARNING: Failed to test all cases of table grow.", e);
2256 } else {
2257 throw e;
2258 }
2259 }
2260 }