blob: 1b86b103d83546d08e361cdb0223f6737dbe4449 [file] [log] [blame]
Chris Lattner4a106452002-12-23 23:50:16 +00001//===- ExecutionDriver.cpp - Allow execution of LLVM program --------------===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
John Criswell7c0e0222003-10-20 17:47:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner21c62da2007-12-29 20:44:31 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman3da94ae2005-04-22 00:00:37 +00007//
John Criswell7c0e0222003-10-20 17:47:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner4a106452002-12-23 23:50:16 +00009//
10// This file contains code used to execute the program utilizing one of the
Gabor Greif8ff70c22007-07-04 21:55:50 +000011// various ways of running LLVM bitcode.
Chris Lattner4a106452002-12-23 23:50:16 +000012//
13//===----------------------------------------------------------------------===//
14
Chris Lattner4a106452002-12-23 23:50:16 +000015#include "BugDriver.h"
Chris Lattnerf1b20d82006-06-06 22:30:59 +000016#include "ToolRunner.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000017#include "llvm/Support/CommandLine.h"
18#include "llvm/Support/Debug.h"
19#include "llvm/Support/FileUtilities.h"
Davide Italianoe99d2082015-10-14 19:48:01 +000020#include "llvm/Support/Program.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000021#include "llvm/Support/SystemUtils.h"
Chris Lattner74382b72009-08-23 22:45:37 +000022#include "llvm/Support/raw_ostream.h"
Chris Lattner4a106452002-12-23 23:50:16 +000023#include <fstream>
Reid Spencer51ab5c82006-06-06 00:00:42 +000024
Brian Gaeked0fde302003-11-11 22:41:34 +000025using namespace llvm;
26
Chris Lattner4a106452002-12-23 23:50:16 +000027namespace {
Justin Bogner388e8b92016-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 Brukman41485562003-09-29 22:40:52 +000041
Justin Bogner388e8b92016-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 Lattnera328c512005-01-23 03:45:26 +000048
Justin Bogner388e8b92016-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 Amini3ffe1132016-10-08 19:41:06 +000063 "the bitcode. Useful for cross-compilation.")),
Justin Bogner388e8b92016-09-02 01:21:37 +000064 cl::init(AutoPick));
Chris Lattner3c053a02003-04-23 20:31:37 +000065
Justin Bogner388e8b92016-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 Amini3ffe1132016-10-08 19:41:06 +000072 "the bitcode. Useful for cross-compilation.")),
Justin Bogner388e8b92016-09-02 01:21:37 +000073 cl::init(AutoPick));
Dan Gohman70ef4492008-12-08 04:02:47 +000074
Justin Bogner388e8b92016-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 Gohman70ef4492008-12-08 04:02:47 +000078
Justin Bogner388e8b92016-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 Spencer5e1452c2006-11-28 07:04:10 +000083
Justin Bogner388e8b92016-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 Lattner7dac6582003-10-14 22:24:31 +000087
Justin Bogner388e8b92016-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 Lattner7d91e492004-07-24 07:53:26 +000091
Justin Bogner388e8b92016-09-02 01:21:37 +000092cl::list<std::string> AdditionalLinkerArgs(
93 "Xlinker", cl::desc("Additional arguments to pass to the linker"));
Anton Korobeynikov9ef74252008-04-28 20:53:48 +000094
Justin Bogner388e8b92016-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 Trickf73311b2011-02-08 18:20:48 +000099
Justin Bogner388e8b92016-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 Lattner4a106452002-12-23 23:50:16 +0000104}
105
Brian Gaeked0fde302003-11-11 22:41:34 +0000106namespace llvm {
Justin Bogner388e8b92016-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 Dunbar68ccdaa2009-09-07 19:26:11 +0000112
Justin Bogner388e8b92016-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 Gohman70ef4492008-12-08 04:02:47 +0000116}
Brian Gaeke636df3d2004-05-04 21:09:16 +0000117
Dan Gohman70ef4492008-12-08 04:02:47 +0000118namespace {
Justin Bogner388e8b92016-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 Gohman70ef4492008-12-08 04:02:47 +0000122
Justin Bogner388e8b92016-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 Wendling38efa382009-03-02 23:13:18 +0000126
Justin Bogner388e8b92016-09-02 01:21:37 +0000127cl::opt<std::string> CCBinary("gcc", cl::init(""),
128 cl::desc("The gcc binary to use."));
Kalle Raiskilafaa95762010-05-10 07:38:37 +0000129
Justin Bogner388e8b92016-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 Lattnerfa761832004-01-14 03:38:37 +0000133}
Misha Brukman9d679cb2003-07-30 20:15:44 +0000134
Chris Lattner4a106452002-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 Bognerd8090ae2016-09-06 17:18:22 +0000142Error BugDriver::initializeExecutionEnvironment() {
Dan Gohmanac95cc72009-07-16 15:30:09 +0000143 outs() << "Initializing execution environment: ";
Chris Lattner4a106452002-12-23 23:50:16 +0000144
Misha Brukman41485562003-09-29 22:40:52 +0000145 // Create an instance of the AbstractInterpreter interface as specified on
146 // the command line
Craig Topper573faec2014-04-25 04:24:47 +0000147 SafeInterpreter = nullptr;
Chris Lattner4a106452002-12-23 23:50:16 +0000148 std::string Message;
Brian Gaeke636df3d2004-05-04 21:09:16 +0000149
Davide Italianof7b2acb2015-10-14 20:29:54 +0000150 if (CCBinary.empty()) {
Brian Gesiakdcf592e2018-12-10 00:56:13 +0000151 if (ErrorOr<std::string> ClangPath =
152 FindProgramByName("clang", getToolName(), &AbsTolerance))
153 CCBinary = *ClangPath;
Davide Italianoe99d2082015-10-14 19:48:01 +0000154 else
Davide Italianof7b2acb2015-10-14 20:29:54 +0000155 CCBinary = "gcc";
Davide Italianoe99d2082015-10-14 19:48:01 +0000156 }
157
Chris Lattnercc876a72003-05-03 03:19:41 +0000158 switch (InterpreterSel) {
Brian Gaekeb5ee5092003-10-21 17:41:35 +0000159 case AutoPick:
Brian Gaekeb5ee5092003-10-21 17:41:35 +0000160 if (!Interpreter) {
161 InterpreterSel = RunJIT;
Justin Bogner388e8b92016-09-02 01:21:37 +0000162 Interpreter =
163 AbstractInterpreter::createJIT(getToolName(), Message, &ToolArgv);
Brian Gaekeb5ee5092003-10-21 17:41:35 +0000164 }
165 if (!Interpreter) {
166 InterpreterSel = RunLLC;
Justin Bogner388e8b92016-09-02 01:21:37 +0000167 Interpreter = AbstractInterpreter::createLLC(
168 getToolName(), Message, CCBinary, &ToolArgv, &CCToolArgv);
Brian Gaekeb5ee5092003-10-21 17:41:35 +0000169 }
170 if (!Interpreter) {
171 InterpreterSel = RunLLI;
Justin Bogner388e8b92016-09-02 01:21:37 +0000172 Interpreter =
173 AbstractInterpreter::createLLI(getToolName(), Message, &ToolArgv);
Brian Gaekeb5ee5092003-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 Lattner769f1fe2003-10-14 21:59:36 +0000180 case RunLLI:
Justin Bogner388e8b92016-09-02 01:21:37 +0000181 Interpreter =
182 AbstractInterpreter::createLLI(getToolName(), Message, &ToolArgv);
Chris Lattner769f1fe2003-10-14 21:59:36 +0000183 break;
184 case RunLLC:
Chris Lattner50010422010-03-16 06:41:47 +0000185 case RunLLCIA:
Dan Gohman70ef4492008-12-08 04:02:47 +0000186 case LLC_Safe:
Justin Bogner388e8b92016-09-02 01:21:37 +0000187 Interpreter = AbstractInterpreter::createLLC(
188 getToolName(), Message, CCBinary, &ToolArgv, &CCToolArgv,
189 InterpreterSel == RunLLCIA);
Chris Lattner769f1fe2003-10-14 21:59:36 +0000190 break;
191 case RunJIT:
Justin Bogner388e8b92016-09-02 01:21:37 +0000192 Interpreter =
193 AbstractInterpreter::createJIT(getToolName(), Message, &ToolArgv);
Chris Lattner769f1fe2003-10-14 21:59:36 +0000194 break;
Andrew Trickf73311b2011-02-08 18:20:48 +0000195 case CompileCustom:
Justin Bogner388e8b92016-09-02 01:21:37 +0000196 Interpreter = AbstractInterpreter::createCustomCompiler(
Brian Gesiakdcf592e2018-12-10 00:56:13 +0000197 getToolName(), Message, CustomCompileCommand);
Andrew Trickf73311b2011-02-08 18:20:48 +0000198 break;
Anton Korobeynikov9ef74252008-04-28 20:53:48 +0000199 case Custom:
Brian Gesiakdcf592e2018-12-10 00:56:13 +0000200 Interpreter = AbstractInterpreter::createCustomExecutor(
201 getToolName(), Message, CustomExecCommand);
Anton Korobeynikov9ef74252008-04-28 20:53:48 +0000202 break;
Chris Lattner4a106452002-12-23 23:50:16 +0000203 }
Matthijs Kooijmanad6996d2008-06-12 13:09:43 +0000204 if (!Interpreter)
Dan Gohman65f57c22009-07-15 16:35:29 +0000205 errs() << Message;
Matthijs Kooijmanad6996d2008-06-12 13:09:43 +0000206 else // Display informational messages on stdout instead of stderr
Dan Gohmanac95cc72009-07-16 15:30:09 +0000207 outs() << Message;
Chris Lattner4a106452002-12-23 23:50:16 +0000208
Dan Gohman70ef4492008-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 Gohman70ef4492008-12-08 04:02:47 +0000215 // In "llc-safe" mode, default to using LLC as the "safe" backend.
Justin Bogner388e8b92016-09-02 01:21:37 +0000216 if (!SafeInterpreter && InterpreterSel == LLC_Safe) {
Dan Gohman70ef4492008-12-08 04:02:47 +0000217 SafeInterpreterSel = RunLLC;
218 SafeToolArgs.push_back("--relocation-model=pic");
Justin Bogner388e8b92016-09-02 01:21:37 +0000219 SafeInterpreter = AbstractInterpreter::createLLC(
220 Path.c_str(), Message, CCBinary, &SafeToolArgs, &CCToolArgv);
Dan Gohman70ef4492008-12-08 04:02:47 +0000221 }
222
Justin Bogner388e8b92016-09-02 01:21:37 +0000223 if (!SafeInterpreter && InterpreterSel != RunLLC &&
Dan Gohman70ef4492008-12-08 04:02:47 +0000224 InterpreterSel != RunJIT) {
225 SafeInterpreterSel = RunLLC;
226 SafeToolArgs.push_back("--relocation-model=pic");
Justin Bogner388e8b92016-09-02 01:21:37 +0000227 SafeInterpreter = AbstractInterpreter::createLLC(
228 Path.c_str(), Message, CCBinary, &SafeToolArgs, &CCToolArgv);
Dan Gohman70ef4492008-12-08 04:02:47 +0000229 }
230 if (!SafeInterpreter) {
231 SafeInterpreterSel = AutoPick;
Saleem Abdulrasooled7fcf42013-01-24 16:49:14 +0000232 Message = "Sorry, I can't automatically select a safe interpreter!\n";
Dan Gohman70ef4492008-12-08 04:02:47 +0000233 }
234 break;
235 case RunLLC:
Chris Lattner50010422010-03-16 06:41:47 +0000236 case RunLLCIA:
Dan Gohman70ef4492008-12-08 04:02:47 +0000237 SafeToolArgs.push_back("--relocation-model=pic");
Justin Bogner388e8b92016-09-02 01:21:37 +0000238 SafeInterpreter = AbstractInterpreter::createLLC(
239 Path.c_str(), Message, CCBinary, &SafeToolArgs, &CCToolArgv,
240 SafeInterpreterSel == RunLLCIA);
Dan Gohman70ef4492008-12-08 04:02:47 +0000241 break;
Dan Gohman70ef4492008-12-08 04:02:47 +0000242 case Custom:
Brian Gesiakdcf592e2018-12-10 00:56:13 +0000243 SafeInterpreter = AbstractInterpreter::createCustomExecutor(
244 getToolName(), Message, CustomExecCommand);
Dan Gohman70ef4492008-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 Lattner7bb11542004-02-18 20:52:02 +0000250 }
Justin Bogner388e8b92016-09-02 01:21:37 +0000251 if (!SafeInterpreter) {
252 outs() << Message << "\nExiting.\n";
253 exit(1);
254 }
Andrew Trickde86cbd2011-02-08 18:07:10 +0000255
Brian Gesiakdcf592e2018-12-10 00:56:13 +0000256 cc = CC::create(getToolName(), Message, CCBinary, &CCToolArgv);
Justin Bogner388e8b92016-09-02 01:21:37 +0000257 if (!cc) {
258 outs() << Message << "\nExiting.\n";
259 exit(1);
260 }
Misha Brukmana259c9b2003-07-24 21:59:10 +0000261
Chris Lattner4a106452002-12-23 23:50:16 +0000262 // If there was an error creating the selected interpreter, quit with error.
Justin Bognerd8090ae2016-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 Lattner4a106452002-12-23 23:50:16 +0000267}
268
Rafael Espindolacdac12c2018-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 Greif8ff70c22007-07-04 21:55:50 +0000272 // Emit the program to a bitcode file...
Rafael Espindolac1382282017-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 Gohman65f57c22009-07-15 16:35:29 +0000278 << "\n";
Reid Spencer51c5a282006-08-23 20:34:57 +0000279 exit(1);
280 }
Rafael Espindolac1382282017-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 Espindola9a230392013-06-18 16:14:09 +0000284 << "'!\n";
Chris Lattnerea9212c2004-02-18 23:25:22 +0000285 exit(1);
286 }
287
Chris Lattnerea9212c2004-02-18 23:25:22 +0000288 // Actually compile the program!
Rafael Espindolac1382282017-11-16 21:40:10 +0000289 return Interpreter->compileProgram(Temp->TmpName, Timeout, MemoryLimit);
Chris Lattnerea9212c2004-02-18 23:25:22 +0000290}
291
Rafael Espindolacdac12c2018-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 Bognerd8090ae2016-09-06 17:18:22 +0000296 std::string OutputFile,
297 std::string BitcodeFile,
298 const std::string &SharedObj,
299 AbstractInterpreter *AI) const {
Justin Bogner388e8b92016-09-02 01:21:37 +0000300 if (!AI)
301 AI = Interpreter;
Chris Lattner769f1fe2003-10-14 21:59:36 +0000302 assert(AI && "Interpreter should have been created already!");
Don Hinton7dee23a2018-09-18 18:39:27 +0000303 bool CreatedBitcode = false;
Gabor Greif8ff70c22007-07-04 21:55:50 +0000304 if (BitcodeFile.empty()) {
305 // Emit the program to a bitcode file...
Don Hinton7dee23a2018-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 Bogner388e8b92016-09-02 01:21:37 +0000312 << "!\n";
Reid Spencer51c5a282006-08-23 20:34:57 +0000313 exit(1);
314 }
Don Hinton7dee23a2018-09-18 18:39:27 +0000315 BitcodeFile = UniqueFilename.str();
Chris Lattner4a106452002-12-23 23:50:16 +0000316
Don Hinton7dee23a2018-09-18 18:39:27 +0000317 if (writeProgramToFile(BitcodeFile, UniqueFD, Program)) {
Justin Bogner388e8b92016-09-02 01:21:37 +0000318 errs() << ToolName << ": Error emitting bitcode to file '" << BitcodeFile
319 << "'!\n";
Chris Lattner4a106452002-12-23 23:50:16 +0000320 exit(1);
321 }
Don Hinton7dee23a2018-09-18 18:39:27 +0000322 CreatedBitcode = true;
Chris Lattner4a106452002-12-23 23:50:16 +0000323 }
324
Don Hinton7dee23a2018-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 Bogner388e8b92016-09-02 01:21:37 +0000329 if (OutputFile.empty())
330 OutputFile = OutputPrefix + "-execution-output-%%%%%%%";
Misha Brukman50733362003-07-24 18:17:43 +0000331
Chris Lattner4a106452002-12-23 23:50:16 +0000332 // Check to see if this is a valid output filename...
Rafael Espindola9a230392013-06-18 16:14:09 +0000333 SmallString<128> UniqueFile;
Rafael Espindola1ad45022014-06-13 03:07:50 +0000334 std::error_code EC = sys::fs::createUniqueFile(OutputFile, UniqueFile);
Rafael Espindola9a230392013-06-18 16:14:09 +0000335 if (EC) {
Justin Bogner388e8b92016-09-02 01:21:37 +0000336 errs() << ToolName << ": Error making unique filename: " << EC.message()
337 << "\n";
Reid Spencer51c5a282006-08-23 20:34:57 +0000338 exit(1);
339 }
Rafael Espindola9a230392013-06-18 16:14:09 +0000340 OutputFile = UniqueFile.str();
Chris Lattner4a106452002-12-23 23:50:16 +0000341
Chris Lattner769f1fe2003-10-14 21:59:36 +0000342 // Figure out which shared objects to run, if any.
Chris Lattner7dac6582003-10-14 22:24:31 +0000343 std::vector<std::string> SharedObjs(AdditionalSOs);
Chris Lattner769f1fe2003-10-14 21:59:36 +0000344 if (!SharedObj.empty())
345 SharedObjs.push_back(SharedObj);
346
Justin Bognerd8090ae2016-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 Lattner7d91e492004-07-24 07:53:26 +0000352
Justin Bognerd8090ae2016-09-06 17:18:22 +0000353 if (*RetVal == -1) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000354 errs() << "<timeout>";
Chris Lattner7d91e492004-07-24 07:53:26 +0000355 static bool FirstTimeout = true;
356 if (FirstTimeout) {
Justin Bogner388e8b92016-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 Lattner7d91e492004-07-24 07:53:26 +0000366 FirstTimeout = false;
367 }
368 }
Chris Lattner769f1fe2003-10-14 21:59:36 +0000369
Reid Spencer5e1452c2006-11-28 07:04:10 +0000370 if (AppendProgramExitCode) {
371 std::ofstream outFile(OutputFile.c_str(), std::ios_base::app);
Justin Bognerd8090ae2016-09-06 17:18:22 +0000372 outFile << "exit " << *RetVal << '\n';
Reid Spencer5e1452c2006-11-28 07:04:10 +0000373 outFile.close();
374 }
375
Chris Lattner4a106452002-12-23 23:50:16 +0000376 // Return the filename we captured the output to.
377 return OutputFile;
378}
379
Rafael Espindolacdac12c2018-02-14 21:44:34 +0000380/// Used to create reference output with the "safe" backend, if reference output
381/// is not provided.
Justin Bognerd8090ae2016-09-06 17:18:22 +0000382Expected<std::string>
Rafael Espindolacdac12c2018-02-14 21:44:34 +0000383BugDriver::executeProgramSafely(const Module &Program,
Justin Bognerd8090ae2016-09-06 17:18:22 +0000384 const std::string &OutputFile) const {
385 return executeProgram(Program, OutputFile, "", "", SafeInterpreter);
Brian Gaekec5cad212004-02-11 18:37:32 +0000386}
Misha Brukman50733362003-07-24 18:17:43 +0000387
Justin Bognerd8090ae2016-09-06 17:18:22 +0000388Expected<std::string>
389BugDriver::compileSharedObject(const std::string &BitcodeFile) {
Misha Brukman50733362003-07-24 18:17:43 +0000390 assert(Interpreter && "Interpreter should have been created already!");
Rafael Espindolaf656a1d2013-06-17 19:21:38 +0000391 std::string OutputFile;
Misha Brukman50733362003-07-24 18:17:43 +0000392
Dan Gohman70ef4492008-12-08 04:02:47 +0000393 // Using the known-good backend.
Justin Bognerd8090ae2016-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 Brukman50733362003-07-24 18:17:43 +0000398
Chris Lattnera0f5b152003-10-14 21:09:11 +0000399 std::string SharedObjectFile;
Justin Bognerd8090ae2016-09-06 17:18:22 +0000400 if (Error E = cc->MakeSharedObject(OutputFile, *FT, SharedObjectFile,
401 AdditionalLinkerArgs))
402 return std::move(E);
Misha Brukman50733362003-07-24 18:17:43 +0000403
404 // Remove the intermediate C file
Rafael Espindolaf656a1d2013-06-17 19:21:38 +0000405 sys::fs::remove(OutputFile);
Misha Brukman50733362003-07-24 18:17:43 +0000406
Rafael Espindola80f27b02014-03-14 15:13:35 +0000407 return SharedObjectFile;
Misha Brukman50733362003-07-24 18:17:43 +0000408}
409
Rafael Espindolacdac12c2018-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 Bognerd8090ae2016-09-06 17:18:22 +0000415 return E;
Nick Lewycky22ff7482010-04-12 05:08:25 +0000416
Rafael Espindolacdac12c2018-02-14 21:44:34 +0000417 Expected<std::string> Result = executeProgramSafely(*Program, Filename);
Justin Bognerd8090ae2016-09-06 17:18:22 +0000418 if (Error E = Result.takeError()) {
Dan Gohman70ef4492008-12-08 04:02:47 +0000419 if (Interpreter != SafeInterpreter) {
Justin Bognerd8090ae2016-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 Jenkins6a3f31c2006-08-15 16:40:49 +0000428 }
Justin Bognerd8090ae2016-09-06 17:18:22 +0000429 return E;
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +0000430 }
Justin Bognerd8090ae2016-09-06 17:18:22 +0000431 ReferenceOutputFile = *Result;
Nick Lewycky22ff7482010-04-12 05:08:25 +0000432 outs() << "\nReference output is: " << ReferenceOutputFile << "\n\n";
Justin Bognerd8090ae2016-09-06 17:18:22 +0000433 return Error::success();
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +0000434}
Misha Brukman50733362003-07-24 18:17:43 +0000435
Rafael Espindolacdac12c2018-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 Bognerd8090ae2016-09-06 17:18:22 +0000441 const std::string &BitcodeFile,
442 const std::string &SharedObject,
443 bool RemoveBitcode) const {
Chris Lattner4a106452002-12-23 23:50:16 +0000444 // Execute the program, generating an output file...
Justin Bognerd8090ae2016-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 Gaekec5cad212004-02-11 18:37:32 +0000449
Chris Lattner65f62792003-08-01 20:29:45 +0000450 std::string Error;
Chris Lattner4a106452002-12-23 23:50:16 +0000451 bool FilesDifferent = false;
Justin Bognerd8090ae2016-09-06 17:18:22 +0000452 if (int Diff = DiffFilesWithTolerance(ReferenceOutputFile, *Output,
Chris Lattnera328c512005-01-23 03:45:26 +0000453 AbsTolerance, RelTolerance, &Error)) {
454 if (Diff == 2) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000455 errs() << "While diffing output: " << Error << '\n';
Chris Lattner65f62792003-08-01 20:29:45 +0000456 exit(1);
457 }
458 FilesDifferent = true;
Justin Bogner388e8b92016-09-02 01:21:37 +0000459 } else {
David Goodwin80becf12009-07-10 21:39:28 +0000460 // Remove the generated output if there are no differences.
Justin Bognerd8090ae2016-09-06 17:18:22 +0000461 sys::fs::remove(*Output);
David Goodwin80becf12009-07-10 21:39:28 +0000462 }
Chris Lattner4a106452002-12-23 23:50:16 +0000463
Gabor Greif8ff70c22007-07-04 21:55:50 +0000464 // Remove the bitcode file if we are supposed to.
465 if (RemoveBitcode)
Rafael Espindola9a230392013-06-18 16:14:09 +0000466 sys::fs::remove(BitcodeFile);
Chris Lattner4a106452002-12-23 23:50:16 +0000467 return FilesDifferent;
468}
Misha Brukman91eabc12003-07-28 19:16:14 +0000469
Justin Bogner388e8b92016-09-02 01:21:37 +0000470bool BugDriver::isExecutingJIT() { return InterpreterSel == RunJIT; }