blob: a45db943cc3032c0c3a01b4d7dd76bdbebc6c8f5 [file] [log] [blame]
Ben Murdoch097c5b22016-05-18 11:27:45 +01001// 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
Ben Murdochc5610432016-08-08 18:44:38 +01005// clang-format off
Ben Murdoch097c5b22016-05-18 11:27:45 +01006// Flags: --expose-wasm
7
8load("test/mjsunit/wasm/wasm-constants.js");
Ben Murdochda12d292016-06-02 14:46:10 +01009load("test/mjsunit/wasm/wasm-module-builder.js");
Ben Murdoch097c5b22016-05-18 11:27:45 +010010
Ben Murdoch097c5b22016-05-18 11:27:45 +010011// The stack trace contains file path, only keep "stack.js".
12function stripPath(s) {
13 return s.replace(/[^ (]*stack\.js/g, "stack.js");
14}
15
Ben Murdochc5610432016-08-08 18:44:38 +010016function verifyStack(frames, expected) {
17 assertEquals(expected.length, frames.length, "number of frames mismatch");
18 expected.forEach(function(exp, i) {
19 if (exp[1] != "?") {
20 assertEquals(exp[1], frames[i].getFunctionName(),
21 "["+i+"].getFunctionName()");
22 }
23 assertEquals(exp[2], frames[i].getLineNumber(), "["+i+"].getLineNumber()");
24 if (exp[0])
25 assertEquals(exp[3], frames[i].getPosition(),
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()");
36 });
37}
38
39
Ben Murdoch097c5b22016-05-18 11:27:45 +010040var stack;
41function STACK() {
42 var e = new Error();
43 stack = e.stack;
44}
45
Ben Murdochc5610432016-08-08 18:44:38 +010046var builder = new WasmModuleBuilder();
Ben Murdochda12d292016-06-02 14:46:10 +010047
Ben Murdochc5610432016-08-08 18:44:38 +010048builder.addImport("func", kSig_v_v);
Ben Murdochda12d292016-06-02 14:46:10 +010049
Ben Murdochc5610432016-08-08 18:44:38 +010050builder.addFunction("main", kSig_v_v)
51 .addBody([kExprCallImport, kArity0, 0])
52 .exportAs("main");
Ben Murdochda12d292016-06-02 14:46:10 +010053
Ben Murdochc5610432016-08-08 18:44:38 +010054builder.addFunction("exec_unreachable", kSig_v_v)
55 .addBody([kExprUnreachable])
56 .exportAs("exec_unreachable");
57
58// Make this function unnamed, just to test also this case.
59var mem_oob_func = builder.addFunction(undefined, kSig_v_v)
60 // Access the memory at offset -1, to provoke a trap.
61 .addBody([kExprI32Const, 0x7f, kExprI32LoadMem8S, 0, 0])
62 .exportAs("mem_out_of_bounds");
63
64// Call the mem_out_of_bounds function, in order to have two WASM stack frames.
65builder.addFunction("call_mem_out_of_bounds", kSig_v_v)
66 .addBody([kExprCallFunction, kArity0, mem_oob_func.index])
67 .exportAs("call_mem_out_of_bounds");
68
69var module = builder.instantiate({func: STACK});
70
71(function testSimpleStack() {
72 var expected_string = "Error\n" +
73 // The line numbers below will change as this test gains / loses lines..
74 " at STACK (stack.js:42:11)\n" + // --
75 " at main (<WASM>:0:1)\n" + // --
76 " at testSimpleStack (stack.js:79:18)\n" + // --
77 " at stack.js:81:3"; // --
78
Ben Murdochda12d292016-06-02 14:46:10 +010079 module.exports.main();
Ben Murdochc5610432016-08-08 18:44:38 +010080 assertEquals(expected_string, stripPath(stack));
81})();
82
83// For the remaining tests, collect the Callsite objects instead of just a
84// string:
85Error.prepareStackTrace = function(error, frames) {
86 return frames;
87};
88
89(function testStackFrames() {
90 module.exports.main();
91
92 verifyStack(stack, [
93 // isWasm function line pos file
94 [ false, "STACK", 42, 0, "stack.js"],
95 [ true, "main", 0, 1, null],
96 [ false, "testStackFrames", 90, 0, "stack.js"],
97 [ false, null, 99, 0, "stack.js"]
98 ]);
99})();
100
101(function testWasmUnreachable() {
102 try {
103 module.exports.exec_unreachable();
104 fail("expected wasm exception");
105 } catch (e) {
106 assertContains("unreachable", e.message);
107 verifyStack(e.stack, [
108 // isWasm function line pos file
109 [ true, "exec_unreachable", 1, 1, null],
110 [ false, "testWasmUnreachable", 103, 0, "stack.js"],
111 [ false, null, 114, 0, "stack.js"]
112 ]);
113 }
114})();
115
116(function testWasmMemOutOfBounds() {
117 try {
118 module.exports.call_mem_out_of_bounds();
119 fail("expected wasm exception");
120 } catch (e) {
121 assertContains("out of bounds", e.message);
122 verifyStack(e.stack, [
123 // isWasm function line pos file
124 [ true, "?", 2, 3, null],
125 [ true, "call_mem_out_of_bounds", 3, 1, null],
126 [ false, "testWasmMemOutOfBounds", 118, 0, "stack.js"],
127 [ false, null, 130, 0, "stack.js"]
128 ]);
129 }
Ben Murdochda12d292016-06-02 14:46:10 +0100130})();