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