huge-memory.js 2.18 KB
Newer Older
1 2 3 4 5
// Copyright 2017 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: --wasm-max-mem-pages=49152
6 7
// Save some memory on Linux; other platforms ignore this flag.
// Flags: --multi-mapped-mock-allocator
8 9 10 11

// This test makes sure things don't break once we support >2GB wasm memories.
load("test/mjsunit/wasm/wasm-module-builder.js");

12 13
(function testHugeMemory() {
  print(arguments.callee.name);
14 15 16 17 18 19 20
  var builder = new WasmModuleBuilder();

  const num_pages = 49152;  // 3GB

  builder.addMemory(num_pages, num_pages, true);
  builder.addFunction("geti", kSig_i_ii)
    .addBody([
21 22
      kExprLocalGet, 0,
      kExprLocalGet, 1,
23 24 25 26 27 28 29 30 31 32 33 34
      kExprI32Mul,
      kExprI32LoadMem, 0, 0,
    ])
    .exportFunc();

  var module = builder.instantiate();
  const geti = module.exports.geti;

  print("In bounds");
  assertEquals(0, geti(2500, 1 << 20));
  print("Out of bounds");
  assertTraps(kTrapMemOutOfBounds, () => geti(3500, 1 << 20));
35
})();
36

37 38
(function testHugeMemoryConstInBounds() {
  print(arguments.callee.name);
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
  var builder = new WasmModuleBuilder();

  const num_pages = 49152;  // 3GB

  builder.addMemory(num_pages, num_pages, true);
  builder.addFunction("geti", kSig_i_v)
    .addBody([
      kExprI32Const, 0x80, 0x80, 0x80, 0x80, 0x7A, // 0xA0000000, 2.5GB
      kExprI32LoadMem, 0, 0,
    ])
    .exportFunc();

  var module = builder.instantiate();
  const geti = module.exports.geti;

  print("In bounds");
  assertEquals(0, geti());
56
})();
57

58 59
(function testHugeMemoryConstOutOfBounds() {
  print(arguments.callee.name);
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
  var builder = new WasmModuleBuilder();

  const num_pages = 49152;  // 3GB

  builder.addMemory(num_pages, num_pages, true);
  builder.addFunction("geti", kSig_i_v)
    .addBody([
      kExprI32Const, 0x80, 0x80, 0x80, 0x80, 0x7E, // 0xE0000000, 3.5GB
      kExprI32LoadMem, 0, 0,
    ])
    .exportFunc();

  var module = builder.instantiate();
  const geti = module.exports.geti;

  print("Out of bounds");
  assertTraps(kTrapMemOutOfBounds, geti);
77 78 79 80 81 82 83 84
})();

(function testGrowHugeMemory() {
  print(arguments.callee.name);

  let mem = new WebAssembly.Memory({initial: 1});
  mem.grow(49151);
})();