Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 1 | //===- unittest/IceParseTypesTest.cpp -------------------------------------===// |
| 2 | // Tests parser for PNaCl bitcode. |
| 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 record errors in the types block when parsing PNaCl bitcode. |
| 12 | |
| 13 | // TODO(kschimpf) Add more tests. |
| 14 | |
| 15 | #include "BitcodeMunge.h" |
| 16 | #include "unittests/Bitcode/NaClMungeTest.h" |
| 17 | |
| 18 | using namespace llvm; |
| 19 | using namespace naclmungetest; |
| 20 | using namespace IceTest; |
| 21 | |
| 22 | namespace { |
| 23 | |
| 24 | static const unsigned NO_LOCAL_ABBREVS = |
| 25 | NaClBitsNeededForValue(naclbitc::DEFAULT_MAX_ABBREV); |
| 26 | |
| 27 | const uint64_t BitcodeRecords[] = { |
| 28 | naclbitc::ENTER_SUBBLOCK, naclbitc::BLK_CODE_ENTER, |
| 29 | naclbitc::MODULE_BLOCK_ID, NO_LOCAL_ABBREVS, Terminator, |
| 30 | naclbitc::ENTER_SUBBLOCK, naclbitc::BLK_CODE_ENTER, |
| 31 | naclbitc::TYPE_BLOCK_ID_NEW, NO_LOCAL_ABBREVS, Terminator, |
| 32 | naclbitc::UNABBREV_RECORD, naclbitc::TYPE_CODE_NUMENTRY, 2, Terminator, |
| 33 | naclbitc::UNABBREV_RECORD, naclbitc::TYPE_CODE_INTEGER, 32, Terminator, |
| 34 | naclbitc::UNABBREV_RECORD, naclbitc::TYPE_CODE_FLOAT, Terminator, |
| 35 | naclbitc::END_BLOCK, naclbitc::BLK_CODE_EXIT, Terminator, |
| 36 | naclbitc::END_BLOCK, naclbitc::BLK_CODE_EXIT, Terminator}; |
| 37 | |
| 38 | const char *ExpectedDump = |
| 39 | " 0:0|<65532, 80, 69, 88, 69, 1, 0,|Magic Number: 'PEXE' (80, 69, " |
| 40 | "88, 69)\n" |
| 41 | " | 8, 0, 17, 0, 4, 0, 2, 0, 0, |PNaCl Version: 2\n" |
| 42 | " | 0> |\n" |
| 43 | " 16:0|1: <65535, 8, 2> |module { // BlockID = 8\n" |
| 44 | " 24:0| 1: <65535, 17, 2> | types { // BlockID = 17\n" |
| 45 | " 32:0| 3: <1, 2> | count 2;\n" |
| 46 | " 34:4| 3: <7, 32> | @t0 = i32;\n" |
| 47 | " 37:6| 3: <3> | @t1 = float;\n" |
| 48 | " 39:4| 0: <65534> | }\n" |
| 49 | " 40:0|0: <65534> |}\n"; |
| 50 | |
| 51 | TEST(NaClParseTypesTest, ShowExpectedDump) { |
| 52 | NaClObjDumpMunger Munger(ARRAY_TERM(BitcodeRecords)); |
| 53 | EXPECT_TRUE(Munger.runTest()); |
| 54 | EXPECT_EQ(ExpectedDump, Munger.getTestResults()); |
| 55 | } |
| 56 | |
| 57 | // Show what happens when misdefining: @t1 = float" |
| 58 | TEST(NaClParseTypesTest, BadFloatTypeDefinition) { |
| 59 | // Index for "@t1 = float;" record. |
| 60 | const uint64_t FloatTypeIndex = 4; |
| 61 | const uint64_t Edit[] = {FloatTypeIndex, NaClMungedBitcode::Replace, |
| 62 | // Add extraneous 1 to end of float record. |
| 63 | naclbitc::UNABBREV_RECORD, naclbitc::TYPE_CODE_FLOAT, |
| 64 | 1, Terminator}; |
| 65 | |
| 66 | SubzeroBitcodeMunger Munger(ARRAY_TERM(BitcodeRecords)); |
| 67 | EXPECT_FALSE(Munger.runTest(ARRAY(Edit))); |
| 68 | EXPECT_EQ("Error(37:6): Invalid type record: <3 1>\n", |
| 69 | Munger.getTestResults()); |
| 70 | } |
| 71 | |
| 72 | // Show what happens when the count record value is way too big. |
| 73 | // See: https://code.google.com/p/nativeclient/issues/detail?id=4195 |
| 74 | TEST(NaClParseTypesTest, BadTypeCountRecord) { |
| 75 | // Index for "count 2;". |
| 76 | const uint64_t CountRecordIndex = 2; |
| 77 | const uint64_t Edit[] = { |
| 78 | CountRecordIndex, NaClMungedBitcode::Replace, naclbitc::UNABBREV_RECORD, |
| 79 | naclbitc::TYPE_CODE_NUMENTRY, 18446744073709547964ULL, Terminator}; |
| 80 | |
| 81 | SubzeroBitcodeMunger Munger(ARRAY_TERM(BitcodeRecords)); |
| 82 | Munger.Flags.setGenerateUnitTestMessages(false); |
| 83 | EXPECT_FALSE(Munger.runTest(ARRAY(Edit))); |
| 84 | EXPECT_EQ("Error(32:0): Size to big for count record: 18446744073709547964\n" |
| 85 | "Error(48:4): Types block expected 4294963644 types but found: 2\n", |
| 86 | Munger.getTestResults()); |
| 87 | } |
| 88 | |
| 89 | } // end of anonymous namespace |