2016-08-17 17:22:09 +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: --validate-asm --allow-natives-syntax
|
|
|
|
|
2016-12-09 11:29:54 +00:00
|
|
|
// Note that this test file contains tests that explicitly check modules are
|
|
|
|
// valid asm.js and then break them with invalid instantiation arguments. If
|
|
|
|
// this script is run more than once (e.g. --stress-opt) then modules remain
|
|
|
|
// broken in the second run and assertions would fail. We prevent re-runs.
|
|
|
|
// Flags: --nostress-opt
|
|
|
|
|
2016-08-23 04:06:52 +00:00
|
|
|
function assertValidAsm(func) {
|
|
|
|
assertTrue(%IsAsmWasmCode(func));
|
2016-08-17 17:22:09 +00:00
|
|
|
}
|
|
|
|
|
2016-10-20 15:48:27 +00:00
|
|
|
(function TestConst() {
|
|
|
|
function Module(s) {
|
|
|
|
"use asm";
|
|
|
|
var fround = s.Math.fround;
|
|
|
|
// Global constants. These are treated just like numeric literals.
|
|
|
|
const fConst = fround(-3.0);
|
|
|
|
const dConst = -3.0;
|
|
|
|
const iConst = -3;
|
|
|
|
|
|
|
|
// consts can be used to initialize other consts.
|
|
|
|
const fPrime = fConst;
|
|
|
|
|
|
|
|
// The following methods verify that return statements with global constants
|
|
|
|
// do not need type annotations.
|
|
|
|
function f() {
|
|
|
|
return fPrime;
|
|
|
|
}
|
|
|
|
function d() {
|
|
|
|
return dConst;
|
|
|
|
}
|
|
|
|
function i() {
|
|
|
|
return iConst;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The following methods verify that locals initialized with global
|
|
|
|
// constants do not need type annotations.
|
|
|
|
function fVar() {
|
|
|
|
var v = fPrime;
|
|
|
|
return fround(v);
|
|
|
|
}
|
|
|
|
function iVar() {
|
|
|
|
var v = iConst;
|
|
|
|
return v|0;
|
|
|
|
}
|
|
|
|
function dVar() {
|
|
|
|
var v = dConst;
|
|
|
|
return +v;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
f: f, d: d, i: i,
|
|
|
|
fVar: fVar, dVar: dVar, iVar: iVar,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function DisallowAssignToConstGlobal() {
|
|
|
|
const constant = 0;
|
|
|
|
function invalid(i) {
|
|
|
|
i = i|0;
|
|
|
|
constant = i;
|
|
|
|
return constant;
|
|
|
|
}
|
|
|
|
return invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
var m = Module(this);
|
|
|
|
assertValidAsm(Module);
|
|
|
|
|
|
|
|
assertEquals(-3, m.i());
|
|
|
|
assertEquals(-3.0, m.d());
|
|
|
|
assertEquals(Math.fround(-3.0), m.f());
|
|
|
|
|
|
|
|
assertEquals(-3, m.iVar());
|
|
|
|
assertEquals(-3.0, m.dVar());
|
|
|
|
assertEquals(Math.fround(-3.0), m.fVar());
|
|
|
|
|
|
|
|
var m = DisallowAssignToConstGlobal();
|
2016-12-09 11:55:40 +00:00
|
|
|
assertFalse(%IsAsmWasmCode(DisallowAssignToConstGlobal));
|
2016-10-20 15:48:27 +00:00
|
|
|
})();
|
|
|
|
|
2016-08-17 17:22:09 +00:00
|
|
|
(function TestModuleArgs() {
|
|
|
|
function Module1(stdlib) {
|
|
|
|
"use asm";
|
|
|
|
function foo() { }
|
|
|
|
return { foo: foo };
|
|
|
|
}
|
|
|
|
function Module2(stdlib, ffi) {
|
|
|
|
"use asm";
|
|
|
|
function foo() { }
|
|
|
|
return { foo: foo };
|
|
|
|
}
|
|
|
|
function Module3(stdlib, ffi, heap) {
|
|
|
|
"use asm";
|
|
|
|
function foo() { }
|
|
|
|
return { foo: foo };
|
|
|
|
}
|
|
|
|
var modules = [Module1, Module2, Module3];
|
|
|
|
var heap = new ArrayBuffer(1024 * 1024);
|
|
|
|
for (var i = 0; i < modules.length; ++i) {
|
|
|
|
print('Module' + (i + 1));
|
|
|
|
var module = modules[i];
|
2016-08-23 04:06:52 +00:00
|
|
|
var m = module();
|
|
|
|
assertValidAsm(module);
|
2016-08-17 17:22:09 +00:00
|
|
|
var m = module({});
|
2016-08-23 04:06:52 +00:00
|
|
|
assertValidAsm(module);
|
2016-08-17 17:22:09 +00:00
|
|
|
var m = module({}, {});
|
2016-08-23 04:06:52 +00:00
|
|
|
assertValidAsm(module);
|
2016-08-17 17:22:09 +00:00
|
|
|
var m = module({}, {}, heap);
|
2016-08-23 04:06:52 +00:00
|
|
|
assertValidAsm(module);
|
2016-08-17 17:22:09 +00:00
|
|
|
var m = module({}, {}, heap, {});
|
2016-08-23 04:06:52 +00:00
|
|
|
assertValidAsm(module);
|
2016-08-17 17:22:09 +00:00
|
|
|
}
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function TestBadModule() {
|
|
|
|
function Module(stdlib, ffi, heap) {
|
|
|
|
"use asm";
|
|
|
|
function foo() { var y = 3; var x = 1 + y; return 123; }
|
|
|
|
return { foo: foo };
|
|
|
|
}
|
|
|
|
var m = Module({});
|
2016-12-09 11:55:40 +00:00
|
|
|
assertFalse(%IsAsmWasmCode(Module));
|
2016-08-17 17:22:09 +00:00
|
|
|
assertEquals(123, m.foo());
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function TestBadArgTypes() {
|
|
|
|
function Module(a, b, c) {
|
|
|
|
"use asm";
|
2016-08-23 04:06:52 +00:00
|
|
|
var NaN = a.NaN;
|
2016-08-17 17:22:09 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
var m = Module(1, 2, 3);
|
2016-12-09 11:55:40 +00:00
|
|
|
assertFalse(%IsAsmWasmCode(Module));
|
2016-08-17 17:22:09 +00:00
|
|
|
assertEquals({}, m);
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function TestBadArgTypesMismatch() {
|
|
|
|
function Module(a, b, c) {
|
|
|
|
"use asm";
|
2016-08-23 04:06:52 +00:00
|
|
|
var NaN = a.NaN;
|
2016-08-17 17:22:09 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
var m = Module(1, 2);
|
2016-12-09 11:55:40 +00:00
|
|
|
assertFalse(%IsAsmWasmCode(Module));
|
2016-08-17 17:22:09 +00:00
|
|
|
assertEquals({}, m);
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function TestModuleNoStdlib() {
|
|
|
|
function Module() {
|
|
|
|
"use asm";
|
|
|
|
function foo() { return 123; }
|
|
|
|
return { foo: foo };
|
|
|
|
}
|
|
|
|
var m = Module({});
|
2016-08-23 04:06:52 +00:00
|
|
|
assertValidAsm(Module);
|
2016-08-17 17:22:09 +00:00
|
|
|
assertEquals(123, m.foo());
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function TestModuleWith5() {
|
|
|
|
function Module(a, b, c, d, e) {
|
|
|
|
"use asm";
|
|
|
|
function foo() { return 123; }
|
|
|
|
return { foo: foo };
|
|
|
|
}
|
|
|
|
var heap = new ArrayBuffer(1024 * 1024);
|
|
|
|
var m = Module({}, {}, heap);
|
2016-12-09 11:55:40 +00:00
|
|
|
assertFalse(%IsAsmWasmCode(Module));
|
2016-08-17 17:22:09 +00:00
|
|
|
assertEquals(123, m.foo());
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function TestModuleNoStdlibCall() {
|
|
|
|
function Module(stdlib, ffi, heap) {
|
|
|
|
"use asm";
|
|
|
|
function foo() { return 123; }
|
|
|
|
return { foo: foo };
|
|
|
|
}
|
|
|
|
var m = Module();
|
2016-08-23 04:06:52 +00:00
|
|
|
assertValidAsm(Module);
|
2016-08-17 17:22:09 +00:00
|
|
|
assertEquals(123, m.foo());
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function TestModuleNew() {
|
|
|
|
function Module(stdlib, ffi, heap) {
|
|
|
|
"use asm";
|
|
|
|
function foo() { return 123; }
|
|
|
|
return { foo: foo };
|
|
|
|
}
|
|
|
|
var m = new Module({}, {});
|
2016-08-23 04:06:52 +00:00
|
|
|
assertValidAsm(Module);
|
2016-08-17 17:22:09 +00:00
|
|
|
assertEquals(123, m.foo());
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function TestMultipleFailures() {
|
|
|
|
function Module(stdlib) {
|
|
|
|
"use asm";
|
2016-08-23 04:06:52 +00:00
|
|
|
var NaN = stdlib.NaN;
|
2016-08-17 17:22:09 +00:00
|
|
|
function foo() { return 123; }
|
|
|
|
return { foo: foo };
|
|
|
|
}
|
|
|
|
var m1 = Module(1, 2, 3);
|
2016-12-09 11:55:40 +00:00
|
|
|
assertFalse(%IsAsmWasmCode(Module));
|
2016-08-17 17:22:09 +00:00
|
|
|
var m2 = Module(1, 2, 3);
|
2016-12-09 11:55:40 +00:00
|
|
|
assertFalse(%IsAsmWasmCode(Module));
|
2016-08-17 17:22:09 +00:00
|
|
|
assertEquals(123, m1.foo());
|
|
|
|
assertEquals(123, m2.foo());
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function TestFailureThenSuccess() {
|
|
|
|
function MkModule() {
|
|
|
|
function Module(stdlib, ffi, heap) {
|
|
|
|
"use asm";
|
2016-08-23 04:06:52 +00:00
|
|
|
var NaN = stdlib.NaN;
|
2016-08-17 17:22:09 +00:00
|
|
|
function foo() { return 123; }
|
|
|
|
return { foo: foo };
|
|
|
|
}
|
|
|
|
return Module;
|
|
|
|
}
|
|
|
|
var Module1 = MkModule();
|
|
|
|
var Module2 = MkModule();
|
|
|
|
var heap = new ArrayBuffer(1024 * 1024);
|
|
|
|
var m1 = Module1(1, 2, 3);
|
2016-12-09 11:55:40 +00:00
|
|
|
assertFalse(%IsAsmWasmCode(Module1));
|
2016-08-17 17:22:09 +00:00
|
|
|
var m2 = Module2({}, {}, heap);
|
2016-12-09 11:55:40 +00:00
|
|
|
assertFalse(%IsAsmWasmCode(Module2));
|
2016-08-17 17:22:09 +00:00
|
|
|
assertEquals(123, m1.foo());
|
|
|
|
assertEquals(123, m2.foo());
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function TestSuccessThenFailure() {
|
|
|
|
function MkModule() {
|
|
|
|
function Module(stdlib, ffi, heap) {
|
|
|
|
"use asm";
|
2016-08-23 04:06:52 +00:00
|
|
|
var NaN = stdlib.NaN;
|
2016-08-17 17:22:09 +00:00
|
|
|
function foo() { return 123; }
|
|
|
|
return { foo: foo };
|
|
|
|
}
|
|
|
|
return Module;
|
|
|
|
}
|
|
|
|
var Module1 = MkModule();
|
|
|
|
var Module2 = MkModule();
|
|
|
|
var heap = new ArrayBuffer(1024 * 1024);
|
2016-08-23 04:06:52 +00:00
|
|
|
var m1 = Module1({NaN: NaN}, {}, heap);
|
|
|
|
assertValidAsm(Module1);
|
2016-08-17 17:22:09 +00:00
|
|
|
var m2 = Module2(1, 2, 3);
|
2016-12-09 11:55:40 +00:00
|
|
|
assertFalse(%IsAsmWasmCode(Module2));
|
2016-08-17 17:22:09 +00:00
|
|
|
assertEquals(123, m1.foo());
|
|
|
|
assertEquals(123, m2.foo());
|
|
|
|
})();
|
|
|
|
|
2016-08-18 07:44:15 +00:00
|
|
|
(function TestSuccessThenFailureThenRetry() {
|
|
|
|
function MkModule() {
|
|
|
|
function Module(stdlib, ffi, heap) {
|
|
|
|
"use asm";
|
2016-08-23 04:06:52 +00:00
|
|
|
var NaN = stdlib.NaN;
|
2016-08-18 07:44:15 +00:00
|
|
|
function foo() { return 123; }
|
|
|
|
return { foo: foo };
|
|
|
|
}
|
|
|
|
return Module;
|
|
|
|
}
|
|
|
|
var Module1 = MkModule();
|
|
|
|
var Module2 = MkModule();
|
|
|
|
var heap = new ArrayBuffer(1024 * 1024);
|
2016-08-23 04:06:52 +00:00
|
|
|
var m1a = Module1({NaN: NaN}, {}, heap);
|
|
|
|
assertValidAsm(Module1);
|
2016-08-18 07:44:15 +00:00
|
|
|
var m2 = Module2(1, 2, 3);
|
2016-12-09 11:55:40 +00:00
|
|
|
assertFalse(%IsAsmWasmCode(Module2));
|
2016-08-23 04:06:52 +00:00
|
|
|
var m1b = Module1({NaN: NaN}, {}, heap);
|
2016-12-09 11:55:40 +00:00
|
|
|
assertFalse(%IsAsmWasmCode(Module1));
|
2016-08-18 07:44:15 +00:00
|
|
|
assertEquals(123, m1a.foo());
|
|
|
|
assertEquals(123, m1b.foo());
|
|
|
|
assertEquals(123, m2.foo());
|
|
|
|
})();
|
|
|
|
|
2016-08-17 17:22:09 +00:00
|
|
|
(function TestBoundFunction() {
|
|
|
|
function Module(stdlib, ffi, heap) {
|
|
|
|
"use asm";
|
|
|
|
function foo() { return 123; }
|
|
|
|
return { foo: foo };
|
|
|
|
}
|
|
|
|
var heap = new ArrayBuffer(1024 * 1024);
|
|
|
|
var ModuleBound = Module.bind(this, {}, {}, heap);
|
|
|
|
var m = ModuleBound();
|
2016-08-23 04:06:52 +00:00
|
|
|
assertValidAsm(Module);
|
2016-08-17 17:22:09 +00:00
|
|
|
assertEquals(123, m.foo());
|
|
|
|
})();
|
2016-11-08 23:31:39 +00:00
|
|
|
|
|
|
|
(function TestBadConstUnsignedReturn() {
|
|
|
|
function Module() {
|
|
|
|
"use asm";
|
|
|
|
const i = 0xffffffff;
|
|
|
|
function foo() { return i; }
|
|
|
|
return { foo: foo };
|
|
|
|
}
|
|
|
|
var m = Module();
|
2016-12-09 11:55:40 +00:00
|
|
|
assertFalse(%IsAsmWasmCode(Module));
|
2016-11-08 23:31:39 +00:00
|
|
|
assertEquals(0xffffffff, m.foo());
|
|
|
|
})();
|
2016-12-08 14:54:33 +00:00
|
|
|
|
2016-12-09 15:00:56 +00:00
|
|
|
(function TestBadBooleanParamAnnotation() {
|
2016-12-08 14:54:33 +00:00
|
|
|
function Module() {
|
|
|
|
"use asm";
|
|
|
|
function foo(x) {
|
|
|
|
x = x | true;
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
return { foo: foo };
|
|
|
|
}
|
|
|
|
var m = Module();
|
2016-12-09 11:55:40 +00:00
|
|
|
assertFalse(%IsAsmWasmCode(Module));
|
2016-12-08 14:54:33 +00:00
|
|
|
assertEquals(3, m.foo(3));
|
|
|
|
})();
|
|
|
|
|
2016-12-12 14:46:47 +00:00
|
|
|
(function TestBadExportTwice() {
|
|
|
|
function Module() {
|
|
|
|
"use asm";
|
|
|
|
function bar() { return 1; }
|
|
|
|
function baz() { return 2; }
|
|
|
|
return {foo: bar, foo: baz};
|
|
|
|
}
|
|
|
|
var m = Module();
|
|
|
|
assertTrue(%IsAsmWasmCode(Module));
|
|
|
|
assertEquals(2, m.foo());
|
|
|
|
})();
|
|
|
|
|
2016-12-09 15:58:16 +00:00
|
|
|
(function TestBadImport() {
|
|
|
|
function Module(stdlib) {
|
|
|
|
"use asm";
|
|
|
|
var set = 0;
|
|
|
|
var foo = stdlib[set];
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
var m = Module(this);
|
|
|
|
assertFalse(%IsAsmWasmCode(Module));
|
|
|
|
})();
|
|
|
|
|
2016-12-12 13:51:06 +00:00
|
|
|
(function TestBadFroundTrue() {
|
|
|
|
function Module(stdlib) {
|
|
|
|
"use asm";
|
|
|
|
var fround = stdlib.Math.fround;
|
|
|
|
function foo() {
|
|
|
|
var x = fround(true);
|
|
|
|
return +x;
|
|
|
|
}
|
|
|
|
return { foo: foo };
|
|
|
|
}
|
|
|
|
var m = Module(this);
|
|
|
|
assertFalse(%IsAsmWasmCode(Module));
|
|
|
|
assertEquals(1, m.foo());
|
|
|
|
})();
|
|
|
|
|
2016-12-08 14:54:33 +00:00
|
|
|
(function TestBadCase() {
|
|
|
|
function Module() {
|
|
|
|
"use asm";
|
|
|
|
function foo(x) {
|
|
|
|
x = x | 0;
|
|
|
|
switch (x|0) {
|
|
|
|
case true:
|
|
|
|
return 42;
|
|
|
|
default:
|
|
|
|
return 43;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return { foo: foo };
|
|
|
|
}
|
|
|
|
var m = Module();
|
2016-12-09 11:55:40 +00:00
|
|
|
assertFalse(%IsAsmWasmCode(Module));
|
2016-12-08 14:54:33 +00:00
|
|
|
assertEquals(43, m.foo(3));
|
|
|
|
})();
|
2016-12-12 12:26:03 +00:00
|
|
|
|
|
|
|
(function TestVarHidesExport() {
|
|
|
|
function Module() {
|
|
|
|
"use asm";
|
|
|
|
var foo;
|
|
|
|
function foo() {}
|
|
|
|
return foo;
|
|
|
|
}
|
|
|
|
Module();
|
|
|
|
assertFalse(%IsAsmWasmCode(Module));
|
|
|
|
})();
|
2017-01-10 04:07:16 +00:00
|
|
|
|
|
|
|
(function TestUndefinedGlobalCall() {
|
|
|
|
function Module() {
|
|
|
|
"use asm";
|
|
|
|
function foo() {
|
|
|
|
return bar() | 0;
|
|
|
|
}
|
|
|
|
return foo;
|
|
|
|
}
|
|
|
|
Module();
|
|
|
|
assertFalse(%IsAsmWasmCode(Module));
|
|
|
|
})();
|
2017-01-26 08:12:07 +00:00
|
|
|
|
|
|
|
(function TestConditionalReturn() {
|
|
|
|
function Module() {
|
|
|
|
'use asm';
|
|
|
|
function foo(a, b) {
|
|
|
|
a = +a;
|
|
|
|
b = +b;
|
|
|
|
// Allowed, despite not matching the spec, as emscripten emits this in
|
|
|
|
// practice.
|
|
|
|
return a == b ? +a : +b;
|
|
|
|
}
|
|
|
|
return foo;
|
|
|
|
}
|
|
|
|
var m = Module();
|
|
|
|
assertEquals(4, m(4, 4));
|
|
|
|
assertEquals(5, m(4, 5));
|
|
|
|
assertEquals(4, m(5, 4));
|
|
|
|
assertValidAsm(Module);
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function TestMismatchedConditionalReturn() {
|
|
|
|
function Module() {
|
|
|
|
'use asm';
|
|
|
|
function foo(a, b) {
|
|
|
|
a = +a;
|
|
|
|
return a == 0.0 ? 0 : +a;
|
|
|
|
}
|
|
|
|
return foo;
|
|
|
|
}
|
|
|
|
Module();
|
2017-02-28 10:00:46 +00:00
|
|
|
assertFalse(%IsAsmWasmCode(Module));
|
2017-01-26 08:12:07 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
(function TestBadIntConditionalReturn() {
|
|
|
|
function Module() {
|
|
|
|
'use asm';
|
|
|
|
function foo(a, b) {
|
|
|
|
a = a | 0;
|
|
|
|
b = b | 0;
|
|
|
|
// Disallowed because signature must be signed, but these will be int.
|
|
|
|
return 1 ? a : b;
|
|
|
|
}
|
|
|
|
return foo;
|
|
|
|
}
|
|
|
|
Module();
|
2017-02-28 10:00:46 +00:00
|
|
|
assertFalse(%IsAsmWasmCode(Module));
|
2017-01-26 08:12:07 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
(function TestBadSignedConditionalReturn() {
|
|
|
|
function Module() {
|
|
|
|
'use asm';
|
|
|
|
function foo(a, b) {
|
|
|
|
a = a | 0;
|
|
|
|
b = b | 0;
|
|
|
|
// Disallowed because conditional yields int, even when both sides
|
|
|
|
// are signed.
|
|
|
|
return 1 ? a | 0 : b | 0;
|
|
|
|
}
|
|
|
|
return foo;
|
|
|
|
}
|
|
|
|
Module();
|
2017-02-28 10:00:46 +00:00
|
|
|
assertFalse(%IsAsmWasmCode(Module));
|
2017-01-26 08:12:07 +00:00
|
|
|
})();
|
2017-01-28 23:15:58 +00:00
|
|
|
|
|
|
|
(function TestAsmIsRegular() {
|
|
|
|
function Module() {
|
|
|
|
'use asm';
|
|
|
|
var g = 123;
|
|
|
|
function foo() {
|
|
|
|
return g | 0;
|
|
|
|
}
|
|
|
|
return {x: foo};
|
|
|
|
}
|
|
|
|
var o = Module();
|
|
|
|
assertValidAsm(Module);
|
|
|
|
assertFalse(o instanceof WebAssembly.Instance);
|
|
|
|
assertTrue(o instanceof Object);
|
|
|
|
assertTrue(o.__proto__ === Object.prototype);
|
|
|
|
o.x = 5;
|
|
|
|
assertTrue(typeof o.x === 'number');
|
|
|
|
assertTrue(o.__single_function__ === undefined);
|
|
|
|
assertTrue(o.__foreign_init__ === undefined);
|
|
|
|
})();
|