2016-11-23 20:44:14 +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.
|
|
|
|
|
|
|
|
// Flags: --expose-wasm --expose-gc
|
|
|
|
|
2021-06-01 12:46:36 +00:00
|
|
|
d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
|
2016-11-23 20:44:14 +00:00
|
|
|
|
|
|
|
// This test verifies that when instances are exported, Gc'ed, the other
|
|
|
|
// instances in the chain still maintain a consistent view of the memory.
|
|
|
|
(function ValidateSharedInstanceMemory() {
|
|
|
|
print("ValidateSharedInstanceMemory");
|
|
|
|
let memory = new WebAssembly.Memory({initial: 5, maximum: 100});
|
|
|
|
var builder = new WasmModuleBuilder();
|
2016-12-20 15:32:56 +00:00
|
|
|
builder.addImportedMemory("mod", "imported_mem");
|
2016-11-23 20:44:14 +00:00
|
|
|
builder.addFunction("mem_size", kSig_i_v)
|
|
|
|
.addBody([kExprMemorySize, kMemoryZero])
|
|
|
|
.exportFunc();
|
|
|
|
builder.addFunction("grow", kSig_i_i)
|
2019-10-08 12:38:48 +00:00
|
|
|
.addBody([kExprLocalGet, 0, kExprMemoryGrow, kMemoryZero])
|
2016-11-23 20:44:14 +00:00
|
|
|
.exportFunc();
|
|
|
|
var instances = [];
|
|
|
|
for (var i = 0; i < 5; i++) {
|
2016-12-20 15:32:56 +00:00
|
|
|
instances.push(builder.instantiate({mod: {imported_mem: memory}}));
|
2016-11-23 20:44:14 +00:00
|
|
|
}
|
2017-07-07 13:52:21 +00:00
|
|
|
function grow_instance(index, pages) {
|
|
|
|
return instances[index].exports.grow(pages);
|
|
|
|
}
|
2016-11-23 20:44:14 +00:00
|
|
|
|
|
|
|
function verify_mem_size(expected_pages) {
|
2017-07-07 13:52:21 +00:00
|
|
|
print(" checking size = " + expected_pages + " pages");
|
2016-11-23 20:44:14 +00:00
|
|
|
assertEquals(expected_pages*kPageSize, memory.buffer.byteLength);
|
2017-07-07 13:52:21 +00:00
|
|
|
for (let i = 0; i < instances.length; i++) {
|
|
|
|
if (instances[i] == null) continue;
|
2016-11-23 20:44:14 +00:00
|
|
|
assertEquals(expected_pages, instances[i].exports.mem_size());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify initial memory size of all instances, grow and verify that all
|
|
|
|
// instances are updated correctly.
|
|
|
|
verify_mem_size(5);
|
|
|
|
assertEquals(5, memory.grow(6));
|
|
|
|
verify_mem_size(11);
|
|
|
|
|
2017-07-07 13:52:21 +00:00
|
|
|
print(" instances[1] = null, GC");
|
2016-11-23 20:44:14 +00:00
|
|
|
instances[1] = null;
|
|
|
|
gc();
|
|
|
|
|
|
|
|
// i[0] - i[2] - i[3] - i[4]
|
|
|
|
verify_mem_size(11);
|
2017-07-07 13:52:21 +00:00
|
|
|
assertEquals(11, grow_instance(2, 10));
|
2016-11-23 20:44:14 +00:00
|
|
|
verify_mem_size(21);
|
|
|
|
|
2017-07-07 13:52:21 +00:00
|
|
|
print(" instances[4] = null, GC");
|
2016-11-23 20:44:14 +00:00
|
|
|
instances[4] = null;
|
|
|
|
gc();
|
2017-07-07 13:52:21 +00:00
|
|
|
verify_mem_size(21);
|
2016-11-23 20:44:14 +00:00
|
|
|
|
|
|
|
assertEquals(21, memory.grow(2));
|
2017-07-07 13:52:21 +00:00
|
|
|
verify_mem_size(23);
|
2016-11-23 20:44:14 +00:00
|
|
|
|
2017-07-07 13:52:21 +00:00
|
|
|
print(" instances[0] = null, GC");
|
2016-11-23 20:44:14 +00:00
|
|
|
instances[0] = null;
|
|
|
|
gc();
|
2017-07-07 13:52:21 +00:00
|
|
|
gc();
|
|
|
|
verify_mem_size(23);
|
2016-11-23 20:44:14 +00:00
|
|
|
|
2017-07-07 13:52:21 +00:00
|
|
|
assertEquals(23, grow_instance(3, 5));
|
|
|
|
verify_mem_size(28);
|
2016-11-23 20:44:14 +00:00
|
|
|
|
2017-07-07 13:52:21 +00:00
|
|
|
print(" new instance, GC");
|
2016-11-23 20:44:14 +00:00
|
|
|
// Instantiate a new instance and verify that it can be grown correctly.
|
2016-12-20 15:32:56 +00:00
|
|
|
instances.push(builder.instantiate({mod: {imported_mem: memory}}));
|
2017-07-07 13:52:21 +00:00
|
|
|
gc();
|
|
|
|
gc();
|
2016-11-23 20:44:14 +00:00
|
|
|
|
2017-07-07 13:52:21 +00:00
|
|
|
verify_mem_size(28);
|
|
|
|
assertEquals(28, grow_instance(5, 2));
|
|
|
|
verify_mem_size(30);
|
2016-11-23 20:44:14 +00:00
|
|
|
assertEquals(30, memory.grow(5));
|
2017-07-07 13:52:21 +00:00
|
|
|
verify_mem_size(35);
|
|
|
|
|
|
|
|
for (let i = 0; i < instances.length; i++) {
|
|
|
|
print(" instances[" + i + "] = null, GC");
|
|
|
|
instances[i] = null;
|
|
|
|
gc();
|
|
|
|
verify_mem_size(35);
|
|
|
|
}
|
2016-11-23 20:44:14 +00:00
|
|
|
|
|
|
|
})();
|