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

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

Issue 2630553002: [wasm] Enforce that function bodies end with the \"end\" opcode. (Closed)
Patch Set: Collapse some tests together Created 3 years, 11 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/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
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
107 // Prepends local variable declarations and renders nice error messages for 124 // Prepends local variable declarations and renders nice error messages for
108 // verification failures. 125 // verification failures.
109 void Verify(ErrorCode expected, FunctionSig* sig, const byte* start, 126 void Verify(ErrorCode expected, FunctionSig* sig, const byte* start,
110 const byte* end) { 127 const byte* end) {
111 local_decls.Prepend(zone(), &start, &end); 128 PrepareBytecode(&start, &end);
129
112 // Verify the code. 130 // Verify the code.
113 DecodeResult result = VerifyWasmCode( 131 DecodeResult result = VerifyWasmCode(
114 zone()->allocator(), module == nullptr ? nullptr : module->module, sig, 132 zone()->allocator(), module == nullptr ? nullptr : module->module, sig,
115 start, end); 133 start, end);
116 134
117 if (result.error_code != expected) { 135 if (result.error_code != expected) {
118 ptrdiff_t pc = result.error_pc - result.start; 136 ptrdiff_t pc = result.error_pc - result.start;
119 ptrdiff_t pt = result.error_pt - result.start; 137 ptrdiff_t pt = result.error_pt - result.start;
120 std::ostringstream str; 138 std::ostringstream str;
121 if (expected == kSuccess) { 139 if (expected == kSuccess) {
(...skipping 332 matching lines...) Expand 10 before | Expand all | Expand 10 after
454 EXPECT_FAILURE_C(i_i, code); 472 EXPECT_FAILURE_C(i_i, code);
455 } 473 }
456 474
457 TEST_F(FunctionBodyDecoderTest, Block0Block0) { 475 TEST_F(FunctionBodyDecoderTest, Block0Block0) {
458 static const byte code[] = {WASM_EMPTY_BLOCK, WASM_EMPTY_BLOCK}; 476 static const byte code[] = {WASM_EMPTY_BLOCK, WASM_EMPTY_BLOCK};
459 EXPECT_VERIFIES_C(v_v, code); 477 EXPECT_VERIFIES_C(v_v, code);
460 EXPECT_FAILURE_C(i_i, code); 478 EXPECT_FAILURE_C(i_i, code);
461 } 479 }
462 480
463 TEST_F(FunctionBodyDecoderTest, Block0_end) { 481 TEST_F(FunctionBodyDecoderTest, Block0_end) {
464 EXPECT_VERIFIES(v_v, WASM_EMPTY_BLOCK, kExprEnd); 482 EXPECT_FAILURE(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);
469 } 483 }
470 484
471 TEST_F(FunctionBodyDecoderTest, Block1) { 485 TEST_F(FunctionBodyDecoderTest, Block1) {
472 byte code[] = {WASM_BLOCK_I(WASM_GET_LOCAL(0))}; 486 byte code[] = {WASM_BLOCK_I(WASM_GET_LOCAL(0))};
473 EXPECT_VERIFIES_C(i_i, code); 487 EXPECT_VERIFIES_C(i_i, code);
474 EXPECT_FAILURE_C(v_i, code); 488 EXPECT_FAILURE_C(v_i, code);
475 EXPECT_FAILURE_C(d_dd, code); 489 EXPECT_FAILURE_C(d_dd, code);
476 EXPECT_FAILURE_C(i_f, code); 490 EXPECT_FAILURE_C(i_f, code);
477 EXPECT_FAILURE_C(i_d, code); 491 EXPECT_FAILURE_C(i_d, code);
478 } 492 }
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after
705 } 719 }
706 720
707 TEST_F(FunctionBodyDecoderTest, Block_else) { 721 TEST_F(FunctionBodyDecoderTest, Block_else) {
708 byte code[] = {kExprI32Const, 0, kExprBlock, kExprElse, kExprEnd}; 722 byte code[] = {kExprI32Const, 0, kExprBlock, kExprElse, kExprEnd};
709 EXPECT_FAILURE_C(v_v, code); 723 EXPECT_FAILURE_C(v_v, code);
710 EXPECT_FAILURE_C(i_i, code); 724 EXPECT_FAILURE_C(i_i, code);
711 } 725 }
712 726
713 TEST_F(FunctionBodyDecoderTest, IfNop) { 727 TEST_F(FunctionBodyDecoderTest, IfNop) {
714 EXPECT_VERIFIES(v_i, WASM_IF(WASM_GET_LOCAL(0), WASM_NOP)); 728 EXPECT_VERIFIES(v_i, WASM_IF(WASM_GET_LOCAL(0), WASM_NOP));
715 }
716
717 TEST_F(FunctionBodyDecoderTest, IfNopElseNop) {
718 EXPECT_VERIFIES(v_i, WASM_IF_ELSE(WASM_GET_LOCAL(0), WASM_NOP, WASM_NOP)); 729 EXPECT_VERIFIES(v_i, WASM_IF_ELSE(WASM_GET_LOCAL(0), WASM_NOP, WASM_NOP));
719 } 730 }
720 731
721 TEST_F(FunctionBodyDecoderTest, If_end_end) { 732 TEST_F(FunctionBodyDecoderTest, If_end) {
722 static const byte code[] = {kExprGetLocal, 0, WASM_IF_OP, kExprEnd, kExprEnd}; 733 EXPECT_VERIFIES(v_i, kExprGetLocal, 0, WASM_IF_OP, kExprEnd);
723 EXPECT_VERIFIES_C(v_i, code); 734 EXPECT_FAILURE(v_i, kExprGetLocal, 0, WASM_IF_OP, kExprEnd, kExprEnd);
724 }
725
726 TEST_F(FunctionBodyDecoderTest, If_end_end_end) {
727 static const byte code[] = {kExprGetLocal, 0, WASM_IF_OP,
728 kExprEnd, kExprEnd, kExprEnd};
729 EXPECT_FAILURE_C(v_i, code);
730 } 735 }
731 736
732 TEST_F(FunctionBodyDecoderTest, If_falloff1) { 737 TEST_F(FunctionBodyDecoderTest, If_falloff1) {
733 static const byte code[] = {kExprGetLocal, 0, kExprIf}; 738 EXPECT_FAILURE(v_i, kExprGetLocal, 0, kExprIf);
734 EXPECT_FAILURE_C(v_i, code); 739 EXPECT_FAILURE(v_i, kExprGetLocal, 0, WASM_IF_OP);
735 } 740 EXPECT_FAILURE(v_i, kExprGetLocal, 0, WASM_IF_OP, kExprNop, kExprElse);
736
737 TEST_F(FunctionBodyDecoderTest, If_falloff2) {
738 static const byte code[] = {kExprGetLocal, 0, WASM_IF_OP};
739 EXPECT_FAILURE_C(v_i, code);
740 }
741
742 TEST_F(FunctionBodyDecoderTest, IfElse_falloff) {
743 static const byte code[] = {kExprGetLocal, 0, WASM_IF_OP, kExprNop,
744 kExprElse};
745 EXPECT_FAILURE_C(v_i, code);
746 } 741 }
747 742
748 TEST_F(FunctionBodyDecoderTest, IfElseNop) { 743 TEST_F(FunctionBodyDecoderTest, IfElseNop) {
749 EXPECT_VERIFIES(v_i, WASM_IF_ELSE(WASM_GET_LOCAL(0), 744 EXPECT_VERIFIES(v_i, WASM_IF_ELSE(WASM_GET_LOCAL(0),
750 WASM_SET_LOCAL(0, WASM_ZERO), WASM_NOP)); 745 WASM_SET_LOCAL(0, WASM_ZERO), WASM_NOP));
751 } 746 }
752 747
753 TEST_F(FunctionBodyDecoderTest, IfBlock1) { 748 TEST_F(FunctionBodyDecoderTest, IfBlock1) {
754 EXPECT_VERIFIES( 749 EXPECT_VERIFIES(
755 v_i, WASM_IF_ELSE(WASM_GET_LOCAL(0), B1(WASM_SET_LOCAL(0, WASM_ZERO)), 750 v_i, WASM_IF_ELSE(WASM_GET_LOCAL(0), B1(WASM_SET_LOCAL(0, WASM_ZERO)),
(...skipping 1972 matching lines...) Expand 10 before | Expand all | Expand 10 after
2728 iter.next(); 2723 iter.next();
2729 EXPECT_TRUE(iter.has_next()); 2724 EXPECT_TRUE(iter.has_next());
2730 EXPECT_EQ(kExprI32Const, iter.current()); 2725 EXPECT_EQ(kExprI32Const, iter.current());
2731 iter.next(); 2726 iter.next();
2732 EXPECT_FALSE(iter.has_next()); 2727 EXPECT_FALSE(iter.has_next());
2733 } 2728 }
2734 2729
2735 } // namespace wasm 2730 } // namespace wasm
2736 } // namespace internal 2731 } // namespace internal
2737 } // namespace v8 2732 } // namespace v8
OLDNEW
« no previous file with comments | « test/unittests/value-serializer-unittest.cc ('k') | test/unittests/wasm/module-decoder-unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698