| 1 |
|
| 2 |
// Test wasm type validation for exception handling instructions. |
| 3 |
|
| 4 |
load(libdir + "wasm-binary.js"); |
| 5 |
|
| 6 |
function wasmValid(mod) { |
| 7 |
assertEq(WebAssembly.validate(mod), true); |
| 8 |
} |
| 9 |
|
| 10 |
function wasmInvalid(mod, pattern) { |
| 11 |
assertEq(WebAssembly.validate(mod), false); |
| 12 |
assertErrorMessage( |
| 13 |
() => new WebAssembly.Module(mod), |
| 14 |
WebAssembly.CompileError, |
| 15 |
pattern |
| 16 |
); |
| 17 |
} |
| 18 |
|
| 19 |
const emptyType = { args: [], ret: VoidCode }; |
| 20 |
const i32Type = { args: [I32Code], ret: VoidCode }; |
| 21 |
const toi32Type = { args: [], ret: I32Code }; |
| 22 |
const i32Toi32Type = { args: [I32Code], ret: I32Code }; |
| 23 |
const i32Toi64Type = { args: [I32Code], ret: I64Code }; |
| 24 |
const i32i32Toi32Type = { args: [I32Code, I32Code], ret: I32Code }; |
| 25 |
|
| 26 |
function testValidateDecode() { |
| 27 |
// Try blocks must have a block type code. |
| 28 |
wasmInvalid( |
| 29 |
moduleWithSections([ |
| 30 |
sigSection([emptyType]), |
| 31 |
declSection([0]), |
| 32 |
tagSection([{ type: 0 }]), |
| 33 |
bodySection([ |
| 34 |
funcBody({ |
| 35 |
locals: [], |
| 36 |
body: [ |
| 37 |
TryCode, |
| 38 |
// Missing type code. |
| 39 |
I32ConstCode, |
| 40 |
0x01, |
| 41 |
CatchCode, |
| 42 |
0x00, |
| 43 |
EndCode, |
| 44 |
DropCode, |
| 45 |
ReturnCode, |
| 46 |
], |
| 47 |
}), |
| 48 |
]), |
| 49 |
]), |
| 50 |
/bad type/ |
| 51 |
); |
| 52 |
|
| 53 |
// Catch must have a tag index. |
| 54 |
wasmInvalid( |
| 55 |
moduleWithSections([ |
| 56 |
sigSection([emptyType]), |
| 57 |
declSection([0]), |
| 58 |
tagSection([{ type: 0 }]), |
| 59 |
bodySection([ |
| 60 |
funcBody( |
| 61 |
{ |
| 62 |
locals: [], |
| 63 |
body: [ |
| 64 |
TryCode, |
| 65 |
I32Code, |
| 66 |
I32ConstCode, |
| 67 |
0x01, |
| 68 |
CatchCode, |
| 69 |
// Index missing. |
| 70 |
], |
| 71 |
}, |
| 72 |
(withEndCode = false) |
| 73 |
), |
| 74 |
]), |
| 75 |
]), |
| 76 |
/expected tag index/ |
| 77 |
); |
| 78 |
|
| 79 |
// Rethrow must have a depth argument. |
| 80 |
wasmInvalid( |
| 81 |
moduleWithSections([ |
| 82 |
sigSection([emptyType]), |
| 83 |
declSection([0]), |
| 84 |
tagSection([{ type: 0 }]), |
| 85 |
bodySection([ |
| 86 |
funcBody( |
| 87 |
{ |
| 88 |
locals: [], |
| 89 |
body: [ |
| 90 |
RethrowCode, |
| 91 |
// Index missing. |
| 92 |
], |
| 93 |
}, |
| 94 |
(withEndCode = false) |
| 95 |
), |
| 96 |
]), |
| 97 |
]), |
| 98 |
/unable to read rethrow depth/ |
| 99 |
); |
| 100 |
|
| 101 |
// Delegate must have a depth argument. |
| 102 |
wasmInvalid( |
| 103 |
moduleWithSections([ |
| 104 |
sigSection([emptyType]), |
| 105 |
declSection([0]), |
| 106 |
tagSection([{ type: 0 }]), |
| 107 |
bodySection([ |
| 108 |
funcBody( |
| 109 |
{ |
| 110 |
locals: [], |
| 111 |
body: [ |
| 112 |
TryCode, |
| 113 |
I32Code, |
| 114 |
I32ConstCode, |
| 115 |
0x01, |
| 116 |
DelegateCode, |
| 117 |
// Index missing. |
| 118 |
], |
| 119 |
}, |
| 120 |
(withEndCode = false) |
| 121 |
), |
| 122 |
]), |
| 123 |
]), |
| 124 |
/unable to read delegate depth/ |
| 125 |
); |
| 126 |
} |
| 127 |
|
| 128 |
function testValidateThrow() { |
| 129 |
valid = `(module |
| 130 |
(type (func (param i32))) |
| 131 |
(func $exn-zero |
| 132 |
i32.const 0 |
| 133 |
throw $exn1) |
| 134 |
(tag $exn1 (type 0)))`; |
| 135 |
|
| 136 |
validSimd = `(module |
| 137 |
(tag $exn (param v128)) |
| 138 |
(func (export "f") (param v128) (result v128) |
| 139 |
try (result v128) |
| 140 |
(v128.const f64x2 1 2) |
| 141 |
(throw $exn) |
| 142 |
catch $exn |
| 143 |
end))`; |
| 144 |
|
| 145 |
invalid0 = `(module |
| 146 |
(type (func (param i32))) |
| 147 |
(func $exn-zero |
| 148 |
throw $exn1) |
| 149 |
(tag $exn1 (type 0)))`; |
| 150 |
error0 = /popping value from empty stack/; |
| 151 |
|
| 152 |
invalid1 = `(module |
| 153 |
(type (func (param i32))) |
| 154 |
(func $exn-zero |
| 155 |
i64.const 0 |
| 156 |
throw $exn1) |
| 157 |
(tag $exn1 (type 0)))`; |
| 158 |
error1 = /expression has type i64 but expected i32/; |
| 159 |
|
| 160 |
invalid2 = `(module |
| 161 |
(type (func (param i32))) |
| 162 |
(func $exn-zero |
| 163 |
i32.const 0 |
| 164 |
throw 1) |
| 165 |
(tag $exn1 (type 0)))`; |
| 166 |
error2 = /tag index out of range/; |
| 167 |
|
| 168 |
wasmValidateText(valid); |
| 169 |
if (wasmSimdEnabled()) { |
| 170 |
wasmValidateText(validSimd); |
| 171 |
} |
| 172 |
wasmFailValidateText(invalid0, error0); |
| 173 |
wasmFailValidateText(invalid1, error1); |
| 174 |
wasmFailValidateText(invalid2, error2); |
| 175 |
} |
| 176 |
|
| 177 |
function testValidateTryCatch() { |
| 178 |
function mod_with(fbody) { |
| 179 |
return moduleWithSections([ |
| 180 |
sigSection([emptyType, i32Type, i32i32Toi32Type]), |
| 181 |
declSection([0]), |
| 182 |
tagSection([{ type: 0 }, { type: 1 }]), |
| 183 |
bodySection([ |
| 184 |
funcBody({ |
| 185 |
locals: [], |
| 186 |
body: fbody, |
| 187 |
}), |
| 188 |
]), |
| 189 |
]); |
| 190 |
} |
| 191 |
|
| 192 |
const body1 = [ |
| 193 |
// try (result i32) |
| 194 |
TryCode, |
| 195 |
I32Code, |
| 196 |
// (i32.const 1) |
| 197 |
I32ConstCode, |
| 198 |
varU32(1), |
| 199 |
// catch 1 |
| 200 |
CatchCode, |
| 201 |
varU32(1), |
| 202 |
]; |
| 203 |
|
| 204 |
const valid1 = mod_with(body1.concat([EndCode, DropCode, ReturnCode])); |
| 205 |
const invalid1 = mod_with( |
| 206 |
body1.concat([I32ConstCode, varU32(2), EndCode, DropCode, ReturnCode]) |
| 207 |
); |
| 208 |
|
| 209 |
const valid2 = mod_with([ |
| 210 |
// (i32.const 0) (i32.const 0) |
| 211 |
I32ConstCode, |
| 212 |
varU32(0), |
| 213 |
I32ConstCode, |
| 214 |
varU32(0), |
| 215 |
// try (param i32 i32) (result i32) drop drop (i32.const 1) |
| 216 |
TryCode, |
| 217 |
varS32(2), |
| 218 |
DropCode, |
| 219 |
DropCode, |
| 220 |
I32ConstCode, |
| 221 |
varU32(1), |
| 222 |
// catch 0 (i32.const 2) end drop return |
| 223 |
CatchCode, |
| 224 |
varU32(0), |
| 225 |
I32ConstCode, |
| 226 |
varU32(2), |
| 227 |
EndCode, |
| 228 |
DropCode, |
| 229 |
ReturnCode, |
| 230 |
]); |
| 231 |
|
| 232 |
wasmValid(valid1); |
| 233 |
wasmInvalid(invalid1, /unused values not explicitly dropped/); |
| 234 |
wasmValid(valid2); |
| 235 |
|
| 236 |
// Test handler-less try blocks. |
| 237 |
wasmValidateText( |
| 238 |
`(module (func try end))` |
| 239 |
); |
| 240 |
|
| 241 |
wasmValidateText( |
| 242 |
`(module (func (result i32) try (result i32) (i32.const 1) end))` |
| 243 |
); |
| 244 |
|
| 245 |
wasmValidateText( |
| 246 |
`(module |
| 247 |
(func (result i32) |
| 248 |
try (result i32) (i32.const 1) (br 0) end))` |
| 249 |
); |
| 250 |
|
| 251 |
wasmFailValidateText( |
| 252 |
`(module |
| 253 |
(func try (result i32) end))`, |
| 254 |
/popping value from empty stack/ |
| 255 |
); |
| 256 |
} |
| 257 |
|
| 258 |
function testValidateCatch() { |
| 259 |
wasmInvalid( |
| 260 |
moduleWithSections([ |
| 261 |
sigSection([emptyType]), |
| 262 |
declSection([0]), |
| 263 |
bodySection([ |
| 264 |
funcBody({ |
| 265 |
locals: [], |
| 266 |
body: [TryCode, VoidCode, CatchCode, varU32(0), EndCode], |
| 267 |
}), |
| 268 |
]), |
| 269 |
]), |
| 270 |
/tag index out of range/ |
| 271 |
); |
| 272 |
} |
| 273 |
|
| 274 |
function testValidateCatchAll() { |
| 275 |
wasmValidateText( |
| 276 |
`(module |
| 277 |
(tag $exn) |
| 278 |
(func try catch $exn catch_all end))` |
| 279 |
); |
| 280 |
|
| 281 |
wasmValidateText( |
| 282 |
`(module |
| 283 |
(func (result i32) |
| 284 |
try (result i32) |
| 285 |
(i32.const 0) |
| 286 |
catch_all |
| 287 |
(i32.const 1) |
| 288 |
end))` |
| 289 |
); |
| 290 |
|
| 291 |
wasmFailValidateText( |
| 292 |
`(module |
| 293 |
(tag $exn) |
| 294 |
(func try catch_all catch 0 end))`, |
| 295 |
/catch cannot follow a catch_all/ |
| 296 |
); |
| 297 |
|
| 298 |
wasmFailValidateText( |
| 299 |
`(module |
| 300 |
(tag $exn) |
| 301 |
(func try (result i32) (i32.const 1) catch_all end drop))`, |
| 302 |
/popping value from empty stack/ |
| 303 |
); |
| 304 |
|
| 305 |
wasmFailValidateText( |
| 306 |
`(module |
| 307 |
(tag $exn (param i32)) |
| 308 |
(func try catch $exn drop catch_all drop end))`, |
| 309 |
/popping value from empty stack/ |
| 310 |
); |
| 311 |
|
| 312 |
// We can't distinguish `else` and `catch_all` in error messages since they |
| 313 |
// share the binary opcode. |
| 314 |
wasmFailValidateText( |
| 315 |
`(module |
| 316 |
(tag $exn) |
| 317 |
(func try catch_all catch_all end))`, |
| 318 |
/catch_all can only be used within a try/ |
| 319 |
); |
| 320 |
|
| 321 |
wasmFailValidateText( |
| 322 |
`(module |
| 323 |
(tag $exn) |
| 324 |
(func catch_all))`, |
| 325 |
/catch_all can only be used within a try/ |
| 326 |
); |
| 327 |
} |
| 328 |
|
| 329 |
function testValidateExnPayload() { |
| 330 |
valid0 = moduleWithSections([ |
| 331 |
sigSection([i32Type, i32Toi32Type]), |
| 332 |
declSection([1]), |
| 333 |
// (tag $exn (param i32)) |
| 334 |
tagSection([{ type: 0 }]), |
| 335 |
bodySection([ |
| 336 |
// (func (param i32) (result i32) ... |
| 337 |
funcBody({ |
| 338 |
locals: [], |
| 339 |
body: [ |
| 340 |
// try (result i32) (local.get 0) (throw $exn) (i32.const 1) |
| 341 |
TryCode, |
| 342 |
I32Code, |
| 343 |
LocalGetCode, |
| 344 |
varU32(0), |
| 345 |
ThrowCode, |
| 346 |
varU32(0), |
| 347 |
I32ConstCode, |
| 348 |
varU32(1), |
| 349 |
// catch $exn (i32.const 1) (i32.add) end |
| 350 |
CatchCode, |
| 351 |
varU32(0), |
| 352 |
I32ConstCode, |
| 353 |
varU32(1), |
| 354 |
I32AddCode, |
| 355 |
EndCode, |
| 356 |
], |
| 357 |
}), |
| 358 |
]), |
| 359 |
]); |
| 360 |
|
| 361 |
// This is to ensure the following sentence from the spec overview holds: |
| 362 |
// > "the operand stack is popped back to the size the operand stack had |
| 363 |
// > when the try block was entered" |
| 364 |
valid1 = moduleWithSections([ |
| 365 |
sigSection([i32Type, toi32Type]), |
| 366 |
declSection([1]), |
| 367 |
// (tag $exn (param i32)) |
| 368 |
tagSection([{ type: 0 }]), |
| 369 |
bodySection([ |
| 370 |
// (func (result i32) ... |
| 371 |
funcBody({ |
| 372 |
locals: [], |
| 373 |
body: [ |
| 374 |
// try (result i32) (i32.const 0) (i32.const 1) (throw $exn) drop |
| 375 |
TryCode, |
| 376 |
I32Code, |
| 377 |
I32ConstCode, |
| 378 |
varU32(0), |
| 379 |
I32ConstCode, |
| 380 |
varU32(1), |
| 381 |
ThrowCode, |
| 382 |
varU32(0), |
| 383 |
DropCode, |
| 384 |
// catch $exn drop (i32.const 2) end |
| 385 |
CatchCode, |
| 386 |
varU32(0), |
| 387 |
DropCode, |
| 388 |
I32ConstCode, |
| 389 |
varU32(2), |
| 390 |
EndCode, |
| 391 |
], |
| 392 |
}), |
| 393 |
]), |
| 394 |
]); |
| 395 |
|
| 396 |
invalid0 = moduleWithSections([ |
| 397 |
sigSection([i32Type, i32Toi64Type]), |
| 398 |
declSection([1]), |
| 399 |
// (tag $exn (param i32)) |
| 400 |
tagSection([{ type: 0 }]), |
| 401 |
bodySection([ |
| 402 |
// (func (param i32) (result i64) ... |
| 403 |
funcBody({ |
| 404 |
locals: [], |
| 405 |
body: [ |
| 406 |
// try (result i64) (local.get 0) (throw $exn) (i64.const 0) |
| 407 |
TryCode, |
| 408 |
I64Code, |
| 409 |
LocalGetCode, |
| 410 |
varU32(0), |
| 411 |
ThrowCode, |
| 412 |
varU32(0), |
| 413 |
I64ConstCode, |
| 414 |
varU32(0), |
| 415 |
// catch $exn end |
| 416 |
CatchCode, |
| 417 |
varU32(0), |
| 418 |
EndCode, |
| 419 |
], |
| 420 |
}), |
| 421 |
]), |
| 422 |
]); |
| 423 |
|
| 424 |
invalid1 = moduleWithSections([ |
| 425 |
// (type (func)) |
| 426 |
sigSection([emptyType]), |
| 427 |
declSection([0]), |
| 428 |
// (tag $exn (type 0)) |
| 429 |
tagSection([{ type: 0 }]), |
| 430 |
bodySection([ |
| 431 |
// (func ... |
| 432 |
funcBody({ |
| 433 |
locals: [], |
| 434 |
body: [ |
| 435 |
// try catch 1 end |
| 436 |
TryCode, |
| 437 |
VoidCode, |
| 438 |
CatchCode, |
| 439 |
varU32(1), |
| 440 |
EndCode, |
| 441 |
], |
| 442 |
}), |
| 443 |
]), |
| 444 |
]); |
| 445 |
|
| 446 |
wasmValid(valid0); |
| 447 |
wasmValid(valid1); |
| 448 |
wasmInvalid(invalid0, /has type i32 but expected i64/); |
| 449 |
wasmInvalid(invalid1, /tag index out of range/); |
| 450 |
} |
| 451 |
|
| 452 |
function testValidateRethrow() { |
| 453 |
wasmValidateText( |
| 454 |
`(module |
| 455 |
(tag $exn (param)) |
| 456 |
(func |
| 457 |
try |
| 458 |
nop |
| 459 |
catch $exn |
| 460 |
rethrow 0 |
| 461 |
end))` |
| 462 |
); |
| 463 |
|
| 464 |
wasmValidateText( |
| 465 |
`(module |
| 466 |
(tag $exn (param)) |
| 467 |
(func |
| 468 |
try |
| 469 |
nop |
| 470 |
catch_all |
| 471 |
rethrow 0 |
| 472 |
end))` |
| 473 |
); |
| 474 |
|
| 475 |
wasmValidateText( |
| 476 |
`(module |
| 477 |
(func (result i32) |
| 478 |
try (result i32) |
| 479 |
(i32.const 1) |
| 480 |
catch_all |
| 481 |
rethrow 0 |
| 482 |
end))` |
| 483 |
); |
| 484 |
|
| 485 |
wasmValidateText( |
| 486 |
`(module |
| 487 |
(tag $exn (param)) |
| 488 |
(func |
| 489 |
try |
| 490 |
nop |
| 491 |
catch $exn |
| 492 |
block |
| 493 |
try |
| 494 |
catch $exn |
| 495 |
rethrow 0 |
| 496 |
end |
| 497 |
end |
| 498 |
end))` |
| 499 |
); |
| 500 |
|
| 501 |
wasmValidateText( |
| 502 |
`(module |
| 503 |
(tag $exn (param)) |
| 504 |
(func |
| 505 |
try |
| 506 |
nop |
| 507 |
catch $exn |
| 508 |
block |
| 509 |
try |
| 510 |
catch $exn |
| 511 |
rethrow 2 |
| 512 |
end |
| 513 |
end |
| 514 |
end))` |
| 515 |
); |
| 516 |
|
| 517 |
wasmFailValidateText( |
| 518 |
`(module |
| 519 |
(tag $exn (param)) |
| 520 |
(func |
| 521 |
try |
| 522 |
nop |
| 523 |
catch $exn |
| 524 |
block |
| 525 |
try |
| 526 |
catch $exn |
| 527 |
rethrow 1 |
| 528 |
end |
| 529 |
end |
| 530 |
end))`, |
| 531 |
/rethrow target was not a catch block/ |
| 532 |
); |
| 533 |
|
| 534 |
wasmFailValidateText( |
| 535 |
`(module (func rethrow 0))`, |
| 536 |
/rethrow target was not a catch block/ |
| 537 |
); |
| 538 |
|
| 539 |
wasmFailValidateText( |
| 540 |
`(module (func try rethrow 0 end))`, |
| 541 |
/rethrow target was not a catch block/ |
| 542 |
); |
| 543 |
|
| 544 |
wasmFailValidateText( |
| 545 |
`(module (func try rethrow 0 catch_all end))`, |
| 546 |
/rethrow target was not a catch block/ |
| 547 |
); |
| 548 |
|
| 549 |
wasmFailValidateText( |
| 550 |
`(module |
| 551 |
(tag $exn (param)) |
| 552 |
(func |
| 553 |
try |
| 554 |
nop |
| 555 |
catch $exn |
| 556 |
block |
| 557 |
try |
| 558 |
catch $exn |
| 559 |
rethrow 4 |
| 560 |
end |
| 561 |
end |
| 562 |
end))`, |
| 563 |
/rethrow depth exceeds current nesting level/ |
| 564 |
); |
| 565 |
} |
| 566 |
|
| 567 |
function testValidateDelegate() { |
| 568 |
wasmValidateText( |
| 569 |
`(module |
| 570 |
(tag $exn (param)) |
| 571 |
(func |
| 572 |
try |
| 573 |
try |
| 574 |
throw $exn |
| 575 |
delegate 0 |
| 576 |
catch $exn |
| 577 |
end))` |
| 578 |
); |
| 579 |
|
| 580 |
wasmValidateText( |
| 581 |
`(module |
| 582 |
(tag $exn (param)) |
| 583 |
(func |
| 584 |
try |
| 585 |
try |
| 586 |
throw $exn |
| 587 |
delegate 1 |
| 588 |
catch $exn |
| 589 |
end))` |
| 590 |
); |
| 591 |
|
| 592 |
wasmValidateText( |
| 593 |
`(module |
| 594 |
(tag $exn (param)) |
| 595 |
(func |
| 596 |
block |
| 597 |
try |
| 598 |
throw $exn |
| 599 |
delegate 0 |
| 600 |
end))` |
| 601 |
); |
| 602 |
|
| 603 |
wasmValidateText( |
| 604 |
`(module |
| 605 |
(tag $exn (param)) |
| 606 |
(func |
| 607 |
try |
| 608 |
catch $exn |
| 609 |
try |
| 610 |
throw $exn |
| 611 |
delegate 0 |
| 612 |
end))` |
| 613 |
); |
| 614 |
|
| 615 |
wasmFailValidateText( |
| 616 |
`(module |
| 617 |
(tag $exn (param)) |
| 618 |
(func (result i32) |
| 619 |
try |
| 620 |
throw $exn |
| 621 |
delegate 0 |
| 622 |
(i64.const 0) |
| 623 |
end))`, |
| 624 |
/type mismatch: expression has type i64 but expected i32/ |
| 625 |
); |
| 626 |
|
| 627 |
wasmFailValidateText( |
| 628 |
`(module |
| 629 |
(tag $exn (param)) |
| 630 |
(func |
| 631 |
try (result i32) |
| 632 |
(i64.const 0) |
| 633 |
delegate 0))`, |
| 634 |
/type mismatch: expression has type i64 but expected i32/ |
| 635 |
); |
| 636 |
|
| 637 |
wasmFailValidateText( |
| 638 |
`(module |
| 639 |
(tag $exn (param)) |
| 640 |
(func |
| 641 |
try |
| 642 |
try |
| 643 |
throw $exn |
| 644 |
delegate 2 |
| 645 |
catch $exn |
| 646 |
end))`, |
| 647 |
/delegate depth exceeds current nesting level/ |
| 648 |
); |
| 649 |
|
| 650 |
wasmFailValidateText( |
| 651 |
`(module (func delegate 0))`, |
| 652 |
/delegate can only be used within a try/ |
| 653 |
); |
| 654 |
} |
| 655 |
|
| 656 |
testValidateDecode(); |
| 657 |
testValidateThrow(); |
| 658 |
testValidateTryCatch(); |
| 659 |
testValidateCatch(); |
| 660 |
testValidateCatchAll(); |
| 661 |
testValidateExnPayload(); |
| 662 |
testValidateRethrow(); |
| 663 |
testValidateDelegate(); |