OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "test/unittests/test-utils.h" | 5 #include "test/unittests/test-utils.h" |
6 | 6 |
7 #include "src/wasm/module-decoder.h" | 7 #include "src/wasm/module-decoder.h" |
8 #include "src/wasm/wasm-macro-gen.h" | 8 #include "src/wasm/wasm-macro-gen.h" |
9 #include "src/wasm/wasm-opcodes.h" | 9 #include "src/wasm/wasm-opcodes.h" |
10 | 10 |
11 namespace v8 { | 11 namespace v8 { |
12 namespace internal { | 12 namespace internal { |
13 namespace wasm { | 13 namespace wasm { |
14 | 14 |
15 #define EMPTY_FUNCTION(sig_index) 0, SIG_INDEX(sig_index), U16_LE(0) | 15 #define EMPTY_FUNCTION(sig_index) 0, SIG_INDEX(sig_index), U16_LE(0) |
16 #define EMPTY_FUNCTION_SIZE ((size_t)5) | 16 #define EMPTY_FUNCTION_SIZE ((size_t)5) |
17 #define EMPTY_BODY 0 | 17 #define EMPTY_BODY 0 |
18 #define EMPTY_BODY_SIZE ((size_t)1) | 18 #define SIZEOF_EMPTY_BODY ((size_t)1) |
19 #define NOP_BODY 2, 0, kExprNop | 19 #define NOP_BODY 2, 0, kExprNop |
20 #define NOP_BODY_SIZE ((size_t)3) | 20 #define SIZEOF_NOP_BODY ((size_t)3) |
21 #define VOID_VOID_SIG 0, kLocalVoid | |
22 #define VOID_VOID_SIG_SIZE ((size_t)2) | |
23 #define INT_INT_SIG 1, kLocalI32, kLocalI32 | |
24 #define INT_INT_SIG_SIZE ((size_t)3) | |
25 | 21 |
26 // TODO(titzer): tricky size logic! Remove -1. | 22 #define SIG_ENTRY_i_i SIG_ENTRY_x_x(kLocalI32, kLocalI32) |
27 #define SECTION(NAME, EXTRA_SIZE) \ | 23 |
28 U32V_1(WASM_SECTION_##NAME##_SIZE + (EXTRA_SIZE)), WASM_SECTION_##NAME | 24 #define UNKNOWN_EMPTY_SECTION_NAME 1, '\0' |
| 25 #define UNKNOWN_SECTION_NAME 4, 'l', 'u', 'l', 'z' |
| 26 |
| 27 #define SECTION(NAME, EXTRA_SIZE) WASM_SECTION_##NAME, U32V_1(EXTRA_SIZE) |
| 28 |
| 29 #define SIGNATURES_SECTION(count, ...) \ |
| 30 SECTION(SIGNATURES, 1 + 3 * (count)), U32V_1(count), __VA_ARGS__ |
| 31 #define FUNCTION_SIGNATURES_SECTION(count, ...) \ |
| 32 SECTION(FUNCTION_SIGNATURES, 1 + (count)), U32V_1(count), __VA_ARGS__ |
| 33 |
| 34 #define FOO_STRING 3, 'f', 'o', 'o' |
| 35 #define NO_LOCAL_NAMES 0 |
| 36 |
| 37 #define EMPTY_SIGNATURES_SECTION SECTION(SIGNATURES, 1), 0 |
| 38 #define EMPTY_FUNCTION_SIGNATURES_SECTION SECTION(FUNCTION_SIGNATURES, 1), 0 |
| 39 #define EMPTY_FUNCTION_BODIES_SECTION SECTION(FUNCTION_BODIES, 1), 0 |
| 40 #define EMPTY_NAMES_SECTION SECTION(NAMES, 1), 0 |
| 41 |
| 42 #define SIGNATURES_SECTION_VOID_VOID \ |
| 43 SECTION(SIGNATURES, 1 + SIZEOF_SIG_ENTRY_v_v), 1, SIG_ENTRY_v_v |
29 | 44 |
30 #define EXPECT_VERIFIES(data) \ | 45 #define EXPECT_VERIFIES(data) \ |
31 do { \ | 46 do { \ |
32 ModuleResult result = DecodeModule(data, data + arraysize(data)); \ | 47 ModuleResult result = DecodeModule(data, data + arraysize(data)); \ |
33 EXPECT_TRUE(result.ok()); \ | 48 EXPECT_TRUE(result.ok()); \ |
34 if (result.val) delete result.val; \ | 49 if (result.val) delete result.val; \ |
35 } while (false) | 50 } while (false) |
36 | 51 |
37 #define EXPECT_FAILURE_LEN(data, length) \ | 52 #define EXPECT_FAILURE_LEN(data, length) \ |
38 do { \ | 53 do { \ |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 | 198 |
184 TEST_F(WasmModuleVerifyTest, NGlobals) { | 199 TEST_F(WasmModuleVerifyTest, NGlobals) { |
185 static const byte data[] = { | 200 static const byte data[] = { |
186 NO_NAME, // name length | 201 NO_NAME, // name length |
187 kMemI32, // memory type | 202 kMemI32, // memory type |
188 0, // exported | 203 0, // exported |
189 }; | 204 }; |
190 | 205 |
191 for (uint32_t i = 0; i < 1000000; i = i * 13 + 1) { | 206 for (uint32_t i = 0; i < 1000000; i = i * 13 + 1) { |
192 std::vector<byte> buffer; | 207 std::vector<byte> buffer; |
193 size_t size = | 208 size_t size = SizeOfVarInt(i) + i * sizeof(data); |
194 WASM_SECTION_GLOBALS_SIZE + SizeOfVarInt(i) + i * sizeof(data); | 209 const byte globals[] = {WASM_SECTION_GLOBALS, U32V_5(size)}; |
195 const byte globals[] = {U32V_5(size), WASM_SECTION_GLOBALS}; | |
196 for (size_t g = 0; g != sizeof(globals); ++g) { | 210 for (size_t g = 0; g != sizeof(globals); ++g) { |
197 buffer.push_back(globals[g]); | 211 buffer.push_back(globals[g]); |
198 } | 212 } |
199 AppendUint32v(buffer, i); // Number of globals. | 213 AppendUint32v(buffer, i); // Number of globals. |
200 for (uint32_t j = 0; j < i; j++) { | 214 for (uint32_t j = 0; j < i; j++) { |
201 buffer.insert(buffer.end(), data, data + sizeof(data)); | 215 buffer.insert(buffer.end(), data, data + sizeof(data)); |
202 } | 216 } |
203 | 217 |
204 ModuleResult result = DecodeModule(&buffer[0], &buffer[0] + buffer.size()); | 218 ModuleResult result = DecodeModule(&buffer[0], &buffer[0] + buffer.size()); |
205 EXPECT_OK(result); | 219 EXPECT_OK(result); |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
267 | 281 |
268 if (result.val) delete result.val; | 282 if (result.val) delete result.val; |
269 } | 283 } |
270 | 284 |
271 EXPECT_OFF_END_FAILURE(data, 1, sizeof(data)); | 285 EXPECT_OFF_END_FAILURE(data, 1, sizeof(data)); |
272 } | 286 } |
273 | 287 |
274 | 288 |
275 TEST_F(WasmModuleVerifyTest, OneSignature) { | 289 TEST_F(WasmModuleVerifyTest, OneSignature) { |
276 { | 290 { |
277 static const byte data[] = {SECTION(SIGNATURES, 1 + VOID_VOID_SIG_SIZE), 1, | 291 static const byte data[] = {SIGNATURES_SECTION_VOID_VOID}; |
278 VOID_VOID_SIG}; | |
279 EXPECT_VERIFIES(data); | 292 EXPECT_VERIFIES(data); |
280 } | 293 } |
281 | 294 |
282 { | 295 { |
283 static const byte data[] = {SECTION(SIGNATURES, 1 + INT_INT_SIG_SIZE), 1, | 296 static const byte data[] = {SECTION(SIGNATURES, 1 + SIZEOF_SIG_ENTRY_x_x), |
284 INT_INT_SIG}; | 297 1, SIG_ENTRY_i_i}; |
285 EXPECT_VERIFIES(data); | 298 EXPECT_VERIFIES(data); |
286 } | 299 } |
287 } | 300 } |
288 | 301 |
289 | 302 |
290 TEST_F(WasmModuleVerifyTest, MultipleSignatures) { | 303 TEST_F(WasmModuleVerifyTest, MultipleSignatures) { |
291 static const byte data[] = { | 304 static const byte data[] = { |
292 SECTION(SIGNATURES, 10), | 305 SECTION(SIGNATURES, 1 + SIZEOF_SIG_ENTRY_v_v + SIZEOF_SIG_ENTRY_x_x + |
293 3, | 306 SIZEOF_SIG_ENTRY_x_xx), // -- |
294 0, | 307 3, // -- |
295 kLocalVoid, // void -> void | 308 SIG_ENTRY_v_v, // void -> void |
296 1, | 309 SIG_ENTRY_x_x(kLocalI32, kLocalF32), // f32 -> i32 |
297 kLocalI32, | 310 SIG_ENTRY_x_xx(kLocalI32, kLocalF64, kLocalF64), // f64,f64 -> i32 |
298 kLocalF32, // f32 -> i32 | |
299 2, | |
300 kLocalI32, | |
301 kLocalF64, | |
302 kLocalF64, // (f64,f64) -> i32 | |
303 }; | 311 }; |
304 | 312 |
305 ModuleResult result = DecodeModule(data, data + arraysize(data)); | 313 ModuleResult result = DecodeModule(data, data + arraysize(data)); |
306 EXPECT_OK(result); | 314 EXPECT_OK(result); |
307 EXPECT_EQ(3, result.val->signatures.size()); | 315 EXPECT_EQ(3, result.val->signatures.size()); |
308 if (result.val->signatures.size() == 3) { | 316 if (result.val->signatures.size() == 3) { |
309 EXPECT_EQ(0, result.val->signatures[0]->return_count()); | 317 EXPECT_EQ(0, result.val->signatures[0]->return_count()); |
310 EXPECT_EQ(1, result.val->signatures[1]->return_count()); | 318 EXPECT_EQ(1, result.val->signatures[1]->return_count()); |
311 EXPECT_EQ(1, result.val->signatures[2]->return_count()); | 319 EXPECT_EQ(1, result.val->signatures[2]->return_count()); |
312 | 320 |
313 EXPECT_EQ(0, result.val->signatures[0]->parameter_count()); | 321 EXPECT_EQ(0, result.val->signatures[0]->parameter_count()); |
314 EXPECT_EQ(1, result.val->signatures[1]->parameter_count()); | 322 EXPECT_EQ(1, result.val->signatures[1]->parameter_count()); |
315 EXPECT_EQ(2, result.val->signatures[2]->parameter_count()); | 323 EXPECT_EQ(2, result.val->signatures[2]->parameter_count()); |
316 } | 324 } |
317 if (result.val) delete result.val; | 325 if (result.val) delete result.val; |
318 | 326 |
319 EXPECT_OFF_END_FAILURE(data, 1, sizeof(data)); | 327 EXPECT_OFF_END_FAILURE(data, 1, sizeof(data)); |
320 } | 328 } |
321 | 329 |
322 | 330 |
323 TEST_F(WasmModuleVerifyTest, FunctionWithoutSig) { | 331 TEST_F(WasmModuleVerifyTest, FunctionWithoutSig) { |
324 static const byte data[] = { | 332 static const byte data[] = { |
325 SECTION(FUNCTIONS, 25), 1, | 333 SECTION(OLD_FUNCTIONS, 25), 1, |
326 // func#0 ------------------------------------------------------ | 334 // func#0 ------------------------------------------------------ |
327 SIG_INDEX(0), // signature index | 335 SIG_INDEX(0), // signature index |
328 NO_NAME, // name length | 336 NO_NAME, // name length |
329 U32_LE(0), // code start offset | 337 U32_LE(0), // code start offset |
330 U32_LE(0), // code end offset | 338 U32_LE(0), // code end offset |
331 U16_LE(899), // local int32 count | 339 U16_LE(899), // local int32 count |
332 U16_LE(799), // local int64 count | 340 U16_LE(799), // local int64 count |
333 U16_LE(699), // local float32 count | 341 U16_LE(699), // local float32 count |
334 U16_LE(599), // local float64 count | 342 U16_LE(599), // local float64 count |
335 0, // exported | 343 0, // exported |
336 1 // external | 344 1 // external |
337 }; | 345 }; |
338 | 346 |
339 ModuleResult result = DecodeModule(data, data + arraysize(data)); | 347 ModuleResult result = DecodeModule(data, data + arraysize(data)); |
340 EXPECT_FALSE(result.ok()); | 348 EXPECT_FALSE(result.ok()); |
341 if (result.val) delete result.val; | 349 if (result.val) delete result.val; |
342 } | 350 } |
343 | 351 |
344 | 352 |
345 TEST_F(WasmModuleVerifyTest, OneEmptyVoidVoidFunction) { | 353 TEST_F(WasmModuleVerifyTest, OneEmptyVoidVoidFunction) { |
346 const int kCodeStartOffset = 51; | 354 const int kCodeStartOffset = 49; |
347 const int kCodeEndOffset = kCodeStartOffset + 1; | 355 const int kCodeEndOffset = kCodeStartOffset + 1; |
348 | 356 |
349 static const byte data[] = { | 357 static const byte data[] = { |
350 SECTION(SIGNATURES, 1 + VOID_VOID_SIG_SIZE), 1, | 358 // signatures |
351 // sig#0 ------------------------------------------------------- | 359 SIGNATURES_SECTION_VOID_VOID, |
352 VOID_VOID_SIG, | |
353 // func#0 ------------------------------------------------------ | 360 // func#0 ------------------------------------------------------ |
354 SECTION(FUNCTIONS, 18), 1, | 361 SECTION(OLD_FUNCTIONS, 18), 1, |
355 kDeclFunctionLocals | kDeclFunctionExport | kDeclFunctionName, | 362 kDeclFunctionLocals | kDeclFunctionExport | kDeclFunctionName, |
356 SIG_INDEX(0), // signature index | 363 SIG_INDEX(0), // signature index |
357 NAME_LENGTH(2), 'h', 'i', // name | 364 NAME_LENGTH(2), 'h', 'i', // name |
358 U16_LE(1466), // local int32 count | 365 U16_LE(1466), // local int32 count |
359 U16_LE(1355), // local int64 count | 366 U16_LE(1355), // local int64 count |
360 U16_LE(1244), // local float32 count | 367 U16_LE(1244), // local float32 count |
361 U16_LE(1133), // local float64 count | 368 U16_LE(1133), // local float64 count |
362 1, 0, // size | 369 1, 0, // size |
363 kExprNop, | 370 kExprNop, |
364 }; | 371 }; |
365 | 372 |
366 { | 373 { |
367 // Should decode to exactly one function. | 374 // Should decode to exactly one function. |
368 ModuleResult result = DecodeModule(data, data + arraysize(data)); | 375 ModuleResult result = DecodeModule(data, data + arraysize(data)); |
369 EXPECT_OK(result); | 376 EXPECT_OK(result); |
370 EXPECT_EQ(0, result.val->globals.size()); | 377 EXPECT_EQ(0, result.val->globals.size()); |
371 EXPECT_EQ(1, result.val->signatures.size()); | 378 EXPECT_EQ(1, result.val->signatures.size()); |
372 EXPECT_EQ(1, result.val->functions.size()); | 379 EXPECT_EQ(1, result.val->functions.size()); |
373 EXPECT_EQ(0, result.val->data_segments.size()); | 380 EXPECT_EQ(0, result.val->data_segments.size()); |
374 EXPECT_EQ(0, result.val->function_table.size()); | 381 EXPECT_EQ(0, result.val->function_table.size()); |
375 | 382 |
376 WasmFunction* function = &result.val->functions.back(); | 383 WasmFunction* function = &result.val->functions.back(); |
377 | 384 |
378 EXPECT_EQ(39, function->name_offset); | 385 EXPECT_EQ(37, function->name_offset); |
379 EXPECT_EQ(2, function->name_length); | 386 EXPECT_EQ(2, function->name_length); |
380 EXPECT_EQ(kCodeStartOffset, function->code_start_offset); | 387 EXPECT_EQ(kCodeStartOffset, function->code_start_offset); |
381 EXPECT_EQ(kCodeEndOffset, function->code_end_offset); | 388 EXPECT_EQ(kCodeEndOffset, function->code_end_offset); |
382 | 389 |
383 EXPECT_EQ(1466, function->local_i32_count); | 390 EXPECT_EQ(1466, function->local_i32_count); |
384 EXPECT_EQ(1355, function->local_i64_count); | 391 EXPECT_EQ(1355, function->local_i64_count); |
385 EXPECT_EQ(1244, function->local_f32_count); | 392 EXPECT_EQ(1244, function->local_f32_count); |
386 EXPECT_EQ(1133, function->local_f64_count); | 393 EXPECT_EQ(1133, function->local_f64_count); |
387 | 394 |
388 EXPECT_TRUE(function->exported); | 395 EXPECT_TRUE(function->exported); |
389 EXPECT_FALSE(function->external); | 396 EXPECT_FALSE(function->external); |
390 | 397 |
391 if (result.val) delete result.val; | 398 if (result.val) delete result.val; |
392 } | 399 } |
393 | 400 |
394 EXPECT_OFF_END_FAILURE(data, 16, sizeof(data)); | 401 EXPECT_OFF_END_FAILURE(data, 16, sizeof(data)); |
395 } | 402 } |
396 | 403 |
397 | 404 |
398 TEST_F(WasmModuleVerifyTest, OneFunctionImported) { | 405 TEST_F(WasmModuleVerifyTest, OneFunctionImported) { |
399 static const byte data[] = { | 406 static const byte data[] = { |
400 // signatures | 407 // signatures |
401 SECTION(SIGNATURES, 1 + VOID_VOID_SIG_SIZE), 1, | 408 SIGNATURES_SECTION_VOID_VOID, |
402 // sig#0 ------------------------------------------------------- | |
403 VOID_VOID_SIG, | |
404 // functions | 409 // functions |
405 SECTION(FUNCTIONS, 4), 1, | 410 SECTION(OLD_FUNCTIONS, 4), 1, |
406 // func#0 ------------------------------------------------------ | 411 // func#0 ------------------------------------------------------ |
407 kDeclFunctionImport, // no name, no locals, imported | 412 kDeclFunctionImport, // no name, no locals, imported |
408 SIG_INDEX(0), | 413 SIG_INDEX(0), |
409 }; | 414 }; |
410 | 415 |
411 ModuleResult result = DecodeModule(data, data + arraysize(data)); | 416 ModuleResult result = DecodeModule(data, data + arraysize(data)); |
412 EXPECT_OK(result); | 417 EXPECT_OK(result); |
413 EXPECT_EQ(1, result.val->functions.size()); | 418 EXPECT_EQ(1, result.val->functions.size()); |
414 WasmFunction* function = &result.val->functions.back(); | 419 WasmFunction* function = &result.val->functions.back(); |
415 | 420 |
416 EXPECT_EQ(0, function->name_length); | 421 EXPECT_EQ(0, function->name_length); |
417 EXPECT_EQ(0, function->code_start_offset); | 422 EXPECT_EQ(0, function->code_start_offset); |
418 EXPECT_EQ(0, function->code_end_offset); | 423 EXPECT_EQ(0, function->code_end_offset); |
419 | 424 |
420 EXPECT_EQ(0, function->local_i32_count); | 425 EXPECT_EQ(0, function->local_i32_count); |
421 EXPECT_EQ(0, function->local_i64_count); | 426 EXPECT_EQ(0, function->local_i64_count); |
422 EXPECT_EQ(0, function->local_f32_count); | 427 EXPECT_EQ(0, function->local_f32_count); |
423 EXPECT_EQ(0, function->local_f64_count); | 428 EXPECT_EQ(0, function->local_f64_count); |
424 | 429 |
425 EXPECT_FALSE(function->exported); | 430 EXPECT_FALSE(function->exported); |
426 EXPECT_TRUE(function->external); | 431 EXPECT_TRUE(function->external); |
427 | 432 |
428 if (result.val) delete result.val; | 433 if (result.val) delete result.val; |
429 } | 434 } |
430 | 435 |
431 TEST_F(WasmModuleVerifyTest, OneFunctionWithNopBody) { | 436 TEST_F(WasmModuleVerifyTest, OneFunctionWithNopBody) { |
432 static const byte kCodeStartOffset = 40; | 437 static const byte kCodeStartOffset = 38; |
433 static const byte kCodeEndOffset = kCodeStartOffset + 1; | 438 static const byte kCodeEndOffset = kCodeStartOffset + 1; |
434 | 439 |
435 static const byte data[] = { | 440 static const byte data[] = { |
436 SECTION(SIGNATURES, 3), 1, | 441 SIGNATURES_SECTION_VOID_VOID, // -- |
437 // sig#0 ------------------------------------------------------- | 442 SECTION(OLD_FUNCTIONS, 7), 1, |
438 0, 0, // void -> void | |
439 SECTION(FUNCTIONS, 7), 1, | |
440 // func#0 ------------------------------------------------------ | 443 // func#0 ------------------------------------------------------ |
441 0, // no name, no locals | 444 0, // no name, no locals |
442 0, 0, // signature index | 445 0, 0, // signature index |
443 1, 0, // body size | 446 1, 0, // body size |
444 kExprNop // body | 447 kExprNop // body |
445 }; | 448 }; |
446 | 449 |
447 ModuleResult result = DecodeModule(data, data + arraysize(data)); | 450 ModuleResult result = DecodeModule(data, data + arraysize(data)); |
448 EXPECT_OK(result); | 451 EXPECT_OK(result); |
449 EXPECT_EQ(1, result.val->functions.size()); | 452 EXPECT_EQ(1, result.val->functions.size()); |
450 WasmFunction* function = &result.val->functions.back(); | 453 WasmFunction* function = &result.val->functions.back(); |
451 | 454 |
452 EXPECT_EQ(0, function->name_length); | 455 EXPECT_EQ(0, function->name_length); |
453 EXPECT_EQ(kCodeStartOffset, function->code_start_offset); | 456 EXPECT_EQ(kCodeStartOffset, function->code_start_offset); |
454 EXPECT_EQ(kCodeEndOffset, function->code_end_offset); | 457 EXPECT_EQ(kCodeEndOffset, function->code_end_offset); |
455 | 458 |
456 EXPECT_EQ(0, function->local_i32_count); | 459 EXPECT_EQ(0, function->local_i32_count); |
457 EXPECT_EQ(0, function->local_i64_count); | 460 EXPECT_EQ(0, function->local_i64_count); |
458 EXPECT_EQ(0, function->local_f32_count); | 461 EXPECT_EQ(0, function->local_f32_count); |
459 EXPECT_EQ(0, function->local_f64_count); | 462 EXPECT_EQ(0, function->local_f64_count); |
460 | 463 |
461 EXPECT_FALSE(function->exported); | 464 EXPECT_FALSE(function->exported); |
462 EXPECT_FALSE(function->external); | 465 EXPECT_FALSE(function->external); |
463 | 466 |
464 if (result.val) delete result.val; | 467 if (result.val) delete result.val; |
465 } | 468 } |
466 | 469 |
467 | 470 |
468 TEST_F(WasmModuleVerifyTest, OneFunctionWithNopBody_WithLocals) { | 471 TEST_F(WasmModuleVerifyTest, OneFunctionWithNopBody_WithLocals) { |
469 static const byte kCodeStartOffset = 48; | 472 static const byte kCodeStartOffset = 46; |
470 static const byte kCodeEndOffset = kCodeStartOffset + 1; | 473 static const byte kCodeEndOffset = kCodeStartOffset + 1; |
471 | 474 |
472 static const byte data[] = { | 475 static const byte data[] = { |
473 SECTION(SIGNATURES, 3), 1, | 476 SIGNATURES_SECTION_VOID_VOID, // -- |
474 // sig#0 ------------------------------------------------------- | 477 SECTION(OLD_FUNCTIONS, 15), 1, |
475 0, 0, // void -> void | |
476 SECTION(FUNCTIONS, 15), 1, | |
477 // func#0 ------------------------------------------------------ | 478 // func#0 ------------------------------------------------------ |
478 kDeclFunctionLocals, 0, 0, // signature index | 479 kDeclFunctionLocals, 0, 0, // signature index |
479 1, 2, // local int32 count | 480 1, 2, // local int32 count |
480 3, 4, // local int64 count | 481 3, 4, // local int64 count |
481 5, 6, // local float32 count | 482 5, 6, // local float32 count |
482 7, 8, // local float64 count | 483 7, 8, // local float64 count |
483 1, 0, // body size | 484 1, 0, // body size |
484 kExprNop // body | 485 kExprNop // body |
485 }; | 486 }; |
486 | 487 |
(...skipping 12 matching lines...) Expand all Loading... |
499 EXPECT_EQ(2055, function->local_f64_count); | 500 EXPECT_EQ(2055, function->local_f64_count); |
500 | 501 |
501 EXPECT_FALSE(function->exported); | 502 EXPECT_FALSE(function->exported); |
502 EXPECT_FALSE(function->external); | 503 EXPECT_FALSE(function->external); |
503 | 504 |
504 if (result.val) delete result.val; | 505 if (result.val) delete result.val; |
505 } | 506 } |
506 | 507 |
507 | 508 |
508 TEST_F(WasmModuleVerifyTest, OneGlobalOneFunctionWithNopBodyOneDataSegment) { | 509 TEST_F(WasmModuleVerifyTest, OneGlobalOneFunctionWithNopBodyOneDataSegment) { |
509 static const byte kCodeStartOffset = 64; | 510 static const byte kNameOffset = 49; |
| 511 static const byte kCodeStartOffset = 61; |
510 static const byte kCodeEndOffset = kCodeStartOffset + 3; | 512 static const byte kCodeEndOffset = kCodeStartOffset + 3; |
511 static const byte kDataSegmentSourceOffset = kCodeEndOffset + 31; | 513 static const byte kDataSegmentSourceOffset = kCodeEndOffset + 22; |
512 | 514 |
513 static const byte data[] = { | 515 static const byte data[] = { |
514 // global#0 -------------------------------------------------- | 516 // global#0 -------------------------------------------------- |
515 SECTION(GLOBALS, 4), 1, | 517 SECTION(GLOBALS, 4), 1, |
516 0, // name length | 518 0, // name length |
517 kMemU8, // memory type | 519 kMemU8, // memory type |
518 0, // exported | 520 0, // exported |
519 // sig#0 ----------------------------------------------------- | 521 // sig#0 ----------------------------------------------------- |
520 SECTION(SIGNATURES, 3), 1, 0, 0, // void -> void | 522 SIGNATURES_SECTION_VOID_VOID, |
521 // func#0 ---------------------------------------------------- | 523 // func#0 ---------------------------------------------------- |
522 SECTION(FUNCTIONS, 20), 1, | 524 SECTION(OLD_FUNCTIONS, 20), 1, |
523 kDeclFunctionLocals | kDeclFunctionName, // -- | 525 kDeclFunctionLocals | kDeclFunctionName, // -- |
524 SIG_INDEX(0), // signature index | 526 SIG_INDEX(0), // signature index |
525 2, 'h', 'i', // name | 527 2, 'h', 'i', // name |
526 1, 2, // local int32 count | 528 1, 2, // local int32 count |
527 3, 4, // local int64 count | 529 3, 4, // local int64 count |
528 5, 6, // local float32 count | 530 5, 6, // local float32 count |
529 7, 8, // local float64 count | 531 7, 8, // local float64 count |
530 3, 0, // body size | 532 3, 0, // body size |
531 kExprNop, // func#0 body | 533 kExprNop, // func#0 body |
532 kExprNop, // func#0 body | 534 kExprNop, // func#0 body |
(...skipping 18 matching lines...) Expand all Loading... |
551 | 553 |
552 WasmGlobal* global = &result.val->globals.back(); | 554 WasmGlobal* global = &result.val->globals.back(); |
553 | 555 |
554 EXPECT_EQ(0, global->name_length); | 556 EXPECT_EQ(0, global->name_length); |
555 EXPECT_EQ(MachineType::Uint8(), global->type); | 557 EXPECT_EQ(MachineType::Uint8(), global->type); |
556 EXPECT_EQ(0, global->offset); | 558 EXPECT_EQ(0, global->offset); |
557 EXPECT_FALSE(global->exported); | 559 EXPECT_FALSE(global->exported); |
558 | 560 |
559 WasmFunction* function = &result.val->functions.back(); | 561 WasmFunction* function = &result.val->functions.back(); |
560 | 562 |
561 EXPECT_EQ(52, function->name_offset); | 563 EXPECT_EQ(kNameOffset, function->name_offset); |
562 EXPECT_EQ(2, function->name_length); | 564 EXPECT_EQ(2, function->name_length); |
563 EXPECT_EQ(kCodeStartOffset, function->code_start_offset); | 565 EXPECT_EQ(kCodeStartOffset, function->code_start_offset); |
564 EXPECT_EQ(kCodeEndOffset, function->code_end_offset); | 566 EXPECT_EQ(kCodeEndOffset, function->code_end_offset); |
565 | 567 |
566 EXPECT_FALSE(function->exported); | 568 EXPECT_FALSE(function->exported); |
567 EXPECT_FALSE(function->external); | 569 EXPECT_FALSE(function->external); |
568 | 570 |
569 WasmDataSegment* segment = &result.val->data_segments.back(); | 571 WasmDataSegment* segment = &result.val->data_segments.back(); |
570 | 572 |
571 EXPECT_EQ(0x8b3ae, segment->dest_addr); | 573 EXPECT_EQ(0x8b3ae, segment->dest_addr); |
572 EXPECT_EQ(kDataSegmentSourceOffset, segment->source_offset); | 574 EXPECT_EQ(kDataSegmentSourceOffset, segment->source_offset); |
573 EXPECT_EQ(5, segment->source_size); | 575 EXPECT_EQ(5, segment->source_size); |
574 EXPECT_TRUE(segment->init); | 576 EXPECT_TRUE(segment->init); |
575 | 577 |
576 if (result.val) delete result.val; | 578 if (result.val) delete result.val; |
577 } | 579 } |
578 } | 580 } |
579 | 581 |
580 | 582 |
581 TEST_F(WasmModuleVerifyTest, OneDataSegment) { | 583 TEST_F(WasmModuleVerifyTest, OneDataSegment) { |
582 const byte kDataSegmentSourceOffset = 39; | 584 const byte kDataSegmentSourceOffset = 30; |
583 const byte data[] = { | 585 const byte data[] = { |
584 SECTION(MEMORY, 3), | 586 SECTION(MEMORY, 3), |
585 28, | 587 28, |
586 28, | 588 28, |
587 1, | 589 1, |
588 SECTION(DATA_SEGMENTS, 8), | 590 SECTION(DATA_SEGMENTS, 8), |
589 1, | 591 1, |
590 U32V_3(0x9bbaa), // dest addr | 592 U32V_3(0x9bbaa), // dest addr |
591 U32V_1(3), // source size | 593 U32V_1(3), // source size |
592 'a', | 594 'a', |
(...skipping 17 matching lines...) Expand all Loading... |
610 EXPECT_TRUE(segment->init); | 612 EXPECT_TRUE(segment->init); |
611 | 613 |
612 if (result.val) delete result.val; | 614 if (result.val) delete result.val; |
613 } | 615 } |
614 | 616 |
615 EXPECT_OFF_END_FAILURE(data, 13, sizeof(data)); | 617 EXPECT_OFF_END_FAILURE(data, 13, sizeof(data)); |
616 } | 618 } |
617 | 619 |
618 | 620 |
619 TEST_F(WasmModuleVerifyTest, TwoDataSegments) { | 621 TEST_F(WasmModuleVerifyTest, TwoDataSegments) { |
620 const byte kDataSegment0SourceOffset = 39; | 622 const byte kDataSegment0SourceOffset = 30; |
621 const byte kDataSegment1SourceOffset = 39 + 8; | 623 const byte kDataSegment1SourceOffset = 30 + 8; |
622 | 624 |
623 const byte data[] = { | 625 const byte data[] = { |
624 SECTION(MEMORY, 3), | 626 SECTION(MEMORY, 3), |
625 28, | 627 28, |
626 28, | 628 28, |
627 1, | 629 1, |
628 SECTION(DATA_SEGMENTS, 23), | 630 SECTION(DATA_SEGMENTS, 23), |
629 2, // segment count | 631 2, // segment count |
630 U32V_3(0x7ffee), // #0: dest addr | 632 U32V_3(0x7ffee), // #0: dest addr |
631 U32V_1(4), // source size | 633 U32V_1(4), // source size |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
702 } | 704 } |
703 } | 705 } |
704 | 706 |
705 | 707 |
706 // To make below tests for indirect calls much shorter. | 708 // To make below tests for indirect calls much shorter. |
707 #define FUNCTION(sig_index, external) kDeclFunctionImport, SIG_INDEX(sig_index) | 709 #define FUNCTION(sig_index, external) kDeclFunctionImport, SIG_INDEX(sig_index) |
708 | 710 |
709 TEST_F(WasmModuleVerifyTest, OneIndirectFunction) { | 711 TEST_F(WasmModuleVerifyTest, OneIndirectFunction) { |
710 static const byte data[] = { | 712 static const byte data[] = { |
711 // sig#0 ------------------------------------------------------- | 713 // sig#0 ------------------------------------------------------- |
712 SECTION(SIGNATURES, 3), 1, 0, 0, // void -> void | 714 SIGNATURES_SECTION_VOID_VOID, |
713 // func#0 ------------------------------------------------------ | 715 // func#0 ------------------------------------------------------ |
714 SECTION(FUNCTIONS, 4), 1, FUNCTION(0, 0), | 716 SECTION(OLD_FUNCTIONS, 4), 1, FUNCTION(0, 0), |
715 // indirect table ---------------------------------------------- | 717 // indirect table ---------------------------------------------- |
716 SECTION(FUNCTION_TABLE, 2), 1, U32V_1(0)}; | 718 SECTION(FUNCTION_TABLE, 2), 1, U32V_1(0)}; |
717 | 719 |
718 ModuleResult result = DecodeModule(data, data + arraysize(data)); | 720 ModuleResult result = DecodeModule(data, data + arraysize(data)); |
719 EXPECT_OK(result); | 721 EXPECT_OK(result); |
720 if (result.ok()) { | 722 if (result.ok()) { |
721 EXPECT_EQ(1, result.val->signatures.size()); | 723 EXPECT_EQ(1, result.val->signatures.size()); |
722 EXPECT_EQ(1, result.val->functions.size()); | 724 EXPECT_EQ(1, result.val->functions.size()); |
723 EXPECT_EQ(1, result.val->function_table.size()); | 725 EXPECT_EQ(1, result.val->function_table.size()); |
724 EXPECT_EQ(0, result.val->function_table[0]); | 726 EXPECT_EQ(0, result.val->function_table[0]); |
725 } | 727 } |
726 if (result.val) delete result.val; | 728 if (result.val) delete result.val; |
727 } | 729 } |
728 | 730 |
729 | 731 |
730 TEST_F(WasmModuleVerifyTest, MultipleIndirectFunctions) { | 732 TEST_F(WasmModuleVerifyTest, MultipleIndirectFunctions) { |
731 static const byte data[] = { | 733 static const byte data[] = { |
732 // sig#0 ------------------------------------------------------- | 734 // sig#0 ------------------------------------------------------- |
733 SECTION(SIGNATURES, 5), 2, 0, 0, // void -> void | 735 SECTION(SIGNATURES, 1 + SIZEOF_SIG_ENTRY_v_v + SIZEOF_SIG_ENTRY_v_x), |
734 0, kLocalI32, // void -> i32 | 736 2, // -- |
| 737 SIG_ENTRY_v_v, // void -> void |
| 738 SIG_ENTRY_v_x(kLocalI32), // void -> i32 |
735 // func#0 ------------------------------------------------------ | 739 // func#0 ------------------------------------------------------ |
736 SECTION(FUNCTIONS, 13), 4, FUNCTION(0, 1), // -- | 740 SECTION(OLD_FUNCTIONS, 13), 4, FUNCTION(0, 1), // -- |
737 FUNCTION(1, 1), // -- | 741 FUNCTION(1, 1), // -- |
738 FUNCTION(0, 1), // -- | 742 FUNCTION(0, 1), // -- |
739 FUNCTION(1, 1), // -- | 743 FUNCTION(1, 1), // -- |
740 // indirect table ---------------------------------------------- | 744 // indirect table ---------------------------------------------- |
741 SECTION(FUNCTION_TABLE, 9), 8, | 745 SECTION(FUNCTION_TABLE, 9), 8, |
742 U32V_1(0), // -- | 746 U32V_1(0), // -- |
743 U32V_1(1), // -- | 747 U32V_1(1), // -- |
744 U32V_1(2), // -- | 748 U32V_1(2), // -- |
745 U32V_1(3), // -- | 749 U32V_1(3), // -- |
746 U32V_1(0), // -- | 750 U32V_1(0), // -- |
747 U32V_1(1), // -- | 751 U32V_1(1), // -- |
748 U32V_1(2), // -- | 752 U32V_1(2), // -- |
749 U32V_1(3), // -- | 753 U32V_1(3), // -- |
750 }; | 754 }; |
751 | 755 |
752 ModuleResult result = DecodeModule(data, data + arraysize(data)); | 756 ModuleResult result = DecodeModule(data, data + arraysize(data)); |
753 EXPECT_OK(result); | 757 EXPECT_OK(result); |
754 if (result.ok()) { | 758 if (result.ok()) { |
755 EXPECT_EQ(2, result.val->signatures.size()); | 759 EXPECT_EQ(2, result.val->signatures.size()); |
756 EXPECT_EQ(4, result.val->functions.size()); | 760 EXPECT_EQ(4, result.val->functions.size()); |
757 EXPECT_EQ(8, result.val->function_table.size()); | 761 EXPECT_EQ(8, result.val->function_table.size()); |
758 for (int i = 0; i < 8; i++) { | 762 for (int i = 0; i < 8; i++) { |
759 EXPECT_EQ(i & 3, result.val->function_table[i]); | 763 EXPECT_EQ(i & 3, result.val->function_table[i]); |
760 } | 764 } |
761 } | 765 } |
762 if (result.val) delete result.val; | 766 if (result.val) delete result.val; |
763 } | 767 } |
764 | 768 |
765 | 769 |
766 TEST_F(WasmModuleVerifyTest, IndirectFunctionNoFunctions) { | 770 TEST_F(WasmModuleVerifyTest, IndirectFunctionNoFunctions) { |
767 static const byte data[] = { | 771 static const byte data[] = { |
768 // sig#0 ------------------------------------------------------- | 772 // sig#0 ------------------------------------------------------- |
769 SECTION(SIGNATURES, 3), 1, 0, 0, // void -> void | 773 SIGNATURES_SECTION_VOID_VOID, |
770 // indirect table ---------------------------------------------- | 774 // indirect table ---------------------------------------------- |
771 SECTION(FUNCTION_TABLE, 3), 1, 0, 0, | 775 SECTION(FUNCTION_TABLE, 3), 1, 0, 0, |
772 }; | 776 }; |
773 | 777 |
774 EXPECT_FAILURE(data); | 778 EXPECT_FAILURE(data); |
775 } | 779 } |
776 | 780 |
777 | 781 |
778 TEST_F(WasmModuleVerifyTest, IndirectFunctionInvalidIndex) { | 782 TEST_F(WasmModuleVerifyTest, IndirectFunctionInvalidIndex) { |
779 static const byte data[] = { | 783 static const byte data[] = { |
780 // sig#0 ------------------------------------------------------- | 784 // sig#0 ------------------------------------------------------- |
781 SECTION(SIGNATURES, 3), 1, 0, 0, // void -> void | 785 SIGNATURES_SECTION_VOID_VOID, |
782 // functions --------------------------------------------------- | 786 // functions --------------------------------------------------- |
783 SECTION(FUNCTIONS, 4), 1, FUNCTION(0, 1), | 787 SECTION(OLD_FUNCTIONS, 4), 1, FUNCTION(0, 1), |
784 // indirect table ---------------------------------------------- | 788 // indirect table ---------------------------------------------- |
785 SECTION(FUNCTION_TABLE, 3), 1, 1, 0, | 789 SECTION(FUNCTION_TABLE, 3), 1, 1, 0, |
786 }; | 790 }; |
787 | 791 |
788 EXPECT_FAILURE(data); | 792 EXPECT_FAILURE(data); |
789 } | 793 } |
790 | 794 |
791 | 795 |
792 class WasmSignatureDecodeTest : public TestWithZone {}; | 796 class WasmSignatureDecodeTest : public TestWithZone {}; |
793 | 797 |
794 | 798 |
795 TEST_F(WasmSignatureDecodeTest, Ok_v_v) { | 799 TEST_F(WasmSignatureDecodeTest, Ok_v_v) { |
796 static const byte data[] = {0, 0}; | 800 static const byte data[] = {SIG_ENTRY_v_v}; |
797 base::AccountingAllocator allocator; | 801 base::AccountingAllocator allocator; |
798 Zone zone(&allocator); | 802 Zone zone(&allocator); |
799 FunctionSig* sig = | 803 FunctionSig* sig = |
800 DecodeWasmSignatureForTesting(&zone, data, data + arraysize(data)); | 804 DecodeWasmSignatureForTesting(&zone, data, data + arraysize(data)); |
801 | 805 |
802 EXPECT_TRUE(sig != nullptr); | 806 EXPECT_TRUE(sig != nullptr); |
803 EXPECT_EQ(0, sig->parameter_count()); | 807 EXPECT_EQ(0, sig->parameter_count()); |
804 EXPECT_EQ(0, sig->return_count()); | 808 EXPECT_EQ(0, sig->return_count()); |
805 } | 809 } |
806 | 810 |
807 | 811 |
808 TEST_F(WasmSignatureDecodeTest, Ok_t_v) { | 812 TEST_F(WasmSignatureDecodeTest, Ok_t_v) { |
809 for (size_t i = 0; i < arraysize(kLocalTypes); i++) { | 813 for (size_t i = 0; i < arraysize(kLocalTypes); i++) { |
810 LocalTypePair ret_type = kLocalTypes[i]; | 814 LocalTypePair ret_type = kLocalTypes[i]; |
811 const byte data[] = {0, ret_type.code}; | 815 const byte data[] = {SIG_ENTRY_x(ret_type.code)}; |
812 FunctionSig* sig = | 816 FunctionSig* sig = |
813 DecodeWasmSignatureForTesting(zone(), data, data + arraysize(data)); | 817 DecodeWasmSignatureForTesting(zone(), data, data + arraysize(data)); |
814 | 818 |
815 EXPECT_TRUE(sig != nullptr); | 819 EXPECT_TRUE(sig != nullptr); |
816 EXPECT_EQ(0, sig->parameter_count()); | 820 EXPECT_EQ(0, sig->parameter_count()); |
817 EXPECT_EQ(1, sig->return_count()); | 821 EXPECT_EQ(1, sig->return_count()); |
818 EXPECT_EQ(ret_type.type, sig->GetReturn()); | 822 EXPECT_EQ(ret_type.type, sig->GetReturn()); |
819 } | 823 } |
820 } | 824 } |
821 | 825 |
822 | 826 |
823 TEST_F(WasmSignatureDecodeTest, Ok_v_t) { | 827 TEST_F(WasmSignatureDecodeTest, Ok_v_t) { |
824 for (size_t i = 0; i < arraysize(kLocalTypes); i++) { | 828 for (size_t i = 0; i < arraysize(kLocalTypes); i++) { |
825 LocalTypePair param_type = kLocalTypes[i]; | 829 LocalTypePair param_type = kLocalTypes[i]; |
826 const byte data[] = {1, 0, param_type.code}; | 830 const byte data[] = {SIG_ENTRY_v_x(param_type.code)}; |
827 FunctionSig* sig = | 831 FunctionSig* sig = |
828 DecodeWasmSignatureForTesting(zone(), data, data + arraysize(data)); | 832 DecodeWasmSignatureForTesting(zone(), data, data + arraysize(data)); |
829 | 833 |
830 EXPECT_TRUE(sig != nullptr); | 834 EXPECT_TRUE(sig != nullptr); |
831 EXPECT_EQ(1, sig->parameter_count()); | 835 EXPECT_EQ(1, sig->parameter_count()); |
832 EXPECT_EQ(0, sig->return_count()); | 836 EXPECT_EQ(0, sig->return_count()); |
833 EXPECT_EQ(param_type.type, sig->GetParam(0)); | 837 EXPECT_EQ(param_type.type, sig->GetParam(0)); |
834 } | 838 } |
835 } | 839 } |
836 | 840 |
837 | 841 |
838 TEST_F(WasmSignatureDecodeTest, Ok_t_t) { | 842 TEST_F(WasmSignatureDecodeTest, Ok_t_t) { |
839 for (size_t i = 0; i < arraysize(kLocalTypes); i++) { | 843 for (size_t i = 0; i < arraysize(kLocalTypes); i++) { |
840 LocalTypePair ret_type = kLocalTypes[i]; | 844 LocalTypePair ret_type = kLocalTypes[i]; |
841 for (size_t j = 0; j < arraysize(kLocalTypes); j++) { | 845 for (size_t j = 0; j < arraysize(kLocalTypes); j++) { |
842 LocalTypePair param_type = kLocalTypes[j]; | 846 LocalTypePair param_type = kLocalTypes[j]; |
843 const byte data[] = {1, // param count | 847 const byte data[] = {SIG_ENTRY_x_x(ret_type.code, param_type.code)}; |
844 ret_type.code, // ret | |
845 param_type.code}; // param | |
846 FunctionSig* sig = | 848 FunctionSig* sig = |
847 DecodeWasmSignatureForTesting(zone(), data, data + arraysize(data)); | 849 DecodeWasmSignatureForTesting(zone(), data, data + arraysize(data)); |
848 | 850 |
849 EXPECT_TRUE(sig != nullptr); | 851 EXPECT_TRUE(sig != nullptr); |
850 EXPECT_EQ(1, sig->parameter_count()); | 852 EXPECT_EQ(1, sig->parameter_count()); |
851 EXPECT_EQ(1, sig->return_count()); | 853 EXPECT_EQ(1, sig->return_count()); |
852 EXPECT_EQ(param_type.type, sig->GetParam(0)); | 854 EXPECT_EQ(param_type.type, sig->GetParam(0)); |
853 EXPECT_EQ(ret_type.type, sig->GetReturn()); | 855 EXPECT_EQ(ret_type.type, sig->GetReturn()); |
854 } | 856 } |
855 } | 857 } |
856 } | 858 } |
857 | 859 |
858 | 860 |
859 TEST_F(WasmSignatureDecodeTest, Ok_i_tt) { | 861 TEST_F(WasmSignatureDecodeTest, Ok_i_tt) { |
860 for (size_t i = 0; i < arraysize(kLocalTypes); i++) { | 862 for (size_t i = 0; i < arraysize(kLocalTypes); i++) { |
861 LocalTypePair p0_type = kLocalTypes[i]; | 863 LocalTypePair p0_type = kLocalTypes[i]; |
862 for (size_t j = 0; j < arraysize(kLocalTypes); j++) { | 864 for (size_t j = 0; j < arraysize(kLocalTypes); j++) { |
863 LocalTypePair p1_type = kLocalTypes[j]; | 865 LocalTypePair p1_type = kLocalTypes[j]; |
864 const byte data[] = {2, // param count | 866 const byte data[] = { |
865 kLocalI32, // ret | 867 SIG_ENTRY_x_xx(kLocalI32, p0_type.code, p1_type.code)}; |
866 p0_type.code, // p0 | |
867 p1_type.code}; // p1 | |
868 FunctionSig* sig = | 868 FunctionSig* sig = |
869 DecodeWasmSignatureForTesting(zone(), data, data + arraysize(data)); | 869 DecodeWasmSignatureForTesting(zone(), data, data + arraysize(data)); |
870 | 870 |
871 EXPECT_TRUE(sig != nullptr); | 871 EXPECT_TRUE(sig != nullptr); |
872 EXPECT_EQ(2, sig->parameter_count()); | 872 EXPECT_EQ(2, sig->parameter_count()); |
873 EXPECT_EQ(1, sig->return_count()); | 873 EXPECT_EQ(1, sig->return_count()); |
874 EXPECT_EQ(p0_type.type, sig->GetParam(0)); | 874 EXPECT_EQ(p0_type.type, sig->GetParam(0)); |
875 EXPECT_EQ(p1_type.type, sig->GetParam(1)); | 875 EXPECT_EQ(p1_type.type, sig->GetParam(1)); |
876 } | 876 } |
877 } | 877 } |
(...skipping 10 matching lines...) Expand all Loading... |
888 // Should fall off the end for all signatures. | 888 // Should fall off the end for all signatures. |
889 FunctionSig* sig = DecodeWasmSignatureForTesting(zone(), data, data + i); | 889 FunctionSig* sig = DecodeWasmSignatureForTesting(zone(), data, data + i); |
890 EXPECT_EQ(nullptr, sig); | 890 EXPECT_EQ(nullptr, sig); |
891 } | 891 } |
892 } | 892 } |
893 } | 893 } |
894 | 894 |
895 | 895 |
896 TEST_F(WasmSignatureDecodeTest, Fail_invalid_type) { | 896 TEST_F(WasmSignatureDecodeTest, Fail_invalid_type) { |
897 byte kInvalidType = 76; | 897 byte kInvalidType = 76; |
898 for (int i = 1; i < 3; i++) { | 898 for (size_t i = 0; i < SIZEOF_SIG_ENTRY_x_xx; i++) { |
899 byte data[] = {2, kLocalI32, kLocalI32, kLocalI32}; | 899 byte data[] = {SIG_ENTRY_x_xx(kLocalI32, kLocalI32, kLocalI32)}; |
900 data[i] = kInvalidType; | 900 data[i] = kInvalidType; |
901 FunctionSig* sig = | 901 FunctionSig* sig = |
902 DecodeWasmSignatureForTesting(zone(), data, data + arraysize(data)); | 902 DecodeWasmSignatureForTesting(zone(), data, data + arraysize(data)); |
903 EXPECT_EQ(nullptr, sig); | 903 EXPECT_EQ(nullptr, sig); |
904 } | 904 } |
905 } | 905 } |
906 | 906 |
| 907 TEST_F(WasmSignatureDecodeTest, Fail_invalid_ret_type1) { |
| 908 static const byte data[] = {SIG_ENTRY_x_x(kLocalVoid, kLocalI32)}; |
| 909 FunctionSig* sig = |
| 910 DecodeWasmSignatureForTesting(zone(), data, data + arraysize(data)); |
| 911 EXPECT_EQ(nullptr, sig); |
| 912 } |
907 | 913 |
908 TEST_F(WasmSignatureDecodeTest, Fail_invalid_param_type) { | 914 TEST_F(WasmSignatureDecodeTest, Fail_invalid_param_type1) { |
909 static const int kParamCount = 3; | 915 static const byte data[] = {SIG_ENTRY_x_x(kLocalI32, kLocalVoid)}; |
910 for (int i = 0; i < kParamCount; i++) { | 916 FunctionSig* sig = |
911 byte data[] = {kParamCount, kLocalI32, kLocalI32, kLocalI32, kLocalI32}; | 917 DecodeWasmSignatureForTesting(zone(), data, data + arraysize(data)); |
912 data[i + 2] = kLocalVoid; | 918 EXPECT_EQ(nullptr, sig); |
913 FunctionSig* sig = | 919 } |
914 DecodeWasmSignatureForTesting(zone(), data, data + arraysize(data)); | 920 |
915 EXPECT_EQ(nullptr, sig); | 921 TEST_F(WasmSignatureDecodeTest, Fail_invalid_param_type2) { |
916 } | 922 static const byte data[] = {SIG_ENTRY_x_xx(kLocalI32, kLocalI32, kLocalVoid)}; |
| 923 FunctionSig* sig = |
| 924 DecodeWasmSignatureForTesting(zone(), data, data + arraysize(data)); |
| 925 EXPECT_EQ(nullptr, sig); |
917 } | 926 } |
918 | 927 |
919 class WasmFunctionVerifyTest : public TestWithIsolateAndZone {}; | 928 class WasmFunctionVerifyTest : public TestWithIsolateAndZone {}; |
920 | 929 |
921 TEST_F(WasmFunctionVerifyTest, Ok_v_v_empty) { | 930 TEST_F(WasmFunctionVerifyTest, Ok_v_v_empty) { |
922 static const byte data[] = { | 931 static const byte data[] = { |
923 0, kLocalVoid, // signature | 932 SIG_ENTRY_v_v, // signature entry |
924 4, // locals | 933 4, // locals |
925 3, kLocalI32, // -- | 934 3, |
926 4, kLocalI64, // -- | 935 kLocalI32, // -- |
927 5, kLocalF32, // -- | 936 4, |
928 6, kLocalF64, // -- | 937 kLocalI64, // -- |
929 kExprNop // body | 938 5, |
| 939 kLocalF32, // -- |
| 940 6, |
| 941 kLocalF64, // -- |
| 942 kExprNop // body |
930 }; | 943 }; |
931 | 944 |
932 FunctionResult result = DecodeWasmFunction(isolate(), zone(), nullptr, data, | 945 FunctionResult result = DecodeWasmFunction(isolate(), zone(), nullptr, data, |
933 data + arraysize(data)); | 946 data + arraysize(data)); |
934 EXPECT_OK(result); | 947 EXPECT_OK(result); |
935 | 948 |
936 if (result.val && result.ok()) { | 949 if (result.val && result.ok()) { |
937 WasmFunction* function = result.val; | 950 WasmFunction* function = result.val; |
938 EXPECT_EQ(0, function->sig->parameter_count()); | 951 EXPECT_EQ(0, function->sig->parameter_count()); |
939 EXPECT_EQ(0, function->sig->return_count()); | 952 EXPECT_EQ(0, function->sig->return_count()); |
940 EXPECT_EQ(0, function->name_offset); | 953 EXPECT_EQ(0, function->name_offset); |
941 EXPECT_EQ(2, function->code_start_offset); | 954 EXPECT_EQ(SIZEOF_SIG_ENTRY_v_v, function->code_start_offset); |
942 EXPECT_EQ(arraysize(data), function->code_end_offset); | 955 EXPECT_EQ(arraysize(data), function->code_end_offset); |
943 // TODO(titzer): verify encoding of local declarations | 956 // TODO(titzer): verify encoding of local declarations |
944 EXPECT_FALSE(function->external); | 957 EXPECT_FALSE(function->external); |
945 EXPECT_FALSE(function->exported); | 958 EXPECT_FALSE(function->exported); |
946 } | 959 } |
947 | 960 |
948 if (result.val) delete result.val; | 961 if (result.val) delete result.val; |
949 } | 962 } |
950 | 963 |
951 TEST_F(WasmModuleVerifyTest, SectionWithoutNameLength) { | 964 TEST_F(WasmModuleVerifyTest, SectionWithoutNameLength) { |
952 const byte data[] = {1}; | 965 const byte data[] = {1}; |
953 EXPECT_FAILURE(data); | 966 EXPECT_FAILURE(data); |
954 } | 967 } |
955 | 968 |
956 TEST_F(WasmModuleVerifyTest, TheLoneliestOfValidModulesTheTrulyEmptyOne) { | 969 TEST_F(WasmModuleVerifyTest, TheLoneliestOfValidModulesTheTrulyEmptyOne) { |
957 const byte data[] = { | 970 const byte data[] = { |
958 1, // Section size. | |
959 0, // Empty section name. | 971 0, // Empty section name. |
960 // No section name, no content, nothing but sadness. | 972 // No section name, no content, nothing but sadness. |
| 973 0, // No section content. |
961 }; | 974 }; |
962 EXPECT_VERIFIES(data); | 975 EXPECT_VERIFIES(data); |
963 } | 976 } |
964 | 977 |
965 TEST_F(WasmModuleVerifyTest, OnlyUnknownSectionEmpty) { | 978 TEST_F(WasmModuleVerifyTest, OnlyUnknownSectionEmpty) { |
966 const byte data[] = { | 979 const byte data[] = { |
967 5, // Section size. | 980 UNKNOWN_SECTION_NAME, 0, |
968 4, 'l', 'u', 'l', 'z', // unknown section. | |
969 }; | 981 }; |
970 EXPECT_VERIFIES(data); | 982 EXPECT_VERIFIES(data); |
971 } | 983 } |
972 | 984 |
973 TEST_F(WasmModuleVerifyTest, OnlyUnknownSectionNonEmpty) { | 985 TEST_F(WasmModuleVerifyTest, OnlyUnknownSectionNonEmpty) { |
974 const byte data[] = { | 986 const byte data[] = { |
975 10, // Section size. | 987 UNKNOWN_SECTION_NAME, |
976 4, 'l', 'u', 'l', 'z', // unknown section. | 988 5, // section size |
977 // Section content: | 989 0xff, |
978 0xff, 0xff, 0xff, 0xff, 0xff, | 990 0xff, |
| 991 0xff, |
| 992 0xff, |
| 993 0xff, // section data |
979 }; | 994 }; |
980 EXPECT_VERIFIES(data); | 995 EXPECT_VERIFIES(data); |
981 } | 996 } |
982 | 997 |
983 TEST_F(WasmModuleVerifyTest, SignatureFollowedByEmptyUnknownSection) { | 998 TEST_F(WasmModuleVerifyTest, SignatureFollowedByEmptyUnknownSection) { |
984 const byte data[] = { | 999 const byte data[] = { |
985 SECTION(SIGNATURES, 1 + VOID_VOID_SIG_SIZE), 1, VOID_VOID_SIG, | 1000 // signatures |
| 1001 SIGNATURES_SECTION_VOID_VOID, |
986 // ----------------------------------------------------------- | 1002 // ----------------------------------------------------------- |
987 5, // Section size. | 1003 UNKNOWN_SECTION_NAME, |
988 4, 'l', 'u', 'l', 'z', // unknown section. | 1004 0 // empty section |
989 }; | 1005 }; |
990 EXPECT_VERIFIES(data); | 1006 EXPECT_VERIFIES(data); |
991 } | 1007 } |
992 | 1008 |
993 TEST_F(WasmModuleVerifyTest, SignatureFollowedByUnknownSection) { | 1009 TEST_F(WasmModuleVerifyTest, SignatureFollowedByUnknownSection) { |
994 const byte data[] = { | 1010 const byte data[] = { |
995 SECTION(SIGNATURES, 1 + VOID_VOID_SIG_SIZE), 1, VOID_VOID_SIG, | 1011 // signatures |
| 1012 SIGNATURES_SECTION_VOID_VOID, |
996 // ----------------------------------------------------------- | 1013 // ----------------------------------------------------------- |
997 10, // Section size. | 1014 UNKNOWN_SECTION_NAME, |
998 4, 'l', 'u', 'l', 'z', // unknown section. | 1015 5, // section size |
999 0xff, 0xff, 0xff, 0xff, 0xff, | 1016 0xff, 0xff, 0xff, 0xff, 0xff, |
1000 }; | 1017 }; |
1001 EXPECT_VERIFIES(data); | 1018 EXPECT_VERIFIES(data); |
1002 } | 1019 } |
1003 | 1020 |
1004 TEST_F(WasmModuleVerifyTest, SignatureFollowedByUnknownSectionWithLongLEB) { | 1021 TEST_F(WasmModuleVerifyTest, SignatureFollowedByUnknownSectionWithLongLEB) { |
1005 const byte data[] = { | 1022 const byte data[] = { |
1006 SECTION(SIGNATURES, 1 + VOID_VOID_SIG_SIZE), 1, VOID_VOID_SIG, | 1023 // signatures |
| 1024 SIGNATURES_SECTION_VOID_VOID, |
1007 // ----------------------------------------------------------- | 1025 // ----------------------------------------------------------- |
1008 0x85, 0x80, 0x80, 0x80, 0x00, // Section size: 1 but in a 5-byte LEB. | 1026 UNKNOWN_SECTION_NAME, 0x81, 0x80, 0x80, 0x80, |
1009 4, 'l', 'u', 'l', 'z', // unknown section. | 1027 0x00, // section size: 1 but in a 5-byte LEB |
| 1028 0, |
1010 }; | 1029 }; |
1011 EXPECT_VERIFIES(data); | 1030 EXPECT_VERIFIES(data); |
1012 } | 1031 } |
1013 | 1032 |
1014 TEST_F(WasmModuleVerifyTest, UnknownSectionOverflow) { | 1033 TEST_F(WasmModuleVerifyTest, UnknownSectionOverflow) { |
1015 static const byte data[] = { | 1034 static const byte data[] = { |
1016 13, // Section size. | 1035 UNKNOWN_EMPTY_SECTION_NAME, |
1017 1, // Section name length. | 1036 9, // section size |
1018 '\0', // Section name. | 1037 1, |
1019 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, // 10 byte section | 1038 2, |
| 1039 3, |
| 1040 4, |
| 1041 5, |
| 1042 6, |
| 1043 7, |
| 1044 8, |
| 1045 9, |
| 1046 10, // 10 byte section |
1020 }; | 1047 }; |
1021 EXPECT_FAILURE(data); | 1048 EXPECT_FAILURE(data); |
1022 } | 1049 } |
1023 | 1050 |
1024 TEST_F(WasmModuleVerifyTest, UnknownSectionUnderflow) { | 1051 TEST_F(WasmModuleVerifyTest, UnknownSectionUnderflow) { |
1025 static const byte data[] = { | 1052 static const byte data[] = { |
1026 0xff, 0xff, 0xff, 0xff, 0x0f, // Section size LEB128 0xffffffff | 1053 UNKNOWN_EMPTY_SECTION_NAME, |
1027 1, '\0', // Section name and name length. | 1054 0xff, |
1028 1, 2, 3, 4, // 4 byte section | 1055 0xff, |
| 1056 0xff, |
| 1057 0xff, |
| 1058 0x0f, // Section size LEB128 0xffffffff |
| 1059 1, |
| 1060 2, |
| 1061 3, |
| 1062 4, // 4 byte section |
1029 }; | 1063 }; |
1030 EXPECT_FAILURE(data); | 1064 EXPECT_FAILURE(data); |
1031 } | 1065 } |
1032 | 1066 |
1033 TEST_F(WasmModuleVerifyTest, UnknownSectionLoop) { | 1067 TEST_F(WasmModuleVerifyTest, UnknownSectionLoop) { |
1034 // Would infinite loop decoding if wrapping and allowed. | 1068 // Would infinite loop decoding if wrapping and allowed. |
1035 static const byte data[] = { | 1069 static const byte data[] = { |
1036 0xfa, 0xff, 0xff, 0xff, 0x0f, // Section size LEB128 0xfffffffa | 1070 UNKNOWN_EMPTY_SECTION_NAME, |
1037 1, '\0', // Section name and name length. | 1071 1, |
1038 1, 2, 3, 4, // 4 byte section | 1072 2, |
| 1073 3, |
| 1074 4, // 4 byte section |
| 1075 0xfa, |
| 1076 0xff, |
| 1077 0xff, |
| 1078 0xff, |
| 1079 0x0f, // Section size LEB128 0xfffffffa |
1039 }; | 1080 }; |
1040 EXPECT_FAILURE(data); | 1081 EXPECT_FAILURE(data); |
1041 } | 1082 } |
1042 | 1083 |
1043 TEST_F(WasmModuleVerifyTest, UnknownSectionSkipped) { | 1084 TEST_F(WasmModuleVerifyTest, UnknownSectionSkipped) { |
1044 static const byte data[] = { | 1085 static const byte data[] = { |
1045 3, // Section size. | 1086 UNKNOWN_EMPTY_SECTION_NAME, |
1046 1, | 1087 1, // section size |
1047 '\0', // Section name: LEB128 1, string '\0' | 1088 0, // one byte section |
1048 0, // one byte section | |
1049 SECTION(GLOBALS, 4), | 1089 SECTION(GLOBALS, 4), |
1050 1, | 1090 1, |
1051 0, // name length | 1091 0, // name length |
1052 kMemI32, // memory type | 1092 kMemI32, // memory type |
1053 0, // exported | 1093 0, // exported |
1054 }; | 1094 }; |
1055 ModuleResult result = DecodeModule(data, data + arraysize(data)); | 1095 ModuleResult result = DecodeModule(data, data + arraysize(data)); |
1056 EXPECT_OK(result); | 1096 EXPECT_OK(result); |
1057 | 1097 |
1058 EXPECT_EQ(1, result.val->globals.size()); | 1098 EXPECT_EQ(1, result.val->globals.size()); |
(...skipping 26 matching lines...) Expand all Loading... |
1085 SECTION(IMPORT_TABLE, 6), 1, // sig table | 1125 SECTION(IMPORT_TABLE, 6), 1, // sig table |
1086 IMPORT_SIG_INDEX(0), // sig index | 1126 IMPORT_SIG_INDEX(0), // sig index |
1087 NAME_LENGTH(1), 'm', // module name | 1127 NAME_LENGTH(1), 'm', // module name |
1088 NAME_LENGTH(1), 'f', // function name | 1128 NAME_LENGTH(1), 'f', // function name |
1089 }; | 1129 }; |
1090 EXPECT_FAILURE(data); | 1130 EXPECT_FAILURE(data); |
1091 } | 1131 } |
1092 | 1132 |
1093 TEST_F(WasmModuleVerifyTest, ImportTable_invalid_sig) { | 1133 TEST_F(WasmModuleVerifyTest, ImportTable_invalid_sig) { |
1094 static const byte data[] = { | 1134 static const byte data[] = { |
1095 SECTION(SIGNATURES, 1), 0, SECTION(IMPORT_TABLE, 6), 1, | 1135 SECTION(SIGNATURES, 1), 0, // -- |
1096 IMPORT_SIG_INDEX(0), // sig index | 1136 SECTION(IMPORT_TABLE, 6), 1, // -- |
1097 NAME_LENGTH(1), 'm', // module name | 1137 IMPORT_SIG_INDEX(0), // sig index |
1098 NAME_LENGTH(1), 'f', // function name | 1138 NAME_LENGTH(1), 'm', // module name |
| 1139 NAME_LENGTH(1), 'f', // function name |
1099 }; | 1140 }; |
1100 EXPECT_FAILURE(data); | 1141 EXPECT_FAILURE(data); |
1101 } | 1142 } |
1102 | 1143 |
1103 TEST_F(WasmModuleVerifyTest, ImportTable_one_sig) { | 1144 TEST_F(WasmModuleVerifyTest, ImportTable_one_sig) { |
1104 static const byte data[] = { | 1145 static const byte data[] = { |
1105 SECTION(SIGNATURES, 1 + VOID_VOID_SIG_SIZE), | 1146 // signatures |
1106 1, | 1147 SIGNATURES_SECTION_VOID_VOID, |
1107 VOID_VOID_SIG, | |
1108 SECTION(IMPORT_TABLE, 6), | 1148 SECTION(IMPORT_TABLE, 6), |
1109 1, // -- | 1149 1, // -- |
1110 IMPORT_SIG_INDEX(0), // sig index | 1150 IMPORT_SIG_INDEX(0), // sig index |
1111 NAME_LENGTH(1), | 1151 NAME_LENGTH(1), |
1112 'm', // module name | 1152 'm', // module name |
1113 NAME_LENGTH(1), | 1153 NAME_LENGTH(1), |
1114 'f', // function name | 1154 'f', // function name |
1115 }; | 1155 }; |
1116 EXPECT_VERIFIES(data); | 1156 EXPECT_VERIFIES(data); |
1117 } | 1157 } |
1118 | 1158 |
1119 TEST_F(WasmModuleVerifyTest, ImportTable_invalid_module) { | 1159 TEST_F(WasmModuleVerifyTest, ImportTable_invalid_module) { |
1120 static const byte data[] = { | 1160 static const byte data[] = { |
1121 SECTION(SIGNATURES, 1 + VOID_VOID_SIG_SIZE), | 1161 // signatures |
1122 1, | 1162 SIGNATURES_SECTION_VOID_VOID, |
1123 VOID_VOID_SIG, | |
1124 SECTION(IMPORT_TABLE, 6), | 1163 SECTION(IMPORT_TABLE, 6), |
1125 1, // -- | 1164 1, // -- |
1126 IMPORT_SIG_INDEX(0), // sig index | 1165 IMPORT_SIG_INDEX(0), // sig index |
1127 NO_NAME, // module name | 1166 NO_NAME, // module name |
1128 NAME_LENGTH(1), | 1167 NAME_LENGTH(1), |
1129 'f' // function name | 1168 'f' // function name |
1130 }; | 1169 }; |
1131 EXPECT_FAILURE(data); | 1170 EXPECT_FAILURE(data); |
1132 } | 1171 } |
1133 | 1172 |
1134 TEST_F(WasmModuleVerifyTest, ImportTable_off_end) { | 1173 TEST_F(WasmModuleVerifyTest, ImportTable_off_end) { |
1135 static const byte data[] = { | 1174 static const byte data[] = { |
1136 SECTION(SIGNATURES, 1 + VOID_VOID_SIG_SIZE), | 1175 // signatures |
1137 1, | 1176 SIGNATURES_SECTION_VOID_VOID, |
1138 VOID_VOID_SIG, | |
1139 SECTION(IMPORT_TABLE, 6), | 1177 SECTION(IMPORT_TABLE, 6), |
1140 1, | 1178 1, |
1141 IMPORT_SIG_INDEX(0), // sig index | 1179 IMPORT_SIG_INDEX(0), // sig index |
1142 NAME_LENGTH(1), | 1180 NAME_LENGTH(1), |
1143 'm', // module name | 1181 'm', // module name |
1144 NAME_LENGTH(1), | 1182 NAME_LENGTH(1), |
1145 'f', // function name | 1183 'f', // function name |
1146 }; | 1184 }; |
1147 | 1185 |
1148 EXPECT_OFF_END_FAILURE(data, 16, sizeof(data)); | 1186 EXPECT_OFF_END_FAILURE(data, 16, sizeof(data)); |
1149 } | 1187 } |
1150 | 1188 |
1151 TEST_F(WasmModuleVerifyTest, ExportTable_empty1) { | 1189 TEST_F(WasmModuleVerifyTest, ExportTable_empty1) { |
1152 static const byte data[] = {SECTION(SIGNATURES, 1 + VOID_VOID_SIG_SIZE), | 1190 static const byte data[] = { |
1153 1, | 1191 // signatures |
1154 VOID_VOID_SIG, | 1192 SIGNATURES_SECTION_VOID_VOID, |
1155 SECTION(FUNCTIONS, 1 + EMPTY_FUNCTION_SIZE), | 1193 SECTION(OLD_FUNCTIONS, 1 + EMPTY_FUNCTION_SIZE), |
1156 1, | 1194 1, |
1157 EMPTY_FUNCTION(0), | 1195 EMPTY_FUNCTION(0), |
1158 SECTION(EXPORT_TABLE, 1), | 1196 SECTION(EXPORT_TABLE, 1), |
1159 0}; | 1197 0 // -- |
| 1198 }; |
1160 EXPECT_VERIFIES(data); | 1199 EXPECT_VERIFIES(data); |
1161 } | 1200 } |
1162 | 1201 |
1163 TEST_F(WasmModuleVerifyTest, ExportTable_empty2) { | 1202 TEST_F(WasmModuleVerifyTest, ExportTable_empty2) { |
1164 static const byte data[] = {SECTION(SIGNATURES, 1), 0, | 1203 static const byte data[] = { |
1165 SECTION(FUNCTIONS, 1), 0, | 1204 SECTION(SIGNATURES, 1), 0, SECTION(OLD_FUNCTIONS, 1), 0, |
1166 SECTION(EXPORT_TABLE, 1), 0}; | 1205 SECTION(EXPORT_TABLE, 1), 0 // -- |
| 1206 }; |
1167 // TODO(titzer): current behavior treats empty functions section as missing. | 1207 // TODO(titzer): current behavior treats empty functions section as missing. |
1168 EXPECT_FAILURE(data); | 1208 EXPECT_FAILURE(data); |
1169 } | 1209 } |
1170 | 1210 |
1171 TEST_F(WasmModuleVerifyTest, ExportTable_NoFunctions1) { | 1211 TEST_F(WasmModuleVerifyTest, ExportTable_NoFunctions1) { |
1172 static const byte data[] = {SECTION(SIGNATURES, 1), 0, | 1212 static const byte data[] = { |
1173 SECTION(EXPORT_TABLE, 1), 0}; | 1213 SECTION(SIGNATURES, 1), 0, SECTION(EXPORT_TABLE, 1), 0 // -- |
| 1214 }; |
1174 EXPECT_FAILURE(data); | 1215 EXPECT_FAILURE(data); |
1175 } | 1216 } |
1176 | 1217 |
1177 TEST_F(WasmModuleVerifyTest, ExportTable_NoFunctions2) { | 1218 TEST_F(WasmModuleVerifyTest, ExportTable_NoFunctions2) { |
1178 static const byte data[] = {SECTION(EXPORT_TABLE, 1), 0}; | 1219 static const byte data[] = {SECTION(EXPORT_TABLE, 1), 0}; |
1179 EXPECT_FAILURE(data); | 1220 EXPECT_FAILURE(data); |
1180 } | 1221 } |
1181 | 1222 |
1182 TEST_F(WasmModuleVerifyTest, ExportTableOne) { | 1223 TEST_F(WasmModuleVerifyTest, ExportTableOne) { |
1183 static const byte data[] = { | 1224 static const byte data[] = { |
1184 SECTION(SIGNATURES, 1 + VOID_VOID_SIG_SIZE), | 1225 // signatures |
1185 1, // sigs | 1226 SIGNATURES_SECTION_VOID_VOID, |
1186 VOID_VOID_SIG, // -- | 1227 SECTION(OLD_FUNCTIONS, 1 + EMPTY_FUNCTION_SIZE), |
1187 SECTION(FUNCTIONS, 1 + EMPTY_FUNCTION_SIZE), | |
1188 1, // functions | 1228 1, // functions |
1189 EMPTY_FUNCTION(0), // -- | 1229 EMPTY_FUNCTION(0), // -- |
1190 SECTION(EXPORT_TABLE, 3), | 1230 SECTION(EXPORT_TABLE, 3), |
1191 1, // exports | 1231 1, // exports |
1192 FUNC_INDEX(0), // -- | 1232 FUNC_INDEX(0), // -- |
1193 NO_NAME // -- | 1233 NO_NAME // -- |
1194 }; | 1234 }; |
1195 EXPECT_VERIFIES(data); | 1235 EXPECT_VERIFIES(data); |
1196 } | 1236 } |
1197 | 1237 |
1198 TEST_F(WasmModuleVerifyTest, ExportTableTwo) { | 1238 TEST_F(WasmModuleVerifyTest, ExportTableTwo) { |
1199 static const byte data[] = { | 1239 static const byte data[] = { |
1200 SECTION(SIGNATURES, 1 + VOID_VOID_SIG_SIZE), | 1240 // signatures |
1201 1, // sigs | 1241 SIGNATURES_SECTION_VOID_VOID, |
1202 VOID_VOID_SIG, // -- | 1242 SECTION(OLD_FUNCTIONS, 1 + EMPTY_FUNCTION_SIZE), |
1203 SECTION(FUNCTIONS, 1 + EMPTY_FUNCTION_SIZE), | |
1204 1, // functions | 1243 1, // functions |
1205 EMPTY_FUNCTION(0), // -- | 1244 EMPTY_FUNCTION(0), // -- |
1206 SECTION(EXPORT_TABLE, 12), | 1245 SECTION(EXPORT_TABLE, 12), |
1207 2, // exports | 1246 2, // exports |
1208 FUNC_INDEX(0), // -- | 1247 FUNC_INDEX(0), // -- |
1209 NAME_LENGTH(4), | 1248 NAME_LENGTH(4), |
1210 'n', | 1249 'n', |
1211 'a', | 1250 'a', |
1212 'm', | 1251 'm', |
1213 'e', // -- | 1252 'e', // -- |
1214 FUNC_INDEX(0), // -- | 1253 FUNC_INDEX(0), // -- |
1215 NAME_LENGTH(3), | 1254 NAME_LENGTH(3), |
1216 'n', | 1255 'n', |
1217 'o', | 1256 'o', |
1218 'm' // -- | 1257 'm' // -- |
1219 }; | 1258 }; |
1220 EXPECT_VERIFIES(data); | 1259 EXPECT_VERIFIES(data); |
1221 } | 1260 } |
1222 | 1261 |
1223 TEST_F(WasmModuleVerifyTest, ExportTableThree) { | 1262 TEST_F(WasmModuleVerifyTest, ExportTableThree) { |
1224 static const byte data[] = { | 1263 static const byte data[] = { |
1225 SECTION(SIGNATURES, 1 + VOID_VOID_SIG_SIZE), | 1264 // signatures |
1226 1, // sigs | 1265 SIGNATURES_SECTION_VOID_VOID, |
1227 VOID_VOID_SIG, // -- | 1266 SECTION(OLD_FUNCTIONS, 1 + 3 * EMPTY_FUNCTION_SIZE), |
1228 SECTION(FUNCTIONS, 1 + 3 * EMPTY_FUNCTION_SIZE), | |
1229 3, // functions | 1267 3, // functions |
1230 EMPTY_FUNCTION(0), // -- | 1268 EMPTY_FUNCTION(0), // -- |
1231 EMPTY_FUNCTION(0), // -- | 1269 EMPTY_FUNCTION(0), // -- |
1232 EMPTY_FUNCTION(0), // -- | 1270 EMPTY_FUNCTION(0), // -- |
1233 SECTION(EXPORT_TABLE, 10), | 1271 SECTION(EXPORT_TABLE, 10), |
1234 3, // exports | 1272 3, // exports |
1235 FUNC_INDEX(0), // -- | 1273 FUNC_INDEX(0), // -- |
1236 NAME_LENGTH(1), | 1274 NAME_LENGTH(1), |
1237 'a', // -- | 1275 'a', // -- |
1238 FUNC_INDEX(1), // -- | 1276 FUNC_INDEX(1), // -- |
1239 NAME_LENGTH(1), | 1277 NAME_LENGTH(1), |
1240 'b', // -- | 1278 'b', // -- |
1241 FUNC_INDEX(2), // -- | 1279 FUNC_INDEX(2), // -- |
1242 NAME_LENGTH(1), | 1280 NAME_LENGTH(1), |
1243 'c' // -- | 1281 'c' // -- |
1244 }; | 1282 }; |
1245 EXPECT_VERIFIES(data); | 1283 EXPECT_VERIFIES(data); |
1246 } | 1284 } |
1247 | 1285 |
1248 TEST_F(WasmModuleVerifyTest, ExportTableThreeOne) { | 1286 TEST_F(WasmModuleVerifyTest, ExportTableThreeOne) { |
1249 for (int i = 0; i < 6; i++) { | 1287 for (int i = 0; i < 6; i++) { |
1250 const byte data[] = { | 1288 const byte data[] = { |
1251 SECTION(SIGNATURES, 1 + VOID_VOID_SIG_SIZE), | 1289 // signatures |
1252 1, // sigs | 1290 SIGNATURES_SECTION_VOID_VOID, |
1253 VOID_VOID_SIG, // -- | 1291 SECTION(OLD_FUNCTIONS, 1 + 3 * EMPTY_FUNCTION_SIZE), |
1254 SECTION(FUNCTIONS, 1 + 3 * EMPTY_FUNCTION_SIZE), | |
1255 3, // functions | 1292 3, // functions |
1256 EMPTY_FUNCTION(0), // -- | 1293 EMPTY_FUNCTION(0), // -- |
1257 EMPTY_FUNCTION(0), // -- | 1294 EMPTY_FUNCTION(0), // -- |
1258 EMPTY_FUNCTION(0), // -- | 1295 EMPTY_FUNCTION(0), // -- |
1259 SECTION(EXPORT_TABLE, 5), | 1296 SECTION(EXPORT_TABLE, 5), |
1260 1, // exports | 1297 1, // exports |
1261 FUNC_INDEX(i), // -- | 1298 FUNC_INDEX(i), // -- |
1262 NAME_LENGTH(2), | 1299 NAME_LENGTH(2), |
1263 'e', | 1300 'e', |
1264 'x', // -- | 1301 'x', // -- |
1265 }; | 1302 }; |
1266 | 1303 |
1267 if (i < 3) { | 1304 if (i < 3) { |
1268 EXPECT_VERIFIES(data); | 1305 EXPECT_VERIFIES(data); |
1269 } else { | 1306 } else { |
1270 EXPECT_FAILURE(data); | 1307 EXPECT_FAILURE(data); |
1271 } | 1308 } |
1272 } | 1309 } |
1273 } | 1310 } |
1274 | 1311 |
1275 TEST_F(WasmModuleVerifyTest, ExportTableOne_off_end) { | 1312 TEST_F(WasmModuleVerifyTest, ExportTableOne_off_end) { |
1276 static const byte data[] = { | 1313 static const byte data[] = { |
1277 SECTION(SIGNATURES, 1 + VOID_VOID_SIG_SIZE), | 1314 // signatures |
1278 1, // sigs | 1315 SIGNATURES_SECTION_VOID_VOID, |
1279 VOID_VOID_SIG, // -- | 1316 SECTION(OLD_FUNCTIONS, 1 + EMPTY_FUNCTION_SIZE), |
1280 SECTION(FUNCTIONS, 1 + EMPTY_FUNCTION_SIZE), | |
1281 1, // functions | 1317 1, // functions |
1282 EMPTY_FUNCTION(0), // -- | 1318 EMPTY_FUNCTION(0), // -- |
1283 SECTION(EXPORT_TABLE, 1 + 6), | 1319 SECTION(EXPORT_TABLE, 1 + 6), |
1284 1, // exports | 1320 1, // exports |
1285 FUNC_INDEX(0), // -- | 1321 FUNC_INDEX(0), // -- |
1286 NO_NAME // -- | 1322 NO_NAME // -- |
1287 }; | 1323 }; |
1288 | 1324 |
1289 for (int length = 33; length < sizeof(data); length++) { | 1325 for (int length = 33; length < sizeof(data); length++) { |
1290 ModuleResult result = DecodeModule(data, data + length); | 1326 ModuleResult result = DecodeModule(data, data + length); |
1291 EXPECT_FALSE(result.ok()); | 1327 EXPECT_FALSE(result.ok()); |
1292 if (result.val) delete result.val; | 1328 if (result.val) delete result.val; |
1293 } | 1329 } |
1294 } | 1330 } |
1295 | 1331 |
1296 #define SIGNATURES_SECTION(count, ...) \ | |
1297 SECTION(SIGNATURES, 1 + 2 * (count)), U32V_1(count), __VA_ARGS__ | |
1298 #define FUNCTION_SIGNATURES_SECTION(count, ...) \ | |
1299 SECTION(FUNCTION_SIGNATURES, 1 + (count)), U32V_1(count), __VA_ARGS__ | |
1300 | |
1301 #define FOO_STRING 3, 'f', 'o', 'o' | |
1302 #define NO_LOCAL_NAMES 0 | |
1303 | |
1304 #define EMPTY_SIGNATURES_SECTION SECTION(SIGNATURES, 1), 0 | |
1305 #define EMPTY_FUNCTION_SIGNATURES_SECTION SECTION(FUNCTION_SIGNATURES, 1), 0 | |
1306 #define EMPTY_FUNCTION_BODIES_SECTION SECTION(FUNCTION_BODIES, 1), 0 | |
1307 #define EMPTY_NAMES_SECTION SECTION(NAMES, 1), 0 | |
1308 | |
1309 TEST_F(WasmModuleVerifyTest, FunctionSignatures_empty) { | 1332 TEST_F(WasmModuleVerifyTest, FunctionSignatures_empty) { |
1310 static const byte data[] = { | 1333 static const byte data[] = { |
1311 SECTION(SIGNATURES, 1), 0, // -- | 1334 SECTION(SIGNATURES, 1), 0, // -- |
1312 SECTION(FUNCTION_SIGNATURES, 1), 0 // -- | 1335 SECTION(FUNCTION_SIGNATURES, 1), 0 // -- |
1313 }; // -- | 1336 }; // -- |
1314 EXPECT_VERIFIES(data); | 1337 EXPECT_VERIFIES(data); |
1315 } | 1338 } |
1316 | 1339 |
1317 TEST_F(WasmModuleVerifyTest, FunctionSignatures_one) { | 1340 TEST_F(WasmModuleVerifyTest, FunctionSignatures_one) { |
1318 static const byte data[] = { | 1341 static const byte data[] = { |
1319 SIGNATURES_SECTION(1, VOID_VOID_SIG), // -- | 1342 SIGNATURES_SECTION(1, SIG_ENTRY_v_v), // -- |
1320 FUNCTION_SIGNATURES_SECTION(1, 0) // -- | 1343 FUNCTION_SIGNATURES_SECTION(1, 0) // -- |
1321 }; | 1344 }; |
1322 EXPECT_VERIFIES(data); | 1345 EXPECT_VERIFIES(data); |
1323 } | 1346 } |
1324 | 1347 |
1325 TEST_F(WasmModuleVerifyTest, FunctionBodies_empty) { | 1348 TEST_F(WasmModuleVerifyTest, FunctionBodies_empty) { |
1326 static const byte data[] = { | 1349 static const byte data[] = { |
1327 EMPTY_SIGNATURES_SECTION, // -- | 1350 EMPTY_SIGNATURES_SECTION, // -- |
1328 EMPTY_FUNCTION_SIGNATURES_SECTION, // -- | 1351 EMPTY_FUNCTION_SIGNATURES_SECTION, // -- |
1329 EMPTY_FUNCTION_BODIES_SECTION // -- | 1352 EMPTY_FUNCTION_BODIES_SECTION // -- |
1330 }; | 1353 }; |
1331 EXPECT_VERIFIES(data); | 1354 EXPECT_VERIFIES(data); |
1332 } | 1355 } |
1333 | 1356 |
1334 TEST_F(WasmModuleVerifyTest, FunctionBodies_one_empty) { | 1357 TEST_F(WasmModuleVerifyTest, FunctionBodies_one_empty) { |
1335 static const byte data[] = { | 1358 static const byte data[] = { |
1336 SIGNATURES_SECTION(1, VOID_VOID_SIG), // -- | 1359 SIGNATURES_SECTION(1, SIG_ENTRY_v_v), // -- |
1337 FUNCTION_SIGNATURES_SECTION(1, 0), // -- | 1360 FUNCTION_SIGNATURES_SECTION(1, 0), // -- |
1338 SECTION(FUNCTION_BODIES, 1 + EMPTY_BODY_SIZE), 1, EMPTY_BODY // -- | 1361 SECTION(FUNCTION_BODIES, 1 + SIZEOF_EMPTY_BODY), 1, EMPTY_BODY // -- |
1339 }; | 1362 }; |
1340 EXPECT_VERIFIES(data); | 1363 EXPECT_VERIFIES(data); |
1341 } | 1364 } |
1342 | 1365 |
1343 TEST_F(WasmModuleVerifyTest, FunctionBodies_one_nop) { | 1366 TEST_F(WasmModuleVerifyTest, FunctionBodies_one_nop) { |
1344 static const byte data[] = { | 1367 static const byte data[] = { |
1345 SIGNATURES_SECTION(1, VOID_VOID_SIG), // -- | 1368 SIGNATURES_SECTION(1, SIG_ENTRY_v_v), // -- |
1346 FUNCTION_SIGNATURES_SECTION(1, 0), // -- | 1369 FUNCTION_SIGNATURES_SECTION(1, 0), // -- |
1347 SECTION(FUNCTION_BODIES, 1 + NOP_BODY_SIZE), 1, NOP_BODY // -- | 1370 SECTION(FUNCTION_BODIES, 1 + SIZEOF_NOP_BODY), 1, NOP_BODY // -- |
1348 }; | 1371 }; |
1349 EXPECT_VERIFIES(data); | 1372 EXPECT_VERIFIES(data); |
1350 } | 1373 } |
1351 | 1374 |
1352 TEST_F(WasmModuleVerifyTest, FunctionBodies_count_mismatch1) { | 1375 TEST_F(WasmModuleVerifyTest, FunctionBodies_count_mismatch1) { |
1353 static const byte data[] = { | 1376 static const byte data[] = { |
1354 SIGNATURES_SECTION(1, VOID_VOID_SIG), // -- | 1377 SIGNATURES_SECTION(1, SIG_ENTRY_v_v), // -- |
1355 FUNCTION_SIGNATURES_SECTION(2, 0, 0), // -- | 1378 FUNCTION_SIGNATURES_SECTION(2, 0, 0), // -- |
1356 SECTION(FUNCTION_BODIES, 1 + EMPTY_BODY_SIZE), 1, // -- | 1379 SECTION(FUNCTION_BODIES, 1 + SIZEOF_EMPTY_BODY), 1, // -- |
1357 EMPTY_BODY // -- | 1380 EMPTY_BODY // -- |
1358 }; | 1381 }; |
1359 EXPECT_FAILURE(data); | 1382 EXPECT_FAILURE(data); |
1360 } | 1383 } |
1361 | 1384 |
1362 TEST_F(WasmModuleVerifyTest, FunctionBodies_count_mismatch2) { | 1385 TEST_F(WasmModuleVerifyTest, FunctionBodies_count_mismatch2) { |
1363 static const byte data[] = { | 1386 static const byte data[] = { |
1364 SIGNATURES_SECTION(1, VOID_VOID_SIG), // -- | 1387 SIGNATURES_SECTION(1, SIG_ENTRY_v_v), // -- |
1365 FUNCTION_SIGNATURES_SECTION(1, 0), // -- | 1388 FUNCTION_SIGNATURES_SECTION(1, 0), // -- |
1366 SECTION(FUNCTION_BODIES, 1 + 2 * NOP_BODY_SIZE), // -- | 1389 SECTION(FUNCTION_BODIES, 1 + 2 * SIZEOF_NOP_BODY), // -- |
1367 2, // -- | 1390 2, // -- |
1368 NOP_BODY, // -- | 1391 NOP_BODY, // -- |
1369 NOP_BODY // -- | 1392 NOP_BODY // -- |
1370 }; | 1393 }; |
1371 EXPECT_FAILURE(data); | 1394 EXPECT_FAILURE(data); |
1372 } | 1395 } |
1373 | 1396 |
1374 TEST_F(WasmModuleVerifyTest, Names_empty) { | 1397 TEST_F(WasmModuleVerifyTest, Names_empty) { |
1375 static const byte data[] = { | 1398 static const byte data[] = { |
1376 EMPTY_SIGNATURES_SECTION, EMPTY_FUNCTION_SIGNATURES_SECTION, | 1399 EMPTY_SIGNATURES_SECTION, EMPTY_FUNCTION_SIGNATURES_SECTION, |
1377 EMPTY_FUNCTION_BODIES_SECTION, EMPTY_NAMES_SECTION}; | 1400 EMPTY_FUNCTION_BODIES_SECTION, EMPTY_NAMES_SECTION}; |
1378 EXPECT_VERIFIES(data); | 1401 EXPECT_VERIFIES(data); |
1379 } | 1402 } |
1380 | 1403 |
1381 TEST_F(WasmModuleVerifyTest, Names_one_empty) { | 1404 TEST_F(WasmModuleVerifyTest, Names_one_empty) { |
1382 static const byte data[] = { | 1405 static const byte data[] = { |
1383 SIGNATURES_SECTION(1, VOID_VOID_SIG), // -- | 1406 SIGNATURES_SECTION(1, SIG_ENTRY_v_v), // -- |
1384 FUNCTION_SIGNATURES_SECTION(1, 0), // -- | 1407 FUNCTION_SIGNATURES_SECTION(1, 0), // -- |
1385 SECTION(FUNCTION_BODIES, 1 + EMPTY_BODY_SIZE), | 1408 SECTION(FUNCTION_BODIES, 1 + SIZEOF_EMPTY_BODY), |
1386 1, | 1409 1, |
1387 EMPTY_BODY, // -- | 1410 EMPTY_BODY, // -- |
1388 SECTION(NAMES, 1 + 5), | 1411 SECTION(NAMES, 1 + 5), |
1389 1, | 1412 1, |
1390 FOO_STRING, | 1413 FOO_STRING, |
1391 NO_LOCAL_NAMES // -- | 1414 NO_LOCAL_NAMES // -- |
1392 }; | 1415 }; |
1393 EXPECT_VERIFIES(data); | 1416 EXPECT_VERIFIES(data); |
1394 } | 1417 } |
1395 | 1418 |
1396 TEST_F(WasmModuleVerifyTest, Names_two_empty) { | 1419 TEST_F(WasmModuleVerifyTest, Names_two_empty) { |
1397 static const byte data[] = { | 1420 static const byte data[] = { |
1398 SIGNATURES_SECTION(1, VOID_VOID_SIG), // -- | 1421 SIGNATURES_SECTION(1, SIG_ENTRY_v_v), // -- |
1399 FUNCTION_SIGNATURES_SECTION(2, 0, 0), // -- | 1422 FUNCTION_SIGNATURES_SECTION(2, 0, 0), // -- |
1400 SECTION(FUNCTION_BODIES, 1 + 2 * EMPTY_BODY_SIZE), // -- | 1423 SECTION(FUNCTION_BODIES, 1 + 2 * SIZEOF_EMPTY_BODY), // -- |
1401 2, | 1424 2, |
1402 EMPTY_BODY, | 1425 EMPTY_BODY, |
1403 EMPTY_BODY, // -- | 1426 EMPTY_BODY, // -- |
1404 SECTION(NAMES, 1 + 10), | 1427 SECTION(NAMES, 1 + 10), |
1405 2, // -- | 1428 2, // -- |
1406 FOO_STRING, | 1429 FOO_STRING, |
1407 NO_LOCAL_NAMES, // -- | 1430 NO_LOCAL_NAMES, // -- |
1408 FOO_STRING, | 1431 FOO_STRING, |
1409 NO_LOCAL_NAMES, // -- | 1432 NO_LOCAL_NAMES, // -- |
1410 }; | 1433 }; |
1411 EXPECT_VERIFIES(data); | 1434 EXPECT_VERIFIES(data); |
1412 } | 1435 } |
1413 | 1436 |
1414 } // namespace wasm | 1437 } // namespace wasm |
1415 } // namespace internal | 1438 } // namespace internal |
1416 } // namespace v8 | 1439 } // namespace v8 |
OLD | NEW |