OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 // Flags: --expose-wasm --expose-gc |
| 6 |
| 7 load("test/mjsunit/wasm/wasm-constants.js"); |
| 8 load("test/mjsunit/wasm/wasm-module-builder.js"); |
| 9 |
| 10 // This test verifies that when instances are exported, Gc'ed, the other |
| 11 // instances in the chain still maintain a consistent view of the memory. |
| 12 (function InstanceMemoryGcStress() { |
| 13 print("InstanceMemoryGcStress"); |
| 14 let memory = new WebAssembly.Memory({initial: 100, maximum: 1500}); |
| 15 var builder = new WasmModuleBuilder(); |
| 16 builder.addImportedMemory("imported_mem"); |
| 17 builder.addFunction("mem_size", kSig_i_v) |
| 18 .addBody([kExprMemorySize, kMemoryZero]) |
| 19 .exportFunc(); |
| 20 builder.addFunction("grow", kSig_i_i) |
| 21 .addBody([kExprGetLocal, 0, kExprGrowMemory, kMemoryZero]) |
| 22 .exportFunc(); |
| 23 var instances = []; |
| 24 for (var i = 0; i < 5; i++) { |
| 25 gc(); |
| 26 instances.push(builder.instantiate({imported_mem: memory})); |
| 27 } |
| 28 function grow_instance_0(pages) { return instances[0].exports.grow(pages); } |
| 29 function grow_instance_1(pages) { return instances[1].exports.grow(pages); } |
| 30 function grow_instance_2(pages) { return instances[2].exports.grow(pages); } |
| 31 function grow_instance_3(pages) { return instances[3].exports.grow(pages); } |
| 32 function grow_instance_4(pages) { return instances[4].exports.grow(pages); } |
| 33 |
| 34 var start_index = 0; |
| 35 var end_index = 5; |
| 36 function verify_mem_size(expected_pages) { |
| 37 assertEquals(expected_pages*kPageSize, memory.buffer.byteLength); |
| 38 for (var i = start_index; i < end_index; i++) { |
| 39 assertEquals(expected_pages, instances[i].exports.mem_size()); |
| 40 } |
| 41 } |
| 42 |
| 43 // Verify initial memory size of all instances, grow and verify that all |
| 44 // instances are updated correctly. |
| 45 verify_mem_size(100); |
| 46 assertEquals(100, memory.grow(500)); |
| 47 verify_mem_size(600); |
| 48 |
| 49 instances[1] = null; |
| 50 gc(); |
| 51 gc(); |
| 52 |
| 53 // i[0] - i[2] - i[3] - i[4] |
| 54 start_index = 2; |
| 55 verify_mem_size(600); |
| 56 assertEquals(600, instances[0].exports.mem_size()); |
| 57 assertEquals(600, grow_instance_2(200)); |
| 58 assertEquals(800*kPageSize, memory.buffer.byteLength); |
| 59 verify_mem_size(800); |
| 60 assertEquals(800, instances[0].exports.mem_size()); |
| 61 |
| 62 // Instantiate a new instance and verify that it can be grown correctly. |
| 63 instances.push(builder.instantiate({imported_mem: memory})); |
| 64 function grow_instance_5(pages) { return instances[5].exports.grow(pages); } |
| 65 gc(); |
| 66 gc(); |
| 67 |
| 68 // i[0] - i[2] - i[3] - i[4] - i[5] |
| 69 start_index = 2; |
| 70 end_index = 6; |
| 71 verify_mem_size(800); |
| 72 assertEquals(800, instances[0].exports.mem_size()); |
| 73 assertEquals(800, grow_instance_2(100)); |
| 74 assertEquals(900*kPageSize, memory.buffer.byteLength); |
| 75 verify_mem_size(900); |
| 76 assertEquals(900, instances[0].exports.mem_size()); |
| 77 |
| 78 instances[4] = null; |
| 79 |
| 80 gc(); |
| 81 gc(); |
| 82 |
| 83 // i[0] - i[2] - i[3] - i[5] |
| 84 assertEquals(900, instances[0].exports.mem_size()); |
| 85 assertEquals(900, instances[2].exports.mem_size()); |
| 86 assertEquals(900, instances[3].exports.mem_size()); |
| 87 assertEquals(900, instances[5].exports.mem_size()); |
| 88 assertEquals(900, memory.grow(100)); |
| 89 assertEquals(1000*kPageSize, memory.buffer.byteLength); |
| 90 assertEquals(1000, instances[0].exports.mem_size()); |
| 91 assertEquals(1000, instances[2].exports.mem_size()); |
| 92 assertEquals(1000, instances[3].exports.mem_size()); |
| 93 assertEquals(1000, instances[5].exports.mem_size()); |
| 94 |
| 95 gc(); |
| 96 gc(); |
| 97 |
| 98 instances[3] = null; |
| 99 |
| 100 // i[0] - i[2] - i[5] |
| 101 assertEquals(1000, instances[0].exports.mem_size()); |
| 102 assertEquals(1000, instances[2].exports.mem_size()); |
| 103 assertEquals(1000, instances[5].exports.mem_size()); |
| 104 assertEquals(1000, memory.grow(100)); |
| 105 assertEquals(1100*kPageSize, memory.buffer.byteLength); |
| 106 assertEquals(1100, instances[0].exports.mem_size()); |
| 107 assertEquals(1100, instances[2].exports.mem_size()); |
| 108 assertEquals(1100, instances[5].exports.mem_size()); |
| 109 |
| 110 instances[0] = null; |
| 111 gc(); |
| 112 gc(); |
| 113 |
| 114 // i[2] - i[5] |
| 115 assertEquals(1100, instances[2].exports.mem_size()); |
| 116 assertEquals(1100, instances[5].exports.mem_size()); |
| 117 assertEquals(1100, grow_instance_5(1)); |
| 118 gc(); |
| 119 gc(); |
| 120 |
| 121 assertEquals(1101*kPageSize, memory.buffer.byteLength); |
| 122 assertEquals(1101, instances[2].exports.mem_size()); |
| 123 assertEquals(1101, instances[5].exports.mem_size()); |
| 124 })(); |
OLD | NEW |