| OLD | NEW |
| (Empty) |
| 1 //===- llvm/unittest/Bitcode/NaClAbbrevErrorTests.cpp ---------------------===// | |
| 2 // Tests parser for PNaCl bitcode instructions. | |
| 3 // | |
| 4 // The LLVM Compiler Infrastructure | |
| 5 // | |
| 6 // This file is distributed under the University of Illinois Open Source | |
| 7 // License. See LICENSE.TXT for details. | |
| 8 // | |
| 9 //===----------------------------------------------------------------------===// | |
| 10 | |
| 11 // Tests errors on bad abbreviation index. | |
| 12 | |
| 13 #include "llvm/ADT/STLExtras.h" | |
| 14 #include "llvm/Bitcode/NaCl/NaClBitcodeMunge.h" | |
| 15 #include "llvm/Bitcode/NaCl/NaClBitcodeParser.h" | |
| 16 #include "llvm/Bitcode/NaCl/NaClLLVMBitCodes.h" | |
| 17 | |
| 18 #include "gtest/gtest.h" | |
| 19 | |
| 20 using namespace llvm; | |
| 21 | |
| 22 namespace { | |
| 23 | |
| 24 static const uint64_t Terminator = 0x5768798008978675LL; | |
| 25 | |
| 26 /// Test if we handle badly defined abbreviation indices. | |
| 27 TEST(MyDeathNaClAbbrevErrorTests, BadAbbreviationIndex) { | |
| 28 const uint64_t BitcodeRecords[] = { | |
| 29 1, naclbitc::BLK_CODE_ENTER, naclbitc::MODULE_BLOCK_ID, 2, Terminator, | |
| 30 1, naclbitc::BLK_CODE_ENTER, naclbitc::TYPE_BLOCK_ID_NEW, 3, Terminator, | |
| 31 3, naclbitc::TYPE_CODE_NUMENTRY, 2, Terminator, | |
| 32 3, naclbitc::TYPE_CODE_VOID, Terminator, | |
| 33 3, naclbitc::TYPE_CODE_FUNCTION, 0, 0, Terminator, | |
| 34 0, naclbitc::BLK_CODE_EXIT, Terminator, | |
| 35 3, naclbitc::MODULE_CODE_FUNCTION, 1, 0, 0, 0, Terminator, | |
| 36 1, naclbitc::BLK_CODE_ENTER, naclbitc::FUNCTION_BLOCK_ID, 2, Terminator, | |
| 37 3, naclbitc::FUNC_CODE_DECLAREBLOCKS, 1, Terminator, | |
| 38 3, naclbitc::FUNC_CODE_INST_RET, Terminator, | |
| 39 0, naclbitc::BLK_CODE_EXIT, Terminator, | |
| 40 0, naclbitc::BLK_CODE_EXIT, Terminator | |
| 41 }; | |
| 42 | |
| 43 const char* ExpectedDump = | |
| 44 " 0:0|<65532, 80, 69, 88, 69, 1, 0,|Magic Number: 'PEXE' (80, 69, " | |
| 45 "88, 69)\n" | |
| 46 " | 8, 0, 17, 0, 4, 0, 2, 0, 0, |PNaCl Version: 2\n" | |
| 47 " | 0> |\n" | |
| 48 " 16:0|1: <65535, 8, 2> |module { // BlockID = 8\n" | |
| 49 " 24:0| 1: <65535, 17, 3> | types { // BlockID = 17\n" | |
| 50 " 32:0| 3: <1, 2> | count 2;\n" | |
| 51 " 34:5| 3: <2> | @t0 = void;\n" | |
| 52 " 36:4| 3: <21, 0, 0> | @t1 = void ();\n" | |
| 53 " 39:7| 0: <65534> | }\n" | |
| 54 " 44:0| 3: <8, 1, 0, 0, 0> | define external void @f0();\n" | |
| 55 " 48:6| 1: <65535, 12, 2> | function void @f0() { \n" | |
| 56 " | | // BlockID " | |
| 57 "= 12\n" | |
| 58 " 56:0| 3: <1, 1> | blocks 1;\n" | |
| 59 " | | %b0:\n" | |
| 60 " 58:4| 3: <10> | ret void;\n" | |
| 61 " 60:2| 0: <65534> | }\n" | |
| 62 " 64:0|0: <65534> |}\n" | |
| 63 ; | |
| 64 | |
| 65 // Show that we can parse this code. | |
| 66 NaClObjDumpMunger DumpMunger(BitcodeRecords, | |
| 67 array_lengthof(BitcodeRecords), Terminator); | |
| 68 | |
| 69 EXPECT_TRUE(DumpMunger.runTest("BadAbbreviationIndex assembly")); | |
| 70 EXPECT_EQ(ExpectedDump, DumpMunger.getTestResults()); | |
| 71 | |
| 72 // Shows what happens when we change the abbreviation index to an | |
| 73 // illegal value. | |
| 74 const uint64_t ReplaceIndex = 3; // Index for TYPE_CODE_VOID; | |
| 75 const uint64_t AbbrevIndex4[] = { | |
| 76 ReplaceIndex, NaClMungedBitcode::Replace, | |
| 77 4, naclbitc::TYPE_CODE_VOID, Terminator, | |
| 78 }; | |
| 79 | |
| 80 // Show that by default, one can't write a bad abbreviation index. | |
| 81 { | |
| 82 NaClObjDumpMunger DumpMunger(BitcodeRecords, | |
| 83 array_lengthof(BitcodeRecords), Terminator); | |
| 84 DumpMunger.setRunAsDeathTest(true); | |
| 85 EXPECT_DEATH( | |
| 86 DumpMunger.runTest("Bad abbreviation index 4", | |
| 87 AbbrevIndex4, array_lengthof(AbbrevIndex4)), | |
| 88 ".*Error \\(Block 17\\)\\: Uses illegal abbreviation index\\:" | |
| 89 " 4\\: \\[2\\].*"); | |
| 90 } | |
| 91 | |
| 92 // Show that the corresponding error is generated when reading | |
| 93 // bitcode with a bad abbreviation index. | |
| 94 { | |
| 95 NaClObjDumpMunger DumpMunger(BitcodeRecords, | |
| 96 array_lengthof(BitcodeRecords), Terminator); | |
| 97 DumpMunger.setRunAsDeathTest(true); | |
| 98 DumpMunger.setWriteBadAbbrevIndex(true); | |
| 99 EXPECT_DEATH( | |
| 100 DumpMunger.runTest("Bad abbreviation index 4", | |
| 101 AbbrevIndex4, array_lengthof(AbbrevIndex4)), | |
| 102 ".*Error \\(Block 17\\)\\: Uses illegal abbreviation index\\:" | |
| 103 " 4\\: \\[2\\].*" | |
| 104 ".*Fatal\\(35\\:0\\)\\: Invalid abbreviation \\# 4 defined for record.*"); | |
| 105 } | |
| 106 | |
| 107 // Test that bitcode reader reports problem correctly. | |
| 108 { | |
| 109 NaClParseBitcodeMunger Munger(BitcodeRecords, | |
| 110 array_lengthof(BitcodeRecords), Terminator); | |
| 111 Munger.setRunAsDeathTest(true); | |
| 112 Munger.setWriteBadAbbrevIndex(true); | |
| 113 EXPECT_DEATH( | |
| 114 Munger.runTest("Bad abbreviation index", | |
| 115 AbbrevIndex4, array_lengthof(AbbrevIndex4), true), | |
| 116 ".*Error \\(Block 17\\)\\: Uses illegal abbreviation index\\:" | |
| 117 " 4\\: \\[2\\].*" | |
| 118 ".*Fatal\\(35\\:0\\)\\: Invalid abbreviation \\# 4 defined for record.*"); | |
| 119 } | |
| 120 | |
| 121 // Show that error recovery works when dumping bitcode. | |
| 122 DumpMunger.setTryToRecoverOnWrite(true); | |
| 123 EXPECT_TRUE( | |
| 124 DumpMunger.runTest("Bad abbreviation index 4", | |
| 125 AbbrevIndex4, array_lengthof(AbbrevIndex4))); | |
| 126 std::string Results( | |
| 127 "Error (Block 17): Uses illegal abbreviation index: 4: [2]\n"); | |
| 128 Results.append(ExpectedDump); | |
| 129 EXPECT_EQ(Results, DumpMunger.getTestResults()); | |
| 130 | |
| 131 // Show that error recovery works when parsing bitcode. | |
| 132 NaClParseBitcodeMunger Munger(BitcodeRecords, | |
| 133 array_lengthof(BitcodeRecords), Terminator); | |
| 134 Munger.setTryToRecoverOnWrite(true); | |
| 135 EXPECT_TRUE( | |
| 136 Munger.runTest("Bad abbreviation index 4", | |
| 137 AbbrevIndex4, array_lengthof(AbbrevIndex4), true)); | |
| 138 EXPECT_EQ( | |
| 139 "Error (Block 17): Uses illegal abbreviation index: 4: [2]\n" | |
| 140 "Successful parse!\n", | |
| 141 Munger.getTestResults()); | |
| 142 } | |
| 143 | |
| 144 } // end of anonymous namespace. | |
| OLD | NEW |