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
|
|
|
|
|
|
|
load("test/mjsunit/wasm/wasm-constants.js");
|
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, // --
|
2016-09-27 20:46:10 +00:00
|
|
|
/* */ kExprGetLocal, 0, // --
|
2017-01-09 13:57:26 +00:00
|
|
|
/* */ kExprIf, kWasmStmt, // --
|
2016-09-27 20:46:10 +00:00
|
|
|
/* */ kExprGetLocal, 0, // --
|
|
|
|
/* */ 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, // --
|
|
|
|
/* */ kExprGetLocal, 0, // --
|
2017-01-09 13:57:26 +00:00
|
|
|
/* */ kExprI32Const, 4, // --
|
2016-09-27 20:46:10 +00:00
|
|
|
/* */ kExprI32Sub, // --
|
|
|
|
/* */ kExprSetLocal, 0, // --
|
|
|
|
/* */ 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);
|
|
|
|
|
|
|
|
for (var i = 0; i < kMemSize; i++) {
|
|
|
|
assertEquals(0, array[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
for (var i = 0; i < kMemSize; i++) {
|
|
|
|
assertEquals(0, array[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
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([
|
2016-04-29 09:15:26 +00:00
|
|
|
kExprGetLocal, 0,
|
|
|
|
kExprGetLocal, 1,
|
|
|
|
kExprI32LoadMem, 0, 0,
|
2016-09-27 20:46:10 +00:00
|
|
|
kExprI32StoreMem, 0, 0,
|
|
|
|
kExprGetLocal, 1,
|
|
|
|
kExprI32LoadMem, 0, 0,
|
2016-03-07 19:32:35 +00:00
|
|
|
])
|
|
|
|
.exportFunc();
|
|
|
|
|
|
|
|
var module = builder.instantiate();
|
2015-12-11 12:26:16 +00:00
|
|
|
var offset;
|
|
|
|
|
2016-03-07 19:32:35 +00:00
|
|
|
function read() { return module.exports.geti(0, offset); }
|
|
|
|
function write() { return module.exports.geti(offset, 0); }
|
2015-12-11 12:26:16 +00:00
|
|
|
|
2016-03-02 23:58:36 +00:00
|
|
|
for (offset = 0; offset < 65533; offset++) {
|
2015-12-11 12:26:16 +00:00
|
|
|
assertEquals(0, read());
|
|
|
|
assertEquals(0, write());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-02 23:58:36 +00:00
|
|
|
for (offset = 65534; offset < 66536; offset++) {
|
2017-09-19 03:36:47 +00:00
|
|
|
const trap_count = %GetWasmRecoveredTrapCount();
|
2015-12-11 12:26:16 +00:00
|
|
|
assertTraps(kTrapMemOutOfBounds, read);
|
|
|
|
assertTraps(kTrapMemOutOfBounds, write);
|
2017-09-19 03:36:47 +00:00
|
|
|
if (%IsWasmTrapHandlerEnabled()) {
|
|
|
|
assertEquals(trap_count + 2, %GetWasmRecoveredTrapCount());
|
|
|
|
}
|
2015-12-11 12:26:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
testOOBThrows();
|