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