blob: 773bad69fae0587715ef5fb51fe3a09900d4b4d9 [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!");
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000302 if (BitcodeFile.empty()) {
303 // Emit the program to a bitcode file...
Rafael Espindolaf9b2c732017-11-16 21:53:51 +0000304 auto File =
305 sys::fs::TempFile::create(OutputPrefix + "-test-program-%%%%%%%.bc");
306 if (!File) {
307 errs() << ToolName
308 << ": Error making unique filename: " << toString(File.takeError())
Justin Bogner8d0a0812016-09-02 01:21:37 +0000309 << "!\n";
Reid Spencere4ca7222006-08-23 20:34:57 +0000310 exit(1);
311 }
Rafael Espindolaf9b2c732017-11-16 21:53:51 +0000312 DiscardTemp Discard{*File};
313 BitcodeFile = File->TmpName;
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000314
Rafael Espindolaf9b2c732017-11-16 21:53:51 +0000315 if (writeProgramToFile(File->FD, Program)) {
Justin Bogner8d0a0812016-09-02 01:21:37 +0000316 errs() << ToolName << ": Error emitting bitcode to file '" << BitcodeFile
317 << "'!\n";
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000318 exit(1);
319 }
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000320 }
321
Justin Bogner8d0a0812016-09-02 01:21:37 +0000322 if (OutputFile.empty())
323 OutputFile = OutputPrefix + "-execution-output-%%%%%%%";
Misha Brukmand792c9b2003-07-24 18:17:43 +0000324
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000325 // Check to see if this is a valid output filename...
Rafael Espindolabecba2b2013-06-18 16:14:09 +0000326 SmallString<128> UniqueFile;
Rafael Espindola4453e42942014-06-13 03:07:50 +0000327 std::error_code EC = sys::fs::createUniqueFile(OutputFile, UniqueFile);
Rafael Espindolabecba2b2013-06-18 16:14:09 +0000328 if (EC) {
Justin Bogner8d0a0812016-09-02 01:21:37 +0000329 errs() << ToolName << ": Error making unique filename: " << EC.message()
330 << "\n";
Reid Spencere4ca7222006-08-23 20:34:57 +0000331 exit(1);
332 }
Rafael Espindolabecba2b2013-06-18 16:14:09 +0000333 OutputFile = UniqueFile.str();
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000334
Chris Lattner3f6e5222003-10-14 21:59:36 +0000335 // Figure out which shared objects to run, if any.
Chris Lattnerdc92fa62003-10-14 22:24:31 +0000336 std::vector<std::string> SharedObjs(AdditionalSOs);
Chris Lattner3f6e5222003-10-14 21:59:36 +0000337 if (!SharedObj.empty())
338 SharedObjs.push_back(SharedObj);
339
Justin Bogner1c039152016-09-06 17:18:22 +0000340 Expected<int> RetVal = AI->ExecuteProgram(BitcodeFile, InputArgv, InputFile,
341 OutputFile, AdditionalLinkerArgs,
342 SharedObjs, Timeout, MemoryLimit);
343 if (Error E = RetVal.takeError())
344 return std::move(E);
Chris Lattner4e0969b2004-07-24 07:53:26 +0000345
Justin Bogner1c039152016-09-06 17:18:22 +0000346 if (*RetVal == -1) {
Dan Gohmand8db3762009-07-15 16:35:29 +0000347 errs() << "<timeout>";
Chris Lattner4e0969b2004-07-24 07:53:26 +0000348 static bool FirstTimeout = true;
349 if (FirstTimeout) {
Justin Bogner8d0a0812016-09-02 01:21:37 +0000350 outs()
351 << "\n"
352 "*** Program execution timed out! This mechanism is designed to "
353 "handle\n"
354 " programs stuck in infinite loops gracefully. The -timeout "
355 "option\n"
356 " can be used to change the timeout threshold or disable it "
357 "completely\n"
358 " (with -timeout=0). This message is only displayed once.\n";
Chris Lattner4e0969b2004-07-24 07:53:26 +0000359 FirstTimeout = false;
360 }
361 }
Chris Lattner3f6e5222003-10-14 21:59:36 +0000362
Reid Spencerd077fe72006-11-28 07:04:10 +0000363 if (AppendProgramExitCode) {
364 std::ofstream outFile(OutputFile.c_str(), std::ios_base::app);
Justin Bogner1c039152016-09-06 17:18:22 +0000365 outFile << "exit " << *RetVal << '\n';
Reid Spencerd077fe72006-11-28 07:04:10 +0000366 outFile.close();
367 }
368
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000369 // Return the filename we captured the output to.
370 return OutputFile;
371}
372
Rafael Espindolaf6074ed2018-02-14 21:44:34 +0000373/// Used to create reference output with the "safe" backend, if reference output
374/// is not provided.
Justin Bogner1c039152016-09-06 17:18:22 +0000375Expected<std::string>
Rafael Espindolaf6074ed2018-02-14 21:44:34 +0000376BugDriver::executeProgramSafely(const Module &Program,
Justin Bogner1c039152016-09-06 17:18:22 +0000377 const std::string &OutputFile) const {
378 return executeProgram(Program, OutputFile, "", "", SafeInterpreter);
Brian Gaeke35145be2004-02-11 18:37:32 +0000379}
Misha Brukmand792c9b2003-07-24 18:17:43 +0000380
Justin Bogner1c039152016-09-06 17:18:22 +0000381Expected<std::string>
382BugDriver::compileSharedObject(const std::string &BitcodeFile) {
Misha Brukmand792c9b2003-07-24 18:17:43 +0000383 assert(Interpreter && "Interpreter should have been created already!");
Rafael Espindola34889ca2013-06-17 19:21:38 +0000384 std::string OutputFile;
Misha Brukmand792c9b2003-07-24 18:17:43 +0000385
Dan Gohman414cf502008-12-08 04:02:47 +0000386 // Using the known-good backend.
Justin Bogner1c039152016-09-06 17:18:22 +0000387 Expected<CC::FileType> FT =
388 SafeInterpreter->OutputCode(BitcodeFile, OutputFile);
389 if (Error E = FT.takeError())
390 return std::move(E);
Misha Brukmand792c9b2003-07-24 18:17:43 +0000391
Chris Lattnerf8a84db2003-10-14 21:09:11 +0000392 std::string SharedObjectFile;
Justin Bogner1c039152016-09-06 17:18:22 +0000393 if (Error E = cc->MakeSharedObject(OutputFile, *FT, SharedObjectFile,
394 AdditionalLinkerArgs))
395 return std::move(E);
Misha Brukmand792c9b2003-07-24 18:17:43 +0000396
397 // Remove the intermediate C file
Rafael Espindola34889ca2013-06-17 19:21:38 +0000398 sys::fs::remove(OutputFile);
Misha Brukmand792c9b2003-07-24 18:17:43 +0000399
Rafael Espindolac32573b2014-03-14 15:13:35 +0000400 return SharedObjectFile;
Misha Brukmand792c9b2003-07-24 18:17:43 +0000401}
402
Rafael Espindolaf6074ed2018-02-14 21:44:34 +0000403/// Calls compileProgram and then records the output into ReferenceOutputFile.
404/// Returns true if reference file created, false otherwise. Note:
405/// initializeExecutionEnvironment should be called BEFORE this function.
406Error BugDriver::createReferenceFile(Module &M, const std::string &Filename) {
407 if (Error E = compileProgram(*Program))
Justin Bogner1c039152016-09-06 17:18:22 +0000408 return E;
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000409
Rafael Espindolaf6074ed2018-02-14 21:44:34 +0000410 Expected<std::string> Result = executeProgramSafely(*Program, Filename);
Justin Bogner1c039152016-09-06 17:18:22 +0000411 if (Error E = Result.takeError()) {
Dan Gohman414cf502008-12-08 04:02:47 +0000412 if (Interpreter != SafeInterpreter) {
Justin Bogner1c039152016-09-06 17:18:22 +0000413 E = joinErrors(
414 std::move(E),
415 make_error<StringError>(
416 "*** There is a bug running the \"safe\" backend. Either"
417 " debug it (for example with the -run-jit bugpoint option,"
418 " if JIT is being used as the \"safe\" backend), or fix the"
419 " error some other way.\n",
420 inconvertibleErrorCode()));
Patrick Jenkinsc46c0382006-08-15 16:40:49 +0000421 }
Justin Bogner1c039152016-09-06 17:18:22 +0000422 return E;
Patrick Jenkinsc46c0382006-08-15 16:40:49 +0000423 }
Justin Bogner1c039152016-09-06 17:18:22 +0000424 ReferenceOutputFile = *Result;
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000425 outs() << "\nReference output is: " << ReferenceOutputFile << "\n\n";
Justin Bogner1c039152016-09-06 17:18:22 +0000426 return Error::success();
Patrick Jenkinsc46c0382006-08-15 16:40:49 +0000427}
Misha Brukmand792c9b2003-07-24 18:17:43 +0000428
Rafael Espindolaf6074ed2018-02-14 21:44:34 +0000429/// This method executes the specified module and diffs the output against the
430/// file specified by ReferenceOutputFile. If the output is different, 1 is
431/// returned. If there is a problem with the code generator (e.g., llc
432/// crashes), this will set ErrMsg.
433Expected<bool> BugDriver::diffProgram(const Module &Program,
Justin Bogner1c039152016-09-06 17:18:22 +0000434 const std::string &BitcodeFile,
435 const std::string &SharedObject,
436 bool RemoveBitcode) const {
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000437 // Execute the program, generating an output file...
Justin Bogner1c039152016-09-06 17:18:22 +0000438 Expected<std::string> Output =
439 executeProgram(Program, "", BitcodeFile, SharedObject, nullptr);
440 if (Error E = Output.takeError())
441 return std::move(E);
Brian Gaeke35145be2004-02-11 18:37:32 +0000442
Chris Lattneraa997fb2003-08-01 20:29:45 +0000443 std::string Error;
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000444 bool FilesDifferent = false;
Justin Bogner1c039152016-09-06 17:18:22 +0000445 if (int Diff = DiffFilesWithTolerance(ReferenceOutputFile, *Output,
Chris Lattnerece10a42005-01-23 03:45:26 +0000446 AbsTolerance, RelTolerance, &Error)) {
447 if (Diff == 2) {
Dan Gohmand8db3762009-07-15 16:35:29 +0000448 errs() << "While diffing output: " << Error << '\n';
Chris Lattneraa997fb2003-08-01 20:29:45 +0000449 exit(1);
450 }
451 FilesDifferent = true;
Justin Bogner8d0a0812016-09-02 01:21:37 +0000452 } else {
David Goodwin73f4e3f2009-07-10 21:39:28 +0000453 // Remove the generated output if there are no differences.
Justin Bogner1c039152016-09-06 17:18:22 +0000454 sys::fs::remove(*Output);
David Goodwin73f4e3f2009-07-10 21:39:28 +0000455 }
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000456
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000457 // Remove the bitcode file if we are supposed to.
458 if (RemoveBitcode)
Rafael Espindolabecba2b2013-06-18 16:14:09 +0000459 sys::fs::remove(BitcodeFile);
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000460 return FilesDifferent;
461}
Misha Brukman539f9592003-07-28 19:16:14 +0000462
Justin Bogner8d0a0812016-09-02 01:21:37 +0000463bool BugDriver::isExecutingJIT() { return InterpreterSel == RunJIT; }