blob: 41b8ccc18e87136dfc1b2000990de0e385fd1148 [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 {
28 // OutputType - Allow the user to specify the way code should be run, to test
29 // for miscompilation.
30 //
31 enum OutputType {
Eric Christopher64a23232012-03-23 05:50:46 +000032 AutoPick, RunLLI, RunJIT, RunLLC, RunLLCIA, LLC_Safe, CompileCustom, Custom
Chris Lattnerde4aa4c2002-12-23 23:50:16 +000033 };
Misha Brukman5bc6a8f2003-09-29 22:40:52 +000034
Chris Lattnerece10a42005-01-23 03:45:26 +000035 cl::opt<double>
36 AbsTolerance("abs-tolerance", cl::desc("Absolute error tolerated"),
37 cl::init(0.0));
38 cl::opt<double>
39 RelTolerance("rel-tolerance", cl::desc("Relative error tolerated"),
40 cl::init(0.0));
41
Chris Lattnerde4aa4c2002-12-23 23:50:16 +000042 cl::opt<OutputType>
Dan Gohman414cf502008-12-08 04:02:47 +000043 InterpreterSel(cl::desc("Specify the \"test\" i.e. suspect back-end:"),
Brian Gaeke9a0bdb12003-10-21 17:41:35 +000044 cl::values(clEnumValN(AutoPick, "auto", "Use best guess"),
Misha Brukman8a32c6d2004-04-19 03:12:35 +000045 clEnumValN(RunLLI, "run-int",
46 "Execute with the interpreter"),
Misha Brukmand792c9b2003-07-24 18:17:43 +000047 clEnumValN(RunJIT, "run-jit", "Execute with JIT"),
48 clEnumValN(RunLLC, "run-llc", "Compile with LLC"),
Chris Lattnerfd381322010-03-16 06:41:47 +000049 clEnumValN(RunLLCIA, "run-llc-ia",
50 "Compile with LLC with integrated assembler"),
Chris Lattnerf1a1a7a2006-11-09 05:57:53 +000051 clEnumValN(LLC_Safe, "llc-safe", "Use LLC for all"),
Andrew Trick8665d592011-02-08 18:20:48 +000052 clEnumValN(CompileCustom, "compile-custom",
53 "Use -compile-command to define a command to "
54 "compile the bitcode. Useful to avoid linking."),
Anton Korobeynikovc53565c2008-04-28 20:53:48 +000055 clEnumValN(Custom, "run-custom",
56 "Use -exec-command to define a command to execute "
57 "the bitcode. Useful for cross-compilation."),
Chris Lattner97161002004-07-16 00:08:28 +000058 clEnumValEnd),
Brian Gaeke9a0bdb12003-10-21 17:41:35 +000059 cl::init(AutoPick));
Chris Lattner68efaa72003-04-23 20:31:37 +000060
Dan Gohman414cf502008-12-08 04:02:47 +000061 cl::opt<OutputType>
62 SafeInterpreterSel(cl::desc("Specify \"safe\" i.e. known-good backend:"),
Evan Chengc9ec7352009-07-21 19:25:09 +000063 cl::values(clEnumValN(AutoPick, "safe-auto", "Use best guess"),
64 clEnumValN(RunLLC, "safe-run-llc", "Compile with LLC"),
Evan Chengc9ec7352009-07-21 19:25:09 +000065 clEnumValN(Custom, "safe-run-custom",
66 "Use -exec-command to define a command to execute "
67 "the bitcode. Useful for cross-compilation."),
68 clEnumValEnd),
Dan Gohman414cf502008-12-08 04:02:47 +000069 cl::init(AutoPick));
70
71 cl::opt<std::string>
72 SafeInterpreterPath("safe-path",
Evan Chengc9ec7352009-07-21 19:25:09 +000073 cl::desc("Specify the path to the \"safe\" backend program"),
74 cl::init(""));
Dan Gohman414cf502008-12-08 04:02:47 +000075
Brian Gaeke35145be2004-02-11 18:37:32 +000076 cl::opt<bool>
Reid Spencerd077fe72006-11-28 07:04:10 +000077 AppendProgramExitCode("append-exit-code",
78 cl::desc("Append the exit code to the output so it gets diff'd too"),
79 cl::init(false));
80
Chris Lattner68efaa72003-04-23 20:31:37 +000081 cl::opt<std::string>
82 InputFile("input", cl::init("/dev/null"),
83 cl::desc("Filename to pipe in as stdin (default: /dev/null)"));
Chris Lattnerdc92fa62003-10-14 22:24:31 +000084
85 cl::list<std::string>
86 AdditionalSOs("additional-so",
87 cl::desc("Additional shared objects to load "
88 "into executing programs"));
Chris Lattner4e0969b2004-07-24 07:53:26 +000089
Reid Spencerc3065b72006-06-06 00:00:42 +000090 cl::list<std::string>
Andrew Trick69a963e2011-02-08 18:07:10 +000091 AdditionalLinkerArgs("Xlinker",
Reid Spencerc3065b72006-06-06 00:00:42 +000092 cl::desc("Additional arguments to pass to the linker"));
Anton Korobeynikovc53565c2008-04-28 20:53:48 +000093
94 cl::opt<std::string>
Andrew Trick8665d592011-02-08 18:20:48 +000095 CustomCompileCommand("compile-command", cl::init("llc"),
96 cl::desc("Command to compile the bitcode (use with -compile-custom) "
97 "(default: llc)"));
98
99 cl::opt<std::string>
Anton Korobeynikovc53565c2008-04-28 20:53:48 +0000100 CustomExecCommand("exec-command", cl::init("simulate"),
101 cl::desc("Command to execute the bitcode (use with -run-custom) "
102 "(default: simulate)"));
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000103}
104
Brian Gaeke960707c2003-11-11 22:41:34 +0000105namespace llvm {
Chris Lattner2f1aa112004-01-14 03:38:37 +0000106 // Anything specified after the --args option are taken as arguments to the
107 // program being debugged.
108 cl::list<std::string>
109 InputArgv("args", cl::Positional, cl::desc("<program arguments>..."),
Chris Lattner6aee736d2004-05-06 22:05:35 +0000110 cl::ZeroOrMore, cl::PositionalEatsArgs);
Daniel Dunbara53337f2009-09-07 19:26:11 +0000111
112 cl::opt<std::string>
113 OutputPrefix("output-prefix", cl::init("bugpoint"),
114 cl::desc("Prefix to use for outputs (default: 'bugpoint')"));
Dan Gohman414cf502008-12-08 04:02:47 +0000115}
Brian Gaeke4a278f02004-05-04 21:09:16 +0000116
Dan Gohman414cf502008-12-08 04:02:47 +0000117namespace {
Brian Gaeke4a278f02004-05-04 21:09:16 +0000118 cl::list<std::string>
119 ToolArgv("tool-args", cl::Positional, cl::desc("<tool arguments>..."),
Chris Lattner6aee736d2004-05-06 22:05:35 +0000120 cl::ZeroOrMore, cl::PositionalEatsArgs);
Dan Gohman414cf502008-12-08 04:02:47 +0000121
122 cl::list<std::string>
123 SafeToolArgv("safe-tool-args", cl::Positional,
124 cl::desc("<safe-tool arguments>..."),
125 cl::ZeroOrMore, cl::PositionalEatsArgs);
Bill Wendlingbf5d8272009-03-02 23:13:18 +0000126
Kalle Raiskila6be58292010-05-10 07:38:37 +0000127 cl::opt<std::string>
Davide Italianoab256212015-10-14 20:29:54 +0000128 CCBinary("gcc", cl::init(""), cl::desc("The gcc binary to use."));
Kalle Raiskila6be58292010-05-10 07:38:37 +0000129
Bill Wendlingbf5d8272009-03-02 23:13:18 +0000130 cl::list<std::string>
Davide Italianoab256212015-10-14 20:29:54 +0000131 CCToolArgv("gcc-tool-args", cl::Positional,
Bill Wendlingbf5d8272009-03-02 23:13:18 +0000132 cl::desc("<gcc-tool arguments>..."),
133 cl::ZeroOrMore, cl::PositionalEatsArgs);
Chris Lattner2f1aa112004-01-14 03:38:37 +0000134}
Misha Brukman40feb362003-07-30 20:15:44 +0000135
Chris Lattnerde4aa4c2002-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 Gohmanee051522009-07-16 15:30:09 +0000144 outs() << "Initializing execution environment: ";
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000145
Misha Brukman5bc6a8f2003-09-29 22:40:52 +0000146 // Create an instance of the AbstractInterpreter interface as specified on
147 // the command line
Craig Toppere6cb63e2014-04-25 04:24:47 +0000148 SafeInterpreter = nullptr;
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000149 std::string Message;
Brian Gaeke4a278f02004-05-04 21:09:16 +0000150
Davide Italianoab256212015-10-14 20:29:54 +0000151 if (CCBinary.empty()) {
Davide Italiano8a5d0432015-10-14 19:48:01 +0000152 if (sys::findProgramByName("clang"))
Davide Italianoab256212015-10-14 20:29:54 +0000153 CCBinary = "clang";
Davide Italiano8a5d0432015-10-14 19:48:01 +0000154 else
Davide Italianoab256212015-10-14 20:29:54 +0000155 CCBinary = "gcc";
Davide Italiano8a5d0432015-10-14 19:48:01 +0000156 }
157
Chris Lattner7709ec52003-05-03 03:19:41 +0000158 switch (InterpreterSel) {
Brian Gaeke9a0bdb12003-10-21 17:41:35 +0000159 case AutoPick:
Brian Gaeke9a0bdb12003-10-21 17:41:35 +0000160 if (!Interpreter) {
161 InterpreterSel = RunJIT;
Brian Gaeke4a278f02004-05-04 21:09:16 +0000162 Interpreter = AbstractInterpreter::createJIT(getToolName(), Message,
163 &ToolArgv);
Brian Gaeke9a0bdb12003-10-21 17:41:35 +0000164 }
165 if (!Interpreter) {
166 InterpreterSel = RunLLC;
Brian Gaeke4a278f02004-05-04 21:09:16 +0000167 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message,
Davide Italianoab256212015-10-14 20:29:54 +0000168 CCBinary, &ToolArgv,
169 &CCToolArgv);
Brian Gaeke9a0bdb12003-10-21 17:41:35 +0000170 }
171 if (!Interpreter) {
172 InterpreterSel = RunLLI;
Brian Gaeke4a278f02004-05-04 21:09:16 +0000173 Interpreter = AbstractInterpreter::createLLI(getToolName(), Message,
174 &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:
Brian Gaeke4a278f02004-05-04 21:09:16 +0000182 Interpreter = AbstractInterpreter::createLLI(getToolName(), Message,
183 &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:
Brian Gaeke4a278f02004-05-04 21:09:16 +0000188 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message,
Davide Italianoab256212015-10-14 20:29:54 +0000189 CCBinary, &ToolArgv,
190 &CCToolArgv,
Chris Lattnerfd381322010-03-16 06:41:47 +0000191 InterpreterSel == RunLLCIA);
Chris Lattner3f6e5222003-10-14 21:59:36 +0000192 break;
193 case RunJIT:
Brian Gaeke4a278f02004-05-04 21:09:16 +0000194 Interpreter = AbstractInterpreter::createJIT(getToolName(), Message,
195 &ToolArgv);
Chris Lattner3f6e5222003-10-14 21:59:36 +0000196 break;
Andrew Trick8665d592011-02-08 18:20:48 +0000197 case CompileCustom:
198 Interpreter =
199 AbstractInterpreter::createCustomCompiler(Message, CustomCompileCommand);
200 break;
Anton Korobeynikovc53565c2008-04-28 20:53:48 +0000201 case Custom:
Andrew Trick8665d592011-02-08 18:20:48 +0000202 Interpreter =
203 AbstractInterpreter::createCustomExecutor(Message, CustomExecCommand);
Anton Korobeynikovc53565c2008-04-28 20:53:48 +0000204 break;
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000205 }
Matthijs Kooijman3bb12762008-06-12 13:09:43 +0000206 if (!Interpreter)
Dan Gohmand8db3762009-07-15 16:35:29 +0000207 errs() << Message;
Matthijs Kooijman3bb12762008-06-12 13:09:43 +0000208 else // Display informational messages on stdout instead of stderr
Dan Gohmanee051522009-07-16 15:30:09 +0000209 outs() << Message;
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000210
Dan Gohman414cf502008-12-08 04:02:47 +0000211 std::string Path = SafeInterpreterPath;
212 if (Path.empty())
213 Path = getToolName();
214 std::vector<std::string> SafeToolArgs = SafeToolArgv;
215 switch (SafeInterpreterSel) {
216 case AutoPick:
Dan Gohman414cf502008-12-08 04:02:47 +0000217 // In "llc-safe" mode, default to using LLC as the "safe" backend.
218 if (!SafeInterpreter &&
219 InterpreterSel == LLC_Safe) {
220 SafeInterpreterSel = RunLLC;
221 SafeToolArgs.push_back("--relocation-model=pic");
Dan Gohman46ffffa2009-08-05 20:21:17 +0000222 SafeInterpreter = AbstractInterpreter::createLLC(Path.c_str(), Message,
Davide Italianoab256212015-10-14 20:29:54 +0000223 CCBinary,
Bill Wendlingbf5d8272009-03-02 23:13:18 +0000224 &SafeToolArgs,
Davide Italianoab256212015-10-14 20:29:54 +0000225 &CCToolArgv);
Dan Gohman414cf502008-12-08 04:02:47 +0000226 }
227
Dan Gohman414cf502008-12-08 04:02:47 +0000228 if (!SafeInterpreter &&
229 InterpreterSel != RunLLC &&
230 InterpreterSel != RunJIT) {
231 SafeInterpreterSel = RunLLC;
232 SafeToolArgs.push_back("--relocation-model=pic");
Dan Gohman46ffffa2009-08-05 20:21:17 +0000233 SafeInterpreter = AbstractInterpreter::createLLC(Path.c_str(), Message,
Davide Italianoab256212015-10-14 20:29:54 +0000234 CCBinary,
Bill Wendlingbf5d8272009-03-02 23:13:18 +0000235 &SafeToolArgs,
Davide Italianoab256212015-10-14 20:29:54 +0000236 &CCToolArgv);
Dan Gohman414cf502008-12-08 04:02:47 +0000237 }
238 if (!SafeInterpreter) {
239 SafeInterpreterSel = AutoPick;
Saleem Abdulrasool5f8609b2013-01-24 16:49:14 +0000240 Message = "Sorry, I can't automatically select a safe interpreter!\n";
Dan Gohman414cf502008-12-08 04:02:47 +0000241 }
242 break;
243 case RunLLC:
Chris Lattnerfd381322010-03-16 06:41:47 +0000244 case RunLLCIA:
Dan Gohman414cf502008-12-08 04:02:47 +0000245 SafeToolArgs.push_back("--relocation-model=pic");
Dan Gohman46ffffa2009-08-05 20:21:17 +0000246 SafeInterpreter = AbstractInterpreter::createLLC(Path.c_str(), Message,
Davide Italianoab256212015-10-14 20:29:54 +0000247 CCBinary, &SafeToolArgs,
248 &CCToolArgv,
Chris Lattnerfd381322010-03-16 06:41:47 +0000249 SafeInterpreterSel == RunLLCIA);
Dan Gohman414cf502008-12-08 04:02:47 +0000250 break;
Dan Gohman414cf502008-12-08 04:02:47 +0000251 case Custom:
Andrew Trick8665d592011-02-08 18:20:48 +0000252 SafeInterpreter =
253 AbstractInterpreter::createCustomExecutor(Message, CustomExecCommand);
Dan Gohman414cf502008-12-08 04:02:47 +0000254 break;
255 default:
256 Message = "Sorry, this back-end is not supported by bugpoint as the "
257 "\"safe\" backend right now!\n";
258 break;
Chris Lattner898de4a2004-02-18 20:52:02 +0000259 }
Dan Gohmanee051522009-07-16 15:30:09 +0000260 if (!SafeInterpreter) { outs() << Message << "\nExiting.\n"; exit(1); }
Andrew Trick69a963e2011-02-08 18:07:10 +0000261
Davide Italianoab256212015-10-14 20:29:54 +0000262 cc = CC::create(Message, CCBinary, &CCToolArgv);
263 if (!cc) { outs() << Message << "\nExiting.\n"; exit(1); }
Misha Brukman0fd31722003-07-24 21:59:10 +0000264
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000265 // If there was an error creating the selected interpreter, quit with error.
Craig Toppere6cb63e2014-04-25 04:24:47 +0000266 return Interpreter == nullptr;
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000267}
268
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000269/// compileProgram - Try to compile the specified module, returning false and
270/// setting Error if an error occurs. This is used for code generation
271/// crash testing.
Chris Lattner96d41dd2004-02-18 23:25:22 +0000272///
Rafael Espindolad1c7ef42010-08-05 03:00:22 +0000273void BugDriver::compileProgram(Module *M, std::string *Error) const {
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000274 // Emit the program to a bitcode file...
Rafael Espindolabecba2b2013-06-18 16:14:09 +0000275 SmallString<128> BitcodeFile;
276 int BitcodeFD;
Rafael Espindola4453e42942014-06-13 03:07:50 +0000277 std::error_code EC = sys::fs::createUniqueFile(
Rafael Espindolabecba2b2013-06-18 16:14:09 +0000278 OutputPrefix + "-test-program-%%%%%%%.bc", BitcodeFD, BitcodeFile);
279 if (EC) {
280 errs() << ToolName << ": Error making unique filename: " << EC.message()
Dan Gohmand8db3762009-07-15 16:35:29 +0000281 << "\n";
Reid Spencere4ca7222006-08-23 20:34:57 +0000282 exit(1);
283 }
Rafael Espindolabecba2b2013-06-18 16:14:09 +0000284 if (writeProgramToFile(BitcodeFile.str(), BitcodeFD, M)) {
285 errs() << ToolName << ": Error emitting bitcode to file '" << BitcodeFile
286 << "'!\n";
Chris Lattner96d41dd2004-02-18 23:25:22 +0000287 exit(1);
288 }
289
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000290 // Remove the temporary bitcode file when we are done.
Michael J. Spencerc60223e2011-03-31 13:04:19 +0000291 FileRemover BitcodeFileRemover(BitcodeFile.str(), !SaveTemps);
Chris Lattner96d41dd2004-02-18 23:25:22 +0000292
293 // Actually compile the program!
Duncan Sandse9cd6d02010-05-24 07:49:55 +0000294 Interpreter->compileProgram(BitcodeFile.str(), Error, Timeout, MemoryLimit);
Chris Lattner96d41dd2004-02-18 23:25:22 +0000295}
296
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000297
298/// 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///
Rafael Espindolac89b1ef2010-07-30 14:19:00 +0000302std::string BugDriver::executeProgram(const Module *Program,
303 std::string OutputFile,
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000304 std::string BitcodeFile,
Chris Lattner3f6e5222003-10-14 21:59:36 +0000305 const std::string &SharedObj,
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000306 AbstractInterpreter *AI,
Rafael Espindolae4904602010-07-31 14:34:49 +0000307 std::string *Error) const {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000308 if (!AI) 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) {
Dan Gohmand8db3762009-07-15 16:35:29 +0000318 errs() << ToolName << ": Error making unique filename: "
Rafael Espindolabecba2b2013-06-18 16:14:09 +0000319 << EC.message() << "!\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)) {
Dan Gohmand8db3762009-07-15 16:35:29 +0000325 errs() << ToolName << ": Error emitting bitcode to file '"
326 << BitcodeFile << "'!\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);
334 FileRemover BitcodeFileRemover(BitcodePath,
Michael J. Spencerc60223e2011-03-31 13:04:19 +0000335 CreatedBitcode && !SaveTemps);
Chris Lattner1f80a922004-02-18 22:01:21 +0000336
Hal Finkele852add2013-06-28 16:37:52 +0000337 if (OutputFile.empty()) 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) {
Dan Gohmand8db3762009-07-15 16:35:29 +0000343 errs() << ToolName << ": Error making unique filename: "
Rafael Espindolabecba2b2013-06-18 16:14:09 +0000344 << EC.message() << "\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
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000354 int RetVal = AI->ExecuteProgram(BitcodeFile, InputArgv, InputFile, OutputFile,
355 Error, AdditionalLinkerArgs, SharedObjs,
Dan Gohman414cf502008-12-08 04:02:47 +0000356 Timeout, MemoryLimit);
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000357 if (!Error->empty())
358 return OutputFile;
Chris Lattner4e0969b2004-07-24 07:53:26 +0000359
360 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) {
Dan Gohmanee051522009-07-16 15:30:09 +0000364 outs() << "\n"
Chris Lattner4e0969b2004-07-24 07:53:26 +0000365 "*** Program execution timed out! This mechanism is designed to handle\n"
366 " programs stuck in infinite loops gracefully. The -timeout option\n"
367 " can be used to change the timeout threshold or disable it completely\n"
368 " (with -timeout=0). This message is only displayed once.\n";
369 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,
387 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.
Davide Italianoab256212015-10-14 20:29:54 +0000398 CC::FileType FT = SafeInterpreter->OutputCode(BitcodeFile, OutputFile,
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000399 Error);
400 if (!Error.empty())
401 return "";
Misha Brukmand792c9b2003-07-24 18:17:43 +0000402
Chris Lattnerf8a84db2003-10-14 21:09:11 +0000403 std::string SharedObjectFile;
Davide Italianoab256212015-10-14 20:29:54 +0000404 bool Failure = cc->MakeSharedObject(OutputFile, FT, SharedObjectFile,
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000405 AdditionalLinkerArgs, Error);
406 if (!Error.empty())
407 return "";
408 if (Failure)
Chris Lattnerf8a84db2003-10-14 21:09:11 +0000409 exit(1);
Misha Brukmand792c9b2003-07-24 18:17:43 +0000410
411 // Remove the intermediate C file
Rafael Espindola34889ca2013-06-17 19:21:38 +0000412 sys::fs::remove(OutputFile);
Misha Brukmand792c9b2003-07-24 18:17:43 +0000413
Rafael Espindolac32573b2014-03-14 15:13:35 +0000414 return SharedObjectFile;
Misha Brukmand792c9b2003-07-24 18:17:43 +0000415}
416
Patrick Jenkinsc46c0382006-08-15 16:40:49 +0000417/// createReferenceFile - calls compileProgram and then records the output
Andrew Trick69a963e2011-02-08 18:07:10 +0000418/// into ReferenceOutputFile. Returns true if reference file created, false
Patrick Jenkinsc46c0382006-08-15 16:40:49 +0000419/// otherwise. Note: initializeExecutionEnvironment should be called BEFORE
420/// this function.
421///
Chris Lattner634bc042006-09-15 21:29:15 +0000422bool BugDriver::createReferenceFile(Module *M, const std::string &Filename) {
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000423 std::string Error;
424 compileProgram(Program, &Error);
425 if (!Error.empty())
Patrick Jenkinsc46c0382006-08-15 16:40:49 +0000426 return false;
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000427
Rafael Espindolac89b1ef2010-07-30 14:19:00 +0000428 ReferenceOutputFile = executeProgramSafely(Program, Filename, &Error);
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000429 if (!Error.empty()) {
430 errs() << Error;
Dan Gohman414cf502008-12-08 04:02:47 +0000431 if (Interpreter != SafeInterpreter) {
Dan Gohmand8db3762009-07-15 16:35:29 +0000432 errs() << "*** There is a bug running the \"safe\" backend. Either"
Eric Christopher64a23232012-03-23 05:50:46 +0000433 << " debug it (for example with the -run-jit bugpoint option,"
434 << " if JIT is being used as the \"safe\" backend), or fix the"
Dan Gohmand8db3762009-07-15 16:35:29 +0000435 << " error some other way.\n";
Patrick Jenkinsc46c0382006-08-15 16:40:49 +0000436 }
437 return false;
438 }
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000439 outs() << "\nReference output is: " << ReferenceOutputFile << "\n\n";
Patrick Jenkinsc46c0382006-08-15 16:40:49 +0000440 return true;
441}
Misha Brukmand792c9b2003-07-24 18:17:43 +0000442
Patrick Jenkinsc46c0382006-08-15 16:40:49 +0000443/// diffProgram - This method executes the specified module and diffs the
444/// output against the file specified by ReferenceOutputFile. If the output
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000445/// is different, 1 is returned. If there is a problem with the code
Andrew Trick55aeb552011-05-11 16:31:24 +0000446/// generator (e.g., llc crashes), this will set ErrMsg.
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000447///
Rafael Espindolac89b1ef2010-07-30 14:19:00 +0000448bool BugDriver::diffProgram(const Module *Program,
449 const std::string &BitcodeFile,
Misha Brukmand792c9b2003-07-24 18:17:43 +0000450 const std::string &SharedObject,
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000451 bool RemoveBitcode,
Rafael Espindolae4904602010-07-31 14:34:49 +0000452 std::string *ErrMsg) const {
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000453 // Execute the program, generating an output file...
Rafael Espindolabecba2b2013-06-18 16:14:09 +0000454 std::string Output(
Craig Toppere6cb63e2014-04-25 04:24:47 +0000455 executeProgram(Program, "", BitcodeFile, SharedObject, nullptr, ErrMsg));
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000456 if (!ErrMsg->empty())
457 return false;
Brian Gaeke35145be2004-02-11 18:37:32 +0000458
Chris Lattneraa997fb2003-08-01 20:29:45 +0000459 std::string Error;
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000460 bool FilesDifferent = false;
Rafael Espindola280e5cb2013-06-13 20:41:00 +0000461 if (int Diff = DiffFilesWithTolerance(ReferenceOutputFile,
Rafael Espindolabecba2b2013-06-18 16:14:09 +0000462 Output,
Chris Lattnerece10a42005-01-23 03:45:26 +0000463 AbsTolerance, RelTolerance, &Error)) {
464 if (Diff == 2) {
Dan Gohmand8db3762009-07-15 16:35:29 +0000465 errs() << "While diffing output: " << Error << '\n';
Chris Lattneraa997fb2003-08-01 20:29:45 +0000466 exit(1);
467 }
468 FilesDifferent = true;
469 }
David Goodwin73f4e3f2009-07-10 21:39:28 +0000470 else {
471 // Remove the generated output if there are no differences.
Rafael Espindolabecba2b2013-06-18 16:14:09 +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
481bool BugDriver::isExecutingJIT() {
482 return InterpreterSel == RunJIT;
483}
Brian Gaeke960707c2003-11-11 22:41:34 +0000484