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 | |
451 TEST_F(DecoderTest, ReadU32v_Bits) { | 431 TEST_F(DecoderTest, ReadU32v_Bits) { |
452 // A more exhaustive test. | 432 // A more exhaustive test. |
453 const int kMaxSize = 5; | 433 const int kMaxSize = 5; |
454 const uint32_t kVals[] = { | 434 const uint32_t kVals[] = { |
455 0xaabbccdd, 0x11223344, 0x33445566, 0xffeeddcc, 0xF0F0F0F0, 0x0F0F0F0F, | 435 0xaabbccdd, 0x11223344, 0x33445566, 0xffeeddcc, 0xF0F0F0F0, 0x0F0F0F0F, |
456 0xEEEEEEEE, 0xAAAAAAAA, 0x12345678, 0x9abcdef0, 0x80309488, 0x729ed997, | 436 0xEEEEEEEE, 0xAAAAAAAA, 0x12345678, 0x9abcdef0, 0x80309488, 0x729ed997, |
457 0xc4a0cf81, 0x16c6eb85, 0x4206db8e, 0xf3b089d5, 0xaa2e223e, 0xf99e29c8, | 437 0xc4a0cf81, 0x16c6eb85, 0x4206db8e, 0xf3b089d5, 0xaa2e223e, 0xf99e29c8, |
458 0x4a4357d8, 0x1890b1c1, 0x8d80a085, 0xacb6ae4c, 0x1b827e10, 0xeb5c7bd9, | 438 0x4a4357d8, 0x1890b1c1, 0x8d80a085, 0xacb6ae4c, 0x1b827e10, 0xeb5c7bd9, |
459 0xbb1bc146, 0xdf57a33l}; | 439 0xbb1bc146, 0xdf57a33l}; |
460 byte data[kMaxSize]; | 440 byte data[kMaxSize]; |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
600 byte data[kMaxSize]; | 580 byte data[kMaxSize]; |
601 | 581 |
602 // foreach value in above array | 582 // foreach value in above array |
603 for (size_t v = 0; v < arraysize(kVals); v++) { | 583 for (size_t v = 0; v < arraysize(kVals); v++) { |
604 // foreach length 1...64 | 584 // foreach length 1...64 |
605 for (int i = 1; i <= 64; i++) { | 585 for (int i = 1; i <= 64; i++) { |
606 const int64_t val = bit_cast<int64_t>(kVals[v] << (64 - i)) >> (64 - i); | 586 const int64_t val = bit_cast<int64_t>(kVals[v] << (64 - i)) >> (64 - i); |
607 | 587 |
608 int length = 1 + i / 7; | 588 int length = 1 + i / 7; |
609 for (int j = 0; j < kMaxSize; j++) { | 589 for (int j = 0; j < kMaxSize; j++) { |
610 data[j] = static_cast<byte>((val >> (7 * j)) & MASK_7); | 590 const uint64_t uval = bit_cast<uint64_t>(val); |
| 591 data[j] = static_cast<byte>((uval >> (7 * j)) & MASK_7); |
611 } | 592 } |
612 for (int j = 0; j < length - 1; j++) { | 593 for (int j = 0; j < length - 1; j++) { |
613 data[j] |= 0x80; | 594 data[j] |= 0x80; |
614 } | 595 } |
615 | 596 |
616 // foreach buffer size 0...10 | 597 // foreach buffer size 0...10 |
617 for (int limit = 0; limit <= kMaxSize; limit++) { | 598 for (int limit = 0; limit <= kMaxSize; limit++) { |
618 decoder.Reset(data, data + limit); | 599 decoder.Reset(data, data + limit); |
619 int rlen; | 600 int rlen; |
620 int64_t result = decoder.checked_read_i64v(data, 0, &rlen); | 601 int64_t result = decoder.checked_read_i64v(data, 0, &rlen); |
(...skipping 14 matching lines...) Expand all Loading... |
635 for (int i = 1; i < 128; i++) { | 616 for (int i = 1; i < 128; i++) { |
636 data[9] = static_cast<byte>(i << 1); | 617 data[9] = static_cast<byte>(i << 1); |
637 int length = 0; | 618 int length = 0; |
638 decoder.Reset(data, data + sizeof(data)); | 619 decoder.Reset(data, data + sizeof(data)); |
639 decoder.checked_read_u64v(decoder.start(), 0, &length); | 620 decoder.checked_read_u64v(decoder.start(), 0, &length); |
640 EXPECT_EQ(10, length); | 621 EXPECT_EQ(10, length); |
641 EXPECT_FALSE(decoder.ok()); | 622 EXPECT_FALSE(decoder.ok()); |
642 } | 623 } |
643 } | 624 } |
644 | 625 |
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 | |
665 } // namespace wasm | 626 } // namespace wasm |
666 } // namespace internal | 627 } // namespace internal |
667 } // namespace v8 | 628 } // namespace v8 |
OLD | NEW |