1 //===- ExecutionDriver.cpp - Allow execution of LLVM program --------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file contains code used to execute the program utilizing one of the
10 // various ways of running LLVM bitcode.
12 //===----------------------------------------------------------------------===//
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"
27 // OutputType - Allow the user to specify the way code should be run, to test
28 // for miscompilation.
40 cl::opt
<double> AbsTolerance("abs-tolerance",
41 cl::desc("Absolute error tolerated"),
43 cl::opt
<double> RelTolerance("rel-tolerance",
44 cl::desc("Relative error tolerated"),
47 cl::opt
<OutputType
> InterpreterSel(
48 cl::desc("Specify the \"test\" i.e. suspect back-end:"),
49 cl::values(clEnumValN(AutoPick
, "auto", "Use best guess"),
50 clEnumValN(RunLLI
, "run-int", "Execute with the interpreter"),
51 clEnumValN(RunJIT
, "run-jit", "Execute with JIT"),
52 clEnumValN(RunLLC
, "run-llc", "Compile with LLC"),
53 clEnumValN(RunLLCIA
, "run-llc-ia",
54 "Compile with LLC with integrated assembler"),
55 clEnumValN(CompileCustom
, "compile-custom",
56 "Use -compile-command to define a command to "
57 "compile the bitcode. Useful to avoid linking."),
58 clEnumValN(Custom
, "run-custom",
59 "Use -exec-command to define a command to execute "
60 "the bitcode. Useful for cross-compilation.")),
63 cl::opt
<OutputType
> SafeInterpreterSel(
64 cl::desc("Specify \"safe\" i.e. known-good backend:"),
65 cl::values(clEnumValN(AutoPick
, "safe-auto", "Use best guess"),
66 clEnumValN(RunLLC
, "safe-run-llc", "Compile with LLC"),
67 clEnumValN(Custom
, "safe-run-custom",
68 "Use -exec-command to define a command to execute "
69 "the bitcode. Useful for cross-compilation.")),
72 cl::opt
<std::string
> SafeInterpreterPath(
73 "safe-path", cl::desc("Specify the path to the \"safe\" backend program"),
76 cl::opt
<bool> AppendProgramExitCode(
78 cl::desc("Append the exit code to the output so it gets diff'd too"),
82 InputFile("input", cl::init("/dev/null"),
83 cl::desc("Filename to pipe in as stdin (default: /dev/null)"));
86 AdditionalSOs("additional-so", cl::desc("Additional shared objects to load "
87 "into executing programs"));
89 cl::list
<std::string
> AdditionalLinkerArgs(
90 "Xlinker", cl::desc("Additional arguments to pass to the linker"));
92 cl::opt
<std::string
> CustomCompileCommand(
93 "compile-command", cl::init("llc"),
94 cl::desc("Command to compile the bitcode (use with -compile-custom) "
97 cl::opt
<std::string
> CustomExecCommand(
98 "exec-command", cl::init("simulate"),
99 cl::desc("Command to execute the bitcode (use with -run-custom) "
100 "(default: simulate)"));
104 // Anything specified after the --args option are taken as arguments to the
105 // program being debugged.
106 cl::list
<std::string
> InputArgv("args", cl::Positional
,
107 cl::desc("<program arguments>..."),
108 cl::PositionalEatsArgs
);
111 OutputPrefix("output-prefix", cl::init("bugpoint"),
112 cl::desc("Prefix to use for outputs (default: 'bugpoint')"));
116 cl::list
<std::string
> ToolArgv("tool-args", cl::Positional
,
117 cl::desc("<tool arguments>..."),
118 cl::PositionalEatsArgs
);
120 cl::list
<std::string
> SafeToolArgv("safe-tool-args", cl::Positional
,
121 cl::desc("<safe-tool arguments>..."),
122 cl::PositionalEatsArgs
);
124 cl::opt
<std::string
> CCBinary("gcc", cl::init(""),
125 cl::desc("The gcc binary to use."));
127 cl::list
<std::string
> CCToolArgv("gcc-tool-args", cl::Positional
,
128 cl::desc("<gcc-tool arguments>..."),
129 cl::PositionalEatsArgs
);
132 //===----------------------------------------------------------------------===//
133 // BugDriver method implementation
136 /// initializeExecutionEnvironment - This method is used to set up the
137 /// environment for executing LLVM programs.
139 Error
BugDriver::initializeExecutionEnvironment() {
140 outs() << "Initializing execution environment: ";
142 // Create an instance of the AbstractInterpreter interface as specified on
144 SafeInterpreter
= nullptr;
147 if (CCBinary
.empty()) {
148 if (ErrorOr
<std::string
> ClangPath
=
149 FindProgramByName("clang", getToolName(), &AbsTolerance
))
150 CCBinary
= *ClangPath
;
155 switch (InterpreterSel
) {
158 InterpreterSel
= RunJIT
;
160 AbstractInterpreter::createJIT(getToolName(), Message
, &ToolArgv
);
163 InterpreterSel
= RunLLC
;
164 Interpreter
= AbstractInterpreter::createLLC(
165 getToolName(), Message
, CCBinary
, &ToolArgv
, &CCToolArgv
);
168 InterpreterSel
= RunLLI
;
170 AbstractInterpreter::createLLI(getToolName(), Message
, &ToolArgv
);
173 InterpreterSel
= AutoPick
;
174 Message
= "Sorry, I can't automatically select an interpreter!\n";
179 AbstractInterpreter::createLLI(getToolName(), Message
, &ToolArgv
);
183 Interpreter
= AbstractInterpreter::createLLC(
184 getToolName(), Message
, CCBinary
, &ToolArgv
, &CCToolArgv
,
185 InterpreterSel
== RunLLCIA
);
189 AbstractInterpreter::createJIT(getToolName(), Message
, &ToolArgv
);
192 Interpreter
= AbstractInterpreter::createCustomCompiler(
193 getToolName(), Message
, CustomCompileCommand
);
196 Interpreter
= AbstractInterpreter::createCustomExecutor(
197 getToolName(), Message
, CustomExecCommand
);
202 else // Display informational messages on stdout instead of stderr
205 std::string Path
= SafeInterpreterPath
;
207 Path
= getToolName();
208 std::vector
<std::string
> SafeToolArgs
= SafeToolArgv
;
209 switch (SafeInterpreterSel
) {
211 // In "llc-safe" mode, default to using LLC as the "safe" backend.
212 if (InterpreterSel
== RunLLC
) {
213 SafeInterpreterSel
= RunLLC
;
214 SafeToolArgs
.push_back("--relocation-model=pic");
215 SafeInterpreter
= AbstractInterpreter::createLLC(
216 Path
.c_str(), Message
, CCBinary
, &SafeToolArgs
, &CCToolArgv
);
217 } else if (InterpreterSel
!= CompileCustom
) {
218 SafeInterpreterSel
= AutoPick
;
219 Message
= "Sorry, I can't automatically select a safe interpreter!\n";
224 SafeToolArgs
.push_back("--relocation-model=pic");
225 SafeInterpreter
= AbstractInterpreter::createLLC(
226 Path
.c_str(), Message
, CCBinary
, &SafeToolArgs
, &CCToolArgv
,
227 SafeInterpreterSel
== RunLLCIA
);
230 SafeInterpreter
= AbstractInterpreter::createCustomExecutor(
231 getToolName(), Message
, CustomExecCommand
);
234 Message
= "Sorry, this back-end is not supported by bugpoint as the "
235 "\"safe\" backend right now!\n";
238 if (!SafeInterpreter
&& InterpreterSel
!= CompileCustom
) {
239 outs() << Message
<< "\nExiting.\n";
243 cc
= CC::create(getToolName(), Message
, CCBinary
, &CCToolArgv
);
245 outs() << Message
<< "\nExiting.\n";
249 // If there was an error creating the selected interpreter, quit with error.
250 if (Interpreter
== nullptr)
251 return make_error
<StringError
>("Failed to init execution environment",
252 inconvertibleErrorCode());
253 return Error::success();
256 /// Try to compile the specified module, returning false and setting Error if an
257 /// error occurs. This is used for code generation crash testing.
258 Error
BugDriver::compileProgram(Module
&M
) const {
259 // Emit the program to a bitcode file...
261 sys::fs::TempFile::create(OutputPrefix
+ "-test-program-%%%%%%%.bc");
264 << ": Error making unique filename: " << toString(Temp
.takeError())
268 DiscardTemp Discard
{*Temp
};
269 if (writeProgramToFile(Temp
->FD
, M
)) {
270 errs() << ToolName
<< ": Error emitting bitcode to file '" << Temp
->TmpName
275 // Actually compile the program!
276 return Interpreter
->compileProgram(Temp
->TmpName
, Timeout
, MemoryLimit
);
279 /// This method runs "Program", capturing the output of the program to a file,
280 /// returning the filename of the file. A recommended filename may be
281 /// optionally specified.
282 Expected
<std::string
> BugDriver::executeProgram(const Module
&Program
,
283 std::string OutputFile
,
284 std::string BitcodeFile
,
285 const std::string
&SharedObj
,
286 AbstractInterpreter
*AI
) const {
289 assert(AI
&& "Interpreter should have been created already!");
290 bool CreatedBitcode
= false;
291 if (BitcodeFile
.empty()) {
292 // Emit the program to a bitcode file...
293 SmallString
<128> UniqueFilename
;
295 std::error_code EC
= sys::fs::createUniqueFile(
296 OutputPrefix
+ "-test-program-%%%%%%%.bc", UniqueFD
, UniqueFilename
);
298 errs() << ToolName
<< ": Error making unique filename: " << EC
.message()
302 BitcodeFile
= std::string(UniqueFilename
);
304 if (writeProgramToFile(BitcodeFile
, UniqueFD
, Program
)) {
305 errs() << ToolName
<< ": Error emitting bitcode to file '" << BitcodeFile
309 CreatedBitcode
= true;
312 // Remove the temporary bitcode file when we are done.
313 std::string
BitcodePath(BitcodeFile
);
314 FileRemover
BitcodeFileRemover(BitcodePath
, CreatedBitcode
&& !SaveTemps
);
316 if (OutputFile
.empty())
317 OutputFile
= OutputPrefix
+ "-execution-output-%%%%%%%";
319 // Check to see if this is a valid output filename...
320 SmallString
<128> UniqueFile
;
321 std::error_code EC
= sys::fs::createUniqueFile(OutputFile
, UniqueFile
);
323 errs() << ToolName
<< ": Error making unique filename: " << EC
.message()
327 OutputFile
= std::string(UniqueFile
);
329 // Figure out which shared objects to run, if any.
330 std::vector
<std::string
> SharedObjs(AdditionalSOs
);
331 if (!SharedObj
.empty())
332 SharedObjs
.push_back(SharedObj
);
334 Expected
<int> RetVal
= AI
->ExecuteProgram(BitcodeFile
, InputArgv
, InputFile
,
335 OutputFile
, AdditionalLinkerArgs
,
336 SharedObjs
, Timeout
, MemoryLimit
);
337 if (Error E
= RetVal
.takeError())
341 errs() << "<timeout>";
342 static bool FirstTimeout
= true;
346 "*** Program execution timed out! This mechanism is designed to "
348 " programs stuck in infinite loops gracefully. The -timeout "
350 " can be used to change the timeout threshold or disable it "
352 " (with -timeout=0). This message is only displayed once.\n";
353 FirstTimeout
= false;
357 if (AppendProgramExitCode
) {
358 std::ofstream
outFile(OutputFile
.c_str(), std::ios_base::app
);
359 outFile
<< "exit " << *RetVal
<< '\n';
363 // Return the filename we captured the output to.
367 /// Used to create reference output with the "safe" backend, if reference output
369 Expected
<std::string
>
370 BugDriver::executeProgramSafely(const Module
&Program
,
371 const std::string
&OutputFile
) const {
372 return executeProgram(Program
, OutputFile
, "", "", SafeInterpreter
);
375 Expected
<std::string
>
376 BugDriver::compileSharedObject(const std::string
&BitcodeFile
) {
377 assert(Interpreter
&& "Interpreter should have been created already!");
378 std::string OutputFile
;
380 // Using the known-good backend.
381 Expected
<CC::FileType
> FT
=
382 SafeInterpreter
->OutputCode(BitcodeFile
, OutputFile
);
383 if (Error E
= FT
.takeError())
386 std::string SharedObjectFile
;
387 if (Error E
= cc
->MakeSharedObject(OutputFile
, *FT
, SharedObjectFile
,
388 AdditionalLinkerArgs
))
391 // Remove the intermediate C file
392 sys::fs::remove(OutputFile
);
394 return SharedObjectFile
;
397 /// Calls compileProgram and then records the output into ReferenceOutputFile.
398 /// Returns true if reference file created, false otherwise. Note:
399 /// initializeExecutionEnvironment should be called BEFORE this function.
400 Error
BugDriver::createReferenceFile(Module
&M
, const std::string
&Filename
) {
401 if (Error E
= compileProgram(*Program
))
404 Expected
<std::string
> Result
= executeProgramSafely(*Program
, Filename
);
405 if (Error E
= Result
.takeError()) {
406 if (Interpreter
!= SafeInterpreter
) {
409 make_error
<StringError
>(
410 "*** There is a bug running the \"safe\" backend. Either"
411 " debug it (for example with the -run-jit bugpoint option,"
412 " if JIT is being used as the \"safe\" backend), or fix the"
413 " error some other way.\n",
414 inconvertibleErrorCode()));
418 ReferenceOutputFile
= *Result
;
419 outs() << "\nReference output is: " << ReferenceOutputFile
<< "\n\n";
420 return Error::success();
423 /// This method executes the specified module and diffs the output against the
424 /// file specified by ReferenceOutputFile. If the output is different, 1 is
425 /// returned. If there is a problem with the code generator (e.g., llc
426 /// crashes), this will set ErrMsg.
427 Expected
<bool> BugDriver::diffProgram(const Module
&Program
,
428 const std::string
&BitcodeFile
,
429 const std::string
&SharedObject
,
430 bool RemoveBitcode
) const {
431 // Execute the program, generating an output file...
432 Expected
<std::string
> Output
=
433 executeProgram(Program
, "", BitcodeFile
, SharedObject
, nullptr);
434 if (Error E
= Output
.takeError())
438 bool FilesDifferent
= false;
439 if (int Diff
= DiffFilesWithTolerance(ReferenceOutputFile
, *Output
,
440 AbsTolerance
, RelTolerance
, &Error
)) {
442 errs() << "While diffing output: " << Error
<< '\n';
445 FilesDifferent
= true;
447 // Remove the generated output if there are no differences.
448 sys::fs::remove(*Output
);
451 // Remove the bitcode file if we are supposed to.
453 sys::fs::remove(BitcodeFile
);
454 return FilesDifferent
;
457 bool BugDriver::isExecutingJIT() { return InterpreterSel
== RunJIT
; }