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

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

Issue 1146203003: Fix abbreviation handling in munged bitcode writer. (Closed) Base URL: https://chromium.googlesource.com/native_client/pnacl-llvm.git@master
Patch Set: Fix nits. 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 | « lib/Bitcode/NaCl/TestUtils/NaClBitcodeMungeWriter.cpp ('k') | no next file » | 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
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
88 // 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.
89 TEST(NaClMungeWriteErrorTests, CantWriteBadAbbrevIndex) { 89 TEST(NaClMungeWriteErrorTests, CantWriteBadAbbrevIndex) {
90 NaClWriteMunger Munger(ARRAY_TERM(BitcodeRecords)); 90 NaClWriteMunger Munger(ARRAY_TERM(BitcodeRecords));
91 EXPECT_FALSE(Munger.runTest(ARRAY(AbbrevIndex4VoidTypeEdit))); 91 EXPECT_FALSE(Munger.runTest(ARRAY(AbbrevIndex4VoidTypeEdit)));
92 EXPECT_EQ( 92 EXPECT_EQ(
93 "Error (Block 17): Uses illegal abbreviation index: 4: [2]\n" 93 "Error (Block 17): Uses illegal abbreviation index: 4: [2]\n"
94 "Error: Unable to generate bitcode file due to write errors\n", 94 "Error: Unable to generate bitcode file due to write errors\n",
95 Munger.getTestResults()); 95 Munger.getTestResults());
96 } 96 }
97 97
98 // Show that we can't write more local abbreviations than specified in 98 // Show that we use more local abbreviations than specified in the
99 // the corresponding enclosing block. 99 // corresponding enclosing block.
100 TEST(NaClMungeWriteErrorTests, CantWriteTooManyLocalAbbreviations) { 100 TEST(NaClMungeWriteErrorTests, CantWriteTooManyLocalAbbreviations) {
101 NaClWriteMunger Munger(ARRAY_TERM(BitcodeRecords)); 101 NaClWriteMunger Munger(ARRAY_TERM(BitcodeRecords));
102 Munger.munge(ARRAY(UseLocalRetVoidAbbrevEdits)); 102 Munger.munge(ARRAY(UseLocalRetVoidAbbrevEdits));
103 EXPECT_EQ( 103 EXPECT_EQ(
104 " 1: [65535, 8, 2]\n" 104 " 1: [65535, 8, 2]\n"
105 " 1: [65535, 17, 3]\n" 105 " 1: [65535, 17, 3]\n"
106 " 3: [1, 2]\n" 106 " 3: [1, 2]\n"
107 " 3: [2]\n" 107 " 3: [2]\n"
108 " 3: [21, 0, 0]\n" 108 " 3: [21, 0, 0]\n"
109 " 0: [65534]\n" 109 " 0: [65534]\n"
110 " 3: [8, 1, 0, 0, 0]\n" 110 " 3: [8, 1, 0, 0, 0]\n"
111 " 1: [65535, 12, 2]\n" 111 " 1: [65535, 12, 2]\n" // Only allows 2 bits for abbrevs.
112 " 3: [1, 1]\n" 112 " 3: [1, 1]\n"
113 " 2: [65533, 1, 1, 10]\n" 113 " 2: [65533, 1, 1, 10]\n" // defines abbev 4:
114 " 4: [10]\n" 114 " 4: [10]\n" // can't use, 4 can't fit in two bits.
115 " 0: [65534]\n" 115 " 0: [65534]\n"
116 " 0: [65534]\n", 116 " 0: [65534]\n",
117 stringify(Munger)); 117 stringify(Munger));
118 118
119 EXPECT_FALSE(Munger.runTest()); 119 EXPECT_FALSE(Munger.runTest());
120 EXPECT_EQ( 120 EXPECT_EQ(
121 "Error (Block 12): Exceeds abbreviation index limit of 3: 2: [65533," 121 "Error (Block 12): Uses illegal abbreviation index: 4: [10]\n"
122 " 1, 1, 10]\n"
123 "Error: Unable to generate bitcode file due to write errors\n", 122 "Error: Unable to generate bitcode file due to write errors\n",
124 Munger.getTestResults()); 123 Munger.getTestResults());
125 } 124 }
126 125
127 // Show what happens when there are more enter blocks then exit blocks. 126 // Show what happens when there are more enter blocks then exit blocks.
128 TEST(NaClMungeWriteErrorTests, CantWriteTooManyEnterBlocks) { 127 TEST(NaClMungeWriteErrorTests, CantWriteTooManyEnterBlocks) {
129 NaClWriteMunger Munger(ARRAY_TERM(BitcodeRecords)); 128 NaClWriteMunger Munger(ARRAY_TERM(BitcodeRecords));
130 // Remove all but first two records (i.e. two enter blocks). 129 // Remove all but first two records (i.e. two enter blocks).
131 NaClMungedBitcode &MungedBitcode = Munger.getMungedBitcode(); 130 NaClMungedBitcode &MungedBitcode = Munger.getMungedBitcode();
132 for (size_t i = 2; i < MungedBitcode.getBaseRecords().size(); ++i) { 131 for (size_t i = 2; i < MungedBitcode.getBaseRecords().size(); ++i) {
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
252 "Error \\(Block 17\\)\\: Uses illegal abbreviation index\\: 4\\: \\[2\\]" 251 "Error \\(Block 17\\)\\: Uses illegal abbreviation index\\: 4\\: \\[2\\]"
253 ".*" 252 ".*"
254 // Corresponding error while parsing. 253 // Corresponding error while parsing.
255 "Fatal\\(35\\:0)\\: Invalid abbreviation \\# 4 defined for record" 254 "Fatal\\(35\\:0)\\: Invalid abbreviation \\# 4 defined for record"
256 ".*" 255 ".*"
257 // Output of report_fatal_error. 256 // Output of report_fatal_error.
258 "LLVM ERROR\\: Unable to continue" 257 "LLVM ERROR\\: Unable to continue"
259 ".*"); 258 ".*");
260 } 259 }
261 260
261 // Show that we check that the abbreviation actually applies to the
262 // record associated with that abbreviation. Also shows that we repair
263 // the problem by applying the default abbreviation instead.
264 TEST(NaClMungeWriteErrorsTests, TestMismatchedAbbreviation) {
265 // Create edits to:
266 // 1) Expand the number of abbreviation index bits for the block from 2 to 3.
267 // 2) Introduce the incorrect abbreviation for the return instruction.
268 // i.e. [9] instead of [10].
269 // 3) Apply the bad abbreviation to record "ret"
270 const uint64_t FunctionEnterIndex = 7;
271 const uint64_t Edits[] {
272 FunctionEnterIndex, NaClMungedBitcode::Replace,
273 1, naclbitc::BLK_CODE_ENTER, naclbitc::FUNCTION_BLOCK_ID, 3, Terminator,
274 RetVoidIndex, NaClMungedBitcode::AddBefore,
275 2, naclbitc::BLK_CODE_DEFINE_ABBREV, 1, 1,
276 naclbitc::FUNC_CODE_INST_RET - 1, Terminator,
277 RetVoidIndex, NaClMungedBitcode::Replace,
278 4, naclbitc::FUNC_CODE_INST_RET, Terminator
279 };
280
281 NaClWriteMunger Munger(ARRAY_TERM(BitcodeRecords));
282 Munger.munge(ARRAY(Edits));
283 EXPECT_EQ(
284 " 1: [65535, 8, 2]\n"
285 " 1: [65535, 17, 3]\n"
286 " 3: [1, 2]\n"
287 " 3: [2]\n"
288 " 3: [21, 0, 0]\n"
289 " 0: [65534]\n"
290 " 3: [8, 1, 0, 0, 0]\n"
291 " 1: [65535, 12, 3]\n" // Upped abbreviation index bits to 3
292 " 3: [1, 1]\n"
293 " 2: [65533, 1, 1, 9]\n" // added abbrev 4: [9]
294 " 4: [10]\n" // "ret" with bad abbreviation.
295 " 0: [65534]\n"
296 " 0: [65534]\n",
297 stringify(Munger));
298
299 // Show detected error
300 EXPECT_FALSE(Munger.runTest());
301 EXPECT_EQ(
302 "Error (Block 12): Abbreviation doesn't apply to record: 4: [10]\n"
303 "Error: Unable to generate bitcode file due to write errors\n",
304 Munger.getTestResults());
305
306 // Show that the writer can recover.
307 Munger.setTryToRecoverOnWrite(true);
308 EXPECT_TRUE(Munger.runTest(ARRAY(Edits)));
309 EXPECT_EQ(
310 "Error (Block 12): Abbreviation doesn't apply to record: 4: [10]\n"
311 " 1: [65535, 8, 2]\n"
312 " 1: [65535, 17, 3]\n"
313 " 3: [1, 2]\n"
314 " 3: [2]\n"
315 " 3: [21, 0, 0]\n"
316 " 0: [65534]\n"
317 " 3: [8, 1, 0, 0, 0]\n"
318 " 1: [65535, 12, 3]\n"
319 " 3: [1, 1]\n"
320 " 2: [65533, 1, 1, 9]\n"
321 " 3: [10]\n" // Implicit repair here.
322 " 0: [65534]\n"
323 " 0: [65534]\n",
324 Munger.getTestResults());
325 }
326
327 // Show that we recognize when an abbreviation definition record is
328 // malformed. Also show that we repair the problem by removing the
329 // definition.
330 TEST(NaClMungeWriteErrorsTests, TestWritingMalformedAbbreviation) {
331 // Create edits to:
332 // 1) Expand the number of abbreviation index bits for the block from 2 to 3.
333 // 2) Leave out the "literal" operand encoding out.
334 const uint64_t FunctionEnterIndex = 7;
335 const uint64_t Edits[] {
336 FunctionEnterIndex, NaClMungedBitcode::Replace, // Set Abbrev bits = 3
337 1, naclbitc::BLK_CODE_ENTER, naclbitc::FUNCTION_BLOCK_ID, 3, Terminator,
338 RetVoidIndex, NaClMungedBitcode::AddBefore,
339 // Bad abbreviation! Intentionally leave out "literal" operand: 1
340 2, naclbitc::BLK_CODE_DEFINE_ABBREV, 1, // 1,
341 naclbitc::FUNC_CODE_INST_RET, Terminator,
342 };
343
344 // Show that the error is detected.
345 NaClWriteMunger Munger(ARRAY_TERM(BitcodeRecords));
346 EXPECT_FALSE(Munger.runTest(ARRAY(Edits)));
347 EXPECT_EQ(
348 "Error (Block 12): Error: Bad abbreviation operand encoding 10:"
349 " 2: [65533, 1, 10]\n"
350 "Error: Unable to generate bitcode file due to write errors\n",
351 Munger.getTestResults());
352
353 // Show that the writer can recover.
354 Munger.setTryToRecoverOnWrite(true);
355 EXPECT_TRUE(Munger.runTest(ARRAY(Edits)));
356 EXPECT_EQ(
357 "Error (Block 12): Error: Bad abbreviation operand encoding 10: "
358 "2: [65533, 1, 10]\n"
359 " 1: [65535, 8, 2]\n"
360 " 1: [65535, 17, 3]\n"
361 " 3: [1, 2]\n"
362 " 3: [2]\n"
363 " 3: [21, 0, 0]\n"
364 " 0: [65534]\n"
365 " 3: [8, 1, 0, 0, 0]\n"
366 " 1: [65535, 12, 3]\n" // Note: not followed by abbreviation def.
367 " 3: [1, 1]\n"
368 " 3: [10]\n"
369 " 0: [65534]\n"
370 " 0: [65534]\n",
371 Munger.getTestResults());
372 }
373
374 // Show how we deal with additional abbreviations defined for a block,
375 // once a bad abbreviation definition record is found. That is, we
376 // remove all succeeding abbreviations definitions for that block. In
377 // addition, any record refering to a remove abbreviation is changed
378 // to use the default abbreviation.
379 TEST(NaClMungedWriteErrorTests, TestRemovingAbbrevWithMultAbbrevs) {
380 NaClWriteMunger Munger(ARRAY_TERM(BitcodeRecords));
381 const uint64_t FunctionEnterIndex = 7;
382 const uint64_t Edits[] {
383 FunctionEnterIndex, NaClMungedBitcode::Replace, // Set Abbrev bits = 3
384 1, naclbitc::BLK_CODE_ENTER, naclbitc::FUNCTION_BLOCK_ID, 3, Terminator,
385 RetVoidIndex, NaClMungedBitcode::AddBefore, // bad abbreviation!
386 2, naclbitc::BLK_CODE_DEFINE_ABBREV, 1, // 1,
387 naclbitc::FUNC_CODE_INST_RET - 1, Terminator,
388 RetVoidIndex, NaClMungedBitcode::AddBefore, // good abbreviation to ignore.
389 2, naclbitc::BLK_CODE_DEFINE_ABBREV, 1, 1,
390 naclbitc::FUNC_CODE_INST_RET, Terminator,
391 RetVoidIndex, NaClMungedBitcode::Replace, // reference to good abreviation.
392 5, naclbitc::FUNC_CODE_INST_RET, Terminator
393 };
394
395 Munger.setTryToRecoverOnWrite(true);
396 EXPECT_TRUE(Munger.runTest(ARRAY(Edits)));
397 EXPECT_EQ(
398 "Error (Block 12): Error: Bad abbreviation operand encoding 9:"
399 " 2: [65533, 1, 9]\n"
400 "Error (Block 12): Ignoring abbreviation: 2: [65533, 1, 1, 10]\n"
401 "Error (Block 12): Uses illegal abbreviation index: 5: [10]\n"
402 " 1: [65535, 8, 2]\n"
403 " 1: [65535, 17, 3]\n"
404 " 3: [1, 2]\n"
405 " 3: [2]\n"
406 " 3: [21, 0, 0]\n"
407 " 0: [65534]\n"
408 " 3: [8, 1, 0, 0, 0]\n"
409 " 1: [65535, 12, 3]\n"
410 " 3: [1, 1]\n"
411 " 3: [10]\n" // Abbreviation index 5 replaced with default.
412 " 0: [65534]\n"
413 " 0: [65534]\n",
414 Munger.getTestResults());
415 }
416
262 // Show that error recovery works when writing an illegal abbreviation 417 // Show that error recovery works when writing an illegal abbreviation
263 // index. Show success by parsing fixed bitcode. 418 // index. Show success by parsing fixed bitcode.
264 TEST(NaClMungeWriteErrorTests, RecoverWhenParsingBadAbbrevIndex) { 419 TEST(NaClMungeWriteErrorTests, RecoverWhenParsingBadAbbrevIndex) {
265 NaClParseBitcodeMunger Munger(ARRAY_TERM(BitcodeRecords)); 420 NaClParseBitcodeMunger Munger(ARRAY_TERM(BitcodeRecords));
266 Munger.setTryToRecoverOnWrite(true); 421 Munger.setTryToRecoverOnWrite(true);
267 EXPECT_TRUE( 422 EXPECT_TRUE(
268 Munger.runTest(ARRAY(AbbrevIndex4VoidTypeEdit), true)); 423 Munger.runTest(ARRAY(AbbrevIndex4VoidTypeEdit), true));
269 EXPECT_EQ( 424 EXPECT_EQ(
270 "Error (Block 17): Uses illegal abbreviation index: 4: [2]\n" 425 "Error (Block 17): Uses illegal abbreviation index: 4: [2]\n"
271 "Successful parse!\n", 426 "Successful parse!\n",
(...skipping 13 matching lines...) Expand all
285 } 440 }
286 441
287 // Show that error recovery works when writing too many locally 442 // Show that error recovery works when writing too many locally
288 // defined abbreviations for the corresponding number of bits defined 443 // defined abbreviations for the corresponding number of bits defined
289 // in the corresponding enter block. Show success by dumping the fixed 444 // in the corresponding enter block. Show success by dumping the fixed
290 // bitcode. 445 // bitcode.
291 TEST(NaClMungeWriteErrorTests, RecoverTooManyLocalAbbreviations) { 446 TEST(NaClMungeWriteErrorTests, RecoverTooManyLocalAbbreviations) {
292 NaClObjDumpMunger Munger(ARRAY_TERM(BitcodeRecords)); 447 NaClObjDumpMunger Munger(ARRAY_TERM(BitcodeRecords));
293 Munger.setTryToRecoverOnWrite(true); 448 Munger.setTryToRecoverOnWrite(true);
294 Munger.munge(ARRAY(UseLocalRetVoidAbbrevEdits)); 449 Munger.munge(ARRAY(UseLocalRetVoidAbbrevEdits));
295
296 EXPECT_TRUE(Munger.runTest()); 450 EXPECT_TRUE(Munger.runTest());
297 std::string Results(
298 "Error (Block 12): Exceeds abbreviation index limit of 3: 2:"
299 " [65533, 1, 1, 10]\n"
300 "Error (Block 12): Uses illegal abbreviation index: 4: [10]\n");
301 Results.append(ExpectedDump);
302 EXPECT_EQ( 451 EXPECT_EQ(
303 Results, 452 "Error (Block 12): Uses illegal abbreviation index: 4: [10]\n"
453 " 0:0|<65532, 80, 69, 88, 69, 1, 0,|Magic Number: 'PEXE'"
454 " (80, 69, 88, 69)\n"
455 " | 8, 0, 17, 0, 4, 0, 2, 0, 0, |PNaCl Version: 2\n"
456 " | 0> |\n"
457 " 16:0|1: <65535, 8, 2> |module { // BlockID = 8\n"
458 " 24:0| 1: <65535, 17, 3> | types { // BlockID = 17\n"
459 " 32:0| 3: <1, 2> | count 2;\n"
460 " 34:5| 3: <2> | @t0 = void;\n"
461 " 36:4| 3: <21, 0, 0> | @t1 = void ();\n"
462 " 39:7| 0: <65534> | }\n"
463 " 44:0| 3: <8, 1, 0, 0, 0> | define external void @f0();\n"
464 // Block only specifies 2 bits for abbreviations (i.e. limit = 3).
465 " 48:6| 1: <65535, 12, 2> | function void @f0() { \n"
466 " | | // BlockID"
467 " = 12\n"
468 " 56:0| 3: <1, 1> | blocks 1;\n"
469 // Added abbreviation. Defines abbreviation index 4.
470 " 58:4| 2: <65533, 1, 1, 10> | %a0 = abbrev <10>;\n"
471 " | | %b0:\n"
472 // Repaired abbreviation index of 4 (now 3).
473 " 60:4| 3: <10> | ret void;\n"
474 " 62:2| 0: <65534> | }\n"
475 " 64:0|0: <65534> |}\n",
304 Munger.getTestResults()); 476 Munger.getTestResults());
305 } 477 }
306 478
307 // Show that error recovery works when writing and there are more 479 // Show that error recovery works when writing and there are more
308 // enter blocks than exit blocks. Show success by dumping fixed 480 // enter blocks than exit blocks. Show success by dumping fixed
309 // bitcode. 481 // bitcode.
310 TEST(NaClMungeWriteErrorTests, RecoverTooManyEnterBlocks) { 482 TEST(NaClMungeWriteErrorTests, RecoverTooManyEnterBlocks) {
311 NaClObjDumpMunger Munger(ARRAY_TERM(BitcodeRecords)); 483 NaClObjDumpMunger Munger(ARRAY_TERM(BitcodeRecords));
312 // Remove all but first two records (i.e. two enter blocks). 484 // Remove all but first two records (i.e. two enter blocks).
313 NaClMungedBitcode &MungedBitcode = Munger.getMungedBitcode(); 485 NaClMungedBitcode &MungedBitcode = Munger.getMungedBitcode();
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
383 " 3: [10]\n" 555 " 3: [10]\n"
384 " 0: [65534]\n" 556 " 0: [65534]\n"
385 " 0: [65534]\n" 557 " 0: [65534]\n"
386 " 1: [65535, 4294967295, 3]\n" 558 " 1: [65535, 4294967295, 3]\n"
387 " 3: [1, 4]\n" 559 " 3: [1, 4]\n"
388 " 0: [65534]\n", 560 " 0: [65534]\n",
389 Munger.getTestResults()); 561 Munger.getTestResults());
390 } 562 }
391 563
392 } // end of namespace naclmungetest 564 } // end of namespace naclmungetest
OLDNEW
« no previous file with comments | « lib/Bitcode/NaCl/TestUtils/NaClBitcodeMungeWriter.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698