blob: 1b86b103d83546d08e361cdb0223f6737dbe4449 [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()) {
Brian Gesiak5cc89202018-12-10 00:56:13 +0000151 if (ErrorOr<std::string> ClangPath =
152 FindProgramByName("clang", getToolName(), &AbsTolerance))
153 CCBinary = *ClangPath;
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;
Justin Bogner8d0a0812016-09-02 01:21:37 +0000162 Interpreter =
163 AbstractInterpreter::createJIT(getToolName(), Message, &ToolArgv);
Brian Gaeke9a0bdb12003-10-21 17:41:35 +0000164 }
165 if (!Interpreter) {
166 InterpreterSel = RunLLC;
Justin Bogner8d0a0812016-09-02 01:21:37 +0000167 Interpreter = AbstractInterpreter::createLLC(
168 getToolName(), Message, CCBinary, &ToolArgv, &CCToolArgv);
Brian Gaeke9a0bdb12003-10-21 17:41:35 +0000169 }
170 if (!Interpreter) {
171 InterpreterSel = RunLLI;
Justin Bogner8d0a0812016-09-02 01:21:37 +0000172 Interpreter =
173 AbstractInterpreter::createLLI(getToolName(), Message, &ToolArgv);
Brian Gaeke9a0bdb12003-10-21 17:41:35 +0000174 }
175 if (!Interpreter) {
176 InterpreterSel = AutoPick;
177 Message = "Sorry, I can't automatically select an interpreter!\n";
178 }
179 break;
Chris Lattner3f6e5222003-10-14 21:59:36 +0000180 case RunLLI:
Justin Bogner8d0a0812016-09-02 01:21:37 +0000181 Interpreter =
182 AbstractInterpreter::createLLI(getToolName(), Message, &ToolArgv);
Chris Lattner3f6e5222003-10-14 21:59:36 +0000183 break;
184 case RunLLC:
Chris Lattnerfd381322010-03-16 06:41:47 +0000185 case RunLLCIA:
Dan Gohman414cf502008-12-08 04:02:47 +0000186 case LLC_Safe:
Justin Bogner8d0a0812016-09-02 01:21:37 +0000187 Interpreter = AbstractInterpreter::createLLC(
188 getToolName(), Message, CCBinary, &ToolArgv, &CCToolArgv,
189 InterpreterSel == RunLLCIA);
Chris Lattner3f6e5222003-10-14 21:59:36 +0000190 break;
191 case RunJIT:
Justin Bogner8d0a0812016-09-02 01:21:37 +0000192 Interpreter =
193 AbstractInterpreter::createJIT(getToolName(), Message, &ToolArgv);
Chris Lattner3f6e5222003-10-14 21:59:36 +0000194 break;
Andrew Trick8665d592011-02-08 18:20:48 +0000195 case CompileCustom:
Justin Bogner8d0a0812016-09-02 01:21:37 +0000196 Interpreter = AbstractInterpreter::createCustomCompiler(
Brian Gesiak5cc89202018-12-10 00:56:13 +0000197 getToolName(), Message, CustomCompileCommand);
Andrew Trick8665d592011-02-08 18:20:48 +0000198 break;
Anton Korobeynikovc53565c2008-04-28 20:53:48 +0000199 case Custom:
Brian Gesiak5cc89202018-12-10 00:56:13 +0000200 Interpreter = AbstractInterpreter::createCustomExecutor(
201 getToolName(), Message, CustomExecCommand);
Anton Korobeynikovc53565c2008-04-28 20:53:48 +0000202 break;
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000203 }
Matthijs Kooijman3bb12762008-06-12 13:09:43 +0000204 if (!Interpreter)
Dan Gohmand8db3762009-07-15 16:35:29 +0000205 errs() << Message;
Matthijs Kooijman3bb12762008-06-12 13:09:43 +0000206 else // Display informational messages on stdout instead of stderr
Dan Gohmanee051522009-07-16 15:30:09 +0000207 outs() << Message;
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000208
Dan Gohman414cf502008-12-08 04:02:47 +0000209 std::string Path = SafeInterpreterPath;
210 if (Path.empty())
211 Path = getToolName();
212 std::vector<std::string> SafeToolArgs = SafeToolArgv;
213 switch (SafeInterpreterSel) {
214 case AutoPick:
Dan Gohman414cf502008-12-08 04:02:47 +0000215 // In "llc-safe" mode, default to using LLC as the "safe" backend.
Justin Bogner8d0a0812016-09-02 01:21:37 +0000216 if (!SafeInterpreter && InterpreterSel == LLC_Safe) {
Dan Gohman414cf502008-12-08 04:02:47 +0000217 SafeInterpreterSel = RunLLC;
218 SafeToolArgs.push_back("--relocation-model=pic");
Justin Bogner8d0a0812016-09-02 01:21:37 +0000219 SafeInterpreter = AbstractInterpreter::createLLC(
220 Path.c_str(), Message, CCBinary, &SafeToolArgs, &CCToolArgv);
Dan Gohman414cf502008-12-08 04:02:47 +0000221 }
222
Justin Bogner8d0a0812016-09-02 01:21:37 +0000223 if (!SafeInterpreter && InterpreterSel != RunLLC &&
Dan Gohman414cf502008-12-08 04:02:47 +0000224 InterpreterSel != RunJIT) {
225 SafeInterpreterSel = RunLLC;
226 SafeToolArgs.push_back("--relocation-model=pic");
Justin Bogner8d0a0812016-09-02 01:21:37 +0000227 SafeInterpreter = AbstractInterpreter::createLLC(
228 Path.c_str(), Message, CCBinary, &SafeToolArgs, &CCToolArgv);
Dan Gohman414cf502008-12-08 04:02:47 +0000229 }
230 if (!SafeInterpreter) {
231 SafeInterpreterSel = AutoPick;
Saleem Abdulrasool5f8609b2013-01-24 16:49:14 +0000232 Message = "Sorry, I can't automatically select a safe interpreter!\n";
Dan Gohman414cf502008-12-08 04:02:47 +0000233 }
234 break;
235 case RunLLC:
Chris Lattnerfd381322010-03-16 06:41:47 +0000236 case RunLLCIA:
Dan Gohman414cf502008-12-08 04:02:47 +0000237 SafeToolArgs.push_back("--relocation-model=pic");
Justin Bogner8d0a0812016-09-02 01:21:37 +0000238 SafeInterpreter = AbstractInterpreter::createLLC(
239 Path.c_str(), Message, CCBinary, &SafeToolArgs, &CCToolArgv,
240 SafeInterpreterSel == RunLLCIA);
Dan Gohman414cf502008-12-08 04:02:47 +0000241 break;
Dan Gohman414cf502008-12-08 04:02:47 +0000242 case Custom:
Brian Gesiak5cc89202018-12-10 00:56:13 +0000243 SafeInterpreter = AbstractInterpreter::createCustomExecutor(
244 getToolName(), Message, CustomExecCommand);
Dan Gohman414cf502008-12-08 04:02:47 +0000245 break;
246 default:
247 Message = "Sorry, this back-end is not supported by bugpoint as the "
248 "\"safe\" backend right now!\n";
249 break;
Chris Lattner898de4a2004-02-18 20:52:02 +0000250 }
Justin Bogner8d0a0812016-09-02 01:21:37 +0000251 if (!SafeInterpreter) {
252 outs() << Message << "\nExiting.\n";
253 exit(1);
254 }
Andrew Trick69a963e2011-02-08 18:07:10 +0000255
Brian Gesiak5cc89202018-12-10 00:56:13 +0000256 cc = CC::create(getToolName(), Message, CCBinary, &CCToolArgv);
Justin Bogner8d0a0812016-09-02 01:21:37 +0000257 if (!cc) {
258 outs() << Message << "\nExiting.\n";
259 exit(1);
260 }
Misha Brukman0fd31722003-07-24 21:59:10 +0000261
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000262 // If there was an error creating the selected interpreter, quit with error.
Justin Bogner1c039152016-09-06 17:18:22 +0000263 if (Interpreter == nullptr)
264 return make_error<StringError>("Failed to init execution environment",
265 inconvertibleErrorCode());
266 return Error::success();
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000267}
268
Rafael Espindolaf6074ed2018-02-14 21:44:34 +0000269/// Try to compile the specified module, returning false and setting Error if an
270/// error occurs. This is used for code generation crash testing.
271Error BugDriver::compileProgram(Module &M) const {
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000272 // Emit the program to a bitcode file...
Rafael Espindolab60bb692017-11-16 21:40:10 +0000273 auto Temp =
274 sys::fs::TempFile::create(OutputPrefix + "-test-program-%%%%%%%.bc");
275 if (!Temp) {
276 errs() << ToolName
277 << ": Error making unique filename: " << toString(Temp.takeError())
Dan Gohmand8db3762009-07-15 16:35:29 +0000278 << "\n";
Reid Spencere4ca7222006-08-23 20:34:57 +0000279 exit(1);
280 }
Rafael Espindolab60bb692017-11-16 21:40:10 +0000281 DiscardTemp Discard{*Temp};
282 if (writeProgramToFile(Temp->FD, M)) {
283 errs() << ToolName << ": Error emitting bitcode to file '" << Temp->TmpName
Rafael Espindolabecba2b2013-06-18 16:14:09 +0000284 << "'!\n";
Chris Lattner96d41dd2004-02-18 23:25:22 +0000285 exit(1);
286 }
287
Chris Lattner96d41dd2004-02-18 23:25:22 +0000288 // Actually compile the program!
Rafael Espindolab60bb692017-11-16 21:40:10 +0000289 return Interpreter->compileProgram(Temp->TmpName, Timeout, MemoryLimit);
Chris Lattner96d41dd2004-02-18 23:25:22 +0000290}
291
Rafael Espindolaf6074ed2018-02-14 21:44:34 +0000292/// This method runs "Program", capturing the output of the program to a file,
293/// returning the filename of the file. A recommended filename may be
294/// optionally specified.
295Expected<std::string> BugDriver::executeProgram(const Module &Program,
Justin Bogner1c039152016-09-06 17:18:22 +0000296 std::string OutputFile,
297 std::string BitcodeFile,
298 const std::string &SharedObj,
299 AbstractInterpreter *AI) const {
Justin Bogner8d0a0812016-09-02 01:21:37 +0000300 if (!AI)
301 AI = Interpreter;
Chris Lattner3f6e5222003-10-14 21:59:36 +0000302 assert(AI && "Interpreter should have been created already!");
Don Hinton3863d542018-09-18 18:39:27 +0000303 bool CreatedBitcode = false;
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000304 if (BitcodeFile.empty()) {
305 // Emit the program to a bitcode file...
Don Hinton3863d542018-09-18 18:39:27 +0000306 SmallString<128> UniqueFilename;
307 int UniqueFD;
308 std::error_code EC = sys::fs::createUniqueFile(
309 OutputPrefix + "-test-program-%%%%%%%.bc", UniqueFD, UniqueFilename);
310 if (EC) {
311 errs() << ToolName << ": Error making unique filename: " << EC.message()
Justin Bogner8d0a0812016-09-02 01:21:37 +0000312 << "!\n";
Reid Spencere4ca7222006-08-23 20:34:57 +0000313 exit(1);
314 }
Don Hinton3863d542018-09-18 18:39:27 +0000315 BitcodeFile = UniqueFilename.str();
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000316
Don Hinton3863d542018-09-18 18:39:27 +0000317 if (writeProgramToFile(BitcodeFile, UniqueFD, Program)) {
Justin Bogner8d0a0812016-09-02 01:21:37 +0000318 errs() << ToolName << ": Error emitting bitcode to file '" << BitcodeFile
319 << "'!\n";
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000320 exit(1);
321 }
Don Hinton3863d542018-09-18 18:39:27 +0000322 CreatedBitcode = true;
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000323 }
324
Don Hinton3863d542018-09-18 18:39:27 +0000325 // Remove the temporary bitcode file when we are done.
326 std::string BitcodePath(BitcodeFile);
327 FileRemover BitcodeFileRemover(BitcodePath, CreatedBitcode && !SaveTemps);
328
Justin Bogner8d0a0812016-09-02 01:21:37 +0000329 if (OutputFile.empty())
330 OutputFile = OutputPrefix + "-execution-output-%%%%%%%";
Misha Brukmand792c9b2003-07-24 18:17:43 +0000331
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000332 // Check to see if this is a valid output filename...
Rafael Espindolabecba2b2013-06-18 16:14:09 +0000333 SmallString<128> UniqueFile;
Rafael Espindola4453e42942014-06-13 03:07:50 +0000334 std::error_code EC = sys::fs::createUniqueFile(OutputFile, UniqueFile);
Rafael Espindolabecba2b2013-06-18 16:14:09 +0000335 if (EC) {
Justin Bogner8d0a0812016-09-02 01:21:37 +0000336 errs() << ToolName << ": Error making unique filename: " << EC.message()
337 << "\n";
Reid Spencere4ca7222006-08-23 20:34:57 +0000338 exit(1);
339 }
Rafael Espindolabecba2b2013-06-18 16:14:09 +0000340 OutputFile = UniqueFile.str();
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000341
Chris Lattner3f6e5222003-10-14 21:59:36 +0000342 // Figure out which shared objects to run, if any.
Chris Lattnerdc92fa62003-10-14 22:24:31 +0000343 std::vector<std::string> SharedObjs(AdditionalSOs);
Chris Lattner3f6e5222003-10-14 21:59:36 +0000344 if (!SharedObj.empty())
345 SharedObjs.push_back(SharedObj);
346
Justin Bogner1c039152016-09-06 17:18:22 +0000347 Expected<int> RetVal = AI->ExecuteProgram(BitcodeFile, InputArgv, InputFile,
348 OutputFile, AdditionalLinkerArgs,
349 SharedObjs, Timeout, MemoryLimit);
350 if (Error E = RetVal.takeError())
351 return std::move(E);
Chris Lattner4e0969b2004-07-24 07:53:26 +0000352
Justin Bogner1c039152016-09-06 17:18:22 +0000353 if (*RetVal == -1) {
Dan Gohmand8db3762009-07-15 16:35:29 +0000354 errs() << "<timeout>";
Chris Lattner4e0969b2004-07-24 07:53:26 +0000355 static bool FirstTimeout = true;
356 if (FirstTimeout) {
Justin Bogner8d0a0812016-09-02 01:21:37 +0000357 outs()
358 << "\n"
359 "*** Program execution timed out! This mechanism is designed to "
360 "handle\n"
361 " programs stuck in infinite loops gracefully. The -timeout "
362 "option\n"
363 " can be used to change the timeout threshold or disable it "
364 "completely\n"
365 " (with -timeout=0). This message is only displayed once.\n";
Chris Lattner4e0969b2004-07-24 07:53:26 +0000366 FirstTimeout = false;
367 }
368 }
Chris Lattner3f6e5222003-10-14 21:59:36 +0000369
Reid Spencerd077fe72006-11-28 07:04:10 +0000370 if (AppendProgramExitCode) {
371 std::ofstream outFile(OutputFile.c_str(), std::ios_base::app);
Justin Bogner1c039152016-09-06 17:18:22 +0000372 outFile << "exit " << *RetVal << '\n';
Reid Spencerd077fe72006-11-28 07:04:10 +0000373 outFile.close();
374 }
375
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000376 // Return the filename we captured the output to.
377 return OutputFile;
378}
379
Rafael Espindolaf6074ed2018-02-14 21:44:34 +0000380/// Used to create reference output with the "safe" backend, if reference output
381/// is not provided.
Justin Bogner1c039152016-09-06 17:18:22 +0000382Expected<std::string>
Rafael Espindolaf6074ed2018-02-14 21:44:34 +0000383BugDriver::executeProgramSafely(const Module &Program,
Justin Bogner1c039152016-09-06 17:18:22 +0000384 const std::string &OutputFile) const {
385 return executeProgram(Program, OutputFile, "", "", SafeInterpreter);
Brian Gaeke35145be2004-02-11 18:37:32 +0000386}
Misha Brukmand792c9b2003-07-24 18:17:43 +0000387
Justin Bogner1c039152016-09-06 17:18:22 +0000388Expected<std::string>
389BugDriver::compileSharedObject(const std::string &BitcodeFile) {
Misha Brukmand792c9b2003-07-24 18:17:43 +0000390 assert(Interpreter && "Interpreter should have been created already!");
Rafael Espindola34889ca2013-06-17 19:21:38 +0000391 std::string OutputFile;
Misha Brukmand792c9b2003-07-24 18:17:43 +0000392
Dan Gohman414cf502008-12-08 04:02:47 +0000393 // Using the known-good backend.
Justin Bogner1c039152016-09-06 17:18:22 +0000394 Expected<CC::FileType> FT =
395 SafeInterpreter->OutputCode(BitcodeFile, OutputFile);
396 if (Error E = FT.takeError())
397 return std::move(E);
Misha Brukmand792c9b2003-07-24 18:17:43 +0000398
Chris Lattnerf8a84db2003-10-14 21:09:11 +0000399 std::string SharedObjectFile;
Justin Bogner1c039152016-09-06 17:18:22 +0000400 if (Error E = cc->MakeSharedObject(OutputFile, *FT, SharedObjectFile,
401 AdditionalLinkerArgs))
402 return std::move(E);
Misha Brukmand792c9b2003-07-24 18:17:43 +0000403
404 // Remove the intermediate C file
Rafael Espindola34889ca2013-06-17 19:21:38 +0000405 sys::fs::remove(OutputFile);
Misha Brukmand792c9b2003-07-24 18:17:43 +0000406
Rafael Espindolac32573b2014-03-14 15:13:35 +0000407 return SharedObjectFile;
Misha Brukmand792c9b2003-07-24 18:17:43 +0000408}
409
Rafael Espindolaf6074ed2018-02-14 21:44:34 +0000410/// Calls compileProgram and then records the output into ReferenceOutputFile.
411/// Returns true if reference file created, false otherwise. Note:
412/// initializeExecutionEnvironment should be called BEFORE this function.
413Error BugDriver::createReferenceFile(Module &M, const std::string &Filename) {
414 if (Error E = compileProgram(*Program))
Justin Bogner1c039152016-09-06 17:18:22 +0000415 return E;
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000416
Rafael Espindolaf6074ed2018-02-14 21:44:34 +0000417 Expected<std::string> Result = executeProgramSafely(*Program, Filename);
Justin Bogner1c039152016-09-06 17:18:22 +0000418 if (Error E = Result.takeError()) {
Dan Gohman414cf502008-12-08 04:02:47 +0000419 if (Interpreter != SafeInterpreter) {
Justin Bogner1c039152016-09-06 17:18:22 +0000420 E = joinErrors(
421 std::move(E),
422 make_error<StringError>(
423 "*** There is a bug running the \"safe\" backend. Either"
424 " debug it (for example with the -run-jit bugpoint option,"
425 " if JIT is being used as the \"safe\" backend), or fix the"
426 " error some other way.\n",
427 inconvertibleErrorCode()));
Patrick Jenkinsc46c0382006-08-15 16:40:49 +0000428 }
Justin Bogner1c039152016-09-06 17:18:22 +0000429 return E;
Patrick Jenkinsc46c0382006-08-15 16:40:49 +0000430 }
Justin Bogner1c039152016-09-06 17:18:22 +0000431 ReferenceOutputFile = *Result;
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000432 outs() << "\nReference output is: " << ReferenceOutputFile << "\n\n";
Justin Bogner1c039152016-09-06 17:18:22 +0000433 return Error::success();
Patrick Jenkinsc46c0382006-08-15 16:40:49 +0000434}
Misha Brukmand792c9b2003-07-24 18:17:43 +0000435
Rafael Espindolaf6074ed2018-02-14 21:44:34 +0000436/// This method executes the specified module and diffs the output against the
437/// file specified by ReferenceOutputFile. If the output is different, 1 is
438/// returned. If there is a problem with the code generator (e.g., llc
439/// crashes), this will set ErrMsg.
440Expected<bool> BugDriver::diffProgram(const Module &Program,
Justin Bogner1c039152016-09-06 17:18:22 +0000441 const std::string &BitcodeFile,
442 const std::string &SharedObject,
443 bool RemoveBitcode) const {
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000444 // Execute the program, generating an output file...
Justin Bogner1c039152016-09-06 17:18:22 +0000445 Expected<std::string> Output =
446 executeProgram(Program, "", BitcodeFile, SharedObject, nullptr);
447 if (Error E = Output.takeError())
448 return std::move(E);
Brian Gaeke35145be2004-02-11 18:37:32 +0000449
Chris Lattneraa997fb2003-08-01 20:29:45 +0000450 std::string Error;
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000451 bool FilesDifferent = false;
Justin Bogner1c039152016-09-06 17:18:22 +0000452 if (int Diff = DiffFilesWithTolerance(ReferenceOutputFile, *Output,
Chris Lattnerece10a42005-01-23 03:45:26 +0000453 AbsTolerance, RelTolerance, &Error)) {
454 if (Diff == 2) {
Dan Gohmand8db3762009-07-15 16:35:29 +0000455 errs() << "While diffing output: " << Error << '\n';
Chris Lattneraa997fb2003-08-01 20:29:45 +0000456 exit(1);
457 }
458 FilesDifferent = true;
Justin Bogner8d0a0812016-09-02 01:21:37 +0000459 } else {
David Goodwin73f4e3f2009-07-10 21:39:28 +0000460 // Remove the generated output if there are no differences.
Justin Bogner1c039152016-09-06 17:18:22 +0000461 sys::fs::remove(*Output);
David Goodwin73f4e3f2009-07-10 21:39:28 +0000462 }
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000463
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000464 // Remove the bitcode file if we are supposed to.
465 if (RemoveBitcode)
Rafael Espindolabecba2b2013-06-18 16:14:09 +0000466 sys::fs::remove(BitcodeFile);
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000467 return FilesDifferent;
468}
Misha Brukman539f9592003-07-28 19:16:14 +0000469
Justin Bogner8d0a0812016-09-02 01:21:37 +0000470bool BugDriver::isExecutingJIT() { return InterpreterSel == RunJIT; }