Debian Codegraph @

js/src/jit-test/tests/wasm/exceptions/calls.js

raw
1 // -----------------------------------------------------------------------------
2 // This file contains tests checking Wasm functions with throwing functions and
3 // try-catch code involving more complex control flow, testing that multiple
4 // values returned from calls in try code are not affected by multiple branching
5 // towards the landing pad, as well as making sure exceptions carrying multiple
6 // values of any Wasm numtype transport the exception values correctly across
7 // calls.
8 //
9 // There are tests for local direct calls, for imported direct calls, for
10 // indirect calls in a local table with local functions, for indirect calls in a
11 // local table of imported functions, and for indirect calls in an imported
12 // table of imported functions.
13 //
14 // - TODO: Add reftype values, when support for reftypes in exceptions is
15 // implemented.
16 // -----------------------------------------------------------------------------
17
18 load(libdir + "eqArrayHelper.js");
19
20 // All individual tests take a string 'localThrow' as argument, and will be run
21 // with each element of the result of the following function.
22
23 function generateLocalThrows(types, baseThrow) {
24 // Arguments:
25 // - 'types': A string of space separated Wasm types.
26 // - 'baseThrow': A string with a Wasm instruction sequence of Wasm functype
27 // `[${types}]-> [t*], which takes `types` arguments and ends
28 // up throwing the tag '$exn'.
29 // Result:
30 // - A JS array of strings, each representing a Wasm instruction sequence
31 // which is like `baseThrow', i.e., a Wasm instruction sequence of Wasm
32 // functype `[${types}]-> [t*], which takes `types` arguments and ends up
33 // throwing the tag '$exn'. The result does not include 'baseThrow'.
34 //
35 // All strings in Wasm text format.
36
37 // Basic throws;
38 let catchlessTryThrow =
39 `try (param ${types})
40 ${baseThrow}
41 end`;
42
43 let catchlessThrowExnref =
44 `try_table (param ${types})
45 ${baseThrow}
46 end`;
47
48 let catchAndThrow =
49 `try (param ${types})
50 ${baseThrow}
51 catch $exn
52 ${baseThrow}
53 catch_all
54 end`;
55
56 let catchAndThrowExnref =
57 `(block $join (param ${types})
58 (block $catch (param ${types}) (result ${types})
59 (block $catchAll (param ${types})
60 try_table (param ${types}) (catch $exn $catch) (catch_all $catchAll)
61 ${baseThrow}
62 unreachable
63 end
64 )
65 br $join
66 )
67 ${baseThrow}
68 )`;
69
70 let blockThrow =
71 `(block (param ${types})
72 ${baseThrow})`;
73
74 // This Wasm code requires that the function it appears in has an i32 local
75 // with name "$ifPredicate".
76 let conditionalThrow =
77 `(if (param ${types})
78 (local.get $ifPredicate)
79 (then ${baseThrow})
80 (else ${baseThrow}))`;
81
82 // Including try-delegate.
83 let baseDelegate =
84 `try (param ${types})
85 ${baseThrow}
86 delegate 0`;
87
88 // Delegate just outside the block.
89 let nestedDelegate1InBlock =
90 `(block $label1 (param ${types})
91 try (param ${types})
92 ${baseThrow}
93 delegate $label1)`;
94
95 let basicThrows = [catchlessTryThrow, blockThrow, conditionalThrow,
96 baseDelegate, nestedDelegate1InBlock];
97 if (wasmExnRefEnabled()) {
98 basicThrows = basicThrows.concat(catchlessThrowExnref, catchAndThrowExnref);
99 }
100
101 // Secondary throws (will end up inside a catch block).
102
103 let baseRethrow =
104 `(rethrow 0)`;
105
106 let nestedRethrow =
107 `try (param ${types})
108 ${baseThrow}
109 catch $exn
110 (rethrow 1)
111 catch_all
112 (rethrow 0)
113 end`;
114
115 let catchAllRethrowOriginal =
116 `try (param ${types})
117 ${baseThrow}
118 catch_all
119 (rethrow 1)
120 end`;
121
122 let secondaryThrows =
123 [].concat(basicThrows,
124 [baseRethrow, nestedRethrow, catchAllRethrowOriginal]);
125
126 // Nestings.
127
128 function basicNesting (basicThrow, secondaryThrow) {
129 return `try (param ${types})
130 ${basicThrow}
131 catch $exn
132 ${secondaryThrow}
133 catch_all
134 end`;
135 };
136
137 let result = [];
138
139 for (let basicThrow of basicThrows) {
140 result.push(basicThrow);
141 let isExnref = basicThrow == catchlessThrowExnref || basicThrow == catchAndThrowExnref;
142 for (let secondaryThrow of secondaryThrows) {
143 let isRethrow = secondaryThrow == baseRethrow || secondaryThrow == nestedRethrow || secondaryThrow == catchAllRethrowOriginal;
144 if (isExnref && isRethrow) {
145 continue;
146 }
147 result.push(basicNesting(basicThrow, secondaryThrow));
148 }
149 }
150
151 return result;
152 };
153
154 {
155 // Some variables to be used in all tests.
156 let typesJS = ["i32", "i64", "f32", "f64", "externref"];
157 let types = typesJS.join(" ");
158
159 // The following depend on whether simd is enabled or not. We write it like
160 // this so we can run this test also when SIMD is not enabled.
161 let exntype = "";
162 let wrongV128 = "";
163 let throwV128 = "";
164 let checkV128Value = "";
165
166 if (wasmSimdEnabled()) {
167 exntype = types + " v128";
168 wrongV128 = `(v128.const i32x4 11 22 33 44)`;
169 throwV128 = `(v128.const i32x4 55 66 77 88)`;
170 checkV128Value = `;; Check the V128 value
171 ${throwV128}
172 (i32x4.eq)
173 (i32x4.all_true)`;
174 } else {
175 exntype = types + " i32";
176 wrongV128 = "(i32.const 0)";
177 throwV128 = "(i32.const 1)";
178 checkV128Value = "";
179 }
180
181 let exnTypeDef = `(type $exnType (func (param ${exntype})))`;
182
183 let throwValues =
184 `;; Values to throw.
185 (i32.const 2)
186 (i64.const 3)
187 (f32.const 4)
188 (f64.const 13.37)
189 (local.get $correctRef)
190 ${throwV128}`;
191
192 // The last 1 is the result of the test that the v128 value is correct, done
193 // in wasm code (if simd is enabled).
194 let correctResultsJS = [2, 3n, 4, 13.37, "foo", 1];
195
196 let wrongValues =
197 `;; Wrong values
198 (i32.const 5)
199 (i64.const 6)
200 (f32.const 0.1)
201 (f64.const 0.6437)
202 (local.get $wrongRef)
203 ${wrongV128}`;
204
205 // The individual tests. -----------------------------------------------------
206
207 function testDirectCallsThrowing(localThrow) {
208 // Test direct function calls throwing any numeric value.
209
210 let throwifTypeInline =
211 // The result of the "throwif" function will be used as an argument the
212 // second time "throwif" is called.
213 `(param $ifPredicate i32) (param $correctRef externref) (result i32)`;
214
215 let moduleHeaderThrowif =
216 `(module
217 ${exnTypeDef}
218 (tag $exn (export "exn") (type $exnType))
219 (func $throwif (export "throwif") ${throwifTypeInline}
220 (if
221 (local.get $ifPredicate)
222 (then
223 ${throwValues}
224 ${localThrow}))
225 (i32.const 1))`;
226
227 let testModuleRest =
228 `(tag $notThrownExn)
229 (func $doNothing)
230 (func (export "testFunc") (param $correctRef externref)
231 (param $wrongRef externref)
232 (result ${types} i32)
233 (local $ifPredicate i32)
234 (local.get $ifPredicate)
235 try (param i32) (result ${exntype})
236 (local.get $wrongRef)
237 (call $throwif) ;; Returns 1.
238 (call $doNothing) ;; Does nothing.
239 (local.get $correctRef)
240 (call $throwif) ;; Throws $exn.
241 (drop)
242 ${wrongValues} ;; Won't reach this point.
243 ${localThrow}
244 unreachable
245 catch $notThrownExn
246 ${wrongValues}
247 catch $exn
248 end
249 ${checkV128Value}))`;
250
251 function testDirectLocalCallsThrowing() {
252 let mod = moduleHeaderThrowif + testModuleRest;
253 // console.log("DIRECT LOCAL MOD = " + mod); // Uncomment for debugging.
254
255 assertEqArray(wasmEvalText(mod).exports.testFunc("foo", "bar"),
256 correctResultsJS);
257 };
258
259 function testDirectImportedCallsThrowing() {
260 let exports = wasmEvalText(moduleHeaderThrowif + `)`).exports;
261 // Uncomment for debugging.
262 //console.log("DIRECT EXPORTS = " + moduleHeaderThrowif + ")");
263
264 let mod =
265 `(module
266 ${exnTypeDef}
267 (import "m" "exn" (tag $exn (type $exnType)))
268 (import "m" "throwif" (func $throwif ${throwifTypeInline}))` +
269 testModuleRest;
270 // console.log("DIRECT IMPORT MOD = " + mod); // Uncomment for debugging.
271
272 assertEqArray(
273 wasmEvalText(mod, { m : exports}).exports.testFunc("foo", "bar"),
274 correctResultsJS);
275 };
276
277 testDirectLocalCallsThrowing();
278 testDirectImportedCallsThrowing();
279 };
280
281 function testIndirectCallsThrowing(localThrow) {
282 // Test indirect calls throwing exceptions.
283
284 let indirectFunctypeInline = `(param ${exntype})
285 (result ${exntype})`;
286 let getIndirectArgs = `(local.get 0) ;; i32
287 (local.get 1) ;; i64
288 (local.get 2) ;; f32
289 (local.get 3) ;; f64
290 (local.get 4) ;; ref
291 ;; v128
292 (local.get 5)`;
293
294 let testFunctypeInline = `(param $correctRef externref)
295 (param $wrongRef externref)
296 ;; The last i32 result is the v128 check.
297 (result ${types} i32)`;
298
299 let moduleHeader =
300 `(module
301 ${exnTypeDef}
302 (type $indirectFunctype (func ${indirectFunctypeInline}))
303 (tag $exn (export "exn") (type $exnType))
304 (tag $emptyExn (export "emptyExn"))
305 (func $throwExn (export "throwExn") ${indirectFunctypeInline}
306 (local $ifPredicate i32)
307 ${getIndirectArgs}
308 ${localThrow}
309 unreachable)
310 (func $throwEmptyExn (export "throwEmptyExn")
311 ${indirectFunctypeInline}
312 (throw $emptyExn)
313 unreachable)
314 (func $returnArgs (export "returnArgs") ${indirectFunctypeInline}
315 ${getIndirectArgs})
316 (table (export "tab") funcref (elem $throwExn ;; 0
317 $throwEmptyExn ;; 1
318 $returnArgs)) ;; 2
319 `;
320
321 // The main test function happens to have the same Wasm functype as the
322 // indirect calls.
323 let testFuncHeader = `(func (export "testFunc") ${testFunctypeInline}
324 (local $ifPredicate i32)
325 `;
326
327 // To test indirect calls to a local table of local functions
328 function moduleIndirectLocalLocal(functionBody) {
329 return moduleHeader + testFuncHeader + functionBody + `))`;
330 };
331
332 let exports = wasmEvalText(moduleHeader + ")").exports;
333 // Uncomment for debugging.
334 //console.log("INDIRECT EXPORTS = " + moduleHeader + ")");
335
336 let moduleHeaderImporting =
337 `(module
338 ${exnTypeDef}
339 (type $indirectFunctype (func ${indirectFunctypeInline}))
340 (import "m" "exn" (tag $exn (type $exnType)))
341 (import "m" "emptyExn" (tag $emptyExn))
342 (import "m" "throwExn" (func $throwExn (type $indirectFunctype)))
343 (import "m" "throwEmptyExn"
344 (func $throwEmptyExn (type $indirectFunctype)))
345 (import "m" "returnArgs"
346 (func $returnArgs (type $indirectFunctype)))`;
347
348 // To test indirect calls to a local table of imported functions.
349 function moduleIndirectLocalImport(functionBody) {
350 return moduleHeaderImporting +
351 `(table funcref (elem $throwExn $throwEmptyExn $returnArgs))` +
352 testFuncHeader + functionBody + `))`;
353 };
354
355 // To test indirect calls to an imported table of imported functions.
356 function moduleIndirectImportImport(functionBody) {
357 return moduleHeaderImporting +
358 `(import "m" "tab" (table 3 funcref))` +
359 testFuncHeader + functionBody + `))`;
360 };
361
362 function getModuleTextsForFunctionBody(functionBody) {
363 return [moduleIndirectLocalLocal(functionBody),
364 moduleIndirectLocalImport(functionBody),
365 moduleIndirectImportImport(functionBody)];
366 };
367
368 // The function bodies for the tests.
369
370 // Three indirect calls, the middle of which throws and will be caught. The
371 // results of the first and second indirect calls are used by the next
372 // indirect call. This should be called from try code, to check that the
373 // pad-branches don't interfere with the results of each call.
374 let indirectThrow = `${throwValues}
375 (call_indirect (type $indirectFunctype) (i32.const 2)) ;; returnArgs
376 (call_indirect (type $indirectFunctype) (i32.const 0)) ;; throwExn
377 drop drop ;; Drop v128 and externref to do trivial and irrelevant ops.
378 (f64.const 5)
379 (f64.add)
380 (local.get $wrongRef)
381 ${wrongV128}
382 ;; throwEmptyExn
383 (call_indirect (type $indirectFunctype) (i32.const 1))
384 unreachable`;
385
386 // Simple try indirect throw and catch.
387 let simpleTryIndirect = `
388 try (result ${exntype})
389 ${indirectThrow}
390 catch $emptyExn
391 ${wrongValues}
392 catch $exn
393 catch_all
394 ${wrongValues}
395 end
396 ${checkV128Value}`;
397
398 // Indirect throw/catch_all, with a simple try indirect throw nested in the
399 // catch_all.
400 let nestedTryIndirect =
401 `try (result ${types} i32)
402 ${wrongValues}
403 ;; throwEmptyExn
404 (call_indirect (type $indirectFunctype) (i32.const 1))
405 drop ;; Drop the last v128 value.
406 (i32.const 0)
407 catch_all
408 ${simpleTryIndirect}
409 end`;
410
411 let functionBodies = [simpleTryIndirect, nestedTryIndirect];
412
413 // Test throwing from indirect calls.
414 for (let functionBody of functionBodies) {
415 // console.log("functionBody = : " + functionBody); // Uncomment for debugging.
416
417 for (let mod of getModuleTextsForFunctionBody(functionBody)) {
418 //console.log("mod = : " + mod); // Uncomment for debugging.
419
420 let testFunction = wasmEvalText(mod, { m : exports}).exports.testFunc;
421 assertEqArray(testFunction("foo", "bar"),
422 correctResultsJS);
423 }
424 }
425 };
426
427 // Run all tests. ------------------------------------------------------------
428
429 let localThrows =
430 ["(throw $exn)"].concat(generateLocalThrows(exntype, "(throw $exn)"));
431
432 for (let localThrow of localThrows) {
433 // Uncomment for debugging.
434 // console.log("Testing with localThrow = " + localThrow);
435
436 testDirectCallsThrowing(localThrow);
437 testIndirectCallsThrowing(localThrow);
438 }
439 }