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