blob: e9c1404a4f0bd70933d84962965a159a34104cf2 [file] [log] [blame]
Ben Murdoch014dc512016-03-22 12:00:34 +00001// 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
3// found in the LICENSE file.
4
5// Flags: --expose-wasm --expose-gc --stress-compaction
6
7load("test/mjsunit/wasm/wasm-constants.js");
8
9var kMemSize = 4096;
10
11function genModule(memory) {
12 var kBodySize = 27;
13 var kNameMainOffset = 28 + kBodySize + 1;
14
15 var data = bytes(
16 kDeclMemory,
17 12, 12, 1, // memory
18 // -- signatures
19 kDeclSignatures, 1,
20 1, kAstI32, kAstI32, // int->int
21 // -- main function
22 kDeclFunctions, 1,
23 kDeclFunctionLocals | kDeclFunctionName | kDeclFunctionExport,
24 0, 0,
25 kNameMainOffset, 0, 0, 0, // name offset
26 1, 0, // local int32 count
27 0, 0, // local int64 count
28 0, 0, // local float32 count
29 0, 0, // local float64 count
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
48 return _WASMEXP_.instantiateModule(data, null, memory);
49}
50
51function testPokeMemory() {
52 var module = genModule(null);
53 var buffer = module.memory;
54 assertEquals(kMemSize, buffer.byteLength);
55
56 var array = new Int8Array(buffer);
57 assertEquals(kMemSize, array.length);
58
59 for (var i = 0; i < kMemSize; i++) {
60 assertEquals(0, array[i]);
61 }
62
63 for (var i = 0; i < 10; i++) {
64 assertEquals(0, module.main(kMemSize - 4));
65
66 array[kMemSize/2 + i] = 1;
67 assertEquals(0, module.main(kMemSize/2 - 4));
68 assertEquals(-1, module.main(kMemSize - 4));
69
70 array[kMemSize/2 + i] = 0;
71 assertEquals(0, module.main(kMemSize - 4));
72 }
73}
74
75testPokeMemory();
76
77function testSurvivalAcrossGc() {
78 var checker = genModule(null).main;
79 for (var i = 0; i < 5; i++) {
80 print("gc run ", i);
81 assertEquals(0, checker(kMemSize - 4));
82 gc();
83 }
84}
85
86testSurvivalAcrossGc();
87testSurvivalAcrossGc();
88testSurvivalAcrossGc();
89testSurvivalAcrossGc();
90
91
92function testPokeOuterMemory() {
93 var buffer = new ArrayBuffer(kMemSize);
94 var module = genModule(buffer);
95 assertEquals(kMemSize, buffer.byteLength);
96
97 var array = new Int8Array(buffer);
98 assertEquals(kMemSize, array.length);
99
100 for (var i = 0; i < kMemSize; i++) {
101 assertEquals(0, array[i]);
102 }
103
104 for (var i = 0; i < 10; i++) {
105 assertEquals(0, module.main(kMemSize - 4));
106
107 array[kMemSize/2 + i] = 1;
108 assertEquals(0, module.main(kMemSize/2 - 4));
109 assertEquals(-1, module.main(kMemSize - 4));
110
111 array[kMemSize/2 + i] = 0;
112 assertEquals(0, module.main(kMemSize - 4));
113 }
114}
115
116testPokeOuterMemory();
117
118function testOuterMemorySurvivalAcrossGc() {
119 var buffer = new ArrayBuffer(kMemSize);
120 var checker = genModule(buffer).main;
121 for (var i = 0; i < 5; i++) {
122 print("gc run ", i);
123 assertEquals(0, checker(kMemSize - 4));
124 gc();
125 }
126}
127
128testOuterMemorySurvivalAcrossGc();
129testOuterMemorySurvivalAcrossGc();
130testOuterMemorySurvivalAcrossGc();
131testOuterMemorySurvivalAcrossGc();
132
133
134function testOOBThrows() {
135 var kBodySize = 8;
136 var kNameMainOffset = 29 + kBodySize + 1;
137
138 var data = bytes(
139 kDeclMemory,
140 12, 12, 1, // memory = 4KB
141 // -- signatures
142 kDeclSignatures, 1,
143 2, kAstI32, kAstI32, kAstI32, // int->int
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
161 var memory = null;
162 var module = _WASMEXP_.instantiateModule(data, null, memory);
163
164 var offset;
165
166 function read() { return module.geti(0, offset); }
167 function write() { return module.geti(offset, 0); }
168
169 for (offset = 0; offset < 4092; offset++) {
170 assertEquals(0, read());
171 assertEquals(0, write());
172 }
173
174
175 for (offset = 4093; offset < 4124; offset++) {
176 assertTraps(kTrapMemOutOfBounds, read);
177 assertTraps(kTrapMemOutOfBounds, write);
178 }
179}
180
181testOOBThrows();