Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(734)

Side by Side Diff: test/unittests/wasm/module-decoder-unittest.cc

Issue 2345593003: [wasm] Master CL for Binary 0xC changes. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Fix test failures and TSAN races. Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 #include "test/unittests/test-utils.h" 5 #include "test/unittests/test-utils.h"
6 6
7 #include "src/handles.h" 7 #include "src/handles.h"
8 #include "src/objects-inl.h" 8 #include "src/objects-inl.h"
9 #include "src/wasm/module-decoder.h" 9 #include "src/wasm/module-decoder.h"
10 #include "src/wasm/wasm-macro-gen.h" 10 #include "src/wasm/wasm-macro-gen.h"
11 #include "src/wasm/wasm-opcodes.h" 11 #include "src/wasm/wasm-opcodes.h"
12 12
13 namespace v8 { 13 namespace v8 {
14 namespace internal { 14 namespace internal {
15 namespace wasm { 15 namespace wasm {
16 16
17 #define EMPTY_FUNCTION(sig_index) 0, SIG_INDEX(sig_index), U16_LE(0) 17 #define WASM_INIT_EXPR_I32V_1(val) WASM_I32V_1(val), kExprEnd
18 #define WASM_INIT_EXPR_I32V_2(val) WASM_I32V_2(val), kExprEnd
19 #define WASM_INIT_EXPR_I32V_3(val) WASM_I32V_3(val), kExprEnd
20 #define WASM_INIT_EXPR_I32V_4(val) WASM_I32V_4(val), kExprEnd
21 #define WASM_INIT_EXPR_I32V_5(val) WASM_I32V_5(val), kExprEnd
22 #define WASM_INIT_EXPR_F32(val) WASM_F32(val), kExprEnd
23 #define WASM_INIT_EXPR_I64(val) WASM_I64(val), kExprEnd
24 #define WASM_INIT_EXPR_F64(val) WASM_F64(val), kExprEnd
25 #define WASM_INIT_EXPR_GLOBAL(index) WASM_GET_GLOBAL(index), kExprEnd
26
18 #define SIZEOF_EMPTY_FUNCTION ((size_t)5) 27 #define SIZEOF_EMPTY_FUNCTION ((size_t)5)
19 #define EMPTY_BODY 0 28 #define EMPTY_BODY 0
20 #define SIZEOF_EMPTY_BODY ((size_t)1) 29 #define SIZEOF_EMPTY_BODY ((size_t)1)
21 #define NOP_BODY 2, 0, kExprNop 30 #define NOP_BODY 2, 0, kExprNop
22 #define SIZEOF_NOP_BODY ((size_t)3) 31 #define SIZEOF_NOP_BODY ((size_t)3)
23 32
24 #define SIG_ENTRY_i_i SIG_ENTRY_x_x(kLocalI32, kLocalI32) 33 #define SIG_ENTRY_i_i SIG_ENTRY_x_x(kLocalI32, kLocalI32)
25 34
26 #define UNKNOWN_EMPTY_SECTION_NAME 1, '\0' 35 #define UNKNOWN_SECTION(size) 0, U32V_1(size + 5), 4, 'l', 'u', 'l', 'z'
27 #define UNKNOWN_SECTION_NAME 4, 'l', 'u', 'l', 'z'
28 36
29 #define SECTION(NAME, EXTRA_SIZE) WASM_SECTION_##NAME, U32V_1(EXTRA_SIZE) 37 #define SECTION(name, size) k##name##SectionCode, U32V_1(size)
30 38
31 #define SIGNATURES_SECTION(count, ...) \ 39 #define SIGNATURES_SECTION(count, ...) \
32 SECTION(SIGNATURES, 1 + 3 * (count)), U32V_1(count), __VA_ARGS__ 40 SECTION(Type, 1 + 3 * (count)), U32V_1(count), __VA_ARGS__
33 #define FUNCTION_SIGNATURES_SECTION(count, ...) \ 41 #define FUNCTION_SIGNATURES_SECTION(count, ...) \
34 SECTION(FUNCTION_SIGNATURES, 1 + (count)), U32V_1(count), __VA_ARGS__ 42 SECTION(Function, 1 + (count)), U32V_1(count), __VA_ARGS__
35 43
36 #define FOO_STRING 3, 'f', 'o', 'o' 44 #define FOO_STRING 3, 'f', 'o', 'o'
37 #define NO_LOCAL_NAMES 0 45 #define NO_LOCAL_NAMES 0
38 46
39 #define EMPTY_SIGNATURES_SECTION SECTION(SIGNATURES, 1), 0 47 #define EMPTY_SIGNATURES_SECTION SECTION(Type, 1), 0
40 #define EMPTY_FUNCTION_SIGNATURES_SECTION SECTION(FUNCTION_SIGNATURES, 1), 0 48 #define EMPTY_FUNCTION_SIGNATURES_SECTION SECTION(Function, 1), 0
41 #define EMPTY_FUNCTION_BODIES_SECTION SECTION(FUNCTION_BODIES, 1), 0 49 #define EMPTY_FUNCTION_BODIES_SECTION SECTION(Code, 1), 0
42 #define EMPTY_NAMES_SECTION SECTION(NAMES, 1), 0 50 #define SECTION_NAMES(size) \
51 kUnknownSectionCode, U32V_1(size + 5), 4, 'n', 'a', 'm', 'e'
52 #define EMPTY_NAMES_SECTION SECTION_NAMES(1), 0
43 53
44 #define X1(...) __VA_ARGS__ 54 #define X1(...) __VA_ARGS__
45 #define X2(...) __VA_ARGS__, __VA_ARGS__ 55 #define X2(...) __VA_ARGS__, __VA_ARGS__
46 #define X3(...) __VA_ARGS__, __VA_ARGS__, __VA_ARGS__ 56 #define X3(...) __VA_ARGS__, __VA_ARGS__, __VA_ARGS__
47 #define X4(...) __VA_ARGS__, __VA_ARGS__, __VA_ARGS__, __VA_ARGS__ 57 #define X4(...) __VA_ARGS__, __VA_ARGS__, __VA_ARGS__, __VA_ARGS__
48 58
49 #define ONE_EMPTY_FUNCTION WASM_SECTION_FUNCTION_SIGNATURES, 1 + 1 * 1, 1, X1(0) 59 #define ONE_EMPTY_FUNCTION SECTION(Function, 1 + 1 * 1), 1, X1(0)
50 60
51 #define TWO_EMPTY_FUNCTIONS \ 61 #define TWO_EMPTY_FUNCTIONS SECTION(Function, 1 + 2 * 1), 2, X2(0)
52 WASM_SECTION_FUNCTION_SIGNATURES, 1 + 2 * 1, 2, X2(0)
53 62
54 #define THREE_EMPTY_FUNCTIONS \ 63 #define THREE_EMPTY_FUNCTIONS SECTION(Function, 1 + 3 * 1), 3, X3(0)
55 WASM_SECTION_FUNCTION_SIGNATURES, 1 + 3 * 1, 3, X3(0)
56 64
57 #define FOUR_EMPTY_FUNCTIONS \ 65 #define FOUR_EMPTY_FUNCTIONS SECTION(Function, 1 + 4 * 1), 4, X4(0)
58 WASM_SECTION_FUNCTION_SIGNATURES, 1 + 4 * 1, 4, X4(0)
59 66
60 #define ONE_EMPTY_BODY \ 67 #define ONE_EMPTY_BODY \
61 WASM_SECTION_FUNCTION_BODIES, 1 + 1 * (1 + SIZEOF_EMPTY_BODY), 1, \ 68 SECTION(Code, 1 + 1 * (1 + SIZEOF_EMPTY_BODY)) \
62 X1(SIZEOF_EMPTY_BODY, EMPTY_BODY) 69 , 1, X1(SIZEOF_EMPTY_BODY, EMPTY_BODY)
63 70
64 #define TWO_EMPTY_BODIES \ 71 #define TWO_EMPTY_BODIES \
65 WASM_SECTION_FUNCTION_BODIES, 1 + 2 * (1 + SIZEOF_EMPTY_BODY), 2, \ 72 SECTION(Code, 1 + 2 * (1 + SIZEOF_EMPTY_BODY)) \
66 X2(SIZEOF_EMPTY_BODY, EMPTY_BODY) 73 , 2, X2(SIZEOF_EMPTY_BODY, EMPTY_BODY)
67 74
68 #define THREE_EMPTY_BODIES \ 75 #define THREE_EMPTY_BODIES \
69 WASM_SECTION_FUNCTION_BODIES, 1 + 3 * (1 + SIZEOF_EMPTY_BODY), 3, \ 76 SECTION(Code, 1 + 3 * (1 + SIZEOF_EMPTY_BODY)) \
70 X3(SIZEOF_EMPTY_BODY, EMPTY_BODY) 77 , 3, X3(SIZEOF_EMPTY_BODY, EMPTY_BODY)
71 78
72 #define FOUR_EMPTY_BODIES \ 79 #define FOUR_EMPTY_BODIES \
73 WASM_SECTION_FUNCTION_BODIES, 1 + 4 * (1 + SIZEOF_EMPTY_BODY), 4, \ 80 SECTION(Code, 1 + 4 * (1 + SIZEOF_EMPTY_BODY)) \
74 X4(SIZEOF_EMPTY_BODY, EMPTY_BODY) 81 , 4, X4(SIZEOF_EMPTY_BODY, EMPTY_BODY)
75 82
76 #define SIGNATURES_SECTION_VOID_VOID \ 83 #define SIGNATURES_SECTION_VOID_VOID \
77 SECTION(SIGNATURES, 1 + SIZEOF_SIG_ENTRY_v_v), 1, SIG_ENTRY_v_v 84 SECTION(Type, 1 + SIZEOF_SIG_ENTRY_v_v), 1, SIG_ENTRY_v_v
78 85
79 #define EXPECT_VERIFIES(data) \ 86 #define LINEAR_MEMORY_INDEX_0 0
80 do { \ 87
81 ModuleResult result = DecodeModule(data, data + arraysize(data)); \ 88 #define EXPECT_VERIFIES(data) \
82 EXPECT_TRUE(result.ok()); \ 89 do { \
83 if (result.val) delete result.val; \ 90 ModuleResult result = DecodeModule(data, data + sizeof(data)); \
91 EXPECT_TRUE(result.ok()); \
92 if (result.val) delete result.val; \
84 } while (false) 93 } while (false)
85 94
86 #define EXPECT_FAILURE_LEN(data, length) \ 95 #define EXPECT_FAILURE_LEN(data, length) \
87 do { \ 96 do { \
88 ModuleResult result = DecodeModule(data, data + length); \ 97 ModuleResult result = DecodeModule(data, data + length); \
89 EXPECT_FALSE(result.ok()); \ 98 EXPECT_FALSE(result.ok()); \
90 if (result.val) delete result.val; \ 99 if (result.val) delete result.val; \
91 } while (false) 100 } while (false)
92 101
93 #define EXPECT_FAILURE(data) EXPECT_FAILURE_LEN(data, sizeof(data)) 102 #define EXPECT_FAILURE(data) EXPECT_FAILURE_LEN(data, sizeof(data))
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
142 } 151 }
143 ModuleResult DecodeModuleNoHeader(const byte* module_start, 152 ModuleResult DecodeModuleNoHeader(const byte* module_start,
144 const byte* module_end) { 153 const byte* module_end) {
145 return DecodeWasmModule(isolate(), zone(), module_start, module_end, false, 154 return DecodeWasmModule(isolate(), zone(), module_start, module_end, false,
146 kWasmOrigin); 155 kWasmOrigin);
147 } 156 }
148 }; 157 };
149 158
150 TEST_F(WasmModuleVerifyTest, WrongMagic) { 159 TEST_F(WasmModuleVerifyTest, WrongMagic) {
151 for (uint32_t x = 1; x; x <<= 1) { 160 for (uint32_t x = 1; x; x <<= 1) {
152 const byte data[] = {U32_LE(kWasmMagic ^ x), U32_LE(kWasmVersion), 161 const byte data[] = {U32_LE(kWasmMagic ^ x), U32_LE(kWasmVersion)};
153 SECTION(END, 0)};
154 ModuleResult result = DecodeModuleNoHeader(data, data + sizeof(data)); 162 ModuleResult result = DecodeModuleNoHeader(data, data + sizeof(data));
155 EXPECT_FALSE(result.ok()); 163 EXPECT_FALSE(result.ok());
156 if (result.val) delete result.val; 164 if (result.val) delete result.val;
157 } 165 }
158 } 166 }
159 167
160 TEST_F(WasmModuleVerifyTest, WrongVersion) { 168 TEST_F(WasmModuleVerifyTest, WrongVersion) {
161 for (uint32_t x = 1; x; x <<= 1) { 169 for (uint32_t x = 1; x; x <<= 1) {
162 const byte data[] = {U32_LE(kWasmMagic), U32_LE(kWasmVersion ^ x), 170 const byte data[] = {U32_LE(kWasmMagic), U32_LE(kWasmVersion ^ x)};
163 SECTION(END, 0)};
164 ModuleResult result = DecodeModuleNoHeader(data, data + sizeof(data)); 171 ModuleResult result = DecodeModuleNoHeader(data, data + sizeof(data));
165 EXPECT_FALSE(result.ok()); 172 EXPECT_FALSE(result.ok());
166 if (result.val) delete result.val; 173 if (result.val) delete result.val;
167 } 174 }
168 } 175 }
169 176
170 TEST_F(WasmModuleVerifyTest, DecodeEmpty) { 177 TEST_F(WasmModuleVerifyTest, DecodeEmpty) {
171 static const byte data[] = {SECTION(END, 0)}; 178 ModuleResult result = DecodeModule(nullptr, 0);
172 EXPECT_VERIFIES(data); 179 EXPECT_TRUE(result.ok());
180 if (result.val) delete result.val;
173 } 181 }
174 182
175 TEST_F(WasmModuleVerifyTest, OneGlobal) { 183 TEST_F(WasmModuleVerifyTest, OneGlobal) {
176 static const byte data[] = { 184 static const byte data[] = {
177 SECTION(GLOBALS, 5), // -- 185 SECTION(Global, 6), // --
178 1, 186 1,
179 NAME_LENGTH(1), 187 kLocalI32, // local type
180 'g', // name 188 0, // immutable
181 kLocalI32, // local type 189 WASM_INIT_EXPR_I32V_1(13) // init
182 0, // exported
183 }; 190 };
184 191
185 { 192 {
186 // Should decode to exactly one global. 193 // Should decode to exactly one global.
187 ModuleResult result = DecodeModule(data, data + arraysize(data)); 194 ModuleResult result = DecodeModule(data, data + sizeof(data));
188 EXPECT_OK(result); 195 EXPECT_OK(result);
189 EXPECT_EQ(1, result.val->globals.size()); 196 EXPECT_EQ(1, result.val->globals.size());
190 EXPECT_EQ(0, result.val->functions.size()); 197 EXPECT_EQ(0, result.val->functions.size());
191 EXPECT_EQ(0, result.val->data_segments.size()); 198 EXPECT_EQ(0, result.val->data_segments.size());
192 199
193 const WasmGlobal* global = &result.val->globals.back(); 200 const WasmGlobal* global = &result.val->globals.back();
194 201
195 EXPECT_EQ(1, global->name_length);
196 EXPECT_EQ(kAstI32, global->type); 202 EXPECT_EQ(kAstI32, global->type);
197 EXPECT_EQ(0, global->offset); 203 EXPECT_EQ(0, global->offset);
198 EXPECT_FALSE(global->exported); 204 EXPECT_EQ(false, global->mutability);
205 EXPECT_EQ(WasmInitExpr::kI32Const, global->init.kind);
206 EXPECT_EQ(13, global->init.val.i32_const);
199 207
200 if (result.val) delete result.val; 208 if (result.val) delete result.val;
201 } 209 }
202 210
203 EXPECT_OFF_END_FAILURE(data, 1, sizeof(data)); 211 EXPECT_OFF_END_FAILURE(data, 1, sizeof(data));
204 } 212 }
205 213
206 TEST_F(WasmModuleVerifyTest, Global_invalid_type) { 214 TEST_F(WasmModuleVerifyTest, Global_invalid_type) {
207 static const byte data[] = { 215 static const byte data[] = {
208 SECTION(GLOBALS, 5), // -- 216 SECTION(Global, 6), // --
209 1, 217 1,
210 NAME_LENGTH(1), 218 64, // invalid memory type
211 'g', // name 219 1, // mutable
212 64, // invalid memory type 220 WASM_INIT_EXPR_I32V_1(33), // init
213 0, // exported
214 }; 221 };
215 222
216 ModuleResult result = DecodeModule(data, data + sizeof(data)); 223 ModuleResult result = DecodeModule(data, data + sizeof(data));
217 EXPECT_FALSE(result.ok()); 224 EXPECT_FALSE(result.ok());
218 if (result.val) delete result.val; 225 if (result.val) delete result.val;
219 } 226 }
220 227
221 TEST_F(WasmModuleVerifyTest, Global_invalid_type2) { 228 TEST_F(WasmModuleVerifyTest, Global_invalid_type2) {
222 static const byte data[] = { 229 static const byte data[] = {
223 SECTION(GLOBALS, 5), // -- 230 SECTION(Global, 6), // --
224 1, 231 1,
225 NAME_LENGTH(1), 232 kLocalVoid, // invalid memory type
226 'g', // name 233 1, // mutable
227 kLocalVoid, // invalid memory type 234 WASM_INIT_EXPR_I32V_1(33), // init
228 0, // exported
229 }; 235 };
230 236
231 ModuleResult result = DecodeModule(data, data + sizeof(data)); 237 ModuleResult result = DecodeModule(data, data + sizeof(data));
232 EXPECT_FALSE(result.ok()); 238 EXPECT_FALSE(result.ok());
233 if (result.val) delete result.val; 239 if (result.val) delete result.val;
234 } 240 }
235 241
236 TEST_F(WasmModuleVerifyTest, ZeroGlobals) { 242 TEST_F(WasmModuleVerifyTest, ZeroGlobals) {
237 static const byte data[] = { 243 static const byte data[] = {
238 SECTION(GLOBALS, 1), // -- 244 SECTION(Global, 1), // --
239 0, // declare 0 globals 245 0, // declare 0 globals
240 }; 246 };
241 ModuleResult result = DecodeModule(data, data + arraysize(data)); 247 ModuleResult result = DecodeModule(data, data + sizeof(data));
242 EXPECT_OK(result); 248 EXPECT_OK(result);
243 if (result.val) delete result.val; 249 if (result.val) delete result.val;
244 } 250 }
245 251
246 static void AppendUint32v(std::vector<byte>& buffer, uint32_t val) { 252 static void AppendUint32v(std::vector<byte>& buffer, uint32_t val) {
247 while (true) { 253 while (true) {
248 uint32_t next = val >> 7; 254 uint32_t next = val >> 7;
249 uint32_t out = val & 0x7f; 255 uint32_t out = val & 0x7f;
250 if (next) { 256 if (next) {
251 buffer.push_back(static_cast<byte>(0x80 | out)); 257 buffer.push_back(static_cast<byte>(0x80 | out));
252 val = next; 258 val = next;
253 } else { 259 } else {
254 buffer.push_back(static_cast<byte>(out)); 260 buffer.push_back(static_cast<byte>(out));
255 break; 261 break;
256 } 262 }
257 } 263 }
258 } 264 }
259 265
260 TEST_F(WasmModuleVerifyTest, NGlobals) { 266 TEST_F(WasmModuleVerifyTest, NGlobals) {
261 static const byte data[] = { 267 static const byte data[] = {
262 NO_NAME, // name length 268 kLocalF32, // memory type
263 kLocalF32, // memory type 269 0, // immutable
264 0, // exported 270 WASM_INIT_EXPR_F32(7.7), // init
265 }; 271 };
266 272
267 for (uint32_t i = 0; i < 1000000; i = i * 13 + 1) { 273 for (uint32_t i = 0; i < 1000000; i = i * 13 + 1) {
268 std::vector<byte> buffer; 274 std::vector<byte> buffer;
269 size_t size = SizeOfVarInt(i) + i * sizeof(data); 275 size_t size = SizeOfVarInt(i) + i * sizeof(data);
270 const byte globals[] = {WASM_SECTION_GLOBALS, U32V_5(size)}; 276 const byte globals[] = {kGlobalSectionCode, U32V_5(size)};
271 for (size_t g = 0; g != sizeof(globals); ++g) { 277 for (size_t g = 0; g != sizeof(globals); ++g) {
272 buffer.push_back(globals[g]); 278 buffer.push_back(globals[g]);
273 } 279 }
274 AppendUint32v(buffer, i); // Number of globals. 280 AppendUint32v(buffer, i); // Number of globals.
275 for (uint32_t j = 0; j < i; j++) { 281 for (uint32_t j = 0; j < i; j++) {
276 buffer.insert(buffer.end(), data, data + sizeof(data)); 282 buffer.insert(buffer.end(), data, data + sizeof(data));
277 } 283 }
278 284
279 ModuleResult result = DecodeModule(&buffer[0], &buffer[0] + buffer.size()); 285 ModuleResult result = DecodeModule(&buffer[0], &buffer[0] + buffer.size());
280 EXPECT_OK(result); 286 EXPECT_OK(result);
281 if (result.val) delete result.val; 287 if (result.val) delete result.val;
282 } 288 }
283 } 289 }
284 290
285 TEST_F(WasmModuleVerifyTest, GlobalWithInvalidNameOffset) {
286 static const byte data[] = {
287 SECTION(GLOBALS, 7),
288 1, // declare one global
289 NO_NAME, // name offset
290 33, // memory type
291 0, // exported
292 };
293
294 EXPECT_FAILURE(data);
295 }
296
297 TEST_F(WasmModuleVerifyTest, GlobalWithInvalidNameLength) {
298 static const byte data[] = {
299 SECTION(GLOBALS, 5), // --
300 1,
301 NAME_LENGTH(56), // invalid length
302 'g', // name
303 kLocalI32, // memory type
304 0, // exported
305 };
306 EXPECT_FAILURE(data);
307 }
308
309 TEST_F(WasmModuleVerifyTest, GlobalWithInvalidMemoryType) { 291 TEST_F(WasmModuleVerifyTest, GlobalWithInvalidMemoryType) {
310 static const byte data[] = { 292 static const byte data[] = {SECTION(Global, 7),
311 SECTION(GLOBALS, 7), 293 33, // memory type
312 1, // declare one global 294 0, // exported
313 NO_NAME, // name offset 295 WASM_INIT_EXPR_I32V_1(1)};
314 33, // memory type
315 0, // exported
316 };
317 296
318 EXPECT_FAILURE(data); 297 EXPECT_FAILURE(data);
319 } 298 }
320 299
321 TEST_F(WasmModuleVerifyTest, TwoGlobals) { 300 TEST_F(WasmModuleVerifyTest, TwoGlobals) {
322 static const byte data[] = { 301 static const byte data[] = {
323 SECTION(GLOBALS, 7), 302 SECTION(Global, 21),
324 2, 303 2,
325 NO_NAME, // #0: name length
326 kLocalF32, // type 304 kLocalF32, // type
327 0, // exported 305 0, // immutable
328 NO_NAME, // #1: name length 306 WASM_INIT_EXPR_F32(22.0),
329 kLocalF64, // type 307 kLocalF64, // type
330 1, // exported 308 1, // mutable
309 WASM_INIT_EXPR_F64(23.0),
331 }; 310 };
332 311
333 { 312 {
334 // Should decode to exactly two globals. 313 // Should decode to exactly two globals.
335 ModuleResult result = DecodeModule(data, data + arraysize(data)); 314 ModuleResult result = DecodeModule(data, data + sizeof(data));
336 EXPECT_OK(result); 315 EXPECT_OK(result);
337 EXPECT_EQ(2, result.val->globals.size()); 316 EXPECT_EQ(2, result.val->globals.size());
338 EXPECT_EQ(0, result.val->functions.size()); 317 EXPECT_EQ(0, result.val->functions.size());
339 EXPECT_EQ(0, result.val->data_segments.size()); 318 EXPECT_EQ(0, result.val->data_segments.size());
340 319
341 const WasmGlobal* g0 = &result.val->globals[0]; 320 const WasmGlobal* g0 = &result.val->globals[0];
321
322 EXPECT_EQ(kAstF32, g0->type);
323 EXPECT_EQ(0, g0->offset);
324 EXPECT_EQ(false, g0->mutability);
325 EXPECT_EQ(WasmInitExpr::kF32Const, g0->init.kind);
326
342 const WasmGlobal* g1 = &result.val->globals[1]; 327 const WasmGlobal* g1 = &result.val->globals[1];
343 328
344 EXPECT_EQ(0, g0->name_length);
345 EXPECT_EQ(kAstF32, g0->type);
346 EXPECT_EQ(0, g0->offset);
347 EXPECT_FALSE(g0->exported);
348
349 EXPECT_EQ(0, g1->name_length);
350 EXPECT_EQ(kAstF64, g1->type); 329 EXPECT_EQ(kAstF64, g1->type);
351 EXPECT_EQ(8, g1->offset); 330 EXPECT_EQ(8, g1->offset);
352 EXPECT_TRUE(g1->exported); 331 EXPECT_EQ(true, g1->mutability);
332 EXPECT_EQ(WasmInitExpr::kF64Const, g1->init.kind);
353 333
354 if (result.val) delete result.val; 334 if (result.val) delete result.val;
355 } 335 }
356 336
357 EXPECT_OFF_END_FAILURE(data, 1, sizeof(data)); 337 EXPECT_OFF_END_FAILURE(data, 1, sizeof(data));
358 } 338 }
359 339
360 TEST_F(WasmModuleVerifyTest, OneSignature) { 340 TEST_F(WasmModuleVerifyTest, OneSignature) {
361 { 341 {
362 static const byte data[] = {SIGNATURES_SECTION_VOID_VOID}; 342 static const byte data[] = {SIGNATURES_SECTION_VOID_VOID};
363 EXPECT_VERIFIES(data); 343 EXPECT_VERIFIES(data);
364 } 344 }
365 345
366 { 346 {
367 static const byte data[] = {SECTION(SIGNATURES, 1 + SIZEOF_SIG_ENTRY_x_x), 347 static const byte data[] = {SECTION(Type, 1 + SIZEOF_SIG_ENTRY_x_x), 1,
368 1, SIG_ENTRY_i_i}; 348 SIG_ENTRY_i_i};
369 EXPECT_VERIFIES(data); 349 EXPECT_VERIFIES(data);
370 } 350 }
371 } 351 }
372 352
373 TEST_F(WasmModuleVerifyTest, MultipleSignatures) { 353 TEST_F(WasmModuleVerifyTest, MultipleSignatures) {
374 static const byte data[] = { 354 static const byte data[] = {
375 SECTION(SIGNATURES, 1 + SIZEOF_SIG_ENTRY_v_v + SIZEOF_SIG_ENTRY_x_x + 355 SECTION(Type, 1 + SIZEOF_SIG_ENTRY_v_v + SIZEOF_SIG_ENTRY_x_x +
376 SIZEOF_SIG_ENTRY_x_xx), // -- 356 SIZEOF_SIG_ENTRY_x_xx), // --
377 3, // -- 357 3, // --
378 SIG_ENTRY_v_v, // void -> void 358 SIG_ENTRY_v_v, // void -> void
379 SIG_ENTRY_x_x(kLocalI32, kLocalF32), // f32 -> i32 359 SIG_ENTRY_x_x(kLocalI32, kLocalF32), // f32 -> i32
380 SIG_ENTRY_x_xx(kLocalI32, kLocalF64, kLocalF64), // f64,f64 -> i32 360 SIG_ENTRY_x_xx(kLocalI32, kLocalF64, kLocalF64), // f64,f64 -> i32
381 }; 361 };
382 362
383 ModuleResult result = DecodeModule(data, data + arraysize(data)); 363 ModuleResult result = DecodeModule(data, data + sizeof(data));
384 EXPECT_OK(result); 364 EXPECT_OK(result);
385 EXPECT_EQ(3, result.val->signatures.size()); 365 EXPECT_EQ(3, result.val->signatures.size());
386 if (result.val->signatures.size() == 3) { 366 if (result.val->signatures.size() == 3) {
387 EXPECT_EQ(0, result.val->signatures[0]->return_count()); 367 EXPECT_EQ(0, result.val->signatures[0]->return_count());
388 EXPECT_EQ(1, result.val->signatures[1]->return_count()); 368 EXPECT_EQ(1, result.val->signatures[1]->return_count());
389 EXPECT_EQ(1, result.val->signatures[2]->return_count()); 369 EXPECT_EQ(1, result.val->signatures[2]->return_count());
390 370
391 EXPECT_EQ(0, result.val->signatures[0]->parameter_count()); 371 EXPECT_EQ(0, result.val->signatures[0]->parameter_count());
392 EXPECT_EQ(1, result.val->signatures[1]->parameter_count()); 372 EXPECT_EQ(1, result.val->signatures[1]->parameter_count());
393 EXPECT_EQ(2, result.val->signatures[2]->parameter_count()); 373 EXPECT_EQ(2, result.val->signatures[2]->parameter_count());
394 } 374 }
395 if (result.val) delete result.val; 375 if (result.val) delete result.val;
396 376
397 EXPECT_OFF_END_FAILURE(data, 1, sizeof(data)); 377 EXPECT_OFF_END_FAILURE(data, 1, sizeof(data));
398 } 378 }
399 379
400 TEST_F(WasmModuleVerifyTest, OneDataSegment) { 380 TEST_F(WasmModuleVerifyTest, OneDataSegment) {
401 const byte kDataSegmentSourceOffset = 30; 381 const byte kDataSegmentSourceOffset = 24;
402 const byte data[] = { 382 const byte data[] = {
403 SECTION(MEMORY, 3), 383 SECTION(Memory, 4),
384 ENTRY_COUNT(1),
385 kResizableMaximumFlag,
404 28, 386 28,
405 28, 387 28,
406 1, 388 SECTION(Data, 11),
407 SECTION(DATA_SEGMENTS, 8), 389 ENTRY_COUNT(1),
408 1, 390 LINEAR_MEMORY_INDEX_0,
409 U32V_3(0x9bbaa), // dest addr 391 WASM_INIT_EXPR_I32V_3(0x9bbaa), // dest addr
410 U32V_1(3), // source size 392 U32V_1(3), // source size
411 'a', 393 'a',
412 'b', 394 'b',
413 'c' // data bytes 395 'c' // data bytes
414 }; 396 };
415 397
416 { 398 {
417 EXPECT_VERIFIES(data); 399 EXPECT_VERIFIES(data);
418 ModuleResult result = DecodeModule(data, data + arraysize(data)); 400 ModuleResult result = DecodeModule(data, data + sizeof(data));
419 EXPECT_OK(result); 401 EXPECT_OK(result);
420 EXPECT_EQ(0, result.val->globals.size()); 402 EXPECT_EQ(0, result.val->globals.size());
421 EXPECT_EQ(0, result.val->functions.size()); 403 EXPECT_EQ(0, result.val->functions.size());
422 EXPECT_EQ(1, result.val->data_segments.size()); 404 EXPECT_EQ(1, result.val->data_segments.size());
423 405
424 const WasmDataSegment* segment = &result.val->data_segments.back(); 406 const WasmDataSegment* segment = &result.val->data_segments.back();
425 407
426 EXPECT_EQ(0x9bbaa, segment->dest_addr); 408 EXPECT_EQ(WasmInitExpr::kI32Const, segment->dest_addr.kind);
409 EXPECT_EQ(0x9bbaa, segment->dest_addr.val.i32_const);
427 EXPECT_EQ(kDataSegmentSourceOffset, segment->source_offset); 410 EXPECT_EQ(kDataSegmentSourceOffset, segment->source_offset);
428 EXPECT_EQ(3, segment->source_size); 411 EXPECT_EQ(3, segment->source_size);
429 EXPECT_TRUE(segment->init);
430 412
431 if (result.val) delete result.val; 413 if (result.val) delete result.val;
432 } 414 }
433 415
434 EXPECT_OFF_END_FAILURE(data, 13, sizeof(data)); 416 EXPECT_OFF_END_FAILURE(data, 14, sizeof(data));
435 } 417 }
436 418
437 TEST_F(WasmModuleVerifyTest, TwoDataSegments) { 419 TEST_F(WasmModuleVerifyTest, TwoDataSegments) {
438 const byte kDataSegment0SourceOffset = 30; 420 const byte kDataSegment0SourceOffset = 24;
439 const byte kDataSegment1SourceOffset = 30 + 8; 421 const byte kDataSegment1SourceOffset = kDataSegment0SourceOffset + 11;
440 422
441 const byte data[] = { 423 const byte data[] = {
442 SECTION(MEMORY, 3), 424 SECTION(Memory, 4),
425 ENTRY_COUNT(1),
426 kResizableMaximumFlag,
443 28, 427 28,
444 28, 428 28,
445 1, 429 SECTION(Data, 29),
446 SECTION(DATA_SEGMENTS, 23), 430 ENTRY_COUNT(2), // segment count
447 2, // segment count 431 LINEAR_MEMORY_INDEX_0,
448 U32V_3(0x7ffee), // #0: dest addr 432 WASM_INIT_EXPR_I32V_3(0x7ffee), // #0: dest addr
449 U32V_1(4), // source size 433 U32V_1(4), // source size
450 1, 434 1,
451 2, 435 2,
452 3, 436 3,
453 4, // data bytes 437 4, // data bytes
454 U32V_3(0x6ddcc), // #1: dest addr 438 LINEAR_MEMORY_INDEX_0,
455 U32V_1(10), // source size 439 WASM_INIT_EXPR_I32V_3(0x6ddcc), // #1: dest addr
440 U32V_1(10), // source size
456 1, 441 1,
457 2, 442 2,
458 3, 443 3,
459 4, 444 4,
460 5, 445 5,
461 6, 446 6,
462 7, 447 7,
463 8, 448 8,
464 9, 449 9,
465 10 // data bytes 450 10 // data bytes
466 }; 451 };
467 452
468 { 453 {
469 ModuleResult result = DecodeModule(data, data + arraysize(data)); 454 ModuleResult result = DecodeModule(data, data + sizeof(data));
470 EXPECT_OK(result); 455 EXPECT_OK(result);
471 EXPECT_EQ(0, result.val->globals.size()); 456 EXPECT_EQ(0, result.val->globals.size());
472 EXPECT_EQ(0, result.val->functions.size()); 457 EXPECT_EQ(0, result.val->functions.size());
473 EXPECT_EQ(2, result.val->data_segments.size()); 458 EXPECT_EQ(2, result.val->data_segments.size());
474 459
475 const WasmDataSegment* s0 = &result.val->data_segments[0]; 460 const WasmDataSegment* s0 = &result.val->data_segments[0];
476 const WasmDataSegment* s1 = &result.val->data_segments[1]; 461 const WasmDataSegment* s1 = &result.val->data_segments[1];
477 462
478 EXPECT_EQ(0x7ffee, s0->dest_addr); 463 EXPECT_EQ(WasmInitExpr::kI32Const, s0->dest_addr.kind);
464 EXPECT_EQ(0x7ffee, s0->dest_addr.val.i32_const);
479 EXPECT_EQ(kDataSegment0SourceOffset, s0->source_offset); 465 EXPECT_EQ(kDataSegment0SourceOffset, s0->source_offset);
480 EXPECT_EQ(4, s0->source_size); 466 EXPECT_EQ(4, s0->source_size);
481 EXPECT_TRUE(s0->init);
482 467
483 EXPECT_EQ(0x6ddcc, s1->dest_addr); 468 EXPECT_EQ(WasmInitExpr::kI32Const, s1->dest_addr.kind);
469 EXPECT_EQ(0x6ddcc, s1->dest_addr.val.i32_const);
484 EXPECT_EQ(kDataSegment1SourceOffset, s1->source_offset); 470 EXPECT_EQ(kDataSegment1SourceOffset, s1->source_offset);
485 EXPECT_EQ(10, s1->source_size); 471 EXPECT_EQ(10, s1->source_size);
486 EXPECT_TRUE(s1->init);
487 472
488 if (result.val) delete result.val; 473 if (result.val) delete result.val;
489 } 474 }
490 475
491 EXPECT_OFF_END_FAILURE(data, 13, sizeof(data)); 476 EXPECT_OFF_END_FAILURE(data, 14, sizeof(data));
492 } 477 }
493 478
494 TEST_F(WasmModuleVerifyTest, DataSegmentWithInvalidDest) { 479 TEST_F(WasmModuleVerifyTest, DataSegment_wrong_init_type) {
495 const int source_size = 3; 480 const byte data[] = {
481 SECTION(Memory, 4),
482 ENTRY_COUNT(1),
483 kResizableMaximumFlag,
484 28,
485 28,
486 SECTION(Data, 11),
487 ENTRY_COUNT(1),
488 LINEAR_MEMORY_INDEX_0,
489 WASM_INIT_EXPR_F64(9.9), // dest addr
490 U32V_1(3), // source size
491 'a',
492 'b',
493 'c' // data bytes
494 };
496 495
497 for (byte mem_pages = 1; mem_pages < 16; mem_pages++) { 496 EXPECT_FAILURE(data);
498 int mem_size = mem_pages * 0x10000; // 64k pages.
499
500 for (int dest_addr = mem_size - source_size;
501 dest_addr < mem_size + source_size; dest_addr++) {
502 byte data[] = {SECTION(MEMORY, 3),
503 mem_pages,
504 mem_pages,
505 1,
506 SECTION(DATA_SEGMENTS, 8),
507 1,
508 U32V_3(dest_addr),
509 U32V_1(source_size),
510 'a',
511 'b',
512 'c'};
513
514 if (dest_addr <= (mem_size - source_size)) {
515 EXPECT_VERIFIES(data);
516 } else {
517 EXPECT_FAILURE(data);
518 }
519 }
520 }
521 } 497 }
522 498
523 TEST_F(WasmModuleVerifyTest, OneIndirectFunction) { 499 TEST_F(WasmModuleVerifyTest, OneIndirectFunction) {
524 static const byte data[] = { 500 static const byte data[] = {
525 // sig#0 ------------------------------------------------------- 501 // sig#0 ---------------------------------------------------------------
526 SIGNATURES_SECTION_VOID_VOID, 502 SIGNATURES_SECTION_VOID_VOID,
527 // funcs ------------------------------------------------------ 503 // funcs ---------------------------------------------------------------
528 ONE_EMPTY_FUNCTION, 504 ONE_EMPTY_FUNCTION,
529 // indirect table ---------------------------------------------- 505 // table declaration ---------------------------------------------------
530 SECTION(FUNCTION_TABLE, 2), 1, U32V_1(0)}; 506 SECTION(Table, 4), ENTRY_COUNT(1), kWasmAnyFunctionTypeForm, 0, 1};
531 507
532 ModuleResult result = DecodeModule(data, data + arraysize(data)); 508 ModuleResult result = DecodeModule(data, data + sizeof(data));
533 EXPECT_OK(result); 509 EXPECT_OK(result);
534 if (result.ok()) { 510 if (result.ok()) {
535 EXPECT_EQ(1, result.val->signatures.size()); 511 EXPECT_EQ(1, result.val->signatures.size());
512 EXPECT_EQ(1, result.val->functions.size());
513 EXPECT_EQ(1, result.val->function_tables.size());
514 EXPECT_EQ(1, result.val->function_tables[0].values.size());
515 EXPECT_EQ(-1, result.val->function_tables[0].values[0]);
516 }
517 if (result.val) delete result.val;
518 }
519
520 TEST_F(WasmModuleVerifyTest, OneIndirectFunction_one_entry) {
521 static const byte data[] = {
522 // sig#0 ---------------------------------------------------------------
523 SIGNATURES_SECTION_VOID_VOID,
524 // funcs ---------------------------------------------------------------
525 ONE_EMPTY_FUNCTION,
526 // table declaration ---------------------------------------------------
527 SECTION(Table, 4), ENTRY_COUNT(1), kWasmAnyFunctionTypeForm, 0, 1,
528 // elements ------------------------------------------------------------
529 SECTION(Element, 7),
530 1, // entry count
531 TABLE_INDEX(0), WASM_INIT_EXPR_I32V_1(0),
532 1, // elements count
533 FUNC_INDEX(0)};
534
535 ModuleResult result = DecodeModule(data, data + sizeof(data));
536 EXPECT_OK(result);
537 if (result.ok()) {
538 EXPECT_EQ(1, result.val->signatures.size());
536 EXPECT_EQ(1, result.val->functions.size()); 539 EXPECT_EQ(1, result.val->functions.size());
537 EXPECT_EQ(1, result.val->function_tables.size()); 540 EXPECT_EQ(1, result.val->function_tables.size());
538 EXPECT_EQ(1, result.val->function_tables[0].values.size()); 541 EXPECT_EQ(1, result.val->function_tables[0].values.size());
539 EXPECT_EQ(0, result.val->function_tables[0].values[0]); 542 EXPECT_EQ(0, result.val->function_tables[0].values[0]);
540 } 543 }
541 if (result.val) delete result.val; 544 if (result.val) delete result.val;
542 } 545 }
543 546
544 TEST_F(WasmModuleVerifyTest, MultipleIndirectFunctions) { 547 TEST_F(WasmModuleVerifyTest, MultipleIndirectFunctions) {
545 static const byte data[] = { 548 static const byte data[] = {
546 // sig#0 ------------------------------------------------------- 549 // sig#0 -------------------------------------------------------
547 SECTION(SIGNATURES, 1 + SIZEOF_SIG_ENTRY_v_v + SIZEOF_SIG_ENTRY_v_x), 550 SECTION(Type, 1 + SIZEOF_SIG_ENTRY_v_v + SIZEOF_SIG_ENTRY_v_x),
548 2, // -- 551 2, // --
549 SIG_ENTRY_v_v, // void -> void 552 SIG_ENTRY_v_v, // void -> void
550 SIG_ENTRY_v_x(kLocalI32), // void -> i32 553 SIG_ENTRY_v_x(kLocalI32), // void -> i32
551 // funcs ------------------------------------------------------ 554 // funcs ------------------------------------------------------
552 FOUR_EMPTY_FUNCTIONS, 555 FOUR_EMPTY_FUNCTIONS,
553 // indirect table ---------------------------------------------- 556 // table declaration -------------------------------------------
554 SECTION(FUNCTION_TABLE, 9), 8, 557 SECTION(Table, 4), ENTRY_COUNT(1), kWasmAnyFunctionTypeForm, 0, 8,
555 U32V_1(0), // -- 558 // table elements ----------------------------------------------
556 U32V_1(1), // -- 559 SECTION(Element, 14),
557 U32V_1(2), // -- 560 1, // entry count
558 U32V_1(3), // -- 561 TABLE_INDEX(0), WASM_INIT_EXPR_I32V_1(0),
559 U32V_1(0), // -- 562 8, // elements count
560 U32V_1(1), // -- 563 FUNC_INDEX(0), // --
561 U32V_1(2), // -- 564 FUNC_INDEX(1), // --
562 U32V_1(3), // -- 565 FUNC_INDEX(2), // --
566 FUNC_INDEX(3), // --
567 FUNC_INDEX(0), // --
568 FUNC_INDEX(1), // --
569 FUNC_INDEX(2), // --
570 FUNC_INDEX(3), // --
563 FOUR_EMPTY_BODIES}; 571 FOUR_EMPTY_BODIES};
564 572
565 ModuleResult result = DecodeModule(data, data + arraysize(data)); 573 ModuleResult result = DecodeModule(data, data + sizeof(data));
566 EXPECT_OK(result); 574 EXPECT_OK(result);
567 if (result.ok()) { 575 if (result.ok()) {
568 EXPECT_EQ(2, result.val->signatures.size()); 576 EXPECT_EQ(2, result.val->signatures.size());
569 EXPECT_EQ(4, result.val->functions.size()); 577 EXPECT_EQ(4, result.val->functions.size());
570 EXPECT_EQ(1, result.val->function_tables.size()); 578 EXPECT_EQ(1, result.val->function_tables.size());
571 EXPECT_EQ(8, result.val->function_tables[0].values.size()); 579 EXPECT_EQ(8, result.val->function_tables[0].values.size());
572 for (int i = 0; i < 8; i++) { 580 for (int i = 0; i < 8; i++) {
573 EXPECT_EQ(i & 3, result.val->function_tables[0].values[i]); 581 EXPECT_EQ(i & 3, result.val->function_tables[0].values[i]);
574 } 582 }
575 } 583 }
576 if (result.val) delete result.val; 584 if (result.val) delete result.val;
577 } 585 }
578 586
579 TEST_F(WasmModuleVerifyTest, IndirectFunctionNoFunctions) { 587 TEST_F(WasmModuleVerifyTest, IndirectFunctionNoFunctions) {
580 static const byte data[] = { 588 static const byte data[] = {
581 // sig#0 ------------------------------------------------------- 589 // sig#0 -------------------------------------------------------
582 SIGNATURES_SECTION_VOID_VOID, 590 SIGNATURES_SECTION_VOID_VOID,
583 // indirect table ---------------------------------------------- 591 // indirect table ----------------------------------------------
584 SECTION(FUNCTION_TABLE, 3), 1, 0, 0, 592 SECTION(Table, 4), ENTRY_COUNT(1), 1, 0, 0,
585 }; 593 };
586 594
587 EXPECT_FAILURE(data); 595 EXPECT_FAILURE(data);
588 } 596 }
589 597
590 TEST_F(WasmModuleVerifyTest, IndirectFunctionInvalidIndex) { 598 TEST_F(WasmModuleVerifyTest, IndirectFunctionInvalidIndex) {
591 static const byte data[] = { 599 static const byte data[] = {
592 // sig#0 ------------------------------------------------------- 600 // sig#0 -------------------------------------------------------
593 SIGNATURES_SECTION_VOID_VOID, 601 SIGNATURES_SECTION_VOID_VOID,
594 // functions --------------------------------------------------- 602 // functions ---------------------------------------------------
595 ONE_EMPTY_FUNCTION, 603 ONE_EMPTY_FUNCTION,
596 // indirect table ---------------------------------------------- 604 // indirect table ----------------------------------------------
597 SECTION(FUNCTION_TABLE, 3), 1, 1, 0, 605 SECTION(Table, 4), ENTRY_COUNT(1), 1, 1, 0,
598 }; 606 };
599 607
600 EXPECT_FAILURE(data); 608 EXPECT_FAILURE(data);
601 } 609 }
602 610
603 class WasmSignatureDecodeTest : public TestWithZone {}; 611 class WasmSignatureDecodeTest : public TestWithZone {};
604 612
605 TEST_F(WasmSignatureDecodeTest, Ok_v_v) { 613 TEST_F(WasmSignatureDecodeTest, Ok_v_v) {
606 static const byte data[] = {SIG_ENTRY_v_v}; 614 static const byte data[] = {SIG_ENTRY_v_v};
607 v8::internal::AccountingAllocator allocator; 615 v8::internal::AccountingAllocator allocator;
608 Zone zone(&allocator); 616 Zone zone(&allocator);
609 FunctionSig* sig = 617 FunctionSig* sig =
610 DecodeWasmSignatureForTesting(&zone, data, data + arraysize(data)); 618 DecodeWasmSignatureForTesting(&zone, data, data + sizeof(data));
611 619
612 EXPECT_TRUE(sig != nullptr); 620 EXPECT_TRUE(sig != nullptr);
613 EXPECT_EQ(0, sig->parameter_count()); 621 EXPECT_EQ(0, sig->parameter_count());
614 EXPECT_EQ(0, sig->return_count()); 622 EXPECT_EQ(0, sig->return_count());
615 } 623 }
616 624
617 TEST_F(WasmSignatureDecodeTest, Ok_t_v) { 625 TEST_F(WasmSignatureDecodeTest, Ok_t_v) {
618 for (size_t i = 0; i < arraysize(kLocalTypes); i++) { 626 for (size_t i = 0; i < arraysize(kLocalTypes); i++) {
619 LocalTypePair ret_type = kLocalTypes[i]; 627 LocalTypePair ret_type = kLocalTypes[i];
620 const byte data[] = {SIG_ENTRY_x(ret_type.code)}; 628 const byte data[] = {SIG_ENTRY_x(ret_type.code)};
621 FunctionSig* sig = 629 FunctionSig* sig =
622 DecodeWasmSignatureForTesting(zone(), data, data + arraysize(data)); 630 DecodeWasmSignatureForTesting(zone(), data, data + sizeof(data));
623 631
624 EXPECT_TRUE(sig != nullptr); 632 EXPECT_TRUE(sig != nullptr);
625 EXPECT_EQ(0, sig->parameter_count()); 633 EXPECT_EQ(0, sig->parameter_count());
626 EXPECT_EQ(1, sig->return_count()); 634 EXPECT_EQ(1, sig->return_count());
627 EXPECT_EQ(ret_type.type, sig->GetReturn()); 635 EXPECT_EQ(ret_type.type, sig->GetReturn());
628 } 636 }
629 } 637 }
630 638
631 TEST_F(WasmSignatureDecodeTest, Ok_v_t) { 639 TEST_F(WasmSignatureDecodeTest, Ok_v_t) {
632 for (size_t i = 0; i < arraysize(kLocalTypes); i++) { 640 for (size_t i = 0; i < arraysize(kLocalTypes); i++) {
633 LocalTypePair param_type = kLocalTypes[i]; 641 LocalTypePair param_type = kLocalTypes[i];
634 const byte data[] = {SIG_ENTRY_v_x(param_type.code)}; 642 const byte data[] = {SIG_ENTRY_v_x(param_type.code)};
635 FunctionSig* sig = 643 FunctionSig* sig =
636 DecodeWasmSignatureForTesting(zone(), data, data + arraysize(data)); 644 DecodeWasmSignatureForTesting(zone(), data, data + sizeof(data));
637 645
638 EXPECT_TRUE(sig != nullptr); 646 EXPECT_TRUE(sig != nullptr);
639 EXPECT_EQ(1, sig->parameter_count()); 647 EXPECT_EQ(1, sig->parameter_count());
640 EXPECT_EQ(0, sig->return_count()); 648 EXPECT_EQ(0, sig->return_count());
641 EXPECT_EQ(param_type.type, sig->GetParam(0)); 649 EXPECT_EQ(param_type.type, sig->GetParam(0));
642 } 650 }
643 } 651 }
644 652
645 TEST_F(WasmSignatureDecodeTest, Ok_t_t) { 653 TEST_F(WasmSignatureDecodeTest, Ok_t_t) {
646 for (size_t i = 0; i < arraysize(kLocalTypes); i++) { 654 for (size_t i = 0; i < arraysize(kLocalTypes); i++) {
647 LocalTypePair ret_type = kLocalTypes[i]; 655 LocalTypePair ret_type = kLocalTypes[i];
648 for (size_t j = 0; j < arraysize(kLocalTypes); j++) { 656 for (size_t j = 0; j < arraysize(kLocalTypes); j++) {
649 LocalTypePair param_type = kLocalTypes[j]; 657 LocalTypePair param_type = kLocalTypes[j];
650 const byte data[] = {SIG_ENTRY_x_x(ret_type.code, param_type.code)}; 658 const byte data[] = {SIG_ENTRY_x_x(ret_type.code, param_type.code)};
651 FunctionSig* sig = 659 FunctionSig* sig =
652 DecodeWasmSignatureForTesting(zone(), data, data + arraysize(data)); 660 DecodeWasmSignatureForTesting(zone(), data, data + sizeof(data));
653 661
654 EXPECT_TRUE(sig != nullptr); 662 EXPECT_TRUE(sig != nullptr);
655 EXPECT_EQ(1, sig->parameter_count()); 663 EXPECT_EQ(1, sig->parameter_count());
656 EXPECT_EQ(1, sig->return_count()); 664 EXPECT_EQ(1, sig->return_count());
657 EXPECT_EQ(param_type.type, sig->GetParam(0)); 665 EXPECT_EQ(param_type.type, sig->GetParam(0));
658 EXPECT_EQ(ret_type.type, sig->GetReturn()); 666 EXPECT_EQ(ret_type.type, sig->GetReturn());
659 } 667 }
660 } 668 }
661 } 669 }
662 670
663 TEST_F(WasmSignatureDecodeTest, Ok_i_tt) { 671 TEST_F(WasmSignatureDecodeTest, Ok_i_tt) {
664 for (size_t i = 0; i < arraysize(kLocalTypes); i++) { 672 for (size_t i = 0; i < arraysize(kLocalTypes); i++) {
665 LocalTypePair p0_type = kLocalTypes[i]; 673 LocalTypePair p0_type = kLocalTypes[i];
666 for (size_t j = 0; j < arraysize(kLocalTypes); j++) { 674 for (size_t j = 0; j < arraysize(kLocalTypes); j++) {
667 LocalTypePair p1_type = kLocalTypes[j]; 675 LocalTypePair p1_type = kLocalTypes[j];
668 const byte data[] = { 676 const byte data[] = {
669 SIG_ENTRY_x_xx(kLocalI32, p0_type.code, p1_type.code)}; 677 SIG_ENTRY_x_xx(kLocalI32, p0_type.code, p1_type.code)};
670 FunctionSig* sig = 678 FunctionSig* sig =
671 DecodeWasmSignatureForTesting(zone(), data, data + arraysize(data)); 679 DecodeWasmSignatureForTesting(zone(), data, data + sizeof(data));
672 680
673 EXPECT_TRUE(sig != nullptr); 681 EXPECT_TRUE(sig != nullptr);
674 EXPECT_EQ(2, sig->parameter_count()); 682 EXPECT_EQ(2, sig->parameter_count());
675 EXPECT_EQ(1, sig->return_count()); 683 EXPECT_EQ(1, sig->return_count());
676 EXPECT_EQ(p0_type.type, sig->GetParam(0)); 684 EXPECT_EQ(p0_type.type, sig->GetParam(0));
677 EXPECT_EQ(p1_type.type, sig->GetParam(1)); 685 EXPECT_EQ(p1_type.type, sig->GetParam(1));
678 } 686 }
679 } 687 }
680 } 688 }
681 689
(...skipping 10 matching lines...) Expand all
692 } 700 }
693 } 701 }
694 } 702 }
695 703
696 TEST_F(WasmSignatureDecodeTest, Fail_invalid_type) { 704 TEST_F(WasmSignatureDecodeTest, Fail_invalid_type) {
697 byte kInvalidType = 76; 705 byte kInvalidType = 76;
698 for (size_t i = 0; i < SIZEOF_SIG_ENTRY_x_xx; i++) { 706 for (size_t i = 0; i < SIZEOF_SIG_ENTRY_x_xx; i++) {
699 byte data[] = {SIG_ENTRY_x_xx(kLocalI32, kLocalI32, kLocalI32)}; 707 byte data[] = {SIG_ENTRY_x_xx(kLocalI32, kLocalI32, kLocalI32)};
700 data[i] = kInvalidType; 708 data[i] = kInvalidType;
701 FunctionSig* sig = 709 FunctionSig* sig =
702 DecodeWasmSignatureForTesting(zone(), data, data + arraysize(data)); 710 DecodeWasmSignatureForTesting(zone(), data, data + sizeof(data));
703 EXPECT_EQ(nullptr, sig); 711 EXPECT_EQ(nullptr, sig);
704 } 712 }
705 } 713 }
706 714
707 TEST_F(WasmSignatureDecodeTest, Fail_invalid_ret_type1) { 715 TEST_F(WasmSignatureDecodeTest, Fail_invalid_ret_type1) {
708 static const byte data[] = {SIG_ENTRY_x_x(kLocalVoid, kLocalI32)}; 716 static const byte data[] = {SIG_ENTRY_x_x(kLocalVoid, kLocalI32)};
709 FunctionSig* sig = 717 FunctionSig* sig =
710 DecodeWasmSignatureForTesting(zone(), data, data + arraysize(data)); 718 DecodeWasmSignatureForTesting(zone(), data, data + sizeof(data));
711 EXPECT_EQ(nullptr, sig); 719 EXPECT_EQ(nullptr, sig);
712 } 720 }
713 721
714 TEST_F(WasmSignatureDecodeTest, Fail_invalid_param_type1) { 722 TEST_F(WasmSignatureDecodeTest, Fail_invalid_param_type1) {
715 static const byte data[] = {SIG_ENTRY_x_x(kLocalI32, kLocalVoid)}; 723 static const byte data[] = {SIG_ENTRY_x_x(kLocalI32, kLocalVoid)};
716 FunctionSig* sig = 724 FunctionSig* sig =
717 DecodeWasmSignatureForTesting(zone(), data, data + arraysize(data)); 725 DecodeWasmSignatureForTesting(zone(), data, data + sizeof(data));
718 EXPECT_EQ(nullptr, sig); 726 EXPECT_EQ(nullptr, sig);
719 } 727 }
720 728
721 TEST_F(WasmSignatureDecodeTest, Fail_invalid_param_type2) { 729 TEST_F(WasmSignatureDecodeTest, Fail_invalid_param_type2) {
722 static const byte data[] = {SIG_ENTRY_x_xx(kLocalI32, kLocalI32, kLocalVoid)}; 730 static const byte data[] = {SIG_ENTRY_x_xx(kLocalI32, kLocalI32, kLocalVoid)};
723 FunctionSig* sig = 731 FunctionSig* sig =
724 DecodeWasmSignatureForTesting(zone(), data, data + arraysize(data)); 732 DecodeWasmSignatureForTesting(zone(), data, data + sizeof(data));
725 EXPECT_EQ(nullptr, sig); 733 EXPECT_EQ(nullptr, sig);
726 } 734 }
727 735
728 class WasmFunctionVerifyTest : public TestWithIsolateAndZone {}; 736 class WasmFunctionVerifyTest : public TestWithIsolateAndZone {};
729 737
730 TEST_F(WasmFunctionVerifyTest, Ok_v_v_empty) { 738 TEST_F(WasmFunctionVerifyTest, Ok_v_v_empty) {
731 static const byte data[] = { 739 static const byte data[] = {
732 SIG_ENTRY_v_v, // signature entry 740 SIG_ENTRY_v_v, // signature entry
733 4, // locals 741 4, // locals
734 3, 742 3,
735 kLocalI32, // -- 743 kLocalI32, // --
736 4, 744 4,
737 kLocalI64, // -- 745 kLocalI64, // --
738 5, 746 5,
739 kLocalF32, // -- 747 kLocalF32, // --
740 6, 748 6,
741 kLocalF64, // -- 749 kLocalF64, // --
742 kExprNop // body 750 kExprNop // body
743 }; 751 };
744 752
745 FunctionResult result = DecodeWasmFunction(isolate(), zone(), nullptr, data, 753 FunctionResult result =
746 data + arraysize(data)); 754 DecodeWasmFunction(isolate(), zone(), nullptr, data, data + sizeof(data));
747 EXPECT_OK(result); 755 EXPECT_OK(result);
748 756
749 if (result.val && result.ok()) { 757 if (result.val && result.ok()) {
750 WasmFunction* function = result.val; 758 WasmFunction* function = result.val;
751 EXPECT_EQ(0, function->sig->parameter_count()); 759 EXPECT_EQ(0, function->sig->parameter_count());
752 EXPECT_EQ(0, function->sig->return_count()); 760 EXPECT_EQ(0, function->sig->return_count());
753 EXPECT_EQ(0, function->name_offset); 761 EXPECT_EQ(0, function->name_offset);
754 EXPECT_EQ(SIZEOF_SIG_ENTRY_v_v, function->code_start_offset); 762 EXPECT_EQ(SIZEOF_SIG_ENTRY_v_v, function->code_start_offset);
755 EXPECT_EQ(arraysize(data), function->code_end_offset); 763 EXPECT_EQ(sizeof(data), function->code_end_offset);
756 // TODO(titzer): verify encoding of local declarations 764 // TODO(titzer): verify encoding of local declarations
757 } 765 }
758 766
759 if (result.val) delete result.val; 767 if (result.val) delete result.val;
760 } 768 }
761 769
762 TEST_F(WasmModuleVerifyTest, SectionWithoutNameLength) { 770 TEST_F(WasmModuleVerifyTest, SectionWithoutNameLength) {
763 const byte data[] = {1}; 771 const byte data[] = {1};
764 EXPECT_FAILURE(data); 772 EXPECT_FAILURE(data);
765 } 773 }
766 774
767 TEST_F(WasmModuleVerifyTest, TheLoneliestOfValidModulesTheTrulyEmptyOne) { 775 TEST_F(WasmModuleVerifyTest, TheLoneliestOfValidModulesTheTrulyEmptyOne) {
768 const byte data[] = { 776 const byte data[] = {
777 0, // unknown section code.
769 0, // Empty section name. 778 0, // Empty section name.
770 // No section name, no content, nothing but sadness. 779 // No section name, no content, nothing but sadness.
771 0, // No section content. 780 0, // No section content.
772 }; 781 };
773 EXPECT_VERIFIES(data); 782 EXPECT_VERIFIES(data);
774 } 783 }
775 784
776 TEST_F(WasmModuleVerifyTest, OnlyUnknownSectionEmpty) { 785 TEST_F(WasmModuleVerifyTest, OnlyUnknownSectionEmpty) {
777 const byte data[] = { 786 const byte data[] = {
778 UNKNOWN_SECTION_NAME, 0, 787 UNKNOWN_SECTION(0),
779 }; 788 };
780 EXPECT_VERIFIES(data); 789 EXPECT_VERIFIES(data);
781 } 790 }
782 791
783 TEST_F(WasmModuleVerifyTest, OnlyUnknownSectionNonEmpty) { 792 TEST_F(WasmModuleVerifyTest, OnlyUnknownSectionNonEmpty) {
784 const byte data[] = { 793 const byte data[] = {
785 UNKNOWN_SECTION_NAME, 794 UNKNOWN_SECTION(5),
786 5, // section size
787 0xff, 795 0xff,
788 0xff, 796 0xff,
789 0xff, 797 0xff,
790 0xff, 798 0xff,
791 0xff, // section data 799 0xff, // section data
792 }; 800 };
793 EXPECT_VERIFIES(data); 801 EXPECT_VERIFIES(data);
794 } 802 }
795 803
796 TEST_F(WasmModuleVerifyTest, SignatureFollowedByEmptyUnknownSection) { 804 TEST_F(WasmModuleVerifyTest, SignatureFollowedByEmptyUnknownSection) {
797 const byte data[] = { 805 const byte data[] = {
798 // signatures 806 // signatures
799 SIGNATURES_SECTION_VOID_VOID, 807 SIGNATURES_SECTION_VOID_VOID,
800 // ----------------------------------------------------------- 808 // -----------------------------------------------------------
801 UNKNOWN_SECTION_NAME, 809 UNKNOWN_SECTION(0)};
802 0 // empty section
803 };
804 EXPECT_VERIFIES(data); 810 EXPECT_VERIFIES(data);
805 } 811 }
806 812
807 TEST_F(WasmModuleVerifyTest, SignatureFollowedByUnknownSection) { 813 TEST_F(WasmModuleVerifyTest, SignatureFollowedByUnknownSection) {
808 const byte data[] = { 814 const byte data[] = {
809 // signatures 815 // signatures
810 SIGNATURES_SECTION_VOID_VOID, 816 SIGNATURES_SECTION_VOID_VOID,
811 // ----------------------------------------------------------- 817 // -----------------------------------------------------------
812 UNKNOWN_SECTION_NAME, 818 UNKNOWN_SECTION(5), 0xff, 0xff, 0xff, 0xff, 0xff,
813 5, // section size
814 0xff, 0xff, 0xff, 0xff, 0xff,
815 };
816 EXPECT_VERIFIES(data);
817 }
818
819 TEST_F(WasmModuleVerifyTest, SignatureFollowedByUnknownSectionWithLongLEB) {
820 const byte data[] = {
821 // signatures
822 SIGNATURES_SECTION_VOID_VOID,
823 // -----------------------------------------------------------
824 UNKNOWN_SECTION_NAME, 0x81, 0x80, 0x80, 0x80,
825 0x00, // section size: 1 but in a 5-byte LEB
826 0,
827 }; 819 };
828 EXPECT_VERIFIES(data); 820 EXPECT_VERIFIES(data);
829 } 821 }
830 822
831 TEST_F(WasmModuleVerifyTest, UnknownSectionOverflow) { 823 TEST_F(WasmModuleVerifyTest, UnknownSectionOverflow) {
832 static const byte data[] = { 824 static const byte data[] = {
833 UNKNOWN_EMPTY_SECTION_NAME, 825 UNKNOWN_SECTION(9),
834 9, // section size
835 1, 826 1,
836 2, 827 2,
837 3, 828 3,
838 4, 829 4,
839 5, 830 5,
840 6, 831 6,
841 7, 832 7,
842 8, 833 8,
843 9, 834 9,
844 10, // 10 byte section 835 10, // 10 byte section
845 }; 836 };
846 EXPECT_FAILURE(data); 837 EXPECT_FAILURE(data);
847 } 838 }
848 839
849 TEST_F(WasmModuleVerifyTest, UnknownSectionUnderflow) { 840 TEST_F(WasmModuleVerifyTest, UnknownSectionUnderflow) {
850 static const byte data[] = { 841 static const byte data[] = {
851 UNKNOWN_EMPTY_SECTION_NAME, 842 UNKNOWN_SECTION(333),
852 0xff,
853 0xff,
854 0xff,
855 0xff,
856 0x0f, // Section size LEB128 0xffffffff
857 1, 843 1,
858 2, 844 2,
859 3, 845 3,
860 4, // 4 byte section 846 4, // 4 byte section
861 }; 847 };
862 EXPECT_FAILURE(data); 848 EXPECT_FAILURE(data);
863 } 849 }
864 850
865 TEST_F(WasmModuleVerifyTest, UnknownSectionLoop) {
866 // Would infinite loop decoding if wrapping and allowed.
867 static const byte data[] = {
868 UNKNOWN_EMPTY_SECTION_NAME,
869 1,
870 2,
871 3,
872 4, // 4 byte section
873 0xfa,
874 0xff,
875 0xff,
876 0xff,
877 0x0f, // Section size LEB128 0xfffffffa
878 };
879 EXPECT_FAILURE(data);
880 }
881
882 TEST_F(WasmModuleVerifyTest, UnknownSectionSkipped) { 851 TEST_F(WasmModuleVerifyTest, UnknownSectionSkipped) {
883 static const byte data[] = { 852 static const byte data[] = {
884 UNKNOWN_EMPTY_SECTION_NAME, 853 UNKNOWN_SECTION(1),
885 1, // section size
886 0, // one byte section 854 0, // one byte section
887 SECTION(GLOBALS, 4), 855 SECTION(Global, 6),
888 1, 856 1,
889 0, // name length 857 kLocalI32, // memory type
890 kLocalI32, // memory type 858 0, // exported
891 0, // exported 859 WASM_INIT_EXPR_I32V_1(33), // init
892 }; 860 };
893 ModuleResult result = DecodeModule(data, data + arraysize(data)); 861 ModuleResult result = DecodeModule(data, data + sizeof(data));
894 EXPECT_OK(result); 862 EXPECT_OK(result);
895 863
896 EXPECT_EQ(1, result.val->globals.size()); 864 EXPECT_EQ(1, result.val->globals.size());
897 EXPECT_EQ(0, result.val->functions.size()); 865 EXPECT_EQ(0, result.val->functions.size());
898 EXPECT_EQ(0, result.val->data_segments.size()); 866 EXPECT_EQ(0, result.val->data_segments.size());
899 867
900 const WasmGlobal* global = &result.val->globals.back(); 868 const WasmGlobal* global = &result.val->globals.back();
901 869
902 EXPECT_EQ(0, global->name_length);
903 EXPECT_EQ(kAstI32, global->type); 870 EXPECT_EQ(kAstI32, global->type);
904 EXPECT_EQ(0, global->offset); 871 EXPECT_EQ(0, global->offset);
905 EXPECT_FALSE(global->exported);
906 872
907 if (result.val) delete result.val; 873 if (result.val) delete result.val;
908 } 874 }
909 875
910 TEST_F(WasmModuleVerifyTest, ImportTable_empty) { 876 TEST_F(WasmModuleVerifyTest, ImportTable_empty) {
911 static const byte data[] = {SECTION(SIGNATURES, 1), 0, 877 static const byte data[] = {SECTION(Type, 1), 0, SECTION(Import, 1), 0};
912 SECTION(IMPORT_TABLE, 1), 0};
913 EXPECT_VERIFIES(data); 878 EXPECT_VERIFIES(data);
914 } 879 }
915 880
916 TEST_F(WasmModuleVerifyTest, ImportTable_nosigs1) { 881 TEST_F(WasmModuleVerifyTest, ImportTable_nosigs1) {
917 static const byte data[] = {SECTION(IMPORT_TABLE, 1), 0}; 882 static const byte data[] = {SECTION(Import, 1), 0};
918 EXPECT_VERIFIES(data); 883 EXPECT_VERIFIES(data);
919 } 884 }
920 885
921 TEST_F(WasmModuleVerifyTest, ImportTable_nosigs2) { 886 TEST_F(WasmModuleVerifyTest, ImportTable_nosigs2) {
922 static const byte data[] = { 887 static const byte data[] = {
923 SECTION(IMPORT_TABLE, 6), 1, // sig table 888 SECTION(Import, 6), 1, // sig table
924 IMPORT_SIG_INDEX(0), // sig index 889 NAME_LENGTH(1), 'm', // module name
925 NAME_LENGTH(1), 'm', // module name 890 NAME_LENGTH(1), 'f', // function name
926 NAME_LENGTH(1), 'f', // function name 891 kExternalFunction, // import kind
892 IMPORT_SIG_INDEX(0), // sig index
927 }; 893 };
928 EXPECT_FAILURE(data); 894 EXPECT_FAILURE(data);
929 } 895 }
930 896
931 TEST_F(WasmModuleVerifyTest, ImportTable_invalid_sig) { 897 TEST_F(WasmModuleVerifyTest, ImportTable_invalid_sig) {
932 static const byte data[] = { 898 static const byte data[] = {
933 SECTION(SIGNATURES, 1), 0, // -- 899 SECTION(Type, 1), 0, // --
934 SECTION(IMPORT_TABLE, 6), 1, // -- 900 SECTION(Import, 6), 1, // --
935 IMPORT_SIG_INDEX(0), // sig index 901 NAME_LENGTH(1), 'm', // module name
936 NAME_LENGTH(1), 'm', // module name 902 NAME_LENGTH(1), 'f', // function name
937 NAME_LENGTH(1), 'f', // function name 903 kExternalFunction, // import kind
904 IMPORT_SIG_INDEX(0), // sig index
938 }; 905 };
939 EXPECT_FAILURE(data); 906 EXPECT_FAILURE(data);
940 } 907 }
941 908
942 TEST_F(WasmModuleVerifyTest, ImportTable_one_sig) { 909 TEST_F(WasmModuleVerifyTest, ImportTable_one_sig) {
943 static const byte data[] = { 910 static const byte data[] = {
944 // signatures 911 // signatures
945 SIGNATURES_SECTION_VOID_VOID, 912 SIGNATURES_SECTION_VOID_VOID,
946 SECTION(IMPORT_TABLE, 6), 913 SECTION(Import, 7),
947 1, // -- 914 1, // --
948 IMPORT_SIG_INDEX(0), // sig index
949 NAME_LENGTH(1), 915 NAME_LENGTH(1),
950 'm', // module name 916 'm', // module name
951 NAME_LENGTH(1), 917 NAME_LENGTH(1),
952 'f', // function name 918 'f', // function name
919 kExternalFunction, // import kind
920 IMPORT_SIG_INDEX(0), // sig index
953 }; 921 };
954 EXPECT_VERIFIES(data); 922 EXPECT_VERIFIES(data);
955 } 923 }
956 924
957 TEST_F(WasmModuleVerifyTest, ImportTable_invalid_module) { 925 TEST_F(WasmModuleVerifyTest, ImportTable_invalid_module) {
958 static const byte data[] = { 926 static const byte data[] = {
959 // signatures 927 // signatures
960 SIGNATURES_SECTION_VOID_VOID, 928 SIGNATURES_SECTION_VOID_VOID, // --
961 SECTION(IMPORT_TABLE, 6), 929 SECTION(Import, 7), // --
962 1, // -- 930 1, // --
963 IMPORT_SIG_INDEX(0), // sig index 931 NO_NAME, // module name
964 NO_NAME, // module name 932 NAME_LENGTH(1), // --
965 NAME_LENGTH(1), 933 'f', // function name
966 'f' // function name 934 kExternalFunction, // import kind
935 IMPORT_SIG_INDEX(0), // sig index
967 }; 936 };
968 EXPECT_FAILURE(data); 937 EXPECT_FAILURE(data);
969 } 938 }
970 939
971 TEST_F(WasmModuleVerifyTest, ImportTable_off_end) { 940 TEST_F(WasmModuleVerifyTest, ImportTable_off_end) {
972 static const byte data[] = { 941 static const byte data[] = {
973 // signatures 942 // signatures
974 SIGNATURES_SECTION_VOID_VOID, 943 SIGNATURES_SECTION_VOID_VOID,
975 SECTION(IMPORT_TABLE, 6), 944 SECTION(Import, 6),
976 1, 945 1,
977 IMPORT_SIG_INDEX(0), // sig index
978 NAME_LENGTH(1), 946 NAME_LENGTH(1),
979 'm', // module name 947 'm', // module name
980 NAME_LENGTH(1), 948 NAME_LENGTH(1),
981 'f', // function name 949 'f', // function name
950 kExternalFunction, // import kind
951 IMPORT_SIG_INDEX(0), // sig index
982 }; 952 };
983 953
984 EXPECT_OFF_END_FAILURE(data, 16, sizeof(data)); 954 EXPECT_OFF_END_FAILURE(data, 16, sizeof(data));
985 } 955 }
986 956
987 TEST_F(WasmModuleVerifyTest, ExportTable_empty1) { 957 TEST_F(WasmModuleVerifyTest, ExportTable_empty1) {
988 static const byte data[] = {// signatures 958 static const byte data[] = { // signatures
989 SIGNATURES_SECTION_VOID_VOID, ONE_EMPTY_FUNCTION, 959 SIGNATURES_SECTION_VOID_VOID, // --
990 SECTION(EXPORT_TABLE, 1), 960 ONE_EMPTY_FUNCTION, SECTION(Export, 1), // --
991 0, // -- 961 0, // --
992 ONE_EMPTY_BODY}; 962 ONE_EMPTY_BODY};
993 963
994 ModuleResult result = DecodeModule(data, data + arraysize(data)); 964 ModuleResult result = DecodeModule(data, data + sizeof(data));
995 EXPECT_OK(result); 965 EXPECT_OK(result);
996 966
997 EXPECT_EQ(1, result.val->functions.size()); 967 EXPECT_EQ(1, result.val->functions.size());
998 EXPECT_EQ(0, result.val->export_table.size()); 968 EXPECT_EQ(0, result.val->export_table.size());
999 969
1000 if (result.val) delete result.val; 970 if (result.val) delete result.val;
1001 } 971 }
1002 972
1003 TEST_F(WasmModuleVerifyTest, ExportTable_empty2) { 973 TEST_F(WasmModuleVerifyTest, ExportTable_empty2) {
1004 static const byte data[] = { 974 static const byte data[] = {
1005 SECTION(SIGNATURES, 1), 0, SECTION(EXPORT_TABLE, 1), 0 // -- 975 SECTION(Type, 1), 0, SECTION(Export, 1), 0 // --
1006 }; 976 };
1007 // TODO(titzer): current behavior treats empty functions section as missing. 977 EXPECT_VERIFIES(data);
1008 EXPECT_FAILURE(data);
1009 }
1010
1011 TEST_F(WasmModuleVerifyTest, ExportTable_NoFunctions1) {
1012 static const byte data[] = {
1013 SECTION(SIGNATURES, 1), 0, SECTION(EXPORT_TABLE, 1), 0 // --
1014 };
1015 EXPECT_FAILURE(data);
1016 } 978 }
1017 979
1018 TEST_F(WasmModuleVerifyTest, ExportTable_NoFunctions2) { 980 TEST_F(WasmModuleVerifyTest, ExportTable_NoFunctions2) {
1019 static const byte data[] = {SECTION(EXPORT_TABLE, 1), 0}; 981 static const byte data[] = {SECTION(Export, 1), 0};
1020 EXPECT_FAILURE(data); 982 EXPECT_VERIFIES(data);
1021 } 983 }
1022 984
1023 TEST_F(WasmModuleVerifyTest, ExportTableOne) { 985 TEST_F(WasmModuleVerifyTest, ExportTableOne) {
1024 static const byte data[] = {// signatures 986 static const byte data[] = {// signatures
1025 SIGNATURES_SECTION_VOID_VOID, 987 SIGNATURES_SECTION_VOID_VOID,
1026 ONE_EMPTY_FUNCTION, 988 ONE_EMPTY_FUNCTION,
1027 SECTION(EXPORT_TABLE, 3), 989 SECTION(Export, 4),
1028 1, // exports 990 1, // exports
1029 FUNC_INDEX(0), // -- 991 NO_NAME, // --
1030 NO_NAME, // -- 992 kExternalFunction, // --
993 FUNC_INDEX(0), // --
1031 ONE_EMPTY_BODY}; 994 ONE_EMPTY_BODY};
1032 ModuleResult result = DecodeModule(data, data + arraysize(data)); 995 ModuleResult result = DecodeModule(data, data + sizeof(data));
1033 EXPECT_OK(result); 996 EXPECT_OK(result);
1034 997
1035 EXPECT_EQ(1, result.val->functions.size()); 998 EXPECT_EQ(1, result.val->functions.size());
1036 EXPECT_EQ(1, result.val->export_table.size()); 999 EXPECT_EQ(1, result.val->export_table.size());
1037 1000
1038 if (result.val) delete result.val; 1001 if (result.val) delete result.val;
1039 } 1002 }
1040 1003
1041 TEST_F(WasmModuleVerifyTest, ExportNameWithInvalidStringLength) { 1004 TEST_F(WasmModuleVerifyTest, ExportNameWithInvalidStringLength) {
1042 static const byte data[] = {// signatures 1005 static const byte data[] = {// signatures
1043 SIGNATURES_SECTION_VOID_VOID, 1006 SIGNATURES_SECTION_VOID_VOID,
1044 ONE_EMPTY_FUNCTION, 1007 ONE_EMPTY_FUNCTION,
1045 SECTION(EXPORT_TABLE, 12), 1008 SECTION(Export, 12),
1046 1, // exports 1009 1, // exports
1047 FUNC_INDEX(0), // -- 1010 NAME_LENGTH(84), // invalid string length
1048 NAME_LENGTH(84), // invalid string length 1011 'e', // --
1049 'e', // -- 1012 kExternalFunction, // --
1050 ONE_EMPTY_BODY}; 1013 FUNC_INDEX(0)};
1051 1014
1052 EXPECT_FAILURE(data); 1015 EXPECT_FAILURE(data);
1053 } 1016 }
1054 1017
1055 TEST_F(WasmModuleVerifyTest, ExportTableTwo) { 1018 TEST_F(WasmModuleVerifyTest, ExportTableTwo) {
1056 static const byte data[] = {// signatures 1019 static const byte data[] = {// signatures
1057 SIGNATURES_SECTION_VOID_VOID, 1020 SIGNATURES_SECTION_VOID_VOID,
1058 ONE_EMPTY_FUNCTION, 1021 ONE_EMPTY_FUNCTION,
1059 SECTION(EXPORT_TABLE, 12), 1022 SECTION(Export, 14),
1060 2, // exports 1023 2, // exports
1061 FUNC_INDEX(0), // --
1062 NAME_LENGTH(4), 1024 NAME_LENGTH(4),
1063 'n', 1025 'n',
1064 'a', 1026 'a',
1065 'm', 1027 'm',
1066 'e', // -- 1028 'e', // --
1029 kExternalFunction,
1067 FUNC_INDEX(0), // -- 1030 FUNC_INDEX(0), // --
1068 NAME_LENGTH(3), 1031 NAME_LENGTH(3),
1069 'n', 1032 'n',
1070 'o', 1033 'o',
1071 'm', // -- 1034 'm', // --
1035 kExternalFunction, // --
1036 FUNC_INDEX(0), // --
1072 ONE_EMPTY_BODY}; 1037 ONE_EMPTY_BODY};
1073 1038
1074 ModuleResult result = DecodeModule(data, data + arraysize(data)); 1039 ModuleResult result = DecodeModule(data, data + sizeof(data));
1075 EXPECT_OK(result); 1040 EXPECT_OK(result);
1076 1041
1077 EXPECT_EQ(1, result.val->functions.size()); 1042 EXPECT_EQ(1, result.val->functions.size());
1078 EXPECT_EQ(2, result.val->export_table.size()); 1043 EXPECT_EQ(2, result.val->export_table.size());
1079 1044
1080 if (result.val) delete result.val; 1045 if (result.val) delete result.val;
1081 } 1046 }
1082 1047
1083 TEST_F(WasmModuleVerifyTest, ExportTableThree) { 1048 TEST_F(WasmModuleVerifyTest, ExportTableThree) {
1084 static const byte data[] = {// signatures 1049 static const byte data[] = {// signatures
1085 SIGNATURES_SECTION_VOID_VOID, 1050 SIGNATURES_SECTION_VOID_VOID,
1086 THREE_EMPTY_FUNCTIONS, 1051 THREE_EMPTY_FUNCTIONS,
1087 SECTION(EXPORT_TABLE, 10), 1052 SECTION(Export, 13),
1088 3, // exports 1053 3, // exports
1054 NAME_LENGTH(1),
1055 'a', // --
1056 kExternalFunction,
1089 FUNC_INDEX(0), // -- 1057 FUNC_INDEX(0), // --
1090 NAME_LENGTH(1), 1058 NAME_LENGTH(1),
1091 'a', // -- 1059 'b', // --
1060 kExternalFunction,
1092 FUNC_INDEX(1), // -- 1061 FUNC_INDEX(1), // --
1093 NAME_LENGTH(1), 1062 NAME_LENGTH(1),
1094 'b', // -- 1063 'c', // --
1064 kExternalFunction,
1095 FUNC_INDEX(2), // -- 1065 FUNC_INDEX(2), // --
1096 NAME_LENGTH(1),
1097 'c', // --
1098 THREE_EMPTY_BODIES}; 1066 THREE_EMPTY_BODIES};
1099 ModuleResult result = DecodeModule(data, data + arraysize(data)); 1067 ModuleResult result = DecodeModule(data, data + sizeof(data));
1100 EXPECT_OK(result); 1068 EXPECT_OK(result);
1101 1069
1102 EXPECT_EQ(3, result.val->functions.size()); 1070 EXPECT_EQ(3, result.val->functions.size());
1103 EXPECT_EQ(3, result.val->export_table.size()); 1071 EXPECT_EQ(3, result.val->export_table.size());
1104 1072
1105 if (result.val) delete result.val; 1073 if (result.val) delete result.val;
1106 } 1074 }
1107 1075
1108 TEST_F(WasmModuleVerifyTest, ExportTableThreeOne) { 1076 TEST_F(WasmModuleVerifyTest, ExportTableThreeOne) {
1109 for (int i = 0; i < 6; i++) { 1077 for (int i = 0; i < 6; i++) {
1110 const byte data[] = {// signatures 1078 const byte data[] = {// signatures
1111 SIGNATURES_SECTION_VOID_VOID, 1079 SIGNATURES_SECTION_VOID_VOID,
1112 THREE_EMPTY_FUNCTIONS, 1080 THREE_EMPTY_FUNCTIONS,
1113 SECTION(EXPORT_TABLE, 5), 1081 SECTION(Export, 6),
1114 1, // exports 1082 1, // exports
1115 FUNC_INDEX(i), // --
1116 NAME_LENGTH(2), 1083 NAME_LENGTH(2),
1117 'e', 1084 'e',
1118 'x', // -- 1085 'x', // --
1086 kExternalFunction,
1087 FUNC_INDEX(i), // --
1119 THREE_EMPTY_BODIES}; 1088 THREE_EMPTY_BODIES};
1120 1089
1121 if (i < 3) { 1090 if (i < 3) {
1122 EXPECT_VERIFIES(data); 1091 EXPECT_VERIFIES(data);
1123 } else { 1092 } else {
1124 EXPECT_FAILURE(data); 1093 EXPECT_FAILURE(data);
1125 } 1094 }
1126 } 1095 }
1127 } 1096 }
1128 1097
1129 TEST_F(WasmModuleVerifyTest, ExportTableOne_off_end) { 1098 TEST_F(WasmModuleVerifyTest, ExportTableOne_off_end) {
1130 static const byte data[] = { 1099 static const byte data[] = {
1131 // signatures 1100 // signatures
1132 SIGNATURES_SECTION_VOID_VOID, 1101 SIGNATURES_SECTION_VOID_VOID,
1133 ONE_EMPTY_FUNCTION, 1102 ONE_EMPTY_FUNCTION,
1134 SECTION(EXPORT_TABLE, 1 + 6), 1103 SECTION(Export, 1 + 6),
1135 1, // exports 1104 1, // exports
1105 NO_NAME, // --
1106 kExternalFunction,
1136 FUNC_INDEX(0), // -- 1107 FUNC_INDEX(0), // --
1137 NO_NAME // --
1138 }; 1108 };
1139 1109
1140 for (int length = 33; length < sizeof(data); length++) { 1110 for (int length = 33; length < sizeof(data); length++) {
1141 ModuleResult result = DecodeModule(data, data + length); 1111 ModuleResult result = DecodeModule(data, data + length);
1142 EXPECT_FALSE(result.ok()); 1112 EXPECT_FALSE(result.ok());
1143 if (result.val) delete result.val; 1113 if (result.val) delete result.val;
1144 } 1114 }
1145 } 1115 }
1146 1116
1147 TEST_F(WasmModuleVerifyTest, FunctionSignatures_empty) { 1117 TEST_F(WasmModuleVerifyTest, FunctionSignatures_empty) {
1148 static const byte data[] = { 1118 static const byte data[] = {
1149 SECTION(SIGNATURES, 1), 0, // -- 1119 SECTION(Type, 1), 0, // --
1150 SECTION(FUNCTION_SIGNATURES, 1), 0 // -- 1120 SECTION(Function, 1), 0 // --
1151 }; // -- 1121 }; // --
1152 EXPECT_VERIFIES(data); 1122 EXPECT_VERIFIES(data);
1153 } 1123 }
1154 1124
1155 TEST_F(WasmModuleVerifyTest, FunctionSignatures_one) { 1125 TEST_F(WasmModuleVerifyTest, FunctionSignatures_one) {
1156 static const byte data[] = { 1126 static const byte data[] = {
1157 SIGNATURES_SECTION(1, SIG_ENTRY_v_v), // -- 1127 SIGNATURES_SECTION(1, SIG_ENTRY_v_v), // --
1158 FUNCTION_SIGNATURES_SECTION(1, 0) // -- 1128 FUNCTION_SIGNATURES_SECTION(1, 0) // --
1159 }; 1129 };
1160 EXPECT_VERIFIES(data); 1130 EXPECT_VERIFIES(data);
1161 } 1131 }
1162 1132
1163 TEST_F(WasmModuleVerifyTest, Regression_648070) { 1133 TEST_F(WasmModuleVerifyTest, Regression_648070) {
1164 static const byte data[] = { 1134 static const byte data[] = {
1165 SECTION(SIGNATURES, 1), 0, // -- 1135 SECTION(Type, 1), 0, // --
1166 SECTION(FUNCTION_SIGNATURES, 5), // -- 1136 SECTION(Function, 5), // --
1167 U32V_5(3500228624) // function count = 3500228624 1137 U32V_5(3500228624) // function count = 3500228624
1168 }; // -- 1138 }; // --
1169 EXPECT_FAILURE(data); 1139 EXPECT_FAILURE(data);
1170 } 1140 }
1171 1141
1172 TEST_F(WasmModuleVerifyTest, FunctionBodies_empty) { 1142 TEST_F(WasmModuleVerifyTest, FunctionBodies_empty) {
1173 static const byte data[] = { 1143 static const byte data[] = {
1174 EMPTY_SIGNATURES_SECTION, // -- 1144 EMPTY_SIGNATURES_SECTION, // --
1175 EMPTY_FUNCTION_SIGNATURES_SECTION, // -- 1145 EMPTY_FUNCTION_SIGNATURES_SECTION, // --
1176 EMPTY_FUNCTION_BODIES_SECTION // -- 1146 EMPTY_FUNCTION_BODIES_SECTION // --
1177 }; 1147 };
1178 EXPECT_VERIFIES(data); 1148 EXPECT_VERIFIES(data);
1179 } 1149 }
1180 1150
1181 TEST_F(WasmModuleVerifyTest, FunctionBodies_one_empty) { 1151 TEST_F(WasmModuleVerifyTest, FunctionBodies_one_empty) {
1182 static const byte data[] = { 1152 static const byte data[] = {
1183 SIGNATURES_SECTION(1, SIG_ENTRY_v_v), // -- 1153 SIGNATURES_SECTION(1, SIG_ENTRY_v_v), // --
1184 FUNCTION_SIGNATURES_SECTION(1, 0), // -- 1154 FUNCTION_SIGNATURES_SECTION(1, 0), // --
1185 SECTION(FUNCTION_BODIES, 1 + SIZEOF_EMPTY_BODY), 1, EMPTY_BODY // -- 1155 SECTION(Code, 1 + SIZEOF_EMPTY_BODY), 1, EMPTY_BODY // --
1186 }; 1156 };
1187 EXPECT_VERIFIES(data); 1157 EXPECT_VERIFIES(data);
1188 } 1158 }
1189 1159
1190 TEST_F(WasmModuleVerifyTest, FunctionBodies_one_nop) { 1160 TEST_F(WasmModuleVerifyTest, FunctionBodies_one_nop) {
1191 static const byte data[] = { 1161 static const byte data[] = {
1192 SIGNATURES_SECTION(1, SIG_ENTRY_v_v), // -- 1162 SIGNATURES_SECTION(1, SIG_ENTRY_v_v), // --
1193 FUNCTION_SIGNATURES_SECTION(1, 0), // -- 1163 FUNCTION_SIGNATURES_SECTION(1, 0), // --
1194 SECTION(FUNCTION_BODIES, 1 + SIZEOF_NOP_BODY), 1, NOP_BODY // -- 1164 SECTION(Code, 1 + SIZEOF_NOP_BODY), 1, NOP_BODY // --
1195 }; 1165 };
1196 EXPECT_VERIFIES(data); 1166 EXPECT_VERIFIES(data);
1197 } 1167 }
1198 1168
1199 TEST_F(WasmModuleVerifyTest, FunctionBodies_count_mismatch1) { 1169 TEST_F(WasmModuleVerifyTest, FunctionBodies_count_mismatch1) {
1200 static const byte data[] = { 1170 static const byte data[] = {
1201 SIGNATURES_SECTION(1, SIG_ENTRY_v_v), // -- 1171 SIGNATURES_SECTION(1, SIG_ENTRY_v_v), // --
1202 FUNCTION_SIGNATURES_SECTION(2, 0, 0), // -- 1172 FUNCTION_SIGNATURES_SECTION(2, 0, 0), // --
1203 SECTION(FUNCTION_BODIES, 1 + SIZEOF_EMPTY_BODY), 1, // -- 1173 SECTION(Code, 1 + SIZEOF_EMPTY_BODY), 1, // --
1204 EMPTY_BODY // -- 1174 EMPTY_BODY // --
1205 }; 1175 };
1206 EXPECT_FAILURE(data); 1176 EXPECT_FAILURE(data);
1207 } 1177 }
1208 1178
1209 TEST_F(WasmModuleVerifyTest, FunctionBodies_count_mismatch2) { 1179 TEST_F(WasmModuleVerifyTest, FunctionBodies_count_mismatch2) {
1210 static const byte data[] = { 1180 static const byte data[] = {
1211 SIGNATURES_SECTION(1, SIG_ENTRY_v_v), // -- 1181 SIGNATURES_SECTION(1, SIG_ENTRY_v_v), // --
1212 FUNCTION_SIGNATURES_SECTION(1, 0), // -- 1182 FUNCTION_SIGNATURES_SECTION(1, 0), // --
1213 SECTION(FUNCTION_BODIES, 1 + 2 * SIZEOF_NOP_BODY), // -- 1183 SECTION(Code, 1 + 2 * SIZEOF_NOP_BODY), // --
1214 2, // -- 1184 ENTRY_COUNT(2), // --
1215 NOP_BODY, // -- 1185 NOP_BODY, // --
1216 NOP_BODY // -- 1186 NOP_BODY // --
1217 }; 1187 };
1218 EXPECT_FAILURE(data); 1188 EXPECT_FAILURE(data);
1219 } 1189 }
1220 1190
1221 TEST_F(WasmModuleVerifyTest, Names_empty) { 1191 TEST_F(WasmModuleVerifyTest, Names_empty) {
1222 static const byte data[] = { 1192 static const byte data[] = {
1223 EMPTY_SIGNATURES_SECTION, EMPTY_FUNCTION_SIGNATURES_SECTION, 1193 EMPTY_SIGNATURES_SECTION, EMPTY_FUNCTION_SIGNATURES_SECTION,
1224 EMPTY_FUNCTION_BODIES_SECTION, EMPTY_NAMES_SECTION}; 1194 EMPTY_FUNCTION_BODIES_SECTION, EMPTY_NAMES_SECTION};
1225 EXPECT_VERIFIES(data); 1195 EXPECT_VERIFIES(data);
1226 } 1196 }
1227 1197
1228 TEST_F(WasmModuleVerifyTest, Names_one_empty) { 1198 TEST_F(WasmModuleVerifyTest, Names_one_empty) {
1229 static const byte data[] = { 1199 static const byte data[] = {
1230 SIGNATURES_SECTION(1, SIG_ENTRY_v_v), // -- 1200 SIGNATURES_SECTION(1, SIG_ENTRY_v_v), // --
1231 FUNCTION_SIGNATURES_SECTION(1, 0), // -- 1201 FUNCTION_SIGNATURES_SECTION(1, 0), // --
1232 SECTION(FUNCTION_BODIES, 1 + SIZEOF_EMPTY_BODY), 1202 SECTION(Code, 1 + SIZEOF_EMPTY_BODY),
1233 1, 1203 ENTRY_COUNT(1),
1234 EMPTY_BODY, // -- 1204 EMPTY_BODY, // --
1235 SECTION(NAMES, 1 + 5), 1205 SECTION_NAMES(1 + 5),
1236 1, 1206 ENTRY_COUNT(1),
1237 FOO_STRING, 1207 FOO_STRING,
1238 NO_LOCAL_NAMES // -- 1208 NO_LOCAL_NAMES // --
1239 }; 1209 };
1240 EXPECT_VERIFIES(data); 1210 EXPECT_VERIFIES(data);
1241 } 1211 }
1242 1212
1243 TEST_F(WasmModuleVerifyTest, Names_two_empty) { 1213 TEST_F(WasmModuleVerifyTest, Names_two_empty) {
1244 static const byte data[] = { 1214 static const byte data[] = {
1245 SIGNATURES_SECTION(1, SIG_ENTRY_v_v), // -- 1215 SIGNATURES_SECTION(1, SIG_ENTRY_v_v), // --
1246 FUNCTION_SIGNATURES_SECTION(2, 0, 0), // -- 1216 FUNCTION_SIGNATURES_SECTION(2, 0, 0), // --
1247 SECTION(FUNCTION_BODIES, 1 + 2 * SIZEOF_EMPTY_BODY), // -- 1217 SECTION(Code, 1 + 2 * SIZEOF_EMPTY_BODY), // --
1248 2, 1218 ENTRY_COUNT(2),
1249 EMPTY_BODY, 1219 EMPTY_BODY,
1250 EMPTY_BODY, // -- 1220 EMPTY_BODY, // --
1251 SECTION(NAMES, 1 + 10), 1221 SECTION_NAMES(1 + 10),
1252 2, // -- 1222 ENTRY_COUNT(2), // --
1253 FOO_STRING, 1223 FOO_STRING,
1254 NO_LOCAL_NAMES, // -- 1224 NO_LOCAL_NAMES, // --
1255 FOO_STRING, 1225 FOO_STRING,
1256 NO_LOCAL_NAMES, // -- 1226 NO_LOCAL_NAMES, // --
1257 }; 1227 };
1258 EXPECT_VERIFIES(data); 1228 EXPECT_VERIFIES(data);
1259 } 1229 }
1260 1230
1231 #define EXPECT_INIT_EXPR(Type, type, value, ...) \
1232 { \
1233 static const byte data[] = {__VA_ARGS__, kExprEnd}; \
1234 WasmInitExpr expr = \
1235 DecodeWasmInitExprForTesting(data, data + sizeof(data)); \
1236 EXPECT_EQ(WasmInitExpr::k##Type##Const, expr.kind); \
1237 EXPECT_EQ(value, expr.val.type##_const); \
1238 }
1239
1240 TEST_F(WasmModuleVerifyTest, InitExpr_i32) {
1241 EXPECT_INIT_EXPR(I32, i32, 33, WASM_I32V_1(33));
1242 EXPECT_INIT_EXPR(I32, i32, -21, WASM_I32V_1(-21));
1243 EXPECT_INIT_EXPR(I32, i32, 437, WASM_I32V_2(437));
1244 EXPECT_INIT_EXPR(I32, i32, 77777, WASM_I32V_3(77777));
1245 }
1246
1247 TEST_F(WasmModuleVerifyTest, InitExpr_f32) {
1248 EXPECT_INIT_EXPR(F32, f32, static_cast<float>(13.1), WASM_F32(13.1));
1249 EXPECT_INIT_EXPR(F32, f32, static_cast<float>(-21.1), WASM_F32(-21.1));
1250 EXPECT_INIT_EXPR(F32, f32, static_cast<float>(437.2), WASM_F32(437.2));
1251 EXPECT_INIT_EXPR(F32, f32, static_cast<float>(77777.3), WASM_F32(77777.3));
1252 }
1253
1254 TEST_F(WasmModuleVerifyTest, InitExpr_i64) {
1255 EXPECT_INIT_EXPR(I64, i64, 33, WASM_I64V_1(33));
1256 EXPECT_INIT_EXPR(I64, i64, -21, WASM_I64V_2(-21));
1257 EXPECT_INIT_EXPR(I64, i64, 437, WASM_I64V_5(437));
1258 EXPECT_INIT_EXPR(I64, i64, 77777, WASM_I64V_7(77777));
1259 }
1260
1261 TEST_F(WasmModuleVerifyTest, InitExpr_f64) {
1262 EXPECT_INIT_EXPR(F64, f64, 83.22, WASM_F64(83.22));
1263 EXPECT_INIT_EXPR(F64, f64, -771.3, WASM_F64(-771.3));
1264 EXPECT_INIT_EXPR(F64, f64, 43703.0, WASM_F64(43703.0));
1265 EXPECT_INIT_EXPR(F64, f64, 77999.1, WASM_F64(77999.1));
1266 }
1267
1268 #define EXPECT_INIT_EXPR_FAIL(...) \
1269 { \
1270 static const byte data[] = {__VA_ARGS__, kExprEnd}; \
1271 WasmInitExpr expr = \
1272 DecodeWasmInitExprForTesting(data, data + sizeof(data)); \
1273 EXPECT_EQ(WasmInitExpr::kNone, expr.kind); \
1274 }
1275
1276 TEST_F(WasmModuleVerifyTest, InitExpr_illegal) {
1277 EXPECT_INIT_EXPR_FAIL(WASM_I32V_1(0), WASM_I32V_1(0));
1278 EXPECT_INIT_EXPR_FAIL(WASM_GET_LOCAL(0));
1279 EXPECT_INIT_EXPR_FAIL(WASM_SET_LOCAL(0, WASM_I32V_1(0)));
1280 EXPECT_INIT_EXPR_FAIL(WASM_I32_ADD(WASM_I32V_1(0), WASM_I32V_1(0)));
1281 EXPECT_INIT_EXPR_FAIL(WASM_IF_ELSE(WASM_ZERO, WASM_ZERO, WASM_ZERO));
1282 }
1283
1284 TEST_F(WasmModuleVerifyTest, InitExpr_global) {
1285 static const byte data[] = {WASM_INIT_EXPR_GLOBAL(37)};
1286 WasmInitExpr expr = DecodeWasmInitExprForTesting(data, data + sizeof(data));
1287 EXPECT_EQ(WasmInitExpr::kGlobalIndex, expr.kind);
1288 EXPECT_EQ(37, expr.val.global_index);
1289 }
1290
1261 } // namespace wasm 1291 } // namespace wasm
1262 } // namespace internal 1292 } // namespace internal
1263 } // namespace v8 1293 } // namespace v8
OLDNEW
« no previous file with comments | « test/unittests/wasm/loop-assignment-analysis-unittest.cc ('k') | test/unittests/wasm/wasm-macro-gen-unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698