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 load("test/mjsunit/wasm/wasm-module-builder.js"); |
9 | 9 |
10 var kMemSize = 65536; | 10 var kMemSize = 65536; |
11 | 11 |
12 function genModule(memory) { | 12 function genModule(memory) { |
13 var builder = new WasmModuleBuilder(); | 13 var builder = new WasmModuleBuilder(); |
14 | 14 |
15 builder.addMemory(1, 1, true); | 15 builder.addMemory(1, 1, true); |
16 builder.addFunction("main", kSig_i_i) | 16 builder.addFunction("main", kSig_i_i) |
17 .addBody([ | 17 .addBody([ |
18 // main body: while(i) { if(mem[i]) return -1; i -= 4; } return 0; | 18 // main body: while(i) { if(mem[i]) return -1; i -= 4; } return 0; |
19 kExprLoop, | 19 // TODO(titzer): this manual bytecode has a copy of test-run-wasm.cc |
20 kExprGetLocal,0, | 20 /**/ kExprLoop, kAstStmt, // -- |
21 kExprIf, | 21 /* */ kExprGetLocal, 0, // -- |
22 kExprGetLocal,0, | 22 /* */ kExprIf, kAstStmt, // -- |
23 kExprI32LoadMem,0,0, | 23 /* */ kExprGetLocal, 0, // -- |
24 kExprIf, | 24 /* */ kExprI32LoadMem, 0, 0, // -- |
25 kExprI8Const,255, | 25 /* */ kExprIf, kAstStmt, // -- |
26 kExprReturn, kArity1, | 26 /* */ kExprI8Const, 255, // -- |
27 kExprEnd, | 27 /* */ kExprReturn, // -- |
28 kExprGetLocal,0, | 28 /* */ kExprEnd, // -- |
29 kExprI8Const,4, | 29 /* */ kExprGetLocal, 0, // -- |
30 kExprI32Sub, | 30 /* */ kExprI8Const, 4, // -- |
31 kExprSetLocal,0, | 31 /* */ kExprI32Sub, // -- |
32 kExprBr, kArity1, 1, | 32 /* */ kExprSetLocal, 0, // -- |
33 kExprEnd, | 33 /* */ kExprBr, 1, // -- |
34 kExprEnd, | 34 /* */ kExprEnd, // -- |
35 kExprI8Const,0 | 35 /* */ kExprEnd, // -- |
| 36 /**/ kExprI8Const, 0 // -- |
36 ]) | 37 ]) |
37 .exportFunc(); | 38 .exportFunc(); |
38 | 39 var module = builder.instantiate(null, memory); |
39 return builder.instantiate(null, memory); | 40 assertTrue(module.exports.memory instanceof WebAssembly.Memory); |
| 41 if (memory != null) assertEquals(memory, module.exports.memory.buffer); |
| 42 return module; |
40 } | 43 } |
41 | 44 |
42 function testPokeMemory() { | 45 function testPokeMemory() { |
43 var module = genModule(null); | 46 var module = genModule(null); |
44 var buffer = module.exports.memory; | 47 var buffer = module.exports.memory.buffer; |
45 var main = module.exports.main; | 48 var main = module.exports.main; |
46 assertEquals(kMemSize, buffer.byteLength); | 49 assertEquals(kMemSize, buffer.byteLength); |
47 | 50 |
48 var array = new Int8Array(buffer); | 51 var array = new Int8Array(buffer); |
49 assertEquals(kMemSize, array.length); | 52 assertEquals(kMemSize, array.length); |
50 | 53 |
51 for (var i = 0; i < kMemSize; i++) { | 54 for (var i = 0; i < kMemSize; i++) { |
52 assertEquals(0, array[i]); | 55 assertEquals(0, array[i]); |
53 } | 56 } |
54 | 57 |
55 for (var i = 0; i < 10; i++) { | 58 for (var i = 0; i < 10; i++) { |
56 assertEquals(0, main(kMemSize - 4)); | 59 assertEquals(0, main(kMemSize - 4)); |
57 | 60 |
58 array[kMemSize/2 + i] = 1; | 61 array[kMemSize/2 + i] = 1; |
59 assertEquals(0, main(kMemSize/2 - 4)); | 62 assertEquals(0, main(kMemSize/2 - 4)); |
60 assertEquals(-1, main(kMemSize - 4)); | 63 assertEquals(-1, main(kMemSize - 4)); |
61 | 64 |
62 array[kMemSize/2 + i] = 0; | 65 array[kMemSize/2 + i] = 0; |
63 assertEquals(0, main(kMemSize - 4)); | 66 assertEquals(0, main(kMemSize - 4)); |
64 } | 67 } |
65 } | 68 } |
66 | 69 |
67 testPokeMemory(); | 70 testPokeMemory(); |
68 | 71 |
| 72 function genAndGetMain(buffer) { |
| 73 return genModule(buffer).exports.main; // to prevent intermediates living |
| 74 } |
| 75 |
69 function testSurvivalAcrossGc() { | 76 function testSurvivalAcrossGc() { |
70 var checker = genModule(null).exports.main; | 77 var checker = genAndGetMain(null); |
71 for (var i = 0; i < 5; i++) { | 78 for (var i = 0; i < 3; i++) { |
72 print("gc run ", i); | 79 print("gc run ", i); |
73 assertEquals(0, checker(kMemSize - 4)); | 80 assertEquals(0, checker(kMemSize - 4)); |
74 gc(); | 81 gc(); |
75 } | 82 } |
76 } | 83 } |
77 | 84 |
78 testSurvivalAcrossGc(); | 85 testSurvivalAcrossGc(); |
79 testSurvivalAcrossGc(); | 86 testSurvivalAcrossGc(); |
80 testSurvivalAcrossGc(); | 87 testSurvivalAcrossGc(); |
81 testSurvivalAcrossGc(); | 88 testSurvivalAcrossGc(); |
(...skipping 21 matching lines...) Expand all Loading... |
103 | 110 |
104 array[kMemSize/2 + i] = 0; | 111 array[kMemSize/2 + i] = 0; |
105 assertEquals(0, main(kMemSize - 4)); | 112 assertEquals(0, main(kMemSize - 4)); |
106 } | 113 } |
107 } | 114 } |
108 | 115 |
109 testPokeOuterMemory(); | 116 testPokeOuterMemory(); |
110 | 117 |
111 function testOuterMemorySurvivalAcrossGc() { | 118 function testOuterMemorySurvivalAcrossGc() { |
112 var buffer = new ArrayBuffer(kMemSize); | 119 var buffer = new ArrayBuffer(kMemSize); |
113 var checker = genModule(buffer).exports.main; | 120 var checker = genAndGetMain(buffer); |
114 for (var i = 0; i < 5; i++) { | 121 for (var i = 0; i < 3; i++) { |
115 print("gc run ", i); | 122 print("gc run ", i); |
116 assertEquals(0, checker(kMemSize - 4)); | 123 assertEquals(0, checker(kMemSize - 4)); |
117 gc(); | 124 gc(); |
118 } | 125 } |
119 } | 126 } |
120 | 127 |
121 testOuterMemorySurvivalAcrossGc(); | 128 testOuterMemorySurvivalAcrossGc(); |
122 testOuterMemorySurvivalAcrossGc(); | 129 testOuterMemorySurvivalAcrossGc(); |
123 testOuterMemorySurvivalAcrossGc(); | 130 testOuterMemorySurvivalAcrossGc(); |
124 testOuterMemorySurvivalAcrossGc(); | 131 testOuterMemorySurvivalAcrossGc(); |
125 | 132 |
126 | 133 |
127 function testOOBThrows() { | 134 function testOOBThrows() { |
128 var builder = new WasmModuleBuilder(); | 135 var builder = new WasmModuleBuilder(); |
129 | 136 |
130 builder.addMemory(1, 1, true); | 137 builder.addMemory(1, 1, true); |
131 builder.addFunction("geti", kSig_i_ii) | 138 builder.addFunction("geti", kSig_i_ii) |
132 .addBody([ | 139 .addBody([ |
133 kExprGetLocal, 0, | 140 kExprGetLocal, 0, |
134 kExprGetLocal, 1, | 141 kExprGetLocal, 1, |
135 kExprI32LoadMem, 0, 0, | 142 kExprI32LoadMem, 0, 0, |
136 kExprI32StoreMem, 0, 0 | 143 kExprI32StoreMem, 0, 0, |
| 144 kExprGetLocal, 1, |
| 145 kExprI32LoadMem, 0, 0, |
137 ]) | 146 ]) |
138 .exportFunc(); | 147 .exportFunc(); |
139 | 148 |
140 var module = builder.instantiate(); | 149 var module = builder.instantiate(); |
141 var offset; | 150 var offset; |
142 | 151 |
143 function read() { return module.exports.geti(0, offset); } | 152 function read() { return module.exports.geti(0, offset); } |
144 function write() { return module.exports.geti(offset, 0); } | 153 function write() { return module.exports.geti(offset, 0); } |
145 | 154 |
146 for (offset = 0; offset < 65533; offset++) { | 155 for (offset = 0; offset < 65533; offset++) { |
147 assertEquals(0, read()); | 156 assertEquals(0, read()); |
148 assertEquals(0, write()); | 157 assertEquals(0, write()); |
149 } | 158 } |
150 | 159 |
151 | 160 |
152 for (offset = 65534; offset < 66536; offset++) { | 161 for (offset = 65534; offset < 66536; offset++) { |
153 assertTraps(kTrapMemOutOfBounds, read); | 162 assertTraps(kTrapMemOutOfBounds, read); |
154 assertTraps(kTrapMemOutOfBounds, write); | 163 assertTraps(kTrapMemOutOfBounds, write); |
155 } | 164 } |
156 } | 165 } |
157 | 166 |
158 testOOBThrows(); | 167 testOOBThrows(); |
OLD | NEW |