2015-12-11 12:26:16 +00:00
|
|
|
// Copyright 2015 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-09-19 03:36:47 +00:00
|
|
|
// Flags: --expose-wasm --expose-gc --stress-compaction --allow-natives-syntax
|
2015-12-11 12:26:16 +00:00
|
|
|
|
2016-03-07 19:32:35 +00:00
|
|
|
load("test/mjsunit/wasm/wasm-module-builder.js");
|
2015-12-11 12:26:16 +00:00
|
|
|
|
2016-03-02 23:58:36 +00:00
|
|
|
var kMemSize = 65536;
|
2015-12-11 12:26:16 +00:00
|
|
|
|
|
|
|
function genModule(memory) {
|
2016-03-07 19:32:35 +00:00
|
|
|
var builder = new WasmModuleBuilder();
|
|
|
|
|
2017-01-17 19:15:59 +00:00
|
|
|
builder.addImportedMemory("", "memory", 1);
|
2017-01-12 16:48:09 +00:00
|
|
|
builder.exportMemoryAs("memory");
|
2016-04-29 09:39:26 +00:00
|
|
|
builder.addFunction("main", kSig_i_i)
|
2016-03-07 19:32:35 +00:00
|
|
|
.addBody([
|
2016-09-27 20:46:10 +00:00
|
|
|
// main body: while(i) { if(mem[i]) return -1; i -= 4; } return 0;
|
|
|
|
// TODO(titzer): this manual bytecode has a copy of test-run-wasm.cc
|
2017-01-09 13:57:26 +00:00
|
|
|
/**/ kExprLoop, kWasmStmt, // --
|
2019-10-08 12:38:48 +00:00
|
|
|
/* */ kExprLocalGet, 0, // --
|
2017-01-09 13:57:26 +00:00
|
|
|
/* */ kExprIf, kWasmStmt, // --
|
2019-10-08 12:38:48 +00:00
|
|
|
/* */ kExprLocalGet, 0, // --
|
2016-09-27 20:46:10 +00:00
|
|
|
/* */ kExprI32LoadMem, 0, 0, // --
|
2017-01-09 13:57:26 +00:00
|
|
|
/* */ kExprIf, kWasmStmt, // --
|
|
|
|
/* */ kExprI32Const, 127, // --
|
2016-09-27 20:46:10 +00:00
|
|
|
/* */ kExprReturn, // --
|
|
|
|
/* */ kExprEnd, // --
|
2019-10-08 12:38:48 +00:00
|
|
|
/* */ kExprLocalGet, 0, // --
|
2017-01-09 13:57:26 +00:00
|
|
|
/* */ kExprI32Const, 4, // --
|
2016-09-27 20:46:10 +00:00
|
|
|
/* */ kExprI32Sub, // --
|
2019-10-08 12:38:48 +00:00
|
|
|
/* */ kExprLocalSet, 0, // --
|
2016-09-27 20:46:10 +00:00
|
|
|
/* */ kExprBr, 1, // --
|
|
|
|
/* */ kExprEnd, // --
|
|
|
|
/* */ kExprEnd, // --
|
2017-01-09 13:57:26 +00:00
|
|
|
/**/ kExprI32Const, 0 // --
|
2016-04-29 09:15:26 +00:00
|
|
|
])
|
2016-03-07 19:32:35 +00:00
|
|
|
.exportFunc();
|
2017-01-12 16:48:09 +00:00
|
|
|
var module = builder.instantiate({"": {memory:memory}});
|
2016-09-27 20:46:10 +00:00
|
|
|
assertTrue(module.exports.memory instanceof WebAssembly.Memory);
|
2016-10-28 18:13:01 +00:00
|
|
|
if (memory != null) assertEquals(memory.buffer, module.exports.memory.buffer);
|
2016-09-27 20:46:10 +00:00
|
|
|
return module;
|
2015-12-11 12:26:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function testPokeMemory() {
|
2016-10-28 18:13:01 +00:00
|
|
|
print("testPokeMemory");
|
2017-01-12 16:48:09 +00:00
|
|
|
var module = genModule(new WebAssembly.Memory({initial: 1}));
|
2016-09-27 20:46:10 +00:00
|
|
|
var buffer = module.exports.memory.buffer;
|
2016-03-07 19:32:35 +00:00
|
|
|
var main = module.exports.main;
|
2015-12-11 12:26:16 +00:00
|
|
|
assertEquals(kMemSize, buffer.byteLength);
|
|
|
|
|
|
|
|
var array = new Int8Array(buffer);
|
|
|
|
assertEquals(kMemSize, array.length);
|
|
|
|
|
2019-09-09 23:32:45 +00:00
|
|
|
assertTrue(array.every((e => e === 0)));
|
2015-12-11 12:26:16 +00:00
|
|
|
|
|
|
|
for (var i = 0; i < 10; i++) {
|
2016-03-07 19:32:35 +00:00
|
|
|
assertEquals(0, main(kMemSize - 4));
|
2015-12-11 12:26:16 +00:00
|
|
|
|
|
|
|
array[kMemSize/2 + i] = 1;
|
2016-03-07 19:32:35 +00:00
|
|
|
assertEquals(0, main(kMemSize/2 - 4));
|
|
|
|
assertEquals(-1, main(kMemSize - 4));
|
2015-12-11 12:26:16 +00:00
|
|
|
|
|
|
|
array[kMemSize/2 + i] = 0;
|
2016-03-07 19:32:35 +00:00
|
|
|
assertEquals(0, main(kMemSize - 4));
|
2015-12-11 12:26:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
testPokeMemory();
|
|
|
|
|
2016-09-27 20:46:10 +00:00
|
|
|
function genAndGetMain(buffer) {
|
|
|
|
return genModule(buffer).exports.main; // to prevent intermediates living
|
|
|
|
}
|
|
|
|
|
2015-12-11 12:26:16 +00:00
|
|
|
function testSurvivalAcrossGc() {
|
2017-01-12 16:48:09 +00:00
|
|
|
var checker = genAndGetMain(new WebAssembly.Memory({initial: 1}));
|
2016-09-27 20:46:10 +00:00
|
|
|
for (var i = 0; i < 3; i++) {
|
2015-12-11 12:26:16 +00:00
|
|
|
print("gc run ", i);
|
|
|
|
assertEquals(0, checker(kMemSize - 4));
|
|
|
|
gc();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
testSurvivalAcrossGc();
|
|
|
|
testSurvivalAcrossGc();
|
|
|
|
testSurvivalAcrossGc();
|
|
|
|
testSurvivalAcrossGc();
|
|
|
|
|
|
|
|
|
|
|
|
function testPokeOuterMemory() {
|
2016-10-28 18:13:01 +00:00
|
|
|
print("testPokeOuterMemory");
|
|
|
|
var buffer = new WebAssembly.Memory({initial: kMemSize / kPageSize});
|
2015-12-11 12:26:16 +00:00
|
|
|
var module = genModule(buffer);
|
2016-03-07 19:32:35 +00:00
|
|
|
var main = module.exports.main;
|
2016-10-28 18:13:01 +00:00
|
|
|
assertEquals(kMemSize, buffer.buffer.byteLength);
|
2015-12-11 12:26:16 +00:00
|
|
|
|
2016-10-28 18:13:01 +00:00
|
|
|
var array = new Int8Array(buffer.buffer);
|
2015-12-11 12:26:16 +00:00
|
|
|
assertEquals(kMemSize, array.length);
|
|
|
|
|
2019-09-09 23:32:45 +00:00
|
|
|
assertTrue(array.every((e => e === 0)));
|
2015-12-11 12:26:16 +00:00
|
|
|
|
|
|
|
for (var i = 0; i < 10; i++) {
|
2016-03-07 19:32:35 +00:00
|
|
|
assertEquals(0, main(kMemSize - 4));
|
2015-12-11 12:26:16 +00:00
|
|
|
|
|
|
|
array[kMemSize/2 + i] = 1;
|
2016-03-07 19:32:35 +00:00
|
|
|
assertEquals(0, main(kMemSize/2 - 4));
|
|
|
|
assertEquals(-1, main(kMemSize - 4));
|
2015-12-11 12:26:16 +00:00
|
|
|
|
|
|
|
array[kMemSize/2 + i] = 0;
|
2016-03-07 19:32:35 +00:00
|
|
|
assertEquals(0, main(kMemSize - 4));
|
2015-12-11 12:26:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
testPokeOuterMemory();
|
|
|
|
|
|
|
|
function testOuterMemorySurvivalAcrossGc() {
|
2016-10-28 18:13:01 +00:00
|
|
|
var buffer = new WebAssembly.Memory({initial: kMemSize / kPageSize});
|
2016-09-27 20:46:10 +00:00
|
|
|
var checker = genAndGetMain(buffer);
|
|
|
|
for (var i = 0; i < 3; i++) {
|
2015-12-11 12:26:16 +00:00
|
|
|
print("gc run ", i);
|
|
|
|
assertEquals(0, checker(kMemSize - 4));
|
|
|
|
gc();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
testOuterMemorySurvivalAcrossGc();
|
|
|
|
testOuterMemorySurvivalAcrossGc();
|
|
|
|
testOuterMemorySurvivalAcrossGc();
|
|
|
|
testOuterMemorySurvivalAcrossGc();
|
|
|
|
|
|
|
|
|
|
|
|
function testOOBThrows() {
|
2016-03-07 19:32:35 +00:00
|
|
|
var builder = new WasmModuleBuilder();
|
|
|
|
|
|
|
|
builder.addMemory(1, 1, true);
|
2016-04-29 09:39:26 +00:00
|
|
|
builder.addFunction("geti", kSig_i_ii)
|
2016-03-07 19:32:35 +00:00
|
|
|
.addBody([
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalGet, 0,
|
|
|
|
kExprLocalGet, 1,
|
2016-04-29 09:15:26 +00:00
|
|
|
kExprI32LoadMem, 0, 0,
|
2016-09-27 20:46:10 +00:00
|
|
|
kExprI32StoreMem, 0, 0,
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalGet, 1,
|
2016-09-27 20:46:10 +00:00
|
|
|
kExprI32LoadMem, 0, 0,
|
2016-03-07 19:32:35 +00:00
|
|
|
])
|
|
|
|
.exportFunc();
|
|
|
|
|
|
|
|
var module = builder.instantiate();
|
2015-12-11 12:26:16 +00:00
|
|
|
|
2019-09-12 16:45:49 +00:00
|
|
|
let read = offset => module.exports.geti(0, offset);
|
|
|
|
let write = offset => module.exports.geti(offset, 0);
|
2015-12-11 12:26:16 +00:00
|
|
|
|
2019-09-12 16:45:49 +00:00
|
|
|
assertEquals(0, read(65532));
|
|
|
|
assertEquals(0, write(65532));
|
2015-12-11 12:26:16 +00:00
|
|
|
|
2018-08-24 11:43:17 +00:00
|
|
|
// Note that this test might be run concurrently in multiple Isolates, which
|
|
|
|
// makes an exact comparison of the expected trap count unreliable. But is is
|
|
|
|
// still possible to check the lower bound for the expected trap count.
|
2019-09-12 16:45:49 +00:00
|
|
|
for (let offset = 65534; offset < 66536; offset++) {
|
2017-09-19 03:36:47 +00:00
|
|
|
const trap_count = %GetWasmRecoveredTrapCount();
|
2019-09-12 16:45:49 +00:00
|
|
|
assertTraps(kTrapMemOutOfBounds, () => read(offset));
|
|
|
|
assertTraps(kTrapMemOutOfBounds, () => write(offset));
|
2017-09-19 03:36:47 +00:00
|
|
|
if (%IsWasmTrapHandlerEnabled()) {
|
2018-08-24 13:36:09 +00:00
|
|
|
assertTrue(trap_count + 2 <= %GetWasmRecoveredTrapCount());
|
2017-09-19 03:36:47 +00:00
|
|
|
}
|
2015-12-11 12:26:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
testOOBThrows();
|
2018-01-06 01:33:31 +00:00
|
|
|
|
|
|
|
function testAddressSpaceLimit() {
|
2018-06-07 00:49:00 +00:00
|
|
|
// 1TiB + 4 GiB, see wasm-memory.h
|
|
|
|
const kMaxAddressSpace = 1 * 1024 * 1024 * 1024 * 1024
|
|
|
|
+ 4 * 1024 * 1024 * 1024;
|
2019-04-17 10:21:50 +00:00
|
|
|
const kAddressSpacePerMemory = 10 * 1024 * 1024 * 1024;
|
2018-01-06 01:33:31 +00:00
|
|
|
|
2018-06-07 00:49:00 +00:00
|
|
|
let last_memory;
|
2018-01-06 01:33:31 +00:00
|
|
|
try {
|
|
|
|
let memories = [];
|
|
|
|
let address_space = 0;
|
|
|
|
while (address_space <= kMaxAddressSpace + 1) {
|
2018-06-07 00:49:00 +00:00
|
|
|
last_memory = new WebAssembly.Memory({initial: 1})
|
|
|
|
memories.push(last_memory);
|
2018-01-06 01:33:31 +00:00
|
|
|
address_space += kAddressSpacePerMemory;
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
assertTrue(e instanceof RangeError);
|
|
|
|
return;
|
|
|
|
}
|
2019-04-17 10:21:50 +00:00
|
|
|
assertUnreachable("should have reached the address space limit");
|
2018-01-06 01:33:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(%IsWasmTrapHandlerEnabled()) {
|
|
|
|
testAddressSpaceLimit();
|
|
|
|
}
|