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