| 1 |
// |jit-test| skip-if: getPrefValue("wasm_lazy_tiering") |
| 2 |
// TODO: skip lazy tiering temporarily to avoid a timeout |
| 3 |
|
| 4 |
load(libdir + "wasm-binary.js"); |
| 5 |
|
| 6 |
// Replace this with a string like 'non-null (ref $s1) -> (ref any) -> (ref any)' to test exactly one specific case. Makes debugging easier. |
| 7 |
const specificTest = ''; |
| 8 |
|
| 9 |
const preamble = ` |
| 10 |
(type $s1 (sub (struct))) |
| 11 |
(type $s2 (sub $s1 (struct (field i32)))) |
| 12 |
(type $a1 (sub (array (ref null $s1)))) |
| 13 |
(type $a2 (sub $a1 (array (ref null $s2)))) |
| 14 |
(type $ft1 (sub (func (param (ref $s1)) (result (ref null $a1))))) |
| 15 |
(type $ft2 (sub $ft1 (func (param (ref null $s1)) (result (ref null $a2))))) |
| 16 |
(type $ft3 (sub (func (param (ref $a2)) (result i32)))) |
| 17 |
(type $ft4 (sub $ft3 (func (param (ref $a1)) (result i32)))) |
| 18 |
|
| 19 |
(func $f1 (type $ft1) ref.null $a1) |
| 20 |
(func $f2 (type $ft2) ref.null $a2) |
| 21 |
(func $f3 (type $ft3) i32.const 0) |
| 22 |
(func $f4 (type $ft4) i32.const 0) |
| 23 |
(elem declare func $f1 $f2 $f3 $f4) ;; allow funcs to be ref.func'd |
| 24 |
`; |
| 25 |
|
| 26 |
// All the concrete subtype relationships present in the preamble. |
| 27 |
// [x, y] means x <: y. |
| 28 |
const subtypes = [ |
| 29 |
['$s2', '$s1'], |
| 30 |
['$a2', '$a1'], |
| 31 |
['$ft2', '$ft1'], |
| 32 |
['$ft4', '$ft3'], |
| 33 |
]; |
| 34 |
|
| 35 |
const any = { name: 'any', top: 'anyref' }; |
| 36 |
const eq = { name: 'eq', top: 'anyref' }; |
| 37 |
const struct = { name: 'struct', top: 'anyref' }; |
| 38 |
const array = { name: 'array', top: 'anyref' }; |
| 39 |
const s1 = { |
| 40 |
index: 0, |
| 41 |
name: '$s1', |
| 42 |
make: 'struct.new_default $s1', |
| 43 |
top: 'anyref', |
| 44 |
}; |
| 45 |
const s2 = { |
| 46 |
index: 1, |
| 47 |
name: '$s2', |
| 48 |
make: 'struct.new_default $s2', |
| 49 |
top: 'anyref', |
| 50 |
}; |
| 51 |
const a1 = { |
| 52 |
index: 2, |
| 53 |
name: '$a1', |
| 54 |
make: '(array.new_default $a1 (i32.const 10))', |
| 55 |
top: 'anyref', |
| 56 |
}; |
| 57 |
const a2 = { |
| 58 |
index: 3, |
| 59 |
name: '$a2', |
| 60 |
make: '(array.new_default $a2 (i32.const 10))', |
| 61 |
top: 'anyref', |
| 62 |
}; |
| 63 |
const i31 = { name: 'i31', make: '(ref.i31 (i32.const 123))', top: 'anyref' }; |
| 64 |
const none = { name: 'none', none: true, top: 'anyref' }; |
| 65 |
|
| 66 |
const func = { name: 'func', top: 'funcref' }; |
| 67 |
const ft1 = { index: 4, name: '$ft1', make: 'ref.func $f1', top: 'funcref' }; |
| 68 |
const ft2 = { index: 5, name: '$ft2', make: 'ref.func $f2', top: 'funcref' }; |
| 69 |
const ft3 = { index: 6, name: '$ft3', make: 'ref.func $f3', top: 'funcref' }; |
| 70 |
const ft4 = { index: 7, name: '$ft4', make: 'ref.func $f4', top: 'funcref' }; |
| 71 |
const nofunc = { name: 'nofunc', none: true, top: 'funcref' }; |
| 72 |
|
| 73 |
const extern = { name: 'extern', make: '(extern.convert_any (struct.new_default $s1))', top: 'externref' } |
| 74 |
const noextern = { name: 'noextern', none: true, top: 'externref' } |
| 75 |
|
| 76 |
const typeSets = [ |
| 77 |
[any, eq, struct, s1, s2, none], |
| 78 |
[any, eq, array, a1, a2, none], |
| 79 |
[any, eq, s1, s2, a1, a2, none], |
| 80 |
[any, eq, i31, none], |
| 81 |
[any, eq, i31, s1, none], |
| 82 |
[any, eq, i31, a1, none], |
| 83 |
|
| 84 |
[func, ft1, ft2, nofunc], |
| 85 |
[func, ft3, ft4, nofunc], |
| 86 |
[func, ft1, ft2, ft3, ft4, nofunc], |
| 87 |
|
| 88 |
[extern, noextern], |
| 89 |
]; |
| 90 |
|
| 91 |
const nullables = [ // for example: |
| 92 |
[true, true, true], // null $s1 -> null any -> null eq |
| 93 |
[true, true, false], // null $s1 -> null any -> eq |
| 94 |
[true, false, true], // null $s1 -> any -> null eq |
| 95 |
[true, false, false], // null $s1 -> any -> eq |
| 96 |
[false, true, true], // $s1 -> null any -> null eq |
| 97 |
[false, true, false], // $s1 -> null any -> eq |
| 98 |
[false, false, true], // $s1 -> any -> null eq |
| 99 |
[false, false, false], // $s1 -> any -> eq |
| 100 |
] |
| 101 |
|
| 102 |
function isSubtype(src, dest) { |
| 103 |
if (src.name === dest.name) { |
| 104 |
return true; |
| 105 |
} |
| 106 |
for (const [src2, dest2] of subtypes) { |
| 107 |
if (src.name === src2 && dest.name === dest2) { |
| 108 |
return true; |
| 109 |
} |
| 110 |
} |
| 111 |
return false; |
| 112 |
} |
| 113 |
|
| 114 |
let numCases = 0; |
| 115 |
|
| 116 |
// This will generate an enormous pile of test cases. All of these should be valid, |
| 117 |
// as in passing WebAssembly.validate, but some may not be good casts at runtime. |
| 118 |
for (const typeSet of typeSets) { |
| 119 |
for (const start of typeSet) { |
| 120 |
for (const middle of typeSet) { |
| 121 |
for (const end of typeSet) { |
| 122 |
for (const [nullable0, nullable1, nullable2] of nullables) { |
| 123 |
for (const makeNull of [true, false]) { |
| 124 |
const concrete0 = !!start.make; |
| 125 |
const concrete1 = !!middle.make; |
| 126 |
const concrete2 = !!end.make; |
| 127 |
|
| 128 |
if (!concrete0 && !makeNull) { |
| 129 |
// We can only start with null values for abstract types |
| 130 |
continue; |
| 131 |
} |
| 132 |
|
| 133 |
if (!nullable0 && makeNull) { |
| 134 |
// Can't use null as a valid value for a non-nullable type |
| 135 |
continue; |
| 136 |
} |
| 137 |
|
| 138 |
numCases += 1; |
| 139 |
|
| 140 |
let good1 = true; |
| 141 |
let good2 = true; |
| 142 |
|
| 143 |
// Null values will fail casts to non-nullable types |
| 144 |
if (makeNull) { |
| 145 |
if (!nullable1) { |
| 146 |
good1 = false; |
| 147 |
} |
| 148 |
if (!nullable2) { |
| 149 |
good2 = false; |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
// Bottom types can't represent non-null, so this will always fail |
| 154 |
if (!makeNull) { |
| 155 |
if (middle.none) { |
| 156 |
good1 = false; |
| 157 |
} |
| 158 |
if (end.none) { |
| 159 |
good2 = false; |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
// Concrete values are subject to subtyping relationships |
| 164 |
if (!makeNull) { |
| 165 |
if (concrete1 && !isSubtype(start, middle)) { |
| 166 |
good1 = false; |
| 167 |
} |
| 168 |
if (concrete2 && !isSubtype(start, end)) { |
| 169 |
good2 = false; |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
let emoji1 = good1 ? '✅' : '❌'; |
| 174 |
let emoji2 = good2 ? '✅' : '❌'; |
| 175 |
|
| 176 |
if (!good1) { |
| 177 |
good2 = false; |
| 178 |
emoji2 = '❓'; |
| 179 |
} |
| 180 |
|
| 181 |
const name = `${makeNull ? 'null' : 'non-null'} (ref ${nullable0 ? 'null ' : ''}${start.name}) -> (ref ${nullable1 ? 'null ' : ''}${middle.name}) -> (ref ${nullable2 ? 'null ' : ''}${end.name})`; |
| 182 |
if (specificTest && name != specificTest) { |
| 183 |
continue; |
| 184 |
} |
| 185 |
|
| 186 |
print(`${emoji1}${emoji2} ${name}`); |
| 187 |
const make = makeNull ? `ref.null ${start.name}` : start.make; |
| 188 |
const type0 = `(ref ${nullable0 ? 'null ' : ''}${start.name})`; |
| 189 |
const type1 = `(ref ${nullable1 ? 'null ' : ''}${middle.name})`; |
| 190 |
const type2 = `(ref ${nullable2 ? 'null ' : ''}${end.name})`; |
| 191 |
const moduleText = `(module |
| 192 |
${preamble} |
| 193 |
(func (export "make") (result ${type0}) |
| 194 |
${make} |
| 195 |
) |
| 196 |
(func (export "cast1") (param ${type0}) (result ${type1}) |
| 197 |
local.get 0 |
| 198 |
ref.cast ${type1} |
| 199 |
) |
| 200 |
(func (export "cast2") (param ${type1}) (result ${type2}) |
| 201 |
local.get 0 |
| 202 |
ref.cast ${type2} |
| 203 |
) |
| 204 |
|
| 205 |
(func (export "test1") (param ${type0}) (result i32) |
| 206 |
local.get 0 |
| 207 |
ref.test ${type1} |
| 208 |
) |
| 209 |
(func (export "test2") (param ${type1}) (result i32) |
| 210 |
local.get 0 |
| 211 |
ref.test ${type2} |
| 212 |
) |
| 213 |
|
| 214 |
;; these are basically ref.test but with branches |
| 215 |
(func (export "branch1") (param ${type0}) (result i32) |
| 216 |
(block (result ${type1}) |
| 217 |
local.get 0 |
| 218 |
br_on_cast 0 ${start.top} ${type1} |
| 219 |
drop |
| 220 |
(return (i32.const 0)) |
| 221 |
) |
| 222 |
drop |
| 223 |
(return (i32.const 1)) |
| 224 |
) |
| 225 |
(func (export "branch2") (param ${type1}) (result i32) |
| 226 |
(block (result ${type2}) |
| 227 |
local.get 0 |
| 228 |
br_on_cast 0 ${middle.top} ${type2} |
| 229 |
drop |
| 230 |
(return (i32.const 0)) |
| 231 |
) |
| 232 |
drop |
| 233 |
(return (i32.const 1)) |
| 234 |
) |
| 235 |
(func (export "branchfail1") (param ${type0}) (result i32) |
| 236 |
(block (result ${middle.top}) |
| 237 |
local.get 0 |
| 238 |
br_on_cast_fail 0 ${start.top} ${type1} |
| 239 |
drop |
| 240 |
(return (i32.const 1)) |
| 241 |
) |
| 242 |
drop |
| 243 |
(return (i32.const 0)) |
| 244 |
) |
| 245 |
(func (export "branchfail2") (param ${type1}) (result i32) |
| 246 |
(block (result ${end.top}) |
| 247 |
local.get 0 |
| 248 |
br_on_cast_fail 0 ${middle.top} ${type2} |
| 249 |
drop |
| 250 |
(return (i32.const 1)) |
| 251 |
) |
| 252 |
drop |
| 253 |
(return (i32.const 0)) |
| 254 |
) |
| 255 |
)`; |
| 256 |
|
| 257 |
function assertCast(func, good) { |
| 258 |
if (good) { |
| 259 |
return [func(), true]; |
| 260 |
} else { |
| 261 |
assertErrorMessage(func, WebAssembly.RuntimeError, /bad cast/); |
| 262 |
return [null, false]; |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
try { |
| 267 |
// The casts are split up so the stack trace will show you which cast is failing. |
| 268 |
const { |
| 269 |
make, |
| 270 |
cast1, cast2, |
| 271 |
test1, test2, |
| 272 |
branch1, branch2, |
| 273 |
branchfail1, branchfail2, |
| 274 |
} = wasmEvalText(moduleText).exports; |
| 275 |
|
| 276 |
const val = make(); |
| 277 |
const [res1, ok1] = assertCast(() => cast1(val), good1); |
| 278 |
assertEq(test1(val), good1 ? 1 : 0); |
| 279 |
assertEq(branch1(val), good1 ? 1 : 0); |
| 280 |
assertEq(branchfail1(val), good1 ? 1 : 0); |
| 281 |
if (ok1) { |
| 282 |
assertCast(() => cast2(res1), good2); |
| 283 |
assertEq(test2(res1), good2 ? 1 : 0); |
| 284 |
assertEq(branch2(res1), good2 ? 1 : 0); |
| 285 |
assertEq(branchfail2(res1), good2 ? 1 : 0); |
| 286 |
} |
| 287 |
} catch (e) { |
| 288 |
print("Failed! Module text was:"); |
| 289 |
print(moduleText); |
| 290 |
throw e; |
| 291 |
} |
| 292 |
} |
| 293 |
} |
| 294 |
} |
| 295 |
} |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
print(`we hope you have enjoyed these ${numCases} test cases 😁`); |