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 = exp[0] | |
29 ? (exp[1]=="?" ? "" : exp[1])+" (<WASM>:"+exp[2]+":"+exp[3]+")" | |
30 : exp[4]+":"+exp[2]+":"; | |
Yang
2016/05/03 18:59:08
This is so hard to read... could we avoid nesting
Clemens Hammacher
2016/05/04 09:06:20
Done.
| |
31 assertContains(toString, frames[i].toString(), "["+i+"].toString()"); | |
27 }); | 32 }); |
28 } | 33 } |
29 | 34 |
30 | 35 |
31 var stack; | 36 var stack; |
32 function STACK() { | 37 function STACK() { |
33 var e = new Error(); | 38 var e = new Error(); |
34 stack = e.stack; | 39 stack = e.stack; |
35 } | 40 } |
36 | 41 |
37 var builder = new WasmModuleBuilder(); | 42 var builder = new WasmModuleBuilder(); |
38 | 43 |
39 builder.addImport("func", kSig_v_v); | 44 builder.addImport("func", kSig_v_v); |
40 | 45 |
41 builder.addFunction("main", kSig_v_v) | 46 builder.addFunction("main", kSig_v_v) |
42 .addBody([kExprCallImport, kArity0, 0]) | 47 .addBody([kExprCallImport, kArity0, 0]) |
43 .exportAs("main"); | 48 .exportAs("main"); |
44 | 49 |
45 builder.addFunction("exec_unreachable", kSig_v_v) | 50 builder.addFunction("exec_unreachable", kSig_v_v) |
46 .addBody([kExprUnreachable]) | 51 .addBody([kExprUnreachable]) |
47 .exportAs("exec_unreachable"); | 52 .exportAs("exec_unreachable"); |
48 | 53 |
49 // make this function unnamed, just to test also this case | 54 // Make this function unnamed, just to test also this case. |
50 var mem_oob_func = builder.addFunction(undefined, kSig_v_v) | 55 var mem_oob_func = builder.addFunction(undefined, kSig_v_v) |
51 // access the memory at offset -1 | 56 // Access the memory at offset -1, to provoke a trap. |
52 .addBody([kExprI32Const, 0x7f, kExprI32LoadMem8S, 0, 0]) | 57 .addBody([kExprI32Const, 0x7f, kExprI32LoadMem8S, 0, 0]) |
53 .exportAs("mem_out_of_bounds"); | 58 .exportAs("mem_out_of_bounds"); |
54 | 59 |
55 // call the mem_out_of_bounds function, in order to have two WASM stack frames | 60 // 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) | 61 builder.addFunction("call_mem_out_of_bounds", kSig_v_v) |
57 .addBody([kExprCallFunction, kArity0, mem_oob_func.index]) | 62 .addBody([kExprCallFunction, kArity0, mem_oob_func.index]) |
58 .exportAs("call_mem_out_of_bounds"); | 63 .exportAs("call_mem_out_of_bounds"); |
59 | 64 |
60 var module = builder.instantiate({func: STACK}); | 65 var module = builder.instantiate({func: STACK}); |
61 | 66 |
62 (function testSimpleStack() { | 67 (function testSimpleStack() { |
63 var expected_string = "Error\n" + | 68 var expected_string = "Error\n" + |
64 // The line numbers below will change as this test gains / loses lines.. | 69 // The line numbers below will change as this test gains / loses lines.. |
65 " at STACK (stack.js:33:11)\n" + // -- | 70 " at STACK (stack.js:38:11)\n" + // -- |
66 " at <WASM> (<anonymous>)\n" + // TODO(jfb): wasm stack here. | 71 " at main (<WASM>:0:1)\n" + // -- |
67 " at testSimpleStack (stack.js:70:18)\n" + // -- | 72 " at testSimpleStack (stack.js:75:18)\n" + // -- |
68 " at stack.js:72:3"; // -- | 73 " at stack.js:77:3"; // -- |
69 | 74 |
70 module.exports.main(); | 75 module.exports.main(); |
71 assertEquals(expected_string, stripPath(stack)); | 76 assertEquals(expected_string, stripPath(stack)); |
72 })(); | 77 })(); |
73 | 78 |
74 // For the remaining tests, collect the Callsite objects instead of just a | 79 // For the remaining tests, collect the Callsite objects instead of just a |
75 // string: | 80 // string: |
76 Error.prepareStackTrace = function(error, frames) { | 81 Error.prepareStackTrace = function(error, frames) { |
77 return frames; | 82 return frames; |
78 }; | 83 }; |
79 | 84 |
80 (function testStackFrames() { | 85 (function testStackFrames() { |
81 module.exports.main(); | 86 module.exports.main(); |
82 | 87 |
83 // TODO(clemensh): add a isWasm() method or similar, and test it | |
84 verifyStack(stack, [ | 88 verifyStack(stack, [ |
85 // function line file toString | 89 // isWasm function line pos file |
86 [ "STACK", 33, "stack.js", "stack.js:33:11"], | 90 [ false, "STACK", 38, 0, "stack.js"], |
87 [ "<WASM>", null, null, "<WASM>"], | 91 [ true, "main", 0, 1, null], |
88 ["testStackFrames", 81, "stack.js", "stack.js:81:18"], | 92 [ false, "testStackFrames", 86, 0, "stack.js"], |
89 [ null, 91, "stack.js", "stack.js:91:3"] | 93 [ false, null, 95, 0, "stack.js"] |
90 ]); | 94 ]); |
91 })(); | 95 })(); |
92 | 96 |
93 (function testWasmUnreachable() { | 97 (function testWasmUnreachable() { |
94 try { | 98 try { |
95 module.exports.exec_unreachable(); | 99 module.exports.exec_unreachable(); |
96 fail("expected wasm exception"); | 100 fail("expected wasm exception"); |
97 } catch (e) { | 101 } catch (e) { |
98 assertContains("unreachable", e.message); | 102 assertContains("unreachable", e.message); |
99 verifyStack(e.stack, [ | 103 verifyStack(e.stack, [ |
100 // function line file toString | 104 // isWasm function line pos file |
101 [ "<WASM>", null, null, "<WASM>"], | 105 // TODO(clemensh): pos should be 1 |
102 ["testWasmUnreachable", 95, "stack.js", "stack.js:95:20"], | 106 [ true, "exec_unreachable", 1, -1, null], |
103 [ null, 106, "stack.js", "stack.js:106:3"] | 107 [ false, "testWasmUnreachable", 99, 0, "stack.js"], |
108 [ false, null, 111, 0, "stack.js"] | |
104 ]); | 109 ]); |
105 } | 110 } |
106 })(); | 111 })(); |
107 | 112 |
108 (function testWasmMemOutOfBounds() { | 113 (function testWasmMemOutOfBounds() { |
109 try { | 114 try { |
110 module.exports.call_mem_out_of_bounds(); | 115 module.exports.call_mem_out_of_bounds(); |
111 fail("expected wasm exception"); | 116 fail("expected wasm exception"); |
112 } catch (e) { | 117 } catch (e) { |
113 assertContains("out of bounds", e.message); | 118 assertContains("out of bounds", e.message); |
114 verifyStack(e.stack, [ | 119 verifyStack(e.stack, [ |
115 // function line file toString | 120 // isWasm function line pos file |
116 [ "<WASM>", null, null, "<WASM>"], | 121 // TODO(clemensh): pos should be 3 |
117 [ "<WASM>", null, null, "<WASM>"], | 122 [ true, "?", 2, -1, null], |
118 ["testWasmMemOutOfBounds", 110, "stack.js", "stack.js:110:20"], | 123 [ true, "call_mem_out_of_bounds", 3, 1, null], |
119 [ null, 122, "stack.js", "stack.js:122:3"] | 124 [ false, "testWasmMemOutOfBounds", 115, 0, "stack.js"], |
125 [ false, null, 128, 0, "stack.js"] | |
120 ]); | 126 ]); |
121 } | 127 } |
122 })(); | 128 })(); |
OLD | NEW |