xref: /freebsd/contrib/llvm-project/llvm/tools/bugpoint/ExecutionDriver.cpp (revision 7a6dacaca14b62ca4b74406814becb87a3fefac0)
10b57cec5SDimitry Andric //===- ExecutionDriver.cpp - Allow execution of LLVM program --------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file contains code used to execute the program utilizing one of the
100b57cec5SDimitry Andric // various ways of running LLVM bitcode.
110b57cec5SDimitry Andric //
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric 
140b57cec5SDimitry Andric #include "BugDriver.h"
150b57cec5SDimitry Andric #include "ToolRunner.h"
160b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h"
170b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
180b57cec5SDimitry Andric #include "llvm/Support/FileUtilities.h"
190b57cec5SDimitry Andric #include "llvm/Support/Program.h"
200b57cec5SDimitry Andric #include "llvm/Support/SystemUtils.h"
210b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
220b57cec5SDimitry Andric #include <fstream>
230b57cec5SDimitry Andric 
240b57cec5SDimitry Andric using namespace llvm;
250b57cec5SDimitry Andric 
260b57cec5SDimitry Andric namespace {
270b57cec5SDimitry Andric // OutputType - Allow the user to specify the way code should be run, to test
280b57cec5SDimitry Andric // for miscompilation.
290b57cec5SDimitry Andric //
300b57cec5SDimitry Andric enum OutputType {
310b57cec5SDimitry Andric   AutoPick,
320b57cec5SDimitry Andric   RunLLI,
330b57cec5SDimitry Andric   RunJIT,
340b57cec5SDimitry Andric   RunLLC,
350b57cec5SDimitry Andric   RunLLCIA,
360b57cec5SDimitry Andric   CompileCustom,
370b57cec5SDimitry Andric   Custom
380b57cec5SDimitry Andric };
390b57cec5SDimitry Andric 
400b57cec5SDimitry Andric cl::opt<double> AbsTolerance("abs-tolerance",
410b57cec5SDimitry Andric                              cl::desc("Absolute error tolerated"),
420b57cec5SDimitry Andric                              cl::init(0.0));
430b57cec5SDimitry Andric cl::opt<double> RelTolerance("rel-tolerance",
440b57cec5SDimitry Andric                              cl::desc("Relative error tolerated"),
450b57cec5SDimitry Andric                              cl::init(0.0));
460b57cec5SDimitry Andric 
470b57cec5SDimitry Andric cl::opt<OutputType> InterpreterSel(
480b57cec5SDimitry Andric     cl::desc("Specify the \"test\" i.e. suspect back-end:"),
490b57cec5SDimitry Andric     cl::values(clEnumValN(AutoPick, "auto", "Use best guess"),
500b57cec5SDimitry Andric                clEnumValN(RunLLI, "run-int", "Execute with the interpreter"),
510b57cec5SDimitry Andric                clEnumValN(RunJIT, "run-jit", "Execute with JIT"),
520b57cec5SDimitry Andric                clEnumValN(RunLLC, "run-llc", "Compile with LLC"),
530b57cec5SDimitry Andric                clEnumValN(RunLLCIA, "run-llc-ia",
540b57cec5SDimitry Andric                           "Compile with LLC with integrated assembler"),
550b57cec5SDimitry Andric                clEnumValN(CompileCustom, "compile-custom",
560b57cec5SDimitry Andric                           "Use -compile-command to define a command to "
570b57cec5SDimitry Andric                           "compile the bitcode. Useful to avoid linking."),
580b57cec5SDimitry Andric                clEnumValN(Custom, "run-custom",
590b57cec5SDimitry Andric                           "Use -exec-command to define a command to execute "
600b57cec5SDimitry Andric                           "the bitcode. Useful for cross-compilation.")),
610b57cec5SDimitry Andric     cl::init(AutoPick));
620b57cec5SDimitry Andric 
630b57cec5SDimitry Andric cl::opt<OutputType> SafeInterpreterSel(
640b57cec5SDimitry Andric     cl::desc("Specify \"safe\" i.e. known-good backend:"),
650b57cec5SDimitry Andric     cl::values(clEnumValN(AutoPick, "safe-auto", "Use best guess"),
660b57cec5SDimitry Andric                clEnumValN(RunLLC, "safe-run-llc", "Compile with LLC"),
670b57cec5SDimitry Andric                clEnumValN(Custom, "safe-run-custom",
680b57cec5SDimitry Andric                           "Use -exec-command to define a command to execute "
690b57cec5SDimitry Andric                           "the bitcode. Useful for cross-compilation.")),
700b57cec5SDimitry Andric     cl::init(AutoPick));
710b57cec5SDimitry Andric 
720b57cec5SDimitry Andric cl::opt<std::string> SafeInterpreterPath(
730b57cec5SDimitry Andric     "safe-path", cl::desc("Specify the path to the \"safe\" backend program"),
740b57cec5SDimitry Andric     cl::init(""));
750b57cec5SDimitry Andric 
760b57cec5SDimitry Andric cl::opt<bool> AppendProgramExitCode(
770b57cec5SDimitry Andric     "append-exit-code",
780b57cec5SDimitry Andric     cl::desc("Append the exit code to the output so it gets diff'd too"),
790b57cec5SDimitry Andric     cl::init(false));
800b57cec5SDimitry Andric 
810b57cec5SDimitry Andric cl::opt<std::string>
820b57cec5SDimitry Andric     InputFile("input", cl::init("/dev/null"),
830b57cec5SDimitry Andric               cl::desc("Filename to pipe in as stdin (default: /dev/null)"));
840b57cec5SDimitry Andric 
850b57cec5SDimitry Andric cl::list<std::string>
860b57cec5SDimitry Andric     AdditionalSOs("additional-so", cl::desc("Additional shared objects to load "
870b57cec5SDimitry Andric                                             "into executing programs"));
880b57cec5SDimitry Andric 
890b57cec5SDimitry Andric cl::list<std::string> AdditionalLinkerArgs(
900b57cec5SDimitry Andric     "Xlinker", cl::desc("Additional arguments to pass to the linker"));
910b57cec5SDimitry Andric 
920b57cec5SDimitry Andric cl::opt<std::string> CustomCompileCommand(
930b57cec5SDimitry Andric     "compile-command", cl::init("llc"),
940b57cec5SDimitry Andric     cl::desc("Command to compile the bitcode (use with -compile-custom) "
950b57cec5SDimitry Andric              "(default: llc)"));
960b57cec5SDimitry Andric 
970b57cec5SDimitry Andric cl::opt<std::string> CustomExecCommand(
980b57cec5SDimitry Andric     "exec-command", cl::init("simulate"),
990b57cec5SDimitry Andric     cl::desc("Command to execute the bitcode (use with -run-custom) "
1000b57cec5SDimitry Andric              "(default: simulate)"));
1010b57cec5SDimitry Andric }
1020b57cec5SDimitry Andric 
1030b57cec5SDimitry Andric namespace llvm {
1040b57cec5SDimitry Andric // Anything specified after the --args option are taken as arguments to the
1050b57cec5SDimitry Andric // program being debugged.
1060b57cec5SDimitry Andric cl::list<std::string> InputArgv("args", cl::Positional,
1070b57cec5SDimitry Andric                                 cl::desc("<program arguments>..."),
10881ad6265SDimitry Andric                                 cl::PositionalEatsArgs);
1090b57cec5SDimitry Andric 
1100b57cec5SDimitry Andric cl::opt<std::string>
1110b57cec5SDimitry Andric     OutputPrefix("output-prefix", cl::init("bugpoint"),
1120b57cec5SDimitry Andric                  cl::desc("Prefix to use for outputs (default: 'bugpoint')"));
1130b57cec5SDimitry Andric }
1140b57cec5SDimitry Andric 
1150b57cec5SDimitry Andric namespace {
1160b57cec5SDimitry Andric cl::list<std::string> ToolArgv("tool-args", cl::Positional,
11781ad6265SDimitry Andric                                cl::desc("<tool arguments>..."),
1180b57cec5SDimitry Andric                                cl::PositionalEatsArgs);
1190b57cec5SDimitry Andric 
1200b57cec5SDimitry Andric cl::list<std::string> SafeToolArgv("safe-tool-args", cl::Positional,
1210b57cec5SDimitry Andric                                    cl::desc("<safe-tool arguments>..."),
12281ad6265SDimitry Andric                                    cl::PositionalEatsArgs);
1230b57cec5SDimitry Andric 
1240b57cec5SDimitry Andric cl::opt<std::string> CCBinary("gcc", cl::init(""),
1250b57cec5SDimitry Andric                               cl::desc("The gcc binary to use."));
1260b57cec5SDimitry Andric 
1270b57cec5SDimitry Andric cl::list<std::string> CCToolArgv("gcc-tool-args", cl::Positional,
1280b57cec5SDimitry Andric                                  cl::desc("<gcc-tool arguments>..."),
12981ad6265SDimitry Andric                                  cl::PositionalEatsArgs);
1300b57cec5SDimitry Andric }
1310b57cec5SDimitry Andric 
1320b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
1330b57cec5SDimitry Andric // BugDriver method implementation
1340b57cec5SDimitry Andric //
1350b57cec5SDimitry Andric 
1360b57cec5SDimitry Andric /// initializeExecutionEnvironment - This method is used to set up the
1370b57cec5SDimitry Andric /// environment for executing LLVM programs.
1380b57cec5SDimitry Andric ///
1390b57cec5SDimitry Andric Error BugDriver::initializeExecutionEnvironment() {
1400b57cec5SDimitry Andric   outs() << "Initializing execution environment: ";
1410b57cec5SDimitry Andric 
1420b57cec5SDimitry Andric   // Create an instance of the AbstractInterpreter interface as specified on
1430b57cec5SDimitry Andric   // the command line
1440b57cec5SDimitry Andric   SafeInterpreter = nullptr;
1450b57cec5SDimitry Andric   std::string Message;
1460b57cec5SDimitry Andric 
1470b57cec5SDimitry Andric   if (CCBinary.empty()) {
1480b57cec5SDimitry Andric     if (ErrorOr<std::string> ClangPath =
1490b57cec5SDimitry Andric             FindProgramByName("clang", getToolName(), &AbsTolerance))
1500b57cec5SDimitry Andric       CCBinary = *ClangPath;
1510b57cec5SDimitry Andric     else
1520b57cec5SDimitry Andric       CCBinary = "gcc";
1530b57cec5SDimitry Andric   }
1540b57cec5SDimitry Andric 
1550b57cec5SDimitry Andric   switch (InterpreterSel) {
1560b57cec5SDimitry Andric   case AutoPick:
1570b57cec5SDimitry Andric     if (!Interpreter) {
1580b57cec5SDimitry Andric       InterpreterSel = RunJIT;
1590b57cec5SDimitry Andric       Interpreter =
1600b57cec5SDimitry Andric           AbstractInterpreter::createJIT(getToolName(), Message, &ToolArgv);
1610b57cec5SDimitry Andric     }
1620b57cec5SDimitry Andric     if (!Interpreter) {
1630b57cec5SDimitry Andric       InterpreterSel = RunLLC;
1640b57cec5SDimitry Andric       Interpreter = AbstractInterpreter::createLLC(
1650b57cec5SDimitry Andric           getToolName(), Message, CCBinary, &ToolArgv, &CCToolArgv);
1660b57cec5SDimitry Andric     }
1670b57cec5SDimitry Andric     if (!Interpreter) {
1680b57cec5SDimitry Andric       InterpreterSel = RunLLI;
1690b57cec5SDimitry Andric       Interpreter =
1700b57cec5SDimitry Andric           AbstractInterpreter::createLLI(getToolName(), Message, &ToolArgv);
1710b57cec5SDimitry Andric     }
1720b57cec5SDimitry Andric     if (!Interpreter) {
1730b57cec5SDimitry Andric       InterpreterSel = AutoPick;
1740b57cec5SDimitry Andric       Message = "Sorry, I can't automatically select an interpreter!\n";
1750b57cec5SDimitry Andric     }
1760b57cec5SDimitry Andric     break;
1770b57cec5SDimitry Andric   case RunLLI:
1780b57cec5SDimitry Andric     Interpreter =
1790b57cec5SDimitry Andric         AbstractInterpreter::createLLI(getToolName(), Message, &ToolArgv);
1800b57cec5SDimitry Andric     break;
1810b57cec5SDimitry Andric   case RunLLC:
1820b57cec5SDimitry Andric   case RunLLCIA:
1830b57cec5SDimitry Andric     Interpreter = AbstractInterpreter::createLLC(
1840b57cec5SDimitry Andric         getToolName(), Message, CCBinary, &ToolArgv, &CCToolArgv,
1850b57cec5SDimitry Andric         InterpreterSel == RunLLCIA);
1860b57cec5SDimitry Andric     break;
1870b57cec5SDimitry Andric   case RunJIT:
1880b57cec5SDimitry Andric     Interpreter =
1890b57cec5SDimitry Andric         AbstractInterpreter::createJIT(getToolName(), Message, &ToolArgv);
1900b57cec5SDimitry Andric     break;
1910b57cec5SDimitry Andric   case CompileCustom:
1920b57cec5SDimitry Andric     Interpreter = AbstractInterpreter::createCustomCompiler(
1930b57cec5SDimitry Andric         getToolName(), Message, CustomCompileCommand);
1940b57cec5SDimitry Andric     break;
1950b57cec5SDimitry Andric   case Custom:
1960b57cec5SDimitry Andric     Interpreter = AbstractInterpreter::createCustomExecutor(
1970b57cec5SDimitry Andric         getToolName(), Message, CustomExecCommand);
1980b57cec5SDimitry Andric     break;
1990b57cec5SDimitry Andric   }
2000b57cec5SDimitry Andric   if (!Interpreter)
2010b57cec5SDimitry Andric     errs() << Message;
2020b57cec5SDimitry Andric   else // Display informational messages on stdout instead of stderr
2030b57cec5SDimitry Andric     outs() << Message;
2040b57cec5SDimitry Andric 
2050b57cec5SDimitry Andric   std::string Path = SafeInterpreterPath;
2060b57cec5SDimitry Andric   if (Path.empty())
2070b57cec5SDimitry Andric     Path = getToolName();
2080b57cec5SDimitry Andric   std::vector<std::string> SafeToolArgs = SafeToolArgv;
2090b57cec5SDimitry Andric   switch (SafeInterpreterSel) {
2100b57cec5SDimitry Andric   case AutoPick:
2110b57cec5SDimitry Andric     // In "llc-safe" mode, default to using LLC as the "safe" backend.
212e8d8bef9SDimitry Andric     if (InterpreterSel == RunLLC) {
2130b57cec5SDimitry Andric       SafeInterpreterSel = RunLLC;
2140b57cec5SDimitry Andric       SafeToolArgs.push_back("--relocation-model=pic");
2150b57cec5SDimitry Andric       SafeInterpreter = AbstractInterpreter::createLLC(
2160b57cec5SDimitry Andric           Path.c_str(), Message, CCBinary, &SafeToolArgs, &CCToolArgv);
217e8d8bef9SDimitry Andric     } else if (InterpreterSel != CompileCustom) {
2180b57cec5SDimitry Andric       SafeInterpreterSel = AutoPick;
2190b57cec5SDimitry Andric       Message = "Sorry, I can't automatically select a safe interpreter!\n";
2200b57cec5SDimitry Andric     }
2210b57cec5SDimitry Andric     break;
2220b57cec5SDimitry Andric   case RunLLC:
2230b57cec5SDimitry Andric   case RunLLCIA:
2240b57cec5SDimitry Andric     SafeToolArgs.push_back("--relocation-model=pic");
2250b57cec5SDimitry Andric     SafeInterpreter = AbstractInterpreter::createLLC(
2260b57cec5SDimitry Andric         Path.c_str(), Message, CCBinary, &SafeToolArgs, &CCToolArgv,
2270b57cec5SDimitry Andric         SafeInterpreterSel == RunLLCIA);
2280b57cec5SDimitry Andric     break;
2290b57cec5SDimitry Andric   case Custom:
2300b57cec5SDimitry Andric     SafeInterpreter = AbstractInterpreter::createCustomExecutor(
2310b57cec5SDimitry Andric         getToolName(), Message, CustomExecCommand);
2320b57cec5SDimitry Andric     break;
2330b57cec5SDimitry Andric   default:
2340b57cec5SDimitry Andric     Message = "Sorry, this back-end is not supported by bugpoint as the "
2350b57cec5SDimitry Andric               "\"safe\" backend right now!\n";
2360b57cec5SDimitry Andric     break;
2370b57cec5SDimitry Andric   }
238e8d8bef9SDimitry Andric   if (!SafeInterpreter && InterpreterSel != CompileCustom) {
2390b57cec5SDimitry Andric     outs() << Message << "\nExiting.\n";
2400b57cec5SDimitry Andric     exit(1);
2410b57cec5SDimitry Andric   }
2420b57cec5SDimitry Andric 
2430b57cec5SDimitry Andric   cc = CC::create(getToolName(), Message, CCBinary, &CCToolArgv);
2440b57cec5SDimitry Andric   if (!cc) {
2450b57cec5SDimitry Andric     outs() << Message << "\nExiting.\n";
2460b57cec5SDimitry Andric     exit(1);
2470b57cec5SDimitry Andric   }
2480b57cec5SDimitry Andric 
2490b57cec5SDimitry Andric   // If there was an error creating the selected interpreter, quit with error.
2500b57cec5SDimitry Andric   if (Interpreter == nullptr)
2510b57cec5SDimitry Andric     return make_error<StringError>("Failed to init execution environment",
2520b57cec5SDimitry Andric                                    inconvertibleErrorCode());
2530b57cec5SDimitry Andric   return Error::success();
2540b57cec5SDimitry Andric }
2550b57cec5SDimitry Andric 
2560b57cec5SDimitry Andric /// Try to compile the specified module, returning false and setting Error if an
2570b57cec5SDimitry Andric /// error occurs.  This is used for code generation crash testing.
2580b57cec5SDimitry Andric Error BugDriver::compileProgram(Module &M) const {
2590b57cec5SDimitry Andric   // Emit the program to a bitcode file...
2600b57cec5SDimitry Andric   auto Temp =
2610b57cec5SDimitry Andric       sys::fs::TempFile::create(OutputPrefix + "-test-program-%%%%%%%.bc");
2620b57cec5SDimitry Andric   if (!Temp) {
2630b57cec5SDimitry Andric     errs() << ToolName
2640b57cec5SDimitry Andric            << ": Error making unique filename: " << toString(Temp.takeError())
2650b57cec5SDimitry Andric            << "\n";
2660b57cec5SDimitry Andric     exit(1);
2670b57cec5SDimitry Andric   }
2680b57cec5SDimitry Andric   DiscardTemp Discard{*Temp};
2690b57cec5SDimitry Andric   if (writeProgramToFile(Temp->FD, M)) {
2700b57cec5SDimitry Andric     errs() << ToolName << ": Error emitting bitcode to file '" << Temp->TmpName
2710b57cec5SDimitry Andric            << "'!\n";
2720b57cec5SDimitry Andric     exit(1);
2730b57cec5SDimitry Andric   }
2740b57cec5SDimitry Andric 
2750b57cec5SDimitry Andric   // Actually compile the program!
2760b57cec5SDimitry Andric   return Interpreter->compileProgram(Temp->TmpName, Timeout, MemoryLimit);
2770b57cec5SDimitry Andric }
2780b57cec5SDimitry Andric 
2790b57cec5SDimitry Andric /// This method runs "Program", capturing the output of the program to a file,
2800b57cec5SDimitry Andric /// returning the filename of the file.  A recommended filename may be
2810b57cec5SDimitry Andric /// optionally specified.
2820b57cec5SDimitry Andric Expected<std::string> BugDriver::executeProgram(const Module &Program,
2830b57cec5SDimitry Andric                                                 std::string OutputFile,
2840b57cec5SDimitry Andric                                                 std::string BitcodeFile,
2850b57cec5SDimitry Andric                                                 const std::string &SharedObj,
2860b57cec5SDimitry Andric                                                 AbstractInterpreter *AI) const {
2870b57cec5SDimitry Andric   if (!AI)
2880b57cec5SDimitry Andric     AI = Interpreter;
2890b57cec5SDimitry Andric   assert(AI && "Interpreter should have been created already!");
2900b57cec5SDimitry Andric   bool CreatedBitcode = false;
2910b57cec5SDimitry Andric   if (BitcodeFile.empty()) {
2920b57cec5SDimitry Andric     // Emit the program to a bitcode file...
2930b57cec5SDimitry Andric     SmallString<128> UniqueFilename;
2940b57cec5SDimitry Andric     int UniqueFD;
2950b57cec5SDimitry Andric     std::error_code EC = sys::fs::createUniqueFile(
2960b57cec5SDimitry Andric         OutputPrefix + "-test-program-%%%%%%%.bc", UniqueFD, UniqueFilename);
2970b57cec5SDimitry Andric     if (EC) {
2980b57cec5SDimitry Andric       errs() << ToolName << ": Error making unique filename: " << EC.message()
2990b57cec5SDimitry Andric              << "!\n";
3000b57cec5SDimitry Andric       exit(1);
3010b57cec5SDimitry Andric     }
302*7a6dacacSDimitry Andric     BitcodeFile = std::string(UniqueFilename);
3030b57cec5SDimitry Andric 
3040b57cec5SDimitry Andric     if (writeProgramToFile(BitcodeFile, UniqueFD, Program)) {
3050b57cec5SDimitry Andric       errs() << ToolName << ": Error emitting bitcode to file '" << BitcodeFile
3060b57cec5SDimitry Andric              << "'!\n";
3070b57cec5SDimitry Andric       exit(1);
3080b57cec5SDimitry Andric     }
3090b57cec5SDimitry Andric     CreatedBitcode = true;
3100b57cec5SDimitry Andric   }
3110b57cec5SDimitry Andric 
3120b57cec5SDimitry Andric   // Remove the temporary bitcode file when we are done.
3130b57cec5SDimitry Andric   std::string BitcodePath(BitcodeFile);
3140b57cec5SDimitry Andric   FileRemover BitcodeFileRemover(BitcodePath, CreatedBitcode && !SaveTemps);
3150b57cec5SDimitry Andric 
3160b57cec5SDimitry Andric   if (OutputFile.empty())
3170b57cec5SDimitry Andric     OutputFile = OutputPrefix + "-execution-output-%%%%%%%";
3180b57cec5SDimitry Andric 
3190b57cec5SDimitry Andric   // Check to see if this is a valid output filename...
3200b57cec5SDimitry Andric   SmallString<128> UniqueFile;
3210b57cec5SDimitry Andric   std::error_code EC = sys::fs::createUniqueFile(OutputFile, UniqueFile);
3220b57cec5SDimitry Andric   if (EC) {
3230b57cec5SDimitry Andric     errs() << ToolName << ": Error making unique filename: " << EC.message()
3240b57cec5SDimitry Andric            << "\n";
3250b57cec5SDimitry Andric     exit(1);
3260b57cec5SDimitry Andric   }
327*7a6dacacSDimitry Andric   OutputFile = std::string(UniqueFile);
3280b57cec5SDimitry Andric 
3290b57cec5SDimitry Andric   // Figure out which shared objects to run, if any.
3300b57cec5SDimitry Andric   std::vector<std::string> SharedObjs(AdditionalSOs);
3310b57cec5SDimitry Andric   if (!SharedObj.empty())
3320b57cec5SDimitry Andric     SharedObjs.push_back(SharedObj);
3330b57cec5SDimitry Andric 
3340b57cec5SDimitry Andric   Expected<int> RetVal = AI->ExecuteProgram(BitcodeFile, InputArgv, InputFile,
3350b57cec5SDimitry Andric                                             OutputFile, AdditionalLinkerArgs,
3360b57cec5SDimitry Andric                                             SharedObjs, Timeout, MemoryLimit);
3370b57cec5SDimitry Andric   if (Error E = RetVal.takeError())
3380b57cec5SDimitry Andric     return std::move(E);
3390b57cec5SDimitry Andric 
3400b57cec5SDimitry Andric   if (*RetVal == -1) {
3410b57cec5SDimitry Andric     errs() << "<timeout>";
3420b57cec5SDimitry Andric     static bool FirstTimeout = true;
3430b57cec5SDimitry Andric     if (FirstTimeout) {
3440b57cec5SDimitry Andric       outs()
3450b57cec5SDimitry Andric           << "\n"
3460b57cec5SDimitry Andric              "*** Program execution timed out!  This mechanism is designed to "
3470b57cec5SDimitry Andric              "handle\n"
3480b57cec5SDimitry Andric              "    programs stuck in infinite loops gracefully.  The -timeout "
3490b57cec5SDimitry Andric              "option\n"
3500b57cec5SDimitry Andric              "    can be used to change the timeout threshold or disable it "
3510b57cec5SDimitry Andric              "completely\n"
3520b57cec5SDimitry Andric              "    (with -timeout=0).  This message is only displayed once.\n";
3530b57cec5SDimitry Andric       FirstTimeout = false;
3540b57cec5SDimitry Andric     }
3550b57cec5SDimitry Andric   }
3560b57cec5SDimitry Andric 
3570b57cec5SDimitry Andric   if (AppendProgramExitCode) {
3580b57cec5SDimitry Andric     std::ofstream outFile(OutputFile.c_str(), std::ios_base::app);
3590b57cec5SDimitry Andric     outFile << "exit " << *RetVal << '\n';
3600b57cec5SDimitry Andric     outFile.close();
3610b57cec5SDimitry Andric   }
3620b57cec5SDimitry Andric 
3630b57cec5SDimitry Andric   // Return the filename we captured the output to.
3640b57cec5SDimitry Andric   return OutputFile;
3650b57cec5SDimitry Andric }
3660b57cec5SDimitry Andric 
3670b57cec5SDimitry Andric /// Used to create reference output with the "safe" backend, if reference output
3680b57cec5SDimitry Andric /// is not provided.
3690b57cec5SDimitry Andric Expected<std::string>
3700b57cec5SDimitry Andric BugDriver::executeProgramSafely(const Module &Program,
3710b57cec5SDimitry Andric                                 const std::string &OutputFile) const {
3720b57cec5SDimitry Andric   return executeProgram(Program, OutputFile, "", "", SafeInterpreter);
3730b57cec5SDimitry Andric }
3740b57cec5SDimitry Andric 
3750b57cec5SDimitry Andric Expected<std::string>
3760b57cec5SDimitry Andric BugDriver::compileSharedObject(const std::string &BitcodeFile) {
3770b57cec5SDimitry Andric   assert(Interpreter && "Interpreter should have been created already!");
3780b57cec5SDimitry Andric   std::string OutputFile;
3790b57cec5SDimitry Andric 
3800b57cec5SDimitry Andric   // Using the known-good backend.
3810b57cec5SDimitry Andric   Expected<CC::FileType> FT =
3820b57cec5SDimitry Andric       SafeInterpreter->OutputCode(BitcodeFile, OutputFile);
3830b57cec5SDimitry Andric   if (Error E = FT.takeError())
3840b57cec5SDimitry Andric     return std::move(E);
3850b57cec5SDimitry Andric 
3860b57cec5SDimitry Andric   std::string SharedObjectFile;
3870b57cec5SDimitry Andric   if (Error E = cc->MakeSharedObject(OutputFile, *FT, SharedObjectFile,
3880b57cec5SDimitry Andric                                      AdditionalLinkerArgs))
3890b57cec5SDimitry Andric     return std::move(E);
3900b57cec5SDimitry Andric 
3910b57cec5SDimitry Andric   // Remove the intermediate C file
3920b57cec5SDimitry Andric   sys::fs::remove(OutputFile);
3930b57cec5SDimitry Andric 
3940b57cec5SDimitry Andric   return SharedObjectFile;
3950b57cec5SDimitry Andric }
3960b57cec5SDimitry Andric 
3970b57cec5SDimitry Andric /// Calls compileProgram and then records the output into ReferenceOutputFile.
3980b57cec5SDimitry Andric /// Returns true if reference file created, false otherwise. Note:
3990b57cec5SDimitry Andric /// initializeExecutionEnvironment should be called BEFORE this function.
4000b57cec5SDimitry Andric Error BugDriver::createReferenceFile(Module &M, const std::string &Filename) {
4010b57cec5SDimitry Andric   if (Error E = compileProgram(*Program))
4020b57cec5SDimitry Andric     return E;
4030b57cec5SDimitry Andric 
4040b57cec5SDimitry Andric   Expected<std::string> Result = executeProgramSafely(*Program, Filename);
4050b57cec5SDimitry Andric   if (Error E = Result.takeError()) {
4060b57cec5SDimitry Andric     if (Interpreter != SafeInterpreter) {
4070b57cec5SDimitry Andric       E = joinErrors(
4080b57cec5SDimitry Andric               std::move(E),
4090b57cec5SDimitry Andric               make_error<StringError>(
4100b57cec5SDimitry Andric                   "*** There is a bug running the \"safe\" backend.  Either"
4110b57cec5SDimitry Andric                   " debug it (for example with the -run-jit bugpoint option,"
4120b57cec5SDimitry Andric                   " if JIT is being used as the \"safe\" backend), or fix the"
4130b57cec5SDimitry Andric                   " error some other way.\n",
4140b57cec5SDimitry Andric                   inconvertibleErrorCode()));
4150b57cec5SDimitry Andric     }
4160b57cec5SDimitry Andric     return E;
4170b57cec5SDimitry Andric   }
4180b57cec5SDimitry Andric   ReferenceOutputFile = *Result;
4190b57cec5SDimitry Andric   outs() << "\nReference output is: " << ReferenceOutputFile << "\n\n";
4200b57cec5SDimitry Andric   return Error::success();
4210b57cec5SDimitry Andric }
4220b57cec5SDimitry Andric 
4230b57cec5SDimitry Andric /// This method executes the specified module and diffs the output against the
4240b57cec5SDimitry Andric /// file specified by ReferenceOutputFile.  If the output is different, 1 is
4250b57cec5SDimitry Andric /// returned.  If there is a problem with the code generator (e.g., llc
4260b57cec5SDimitry Andric /// crashes), this will set ErrMsg.
4270b57cec5SDimitry Andric Expected<bool> BugDriver::diffProgram(const Module &Program,
4280b57cec5SDimitry Andric                                       const std::string &BitcodeFile,
4290b57cec5SDimitry Andric                                       const std::string &SharedObject,
4300b57cec5SDimitry Andric                                       bool RemoveBitcode) const {
4310b57cec5SDimitry Andric   // Execute the program, generating an output file...
4320b57cec5SDimitry Andric   Expected<std::string> Output =
4330b57cec5SDimitry Andric       executeProgram(Program, "", BitcodeFile, SharedObject, nullptr);
4340b57cec5SDimitry Andric   if (Error E = Output.takeError())
4350b57cec5SDimitry Andric     return std::move(E);
4360b57cec5SDimitry Andric 
4370b57cec5SDimitry Andric   std::string Error;
4380b57cec5SDimitry Andric   bool FilesDifferent = false;
4390b57cec5SDimitry Andric   if (int Diff = DiffFilesWithTolerance(ReferenceOutputFile, *Output,
4400b57cec5SDimitry Andric                                         AbsTolerance, RelTolerance, &Error)) {
4410b57cec5SDimitry Andric     if (Diff == 2) {
4420b57cec5SDimitry Andric       errs() << "While diffing output: " << Error << '\n';
4430b57cec5SDimitry Andric       exit(1);
4440b57cec5SDimitry Andric     }
4450b57cec5SDimitry Andric     FilesDifferent = true;
4460b57cec5SDimitry Andric   } else {
4470b57cec5SDimitry Andric     // Remove the generated output if there are no differences.
4480b57cec5SDimitry Andric     sys::fs::remove(*Output);
4490b57cec5SDimitry Andric   }
4500b57cec5SDimitry Andric 
4510b57cec5SDimitry Andric   // Remove the bitcode file if we are supposed to.
4520b57cec5SDimitry Andric   if (RemoveBitcode)
4530b57cec5SDimitry Andric     sys::fs::remove(BitcodeFile);
4540b57cec5SDimitry Andric   return FilesDifferent;
4550b57cec5SDimitry Andric }
4560b57cec5SDimitry Andric 
4570b57cec5SDimitry Andric bool BugDriver::isExecutingJIT() { return InterpreterSel == RunJIT; }
458