empirical_max_memory.js 2.45 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
// 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: --expose-wasm

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

let k1GiB = 1 * 1024 * 1024 * 1024;
let k4GiB = 4 * k1GiB;
11 12
// TODO(4153): Raise this to 4GiB, but only on 64-bit platforms.
let kMaxMemory = 2 * k1GiB - kPageSize;
13 14 15 16 17 18 19 20 21

(function Test() {
  var memory;

  function BuildAccessors(type, load_opcode, store_opcode) {
    builder = new WasmModuleBuilder();
    builder.addImportedMemory("i", "mem");
    builder.addFunction("load", makeSig([kWasmI32], [type]))
      .addBody([           // --
22
        kExprLocalGet, 0,  // --
23 24 25 26 27
        load_opcode, 0, 0, // --
      ])                   // --
      .exportFunc();
    builder.addFunction("store", makeSig([kWasmI32, type], []))
      .addBody([             // --
28 29
        kExprLocalGet, 0,    // --
        kExprLocalGet, 1,    // --
30 31 32 33 34 35 36 37 38
        store_opcode, 0, 0,  // --
      ])                     // --
      .exportFunc();
    let i = builder.instantiate({i: {mem: memory}});
    return {load: i.exports.load, store: i.exports.store};
  }

  function probe(a, f) {
    print("------------------------");
39
    let stride = kPageSize * 32;  // Don't check every page to save time.
40 41 42 43 44 45 46 47 48 49 50
    let max = kMaxMemory;
    for (let i = 0; i < max; i += stride) {
      a.store(i, f(i));
    }
    for (let i = 0; i < max; i += stride) {
      assertEquals(f(i), a.load(i));
    }
  }

  try {
    let kPages = kMaxMemory / kPageSize;
51
    memory = new WebAssembly.Memory({ initial: kPages, maximum: kPages });
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
  } catch (e) {
    print("OOM: sorry, best effort max memory size test.");
    return;
  }

  assertEquals(kMaxMemory, memory.buffer.byteLength);

  {
    let a = BuildAccessors(kWasmI32, kExprI32LoadMem, kExprI32StoreMem);
    probe(a, i => (0xaabbccee ^ ((i >> 11) * 0x110005)) | 0);
  }

  {
    let a = BuildAccessors(kWasmI32, kExprI32LoadMem16U, kExprI32StoreMem16);
    probe(a, i => (0xccee ^ ((i >> 11) * 0x110005)) & 0xFFFF);
  }

  {
    let a = BuildAccessors(kWasmI32, kExprI32LoadMem8U, kExprI32StoreMem8);
    probe(a, i => (0xee ^ ((i >> 11) * 0x05)) & 0xFF);
  }

  {
    let a = BuildAccessors(kWasmF64, kExprF64LoadMem, kExprF64StoreMem);
    probe(a, i => 0xaabbccee ^ ((i >> 11) * 0x110005));
  }

  {
    let a = BuildAccessors(kWasmF32, kExprF32LoadMem, kExprF32StoreMem);
    probe(a, i => Math.fround(0xaabbccee ^ ((i >> 11) * 0x110005)));
  }
})();