2019-01-23 14:27:58 +00:00
|
|
|
// Copyright 2018 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: --experimental-wasm-bulk-memory
|
|
|
|
|
|
|
|
load("test/mjsunit/wasm/wasm-module-builder.js");
|
|
|
|
|
|
|
|
function addFunction(builder, k) {
|
|
|
|
let m = builder.addFunction("", kSig_i_v)
|
|
|
|
.addBody([...wasmI32Const(k)]);
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
2019-03-05 19:35:07 +00:00
|
|
|
function addFunctions(builder, count) {
|
2019-01-23 14:27:58 +00:00
|
|
|
let o = {};
|
|
|
|
for (var i = 0; i < count; i++) {
|
|
|
|
let name = `f${i}`;
|
|
|
|
o[name] = addFunction(builder, i);
|
2019-03-05 19:35:07 +00:00
|
|
|
o[name].exportAs(name);
|
2019-01-23 14:27:58 +00:00
|
|
|
}
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
function assertTable(obj, ...elems) {
|
|
|
|
for (var i = 0; i < elems.length; i++) {
|
|
|
|
assertEquals(elems[i], obj.get(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
(function TestTableInitInBounds() {
|
|
|
|
print(arguments.callee.name);
|
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
let sig_v_iii = builder.addType(kSig_v_iii);
|
|
|
|
let kTableSize = 5;
|
|
|
|
|
|
|
|
builder.setTableBounds(kTableSize, kTableSize);
|
|
|
|
{
|
2019-03-05 19:35:07 +00:00
|
|
|
let o = addFunctions(builder, kTableSize);
|
2019-01-23 14:27:58 +00:00
|
|
|
builder.addPassiveElementSegment(
|
2019-03-04 18:52:03 +00:00
|
|
|
[o.f0.index, o.f1.index, o.f2.index, o.f3.index, o.f4.index, null]);
|
2019-01-23 14:27:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
builder.addFunction("init0", sig_v_iii)
|
|
|
|
.addBody([
|
|
|
|
kExprGetLocal, 0,
|
|
|
|
kExprGetLocal, 1,
|
|
|
|
kExprGetLocal, 2,
|
2019-01-31 01:50:44 +00:00
|
|
|
kNumericPrefix, kExprTableInit, kSegmentZero, kTableZero])
|
2019-01-23 14:27:58 +00:00
|
|
|
.exportAs("init0");
|
|
|
|
|
|
|
|
builder.addExportOfKind("table", kExternalTable, 0);
|
|
|
|
|
|
|
|
let instance = builder.instantiate();
|
|
|
|
let x = instance.exports;
|
|
|
|
|
|
|
|
assertTable(x.table, null, null, null, null, null);
|
|
|
|
|
2019-03-05 19:35:07 +00:00
|
|
|
// 0 count is ok in bounds, and at end of regions.
|
|
|
|
x.init0(0, 0, 0);
|
|
|
|
x.init0(kTableSize, 0, 0);
|
|
|
|
x.init0(0, kTableSize, 0);
|
|
|
|
|
2019-01-23 14:27:58 +00:00
|
|
|
// test actual writes.
|
|
|
|
x.init0(0, 0, 1);
|
|
|
|
assertTable(x.table, x.f0, null, null, null, null);
|
|
|
|
x.init0(0, 0, 2);
|
|
|
|
assertTable(x.table, x.f0, x.f1, null, null, null);
|
|
|
|
x.init0(0, 0, 3);
|
|
|
|
assertTable(x.table, x.f0, x.f1, x.f2, null, null);
|
|
|
|
x.init0(3, 0, 2);
|
|
|
|
assertTable(x.table, x.f0, x.f1, x.f2, x.f0, x.f1);
|
|
|
|
x.init0(3, 1, 2);
|
|
|
|
assertTable(x.table, x.f0, x.f1, x.f2, x.f1, x.f2);
|
|
|
|
x.init0(3, 2, 2);
|
|
|
|
assertTable(x.table, x.f0, x.f1, x.f2, x.f2, x.f3);
|
|
|
|
x.init0(3, 3, 2);
|
|
|
|
assertTable(x.table, x.f0, x.f1, x.f2, x.f3, x.f4);
|
2019-03-04 18:52:03 +00:00
|
|
|
|
|
|
|
// test writing null
|
|
|
|
x.init0(0, 5, 1);
|
|
|
|
assertTable(x.table, null, x.f1, x.f2, x.f3, x.f4);
|
2019-01-23 14:27:58 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
(function TestTableInitOob() {
|
|
|
|
print(arguments.callee.name);
|
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
let sig_v_iii = builder.addType(kSig_v_iii);
|
|
|
|
let kTableSize = 5;
|
|
|
|
|
|
|
|
builder.setTableBounds(kTableSize, kTableSize);
|
|
|
|
{
|
|
|
|
let o = addFunctions(builder, kTableSize);
|
|
|
|
builder.addPassiveElementSegment(
|
|
|
|
[o.f0.index, o.f1.index, o.f2.index, o.f3.index, o.f4.index]);
|
|
|
|
}
|
|
|
|
|
|
|
|
builder.addFunction("init0", sig_v_iii)
|
|
|
|
.addBody([
|
|
|
|
kExprGetLocal, 0,
|
|
|
|
kExprGetLocal, 1,
|
|
|
|
kExprGetLocal, 2,
|
2019-01-31 01:50:44 +00:00
|
|
|
kNumericPrefix, kExprTableInit, kSegmentZero, kTableZero])
|
2019-01-23 14:27:58 +00:00
|
|
|
.exportAs("init0");
|
|
|
|
|
|
|
|
builder.addExportOfKind("table", kExternalTable, 0);
|
|
|
|
|
|
|
|
let instance = builder.instantiate();
|
|
|
|
let x = instance.exports;
|
|
|
|
|
|
|
|
assertTable(x.table, null, null, null, null, null);
|
2019-01-28 10:46:03 +00:00
|
|
|
|
2019-03-05 19:35:07 +00:00
|
|
|
// Write all values up to the out-of-bounds write.
|
|
|
|
assertThrows(() => x.init0(3, 0, 3));
|
|
|
|
assertTable(x.table, null, null, null, x.f0, x.f1);
|
|
|
|
|
|
|
|
// Write all values up to the out-of-bounds read.
|
|
|
|
assertThrows(() => x.init0(0, 3, 3));
|
|
|
|
assertTable(x.table, x.f3, x.f4, null, x.f0, x.f1);
|
|
|
|
|
2019-01-28 10:46:03 +00:00
|
|
|
// 0-count is oob.
|
|
|
|
assertThrows(() => x.init0(kTableSize+1, 0, 0));
|
|
|
|
assertThrows(() => x.init0(0, kTableSize+1, 0));
|
|
|
|
|
2019-01-23 14:27:58 +00:00
|
|
|
assertThrows(() => x.init0(0, 0, 6));
|
|
|
|
assertThrows(() => x.init0(0, 1, 5));
|
|
|
|
assertThrows(() => x.init0(0, 2, 4));
|
|
|
|
assertThrows(() => x.init0(0, 3, 3));
|
|
|
|
assertThrows(() => x.init0(0, 4, 2));
|
|
|
|
assertThrows(() => x.init0(0, 5, 1));
|
|
|
|
|
|
|
|
assertThrows(() => x.init0(0, 0, 6));
|
|
|
|
assertThrows(() => x.init0(1, 0, 5));
|
|
|
|
assertThrows(() => x.init0(2, 0, 4));
|
|
|
|
assertThrows(() => x.init0(3, 0, 3));
|
|
|
|
assertThrows(() => x.init0(4, 0, 2));
|
|
|
|
assertThrows(() => x.init0(5, 0, 1));
|
|
|
|
|
|
|
|
assertThrows(() => x.init0(10, 0, 1));
|
|
|
|
assertThrows(() => x.init0(0, 10, 1));
|
|
|
|
})();
|