blob: 3d3dac3274d151bc5ea9e5bd45b07075fc77f53f [file] [log] [blame]
Chris Lattner4a106452002-12-23 23:50:16 +00001//===- ExecutionDriver.cpp - Allow execution of LLVM program --------------===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
John Criswell7c0e0222003-10-20 17:47:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner21c62da2007-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 Brukman3da94ae2005-04-22 00:00:37 +00007//
John Criswell7c0e0222003-10-20 17:47:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner4a106452002-12-23 23:50:16 +00009//
10// This file contains code used to execute the program utilizing one of the
Gabor Greif8ff70c22007-07-04 21:55:50 +000011// various ways of running LLVM bitcode.
Chris Lattner4a106452002-12-23 23:50:16 +000012//
13//===----------------------------------------------------------------------===//
14
Chris Lattner4a106452002-12-23 23:50:16 +000015#include "BugDriver.h"
Chris Lattnerf1b20d82006-06-06 22:30:59 +000016#include "ToolRunner.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000017#include "llvm/Support/CommandLine.h"
18#include "llvm/Support/Debug.h"
19#include "llvm/Support/FileUtilities.h"
20#include "llvm/Support/SystemUtils.h"
Chris Lattner74382b72009-08-23 22:45:37 +000021#include "llvm/Support/raw_ostream.h"
Chris Lattner4a106452002-12-23 23:50:16 +000022#include <fstream>
Reid Spencer51ab5c82006-06-06 00:00:42 +000023
Brian Gaeked0fde302003-11-11 22:41:34 +000024using namespace llvm;
25
Chris Lattner4a106452002-12-23 23:50:16 +000026namespace {
27 // OutputType - Allow the user to specify the way code should be run, to test
28 // for miscompilation.
29 //
30 enum OutputType {
Eric Christophera443e5b2012-03-23 05:50:46 +000031 AutoPick, RunLLI, RunJIT, RunLLC, RunLLCIA, LLC_Safe, CompileCustom, Custom
Chris Lattner4a106452002-12-23 23:50:16 +000032 };
Misha Brukman41485562003-09-29 22:40:52 +000033
Chris Lattnera328c512005-01-23 03:45:26 +000034 cl::opt<double>
35 AbsTolerance("abs-tolerance", cl::desc("Absolute error tolerated"),
36 cl::init(0.0));
37 cl::opt<double>
38 RelTolerance("rel-tolerance", cl::desc("Relative error tolerated"),
39 cl::init(0.0));
40
Chris Lattner4a106452002-12-23 23:50:16 +000041 cl::opt<OutputType>
Dan Gohman70ef4492008-12-08 04:02:47 +000042 InterpreterSel(cl::desc("Specify the \"test\" i.e. suspect back-end:"),
Brian Gaekeb5ee5092003-10-21 17:41:35 +000043 cl::values(clEnumValN(AutoPick, "auto", "Use best guess"),
Misha Brukmanb687d822004-04-19 03:12:35 +000044 clEnumValN(RunLLI, "run-int",
45 "Execute with the interpreter"),
Misha Brukman50733362003-07-24 18:17:43 +000046 clEnumValN(RunJIT, "run-jit", "Execute with JIT"),
47 clEnumValN(RunLLC, "run-llc", "Compile with LLC"),
Chris Lattner50010422010-03-16 06:41:47 +000048 clEnumValN(RunLLCIA, "run-llc-ia",
49 "Compile with LLC with integrated assembler"),
Chris Lattnercd6f46e2006-11-09 05:57:53 +000050 clEnumValN(LLC_Safe, "llc-safe", "Use LLC for all"),
Andrew Trickf73311b2011-02-08 18:20:48 +000051 clEnumValN(CompileCustom, "compile-custom",
52 "Use -compile-command to define a command to "
53 "compile the bitcode. Useful to avoid linking."),
Anton Korobeynikov9ef74252008-04-28 20:53:48 +000054 clEnumValN(Custom, "run-custom",
55 "Use -exec-command to define a command to execute "
56 "the bitcode. Useful for cross-compilation."),
Chris Lattner4d143ee2004-07-16 00:08:28 +000057 clEnumValEnd),
Brian Gaekeb5ee5092003-10-21 17:41:35 +000058 cl::init(AutoPick));
Chris Lattner3c053a02003-04-23 20:31:37 +000059
Dan Gohman70ef4492008-12-08 04:02:47 +000060 cl::opt<OutputType>
61 SafeInterpreterSel(cl::desc("Specify \"safe\" i.e. known-good backend:"),
Evan Cheng49419e22009-07-21 19:25:09 +000062 cl::values(clEnumValN(AutoPick, "safe-auto", "Use best guess"),
63 clEnumValN(RunLLC, "safe-run-llc", "Compile with LLC"),
Evan Cheng49419e22009-07-21 19:25:09 +000064 clEnumValN(Custom, "safe-run-custom",
65 "Use -exec-command to define a command to execute "
66 "the bitcode. Useful for cross-compilation."),
67 clEnumValEnd),
Dan Gohman70ef4492008-12-08 04:02:47 +000068 cl::init(AutoPick));
69
70 cl::opt<std::string>
71 SafeInterpreterPath("safe-path",
Evan Cheng49419e22009-07-21 19:25:09 +000072 cl::desc("Specify the path to the \"safe\" backend program"),
73 cl::init(""));
Dan Gohman70ef4492008-12-08 04:02:47 +000074
Brian Gaekec5cad212004-02-11 18:37:32 +000075 cl::opt<bool>
Reid Spencer5e1452c2006-11-28 07:04:10 +000076 AppendProgramExitCode("append-exit-code",
77 cl::desc("Append the exit code to the output so it gets diff'd too"),
78 cl::init(false));
79
Chris Lattner3c053a02003-04-23 20:31:37 +000080 cl::opt<std::string>
81 InputFile("input", cl::init("/dev/null"),
82 cl::desc("Filename to pipe in as stdin (default: /dev/null)"));
Chris Lattner7dac6582003-10-14 22:24:31 +000083
84 cl::list<std::string>
85 AdditionalSOs("additional-so",
86 cl::desc("Additional shared objects to load "
87 "into executing programs"));
Chris Lattner7d91e492004-07-24 07:53:26 +000088
Reid Spencer51ab5c82006-06-06 00:00:42 +000089 cl::list<std::string>
Andrew Trickde86cbd2011-02-08 18:07:10 +000090 AdditionalLinkerArgs("Xlinker",
Reid Spencer51ab5c82006-06-06 00:00:42 +000091 cl::desc("Additional arguments to pass to the linker"));
Anton Korobeynikov9ef74252008-04-28 20:53:48 +000092
93 cl::opt<std::string>
Andrew Trickf73311b2011-02-08 18:20:48 +000094 CustomCompileCommand("compile-command", cl::init("llc"),
95 cl::desc("Command to compile the bitcode (use with -compile-custom) "
96 "(default: llc)"));
97
98 cl::opt<std::string>
Anton Korobeynikov9ef74252008-04-28 20:53:48 +000099 CustomExecCommand("exec-command", cl::init("simulate"),
100 cl::desc("Command to execute the bitcode (use with -run-custom) "
101 "(default: simulate)"));
Chris Lattner4a106452002-12-23 23:50:16 +0000102}
103
Brian Gaeked0fde302003-11-11 22:41:34 +0000104namespace llvm {
Chris Lattnerfa761832004-01-14 03:38:37 +0000105 // Anything specified after the --args option are taken as arguments to the
106 // program being debugged.
107 cl::list<std::string>
108 InputArgv("args", cl::Positional, cl::desc("<program arguments>..."),
Chris Lattner60083e22004-05-06 22:05:35 +0000109 cl::ZeroOrMore, cl::PositionalEatsArgs);
Daniel Dunbar68ccdaa2009-09-07 19:26:11 +0000110
111 cl::opt<std::string>
112 OutputPrefix("output-prefix", cl::init("bugpoint"),
113 cl::desc("Prefix to use for outputs (default: 'bugpoint')"));
Dan Gohman70ef4492008-12-08 04:02:47 +0000114}
Brian Gaeke636df3d2004-05-04 21:09:16 +0000115
Dan Gohman70ef4492008-12-08 04:02:47 +0000116namespace {
Brian Gaeke636df3d2004-05-04 21:09:16 +0000117 cl::list<std::string>
118 ToolArgv("tool-args", cl::Positional, cl::desc("<tool arguments>..."),
Chris Lattner60083e22004-05-06 22:05:35 +0000119 cl::ZeroOrMore, cl::PositionalEatsArgs);
Dan Gohman70ef4492008-12-08 04:02:47 +0000120
121 cl::list<std::string>
122 SafeToolArgv("safe-tool-args", cl::Positional,
123 cl::desc("<safe-tool arguments>..."),
124 cl::ZeroOrMore, cl::PositionalEatsArgs);
Bill Wendling38efa382009-03-02 23:13:18 +0000125
Kalle Raiskilafaa95762010-05-10 07:38:37 +0000126 cl::opt<std::string>
Andrew Trickde86cbd2011-02-08 18:07:10 +0000127 GCCBinary("gcc", cl::init("gcc"),
Kalle Raiskilafaa95762010-05-10 07:38:37 +0000128 cl::desc("The gcc binary to use. (default 'gcc')"));
129
Bill Wendling38efa382009-03-02 23:13:18 +0000130 cl::list<std::string>
131 GCCToolArgv("gcc-tool-args", cl::Positional,
132 cl::desc("<gcc-tool arguments>..."),
133 cl::ZeroOrMore, cl::PositionalEatsArgs);
Chris Lattnerfa761832004-01-14 03:38:37 +0000134}
Misha Brukman9d679cb2003-07-30 20:15:44 +0000135
Chris Lattner4a106452002-12-23 23:50:16 +0000136//===----------------------------------------------------------------------===//
137// BugDriver method implementation
138//
139
140/// initializeExecutionEnvironment - This method is used to set up the
141/// environment for executing LLVM programs.
142///
143bool BugDriver::initializeExecutionEnvironment() {
Dan Gohmanac95cc72009-07-16 15:30:09 +0000144 outs() << "Initializing execution environment: ";
Chris Lattner4a106452002-12-23 23:50:16 +0000145
Misha Brukman41485562003-09-29 22:40:52 +0000146 // Create an instance of the AbstractInterpreter interface as specified on
147 // the command line
Dan Gohman70ef4492008-12-08 04:02:47 +0000148 SafeInterpreter = 0;
Chris Lattner4a106452002-12-23 23:50:16 +0000149 std::string Message;
Brian Gaeke636df3d2004-05-04 21:09:16 +0000150
Chris Lattnercc876a72003-05-03 03:19:41 +0000151 switch (InterpreterSel) {
Brian Gaekeb5ee5092003-10-21 17:41:35 +0000152 case AutoPick:
Brian Gaekeb5ee5092003-10-21 17:41:35 +0000153 if (!Interpreter) {
154 InterpreterSel = RunJIT;
Brian Gaeke636df3d2004-05-04 21:09:16 +0000155 Interpreter = AbstractInterpreter::createJIT(getToolName(), Message,
156 &ToolArgv);
Brian Gaekeb5ee5092003-10-21 17:41:35 +0000157 }
158 if (!Interpreter) {
159 InterpreterSel = RunLLC;
Brian Gaeke636df3d2004-05-04 21:09:16 +0000160 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message,
Andrew Trickde86cbd2011-02-08 18:07:10 +0000161 GCCBinary, &ToolArgv,
Kalle Raiskilafaa95762010-05-10 07:38:37 +0000162 &GCCToolArgv);
Brian Gaekeb5ee5092003-10-21 17:41:35 +0000163 }
164 if (!Interpreter) {
165 InterpreterSel = RunLLI;
Brian Gaeke636df3d2004-05-04 21:09:16 +0000166 Interpreter = AbstractInterpreter::createLLI(getToolName(), Message,
167 &ToolArgv);
Brian Gaekeb5ee5092003-10-21 17:41:35 +0000168 }
169 if (!Interpreter) {
170 InterpreterSel = AutoPick;
171 Message = "Sorry, I can't automatically select an interpreter!\n";
172 }
173 break;
Chris Lattner769f1fe2003-10-14 21:59:36 +0000174 case RunLLI:
Brian Gaeke636df3d2004-05-04 21:09:16 +0000175 Interpreter = AbstractInterpreter::createLLI(getToolName(), Message,
176 &ToolArgv);
Chris Lattner769f1fe2003-10-14 21:59:36 +0000177 break;
178 case RunLLC:
Chris Lattner50010422010-03-16 06:41:47 +0000179 case RunLLCIA:
Dan Gohman70ef4492008-12-08 04:02:47 +0000180 case LLC_Safe:
Brian Gaeke636df3d2004-05-04 21:09:16 +0000181 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message,
Andrew Trickde86cbd2011-02-08 18:07:10 +0000182 GCCBinary, &ToolArgv,
Kalle Raiskilafaa95762010-05-10 07:38:37 +0000183 &GCCToolArgv,
Chris Lattner50010422010-03-16 06:41:47 +0000184 InterpreterSel == RunLLCIA);
Chris Lattner769f1fe2003-10-14 21:59:36 +0000185 break;
186 case RunJIT:
Brian Gaeke636df3d2004-05-04 21:09:16 +0000187 Interpreter = AbstractInterpreter::createJIT(getToolName(), Message,
188 &ToolArgv);
Chris Lattner769f1fe2003-10-14 21:59:36 +0000189 break;
Andrew Trickf73311b2011-02-08 18:20:48 +0000190 case CompileCustom:
191 Interpreter =
192 AbstractInterpreter::createCustomCompiler(Message, CustomCompileCommand);
193 break;
Anton Korobeynikov9ef74252008-04-28 20:53:48 +0000194 case Custom:
Andrew Trickf73311b2011-02-08 18:20:48 +0000195 Interpreter =
196 AbstractInterpreter::createCustomExecutor(Message, CustomExecCommand);
Anton Korobeynikov9ef74252008-04-28 20:53:48 +0000197 break;
Chris Lattner4a106452002-12-23 23:50:16 +0000198 }
Matthijs Kooijmanad6996d2008-06-12 13:09:43 +0000199 if (!Interpreter)
Dan Gohman65f57c22009-07-15 16:35:29 +0000200 errs() << Message;
Matthijs Kooijmanad6996d2008-06-12 13:09:43 +0000201 else // Display informational messages on stdout instead of stderr
Dan Gohmanac95cc72009-07-16 15:30:09 +0000202 outs() << Message;
Chris Lattner4a106452002-12-23 23:50:16 +0000203
Dan Gohman70ef4492008-12-08 04:02:47 +0000204 std::string Path = SafeInterpreterPath;
205 if (Path.empty())
206 Path = getToolName();
207 std::vector<std::string> SafeToolArgs = SafeToolArgv;
208 switch (SafeInterpreterSel) {
209 case AutoPick:
Dan Gohman70ef4492008-12-08 04:02:47 +0000210 // In "llc-safe" mode, default to using LLC as the "safe" backend.
211 if (!SafeInterpreter &&
212 InterpreterSel == LLC_Safe) {
213 SafeInterpreterSel = RunLLC;
214 SafeToolArgs.push_back("--relocation-model=pic");
Dan Gohman197f7282009-08-05 20:21:17 +0000215 SafeInterpreter = AbstractInterpreter::createLLC(Path.c_str(), Message,
Andrew Trickde86cbd2011-02-08 18:07:10 +0000216 GCCBinary,
Bill Wendling38efa382009-03-02 23:13:18 +0000217 &SafeToolArgs,
218 &GCCToolArgv);
Dan Gohman70ef4492008-12-08 04:02:47 +0000219 }
220
Dan Gohman70ef4492008-12-08 04:02:47 +0000221 if (!SafeInterpreter &&
222 InterpreterSel != RunLLC &&
223 InterpreterSel != RunJIT) {
224 SafeInterpreterSel = RunLLC;
225 SafeToolArgs.push_back("--relocation-model=pic");
Dan Gohman197f7282009-08-05 20:21:17 +0000226 SafeInterpreter = AbstractInterpreter::createLLC(Path.c_str(), Message,
Andrew Trickde86cbd2011-02-08 18:07:10 +0000227 GCCBinary,
Bill Wendling38efa382009-03-02 23:13:18 +0000228 &SafeToolArgs,
229 &GCCToolArgv);
Dan Gohman70ef4492008-12-08 04:02:47 +0000230 }
231 if (!SafeInterpreter) {
232 SafeInterpreterSel = AutoPick;
Saleem Abdulrasooled7fcf42013-01-24 16:49:14 +0000233 Message = "Sorry, I can't automatically select a safe interpreter!\n";
Dan Gohman70ef4492008-12-08 04:02:47 +0000234 }
235 break;
236 case RunLLC:
Chris Lattner50010422010-03-16 06:41:47 +0000237 case RunLLCIA:
Dan Gohman70ef4492008-12-08 04:02:47 +0000238 SafeToolArgs.push_back("--relocation-model=pic");
Dan Gohman197f7282009-08-05 20:21:17 +0000239 SafeInterpreter = AbstractInterpreter::createLLC(Path.c_str(), Message,
Kalle Raiskilafaa95762010-05-10 07:38:37 +0000240 GCCBinary, &SafeToolArgs,
Chris Lattner50010422010-03-16 06:41:47 +0000241 &GCCToolArgv,
242 SafeInterpreterSel == RunLLCIA);
Dan Gohman70ef4492008-12-08 04:02:47 +0000243 break;
Dan Gohman70ef4492008-12-08 04:02:47 +0000244 case Custom:
Andrew Trickf73311b2011-02-08 18:20:48 +0000245 SafeInterpreter =
246 AbstractInterpreter::createCustomExecutor(Message, CustomExecCommand);
Dan Gohman70ef4492008-12-08 04:02:47 +0000247 break;
248 default:
249 Message = "Sorry, this back-end is not supported by bugpoint as the "
250 "\"safe\" backend right now!\n";
251 break;
Chris Lattner7bb11542004-02-18 20:52:02 +0000252 }
Dan Gohmanac95cc72009-07-16 15:30:09 +0000253 if (!SafeInterpreter) { outs() << Message << "\nExiting.\n"; exit(1); }
Andrew Trickde86cbd2011-02-08 18:07:10 +0000254
Kalle Raiskilafaa95762010-05-10 07:38:37 +0000255 gcc = GCC::create(Message, GCCBinary, &GCCToolArgv);
Dan Gohmanac95cc72009-07-16 15:30:09 +0000256 if (!gcc) { outs() << Message << "\nExiting.\n"; exit(1); }
Misha Brukmana259c9b2003-07-24 21:59:10 +0000257
Chris Lattner4a106452002-12-23 23:50:16 +0000258 // If there was an error creating the selected interpreter, quit with error.
259 return Interpreter == 0;
260}
261
Nick Lewycky22ff7482010-04-12 05:08:25 +0000262/// compileProgram - Try to compile the specified module, returning false and
263/// setting Error if an error occurs. This is used for code generation
264/// crash testing.
Chris Lattnerea9212c2004-02-18 23:25:22 +0000265///
Rafael Espindola248d1c62010-08-05 03:00:22 +0000266void BugDriver::compileProgram(Module *M, std::string *Error) const {
Gabor Greif8ff70c22007-07-04 21:55:50 +0000267 // Emit the program to a bitcode file...
Rafael Espindola9a230392013-06-18 16:14:09 +0000268 SmallString<128> BitcodeFile;
269 int BitcodeFD;
Rafael Espindola200c7482013-07-05 21:01:08 +0000270 error_code EC = sys::fs::createUniqueFile(
Rafael Espindola9a230392013-06-18 16:14:09 +0000271 OutputPrefix + "-test-program-%%%%%%%.bc", BitcodeFD, BitcodeFile);
272 if (EC) {
273 errs() << ToolName << ": Error making unique filename: " << EC.message()
Dan Gohman65f57c22009-07-15 16:35:29 +0000274 << "\n";
Reid Spencer51c5a282006-08-23 20:34:57 +0000275 exit(1);
276 }
Rafael Espindola9a230392013-06-18 16:14:09 +0000277 if (writeProgramToFile(BitcodeFile.str(), BitcodeFD, M)) {
278 errs() << ToolName << ": Error emitting bitcode to file '" << BitcodeFile
279 << "'!\n";
Chris Lattnerea9212c2004-02-18 23:25:22 +0000280 exit(1);
281 }
282
Nick Lewycky22ff7482010-04-12 05:08:25 +0000283 // Remove the temporary bitcode file when we are done.
Michael J. Spencerc9c08fb2011-03-31 13:04:19 +0000284 FileRemover BitcodeFileRemover(BitcodeFile.str(), !SaveTemps);
Chris Lattnerea9212c2004-02-18 23:25:22 +0000285
286 // Actually compile the program!
Duncan Sands41396302010-05-24 07:49:55 +0000287 Interpreter->compileProgram(BitcodeFile.str(), Error, Timeout, MemoryLimit);
Chris Lattnerea9212c2004-02-18 23:25:22 +0000288}
289
Chris Lattner4a106452002-12-23 23:50:16 +0000290
291/// executeProgram - This method runs "Program", capturing the output of the
292/// program to a file, returning the filename of the file. A recommended
293/// filename may be optionally specified.
294///
Rafael Espindola10757dd2010-07-30 14:19:00 +0000295std::string BugDriver::executeProgram(const Module *Program,
296 std::string OutputFile,
Gabor Greif8ff70c22007-07-04 21:55:50 +0000297 std::string BitcodeFile,
Chris Lattner769f1fe2003-10-14 21:59:36 +0000298 const std::string &SharedObj,
Nick Lewycky22ff7482010-04-12 05:08:25 +0000299 AbstractInterpreter *AI,
Rafael Espindola13793262010-07-31 14:34:49 +0000300 std::string *Error) const {
Chris Lattner769f1fe2003-10-14 21:59:36 +0000301 if (AI == 0) AI = Interpreter;
302 assert(AI && "Interpreter should have been created already!");
Gabor Greif8ff70c22007-07-04 21:55:50 +0000303 bool CreatedBitcode = false;
Reid Spencer51c5a282006-08-23 20:34:57 +0000304 std::string ErrMsg;
Gabor Greif8ff70c22007-07-04 21:55:50 +0000305 if (BitcodeFile.empty()) {
306 // Emit the program to a bitcode file...
Rafael Espindola9a230392013-06-18 16:14:09 +0000307 SmallString<128> UniqueFilename;
308 int UniqueFD;
Rafael Espindola200c7482013-07-05 21:01:08 +0000309 error_code EC = sys::fs::createUniqueFile(
Rafael Espindola9a230392013-06-18 16:14:09 +0000310 OutputPrefix + "-test-program-%%%%%%%.bc", UniqueFD, UniqueFilename);
311 if (EC) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000312 errs() << ToolName << ": Error making unique filename: "
Rafael Espindola9a230392013-06-18 16:14:09 +0000313 << EC.message() << "!\n";
Reid Spencer51c5a282006-08-23 20:34:57 +0000314 exit(1);
315 }
Rafael Espindola9a230392013-06-18 16:14:09 +0000316 BitcodeFile = UniqueFilename.str();
Chris Lattner4a106452002-12-23 23:50:16 +0000317
Rafael Espindola9a230392013-06-18 16:14:09 +0000318 if (writeProgramToFile(BitcodeFile, UniqueFD, Program)) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000319 errs() << ToolName << ": Error emitting bitcode to file '"
320 << BitcodeFile << "'!\n";
Chris Lattner4a106452002-12-23 23:50:16 +0000321 exit(1);
322 }
Gabor Greif8ff70c22007-07-04 21:55:50 +0000323 CreatedBitcode = true;
Chris Lattner4a106452002-12-23 23:50:16 +0000324 }
325
Gabor Greif8ff70c22007-07-04 21:55:50 +0000326 // Remove the temporary bitcode file when we are done.
Rafael Espindola9a230392013-06-18 16:14:09 +0000327 std::string BitcodePath(BitcodeFile);
328 FileRemover BitcodeFileRemover(BitcodePath,
Michael J. Spencerc9c08fb2011-03-31 13:04:19 +0000329 CreatedBitcode && !SaveTemps);
Chris Lattner97092722004-02-18 22:01:21 +0000330
Hal Finkele528a2c2013-06-28 16:37:52 +0000331 if (OutputFile.empty()) OutputFile = OutputPrefix + "-execution-output-%%%%%%%";
Misha Brukman50733362003-07-24 18:17:43 +0000332
Chris Lattner4a106452002-12-23 23:50:16 +0000333 // Check to see if this is a valid output filename...
Rafael Espindola9a230392013-06-18 16:14:09 +0000334 SmallString<128> UniqueFile;
Rafael Espindola200c7482013-07-05 21:01:08 +0000335 error_code EC = sys::fs::createUniqueFile(OutputFile, UniqueFile);
Rafael Espindola9a230392013-06-18 16:14:09 +0000336 if (EC) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000337 errs() << ToolName << ": Error making unique filename: "
Rafael Espindola9a230392013-06-18 16:14:09 +0000338 << EC.message() << "\n";
Reid Spencer51c5a282006-08-23 20:34:57 +0000339 exit(1);
340 }
Rafael Espindola9a230392013-06-18 16:14:09 +0000341 OutputFile = UniqueFile.str();
Chris Lattner4a106452002-12-23 23:50:16 +0000342
Chris Lattner769f1fe2003-10-14 21:59:36 +0000343 // Figure out which shared objects to run, if any.
Chris Lattner7dac6582003-10-14 22:24:31 +0000344 std::vector<std::string> SharedObjs(AdditionalSOs);
Chris Lattner769f1fe2003-10-14 21:59:36 +0000345 if (!SharedObj.empty())
346 SharedObjs.push_back(SharedObj);
347
Nick Lewycky22ff7482010-04-12 05:08:25 +0000348 int RetVal = AI->ExecuteProgram(BitcodeFile, InputArgv, InputFile, OutputFile,
349 Error, AdditionalLinkerArgs, SharedObjs,
Dan Gohman70ef4492008-12-08 04:02:47 +0000350 Timeout, MemoryLimit);
Nick Lewycky22ff7482010-04-12 05:08:25 +0000351 if (!Error->empty())
352 return OutputFile;
Chris Lattner7d91e492004-07-24 07:53:26 +0000353
354 if (RetVal == -1) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000355 errs() << "<timeout>";
Chris Lattner7d91e492004-07-24 07:53:26 +0000356 static bool FirstTimeout = true;
357 if (FirstTimeout) {
Dan Gohmanac95cc72009-07-16 15:30:09 +0000358 outs() << "\n"
Chris Lattner7d91e492004-07-24 07:53:26 +0000359 "*** Program execution timed out! This mechanism is designed to handle\n"
360 " programs stuck in infinite loops gracefully. The -timeout option\n"
361 " can be used to change the timeout threshold or disable it completely\n"
362 " (with -timeout=0). This message is only displayed once.\n";
363 FirstTimeout = false;
364 }
365 }
Chris Lattner769f1fe2003-10-14 21:59:36 +0000366
Reid Spencer5e1452c2006-11-28 07:04:10 +0000367 if (AppendProgramExitCode) {
368 std::ofstream outFile(OutputFile.c_str(), std::ios_base::app);
369 outFile << "exit " << RetVal << '\n';
370 outFile.close();
371 }
372
Chris Lattner4a106452002-12-23 23:50:16 +0000373 // Return the filename we captured the output to.
374 return OutputFile;
375}
376
Dan Gohman70ef4492008-12-08 04:02:47 +0000377/// executeProgramSafely - Used to create reference output with the "safe"
Brian Gaekec5cad212004-02-11 18:37:32 +0000378/// backend, if reference output is not provided.
379///
Rafael Espindola10757dd2010-07-30 14:19:00 +0000380std::string BugDriver::executeProgramSafely(const Module *Program,
381 std::string OutputFile,
Rafael Espindola13793262010-07-31 14:34:49 +0000382 std::string *Error) const {
Rafael Espindola10757dd2010-07-30 14:19:00 +0000383 return executeProgram(Program, OutputFile, "", "", SafeInterpreter, Error);
Brian Gaekec5cad212004-02-11 18:37:32 +0000384}
Misha Brukman50733362003-07-24 18:17:43 +0000385
Nick Lewycky22ff7482010-04-12 05:08:25 +0000386std::string BugDriver::compileSharedObject(const std::string &BitcodeFile,
387 std::string &Error) {
Misha Brukman50733362003-07-24 18:17:43 +0000388 assert(Interpreter && "Interpreter should have been created already!");
Rafael Espindolaf656a1d2013-06-17 19:21:38 +0000389 std::string OutputFile;
Misha Brukman50733362003-07-24 18:17:43 +0000390
Dan Gohman70ef4492008-12-08 04:02:47 +0000391 // Using the known-good backend.
Nick Lewycky22ff7482010-04-12 05:08:25 +0000392 GCC::FileType FT = SafeInterpreter->OutputCode(BitcodeFile, OutputFile,
393 Error);
394 if (!Error.empty())
395 return "";
Misha Brukman50733362003-07-24 18:17:43 +0000396
Chris Lattnera0f5b152003-10-14 21:09:11 +0000397 std::string SharedObjectFile;
Rafael Espindolaf656a1d2013-06-17 19:21:38 +0000398 bool Failure = gcc->MakeSharedObject(OutputFile, FT, SharedObjectFile,
Nick Lewycky22ff7482010-04-12 05:08:25 +0000399 AdditionalLinkerArgs, Error);
400 if (!Error.empty())
401 return "";
402 if (Failure)
Chris Lattnera0f5b152003-10-14 21:09:11 +0000403 exit(1);
Misha Brukman50733362003-07-24 18:17:43 +0000404
405 // Remove the intermediate C file
Rafael Espindolaf656a1d2013-06-17 19:21:38 +0000406 sys::fs::remove(OutputFile);
Misha Brukman50733362003-07-24 18:17:43 +0000407
Chris Lattner6ebe44d2003-10-19 21:54:13 +0000408 return "./" + SharedObjectFile;
Misha Brukman50733362003-07-24 18:17:43 +0000409}
410
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +0000411/// createReferenceFile - calls compileProgram and then records the output
Andrew Trickde86cbd2011-02-08 18:07:10 +0000412/// into ReferenceOutputFile. Returns true if reference file created, false
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +0000413/// otherwise. Note: initializeExecutionEnvironment should be called BEFORE
414/// this function.
415///
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000416bool BugDriver::createReferenceFile(Module *M, const std::string &Filename) {
Nick Lewycky22ff7482010-04-12 05:08:25 +0000417 std::string Error;
418 compileProgram(Program, &Error);
419 if (!Error.empty())
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +0000420 return false;
Nick Lewycky22ff7482010-04-12 05:08:25 +0000421
Rafael Espindola10757dd2010-07-30 14:19:00 +0000422 ReferenceOutputFile = executeProgramSafely(Program, Filename, &Error);
Nick Lewycky22ff7482010-04-12 05:08:25 +0000423 if (!Error.empty()) {
424 errs() << Error;
Dan Gohman70ef4492008-12-08 04:02:47 +0000425 if (Interpreter != SafeInterpreter) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000426 errs() << "*** There is a bug running the \"safe\" backend. Either"
Eric Christophera443e5b2012-03-23 05:50:46 +0000427 << " debug it (for example with the -run-jit bugpoint option,"
428 << " if JIT is being used as the \"safe\" backend), or fix the"
Dan Gohman65f57c22009-07-15 16:35:29 +0000429 << " error some other way.\n";
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +0000430 }
431 return false;
432 }
Nick Lewycky22ff7482010-04-12 05:08:25 +0000433 outs() << "\nReference output is: " << ReferenceOutputFile << "\n\n";
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +0000434 return true;
435}
Misha Brukman50733362003-07-24 18:17:43 +0000436
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +0000437/// diffProgram - This method executes the specified module and diffs the
438/// output against the file specified by ReferenceOutputFile. If the output
Nick Lewycky22ff7482010-04-12 05:08:25 +0000439/// is different, 1 is returned. If there is a problem with the code
Andrew Trick7c863eb2011-05-11 16:31:24 +0000440/// generator (e.g., llc crashes), this will set ErrMsg.
Chris Lattner4a106452002-12-23 23:50:16 +0000441///
Rafael Espindola10757dd2010-07-30 14:19:00 +0000442bool BugDriver::diffProgram(const Module *Program,
443 const std::string &BitcodeFile,
Misha Brukman50733362003-07-24 18:17:43 +0000444 const std::string &SharedObject,
Nick Lewycky22ff7482010-04-12 05:08:25 +0000445 bool RemoveBitcode,
Rafael Espindola13793262010-07-31 14:34:49 +0000446 std::string *ErrMsg) const {
Chris Lattner4a106452002-12-23 23:50:16 +0000447 // Execute the program, generating an output file...
Rafael Espindola9a230392013-06-18 16:14:09 +0000448 std::string Output(
449 executeProgram(Program, "", BitcodeFile, SharedObject, 0, ErrMsg));
Nick Lewycky22ff7482010-04-12 05:08:25 +0000450 if (!ErrMsg->empty())
451 return false;
Brian Gaekec5cad212004-02-11 18:37:32 +0000452
Chris Lattner65f62792003-08-01 20:29:45 +0000453 std::string Error;
Chris Lattner4a106452002-12-23 23:50:16 +0000454 bool FilesDifferent = false;
Rafael Espindolabbf97ea2013-06-13 20:41:00 +0000455 if (int Diff = DiffFilesWithTolerance(ReferenceOutputFile,
Rafael Espindola9a230392013-06-18 16:14:09 +0000456 Output,
Chris Lattnera328c512005-01-23 03:45:26 +0000457 AbsTolerance, RelTolerance, &Error)) {
458 if (Diff == 2) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000459 errs() << "While diffing output: " << Error << '\n';
Chris Lattner65f62792003-08-01 20:29:45 +0000460 exit(1);
461 }
462 FilesDifferent = true;
463 }
David Goodwin80becf12009-07-10 21:39:28 +0000464 else {
465 // Remove the generated output if there are no differences.
Rafael Espindola9a230392013-06-18 16:14:09 +0000466 sys::fs::remove(Output);
David Goodwin80becf12009-07-10 21:39:28 +0000467 }
Chris Lattner4a106452002-12-23 23:50:16 +0000468
Gabor Greif8ff70c22007-07-04 21:55:50 +0000469 // Remove the bitcode file if we are supposed to.
470 if (RemoveBitcode)
Rafael Espindola9a230392013-06-18 16:14:09 +0000471 sys::fs::remove(BitcodeFile);
Chris Lattner4a106452002-12-23 23:50:16 +0000472 return FilesDifferent;
473}
Misha Brukman91eabc12003-07-28 19:16:14 +0000474
475bool BugDriver::isExecutingJIT() {
476 return InterpreterSel == RunJIT;
477}
Brian Gaeked0fde302003-11-11 22:41:34 +0000478