2016-06-28 16:46:34 +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.
|
|
|
|
|
2016-10-20 07:38:55 +00:00
|
|
|
// Flags: --expose-wasm --stress-compaction
|
2016-06-28 16:46:34 +00:00
|
|
|
|
2021-06-01 12:46:36 +00:00
|
|
|
d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
|
2016-06-28 16:46:34 +00:00
|
|
|
|
|
|
|
|
2018-10-26 17:28:37 +00:00
|
|
|
function genMemoryGrowBuilder() {
|
2016-06-28 16:46:34 +00:00
|
|
|
var builder = new WasmModuleBuilder();
|
|
|
|
builder.addFunction("grow_memory", kSig_i_i)
|
2019-10-08 12:38:48 +00:00
|
|
|
.addBody([kExprLocalGet, 0, kExprMemoryGrow, kMemoryZero])
|
2016-06-28 16:46:34 +00:00
|
|
|
.exportFunc();
|
|
|
|
builder.addFunction("load", kSig_i_i)
|
2019-10-08 12:38:48 +00:00
|
|
|
.addBody([kExprLocalGet, 0, kExprI32LoadMem, 0, 0])
|
2016-06-28 16:46:34 +00:00
|
|
|
.exportFunc();
|
|
|
|
builder.addFunction("store", kSig_i_ii)
|
2019-10-08 12:38:48 +00:00
|
|
|
.addBody([kExprLocalGet, 0, kExprLocalGet, 1, kExprI32StoreMem, 0, 0,
|
|
|
|
kExprLocalGet, 1])
|
2016-06-28 16:46:34 +00:00
|
|
|
.exportFunc();
|
2016-09-12 07:32:42 +00:00
|
|
|
builder.addFunction("load16", kSig_i_i)
|
2019-10-08 12:38:48 +00:00
|
|
|
.addBody([kExprLocalGet, 0, kExprI32LoadMem16U, 0, 0])
|
2016-09-12 07:32:42 +00:00
|
|
|
.exportFunc();
|
|
|
|
builder.addFunction("store16", kSig_i_ii)
|
2019-10-08 12:38:48 +00:00
|
|
|
.addBody([kExprLocalGet, 0, kExprLocalGet, 1, kExprI32StoreMem16, 0, 0,
|
|
|
|
kExprLocalGet, 1])
|
2016-09-12 07:32:42 +00:00
|
|
|
.exportFunc();
|
|
|
|
builder.addFunction("load8", kSig_i_i)
|
2019-10-08 12:38:48 +00:00
|
|
|
.addBody([kExprLocalGet, 0, kExprI32LoadMem8U, 0, 0])
|
2016-09-12 07:32:42 +00:00
|
|
|
.exportFunc();
|
|
|
|
builder.addFunction("store8", kSig_i_ii)
|
2019-10-08 12:38:48 +00:00
|
|
|
.addBody([kExprLocalGet, 0, kExprLocalGet, 1, kExprI32StoreMem8, 0, 0,
|
|
|
|
kExprLocalGet, 1])
|
2016-09-12 07:32:42 +00:00
|
|
|
.exportFunc();
|
2016-06-28 16:46:34 +00:00
|
|
|
return builder;
|
|
|
|
}
|
|
|
|
|
2018-08-28 07:50:47 +00:00
|
|
|
// V8 internal memory size limit.
|
2020-02-28 10:57:06 +00:00
|
|
|
var kV8MaxPages = 65536;
|
2018-08-28 07:50:47 +00:00
|
|
|
|
|
|
|
|
2020-03-24 00:23:02 +00:00
|
|
|
function testMemoryGrowReadWriteBase(size, load_fn, store_fn) {
|
|
|
|
// size is the number of bytes for load and stores.
|
2018-10-26 17:28:37 +00:00
|
|
|
var builder = genMemoryGrowBuilder();
|
2018-08-28 07:50:47 +00:00
|
|
|
builder.addMemory(1, undefined, false);
|
2016-06-28 16:46:34 +00:00
|
|
|
var module = builder.instantiate();
|
|
|
|
var offset;
|
2020-03-24 00:23:02 +00:00
|
|
|
var load = module.exports[load_fn];
|
|
|
|
var store = module.exports[store_fn];
|
|
|
|
function peek() { return load(offset); }
|
|
|
|
function poke(value) { return store(offset, value); }
|
2016-06-28 16:46:34 +00:00
|
|
|
function growMem(pages) { return module.exports.grow_memory(pages); }
|
|
|
|
|
2020-03-24 00:23:02 +00:00
|
|
|
// Instead of checking every n-th offset, check the first 5.
|
|
|
|
for(offset = 0; offset <= (4*size); offset+=size) {
|
2016-06-28 16:46:34 +00:00
|
|
|
poke(20);
|
2016-08-04 20:37:39 +00:00
|
|
|
assertEquals(20, peek());
|
2016-06-28 16:46:34 +00:00
|
|
|
}
|
2020-03-24 00:23:02 +00:00
|
|
|
for (offset = kPageSize - (size - 1); offset < kPageSize + size; offset++) {
|
2016-06-28 16:46:34 +00:00
|
|
|
assertTraps(kTrapMemOutOfBounds, poke);
|
|
|
|
assertTraps(kTrapMemOutOfBounds, peek);
|
|
|
|
}
|
|
|
|
|
2016-08-04 20:37:39 +00:00
|
|
|
assertEquals(1, growMem(3));
|
2016-06-28 16:46:34 +00:00
|
|
|
|
2020-03-24 00:23:02 +00:00
|
|
|
for (let n = 1; n <= 3; n++) {
|
|
|
|
for (offset = n * kPageSize - 5 * size; offset <= n * kPageSize + 4 * size;
|
|
|
|
offset += size) {
|
|
|
|
// Check the 5 offsets to the before and after the n-th page.
|
|
|
|
// page n-1 page n page n+1
|
|
|
|
// +---- ... ------------+---------- ... +------ ...
|
|
|
|
// | | | ... | | | | | | | | | | | | ... | | | | ...
|
|
|
|
// <+> ^ ^
|
|
|
|
// | first offset last offset
|
|
|
|
// +-> size bytes
|
|
|
|
poke(20);
|
|
|
|
assertEquals(20, peek());
|
|
|
|
}
|
2016-06-28 16:46:34 +00:00
|
|
|
}
|
2016-09-12 07:32:42 +00:00
|
|
|
|
2020-03-24 00:23:02 +00:00
|
|
|
// Check the last 5 valid offsets of the last page.
|
|
|
|
for (offset = 4*kPageSize-size-(4*size); offset <= 4*kPageSize -size; offset+=size) {
|
2016-09-12 07:32:42 +00:00
|
|
|
poke(20);
|
|
|
|
assertEquals(20, peek());
|
|
|
|
}
|
|
|
|
|
2020-03-24 00:23:02 +00:00
|
|
|
for (offset = 4*kPageSize - (size-1); offset < 4*kPageSize + size; offset++) {
|
2016-09-12 07:32:42 +00:00
|
|
|
assertTraps(kTrapMemOutOfBounds, poke);
|
|
|
|
assertTraps(kTrapMemOutOfBounds, peek);
|
|
|
|
}
|
|
|
|
|
|
|
|
assertEquals(4, growMem(15));
|
|
|
|
|
2020-03-24 00:23:02 +00:00
|
|
|
for (offset = 4*kPageSize - (size-1); offset <= 4*kPageSize + size; offset+=size) {
|
2016-09-12 07:32:42 +00:00
|
|
|
poke(20);
|
|
|
|
assertEquals(20, peek());
|
|
|
|
}
|
2020-03-24 00:23:02 +00:00
|
|
|
for (offset = 19*kPageSize - 10; offset <= 19*kPageSize - size; offset+=size) {
|
2016-09-12 07:32:42 +00:00
|
|
|
poke(20);
|
|
|
|
assertEquals(20, peek());
|
|
|
|
}
|
2020-03-24 00:23:02 +00:00
|
|
|
for (offset = 19*kPageSize - (size-1); offset < 19*kPageSize + 5; offset++) {
|
2016-09-12 07:32:42 +00:00
|
|
|
assertTraps(kTrapMemOutOfBounds, poke);
|
|
|
|
assertTraps(kTrapMemOutOfBounds, peek);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-24 00:23:02 +00:00
|
|
|
(function testMemoryGrowReadWrite32() {
|
|
|
|
testMemoryGrowReadWriteBase(4, "load", "store");
|
|
|
|
})();
|
2016-09-12 07:32:42 +00:00
|
|
|
|
2020-03-24 00:23:02 +00:00
|
|
|
(function testMemoryGrowReadWrite16() {
|
|
|
|
testMemoryGrowReadWriteBase(2, "load16", "store16");
|
|
|
|
})();
|
2016-09-12 07:32:42 +00:00
|
|
|
|
2020-03-24 00:23:02 +00:00
|
|
|
(function testMemoryGrowReadWrite8() {
|
|
|
|
testMemoryGrowReadWriteBase(1, "load8", "store8");
|
|
|
|
})();
|
2016-06-28 16:46:34 +00:00
|
|
|
|
2018-10-26 17:28:37 +00:00
|
|
|
function testMemoryGrowZeroInitialSize() {
|
|
|
|
var builder = genMemoryGrowBuilder();
|
2018-08-28 07:50:47 +00:00
|
|
|
builder.addMemory(0, undefined, false);
|
2016-06-28 16:46:34 +00:00
|
|
|
var module = builder.instantiate();
|
|
|
|
var offset;
|
|
|
|
function peek() { return module.exports.load(offset); }
|
|
|
|
function poke(value) { return module.exports.store(offset, value); }
|
|
|
|
function growMem(pages) { return module.exports.grow_memory(pages); }
|
|
|
|
|
|
|
|
assertTraps(kTrapMemOutOfBounds, peek);
|
|
|
|
assertTraps(kTrapMemOutOfBounds, poke);
|
|
|
|
|
2016-08-04 20:37:39 +00:00
|
|
|
assertEquals(0, growMem(1));
|
2016-06-28 16:46:34 +00:00
|
|
|
|
2020-03-26 18:13:23 +00:00
|
|
|
// Check first 5 offsets.
|
|
|
|
for(offset = 0; offset <= 5; offset++) {
|
|
|
|
poke(20);
|
|
|
|
assertEquals(20, peek());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check last 5 offsets.
|
|
|
|
for(offset = kPageSize - 5*4; offset <= kPageSize - 4; offset++) {
|
2016-06-28 16:46:34 +00:00
|
|
|
poke(20);
|
2016-08-04 20:37:39 +00:00
|
|
|
assertEquals(20, peek());
|
2016-06-28 16:46:34 +00:00
|
|
|
}
|
|
|
|
|
2016-10-14 23:54:38 +00:00
|
|
|
for(offset = kPageSize - 3; offset <= kPageSize + 5; offset++) {
|
2016-10-14 21:01:23 +00:00
|
|
|
assertTraps(kTrapMemOutOfBounds, peek);
|
|
|
|
}
|
2016-10-14 23:54:38 +00:00
|
|
|
|
|
|
|
offset = 3*kPageSize;
|
|
|
|
for (var i = 1; i < 4; i++) {
|
|
|
|
assertTraps(kTrapMemOutOfBounds, poke);
|
|
|
|
assertEquals(i, growMem(1));
|
|
|
|
}
|
|
|
|
poke(20);
|
|
|
|
assertEquals(20, peek());
|
2016-10-14 21:01:23 +00:00
|
|
|
}
|
|
|
|
|
2018-10-26 17:28:37 +00:00
|
|
|
testMemoryGrowZeroInitialSize();
|
2016-10-14 21:01:23 +00:00
|
|
|
|
2020-03-24 00:23:02 +00:00
|
|
|
function testMemoryGrowZeroInitialSizeBase(size, load_fn, store_fn) {
|
2018-10-26 17:28:37 +00:00
|
|
|
var builder = genMemoryGrowBuilder();
|
2018-08-28 07:50:47 +00:00
|
|
|
builder.addMemory(0, undefined, false);
|
2016-10-14 23:54:38 +00:00
|
|
|
var module = builder.instantiate();
|
|
|
|
var offset;
|
2020-03-24 00:23:02 +00:00
|
|
|
var load = module.exports[load_fn];
|
|
|
|
var store = module.exports[store_fn];
|
|
|
|
function peek() { return load(offset); }
|
|
|
|
function poke(value) { return store(offset, value); }
|
2016-10-14 23:54:38 +00:00
|
|
|
function growMem(pages) { return module.exports.grow_memory(pages); }
|
|
|
|
|
|
|
|
assertTraps(kTrapMemOutOfBounds, peek);
|
|
|
|
assertTraps(kTrapMemOutOfBounds, poke);
|
|
|
|
|
|
|
|
assertEquals(0, growMem(1));
|
|
|
|
|
2020-03-24 00:23:02 +00:00
|
|
|
// Instead of checking every offset, check the first 5.
|
|
|
|
for(offset = 0; offset <= 4; offset++) {
|
2016-10-14 23:54:38 +00:00
|
|
|
poke(20);
|
|
|
|
assertEquals(20, peek());
|
|
|
|
}
|
|
|
|
|
2020-03-24 00:23:02 +00:00
|
|
|
// Check the last 5 valid ones.
|
|
|
|
for(offset = kPageSize - (size * 4); offset <= kPageSize - size; offset++) {
|
2016-10-14 23:54:38 +00:00
|
|
|
poke(20);
|
|
|
|
assertEquals(20, peek());
|
|
|
|
}
|
|
|
|
|
2020-03-24 00:23:02 +00:00
|
|
|
for(offset = kPageSize - (size - 1); offset <= kPageSize + 5; offset++) {
|
2016-10-14 23:54:38 +00:00
|
|
|
assertTraps(kTrapMemOutOfBounds, peek);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-24 00:23:02 +00:00
|
|
|
(function testMemoryGrowZeroInitialSize32() {
|
|
|
|
testMemoryGrowZeroInitialSizeBase(4, "load", "store");
|
|
|
|
})();
|
2016-10-14 23:54:38 +00:00
|
|
|
|
2020-03-24 00:23:02 +00:00
|
|
|
(function testMemoryGrowZeroInitialSize16() {
|
|
|
|
testMemoryGrowZeroInitialSizeBase(2, "load16", "store16");
|
|
|
|
})();
|
2016-10-14 23:54:38 +00:00
|
|
|
|
2020-03-24 00:23:02 +00:00
|
|
|
(function testMemoryGrowZeroInitialSize8() {
|
|
|
|
testMemoryGrowZeroInitialSizeBase(1, "load8", "store8");
|
|
|
|
})();
|
2016-10-14 23:54:38 +00:00
|
|
|
|
2018-10-26 17:28:37 +00:00
|
|
|
function testMemoryGrowTrapMaxPagesZeroInitialMemory() {
|
|
|
|
var builder = genMemoryGrowBuilder();
|
2018-08-28 07:50:47 +00:00
|
|
|
builder.addMemory(0, undefined, false);
|
2016-06-28 16:46:34 +00:00
|
|
|
var module = builder.instantiate();
|
2016-08-04 20:37:39 +00:00
|
|
|
function growMem(pages) { return module.exports.grow_memory(pages); }
|
2017-06-16 03:35:09 +00:00
|
|
|
assertEquals(-1, growMem(kV8MaxPages + 1));
|
2016-06-28 16:46:34 +00:00
|
|
|
}
|
|
|
|
|
2018-10-26 17:28:37 +00:00
|
|
|
testMemoryGrowTrapMaxPagesZeroInitialMemory();
|
2016-06-28 16:46:34 +00:00
|
|
|
|
2018-10-26 17:28:37 +00:00
|
|
|
function testMemoryGrowTrapMaxPages() {
|
|
|
|
var builder = genMemoryGrowBuilder();
|
2016-06-28 16:46:34 +00:00
|
|
|
builder.addMemory(1, 1, false);
|
|
|
|
var module = builder.instantiate();
|
2016-08-04 20:37:39 +00:00
|
|
|
function growMem(pages) { return module.exports.grow_memory(pages); }
|
2017-06-16 03:35:09 +00:00
|
|
|
assertEquals(-1, growMem(kV8MaxPages));
|
2016-06-28 16:46:34 +00:00
|
|
|
}
|
|
|
|
|
2018-10-26 17:28:37 +00:00
|
|
|
testMemoryGrowTrapMaxPages();
|
2016-08-30 16:59:57 +00:00
|
|
|
|
2018-10-26 17:28:37 +00:00
|
|
|
function testMemoryGrowTrapsWithNonSmiInput() {
|
|
|
|
var builder = genMemoryGrowBuilder();
|
2018-08-28 07:50:47 +00:00
|
|
|
builder.addMemory(0, undefined, false);
|
2016-08-30 16:59:57 +00:00
|
|
|
var module = builder.instantiate();
|
|
|
|
function growMem(pages) { return module.exports.grow_memory(pages); }
|
|
|
|
// The parameter of grow_memory is unsigned. Therefore -1 stands for
|
|
|
|
// UINT32_MIN, which cannot be represented as SMI.
|
|
|
|
assertEquals(-1, growMem(-1));
|
|
|
|
};
|
|
|
|
|
2018-10-26 17:28:37 +00:00
|
|
|
testMemoryGrowTrapsWithNonSmiInput();
|
2016-09-02 01:38:10 +00:00
|
|
|
|
2018-10-26 17:28:37 +00:00
|
|
|
function testMemoryGrowCurrentMemory() {
|
|
|
|
var builder = genMemoryGrowBuilder();
|
2018-08-28 07:50:47 +00:00
|
|
|
builder.addMemory(1, undefined, false);
|
2016-09-02 01:38:10 +00:00
|
|
|
builder.addFunction("memory_size", kSig_i_v)
|
2016-10-26 16:56:05 +00:00
|
|
|
.addBody([kExprMemorySize, kMemoryZero])
|
2016-09-02 01:38:10 +00:00
|
|
|
.exportFunc();
|
|
|
|
var module = builder.instantiate();
|
|
|
|
function growMem(pages) { return module.exports.grow_memory(pages); }
|
|
|
|
function MemSize() { return module.exports.memory_size(); }
|
2016-09-27 20:46:10 +00:00
|
|
|
assertEquals(1, MemSize());
|
2016-09-02 01:38:10 +00:00
|
|
|
assertEquals(1, growMem(1));
|
2016-09-27 20:46:10 +00:00
|
|
|
assertEquals(2, MemSize());
|
2016-09-02 01:38:10 +00:00
|
|
|
}
|
|
|
|
|
2018-10-26 17:28:37 +00:00
|
|
|
testMemoryGrowCurrentMemory();
|
2016-09-12 07:32:42 +00:00
|
|
|
|
2020-03-24 17:56:20 +00:00
|
|
|
function testMemoryGrowPreservesDataMemOpBase(size, load_fn, store_fn) {
|
2018-10-26 17:28:37 +00:00
|
|
|
var builder = genMemoryGrowBuilder();
|
2018-08-28 07:50:47 +00:00
|
|
|
builder.addMemory(1, undefined, false);
|
2016-09-12 07:32:42 +00:00
|
|
|
var module = builder.instantiate();
|
2020-03-24 17:56:20 +00:00
|
|
|
var offset;
|
|
|
|
var load = module.exports[load_fn];
|
|
|
|
var store = module.exports[store_fn];
|
|
|
|
function peek() { return load(offset); }
|
|
|
|
function poke(value) { return store(offset, value); }
|
2016-09-12 07:32:42 +00:00
|
|
|
function growMem(pages) { return module.exports.grow_memory(pages); }
|
2020-03-24 17:56:20 +00:00
|
|
|
// Maximum unsigned integer of size bits.
|
|
|
|
const max = Math.pow(2, (size * 8)) - 1;
|
2016-09-12 07:32:42 +00:00
|
|
|
|
2020-03-24 17:56:20 +00:00
|
|
|
// Check the first 5 offsets.
|
|
|
|
for(offset = 0; offset <= (4*size); offset+=size) {
|
|
|
|
poke(offset % max);
|
|
|
|
assertEquals(offset % max, peek());
|
2016-09-12 07:32:42 +00:00
|
|
|
}
|
|
|
|
|
2020-03-24 17:56:20 +00:00
|
|
|
// Check the last 5 valid offsets.
|
|
|
|
for(offset = kPageSize - 5*size; offset <= (kPageSize - size); offset+=size) {
|
|
|
|
poke(offset % max);
|
|
|
|
assertEquals(offset % max, peek());
|
2016-09-12 07:32:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
assertEquals(1, growMem(3));
|
|
|
|
|
2020-03-24 17:56:20 +00:00
|
|
|
// Check the first 5 offsets are preserved by growMem.
|
|
|
|
for(offset = 0; offset <= (4*size); offset+=size) {
|
|
|
|
assertEquals(offset % max, peek());
|
2016-09-12 07:32:42 +00:00
|
|
|
}
|
|
|
|
|
2020-03-24 17:56:20 +00:00
|
|
|
// Check the last 5 valid offsets are preserved by growMem.
|
|
|
|
for(offset = kPageSize - 5*size; offset <= (kPageSize - size); offset+=size) {
|
|
|
|
assertEquals(offset % max, peek());
|
2016-09-12 07:32:42 +00:00
|
|
|
}
|
2020-03-24 17:56:20 +00:00
|
|
|
}
|
2016-09-12 07:32:42 +00:00
|
|
|
|
2020-03-24 17:56:20 +00:00
|
|
|
(function testMemoryGrowPreservesDataMemOp32() {
|
|
|
|
testMemoryGrowPreservesDataMemOpBase(4, "load", "store");
|
|
|
|
})();
|
2016-09-12 07:32:42 +00:00
|
|
|
|
2020-03-24 17:56:20 +00:00
|
|
|
(function testMemoryGrowPreservesDataMemOp16() {
|
|
|
|
testMemoryGrowPreservesDataMemOpBase(2, "load16", "store16");
|
|
|
|
})();
|
2016-09-12 07:32:42 +00:00
|
|
|
|
2020-03-24 17:56:20 +00:00
|
|
|
(function testMemoryGrowPreservesDataMemOp8() {
|
|
|
|
testMemoryGrowPreservesDataMemOpBase(1, "load8", "store8");
|
|
|
|
})();
|
2016-09-28 20:55:42 +00:00
|
|
|
|
2018-10-26 17:28:37 +00:00
|
|
|
function testMemoryGrowOutOfBoundsOffset() {
|
|
|
|
var builder = genMemoryGrowBuilder();
|
2018-08-28 07:50:47 +00:00
|
|
|
builder.addMemory(1, undefined, false);
|
2016-09-28 20:55:42 +00:00
|
|
|
var module = builder.instantiate();
|
|
|
|
var offset, val;
|
|
|
|
function peek() { return module.exports.load(offset); }
|
|
|
|
function poke(value) { return module.exports.store(offset, value); }
|
|
|
|
function growMem(pages) { return module.exports.grow_memory(pages); }
|
|
|
|
|
|
|
|
offset = 3*kPageSize + 4;
|
|
|
|
assertTraps(kTrapMemOutOfBounds, poke);
|
|
|
|
|
|
|
|
assertEquals(1, growMem(1));
|
|
|
|
assertTraps(kTrapMemOutOfBounds, poke);
|
|
|
|
|
|
|
|
assertEquals(2, growMem(1));
|
|
|
|
assertTraps(kTrapMemOutOfBounds, poke);
|
|
|
|
|
|
|
|
assertEquals(3, growMem(1));
|
|
|
|
|
2020-03-26 18:13:23 +00:00
|
|
|
for (offset = 3*kPageSize; offset <= 3*kPageSize + 4; offset++) {
|
|
|
|
poke(0xaced);
|
|
|
|
assertEquals(0xaced, peek());
|
|
|
|
}
|
|
|
|
|
|
|
|
for (offset = 4*kPageSize-8; offset <= 4*kPageSize - 4; offset++) {
|
2016-09-28 20:55:42 +00:00
|
|
|
poke(0xaced);
|
|
|
|
assertEquals(0xaced, peek());
|
|
|
|
}
|
|
|
|
|
|
|
|
for (offset = 4*kPageSize - 3; offset <= 4*kPageSize + 4; offset++) {
|
|
|
|
assertTraps(kTrapMemOutOfBounds, poke);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-26 17:28:37 +00:00
|
|
|
testMemoryGrowOutOfBoundsOffset();
|
2016-10-20 07:38:55 +00:00
|
|
|
|
2018-10-26 17:28:37 +00:00
|
|
|
function testMemoryGrowOutOfBoundsOffset2() {
|
2016-10-20 07:38:55 +00:00
|
|
|
var builder = new WasmModuleBuilder();
|
|
|
|
builder.addMemory(16, 128, false);
|
|
|
|
builder.addFunction("main", kSig_v_v)
|
|
|
|
.addBody([
|
|
|
|
kExprI32Const, 20,
|
|
|
|
kExprI32Const, 29,
|
2018-10-26 17:28:37 +00:00
|
|
|
kExprMemoryGrow, kMemoryZero,
|
2016-10-20 07:38:55 +00:00
|
|
|
kExprI32StoreMem, 0, 0xFF, 0xFF, 0xFF, 0x3a
|
|
|
|
])
|
|
|
|
.exportAs("main");
|
|
|
|
var module = builder.instantiate();
|
|
|
|
assertTraps(kTrapMemOutOfBounds, module.exports.main);
|
|
|
|
}
|
|
|
|
|
2018-10-26 17:28:37 +00:00
|
|
|
testMemoryGrowOutOfBoundsOffset2();
|
2016-11-08 09:55:05 +00:00
|
|
|
|
2018-10-26 17:28:37 +00:00
|
|
|
function testMemoryGrowDeclaredMaxTraps() {
|
|
|
|
var builder = genMemoryGrowBuilder();
|
2016-11-08 09:55:05 +00:00
|
|
|
builder.addMemory(1, 16, false);
|
|
|
|
var module = builder.instantiate();
|
|
|
|
function growMem(pages) { return module.exports.grow_memory(pages); }
|
|
|
|
assertEquals(1, growMem(5));
|
|
|
|
assertEquals(6, growMem(5));
|
|
|
|
assertEquals(-1, growMem(6));
|
|
|
|
}
|
|
|
|
|
2018-10-26 17:28:37 +00:00
|
|
|
testMemoryGrowDeclaredMaxTraps();
|
2016-11-08 09:55:05 +00:00
|
|
|
|
2020-02-28 10:57:06 +00:00
|
|
|
(function testMemoryGrowInternalMaxTraps() {
|
|
|
|
// This test checks that grow_memory does not grow past the internally
|
|
|
|
// defined maximum memory size.
|
2018-10-26 17:28:37 +00:00
|
|
|
var builder = genMemoryGrowBuilder();
|
2016-11-08 09:55:05 +00:00
|
|
|
builder.addMemory(1, kSpecMaxPages, false);
|
|
|
|
var module = builder.instantiate();
|
|
|
|
function growMem(pages) { return module.exports.grow_memory(pages); }
|
|
|
|
assertEquals(1, growMem(20));
|
|
|
|
assertEquals(-1, growMem(kV8MaxPages - 20));
|
2020-02-28 10:57:06 +00:00
|
|
|
})();
|
2017-06-16 03:35:09 +00:00
|
|
|
|
2020-02-28 10:57:06 +00:00
|
|
|
(function testMemoryGrow4Gb() {
|
2018-10-26 17:28:37 +00:00
|
|
|
var builder = genMemoryGrowBuilder();
|
2018-08-28 07:50:47 +00:00
|
|
|
builder.addMemory(1, undefined, false);
|
2017-06-16 03:35:09 +00:00
|
|
|
var module = builder.instantiate();
|
|
|
|
var offset, val;
|
|
|
|
function peek() { return module.exports.load(offset); }
|
|
|
|
function poke(value) { return module.exports.store(offset, value); }
|
|
|
|
function growMem(pages) { return module.exports.grow_memory(pages); }
|
|
|
|
|
2020-03-26 18:13:23 +00:00
|
|
|
// Check first 5 offsets.
|
|
|
|
for (offset = 0; offset <= 4 * 4; offset += 4) {
|
|
|
|
poke(100000 - offset);
|
|
|
|
assertEquals(100000 - offset, peek());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check last 5 offsets.
|
|
|
|
for (offset = (kPageSize - 5 * 4); offset <= (kPageSize - 4); offset += 4) {
|
2017-06-16 03:35:09 +00:00
|
|
|
poke(100000 - offset);
|
|
|
|
assertEquals(100000 - offset, peek());
|
|
|
|
}
|
|
|
|
|
|
|
|
let result = growMem(kV8MaxPages - 1);
|
2020-02-28 10:57:06 +00:00
|
|
|
if (result == 1) {
|
2020-03-26 18:13:23 +00:00
|
|
|
// Check first 5 offsets.
|
|
|
|
for (offset = 0; offset <= 4 * 4; offset += 4) {
|
|
|
|
assertEquals(100000 - offset, peek());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check last 5 offsets.
|
|
|
|
for (offset = (kPageSize - 5 * 4); offset <= (kPageSize - 4); offset += 4) {
|
2017-06-16 03:35:09 +00:00
|
|
|
assertEquals(100000 - offset, peek());
|
|
|
|
}
|
|
|
|
|
2020-02-28 10:57:06 +00:00
|
|
|
// Bounds check for large mem size.
|
|
|
|
let kMemSize = (kV8MaxPages * kPageSize);
|
|
|
|
let kLastValidOffset = kMemSize - 4; // Accommodate a 4-byte read/write.
|
2020-03-26 18:13:23 +00:00
|
|
|
// Check first 5 offsets of last page.
|
|
|
|
for (offset = kMemSize - kPageSize; offset <= kMemSize - kPageSize + 4 * 4;
|
|
|
|
offset += 4) {
|
|
|
|
poke(0xaced);
|
|
|
|
assertEquals(0xaced, peek());
|
|
|
|
}
|
|
|
|
for (offset = kLastValidOffset - 5 * 4; offset <= kLastValidOffset;
|
2020-02-28 10:57:06 +00:00
|
|
|
offset += 4) {
|
2017-06-16 03:35:09 +00:00
|
|
|
poke(0xaced);
|
|
|
|
assertEquals(0xaced, peek());
|
|
|
|
}
|
|
|
|
|
2020-02-28 10:57:06 +00:00
|
|
|
for (offset = kLastValidOffset + 1; offset < kMemSize; offset++) {
|
2017-06-16 03:35:09 +00:00
|
|
|
assertTraps(kTrapMemOutOfBounds, poke);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Allocating big chunks of memory can fail on gc_stress, especially on 32
|
|
|
|
// bit platforms. When grow_memory fails, expected result is -1.
|
|
|
|
assertEquals(-1, result);
|
|
|
|
}
|
2020-02-28 10:57:06 +00:00
|
|
|
})();
|