module-memory.js 5.69 KB
Newer Older
1 2 3 4
// Copyright 2015 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.

5
// Flags: --expose-wasm --expose-gc --stress-compaction --allow-natives-syntax
6

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

9
var kMemSize = 65536;
10 11

function genModule(memory) {
12 13
  var builder = new WasmModuleBuilder();

14
  builder.addImportedMemory("", "memory", 1);
mtrofin's avatar
mtrofin committed
15
  builder.exportMemoryAs("memory");
16
  builder.addFunction("main", kSig_i_i)
17
    .addBody([
18 19
      // main body: while(i) { if(mem[i]) return -1; i -= 4; } return 0;
      // TODO(titzer): this manual bytecode has a copy of test-run-wasm.cc
20
      /**/ kExprLoop, kWasmStmt,           // --
21
      /*  */ kExprLocalGet, 0,             // --
22
      /*  */ kExprIf, kWasmStmt,           // --
23
      /*    */ kExprLocalGet, 0,           // --
24
      /*    */ kExprI32LoadMem, 0, 0,      // --
25 26
      /*    */ kExprIf, kWasmStmt,         // --
      /*      */ kExprI32Const, 127,       // --
27 28
      /*      */ kExprReturn,              // --
      /*      */ kExprEnd,                 // --
29
      /*    */ kExprLocalGet, 0,           // --
30
      /*    */ kExprI32Const, 4,           // --
31
      /*    */ kExprI32Sub,                // --
32
      /*    */ kExprLocalSet, 0,           // --
33 34 35
      /*    */ kExprBr, 1,                 // --
      /*    */ kExprEnd,                   // --
      /*  */ kExprEnd,                     // --
36
      /**/ kExprI32Const, 0                // --
37
    ])
38
    .exportFunc();
mtrofin's avatar
mtrofin committed
39
  var module = builder.instantiate({"": {memory:memory}});
40
  assertTrue(module.exports.memory instanceof WebAssembly.Memory);
41
  if (memory != null) assertEquals(memory.buffer, module.exports.memory.buffer);
42
  return module;
43 44 45
}

function testPokeMemory() {
46
  print("testPokeMemory");
mtrofin's avatar
mtrofin committed
47
  var module = genModule(new WebAssembly.Memory({initial: 1}));
48
  var buffer = module.exports.memory.buffer;
49
  var main = module.exports.main;
50 51 52 53 54
  assertEquals(kMemSize, buffer.byteLength);

  var array = new Int8Array(buffer);
  assertEquals(kMemSize, array.length);

55
  assertTrue(array.every((e => e === 0)));
56 57

  for (var i = 0; i < 10; i++) {
58
    assertEquals(0, main(kMemSize - 4));
59 60

    array[kMemSize/2 + i] = 1;
61 62
    assertEquals(0, main(kMemSize/2 - 4));
    assertEquals(-1, main(kMemSize - 4));
63 64

    array[kMemSize/2 + i] = 0;
65
    assertEquals(0, main(kMemSize - 4));
66 67 68 69 70
  }
}

testPokeMemory();

71 72 73 74
function genAndGetMain(buffer) {
  return genModule(buffer).exports.main;  // to prevent intermediates living
}

75
function testSurvivalAcrossGc() {
mtrofin's avatar
mtrofin committed
76
  var checker = genAndGetMain(new WebAssembly.Memory({initial: 1}));
77
  for (var i = 0; i < 3; i++) {
78 79 80 81 82 83 84 85 86 87 88 89 90
    print("gc run ", i);
    assertEquals(0, checker(kMemSize - 4));
    gc();
  }
}

testSurvivalAcrossGc();
testSurvivalAcrossGc();
testSurvivalAcrossGc();
testSurvivalAcrossGc();


