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

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

Issue 1140153004: Clean up bitcode munging tests. (Closed) Base URL: https://chromium.googlesource.com/native_client/pnacl-llvm.git@master
Patch Set: Fix issues in patch set 2. 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
« no previous file with comments | « unittests/Bitcode/NaClMungeTest.h ('k') | unittests/Bitcode/NaClMungedBitcodeTest.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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
11 // Tests write errors for munged bitcode. 11 // Tests write errors for munged bitcode.
12 12
13 #include "llvm/ADT/STLExtras.h" 13 #include "NaClMungeTest.h"
14 #include "llvm/Bitcode/NaCl/NaClBitcodeMunge.h" 14
15 #include "llvm/Bitcode/NaCl/NaClBitcodeParser.h" 15 #include "llvm/Bitcode/NaCl/NaClBitcodeParser.h"
16 #include "llvm/Bitcode/NaCl/NaClLLVMBitCodes.h" 16 #include "llvm/Bitcode/NaCl/NaClLLVMBitCodes.h"
17 17
18 #include "gtest/gtest.h" 18 #include "gtest/gtest.h"
19 19
20 using namespace llvm; 20 using namespace llvm;
21 21
22 namespace { 22 namespace naclmungetest {
23 23
24 // Test list of bitcode records. 24 // Test list of bitcode records.
25 static const uint64_t Terminator = 0x5768798008978675LL; 25 const uint64_t BitcodeRecords[] = {
26 const uint64_t BitcodeRecords[] = { 26 1, naclbitc::BLK_CODE_ENTER, naclbitc::MODULE_BLOCK_ID, 2, Terminator,
27 1, naclbitc::BLK_CODE_ENTER, naclbitc::MODULE_BLOCK_ID, 2, Terminator, 27 1, naclbitc::BLK_CODE_ENTER, naclbitc::TYPE_BLOCK_ID_NEW, 3, Terminator,
28 1, naclbitc::BLK_CODE_ENTER, naclbitc::TYPE_BLOCK_ID_NEW, 3, Terminator, 28 3, naclbitc::TYPE_CODE_NUMENTRY, 2, Terminator,
29 3, naclbitc::TYPE_CODE_NUMENTRY, 2, Terminator, 29 3, naclbitc::TYPE_CODE_VOID, Terminator,
30 3, naclbitc::TYPE_CODE_VOID, Terminator, 30 3, naclbitc::TYPE_CODE_FUNCTION, 0, 0, Terminator,
31 3, naclbitc::TYPE_CODE_FUNCTION, 0, 0, Terminator, 31 0, naclbitc::BLK_CODE_EXIT, Terminator,
32 0, naclbitc::BLK_CODE_EXIT, Terminator, 32 3, naclbitc::MODULE_CODE_FUNCTION, 1, 0, 0, 0, Terminator,
33 3, naclbitc::MODULE_CODE_FUNCTION, 1, 0, 0, 0, Terminator, 33 1, naclbitc::BLK_CODE_ENTER, naclbitc::FUNCTION_BLOCK_ID, 2, Terminator,
34 1, naclbitc::BLK_CODE_ENTER, naclbitc::FUNCTION_BLOCK_ID, 2, Terminator, 34 3, naclbitc::FUNC_CODE_DECLAREBLOCKS, 1, Terminator,
35 3, naclbitc::FUNC_CODE_DECLAREBLOCKS, 1, Terminator, 35 3, naclbitc::FUNC_CODE_INST_RET, Terminator,
36 3, naclbitc::FUNC_CODE_INST_RET, Terminator, 36 0, naclbitc::BLK_CODE_EXIT, Terminator,
37 0, naclbitc::BLK_CODE_EXIT, Terminator, 37 0, naclbitc::BLK_CODE_EXIT, Terminator
38 0, naclbitc::BLK_CODE_EXIT, Terminator 38 };
39 };
40 39
41 // Expected output when bitcode records are dumped. 40 // Expected output when bitcode records are dumped.
42 const char* ExpectedDump = 41 const char* ExpectedDump =
43 " 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, "
44 "88, 69)\n" 43 "88, 69)\n"
45 " | 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"
46 " | 0> |\n" 45 " | 0> |\n"
47 " 16:0|1: <65535, 8, 2> |module { // BlockID = 8\n" 46 " 16:0|1: <65535, 8, 2> |module { // BlockID = 8\n"
48 " 24:0| 1: <65535, 17, 3> | types { // BlockID = 17\n" 47 " 24:0| 1: <65535, 17, 3> | types { // BlockID = 17\n"
49 " 32:0| 3: <1, 2> | count 2;\n" 48 " 32:0| 3: <1, 2> | count 2;\n"
(...skipping 22 matching lines...) Expand all
72 // instruction. 71 // instruction.
73 const uint64_t RetVoidIndex = 9; // return void; 72 const uint64_t RetVoidIndex = 9; // return void;
74 const uint64_t UseLocalRetVoidAbbrevEdits[] = { 73 const uint64_t UseLocalRetVoidAbbrevEdits[] = {
75 RetVoidIndex, NaClMungedBitcode::AddBefore, 74 RetVoidIndex, NaClMungedBitcode::AddBefore,
76 2, naclbitc::BLK_CODE_DEFINE_ABBREV, 1, 1, 75 2, naclbitc::BLK_CODE_DEFINE_ABBREV, 1, 1,
77 naclbitc::FUNC_CODE_INST_RET, Terminator, 76 naclbitc::FUNC_CODE_INST_RET, Terminator,
78 RetVoidIndex, NaClMungedBitcode::Replace, 77 RetVoidIndex, NaClMungedBitcode::Replace,
79 4, naclbitc::FUNC_CODE_INST_RET, Terminator 78 4, naclbitc::FUNC_CODE_INST_RET, Terminator
80 }; 79 };
81 80
82 #define ARRAY_ARGS(Records) Records, array_lengthof(Records)
83
84 #define ARRAY_ARGS_TERM(Records) ARRAY_ARGS(Records), Terminator
85
86 std::string stringify(NaClBitcodeMunger &Munger) {
87 std::string Buffer;
88 raw_string_ostream StrBuf(Buffer);
89 Munger.getMungedBitcode().print(StrBuf);
90 return StrBuf.str();
91 }
92
93 // Show that we can dump the bitcode records 81 // Show that we can dump the bitcode records
94 TEST(NaClMungeWriteErrorTests, DumpBitcodeRecords) { 82 TEST(NaClMungeWriteErrorTests, DumpBitcodeRecords) {
95 NaClObjDumpMunger Munger(ARRAY_ARGS_TERM(BitcodeRecords)); 83 NaClObjDumpMunger Munger(ARRAY_TERM(BitcodeRecords));
96 EXPECT_TRUE(Munger.runTest()); 84 EXPECT_TRUE(Munger.runTest());
97 EXPECT_EQ(ExpectedDump, Munger.getTestResults()); 85 EXPECT_EQ(ExpectedDump, Munger.getTestResults());
98 } 86 }
99 87
100 // Show that by default, one can't write a bad abbreviation index. 88 // Show that by default, one can't write a bad abbreviation index.
101 TEST(NaClMungeWriteErrorTests, CantWriteBadAbbrevIndex) { 89 TEST(NaClMungeWriteErrorTests, CantWriteBadAbbrevIndex) {
102 NaClWriteMunger Munger(ARRAY_ARGS_TERM(BitcodeRecords)); 90 NaClWriteMunger Munger(ARRAY_TERM(BitcodeRecords));
103 EXPECT_FALSE(Munger.runTest(ARRAY_ARGS(AbbrevIndex4VoidTypeEdit))); 91 EXPECT_FALSE(Munger.runTest(ARRAY(AbbrevIndex4VoidTypeEdit)));
104 EXPECT_EQ( 92 EXPECT_EQ(
105 "Error (Block 17): Uses illegal abbreviation index: 4: [2]\n" 93 "Error (Block 17): Uses illegal abbreviation index: 4: [2]\n"
106 "Error: Unable to generate bitcode file due to write errors\n", 94 "Error: Unable to generate bitcode file due to write errors\n",
107 Munger.getTestResults()); 95 Munger.getTestResults());
108 } 96 }
109 97
110 // Show that we can't write more local abbreviations than specified in 98 // Show that we can't write more local abbreviations than specified in
111 // the corresponding enclosing block. 99 // the corresponding enclosing block.
112 TEST(NaClMungeWriteErrorTests, CantWriteTooManyLocalAbbreviations) { 100 TEST(NaClMungeWriteErrorTests, CantWriteTooManyLocalAbbreviations) {
113 NaClWriteMunger Munger(ARRAY_ARGS_TERM(BitcodeRecords)); 101 NaClWriteMunger Munger(ARRAY_TERM(BitcodeRecords));
114 Munger.munge(ARRAY_ARGS(UseLocalRetVoidAbbrevEdits)); 102 Munger.munge(ARRAY(UseLocalRetVoidAbbrevEdits));
115 EXPECT_EQ( 103 EXPECT_EQ(
116 " 1: [65535, 8, 2]\n" 104 " 1: [65535, 8, 2]\n"
117 " 1: [65535, 17, 3]\n" 105 " 1: [65535, 17, 3]\n"
118 " 3: [1, 2]\n" 106 " 3: [1, 2]\n"
119 " 3: [2]\n" 107 " 3: [2]\n"
120 " 3: [21, 0, 0]\n" 108 " 3: [21, 0, 0]\n"
121 " 0: [65534]\n" 109 " 0: [65534]\n"
122 " 3: [8, 1, 0, 0, 0]\n" 110 " 3: [8, 1, 0, 0, 0]\n"
123 " 1: [65535, 12, 2]\n" 111 " 1: [65535, 12, 2]\n"
124 " 3: [1, 1]\n" 112 " 3: [1, 1]\n"
125 " 2: [65533, 1, 1, 10]\n" 113 " 2: [65533, 1, 1, 10]\n"
126 " 4: [10]\n" 114 " 4: [10]\n"
127 " 0: [65534]\n" 115 " 0: [65534]\n"
128 " 0: [65534]\n", 116 " 0: [65534]\n",
129 stringify(Munger)); 117 stringify(Munger));
130 118
131 EXPECT_FALSE(Munger.runTest()); 119 EXPECT_FALSE(Munger.runTest());
132 EXPECT_EQ( 120 EXPECT_EQ(
133 "Error (Block 12): Exceeds abbreviation index limit of 3: 2: [65533," 121 "Error (Block 12): Exceeds abbreviation index limit of 3: 2: [65533,"
134 " 1, 1, 10]\n" 122 " 1, 1, 10]\n"
135 "Error: Unable to generate bitcode file due to write errors\n", 123 "Error: Unable to generate bitcode file due to write errors\n",
136 Munger.getTestResults()); 124 Munger.getTestResults());
137 } 125 }
138 126
139 // Show what happens when there are more enter blocks then exit blocks. 127 // Show what happens when there are more enter blocks then exit blocks.
140 TEST(NaClMungeWriteErrorTests, CantWriteTooManyEnterBlocks) { 128 TEST(NaClMungeWriteErrorTests, CantWriteTooManyEnterBlocks) {
141 NaClWriteMunger Munger(ARRAY_ARGS_TERM(BitcodeRecords)); 129 NaClWriteMunger Munger(ARRAY_TERM(BitcodeRecords));
142 // Remove all but first two records (i.e. two enter blocks). 130 // Remove all but first two records (i.e. two enter blocks).
143 NaClMungedBitcode &MungedBitcode = Munger.getMungedBitcode(); 131 NaClMungedBitcode &MungedBitcode = Munger.getMungedBitcode();
144 for (size_t i = 2; i < MungedBitcode.getBaseRecords().size(); ++i) { 132 for (size_t i = 2; i < MungedBitcode.getBaseRecords().size(); ++i) {
145 MungedBitcode.remove(i); 133 MungedBitcode.remove(i);
146 } 134 }
147 135
148 EXPECT_FALSE(Munger.runTest()); 136 EXPECT_FALSE(Munger.runTest());
149 EXPECT_EQ( 137 EXPECT_EQ(
150 "Error (Block 17): Missing close block.\n" 138 "Error (Block 17): Missing close block.\n"
151 "Error (Block 8): Missing close block.\n" 139 "Error (Block 8): Missing close block.\n"
152 "Error: Unable to generate bitcode file due to write errors\n", 140 "Error: Unable to generate bitcode file due to write errors\n",
153 Munger.getTestResults()); 141 Munger.getTestResults());
154 } 142 }
155 143
156 // Show what happens when there are fewer enter blocks than exit 144 // Show what happens when there are fewer enter blocks than exit
157 // blocks. 145 // blocks.
158 TEST(NaClMungeWriteErrorTests, CantWriteTooManyExitBlocks) { 146 TEST(NaClMungeWriteErrorTests, CantWriteTooManyExitBlocks) {
159 NaClWriteMunger Munger(ARRAY_ARGS_TERM(BitcodeRecords)); 147 NaClWriteMunger Munger(ARRAY_TERM(BitcodeRecords));
160 // Add two exit blocks. 148 // Add two exit blocks.
161 NaClMungedBitcode &MungedBitcode = Munger.getMungedBitcode(); 149 NaClMungedBitcode &MungedBitcode = Munger.getMungedBitcode();
162 NaClRecordVector Values; 150 NaClRecordVector Values;
163 NaClBitcodeAbbrevRecord Record(0, naclbitc::BLK_CODE_EXIT, Values); 151 NaClBitcodeAbbrevRecord Record(0, naclbitc::BLK_CODE_EXIT, Values);
164 for (size_t i = 0; i < 2; ++i) 152 for (size_t i = 0; i < 2; ++i)
165 MungedBitcode.addAfter(MungedBitcode.getBaseRecords().size() - 1, Record); 153 MungedBitcode.addAfter(MungedBitcode.getBaseRecords().size() - 1, Record);
166 154
167 EXPECT_FALSE(Munger.runTest()); 155 EXPECT_FALSE(Munger.runTest());
168 EXPECT_EQ( 156 EXPECT_EQ(
169 "Error (Block unknown): Extraneous exit block: 0: [65534]\n" 157 "Error (Block unknown): Extraneous exit block: 0: [65534]\n"
170 "Error: Unable to generate bitcode file due to write errors\n", 158 "Error: Unable to generate bitcode file due to write errors\n",
171 Munger.getTestResults()); 159 Munger.getTestResults());
172 } 160 }
173 161
174 // Show that an error occurs when writing a bitcode record that isn't 162 // Show that an error occurs when writing a bitcode record that isn't
175 // in any block. 163 // in any block.
176 TEST(NaClMungeWriteErrorTests, CantWriteRecordOutsideBlock) { 164 TEST(NaClMungeWriteErrorTests, CantWriteRecordOutsideBlock) {
177 NaClWriteMunger Munger(ARRAY_ARGS_TERM(BitcodeRecords)); 165 NaClWriteMunger Munger(ARRAY_TERM(BitcodeRecords));
178 NaClMungedBitcode &MungedBitcode = Munger.getMungedBitcode(); 166 NaClMungedBitcode &MungedBitcode = Munger.getMungedBitcode();
179 NaClRecordVector Values; 167 NaClRecordVector Values;
180 Values.push_back(4); 168 Values.push_back(4);
181 NaClBitcodeAbbrevRecord Record(naclbitc::UNABBREV_RECORD, 169 NaClBitcodeAbbrevRecord Record(naclbitc::UNABBREV_RECORD,
182 naclbitc::MODULE_CODE_VERSION, 170 naclbitc::MODULE_CODE_VERSION,
183 Values); 171 Values);
184 172
185 MungedBitcode.addAfter(MungedBitcode.getBaseRecords().size() - 1, Record); 173 MungedBitcode.addAfter(MungedBitcode.getBaseRecords().size() - 1, Record);
186 EXPECT_FALSE(Munger.runTest()); 174 EXPECT_FALSE(Munger.runTest());
187 EXPECT_EQ( 175 EXPECT_EQ(
188 "Error (Block unknown): Record outside block: 3: [1, 4]\n" 176 "Error (Block unknown): Record outside block: 3: [1, 4]\n"
189 "Error: Unable to generate bitcode file due to write errors\n", 177 "Error: Unable to generate bitcode file due to write errors\n",
190 Munger.getTestResults()); 178 Munger.getTestResults());
191 } 179 }
192 180
193 // Show that no error occurs if we write out the maximum allowable 181 // Show that no error occurs if we write out the maximum allowable
194 // block abbreviation index bit limit. 182 // block abbreviation index bit limit.
195 TEST(NaClMungerWriteErrorTests, CanWriteBlockWithMaxLimit) { 183 TEST(NaClMungerWriteErrorTests, CanWriteBlockWithMaxLimit) {
196 // Replace initial block enter with maximum bit size. 184 // Replace initial block enter with maximum bit size.
197 const uint64_t Edit[] = { 185 const uint64_t Edit[] = {
198 0, NaClMungedBitcode::Replace, 186 0, NaClMungedBitcode::Replace,
199 1, naclbitc::BLK_CODE_ENTER, naclbitc::MODULE_BLOCK_ID, 187 1, naclbitc::BLK_CODE_ENTER, naclbitc::MODULE_BLOCK_ID,
200 naclbitc::MaxAbbrevWidth, Terminator 188 naclbitc::MaxAbbrevWidth, Terminator
201 }; 189 };
202 NaClWriteMunger Munger(ARRAY_ARGS_TERM(BitcodeRecords)); 190 NaClWriteMunger Munger(ARRAY_TERM(BitcodeRecords));
203 EXPECT_TRUE(Munger.runTest(ARRAY_ARGS(Edit))); 191 EXPECT_TRUE(Munger.runTest(ARRAY(Edit)));
204 EXPECT_EQ( 192 EXPECT_EQ(
205 " 1: [65535, 8, 32]\n" 193 " 1: [65535, 8, 32]\n"
206 " 1: [65535, 17, 3]\n" 194 " 1: [65535, 17, 3]\n"
207 " 3: [1, 2]\n" 195 " 3: [1, 2]\n"
208 " 3: [2]\n" 196 " 3: [2]\n"
209 " 3: [21, 0, 0]\n" 197 " 3: [21, 0, 0]\n"
210 " 0: [65534]\n" 198 " 0: [65534]\n"
211 " 3: [8, 1, 0, 0, 0]\n" 199 " 3: [8, 1, 0, 0, 0]\n"
212 " 1: [65535, 12, 2]\n" 200 " 1: [65535, 12, 2]\n"
213 " 3: [1, 1]\n" 201 " 3: [1, 1]\n"
214 " 3: [10]\n" 202 " 3: [10]\n"
215 " 0: [65534]\n" 203 " 0: [65534]\n"
216 " 0: [65534]\n", 204 " 0: [65534]\n",
217 Munger.getTestResults()); 205 Munger.getTestResults());
218 } 206 }
219 207
220 // Show that an error occurs if the block abbreviation index bit limit is 208 // Show that an error occurs if the block abbreviation index bit limit is
221 // greater than the maximum allowable. 209 // greater than the maximum allowable.
222 TEST(NaClMungerWriteErrorTests, CantWriteBlockWithBadBitLimit) { 210 TEST(NaClMungerWriteErrorTests, CantWriteBlockWithBadBitLimit) {
223 // Replace initial block enter with value out of range. 211 // Replace initial block enter with value out of range.
224 const uint64_t Edit[] = { 212 const uint64_t Edit[] = {
225 0, NaClMungedBitcode::Replace, 213 0, NaClMungedBitcode::Replace,
226 1, naclbitc::BLK_CODE_ENTER, naclbitc::MODULE_BLOCK_ID, 214 1, naclbitc::BLK_CODE_ENTER, naclbitc::MODULE_BLOCK_ID,
227 naclbitc::MaxAbbrevWidth + 1, Terminator 215 naclbitc::MaxAbbrevWidth + 1, Terminator
228 }; 216 };
229 NaClWriteMunger Munger(ARRAY_ARGS_TERM(BitcodeRecords)); 217 NaClWriteMunger Munger(ARRAY_TERM(BitcodeRecords));
230 EXPECT_FALSE(Munger.runTest(ARRAY_ARGS(Edit))); 218 EXPECT_FALSE(Munger.runTest(ARRAY(Edit)));
231 EXPECT_EQ( 219 EXPECT_EQ(
232 "Error (Block unknown): Block index bit limit 33 invalid. Must be in" 220 "Error (Block unknown): Block index bit limit 33 invalid. Must be in"
233 " [2..32]: 1: [65535, 8, 33]\n" 221 " [2..32]: 1: [65535, 8, 33]\n"
234 "Error: Unable to generate bitcode file due to write errors\n", 222 "Error: Unable to generate bitcode file due to write errors\n",
235 Munger.getTestResults()); 223 Munger.getTestResults());
236 } 224 }
237 225
238 // Show that we can't write an enter block with a very large block id. 226 // Show that we can't write an enter block with a very large block id.
239 TEST(NaClMungerWriteErrorTests, CantWriteBlockWithLargeBlockID) { 227 TEST(NaClMungerWriteErrorTests, CantWriteBlockWithLargeBlockID) {
240 // Replace initial block enter with value out of range. 228 // Replace initial block enter with value out of range.
241 const uint64_t Edit[] = { 229 const uint64_t Edit[] = {
242 0, NaClMungedBitcode::Replace, 230 0, NaClMungedBitcode::Replace,
243 1, naclbitc::BLK_CODE_ENTER, (uint64_t)1 << 33, 2, Terminator 231 1, naclbitc::BLK_CODE_ENTER, (uint64_t)1 << 33, 2, Terminator
244 }; 232 };
245 NaClWriteMunger Munger(ARRAY_ARGS_TERM(BitcodeRecords)); 233 NaClWriteMunger Munger(ARRAY_TERM(BitcodeRecords));
246 EXPECT_FALSE(Munger.runTest(ARRAY_ARGS(Edit))); 234 EXPECT_FALSE(Munger.runTest(ARRAY(Edit)));
247 EXPECT_EQ( 235 EXPECT_EQ(
248 "Error (Block unknown): Block id must be <= 4294967295: 1:" 236 "Error (Block unknown): Block id must be <= 4294967295: 1:"
249 " [65535, 8589934592, 2]\n" 237 " [65535, 8589934592, 2]\n"
250 "Error: Unable to generate bitcode file due to write errors\n", 238 "Error: Unable to generate bitcode file due to write errors\n",
251 Munger.getTestResults()); 239 Munger.getTestResults());
252 } 240 }
253 241
254 // Show that writing successfully writes out an illegal abbreviation 242 // Show that writing successfully writes out an illegal abbreviation
255 // index, and then the parser fails to parse that illegal abbreviation. 243 // index, and then the parser fails to parse that illegal abbreviation.
256 TEST(MyNaClMungerWriteErrorTests, DieOnWriteBadAbbreviationIndex) { 244 TEST(MyNaClMungerWriteErrorTests, DieOnWriteBadAbbreviationIndex) {
257 NaClWriteMunger Munger(ARRAY_ARGS_TERM(BitcodeRecords)); 245 NaClWriteMunger Munger(ARRAY_TERM(BitcodeRecords));
258 Munger.setWriteBadAbbrevIndex(true); 246 Munger.setWriteBadAbbrevIndex(true);
259 Munger.setRunAsDeathTest(true); 247 Munger.setRunAsDeathTest(true);
260 EXPECT_DEATH( 248 EXPECT_DEATH(
261 Munger.runTest(ARRAY_ARGS(AbbrevIndex4VoidTypeEdit)), 249 Munger.runTest(ARRAY(AbbrevIndex4VoidTypeEdit)),
262 ".*" 250 ".*"
263 // Report problem while writing. 251 // Report problem while writing.
264 "Error \\(Block 17\\)\\: Uses illegal abbreviation index\\: 4\\: \\[2\\]" 252 "Error \\(Block 17\\)\\: Uses illegal abbreviation index\\: 4\\: \\[2\\]"
265 ".*" 253 ".*"
266 // Corresponding error while parsing. 254 // Corresponding error while parsing.
267 "Fatal\\(35\\:0)\\: Invalid abbreviation \\# 4 defined for record" 255 "Fatal\\(35\\:0)\\: Invalid abbreviation \\# 4 defined for record"
268 ".*" 256 ".*"
269 // Output of report_fatal_error. 257 // Output of report_fatal_error.
270 "LLVM ERROR\\: Unable to continue" 258 "LLVM ERROR\\: Unable to continue"
271 ".*"); 259 ".*");
272 } 260 }
273 261
274 // Show that error recovery works when writing an illegal abbreviation 262 // Show that error recovery works when writing an illegal abbreviation
275 // index. Show success by parsing fixed bitcode. 263 // index. Show success by parsing fixed bitcode.
276 TEST(NaClMungeWriteErrorTests, RecoverWhenParsingBadAbbrevIndex) { 264 TEST(NaClMungeWriteErrorTests, RecoverWhenParsingBadAbbrevIndex) {
277 NaClParseBitcodeMunger Munger(ARRAY_ARGS_TERM(BitcodeRecords)); 265 NaClParseBitcodeMunger Munger(ARRAY_TERM(BitcodeRecords));
278 Munger.setTryToRecoverOnWrite(true); 266 Munger.setTryToRecoverOnWrite(true);
279 EXPECT_TRUE( 267 EXPECT_TRUE(
280 Munger.runTest(ARRAY_ARGS(AbbrevIndex4VoidTypeEdit), true)); 268 Munger.runTest(ARRAY(AbbrevIndex4VoidTypeEdit), true));
281 EXPECT_EQ( 269 EXPECT_EQ(
282 "Error (Block 17): Uses illegal abbreviation index: 4: [2]\n" 270 "Error (Block 17): Uses illegal abbreviation index: 4: [2]\n"
283 "Successful parse!\n", 271 "Successful parse!\n",
284 Munger.getTestResults()); 272 Munger.getTestResults());
285 } 273 }
286 274
287 // Show that error recovery works when writing an illegal abbreviation 275 // Show that error recovery works when writing an illegal abbreviation
288 // index. Show success by Dumping fixed bitcode. 276 // index. Show success by Dumping fixed bitcode.
289 TEST(NaClMungeWriteErrorTests, RecoverWhenParsingBadAbbreviationIndex) { 277 TEST(NaClMungeWriteErrorTests, RecoverWhenParsingBadAbbreviationIndex) {
290 NaClObjDumpMunger Munger(ARRAY_ARGS_TERM(BitcodeRecords)); 278 NaClObjDumpMunger Munger(ARRAY_TERM(BitcodeRecords));
291 Munger.setTryToRecoverOnWrite(true); 279 Munger.setTryToRecoverOnWrite(true);
292 EXPECT_TRUE(Munger.runTest(ARRAY_ARGS(AbbrevIndex4VoidTypeEdit))); 280 EXPECT_TRUE(Munger.runTest(ARRAY(AbbrevIndex4VoidTypeEdit)));
293 std::string Results( 281 std::string Results(
294 "Error (Block 17): Uses illegal abbreviation index: 4: [2]\n"); 282 "Error (Block 17): Uses illegal abbreviation index: 4: [2]\n");
295 Results.append(ExpectedDump); 283 Results.append(ExpectedDump);
296 EXPECT_EQ(Results, Munger.getTestResults()); 284 EXPECT_EQ(Results, Munger.getTestResults());
297 } 285 }
298 286
299 // Show that error recovery works when writing too many locally 287 // Show that error recovery works when writing too many locally
300 // defined abbreviations for the corresponding number of bits defined 288 // defined abbreviations for the corresponding number of bits defined
301 // in the corresponding enter block. Show success by dumping the fixed 289 // in the corresponding enter block. Show success by dumping the fixed
302 // bitcode. 290 // bitcode.
303 TEST(NaClMungeWriteErrorTests, RecoverTooManyLocalAbbreviations) { 291 TEST(NaClMungeWriteErrorTests, RecoverTooManyLocalAbbreviations) {
304 NaClObjDumpMunger Munger(ARRAY_ARGS_TERM(BitcodeRecords)); 292 NaClObjDumpMunger Munger(ARRAY_TERM(BitcodeRecords));
305 Munger.setTryToRecoverOnWrite(true); 293 Munger.setTryToRecoverOnWrite(true);
306 Munger.munge(ARRAY_ARGS(UseLocalRetVoidAbbrevEdits)); 294 Munger.munge(ARRAY(UseLocalRetVoidAbbrevEdits));
307 295
308 EXPECT_TRUE(Munger.runTest()); 296 EXPECT_TRUE(Munger.runTest());
309 std::string Results( 297 std::string Results(
310 "Error (Block 12): Exceeds abbreviation index limit of 3: 2:" 298 "Error (Block 12): Exceeds abbreviation index limit of 3: 2:"
311 " [65533, 1, 1, 10]\n" 299 " [65533, 1, 1, 10]\n"
312 "Error (Block 12): Uses illegal abbreviation index: 4: [10]\n"); 300 "Error (Block 12): Uses illegal abbreviation index: 4: [10]\n");
313 Results.append(ExpectedDump); 301 Results.append(ExpectedDump);
314 EXPECT_EQ( 302 EXPECT_EQ(
315 Results, 303 Results,
316 Munger.getTestResults()); 304 Munger.getTestResults());
317 } 305 }
318 306
319 // Show that error recovery works when writing and there are more 307 // Show that error recovery works when writing and there are more
320 // enter blocks than exit blocks. Show success by dumping fixed 308 // enter blocks than exit blocks. Show success by dumping fixed
321 // bitcode. 309 // bitcode.
322 TEST(NaClMungeWriteErrorTests, RecoverTooManyEnterBlocks) { 310 TEST(NaClMungeWriteErrorTests, RecoverTooManyEnterBlocks) {
323 NaClObjDumpMunger Munger(ARRAY_ARGS_TERM(BitcodeRecords)); 311 NaClObjDumpMunger Munger(ARRAY_TERM(BitcodeRecords));
324 // Remove all but first two records (i.e. two enter blocks). 312 // Remove all but first two records (i.e. two enter blocks).
325 NaClMungedBitcode &MungedBitcode = Munger.getMungedBitcode(); 313 NaClMungedBitcode &MungedBitcode = Munger.getMungedBitcode();
326 for (size_t i = 2; i < MungedBitcode.getBaseRecords().size(); ++i) { 314 for (size_t i = 2; i < MungedBitcode.getBaseRecords().size(); ++i) {
327 MungedBitcode.remove(i); 315 MungedBitcode.remove(i);
328 } 316 }
329 317
330 Munger.setTryToRecoverOnWrite(true); 318 Munger.setTryToRecoverOnWrite(true);
331 EXPECT_TRUE(Munger.runTest()); 319 EXPECT_TRUE(Munger.runTest());
332 EXPECT_EQ( 320 EXPECT_EQ(
333 "Error (Block 17): Missing close block.\n" 321 "Error (Block 17): Missing close block.\n"
334 "Error (Block 8): Missing close block.\n" 322 "Error (Block 8): Missing close block.\n"
335 " 0:0|<65532, 80, 69, 88, 69, 1, 0,|Magic Number: 'PEXE' (80, 69," 323 " 0:0|<65532, 80, 69, 88, 69, 1, 0,|Magic Number: 'PEXE' (80, 69,"
336 " 88, 69)\n" 324 " 88, 69)\n"
337 " | 8, 0, 17, 0, 4, 0, 2, 0, 0, |PNaCl Version: 2\n" 325 " | 8, 0, 17, 0, 4, 0, 2, 0, 0, |PNaCl Version: 2\n"
338 " | 0> |\n" 326 " | 0> |\n"
339 " 16:0|1: <65535, 8, 2> |module { // BlockID = 8\n" 327 " 16:0|1: <65535, 8, 2> |module { // BlockID = 8\n"
340 " 24:0| 1: <65535, 17, 3> | types { // BlockID = 17\n" 328 " 24:0| 1: <65535, 17, 3> | types { // BlockID = 17\n"
341 " 32:0| 0: <65534> | }\n" 329 " 32:0| 0: <65534> | }\n"
342 " 36:0|0: <65534> |}\n", 330 " 36:0|0: <65534> |}\n",
343 Munger.getTestResults()); 331 Munger.getTestResults());
344 } 332 }
345 333
346 // Show that error recovery works when writing and there are fewer 334 // Show that error recovery works when writing and there are fewer
347 // enter blocks than exit blocks. Show success by dumping the fixed 335 // enter blocks than exit blocks. Show success by dumping the fixed
348 // bitcode. 336 // bitcode.
349 TEST(NaClMungeWriteErrorTests, RecoverTooManyExitBlocks) { 337 TEST(NaClMungeWriteErrorTests, RecoverTooManyExitBlocks) {
350 NaClObjDumpMunger Munger(ARRAY_ARGS_TERM(BitcodeRecords)); 338 NaClObjDumpMunger Munger(ARRAY_TERM(BitcodeRecords));
351 // Add two exit blocks. 339 // Add two exit blocks.
352 NaClMungedBitcode &MungedBitcode = Munger.getMungedBitcode(); 340 NaClMungedBitcode &MungedBitcode = Munger.getMungedBitcode();
353 NaClRecordVector Values; 341 NaClRecordVector Values;
354 NaClBitcodeAbbrevRecord Record(0, naclbitc::BLK_CODE_EXIT, Values); 342 NaClBitcodeAbbrevRecord Record(0, naclbitc::BLK_CODE_EXIT, Values);
355 for (size_t i = 0; i < 2; ++i) 343 for (size_t i = 0; i < 2; ++i)
356 MungedBitcode.addAfter(MungedBitcode.getBaseRecords().size() - 1, Record); 344 MungedBitcode.addAfter(MungedBitcode.getBaseRecords().size() - 1, Record);
357 345
358 Munger.setTryToRecoverOnWrite(true); 346 Munger.setTryToRecoverOnWrite(true);
359 EXPECT_TRUE(Munger.runTest()); 347 EXPECT_TRUE(Munger.runTest());
360 std::string Results( 348 std::string Results(
361 "Error (Block unknown): Extraneous exit block: 0: [65534]\n" 349 "Error (Block unknown): Extraneous exit block: 0: [65534]\n"
362 "Error (Block unknown): Extraneous exit block: 0: [65534]\n"); 350 "Error (Block unknown): Extraneous exit block: 0: [65534]\n");
363 Results.append(ExpectedDump); 351 Results.append(ExpectedDump);
364 EXPECT_EQ( 352 EXPECT_EQ(
365 Results, 353 Results,
366 Munger.getTestResults()); 354 Munger.getTestResults());
367 } 355 }
368 356
369 // Show that error recovery works when writing a bitcode record that 357 // Show that error recovery works when writing a bitcode record that
370 // isn't in any block. Show success by showing fixed bitcode records. 358 // isn't in any block. Show success by showing fixed bitcode records.
371 TEST(NaClMungeWriteErrorTests, RecoverWriteRecordOutsideBlock) { 359 TEST(NaClMungeWriteErrorTests, RecoverWriteRecordOutsideBlock) {
372 NaClWriteMunger Munger(ARRAY_ARGS_TERM(BitcodeRecords)); 360 NaClWriteMunger Munger(ARRAY_TERM(BitcodeRecords));
373 NaClMungedBitcode &MungedBitcode = Munger.getMungedBitcode(); 361 NaClMungedBitcode &MungedBitcode = Munger.getMungedBitcode();
374 NaClRecordVector Values; 362 NaClRecordVector Values;
375 Values.push_back(4); 363 Values.push_back(4);
376 NaClBitcodeAbbrevRecord Record(naclbitc::UNABBREV_RECORD, 364 NaClBitcodeAbbrevRecord Record(naclbitc::UNABBREV_RECORD,
377 naclbitc::MODULE_CODE_VERSION, 365 naclbitc::MODULE_CODE_VERSION,
378 Values); 366 Values);
379 MungedBitcode.addAfter(MungedBitcode.getBaseRecords().size() - 1, Record); 367 MungedBitcode.addAfter(MungedBitcode.getBaseRecords().size() - 1, Record);
380 368
381 Munger.setTryToRecoverOnWrite(true); 369 Munger.setTryToRecoverOnWrite(true);
382 EXPECT_TRUE(Munger.runTest()); 370 EXPECT_TRUE(Munger.runTest());
(...skipping 11 matching lines...) Expand all
394 " 3: [1, 1]\n" 382 " 3: [1, 1]\n"
395 " 3: [10]\n" 383 " 3: [10]\n"
396 " 0: [65534]\n" 384 " 0: [65534]\n"
397 " 0: [65534]\n" 385 " 0: [65534]\n"
398 " 1: [65535, 4294967295, 3]\n" 386 " 1: [65535, 4294967295, 3]\n"
399 " 3: [1, 4]\n" 387 " 3: [1, 4]\n"
400 " 0: [65534]\n", 388 " 0: [65534]\n",
401 Munger.getTestResults()); 389 Munger.getTestResults());
402 } 390 }
403 391
404 } // end of anonymous namespace. 392 } // end of namespace naclmungetest
OLDNEW
« no previous file with comments | « unittests/Bitcode/NaClMungeTest.h ('k') | unittests/Bitcode/NaClMungedBitcodeTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698