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

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

Issue 2628203003: Reland of [wasm] Enforce that function bodies end with the \"end\" opcode. (Closed)
Patch Set: 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 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 // Prepends local variable declarations and renders nice error messages for 107 // Prepends local variable declarations and renders nice error messages for
108 // verification failures. 108 // verification failures.
109 void Verify(ErrorCode expected, FunctionSig* sig, const byte* start, 109 void Verify(ErrorCode expected, FunctionSig* sig, const byte* start,
110 const byte* end) { 110 const byte* end) {
111 local_decls.Prepend(zone(), &start, &end); 111 size_t locals_size = local_decls.Size();
112 size_t total_size = end - start + locals_size + 1;
113 byte* buffer = static_cast<byte*>(zone()->New(total_size));
114 // Prepend the local decls to the code.
115 local_decls.Emit(buffer);
116 // Emit the code.
117 memcpy(buffer + locals_size, start, end - start);
118 // Append an extra end opcode.
119 buffer[total_size - 1] = kExprEnd;
120
121 start = buffer;
122 end = buffer + total_size;
123
112 // Verify the code. 124 // Verify the code.
113 DecodeResult result = VerifyWasmCode( 125 DecodeResult result = VerifyWasmCode(
114 zone()->allocator(), module == nullptr ? nullptr : module->module, sig, 126 zone()->allocator(), module == nullptr ? nullptr : module->module, sig,
115 start, end); 127 start, end);
116 128
117 if (result.error_code != expected) { 129 if (result.error_code != expected) {
118 ptrdiff_t pc = result.error_pc - result.start; 130 ptrdiff_t pc = result.error_pc - result.start;
119 ptrdiff_t pt = result.error_pt - result.start; 131 ptrdiff_t pt = result.error_pt - result.start;
120 std::ostringstream str; 132 std::ostringstream str;
121 if (expected == kSuccess) { 133 if (expected == kSuccess) {
(...skipping 332 matching lines...) Expand 10 before | Expand all | Expand 10 after
454 EXPECT_FAILURE_C(i_i, code); 466 EXPECT_FAILURE_C(i_i, code);
455 } 467 }
456 468
457 TEST_F(FunctionBodyDecoderTest, Block0Block0) { 469 TEST_F(FunctionBodyDecoderTest, Block0Block0) {
458 static const byte code[] = {WASM_EMPTY_BLOCK, WASM_EMPTY_BLOCK}; 470 static const byte code[] = {WASM_EMPTY_BLOCK, WASM_EMPTY_BLOCK};
459 EXPECT_VERIFIES_C(v_v, code); 471 EXPECT_VERIFIES_C(v_v, code);
460 EXPECT_FAILURE_C(i_i, code); 472 EXPECT_FAILURE_C(i_i, code);
461 } 473 }
462 474
463 TEST_F(FunctionBodyDecoderTest, Block0_end) { 475 TEST_F(FunctionBodyDecoderTest, Block0_end) {
464 EXPECT_VERIFIES(v_v, WASM_EMPTY_BLOCK, kExprEnd); 476 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 } 477 }
470 478
471 TEST_F(FunctionBodyDecoderTest, Block1) { 479 TEST_F(FunctionBodyDecoderTest, Block1) {
472 byte code[] = {WASM_BLOCK_I(WASM_GET_LOCAL(0))}; 480 byte code[] = {WASM_BLOCK_I(WASM_GET_LOCAL(0))};
473 EXPECT_VERIFIES_C(i_i, code); 481 EXPECT_VERIFIES_C(i_i, code);
474 EXPECT_FAILURE_C(v_i, code); 482 EXPECT_FAILURE_C(v_i, code);
475 EXPECT_FAILURE_C(d_dd, code); 483 EXPECT_FAILURE_C(d_dd, code);
476 EXPECT_FAILURE_C(i_f, code); 484 EXPECT_FAILURE_C(i_f, code);
477 EXPECT_FAILURE_C(i_d, code); 485 EXPECT_FAILURE_C(i_d, code);
478 } 486 }
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after
711 } 719 }
712 720
713 TEST_F(FunctionBodyDecoderTest, IfNop) { 721 TEST_F(FunctionBodyDecoderTest, IfNop) {
714 EXPECT_VERIFIES(v_i, WASM_IF(WASM_GET_LOCAL(0), WASM_NOP)); 722 EXPECT_VERIFIES(v_i, WASM_IF(WASM_GET_LOCAL(0), WASM_NOP));
715 } 723 }
716 724
717 TEST_F(FunctionBodyDecoderTest, IfNopElseNop) { 725 TEST_F(FunctionBodyDecoderTest, IfNopElseNop) {
718 EXPECT_VERIFIES(v_i, WASM_IF_ELSE(WASM_GET_LOCAL(0), WASM_NOP, WASM_NOP)); 726 EXPECT_VERIFIES(v_i, WASM_IF_ELSE(WASM_GET_LOCAL(0), WASM_NOP, WASM_NOP));
719 } 727 }
720 728
729 TEST_F(FunctionBodyDecoderTest, If_end) {
730 static const byte code[] = {kExprGetLocal, 0, WASM_IF_OP, kExprEnd};
731 EXPECT_VERIFIES_C(v_i, code);
732 }
733
721 TEST_F(FunctionBodyDecoderTest, If_end_end) { 734 TEST_F(FunctionBodyDecoderTest, If_end_end) {
722 static const byte code[] = {kExprGetLocal, 0, WASM_IF_OP, kExprEnd, kExprEnd}; 735 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};
729 EXPECT_FAILURE_C(v_i, code); 736 EXPECT_FAILURE_C(v_i, code);
730 } 737 }
731 738
732 TEST_F(FunctionBodyDecoderTest, If_falloff1) { 739 TEST_F(FunctionBodyDecoderTest, If_falloff1) {
733 static const byte code[] = {kExprGetLocal, 0, kExprIf}; 740 static const byte code[] = {kExprGetLocal, 0, kExprIf};
734 EXPECT_FAILURE_C(v_i, code); 741 EXPECT_FAILURE_C(v_i, code);
735 } 742 }
736 743
737 TEST_F(FunctionBodyDecoderTest, If_falloff2) { 744 TEST_F(FunctionBodyDecoderTest, If_falloff2) {
738 static const byte code[] = {kExprGetLocal, 0, WASM_IF_OP}; 745 static const byte code[] = {kExprGetLocal, 0, WASM_IF_OP};
(...skipping 1989 matching lines...) Expand 10 before | Expand all | Expand 10 after
2728 iter.next(); 2735 iter.next();
2729 EXPECT_TRUE(iter.has_next()); 2736 EXPECT_TRUE(iter.has_next());
2730 EXPECT_EQ(kExprI32Const, iter.current()); 2737 EXPECT_EQ(kExprI32Const, iter.current());
2731 iter.next(); 2738 iter.next();
2732 EXPECT_FALSE(iter.has_next()); 2739 EXPECT_FALSE(iter.has_next());
2733 } 2740 }
2734 2741
2735 } // namespace wasm 2742 } // namespace wasm
2736 } // namespace internal 2743 } // namespace internal
2737 } // namespace v8 2744 } // 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