function testPokeOuterMemory() {
91 92
  print("testPokeOuterMemory");
  var buffer = new WebAssembly.Memory({initial: kMemSize / kPageSize});
93
  var module = genModule(buffer);
94
  var main = module.exports.main;
95
  assertEquals(kMemSize, buffer.buffer.byteLength);
96

97
  var array = new Int8Array(buffer.buffer);
98 99
  assertEquals(kMemSize, array.length);

100
  assertTrue(array.every((e => e === 0)));
101 102

  for (var i = 0; i < 10; i++) {
103
    assertEquals(0, main(kMemSize - 4));
104 105

    array[kMemSize/2 + i] = 1;
106 107
    assertEquals(0, main(kMemSize/2 - 4));
    assertEquals(-1, main(kMemSize - 4));
108 109

    array[kMemSize/2 + i] = 0;
110
    assertEquals(0, main(kMemSize - 4));
111 112 113 114 115 116
  }
}

testPokeOuterMemory();

function testOuterMemorySurvivalAcrossGc() {
117
  var buffer = new WebAssembly.Memory({initial: kMemSize / kPageSize});
118 119
  var checker = genAndGetMain(buffer);
  for (var i = 0; i < 3; i++) {
120 121 122 123 124 125 126 127 128 129 130 131 132
    print("gc run ", i);
    assertEquals(0, checker(kMemSize - 4));
    gc();
  }
}

testOuterMemorySurvivalAcrossGc();
testOuterMemorySurvivalAcrossGc();
testOuterMemorySurvivalAcrossGc();
testOuterMemorySurvivalAcrossGc();


function testOOBThrows() {
133 134 135
  var builder = new WasmModuleBuilder();

  builder.addMemory(1, 1, true);
136
  builder.addFunction("geti", kSig_i_ii)
137
    .addBody([
138 139
      kExprLocalGet, 0,
      kExprLocalGet, 1,
140
      kExprI32LoadMem, 0, 0,
141
      kExprI32StoreMem, 0, 0,
142
      kExprLocalGet, 1,
143
      kExprI32LoadMem, 0, 0,
144 145 146 147
    ])
    .exportFunc();

  var module = builder.instantiate();
148

149 150
  let read = offset => module.exports.geti(0, offset);
  let write = offset =>  module.exports.geti(offset, 0);
151

152 153
  assertEquals(0, read(65532));
  assertEquals(0, write(65532));
154

155 156 157
  // Note that this test might be run concurrently in multiple Isolates, which
  // makes an exact comparison of the expected trap count unreliable. But is is
  // still possible to check the lower bound for the expected trap count.
158
  for (let offset = 65534; offset < 66536; offset++) {
159
    const trap_count = %GetWasmRecoveredTrapCount();
160 161
    assertTraps(kTrapMemOutOfBounds, () => read(offset));
    assertTraps(kTrapMemOutOfBounds, () => write(offset));
162
    if (%IsWasmTrapHandlerEnabled()) {
163
      assertTrue(trap_count + 2 <= %GetWasmRecoveredTrapCount());
164
    }
165 166 167 168
  }
}

testOOBThrows();
169 170

function testAddressSpaceLimit() {
171 172 173
  // 1TiB + 4 GiB, see wasm-memory.h
  const kMaxAddressSpace = 1 * 1024 * 1024 * 1024 * 1024
                         + 4 * 1024 * 1024 * 1024;
174
  const kAddressSpacePerMemory = 10 * 1024 * 1024 * 1024;
175

176
  let last_memory;
177 178 179 180
  try {
    let memories = [];
    let address_space = 0;
    while (address_space <= kMaxAddressSpace + 1) {
181 182
      last_memory = new WebAssembly.Memory({initial: 1})
      memories.push(last_memory);
183 184 185 186 187 188
      address_space += kAddressSpacePerMemory;
    }
  } catch (e) {
    assertTrue(e instanceof RangeError);
    return;
  }
189
  assertUnreachable("should have reached the address space limit");
190 191 192 193 194
}

if(%IsWasmTrapHandlerEnabled()) {
  testAddressSpaceLimit();
}