OLD | NEW |
1 // Copyright 2016 the V8 project authors. All rights reserved. | 1 // Copyright 2016 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "test/unittests/test-utils.h" | 5 #include "test/unittests/test-utils.h" |
6 | 6 |
7 #include "src/wasm/decoder.h" | 7 #include "src/wasm/decoder.h" |
8 #include "src/wasm/wasm-macro-gen.h" | 8 #include "src/wasm/wasm-macro-gen.h" |
9 | 9 |
10 namespace v8 { | 10 namespace v8 { |
(...skipping 410 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
421 for (int i = 1; i < 16; i++) { | 421 for (int i = 1; i < 16; i++) { |
422 data[4] = static_cast<byte>(i << 4); | 422 data[4] = static_cast<byte>(i << 4); |
423 int length = 0; | 423 int length = 0; |
424 decoder.Reset(data, data + sizeof(data)); | 424 decoder.Reset(data, data + sizeof(data)); |
425 decoder.checked_read_u32v(decoder.start(), 0, &length); | 425 decoder.checked_read_u32v(decoder.start(), 0, &length); |
426 EXPECT_EQ(5, length); | 426 EXPECT_EQ(5, length); |
427 EXPECT_FALSE(decoder.ok()); | 427 EXPECT_FALSE(decoder.ok()); |
428 } | 428 } |
429 } | 429 } |
430 | 430 |
| 431 TEST_F(DecoderTest, ReadI32v_extra_bits_negative) { |
| 432 // OK for negative signed values to have extra ones. |
| 433 int length = 0; |
| 434 byte data[] = {0xff, 0xff, 0xff, 0xff, 0x7f}; |
| 435 decoder.Reset(data, data + sizeof(data)); |
| 436 decoder.checked_read_i32v(decoder.start(), 0, &length); |
| 437 EXPECT_EQ(5, length); |
| 438 EXPECT_TRUE(decoder.ok()); |
| 439 } |
| 440 |
| 441 TEST_F(DecoderTest, ReadI32v_extra_bits_positive) { |
| 442 // Not OK for positive signed values to have extra ones. |
| 443 int length = 0; |
| 444 byte data[] = {0x80, 0x80, 0x80, 0x80, 0x77}; |
| 445 decoder.Reset(data, data + sizeof(data)); |
| 446 decoder.checked_read_i32v(decoder.start(), 0, &length); |
| 447 EXPECT_EQ(5, length); |
| 448 EXPECT_FALSE(decoder.ok()); |
| 449 } |
| 450 |
431 TEST_F(DecoderTest, ReadU32v_Bits) { | 451 TEST_F(DecoderTest, ReadU32v_Bits) { |
432 // A more exhaustive test. | 452 // A more exhaustive test. |
433 const int kMaxSize = 5; | 453 const int kMaxSize = 5; |
434 const uint32_t kVals[] = { | 454 const uint32_t kVals[] = { |
435 0xaabbccdd, 0x11223344, 0x33445566, 0xffeeddcc, 0xF0F0F0F0, 0x0F0F0F0F, | 455 0xaabbccdd, 0x11223344, 0x33445566, 0xffeeddcc, 0xF0F0F0F0, 0x0F0F0F0F, |
436 0xEEEEEEEE, 0xAAAAAAAA, 0x12345678, 0x9abcdef0, 0x80309488, 0x729ed997, | 456 0xEEEEEEEE, 0xAAAAAAAA, 0x12345678, 0x9abcdef0, 0x80309488, 0x729ed997, |
437 0xc4a0cf81, 0x16c6eb85, 0x4206db8e, 0xf3b089d5, 0xaa2e223e, 0xf99e29c8, | 457 0xc4a0cf81, 0x16c6eb85, 0x4206db8e, 0xf3b089d5, 0xaa2e223e, 0xf99e29c8, |
438 0x4a4357d8, 0x1890b1c1, 0x8d80a085, 0xacb6ae4c, 0x1b827e10, 0xeb5c7bd9, | 458 0x4a4357d8, 0x1890b1c1, 0x8d80a085, 0xacb6ae4c, 0x1b827e10, 0xeb5c7bd9, |
439 0xbb1bc146, 0xdf57a33l}; | 459 0xbb1bc146, 0xdf57a33l}; |
440 byte data[kMaxSize]; | 460 byte data[kMaxSize]; |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
580 byte data[kMaxSize]; | 600 byte data[kMaxSize]; |
581 | 601 |
582 // foreach value in above array | 602 // foreach value in above array |
583 for (size_t v = 0; v < arraysize(kVals); v++) { | 603 for (size_t v = 0; v < arraysize(kVals); v++) { |
584 // foreach length 1...64 | 604 // foreach length 1...64 |
585 for (int i = 1; i <= 64; i++) { | 605 for (int i = 1; i <= 64; i++) { |
586 const int64_t val = bit_cast<int64_t>(kVals[v] << (64 - i)) >> (64 - i); | 606 const int64_t val = bit_cast<int64_t>(kVals[v] << (64 - i)) >> (64 - i); |
587 | 607 |
588 int length = 1 + i / 7; | 608 int length = 1 + i / 7; |
589 for (int j = 0; j < kMaxSize; j++) { | 609 for (int j = 0; j < kMaxSize; j++) { |
590 const uint64_t uval = bit_cast<uint64_t>(val); | 610 data[j] = static_cast<byte>((val >> (7 * j)) & MASK_7); |
591 data[j] = static_cast<byte>((uval >> (7 * j)) & MASK_7); | |
592 } | 611 } |
593 for (int j = 0; j < length - 1; j++) { | 612 for (int j = 0; j < length - 1; j++) { |
594 data[j] |= 0x80; | 613 data[j] |= 0x80; |
595 } | 614 } |
596 | 615 |
597 // foreach buffer size 0...10 | 616 // foreach buffer size 0...10 |
598 for (int limit = 0; limit <= kMaxSize; limit++) { | 617 for (int limit = 0; limit <= kMaxSize; limit++) { |
599 decoder.Reset(data, data + limit); | 618 decoder.Reset(data, data + limit); |
600 int rlen; | 619 int rlen; |
601 int64_t result = decoder.checked_read_i64v(data, 0, &rlen); | 620 int64_t result = decoder.checked_read_i64v(data, 0, &rlen); |
(...skipping 14 matching lines...) Expand all Loading... |
616 for (int i = 1; i < 128; i++) { | 635 for (int i = 1; i < 128; i++) { |
617 data[9] = static_cast<byte>(i << 1); | 636 data[9] = static_cast<byte>(i << 1); |
618 int length = 0; | 637 int length = 0; |
619 decoder.Reset(data, data + sizeof(data)); | 638 decoder.Reset(data, data + sizeof(data)); |
620 decoder.checked_read_u64v(decoder.start(), 0, &length); | 639 decoder.checked_read_u64v(decoder.start(), 0, &length); |
621 EXPECT_EQ(10, length); | 640 EXPECT_EQ(10, length); |
622 EXPECT_FALSE(decoder.ok()); | 641 EXPECT_FALSE(decoder.ok()); |
623 } | 642 } |
624 } | 643 } |
625 | 644 |
| 645 TEST_F(DecoderTest, ReadI64v_extra_bits_negative) { |
| 646 // OK for negative signed values to have extra ones. |
| 647 int length = 0; |
| 648 byte data[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}; |
| 649 decoder.Reset(data, data + sizeof(data)); |
| 650 decoder.checked_read_i64v(decoder.start(), 0, &length); |
| 651 EXPECT_EQ(10, length); |
| 652 EXPECT_TRUE(decoder.ok()); |
| 653 } |
| 654 |
| 655 TEST_F(DecoderTest, ReadI64v_extra_bits_positive) { |
| 656 // Not OK for positive signed values to have extra ones. |
| 657 int length = 0; |
| 658 byte data[] = {0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x77}; |
| 659 decoder.Reset(data, data + sizeof(data)); |
| 660 decoder.checked_read_i64v(decoder.start(), 0, &length); |
| 661 EXPECT_EQ(10, length); |
| 662 EXPECT_FALSE(decoder.ok()); |
| 663 } |
| 664 |
626 } // namespace wasm | 665 } // namespace wasm |
627 } // namespace internal | 666 } // namespace internal |
628 } // namespace v8 | 667 } // namespace v8 |
OLD | NEW |