1 //===- CrashDebugger.cpp - Debug compilation crashes ----------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file defines the bugpoint internals that narrow down compilation crashes 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "BugDriver.h" 15 #include "ListReducer.h" 16 #include "ToolRunner.h" 17 #include "llvm/ADT/SmallPtrSet.h" 18 #include "llvm/IR/CFG.h" 19 #include "llvm/IR/Constants.h" 20 #include "llvm/IR/DerivedTypes.h" 21 #include "llvm/IR/Instructions.h" 22 #include "llvm/IR/LegacyPassManager.h" 23 #include "llvm/IR/Module.h" 24 #include "llvm/IR/ValueSymbolTable.h" 25 #include "llvm/IR/Verifier.h" 26 #include "llvm/Pass.h" 27 #include "llvm/Support/CommandLine.h" 28 #include "llvm/Support/FileUtilities.h" 29 #include "llvm/Transforms/Scalar.h" 30 #include "llvm/Transforms/Utils/Cloning.h" 31 #include <set> 32 using namespace llvm; 33 34 namespace { 35 cl::opt<bool> 36 KeepMain("keep-main", 37 cl::desc("Force function reduction to keep main"), 38 cl::init(false)); 39 cl::opt<bool> 40 NoGlobalRM ("disable-global-remove", 41 cl::desc("Do not remove global variables"), 42 cl::init(false)); 43 44 cl::opt<bool> 45 ReplaceFuncsWithNull("replace-funcs-with-null", 46 cl::desc("When stubbing functions, replace all uses will null"), 47 cl::init(false)); 48 cl::opt<bool> 49 DontReducePassList("disable-pass-list-reduction", 50 cl::desc("Skip pass list reduction steps"), 51 cl::init(false)); 52 } 53 54 namespace llvm { 55 class ReducePassList : public ListReducer<std::string> { 56 BugDriver &BD; 57 public: 58 ReducePassList(BugDriver &bd) : BD(bd) {} 59 60 // doTest - Return true iff running the "removed" passes succeeds, and 61 // running the "Kept" passes fail when run on the output of the "removed" 62 // passes. If we return true, we update the current module of bugpoint. 63 // 64 TestResult doTest(std::vector<std::string> &Removed, 65 std::vector<std::string> &Kept, 66 std::string &Error) override; 67 }; 68 } 69 70 ReducePassList::TestResult 71 ReducePassList::doTest(std::vector<std::string> &Prefix, 72 std::vector<std::string> &Suffix, 73 std::string &Error) { 74 std::string PrefixOutput; 75 Module *OrigProgram = nullptr; 76 if (!Prefix.empty()) { 77 outs() << "Checking to see if these passes crash: " 78 << getPassesString(Prefix) << ": "; 79 if (BD.runPasses(BD.getProgram(), Prefix, PrefixOutput)) 80 return KeepPrefix; 81 82 OrigProgram = BD.Program; 83 84 BD.Program = parseInputFile(PrefixOutput, BD.getContext()).release(); 85 if (BD.Program == nullptr) { 86 errs() << BD.getToolName() << ": Error reading bitcode file '" 87 << PrefixOutput << "'!\n"; 88 exit(1); 89 } 90 sys::fs::remove(PrefixOutput); 91 } 92 93 outs() << "Checking to see if these passes crash: " 94 << getPassesString(Suffix) << ": "; 95 96 if (BD.runPasses(BD.getProgram(), Suffix)) { 97 delete OrigProgram; // The suffix crashes alone... 98 return KeepSuffix; 99 } 100 101 // Nothing failed, restore state... 102 if (OrigProgram) { 103 delete BD.Program; 104 BD.Program = OrigProgram; 105 } 106 return NoFailure; 107 } 108 109 namespace { 110 /// ReduceCrashingGlobalVariables - This works by removing the global 111 /// variable's initializer and seeing if the program still crashes. If it 112 /// does, then we keep that program and try again. 113 /// 114 class ReduceCrashingGlobalVariables : public ListReducer<GlobalVariable*> { 115 BugDriver &BD; 116 bool (*TestFn)(const BugDriver &, Module *); 117 public: 118 ReduceCrashingGlobalVariables(BugDriver &bd, 119 bool (*testFn)(const BugDriver &, Module *)) 120 : BD(bd), TestFn(testFn) {} 121 122 TestResult doTest(std::vector<GlobalVariable*> &Prefix, 123 std::vector<GlobalVariable*> &Kept, 124 std::string &Error) override { 125 if (!Kept.empty() && TestGlobalVariables(Kept)) 126 return KeepSuffix; 127 if (!Prefix.empty() && TestGlobalVariables(Prefix)) 128 return KeepPrefix; 129 return NoFailure; 130 } 131 132 bool TestGlobalVariables(std::vector<GlobalVariable*> &GVs); 133 }; 134 } 135 136 bool 137 ReduceCrashingGlobalVariables::TestGlobalVariables( 138 std::vector<GlobalVariable*> &GVs) { 139 // Clone the program to try hacking it apart... 140 ValueToValueMapTy VMap; 141 Module *M = CloneModule(BD.getProgram(), VMap); 142 143 // Convert list to set for fast lookup... 144 std::set<GlobalVariable*> GVSet; 145 146 for (unsigned i = 0, e = GVs.size(); i != e; ++i) { 147 GlobalVariable* CMGV = cast<GlobalVariable>(VMap[GVs[i]]); 148 assert(CMGV && "Global Variable not in module?!"); 149 GVSet.insert(CMGV); 150 } 151 152 outs() << "Checking for crash with only these global variables: "; 153 PrintGlobalVariableList(GVs); 154 outs() << ": "; 155 156 // Loop over and delete any global variables which we aren't supposed to be 157 // playing with... 158 for (Module::global_iterator I = M->global_begin(), E = M->global_end(); 159 I != E; ++I) 160 if (I->hasInitializer() && !GVSet.count(I)) { 161 I->setInitializer(nullptr); 162 I->setLinkage(GlobalValue::ExternalLinkage); 163 } 164 165 // Try running the hacked up program... 166 if (TestFn(BD, M)) { 167 BD.setNewProgram(M); // It crashed, keep the trimmed version... 168 169 // Make sure to use global variable pointers that point into the now-current 170 // module. 171 GVs.assign(GVSet.begin(), GVSet.end()); 172 return true; 173 } 174 175 delete M; 176 return false; 177 } 178 179 namespace { 180 /// ReduceCrashingFunctions reducer - This works by removing functions and 181 /// seeing if the program still crashes. If it does, then keep the newer, 182 /// smaller program. 183 /// 184 class ReduceCrashingFunctions : public ListReducer<Function*> { 185 BugDriver &BD; 186 bool (*TestFn)(const BugDriver &, Module *); 187 public: 188 ReduceCrashingFunctions(BugDriver &bd, 189 bool (*testFn)(const BugDriver &, Module *)) 190 : BD(bd), TestFn(testFn) {} 191 192 TestResult doTest(std::vector<Function*> &Prefix, 193 std::vector<Function*> &Kept, 194 std::string &Error) override { 195 if (!Kept.empty() && TestFuncs(Kept)) 196 return KeepSuffix; 197 if (!Prefix.empty() && TestFuncs(Prefix)) 198 return KeepPrefix; 199 return NoFailure; 200 } 201 202 bool TestFuncs(std::vector<Function*> &Prefix); 203 }; 204 } 205 206 static void RemoveFunctionReferences(Module *M, const char* Name) { 207 auto *UsedVar = M->getGlobalVariable(Name, true); 208 if (!UsedVar || !UsedVar->hasInitializer()) return; 209 if (isa<ConstantAggregateZero>(UsedVar->getInitializer())) { 210 assert(UsedVar->use_empty()); 211 UsedVar->eraseFromParent(); 212 return; 213 } 214 auto *OldUsedVal = cast<ConstantArray>(UsedVar->getInitializer()); 215 std::vector<Constant*> Used; 216 for(Value *V : OldUsedVal->operand_values()) { 217 Constant *Op = cast<Constant>(V->stripPointerCasts()); 218 if(!Op->isNullValue()) { 219 Used.push_back(cast<Constant>(V)); 220 } 221 } 222 auto *NewValElemTy = OldUsedVal->getType()->getElementType(); 223 auto *NewValTy = ArrayType::get(NewValElemTy, Used.size()); 224 auto *NewUsedVal = ConstantArray::get(NewValTy, Used); 225 UsedVar->mutateType(NewUsedVal->getType()->getPointerTo()); 226 UsedVar->setInitializer(NewUsedVal); 227 } 228 229 bool ReduceCrashingFunctions::TestFuncs(std::vector<Function*> &Funcs) { 230 // If main isn't present, claim there is no problem. 231 if (KeepMain && std::find(Funcs.begin(), Funcs.end(), 232 BD.getProgram()->getFunction("main")) == 233 Funcs.end()) 234 return false; 235 236 // Clone the program to try hacking it apart... 237 ValueToValueMapTy VMap; 238 Module *M = CloneModule(BD.getProgram(), VMap); 239 240 // Convert list to set for fast lookup... 241 std::set<Function*> Functions; 242 for (unsigned i = 0, e = Funcs.size(); i != e; ++i) { 243 Function *CMF = cast<Function>(VMap[Funcs[i]]); 244 assert(CMF && "Function not in module?!"); 245 assert(CMF->getFunctionType() == Funcs[i]->getFunctionType() && "wrong ty"); 246 assert(CMF->getName() == Funcs[i]->getName() && "wrong name"); 247 Functions.insert(CMF); 248 } 249 250 outs() << "Checking for crash with only these functions: "; 251 PrintFunctionList(Funcs); 252 outs() << ": "; 253 if (!ReplaceFuncsWithNull) { 254 // Loop over and delete any functions which we aren't supposed to be playing 255 // with... 256 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) 257 if (!I->isDeclaration() && !Functions.count(I)) 258 DeleteFunctionBody(I); 259 } else { 260 std::vector<GlobalValue*> ToRemove; 261 // First, remove aliases to functions we're about to purge. 262 for (GlobalAlias &Alias : M->aliases()) { 263 Constant *Root = Alias.getAliasee()->stripPointerCasts(); 264 Function *F = dyn_cast<Function>(Root); 265 if (F) { 266 if (Functions.count(F)) 267 // We're keeping this function. 268 continue; 269 } else if (Root->isNullValue()) { 270 // This referenced a globalalias that we've already replaced, 271 // so we still need to replace this alias. 272 } else if (!F) { 273 // Not a function, therefore not something we mess with. 274 continue; 275 } 276 277 PointerType *Ty = cast<PointerType>(Alias.getType()); 278 Constant *Replacement = ConstantPointerNull::get(Ty); 279 Alias.replaceAllUsesWith(Replacement); 280 ToRemove.push_back(&Alias); 281 } 282 283 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) { 284 if (!I->isDeclaration() && !Functions.count(I)) { 285 PointerType *Ty = cast<PointerType>(I->getType()); 286 Constant *Replacement = ConstantPointerNull::get(Ty); 287 I->replaceAllUsesWith(Replacement); 288 ToRemove.push_back(I); 289 } 290 } 291 292 for (auto *F : ToRemove) { 293 F->eraseFromParent(); 294 } 295 296 // Finally, remove any null members from any global intrinsic. 297 RemoveFunctionReferences(M, "llvm.used"); 298 RemoveFunctionReferences(M, "llvm.compiler.used"); 299 } 300 // Try running the hacked up program... 301 if (TestFn(BD, M)) { 302 BD.setNewProgram(M); // It crashed, keep the trimmed version... 303 304 // Make sure to use function pointers that point into the now-current 305 // module. 306 Funcs.assign(Functions.begin(), Functions.end()); 307 return true; 308 } 309 delete M; 310 return false; 311 } 312 313 314 namespace { 315 /// ReduceCrashingBlocks reducer - This works by setting the terminators of 316 /// all terminators except the specified basic blocks to a 'ret' instruction, 317 /// then running the simplify-cfg pass. This has the effect of chopping up 318 /// the CFG really fast which can reduce large functions quickly. 319 /// 320 class ReduceCrashingBlocks : public ListReducer<const BasicBlock*> { 321 BugDriver &BD; 322 bool (*TestFn)(const BugDriver &, Module *); 323 public: 324 ReduceCrashingBlocks(BugDriver &bd, 325 bool (*testFn)(const BugDriver &, Module *)) 326 : BD(bd), TestFn(testFn) {} 327 328 TestResult doTest(std::vector<const BasicBlock*> &Prefix, 329 std::vector<const BasicBlock*> &Kept, 330 std::string &Error) override { 331 if (!Kept.empty() && TestBlocks(Kept)) 332 return KeepSuffix; 333 if (!Prefix.empty() && TestBlocks(Prefix)) 334 return KeepPrefix; 335 return NoFailure; 336 } 337 338 bool TestBlocks(std::vector<const BasicBlock*> &Prefix); 339 }; 340 } 341 342 bool ReduceCrashingBlocks::TestBlocks(std::vector<const BasicBlock*> &BBs) { 343 // Clone the program to try hacking it apart... 344 ValueToValueMapTy VMap; 345 Module *M = CloneModule(BD.getProgram(), VMap); 346 347 // Convert list to set for fast lookup... 348 SmallPtrSet<BasicBlock*, 8> Blocks; 349 for (unsigned i = 0, e = BBs.size(); i != e; ++i) 350 Blocks.insert(cast<BasicBlock>(VMap[BBs[i]])); 351 352 outs() << "Checking for crash with only these blocks:"; 353 unsigned NumPrint = Blocks.size(); 354 if (NumPrint > 10) NumPrint = 10; 355 for (unsigned i = 0, e = NumPrint; i != e; ++i) 356 outs() << " " << BBs[i]->getName(); 357 if (NumPrint < Blocks.size()) 358 outs() << "... <" << Blocks.size() << " total>"; 359 outs() << ": "; 360 361 // Loop over and delete any hack up any blocks that are not listed... 362 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) 363 for (Function::iterator BB = I->begin(), E = I->end(); BB != E; ++BB) 364 if (!Blocks.count(BB) && BB->getTerminator()->getNumSuccessors()) { 365 // Loop over all of the successors of this block, deleting any PHI nodes 366 // that might include it. 367 for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI) 368 (*SI)->removePredecessor(BB); 369 370 TerminatorInst *BBTerm = BB->getTerminator(); 371 372 if (!BB->getTerminator()->getType()->isVoidTy()) 373 BBTerm->replaceAllUsesWith(Constant::getNullValue(BBTerm->getType())); 374 375 // Replace the old terminator instruction. 376 BB->getInstList().pop_back(); 377 new UnreachableInst(BB->getContext(), BB); 378 } 379 380 // The CFG Simplifier pass may delete one of the basic blocks we are 381 // interested in. If it does we need to take the block out of the list. Make 382 // a "persistent mapping" by turning basic blocks into <function, name> pairs. 383 // This won't work well if blocks are unnamed, but that is just the risk we 384 // have to take. 385 std::vector<std::pair<std::string, std::string> > BlockInfo; 386 387 for (BasicBlock *BB : Blocks) 388 BlockInfo.push_back(std::make_pair(BB->getParent()->getName(), 389 BB->getName())); 390 391 // Now run the CFG simplify pass on the function... 392 std::vector<std::string> Passes; 393 Passes.push_back("simplifycfg"); 394 Passes.push_back("verify"); 395 std::unique_ptr<Module> New = BD.runPassesOn(M, Passes); 396 delete M; 397 if (!New) { 398 errs() << "simplifycfg failed!\n"; 399 exit(1); 400 } 401 M = New.release(); 402 403 // Try running on the hacked up program... 404 if (TestFn(BD, M)) { 405 BD.setNewProgram(M); // It crashed, keep the trimmed version... 406 407 // Make sure to use basic block pointers that point into the now-current 408 // module, and that they don't include any deleted blocks. 409 BBs.clear(); 410 const ValueSymbolTable &GST = M->getValueSymbolTable(); 411 for (unsigned i = 0, e = BlockInfo.size(); i != e; ++i) { 412 Function *F = cast<Function>(GST.lookup(BlockInfo[i].first)); 413 ValueSymbolTable &ST = F->getValueSymbolTable(); 414 Value* V = ST.lookup(BlockInfo[i].second); 415 if (V && V->getType() == Type::getLabelTy(V->getContext())) 416 BBs.push_back(cast<BasicBlock>(V)); 417 } 418 return true; 419 } 420 delete M; // It didn't crash, try something else. 421 return false; 422 } 423 424 namespace { 425 /// ReduceCrashingInstructions reducer - This works by removing the specified 426 /// non-terminator instructions and replacing them with undef. 427 /// 428 class ReduceCrashingInstructions : public ListReducer<const Instruction*> { 429 BugDriver &BD; 430 bool (*TestFn)(const BugDriver &, Module *); 431 public: 432 ReduceCrashingInstructions(BugDriver &bd, 433 bool (*testFn)(const BugDriver &, Module *)) 434 : BD(bd), TestFn(testFn) {} 435 436 TestResult doTest(std::vector<const Instruction*> &Prefix, 437 std::vector<const Instruction*> &Kept, 438 std::string &Error) override { 439 if (!Kept.empty() && TestInsts(Kept)) 440 return KeepSuffix; 441 if (!Prefix.empty() && TestInsts(Prefix)) 442 return KeepPrefix; 443 return NoFailure; 444 } 445 446 bool TestInsts(std::vector<const Instruction*> &Prefix); 447 }; 448 } 449 450 bool ReduceCrashingInstructions::TestInsts(std::vector<const Instruction*> 451 &Insts) { 452 // Clone the program to try hacking it apart... 453 ValueToValueMapTy VMap; 454 Module *M = CloneModule(BD.getProgram(), VMap); 455 456 // Convert list to set for fast lookup... 457 SmallPtrSet<Instruction*, 64> Instructions; 458 for (unsigned i = 0, e = Insts.size(); i != e; ++i) { 459 assert(!isa<TerminatorInst>(Insts[i])); 460 Instructions.insert(cast<Instruction>(VMap[Insts[i]])); 461 } 462 463 outs() << "Checking for crash with only " << Instructions.size(); 464 if (Instructions.size() == 1) 465 outs() << " instruction: "; 466 else 467 outs() << " instructions: "; 468 469 for (Module::iterator MI = M->begin(), ME = M->end(); MI != ME; ++MI) 470 for (Function::iterator FI = MI->begin(), FE = MI->end(); FI != FE; ++FI) 471 for (BasicBlock::iterator I = FI->begin(), E = FI->end(); I != E;) { 472 Instruction *Inst = I++; 473 if (!Instructions.count(Inst) && !isa<TerminatorInst>(Inst) && 474 !isa<LandingPadInst>(Inst)) { 475 if (!Inst->getType()->isVoidTy()) 476 Inst->replaceAllUsesWith(UndefValue::get(Inst->getType())); 477 Inst->eraseFromParent(); 478 } 479 } 480 481 // Verify that this is still valid. 482 legacy::PassManager Passes; 483 Passes.add(createVerifierPass()); 484 Passes.run(*M); 485 486 // Try running on the hacked up program... 487 if (TestFn(BD, M)) { 488 BD.setNewProgram(M); // It crashed, keep the trimmed version... 489 490 // Make sure to use instruction pointers that point into the now-current 491 // module, and that they don't include any deleted blocks. 492 Insts.clear(); 493 for (Instruction *Inst : Instructions) 494 Insts.push_back(Inst); 495 return true; 496 } 497 delete M; // It didn't crash, try something else. 498 return false; 499 } 500 501 /// DebugACrash - Given a predicate that determines whether a component crashes 502 /// on a program, try to destructively reduce the program while still keeping 503 /// the predicate true. 504 static bool DebugACrash(BugDriver &BD, 505 bool (*TestFn)(const BugDriver &, Module *), 506 std::string &Error) { 507 // See if we can get away with nuking some of the global variable initializers 508 // in the program... 509 if (!NoGlobalRM && 510 BD.getProgram()->global_begin() != BD.getProgram()->global_end()) { 511 // Now try to reduce the number of global variable initializers in the 512 // module to something small. 513 Module *M = CloneModule(BD.getProgram()); 514 bool DeletedInit = false; 515 516 for (Module::global_iterator I = M->global_begin(), E = M->global_end(); 517 I != E; ++I) 518 if (I->hasInitializer()) { 519 I->setInitializer(nullptr); 520 I->setLinkage(GlobalValue::ExternalLinkage); 521 DeletedInit = true; 522 } 523 524 if (!DeletedInit) { 525 delete M; // No change made... 526 } else { 527 // See if the program still causes a crash... 528 outs() << "\nChecking to see if we can delete global inits: "; 529 530 if (TestFn(BD, M)) { // Still crashes? 531 BD.setNewProgram(M); 532 outs() << "\n*** Able to remove all global initializers!\n"; 533 } else { // No longer crashes? 534 outs() << " - Removing all global inits hides problem!\n"; 535 delete M; 536 537 std::vector<GlobalVariable*> GVs; 538 539 for (Module::global_iterator I = BD.getProgram()->global_begin(), 540 E = BD.getProgram()->global_end(); I != E; ++I) 541 if (I->hasInitializer()) 542 GVs.push_back(I); 543 544 if (GVs.size() > 1 && !BugpointIsInterrupted) { 545 outs() << "\n*** Attempting to reduce the number of global " 546 << "variables in the testcase\n"; 547 548 unsigned OldSize = GVs.size(); 549 ReduceCrashingGlobalVariables(BD, TestFn).reduceList(GVs, Error); 550 if (!Error.empty()) 551 return true; 552 553 if (GVs.size() < OldSize) 554 BD.EmitProgressBitcode(BD.getProgram(), "reduced-global-variables"); 555 } 556 } 557 } 558 } 559 560 // Now try to reduce the number of functions in the module to something small. 561 std::vector<Function*> Functions; 562 for (Module::iterator I = BD.getProgram()->begin(), 563 E = BD.getProgram()->end(); I != E; ++I) 564 if (!I->isDeclaration()) 565 Functions.push_back(I); 566 567 if (Functions.size() > 1 && !BugpointIsInterrupted) { 568 outs() << "\n*** Attempting to reduce the number of functions " 569 "in the testcase\n"; 570 571 unsigned OldSize = Functions.size(); 572 ReduceCrashingFunctions(BD, TestFn).reduceList(Functions, Error); 573 574 if (Functions.size() < OldSize) 575 BD.EmitProgressBitcode(BD.getProgram(), "reduced-function"); 576 } 577 578 // Attempt to delete entire basic blocks at a time to speed up 579 // convergence... this actually works by setting the terminator of the blocks 580 // to a return instruction then running simplifycfg, which can potentially 581 // shrinks the code dramatically quickly 582 // 583 if (!DisableSimplifyCFG && !BugpointIsInterrupted) { 584 std::vector<const BasicBlock*> Blocks; 585 for (Module::const_iterator I = BD.getProgram()->begin(), 586 E = BD.getProgram()->end(); I != E; ++I) 587 for (Function::const_iterator FI = I->begin(), E = I->end(); FI !=E; ++FI) 588 Blocks.push_back(FI); 589 unsigned OldSize = Blocks.size(); 590 ReduceCrashingBlocks(BD, TestFn).reduceList(Blocks, Error); 591 if (Blocks.size() < OldSize) 592 BD.EmitProgressBitcode(BD.getProgram(), "reduced-blocks"); 593 } 594 595 // Attempt to delete instructions using bisection. This should help out nasty 596 // cases with large basic blocks where the problem is at one end. 597 if (!BugpointIsInterrupted) { 598 std::vector<const Instruction*> Insts; 599 for (Module::const_iterator MI = BD.getProgram()->begin(), 600 ME = BD.getProgram()->end(); MI != ME; ++MI) 601 for (Function::const_iterator FI = MI->begin(), FE = MI->end(); FI != FE; 602 ++FI) 603 for (BasicBlock::const_iterator I = FI->begin(), E = FI->end(); 604 I != E; ++I) 605 if (!isa<TerminatorInst>(I)) 606 Insts.push_back(I); 607 608 ReduceCrashingInstructions(BD, TestFn).reduceList(Insts, Error); 609 } 610 611 // FIXME: This should use the list reducer to converge faster by deleting 612 // larger chunks of instructions at a time! 613 unsigned Simplification = 2; 614 do { 615 if (BugpointIsInterrupted) break; 616 --Simplification; 617 outs() << "\n*** Attempting to reduce testcase by deleting instruc" 618 << "tions: Simplification Level #" << Simplification << '\n'; 619 620 // Now that we have deleted the functions that are unnecessary for the 621 // program, try to remove instructions that are not necessary to cause the 622 // crash. To do this, we loop through all of the instructions in the 623 // remaining functions, deleting them (replacing any values produced with 624 // nulls), and then running ADCE and SimplifyCFG. If the transformed input 625 // still triggers failure, keep deleting until we cannot trigger failure 626 // anymore. 627 // 628 unsigned InstructionsToSkipBeforeDeleting = 0; 629 TryAgain: 630 631 // Loop over all of the (non-terminator) instructions remaining in the 632 // function, attempting to delete them. 633 unsigned CurInstructionNum = 0; 634 for (Module::const_iterator FI = BD.getProgram()->begin(), 635 E = BD.getProgram()->end(); FI != E; ++FI) 636 if (!FI->isDeclaration()) 637 for (Function::const_iterator BI = FI->begin(), E = FI->end(); BI != E; 638 ++BI) 639 for (BasicBlock::const_iterator I = BI->begin(), E = --BI->end(); 640 I != E; ++I, ++CurInstructionNum) { 641 if (InstructionsToSkipBeforeDeleting) { 642 --InstructionsToSkipBeforeDeleting; 643 } else { 644 if (BugpointIsInterrupted) goto ExitLoops; 645 646 if (isa<LandingPadInst>(I)) 647 continue; 648 649 outs() << "Checking instruction: " << *I; 650 std::unique_ptr<Module> M = 651 BD.deleteInstructionFromProgram(I, Simplification); 652 653 // Find out if the pass still crashes on this pass... 654 if (TestFn(BD, M.get())) { 655 // Yup, it does, we delete the old module, and continue trying 656 // to reduce the testcase... 657 BD.setNewProgram(M.release()); 658 InstructionsToSkipBeforeDeleting = CurInstructionNum; 659 goto TryAgain; // I wish I had a multi-level break here! 660 } 661 } 662 } 663 664 if (InstructionsToSkipBeforeDeleting) { 665 InstructionsToSkipBeforeDeleting = 0; 666 goto TryAgain; 667 } 668 669 } while (Simplification); 670 ExitLoops: 671 672 // Try to clean up the testcase by running funcresolve and globaldce... 673 if (!BugpointIsInterrupted) { 674 outs() << "\n*** Attempting to perform final cleanups: "; 675 Module *M = CloneModule(BD.getProgram()); 676 M = BD.performFinalCleanups(M, true).release(); 677 678 // Find out if the pass still crashes on the cleaned up program... 679 if (TestFn(BD, M)) { 680 BD.setNewProgram(M); // Yup, it does, keep the reduced version... 681 } else { 682 delete M; 683 } 684 } 685 686 BD.EmitProgressBitcode(BD.getProgram(), "reduced-simplified"); 687 688 return false; 689 } 690 691 static bool TestForOptimizerCrash(const BugDriver &BD, Module *M) { 692 return BD.runPasses(M); 693 } 694 695 /// debugOptimizerCrash - This method is called when some pass crashes on input. 696 /// It attempts to prune down the testcase to something reasonable, and figure 697 /// out exactly which pass is crashing. 698 /// 699 bool BugDriver::debugOptimizerCrash(const std::string &ID) { 700 outs() << "\n*** Debugging optimizer crash!\n"; 701 702 std::string Error; 703 // Reduce the list of passes which causes the optimizer to crash... 704 if (!BugpointIsInterrupted && !DontReducePassList) 705 ReducePassList(*this).reduceList(PassesToRun, Error); 706 assert(Error.empty()); 707 708 outs() << "\n*** Found crashing pass" 709 << (PassesToRun.size() == 1 ? ": " : "es: ") 710 << getPassesString(PassesToRun) << '\n'; 711 712 EmitProgressBitcode(Program, ID); 713 714 bool Success = DebugACrash(*this, TestForOptimizerCrash, Error); 715 assert(Error.empty()); 716 return Success; 717 } 718 719 static bool TestForCodeGenCrash(const BugDriver &BD, Module *M) { 720 std::string Error; 721 BD.compileProgram(M, &Error); 722 if (!Error.empty()) { 723 errs() << "<crash>\n"; 724 return true; // Tool is still crashing. 725 } 726 errs() << '\n'; 727 return false; 728 } 729 730 /// debugCodeGeneratorCrash - This method is called when the code generator 731 /// crashes on an input. It attempts to reduce the input as much as possible 732 /// while still causing the code generator to crash. 733 bool BugDriver::debugCodeGeneratorCrash(std::string &Error) { 734 errs() << "*** Debugging code generator crash!\n"; 735 736 return DebugACrash(*this, TestForCodeGenCrash, Error); 737 } 738