2016-02-04 16:24:56 +00:00
|
|
|
// Copyright 2016 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
// Flags: --expose-wasm
|
|
|
|
|
|
|
|
load("test/mjsunit/wasm/wasm-constants.js");
|
2016-03-07 19:32:35 +00:00
|
|
|
load("test/mjsunit/wasm/wasm-module-builder.js");
|
2016-02-04 16:24:56 +00:00
|
|
|
|
|
|
|
// The stack trace contains file path, only keep "stack.js".
|
|
|
|
function stripPath(s) {
|
|
|
|
return s.replace(/[^ (]*stack\.js/g, "stack.js");
|
|
|
|
}
|
|
|
|
|
2016-04-21 08:35:21 +00:00
|
|
|
function verifyStack(frames, expected) {
|
|
|
|
assertEquals(expected.length, frames.length, "number of frames mismatch");
|
|
|
|
expected.forEach(function(exp, i) {
|
2016-05-19 09:19:09 +00:00
|
|
|
assertEquals(exp[1], frames[i].getFunctionName(),
|
|
|
|
"["+i+"].getFunctionName()");
|
2016-05-06 09:07:30 +00:00
|
|
|
assertEquals(exp[2], frames[i].getLineNumber(), "["+i+"].getLineNumber()");
|
|
|
|
if (exp[0])
|
|
|
|
assertEquals(exp[3], frames[i].getPosition(),
|
|
|
|
"["+i+"].getPosition()");
|
|
|
|
assertContains(exp[4], frames[i].getFileName(), "["+i+"].getFileName()");
|
|
|
|
var toString;
|
|
|
|
if (exp[0]) {
|
2017-06-09 12:19:20 +00:00
|
|
|
toString = "wasm-function[" + exp[2] + "]:" + exp[3];
|
|
|
|
if (exp[1] !== null) toString = exp[1] + " (" + toString + ")";
|
2016-05-06 09:07:30 +00:00
|
|
|
} else {
|
|
|
|
toString = exp[4] + ":" + exp[2] + ":";
|
|
|
|
}
|
|
|
|
assertContains(toString, frames[i].toString(), "["+i+"].toString()");
|
2016-04-21 08:35:21 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-02-04 16:24:56 +00:00
|
|
|
var stack;
|
|
|
|
function STACK() {
|
|
|
|
var e = new Error();
|
|
|
|
stack = e.stack;
|
|
|
|
}
|
|
|
|
|
2016-04-21 08:35:21 +00:00
|
|
|
var builder = new WasmModuleBuilder();
|
|
|
|
|
2017-01-18 12:07:57 +00:00
|
|
|
builder.addMemory(0, 1, false);
|
|
|
|
|
2016-12-20 15:32:56 +00:00
|
|
|
builder.addImport("mod", "func", kSig_v_v);
|
2016-02-04 16:24:56 +00:00
|
|
|
|
2016-04-29 09:39:26 +00:00
|
|
|
builder.addFunction("main", kSig_v_v)
|
2016-09-27 20:46:10 +00:00
|
|
|
.addBody([kExprCallFunction, 0])
|
2016-04-21 08:35:21 +00:00
|
|
|
.exportAs("main");
|
2016-03-07 19:32:35 +00:00
|
|
|
|
2016-04-29 09:39:26 +00:00
|
|
|
builder.addFunction("exec_unreachable", kSig_v_v)
|
2016-04-21 09:36:49 +00:00
|
|
|
.addBody([kExprUnreachable])
|
|
|
|
.exportAs("exec_unreachable");
|
|
|
|
|
2016-05-06 09:07:30 +00:00
|
|
|
// Make this function unnamed, just to test also this case.
|
2016-09-27 20:46:10 +00:00
|
|
|
var mem_oob_func = builder.addFunction(undefined, kSig_i_v)
|
2016-05-06 09:07:30 +00:00
|
|
|
// Access the memory at offset -1, to provoke a trap.
|
2016-04-29 09:15:26 +00:00
|
|
|
.addBody([kExprI32Const, 0x7f, kExprI32LoadMem8S, 0, 0])
|
2016-04-21 09:36:49 +00:00
|
|
|
.exportAs("mem_out_of_bounds");
|
|
|
|
|
2017-06-09 12:21:52 +00:00
|
|
|
// Call the mem_out_of_bounds function, in order to have two wasm stack frames.
|
2016-09-27 20:46:10 +00:00
|
|
|
builder.addFunction("call_mem_out_of_bounds", kSig_i_v)
|
|
|
|
.addBody([kExprCallFunction, mem_oob_func.index])
|
2016-04-21 09:36:49 +00:00
|
|
|
.exportAs("call_mem_out_of_bounds");
|
|
|
|
|
2016-12-20 15:32:56 +00:00
|
|
|
var module = builder.instantiate({mod: {func: STACK}});
|
2016-02-04 16:24:56 +00:00
|
|
|
|
2016-04-21 08:35:21 +00:00
|
|
|
(function testSimpleStack() {
|
2017-06-09 12:19:20 +00:00
|
|
|
var expected_string = 'Error\n' +
|
|
|
|
// The line numbers below will change as this test gains / loses lines..
|
|
|
|
' at STACK (stack.js:39:11)\n' + // --
|
|
|
|
' at main (wasm-function[1]:1)\n' + // --
|
|
|
|
' at testSimpleStack (stack.js:78:18)\n' + // --
|
|
|
|
' at stack.js:80:3'; // --
|
2016-04-21 08:35:21 +00:00
|
|
|
|
|
|
|
module.exports.main();
|
|
|
|
assertEquals(expected_string, stripPath(stack));
|
|
|
|
})();
|
|
|
|
|
|
|
|
// For the remaining tests, collect the Callsite objects instead of just a
|
|
|
|
// string:
|
|
|
|
Error.prepareStackTrace = function(error, frames) {
|
|
|
|
return frames;
|
|
|
|
};
|
|
|
|
|
|
|
|
(function testStackFrames() {
|
2016-03-07 19:32:35 +00:00
|
|
|
module.exports.main();
|
2016-04-21 08:35:21 +00:00
|
|
|
|
|
|
|
verifyStack(stack, [
|
2016-05-06 09:07:30 +00:00
|
|
|
// isWasm function line pos file
|
2017-06-09 12:19:20 +00:00
|
|
|
[ false, "STACK", 39, 0, "stack.js"],
|
2016-09-27 20:46:10 +00:00
|
|
|
[ true, "main", 1, 1, null],
|
2017-06-09 12:19:20 +00:00
|
|
|
[ false, "testStackFrames", 89, 0, "stack.js"],
|
|
|
|
[ false, null, 98, 0, "stack.js"]
|
2016-04-21 08:35:21 +00:00
|
|
|
]);
|
2016-03-07 19:32:35 +00:00
|
|
|
})();
|
2016-04-21 09:36:49 +00:00
|
|
|
|
|
|
|
(function testWasmUnreachable() {
|
|
|
|
try {
|
|
|
|
module.exports.exec_unreachable();
|
|
|
|
fail("expected wasm exception");
|
|
|
|
} catch (e) {
|
|
|
|
assertContains("unreachable", e.message);
|
|
|
|
verifyStack(e.stack, [
|
2016-05-06 09:07:30 +00:00
|
|
|
// isWasm function line pos file
|
2016-09-27 20:46:10 +00:00
|
|
|
[ true, "exec_unreachable", 2, 1, null],
|
2017-06-09 12:19:20 +00:00
|
|
|
[ false, "testWasmUnreachable", 102, 0, "stack.js"],
|
|
|
|
[ false, null, 113, 0, "stack.js"]
|
2016-04-21 09:36:49 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function testWasmMemOutOfBounds() {
|
|
|
|
try {
|
|
|
|
module.exports.call_mem_out_of_bounds();
|
|
|
|
fail("expected wasm exception");
|
|
|
|
} catch (e) {
|
|
|
|
assertContains("out of bounds", e.message);
|
|
|
|
verifyStack(e.stack, [
|
2016-05-06 09:07:30 +00:00
|
|
|
// isWasm function line pos file
|
2017-04-07 14:37:30 +00:00
|
|
|
[ true, null, 3, 3, null],
|
2016-09-27 20:46:10 +00:00
|
|
|
[ true, "call_mem_out_of_bounds", 4, 1, null],
|
2017-06-09 12:19:20 +00:00
|
|
|
[ false, "testWasmMemOutOfBounds", 117, 0, "stack.js"],
|
|
|
|
[ false, null, 129, 0, "stack.js"]
|
2016-04-21 09:36:49 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
})();
|
2016-08-19 08:54:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
(function testStackOverflow() {
|
|
|
|
print("testStackOverflow");
|
|
|
|
var builder = new WasmModuleBuilder();
|
|
|
|
|
|
|
|
var sig_index = builder.addType(kSig_v_v);
|
|
|
|
builder.addFunction("recursion", sig_index)
|
|
|
|
.addBody([
|
|
|
|
kExprI32Const, 0,
|
2016-10-26 16:56:05 +00:00
|
|
|
kExprCallIndirect, sig_index, kTableZero
|
2016-08-19 08:54:22 +00:00
|
|
|
])
|
|
|
|
.exportFunc()
|
|
|
|
builder.appendToTable([0]);
|
|
|
|
|
|
|
|
try {
|
|
|
|
builder.instantiate().exports.recursion();
|
|
|
|
fail("expected wasm exception");
|
|
|
|
} catch (e) {
|
|
|
|
assertEquals("Maximum call stack size exceeded", e.message, "trap reason");
|
2017-01-09 09:43:04 +00:00
|
|
|
assertTrue(e.stack.length >= 4, "expected at least 4 stack entries");
|
|
|
|
verifyStack(e.stack.splice(0, 4), [
|
|
|
|
// isWasm function line pos file
|
|
|
|
[ true, "recursion", 0, 0, null],
|
|
|
|
[ true, "recursion", 0, 3, null],
|
|
|
|
[ true, "recursion", 0, 3, null],
|
|
|
|
[ true, "recursion", 0, 3, null]
|
|
|
|
]);
|
2016-08-19 08:54:22 +00:00
|
|
|
}
|
|
|
|
})();
|