blob: 690a48de35aeb1b5da673571d31813cfc3f7c17e [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 "
Mehdi Amini732afdd2016-10-08 19:41:06 +000063 "the bitcode. Useful for cross-compilation.")),
Justin Bogner8d0a0812016-09-02 01:21:37 +000064 cl::init(AutoPick));
Chris Lattner68efaa72003-04-23 20:31:37 +000065
Justin Bogner8d0a0812016-09-02 01:21:37 +000066cl::opt<OutputType> SafeInterpreterSel(
67 cl::desc("Specify \"safe\" i.e. known-good backend:"),
68 cl::values(clEnumValN(AutoPick, "safe-auto", "Use best guess"),
69 clEnumValN(RunLLC, "safe-run-llc", "Compile with LLC"),
70 clEnumValN(Custom, "safe-run-custom",
71 "Use -exec-command to define a command to execute "
Mehdi Amini732afdd2016-10-08 19:41:06 +000072 "the bitcode. Useful for cross-compilation.")),
Justin Bogner8d0a0812016-09-02 01:21:37 +000073 cl::init(AutoPick));
Dan Gohman414cf502008-12-08 04:02:47 +000074
Justin Bogner8d0a0812016-09-02 01:21:37 +000075cl::opt<std::string> SafeInterpreterPath(
76 "safe-path", cl::desc("Specify the path to the \"safe\" backend program"),
77 cl::init(""));
Dan Gohman414cf502008-12-08 04:02:47 +000078
Justin Bogner8d0a0812016-09-02 01:21:37 +000079cl::opt<bool> AppendProgramExitCode(
80 "append-exit-code",
81 cl::desc("Append the exit code to the output so it gets diff'd too"),
82 cl::init(false));
Reid Spencerd077fe72006-11-28 07:04:10 +000083
Justin Bogner8d0a0812016-09-02 01:21:37 +000084cl::opt<std::string>
85 InputFile("input", cl::init("/dev/null"),
86 cl::desc("Filename to pipe in as stdin (default: /dev/null)"));
Chris Lattnerdc92fa62003-10-14 22:24:31 +000087
Justin Bogner8d0a0812016-09-02 01:21:37 +000088cl::list<std::string>
89 AdditionalSOs("additional-so", cl::desc("Additional shared objects to load "
90 "into executing programs"));
Chris Lattner4e0969b2004-07-24 07:53:26 +000091
Justin Bogner8d0a0812016-09-02 01:21:37 +000092cl::list<std::string> AdditionalLinkerArgs(
93 "Xlinker", cl::desc("Additional arguments to pass to the linker"));
Anton Korobeynikovc53565c2008-04-28 20:53:48 +000094
Justin Bogner8d0a0812016-09-02 01:21:37 +000095cl::opt<std::string> CustomCompileCommand(
96 "compile-command", cl::init("llc"),
97 cl::desc("Command to compile the bitcode (use with -compile-custom) "
98 "(default: llc)"));
Andrew Trick8665d592011-02-08 18:20:48 +000099
Justin Bogner8d0a0812016-09-02 01:21:37 +0000100cl::opt<std::string> CustomExecCommand(
101 "exec-command", cl::init("simulate"),
102 cl::desc("Command to execute the bitcode (use with -run-custom) "
103 "(default: simulate)"));
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000104}
105
Brian Gaeke960707c2003-11-11 22:41:34 +0000106namespace llvm {
Justin Bogner8d0a0812016-09-02 01:21:37 +0000107// Anything specified after the --args option are taken as arguments to the
108// program being debugged.
109cl::list<std::string> InputArgv("args", cl::Positional,
110 cl::desc("<program arguments>..."),
111 cl::ZeroOrMore, cl::PositionalEatsArgs);
Daniel Dunbara53337f2009-09-07 19:26:11 +0000112
Justin Bogner8d0a0812016-09-02 01:21:37 +0000113cl::opt<std::string>
114 OutputPrefix("output-prefix", cl::init("bugpoint"),
115 cl::desc("Prefix to use for outputs (default: 'bugpoint')"));
Dan Gohman414cf502008-12-08 04:02:47 +0000116}
Brian Gaeke4a278f02004-05-04 21:09:16 +0000117
Dan Gohman414cf502008-12-08 04:02:47 +0000118namespace {
Justin Bogner8d0a0812016-09-02 01:21:37 +0000119cl::list<std::string> ToolArgv("tool-args", cl::Positional,
120 cl::desc("<tool arguments>..."), cl::ZeroOrMore,
121 cl::PositionalEatsArgs);
Dan Gohman414cf502008-12-08 04:02:47 +0000122
Justin Bogner8d0a0812016-09-02 01:21:37 +0000123cl::list<std::string> 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
Justin Bogner8d0a0812016-09-02 01:21:37 +0000127cl::opt<std::string> CCBinary("gcc", cl::init(""),
128 cl::desc("The gcc binary to use."));
Kalle Raiskila6be58292010-05-10 07:38:37 +0000129
Justin Bogner8d0a0812016-09-02 01:21:37 +0000130cl::list<std::string> CCToolArgv("gcc-tool-args", cl::Positional,
131 cl::desc("<gcc-tool arguments>..."),
132 cl::ZeroOrMore, cl::PositionalEatsArgs);
Chris Lattner2f1aa112004-01-14 03:38:37 +0000133}
Misha Brukman40feb362003-07-30 20:15:44 +0000134
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000135//===----------------------------------------------------------------------===//
136// BugDriver method implementation
137//
138
139/// initializeExecutionEnvironment - This method is used to set up the
140/// environment for executing LLVM programs.
141///
Justin Bogner1c039152016-09-06 17:18:22 +0000142Error BugDriver::initializeExecutionEnvironment() {
Dan Gohmanee051522009-07-16 15:30:09 +0000143 outs() << "Initializing execution environment: ";
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000144
Misha Brukman5bc6a8f2003-09-29 22:40:52 +0000145 // Create an instance of the AbstractInterpreter interface as specified on
146 // the command line
Craig Toppere6cb63e2014-04-25 04:24:47 +0000147 SafeInterpreter = nullptr;
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000148 std::string Message;
Brian Gaeke4a278f02004-05-04 21:09:16 +0000149
Davide Italianoab256212015-10-14 20:29:54 +0000150 if (CCBinary.empty()) {
Davide Italiano8a5d0432015-10-14 19:48:01 +0000151 if (sys::findProgramByName("clang"))
Davide Italianoab256212015-10-14 20:29:54 +0000152 CCBinary = "clang";
Davide Italiano8a5d0432015-10-14 19:48:01 +0000153 else
Davide Italianoab256212015-10-14 20:29:54 +0000154 CCBinary = "gcc";
Davide Italiano8a5d0432015-10-14 19:48:01 +0000155 }
156
Chris Lattner7709ec52003-05-03 03:19:41 +0000157 switch (InterpreterSel) {
Brian Gaeke9a0bdb12003-10-21 17:41:35 +0000158 case AutoPick:
Brian Gaeke9a0bdb12003-10-21 17:41:35 +0000159 if (!Interpreter) {
160 InterpreterSel = RunJIT;
Justin Bogner8d0a0812016-09-02 01:21:37 +0000161 Interpreter =
162 AbstractInterpreter::createJIT(getToolName(), Message, &ToolArgv);
Brian Gaeke9a0bdb12003-10-21 17:41:35 +0000163 }
164 if (!Interpreter) {
165 InterpreterSel = RunLLC;
Justin Bogner8d0a0812016-09-02 01:21:37 +0000166 Interpreter = AbstractInterpreter::createLLC(
167 getToolName(), Message, CCBinary, &ToolArgv, &CCToolArgv);
Brian Gaeke9a0bdb12003-10-21 17:41:35 +0000168 }
169 if (!Interpreter) {
170 InterpreterSel = RunLLI;
Justin Bogner8d0a0812016-09-02 01:21:37 +0000171 Interpreter =
172 AbstractInterpreter::createLLI(getToolName(), Message, &ToolArgv);
Brian Gaeke9a0bdb12003-10-21 17:41:35 +0000173 }
174 if (!Interpreter) {
175 InterpreterSel = AutoPick;
176 Message = "Sorry, I can't automatically select an interpreter!\n";
177 }
178 break;
Chris Lattner3f6e5222003-10-14 21:59:36 +0000179 case RunLLI:
Justin Bogner8d0a0812016-09-02 01:21:37 +0000180 Interpreter =
181 AbstractInterpreter::createLLI(getToolName(), Message, &ToolArgv);
Chris Lattner3f6e5222003-10-14 21:59:36 +0000182 break;
183 case RunLLC:
Chris Lattnerfd381322010-03-16 06:41:47 +0000184 case RunLLCIA:
Dan Gohman414cf502008-12-08 04:02:47 +0000185 case LLC_Safe:
Justin Bogner8d0a0812016-09-02 01:21:37 +0000186 Interpreter = AbstractInterpreter::createLLC(
187 getToolName(), Message, CCBinary, &ToolArgv, &CCToolArgv,
188 InterpreterSel == RunLLCIA);
Chris Lattner3f6e5222003-10-14 21:59:36 +0000189 break;
190 case RunJIT:
Justin Bogner8d0a0812016-09-02 01:21:37 +0000191 Interpreter =
192 AbstractInterpreter::createJIT(getToolName(), Message, &ToolArgv);
Chris Lattner3f6e5222003-10-14 21:59:36 +0000193 break;
Andrew Trick8665d592011-02-08 18:20:48 +0000194 case CompileCustom:
Justin Bogner8d0a0812016-09-02 01:21:37 +0000195 Interpreter = AbstractInterpreter::createCustomCompiler(
196 Message, CustomCompileCommand);
Andrew Trick8665d592011-02-08 18:20:48 +0000197 break;
Anton Korobeynikovc53565c2008-04-28 20:53:48 +0000198 case Custom:
Andrew Trick8665d592011-02-08 18:20:48 +0000199 Interpreter =
Justin Bogner8d0a0812016-09-02 01:21:37 +0000200 AbstractInterpreter::createCustomExecutor(Message, CustomExecCommand);
Anton Korobeynikovc53565c2008-04-28 20:53:48 +0000201 break;
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000202 }
Matthijs Kooijman3bb12762008-06-12 13:09:43 +0000203 if (!Interpreter)
Dan Gohmand8db3762009-07-15 16:35:29 +0000204 errs() << Message;
Matthijs Kooijman3bb12762008-06-12 13:09:43 +0000205 else // Display informational messages on stdout instead of stderr
Dan Gohmanee051522009-07-16 15:30:09 +0000206 outs() << Message;
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000207
Dan Gohman414cf502008-12-08 04:02:47 +0000208 std::string Path = SafeInterpreterPath;
209 if (Path.empty())
210 Path = getToolName();
211 std::vector<std::string> SafeToolArgs = SafeToolArgv;
212 switch (SafeInterpreterSel) {
213 case AutoPick:
Dan Gohman414cf502008-12-08 04:02:47 +0000214 // In "llc-safe" mode, default to using LLC as the "safe" backend.
Justin Bogner8d0a0812016-09-02 01:21:37 +0000215 if (!SafeInterpreter && InterpreterSel == LLC_Safe) {
Dan Gohman414cf502008-12-08 04:02:47 +0000216 SafeInterpreterSel = RunLLC;
217 SafeToolArgs.push_back("--relocation-model=pic");
Justin Bogner8d0a0812016-09-02 01:21:37 +0000218 SafeInterpreter = AbstractInterpreter::createLLC(
219 Path.c_str(), Message, CCBinary, &SafeToolArgs, &CCToolArgv);
Dan Gohman414cf502008-12-08 04:02:47 +0000220 }
221
Justin Bogner8d0a0812016-09-02 01:21:37 +0000222 if (!SafeInterpreter && InterpreterSel != RunLLC &&
Dan Gohman414cf502008-12-08 04:02:47 +0000223 InterpreterSel != RunJIT) {
224 SafeInterpreterSel = RunLLC;
225 SafeToolArgs.push_back("--relocation-model=pic");
Justin Bogner8d0a0812016-09-02 01:21:37 +0000226 SafeInterpreter = AbstractInterpreter::createLLC(
227 Path.c_str(), Message, CCBinary, &SafeToolArgs, &CCToolArgv);
Dan Gohman414cf502008-12-08 04:02:47 +0000228 }
229 if (!SafeInterpreter) {
230 SafeInterpreterSel = AutoPick;
Saleem Abdulrasool5f8609b2013-01-24 16:49:14 +0000231 Message = "Sorry, I can't automatically select a safe interpreter!\n";
Dan Gohman414cf502008-12-08 04:02:47 +0000232 }
233 break;
234 case RunLLC:
Chris Lattnerfd381322010-03-16 06:41:47 +0000235 case RunLLCIA:
Dan Gohman414cf502008-12-08 04:02:47 +0000236 SafeToolArgs.push_back("--relocation-model=pic");
Justin Bogner8d0a0812016-09-02 01:21:37 +0000237 SafeInterpreter = AbstractInterpreter::createLLC(
238 Path.c_str(), Message, CCBinary, &SafeToolArgs, &CCToolArgv,
239 SafeInterpreterSel == RunLLCIA);
Dan Gohman414cf502008-12-08 04:02:47 +0000240 break;
Dan Gohman414cf502008-12-08 04:02:47 +0000241 case Custom:
Andrew Trick8665d592011-02-08 18:20:48 +0000242 SafeInterpreter =
Justin Bogner8d0a0812016-09-02 01:21:37 +0000243 AbstractInterpreter::createCustomExecutor(Message, CustomExecCommand);
Dan Gohman414cf502008-12-08 04:02:47 +0000244 break;
245 default:
246 Message = "Sorry, this back-end is not supported by bugpoint as the "
247 "\"safe\" backend right now!\n";
248 break;
Chris Lattner898de4a2004-02-18 20:52:02 +0000249 }
Justin Bogner8d0a0812016-09-02 01:21:37 +0000250 if (!SafeInterpreter) {
251 outs() << Message << "\nExiting.\n";
252 exit(1);
253 }
Andrew Trick69a963e2011-02-08 18:07:10 +0000254
Davide Italianoab256212015-10-14 20:29:54 +0000255 cc = CC::create(Message, CCBinary, &CCToolArgv);
Justin Bogner8d0a0812016-09-02 01:21:37 +0000256 if (!cc) {
257 outs() << Message << "\nExiting.\n";
258 exit(1);
259 }
Misha Brukman0fd31722003-07-24 21:59:10 +0000260
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000261 // If there was an error creating the selected interpreter, quit with error.
Justin Bogner1c039152016-09-06 17:18:22 +0000262 if (Interpreter == nullptr)
263 return make_error<StringError>("Failed to init execution environment",
264 inconvertibleErrorCode());
265 return Error::success();
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000266}
267
Rafael Espindolaf6074ed2018-02-14 21:44:34 +0000268/// Try to compile the specified module, returning false and setting Error if an
269/// error occurs. This is used for code generation crash testing.
270Error BugDriver::compileProgram(Module &M) const {
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000271 // Emit the program to a bitcode file...
Rafael Espindolab60bb692017-11-16 21:40:10 +0000272 auto Temp =
273 sys::fs::TempFile::create(OutputPrefix + "-test-program-%%%%%%%.bc");
274 if (!Temp) {
275 errs() << ToolName
276 << ": Error making unique filename: " << toString(Temp.takeError())
Dan Gohmand8db3762009-07-15 16:35:29 +0000277 << "\n";
Reid Spencere4ca7222006-08-23 20:34:57 +0000278 exit(1);
279 }
Rafael Espindolab60bb692017-11-16 21:40:10 +0000280 DiscardTemp Discard{*Temp};
281 if (writeProgramToFile(Temp->FD, M)) {
282 errs() << ToolName << ": Error emitting bitcode to file '" << Temp->TmpName
Rafael Espindolabecba2b2013-06-18 16:14:09 +0000283 << "'!\n";
Chris Lattner96d41dd2004-02-18 23:25:22 +0000284 exit(1);
285 }
286
Chris Lattner96d41dd2004-02-18 23:25:22 +0000287 // Actually compile the program!
Rafael Espindolab60bb692017-11-16 21:40:10 +0000288 return Interpreter->compileProgram(Temp->TmpName, Timeout, MemoryLimit);
Chris Lattner96d41dd2004-02-18 23:25:22 +0000289}
290
Rafael Espindolaf6074ed2018-02-14 21:44:34 +0000291/// This method runs "Program", capturing the output of the program to a file,
292/// returning the filename of the file. A recommended filename may be
293/// optionally specified.
294Expected<std::string> BugDriver::executeProgram(const Module &Program,
Justin Bogner1c039152016-09-06 17:18:22 +0000295 std::string OutputFile,
296 std::string BitcodeFile,
297 const std::string &SharedObj,
298 AbstractInterpreter *AI) const {
Justin Bogner8d0a0812016-09-02 01:21:37 +0000299 if (!AI)
300 AI = Interpreter;
Chris Lattner3f6e5222003-10-14 21:59:36 +0000301 assert(AI && "Interpreter should have been created already!");
Don Hinton3863d542018-09-18 18:39:27 +0000302 bool CreatedBitcode = false;
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000303 if (BitcodeFile.empty()) {
304 // Emit the program to a bitcode file...
Don Hinton3863d542018-09-18 18:39:27 +0000305 SmallString<128> UniqueFilename;
306 int UniqueFD;
307 std::error_code EC = sys::fs::createUniqueFile(
308 OutputPrefix + "-test-program-%%%%%%%.bc", UniqueFD, UniqueFilename);
309 if (EC) {
310 errs() << ToolName << ": Error making unique filename: " << EC.message()
Justin Bogner8d0a0812016-09-02 01:21:37 +0000311 << "!\n";
Reid Spencere4ca7222006-08-23 20:34:57 +0000312 exit(1);
313 }
Don Hinton3863d542018-09-18 18:39:27 +0000314 BitcodeFile = UniqueFilename.str();
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000315
Don Hinton3863d542018-09-18 18:39:27 +0000316 if (writeProgramToFile(BitcodeFile, UniqueFD, Program)) {
Justin Bogner8d0a0812016-09-02 01:21:37 +0000317 errs() << ToolName << ": Error emitting bitcode to file '" << BitcodeFile
318 << "'!\n";
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000319 exit(1);
320 }
Don Hinton3863d542018-09-18 18:39:27 +0000321 CreatedBitcode = true;
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000322 }
323
Don Hinton3863d542018-09-18 18:39:27 +0000324 // Remove the temporary bitcode file when we are done.
325 std::string BitcodePath(BitcodeFile);
326 FileRemover BitcodeFileRemover(BitcodePath, CreatedBitcode && !SaveTemps);
327
Justin Bogner8d0a0812016-09-02 01:21:37 +0000328 if (OutputFile.empty())
329 OutputFile = OutputPrefix + "-execution-output-%%%%%%%";
Misha Brukmand792c9b2003-07-24 18:17:43 +0000330
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000331 // Check to see if this is a valid output filename...
Rafael Espindolabecba2b2013-06-18 16:14:09 +0000332 SmallString<128> UniqueFile;
Rafael Espindola4453e42942014-06-13 03:07:50 +0000333 std::error_code EC = sys::fs::createUniqueFile(OutputFile, UniqueFile);
Rafael Espindolabecba2b2013-06-18 16:14:09 +0000334 if (EC) {
Justin Bogner8d0a0812016-09-02 01:21:37 +0000335 errs() << ToolName << ": Error making unique filename: " << EC.message()
336 << "\n";
Reid Spencere4ca7222006-08-23 20:34:57 +0000337 exit(1);
338 }
Rafael Espindolabecba2b2013-06-18 16:14:09 +0000339 OutputFile = UniqueFile.str();
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000340
Chris Lattner3f6e5222003-10-14 21:59:36 +0000341 // Figure out which shared objects to run, if any.
Chris Lattnerdc92fa62003-10-14 22:24:31 +0000342 std::vector<std::string> SharedObjs(AdditionalSOs);
Chris Lattner3f6e5222003-10-14 21:59:36 +0000343 if (!SharedObj.empty())
344 SharedObjs.push_back(SharedObj);
345
Justin Bogner1c039152016-09-06 17:18:22 +0000346 Expected<int> RetVal = AI->ExecuteProgram(BitcodeFile, InputArgv, InputFile,
347 OutputFile, AdditionalLinkerArgs,
348 SharedObjs, Timeout, MemoryLimit);
349 if (Error E = RetVal.takeError())
350 return std::move(E);
Chris Lattner4e0969b2004-07-24 07:53:26 +0000351
Justin Bogner1c039152016-09-06 17:18:22 +0000352 if (*RetVal == -1) {
Dan Gohmand8db3762009-07-15 16:35:29 +0000353 errs() << "<timeout>";
Chris Lattner4e0969b2004-07-24 07:53:26 +0000354 static bool FirstTimeout = true;
355 if (FirstTimeout) {
Justin Bogner8d0a0812016-09-02 01:21:37 +0000356 outs()
357 << "\n"
358 "*** Program execution timed out! This mechanism is designed to "
359 "handle\n"
360 " programs stuck in infinite loops gracefully. The -timeout "
361 "option\n"
362 " can be used to change the timeout threshold or disable it "
363 "completely\n"
364 " (with -timeout=0). This message is only displayed once.\n";
Chris Lattner4e0969b2004-07-24 07:53:26 +0000365 FirstTimeout = false;
366 }
367 }
Chris Lattner3f6e5222003-10-14 21:59:36 +0000368
Reid Spencerd077fe72006-11-28 07:04:10 +0000369 if (AppendProgramExitCode) {
370 std::ofstream outFile(OutputFile.c_str(), std::ios_base::app);
Justin Bogner1c039152016-09-06 17:18:22 +0000371 outFile << "exit " << *RetVal << '\n';
Reid Spencerd077fe72006-11-28 07:04:10 +0000372 outFile.close();
373 }
374
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000375 // Return the filename we captured the output to.
376 return OutputFile;
377}
378
Rafael Espindolaf6074ed2018-02-14 21:44:34 +0000379/// Used to create reference output with the "safe" backend, if reference output
380/// is not provided.
Justin Bogner1c039152016-09-06 17:18:22 +0000381Expected<std::string>
Rafael Espindolaf6074ed2018-02-14 21:44:34 +0000382BugDriver::executeProgramSafely(const Module &Program,
Justin Bogner1c039152016-09-06 17:18:22 +0000383 const std::string &OutputFile) const {
384 return executeProgram(Program, OutputFile, "", "", SafeInterpreter);
Brian Gaeke35145be2004-02-11 18:37:32 +0000385}
Misha Brukmand792c9b2003-07-24 18:17:43 +0000386
Justin Bogner1c039152016-09-06 17:18:22 +0000387Expected<std::string>
388BugDriver::compileSharedObject(const std::string &BitcodeFile) {
Misha Brukmand792c9b2003-07-24 18:17:43 +0000389 assert(Interpreter && "Interpreter should have been created already!");
Rafael Espindola34889ca2013-06-17 19:21:38 +0000390 std::string OutputFile;
Misha Brukmand792c9b2003-07-24 18:17:43 +0000391
Dan Gohman414cf502008-12-08 04:02:47 +0000392 // Using the known-good backend.
Justin Bogner1c039152016-09-06 17:18:22 +0000393 Expected<CC::FileType> FT =
394 SafeInterpreter->OutputCode(BitcodeFile, OutputFile);
395 if (Error E = FT.takeError())
396 return std::move(E);
Misha Brukmand792c9b2003-07-24 18:17:43 +0000397
Chris Lattnerf8a84db2003-10-14 21:09:11 +0000398 std::string SharedObjectFile;
Justin Bogner1c039152016-09-06 17:18:22 +0000399 if (Error E = cc->MakeSharedObject(OutputFile, *FT, SharedObjectFile,
400 AdditionalLinkerArgs))
401 return std::move(E);
Misha Brukmand792c9b2003-07-24 18:17:43 +0000402
403 // Remove the intermediate C file
Rafael Espindola34889ca2013-06-17 19:21:38 +0000404 sys::fs::remove(OutputFile);
Misha Brukmand792c9b2003-07-24 18:17:43 +0000405
Rafael Espindolac32573b2014-03-14 15:13:35 +0000406 return SharedObjectFile;
Misha Brukmand792c9b2003-07-24 18:17:43 +0000407}
408
Rafael Espindolaf6074ed2018-02-14 21:44:34 +0000409/// Calls compileProgram and then records the output into ReferenceOutputFile.
410/// Returns true if reference file created, false otherwise. Note:
411/// initializeExecutionEnvironment should be called BEFORE this function.
412Error BugDriver::createReferenceFile(Module &M, const std::string &Filename) {
413 if (Error E = compileProgram(*Program))
Justin Bogner1c039152016-09-06 17:18:22 +0000414 return E;
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000415
Rafael Espindolaf6074ed2018-02-14 21:44:34 +0000416 Expected<std::string> Result = executeProgramSafely(*Program, Filename);
Justin Bogner1c039152016-09-06 17:18:22 +0000417 if (Error E = Result.takeError()) {
Dan Gohman414cf502008-12-08 04:02:47 +0000418 if (Interpreter != SafeInterpreter) {
Justin Bogner1c039152016-09-06 17:18:22 +0000419 E = joinErrors(
420 std::move(E),
421 make_error<StringError>(
422 "*** There is a bug running the \"safe\" backend. Either"
423 " debug it (for example with the -run-jit bugpoint option,"
424 " if JIT is being used as the \"safe\" backend), or fix the"
425 " error some other way.\n",
426 inconvertibleErrorCode()));
Patrick Jenkinsc46c0382006-08-15 16:40:49 +0000427 }
Justin Bogner1c039152016-09-06 17:18:22 +0000428 return E;
Patrick Jenkinsc46c0382006-08-15 16:40:49 +0000429 }
Justin Bogner1c039152016-09-06 17:18:22 +0000430 ReferenceOutputFile = *Result;
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000431 outs() << "\nReference output is: " << ReferenceOutputFile << "\n\n";
Justin Bogner1c039152016-09-06 17:18:22 +0000432 return Error::success();
Patrick Jenkinsc46c0382006-08-15 16:40:49 +0000433}
Misha Brukmand792c9b2003-07-24 18:17:43 +0000434
Rafael Espindolaf6074ed2018-02-14 21:44:34 +0000435/// This method executes the specified module and diffs the output against the
436/// file specified by ReferenceOutputFile. If the output is different, 1 is
437/// returned. If there is a problem with the code generator (e.g., llc
438/// crashes), this will set ErrMsg.
439Expected<bool> BugDriver::diffProgram(const Module &Program,
Justin Bogner1c039152016-09-06 17:18:22 +0000440 const std::string &BitcodeFile,
441 const std::string &SharedObject,
442 bool RemoveBitcode) const {
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000443 // Execute the program, generating an output file...
Justin Bogner1c039152016-09-06 17:18:22 +0000444 Expected<std::string> Output =
445 executeProgram(Program, "", BitcodeFile, SharedObject, nullptr);
446 if (Error E = Output.takeError())
447 return std::move(E);
Brian Gaeke35145be2004-02-11 18:37:32 +0000448
Chris Lattneraa997fb2003-08-01 20:29:45 +0000449 std::string Error;
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000450 bool FilesDifferent = false;
Justin Bogner1c039152016-09-06 17:18:22 +0000451 if (int Diff = DiffFilesWithTolerance(ReferenceOutputFile, *Output,
Chris Lattnerece10a42005-01-23 03:45:26 +0000452 AbsTolerance, RelTolerance, &Error)) {
453 if (Diff == 2) {
Dan Gohmand8db3762009-07-15 16:35:29 +0000454 errs() << "While diffing output: " << Error << '\n';
Chris Lattneraa997fb2003-08-01 20:29:45 +0000455 exit(1);
456 }
457 FilesDifferent = true;
Justin Bogner8d0a0812016-09-02 01:21:37 +0000458 } else {
David Goodwin73f4e3f2009-07-10 21:39:28 +0000459 // Remove the generated output if there are no differences.
Justin Bogner1c039152016-09-06 17:18:22 +0000460 sys::fs::remove(*Output);
David Goodwin73f4e3f2009-07-10 21:39:28 +0000461 }
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000462
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000463 // Remove the bitcode file if we are supposed to.
464 if (RemoveBitcode)
Rafael Espindolabecba2b2013-06-18 16:14:09 +0000465 sys::fs::remove(BitcodeFile);
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000466 return FilesDifferent;
467}
Misha Brukman539f9592003-07-28 19:16:14 +0000468
Justin Bogner8d0a0812016-09-02 01:21:37 +0000469bool BugDriver::isExecutingJIT() { return InterpreterSel == RunJIT; }