1 //===- CrashDebugger.cpp - Debug compilation crashes ----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the bugpoint internals that narrow down compilation crashes
12 //===----------------------------------------------------------------------===//
14 #include "BugDriver.h"
15 #include "ListReducer.h"
16 #include "ToolRunner.h"
17 #include "llvm/ADT/SmallPtrSet.h"
18 #include "llvm/Analysis/Verifier.h"
19 #include "llvm/IR/Constants.h"
20 #include "llvm/IR/DerivedTypes.h"
21 #include "llvm/IR/Instructions.h"
22 #include "llvm/IR/Module.h"
23 #include "llvm/IR/ValueSymbolTable.h"
24 #include "llvm/Pass.h"
25 #include "llvm/PassManager.h"
26 #include "llvm/Support/CFG.h"
27 #include "llvm/Support/CommandLine.h"
28 #include "llvm/Support/FileUtilities.h"
29 #include "llvm/Support/PathV1.h"
30 #include "llvm/Transforms/Scalar.h"
31 #include "llvm/Transforms/Utils/Cloning.h"
38 cl::desc("Force function reduction to keep main"),
41 NoGlobalRM ("disable-global-remove",
42 cl::desc("Do not remove global variables"),
47 class ReducePassList : public ListReducer<std::string> {
50 ReducePassList(BugDriver &bd) : BD(bd) {}
52 // doTest - Return true iff running the "removed" passes succeeds, and
53 // running the "Kept" passes fail when run on the output of the "removed"
54 // passes. If we return true, we update the current module of bugpoint.
56 virtual TestResult doTest(std::vector<std::string> &Removed,
57 std::vector<std::string> &Kept,
62 ReducePassList::TestResult
63 ReducePassList::doTest(std::vector<std::string> &Prefix,
64 std::vector<std::string> &Suffix,
66 sys::Path PrefixOutput;
67 Module *OrigProgram = 0;
68 if (!Prefix.empty()) {
69 outs() << "Checking to see if these passes crash: "
70 << getPassesString(Prefix) << ": ";
71 std::string PfxOutput;
72 if (BD.runPasses(BD.getProgram(), Prefix, PfxOutput))
75 PrefixOutput.set(PfxOutput);
76 OrigProgram = BD.Program;
78 BD.Program = ParseInputFile(PrefixOutput.str(), BD.getContext());
79 if (BD.Program == 0) {
80 errs() << BD.getToolName() << ": Error reading bitcode file '"
81 << PrefixOutput.str() << "'!\n";
84 PrefixOutput.eraseFromDisk();
87 outs() << "Checking to see if these passes crash: "
88 << getPassesString(Suffix) << ": ";
90 if (BD.runPasses(BD.getProgram(), Suffix)) {
91 delete OrigProgram; // The suffix crashes alone...
95 // Nothing failed, restore state...
98 BD.Program = OrigProgram;
104 /// ReduceCrashingGlobalVariables - This works by removing the global
105 /// variable's initializer and seeing if the program still crashes. If it
106 /// does, then we keep that program and try again.
108 class ReduceCrashingGlobalVariables : public ListReducer<GlobalVariable*> {
110 bool (*TestFn)(const BugDriver &, Module *);
112 ReduceCrashingGlobalVariables(BugDriver &bd,
113 bool (*testFn)(const BugDriver &, Module *))
114 : BD(bd), TestFn(testFn) {}
116 virtual TestResult doTest(std::vector<GlobalVariable*> &Prefix,
117 std::vector<GlobalVariable*> &Kept,
118 std::string &Error) {
119 if (!Kept.empty() && TestGlobalVariables(Kept))
121 if (!Prefix.empty() && TestGlobalVariables(Prefix))
126 bool TestGlobalVariables(std::vector<GlobalVariable*> &GVs);
131 ReduceCrashingGlobalVariables::TestGlobalVariables(
132 std::vector<GlobalVariable*> &GVs) {
133 // Clone the program to try hacking it apart...
134 ValueToValueMapTy VMap;
135 Module *M = CloneModule(BD.getProgram(), VMap);
137 // Convert list to set for fast lookup...
138 std::set<GlobalVariable*> GVSet;
140 for (unsigned i = 0, e = GVs.size(); i != e; ++i) {
141 GlobalVariable* CMGV = cast<GlobalVariable>(VMap[GVs[i]]);
142 assert(CMGV && "Global Variable not in module?!");
146 outs() << "Checking for crash with only these global variables: ";
147 PrintGlobalVariableList(GVs);
150 // Loop over and delete any global variables which we aren't supposed to be
152 for (Module::global_iterator I = M->global_begin(), E = M->global_end();
154 if (I->hasInitializer() && !GVSet.count(I)) {
155 I->setInitializer(0);
156 I->setLinkage(GlobalValue::ExternalLinkage);
159 // Try running the hacked up program...
161 BD.setNewProgram(M); // It crashed, keep the trimmed version...
163 // Make sure to use global variable pointers that point into the now-current
165 GVs.assign(GVSet.begin(), GVSet.end());
174 /// ReduceCrashingFunctions reducer - This works by removing functions and
175 /// seeing if the program still crashes. If it does, then keep the newer,
178 class ReduceCrashingFunctions : public ListReducer<Function*> {
180 bool (*TestFn)(const BugDriver &, Module *);
182 ReduceCrashingFunctions(BugDriver &bd,
183 bool (*testFn)(const BugDriver &, Module *))
184 : BD(bd), TestFn(testFn) {}
186 virtual TestResult doTest(std::vector<Function*> &Prefix,
187 std::vector<Function*> &Kept,
188 std::string &Error) {
189 if (!Kept.empty() && TestFuncs(Kept))
191 if (!Prefix.empty() && TestFuncs(Prefix))
196 bool TestFuncs(std::vector<Function*> &Prefix);
200 bool ReduceCrashingFunctions::TestFuncs(std::vector<Function*> &Funcs) {
202 //if main isn't present, claim there is no problem
203 if (KeepMain && find(Funcs.begin(), Funcs.end(),
204 BD.getProgram()->getFunction("main")) == Funcs.end())
207 // Clone the program to try hacking it apart...
208 ValueToValueMapTy VMap;
209 Module *M = CloneModule(BD.getProgram(), VMap);
211 // Convert list to set for fast lookup...
212 std::set<Function*> Functions;
213 for (unsigned i = 0, e = Funcs.size(); i != e; ++i) {
214 Function *CMF = cast<Function>(VMap[Funcs[i]]);
215 assert(CMF && "Function not in module?!");
216 assert(CMF->getFunctionType() == Funcs[i]->getFunctionType() && "wrong ty");
217 assert(CMF->getName() == Funcs[i]->getName() && "wrong name");
218 Functions.insert(CMF);
221 outs() << "Checking for crash with only these functions: ";
222 PrintFunctionList(Funcs);
225 // Loop over and delete any functions which we aren't supposed to be playing
227 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
228 if (!I->isDeclaration() && !Functions.count(I))
229 DeleteFunctionBody(I);
231 // Try running the hacked up program...
233 BD.setNewProgram(M); // It crashed, keep the trimmed version...
235 // Make sure to use function pointers that point into the now-current
237 Funcs.assign(Functions.begin(), Functions.end());
246 /// ReduceCrashingBlocks reducer - This works by setting the terminators of
247 /// all terminators except the specified basic blocks to a 'ret' instruction,
248 /// then running the simplify-cfg pass. This has the effect of chopping up
249 /// the CFG really fast which can reduce large functions quickly.
251 class ReduceCrashingBlocks : public ListReducer<const BasicBlock*> {
253 bool (*TestFn)(const BugDriver &, Module *);
255 ReduceCrashingBlocks(BugDriver &bd,
256 bool (*testFn)(const BugDriver &, Module *))
257 : BD(bd), TestFn(testFn) {}
259 virtual TestResult doTest(std::vector<const BasicBlock*> &Prefix,
260 std::vector<const BasicBlock*> &Kept,
261 std::string &Error) {
262 if (!Kept.empty() && TestBlocks(Kept))
264 if (!Prefix.empty() && TestBlocks(Prefix))
269 bool TestBlocks(std::vector<const BasicBlock*> &Prefix);
273 bool ReduceCrashingBlocks::TestBlocks(std::vector<const BasicBlock*> &BBs) {
274 // Clone the program to try hacking it apart...
275 ValueToValueMapTy VMap;
276 Module *M = CloneModule(BD.getProgram(), VMap);
278 // Convert list to set for fast lookup...
279 SmallPtrSet<BasicBlock*, 8> Blocks;
280 for (unsigned i = 0, e = BBs.size(); i != e; ++i)
281 Blocks.insert(cast<BasicBlock>(VMap[BBs[i]]));
283 outs() << "Checking for crash with only these blocks:";
284 unsigned NumPrint = Blocks.size();
285 if (NumPrint > 10) NumPrint = 10;
286 for (unsigned i = 0, e = NumPrint; i != e; ++i)
287 outs() << " " << BBs[i]->getName();
288 if (NumPrint < Blocks.size())
289 outs() << "... <" << Blocks.size() << " total>";
292 // Loop over and delete any hack up any blocks that are not listed...
293 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
294 for (Function::iterator BB = I->begin(), E = I->end(); BB != E; ++BB)
295 if (!Blocks.count(BB) && BB->getTerminator()->getNumSuccessors()) {
296 // Loop over all of the successors of this block, deleting any PHI nodes
297 // that might include it.
298 for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI)
299 (*SI)->removePredecessor(BB);
301 TerminatorInst *BBTerm = BB->getTerminator();
303 if (!BB->getTerminator()->getType()->isVoidTy())
304 BBTerm->replaceAllUsesWith(Constant::getNullValue(BBTerm->getType()));
306 // Replace the old terminator instruction.
307 BB->getInstList().pop_back();
308 new UnreachableInst(BB->getContext(), BB);
311 // The CFG Simplifier pass may delete one of the basic blocks we are
312 // interested in. If it does we need to take the block out of the list. Make
313 // a "persistent mapping" by turning basic blocks into <function, name> pairs.
314 // This won't work well if blocks are unnamed, but that is just the risk we
316 std::vector<std::pair<std::string, std::string> > BlockInfo;
318 for (SmallPtrSet<BasicBlock*, 8>::iterator I = Blocks.begin(),
319 E = Blocks.end(); I != E; ++I)
320 BlockInfo.push_back(std::make_pair((*I)->getParent()->getName(),
323 // Now run the CFG simplify pass on the function...
324 std::vector<std::string> Passes;
325 Passes.push_back("simplifycfg");
326 Passes.push_back("verify");
327 Module *New = BD.runPassesOn(M, Passes);
330 errs() << "simplifycfg failed!\n";
335 // Try running on the hacked up program...
337 BD.setNewProgram(M); // It crashed, keep the trimmed version...
339 // Make sure to use basic block pointers that point into the now-current
340 // module, and that they don't include any deleted blocks.
342 const ValueSymbolTable &GST = M->getValueSymbolTable();
343 for (unsigned i = 0, e = BlockInfo.size(); i != e; ++i) {
344 Function *F = cast<Function>(GST.lookup(BlockInfo[i].first));
345 ValueSymbolTable &ST = F->getValueSymbolTable();
346 Value* V = ST.lookup(BlockInfo[i].second);
347 if (V && V->getType() == Type::getLabelTy(V->getContext()))
348 BBs.push_back(cast<BasicBlock>(V));
352 delete M; // It didn't crash, try something else.
357 /// ReduceCrashingInstructions reducer - This works by removing the specified
358 /// non-terminator instructions and replacing them with undef.
360 class ReduceCrashingInstructions : public ListReducer<const Instruction*> {
362 bool (*TestFn)(const BugDriver &, Module *);
364 ReduceCrashingInstructions(BugDriver &bd,
365 bool (*testFn)(const BugDriver &, Module *))
366 : BD(bd), TestFn(testFn) {}
368 virtual TestResult doTest(std::vector<const Instruction*> &Prefix,
369 std::vector<const Instruction*> &Kept,
370 std::string &Error) {
371 if (!Kept.empty() && TestInsts(Kept))
373 if (!Prefix.empty() && TestInsts(Prefix))
378 bool TestInsts(std::vector<const Instruction*> &Prefix);
382 bool ReduceCrashingInstructions::TestInsts(std::vector<const Instruction*>
384 // Clone the program to try hacking it apart...
385 ValueToValueMapTy VMap;
386 Module *M = CloneModule(BD.getProgram(), VMap);
388 // Convert list to set for fast lookup...
389 SmallPtrSet<Instruction*, 64> Instructions;
390 for (unsigned i = 0, e = Insts.size(); i != e; ++i) {
391 assert(!isa<TerminatorInst>(Insts[i]));
392 Instructions.insert(cast<Instruction>(VMap[Insts[i]]));
395 outs() << "Checking for crash with only " << Instructions.size();
396 if (Instructions.size() == 1)
397 outs() << " instruction: ";
399 outs() << " instructions: ";
401 for (Module::iterator MI = M->begin(), ME = M->end(); MI != ME; ++MI)
402 for (Function::iterator FI = MI->begin(), FE = MI->end(); FI != FE; ++FI)
403 for (BasicBlock::iterator I = FI->begin(), E = FI->end(); I != E;) {
404 Instruction *Inst = I++;
405 if (!Instructions.count(Inst) && !isa<TerminatorInst>(Inst) &&
406 !isa<LandingPadInst>(Inst)) {
407 if (!Inst->getType()->isVoidTy())
408 Inst->replaceAllUsesWith(UndefValue::get(Inst->getType()));
409 Inst->eraseFromParent();
413 // Verify that this is still valid.
415 Passes.add(createVerifierPass());
418 // Try running on the hacked up program...
420 BD.setNewProgram(M); // It crashed, keep the trimmed version...
422 // Make sure to use instruction pointers that point into the now-current
423 // module, and that they don't include any deleted blocks.
425 for (SmallPtrSet<Instruction*, 64>::const_iterator I = Instructions.begin(),
426 E = Instructions.end(); I != E; ++I)
430 delete M; // It didn't crash, try something else.
434 /// DebugACrash - Given a predicate that determines whether a component crashes
435 /// on a program, try to destructively reduce the program while still keeping
436 /// the predicate true.
437 static bool DebugACrash(BugDriver &BD,
438 bool (*TestFn)(const BugDriver &, Module *),
439 std::string &Error) {
440 // See if we can get away with nuking some of the global variable initializers
443 BD.getProgram()->global_begin() != BD.getProgram()->global_end()) {
444 // Now try to reduce the number of global variable initializers in the
445 // module to something small.
446 Module *M = CloneModule(BD.getProgram());
447 bool DeletedInit = false;
449 for (Module::global_iterator I = M->global_begin(), E = M->global_end();
451 if (I->hasInitializer()) {
452 I->setInitializer(0);
453 I->setLinkage(GlobalValue::ExternalLinkage);
458 delete M; // No change made...
460 // See if the program still causes a crash...
461 outs() << "\nChecking to see if we can delete global inits: ";
463 if (TestFn(BD, M)) { // Still crashes?
465 outs() << "\n*** Able to remove all global initializers!\n";
466 } else { // No longer crashes?
467 outs() << " - Removing all global inits hides problem!\n";
470 std::vector<GlobalVariable*> GVs;
472 for (Module::global_iterator I = BD.getProgram()->global_begin(),
473 E = BD.getProgram()->global_end(); I != E; ++I)
474 if (I->hasInitializer())
477 if (GVs.size() > 1 && !BugpointIsInterrupted) {
478 outs() << "\n*** Attempting to reduce the number of global "
479 << "variables in the testcase\n";
481 unsigned OldSize = GVs.size();
482 ReduceCrashingGlobalVariables(BD, TestFn).reduceList(GVs, Error);
486 if (GVs.size() < OldSize)
487 BD.EmitProgressBitcode(BD.getProgram(), "reduced-global-variables");
493 // Now try to reduce the number of functions in the module to something small.
494 std::vector<Function*> Functions;
495 for (Module::iterator I = BD.getProgram()->begin(),
496 E = BD.getProgram()->end(); I != E; ++I)
497 if (!I->isDeclaration())
498 Functions.push_back(I);
500 if (Functions.size() > 1 && !BugpointIsInterrupted) {
501 outs() << "\n*** Attempting to reduce the number of functions "
504 unsigned OldSize = Functions.size();
505 ReduceCrashingFunctions(BD, TestFn).reduceList(Functions, Error);
507 if (Functions.size() < OldSize)
508 BD.EmitProgressBitcode(BD.getProgram(), "reduced-function");
511 // Attempt to delete entire basic blocks at a time to speed up
512 // convergence... this actually works by setting the terminator of the blocks
513 // to a return instruction then running simplifycfg, which can potentially
514 // shrinks the code dramatically quickly
516 if (!DisableSimplifyCFG && !BugpointIsInterrupted) {
517 std::vector<const BasicBlock*> Blocks;
518 for (Module::const_iterator I = BD.getProgram()->begin(),
519 E = BD.getProgram()->end(); I != E; ++I)
520 for (Function::const_iterator FI = I->begin(), E = I->end(); FI !=E; ++FI)
521 Blocks.push_back(FI);
522 unsigned OldSize = Blocks.size();
523 ReduceCrashingBlocks(BD, TestFn).reduceList(Blocks, Error);
524 if (Blocks.size() < OldSize)
525 BD.EmitProgressBitcode(BD.getProgram(), "reduced-blocks");
528 // Attempt to delete instructions using bisection. This should help out nasty
529 // cases with large basic blocks where the problem is at one end.
530 if (!BugpointIsInterrupted) {
531 std::vector<const Instruction*> Insts;
532 for (Module::const_iterator MI = BD.getProgram()->begin(),
533 ME = BD.getProgram()->end(); MI != ME; ++MI)
534 for (Function::const_iterator FI = MI->begin(), FE = MI->end(); FI != FE;
536 for (BasicBlock::const_iterator I = FI->begin(), E = FI->end();
538 if (!isa<TerminatorInst>(I))
541 ReduceCrashingInstructions(BD, TestFn).reduceList(Insts, Error);
544 // FIXME: This should use the list reducer to converge faster by deleting
545 // larger chunks of instructions at a time!
546 unsigned Simplification = 2;
548 if (BugpointIsInterrupted) break;
550 outs() << "\n*** Attempting to reduce testcase by deleting instruc"
551 << "tions: Simplification Level #" << Simplification << '\n';
553 // Now that we have deleted the functions that are unnecessary for the
554 // program, try to remove instructions that are not necessary to cause the
555 // crash. To do this, we loop through all of the instructions in the
556 // remaining functions, deleting them (replacing any values produced with
557 // nulls), and then running ADCE and SimplifyCFG. If the transformed input
558 // still triggers failure, keep deleting until we cannot trigger failure
561 unsigned InstructionsToSkipBeforeDeleting = 0;
564 // Loop over all of the (non-terminator) instructions remaining in the
565 // function, attempting to delete them.
566 unsigned CurInstructionNum = 0;
567 for (Module::const_iterator FI = BD.getProgram()->begin(),
568 E = BD.getProgram()->end(); FI != E; ++FI)
569 if (!FI->isDeclaration())
570 for (Function::const_iterator BI = FI->begin(), E = FI->end(); BI != E;
572 for (BasicBlock::const_iterator I = BI->begin(), E = --BI->end();
573 I != E; ++I, ++CurInstructionNum) {
574 if (InstructionsToSkipBeforeDeleting) {
575 --InstructionsToSkipBeforeDeleting;
577 if (BugpointIsInterrupted) goto ExitLoops;
579 if (isa<LandingPadInst>(I))
582 outs() << "Checking instruction: " << *I;
583 Module *M = BD.deleteInstructionFromProgram(I, Simplification);
585 // Find out if the pass still crashes on this pass...
587 // Yup, it does, we delete the old module, and continue trying
588 // to reduce the testcase...
590 InstructionsToSkipBeforeDeleting = CurInstructionNum;
591 goto TryAgain; // I wish I had a multi-level break here!
594 // This pass didn't crash without this instruction, try the next
600 if (InstructionsToSkipBeforeDeleting) {
601 InstructionsToSkipBeforeDeleting = 0;
605 } while (Simplification);
608 // Try to clean up the testcase by running funcresolve and globaldce...
609 if (!BugpointIsInterrupted) {
610 outs() << "\n*** Attempting to perform final cleanups: ";
611 Module *M = CloneModule(BD.getProgram());
612 M = BD.performFinalCleanups(M, true);
614 // Find out if the pass still crashes on the cleaned up program...
616 BD.setNewProgram(M); // Yup, it does, keep the reduced version...
622 BD.EmitProgressBitcode(BD.getProgram(), "reduced-simplified");
627 static bool TestForOptimizerCrash(const BugDriver &BD, Module *M) {
628 return BD.runPasses(M);
631 /// debugOptimizerCrash - This method is called when some pass crashes on input.
632 /// It attempts to prune down the testcase to something reasonable, and figure
633 /// out exactly which pass is crashing.
635 bool BugDriver::debugOptimizerCrash(const std::string &ID) {
636 outs() << "\n*** Debugging optimizer crash!\n";
639 // Reduce the list of passes which causes the optimizer to crash...
640 if (!BugpointIsInterrupted)
641 ReducePassList(*this).reduceList(PassesToRun, Error);
642 assert(Error.empty());
644 outs() << "\n*** Found crashing pass"
645 << (PassesToRun.size() == 1 ? ": " : "es: ")
646 << getPassesString(PassesToRun) << '\n';
648 EmitProgressBitcode(Program, ID);
650 bool Success = DebugACrash(*this, TestForOptimizerCrash, Error);
651 assert(Error.empty());
655 static bool TestForCodeGenCrash(const BugDriver &BD, Module *M) {
657 BD.compileProgram(M, &Error);
658 if (!Error.empty()) {
659 errs() << "<crash>\n";
660 return true; // Tool is still crashing.
666 /// debugCodeGeneratorCrash - This method is called when the code generator
667 /// crashes on an input. It attempts to reduce the input as much as possible
668 /// while still causing the code generator to crash.
669 bool BugDriver::debugCodeGeneratorCrash(std::string &Error) {
670 errs() << "*** Debugging code generator crash!\n";
672 return DebugACrash(*this, TestForCodeGenCrash, Error);