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/ADT/StringSet.h"
19 #include "llvm/IR/CFG.h"
20 #include "llvm/IR/Constants.h"
21 #include "llvm/IR/DerivedTypes.h"
22 #include "llvm/IR/Instructions.h"
23 #include "llvm/IR/LegacyPassManager.h"
24 #include "llvm/IR/Module.h"
25 #include "llvm/IR/ValueSymbolTable.h"
26 #include "llvm/IR/Verifier.h"
27 #include "llvm/Pass.h"
28 #include "llvm/Support/CommandLine.h"
29 #include "llvm/Support/FileUtilities.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"),
46 ReplaceFuncsWithNull("replace-funcs-with-null",
47 cl::desc("When stubbing functions, replace all uses will null"),
50 DontReducePassList("disable-pass-list-reduction",
51 cl::desc("Skip pass list reduction steps"),
54 cl::opt<bool> NoNamedMDRM("disable-namedmd-remove",
55 cl::desc("Do not remove global named metadata"),
57 cl::opt<bool> VerboseErrors("verbose-errors",
58 cl::desc("Print the output of crashing program"),
63 class ReducePassList : public ListReducer<std::string> {
66 ReducePassList(BugDriver &bd) : BD(bd) {}
68 // doTest - Return true iff running the "removed" passes succeeds, and
69 // running the "Kept" passes fail when run on the output of the "removed"
70 // passes. If we return true, we update the current module of bugpoint.
72 TestResult doTest(std::vector<std::string> &Removed,
73 std::vector<std::string> &Kept,
74 std::string &Error) override;
78 ReducePassList::TestResult
79 ReducePassList::doTest(std::vector<std::string> &Prefix,
80 std::vector<std::string> &Suffix,
82 std::string PrefixOutput;
83 Module *OrigProgram = nullptr;
84 if (!Prefix.empty()) {
85 outs() << "Checking to see if these passes crash: "
86 << getPassesString(Prefix) << ": ";
87 if (BD.runPasses(BD.getProgram(), Prefix, PrefixOutput))
90 OrigProgram = BD.Program;
92 BD.Program = parseInputFile(PrefixOutput, BD.getContext()).release();
93 if (BD.Program == nullptr) {
94 errs() << BD.getToolName() << ": Error reading bitcode file '"
95 << PrefixOutput << "'!\n";
98 sys::fs::remove(PrefixOutput);
101 outs() << "Checking to see if these passes crash: "
102 << getPassesString(Suffix) << ": ";
104 if (BD.runPasses(BD.getProgram(), Suffix)) {
105 delete OrigProgram; // The suffix crashes alone...
109 // Nothing failed, restore state...
112 BD.Program = OrigProgram;
118 /// ReduceCrashingGlobalVariables - This works by removing the global
119 /// variable's initializer and seeing if the program still crashes. If it
120 /// does, then we keep that program and try again.
122 class ReduceCrashingGlobalVariables : public ListReducer<GlobalVariable*> {
124 bool (*TestFn)(const BugDriver &, Module *);
126 ReduceCrashingGlobalVariables(BugDriver &bd,
127 bool (*testFn)(const BugDriver &, Module *))
128 : BD(bd), TestFn(testFn) {}
130 TestResult doTest(std::vector<GlobalVariable*> &Prefix,
131 std::vector<GlobalVariable*> &Kept,
132 std::string &Error) override {
133 if (!Kept.empty() && TestGlobalVariables(Kept))
135 if (!Prefix.empty() && TestGlobalVariables(Prefix))
140 bool TestGlobalVariables(std::vector<GlobalVariable*> &GVs);
145 ReduceCrashingGlobalVariables::TestGlobalVariables(
146 std::vector<GlobalVariable*> &GVs) {
147 // Clone the program to try hacking it apart...
148 ValueToValueMapTy VMap;
149 Module *M = CloneModule(BD.getProgram(), VMap).release();
151 // Convert list to set for fast lookup...
152 std::set<GlobalVariable*> GVSet;
154 for (unsigned i = 0, e = GVs.size(); i != e; ++i) {
155 GlobalVariable* CMGV = cast<GlobalVariable>(VMap[GVs[i]]);
156 assert(CMGV && "Global Variable not in module?!");
160 outs() << "Checking for crash with only these global variables: ";
161 PrintGlobalVariableList(GVs);
164 // Loop over and delete any global variables which we aren't supposed to be
166 for (GlobalVariable &I : M->globals())
167 if (I.hasInitializer() && !GVSet.count(&I)) {
168 DeleteGlobalInitializer(&I);
169 I.setLinkage(GlobalValue::ExternalLinkage);
170 I.setComdat(nullptr);
173 // Try running the hacked up program...
175 BD.setNewProgram(M); // It crashed, keep the trimmed version...
177 // Make sure to use global variable pointers that point into the now-current
179 GVs.assign(GVSet.begin(), GVSet.end());
188 /// ReduceCrashingFunctions reducer - This works by removing functions and
189 /// seeing if the program still crashes. If it does, then keep the newer,
192 class ReduceCrashingFunctions : public ListReducer<Function*> {
194 bool (*TestFn)(const BugDriver &, Module *);
196 ReduceCrashingFunctions(BugDriver &bd,
197 bool (*testFn)(const BugDriver &, Module *))
198 : BD(bd), TestFn(testFn) {}
200 TestResult doTest(std::vector<Function*> &Prefix,
201 std::vector<Function*> &Kept,
202 std::string &Error) override {
203 if (!Kept.empty() && TestFuncs(Kept))
205 if (!Prefix.empty() && TestFuncs(Prefix))
210 bool TestFuncs(std::vector<Function*> &Prefix);
214 static void RemoveFunctionReferences(Module *M, const char* Name) {
215 auto *UsedVar = M->getGlobalVariable(Name, true);
216 if (!UsedVar || !UsedVar->hasInitializer()) return;
217 if (isa<ConstantAggregateZero>(UsedVar->getInitializer())) {
218 assert(UsedVar->use_empty());
219 UsedVar->eraseFromParent();
222 auto *OldUsedVal = cast<ConstantArray>(UsedVar->getInitializer());
223 std::vector<Constant*> Used;
224 for(Value *V : OldUsedVal->operand_values()) {
225 Constant *Op = cast<Constant>(V->stripPointerCasts());
226 if(!Op->isNullValue()) {
227 Used.push_back(cast<Constant>(V));
230 auto *NewValElemTy = OldUsedVal->getType()->getElementType();
231 auto *NewValTy = ArrayType::get(NewValElemTy, Used.size());
232 auto *NewUsedVal = ConstantArray::get(NewValTy, Used);
233 UsedVar->mutateType(NewUsedVal->getType()->getPointerTo());
234 UsedVar->setInitializer(NewUsedVal);
237 bool ReduceCrashingFunctions::TestFuncs(std::vector<Function*> &Funcs) {
238 // If main isn't present, claim there is no problem.
239 if (KeepMain && std::find(Funcs.begin(), Funcs.end(),
240 BD.getProgram()->getFunction("main")) ==
244 // Clone the program to try hacking it apart...
245 ValueToValueMapTy VMap;
246 Module *M = CloneModule(BD.getProgram(), VMap).release();
248 // Convert list to set for fast lookup...
249 std::set<Function*> Functions;
250 for (unsigned i = 0, e = Funcs.size(); i != e; ++i) {
251 Function *CMF = cast<Function>(VMap[Funcs[i]]);
252 assert(CMF && "Function not in module?!");
253 assert(CMF->getFunctionType() == Funcs[i]->getFunctionType() && "wrong ty");
254 assert(CMF->getName() == Funcs[i]->getName() && "wrong name");
255 Functions.insert(CMF);
258 outs() << "Checking for crash with only these functions: ";
259 PrintFunctionList(Funcs);
261 if (!ReplaceFuncsWithNull) {
262 // Loop over and delete any functions which we aren't supposed to be playing
264 for (Function &I : *M)
265 if (!I.isDeclaration() && !Functions.count(&I))
266 DeleteFunctionBody(&I);
268 std::vector<GlobalValue*> ToRemove;
269 // First, remove aliases to functions we're about to purge.
270 for (GlobalAlias &Alias : M->aliases()) {
271 GlobalObject *Root = Alias.getBaseObject();
272 Function *F = dyn_cast_or_null<Function>(Root);
274 if (Functions.count(F))
275 // We're keeping this function.
277 } else if (Root->isNullValue()) {
278 // This referenced a globalalias that we've already replaced,
279 // so we still need to replace this alias.
281 // Not a function, therefore not something we mess with.
285 PointerType *Ty = cast<PointerType>(Alias.getType());
286 Constant *Replacement = ConstantPointerNull::get(Ty);
287 Alias.replaceAllUsesWith(Replacement);
288 ToRemove.push_back(&Alias);
291 for (Function &I : *M) {
292 if (!I.isDeclaration() && !Functions.count(&I)) {
293 PointerType *Ty = cast<PointerType>(I.getType());
294 Constant *Replacement = ConstantPointerNull::get(Ty);
295 I.replaceAllUsesWith(Replacement);
296 ToRemove.push_back(&I);
300 for (auto *F : ToRemove) {
301 F->eraseFromParent();
304 // Finally, remove any null members from any global intrinsic.
305 RemoveFunctionReferences(M, "llvm.used");
306 RemoveFunctionReferences(M, "llvm.compiler.used");
308 // Try running the hacked up program...
310 BD.setNewProgram(M); // It crashed, keep the trimmed version...
312 // Make sure to use function pointers that point into the now-current
314 Funcs.assign(Functions.begin(), Functions.end());
323 /// ReduceCrashingBlocks reducer - This works by setting the terminators of
324 /// all terminators except the specified basic blocks to a 'ret' instruction,
325 /// then running the simplify-cfg pass. This has the effect of chopping up
326 /// the CFG really fast which can reduce large functions quickly.
328 class ReduceCrashingBlocks : public ListReducer<const BasicBlock*> {
330 bool (*TestFn)(const BugDriver &, Module *);
332 ReduceCrashingBlocks(BugDriver &bd,
333 bool (*testFn)(const BugDriver &, Module *))
334 : BD(bd), TestFn(testFn) {}
336 TestResult doTest(std::vector<const BasicBlock*> &Prefix,
337 std::vector<const BasicBlock*> &Kept,
338 std::string &Error) override {
339 if (!Kept.empty() && TestBlocks(Kept))
341 if (!Prefix.empty() && TestBlocks(Prefix))
346 bool TestBlocks(std::vector<const BasicBlock*> &Prefix);
350 bool ReduceCrashingBlocks::TestBlocks(std::vector<const BasicBlock*> &BBs) {
351 // Clone the program to try hacking it apart...
352 ValueToValueMapTy VMap;
353 Module *M = CloneModule(BD.getProgram(), VMap).release();
355 // Convert list to set for fast lookup...
356 SmallPtrSet<BasicBlock*, 8> Blocks;
357 for (unsigned i = 0, e = BBs.size(); i != e; ++i)
358 Blocks.insert(cast<BasicBlock>(VMap[BBs[i]]));
360 outs() << "Checking for crash with only these blocks:";
361 unsigned NumPrint = Blocks.size();
362 if (NumPrint > 10) NumPrint = 10;
363 for (unsigned i = 0, e = NumPrint; i != e; ++i)
364 outs() << " " << BBs[i]->getName();
365 if (NumPrint < Blocks.size())
366 outs() << "... <" << Blocks.size() << " total>";
369 // Loop over and delete any hack up any blocks that are not listed...
370 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
371 for (Function::iterator BB = I->begin(), E = I->end(); BB != E; ++BB)
372 if (!Blocks.count(&*BB) && BB->getTerminator()->getNumSuccessors()) {
373 // Loop over all of the successors of this block, deleting any PHI nodes
374 // that might include it.
375 for (succ_iterator SI = succ_begin(&*BB), E = succ_end(&*BB); SI != E;
377 (*SI)->removePredecessor(&*BB);
379 TerminatorInst *BBTerm = BB->getTerminator();
380 if (BBTerm->isEHPad() || BBTerm->getType()->isTokenTy())
382 if (!BBTerm->getType()->isVoidTy())
383 BBTerm->replaceAllUsesWith(Constant::getNullValue(BBTerm->getType()));
385 // Replace the old terminator instruction.
386 BB->getInstList().pop_back();
387 new UnreachableInst(BB->getContext(), &*BB);
390 // The CFG Simplifier pass may delete one of the basic blocks we are
391 // interested in. If it does we need to take the block out of the list. Make
392 // a "persistent mapping" by turning basic blocks into <function, name> pairs.
393 // This won't work well if blocks are unnamed, but that is just the risk we
395 std::vector<std::pair<std::string, std::string> > BlockInfo;
397 for (BasicBlock *BB : Blocks)
398 BlockInfo.emplace_back(BB->getParent()->getName(), BB->getName());
400 // Now run the CFG simplify pass on the function...
401 std::vector<std::string> Passes;
402 Passes.push_back("simplifycfg");
403 Passes.push_back("verify");
404 std::unique_ptr<Module> New = BD.runPassesOn(M, Passes);
407 errs() << "simplifycfg failed!\n";
412 // Try running on the hacked up program...
414 BD.setNewProgram(M); // It crashed, keep the trimmed version...
416 // Make sure to use basic block pointers that point into the now-current
417 // module, and that they don't include any deleted blocks.
419 const ValueSymbolTable &GST = M->getValueSymbolTable();
420 for (unsigned i = 0, e = BlockInfo.size(); i != e; ++i) {
421 Function *F = cast<Function>(GST.lookup(BlockInfo[i].first));
422 ValueSymbolTable &ST = F->getValueSymbolTable();
423 Value* V = ST.lookup(BlockInfo[i].second);
424 if (V && V->getType() == Type::getLabelTy(V->getContext()))
425 BBs.push_back(cast<BasicBlock>(V));
429 delete M; // It didn't crash, try something else.
434 /// ReduceCrashingInstructions reducer - This works by removing the specified
435 /// non-terminator instructions and replacing them with undef.
437 class ReduceCrashingInstructions : public ListReducer<const Instruction*> {
439 bool (*TestFn)(const BugDriver &, Module *);
441 ReduceCrashingInstructions(BugDriver &bd,
442 bool (*testFn)(const BugDriver &, Module *))
443 : BD(bd), TestFn(testFn) {}
445 TestResult doTest(std::vector<const Instruction*> &Prefix,
446 std::vector<const Instruction*> &Kept,
447 std::string &Error) override {
448 if (!Kept.empty() && TestInsts(Kept))
450 if (!Prefix.empty() && TestInsts(Prefix))
455 bool TestInsts(std::vector<const Instruction*> &Prefix);
459 bool ReduceCrashingInstructions::TestInsts(std::vector<const Instruction*>
461 // Clone the program to try hacking it apart...
462 ValueToValueMapTy VMap;
463 Module *M = CloneModule(BD.getProgram(), VMap).release();
465 // Convert list to set for fast lookup...
466 SmallPtrSet<Instruction*, 32> Instructions;
467 for (unsigned i = 0, e = Insts.size(); i != e; ++i) {
468 assert(!isa<TerminatorInst>(Insts[i]));
469 Instructions.insert(cast<Instruction>(VMap[Insts[i]]));
472 outs() << "Checking for crash with only " << Instructions.size();
473 if (Instructions.size() == 1)
474 outs() << " instruction: ";
476 outs() << " instructions: ";
478 for (Module::iterator MI = M->begin(), ME = M->end(); MI != ME; ++MI)
479 for (Function::iterator FI = MI->begin(), FE = MI->end(); FI != FE; ++FI)
480 for (BasicBlock::iterator I = FI->begin(), E = FI->end(); I != E;) {
481 Instruction *Inst = &*I++;
482 if (!Instructions.count(Inst) && !isa<TerminatorInst>(Inst) &&
483 !Inst->isEHPad() && !Inst->getType()->isTokenTy()) {
484 if (!Inst->getType()->isVoidTy())
485 Inst->replaceAllUsesWith(UndefValue::get(Inst->getType()));
486 Inst->eraseFromParent();
490 // Verify that this is still valid.
491 legacy::PassManager Passes;
492 Passes.add(createVerifierPass());
495 // Try running on the hacked up program...
497 BD.setNewProgram(M); // It crashed, keep the trimmed version...
499 // Make sure to use instruction pointers that point into the now-current
500 // module, and that they don't include any deleted blocks.
502 for (Instruction *Inst : Instructions)
503 Insts.push_back(Inst);
506 delete M; // It didn't crash, try something else.
511 // Reduce the list of Named Metadata nodes. We keep this as a list of
512 // names to avoid having to convert back and forth every time.
513 class ReduceCrashingNamedMD : public ListReducer<std::string> {
515 bool (*TestFn)(const BugDriver &, Module *);
518 ReduceCrashingNamedMD(BugDriver &bd,
519 bool (*testFn)(const BugDriver &, Module *))
520 : BD(bd), TestFn(testFn) {}
522 TestResult doTest(std::vector<std::string> &Prefix,
523 std::vector<std::string> &Kept,
524 std::string &Error) override {
525 if (!Kept.empty() && TestNamedMDs(Kept))
527 if (!Prefix.empty() && TestNamedMDs(Prefix))
532 bool TestNamedMDs(std::vector<std::string> &NamedMDs);
536 bool ReduceCrashingNamedMD::TestNamedMDs(std::vector<std::string> &NamedMDs) {
538 ValueToValueMapTy VMap;
539 Module *M = CloneModule(BD.getProgram(), VMap).release();
541 outs() << "Checking for crash with only these named metadata nodes:";
542 unsigned NumPrint = std::min<size_t>(NamedMDs.size(), 10);
543 for (unsigned i = 0, e = NumPrint; i != e; ++i)
544 outs() << " " << NamedMDs[i];
545 if (NumPrint < NamedMDs.size())
546 outs() << "... <" << NamedMDs.size() << " total>";
549 // Make a StringMap for faster lookup
551 for (const std::string &Name : NamedMDs)
554 // First collect all the metadata to delete in a vector, then
555 // delete them all at once to avoid invalidating the iterator
556 std::vector<NamedMDNode *> ToDelete;
557 ToDelete.reserve(M->named_metadata_size() - Names.size());
558 for (auto &NamedMD : M->named_metadata())
559 // Always keep a nonempty llvm.dbg.cu because the Verifier would complain.
560 if (!Names.count(NamedMD.getName()) &&
561 (!(NamedMD.getName() == "llvm.dbg.cu" && NamedMD.getNumOperands() > 0)))
562 ToDelete.push_back(&NamedMD);
564 for (auto *NamedMD : ToDelete)
565 NamedMD->eraseFromParent();
567 // Verify that this is still valid.
568 legacy::PassManager Passes;
569 Passes.add(createVerifierPass());
572 // Try running on the hacked up program...
574 BD.setNewProgram(M); // It crashed, keep the trimmed version...
577 delete M; // It didn't crash, try something else.
582 // Reduce the list of operands to named metadata nodes
583 class ReduceCrashingNamedMDOps : public ListReducer<const MDNode *> {
585 bool (*TestFn)(const BugDriver &, Module *);
588 ReduceCrashingNamedMDOps(BugDriver &bd,
589 bool (*testFn)(const BugDriver &, Module *))
590 : BD(bd), TestFn(testFn) {}
592 TestResult doTest(std::vector<const MDNode *> &Prefix,
593 std::vector<const MDNode *> &Kept,
594 std::string &Error) override {
595 if (!Kept.empty() && TestNamedMDOps(Kept))
597 if (!Prefix.empty() && TestNamedMDOps(Prefix))
602 bool TestNamedMDOps(std::vector<const MDNode *> &NamedMDOps);
606 bool ReduceCrashingNamedMDOps::TestNamedMDOps(
607 std::vector<const MDNode *> &NamedMDOps) {
608 // Convert list to set for fast lookup...
609 SmallPtrSet<const MDNode *, 32> OldMDNodeOps;
610 for (unsigned i = 0, e = NamedMDOps.size(); i != e; ++i) {
611 OldMDNodeOps.insert(NamedMDOps[i]);
614 outs() << "Checking for crash with only " << OldMDNodeOps.size();
615 if (OldMDNodeOps.size() == 1)
616 outs() << " named metadata operand: ";
618 outs() << " named metadata operands: ";
620 ValueToValueMapTy VMap;
621 Module *M = CloneModule(BD.getProgram(), VMap).release();
623 // This is a little wasteful. In the future it might be good if we could have
624 // these dropped during cloning.
625 for (auto &NamedMD : BD.getProgram()->named_metadata()) {
626 // Drop the old one and create a new one
627 M->eraseNamedMetadata(M->getNamedMetadata(NamedMD.getName()));
628 NamedMDNode *NewNamedMDNode =
629 M->getOrInsertNamedMetadata(NamedMD.getName());
630 for (MDNode *op : NamedMD.operands())
631 if (OldMDNodeOps.count(op))
632 NewNamedMDNode->addOperand(cast<MDNode>(MapMetadata(op, VMap)));
635 // Verify that this is still valid.
636 legacy::PassManager Passes;
637 Passes.add(createVerifierPass());
640 // Try running on the hacked up program...
642 // Make sure to use instruction pointers that point into the now-current
643 // module, and that they don't include any deleted blocks.
645 for (const MDNode *Node : OldMDNodeOps)
646 NamedMDOps.push_back(cast<MDNode>(*VMap.getMappedMD(Node)));
648 BD.setNewProgram(M); // It crashed, keep the trimmed version...
651 delete M; // It didn't crash, try something else.
655 static void ReduceGlobalInitializers(BugDriver &BD,
656 bool (*TestFn)(const BugDriver &, Module *),
657 std::string &Error) {
658 if (BD.getProgram()->global_begin() != BD.getProgram()->global_end()) {
659 // Now try to reduce the number of global variable initializers in the
660 // module to something small.
661 Module *M = CloneModule(BD.getProgram()).release();
662 bool DeletedInit = false;
664 for (Module::global_iterator I = M->global_begin(), E = M->global_end();
666 if (I->hasInitializer()) {
667 DeleteGlobalInitializer(&*I);
668 I->setLinkage(GlobalValue::ExternalLinkage);
669 I->setComdat(nullptr);
674 delete M; // No change made...
676 // See if the program still causes a crash...
677 outs() << "\nChecking to see if we can delete global inits: ";
679 if (TestFn(BD, M)) { // Still crashes?
681 outs() << "\n*** Able to remove all global initializers!\n";
682 } else { // No longer crashes?
683 outs() << " - Removing all global inits hides problem!\n";
686 std::vector<GlobalVariable*> GVs;
688 for (Module::global_iterator I = BD.getProgram()->global_begin(),
689 E = BD.getProgram()->global_end(); I != E; ++I)
690 if (I->hasInitializer())
693 if (GVs.size() > 1 && !BugpointIsInterrupted) {
694 outs() << "\n*** Attempting to reduce the number of global "
695 << "variables in the testcase\n";
697 unsigned OldSize = GVs.size();
698 ReduceCrashingGlobalVariables(BD, TestFn).reduceList(GVs, Error);
699 assert(!Error.empty());
701 if (GVs.size() < OldSize)
702 BD.EmitProgressBitcode(BD.getProgram(), "reduced-global-variables");
709 static void ReduceInsts(BugDriver &BD,
710 bool (*TestFn)(const BugDriver &, Module *),
711 std::string &Error) {
712 // Attempt to delete instructions using bisection. This should help out nasty
713 // cases with large basic blocks where the problem is at one end.
714 if (!BugpointIsInterrupted) {
715 std::vector<const Instruction*> Insts;
716 for (const Function &F : *BD.getProgram())
717 for (const BasicBlock &BB : F)
718 for (const Instruction &I : BB)
719 if (!isa<TerminatorInst>(&I))
722 ReduceCrashingInstructions(BD, TestFn).reduceList(Insts, Error);
725 unsigned Simplification = 2;
727 if (BugpointIsInterrupted)
730 outs() << "\n*** Attempting to reduce testcase by deleting instruc"
731 << "tions: Simplification Level #" << Simplification << '\n';
733 // Now that we have deleted the functions that are unnecessary for the
734 // program, try to remove instructions that are not necessary to cause the
735 // crash. To do this, we loop through all of the instructions in the
736 // remaining functions, deleting them (replacing any values produced with
737 // nulls), and then running ADCE and SimplifyCFG. If the transformed input
738 // still triggers failure, keep deleting until we cannot trigger failure
741 unsigned InstructionsToSkipBeforeDeleting = 0;
744 // Loop over all of the (non-terminator) instructions remaining in the
745 // function, attempting to delete them.
746 unsigned CurInstructionNum = 0;
747 for (Module::const_iterator FI = BD.getProgram()->begin(),
748 E = BD.getProgram()->end(); FI != E; ++FI)
749 if (!FI->isDeclaration())
750 for (Function::const_iterator BI = FI->begin(), E = FI->end(); BI != E;
752 for (BasicBlock::const_iterator I = BI->begin(), E = --BI->end();
753 I != E; ++I, ++CurInstructionNum) {
754 if (InstructionsToSkipBeforeDeleting) {
755 --InstructionsToSkipBeforeDeleting;
757 if (BugpointIsInterrupted)
760 if (I->isEHPad() || I->getType()->isTokenTy())
763 outs() << "Checking instruction: " << *I;
764 std::unique_ptr<Module> M =
765 BD.deleteInstructionFromProgram(&*I, Simplification);
767 // Find out if the pass still crashes on this pass...
768 if (TestFn(BD, M.get())) {
769 // Yup, it does, we delete the old module, and continue trying
770 // to reduce the testcase...
771 BD.setNewProgram(M.release());
772 InstructionsToSkipBeforeDeleting = CurInstructionNum;
773 goto TryAgain; // I wish I had a multi-level break here!
778 if (InstructionsToSkipBeforeDeleting) {
779 InstructionsToSkipBeforeDeleting = 0;
783 } while (Simplification);
784 BD.EmitProgressBitcode(BD.getProgram(), "reduced-instructions");
788 /// DebugACrash - Given a predicate that determines whether a component crashes
789 /// on a program, try to destructively reduce the program while still keeping
790 /// the predicate true.
791 static bool DebugACrash(BugDriver &BD,
792 bool (*TestFn)(const BugDriver &, Module *),
793 std::string &Error) {
794 // See if we can get away with nuking some of the global variable initializers
797 ReduceGlobalInitializers(BD, TestFn, Error);
799 // Now try to reduce the number of functions in the module to something small.
800 std::vector<Function*> Functions;
801 for (Function &F : *BD.getProgram())
802 if (!F.isDeclaration())
803 Functions.push_back(&F);
805 if (Functions.size() > 1 && !BugpointIsInterrupted) {
806 outs() << "\n*** Attempting to reduce the number of functions "
809 unsigned OldSize = Functions.size();
810 ReduceCrashingFunctions(BD, TestFn).reduceList(Functions, Error);
812 if (Functions.size() < OldSize)
813 BD.EmitProgressBitcode(BD.getProgram(), "reduced-function");
816 // Attempt to delete entire basic blocks at a time to speed up
817 // convergence... this actually works by setting the terminator of the blocks
818 // to a return instruction then running simplifycfg, which can potentially
819 // shrinks the code dramatically quickly
821 if (!DisableSimplifyCFG && !BugpointIsInterrupted) {
822 std::vector<const BasicBlock*> Blocks;
823 for (Function &F : *BD.getProgram())
824 for (BasicBlock &BB : F)
825 Blocks.push_back(&BB);
826 unsigned OldSize = Blocks.size();
827 ReduceCrashingBlocks(BD, TestFn).reduceList(Blocks, Error);
828 if (Blocks.size() < OldSize)
829 BD.EmitProgressBitcode(BD.getProgram(), "reduced-blocks");
832 // Attempt to delete instructions using bisection. This should help out nasty
833 // cases with large basic blocks where the problem is at one end.
834 if (!BugpointIsInterrupted)
835 ReduceInsts(BD, TestFn, Error);
838 if (!BugpointIsInterrupted) {
839 // Try to reduce the amount of global metadata (particularly debug info),
840 // by dropping global named metadata that anchors them
841 outs() << "\n*** Attempting to remove named metadata: ";
842 std::vector<std::string> NamedMDNames;
843 for (auto &NamedMD : BD.getProgram()->named_metadata())
844 NamedMDNames.push_back(NamedMD.getName().str());
845 ReduceCrashingNamedMD(BD, TestFn).reduceList(NamedMDNames, Error);
848 if (!BugpointIsInterrupted) {
849 // Now that we quickly dropped all the named metadata that doesn't
850 // contribute to the crash, bisect the operands of the remaining ones
851 std::vector<const MDNode *> NamedMDOps;
852 for (auto &NamedMD : BD.getProgram()->named_metadata())
853 for (auto op : NamedMD.operands())
854 NamedMDOps.push_back(op);
855 ReduceCrashingNamedMDOps(BD, TestFn).reduceList(NamedMDOps, Error);
857 BD.EmitProgressBitcode(BD.getProgram(), "reduced-named-md");
860 // Try to clean up the testcase by running funcresolve and globaldce...
861 if (!BugpointIsInterrupted) {
862 outs() << "\n*** Attempting to perform final cleanups: ";
863 Module *M = CloneModule(BD.getProgram()).release();
864 M = BD.performFinalCleanups(M, true).release();
866 // Find out if the pass still crashes on the cleaned up program...
868 BD.setNewProgram(M); // Yup, it does, keep the reduced version...
874 BD.EmitProgressBitcode(BD.getProgram(), "reduced-simplified");
879 static bool TestForOptimizerCrash(const BugDriver &BD, Module *M) {
880 return BD.runPasses(M, BD.getPassesToRun());
883 /// debugOptimizerCrash - This method is called when some pass crashes on input.
884 /// It attempts to prune down the testcase to something reasonable, and figure
885 /// out exactly which pass is crashing.
887 bool BugDriver::debugOptimizerCrash(const std::string &ID) {
888 outs() << "\n*** Debugging optimizer crash!\n";
891 // Reduce the list of passes which causes the optimizer to crash...
892 if (!BugpointIsInterrupted && !DontReducePassList)
893 ReducePassList(*this).reduceList(PassesToRun, Error);
894 assert(Error.empty());
896 outs() << "\n*** Found crashing pass"
897 << (PassesToRun.size() == 1 ? ": " : "es: ")
898 << getPassesString(PassesToRun) << '\n';
900 EmitProgressBitcode(Program, ID);
902 bool Success = DebugACrash(*this, TestForOptimizerCrash, Error);
903 assert(Error.empty());
907 static bool TestForCodeGenCrash(const BugDriver &BD, Module *M) {
909 BD.compileProgram(M, &Error);
910 if (!Error.empty()) {
912 errs() << Error << "\n";
914 errs() << "<crash>\n";
915 return true; // Tool is still crashing.
921 /// debugCodeGeneratorCrash - This method is called when the code generator
922 /// crashes on an input. It attempts to reduce the input as much as possible
923 /// while still causing the code generator to crash.
924 bool BugDriver::debugCodeGeneratorCrash(std::string &Error) {
925 errs() << "*** Debugging code generator crash!\n";
927 return DebugACrash(*this, TestForCodeGenCrash, Error);