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.
|
|
|
|
|
2015-12-17 10:54:08 +00:00
|
|
|
// Flags: --expose-wasm --expose-gc --stress-compaction
|
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();
|
|
|
|
|
|
|
|
builder.addMemory(1, 1, true);
|
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-23 17:58:07 +00:00
|
|
|
// main body: while(i) { if(mem[i]) return -1; i -= 4; } return 0;
|
|
|
|
kExprLoop,
|
|
|
|
kExprGetLocal,0,
|
|
|
|
kExprIf,
|
|
|
|
kExprGetLocal,0,
|
|
|
|
kExprI32LoadMem,0,0,
|
|
|
|
kExprIf,
|
|
|
|
kExprI8Const,255,
|
|
|
|
kExprReturn, kArity1,
|
|
|
|
kExprEnd,
|
|
|
|
kExprGetLocal,0,
|
|
|
|
kExprI8Const,4,
|
|
|
|
kExprI32Sub,
|
|
|
|
kExprSetLocal,0,
|
|
|
|
kExprBr, kArity1, 1,
|
|
|
|
kExprEnd,
|
|
|
|
kExprEnd,
|
|
|
|
kExprI8Const,0
|
2016-04-29 09:15:26 +00:00
|
|
|
])
|
2016-03-07 19:32:35 +00:00
|
|
|
.exportFunc();
|
2016-09-23 17:58:07 +00:00
|
|
|
|
|
|
|
return builder.instantiate(null, memory);
|
2015-12-11 12:26:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function testPokeMemory() {
|
|
|
|
var module = genModule(null);
|
2016-09-23 17:58:07 +00:00
|
|
|
var buffer = module.exports.memory;
|
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();
|
|
|
|
|
|
|
|
function testSurvivalAcrossGc() {
|
2016-09-23 17:58:07 +00:00
|
|
|
var checker = genModule(null).exports.main;
|
|
|
|
for (var i = 0; i < 5; 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() {
|
|
|
|
var buffer = new ArrayBuffer(kMemSize);
|
|
|
|
var module = genModule(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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
testPokeOuterMemory();
|
|
|
|
|
|
|
|
function testOuterMemorySurvivalAcrossGc() {
|
|
|
|
var buffer = new ArrayBuffer(kMemSize);
|
2016-09-23 17:58:07 +00:00
|
|
|
var checker = genModule(buffer).exports.main;
|
|
|
|
for (var i = 0; i < 5; 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-23 17:58:07 +00:00
|
|
|
kExprI32StoreMem, 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++) {
|
2015-12-11 12:26:16 +00:00
|
|
|
assertTraps(kTrapMemOutOfBounds, read);
|
|
|
|
assertTraps(kTrapMemOutOfBounds, write);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
testOOBThrows();
|