blob: 695461e2d508a77ca3b8ee467867bab08c74a637 [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///
144bool 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.
Craig Toppere6cb63e2014-04-25 04:24:47 +0000264 return Interpreter == nullptr;
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000265}
266
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000267/// compileProgram - Try to compile the specified module, returning false and
268/// setting Error if an error occurs. This is used for code generation
269/// crash testing.
Chris Lattner96d41dd2004-02-18 23:25:22 +0000270///
Rafael Espindolad1c7ef42010-08-05 03:00:22 +0000271void BugDriver::compileProgram(Module *M, std::string *Error) const {
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000272 // Emit the program to a bitcode file...
Rafael Espindolabecba2b2013-06-18 16:14:09 +0000273 SmallString<128> BitcodeFile;
274 int BitcodeFD;
Rafael Espindola4453e42942014-06-13 03:07:50 +0000275 std::error_code EC = sys::fs::createUniqueFile(
Rafael Espindolabecba2b2013-06-18 16:14:09 +0000276 OutputPrefix + "-test-program-%%%%%%%.bc", BitcodeFD, BitcodeFile);
277 if (EC) {
278 errs() << ToolName << ": Error making unique filename: " << EC.message()
Dan Gohmand8db3762009-07-15 16:35:29 +0000279 << "\n";
Reid Spencere4ca7222006-08-23 20:34:57 +0000280 exit(1);
281 }
Rafael Espindolabecba2b2013-06-18 16:14:09 +0000282 if (writeProgramToFile(BitcodeFile.str(), BitcodeFD, M)) {
283 errs() << ToolName << ": Error emitting bitcode to file '" << BitcodeFile
284 << "'!\n";
Chris Lattner96d41dd2004-02-18 23:25:22 +0000285 exit(1);
286 }
287
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000288 // Remove the temporary bitcode file when we are done.
Michael J. Spencerc60223e2011-03-31 13:04:19 +0000289 FileRemover BitcodeFileRemover(BitcodeFile.str(), !SaveTemps);
Chris Lattner96d41dd2004-02-18 23:25:22 +0000290
291 // Actually compile the program!
Duncan Sandse9cd6d02010-05-24 07:49:55 +0000292 Interpreter->compileProgram(BitcodeFile.str(), Error, Timeout, MemoryLimit);
Chris Lattner96d41dd2004-02-18 23:25:22 +0000293}
294
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000295/// executeProgram - This method runs "Program", capturing the output of the
296/// program to a file, returning the filename of the file. A recommended
297/// filename may be optionally specified.
298///
Justin Bogner8d0a0812016-09-02 01:21:37 +0000299std::string
300BugDriver::executeProgram(const Module *Program, std::string OutputFile,
301 std::string BitcodeFile, const std::string &SharedObj,
302 AbstractInterpreter *AI, std::string *Error) const {
303 if (!AI)
304 AI = Interpreter;
Chris Lattner3f6e5222003-10-14 21:59:36 +0000305 assert(AI && "Interpreter should have been created already!");
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000306 bool CreatedBitcode = false;
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000307 if (BitcodeFile.empty()) {
308 // Emit the program to a bitcode file...
Rafael Espindolabecba2b2013-06-18 16:14:09 +0000309 SmallString<128> UniqueFilename;
310 int UniqueFD;
Rafael Espindola4453e42942014-06-13 03:07:50 +0000311 std::error_code EC = sys::fs::createUniqueFile(
Rafael Espindolabecba2b2013-06-18 16:14:09 +0000312 OutputPrefix + "-test-program-%%%%%%%.bc", UniqueFD, UniqueFilename);
313 if (EC) {
Justin Bogner8d0a0812016-09-02 01:21:37 +0000314 errs() << ToolName << ": Error making unique filename: " << EC.message()
315 << "!\n";
Reid Spencere4ca7222006-08-23 20:34:57 +0000316 exit(1);
317 }
Rafael Espindolabecba2b2013-06-18 16:14:09 +0000318 BitcodeFile = UniqueFilename.str();
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000319
Rafael Espindolabecba2b2013-06-18 16:14:09 +0000320 if (writeProgramToFile(BitcodeFile, UniqueFD, Program)) {
Justin Bogner8d0a0812016-09-02 01:21:37 +0000321 errs() << ToolName << ": Error emitting bitcode to file '" << BitcodeFile
322 << "'!\n";
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000323 exit(1);
324 }
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000325 CreatedBitcode = true;
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000326 }
327
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000328 // Remove the temporary bitcode file when we are done.
Rafael Espindolabecba2b2013-06-18 16:14:09 +0000329 std::string BitcodePath(BitcodeFile);
Justin Bogner8d0a0812016-09-02 01:21:37 +0000330 FileRemover BitcodeFileRemover(BitcodePath, CreatedBitcode && !SaveTemps);
Chris Lattner1f80a922004-02-18 22:01:21 +0000331
Justin Bogner8d0a0812016-09-02 01:21:37 +0000332 if (OutputFile.empty())
333 OutputFile = OutputPrefix + "-execution-output-%%%%%%%";
Misha Brukmand792c9b2003-07-24 18:17:43 +0000334
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000335 // Check to see if this is a valid output filename...
Rafael Espindolabecba2b2013-06-18 16:14:09 +0000336 SmallString<128> UniqueFile;
Rafael Espindola4453e42942014-06-13 03:07:50 +0000337 std::error_code EC = sys::fs::createUniqueFile(OutputFile, UniqueFile);
Rafael Espindolabecba2b2013-06-18 16:14:09 +0000338 if (EC) {
Justin Bogner8d0a0812016-09-02 01:21:37 +0000339 errs() << ToolName << ": Error making unique filename: " << EC.message()
340 << "\n";
Reid Spencere4ca7222006-08-23 20:34:57 +0000341 exit(1);
342 }
Rafael Espindolabecba2b2013-06-18 16:14:09 +0000343 OutputFile = UniqueFile.str();
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000344
Chris Lattner3f6e5222003-10-14 21:59:36 +0000345 // Figure out which shared objects to run, if any.
Chris Lattnerdc92fa62003-10-14 22:24:31 +0000346 std::vector<std::string> SharedObjs(AdditionalSOs);
Chris Lattner3f6e5222003-10-14 21:59:36 +0000347 if (!SharedObj.empty())
348 SharedObjs.push_back(SharedObj);
349
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000350 int RetVal = AI->ExecuteProgram(BitcodeFile, InputArgv, InputFile, OutputFile,
351 Error, AdditionalLinkerArgs, SharedObjs,
Dan Gohman414cf502008-12-08 04:02:47 +0000352 Timeout, MemoryLimit);
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000353 if (!Error->empty())
354 return OutputFile;
Chris Lattner4e0969b2004-07-24 07:53:26 +0000355
356 if (RetVal == -1) {
Dan Gohmand8db3762009-07-15 16:35:29 +0000357 errs() << "<timeout>";
Chris Lattner4e0969b2004-07-24 07:53:26 +0000358 static bool FirstTimeout = true;
359 if (FirstTimeout) {
Justin Bogner8d0a0812016-09-02 01:21:37 +0000360 outs()
361 << "\n"
362 "*** Program execution timed out! This mechanism is designed to "
363 "handle\n"
364 " programs stuck in infinite loops gracefully. The -timeout "
365 "option\n"
366 " can be used to change the timeout threshold or disable it "
367 "completely\n"
368 " (with -timeout=0). This message is only displayed once.\n";
Chris Lattner4e0969b2004-07-24 07:53:26 +0000369 FirstTimeout = false;
370 }
371 }
Chris Lattner3f6e5222003-10-14 21:59:36 +0000372
Reid Spencerd077fe72006-11-28 07:04:10 +0000373 if (AppendProgramExitCode) {
374 std::ofstream outFile(OutputFile.c_str(), std::ios_base::app);
375 outFile << "exit " << RetVal << '\n';
376 outFile.close();
377 }
378
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000379 // Return the filename we captured the output to.
380 return OutputFile;
381}
382
Dan Gohman414cf502008-12-08 04:02:47 +0000383/// executeProgramSafely - Used to create reference output with the "safe"
Brian Gaeke35145be2004-02-11 18:37:32 +0000384/// backend, if reference output is not provided.
385///
Rafael Espindolac89b1ef2010-07-30 14:19:00 +0000386std::string BugDriver::executeProgramSafely(const Module *Program,
Benjamin Kramerc321e532016-06-08 19:09:22 +0000387 const std::string &OutputFile,
Rafael Espindolae4904602010-07-31 14:34:49 +0000388 std::string *Error) const {
Rafael Espindolac89b1ef2010-07-30 14:19:00 +0000389 return executeProgram(Program, OutputFile, "", "", SafeInterpreter, Error);
Brian Gaeke35145be2004-02-11 18:37:32 +0000390}
Misha Brukmand792c9b2003-07-24 18:17:43 +0000391
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000392std::string BugDriver::compileSharedObject(const std::string &BitcodeFile,
393 std::string &Error) {
Misha Brukmand792c9b2003-07-24 18:17:43 +0000394 assert(Interpreter && "Interpreter should have been created already!");
Rafael Espindola34889ca2013-06-17 19:21:38 +0000395 std::string OutputFile;
Misha Brukmand792c9b2003-07-24 18:17:43 +0000396
Dan Gohman414cf502008-12-08 04:02:47 +0000397 // Using the known-good backend.
Justin Bogner8d0a0812016-09-02 01:21:37 +0000398 CC::FileType FT = SafeInterpreter->OutputCode(BitcodeFile, OutputFile, Error);
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000399 if (!Error.empty())
400 return "";
Misha Brukmand792c9b2003-07-24 18:17:43 +0000401
Chris Lattnerf8a84db2003-10-14 21:09:11 +0000402 std::string SharedObjectFile;
Davide Italianoab256212015-10-14 20:29:54 +0000403 bool Failure = cc->MakeSharedObject(OutputFile, FT, SharedObjectFile,
Justin Bogner8d0a0812016-09-02 01:21:37 +0000404 AdditionalLinkerArgs, Error);
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000405 if (!Error.empty())
406 return "";
407 if (Failure)
Chris Lattnerf8a84db2003-10-14 21:09:11 +0000408 exit(1);
Misha Brukmand792c9b2003-07-24 18:17:43 +0000409
410 // Remove the intermediate C file
Rafael Espindola34889ca2013-06-17 19:21:38 +0000411 sys::fs::remove(OutputFile);
Misha Brukmand792c9b2003-07-24 18:17:43 +0000412
Rafael Espindolac32573b2014-03-14 15:13:35 +0000413 return SharedObjectFile;
Misha Brukmand792c9b2003-07-24 18:17:43 +0000414}
415
Patrick Jenkinsc46c0382006-08-15 16:40:49 +0000416/// createReferenceFile - calls compileProgram and then records the output
Andrew Trick69a963e2011-02-08 18:07:10 +0000417/// into ReferenceOutputFile. Returns true if reference file created, false
Patrick Jenkinsc46c0382006-08-15 16:40:49 +0000418/// otherwise. Note: initializeExecutionEnvironment should be called BEFORE
419/// this function.
420///
Chris Lattner634bc042006-09-15 21:29:15 +0000421bool BugDriver::createReferenceFile(Module *M, const std::string &Filename) {
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000422 std::string Error;
423 compileProgram(Program, &Error);
424 if (!Error.empty())
Patrick Jenkinsc46c0382006-08-15 16:40:49 +0000425 return false;
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000426
Rafael Espindolac89b1ef2010-07-30 14:19:00 +0000427 ReferenceOutputFile = executeProgramSafely(Program, Filename, &Error);
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000428 if (!Error.empty()) {
429 errs() << Error;
Dan Gohman414cf502008-12-08 04:02:47 +0000430 if (Interpreter != SafeInterpreter) {
Dan Gohmand8db3762009-07-15 16:35:29 +0000431 errs() << "*** There is a bug running the \"safe\" backend. Either"
Eric Christopher64a23232012-03-23 05:50:46 +0000432 << " debug it (for example with the -run-jit bugpoint option,"
433 << " if JIT is being used as the \"safe\" backend), or fix the"
Dan Gohmand8db3762009-07-15 16:35:29 +0000434 << " error some other way.\n";
Patrick Jenkinsc46c0382006-08-15 16:40:49 +0000435 }
436 return false;
437 }
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000438 outs() << "\nReference output is: " << ReferenceOutputFile << "\n\n";
Patrick Jenkinsc46c0382006-08-15 16:40:49 +0000439 return true;
440}
Misha Brukmand792c9b2003-07-24 18:17:43 +0000441
Patrick Jenkinsc46c0382006-08-15 16:40:49 +0000442/// diffProgram - This method executes the specified module and diffs the
443/// output against the file specified by ReferenceOutputFile. If the output
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000444/// is different, 1 is returned. If there is a problem with the code
Andrew Trick55aeb552011-05-11 16:31:24 +0000445/// generator (e.g., llc crashes), this will set ErrMsg.
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000446///
Rafael Espindolac89b1ef2010-07-30 14:19:00 +0000447bool BugDriver::diffProgram(const Module *Program,
448 const std::string &BitcodeFile,
Justin Bogner8d0a0812016-09-02 01:21:37 +0000449 const std::string &SharedObject, bool RemoveBitcode,
Rafael Espindolae4904602010-07-31 14:34:49 +0000450 std::string *ErrMsg) const {
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000451 // Execute the program, generating an output file...
Rafael Espindolabecba2b2013-06-18 16:14:09 +0000452 std::string Output(
Craig Toppere6cb63e2014-04-25 04:24:47 +0000453 executeProgram(Program, "", BitcodeFile, SharedObject, nullptr, ErrMsg));
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000454 if (!ErrMsg->empty())
455 return false;
Brian Gaeke35145be2004-02-11 18:37:32 +0000456
Chris Lattneraa997fb2003-08-01 20:29:45 +0000457 std::string Error;
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000458 bool FilesDifferent = false;
Justin Bogner8d0a0812016-09-02 01:21:37 +0000459 if (int Diff = DiffFilesWithTolerance(ReferenceOutputFile, Output,
Chris Lattnerece10a42005-01-23 03:45:26 +0000460 AbsTolerance, RelTolerance, &Error)) {
461 if (Diff == 2) {
Dan Gohmand8db3762009-07-15 16:35:29 +0000462 errs() << "While diffing output: " << Error << '\n';
Chris Lattneraa997fb2003-08-01 20:29:45 +0000463 exit(1);
464 }
465 FilesDifferent = true;
Justin Bogner8d0a0812016-09-02 01:21:37 +0000466 } else {
David Goodwin73f4e3f2009-07-10 21:39:28 +0000467 // Remove the generated output if there are no differences.
Rafael Espindolabecba2b2013-06-18 16:14:09 +0000468 sys::fs::remove(Output);
David Goodwin73f4e3f2009-07-10 21:39:28 +0000469 }
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000470
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000471 // Remove the bitcode file if we are supposed to.
472 if (RemoveBitcode)
Rafael Espindolabecba2b2013-06-18 16:14:09 +0000473 sys::fs::remove(BitcodeFile);
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000474 return FilesDifferent;
475}
Misha Brukman539f9592003-07-28 19:16:14 +0000476
Justin Bogner8d0a0812016-09-02 01:21:37 +0000477bool BugDriver::isExecutingJIT() { return InterpreterSel == RunJIT; }