memory-instance-validation.js 2.64 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// 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

load("test/mjsunit/wasm/wasm-constants.js");
load("test/mjsunit/wasm/wasm-module-builder.js");

// 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();
16
  builder.addImportedMemory("mod", "imported_mem");
17 18 19 20 21 22 23 24
  builder.addFunction("mem_size", kSig_i_v)
    .addBody([kExprMemorySize, kMemoryZero])
    .exportFunc();
  builder.addFunction("grow", kSig_i_i)
    .addBody([kExprGetLocal, 0, kExprGrowMemory, kMemoryZero])
    .exportFunc();
  var instances = [];
  for (var i = 0; i < 5; i++) {
25
    instances.push(builder.instantiate({mod: {imported_mem: memory}}));
26
  }
27 28 29
  function grow_instance(index, pages) {
    return instances[index].exports.grow(pages);
  }
30 31

  function verify_mem_size(expected_pages) {
32
    print("  checking size = " + expected_pages + " pages");
33
    assertEquals(expected_pages*kPageSize, memory.buffer.byteLength);
34 35
    for (let i = 0; i < instances.length; i++) {
      if (instances[i] == null) continue;
36 37 38 39 40 41 42 43 44 45
      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);

46
  print("  instances[1] = null, GC");
47 48 49 50 51
  instances[1] = null;
  gc();

  // i[0] - i[2] - i[3] - i[4]
  verify_mem_size(11);
52
  assertEquals(11, grow_instance(2, 10));
53 54
  verify_mem_size(21);

55
  print("  instances[4] = null, GC");
56 57
  instances[4] = null;
  gc();
58
  verify_mem_size(21);
59 60

  assertEquals(21, memory.grow(2));
61
  verify_mem_size(23);
62

63
  print("  instances[0] = null, GC");
64 65
  instances[0] = null;
  gc();
66 67
  gc();
  verify_mem_size(23);
68

69 70
  assertEquals(23, grow_instance(3, 5));
  verify_mem_size(28);
71

72
  print("  new instance, GC");
73
  // Instantiate a new instance and verify that it can be grown correctly.
74
  instances.push(builder.instantiate({mod: {imported_mem: memory}}));
75 76
  gc();
  gc();
77

78 79 80
  verify_mem_size(28);
  assertEquals(28, grow_instance(5, 2));
  verify_mem_size(30);
81
  assertEquals(30, memory.grow(5));
82 83 84 85 86 87 88 89
  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);
  }
90 91

})();