1 //===- OptimizerDriver.cpp - Allow BugPoint to run passes safely ----------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines an interface that allows bugpoint to run various passes
11 // without the threat of a buggy pass corrupting bugpoint (of course, bugpoint
12 // may have its own bugs, but that's another story...).  It achieves this by
13 // forking a copy of itself and having the child process do the optimizations.
14 // If this client dies, we can always fork a new one.  :)
15 //
16 //===----------------------------------------------------------------------===//
17 
18 #include "BugDriver.h"
19 #include "llvm/Bitcode/BitcodeWriter.h"
20 #include "llvm/IR/DataLayout.h"
21 #include "llvm/IR/LegacyPassManager.h"
22 #include "llvm/IR/Module.h"
23 #include "llvm/IR/Verifier.h"
24 #include "llvm/Support/CommandLine.h"
25 #include "llvm/Support/Debug.h"
26 #include "llvm/Support/FileUtilities.h"
27 #include "llvm/Support/Path.h"
28 #include "llvm/Support/Program.h"
29 #include "llvm/Support/SystemUtils.h"
30 #include "llvm/Support/ToolOutputFile.h"
31 
32 #define DONT_GET_PLUGIN_LOADER_OPTION
33 #include "llvm/Support/PluginLoader.h"
34 
35 #include <fstream>
36 
37 using namespace llvm;
38 
39 #define DEBUG_TYPE "bugpoint"
40 
41 namespace llvm {
42 extern cl::opt<std::string> OutputPrefix;
43 }
44 
45 static cl::opt<bool> PreserveBitcodeUseListOrder(
46     "preserve-bc-uselistorder",
47     cl::desc("Preserve use-list order when writing LLVM bitcode."),
48     cl::init(true), cl::Hidden);
49 
50 // ChildOutput - This option captures the name of the child output file that
51 // is set up by the parent bugpoint process
52 static cl::opt<std::string> ChildOutput("child-output", cl::ReallyHidden);
53 static cl::opt<std::string>
54     OptCmd("opt-command", cl::init(""),
55            cl::desc("Path to opt. (default: search path "
56                     "for 'opt'.)"));
57 
58 /// writeProgramToFile - This writes the current "Program" to the named bitcode
59 /// file.  If an error occurs, true is returned.
60 ///
61 static bool writeProgramToFileAux(ToolOutputFile &Out, const Module *M) {
62   WriteBitcodeToFile(M, Out.os(), PreserveBitcodeUseListOrder);
63   Out.os().close();
64   if (!Out.os().has_error()) {
65     Out.keep();
66     return false;
67   }
68   return true;
69 }
70 
71 bool BugDriver::writeProgramToFile(const std::string &Filename, int FD,
72                                    const Module *M) const {
73   ToolOutputFile Out(Filename, FD);
74   return writeProgramToFileAux(Out, M);
75 }
76 
77 bool BugDriver::writeProgramToFile(int FD, const Module *M) const {
78   raw_fd_ostream OS(FD, /*shouldClose*/ false);
79   WriteBitcodeToFile(M, OS, PreserveBitcodeUseListOrder);
80   OS.flush();
81   if (!OS.has_error())
82     return false;
83   OS.clear_error();
84   return true;
85 }
86 
87 bool BugDriver::writeProgramToFile(const std::string &Filename,
88                                    const Module *M) const {
89   std::error_code EC;
90   ToolOutputFile Out(Filename, EC, sys::fs::F_None);
91   if (!EC)
92     return writeProgramToFileAux(Out, M);
93   return true;
94 }
95 
96 /// EmitProgressBitcode - This function is used to output the current Program
97 /// to a file named "bugpoint-ID.bc".
98 ///
99 void BugDriver::EmitProgressBitcode(const Module *M, const std::string &ID,
100                                     bool NoFlyer) const {
101   // Output the input to the current pass to a bitcode file, emit a message
102   // telling the user how to reproduce it: opt -foo blah.bc
103   //
104   std::string Filename = OutputPrefix + "-" + ID + ".bc";
105   if (writeProgramToFile(Filename, M)) {
106     errs() << "Error opening file '" << Filename << "' for writing!\n";
107     return;
108   }
109 
110   outs() << "Emitted bitcode to '" << Filename << "'\n";
111   if (NoFlyer || PassesToRun.empty())
112     return;
113   outs() << "\n*** You can reproduce the problem with: ";
114   if (UseValgrind)
115     outs() << "valgrind ";
116   outs() << "opt " << Filename;
117   for (unsigned i = 0, e = PluginLoader::getNumPlugins(); i != e; ++i) {
118     outs() << " -load " << PluginLoader::getPlugin(i);
119   }
120   outs() << " " << getPassesString(PassesToRun) << "\n";
121 }
122 
123 cl::opt<bool> SilencePasses(
124     "silence-passes",
125     cl::desc("Suppress output of running passes (both stdout and stderr)"));
126 
127 static cl::list<std::string> OptArgs("opt-args", cl::Positional,
128                                      cl::desc("<opt arguments>..."),
129                                      cl::ZeroOrMore, cl::PositionalEatsArgs);
130 
131 /// runPasses - Run the specified passes on Program, outputting a bitcode file
132 /// and writing the filename into OutputFile if successful.  If the
133 /// optimizations fail for some reason (optimizer crashes), return true,
134 /// otherwise return false.  If DeleteOutput is set to true, the bitcode is
135 /// deleted on success, and the filename string is undefined.  This prints to
136 /// outs() a single line message indicating whether compilation was successful
137 /// or failed.
138 ///
139 bool BugDriver::runPasses(Module *Program,
140                           const std::vector<std::string> &Passes,
141                           std::string &OutputFilename, bool DeleteOutput,
142                           bool Quiet, unsigned NumExtraArgs,
143                           const char *const *ExtraArgs) const {
144   // setup the output file name
145   outs().flush();
146   SmallString<128> UniqueFilename;
147   std::error_code EC = sys::fs::createUniqueFile(
148       OutputPrefix + "-output-%%%%%%%.bc", UniqueFilename);
149   if (EC) {
150     errs() << getToolName()
151            << ": Error making unique filename: " << EC.message() << "\n";
152     return 1;
153   }
154   OutputFilename = UniqueFilename.str();
155 
156   // set up the input file name
157   Expected<sys::fs::TempFile> Temp =
158       sys::fs::TempFile::create(OutputPrefix + "-input-%%%%%%%.bc");
159   if (!Temp) {
160     errs() << getToolName()
161            << ": Error making unique filename: " << toString(Temp.takeError())
162            << "\n";
163     return 1;
164   }
165   DiscardTemp Discard{*Temp};
166   raw_fd_ostream OS(Temp->FD, /*shouldClose*/ false);
167 
168   WriteBitcodeToFile(Program, OS, PreserveBitcodeUseListOrder);
169   OS.flush();
170   if (OS.has_error()) {
171     errs() << "Error writing bitcode file: " << Temp->TmpName << "\n";
172     OS.clear_error();
173     return 1;
174   }
175 
176   std::string tool = OptCmd;
177   if (OptCmd.empty()) {
178     if (ErrorOr<std::string> Path = sys::findProgramByName("opt"))
179       tool = *Path;
180     else
181       errs() << Path.getError().message() << "\n";
182   }
183   if (tool.empty()) {
184     errs() << "Cannot find `opt' in PATH!\n";
185     return 1;
186   }
187 
188   std::string Prog;
189   if (UseValgrind) {
190     if (ErrorOr<std::string> Path = sys::findProgramByName("valgrind"))
191       Prog = *Path;
192     else
193       errs() << Path.getError().message() << "\n";
194   } else
195     Prog = tool;
196   if (Prog.empty()) {
197     errs() << "Cannot find `valgrind' in PATH!\n";
198     return 1;
199   }
200 
201   // setup the child process' arguments
202   SmallVector<const char *, 8> Args;
203   if (UseValgrind) {
204     Args.push_back("valgrind");
205     Args.push_back("--error-exitcode=1");
206     Args.push_back("-q");
207     Args.push_back(tool.c_str());
208   } else
209     Args.push_back(tool.c_str());
210 
211   for (unsigned i = 0, e = OptArgs.size(); i != e; ++i)
212     Args.push_back(OptArgs[i].c_str());
213   Args.push_back("-disable-symbolication");
214   Args.push_back("-o");
215   Args.push_back(OutputFilename.c_str());
216   std::vector<std::string> pass_args;
217   for (unsigned i = 0, e = PluginLoader::getNumPlugins(); i != e; ++i) {
218     pass_args.push_back(std::string("-load"));
219     pass_args.push_back(PluginLoader::getPlugin(i));
220   }
221   for (std::vector<std::string>::const_iterator I = Passes.begin(),
222                                                 E = Passes.end();
223        I != E; ++I)
224     pass_args.push_back(std::string("-") + (*I));
225   for (std::vector<std::string>::const_iterator I = pass_args.begin(),
226                                                 E = pass_args.end();
227        I != E; ++I)
228     Args.push_back(I->c_str());
229   Args.push_back(Temp->TmpName.c_str());
230   for (unsigned i = 0; i < NumExtraArgs; ++i)
231     Args.push_back(*ExtraArgs);
232   Args.push_back(nullptr);
233 
234   DEBUG(errs() << "\nAbout to run:\t";
235         for (unsigned i = 0, e = Args.size() - 1; i != e; ++i) errs()
236         << " " << Args[i];
237         errs() << "\n";);
238 
239   Optional<StringRef> Redirects[3] = {None, None, None};
240   // Redirect stdout and stderr to nowhere if SilencePasses is given.
241   if (SilencePasses) {
242     Redirects[1] = "";
243     Redirects[2] = "";
244   }
245 
246   std::string ErrMsg;
247   int result = sys::ExecuteAndWait(Prog, Args.data(), nullptr, Redirects,
248                                    Timeout, MemoryLimit, &ErrMsg);
249 
250   // If we are supposed to delete the bitcode file or if the passes crashed,
251   // remove it now.  This may fail if the file was never created, but that's ok.
252   if (DeleteOutput || result != 0)
253     sys::fs::remove(OutputFilename);
254 
255   // Remove the temporary input file as well
256   consumeError(Temp->discard());
257 
258   if (!Quiet) {
259     if (result == 0)
260       outs() << "Success!\n";
261     else if (result > 0)
262       outs() << "Exited with error code '" << result << "'\n";
263     else if (result < 0) {
264       if (result == -1)
265         outs() << "Execute failed: " << ErrMsg << "\n";
266       else
267         outs() << "Crashed: " << ErrMsg << "\n";
268     }
269     if (result & 0x01000000)
270       outs() << "Dumped core\n";
271   }
272 
273   // Was the child successful?
274   return result != 0;
275 }
276 
277 std::unique_ptr<Module>
278 BugDriver::runPassesOn(Module *M, const std::vector<std::string> &Passes,
279                        unsigned NumExtraArgs, const char *const *ExtraArgs) {
280   std::string BitcodeResult;
281   if (runPasses(M, Passes, BitcodeResult, false /*delete*/, true /*quiet*/,
282                 NumExtraArgs, ExtraArgs)) {
283     return nullptr;
284   }
285 
286   std::unique_ptr<Module> Ret = parseInputFile(BitcodeResult, Context);
287   if (!Ret) {
288     errs() << getToolName() << ": Error reading bitcode file '" << BitcodeResult
289            << "'!\n";
290     exit(1);
291   }
292   sys::fs::remove(BitcodeResult);
293   return Ret;
294 }
295