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 ValidateSharedInstanceMemory() { | |
13 print("ValidateSharedInstanceMemory"); | |
14 let memory = new WebAssembly.Memory({initial: 5, maximum: 100}); | |
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 instances.push(builder.instantiate({imported_mem: memory})); | |
26 } | |
27 function grow_instance_0(pages) { return instances[0].exports.grow(pages); } | |
28 function grow_instance_1(pages) { return instances[1].exports.grow(pages); } | |
29 function grow_instance_2(pages) { return instances[2].exports.grow(pages); } | |
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 | |
33 var start_index = 0; | |
34 var end_index = 5; | |
35 function verify_mem_size(expected_pages) { | |
36 assertEquals(expected_pages*kPageSize, memory.buffer.byteLength); | |
37 for (var i = start_index; i < end_index; i++) { | |
38 assertEquals(expected_pages, instances[i].exports.mem_size()); | |
39 } | |
40 } | |
41 | |
42 // Verify initial memory size of all instances, grow and verify that all | |
43 // instances are updated correctly. | |
44 verify_mem_size(5); | |
45 assertEquals(5, memory.grow(6)); | |
46 verify_mem_size(11); | |
47 | |
48 instances[1] = null; | |
49 gc(); | |
50 | |
51 // i[0] - i[2] - i[3] - i[4] | |
52 start_index = 2; | |
53 verify_mem_size(11); | |
54 assertEquals(11, instances[0].exports.mem_size()); | |
55 assertEquals(11, grow_instance_2(10)); | |
56 assertEquals(21*kPageSize, memory.buffer.byteLength); | |
57 verify_mem_size(21); | |
58 assertEquals(21, instances[0].exports.mem_size()); | |
59 | |
60 instances[4] = null; | |
61 gc(); | |
62 | |
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)); | |
68 assertEquals(23*kPageSize, memory.buffer.byteLength); | |
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 | |
73 instances[0] = null; | |
74 gc(); | |
75 | |
76 // i[2] - i[3] | |
77 assertEquals(23, instances[2].exports.mem_size()); | |
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 | |
84 // Instantiate a new instance and verify that it can be grown correctly. | |
85 instances.push(builder.instantiate({imported_mem: memory})); | |
86 function grow_instance_5(pages) { return instances[5].exports.grow(pages); } | |
87 | |
88 // i[2] - i[3] - i[5] | |
89 assertEquals(28, instances[2].exports.mem_size()); | |
90 assertEquals(28, instances[3].exports.mem_size()); | |
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)); | |
98 assertEquals(35*kPageSize, memory.buffer.byteLength); | |
99 assertEquals(35, instances[2].exports.mem_size()); | |
100 assertEquals(35, instances[3].exports.mem_size()); | |
101 assertEquals(35, instances[5].exports.mem_size()); | |
102 | |
103 })(); | |
OLD | NEW |