| 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/v8.h" | 7 #include "src/v8.h" |
| 8 | 8 |
| 9 #include "test/common/wasm/test-signatures.h" | 9 #include "test/common/wasm/test-signatures.h" |
| 10 | 10 |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 } | 97 } |
| 98 | 98 |
| 99 TestSignatures sigs; | 99 TestSignatures sigs; |
| 100 ModuleEnv* module; | 100 ModuleEnv* module; |
| 101 LocalDeclEncoder local_decls; | 101 LocalDeclEncoder local_decls; |
| 102 | 102 |
| 103 void AddLocals(ValueType type, uint32_t count) { | 103 void AddLocals(ValueType type, uint32_t count) { |
| 104 local_decls.AddLocals(count, type); | 104 local_decls.AddLocals(count, type); |
| 105 } | 105 } |
| 106 | 106 |
| 107 void PrepareBytecode(const byte** startp, const byte** endp) { | |
| 108 const byte* start = *startp; | |
| 109 const byte* end = *endp; | |
| 110 size_t locals_size = local_decls.Size(); | |
| 111 size_t total_size = end - start + locals_size + 1; | |
| 112 byte* buffer = static_cast<byte*>(zone()->New(total_size)); | |
| 113 // Prepend the local decls to the code. | |
| 114 local_decls.Emit(buffer); | |
| 115 // Emit the code. | |
| 116 memcpy(buffer + locals_size, start, end - start); | |
| 117 // Append an extra end opcode. | |
| 118 buffer[total_size - 1] = kExprEnd; | |
| 119 | |
| 120 *startp = buffer; | |
| 121 *endp = buffer + total_size; | |
| 122 } | |
| 123 | |
| 124 // Prepends local variable declarations and renders nice error messages for | 107 // Prepends local variable declarations and renders nice error messages for |
| 125 // verification failures. | 108 // verification failures. |
| 126 void Verify(ErrorCode expected, FunctionSig* sig, const byte* start, | 109 void Verify(ErrorCode expected, FunctionSig* sig, const byte* start, |
| 127 const byte* end) { | 110 const byte* end) { |
| 128 PrepareBytecode(&start, &end); | 111 local_decls.Prepend(zone(), &start, &end); |
| 129 | |
| 130 // Verify the code. | 112 // Verify the code. |
| 131 DecodeResult result = VerifyWasmCode( | 113 DecodeResult result = VerifyWasmCode( |
| 132 zone()->allocator(), module == nullptr ? nullptr : module->module, sig, | 114 zone()->allocator(), module == nullptr ? nullptr : module->module, sig, |
| 133 start, end); | 115 start, end); |
| 134 | 116 |
| 135 if (result.error_code != expected) { | 117 if (result.error_code != expected) { |
| 136 ptrdiff_t pc = result.error_pc - result.start; | 118 ptrdiff_t pc = result.error_pc - result.start; |
| 137 ptrdiff_t pt = result.error_pt - result.start; | 119 ptrdiff_t pt = result.error_pt - result.start; |
| 138 std::ostringstream str; | 120 std::ostringstream str; |
| 139 if (expected == kSuccess) { | 121 if (expected == kSuccess) { |
| (...skipping 332 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 472 EXPECT_FAILURE_C(i_i, code); | 454 EXPECT_FAILURE_C(i_i, code); |
| 473 } | 455 } |
| 474 | 456 |
| 475 TEST_F(FunctionBodyDecoderTest, Block0Block0) { | 457 TEST_F(FunctionBodyDecoderTest, Block0Block0) { |
| 476 static const byte code[] = {WASM_EMPTY_BLOCK, WASM_EMPTY_BLOCK}; | 458 static const byte code[] = {WASM_EMPTY_BLOCK, WASM_EMPTY_BLOCK}; |
| 477 EXPECT_VERIFIES_C(v_v, code); | 459 EXPECT_VERIFIES_C(v_v, code); |
| 478 EXPECT_FAILURE_C(i_i, code); | 460 EXPECT_FAILURE_C(i_i, code); |
| 479 } | 461 } |
| 480 | 462 |
| 481 TEST_F(FunctionBodyDecoderTest, Block0_end) { | 463 TEST_F(FunctionBodyDecoderTest, Block0_end) { |
| 482 EXPECT_FAILURE(v_v, WASM_EMPTY_BLOCK, kExprEnd); | 464 EXPECT_VERIFIES(v_v, WASM_EMPTY_BLOCK, kExprEnd); |
| 465 } |
| 466 |
| 467 TEST_F(FunctionBodyDecoderTest, Block0_end_end) { |
| 468 EXPECT_FAILURE(v_v, WASM_EMPTY_BLOCK, kExprEnd, kExprEnd); |
| 483 } | 469 } |
| 484 | 470 |
| 485 TEST_F(FunctionBodyDecoderTest, Block1) { | 471 TEST_F(FunctionBodyDecoderTest, Block1) { |
| 486 byte code[] = {WASM_BLOCK_I(WASM_GET_LOCAL(0))}; | 472 byte code[] = {WASM_BLOCK_I(WASM_GET_LOCAL(0))}; |
| 487 EXPECT_VERIFIES_C(i_i, code); | 473 EXPECT_VERIFIES_C(i_i, code); |
| 488 EXPECT_FAILURE_C(v_i, code); | 474 EXPECT_FAILURE_C(v_i, code); |
| 489 EXPECT_FAILURE_C(d_dd, code); | 475 EXPECT_FAILURE_C(d_dd, code); |
| 490 EXPECT_FAILURE_C(i_f, code); | 476 EXPECT_FAILURE_C(i_f, code); |
| 491 EXPECT_FAILURE_C(i_d, code); | 477 EXPECT_FAILURE_C(i_d, code); |
| 492 } | 478 } |
| (...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 725 } | 711 } |
| 726 | 712 |
| 727 TEST_F(FunctionBodyDecoderTest, IfNop) { | 713 TEST_F(FunctionBodyDecoderTest, IfNop) { |
| 728 EXPECT_VERIFIES(v_i, WASM_IF(WASM_GET_LOCAL(0), WASM_NOP)); | 714 EXPECT_VERIFIES(v_i, WASM_IF(WASM_GET_LOCAL(0), WASM_NOP)); |
| 729 } | 715 } |
| 730 | 716 |
| 731 TEST_F(FunctionBodyDecoderTest, IfNopElseNop) { | 717 TEST_F(FunctionBodyDecoderTest, IfNopElseNop) { |
| 732 EXPECT_VERIFIES(v_i, WASM_IF_ELSE(WASM_GET_LOCAL(0), WASM_NOP, WASM_NOP)); | 718 EXPECT_VERIFIES(v_i, WASM_IF_ELSE(WASM_GET_LOCAL(0), WASM_NOP, WASM_NOP)); |
| 733 } | 719 } |
| 734 | 720 |
| 735 TEST_F(FunctionBodyDecoderTest, If_end) { | |
| 736 static const byte code[] = {kExprGetLocal, 0, WASM_IF_OP, kExprEnd}; | |
| 737 EXPECT_VERIFIES_C(v_i, code); | |
| 738 } | |
| 739 | |
| 740 TEST_F(FunctionBodyDecoderTest, If_end_end) { | 721 TEST_F(FunctionBodyDecoderTest, If_end_end) { |
| 741 static const byte code[] = {kExprGetLocal, 0, WASM_IF_OP, kExprEnd, kExprEnd}; | 722 static const byte code[] = {kExprGetLocal, 0, WASM_IF_OP, kExprEnd, kExprEnd}; |
| 723 EXPECT_VERIFIES_C(v_i, code); |
| 724 } |
| 725 |
| 726 TEST_F(FunctionBodyDecoderTest, If_end_end_end) { |
| 727 static const byte code[] = {kExprGetLocal, 0, WASM_IF_OP, |
| 728 kExprEnd, kExprEnd, kExprEnd}; |
| 742 EXPECT_FAILURE_C(v_i, code); | 729 EXPECT_FAILURE_C(v_i, code); |
| 743 } | 730 } |
| 744 | 731 |
| 745 TEST_F(FunctionBodyDecoderTest, If_falloff1) { | 732 TEST_F(FunctionBodyDecoderTest, If_falloff1) { |
| 746 static const byte code[] = {kExprGetLocal, 0, kExprIf}; | 733 static const byte code[] = {kExprGetLocal, 0, kExprIf}; |
| 747 EXPECT_FAILURE_C(v_i, code); | 734 EXPECT_FAILURE_C(v_i, code); |
| 748 } | 735 } |
| 749 | 736 |
| 750 TEST_F(FunctionBodyDecoderTest, If_falloff2) { | 737 TEST_F(FunctionBodyDecoderTest, If_falloff2) { |
| 751 static const byte code[] = {kExprGetLocal, 0, WASM_IF_OP}; | 738 static const byte code[] = {kExprGetLocal, 0, WASM_IF_OP}; |
| (...skipping 1989 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2741 iter.next(); | 2728 iter.next(); |
| 2742 EXPECT_TRUE(iter.has_next()); | 2729 EXPECT_TRUE(iter.has_next()); |
| 2743 EXPECT_EQ(kExprI32Const, iter.current()); | 2730 EXPECT_EQ(kExprI32Const, iter.current()); |
| 2744 iter.next(); | 2731 iter.next(); |
| 2745 EXPECT_FALSE(iter.has_next()); | 2732 EXPECT_FALSE(iter.has_next()); |
| 2746 } | 2733 } |
| 2747 | 2734 |
| 2748 } // namespace wasm | 2735 } // namespace wasm |
| 2749 } // namespace internal | 2736 } // namespace internal |
| 2750 } // namespace v8 | 2737 } // namespace v8 |
| OLD | NEW |