blob: b0f3880fa74a368d949c7cb6960ef88e95f2a925 [file] [log] [blame]
Chris Lattnerde4aa4c2002-12-23 23:50:16 +00001//===- ExecutionDriver.cpp - Allow execution of LLVM program --------------===//
Misha Brukman650ba8e2005-04-22 00:00:37 +00002//
John Criswell09344dc2003-10-20 17:47:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner345353d2007-12-29 20:44:31 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman650ba8e2005-04-22 00:00:37 +00007//
John Criswell09344dc2003-10-20 17:47:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnerde4aa4c2002-12-23 23:50:16 +00009//
10// This file contains code used to execute the program utilizing one of the
Gabor Greif0e535c3c2007-07-04 21:55:50 +000011// various ways of running LLVM bitcode.
Chris Lattnerde4aa4c2002-12-23 23:50:16 +000012//
13//===----------------------------------------------------------------------===//
14
Chris Lattnerde4aa4c2002-12-23 23:50:16 +000015#include "BugDriver.h"
Chris Lattnerffac2862006-06-06 22:30:59 +000016#include "ToolRunner.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000017#include "llvm/Support/CommandLine.h"
18#include "llvm/Support/Debug.h"
19#include "llvm/Support/FileUtilities.h"
Davide Italiano8a5d0432015-10-14 19:48:01 +000020#include "llvm/Support/Program.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000021#include "llvm/Support/SystemUtils.h"
Chris Lattnerc521f542009-08-23 22:45:37 +000022#include "llvm/Support/raw_ostream.h"
Chris Lattnerde4aa4c2002-12-23 23:50:16 +000023#include <fstream>
Reid Spencerc3065b72006-06-06 00:00:42 +000024
Brian Gaeke960707c2003-11-11 22:41:34 +000025using namespace llvm;
26
Chris Lattnerde4aa4c2002-12-23 23:50:16 +000027namespace {
Justin Bogner8d0a0812016-09-02 01:21:37 +000028// OutputType - Allow the user to specify the way code should be run, to test
29// for miscompilation.
30//
31enum OutputType {
32 AutoPick,
33 RunLLI,
34 RunJIT,
35 RunLLC,
36 RunLLCIA,
37 LLC_Safe,
38 CompileCustom,
39 Custom
40};
Misha Brukman5bc6a8f2003-09-29 22:40:52 +000041
Justin Bogner8d0a0812016-09-02 01:21:37 +000042cl::opt<double> AbsTolerance("abs-tolerance",
43 cl::desc("Absolute error tolerated"),
44 cl::init(0.0));
45cl::opt<double> RelTolerance("rel-tolerance",
46 cl::desc("Relative error tolerated"),
47 cl::init(0.0));
Chris Lattnerece10a42005-01-23 03:45:26 +000048
Justin Bogner8d0a0812016-09-02 01:21:37 +000049cl::opt<OutputType> InterpreterSel(
50 cl::desc("Specify the \"test\" i.e. suspect back-end:"),
51 cl::values(clEnumValN(AutoPick, "auto", "Use best guess"),
52 clEnumValN(RunLLI, "run-int", "Execute with the interpreter"),
53 clEnumValN(RunJIT, "run-jit", "Execute with JIT"),
54 clEnumValN(RunLLC, "run-llc", "Compile with LLC"),
55 clEnumValN(RunLLCIA, "run-llc-ia",
56 "Compile with LLC with integrated assembler"),
57 clEnumValN(LLC_Safe, "llc-safe", "Use LLC for all"),
58 clEnumValN(CompileCustom, "compile-custom",
59 "Use -compile-command to define a command to "
60 "compile the bitcode. Useful to avoid linking."),
61 clEnumValN(Custom, "run-custom",
62 "Use -exec-command to define a command to execute "
63 "the bitcode. Useful for cross-compilation."),
64 clEnumValEnd),
65 cl::init(AutoPick));
Chris Lattner68efaa72003-04-23 20:31:37 +000066
Justin Bogner8d0a0812016-09-02 01:21:37 +000067cl::opt<OutputType> SafeInterpreterSel(
68 cl::desc("Specify \"safe\" i.e. known-good backend:"),
69 cl::values(clEnumValN(AutoPick, "safe-auto", "Use best guess"),
70 clEnumValN(RunLLC, "safe-run-llc", "Compile with LLC"),
71 clEnumValN(Custom, "safe-run-custom",
72 "Use -exec-command to define a command to execute "
73 "the bitcode. Useful for cross-compilation."),
74 clEnumValEnd),
75 cl::init(AutoPick));
Dan Gohman414cf502008-12-08 04:02:47 +000076
Justin Bogner8d0a0812016-09-02 01:21:37 +000077cl::opt<std::string> SafeInterpreterPath(
78 "safe-path", cl::desc("Specify the path to the \"safe\" backend program"),
79 cl::init(""));
Dan Gohman414cf502008-12-08 04:02:47 +000080
Justin Bogner8d0a0812016-09-02 01:21:37 +000081cl::opt<bool> AppendProgramExitCode(
82 "append-exit-code",
83 cl::desc("Append the exit code to the output so it gets diff'd too"),
84 cl::init(false));
Reid Spencerd077fe72006-11-28 07:04:10 +000085
Justin Bogner8d0a0812016-09-02 01:21:37 +000086cl::opt<std::string>
87 InputFile("input", cl::init("/dev/null"),
88 cl::desc("Filename to pipe in as stdin (default: /dev/null)"));
Chris Lattnerdc92fa62003-10-14 22:24:31 +000089
Justin Bogner8d0a0812016-09-02 01:21:37 +000090cl::list<std::string>
91 AdditionalSOs("additional-so", cl::desc("Additional shared objects to load "
92 "into executing programs"));
Chris Lattner4e0969b2004-07-24 07:53:26 +000093
Justin Bogner8d0a0812016-09-02 01:21:37 +000094cl::list<std::string> AdditionalLinkerArgs(
95 "Xlinker", cl::desc("Additional arguments to pass to the linker"));
Anton Korobeynikovc53565c2008-04-28 20:53:48 +000096
Justin Bogner8d0a0812016-09-02 01:21:37 +000097cl::opt<std::string> CustomCompileCommand(
98 "compile-command", cl::init("llc"),
99 cl::desc("Command to compile the bitcode (use with -compile-custom) "
100 "(default: llc)"));
Andrew Trick8665d592011-02-08 18:20:48 +0000101
Justin Bogner8d0a0812016-09-02 01:21:37 +0000102cl::opt<std::string> CustomExecCommand(
103 "exec-command", cl::init("simulate"),
104 cl::desc("Command to execute the bitcode (use with -run-custom) "
105 "(default: simulate)"));
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000106}
107
Brian Gaeke960707c2003-11-11 22:41:34 +0000108namespace llvm {
Justin Bogner8d0a0812016-09-02 01:21:37 +0000109// Anything specified after the --args option are taken as arguments to the
110// program being debugged.
111cl::list<std::string> InputArgv("args", cl::Positional,
112 cl::desc("<program arguments>..."),
113 cl::ZeroOrMore, cl::PositionalEatsArgs);
Daniel Dunbara53337f2009-09-07 19:26:11 +0000114
Justin Bogner8d0a0812016-09-02 01:21:37 +0000115cl::opt<std::string>
116 OutputPrefix("output-prefix", cl::init("bugpoint"),
117 cl::desc("Prefix to use for outputs (default: 'bugpoint')"));
Dan Gohman414cf502008-12-08 04:02:47 +0000118}
Brian Gaeke4a278f02004-05-04 21:09:16 +0000119
Dan Gohman414cf502008-12-08 04:02:47 +0000120namespace {
Justin Bogner8d0a0812016-09-02 01:21:37 +0000121cl::list<std::string> ToolArgv("tool-args", cl::Positional,
122 cl::desc("<tool arguments>..."), cl::ZeroOrMore,
123 cl::PositionalEatsArgs);
Dan Gohman414cf502008-12-08 04:02:47 +0000124
Justin Bogner8d0a0812016-09-02 01:21:37 +0000125cl::list<std::string> SafeToolArgv("safe-tool-args", cl::Positional,
126 cl::desc("<safe-tool arguments>..."),
127 cl::ZeroOrMore, cl::PositionalEatsArgs);
Bill Wendlingbf5d8272009-03-02 23:13:18 +0000128
Justin Bogner8d0a0812016-09-02 01:21:37 +0000129cl::opt<std::string> CCBinary("gcc", cl::init(""),
130 cl::desc("The gcc binary to use."));
Kalle Raiskila6be58292010-05-10 07:38:37 +0000131
Justin Bogner8d0a0812016-09-02 01:21:37 +0000132cl::list<std::string> CCToolArgv("gcc-tool-args", cl::Positional,
133 cl::desc("<gcc-tool arguments>..."),
134 cl::ZeroOrMore, cl::PositionalEatsArgs);
Chris Lattner2f1aa112004-01-14 03:38:37 +0000135}
Misha Brukman40feb362003-07-30 20:15:44 +0000136
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000137//===----------------------------------------------------------------------===//
138// BugDriver method implementation
139//
140
141/// initializeExecutionEnvironment - This method is used to set up the
142/// environment for executing LLVM programs.
143///
Justin Bogner1c039152016-09-06 17:18:22 +0000144Error BugDriver::initializeExecutionEnvironment() {
Dan Gohmanee051522009-07-16 15:30:09 +0000145 outs() << "Initializing execution environment: ";
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000146
Misha Brukman5bc6a8f2003-09-29 22:40:52 +0000147 // Create an instance of the AbstractInterpreter interface as specified on
148 // the command line
Craig Toppere6cb63e2014-04-25 04:24:47 +0000149 SafeInterpreter = nullptr;
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000150 std::string Message;
Brian Gaeke4a278f02004-05-04 21:09:16 +0000151
Davide Italianoab256212015-10-14 20:29:54 +0000152 if (CCBinary.empty()) {
Davide Italiano8a5d0432015-10-14 19:48:01 +0000153 if (sys::findProgramByName("clang"))
Davide Italianoab256212015-10-14 20:29:54 +0000154 CCBinary = "clang";
Davide Italiano8a5d0432015-10-14 19:48:01 +0000155 else
Davide Italianoab256212015-10-14 20:29:54 +0000156 CCBinary = "gcc";
Davide Italiano8a5d0432015-10-14 19:48:01 +0000157 }
158
Chris Lattner7709ec52003-05-03 03:19:41 +0000159 switch (InterpreterSel) {
Brian Gaeke9a0bdb12003-10-21 17:41:35 +0000160 case AutoPick:
Brian Gaeke9a0bdb12003-10-21 17:41:35 +0000161 if (!Interpreter) {
162 InterpreterSel = RunJIT;
Justin Bogner8d0a0812016-09-02 01:21:37 +0000163 Interpreter =
164 AbstractInterpreter::createJIT(getToolName(), Message, &ToolArgv);
Brian Gaeke9a0bdb12003-10-21 17:41:35 +0000165 }
166 if (!Interpreter) {
167 InterpreterSel = RunLLC;
Justin Bogner8d0a0812016-09-02 01:21:37 +0000168 Interpreter = AbstractInterpreter::createLLC(
169 getToolName(), Message, CCBinary, &ToolArgv, &CCToolArgv);
Brian Gaeke9a0bdb12003-10-21 17:41:35 +0000170 }
171 if (!Interpreter) {
172 InterpreterSel = RunLLI;
Justin Bogner8d0a0812016-09-02 01:21:37 +0000173 Interpreter =
174 AbstractInterpreter::createLLI(getToolName(), Message, &ToolArgv);
Brian Gaeke9a0bdb12003-10-21 17:41:35 +0000175 }
176 if (!Interpreter) {
177 InterpreterSel = AutoPick;
178 Message = "Sorry, I can't automatically select an interpreter!\n";
179 }
180 break;
Chris Lattner3f6e5222003-10-14 21:59:36 +0000181 case RunLLI:
Justin Bogner8d0a0812016-09-02 01:21:37 +0000182 Interpreter =
183 AbstractInterpreter::createLLI(getToolName(), Message, &ToolArgv);
Chris Lattner3f6e5222003-10-14 21:59:36 +0000184 break;
185 case RunLLC:
Chris Lattnerfd381322010-03-16 06:41:47 +0000186 case RunLLCIA:
Dan Gohman414cf502008-12-08 04:02:47 +0000187 case LLC_Safe:
Justin Bogner8d0a0812016-09-02 01:21:37 +0000188 Interpreter = AbstractInterpreter::createLLC(
189 getToolName(), Message, CCBinary, &ToolArgv, &CCToolArgv,
190 InterpreterSel == RunLLCIA);
Chris Lattner3f6e5222003-10-14 21:59:36 +0000191 break;
192 case RunJIT:
Justin Bogner8d0a0812016-09-02 01:21:37 +0000193 Interpreter =
194 AbstractInterpreter::createJIT(getToolName(), Message, &ToolArgv);
Chris Lattner3f6e5222003-10-14 21:59:36 +0000195 break;
Andrew Trick8665d592011-02-08 18:20:48 +0000196 case CompileCustom:
Justin Bogner8d0a0812016-09-02 01:21:37 +0000197 Interpreter = AbstractInterpreter::createCustomCompiler(
198 Message, CustomCompileCommand);
Andrew Trick8665d592011-02-08 18:20:48 +0000199 break;
Anton Korobeynikovc53565c2008-04-28 20:53:48 +0000200 case Custom:
Andrew Trick8665d592011-02-08 18:20:48 +0000201 Interpreter =
Justin Bogner8d0a0812016-09-02 01:21:37 +0000202 AbstractInterpreter::createCustomExecutor(Message, CustomExecCommand);
Anton Korobeynikovc53565c2008-04-28 20:53:48 +0000203 break;
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000204 }
Matthijs Kooijman3bb12762008-06-12 13:09:43 +0000205 if (!Interpreter)
Dan Gohmand8db3762009-07-15 16:35:29 +0000206 errs() << Message;
Matthijs Kooijman3bb12762008-06-12 13:09:43 +0000207 else // Display informational messages on stdout instead of stderr
Dan Gohmanee051522009-07-16 15:30:09 +0000208 outs() << Message;
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000209
Dan Gohman414cf502008-12-08 04:02:47 +0000210 std::string Path = SafeInterpreterPath;
211 if (Path.empty())
212 Path = getToolName();
213 std::vector<std::string> SafeToolArgs = SafeToolArgv;
214 switch (SafeInterpreterSel) {
215 case AutoPick:
Dan Gohman414cf502008-12-08 04:02:47 +0000216 // In "llc-safe" mode, default to using LLC as the "safe" backend.
Justin Bogner8d0a0812016-09-02 01:21:37 +0000217 if (!SafeInterpreter && InterpreterSel == LLC_Safe) {
Dan Gohman414cf502008-12-08 04:02:47 +0000218 SafeInterpreterSel = RunLLC;
219 SafeToolArgs.push_back("--relocation-model=pic");
Justin Bogner8d0a0812016-09-02 01:21:37 +0000220 SafeInterpreter = AbstractInterpreter::createLLC(
221 Path.c_str(), Message, CCBinary, &SafeToolArgs, &CCToolArgv);
Dan Gohman414cf502008-12-08 04:02:47 +0000222 }
223
Justin Bogner8d0a0812016-09-02 01:21:37 +0000224 if (!SafeInterpreter && InterpreterSel != RunLLC &&
Dan Gohman414cf502008-12-08 04:02:47 +0000225 InterpreterSel != RunJIT) {
226 SafeInterpreterSel = RunLLC;
227 SafeToolArgs.push_back("--relocation-model=pic");
Justin Bogner8d0a0812016-09-02 01:21:37 +0000228 SafeInterpreter = AbstractInterpreter::createLLC(
229 Path.c_str(), Message, CCBinary, &SafeToolArgs, &CCToolArgv);
Dan Gohman414cf502008-12-08 04:02:47 +0000230 }
231 if (!SafeInterpreter) {
232 SafeInterpreterSel = AutoPick;
Saleem Abdulrasool5f8609b2013-01-24 16:49:14 +0000233 Message = "Sorry, I can't automatically select a safe interpreter!\n";
Dan Gohman414cf502008-12-08 04:02:47 +0000234 }
235 break;
236 case RunLLC:
Chris Lattnerfd381322010-03-16 06:41:47 +0000237 case RunLLCIA:
Dan Gohman414cf502008-12-08 04:02:47 +0000238 SafeToolArgs.push_back("--relocation-model=pic");
Justin Bogner8d0a0812016-09-02 01:21:37 +0000239 SafeInterpreter = AbstractInterpreter::createLLC(
240 Path.c_str(), Message, CCBinary, &SafeToolArgs, &CCToolArgv,
241 SafeInterpreterSel == RunLLCIA);
Dan Gohman414cf502008-12-08 04:02:47 +0000242 break;
Dan Gohman414cf502008-12-08 04:02:47 +0000243 case Custom:
Andrew Trick8665d592011-02-08 18:20:48 +0000244 SafeInterpreter =
Justin Bogner8d0a0812016-09-02 01:21:37 +0000245 AbstractInterpreter::createCustomExecutor(Message, CustomExecCommand);
Dan Gohman414cf502008-12-08 04:02:47 +0000246 break;
247 default:
248 Message = "Sorry, this back-end is not supported by bugpoint as the "
249 "\"safe\" backend right now!\n";
250 break;
Chris Lattner898de4a2004-02-18 20:52:02 +0000251 }
Justin Bogner8d0a0812016-09-02 01:21:37 +0000252 if (!SafeInterpreter) {
253 outs() << Message << "\nExiting.\n";
254 exit(1);
255 }
Andrew Trick69a963e2011-02-08 18:07:10 +0000256
Davide Italianoab256212015-10-14 20:29:54 +0000257 cc = CC::create(Message, CCBinary, &CCToolArgv);
Justin Bogner8d0a0812016-09-02 01:21:37 +0000258 if (!cc) {
259 outs() << Message << "\nExiting.\n";
260 exit(1);
261 }
Misha Brukman0fd31722003-07-24 21:59:10 +0000262
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000263 // If there was an error creating the selected interpreter, quit with error.
Justin Bogner1c039152016-09-06 17:18:22 +0000264 if (Interpreter == nullptr)
265 return make_error<StringError>("Failed to init execution environment",
266 inconvertibleErrorCode());
267 return Error::success();
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000268}
269
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000270/// compileProgram - Try to compile the specified module, returning false and
271/// setting Error if an error occurs. This is used for code generation
272/// crash testing.
Chris Lattner96d41dd2004-02-18 23:25:22 +0000273///
Justin Bogner1c039152016-09-06 17:18:22 +0000274Error BugDriver::compileProgram(Module *M) const {
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000275 // Emit the program to a bitcode file...
Rafael Espindolabecba2b2013-06-18 16:14:09 +0000276 SmallString<128> BitcodeFile;
277 int BitcodeFD;
Rafael Espindola4453e42942014-06-13 03:07:50 +0000278 std::error_code EC = sys::fs::createUniqueFile(
Rafael Espindolabecba2b2013-06-18 16:14:09 +0000279 OutputPrefix + "-test-program-%%%%%%%.bc", BitcodeFD, BitcodeFile);
280 if (EC) {
281 errs() << ToolName << ": Error making unique filename: " << EC.message()
Dan Gohmand8db3762009-07-15 16:35:29 +0000282 << "\n";
Reid Spencere4ca7222006-08-23 20:34:57 +0000283 exit(1);
284 }
Rafael Espindolabecba2b2013-06-18 16:14:09 +0000285 if (writeProgramToFile(BitcodeFile.str(), BitcodeFD, M)) {
286 errs() << ToolName << ": Error emitting bitcode to file '" << BitcodeFile
287 << "'!\n";
Chris Lattner96d41dd2004-02-18 23:25:22 +0000288 exit(1);
289 }
290
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000291 // Remove the temporary bitcode file when we are done.
Michael J. Spencerc60223e2011-03-31 13:04:19 +0000292 FileRemover BitcodeFileRemover(BitcodeFile.str(), !SaveTemps);
Chris Lattner96d41dd2004-02-18 23:25:22 +0000293
294 // Actually compile the program!
Justin Bogner1c039152016-09-06 17:18:22 +0000295 return Interpreter->compileProgram(BitcodeFile.str(), Timeout, MemoryLimit);
Chris Lattner96d41dd2004-02-18 23:25:22 +0000296}
297
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000298/// executeProgram - This method runs "Program", capturing the output of the
299/// program to a file, returning the filename of the file. A recommended
300/// filename may be optionally specified.
301///
Justin Bogner1c039152016-09-06 17:18:22 +0000302Expected<std::string> BugDriver::executeProgram(const Module *Program,
303 std::string OutputFile,
304 std::string BitcodeFile,
305 const std::string &SharedObj,
306 AbstractInterpreter *AI) const {
Justin Bogner8d0a0812016-09-02 01:21:37 +0000307 if (!AI)
308 AI = Interpreter;
Chris Lattner3f6e5222003-10-14 21:59:36 +0000309 assert(AI && "Interpreter should have been created already!");
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000310 bool CreatedBitcode = false;
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000311 if (BitcodeFile.empty()) {
312 // Emit the program to a bitcode file...
Rafael Espindolabecba2b2013-06-18 16:14:09 +0000313 SmallString<128> UniqueFilename;
314 int UniqueFD;
Rafael Espindola4453e42942014-06-13 03:07:50 +0000315 std::error_code EC = sys::fs::createUniqueFile(
Rafael Espindolabecba2b2013-06-18 16:14:09 +0000316 OutputPrefix + "-test-program-%%%%%%%.bc", UniqueFD, UniqueFilename);
317 if (EC) {
Justin Bogner8d0a0812016-09-02 01:21:37 +0000318 errs() << ToolName << ": Error making unique filename: " << EC.message()
319 << "!\n";
Reid Spencere4ca7222006-08-23 20:34:57 +0000320 exit(1);
321 }
Rafael Espindolabecba2b2013-06-18 16:14:09 +0000322 BitcodeFile = UniqueFilename.str();
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000323
Rafael Espindolabecba2b2013-06-18 16:14:09 +0000324 if (writeProgramToFile(BitcodeFile, UniqueFD, Program)) {
Justin Bogner8d0a0812016-09-02 01:21:37 +0000325 errs() << ToolName << ": Error emitting bitcode to file '" << BitcodeFile
326 << "'!\n";
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000327 exit(1);
328 }
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000329 CreatedBitcode = true;
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000330 }
331
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000332 // Remove the temporary bitcode file when we are done.
Rafael Espindolabecba2b2013-06-18 16:14:09 +0000333 std::string BitcodePath(BitcodeFile);
Justin Bogner8d0a0812016-09-02 01:21:37 +0000334 FileRemover BitcodeFileRemover(BitcodePath, CreatedBitcode && !SaveTemps);
Chris Lattner1f80a922004-02-18 22:01:21 +0000335
Justin Bogner8d0a0812016-09-02 01:21:37 +0000336 if (OutputFile.empty())
337 OutputFile = OutputPrefix + "-execution-output-%%%%%%%";
Misha Brukmand792c9b2003-07-24 18:17:43 +0000338
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000339 // Check to see if this is a valid output filename...
Rafael Espindolabecba2b2013-06-18 16:14:09 +0000340 SmallString<128> UniqueFile;
Rafael Espindola4453e42942014-06-13 03:07:50 +0000341 std::error_code EC = sys::fs::createUniqueFile(OutputFile, UniqueFile);
Rafael Espindolabecba2b2013-06-18 16:14:09 +0000342 if (EC) {
Justin Bogner8d0a0812016-09-02 01:21:37 +0000343 errs() << ToolName << ": Error making unique filename: " << EC.message()
344 << "\n";
Reid Spencere4ca7222006-08-23 20:34:57 +0000345 exit(1);
346 }
Rafael Espindolabecba2b2013-06-18 16:14:09 +0000347 OutputFile = UniqueFile.str();
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000348
Chris Lattner3f6e5222003-10-14 21:59:36 +0000349 // Figure out which shared objects to run, if any.
Chris Lattnerdc92fa62003-10-14 22:24:31 +0000350 std::vector<std::string> SharedObjs(AdditionalSOs);
Chris Lattner3f6e5222003-10-14 21:59:36 +0000351 if (!SharedObj.empty())
352 SharedObjs.push_back(SharedObj);
353
Justin Bogner1c039152016-09-06 17:18:22 +0000354 Expected<int> RetVal = AI->ExecuteProgram(BitcodeFile, InputArgv, InputFile,
355 OutputFile, AdditionalLinkerArgs,
356 SharedObjs, Timeout, MemoryLimit);
357 if (Error E = RetVal.takeError())
358 return std::move(E);
Chris Lattner4e0969b2004-07-24 07:53:26 +0000359
Justin Bogner1c039152016-09-06 17:18:22 +0000360 if (*RetVal == -1) {
Dan Gohmand8db3762009-07-15 16:35:29 +0000361 errs() << "<timeout>";
Chris Lattner4e0969b2004-07-24 07:53:26 +0000362 static bool FirstTimeout = true;
363 if (FirstTimeout) {
Justin Bogner8d0a0812016-09-02 01:21:37 +0000364 outs()
365 << "\n"
366 "*** Program execution timed out! This mechanism is designed to "
367 "handle\n"
368 " programs stuck in infinite loops gracefully. The -timeout "
369 "option\n"
370 " can be used to change the timeout threshold or disable it "
371 "completely\n"
372 " (with -timeout=0). This message is only displayed once.\n";
Chris Lattner4e0969b2004-07-24 07:53:26 +0000373 FirstTimeout = false;
374 }
375 }
Chris Lattner3f6e5222003-10-14 21:59:36 +0000376
Reid Spencerd077fe72006-11-28 07:04:10 +0000377 if (AppendProgramExitCode) {
378 std::ofstream outFile(OutputFile.c_str(), std::ios_base::app);
Justin Bogner1c039152016-09-06 17:18:22 +0000379 outFile << "exit " << *RetVal << '\n';
Reid Spencerd077fe72006-11-28 07:04:10 +0000380 outFile.close();
381 }
382
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000383 // Return the filename we captured the output to.
384 return OutputFile;
385}
386
Dan Gohman414cf502008-12-08 04:02:47 +0000387/// executeProgramSafely - Used to create reference output with the "safe"
Brian Gaeke35145be2004-02-11 18:37:32 +0000388/// backend, if reference output is not provided.
389///
Justin Bogner1c039152016-09-06 17:18:22 +0000390Expected<std::string>
391BugDriver::executeProgramSafely(const Module *Program,
392 const std::string &OutputFile) const {
393 return executeProgram(Program, OutputFile, "", "", SafeInterpreter);
Brian Gaeke35145be2004-02-11 18:37:32 +0000394}
Misha Brukmand792c9b2003-07-24 18:17:43 +0000395
Justin Bogner1c039152016-09-06 17:18:22 +0000396Expected<std::string>
397BugDriver::compileSharedObject(const std::string &BitcodeFile) {
Misha Brukmand792c9b2003-07-24 18:17:43 +0000398 assert(Interpreter && "Interpreter should have been created already!");
Rafael Espindola34889ca2013-06-17 19:21:38 +0000399 std::string OutputFile;
Misha Brukmand792c9b2003-07-24 18:17:43 +0000400
Dan Gohman414cf502008-12-08 04:02:47 +0000401 // Using the known-good backend.
Justin Bogner1c039152016-09-06 17:18:22 +0000402 Expected<CC::FileType> FT =
403 SafeInterpreter->OutputCode(BitcodeFile, OutputFile);
404 if (Error E = FT.takeError())
405 return std::move(E);
Misha Brukmand792c9b2003-07-24 18:17:43 +0000406
Chris Lattnerf8a84db2003-10-14 21:09:11 +0000407 std::string SharedObjectFile;
Justin Bogner1c039152016-09-06 17:18:22 +0000408 if (Error E = cc->MakeSharedObject(OutputFile, *FT, SharedObjectFile,
409 AdditionalLinkerArgs))
410 return std::move(E);
Misha Brukmand792c9b2003-07-24 18:17:43 +0000411
412 // Remove the intermediate C file
Rafael Espindola34889ca2013-06-17 19:21:38 +0000413 sys::fs::remove(OutputFile);
Misha Brukmand792c9b2003-07-24 18:17:43 +0000414
Rafael Espindolac32573b2014-03-14 15:13:35 +0000415 return SharedObjectFile;
Misha Brukmand792c9b2003-07-24 18:17:43 +0000416}
417
Patrick Jenkinsc46c0382006-08-15 16:40:49 +0000418/// createReferenceFile - calls compileProgram and then records the output
Andrew Trick69a963e2011-02-08 18:07:10 +0000419/// into ReferenceOutputFile. Returns true if reference file created, false
Patrick Jenkinsc46c0382006-08-15 16:40:49 +0000420/// otherwise. Note: initializeExecutionEnvironment should be called BEFORE
421/// this function.
422///
Justin Bogner1c039152016-09-06 17:18:22 +0000423Error BugDriver::createReferenceFile(Module *M, const std::string &Filename) {
424 if (Error E = compileProgram(Program))
425 return E;
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000426
Justin Bogner1c039152016-09-06 17:18:22 +0000427 Expected<std::string> Result = executeProgramSafely(Program, Filename);
428 if (Error E = Result.takeError()) {
Dan Gohman414cf502008-12-08 04:02:47 +0000429 if (Interpreter != SafeInterpreter) {
Justin Bogner1c039152016-09-06 17:18:22 +0000430 E = joinErrors(
431 std::move(E),
432 make_error<StringError>(
433 "*** There is a bug running the \"safe\" backend. Either"
434 " debug it (for example with the -run-jit bugpoint option,"
435 " if JIT is being used as the \"safe\" backend), or fix the"
436 " error some other way.\n",
437 inconvertibleErrorCode()));
Patrick Jenkinsc46c0382006-08-15 16:40:49 +0000438 }
Justin Bogner1c039152016-09-06 17:18:22 +0000439 return E;
Patrick Jenkinsc46c0382006-08-15 16:40:49 +0000440 }
Justin Bogner1c039152016-09-06 17:18:22 +0000441 ReferenceOutputFile = *Result;
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000442 outs() << "\nReference output is: " << ReferenceOutputFile << "\n\n";
Justin Bogner1c039152016-09-06 17:18:22 +0000443 return Error::success();
Patrick Jenkinsc46c0382006-08-15 16:40:49 +0000444}
Misha Brukmand792c9b2003-07-24 18:17:43 +0000445
Patrick Jenkinsc46c0382006-08-15 16:40:49 +0000446/// diffProgram - This method executes the specified module and diffs the
447/// output against the file specified by ReferenceOutputFile. If the output
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000448/// is different, 1 is returned. If there is a problem with the code
Andrew Trick55aeb552011-05-11 16:31:24 +0000449/// generator (e.g., llc crashes), this will set ErrMsg.
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000450///
Justin Bogner1c039152016-09-06 17:18:22 +0000451Expected<bool> BugDriver::diffProgram(const Module *Program,
452 const std::string &BitcodeFile,
453 const std::string &SharedObject,
454 bool RemoveBitcode) const {
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000455 // Execute the program, generating an output file...
Justin Bogner1c039152016-09-06 17:18:22 +0000456 Expected<std::string> Output =
457 executeProgram(Program, "", BitcodeFile, SharedObject, nullptr);
458 if (Error E = Output.takeError())
459 return std::move(E);
Brian Gaeke35145be2004-02-11 18:37:32 +0000460
Chris Lattneraa997fb2003-08-01 20:29:45 +0000461 std::string Error;
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000462 bool FilesDifferent = false;
Justin Bogner1c039152016-09-06 17:18:22 +0000463 if (int Diff = DiffFilesWithTolerance(ReferenceOutputFile, *Output,
Chris Lattnerece10a42005-01-23 03:45:26 +0000464 AbsTolerance, RelTolerance, &Error)) {
465 if (Diff == 2) {
Dan Gohmand8db3762009-07-15 16:35:29 +0000466 errs() << "While diffing output: " << Error << '\n';
Chris Lattneraa997fb2003-08-01 20:29:45 +0000467 exit(1);
468 }
469 FilesDifferent = true;
Justin Bogner8d0a0812016-09-02 01:21:37 +0000470 } else {
David Goodwin73f4e3f2009-07-10 21:39:28 +0000471 // Remove the generated output if there are no differences.
Justin Bogner1c039152016-09-06 17:18:22 +0000472 sys::fs::remove(*Output);
David Goodwin73f4e3f2009-07-10 21:39:28 +0000473 }
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000474
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000475 // Remove the bitcode file if we are supposed to.
476 if (RemoveBitcode)
Rafael Espindolabecba2b2013-06-18 16:14:09 +0000477 sys::fs::remove(BitcodeFile);
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000478 return FilesDifferent;
479}
Misha Brukman539f9592003-07-28 19:16:14 +0000480
Justin Bogner8d0a0812016-09-02 01:21:37 +0000481bool BugDriver::isExecutingJIT() { return InterpreterSel == RunJIT; }