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