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

Side by Side Diff: unittests/Bitcode/NaClMungeWriteErrorTests.cpp

Issue 1157273004: Check for invalid abbreviation operators in munged bitcode. (Closed) Base URL: https://chromium.googlesource.com/native_client/pnacl-llvm.git@master
Patch Set: Fix nit. Remove copied comment from tests. Created 5 years, 7 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 //===- llvm/unittest/Bitcode/NaClMungeWriteErrorTests.cpp -----------------===// 1 //===- llvm/unittest/Bitcode/NaClMungeWriteErrorTests.cpp -----------------===//
2 // Tests parser for PNaCl bitcode instructions. 2 // Tests parser for PNaCl bitcode instructions.
3 // 3 //
4 // The LLVM Compiler Infrastructure 4 // The LLVM Compiler Infrastructure
5 // 5 //
6 // This file is distributed under the University of Illinois Open Source 6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details. 7 // License. See LICENSE.TXT for details.
8 // 8 //
9 //===----------------------------------------------------------------------===// 9 //===----------------------------------------------------------------------===//
10 10
(...skipping 20 matching lines...) Expand all
31 0, naclbitc::BLK_CODE_EXIT, Terminator, 31 0, naclbitc::BLK_CODE_EXIT, Terminator,
32 3, naclbitc::MODULE_CODE_FUNCTION, 1, 0, 0, 0, Terminator, 32 3, naclbitc::MODULE_CODE_FUNCTION, 1, 0, 0, 0, Terminator,
33 1, naclbitc::BLK_CODE_ENTER, naclbitc::FUNCTION_BLOCK_ID, 2, Terminator, 33 1, naclbitc::BLK_CODE_ENTER, naclbitc::FUNCTION_BLOCK_ID, 2, Terminator,
34 3, naclbitc::FUNC_CODE_DECLAREBLOCKS, 1, Terminator, 34 3, naclbitc::FUNC_CODE_DECLAREBLOCKS, 1, Terminator,
35 3, naclbitc::FUNC_CODE_INST_RET, Terminator, 35 3, naclbitc::FUNC_CODE_INST_RET, Terminator,
36 0, naclbitc::BLK_CODE_EXIT, Terminator, 36 0, naclbitc::BLK_CODE_EXIT, Terminator,
37 0, naclbitc::BLK_CODE_EXIT, Terminator 37 0, naclbitc::BLK_CODE_EXIT, Terminator
38 }; 38 };
39 39
40 // Expected output when bitcode records are dumped. 40 // Expected output when bitcode records are dumped.
41 const char* ExpectedDump = 41 const char *ExpectedDump =
42 " 0:0|<65532, 80, 69, 88, 69, 1, 0,|Magic Number: 'PEXE' (80, 69, " 42 " 0:0|<65532, 80, 69, 88, 69, 1, 0,|Magic Number: 'PEXE' (80, 69, "
43 "88, 69)\n" 43 "88, 69)\n"
44 " | 8, 0, 17, 0, 4, 0, 2, 0, 0, |PNaCl Version: 2\n" 44 " | 8, 0, 17, 0, 4, 0, 2, 0, 0, |PNaCl Version: 2\n"
45 " | 0> |\n" 45 " | 0> |\n"
46 " 16:0|1: <65535, 8, 2> |module { // BlockID = 8\n" 46 " 16:0|1: <65535, 8, 2> |module { // BlockID = 8\n"
47 " 24:0| 1: <65535, 17, 3> | types { // BlockID = 17\n" 47 " 24:0| 1: <65535, 17, 3> | types { // BlockID = 17\n"
48 " 32:0| 3: <1, 2> | count 2;\n" 48 " 32:0| 3: <1, 2> | count 2;\n"
49 " 34:5| 3: <2> | @t0 = void;\n" 49 " 34:5| 3: <2> | @t0 = void;\n"
50 " 36:4| 3: <21, 0, 0> | @t1 = void ();\n" 50 " 36:4| 3: <21, 0, 0> | @t1 = void ();\n"
51 " 39:7| 0: <65534> | }\n" 51 " 39:7| 0: <65534> | }\n"
52 " 44:0| 3: <8, 1, 0, 0, 0> | define external void @f0();\n" 52 " 44:0| 3: <8, 1, 0, 0, 0> | define external void @f0();\n"
53 " 48:6| 1: <65535, 12, 2> | function void @f0() { \n" 53 " 48:6| 1: <65535, 12, 2> | function void @f0() { \n"
54 " | | // BlockID " 54 " | | // BlockID "
55 "= 12\n" 55 "= 12\n"
56 " 56:0| 3: <1, 1> | blocks 1;\n" 56 " 56:0| 3: <1, 1> | blocks 1;\n"
57 " | | %b0:\n" 57 " | | %b0:\n"
58 " 58:4| 3: <10> | ret void;\n" 58 " 58:4| 3: <10> | ret void;\n"
59 " 60:2| 0: <65534> | }\n" 59 " 60:2| 0: <65534> | }\n"
60 " 64:0|0: <65534> |}\n" 60 " 64:0|0: <65534> |}\n"
61 ; 61 ;
62 62
63 // Same as above, but just the records as generated by NaClWriteMunger.
jvoung (off chromium) 2015/05/27 20:09:37 I don't see a class named NaClWriteMunger.
Karl 2015/05/28 20:50:31 I was trying to clarify what tool generated the ou
64 const char *ExpectedRecords =
65 " 1: [65535, 8, 2]\n"
66 " 1: [65535, 17, 3]\n"
67 " 3: [1, 2]\n"
68 " 3: [2]\n"
69 " 3: [21, 0, 0]\n"
70 " 0: [65534]\n"
71 " 3: [8, 1, 0, 0, 0]\n"
72 " 1: [65535, 12, 2]\n"
73 " 3: [1, 1]\n"
74 " 3: [10]\n"
75 " 0: [65534]\n"
76 " 0: [65534]\n";
77
78 const char *UnableToContinue =
79 "Error: Unable to generate bitcode file due to write errors\n";
80
63 // Edit to change void type with an illegal abbreviation index. 81 // Edit to change void type with an illegal abbreviation index.
64 const uint64_t VoidTypeIndex = 3; // Index for "@t0 = void". 82 const uint64_t VoidTypeIndex = 3; // Index for "@t0 = void".
65 const uint64_t AbbrevIndex4VoidTypeEdit[] = { 83 const uint64_t AbbrevIndex4VoidTypeEdit[] = {
66 VoidTypeIndex, NaClMungedBitcode::Replace, 84 VoidTypeIndex, NaClMungedBitcode::Replace,
67 4, naclbitc::TYPE_CODE_VOID, Terminator, 85 4, naclbitc::TYPE_CODE_VOID, Terminator,
68 }; 86 };
69 87
70 // Edit to add local abbreviation for "ret void", and then use on that 88 // Edit to add local abbreviation for "ret void", and then use on that
71 // instruction. 89 // instruction.
72 const uint64_t RetVoidIndex = 9; // return void; 90 const uint64_t RetVoidIndex = 9; // return void;
(...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after
338 RetVoidIndex, NaClMungedBitcode::AddBefore, 356 RetVoidIndex, NaClMungedBitcode::AddBefore,
339 // Bad abbreviation! Intentionally leave out "literal" operand: 1 357 // Bad abbreviation! Intentionally leave out "literal" operand: 1
340 2, naclbitc::BLK_CODE_DEFINE_ABBREV, 1, // 1, 358 2, naclbitc::BLK_CODE_DEFINE_ABBREV, 1, // 1,
341 naclbitc::FUNC_CODE_INST_RET, Terminator, 359 naclbitc::FUNC_CODE_INST_RET, Terminator,
342 }; 360 };
343 361
344 // Show that the error is detected. 362 // Show that the error is detected.
345 NaClWriteMunger Munger(ARRAY_TERM(BitcodeRecords)); 363 NaClWriteMunger Munger(ARRAY_TERM(BitcodeRecords));
346 EXPECT_FALSE(Munger.runTest(ARRAY(Edits))); 364 EXPECT_FALSE(Munger.runTest(ARRAY(Edits)));
347 EXPECT_EQ( 365 EXPECT_EQ(
348 "Error (Block 12): Error: Bad abbreviation operand encoding 10:" 366 "Error (Block 12): Bad abbreviation operand encoding 10:"
349 " 2: [65533, 1, 10]\n" 367 " 2: [65533, 1, 10]\n"
350 "Error: Unable to generate bitcode file due to write errors\n", 368 "Error: Unable to generate bitcode file due to write errors\n",
351 Munger.getTestResults()); 369 Munger.getTestResults());
352 370
353 // Show that the writer can recover. 371 // Show that the writer can recover.
354 Munger.setTryToRecoverOnWrite(true); 372 Munger.setTryToRecoverOnWrite(true);
355 EXPECT_TRUE(Munger.runTest(ARRAY(Edits))); 373 EXPECT_TRUE(Munger.runTest(ARRAY(Edits)));
356 EXPECT_EQ( 374 EXPECT_EQ(
357 "Error (Block 12): Error: Bad abbreviation operand encoding 10: " 375 "Error (Block 12): Bad abbreviation operand encoding 10: "
358 "2: [65533, 1, 10]\n" 376 "2: [65533, 1, 10]\n"
359 " 1: [65535, 8, 2]\n" 377 " 1: [65535, 8, 2]\n"
360 " 1: [65535, 17, 3]\n" 378 " 1: [65535, 17, 3]\n"
361 " 3: [1, 2]\n" 379 " 3: [1, 2]\n"
362 " 3: [2]\n" 380 " 3: [2]\n"
363 " 3: [21, 0, 0]\n" 381 " 3: [21, 0, 0]\n"
364 " 0: [65534]\n" 382 " 0: [65534]\n"
365 " 3: [8, 1, 0, 0, 0]\n" 383 " 3: [8, 1, 0, 0, 0]\n"
366 " 1: [65535, 12, 3]\n" // Note: not followed by abbreviation def. 384 " 1: [65535, 12, 3]\n" // Note: not followed by abbreviation def.
367 " 3: [1, 1]\n" 385 " 3: [1, 1]\n"
(...skipping 20 matching lines...) Expand all
388 RetVoidIndex, NaClMungedBitcode::AddBefore, // good abbreviation to ignore. 406 RetVoidIndex, NaClMungedBitcode::AddBefore, // good abbreviation to ignore.
389 2, naclbitc::BLK_CODE_DEFINE_ABBREV, 1, 1, 407 2, naclbitc::BLK_CODE_DEFINE_ABBREV, 1, 1,
390 naclbitc::FUNC_CODE_INST_RET, Terminator, 408 naclbitc::FUNC_CODE_INST_RET, Terminator,
391 RetVoidIndex, NaClMungedBitcode::Replace, // reference to good abreviation. 409 RetVoidIndex, NaClMungedBitcode::Replace, // reference to good abreviation.
392 5, naclbitc::FUNC_CODE_INST_RET, Terminator 410 5, naclbitc::FUNC_CODE_INST_RET, Terminator
393 }; 411 };
394 412
395 Munger.setTryToRecoverOnWrite(true); 413 Munger.setTryToRecoverOnWrite(true);
396 EXPECT_TRUE(Munger.runTest(ARRAY(Edits))); 414 EXPECT_TRUE(Munger.runTest(ARRAY(Edits)));
397 EXPECT_EQ( 415 EXPECT_EQ(
398 "Error (Block 12): Error: Bad abbreviation operand encoding 9:" 416 "Error (Block 12): Bad abbreviation operand encoding 9:"
399 " 2: [65533, 1, 9]\n" 417 " 2: [65533, 1, 9]\n"
400 "Error (Block 12): Ignoring abbreviation: 2: [65533, 1, 1, 10]\n" 418 "Error (Block 12): Ignoring abbreviation: 2: [65533, 1, 1, 10]\n"
401 "Error (Block 12): Uses illegal abbreviation index: 5: [10]\n" 419 "Error (Block 12): Uses illegal abbreviation index: 5: [10]\n"
402 " 1: [65535, 8, 2]\n" 420 " 1: [65535, 8, 2]\n"
403 " 1: [65535, 17, 3]\n" 421 " 1: [65535, 17, 3]\n"
404 " 3: [1, 2]\n" 422 " 3: [1, 2]\n"
405 " 3: [2]\n" 423 " 3: [2]\n"
406 " 3: [21, 0, 0]\n" 424 " 3: [21, 0, 0]\n"
407 " 0: [65534]\n" 425 " 0: [65534]\n"
408 " 3: [8, 1, 0, 0, 0]\n" 426 " 3: [8, 1, 0, 0, 0]\n"
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
554 " 3: [1, 1]\n" 572 " 3: [1, 1]\n"
555 " 3: [10]\n" 573 " 3: [10]\n"
556 " 0: [65534]\n" 574 " 0: [65534]\n"
557 " 0: [65534]\n" 575 " 0: [65534]\n"
558 " 1: [65535, 4294967295, 3]\n" 576 " 1: [65535, 4294967295, 3]\n"
559 " 3: [1, 4]\n" 577 " 3: [1, 4]\n"
560 " 0: [65534]\n", 578 " 0: [65534]\n",
561 Munger.getTestResults()); 579 Munger.getTestResults());
562 } 580 }
563 581
582 // Runs write munging tests on BitcodeRecords, inserting bad
583 // abbreviation definitions via Edit. ErrorMessage is the expected
jvoung (off chromium) 2015/05/27 20:09:37 via Edit -> via Edits ?
Karl 2015/05/28 20:50:32 Done.
584 // error message to be generated by the write munger. Assumes the the
jvoung (off chromium) 2015/05/27 20:09:37 the the -> that the
Karl 2015/05/28 20:50:31 Done.
585 // error recovery is to delete the bad abbreviation definitions.
jvoung (off chromium) 2015/05/27 20:09:37 error recover -> error recover method or somethin
Karl 2015/05/28 20:50:31 No longer applicable. Comments were rewritten.
586 void CheckWriteBadAbbrevMunging(const uint64_t *Edits, size_t EditsSize,
587 const char *ErrorMessage) {
588 NaClWriteMunger Munger(ARRAY_TERM(BitcodeRecords));
589 EXPECT_FALSE(Munger.runTest(Edits, EditsSize));
590 std::string BadResults(ErrorMessage);
591 BadResults.append(UnableToContinue);
592 EXPECT_EQ(BadResults, Munger.getTestResults());
593
594 Munger.setTryToRecoverOnWrite(true);
595 EXPECT_TRUE(Munger.runTest(Edits, EditsSize));
596 std::string GoodResults(ErrorMessage);
597 GoodResults.append(ExpectedRecords);
598 EXPECT_EQ(GoodResults, Munger.getTestResults());
599 }
600
601 // Show that inserting an abbreviation with a bad fixed width is dealt with.
602 TEST(NaClMungeWriteErrorTests, InvalidFixedAbbreviationSize) {
603 // Insert bad abbreviation Fixed(36) into type block.
jvoung (off chromium) 2015/05/27 20:09:37 how about static_assert(36 > naclbitc::MaxAbbrevWi
Karl 2015/05/28 20:50:31 Done.
604 const uint64_t Edit[] = {
605 VoidTypeIndex, NaClMungedBitcode::AddBefore,
606 naclbitc::DEFINE_ABBREV, naclbitc::BLK_CODE_DEFINE_ABBREV, 1,
607 0, NaClBitCodeAbbrevOp::Fixed, 36, Terminator
jvoung (off chromium) 2015/05/27 20:09:38 can line up the 0, ... with the other array elems
Karl 2015/05/28 20:50:32 Done.
608 };
609 CheckWriteBadAbbrevMunging(
610 ARRAY(Edit),
611 "Error (Block 17): Invalid abbreviation Fixed(36) in: 2: [65533, 1, 0,"
612 " 1, 36]\n");
613 }
614
615 // Show that inserting an abbreviation with a bad vbr width is dealt with.
616 TEST(NaClMungeWriteErrorTests, InvalidVbrAbbreviationSize) {
617 // Insert bad abbreviation Vbr(36) into type block.
618 const uint64_t Edit[] = {
619 VoidTypeIndex, NaClMungedBitcode::AddBefore,
620 naclbitc::DEFINE_ABBREV, naclbitc::BLK_CODE_DEFINE_ABBREV, 1,
621 0, NaClBitCodeAbbrevOp::VBR, 36,
622 Terminator
623 };
624 CheckWriteBadAbbrevMunging(
625 ARRAY(Edit),
626 "Error (Block 17): Invalid abbreviation VBR(36) in: 2: [65533, 1, 0,"
627 " 2, 36]\n");
628 }
629
630 // Show that the array operator can't appear last.
631 TEST(NaClMungeWriteErrorTests, InvalidArrayAbbreviationLast) {
632 const uint64_t Edit[] = {
633 VoidTypeIndex, NaClMungedBitcode::AddBefore,
634 naclbitc::DEFINE_ABBREV, naclbitc::BLK_CODE_DEFINE_ABBREV, 1,
635 0, NaClBitCodeAbbrevOp::Array, Terminator
636 };
637 CheckWriteBadAbbrevMunging(
638 ARRAY(Edit),
639 "Error (Block 17): Malformed array abbreviation found: 2: [65533, 1,"
640 " 0, 3]\n");
641 }
642
643 // Show that the array operator can't appear before the second to last
644 // operand.
645 TEST(NaClMungeWriteErrorTests, InvalidArrayAbbreviationTooEarly) {
646 const uint64_t Edit[] = {
647 VoidTypeIndex, NaClMungedBitcode::AddBefore,
648 naclbitc::DEFINE_ABBREV, naclbitc::BLK_CODE_DEFINE_ABBREV, 3,
649 0, NaClBitCodeAbbrevOp::Array, // array
650 1, 15, // lit(15)
651 1, 10, // lit(10)
652 Terminator
653 };
654 CheckWriteBadAbbrevMunging(
655 ARRAY(Edit),
656 "Error (Block 17): Array abbreviation must be second to last: 2: [65533,"
657 " 3, 0, 3, 1, 15, 1, 10]\n");
658 }
659
660 // Show that the array operator can't appear as last two operators.
661 TEST(NaClMungeWriteErrorTests, InvalidArrayAbbreviationLastTwo) {
662 const uint64_t Edit[] = {
663 VoidTypeIndex, NaClMungedBitcode::AddBefore,
664 naclbitc::DEFINE_ABBREV, naclbitc::BLK_CODE_DEFINE_ABBREV, 2,
665 0, NaClBitCodeAbbrevOp::Array, // array
666 0, NaClBitCodeAbbrevOp::Array, // array
667 Terminator
668 };
669 CheckWriteBadAbbrevMunging(
670 ARRAY(Edit),
671 "Error (Block 17): Malformed array abbreviation found: 2: [65533, 2,"
672 " 0, 3, 0, 3]\n");
673 }
674
675 // Show that the code checks if specifies too few operands for an
jvoung (off chromium) 2015/05/27 20:09:37 nit: "Show that the code checks if specifies too f
Karl 2015/05/28 20:50:31 Rewritten.
676 // abbreviation, based on the record size.
677 TEST(NaClMungeWriteErrorTests, SpecifiesTooFewOperands) {
678 const uint64_t Edit[] = {
679 VoidTypeIndex, NaClMungedBitcode::AddBefore,
680 naclbitc::DEFINE_ABBREV, naclbitc::BLK_CODE_DEFINE_ABBREV, 1,
681 1, 10, // lit(10)
682 1, 15, // lit(15)
683 Terminator
684 };
685 CheckWriteBadAbbrevMunging(
686 ARRAY(Edit),
687 "Error (Block 17): Error: Too many values for number of operands (1):"
688 " 2: [65533, 1, 1, 10, 1, 15]\n");
689 }
690
691 // Show that the code checks if specifies too many operands for an
692 // abbreviation, based on the record size.
693 TEST(NaClMungeWriteErrorTests, SpecifiesTooManyOperands) {
694 // Insert bad abbreviation Vbr(36) into type block.
695 const uint64_t Edit[] = {
696 VoidTypeIndex, NaClMungedBitcode::AddBefore,
697 naclbitc::DEFINE_ABBREV, naclbitc::BLK_CODE_DEFINE_ABBREV, 3,
698 1, 10, // lit(10)
699 1, 15, // lit(15)
700 Terminator
701 };
702 CheckWriteBadAbbrevMunging(
703 ARRAY(Edit),
704 "Error (Block 17): Malformed abbreviation found. Expects 3 operands"
705 " but found 2: 2: [65533, 3, 1, 10, 1, 15]\n");
706 }
707
564 } // end of namespace naclmungetest 708 } // end of namespace naclmungetest
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698