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 --stress-compaction | |
6 | |
7 load("test/mjsunit/wasm/wasm-constants.js"); | |
8 load("test/mjsunit/wasm/wasm-module-builder.js"); | |
9 | |
10 function genGrowMemoryBuilder() { | |
11 var builder = new WasmModuleBuilder(); | |
12 builder.addFunction("grow_memory", kSig_i_i) | |
13 .addBody([kExprGetLocal, 0, kExprGrowMemory]) | |
14 .exportFunc(); | |
15 builder.addFunction("load", kSig_i_i) | |
16 .addBody([kExprGetLocal, 0, kExprI32LoadMem, 0, 0]) | |
17 .exportFunc(); | |
18 builder.addFunction("store", kSig_i_ii) | |
19 .addBody([kExprGetLocal, 0, kExprGetLocal, 1, kExprI32StoreMem, 0, 0]) | |
20 .exportFunc(); | |
21 return builder; | |
22 } | |
23 | |
24 function testGrowMemoryReadWrite() { | |
25 var builder = genGrowMemoryBuilder(); | |
26 builder.addMemory(1, 1, false); | |
27 var module = builder.instantiate(); | |
28 var offset; | |
29 function peek() { return module.exports.load(offset); } | |
ahaas
2016/06/24 11:10:15
why don't you pass in offset as a parameter?
gdeepti
2016/06/25 00:28:42
Because the assertTraps function in wasm-constants
| |
30 function poke(value) { return module.exports.store(offset, value); } | |
31 function growMem(pages) { return module.exports.grow_memory(pages); } | |
32 | |
33 for(offset = 0; offset < 65533; offset++) { | |
ahaas
2016/06/24 11:10:15
could you use a named constant here instead of 655
gdeepti
2016/06/25 00:28:42
Done.
| |
34 poke(20); | |
35 assertEquals(peek(), 20); | |
36 } | |
37 for (offset = 65534; offset < 66538; offset++) { | |
ahaas
2016/06/24 11:10:15
same here
gdeepti
2016/06/25 00:28:41
Done.
| |
38 assertTraps(kTrapMemOutOfBounds, poke); | |
39 assertTraps(kTrapMemOutOfBounds, peek); | |
40 } | |
41 | |
42 try { | |
43 assertEquals(growMem(3), 1); | |
44 } catch (e) { | |
45 assertEquals("object", typeof e); | |
46 assertEquals(e.message, "failed to allocate memory"); | |
47 return; | |
48 } | |
49 | |
50 for (offset = 65534; offset < 262141; offset++) { | |
ahaas
2016/06/24 11:10:15
same here
gdeepti
2016/06/25 00:28:42
Done.
| |
51 poke(20); | |
52 assertEquals(peek(), 20); | |
53 } | |
54 for (offset = 262142; offset < 262145; offset++) { | |
ahaas
2016/06/24 11:10:15
same here
gdeepti
2016/06/25 00:28:41
Done.
| |
55 assertTraps(kTrapMemOutOfBounds, poke); | |
56 assertTraps(kTrapMemOutOfBounds, peek); | |
57 } | |
58 | |
59 try { | |
60 assertEquals(growMem(15), 4); | |
61 } catch (e) { | |
62 assertEquals("object", typeof e); | |
63 assertEquals(e.message, "failed to allocate memory"); | |
64 return; | |
65 } | |
66 | |
67 for (offset = 262142; offset < 262148; offset++) { | |
68 poke(20); | |
69 assertEquals(peek(), 20); | |
70 } | |
71 for (offset = 1245184; offset < 1245188; offset++) { | |
ahaas
2016/06/24 11:10:15
could you add a test for the upper bound?, e.g. by
gdeepti
2016/06/25 00:28:42
Done.
| |
72 assertTraps(kTrapMemOutOfBounds, poke); | |
73 assertTraps(kTrapMemOutOfBounds, peek); | |
74 } | |
75 } | |
76 | |
77 testGrowMemoryReadWrite(); | |
78 | |
79 function testGrowMemoryZeroInitialSize() { | |
80 var builder = genGrowMemoryBuilder(); | |
81 var module = builder.instantiate(); | |
82 var offset; | |
83 function peek() { return module.exports.load(offset); } | |
84 function poke(value) { return module.exports.store(offset, value); } | |
85 function growMem(pages) { return module.exports.grow_memory(pages); } | |
86 | |
87 assertTraps(kTrapMemOutOfBounds, peek); | |
88 assertTraps(kTrapMemOutOfBounds, poke); | |
89 | |
90 try { | |
91 assertEquals(growMem(1), 0); | |
92 } catch (e) { | |
93 assertEquals("object", typeof e); | |
94 assertEquals(e.message, "failed to allocate memory"); | |
95 return; | |
96 } | |
97 | |
98 for(offset = 0; offset <=65533; offset++) { | |
99 poke(20); | |
100 assertEquals(peek(), 20); | |
101 } | |
102 | |
103 //TODO(gdeepti): Fix tests with correct write boundaries | |
104 //when runtime function is fixed. | |
105 for(offset = 65536; offset <= 65539; offset++) { | |
106 assertTraps(kTrapMemOutOfBounds, peek); | |
107 } | |
108 } | |
109 | |
110 testGrowMemoryZeroInitialSize(); | |
111 | |
112 function testGrowMemoryTrapMaxPagesZeroInitialMemory() { | |
113 var builder = genGrowMemoryBuilder(); | |
114 var module = builder.instantiate(); | |
115 var maxPages = 16385; | |
116 function growMem() { return module.exports.grow_memory(maxPages); } | |
117 try { | |
118 growMem(); | |
119 } catch (e) { | |
120 assertEquals("object", typeof e); | |
121 if (e.message === "failed to allocate memory" || | |
ahaas
2016/06/24 11:10:15
How can both of these messages be possible?
gdeepti
2016/06/25 00:28:42
Left over from when I was trying to catch windows
| |
122 e.message === "memory access out of bounds") { | |
123 return; | |
124 } | |
125 } | |
126 } | |
127 | |
128 testGrowMemoryTrapMaxPagesZeroInitialMemory(); | |
129 | |
130 function testGrowMemoryTrapMaxPages() { | |
131 var builder = genGrowMemoryBuilder(); | |
132 builder.addMemory(1, 1, false); | |
133 var module = builder.instantiate(); | |
134 var maxPages = 16384; | |
135 function growMem() { return module.exports.grow_memory(maxPages); } | |
136 try { | |
137 growMem(); | |
138 } catch (e) { | |
139 assertEquals("object", typeof e); | |
140 if (e.message === "failed to allocate memory" || | |
141 e.message === "memory access out of bounds") { | |
142 return; | |
143 } | |
144 } | |
145 } | |
146 | |
147 testGrowMemoryTrapMaxPages(); | |
148 | |
149 function testGrowMemoryOobThrows() { | |
150 var builder = genGrowMemoryBuilder(); | |
151 builder.addMemory(1, 1, false); | |
152 builder.addFunction("geti", kSig_i_ii) | |
153 .addBody([ | |
154 kExprGetLocal, 0, | |
155 kExprGetLocal, 1, | |
156 kExprI32LoadMem, 0, 0, | |
157 kExprI32StoreMem, 0, 0 | |
158 ]) | |
159 .exportFunc(); | |
160 | |
161 var module = builder.instantiate(); | |
162 var offset; | |
163 | |
164 function read() { return module.exports.geti(0, offset); } | |
165 function write() { return module.exports.geti(offset, 0); } | |
166 | |
167 for (offset = 0; offset < 65533; offset++) { | |
168 assertEquals(0, read()); | |
ahaas
2016/06/24 11:10:15
I don't understand this test. It seems to be eithe
gdeepti
2016/06/25 00:28:42
I added this test as a sanity check for oob with g
| |
169 assertEquals(0, write()); | |
170 } | |
171 | |
172 for (offset = 65534; offset < 66536; offset++) { | |
173 assertTraps(kTrapMemOutOfBounds, read); | |
174 assertTraps(kTrapMemOutOfBounds, write); | |
175 } | |
176 | |
177 function resizeMem() { return module.exports.grow_memory(2); } | |
178 | |
179 try { | |
180 assertEquals(1, resizeMem()); | |
181 } catch (e) { | |
182 assertEquals("object", typeof e); | |
183 assertEquals(e.message, "failed to allocate memory"); | |
184 return; | |
185 } | |
186 | |
187 | |
188 for (offset = 0; offset < 196605; offset++) { | |
189 assertEquals(0, read()); | |
190 assertEquals(0, write()); | |
191 } | |
192 | |
193 for (offset = 196605; offset < 196610; offset++) { | |
194 assertTraps(kTrapMemOutOfBounds, read); | |
195 assertTraps(kTrapMemOutOfBounds, write); | |
196 } | |
197 | |
198 } | |
199 | |
200 testGrowMemoryOobThrows(); | |
OLD | NEW |