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 --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 kPageSize = 0x10000; | 10 var kPageSize = 0x10000; |
11 | 11 |
12 function genGrowMemoryBuilder() { | 12 function genGrowMemoryBuilder() { |
13 var builder = new WasmModuleBuilder(); | 13 var builder = new WasmModuleBuilder(); |
14 builder.addFunction("grow_memory", kSig_i_i) | 14 builder.addFunction("grow_memory", kSig_i_i) |
15 .addBody([kExprGetLocal, 0, kExprGrowMemory]) | 15 .addBody([kExprGetLocal, 0, kExprGrowMemory]) |
16 .exportFunc(); | 16 .exportFunc(); |
17 builder.addFunction("load", kSig_i_i) | 17 builder.addFunction("load", kSig_i_i) |
18 .addBody([kExprGetLocal, 0, kExprI32LoadMem, 0, 0]) | 18 .addBody([kExprGetLocal, 0, kExprI32LoadMem, 0, 0]) |
19 .exportFunc(); | 19 .exportFunc(); |
20 builder.addFunction("store", kSig_i_ii) | 20 builder.addFunction("store", kSig_i_ii) |
21 .addBody([kExprGetLocal, 0, kExprGetLocal, 1, kExprI32StoreMem, 0, 0]) | 21 .addBody([kExprGetLocal, 0, kExprGetLocal, 1, kExprI32StoreMem, 0, 0]) |
22 .exportFunc(); | 22 .exportFunc(); |
| 23 builder.addFunction("load16", kSig_i_i) |
| 24 .addBody([kExprGetLocal, 0, kExprI32LoadMem16U, 0, 0]) |
| 25 .exportFunc(); |
| 26 builder.addFunction("store16", kSig_i_ii) |
| 27 .addBody([kExprGetLocal, 0, kExprGetLocal, 1, kExprI32StoreMem16, 0, 0]) |
| 28 .exportFunc(); |
| 29 builder.addFunction("load8", kSig_i_i) |
| 30 .addBody([kExprGetLocal, 0, kExprI32LoadMem8U, 0, 0]) |
| 31 .exportFunc(); |
| 32 builder.addFunction("store8", kSig_i_ii) |
| 33 .addBody([kExprGetLocal, 0, kExprGetLocal, 1, kExprI32StoreMem8, 0, 0]) |
| 34 .exportFunc(); |
23 return builder; | 35 return builder; |
24 } | 36 } |
25 | 37 |
26 function testGrowMemoryReadWrite() { | 38 function testGrowMemoryReadWrite32() { |
27 var builder = genGrowMemoryBuilder(); | 39 var builder = genGrowMemoryBuilder(); |
28 builder.addMemory(1, 1, false); | 40 builder.addMemory(1, 1, false); |
29 var module = builder.instantiate(); | 41 var module = builder.instantiate(); |
30 var offset; | 42 var offset; |
31 function peek() { return module.exports.load(offset); } | 43 function peek() { return module.exports.load(offset); } |
32 function poke(value) { return module.exports.store(offset, value); } | 44 function poke(value) { return module.exports.store(offset, value); } |
33 function growMem(pages) { return module.exports.grow_memory(pages); } | 45 function growMem(pages) { return module.exports.grow_memory(pages); } |
34 | 46 |
35 for(offset = 0; offset <= (kPageSize - 4); offset++) { | 47 for(offset = 0; offset <= (kPageSize - 4); offset+=4) { |
36 poke(20); | 48 poke(20); |
37 assertEquals(20, peek()); | 49 assertEquals(20, peek()); |
38 } | 50 } |
39 for (offset = kPageSize - 3; offset < kPageSize + 4; offset++) { | 51 for (offset = kPageSize - 3; offset < kPageSize + 4; offset++) { |
40 assertTraps(kTrapMemOutOfBounds, poke); | 52 assertTraps(kTrapMemOutOfBounds, poke); |
41 assertTraps(kTrapMemOutOfBounds, peek); | 53 assertTraps(kTrapMemOutOfBounds, peek); |
42 } | 54 } |
43 | 55 |
44 assertEquals(1, growMem(3)); | 56 assertEquals(1, growMem(3)); |
45 | 57 |
46 for (offset = kPageSize; offset <= 4*kPageSize -4; offset++) { | 58 for (offset = kPageSize; offset <= 4*kPageSize -4; offset+=4) { |
47 poke(20); | 59 poke(20); |
48 assertEquals(20, peek()); | 60 assertEquals(20, peek()); |
49 } | 61 } |
50 for (offset = 4*kPageSize - 3; offset < 4*kPageSize + 4; offset++) { | 62 for (offset = 4*kPageSize - 3; offset < 4*kPageSize + 4; offset++) { |
51 assertTraps(kTrapMemOutOfBounds, poke); | 63 assertTraps(kTrapMemOutOfBounds, poke); |
52 assertTraps(kTrapMemOutOfBounds, peek); | 64 assertTraps(kTrapMemOutOfBounds, peek); |
53 } | 65 } |
54 | 66 |
55 assertEquals(4, growMem(15)); | 67 assertEquals(4, growMem(15)); |
56 | 68 |
57 for (offset = 4*kPageSize - 3; offset <= 4*kPageSize + 4; offset++) { | 69 for (offset = 4*kPageSize - 3; offset <= 4*kPageSize + 4; offset+=4) { |
58 poke(20); | 70 poke(20); |
59 assertEquals(20, peek()); | 71 assertEquals(20, peek()); |
60 } | 72 } |
61 for (offset = 19*kPageSize - 10; offset <= 19*kPageSize - 4; offset++) { | 73 for (offset = 19*kPageSize - 10; offset <= 19*kPageSize - 4; offset+=4) { |
62 poke(20); | 74 poke(20); |
63 assertEquals(20, peek()); | 75 assertEquals(20, peek()); |
64 } | 76 } |
65 for (offset = 19*kPageSize - 3; offset < 19*kPageSize + 5; offset++) { | 77 for (offset = 19*kPageSize - 3; offset < 19*kPageSize + 5; offset++) { |
66 assertTraps(kTrapMemOutOfBounds, poke); | 78 assertTraps(kTrapMemOutOfBounds, poke); |
67 assertTraps(kTrapMemOutOfBounds, peek); | 79 assertTraps(kTrapMemOutOfBounds, peek); |
68 } | 80 } |
69 } | 81 } |
70 | 82 |
71 testGrowMemoryReadWrite(); | 83 testGrowMemoryReadWrite32(); |
| 84 |
| 85 function testGrowMemoryReadWrite16() { |
| 86 var builder = genGrowMemoryBuilder(); |
| 87 builder.addMemory(1, 1, false); |
| 88 var module = builder.instantiate(); |
| 89 var offset; |
| 90 function peek() { return module.exports.load16(offset); } |
| 91 function poke(value) { return module.exports.store16(offset, value); } |
| 92 function growMem(pages) { return module.exports.grow_memory(pages); } |
| 93 |
| 94 for(offset = 0; offset <= (kPageSize - 2); offset+=2) { |
| 95 poke(20); |
| 96 assertEquals(20, peek()); |
| 97 } |
| 98 for (offset = kPageSize - 1; offset < kPageSize + 4; offset++) { |
| 99 assertTraps(kTrapMemOutOfBounds, poke); |
| 100 assertTraps(kTrapMemOutOfBounds, peek); |
| 101 } |
| 102 |
| 103 assertEquals(1, growMem(3)); |
| 104 |
| 105 for (offset = kPageSize; offset <= 4*kPageSize -2; offset+=2) { |
| 106 poke(20); |
| 107 assertEquals(20, peek()); |
| 108 } |
| 109 for (offset = 4*kPageSize - 1; offset < 4*kPageSize + 4; offset++) { |
| 110 assertTraps(kTrapMemOutOfBounds, poke); |
| 111 assertTraps(kTrapMemOutOfBounds, peek); |
| 112 } |
| 113 |
| 114 assertEquals(4, growMem(15)); |
| 115 |
| 116 for (offset = 4*kPageSize - 2; offset <= 4*kPageSize + 4; offset+=2) { |
| 117 poke(20); |
| 118 assertEquals(20, peek()); |
| 119 } |
| 120 for (offset = 19*kPageSize - 10; offset <= 19*kPageSize - 2; offset+=2) { |
| 121 poke(20); |
| 122 assertEquals(20, peek()); |
| 123 } |
| 124 for (offset = 19*kPageSize - 1; offset < 19*kPageSize + 5; offset++) { |
| 125 assertTraps(kTrapMemOutOfBounds, poke); |
| 126 assertTraps(kTrapMemOutOfBounds, peek); |
| 127 } |
| 128 } |
| 129 |
| 130 testGrowMemoryReadWrite16(); |
| 131 |
| 132 function testGrowMemoryReadWrite8() { |
| 133 var builder = genGrowMemoryBuilder(); |
| 134 builder.addMemory(1, 1, false); |
| 135 var module = builder.instantiate(); |
| 136 var offset; |
| 137 function peek() { return module.exports.load8(offset); } |
| 138 function poke(value) { return module.exports.store8(offset, value); } |
| 139 function growMem(pages) { return module.exports.grow_memory(pages); } |
| 140 |
| 141 for(offset = 0; offset <= kPageSize - 1; offset++) { |
| 142 poke(20); |
| 143 assertEquals(20, peek()); |
| 144 } |
| 145 for (offset = kPageSize; offset < kPageSize + 4; offset++) { |
| 146 assertTraps(kTrapMemOutOfBounds, poke); |
| 147 assertTraps(kTrapMemOutOfBounds, peek); |
| 148 } |
| 149 |
| 150 assertEquals(1, growMem(3)); |
| 151 |
| 152 for (offset = kPageSize; offset <= 4*kPageSize -1; offset++) { |
| 153 poke(20); |
| 154 assertEquals(20, peek()); |
| 155 } |
| 156 for (offset = 4*kPageSize; offset < 4*kPageSize + 4; offset++) { |
| 157 assertTraps(kTrapMemOutOfBounds, poke); |
| 158 assertTraps(kTrapMemOutOfBounds, peek); |
| 159 } |
| 160 |
| 161 assertEquals(4, growMem(15)); |
| 162 |
| 163 for (offset = 4*kPageSize; offset <= 4*kPageSize + 4; offset++) { |
| 164 poke(20); |
| 165 assertEquals(20, peek()); |
| 166 } |
| 167 for (offset = 19*kPageSize - 10; offset <= 19*kPageSize - 1; offset++) { |
| 168 poke(20); |
| 169 assertEquals(20, peek()); |
| 170 } |
| 171 for (offset = 19*kPageSize; offset < 19*kPageSize + 5; offset++) { |
| 172 assertTraps(kTrapMemOutOfBounds, poke); |
| 173 assertTraps(kTrapMemOutOfBounds, peek); |
| 174 } |
| 175 } |
| 176 |
| 177 testGrowMemoryReadWrite8(); |
72 | 178 |
73 function testGrowMemoryZeroInitialSize() { | 179 function testGrowMemoryZeroInitialSize() { |
74 var builder = genGrowMemoryBuilder(); | 180 var builder = genGrowMemoryBuilder(); |
75 var module = builder.instantiate(); | 181 var module = builder.instantiate(); |
76 var offset; | 182 var offset; |
77 function peek() { return module.exports.load(offset); } | 183 function peek() { return module.exports.load(offset); } |
78 function poke(value) { return module.exports.store(offset, value); } | 184 function poke(value) { return module.exports.store(offset, value); } |
79 function growMem(pages) { return module.exports.grow_memory(pages); } | 185 function growMem(pages) { return module.exports.grow_memory(pages); } |
80 | 186 |
81 assertTraps(kTrapMemOutOfBounds, peek); | 187 assertTraps(kTrapMemOutOfBounds, peek); |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 .exportFunc(); | 243 .exportFunc(); |
138 var module = builder.instantiate(); | 244 var module = builder.instantiate(); |
139 function growMem(pages) { return module.exports.grow_memory(pages); } | 245 function growMem(pages) { return module.exports.grow_memory(pages); } |
140 function MemSize() { return module.exports.memory_size(); } | 246 function MemSize() { return module.exports.memory_size(); } |
141 assertEquals(65536, MemSize()); | 247 assertEquals(65536, MemSize()); |
142 assertEquals(1, growMem(1)); | 248 assertEquals(1, growMem(1)); |
143 assertEquals(131072, MemSize()); | 249 assertEquals(131072, MemSize()); |
144 } | 250 } |
145 | 251 |
146 testGrowMemoryCurrentMemory(); | 252 testGrowMemoryCurrentMemory(); |
| 253 |
| 254 function testGrowMemoryPreservesDataMemOp32() { |
| 255 var builder = genGrowMemoryBuilder(); |
| 256 builder.addMemory(1, 1, false); |
| 257 var module = builder.instantiate(); |
| 258 var offset, val; |
| 259 function peek() { return module.exports.load(offset); } |
| 260 function poke(value) { return module.exports.store(offset, value); } |
| 261 function growMem(pages) { return module.exports.grow_memory(pages); } |
| 262 |
| 263 for(offset = 0; offset <= (kPageSize - 4); offset+=4) { |
| 264 poke(100000 - offset); |
| 265 assertEquals(100000 - offset, peek()); |
| 266 } |
| 267 |
| 268 assertEquals(1, growMem(3)); |
| 269 |
| 270 for(offset = 0; offset <= (kPageSize - 4); offset+=4) { |
| 271 assertEquals(100000 - offset, peek()); |
| 272 } |
| 273 } |
| 274 |
| 275 testGrowMemoryPreservesDataMemOp32(); |
| 276 |
| 277 function testGrowMemoryPreservesDataMemOp16() { |
| 278 var builder = genGrowMemoryBuilder(); |
| 279 builder.addMemory(1, 1, false); |
| 280 var module = builder.instantiate(); |
| 281 var offset, val; |
| 282 function peek() { return module.exports.load16(offset); } |
| 283 function poke(value) { return module.exports.store16(offset, value); } |
| 284 function growMem(pages) { return module.exports.grow_memory(pages); } |
| 285 |
| 286 for(offset = 0; offset <= (kPageSize - 2); offset+=2) { |
| 287 poke(65535 - offset); |
| 288 assertEquals(65535 - offset, peek()); |
| 289 } |
| 290 |
| 291 assertEquals(1, growMem(3)); |
| 292 |
| 293 for(offset = 0; offset <= (kPageSize - 2); offset+=2) { |
| 294 assertEquals(65535 - offset, peek()); |
| 295 } |
| 296 } |
| 297 |
| 298 testGrowMemoryPreservesDataMemOp16(); |
| 299 |
| 300 function testGrowMemoryPreservesDataMemOp8() { |
| 301 var builder = genGrowMemoryBuilder(); |
| 302 builder.addMemory(1, 1, false); |
| 303 var module = builder.instantiate(); |
| 304 var offset, val = 0; |
| 305 function peek() { return module.exports.load8(offset); } |
| 306 function poke(value) { return module.exports.store8(offset, value); } |
| 307 function growMem(pages) { return module.exports.grow_memory(pages); } |
| 308 |
| 309 for(offset = 0; offset <= (kPageSize - 1); offset++, val++) { |
| 310 poke(val); |
| 311 assertEquals(val, peek()); |
| 312 if (val == 255) val = 0; |
| 313 } |
| 314 |
| 315 assertEquals(1, growMem(3)); |
| 316 |
| 317 val = 0; |
| 318 |
| 319 for(offset = 0; offset <= (kPageSize - 1); offset++, val++) { |
| 320 assertEquals(val, peek()); |
| 321 if (val == 255) val = 0; |
| 322 } |
| 323 } |
| 324 |
| 325 testGrowMemoryPreservesDataMemOp8(); |
OLD | NEW |