Chris Lattner | 73a6bdd | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 1 | //===- CrashDebugger.cpp - Debug compilation crashes ----------------------===// |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 09344dc | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 345353d | 2007-12-29 20:44:31 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 09344dc | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 73a6bdd | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file defines the bugpoint internals that narrow down compilation crashes |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "BugDriver.h" |
Chris Lattner | 1d080f2 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 15 | #include "ListReducer.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 16 | #include "ToolRunner.h" |
| 17 | #include "llvm/ADT/SmallPtrSet.h" |
Keno Fischer | 34ca831 | 2015-11-06 00:12:50 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/StringSet.h" |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/TargetTransformInfo.h" |
Chandler Carruth | 1305dc3 | 2014-03-04 11:45:46 +0000 | [diff] [blame] | 20 | #include "llvm/IR/CFG.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 21 | #include "llvm/IR/Constants.h" |
Michael Ilseman | e542804 | 2016-10-25 18:44:13 +0000 | [diff] [blame] | 22 | #include "llvm/IR/DebugInfo.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 23 | #include "llvm/IR/DerivedTypes.h" |
| 24 | #include "llvm/IR/Instructions.h" |
Chandler Carruth | 30d69c2 | 2015-02-13 10:01:29 +0000 | [diff] [blame] | 25 | #include "llvm/IR/LegacyPassManager.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 26 | #include "llvm/IR/Module.h" |
| 27 | #include "llvm/IR/ValueSymbolTable.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 28 | #include "llvm/IR/Verifier.h" |
Misha Brukman | 0c2305b | 2003-08-07 21:19:30 +0000 | [diff] [blame] | 29 | #include "llvm/Pass.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 30 | #include "llvm/Support/CommandLine.h" |
| 31 | #include "llvm/Support/FileUtilities.h" |
Chris Lattner | f32939b | 2003-04-24 23:51:38 +0000 | [diff] [blame] | 32 | #include "llvm/Transforms/Scalar.h" |
Daniel Berlin | 271ca40 | 2016-07-27 16:13:25 +0000 | [diff] [blame] | 33 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Chris Lattner | 1d080f2 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 34 | #include "llvm/Transforms/Utils/Cloning.h" |
Daniel Berlin | 271ca40 | 2016-07-27 16:13:25 +0000 | [diff] [blame] | 35 | #include "llvm/Transforms/Utils/Local.h" |
Chris Lattner | 1d080f2 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 36 | #include <set> |
Chris Lattner | 2f1aa11 | 2004-01-14 03:38:37 +0000 | [diff] [blame] | 37 | using namespace llvm; |
Chris Lattner | 73a6bdd | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 38 | |
Andrew Lenharth | ef9aa12 | 2006-03-05 22:21:36 +0000 | [diff] [blame] | 39 | namespace { |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 40 | cl::opt<bool> KeepMain("keep-main", |
| 41 | cl::desc("Force function reduction to keep main"), |
| 42 | cl::init(false)); |
| 43 | cl::opt<bool> NoGlobalRM("disable-global-remove", |
| 44 | cl::desc("Do not remove global variables"), |
| 45 | cl::init(false)); |
JF Bastien | f87e20d | 2015-04-20 23:42:22 +0000 | [diff] [blame] | 46 | |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 47 | cl::opt<bool> ReplaceFuncsWithNull( |
| 48 | "replace-funcs-with-null", |
| 49 | cl::desc("When stubbing functions, replace all uses will null"), |
| 50 | cl::init(false)); |
| 51 | cl::opt<bool> DontReducePassList("disable-pass-list-reduction", |
| 52 | cl::desc("Skip pass list reduction steps"), |
| 53 | cl::init(false)); |
Keno Fischer | 34ca831 | 2015-11-06 00:12:50 +0000 | [diff] [blame] | 54 | |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 55 | cl::opt<bool> NoNamedMDRM("disable-namedmd-remove", |
| 56 | cl::desc("Do not remove global named metadata"), |
| 57 | cl::init(false)); |
Michael Ilseman | e542804 | 2016-10-25 18:44:13 +0000 | [diff] [blame] | 58 | cl::opt<bool> NoStripDebugInfo("disable-strip-debuginfo", |
| 59 | cl::desc("Do not strip debug info metadata"), |
| 60 | cl::init(false)); |
| 61 | cl::opt<bool> NoStripDebugTypeInfo("disable-strip-debug-types", |
| 62 | cl::desc("Do not strip debug type info metadata"), |
| 63 | cl::init(false)); |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 64 | cl::opt<bool> VerboseErrors("verbose-errors", |
Sebastian Pop | 8f7d019 | 2016-07-15 23:15:06 +0000 | [diff] [blame] | 65 | cl::desc("Print the output of crashing program"), |
| 66 | cl::init(false)); |
Andrew Lenharth | ef9aa12 | 2006-03-05 22:21:36 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 69 | namespace llvm { |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 70 | class ReducePassList : public ListReducer<std::string> { |
| 71 | BugDriver &BD; |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 72 | |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 73 | public: |
| 74 | ReducePassList(BugDriver &bd) : BD(bd) {} |
| 75 | |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 76 | // Return true iff running the "removed" passes succeeds, and running the |
| 77 | // "Kept" passes fail when run on the output of the "removed" passes. If we |
| 78 | // return true, we update the current module of bugpoint. |
| 79 | Expected<TestResult> doTest(std::vector<std::string> &Removed, |
| 80 | std::vector<std::string> &Kept) override; |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 81 | }; |
Chris Lattner | 2f1aa11 | 2004-01-14 03:38:37 +0000 | [diff] [blame] | 82 | } |
Chris Lattner | 1d080f2 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 83 | |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 84 | Expected<ReducePassList::TestResult> |
Rafael Espindola | 33e81a8 | 2010-08-08 03:55:08 +0000 | [diff] [blame] | 85 | ReducePassList::doTest(std::vector<std::string> &Prefix, |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 86 | std::vector<std::string> &Suffix) { |
Rafael Espindola | bb87665 | 2013-06-17 19:33:18 +0000 | [diff] [blame] | 87 | std::string PrefixOutput; |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 88 | Module *OrigProgram = nullptr; |
Chris Lattner | 1d080f2 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 89 | if (!Prefix.empty()) { |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 90 | outs() << "Checking to see if these passes crash: " |
| 91 | << getPassesString(Prefix) << ": "; |
Rafael Espindola | bb87665 | 2013-06-17 19:33:18 +0000 | [diff] [blame] | 92 | if (BD.runPasses(BD.getProgram(), Prefix, PrefixOutput)) |
Chris Lattner | 1d080f2 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 93 | return KeepPrefix; |
Chris Lattner | a965631 | 2003-06-02 04:54:29 +0000 | [diff] [blame] | 94 | |
| 95 | OrigProgram = BD.Program; |
| 96 | |
Rafael Espindola | 28b351a | 2014-08-26 17:19:03 +0000 | [diff] [blame] | 97 | BD.Program = parseInputFile(PrefixOutput, BD.getContext()).release(); |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 98 | if (BD.Program == nullptr) { |
Dan Gohman | d8db376 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 99 | errs() << BD.getToolName() << ": Error reading bitcode file '" |
Rafael Espindola | bb87665 | 2013-06-17 19:33:18 +0000 | [diff] [blame] | 100 | << PrefixOutput << "'!\n"; |
Chris Lattner | a965631 | 2003-06-02 04:54:29 +0000 | [diff] [blame] | 101 | exit(1); |
| 102 | } |
Rafael Espindola | bb87665 | 2013-06-17 19:33:18 +0000 | [diff] [blame] | 103 | sys::fs::remove(PrefixOutput); |
Chris Lattner | 1d080f2 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 106 | outs() << "Checking to see if these passes crash: " << getPassesString(Suffix) |
| 107 | << ": "; |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 108 | |
Rafael Espindola | 37302ea | 2010-08-05 02:16:32 +0000 | [diff] [blame] | 109 | if (BD.runPasses(BD.getProgram(), Suffix)) { |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 110 | delete OrigProgram; // The suffix crashes alone... |
Chris Lattner | 1d080f2 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 111 | return KeepSuffix; |
| 112 | } |
| 113 | |
| 114 | // Nothing failed, restore state... |
Chris Lattner | a965631 | 2003-06-02 04:54:29 +0000 | [diff] [blame] | 115 | if (OrigProgram) { |
| 116 | delete BD.Program; |
| 117 | BD.Program = OrigProgram; |
| 118 | } |
Chris Lattner | 1d080f2 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 119 | return NoFailure; |
| 120 | } |
| 121 | |
Vedant Kumar | 08d829d | 2018-02-08 18:46:49 +0000 | [diff] [blame] | 122 | using BugTester = bool (*)(const BugDriver &, Module *); |
| 123 | |
Bill Wendling | 3f83343 | 2006-10-25 18:36:14 +0000 | [diff] [blame] | 124 | namespace { |
Vedant Kumar | 66e85e6 | 2018-02-08 20:27:09 +0000 | [diff] [blame] | 125 | /// ReduceCrashingGlobalInitializers - This works by removing global variable |
| 126 | /// initializers and seeing if the program still crashes. If it does, then we |
| 127 | /// keep that program and try again. |
| 128 | class ReduceCrashingGlobalInitializers : public ListReducer<GlobalVariable *> { |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 129 | BugDriver &BD; |
Vedant Kumar | 08d829d | 2018-02-08 18:46:49 +0000 | [diff] [blame] | 130 | BugTester TestFn; |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 131 | |
| 132 | public: |
Vedant Kumar | 66e85e6 | 2018-02-08 20:27:09 +0000 | [diff] [blame] | 133 | ReduceCrashingGlobalInitializers(BugDriver &bd, BugTester testFn) |
Bill Wendling | 3f83343 | 2006-10-25 18:36:14 +0000 | [diff] [blame] | 134 | : BD(bd), TestFn(testFn) {} |
| 135 | |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 136 | Expected<TestResult> doTest(std::vector<GlobalVariable *> &Prefix, |
| 137 | std::vector<GlobalVariable *> &Kept) override { |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 138 | if (!Kept.empty() && TestGlobalVariables(Kept)) |
| 139 | return KeepSuffix; |
| 140 | if (!Prefix.empty() && TestGlobalVariables(Prefix)) |
| 141 | return KeepPrefix; |
| 142 | return NoFailure; |
| 143 | } |
Bill Wendling | 3f83343 | 2006-10-25 18:36:14 +0000 | [diff] [blame] | 144 | |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 145 | bool TestGlobalVariables(std::vector<GlobalVariable *> &GVs); |
| 146 | }; |
Bill Wendling | 3f83343 | 2006-10-25 18:36:14 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Vedant Kumar | 66e85e6 | 2018-02-08 20:27:09 +0000 | [diff] [blame] | 149 | bool ReduceCrashingGlobalInitializers::TestGlobalVariables( |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 150 | std::vector<GlobalVariable *> &GVs) { |
Bill Wendling | 3f83343 | 2006-10-25 18:36:14 +0000 | [diff] [blame] | 151 | // Clone the program to try hacking it apart... |
Rafael Espindola | 229e38f | 2010-10-13 01:36:30 +0000 | [diff] [blame] | 152 | ValueToValueMapTy VMap; |
Rafael Espindola | 7186753 | 2018-02-14 19:50:40 +0000 | [diff] [blame^] | 153 | std::unique_ptr<Module> M = CloneModule(*BD.getProgram(), VMap); |
Bill Wendling | 3f83343 | 2006-10-25 18:36:14 +0000 | [diff] [blame] | 154 | |
| 155 | // Convert list to set for fast lookup... |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 156 | std::set<GlobalVariable *> GVSet; |
Bill Wendling | 3f83343 | 2006-10-25 18:36:14 +0000 | [diff] [blame] | 157 | |
| 158 | for (unsigned i = 0, e = GVs.size(); i != e; ++i) { |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 159 | GlobalVariable *CMGV = cast<GlobalVariable>(VMap[GVs[i]]); |
Bill Wendling | 3f83343 | 2006-10-25 18:36:14 +0000 | [diff] [blame] | 160 | assert(CMGV && "Global Variable not in module?!"); |
| 161 | GVSet.insert(CMGV); |
| 162 | } |
| 163 | |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 164 | outs() << "Checking for crash with only these global variables: "; |
Bill Wendling | 3f83343 | 2006-10-25 18:36:14 +0000 | [diff] [blame] | 165 | PrintGlobalVariableList(GVs); |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 166 | outs() << ": "; |
Bill Wendling | 3f83343 | 2006-10-25 18:36:14 +0000 | [diff] [blame] | 167 | |
| 168 | // Loop over and delete any global variables which we aren't supposed to be |
| 169 | // playing with... |
Duncan P. N. Exon Smith | 306d16e | 2015-10-20 19:36:39 +0000 | [diff] [blame] | 170 | for (GlobalVariable &I : M->globals()) |
| 171 | if (I.hasInitializer() && !GVSet.count(&I)) { |
Hal Finkel | 28ad2b4 | 2015-11-26 19:23:49 +0000 | [diff] [blame] | 172 | DeleteGlobalInitializer(&I); |
Duncan P. N. Exon Smith | 306d16e | 2015-10-20 19:36:39 +0000 | [diff] [blame] | 173 | I.setLinkage(GlobalValue::ExternalLinkage); |
Justin Lebar | 2a445cf | 2016-06-15 23:20:12 +0000 | [diff] [blame] | 174 | I.setComdat(nullptr); |
Bill Wendling | 3f83343 | 2006-10-25 18:36:14 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | // Try running the hacked up program... |
Vedant Kumar | 66e85e6 | 2018-02-08 20:27:09 +0000 | [diff] [blame] | 178 | if (TestFn(BD, M.get())) { |
| 179 | BD.setNewProgram(M.release()); // It crashed, keep the trimmed version... |
Bill Wendling | 3f83343 | 2006-10-25 18:36:14 +0000 | [diff] [blame] | 180 | |
| 181 | // Make sure to use global variable pointers that point into the now-current |
| 182 | // module. |
| 183 | GVs.assign(GVSet.begin(), GVSet.end()); |
| 184 | return true; |
| 185 | } |
| 186 | |
Bill Wendling | 3f83343 | 2006-10-25 18:36:14 +0000 | [diff] [blame] | 187 | return false; |
| 188 | } |
| 189 | |
David Blaikie | a379b181 | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 190 | namespace { |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 191 | /// ReduceCrashingFunctions reducer - This works by removing functions and |
| 192 | /// seeing if the program still crashes. If it does, then keep the newer, |
| 193 | /// smaller program. |
| 194 | /// |
| 195 | class ReduceCrashingFunctions : public ListReducer<Function *> { |
| 196 | BugDriver &BD; |
Vedant Kumar | 08d829d | 2018-02-08 18:46:49 +0000 | [diff] [blame] | 197 | BugTester TestFn; |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 198 | |
| 199 | public: |
Vedant Kumar | 08d829d | 2018-02-08 18:46:49 +0000 | [diff] [blame] | 200 | ReduceCrashingFunctions(BugDriver &bd, BugTester testFn) |
Chris Lattner | 8bda4c4 | 2004-02-18 23:26:28 +0000 | [diff] [blame] | 201 | : BD(bd), TestFn(testFn) {} |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 202 | |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 203 | Expected<TestResult> doTest(std::vector<Function *> &Prefix, |
| 204 | std::vector<Function *> &Kept) override { |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 205 | if (!Kept.empty() && TestFuncs(Kept)) |
| 206 | return KeepSuffix; |
| 207 | if (!Prefix.empty() && TestFuncs(Prefix)) |
| 208 | return KeepPrefix; |
| 209 | return NoFailure; |
| 210 | } |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 211 | |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 212 | bool TestFuncs(std::vector<Function *> &Prefix); |
| 213 | }; |
Chris Lattner | 2f1aa11 | 2004-01-14 03:38:37 +0000 | [diff] [blame] | 214 | } |
Chris Lattner | 1d080f2 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 215 | |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 216 | static void RemoveFunctionReferences(Module *M, const char *Name) { |
JF Bastien | f87e20d | 2015-04-20 23:42:22 +0000 | [diff] [blame] | 217 | auto *UsedVar = M->getGlobalVariable(Name, true); |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 218 | if (!UsedVar || !UsedVar->hasInitializer()) |
| 219 | return; |
JF Bastien | f87e20d | 2015-04-20 23:42:22 +0000 | [diff] [blame] | 220 | if (isa<ConstantAggregateZero>(UsedVar->getInitializer())) { |
| 221 | assert(UsedVar->use_empty()); |
| 222 | UsedVar->eraseFromParent(); |
| 223 | return; |
| 224 | } |
| 225 | auto *OldUsedVal = cast<ConstantArray>(UsedVar->getInitializer()); |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 226 | std::vector<Constant *> Used; |
| 227 | for (Value *V : OldUsedVal->operand_values()) { |
JF Bastien | f87e20d | 2015-04-20 23:42:22 +0000 | [diff] [blame] | 228 | Constant *Op = cast<Constant>(V->stripPointerCasts()); |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 229 | if (!Op->isNullValue()) { |
JF Bastien | f87e20d | 2015-04-20 23:42:22 +0000 | [diff] [blame] | 230 | Used.push_back(cast<Constant>(V)); |
| 231 | } |
| 232 | } |
| 233 | auto *NewValElemTy = OldUsedVal->getType()->getElementType(); |
| 234 | auto *NewValTy = ArrayType::get(NewValElemTy, Used.size()); |
| 235 | auto *NewUsedVal = ConstantArray::get(NewValTy, Used); |
| 236 | UsedVar->mutateType(NewUsedVal->getType()->getPointerTo()); |
| 237 | UsedVar->setInitializer(NewUsedVal); |
| 238 | } |
| 239 | |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 240 | bool ReduceCrashingFunctions::TestFuncs(std::vector<Function *> &Funcs) { |
Dmitri Gribenko | 6e0520e | 2013-09-02 01:18:56 +0000 | [diff] [blame] | 241 | // If main isn't present, claim there is no problem. |
David Majnemer | 0d955d0 | 2016-08-11 22:21:41 +0000 | [diff] [blame] | 242 | if (KeepMain && !is_contained(Funcs, BD.getProgram()->getFunction("main"))) |
Andrew Lenharth | ef9aa12 | 2006-03-05 22:21:36 +0000 | [diff] [blame] | 243 | return false; |
| 244 | |
Misha Brukman | 8b2bd4e | 2003-10-10 17:57:28 +0000 | [diff] [blame] | 245 | // Clone the program to try hacking it apart... |
Rafael Espindola | 229e38f | 2010-10-13 01:36:30 +0000 | [diff] [blame] | 246 | ValueToValueMapTy VMap; |
Rafael Espindola | 7186753 | 2018-02-14 19:50:40 +0000 | [diff] [blame^] | 247 | Module *M = CloneModule(*BD.getProgram(), VMap).release(); |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 248 | |
Chris Lattner | 1d080f2 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 249 | // Convert list to set for fast lookup... |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 250 | std::set<Function *> Functions; |
Chris Lattner | 1d080f2 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 251 | for (unsigned i = 0, e = Funcs.size(); i != e; ++i) { |
Devang Patel | 0dc3c2d | 2010-06-24 00:33:28 +0000 | [diff] [blame] | 252 | Function *CMF = cast<Function>(VMap[Funcs[i]]); |
Chris Lattner | 1d080f2 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 253 | assert(CMF && "Function not in module?!"); |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 254 | assert(CMF->getFunctionType() == Funcs[i]->getFunctionType() && "wrong ty"); |
Dan Gohman | bcad718 | 2009-03-06 02:16:23 +0000 | [diff] [blame] | 255 | assert(CMF->getName() == Funcs[i]->getName() && "wrong name"); |
Chris Lattner | de39f2b | 2003-04-24 22:54:06 +0000 | [diff] [blame] | 256 | Functions.insert(CMF); |
Chris Lattner | 1d080f2 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 259 | outs() << "Checking for crash with only these functions: "; |
Chris Lattner | fd72bed | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 260 | PrintFunctionList(Funcs); |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 261 | outs() << ": "; |
JF Bastien | f87e20d | 2015-04-20 23:42:22 +0000 | [diff] [blame] | 262 | if (!ReplaceFuncsWithNull) { |
| 263 | // Loop over and delete any functions which we aren't supposed to be playing |
| 264 | // with... |
Duncan P. N. Exon Smith | 306d16e | 2015-10-20 19:36:39 +0000 | [diff] [blame] | 265 | for (Function &I : *M) |
| 266 | if (!I.isDeclaration() && !Functions.count(&I)) |
| 267 | DeleteFunctionBody(&I); |
JF Bastien | f87e20d | 2015-04-20 23:42:22 +0000 | [diff] [blame] | 268 | } else { |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 269 | std::vector<GlobalValue *> ToRemove; |
JF Bastien | f87e20d | 2015-04-20 23:42:22 +0000 | [diff] [blame] | 270 | // First, remove aliases to functions we're about to purge. |
| 271 | for (GlobalAlias &Alias : M->aliases()) { |
David Majnemer | 9554949 | 2016-05-04 00:20:48 +0000 | [diff] [blame] | 272 | GlobalObject *Root = Alias.getBaseObject(); |
| 273 | Function *F = dyn_cast_or_null<Function>(Root); |
JF Bastien | f87e20d | 2015-04-20 23:42:22 +0000 | [diff] [blame] | 274 | if (F) { |
| 275 | if (Functions.count(F)) |
| 276 | // We're keeping this function. |
| 277 | continue; |
| 278 | } else if (Root->isNullValue()) { |
| 279 | // This referenced a globalalias that we've already replaced, |
| 280 | // so we still need to replace this alias. |
| 281 | } else if (!F) { |
| 282 | // Not a function, therefore not something we mess with. |
| 283 | continue; |
| 284 | } |
Chris Lattner | 1d080f2 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 285 | |
JF Bastien | f87e20d | 2015-04-20 23:42:22 +0000 | [diff] [blame] | 286 | PointerType *Ty = cast<PointerType>(Alias.getType()); |
| 287 | Constant *Replacement = ConstantPointerNull::get(Ty); |
| 288 | Alias.replaceAllUsesWith(Replacement); |
| 289 | ToRemove.push_back(&Alias); |
| 290 | } |
Chris Lattner | 1d080f2 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 291 | |
Duncan P. N. Exon Smith | 306d16e | 2015-10-20 19:36:39 +0000 | [diff] [blame] | 292 | for (Function &I : *M) { |
| 293 | if (!I.isDeclaration() && !Functions.count(&I)) { |
| 294 | PointerType *Ty = cast<PointerType>(I.getType()); |
JF Bastien | f87e20d | 2015-04-20 23:42:22 +0000 | [diff] [blame] | 295 | Constant *Replacement = ConstantPointerNull::get(Ty); |
Duncan P. N. Exon Smith | 306d16e | 2015-10-20 19:36:39 +0000 | [diff] [blame] | 296 | I.replaceAllUsesWith(Replacement); |
| 297 | ToRemove.push_back(&I); |
JF Bastien | f87e20d | 2015-04-20 23:42:22 +0000 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | |
| 301 | for (auto *F : ToRemove) { |
| 302 | F->eraseFromParent(); |
| 303 | } |
| 304 | |
| 305 | // Finally, remove any null members from any global intrinsic. |
| 306 | RemoveFunctionReferences(M, "llvm.used"); |
| 307 | RemoveFunctionReferences(M, "llvm.compiler.used"); |
| 308 | } |
Chris Lattner | 1d080f2 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 309 | // Try running the hacked up program... |
Chris Lattner | 8bda4c4 | 2004-02-18 23:26:28 +0000 | [diff] [blame] | 310 | if (TestFn(BD, M)) { |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 311 | BD.setNewProgram(M); // It crashed, keep the trimmed version... |
Chris Lattner | 1d080f2 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 312 | |
| 313 | // Make sure to use function pointers that point into the now-current |
| 314 | // module. |
| 315 | Funcs.assign(Functions.begin(), Functions.end()); |
| 316 | return true; |
| 317 | } |
Chris Lattner | 327019b | 2004-02-18 21:24:48 +0000 | [diff] [blame] | 318 | delete M; |
Chris Lattner | 1d080f2 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 319 | return false; |
| 320 | } |
| 321 | |
Chris Lattner | 1942f98 | 2004-02-18 21:29:46 +0000 | [diff] [blame] | 322 | namespace { |
Daniel Berlin | 6060626 | 2016-07-28 22:29:25 +0000 | [diff] [blame] | 323 | /// Simplify the CFG without completely destroying it. |
| 324 | /// This is not well defined, but basically comes down to "try to eliminate |
| 325 | /// unreachable blocks and constant fold terminators without deciding that |
| 326 | /// certain undefined behavior cuts off the program at the legs". |
| 327 | void simpleSimplifyCfg(Function &F, SmallVectorImpl<BasicBlock *> &BBs) { |
| 328 | if (F.empty()) |
| 329 | return; |
| 330 | |
| 331 | for (auto *BB : BBs) { |
| 332 | ConstantFoldTerminator(BB); |
| 333 | MergeBlockIntoPredecessor(BB); |
| 334 | } |
| 335 | |
| 336 | // Remove unreachable blocks |
| 337 | // removeUnreachableBlocks can't be used here, it will turn various |
| 338 | // undefined behavior into unreachables, but bugpoint was the thing that |
| 339 | // generated the undefined behavior, and we don't want it to kill the entire |
| 340 | // program. |
| 341 | SmallPtrSet<BasicBlock *, 16> Visited; |
| 342 | for (auto *BB : depth_first(&F.getEntryBlock())) |
| 343 | Visited.insert(BB); |
| 344 | |
| 345 | SmallVector<BasicBlock *, 16> Unreachable; |
| 346 | for (auto &BB : F) |
| 347 | if (!Visited.count(&BB)) |
| 348 | Unreachable.push_back(&BB); |
| 349 | |
| 350 | // The dead BB's may be in a dead cycle or otherwise have references to each |
| 351 | // other. Because of this, we have to drop all references first, then delete |
| 352 | // them all at once. |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 353 | for (auto *BB : Unreachable) { |
Daniel Berlin | 6060626 | 2016-07-28 22:29:25 +0000 | [diff] [blame] | 354 | for (BasicBlock *Successor : successors(&*BB)) |
| 355 | if (Visited.count(Successor)) |
| 356 | Successor->removePredecessor(&*BB); |
| 357 | BB->dropAllReferences(); |
| 358 | } |
| 359 | for (auto *BB : Unreachable) |
| 360 | BB->eraseFromParent(); |
| 361 | } |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 362 | /// ReduceCrashingBlocks reducer - This works by setting the terminators of |
| 363 | /// all terminators except the specified basic blocks to a 'ret' instruction, |
| 364 | /// then running the simplify-cfg pass. This has the effect of chopping up |
| 365 | /// the CFG really fast which can reduce large functions quickly. |
| 366 | /// |
| 367 | class ReduceCrashingBlocks : public ListReducer<const BasicBlock *> { |
| 368 | BugDriver &BD; |
Vedant Kumar | 08d829d | 2018-02-08 18:46:49 +0000 | [diff] [blame] | 369 | BugTester TestFn; |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 370 | |
| 371 | public: |
Vedant Kumar | 08d829d | 2018-02-08 18:46:49 +0000 | [diff] [blame] | 372 | ReduceCrashingBlocks(BugDriver &BD, BugTester testFn) |
Daniel Berlin | 6060626 | 2016-07-28 22:29:25 +0000 | [diff] [blame] | 373 | : BD(BD), TestFn(testFn) {} |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 374 | |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 375 | Expected<TestResult> doTest(std::vector<const BasicBlock *> &Prefix, |
| 376 | std::vector<const BasicBlock *> &Kept) override { |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 377 | if (!Kept.empty() && TestBlocks(Kept)) |
| 378 | return KeepSuffix; |
| 379 | if (!Prefix.empty() && TestBlocks(Prefix)) |
| 380 | return KeepPrefix; |
| 381 | return NoFailure; |
| 382 | } |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 383 | |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 384 | bool TestBlocks(std::vector<const BasicBlock *> &Prefix); |
| 385 | }; |
Chris Lattner | 2f1aa11 | 2004-01-14 03:38:37 +0000 | [diff] [blame] | 386 | } |
Chris Lattner | f32939b | 2003-04-24 23:51:38 +0000 | [diff] [blame] | 387 | |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 388 | bool ReduceCrashingBlocks::TestBlocks(std::vector<const BasicBlock *> &BBs) { |
Misha Brukman | 8b2bd4e | 2003-10-10 17:57:28 +0000 | [diff] [blame] | 389 | // Clone the program to try hacking it apart... |
Rafael Espindola | 229e38f | 2010-10-13 01:36:30 +0000 | [diff] [blame] | 390 | ValueToValueMapTy VMap; |
Rafael Espindola | 7186753 | 2018-02-14 19:50:40 +0000 | [diff] [blame^] | 391 | std::unique_ptr<Module> M = CloneModule(*BD.getProgram(), VMap); |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 392 | |
Chris Lattner | f32939b | 2003-04-24 23:51:38 +0000 | [diff] [blame] | 393 | // Convert list to set for fast lookup... |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 394 | SmallPtrSet<BasicBlock *, 8> Blocks; |
Nick Lewycky | b294e31 | 2009-04-04 09:39:23 +0000 | [diff] [blame] | 395 | for (unsigned i = 0, e = BBs.size(); i != e; ++i) |
Devang Patel | 0dc3c2d | 2010-06-24 00:33:28 +0000 | [diff] [blame] | 396 | Blocks.insert(cast<BasicBlock>(VMap[BBs[i]])); |
Chris Lattner | f32939b | 2003-04-24 23:51:38 +0000 | [diff] [blame] | 397 | |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 398 | outs() << "Checking for crash with only these blocks:"; |
Chris Lattner | 4f50ebd | 2003-10-27 04:44:59 +0000 | [diff] [blame] | 399 | unsigned NumPrint = Blocks.size(); |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 400 | if (NumPrint > 10) |
| 401 | NumPrint = 10; |
Chris Lattner | 4f50ebd | 2003-10-27 04:44:59 +0000 | [diff] [blame] | 402 | for (unsigned i = 0, e = NumPrint; i != e; ++i) |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 403 | outs() << " " << BBs[i]->getName(); |
Chris Lattner | 4f50ebd | 2003-10-27 04:44:59 +0000 | [diff] [blame] | 404 | if (NumPrint < Blocks.size()) |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 405 | outs() << "... <" << Blocks.size() << " total>"; |
| 406 | outs() << ": "; |
Chris Lattner | f32939b | 2003-04-24 23:51:38 +0000 | [diff] [blame] | 407 | |
| 408 | // Loop over and delete any hack up any blocks that are not listed... |
Vedant Kumar | e84e767 | 2018-02-09 05:09:50 +0000 | [diff] [blame] | 409 | for (Function &F : M->functions()) { |
| 410 | for (BasicBlock &BB : F) { |
| 411 | if (!Blocks.count(&BB) && BB.getTerminator()->getNumSuccessors()) { |
Chris Lattner | f32939b | 2003-04-24 23:51:38 +0000 | [diff] [blame] | 412 | // Loop over all of the successors of this block, deleting any PHI nodes |
| 413 | // that might include it. |
Vedant Kumar | e84e767 | 2018-02-09 05:09:50 +0000 | [diff] [blame] | 414 | for (BasicBlock *Succ : successors(&BB)) |
| 415 | Succ->removePredecessor(&BB); |
Chris Lattner | f32939b | 2003-04-24 23:51:38 +0000 | [diff] [blame] | 416 | |
Vedant Kumar | e84e767 | 2018-02-09 05:09:50 +0000 | [diff] [blame] | 417 | TerminatorInst *BBTerm = BB.getTerminator(); |
Philip Reames | 29e641c | 2016-06-29 00:15:35 +0000 | [diff] [blame] | 418 | if (BBTerm->isEHPad() || BBTerm->getType()->isTokenTy()) |
David Majnemer | 189d7da | 2015-11-08 04:16:12 +0000 | [diff] [blame] | 419 | continue; |
Philip Reames | 29e641c | 2016-06-29 00:15:35 +0000 | [diff] [blame] | 420 | if (!BBTerm->getType()->isVoidTy()) |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 421 | BBTerm->replaceAllUsesWith(Constant::getNullValue(BBTerm->getType())); |
Chris Lattner | 7b233af | 2003-11-22 02:10:38 +0000 | [diff] [blame] | 422 | |
Chris Lattner | 7855a5c | 2008-04-28 00:04:58 +0000 | [diff] [blame] | 423 | // Replace the old terminator instruction. |
Vedant Kumar | e84e767 | 2018-02-09 05:09:50 +0000 | [diff] [blame] | 424 | BB.getInstList().pop_back(); |
| 425 | new UnreachableInst(BB.getContext(), &BB); |
Chris Lattner | f32939b | 2003-04-24 23:51:38 +0000 | [diff] [blame] | 426 | } |
Vedant Kumar | e84e767 | 2018-02-09 05:09:50 +0000 | [diff] [blame] | 427 | } |
| 428 | } |
Chris Lattner | f32939b | 2003-04-24 23:51:38 +0000 | [diff] [blame] | 429 | |
| 430 | // The CFG Simplifier pass may delete one of the basic blocks we are |
| 431 | // interested in. If it does we need to take the block out of the list. Make |
| 432 | // a "persistent mapping" by turning basic blocks into <function, name> pairs. |
| 433 | // This won't work well if blocks are unnamed, but that is just the risk we |
Vedant Kumar | e84e767 | 2018-02-09 05:09:50 +0000 | [diff] [blame] | 434 | // have to take. FIXME: Can we just name the blocks? |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 435 | std::vector<std::pair<std::string, std::string>> BlockInfo; |
Chris Lattner | f32939b | 2003-04-24 23:51:38 +0000 | [diff] [blame] | 436 | |
Craig Topper | 4627679 | 2014-08-24 23:23:06 +0000 | [diff] [blame] | 437 | for (BasicBlock *BB : Blocks) |
Benjamin Kramer | f5e2fc4 | 2015-05-29 19:43:39 +0000 | [diff] [blame] | 438 | BlockInfo.emplace_back(BB->getParent()->getName(), BB->getName()); |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 439 | |
Daniel Berlin | 6060626 | 2016-07-28 22:29:25 +0000 | [diff] [blame] | 440 | SmallVector<BasicBlock *, 16> ToProcess; |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 441 | for (auto &F : *M) { |
Daniel Berlin | 6060626 | 2016-07-28 22:29:25 +0000 | [diff] [blame] | 442 | for (auto &BB : F) |
| 443 | if (!Blocks.count(&BB)) |
| 444 | ToProcess.push_back(&BB); |
| 445 | simpleSimplifyCfg(F, ToProcess); |
| 446 | ToProcess.clear(); |
| 447 | } |
| 448 | // Verify we didn't break anything |
Rafael Espindola | d1e241a | 2010-08-10 15:46:11 +0000 | [diff] [blame] | 449 | std::vector<std::string> Passes; |
Rafael Espindola | d1e241a | 2010-08-10 15:46:11 +0000 | [diff] [blame] | 450 | Passes.push_back("verify"); |
Vedant Kumar | e84e767 | 2018-02-09 05:09:50 +0000 | [diff] [blame] | 451 | std::unique_ptr<Module> New = BD.runPassesOn(M.get(), Passes); |
Rafael Espindola | d1e241a | 2010-08-10 15:46:11 +0000 | [diff] [blame] | 452 | if (!New) { |
Daniel Berlin | 6060626 | 2016-07-28 22:29:25 +0000 | [diff] [blame] | 453 | errs() << "verify failed!\n"; |
Rafael Espindola | d1e241a | 2010-08-10 15:46:11 +0000 | [diff] [blame] | 454 | exit(1); |
| 455 | } |
Vedant Kumar | e84e767 | 2018-02-09 05:09:50 +0000 | [diff] [blame] | 456 | M = std::move(New); |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 457 | |
Chris Lattner | f32939b | 2003-04-24 23:51:38 +0000 | [diff] [blame] | 458 | // Try running on the hacked up program... |
Vedant Kumar | e84e767 | 2018-02-09 05:09:50 +0000 | [diff] [blame] | 459 | if (TestFn(BD, M.get())) { |
| 460 | BD.setNewProgram(M.release()); // It crashed, keep the trimmed version... |
Chris Lattner | f32939b | 2003-04-24 23:51:38 +0000 | [diff] [blame] | 461 | |
| 462 | // Make sure to use basic block pointers that point into the now-current |
| 463 | // module, and that they don't include any deleted blocks. |
| 464 | BBs.clear(); |
Vedant Kumar | e84e767 | 2018-02-09 05:09:50 +0000 | [diff] [blame] | 465 | const ValueSymbolTable &GST = BD.getProgram()->getValueSymbolTable(); |
| 466 | for (const auto &BI : BlockInfo) { |
| 467 | Function *F = cast<Function>(GST.lookup(BI.first)); |
| 468 | Value *V = F->getValueSymbolTable()->lookup(BI.second); |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 469 | if (V && V->getType() == Type::getLabelTy(V->getContext())) |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 470 | BBs.push_back(cast<BasicBlock>(V)); |
Chris Lattner | f32939b | 2003-04-24 23:51:38 +0000 | [diff] [blame] | 471 | } |
| 472 | return true; |
| 473 | } |
Vedant Kumar | e84e767 | 2018-02-09 05:09:50 +0000 | [diff] [blame] | 474 | // It didn't crash, try something else. |
Chris Lattner | f32939b | 2003-04-24 23:51:38 +0000 | [diff] [blame] | 475 | return false; |
| 476 | } |
| 477 | |
Nick Lewycky | 6e090c9 | 2009-05-25 05:30:00 +0000 | [diff] [blame] | 478 | namespace { |
Daniel Berlin | 271ca40 | 2016-07-27 16:13:25 +0000 | [diff] [blame] | 479 | /// ReduceCrashingConditionals reducer - This works by changing |
| 480 | /// conditional branches to unconditional ones, then simplifying the CFG |
| 481 | /// This has the effect of chopping up the CFG really fast which can reduce |
| 482 | /// large functions quickly. |
| 483 | /// |
| 484 | class ReduceCrashingConditionals : public ListReducer<const BasicBlock *> { |
| 485 | BugDriver &BD; |
Vedant Kumar | 08d829d | 2018-02-08 18:46:49 +0000 | [diff] [blame] | 486 | BugTester TestFn; |
Daniel Berlin | 271ca40 | 2016-07-27 16:13:25 +0000 | [diff] [blame] | 487 | bool Direction; |
| 488 | |
| 489 | public: |
Vedant Kumar | 08d829d | 2018-02-08 18:46:49 +0000 | [diff] [blame] | 490 | ReduceCrashingConditionals(BugDriver &bd, BugTester testFn, bool Direction) |
Daniel Berlin | 271ca40 | 2016-07-27 16:13:25 +0000 | [diff] [blame] | 491 | : BD(bd), TestFn(testFn), Direction(Direction) {} |
| 492 | |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 493 | Expected<TestResult> doTest(std::vector<const BasicBlock *> &Prefix, |
| 494 | std::vector<const BasicBlock *> &Kept) override { |
Daniel Berlin | 271ca40 | 2016-07-27 16:13:25 +0000 | [diff] [blame] | 495 | if (!Kept.empty() && TestBlocks(Kept)) |
| 496 | return KeepSuffix; |
| 497 | if (!Prefix.empty() && TestBlocks(Prefix)) |
| 498 | return KeepPrefix; |
| 499 | return NoFailure; |
| 500 | } |
| 501 | |
| 502 | bool TestBlocks(std::vector<const BasicBlock *> &Prefix); |
| 503 | }; |
| 504 | } |
| 505 | |
| 506 | bool ReduceCrashingConditionals::TestBlocks( |
| 507 | std::vector<const BasicBlock *> &BBs) { |
| 508 | // Clone the program to try hacking it apart... |
| 509 | ValueToValueMapTy VMap; |
Rafael Espindola | 7186753 | 2018-02-14 19:50:40 +0000 | [diff] [blame^] | 510 | std::unique_ptr<Module> M = CloneModule(*BD.getProgram(), VMap); |
Daniel Berlin | 271ca40 | 2016-07-27 16:13:25 +0000 | [diff] [blame] | 511 | |
| 512 | // Convert list to set for fast lookup... |
| 513 | SmallPtrSet<const BasicBlock *, 8> Blocks; |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 514 | for (const auto *BB : BBs) |
Daniel Berlin | 271ca40 | 2016-07-27 16:13:25 +0000 | [diff] [blame] | 515 | Blocks.insert(cast<BasicBlock>(VMap[BB])); |
| 516 | |
| 517 | outs() << "Checking for crash with changing conditionals to always jump to " |
| 518 | << (Direction ? "true" : "false") << ":"; |
| 519 | unsigned NumPrint = Blocks.size(); |
| 520 | if (NumPrint > 10) |
| 521 | NumPrint = 10; |
| 522 | for (unsigned i = 0, e = NumPrint; i != e; ++i) |
| 523 | outs() << " " << BBs[i]->getName(); |
| 524 | if (NumPrint < Blocks.size()) |
| 525 | outs() << "... <" << Blocks.size() << " total>"; |
| 526 | outs() << ": "; |
| 527 | |
| 528 | // Loop over and delete any hack up any blocks that are not listed... |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 529 | for (auto &F : *M) |
Daniel Berlin | 271ca40 | 2016-07-27 16:13:25 +0000 | [diff] [blame] | 530 | for (auto &BB : F) |
| 531 | if (!Blocks.count(&BB)) { |
| 532 | auto *BR = dyn_cast<BranchInst>(BB.getTerminator()); |
| 533 | if (!BR || !BR->isConditional()) |
| 534 | continue; |
| 535 | if (Direction) |
| 536 | BR->setCondition(ConstantInt::getTrue(BR->getContext())); |
| 537 | else |
| 538 | BR->setCondition(ConstantInt::getFalse(BR->getContext())); |
| 539 | } |
| 540 | |
| 541 | // The following may destroy some blocks, so we save them first |
| 542 | std::vector<std::pair<std::string, std::string>> BlockInfo; |
| 543 | |
| 544 | for (const BasicBlock *BB : Blocks) |
| 545 | BlockInfo.emplace_back(BB->getParent()->getName(), BB->getName()); |
| 546 | |
| 547 | SmallVector<BasicBlock *, 16> ToProcess; |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 548 | for (auto &F : *M) { |
Daniel Berlin | 271ca40 | 2016-07-27 16:13:25 +0000 | [diff] [blame] | 549 | for (auto &BB : F) |
| 550 | if (!Blocks.count(&BB)) |
| 551 | ToProcess.push_back(&BB); |
| 552 | simpleSimplifyCfg(F, ToProcess); |
| 553 | ToProcess.clear(); |
| 554 | } |
| 555 | // Verify we didn't break anything |
| 556 | std::vector<std::string> Passes; |
| 557 | Passes.push_back("verify"); |
Vedant Kumar | e84e767 | 2018-02-09 05:09:50 +0000 | [diff] [blame] | 558 | std::unique_ptr<Module> New = BD.runPassesOn(M.get(), Passes); |
Daniel Berlin | 271ca40 | 2016-07-27 16:13:25 +0000 | [diff] [blame] | 559 | if (!New) { |
| 560 | errs() << "verify failed!\n"; |
| 561 | exit(1); |
| 562 | } |
Vedant Kumar | e84e767 | 2018-02-09 05:09:50 +0000 | [diff] [blame] | 563 | M = std::move(New); |
Daniel Berlin | 271ca40 | 2016-07-27 16:13:25 +0000 | [diff] [blame] | 564 | |
| 565 | // Try running on the hacked up program... |
Vedant Kumar | e84e767 | 2018-02-09 05:09:50 +0000 | [diff] [blame] | 566 | if (TestFn(BD, M.get())) { |
| 567 | BD.setNewProgram(M.release()); // It crashed, keep the trimmed version... |
Daniel Berlin | 271ca40 | 2016-07-27 16:13:25 +0000 | [diff] [blame] | 568 | |
| 569 | // Make sure to use basic block pointers that point into the now-current |
| 570 | // module, and that they don't include any deleted blocks. |
| 571 | BBs.clear(); |
Vedant Kumar | e84e767 | 2018-02-09 05:09:50 +0000 | [diff] [blame] | 572 | const ValueSymbolTable &GST = BD.getProgram()->getValueSymbolTable(); |
Daniel Berlin | 271ca40 | 2016-07-27 16:13:25 +0000 | [diff] [blame] | 573 | for (auto &BI : BlockInfo) { |
| 574 | auto *F = cast<Function>(GST.lookup(BI.first)); |
Mehdi Amini | a53d49e | 2016-09-17 06:00:02 +0000 | [diff] [blame] | 575 | Value *V = F->getValueSymbolTable()->lookup(BI.second); |
Daniel Berlin | 271ca40 | 2016-07-27 16:13:25 +0000 | [diff] [blame] | 576 | if (V && V->getType() == Type::getLabelTy(V->getContext())) |
| 577 | BBs.push_back(cast<BasicBlock>(V)); |
| 578 | } |
| 579 | return true; |
| 580 | } |
Vedant Kumar | e84e767 | 2018-02-09 05:09:50 +0000 | [diff] [blame] | 581 | // It didn't crash, try something else. |
Daniel Berlin | 271ca40 | 2016-07-27 16:13:25 +0000 | [diff] [blame] | 582 | return false; |
| 583 | } |
| 584 | |
| 585 | namespace { |
Daniel Berlin | 6060626 | 2016-07-28 22:29:25 +0000 | [diff] [blame] | 586 | /// SimplifyCFG reducer - This works by calling SimplifyCFG on each basic block |
| 587 | /// in the program. |
| 588 | |
| 589 | class ReduceSimplifyCFG : public ListReducer<const BasicBlock *> { |
| 590 | BugDriver &BD; |
Vedant Kumar | 08d829d | 2018-02-08 18:46:49 +0000 | [diff] [blame] | 591 | BugTester TestFn; |
Daniel Berlin | 6060626 | 2016-07-28 22:29:25 +0000 | [diff] [blame] | 592 | TargetTransformInfo TTI; |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 593 | |
Daniel Berlin | 6060626 | 2016-07-28 22:29:25 +0000 | [diff] [blame] | 594 | public: |
Vedant Kumar | 08d829d | 2018-02-08 18:46:49 +0000 | [diff] [blame] | 595 | ReduceSimplifyCFG(BugDriver &bd, BugTester testFn) |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 596 | : BD(bd), TestFn(testFn), TTI(bd.getProgram()->getDataLayout()) {} |
Daniel Berlin | 6060626 | 2016-07-28 22:29:25 +0000 | [diff] [blame] | 597 | |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 598 | Expected<TestResult> doTest(std::vector<const BasicBlock *> &Prefix, |
| 599 | std::vector<const BasicBlock *> &Kept) override { |
Daniel Berlin | 6060626 | 2016-07-28 22:29:25 +0000 | [diff] [blame] | 600 | if (!Kept.empty() && TestBlocks(Kept)) |
| 601 | return KeepSuffix; |
| 602 | if (!Prefix.empty() && TestBlocks(Prefix)) |
| 603 | return KeepPrefix; |
| 604 | return NoFailure; |
| 605 | } |
| 606 | |
| 607 | bool TestBlocks(std::vector<const BasicBlock *> &Prefix); |
| 608 | }; |
| 609 | } |
| 610 | |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 611 | bool ReduceSimplifyCFG::TestBlocks(std::vector<const BasicBlock *> &BBs) { |
Daniel Berlin | 6060626 | 2016-07-28 22:29:25 +0000 | [diff] [blame] | 612 | // Clone the program to try hacking it apart... |
| 613 | ValueToValueMapTy VMap; |
Rafael Espindola | 7186753 | 2018-02-14 19:50:40 +0000 | [diff] [blame^] | 614 | std::unique_ptr<Module> M = CloneModule(*BD.getProgram(), VMap); |
Daniel Berlin | 6060626 | 2016-07-28 22:29:25 +0000 | [diff] [blame] | 615 | |
| 616 | // Convert list to set for fast lookup... |
| 617 | SmallPtrSet<const BasicBlock *, 8> Blocks; |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 618 | for (const auto *BB : BBs) |
Daniel Berlin | 6060626 | 2016-07-28 22:29:25 +0000 | [diff] [blame] | 619 | Blocks.insert(cast<BasicBlock>(VMap[BB])); |
| 620 | |
| 621 | outs() << "Checking for crash with CFG simplifying:"; |
| 622 | unsigned NumPrint = Blocks.size(); |
| 623 | if (NumPrint > 10) |
| 624 | NumPrint = 10; |
| 625 | for (unsigned i = 0, e = NumPrint; i != e; ++i) |
| 626 | outs() << " " << BBs[i]->getName(); |
| 627 | if (NumPrint < Blocks.size()) |
| 628 | outs() << "... <" << Blocks.size() << " total>"; |
| 629 | outs() << ": "; |
| 630 | |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 631 | // The following may destroy some blocks, so we save them first |
Daniel Berlin | 6060626 | 2016-07-28 22:29:25 +0000 | [diff] [blame] | 632 | std::vector<std::pair<std::string, std::string>> BlockInfo; |
| 633 | |
| 634 | for (const BasicBlock *BB : Blocks) |
| 635 | BlockInfo.emplace_back(BB->getParent()->getName(), BB->getName()); |
| 636 | |
Daniel Berlin | 6060626 | 2016-07-28 22:29:25 +0000 | [diff] [blame] | 637 | // Loop over and delete any hack up any blocks that are not listed... |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 638 | for (auto &F : *M) |
| 639 | // Loop over all of the basic blocks and remove them if they are unneeded. |
| 640 | for (Function::iterator BBIt = F.begin(); BBIt != F.end();) { |
| 641 | if (!Blocks.count(&*BBIt)) { |
| 642 | ++BBIt; |
| 643 | continue; |
| 644 | } |
Sanjay Patel | 4c33d52 | 2017-10-04 20:26:25 +0000 | [diff] [blame] | 645 | simplifyCFG(&*BBIt++, TTI); |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 646 | } |
Daniel Berlin | 6060626 | 2016-07-28 22:29:25 +0000 | [diff] [blame] | 647 | // Verify we didn't break anything |
| 648 | std::vector<std::string> Passes; |
| 649 | Passes.push_back("verify"); |
Vedant Kumar | e84e767 | 2018-02-09 05:09:50 +0000 | [diff] [blame] | 650 | std::unique_ptr<Module> New = BD.runPassesOn(M.get(), Passes); |
Daniel Berlin | 6060626 | 2016-07-28 22:29:25 +0000 | [diff] [blame] | 651 | if (!New) { |
| 652 | errs() << "verify failed!\n"; |
| 653 | exit(1); |
| 654 | } |
Vedant Kumar | e84e767 | 2018-02-09 05:09:50 +0000 | [diff] [blame] | 655 | M = std::move(New); |
Daniel Berlin | 6060626 | 2016-07-28 22:29:25 +0000 | [diff] [blame] | 656 | |
| 657 | // Try running on the hacked up program... |
Vedant Kumar | e84e767 | 2018-02-09 05:09:50 +0000 | [diff] [blame] | 658 | if (TestFn(BD, M.get())) { |
| 659 | BD.setNewProgram(M.release()); // It crashed, keep the trimmed version... |
Daniel Berlin | 6060626 | 2016-07-28 22:29:25 +0000 | [diff] [blame] | 660 | |
| 661 | // Make sure to use basic block pointers that point into the now-current |
| 662 | // module, and that they don't include any deleted blocks. |
| 663 | BBs.clear(); |
Vedant Kumar | e84e767 | 2018-02-09 05:09:50 +0000 | [diff] [blame] | 664 | const ValueSymbolTable &GST = BD.getProgram()->getValueSymbolTable(); |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 665 | for (auto &BI : BlockInfo) { |
Daniel Berlin | 6060626 | 2016-07-28 22:29:25 +0000 | [diff] [blame] | 666 | auto *F = cast<Function>(GST.lookup(BI.first)); |
Mehdi Amini | a53d49e | 2016-09-17 06:00:02 +0000 | [diff] [blame] | 667 | Value *V = F->getValueSymbolTable()->lookup(BI.second); |
Daniel Berlin | 6060626 | 2016-07-28 22:29:25 +0000 | [diff] [blame] | 668 | if (V && V->getType() == Type::getLabelTy(V->getContext())) |
| 669 | BBs.push_back(cast<BasicBlock>(V)); |
| 670 | } |
| 671 | return true; |
| 672 | } |
Vedant Kumar | e84e767 | 2018-02-09 05:09:50 +0000 | [diff] [blame] | 673 | // It didn't crash, try something else. |
Daniel Berlin | 6060626 | 2016-07-28 22:29:25 +0000 | [diff] [blame] | 674 | return false; |
| 675 | } |
| 676 | |
| 677 | namespace { |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 678 | /// ReduceCrashingInstructions reducer - This works by removing the specified |
| 679 | /// non-terminator instructions and replacing them with undef. |
| 680 | /// |
| 681 | class ReduceCrashingInstructions : public ListReducer<const Instruction *> { |
| 682 | BugDriver &BD; |
Vedant Kumar | 08d829d | 2018-02-08 18:46:49 +0000 | [diff] [blame] | 683 | BugTester TestFn; |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 684 | |
| 685 | public: |
Vedant Kumar | 08d829d | 2018-02-08 18:46:49 +0000 | [diff] [blame] | 686 | ReduceCrashingInstructions(BugDriver &bd, BugTester testFn) |
Nick Lewycky | 6e090c9 | 2009-05-25 05:30:00 +0000 | [diff] [blame] | 687 | : BD(bd), TestFn(testFn) {} |
| 688 | |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 689 | Expected<TestResult> doTest(std::vector<const Instruction *> &Prefix, |
| 690 | std::vector<const Instruction *> &Kept) override { |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 691 | if (!Kept.empty() && TestInsts(Kept)) |
| 692 | return KeepSuffix; |
| 693 | if (!Prefix.empty() && TestInsts(Prefix)) |
| 694 | return KeepPrefix; |
| 695 | return NoFailure; |
| 696 | } |
Nick Lewycky | 6e090c9 | 2009-05-25 05:30:00 +0000 | [diff] [blame] | 697 | |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 698 | bool TestInsts(std::vector<const Instruction *> &Prefix); |
| 699 | }; |
Nick Lewycky | 6e090c9 | 2009-05-25 05:30:00 +0000 | [diff] [blame] | 700 | } |
| 701 | |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 702 | bool ReduceCrashingInstructions::TestInsts( |
| 703 | std::vector<const Instruction *> &Insts) { |
Nick Lewycky | 6e090c9 | 2009-05-25 05:30:00 +0000 | [diff] [blame] | 704 | // Clone the program to try hacking it apart... |
Rafael Espindola | 229e38f | 2010-10-13 01:36:30 +0000 | [diff] [blame] | 705 | ValueToValueMapTy VMap; |
Rafael Espindola | 7186753 | 2018-02-14 19:50:40 +0000 | [diff] [blame^] | 706 | Module *M = CloneModule(*BD.getProgram(), VMap).release(); |
Nick Lewycky | 6e090c9 | 2009-05-25 05:30:00 +0000 | [diff] [blame] | 707 | |
| 708 | // Convert list to set for fast lookup... |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 709 | SmallPtrSet<Instruction *, 32> Instructions; |
Nick Lewycky | 6e090c9 | 2009-05-25 05:30:00 +0000 | [diff] [blame] | 710 | for (unsigned i = 0, e = Insts.size(); i != e; ++i) { |
| 711 | assert(!isa<TerminatorInst>(Insts[i])); |
Devang Patel | 0dc3c2d | 2010-06-24 00:33:28 +0000 | [diff] [blame] | 712 | Instructions.insert(cast<Instruction>(VMap[Insts[i]])); |
Nick Lewycky | 6e090c9 | 2009-05-25 05:30:00 +0000 | [diff] [blame] | 713 | } |
| 714 | |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 715 | outs() << "Checking for crash with only " << Instructions.size(); |
Nick Lewycky | 6e090c9 | 2009-05-25 05:30:00 +0000 | [diff] [blame] | 716 | if (Instructions.size() == 1) |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 717 | outs() << " instruction: "; |
Nick Lewycky | 6e090c9 | 2009-05-25 05:30:00 +0000 | [diff] [blame] | 718 | else |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 719 | outs() << " instructions: "; |
Nick Lewycky | 6e090c9 | 2009-05-25 05:30:00 +0000 | [diff] [blame] | 720 | |
| 721 | for (Module::iterator MI = M->begin(), ME = M->end(); MI != ME; ++MI) |
| 722 | for (Function::iterator FI = MI->begin(), FE = MI->end(); FI != FE; ++FI) |
| 723 | for (BasicBlock::iterator I = FI->begin(), E = FI->end(); I != E;) { |
Duncan P. N. Exon Smith | 306d16e | 2015-10-20 19:36:39 +0000 | [diff] [blame] | 724 | Instruction *Inst = &*I++; |
Eli Friedman | d4e02a5 | 2011-11-01 04:40:56 +0000 | [diff] [blame] | 725 | if (!Instructions.count(Inst) && !isa<TerminatorInst>(Inst) && |
Arnold Schwaighofer | a8f5a82 | 2017-03-07 20:28:59 +0000 | [diff] [blame] | 726 | !Inst->isEHPad() && !Inst->getType()->isTokenTy() && |
| 727 | !Inst->isSwiftError()) { |
Philip Reames | 29e641c | 2016-06-29 00:15:35 +0000 | [diff] [blame] | 728 | if (!Inst->getType()->isVoidTy()) |
Nick Lewycky | 6e090c9 | 2009-05-25 05:30:00 +0000 | [diff] [blame] | 729 | Inst->replaceAllUsesWith(UndefValue::get(Inst->getType())); |
| 730 | Inst->eraseFromParent(); |
| 731 | } |
| 732 | } |
| 733 | |
| 734 | // Verify that this is still valid. |
Chandler Carruth | 30d69c2 | 2015-02-13 10:01:29 +0000 | [diff] [blame] | 735 | legacy::PassManager Passes; |
Adrian Prantl | 919bdf1 | 2016-10-18 16:24:43 +0000 | [diff] [blame] | 736 | Passes.add(createVerifierPass(/*FatalErrors=*/false)); |
Nick Lewycky | 6e090c9 | 2009-05-25 05:30:00 +0000 | [diff] [blame] | 737 | Passes.run(*M); |
| 738 | |
| 739 | // Try running on the hacked up program... |
| 740 | if (TestFn(BD, M)) { |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 741 | BD.setNewProgram(M); // It crashed, keep the trimmed version... |
Nick Lewycky | 6e090c9 | 2009-05-25 05:30:00 +0000 | [diff] [blame] | 742 | |
| 743 | // Make sure to use instruction pointers that point into the now-current |
| 744 | // module, and that they don't include any deleted blocks. |
| 745 | Insts.clear(); |
Craig Topper | 4627679 | 2014-08-24 23:23:06 +0000 | [diff] [blame] | 746 | for (Instruction *Inst : Instructions) |
| 747 | Insts.push_back(Inst); |
Nick Lewycky | 6e090c9 | 2009-05-25 05:30:00 +0000 | [diff] [blame] | 748 | return true; |
| 749 | } |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 750 | delete M; // It didn't crash, try something else. |
Nick Lewycky | 6e090c9 | 2009-05-25 05:30:00 +0000 | [diff] [blame] | 751 | return false; |
| 752 | } |
| 753 | |
Keno Fischer | 34ca831 | 2015-11-06 00:12:50 +0000 | [diff] [blame] | 754 | namespace { |
| 755 | // Reduce the list of Named Metadata nodes. We keep this as a list of |
| 756 | // names to avoid having to convert back and forth every time. |
| 757 | class ReduceCrashingNamedMD : public ListReducer<std::string> { |
| 758 | BugDriver &BD; |
Vedant Kumar | 08d829d | 2018-02-08 18:46:49 +0000 | [diff] [blame] | 759 | BugTester TestFn; |
Keno Fischer | 34ca831 | 2015-11-06 00:12:50 +0000 | [diff] [blame] | 760 | |
| 761 | public: |
Vedant Kumar | 08d829d | 2018-02-08 18:46:49 +0000 | [diff] [blame] | 762 | ReduceCrashingNamedMD(BugDriver &bd, BugTester testFn) |
Keno Fischer | 34ca831 | 2015-11-06 00:12:50 +0000 | [diff] [blame] | 763 | : BD(bd), TestFn(testFn) {} |
| 764 | |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 765 | Expected<TestResult> doTest(std::vector<std::string> &Prefix, |
| 766 | std::vector<std::string> &Kept) override { |
Keno Fischer | 34ca831 | 2015-11-06 00:12:50 +0000 | [diff] [blame] | 767 | if (!Kept.empty() && TestNamedMDs(Kept)) |
| 768 | return KeepSuffix; |
| 769 | if (!Prefix.empty() && TestNamedMDs(Prefix)) |
| 770 | return KeepPrefix; |
| 771 | return NoFailure; |
| 772 | } |
| 773 | |
| 774 | bool TestNamedMDs(std::vector<std::string> &NamedMDs); |
| 775 | }; |
| 776 | } |
| 777 | |
| 778 | bool ReduceCrashingNamedMD::TestNamedMDs(std::vector<std::string> &NamedMDs) { |
| 779 | |
| 780 | ValueToValueMapTy VMap; |
Rafael Espindola | 7186753 | 2018-02-14 19:50:40 +0000 | [diff] [blame^] | 781 | Module *M = CloneModule(*BD.getProgram(), VMap).release(); |
Keno Fischer | 34ca831 | 2015-11-06 00:12:50 +0000 | [diff] [blame] | 782 | |
| 783 | outs() << "Checking for crash with only these named metadata nodes:"; |
| 784 | unsigned NumPrint = std::min<size_t>(NamedMDs.size(), 10); |
| 785 | for (unsigned i = 0, e = NumPrint; i != e; ++i) |
| 786 | outs() << " " << NamedMDs[i]; |
| 787 | if (NumPrint < NamedMDs.size()) |
| 788 | outs() << "... <" << NamedMDs.size() << " total>"; |
| 789 | outs() << ": "; |
| 790 | |
| 791 | // Make a StringMap for faster lookup |
| 792 | StringSet<> Names; |
| 793 | for (const std::string &Name : NamedMDs) |
| 794 | Names.insert(Name); |
| 795 | |
| 796 | // First collect all the metadata to delete in a vector, then |
| 797 | // delete them all at once to avoid invalidating the iterator |
| 798 | std::vector<NamedMDNode *> ToDelete; |
| 799 | ToDelete.reserve(M->named_metadata_size() - Names.size()); |
| 800 | for (auto &NamedMD : M->named_metadata()) |
Adrian Prantl | faebbb0 | 2016-03-28 21:06:26 +0000 | [diff] [blame] | 801 | // Always keep a nonempty llvm.dbg.cu because the Verifier would complain. |
| 802 | if (!Names.count(NamedMD.getName()) && |
| 803 | (!(NamedMD.getName() == "llvm.dbg.cu" && NamedMD.getNumOperands() > 0))) |
Keno Fischer | 34ca831 | 2015-11-06 00:12:50 +0000 | [diff] [blame] | 804 | ToDelete.push_back(&NamedMD); |
| 805 | |
| 806 | for (auto *NamedMD : ToDelete) |
| 807 | NamedMD->eraseFromParent(); |
| 808 | |
| 809 | // Verify that this is still valid. |
| 810 | legacy::PassManager Passes; |
Adrian Prantl | 919bdf1 | 2016-10-18 16:24:43 +0000 | [diff] [blame] | 811 | Passes.add(createVerifierPass(/*FatalErrors=*/false)); |
Keno Fischer | 34ca831 | 2015-11-06 00:12:50 +0000 | [diff] [blame] | 812 | Passes.run(*M); |
| 813 | |
| 814 | // Try running on the hacked up program... |
| 815 | if (TestFn(BD, M)) { |
| 816 | BD.setNewProgram(M); // It crashed, keep the trimmed version... |
| 817 | return true; |
| 818 | } |
| 819 | delete M; // It didn't crash, try something else. |
| 820 | return false; |
| 821 | } |
| 822 | |
| 823 | namespace { |
| 824 | // Reduce the list of operands to named metadata nodes |
| 825 | class ReduceCrashingNamedMDOps : public ListReducer<const MDNode *> { |
| 826 | BugDriver &BD; |
Vedant Kumar | 08d829d | 2018-02-08 18:46:49 +0000 | [diff] [blame] | 827 | BugTester TestFn; |
Keno Fischer | 34ca831 | 2015-11-06 00:12:50 +0000 | [diff] [blame] | 828 | |
| 829 | public: |
Vedant Kumar | 08d829d | 2018-02-08 18:46:49 +0000 | [diff] [blame] | 830 | ReduceCrashingNamedMDOps(BugDriver &bd, BugTester testFn) |
Keno Fischer | 34ca831 | 2015-11-06 00:12:50 +0000 | [diff] [blame] | 831 | : BD(bd), TestFn(testFn) {} |
| 832 | |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 833 | Expected<TestResult> doTest(std::vector<const MDNode *> &Prefix, |
| 834 | std::vector<const MDNode *> &Kept) override { |
Keno Fischer | 34ca831 | 2015-11-06 00:12:50 +0000 | [diff] [blame] | 835 | if (!Kept.empty() && TestNamedMDOps(Kept)) |
| 836 | return KeepSuffix; |
| 837 | if (!Prefix.empty() && TestNamedMDOps(Prefix)) |
| 838 | return KeepPrefix; |
| 839 | return NoFailure; |
| 840 | } |
| 841 | |
| 842 | bool TestNamedMDOps(std::vector<const MDNode *> &NamedMDOps); |
| 843 | }; |
| 844 | } |
| 845 | |
| 846 | bool ReduceCrashingNamedMDOps::TestNamedMDOps( |
| 847 | std::vector<const MDNode *> &NamedMDOps) { |
| 848 | // Convert list to set for fast lookup... |
Matthias Braun | b30f2f51 | 2016-01-30 01:24:31 +0000 | [diff] [blame] | 849 | SmallPtrSet<const MDNode *, 32> OldMDNodeOps; |
Keno Fischer | 34ca831 | 2015-11-06 00:12:50 +0000 | [diff] [blame] | 850 | for (unsigned i = 0, e = NamedMDOps.size(); i != e; ++i) { |
| 851 | OldMDNodeOps.insert(NamedMDOps[i]); |
| 852 | } |
| 853 | |
| 854 | outs() << "Checking for crash with only " << OldMDNodeOps.size(); |
| 855 | if (OldMDNodeOps.size() == 1) |
| 856 | outs() << " named metadata operand: "; |
| 857 | else |
| 858 | outs() << " named metadata operands: "; |
| 859 | |
| 860 | ValueToValueMapTy VMap; |
Rafael Espindola | 7186753 | 2018-02-14 19:50:40 +0000 | [diff] [blame^] | 861 | Module *M = CloneModule(*BD.getProgram(), VMap).release(); |
Keno Fischer | 34ca831 | 2015-11-06 00:12:50 +0000 | [diff] [blame] | 862 | |
| 863 | // This is a little wasteful. In the future it might be good if we could have |
| 864 | // these dropped during cloning. |
| 865 | for (auto &NamedMD : BD.getProgram()->named_metadata()) { |
| 866 | // Drop the old one and create a new one |
| 867 | M->eraseNamedMetadata(M->getNamedMetadata(NamedMD.getName())); |
| 868 | NamedMDNode *NewNamedMDNode = |
| 869 | M->getOrInsertNamedMetadata(NamedMD.getName()); |
| 870 | for (MDNode *op : NamedMD.operands()) |
| 871 | if (OldMDNodeOps.count(op)) |
| 872 | NewNamedMDNode->addOperand(cast<MDNode>(MapMetadata(op, VMap))); |
| 873 | } |
| 874 | |
| 875 | // Verify that this is still valid. |
| 876 | legacy::PassManager Passes; |
Adrian Prantl | 919bdf1 | 2016-10-18 16:24:43 +0000 | [diff] [blame] | 877 | Passes.add(createVerifierPass(/*FatalErrors=*/false)); |
Keno Fischer | 34ca831 | 2015-11-06 00:12:50 +0000 | [diff] [blame] | 878 | Passes.run(*M); |
| 879 | |
| 880 | // Try running on the hacked up program... |
| 881 | if (TestFn(BD, M)) { |
| 882 | // Make sure to use instruction pointers that point into the now-current |
| 883 | // module, and that they don't include any deleted blocks. |
| 884 | NamedMDOps.clear(); |
| 885 | for (const MDNode *Node : OldMDNodeOps) |
Duncan P. N. Exon Smith | da4a56d | 2016-04-02 17:04:38 +0000 | [diff] [blame] | 886 | NamedMDOps.push_back(cast<MDNode>(*VMap.getMappedMD(Node))); |
Keno Fischer | 34ca831 | 2015-11-06 00:12:50 +0000 | [diff] [blame] | 887 | |
| 888 | BD.setNewProgram(M); // It crashed, keep the trimmed version... |
| 889 | return true; |
| 890 | } |
| 891 | delete M; // It didn't crash, try something else. |
| 892 | return false; |
| 893 | } |
| 894 | |
Vedant Kumar | 66e85e6 | 2018-02-08 20:27:09 +0000 | [diff] [blame] | 895 | /// Attempt to eliminate as many global initializers as possible. |
Vedant Kumar | 08d829d | 2018-02-08 18:46:49 +0000 | [diff] [blame] | 896 | static Error ReduceGlobalInitializers(BugDriver &BD, BugTester TestFn) { |
Vedant Kumar | 66e85e6 | 2018-02-08 20:27:09 +0000 | [diff] [blame] | 897 | Module *OrigM = BD.getProgram(); |
| 898 | if (OrigM->global_empty()) |
| 899 | return Error::success(); |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 900 | |
Vedant Kumar | 66e85e6 | 2018-02-08 20:27:09 +0000 | [diff] [blame] | 901 | // Now try to reduce the number of global variable initializers in the |
| 902 | // module to something small. |
Rafael Espindola | 7186753 | 2018-02-14 19:50:40 +0000 | [diff] [blame^] | 903 | std::unique_ptr<Module> M = CloneModule(*OrigM); |
Vedant Kumar | 66e85e6 | 2018-02-08 20:27:09 +0000 | [diff] [blame] | 904 | bool DeletedInit = false; |
Bill Wendling | 3f83343 | 2006-10-25 18:36:14 +0000 | [diff] [blame] | 905 | |
Vedant Kumar | 66e85e6 | 2018-02-08 20:27:09 +0000 | [diff] [blame] | 906 | for (GlobalVariable &GV : M->globals()) { |
| 907 | if (GV.hasInitializer()) { |
| 908 | DeleteGlobalInitializer(&GV); |
| 909 | GV.setLinkage(GlobalValue::ExternalLinkage); |
| 910 | GV.setComdat(nullptr); |
| 911 | DeletedInit = true; |
Chris Lattner | 65e5f65 | 2003-04-25 00:53:05 +0000 | [diff] [blame] | 912 | } |
| 913 | } |
Vedant Kumar | 66e85e6 | 2018-02-08 20:27:09 +0000 | [diff] [blame] | 914 | |
| 915 | if (!DeletedInit) |
| 916 | return Error::success(); |
| 917 | |
| 918 | // See if the program still causes a crash... |
| 919 | outs() << "\nChecking to see if we can delete global inits: "; |
| 920 | |
| 921 | if (TestFn(BD, M.get())) { // Still crashes? |
| 922 | BD.setNewProgram(M.release()); |
| 923 | outs() << "\n*** Able to remove all global initializers!\n"; |
| 924 | return Error::success(); |
| 925 | } |
| 926 | |
| 927 | // No longer crashes. |
| 928 | outs() << " - Removing all global inits hides problem!\n"; |
| 929 | |
| 930 | std::vector<GlobalVariable *> GVs; |
| 931 | for (GlobalVariable &GV : OrigM->globals()) |
| 932 | if (GV.hasInitializer()) |
| 933 | GVs.push_back(&GV); |
| 934 | |
| 935 | if (GVs.size() > 1 && !BugpointIsInterrupted) { |
| 936 | outs() << "\n*** Attempting to reduce the number of global initializers " |
| 937 | << "in the testcase\n"; |
| 938 | |
| 939 | unsigned OldSize = GVs.size(); |
| 940 | Expected<bool> Result = |
| 941 | ReduceCrashingGlobalInitializers(BD, TestFn).reduceList(GVs); |
| 942 | if (Error E = Result.takeError()) |
| 943 | return E; |
| 944 | |
| 945 | if (GVs.size() < OldSize) |
| 946 | BD.EmitProgressBitcode(BD.getProgram(), "reduced-global-variables"); |
| 947 | } |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 948 | return Error::success(); |
Philip Reames | 1c232f9 | 2016-06-29 00:43:18 +0000 | [diff] [blame] | 949 | } |
| 950 | |
Vedant Kumar | 08d829d | 2018-02-08 18:46:49 +0000 | [diff] [blame] | 951 | static Error ReduceInsts(BugDriver &BD, BugTester TestFn) { |
Philip Reames | 1c232f9 | 2016-06-29 00:43:18 +0000 | [diff] [blame] | 952 | // Attempt to delete instructions using bisection. This should help out nasty |
| 953 | // cases with large basic blocks where the problem is at one end. |
| 954 | if (!BugpointIsInterrupted) { |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 955 | std::vector<const Instruction *> Insts; |
Philip Reames | 1c232f9 | 2016-06-29 00:43:18 +0000 | [diff] [blame] | 956 | for (const Function &F : *BD.getProgram()) |
| 957 | for (const BasicBlock &BB : F) |
| 958 | for (const Instruction &I : BB) |
| 959 | if (!isa<TerminatorInst>(&I)) |
| 960 | Insts.push_back(&I); |
| 961 | |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 962 | Expected<bool> Result = |
| 963 | ReduceCrashingInstructions(BD, TestFn).reduceList(Insts); |
| 964 | if (Error E = Result.takeError()) |
| 965 | return E; |
Philip Reames | 1c232f9 | 2016-06-29 00:43:18 +0000 | [diff] [blame] | 966 | } |
| 967 | |
| 968 | unsigned Simplification = 2; |
| 969 | do { |
| 970 | if (BugpointIsInterrupted) |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 971 | // TODO: Should we distinguish this with an "interrupted error"? |
| 972 | return Error::success(); |
Philip Reames | 1c232f9 | 2016-06-29 00:43:18 +0000 | [diff] [blame] | 973 | --Simplification; |
| 974 | outs() << "\n*** Attempting to reduce testcase by deleting instruc" |
| 975 | << "tions: Simplification Level #" << Simplification << '\n'; |
| 976 | |
| 977 | // Now that we have deleted the functions that are unnecessary for the |
| 978 | // program, try to remove instructions that are not necessary to cause the |
| 979 | // crash. To do this, we loop through all of the instructions in the |
| 980 | // remaining functions, deleting them (replacing any values produced with |
| 981 | // nulls), and then running ADCE and SimplifyCFG. If the transformed input |
| 982 | // still triggers failure, keep deleting until we cannot trigger failure |
| 983 | // anymore. |
| 984 | // |
| 985 | unsigned InstructionsToSkipBeforeDeleting = 0; |
| 986 | TryAgain: |
| 987 | |
| 988 | // Loop over all of the (non-terminator) instructions remaining in the |
| 989 | // function, attempting to delete them. |
| 990 | unsigned CurInstructionNum = 0; |
| 991 | for (Module::const_iterator FI = BD.getProgram()->begin(), |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 992 | E = BD.getProgram()->end(); |
| 993 | FI != E; ++FI) |
Philip Reames | 1c232f9 | 2016-06-29 00:43:18 +0000 | [diff] [blame] | 994 | if (!FI->isDeclaration()) |
| 995 | for (Function::const_iterator BI = FI->begin(), E = FI->end(); BI != E; |
| 996 | ++BI) |
| 997 | for (BasicBlock::const_iterator I = BI->begin(), E = --BI->end(); |
| 998 | I != E; ++I, ++CurInstructionNum) { |
| 999 | if (InstructionsToSkipBeforeDeleting) { |
| 1000 | --InstructionsToSkipBeforeDeleting; |
| 1001 | } else { |
| 1002 | if (BugpointIsInterrupted) |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 1003 | // TODO: Should this be some kind of interrupted error? |
| 1004 | return Error::success(); |
Philip Reames | 1c232f9 | 2016-06-29 00:43:18 +0000 | [diff] [blame] | 1005 | |
Arnold Schwaighofer | a8f5a82 | 2017-03-07 20:28:59 +0000 | [diff] [blame] | 1006 | if (I->isEHPad() || I->getType()->isTokenTy() || |
| 1007 | I->isSwiftError()) |
Philip Reames | 1c232f9 | 2016-06-29 00:43:18 +0000 | [diff] [blame] | 1008 | continue; |
| 1009 | |
| 1010 | outs() << "Checking instruction: " << *I; |
| 1011 | std::unique_ptr<Module> M = |
| 1012 | BD.deleteInstructionFromProgram(&*I, Simplification); |
| 1013 | |
| 1014 | // Find out if the pass still crashes on this pass... |
| 1015 | if (TestFn(BD, M.get())) { |
| 1016 | // Yup, it does, we delete the old module, and continue trying |
| 1017 | // to reduce the testcase... |
| 1018 | BD.setNewProgram(M.release()); |
| 1019 | InstructionsToSkipBeforeDeleting = CurInstructionNum; |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 1020 | goto TryAgain; // I wish I had a multi-level break here! |
Philip Reames | 1c232f9 | 2016-06-29 00:43:18 +0000 | [diff] [blame] | 1021 | } |
| 1022 | } |
| 1023 | } |
| 1024 | |
| 1025 | if (InstructionsToSkipBeforeDeleting) { |
| 1026 | InstructionsToSkipBeforeDeleting = 0; |
| 1027 | goto TryAgain; |
| 1028 | } |
| 1029 | |
| 1030 | } while (Simplification); |
| 1031 | BD.EmitProgressBitcode(BD.getProgram(), "reduced-instructions"); |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 1032 | return Error::success(); |
Philip Reames | 1c232f9 | 2016-06-29 00:43:18 +0000 | [diff] [blame] | 1033 | } |
| 1034 | |
Philip Reames | 1c232f9 | 2016-06-29 00:43:18 +0000 | [diff] [blame] | 1035 | /// DebugACrash - Given a predicate that determines whether a component crashes |
| 1036 | /// on a program, try to destructively reduce the program while still keeping |
| 1037 | /// the predicate true. |
Vedant Kumar | 08d829d | 2018-02-08 18:46:49 +0000 | [diff] [blame] | 1038 | static Error DebugACrash(BugDriver &BD, BugTester TestFn) { |
Philip Reames | 1c232f9 | 2016-06-29 00:43:18 +0000 | [diff] [blame] | 1039 | // See if we can get away with nuking some of the global variable initializers |
| 1040 | // in the program... |
| 1041 | if (!NoGlobalRM) |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 1042 | if (Error E = ReduceGlobalInitializers(BD, TestFn)) |
| 1043 | return E; |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 1044 | |
Chris Lattner | 1d080f2 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 1045 | // Now try to reduce the number of functions in the module to something small. |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 1046 | std::vector<Function *> Functions; |
Duncan P. N. Exon Smith | 306d16e | 2015-10-20 19:36:39 +0000 | [diff] [blame] | 1047 | for (Function &F : *BD.getProgram()) |
| 1048 | if (!F.isDeclaration()) |
| 1049 | Functions.push_back(&F); |
Chris Lattner | 73a6bdd | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 1050 | |
Chris Lattner | beb01fa | 2005-08-02 02:16:17 +0000 | [diff] [blame] | 1051 | if (Functions.size() > 1 && !BugpointIsInterrupted) { |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 1052 | outs() << "\n*** Attempting to reduce the number of functions " |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 1053 | "in the testcase\n"; |
Chris Lattner | 73a6bdd | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 1054 | |
Chris Lattner | 8bda4c4 | 2004-02-18 23:26:28 +0000 | [diff] [blame] | 1055 | unsigned OldSize = Functions.size(); |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 1056 | Expected<bool> Result = |
| 1057 | ReduceCrashingFunctions(BD, TestFn).reduceList(Functions); |
| 1058 | if (Error E = Result.takeError()) |
| 1059 | return E; |
Chris Lattner | 73a6bdd | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 1060 | |
Chris Lattner | beb01fa | 2005-08-02 02:16:17 +0000 | [diff] [blame] | 1061 | if (Functions.size() < OldSize) |
Rafael Espindola | 594994a | 2010-07-28 18:12:30 +0000 | [diff] [blame] | 1062 | BD.EmitProgressBitcode(BD.getProgram(), "reduced-function"); |
Chris Lattner | 73a6bdd | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 1063 | } |
| 1064 | |
Daniel Berlin | 271ca40 | 2016-07-27 16:13:25 +0000 | [diff] [blame] | 1065 | // Attempt to change conditional branches into unconditional branches to |
| 1066 | // eliminate blocks. |
| 1067 | if (!DisableSimplifyCFG && !BugpointIsInterrupted) { |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 1068 | std::vector<const BasicBlock *> Blocks; |
Daniel Berlin | 271ca40 | 2016-07-27 16:13:25 +0000 | [diff] [blame] | 1069 | for (Function &F : *BD.getProgram()) |
| 1070 | for (BasicBlock &BB : F) |
| 1071 | Blocks.push_back(&BB); |
| 1072 | unsigned OldSize = Blocks.size(); |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 1073 | Expected<bool> Result = |
| 1074 | ReduceCrashingConditionals(BD, TestFn, true).reduceList(Blocks); |
| 1075 | if (Error E = Result.takeError()) |
| 1076 | return E; |
| 1077 | Result = ReduceCrashingConditionals(BD, TestFn, false).reduceList(Blocks); |
| 1078 | if (Error E = Result.takeError()) |
| 1079 | return E; |
Daniel Berlin | 271ca40 | 2016-07-27 16:13:25 +0000 | [diff] [blame] | 1080 | if (Blocks.size() < OldSize) |
| 1081 | BD.EmitProgressBitcode(BD.getProgram(), "reduced-conditionals"); |
| 1082 | } |
| 1083 | |
Chris Lattner | f32939b | 2003-04-24 23:51:38 +0000 | [diff] [blame] | 1084 | // Attempt to delete entire basic blocks at a time to speed up |
| 1085 | // convergence... this actually works by setting the terminator of the blocks |
| 1086 | // to a return instruction then running simplifycfg, which can potentially |
| 1087 | // shrinks the code dramatically quickly |
| 1088 | // |
Chris Lattner | beb01fa | 2005-08-02 02:16:17 +0000 | [diff] [blame] | 1089 | if (!DisableSimplifyCFG && !BugpointIsInterrupted) { |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 1090 | std::vector<const BasicBlock *> Blocks; |
Duncan P. N. Exon Smith | 306d16e | 2015-10-20 19:36:39 +0000 | [diff] [blame] | 1091 | for (Function &F : *BD.getProgram()) |
| 1092 | for (BasicBlock &BB : F) |
| 1093 | Blocks.push_back(&BB); |
Torok Edwin | 6ee6f73 | 2009-05-24 09:40:47 +0000 | [diff] [blame] | 1094 | unsigned OldSize = Blocks.size(); |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 1095 | Expected<bool> Result = ReduceCrashingBlocks(BD, TestFn).reduceList(Blocks); |
| 1096 | if (Error E = Result.takeError()) |
| 1097 | return E; |
Torok Edwin | 6ee6f73 | 2009-05-24 09:40:47 +0000 | [diff] [blame] | 1098 | if (Blocks.size() < OldSize) |
Rafael Espindola | 594994a | 2010-07-28 18:12:30 +0000 | [diff] [blame] | 1099 | BD.EmitProgressBitcode(BD.getProgram(), "reduced-blocks"); |
Chris Lattner | d1e2aae | 2003-08-05 15:51:05 +0000 | [diff] [blame] | 1100 | } |
Chris Lattner | d4e0474 | 2002-12-23 23:49:59 +0000 | [diff] [blame] | 1101 | |
Simon Pilgrim | 0c559f6 | 2017-04-14 15:21:15 +0000 | [diff] [blame] | 1102 | if (!DisableSimplifyCFG && !BugpointIsInterrupted) { |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 1103 | std::vector<const BasicBlock *> Blocks; |
Daniel Berlin | 6060626 | 2016-07-28 22:29:25 +0000 | [diff] [blame] | 1104 | for (Function &F : *BD.getProgram()) |
| 1105 | for (BasicBlock &BB : F) |
| 1106 | Blocks.push_back(&BB); |
| 1107 | unsigned OldSize = Blocks.size(); |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 1108 | Expected<bool> Result = ReduceSimplifyCFG(BD, TestFn).reduceList(Blocks); |
| 1109 | if (Error E = Result.takeError()) |
| 1110 | return E; |
Daniel Berlin | 6060626 | 2016-07-28 22:29:25 +0000 | [diff] [blame] | 1111 | if (Blocks.size() < OldSize) |
| 1112 | BD.EmitProgressBitcode(BD.getProgram(), "reduced-simplifycfg"); |
| 1113 | } |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 1114 | |
Nick Lewycky | 6e090c9 | 2009-05-25 05:30:00 +0000 | [diff] [blame] | 1115 | // Attempt to delete instructions using bisection. This should help out nasty |
| 1116 | // cases with large basic blocks where the problem is at one end. |
Philip Reames | 1c232f9 | 2016-06-29 00:43:18 +0000 | [diff] [blame] | 1117 | if (!BugpointIsInterrupted) |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 1118 | if (Error E = ReduceInsts(BD, TestFn)) |
| 1119 | return E; |
Keno Fischer | 34ca831 | 2015-11-06 00:12:50 +0000 | [diff] [blame] | 1120 | |
Michael Ilseman | e542804 | 2016-10-25 18:44:13 +0000 | [diff] [blame] | 1121 | // Attempt to strip debug info metadata. |
| 1122 | auto stripMetadata = [&](std::function<bool(Module &)> strip) { |
Rafael Espindola | 7186753 | 2018-02-14 19:50:40 +0000 | [diff] [blame^] | 1123 | std::unique_ptr<Module> M = CloneModule(*BD.getProgram()); |
Michael Ilseman | e542804 | 2016-10-25 18:44:13 +0000 | [diff] [blame] | 1124 | strip(*M); |
| 1125 | if (TestFn(BD, M.get())) |
| 1126 | BD.setNewProgram(M.release()); |
| 1127 | }; |
| 1128 | if (!NoStripDebugInfo && !BugpointIsInterrupted) { |
| 1129 | outs() << "\n*** Attempting to strip the debug info: "; |
| 1130 | stripMetadata(StripDebugInfo); |
| 1131 | } |
| 1132 | if (!NoStripDebugTypeInfo && !BugpointIsInterrupted) { |
| 1133 | outs() << "\n*** Attempting to strip the debug type info: "; |
| 1134 | stripMetadata(stripNonLineTableDebugInfo); |
| 1135 | } |
| 1136 | |
Keno Fischer | 34ca831 | 2015-11-06 00:12:50 +0000 | [diff] [blame] | 1137 | if (!NoNamedMDRM) { |
Keno Fischer | 34ca831 | 2015-11-06 00:12:50 +0000 | [diff] [blame] | 1138 | if (!BugpointIsInterrupted) { |
| 1139 | // Try to reduce the amount of global metadata (particularly debug info), |
| 1140 | // by dropping global named metadata that anchors them |
| 1141 | outs() << "\n*** Attempting to remove named metadata: "; |
| 1142 | std::vector<std::string> NamedMDNames; |
| 1143 | for (auto &NamedMD : BD.getProgram()->named_metadata()) |
| 1144 | NamedMDNames.push_back(NamedMD.getName().str()); |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 1145 | Expected<bool> Result = |
| 1146 | ReduceCrashingNamedMD(BD, TestFn).reduceList(NamedMDNames); |
| 1147 | if (Error E = Result.takeError()) |
| 1148 | return E; |
Keno Fischer | 34ca831 | 2015-11-06 00:12:50 +0000 | [diff] [blame] | 1149 | } |
| 1150 | |
| 1151 | if (!BugpointIsInterrupted) { |
| 1152 | // Now that we quickly dropped all the named metadata that doesn't |
| 1153 | // contribute to the crash, bisect the operands of the remaining ones |
| 1154 | std::vector<const MDNode *> NamedMDOps; |
| 1155 | for (auto &NamedMD : BD.getProgram()->named_metadata()) |
Keno Fischer | 256df86 | 2015-11-06 00:45:47 +0000 | [diff] [blame] | 1156 | for (auto op : NamedMD.operands()) |
| 1157 | NamedMDOps.push_back(op); |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 1158 | Expected<bool> Result = |
| 1159 | ReduceCrashingNamedMDOps(BD, TestFn).reduceList(NamedMDOps); |
| 1160 | if (Error E = Result.takeError()) |
| 1161 | return E; |
Keno Fischer | 34ca831 | 2015-11-06 00:12:50 +0000 | [diff] [blame] | 1162 | } |
Philip Reames | ac285cc | 2016-06-29 00:10:39 +0000 | [diff] [blame] | 1163 | BD.EmitProgressBitcode(BD.getProgram(), "reduced-named-md"); |
Keno Fischer | 34ca831 | 2015-11-06 00:12:50 +0000 | [diff] [blame] | 1164 | } |
| 1165 | |
Chris Lattner | 514c02e | 2003-02-28 16:13:20 +0000 | [diff] [blame] | 1166 | // Try to clean up the testcase by running funcresolve and globaldce... |
Chris Lattner | beb01fa | 2005-08-02 02:16:17 +0000 | [diff] [blame] | 1167 | if (!BugpointIsInterrupted) { |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 1168 | outs() << "\n*** Attempting to perform final cleanups: "; |
Rafael Espindola | 7186753 | 2018-02-14 19:50:40 +0000 | [diff] [blame^] | 1169 | std::unique_ptr<Module> M = CloneModule(*BD.getProgram()); |
Vedant Kumar | e84e767 | 2018-02-09 05:09:50 +0000 | [diff] [blame] | 1170 | M = BD.performFinalCleanups(M.release(), true); |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 1171 | |
Chris Lattner | beb01fa | 2005-08-02 02:16:17 +0000 | [diff] [blame] | 1172 | // Find out if the pass still crashes on the cleaned up program... |
Vedant Kumar | e84e767 | 2018-02-09 05:09:50 +0000 | [diff] [blame] | 1173 | if (M && TestFn(BD, M.get())) |
| 1174 | BD.setNewProgram(M.release()); // Yup, it does, keep the reduced version... |
Chris Lattner | 514c02e | 2003-02-28 16:13:20 +0000 | [diff] [blame] | 1175 | } |
| 1176 | |
Rafael Espindola | 594994a | 2010-07-28 18:12:30 +0000 | [diff] [blame] | 1177 | BD.EmitProgressBitcode(BD.getProgram(), "reduced-simplified"); |
Chris Lattner | 514c02e | 2003-02-28 16:13:20 +0000 | [diff] [blame] | 1178 | |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 1179 | return Error::success(); |
Chris Lattner | 73a6bdd | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 1180 | } |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 1181 | |
Rafael Espindola | d1c7ef4 | 2010-08-05 03:00:22 +0000 | [diff] [blame] | 1182 | static bool TestForOptimizerCrash(const BugDriver &BD, Module *M) { |
Philip Reames | e5b5602 | 2016-06-29 03:01:13 +0000 | [diff] [blame] | 1183 | return BD.runPasses(M, BD.getPassesToRun()); |
Chris Lattner | 8bda4c4 | 2004-02-18 23:26:28 +0000 | [diff] [blame] | 1184 | } |
Chris Lattner | ead1dff | 2004-02-18 21:02:04 +0000 | [diff] [blame] | 1185 | |
Chris Lattner | 8bda4c4 | 2004-02-18 23:26:28 +0000 | [diff] [blame] | 1186 | /// debugOptimizerCrash - This method is called when some pass crashes on input. |
| 1187 | /// It attempts to prune down the testcase to something reasonable, and figure |
| 1188 | /// out exactly which pass is crashing. |
| 1189 | /// |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 1190 | Error BugDriver::debugOptimizerCrash(const std::string &ID) { |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 1191 | outs() << "\n*** Debugging optimizer crash!\n"; |
Chris Lattner | 8bda4c4 | 2004-02-18 23:26:28 +0000 | [diff] [blame] | 1192 | |
| 1193 | // Reduce the list of passes which causes the optimizer to crash... |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 1194 | if (!BugpointIsInterrupted && !DontReducePassList) { |
| 1195 | Expected<bool> Result = ReducePassList(*this).reduceList(PassesToRun); |
| 1196 | if (Error E = Result.takeError()) |
| 1197 | return E; |
| 1198 | } |
Chris Lattner | 8bda4c4 | 2004-02-18 23:26:28 +0000 | [diff] [blame] | 1199 | |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 1200 | outs() << "\n*** Found crashing pass" |
| 1201 | << (PassesToRun.size() == 1 ? ": " : "es: ") |
| 1202 | << getPassesString(PassesToRun) << '\n'; |
Chris Lattner | 8bda4c4 | 2004-02-18 23:26:28 +0000 | [diff] [blame] | 1203 | |
Rafael Espindola | 594994a | 2010-07-28 18:12:30 +0000 | [diff] [blame] | 1204 | EmitProgressBitcode(Program, ID); |
Chris Lattner | 8bda4c4 | 2004-02-18 23:26:28 +0000 | [diff] [blame] | 1205 | |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 1206 | return DebugACrash(*this, TestForOptimizerCrash); |
Chris Lattner | 8bda4c4 | 2004-02-18 23:26:28 +0000 | [diff] [blame] | 1207 | } |
| 1208 | |
Rafael Espindola | d1c7ef4 | 2010-08-05 03:00:22 +0000 | [diff] [blame] | 1209 | static bool TestForCodeGenCrash(const BugDriver &BD, Module *M) { |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 1210 | if (Error E = BD.compileProgram(M)) { |
Sebastian Pop | 8f7d019 | 2016-07-15 23:15:06 +0000 | [diff] [blame] | 1211 | if (VerboseErrors) |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 1212 | errs() << toString(std::move(E)) << "\n"; |
| 1213 | else { |
| 1214 | consumeError(std::move(E)); |
Sebastian Pop | 8f7d019 | 2016-07-15 23:15:06 +0000 | [diff] [blame] | 1215 | errs() << "<crash>\n"; |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 1216 | } |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 1217 | return true; // Tool is still crashing. |
Chris Lattner | 8bda4c4 | 2004-02-18 23:26:28 +0000 | [diff] [blame] | 1218 | } |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 1219 | errs() << '\n'; |
| 1220 | return false; |
Chris Lattner | 8bda4c4 | 2004-02-18 23:26:28 +0000 | [diff] [blame] | 1221 | } |
Chris Lattner | ead1dff | 2004-02-18 21:02:04 +0000 | [diff] [blame] | 1222 | |
| 1223 | /// debugCodeGeneratorCrash - This method is called when the code generator |
| 1224 | /// crashes on an input. It attempts to reduce the input as much as possible |
| 1225 | /// while still causing the code generator to crash. |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 1226 | Error BugDriver::debugCodeGeneratorCrash() { |
Dan Gohman | d8db376 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 1227 | errs() << "*** Debugging code generator crash!\n"; |
Chris Lattner | ead1dff | 2004-02-18 21:02:04 +0000 | [diff] [blame] | 1228 | |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 1229 | return DebugACrash(*this, TestForCodeGenCrash); |
Chris Lattner | ead1dff | 2004-02-18 21:02:04 +0000 | [diff] [blame] | 1230 | } |