| 1 |
// |jit-test| test-also=--setpref=wasm_tail_calls=true |
| 2 |
|
| 3 |
// Test that call_indirect will respect subtyping by defining a bunch of types |
| 4 |
// and checking every combination of (expected, actual) type. |
| 5 |
// |
| 6 |
// NOTE: Several of these types are identical to each other to test |
| 7 |
// canonicalization as well, and this causes some bloat in the 'subtypeOf' |
| 8 |
// lists. |
| 9 |
const TESTS = [ |
| 10 |
{ |
| 11 |
// (type 0) (equivalent to 1) |
| 12 |
type: `(type (sub (func)))`, |
| 13 |
subtypeOf: [0, 1], |
| 14 |
}, |
| 15 |
{ |
| 16 |
// (type 1) (equivalent to 0) |
| 17 |
type: `(rec (type (sub (func))))`, |
| 18 |
subtypeOf: [0, 1], |
| 19 |
}, |
| 20 |
{ |
| 21 |
// (type 2) |
| 22 |
type: `(rec (type (sub (func))) (type (sub (func))))`, |
| 23 |
subtypeOf: [2], |
| 24 |
}, |
| 25 |
{ |
| 26 |
// (type 3) |
| 27 |
// Hack entry of previous to capture that it actually defines |
| 28 |
// two types in the recursion group. |
| 29 |
type: undefined, |
| 30 |
subtypeOf: [3], |
| 31 |
}, |
| 32 |
{ |
| 33 |
// (type 4) (equivalent to 7) |
| 34 |
type: `(type (sub 0 (func)))`, |
| 35 |
subtypeOf: [0, 1, 4, 7], |
| 36 |
}, |
| 37 |
{ |
| 38 |
// (type 5) (equivalent to 8) |
| 39 |
type: `(type (sub 4 (func)))`, |
| 40 |
subtypeOf: [0, 1, 4, 5, 7, 8], |
| 41 |
}, |
| 42 |
{ |
| 43 |
// (type 6) |
| 44 |
type: `(type (sub 5 (func)))`, |
| 45 |
subtypeOf: [0, 1, 4, 5, 6, 7, 8], |
| 46 |
}, |
| 47 |
{ |
| 48 |
// (type 7) (equivalent to 4) |
| 49 |
type: `(type (sub 0 (func)))`, |
| 50 |
subtypeOf: [0, 1, 4, 7], |
| 51 |
}, |
| 52 |
{ |
| 53 |
// (type 8) (equivalent to 5) |
| 54 |
type: `(type (sub 7 (func)))`, |
| 55 |
subtypeOf: [0, 1, 4, 5, 7, 8], |
| 56 |
}, |
| 57 |
{ |
| 58 |
// (type 9) - a final type that has an immediate form |
| 59 |
type: `(type (func))`, |
| 60 |
subtypeOf: [9], |
| 61 |
} |
| 62 |
]; |
| 63 |
|
| 64 |
// Build a module with all the types, functions with those types, and functions |
| 65 |
// that call_indirect with those types, and a table with all the functions in |
| 66 |
// it. |
| 67 |
let typeSection = ''; |
| 68 |
let importedFuncs = ''; |
| 69 |
let definedFuncs = ''; |
| 70 |
let callIndirectFuncs = ''; |
| 71 |
let returnCallIndirectFuncs = ''; |
| 72 |
let i = 0; |
| 73 |
for (let {type} of TESTS) { |
| 74 |
if (type) { |
| 75 |
typeSection += type + '\n'; |
| 76 |
} |
| 77 |
importedFuncs += `(func \$import${i} (import "" "import${i}") (type ${i}))\n`; |
| 78 |
definedFuncs += `(func \$define${i} (export "define${i}") (type ${i}))\n`; |
| 79 |
callIndirectFuncs += `(func (export "call_indirect ${i}") (param i32) |
| 80 |
(drop (ref.cast (ref ${i}) (table.get local.get 0))) |
| 81 |
(call_indirect (type ${i}) local.get 0) |
| 82 |
)\n`; |
| 83 |
returnCallIndirectFuncs += `(func (export "return_call_indirect ${i}") (param i32) |
| 84 |
(drop (ref.cast (ref ${i}) (table.get local.get 0))) |
| 85 |
(return_call_indirect (type ${i}) local.get 0) |
| 86 |
)\n`; |
| 87 |
i++; |
| 88 |
} |
| 89 |
let moduleText = `(module |
| 90 |
${typeSection} |
| 91 |
${importedFuncs} |
| 92 |
${definedFuncs} |
| 93 |
${callIndirectFuncs} |
| 94 |
${returnCallIndirectFuncs} |
| 95 |
(table |
| 96 |
(export "table") |
| 97 |
funcref |
| 98 |
(elem ${TESTS.map((x, i) => `\$import${i} \$define${i}`).join(" ")}) |
| 99 |
) |
| 100 |
)`; |
| 101 |
|
| 102 |
// Now go over every combination of (actual, expected). In this case the caller |
| 103 |
// (which does the call_indirect) specifies expected and the callee will be the |
| 104 |
// actual. |
| 105 |
let imports = { |
| 106 |
"": Object.fromEntries(TESTS.map((x, i) => [`import${i}`, () => {}])), |
| 107 |
}; |
| 108 |
let exports = wasmEvalText(moduleText, imports).exports; |
| 109 |
for (let callerTypeIndex = 0; callerTypeIndex < TESTS.length; callerTypeIndex++) { |
| 110 |
for (let calleeTypeIndex = 0; calleeTypeIndex < TESTS.length; calleeTypeIndex++) { |
| 111 |
let calleeType = TESTS[calleeTypeIndex]; |
| 112 |
|
| 113 |
// If the callee (actual) is a subtype of caller (expected), then this |
| 114 |
// should succeed. |
| 115 |
let shouldPass = calleeType.subtypeOf.includes(callerTypeIndex); |
| 116 |
|
| 117 |
let calleeImportFuncIndex = calleeTypeIndex * 2; |
| 118 |
let calleeDefinedFuncIndex = calleeTypeIndex * 2 + 1; |
| 119 |
|
| 120 |
// print(`expected (type ${callerTypeIndex}) (actual ${calleeTypeIndex})`); |
| 121 |
let test = () => { |
| 122 |
exports[`call_indirect ${callerTypeIndex}`](calleeImportFuncIndex) |
| 123 |
exports[`call_indirect ${callerTypeIndex}`](calleeDefinedFuncIndex) |
| 124 |
exports[`return_call_indirect ${callerTypeIndex}`](calleeImportFuncIndex) |
| 125 |
exports[`return_call_indirect ${callerTypeIndex}`](calleeDefinedFuncIndex) |
| 126 |
}; |
| 127 |
if (shouldPass) { |
| 128 |
test(); |
| 129 |
} else { |
| 130 |
assertErrorMessage(test, WebAssembly.RuntimeError, /mismatch|cast/); |
| 131 |
} |
| 132 |
} |
| 133 |
} |