| OLD | NEW |
| 1 // Copyright 2016 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // clang-format off | 5 // clang-format off |
| 6 // Flags: --expose-wasm | 6 // Flags: --expose-wasm |
| 7 | 7 |
| 8 load("test/mjsunit/wasm/wasm-constants.js"); | 8 load("test/mjsunit/wasm/wasm-constants.js"); |
| 9 load("test/mjsunit/wasm/wasm-module-builder.js"); | 9 load("test/mjsunit/wasm/wasm-module-builder.js"); |
| 10 | 10 |
| 11 // The stack trace contains file path, only keep "stack.js". | 11 // The stack trace contains file path, only keep "stack.js". |
| 12 function stripPath(s) { | 12 function stripPath(s) { |
| 13 return s.replace(/[^ (]*stack\.js/g, "stack.js"); | 13 return s.replace(/[^ (]*stack\.js/g, "stack.js"); |
| 14 } | 14 } |
| 15 | 15 |
| 16 function verifyStack(frames, expected) { | 16 function verifyStack(frames, expected) { |
| 17 assertEquals(expected.length, frames.length, "number of frames mismatch"); | 17 assertEquals(expected.length, frames.length, "number of frames mismatch"); |
| 18 expected.forEach(function(exp, i) { | 18 expected.forEach(function(exp, i) { |
| 19 assertEquals(exp[0], frames[i].getFunctionName(), | 19 if (exp[1] != "?") { |
| 20 "["+i+"].getFunctionName()"); | 20 assertEquals(exp[1], frames[i].getFunctionName(), |
| 21 assertEquals(exp[1], frames[i].getLineNumber(), | 21 "["+i+"].getFunctionName()"); |
| 22 "["+i+"].getLineNumber()"); | 22 } |
| 23 assertContains(exp[2], frames[i].getFileName(), | 23 assertEquals(exp[2], frames[i].getLineNumber(), "["+i+"].getLineNumber()"); |
| 24 "["+i+"].getFileName()"); | 24 if (exp[0]) |
| 25 assertContains(exp[3], frames[i].toString(), | 25 assertEquals(exp[3], frames[i].getPosition(), |
| 26 "["+i+"].toString()"); | 26 "["+i+"].getPosition()"); |
| 27 assertContains(exp[4], frames[i].getFileName(), "["+i+"].getFileName()"); |
| 28 var toString; |
| 29 if (exp[0]) { |
| 30 var funName = exp[1] == "?" ? "" : exp[1]; |
| 31 toString = funName + " (<WASM>:" + exp[2] + ":" + exp[3] + ")"; |
| 32 } else { |
| 33 toString = exp[4] + ":" + exp[2] + ":"; |
| 34 } |
| 35 assertContains(toString, frames[i].toString(), "["+i+"].toString()"); |
| 27 }); | 36 }); |
| 28 } | 37 } |
| 29 | 38 |
| 30 | 39 |
| 31 var stack; | 40 var stack; |
| 32 function STACK() { | 41 function STACK() { |
| 33 var e = new Error(); | 42 var e = new Error(); |
| 34 stack = e.stack; | 43 stack = e.stack; |
| 35 } | 44 } |
| 36 | 45 |
| 37 var builder = new WasmModuleBuilder(); | 46 var builder = new WasmModuleBuilder(); |
| 38 | 47 |
| 39 builder.addImport("func", kSig_v_v); | 48 builder.addImport("func", kSig_v_v); |
| 40 | 49 |
| 41 builder.addFunction("main", kSig_v_v) | 50 builder.addFunction("main", kSig_v_v) |
| 42 .addBody([kExprCallImport, kArity0, 0]) | 51 .addBody([kExprCallImport, kArity0, 0]) |
| 43 .exportAs("main"); | 52 .exportAs("main"); |
| 44 | 53 |
| 45 builder.addFunction("exec_unreachable", kSig_v_v) | 54 builder.addFunction("exec_unreachable", kSig_v_v) |
| 46 .addBody([kExprUnreachable]) | 55 .addBody([kExprUnreachable]) |
| 47 .exportAs("exec_unreachable"); | 56 .exportAs("exec_unreachable"); |
| 48 | 57 |
| 49 // make this function unnamed, just to test also this case | 58 // Make this function unnamed, just to test also this case. |
| 50 var mem_oob_func = builder.addFunction(undefined, kSig_v_v) | 59 var mem_oob_func = builder.addFunction(undefined, kSig_v_v) |
| 51 // access the memory at offset -1 | 60 // Access the memory at offset -1, to provoke a trap. |
| 52 .addBody([kExprI32Const, 0x7f, kExprI32LoadMem8S, 0, 0]) | 61 .addBody([kExprI32Const, 0x7f, kExprI32LoadMem8S, 0, 0]) |
| 53 .exportAs("mem_out_of_bounds"); | 62 .exportAs("mem_out_of_bounds"); |
| 54 | 63 |
| 55 // call the mem_out_of_bounds function, in order to have two WASM stack frames | 64 // Call the mem_out_of_bounds function, in order to have two WASM stack frames. |
| 56 builder.addFunction("call_mem_out_of_bounds", kSig_v_v) | 65 builder.addFunction("call_mem_out_of_bounds", kSig_v_v) |
| 57 .addBody([kExprCallFunction, kArity0, mem_oob_func.index]) | 66 .addBody([kExprCallFunction, kArity0, mem_oob_func.index]) |
| 58 .exportAs("call_mem_out_of_bounds"); | 67 .exportAs("call_mem_out_of_bounds"); |
| 59 | 68 |
| 60 var module = builder.instantiate({func: STACK}); | 69 var module = builder.instantiate({func: STACK}); |
| 61 | 70 |
| 62 (function testSimpleStack() { | 71 (function testSimpleStack() { |
| 63 var expected_string = "Error\n" + | 72 var expected_string = "Error\n" + |
| 64 // The line numbers below will change as this test gains / loses lines.. | 73 // The line numbers below will change as this test gains / loses lines.. |
| 65 " at STACK (stack.js:33:11)\n" + // -- | 74 " at STACK (stack.js:42:11)\n" + // -- |
| 66 " at <WASM> (<anonymous>)\n" + // TODO(jfb): wasm stack here. | 75 " at main (<WASM>:0:1)\n" + // -- |
| 67 " at testSimpleStack (stack.js:70:18)\n" + // -- | 76 " at testSimpleStack (stack.js:79:18)\n" + // -- |
| 68 " at stack.js:72:3"; // -- | 77 " at stack.js:81:3"; // -- |
| 69 | 78 |
| 70 module.exports.main(); | 79 module.exports.main(); |
| 71 assertEquals(expected_string, stripPath(stack)); | 80 assertEquals(expected_string, stripPath(stack)); |
| 72 })(); | 81 })(); |
| 73 | 82 |
| 74 // For the remaining tests, collect the Callsite objects instead of just a | 83 // For the remaining tests, collect the Callsite objects instead of just a |
| 75 // string: | 84 // string: |
| 76 Error.prepareStackTrace = function(error, frames) { | 85 Error.prepareStackTrace = function(error, frames) { |
| 77 return frames; | 86 return frames; |
| 78 }; | 87 }; |
| 79 | 88 |
| 80 (function testStackFrames() { | 89 (function testStackFrames() { |
| 81 module.exports.main(); | 90 module.exports.main(); |
| 82 | 91 |
| 83 // TODO(clemensh): add a isWasm() method or similar, and test it | |
| 84 verifyStack(stack, [ | 92 verifyStack(stack, [ |
| 85 // function line file toString | 93 // isWasm function line pos file |
| 86 [ "STACK", 33, "stack.js", "stack.js:33:11"], | 94 [ false, "STACK", 42, 0, "stack.js"], |
| 87 [ "<WASM>", null, null, "<WASM>"], | 95 [ true, "main", 0, 1, null], |
| 88 ["testStackFrames", 81, "stack.js", "stack.js:81:18"], | 96 [ false, "testStackFrames", 90, 0, "stack.js"], |
| 89 [ null, 91, "stack.js", "stack.js:91:3"] | 97 [ false, null, 99, 0, "stack.js"] |
| 90 ]); | 98 ]); |
| 91 })(); | 99 })(); |
| 92 | 100 |
| 93 (function testWasmUnreachable() { | 101 (function testWasmUnreachable() { |
| 94 try { | 102 try { |
| 95 module.exports.exec_unreachable(); | 103 module.exports.exec_unreachable(); |
| 96 fail("expected wasm exception"); | 104 fail("expected wasm exception"); |
| 97 } catch (e) { | 105 } catch (e) { |
| 98 assertContains("unreachable", e.message); | 106 assertContains("unreachable", e.message); |
| 99 verifyStack(e.stack, [ | 107 verifyStack(e.stack, [ |
| 100 // function line file toString | 108 // isWasm function line pos file |
| 101 [ "<WASM>", null, null, "<WASM>"], | 109 // TODO(clemensh): pos should be 1 |
| 102 ["testWasmUnreachable", 95, "stack.js", "stack.js:95:20"], | 110 [ true, "exec_unreachable", 1, -1, null], |
| 103 [ null, 106, "stack.js", "stack.js:106:3"] | 111 [ false, "testWasmUnreachable", 103, 0, "stack.js"], |
| 112 [ false, null, 115, 0, "stack.js"] |
| 104 ]); | 113 ]); |
| 105 } | 114 } |
| 106 })(); | 115 })(); |
| 107 | 116 |
| 108 (function testWasmMemOutOfBounds() { | 117 (function testWasmMemOutOfBounds() { |
| 109 try { | 118 try { |
| 110 module.exports.call_mem_out_of_bounds(); | 119 module.exports.call_mem_out_of_bounds(); |
| 111 fail("expected wasm exception"); | 120 fail("expected wasm exception"); |
| 112 } catch (e) { | 121 } catch (e) { |
| 113 assertContains("out of bounds", e.message); | 122 assertContains("out of bounds", e.message); |
| 114 verifyStack(e.stack, [ | 123 verifyStack(e.stack, [ |
| 115 // function line file toString | 124 // isWasm function line pos file |
| 116 [ "<WASM>", null, null, "<WASM>"], | 125 // TODO(clemensh): pos should be 3 |
| 117 [ "<WASM>", null, null, "<WASM>"], | 126 [ true, "?", 2, -1, null], |
| 118 ["testWasmMemOutOfBounds", 110, "stack.js", "stack.js:110:20"], | 127 [ true, "call_mem_out_of_bounds", 3, 1, null], |
| 119 [ null, 122, "stack.js", "stack.js:122:3"] | 128 [ false, "testWasmMemOutOfBounds", 119, 0, "stack.js"], |
| 129 [ false, null, 132, 0, "stack.js"] |
| 120 ]); | 130 ]); |
| 121 } | 131 } |
| 122 })(); | 132 })(); |
| OLD | NEW |