OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 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 --stress-compaction | 5 // Flags: --expose-wasm --expose-gc --stress-compaction |
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 | 9 |
9 var kMemSize = 65536; | 10 var kMemSize = 65536; |
10 | 11 |
11 function genModule(memory) { | 12 function genModule(memory) { |
12 var kBodySize = 27; | 13 var builder = new WasmModuleBuilder(); |
13 var kNameMainOffset = kHeaderSize + 28 + kBodySize + 1; | |
14 | 14 |
15 var data = bytesWithHeader( | 15 builder.addMemory(1, 1, true); |
16 kDeclMemory, | 16 builder.addFunction("main", [kAstI32, kAstI32]) |
17 1, 1, 1, // memory | 17 .addBody([ |
18 // -- signatures | 18 kExprBlock,2, |
19 kDeclSignatures, 1, | 19 kExprLoop,1, |
20 1, kAstI32, kAstI32, // int->int | 20 kExprIf, |
21 // -- main function | 21 kExprGetLocal,0, |
22 kDeclFunctions, 1, | 22 kExprBr, 0, |
23 kDeclFunctionLocals | kDeclFunctionName | kDeclFunctionExport, | 23 kExprIfElse, |
24 0, 0, | 24 kExprI32LoadMem,0,kExprGetLocal,0, |
25 kNameMainOffset, 0, 0, 0, // name offset | 25 kExprBr,2, kExprI8Const, 255, |
26 1, 0, // local int32 count | 26 kExprSetLocal,0, |
27 0, 0, // local int64 count | 27 kExprI32Sub,kExprGetLocal,0,kExprI8Const,4, |
28 0, 0, // local float32 count | 28 kExprI8Const,0]) |
29 0, 0, // local float64 count | 29 .exportFunc(); |
30 kBodySize, 0, // code size | |
31 // main body: while(i) { if(mem[i]) return -1; i -= 4; } return 0; | |
32 kExprBlock,2, | |
33 kExprLoop,1, | |
34 kExprIf, | |
35 kExprGetLocal,0, | |
36 kExprBr, 0, | |
37 kExprIfElse, | |
38 kExprI32LoadMem,0,kExprGetLocal,0, | |
39 kExprBr,2, kExprI8Const, 255, | |
40 kExprSetLocal,0, | |
41 kExprI32Sub,kExprGetLocal,0,kExprI8Const,4, | |
42 kExprI8Const,0, | |
43 // names | |
44 kDeclEnd, | |
45 'm', 'a', 'i', 'n', 0 // -- | |
46 ); | |
47 | 30 |
48 return _WASMEXP_.instantiateModule(data, null, memory); | 31 return builder.instantiate(null, memory); |
49 } | 32 } |
50 | 33 |
51 function testPokeMemory() { | 34 function testPokeMemory() { |
52 var module = genModule(null); | 35 var module = genModule(null); |
53 var buffer = module.memory; | 36 var buffer = module.memory; |
| 37 var main = module.exports.main; |
54 assertEquals(kMemSize, buffer.byteLength); | 38 assertEquals(kMemSize, buffer.byteLength); |
55 | 39 |
56 var array = new Int8Array(buffer); | 40 var array = new Int8Array(buffer); |
57 assertEquals(kMemSize, array.length); | 41 assertEquals(kMemSize, array.length); |
58 | 42 |
59 for (var i = 0; i < kMemSize; i++) { | 43 for (var i = 0; i < kMemSize; i++) { |
60 assertEquals(0, array[i]); | 44 assertEquals(0, array[i]); |
61 } | 45 } |
62 | 46 |
63 for (var i = 0; i < 10; i++) { | 47 for (var i = 0; i < 10; i++) { |
64 assertEquals(0, module.main(kMemSize - 4)); | 48 assertEquals(0, main(kMemSize - 4)); |
65 | 49 |
66 array[kMemSize/2 + i] = 1; | 50 array[kMemSize/2 + i] = 1; |
67 assertEquals(0, module.main(kMemSize/2 - 4)); | 51 assertEquals(0, main(kMemSize/2 - 4)); |
68 assertEquals(-1, module.main(kMemSize - 4)); | 52 assertEquals(-1, main(kMemSize - 4)); |
69 | 53 |
70 array[kMemSize/2 + i] = 0; | 54 array[kMemSize/2 + i] = 0; |
71 assertEquals(0, module.main(kMemSize - 4)); | 55 assertEquals(0, main(kMemSize - 4)); |
72 } | 56 } |
73 } | 57 } |
74 | 58 |
75 testPokeMemory(); | 59 testPokeMemory(); |
76 | 60 |
77 function testSurvivalAcrossGc() { | 61 function testSurvivalAcrossGc() { |
78 var checker = genModule(null).main; | 62 var checker = genModule(null).exports.main; |
79 for (var i = 0; i < 5; i++) { | 63 for (var i = 0; i < 5; i++) { |
80 print("gc run ", i); | 64 print("gc run ", i); |
81 assertEquals(0, checker(kMemSize - 4)); | 65 assertEquals(0, checker(kMemSize - 4)); |
82 gc(); | 66 gc(); |
83 } | 67 } |
84 } | 68 } |
85 | 69 |
86 testSurvivalAcrossGc(); | 70 testSurvivalAcrossGc(); |
87 testSurvivalAcrossGc(); | 71 testSurvivalAcrossGc(); |
88 testSurvivalAcrossGc(); | 72 testSurvivalAcrossGc(); |
89 testSurvivalAcrossGc(); | 73 testSurvivalAcrossGc(); |
90 | 74 |
91 | 75 |
92 function testPokeOuterMemory() { | 76 function testPokeOuterMemory() { |
93 var buffer = new ArrayBuffer(kMemSize); | 77 var buffer = new ArrayBuffer(kMemSize); |
94 var module = genModule(buffer); | 78 var module = genModule(buffer); |
| 79 var main = module.exports.main; |
95 assertEquals(kMemSize, buffer.byteLength); | 80 assertEquals(kMemSize, buffer.byteLength); |
96 | 81 |
97 var array = new Int8Array(buffer); | 82 var array = new Int8Array(buffer); |
98 assertEquals(kMemSize, array.length); | 83 assertEquals(kMemSize, array.length); |
99 | 84 |
100 for (var i = 0; i < kMemSize; i++) { | 85 for (var i = 0; i < kMemSize; i++) { |
101 assertEquals(0, array[i]); | 86 assertEquals(0, array[i]); |
102 } | 87 } |
103 | 88 |
104 for (var i = 0; i < 10; i++) { | 89 for (var i = 0; i < 10; i++) { |
105 assertEquals(0, module.main(kMemSize - 4)); | 90 assertEquals(0, main(kMemSize - 4)); |
106 | 91 |
107 array[kMemSize/2 + i] = 1; | 92 array[kMemSize/2 + i] = 1; |
108 assertEquals(0, module.main(kMemSize/2 - 4)); | 93 assertEquals(0, main(kMemSize/2 - 4)); |
109 assertEquals(-1, module.main(kMemSize - 4)); | 94 assertEquals(-1, main(kMemSize - 4)); |
110 | 95 |
111 array[kMemSize/2 + i] = 0; | 96 array[kMemSize/2 + i] = 0; |
112 assertEquals(0, module.main(kMemSize - 4)); | 97 assertEquals(0, main(kMemSize - 4)); |
113 } | 98 } |
114 } | 99 } |
115 | 100 |
116 testPokeOuterMemory(); | 101 testPokeOuterMemory(); |
117 | 102 |
118 function testOuterMemorySurvivalAcrossGc() { | 103 function testOuterMemorySurvivalAcrossGc() { |
119 var buffer = new ArrayBuffer(kMemSize); | 104 var buffer = new ArrayBuffer(kMemSize); |
120 var checker = genModule(buffer).main; | 105 var checker = genModule(buffer).exports.main; |
121 for (var i = 0; i < 5; i++) { | 106 for (var i = 0; i < 5; i++) { |
122 print("gc run ", i); | 107 print("gc run ", i); |
123 assertEquals(0, checker(kMemSize - 4)); | 108 assertEquals(0, checker(kMemSize - 4)); |
124 gc(); | 109 gc(); |
125 } | 110 } |
126 } | 111 } |
127 | 112 |
128 testOuterMemorySurvivalAcrossGc(); | 113 testOuterMemorySurvivalAcrossGc(); |
129 testOuterMemorySurvivalAcrossGc(); | 114 testOuterMemorySurvivalAcrossGc(); |
130 testOuterMemorySurvivalAcrossGc(); | 115 testOuterMemorySurvivalAcrossGc(); |
131 testOuterMemorySurvivalAcrossGc(); | 116 testOuterMemorySurvivalAcrossGc(); |
132 | 117 |
133 | 118 |
134 function testOOBThrows() { | 119 function testOOBThrows() { |
135 var kBodySize = 8; | 120 var builder = new WasmModuleBuilder(); |
136 var kNameMainOffset = kHeaderSize + 29 + kBodySize + 1; | |
137 | 121 |
138 var data = bytesWithHeader( | 122 builder.addMemory(1, 1, true); |
139 kDeclMemory, | 123 builder.addFunction("geti", [kAstI32, kAstI32, kAstI32]) |
140 1, 1, 1, // memory = 64KB | 124 .addBody([ |
141 // -- signatures | 125 kExprI32StoreMem, 0, kExprGetLocal, 0, kExprI32LoadMem, 0, kExprGetLocal,
1 |
142 kDeclSignatures, 1, | 126 ]) |
143 2, kAstI32, kAstI32, kAstI32, // int->int | 127 .exportFunc(); |
144 // -- main function | |
145 kDeclFunctions, 1, | |
146 kDeclFunctionLocals | kDeclFunctionName | kDeclFunctionExport, | |
147 0, 0, | |
148 kNameMainOffset, 0, 0, 0, // name offset | |
149 1, 0, // local int32 count | |
150 0, 0, // local int64 count | |
151 0, 0, // local float32 count | |
152 0, 0, // local float64 count | |
153 kBodySize, 0, // code size | |
154 // geti: return mem[a] = mem[b] | |
155 kExprI32StoreMem, 0, kExprGetLocal, 0, kExprI32LoadMem, 0, kExprGetLocal, 1, | |
156 // names | |
157 kDeclEnd, | |
158 'g','e','t','i', 0 // -- | |
159 ); | |
160 | 128 |
161 var memory = null; | 129 var module = builder.instantiate(); |
162 var module = _WASMEXP_.instantiateModule(data, null, memory); | |
163 | 130 |
164 var offset; | 131 var offset; |
165 | 132 |
166 function read() { return module.geti(0, offset); } | 133 function read() { return module.exports.geti(0, offset); } |
167 function write() { return module.geti(offset, 0); } | 134 function write() { return module.exports.geti(offset, 0); } |
168 | 135 |
169 for (offset = 0; offset < 65533; offset++) { | 136 for (offset = 0; offset < 65533; offset++) { |
170 assertEquals(0, read()); | 137 assertEquals(0, read()); |
171 assertEquals(0, write()); | 138 assertEquals(0, write()); |
172 } | 139 } |
173 | 140 |
174 | 141 |
175 for (offset = 65534; offset < 66536; offset++) { | 142 for (offset = 65534; offset < 66536; offset++) { |
176 assertTraps(kTrapMemOutOfBounds, read); | 143 assertTraps(kTrapMemOutOfBounds, read); |
177 assertTraps(kTrapMemOutOfBounds, write); | 144 assertTraps(kTrapMemOutOfBounds, write); |
178 } | 145 } |
179 } | 146 } |
180 | 147 |
181 testOOBThrows(); | 148 testOOBThrows(); |
OLD | NEW |