OLD | NEW |
1 //===- llvm/unittest/Bitcode/NaClMungedIoTest.cpp -------------------------===// | 1 //===- llvm/unittest/Bitcode/NaClMungedIoTest.cpp -------------------------===// |
2 // Tests munging NaCl bitcode records. | 2 // Tests munging NaCl bitcode records. |
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 |
11 // For class NaClMungedBitcode, tests reading initial sequence of records and | 11 // For class NaClMungedBitcode, tests reading initial sequence of records and |
12 // writing out munged set of bitcode records. | 12 // writing out munged set of bitcode records. |
13 | 13 |
14 #include "llvm/ADT/STLExtras.h" | 14 #include "NaClMungeTest.h" |
15 #include "llvm/Bitcode/NaCl/NaClBitcodeMungeUtils.h" | |
16 #include "llvm/Bitcode/NaCl/NaClBitcodeMunge.h" | |
17 | 15 |
18 #include "gtest/gtest.h" | 16 namespace naclmungetest { |
19 | 17 |
20 using namespace llvm; | 18 using namespace llvm; |
21 | 19 |
22 namespace { | |
23 | |
24 static const uint64_t Terminator = 0x5768798008978675LL; | |
25 | |
26 #define ARRAY_ARGS(name) name, array_lengthof(name), Terminator | |
27 | |
28 std::string stringify(NaClMungedBitcode &MungedBitcode) { | |
29 std::string Buffer; | |
30 raw_string_ostream StrBuf(Buffer); | |
31 MungedBitcode.print(StrBuf); | |
32 return StrBuf.str(); | |
33 } | |
34 | |
35 typedef SmallVector<char, 1024> TextBuffer; | 20 typedef SmallVector<char, 1024> TextBuffer; |
36 | 21 |
37 // Writes out a sequence of munged bitcode records, and writes them into | 22 // Writes out a sequence of munged bitcode records, and writes them into |
38 // the text buffer. Returns a corresponding memory buffer containing | 23 // the text buffer. Returns a corresponding memory buffer containing |
39 // the munged bitcode records. | 24 // the munged bitcode records. |
40 std::unique_ptr<MemoryBuffer> writeMungedBitcode( | 25 std::unique_ptr<MemoryBuffer> writeMungedBitcode( |
41 NaClMungedBitcode &Bitcode, TextBuffer &Buffer, | 26 NaClMungedBitcode &Bitcode, TextBuffer &Buffer, |
42 NaClMungedBitcode::WriteFlags &Flags) { | 27 NaClMungedBitcode::WriteFlags &Flags) { |
43 Bitcode.write(Buffer, /* AddHeader = */ true, Flags); | 28 Bitcode.write(Buffer, /* AddHeader = */ true, Flags); |
44 StringRef Input(Buffer.data(), Buffer.size()); | 29 StringRef Input(Buffer.data(), Buffer.size()); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 3, naclbitc::MODULE_CODE_FUNCTION, 1, 0, 0, 3, Terminator, | 62 3, naclbitc::MODULE_CODE_FUNCTION, 1, 0, 0, 3, Terminator, |
78 1, naclbitc::BLK_CODE_ENTER, 12, 3, Terminator, | 63 1, naclbitc::BLK_CODE_ENTER, 12, 3, Terminator, |
79 3, naclbitc::FUNC_CODE_DECLAREBLOCKS, 1, Terminator, | 64 3, naclbitc::FUNC_CODE_DECLAREBLOCKS, 1, Terminator, |
80 4, naclbitc::FUNC_CODE_INST_RET, Terminator, | 65 4, naclbitc::FUNC_CODE_INST_RET, Terminator, |
81 0, naclbitc::BLK_CODE_EXIT, Terminator, | 66 0, naclbitc::BLK_CODE_EXIT, Terminator, |
82 0, naclbitc::BLK_CODE_EXIT, Terminator, | 67 0, naclbitc::BLK_CODE_EXIT, Terminator, |
83 }; | 68 }; |
84 | 69 |
85 // Show a more readable form of what the program is. | 70 // Show a more readable form of what the program is. |
86 TEST(NaClMungedIoTest, TestDumpingBitcode) { | 71 TEST(NaClMungedIoTest, TestDumpingBitcode) { |
87 NaClObjDumpMunger DumpMunger(ARRAY_ARGS(Records)); | 72 NaClObjDumpMunger DumpMunger(ARRAY_TERM(Records)); |
88 EXPECT_TRUE(DumpMunger.runTest("Display assembly")); | 73 EXPECT_TRUE(DumpMunger.runTest()); |
89 EXPECT_EQ( | 74 EXPECT_EQ( |
90 " 0:0|<65532, 80, 69, 88, 69, 1, 0,|Magic Number: 'PEXE' (80, 69, " | 75 " 0:0|<65532, 80, 69, 88, 69, 1, 0,|Magic Number: 'PEXE' (80, 69, " |
91 "88, 69)\n" | 76 "88, 69)\n" |
92 " | 8, 0, 17, 0, 4, 0, 2, 0, 0, |PNaCl Version: 2\n" | 77 " | 8, 0, 17, 0, 4, 0, 2, 0, 0, |PNaCl Version: 2\n" |
93 " | 0> |\n" | 78 " | 0> |\n" |
94 " 16:0|1: <65535, 8, 2> |module { // BlockID = 8\n" | 79 " 16:0|1: <65535, 8, 2> |module { // BlockID = 8\n" |
95 " 24:0| 3: <1, 1> | version 1;\n" | 80 " 24:0| 3: <1, 1> | version 1;\n" |
96 " 26:4| 1: <65535, 0, 2> | abbreviations { // BlockID " | 81 " 26:4| 1: <65535, 0, 2> | abbreviations { // BlockID " |
97 "= 0\n" | 82 "= 0\n" |
98 " 36:0| 3: <1, 12> | function:\n" | 83 " 36:0| 3: <1, 12> | function:\n" |
(...skipping 16 matching lines...) Expand all Loading... |
115 " | | %b0:\n" | 100 " | | %b0:\n" |
116 " 78:5| 4: <10> | ret void; <@a0>\n" | 101 " 78:5| 4: <10> | ret void; <@a0>\n" |
117 " 79:0| 0: <65534> | }\n" | 102 " 79:0| 0: <65534> | }\n" |
118 " 80:0|0: <65534> |}\n", | 103 " 80:0|0: <65534> |}\n", |
119 DumpMunger.getTestResults()); | 104 DumpMunger.getTestResults()); |
120 } | 105 } |
121 | 106 |
122 // Test that we can write out bitcode, and then read it back in. | 107 // Test that we can write out bitcode, and then read it back in. |
123 TEST(NaClMungedIoTest, TestWriteThenRead) { | 108 TEST(NaClMungedIoTest, TestWriteThenRead) { |
124 // Create munged bitcode for the given records. | 109 // Create munged bitcode for the given records. |
125 NaClMungedBitcode Bitcode(ARRAY_ARGS(Records)); | 110 NaClMungedBitcode Bitcode(ARRAY_TERM(Records)); |
126 | 111 |
127 // The expected output when stringifying this input. | 112 // The expected output when stringifying this input. |
128 const std::string ExpectedRecords( | 113 const std::string ExpectedRecords( |
129 " 1: [65535, 8, 2]\n" | 114 " 1: [65535, 8, 2]\n" |
130 " 3: [1, 1]\n" | 115 " 3: [1, 1]\n" |
131 " 1: [65535, 0, 2]\n" | 116 " 1: [65535, 0, 2]\n" |
132 " 3: [1, 12]\n" | 117 " 3: [1, 12]\n" |
133 " 2: [65533, 1, 1, 10]\n" | 118 " 2: [65533, 1, 1, 10]\n" |
134 " 0: [65534]\n" | 119 " 0: [65534]\n" |
135 " 1: [65535, 17, 3]\n" | 120 " 1: [65535, 17, 3]\n" |
(...skipping 13 matching lines...) Expand all Loading... |
149 // Write and read the bitcode back into a sequence of records. | 134 // Write and read the bitcode back into a sequence of records. |
150 EXPECT_EQ(ExpectedRecords, parseWrittenMungedBitcode(Bitcode)); | 135 EXPECT_EQ(ExpectedRecords, parseWrittenMungedBitcode(Bitcode)); |
151 } | 136 } |
152 | 137 |
153 | 138 |
154 // Test that writing truncated bitcode is difficult, due to word | 139 // Test that writing truncated bitcode is difficult, due to word |
155 // alignment requirements for bitcode files. Note: Bitcode files must | 140 // alignment requirements for bitcode files. Note: Bitcode files must |
156 // be divisible by 4. | 141 // be divisible by 4. |
157 TEST(NaClMungedIoTest, TestTruncatedNonalignedBitcode) { | 142 TEST(NaClMungedIoTest, TestTruncatedNonalignedBitcode) { |
158 // Created an example of a truncated bitcode file. | 143 // Created an example of a truncated bitcode file. |
159 NaClMungedBitcode Bitcode(ARRAY_ARGS(Records)); | 144 NaClMungedBitcode Bitcode(ARRAY_TERM(Records)); |
160 for (size_t i = 2, e = Bitcode.getBaseRecords().size(); i < e; ++i) | 145 for (size_t i = 2, e = Bitcode.getBaseRecords().size(); i < e; ++i) |
161 Bitcode.remove(i); | 146 Bitcode.remove(i); |
162 | 147 |
163 // The expected output when stringifying this input. | 148 // The expected output when stringifying this input. |
164 EXPECT_EQ( | 149 EXPECT_EQ( |
165 " 1: [65535, 8, 2]\n" | 150 " 1: [65535, 8, 2]\n" |
166 " 3: [1, 1]\n", | 151 " 3: [1, 1]\n", |
167 stringify(Bitcode)); | 152 stringify(Bitcode)); |
168 | 153 |
169 // Show that we can't write the bitcode correctly. | 154 // Show that we can't write the bitcode correctly. |
170 TextBuffer WriteBuffer; | 155 TextBuffer WriteBuffer; |
171 std::string LogBuffer; | 156 std::string LogBuffer; |
172 raw_string_ostream StrBuf(LogBuffer); | 157 raw_string_ostream StrBuf(LogBuffer); |
173 NaClMungedBitcode::WriteFlags Flags; | 158 NaClMungedBitcode::WriteFlags Flags; |
174 Flags.setErrStream(StrBuf); | 159 Flags.setErrStream(StrBuf); |
175 writeMungedBitcode(Bitcode, WriteBuffer, Flags); | 160 writeMungedBitcode(Bitcode, WriteBuffer, Flags); |
176 EXPECT_EQ( | 161 EXPECT_EQ( |
177 "Error (Block 8): Missing close block.\n", | 162 "Error (Block 8): Missing close block.\n", |
178 StrBuf.str()); | 163 StrBuf.str()); |
179 } | 164 } |
180 | 165 |
181 } // end of anonymous namespace | 166 } // end of namespace naclmungetest |
OLD | NEW |