2016-10-13 16:17:44 +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.
|
|
|
|
|
2017-12-15 21:24:43 +00:00
|
|
|
// Flags: --expose-wasm --allow-natives-syntax
|
2016-10-13 16:17:44 +00:00
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
load("test/mjsunit/wasm/wasm-constants.js");
|
|
|
|
load("test/mjsunit/wasm/wasm-module-builder.js");
|
|
|
|
|
|
|
|
function module(bytes) {
|
|
|
|
let buffer = bytes;
|
|
|
|
if (typeof buffer === 'string') {
|
|
|
|
buffer = new ArrayBuffer(bytes.length);
|
|
|
|
let view = new Uint8Array(buffer);
|
|
|
|
for (let i = 0; i < bytes.length; ++i) {
|
|
|
|
view[i] = bytes.charCodeAt(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return new WebAssembly.Module(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
function instance(bytes, imports = {}) {
|
|
|
|
return new WebAssembly.Instance(module(bytes), imports);
|
|
|
|
}
|
|
|
|
|
2017-01-13 20:47:44 +00:00
|
|
|
// instantiate should succeed but run should fail.
|
|
|
|
function instantiateAndFailAtRuntime(bytes, imports = {}) {
|
2017-07-24 16:20:40 +00:00
|
|
|
var instance =
|
|
|
|
assertDoesNotThrow(new WebAssembly.Instance(module(bytes), imports));
|
2017-01-13 20:47:44 +00:00
|
|
|
instance.exports.run();
|
|
|
|
}
|
|
|
|
|
2016-10-13 16:17:44 +00:00
|
|
|
function builder() {
|
|
|
|
return new WasmModuleBuilder;
|
|
|
|
}
|
|
|
|
|
2017-07-24 16:20:40 +00:00
|
|
|
function assertCompileError(bytes, msg) {
|
|
|
|
assertThrows(() => module(bytes), WebAssembly.CompileError, msg);
|
2016-10-13 16:17:44 +00:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:47:44 +00:00
|
|
|
// default imports to {} so we get LinkError by default, thus allowing us to
|
|
|
|
// distinguish the TypeError we want to catch
|
2017-07-24 16:20:40 +00:00
|
|
|
function assertTypeError(bytes, imports = {}, msg) {
|
|
|
|
assertThrows(() => instance(bytes, imports), TypeError, msg);
|
2016-10-13 16:17:44 +00:00
|
|
|
}
|
|
|
|
|
2017-07-24 16:20:40 +00:00
|
|
|
function assertLinkError(bytes, imports, msg) {
|
|
|
|
assertThrows(() => instance(bytes, imports), WebAssembly.LinkError, msg);
|
2016-12-16 14:23:35 +00:00
|
|
|
}
|
|
|
|
|
2017-07-24 16:20:40 +00:00
|
|
|
function assertRuntimeError(bytes, imports, msg) {
|
|
|
|
assertThrows(
|
|
|
|
() => instantiateAndFailAtRuntime(bytes, imports),
|
|
|
|
WebAssembly.RuntimeError, msg);
|
2016-10-13 16:17:44 +00:00
|
|
|
}
|
|
|
|
|
2017-07-24 16:20:40 +00:00
|
|
|
function assertConversionError(bytes, imports, msg) {
|
|
|
|
assertThrows(
|
|
|
|
() => instantiateAndFailAtRuntime(bytes, imports), TypeError, msg);
|
2016-10-13 16:17:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
(function TestDecodingError() {
|
2017-07-24 16:20:40 +00:00
|
|
|
assertCompileError("", /is empty/);
|
|
|
|
assertCompileError("X", /expected 4 bytes, fell off end @\+0/);
|
|
|
|
assertCompileError(
|
|
|
|
"\0x00asm", /expected magic word 00 61 73 6d, found 00 78 30 30 @\+0/);
|
2016-10-13 16:17:44 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
(function TestValidationError() {
|
2017-07-24 16:20:40 +00:00
|
|
|
assertCompileError(
|
|
|
|
builder().addFunction("f", kSig_i_v).end().toBuffer(),
|
|
|
|
/function body must end with "end" opcode @/);
|
2016-10-13 16:17:44 +00:00
|
|
|
assertCompileError(builder().addFunction("f", kSig_i_v).addBody([
|
|
|
|
kExprReturn
|
2017-07-24 16:20:40 +00:00
|
|
|
]).end().toBuffer(), /return found empty stack @/);
|
2016-10-13 16:17:44 +00:00
|
|
|
assertCompileError(builder().addFunction("f", kSig_v_v).addBody([
|
|
|
|
kExprGetLocal, 0
|
2017-07-24 16:20:40 +00:00
|
|
|
]).end().toBuffer(), /invalid local index: 0 @/);
|
|
|
|
assertCompileError(
|
|
|
|
builder().addStart(0).toBuffer(), /function index 0 out of bounds/);
|
2016-10-13 16:17:44 +00:00
|
|
|
})();
|
|
|
|
|
2017-04-11 20:09:20 +00:00
|
|
|
(function TestTypeError() {
|
2016-10-13 16:17:44 +00:00
|
|
|
let b;
|
|
|
|
b = builder();
|
2016-12-20 15:32:56 +00:00
|
|
|
b.addImport("foo", "bar", kSig_v_v);
|
2017-07-24 16:20:40 +00:00
|
|
|
assertTypeError(b.toBuffer(), {}, /module is not an object or function/);
|
2017-04-11 20:09:20 +00:00
|
|
|
|
|
|
|
b = builder();
|
|
|
|
b.addImportedGlobal("foo", "bar", kWasmI32);
|
2017-07-24 16:20:40 +00:00
|
|
|
assertTypeError(b.toBuffer(), {}, /module is not an object or function/);
|
2017-04-11 20:09:20 +00:00
|
|
|
|
|
|
|
b = builder();
|
|
|
|
b.addImportedMemory("foo", "bar");
|
2017-07-24 16:20:40 +00:00
|
|
|
assertTypeError(b.toBuffer(), {}, /module is not an object or function/);
|
2017-04-11 20:09:20 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
(function TestLinkingError() {
|
|
|
|
let b;
|
|
|
|
|
2016-10-13 16:17:44 +00:00
|
|
|
b = builder();
|
2016-12-20 15:32:56 +00:00
|
|
|
b.addImport("foo", "bar", kSig_v_v);
|
2017-07-24 16:20:40 +00:00
|
|
|
assertLinkError(
|
|
|
|
b.toBuffer(), {foo: {}}, /function import requires a callable/);
|
2016-10-13 16:17:44 +00:00
|
|
|
b = builder();
|
2016-12-20 15:32:56 +00:00
|
|
|
b.addImport("foo", "bar", kSig_v_v);
|
2017-07-24 16:20:40 +00:00
|
|
|
assertLinkError(
|
|
|
|
b.toBuffer(), {foo: {bar: 9}}, /function import requires a callable/);
|
2016-10-13 16:17:44 +00:00
|
|
|
|
2016-12-16 14:23:35 +00:00
|
|
|
b = builder();
|
2016-12-21 13:43:00 +00:00
|
|
|
b.addImportedGlobal("foo", "bar", kWasmI32);
|
2017-07-24 16:20:40 +00:00
|
|
|
assertLinkError(b.toBuffer(), {foo: {}}, /global import must be a number/);
|
2016-12-16 14:23:35 +00:00
|
|
|
b = builder();
|
2016-12-21 13:43:00 +00:00
|
|
|
b.addImportedGlobal("foo", "bar", kWasmI32);
|
2017-07-24 16:20:40 +00:00
|
|
|
assertLinkError(
|
|
|
|
b.toBuffer(), {foo: {bar: ""}}, /global import must be a number/);
|
2016-12-16 14:23:35 +00:00
|
|
|
b = builder();
|
2016-12-21 13:43:00 +00:00
|
|
|
b.addImportedGlobal("foo", "bar", kWasmI32);
|
2017-07-24 16:20:40 +00:00
|
|
|
assertLinkError(
|
|
|
|
b.toBuffer(), {foo: {bar: () => 9}}, /global import must be a number/);
|
2016-10-13 16:17:44 +00:00
|
|
|
|
|
|
|
b = builder();
|
|
|
|
b.addImportedMemory("foo", "bar");
|
2017-07-24 16:20:40 +00:00
|
|
|
assertLinkError(
|
|
|
|
b.toBuffer(), {foo: {}},
|
|
|
|
/memory import must be a WebAssembly\.Memory object/);
|
2016-12-16 14:23:35 +00:00
|
|
|
b = builder();
|
|
|
|
b.addImportedMemory("foo", "bar", 1);
|
2017-07-24 16:20:40 +00:00
|
|
|
assertLinkError(
|
|
|
|
b.toBuffer(), {foo: {bar: () => new WebAssembly.Memory({initial: 0})}},
|
|
|
|
/memory import must be a WebAssembly\.Memory object/);
|
2016-12-16 14:23:35 +00:00
|
|
|
|
|
|
|
b = builder();
|
2017-01-13 20:47:44 +00:00
|
|
|
b.addFunction("startup", kSig_v_v).addBody([
|
2016-12-16 14:23:35 +00:00
|
|
|
kExprUnreachable,
|
|
|
|
]).end().addStart(0);
|
2017-07-24 16:20:40 +00:00
|
|
|
assertRuntimeError(b.toBuffer(), {}, "unreachable");
|
2016-10-13 16:17:44 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
(function TestTrapError() {
|
|
|
|
assertRuntimeError(builder().addFunction("run", kSig_v_v).addBody([
|
|
|
|
kExprUnreachable
|
2017-07-24 16:20:40 +00:00
|
|
|
]).exportFunc().end().toBuffer(), {}, "unreachable");
|
2016-10-13 16:17:44 +00:00
|
|
|
|
|
|
|
assertRuntimeError(builder().addFunction("run", kSig_v_v).addBody([
|
|
|
|
kExprI32Const, 1,
|
|
|
|
kExprI32Const, 0,
|
|
|
|
kExprI32DivS,
|
|
|
|
kExprDrop
|
2017-07-24 16:20:40 +00:00
|
|
|
]).exportFunc().end().toBuffer(), {}, "divide by zero");
|
2016-10-13 16:17:44 +00:00
|
|
|
|
2017-07-24 16:20:40 +00:00
|
|
|
assertRuntimeError(builder().
|
|
|
|
addFunction("run", kSig_v_v).addBody([]).exportFunc().end().
|
|
|
|
addFunction("start", kSig_v_v).addBody([kExprUnreachable]).end().
|
|
|
|
addStart(1).toBuffer(),
|
|
|
|
{}, "unreachable");
|
2016-10-13 16:17:44 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
(function TestConversionError() {
|
|
|
|
let b = builder();
|
2018-02-02 10:27:11 +00:00
|
|
|
b.addImport('foo', 'bar', kSig_v_l);
|
|
|
|
let buffer = b.addFunction('run', kSig_v_v)
|
|
|
|
.addBody([kExprI64Const, 0, kExprCallFunction, 0])
|
|
|
|
.exportFunc()
|
|
|
|
.end()
|
|
|
|
.toBuffer();
|
|
|
|
assertConversionError(
|
|
|
|
buffer, {foo: {bar: (l) => {}}}, kTrapMsgs[kTrapTypeError]);
|
|
|
|
|
|
|
|
buffer = builder()
|
|
|
|
.addFunction('run', kSig_l_v)
|
|
|
|
.addBody([kExprI64Const, 0])
|
|
|
|
.exportFunc()
|
|
|
|
.end()
|
|
|
|
.toBuffer();
|
|
|
|
assertConversionError(buffer, {}, kTrapMsgs[kTrapTypeError]);
|
2016-10-13 16:17:44 +00:00
|
|
|
})();
|
2017-12-15 21:24:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
(function InternalDebugTrace() {
|
|
|
|
var builder = new WasmModuleBuilder();
|
|
|
|
var sig = builder.addType(kSig_i_dd);
|
|
|
|
builder.addImport("mod", "func", sig);
|
|
|
|
builder.addFunction("main", sig)
|
|
|
|
.addBody([kExprGetLocal, 0, kExprGetLocal, 1, kExprCallFunction, 0])
|
2018-02-02 10:27:11 +00:00
|
|
|
.exportAs("main");
|
2017-12-15 21:24:43 +00:00
|
|
|
var main = builder.instantiate({
|
|
|
|
mod: {
|
|
|
|
func: ()=>{%DebugTrace();}
|
|
|
|
}
|
|
|
|
}).exports.main;
|
|
|
|
main();
|
|
|
|
})();
|