OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 // Flags: --expose-wasm |
| 6 |
| 7 load("test/mjsunit/wasm/wasm-constants.js"); |
| 8 load("test/mjsunit/wasm/wasm-module-builder.js"); |
| 9 |
| 10 // Collect the Callsite objects instead of just a string: |
| 11 Error.prepareStackTrace = function(error, frames) { |
| 12 return frames; |
| 13 }; |
| 14 |
| 15 var builder = new WasmModuleBuilder(); |
| 16 |
| 17 var sig_index = builder.addSignature([kAstI32]) |
| 18 |
| 19 // Build a function to resemble this code: |
| 20 // if (idx < 2) { |
| 21 // return load(-2 / idx); |
| 22 // } else if (idx == 2) { |
| 23 // unreachable; |
| 24 // } else { |
| 25 // return call_indirect(idx); |
| 26 // } |
| 27 // There are four different traps which are triggered by different input values: |
| 28 // (0) division by zero; (1) mem oob; (2) unreachable; (3) invalid call target |
| 29 // Each of them also has a different location where it traps. |
| 30 builder.addFunction("main", [kAstI32, kAstI32]) |
| 31 .addBody([ |
| 32 // offset 1 |
| 33 kExprBlock, 3, |
| 34 // offset 3 |
| 35 kExprIf, kExprI32LtU, kExprGetLocal, 0, kExprI32Const, 2, |
| 36 // offset 9 |
| 37 kExprBlock, 2, |
| 38 // offset 11 |
| 39 kExprI32LoadMem, 0, 0, |
| 40 // offset 14 |
| 41 kExprI32DivU, |
| 42 kExprI32Const, 0x7e /* -2 */, |
| 43 kExprGetLocal, 0, |
| 44 kExprBr, 1, kExprI32Const, 0, |
| 45 // offset 23 |
| 46 kExprIf, kExprI32Eq, kExprGetLocal, 0, kExprI32Const, 2, |
| 47 // offset 29 |
| 48 kExprUnreachable, |
| 49 // offset 30 |
| 50 kExprCallIndirect, sig_index, kExprGetLocal, 0, |
| 51 ]) |
| 52 .exportAs("main"); |
| 53 |
| 54 var module = builder.instantiate(); |
| 55 |
| 56 function testWasmTrap(value, reason, position) { |
| 57 try { |
| 58 module.exports.main(value); |
| 59 fail("expected wasm exception"); |
| 60 } catch (e) { |
| 61 assertEquals(kTrapMsgs[reason], e.message, "trap reason"); |
| 62 assertEquals(3, e.stack.length, "number of frames"); |
| 63 assertTrue(e.stack[0].isWasm(), "isWasm"); |
| 64 assertEquals(0, e.stack[0].getWasmFunctionIndex(), "wasmFunctionIndex"); |
| 65 assertEquals(position, e.stack[0].getPosition(), "position"); |
| 66 for (i = 1; i < 3; ++i) |
| 67 assertFalse(e.stack[i].isWasm(), "isWasm"); |
| 68 } |
| 69 } |
| 70 |
| 71 // The actual tests: |
| 72 testWasmTrap(0, kTrapDivByZero, 14); |
| 73 testWasmTrap(1, kTrapMemOutOfBounds, 11); |
| 74 testWasmTrap(2, kTrapUnreachable, 29); |
| 75 testWasmTrap(3, kTrapFuncInvalid, 30); |
OLD | NEW |