Home | History | Annotate | Download | only in bugpoint
      1 //===- ExecutionDriver.cpp - Allow execution of LLVM program --------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This file contains code used to execute the program utilizing one of the
     11 // various ways of running LLVM bitcode.
     12 //
     13 //===----------------------------------------------------------------------===//
     14 
     15 #include "BugDriver.h"
     16 #include "ToolRunner.h"
     17 #include "llvm/Support/CommandLine.h"
     18 #include "llvm/Support/Debug.h"
     19 #include "llvm/Support/FileUtilities.h"
     20 #include "llvm/Support/SystemUtils.h"
     21 #include "llvm/Support/raw_ostream.h"
     22 #include <fstream>
     23 
     24 using namespace llvm;
     25 
     26 namespace {
     27   // OutputType - Allow the user to specify the way code should be run, to test
     28   // for miscompilation.
     29   //
     30   enum OutputType {
     31     AutoPick, RunLLI, RunJIT, RunLLC, RunLLCIA, LLC_Safe, CompileCustom, Custom
     32   };
     33 
     34   cl::opt<double>
     35   AbsTolerance("abs-tolerance", cl::desc("Absolute error tolerated"),
     36                cl::init(0.0));
     37   cl::opt<double>
     38   RelTolerance("rel-tolerance", cl::desc("Relative error tolerated"),
     39                cl::init(0.0));
     40 
     41   cl::opt<OutputType>
     42   InterpreterSel(cl::desc("Specify the \"test\" i.e. suspect back-end:"),
     43                  cl::values(clEnumValN(AutoPick, "auto", "Use best guess"),
     44                             clEnumValN(RunLLI, "run-int",
     45                                        "Execute with the interpreter"),
     46                             clEnumValN(RunJIT, "run-jit", "Execute with JIT"),
     47                             clEnumValN(RunLLC, "run-llc", "Compile with LLC"),
     48                             clEnumValN(RunLLCIA, "run-llc-ia",
     49                                   "Compile with LLC with integrated assembler"),
     50                             clEnumValN(LLC_Safe, "llc-safe", "Use LLC for all"),
     51                             clEnumValN(CompileCustom, "compile-custom",
     52                             "Use -compile-command to define a command to "
     53                             "compile the bitcode. Useful to avoid linking."),
     54                             clEnumValN(Custom, "run-custom",
     55                             "Use -exec-command to define a command to execute "
     56                             "the bitcode. Useful for cross-compilation."),
     57                             clEnumValEnd),
     58                  cl::init(AutoPick));
     59 
     60   cl::opt<OutputType>
     61   SafeInterpreterSel(cl::desc("Specify \"safe\" i.e. known-good backend:"),
     62               cl::values(clEnumValN(AutoPick, "safe-auto", "Use best guess"),
     63                          clEnumValN(RunLLC, "safe-run-llc", "Compile with LLC"),
     64                          clEnumValN(Custom, "safe-run-custom",
     65                          "Use -exec-command to define a command to execute "
     66                          "the bitcode. Useful for cross-compilation."),
     67                          clEnumValEnd),
     68                      cl::init(AutoPick));
     69 
     70   cl::opt<std::string>
     71   SafeInterpreterPath("safe-path",
     72                    cl::desc("Specify the path to the \"safe\" backend program"),
     73                    cl::init(""));
     74 
     75   cl::opt<bool>
     76   AppendProgramExitCode("append-exit-code",
     77       cl::desc("Append the exit code to the output so it gets diff'd too"),
     78       cl::init(false));
     79 
     80   cl::opt<std::string>
     81   InputFile("input", cl::init("/dev/null"),
     82             cl::desc("Filename to pipe in as stdin (default: /dev/null)"));
     83 
     84   cl::list<std::string>
     85   AdditionalSOs("additional-so",
     86                 cl::desc("Additional shared objects to load "
     87                          "into executing programs"));
     88 
     89   cl::list<std::string>
     90   AdditionalLinkerArgs("Xlinker",
     91       cl::desc("Additional arguments to pass to the linker"));
     92 
     93   cl::opt<std::string>
     94   CustomCompileCommand("compile-command", cl::init("llc"),
     95       cl::desc("Command to compile the bitcode (use with -compile-custom) "
     96                "(default: llc)"));
     97 
     98   cl::opt<std::string>
     99   CustomExecCommand("exec-command", cl::init("simulate"),
    100       cl::desc("Command to execute the bitcode (use with -run-custom) "
    101                "(default: simulate)"));
    102 }
    103 
    104 namespace llvm {
    105   // Anything specified after the --args option are taken as arguments to the
    106   // program being debugged.
    107   cl::list<std::string>
    108   InputArgv("args", cl::Positional, cl::desc("<program arguments>..."),
    109             cl::ZeroOrMore, cl::PositionalEatsArgs);
    110 
    111   cl::opt<std::string>
    112   OutputPrefix("output-prefix", cl::init("bugpoint"),
    113             cl::desc("Prefix to use for outputs (default: 'bugpoint')"));
    114 }
    115 
    116 namespace {
    117   cl::list<std::string>
    118   ToolArgv("tool-args", cl::Positional, cl::desc("<tool arguments>..."),
    119            cl::ZeroOrMore, cl::PositionalEatsArgs);
    120 
    121   cl::list<std::string>
    122   SafeToolArgv("safe-tool-args", cl::Positional,
    123                cl::desc("<safe-tool arguments>..."),
    124                cl::ZeroOrMore, cl::PositionalEatsArgs);
    125 
    126   cl::opt<std::string>
    127   GCCBinary("gcc", cl::init("gcc"),
    128               cl::desc("The gcc binary to use. (default 'gcc')"));
    129 
    130   cl::list<std::string>
    131   GCCToolArgv("gcc-tool-args", cl::Positional,
    132               cl::desc("<gcc-tool arguments>..."),
    133               cl::ZeroOrMore, cl::PositionalEatsArgs);
    134 }
    135 
    136 //===----------------------------------------------------------------------===//
    137 // BugDriver method implementation
    138 //
    139 
    140 /// initializeExecutionEnvironment - This method is used to set up the
    141 /// environment for executing LLVM programs.
    142 ///
    143 bool BugDriver::initializeExecutionEnvironment() {
    144   outs() << "Initializing execution environment: ";
    145 
    146   // Create an instance of the AbstractInterpreter interface as specified on
    147   // the command line
    148   SafeInterpreter = nullptr;
    149   std::string Message;
    150 
    151   switch (InterpreterSel) {
    152   case AutoPick:
    153     if (!Interpreter) {
    154       InterpreterSel = RunJIT;
    155       Interpreter = AbstractInterpreter::createJIT(getToolName(), Message,
    156                                                    &ToolArgv);
    157     }
    158     if (!Interpreter) {
    159       InterpreterSel = RunLLC;
    160       Interpreter = AbstractInterpreter::createLLC(getToolName(), Message,
    161                                                    GCCBinary, &ToolArgv,
    162                                                    &GCCToolArgv);
    163     }
    164     if (!Interpreter) {
    165       InterpreterSel = RunLLI;
    166       Interpreter = AbstractInterpreter::createLLI(getToolName(), Message,
    167                                                    &ToolArgv);
    168     }
    169     if (!Interpreter) {
    170       InterpreterSel = AutoPick;
    171       Message = "Sorry, I can't automatically select an interpreter!\n";
    172     }
    173     break;
    174   case RunLLI:
    175     Interpreter = AbstractInterpreter::createLLI(getToolName(), Message,
    176                                                  &ToolArgv);
    177     break;
    178   case RunLLC:
    179   case RunLLCIA:
    180   case LLC_Safe:
    181     Interpreter = AbstractInterpreter::createLLC(getToolName(), Message,
    182                                                  GCCBinary, &ToolArgv,
    183                                                  &GCCToolArgv,
    184                                                  InterpreterSel == RunLLCIA);
    185     break;
    186   case RunJIT:
    187     Interpreter = AbstractInterpreter::createJIT(getToolName(), Message,
    188                                                  &ToolArgv);
    189     break;
    190   case CompileCustom:
    191     Interpreter =
    192       AbstractInterpreter::createCustomCompiler(Message, CustomCompileCommand);
    193     break;
    194   case Custom:
    195     Interpreter =
    196       AbstractInterpreter::createCustomExecutor(Message, CustomExecCommand);
    197     break;
    198   }
    199   if (!Interpreter)
    200     errs() << Message;
    201   else // Display informational messages on stdout instead of stderr
    202     outs() << Message;
    203 
    204   std::string Path = SafeInterpreterPath;
    205   if (Path.empty())
    206     Path = getToolName();
    207   std::vector<std::string> SafeToolArgs = SafeToolArgv;
    208   switch (SafeInterpreterSel) {
    209   case AutoPick:
    210     // In "llc-safe" mode, default to using LLC as the "safe" backend.
    211     if (!SafeInterpreter &&
    212         InterpreterSel == LLC_Safe) {
    213       SafeInterpreterSel = RunLLC;
    214       SafeToolArgs.push_back("--relocation-model=pic");
    215       SafeInterpreter = AbstractInterpreter::createLLC(Path.c_str(), Message,
    216                                                        GCCBinary,
    217                                                        &SafeToolArgs,
    218                                                        &GCCToolArgv);
    219     }
    220 
    221     if (!SafeInterpreter &&
    222         InterpreterSel != RunLLC &&
    223         InterpreterSel != RunJIT) {
    224       SafeInterpreterSel = RunLLC;
    225       SafeToolArgs.push_back("--relocation-model=pic");
    226       SafeInterpreter = AbstractInterpreter::createLLC(Path.c_str(), Message,
    227                                                        GCCBinary,
    228                                                        &SafeToolArgs,
    229                                                        &GCCToolArgv);
    230     }
    231     if (!SafeInterpreter) {
    232       SafeInterpreterSel = AutoPick;
    233       Message = "Sorry, I can't automatically select a safe interpreter!\n";
    234     }
    235     break;
    236   case RunLLC:
    237   case RunLLCIA:
    238     SafeToolArgs.push_back("--relocation-model=pic");
    239     SafeInterpreter = AbstractInterpreter::createLLC(Path.c_str(), Message,
    240                                                      GCCBinary, &SafeToolArgs,
    241                                                      &GCCToolArgv,
    242                                                 SafeInterpreterSel == RunLLCIA);
    243     break;
    244   case Custom:
    245     SafeInterpreter =
    246       AbstractInterpreter::createCustomExecutor(Message, CustomExecCommand);
    247     break;
    248   default:
    249     Message = "Sorry, this back-end is not supported by bugpoint as the "
    250               "\"safe\" backend right now!\n";
    251     break;
    252   }
    253   if (!SafeInterpreter) { outs() << Message << "\nExiting.\n"; exit(1); }
    254 
    255   gcc = GCC::create(Message, GCCBinary, &GCCToolArgv);
    256   if (!gcc) { outs() << Message << "\nExiting.\n"; exit(1); }
    257 
    258   // If there was an error creating the selected interpreter, quit with error.
    259   return Interpreter == nullptr;
    260 }
    261 
    262 /// compileProgram - Try to compile the specified module, returning false and
    263 /// setting Error if an error occurs.  This is used for code generation
    264 /// crash testing.
    265 ///
    266 void BugDriver::compileProgram(Module *M, std::string *Error) const {
    267   // Emit the program to a bitcode file...
    268   SmallString<128> BitcodeFile;
    269   int BitcodeFD;
    270   std::error_code EC = sys::fs::createUniqueFile(
    271       OutputPrefix + "-test-program-%%%%%%%.bc", BitcodeFD, BitcodeFile);
    272   if (EC) {
    273     errs() << ToolName << ": Error making unique filename: " << EC.message()
    274            << "\n";
    275     exit(1);
    276   }
    277   if (writeProgramToFile(BitcodeFile.str(), BitcodeFD, M)) {
    278     errs() << ToolName << ": Error emitting bitcode to file '" << BitcodeFile
    279            << "'!\n";
    280     exit(1);
    281   }
    282 
    283   // Remove the temporary bitcode file when we are done.
    284   FileRemover BitcodeFileRemover(BitcodeFile.str(), !SaveTemps);
    285 
    286   // Actually compile the program!
    287   Interpreter->compileProgram(BitcodeFile.str(), Error, Timeout, MemoryLimit);
    288 }
    289 
    290 
    291 /// executeProgram - This method runs "Program", capturing the output of the
    292 /// program to a file, returning the filename of the file.  A recommended
    293 /// filename may be optionally specified.
    294 ///
    295 std::string BugDriver::executeProgram(const Module *Program,
    296                                       std::string OutputFile,
    297                                       std::string BitcodeFile,
    298                                       const std::string &SharedObj,
    299                                       AbstractInterpreter *AI,
    300                                       std::string *Error) const {
    301   if (!AI) AI = Interpreter;
    302   assert(AI && "Interpreter should have been created already!");
    303   bool CreatedBitcode = false;
    304   if (BitcodeFile.empty()) {
    305     // Emit the program to a bitcode file...
    306     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: "
    312              << EC.message() << "!\n";
    313       exit(1);
    314     }
    315     BitcodeFile = UniqueFilename.str();
    316 
    317     if (writeProgramToFile(BitcodeFile, UniqueFD, Program)) {
    318       errs() << ToolName << ": Error emitting bitcode to file '"
    319              << BitcodeFile << "'!\n";
    320       exit(1);
    321     }
    322     CreatedBitcode = true;
    323   }
    324 
    325   // Remove the temporary bitcode file when we are done.
    326   std::string BitcodePath(BitcodeFile);
    327   FileRemover BitcodeFileRemover(BitcodePath,
    328     CreatedBitcode && !SaveTemps);
    329 
    330   if (OutputFile.empty()) OutputFile = OutputPrefix + "-execution-output-%%%%%%%";
    331 
    332   // Check to see if this is a valid output filename...
    333   SmallString<128> UniqueFile;
    334   std::error_code EC = sys::fs::createUniqueFile(OutputFile, UniqueFile);
    335   if (EC) {
    336     errs() << ToolName << ": Error making unique filename: "
    337            << EC.message() << "\n";
    338     exit(1);
    339   }
    340   OutputFile = UniqueFile.str();
    341 
    342   // Figure out which shared objects to run, if any.
    343   std::vector<std::string> SharedObjs(AdditionalSOs);
    344   if (!SharedObj.empty())
    345     SharedObjs.push_back(SharedObj);
    346 
    347   int RetVal = AI->ExecuteProgram(BitcodeFile, InputArgv, InputFile, OutputFile,
    348                                   Error, AdditionalLinkerArgs, SharedObjs,
    349                                   Timeout, MemoryLimit);
    350   if (!Error->empty())
    351     return OutputFile;
    352 
    353   if (RetVal == -1) {
    354     errs() << "<timeout>";
    355     static bool FirstTimeout = true;
    356     if (FirstTimeout) {
    357       outs() << "\n"
    358  "*** Program execution timed out!  This mechanism is designed to handle\n"
    359  "    programs stuck in infinite loops gracefully.  The -timeout option\n"
    360  "    can be used to change the timeout threshold or disable it completely\n"
    361  "    (with -timeout=0).  This message is only displayed once.\n";
    362       FirstTimeout = false;
    363     }
    364   }
    365 
    366   if (AppendProgramExitCode) {
    367     std::ofstream outFile(OutputFile.c_str(), std::ios_base::app);
    368     outFile << "exit " << RetVal << '\n';
    369     outFile.close();
    370   }
    371 
    372   // Return the filename we captured the output to.
    373   return OutputFile;
    374 }
    375 
    376 /// executeProgramSafely - Used to create reference output with the "safe"
    377 /// backend, if reference output is not provided.
    378 ///
    379 std::string BugDriver::executeProgramSafely(const Module *Program,
    380                                             std::string OutputFile,
    381                                             std::string *Error) const {
    382   return executeProgram(Program, OutputFile, "", "", SafeInterpreter, Error);
    383 }
    384 
    385 std::string BugDriver::compileSharedObject(const std::string &BitcodeFile,
    386                                            std::string &Error) {
    387   assert(Interpreter && "Interpreter should have been created already!");
    388   std::string OutputFile;
    389 
    390   // Using the known-good backend.
    391   GCC::FileType FT = SafeInterpreter->OutputCode(BitcodeFile, OutputFile,
    392                                                  Error);
    393   if (!Error.empty())
    394     return "";
    395 
    396   std::string SharedObjectFile;
    397   bool Failure = gcc->MakeSharedObject(OutputFile, FT, SharedObjectFile,
    398                                        AdditionalLinkerArgs, Error);
    399   if (!Error.empty())
    400     return "";
    401   if (Failure)
    402     exit(1);
    403 
    404   // Remove the intermediate C file
    405   sys::fs::remove(OutputFile);
    406 
    407   return SharedObjectFile;
    408 }
    409 
    410 /// createReferenceFile - calls compileProgram and then records the output
    411 /// into ReferenceOutputFile. Returns true if reference file created, false
    412 /// otherwise. Note: initializeExecutionEnvironment should be called BEFORE
    413 /// this function.
    414 ///
    415 bool BugDriver::createReferenceFile(Module *M, const std::string &Filename) {
    416   std::string Error;
    417   compileProgram(Program, &Error);
    418   if (!Error.empty())
    419     return false;
    420 
    421   ReferenceOutputFile = executeProgramSafely(Program, Filename, &Error);
    422   if (!Error.empty()) {
    423     errs() << Error;
    424     if (Interpreter != SafeInterpreter) {
    425       errs() << "*** There is a bug running the \"safe\" backend.  Either"
    426              << " debug it (for example with the -run-jit bugpoint option,"
    427              << " if JIT is being used as the \"safe\" backend), or fix the"
    428              << " error some other way.\n";
    429     }
    430     return false;
    431   }
    432   outs() << "\nReference output is: " << ReferenceOutputFile << "\n\n";
    433   return true;
    434 }
    435 
    436 /// diffProgram - This method executes the specified module and diffs the
    437 /// output against the file specified by ReferenceOutputFile.  If the output
    438 /// is different, 1 is returned.  If there is a problem with the code
    439 /// generator (e.g., llc crashes), this will set ErrMsg.
    440 ///
    441 bool BugDriver::diffProgram(const Module *Program,
    442                             const std::string &BitcodeFile,
    443                             const std::string &SharedObject,
    444                             bool RemoveBitcode,
    445                             std::string *ErrMsg) const {
    446   // Execute the program, generating an output file...
    447   std::string Output(
    448       executeProgram(Program, "", BitcodeFile, SharedObject, nullptr, ErrMsg));
    449   if (!ErrMsg->empty())
    450     return false;
    451 
    452   std::string Error;
    453   bool FilesDifferent = false;
    454   if (int Diff = DiffFilesWithTolerance(ReferenceOutputFile,
    455                                         Output,
    456                                         AbsTolerance, RelTolerance, &Error)) {
    457     if (Diff == 2) {
    458       errs() << "While diffing output: " << Error << '\n';
    459       exit(1);
    460     }
    461     FilesDifferent = true;
    462   }
    463   else {
    464     // Remove the generated output if there are no differences.
    465     sys::fs::remove(Output);
    466   }
    467 
    468   // Remove the bitcode file if we are supposed to.
    469   if (RemoveBitcode)
    470     sys::fs::remove(BitcodeFile);
    471   return FilesDifferent;
    472 }
    473 
    474 bool BugDriver::isExecutingJIT() {
    475   return InterpreterSel == RunJIT;
    476 }
    477 
    478