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