#include "BugDriver.h"
#include "ToolRunner.h"
#include "llvm/Pass.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm>
#include <ctime>
using namespace llvm;
bool BugDriver::runManyPasses(const std::vector<const PassInfo*> &AllPasses) {
setPassesToRun(AllPasses);
outs() << "Starting bug finding procedure...\n\n";
if (initializeExecutionEnvironment()) return false;
outs() << "\n";
if (ReferenceOutputFile.empty()) {
outs() << "Generating reference output from raw program: \n";
if (!createReferenceFile(Program))
return false;
}
srand(time(NULL));
unsigned num = 1;
while(1) {
std::random_shuffle(PassesToRun.begin(), PassesToRun.end());
outs() << "Running selected passes on program to test for crash: ";
for(int i = 0, e = PassesToRun.size(); i != e; i++) {
outs() << "-" << PassesToRun[i]->getPassArgument( )<< " ";
}
std::string Filename;
if(runPasses(PassesToRun, Filename, false)) {
outs() << "\n";
outs() << "Optimizer passes caused failure!\n\n";
debugOptimizerCrash();
return true;
} else {
outs() << "Combination " << num << " optimized successfully!\n";
}
outs() << "Running the code generator to test for a crash: ";
try {
compileProgram(Program);
outs() << '\n';
} catch (ToolExecutionError &TEE) {
outs() << "\n*** compileProgram threw an exception: ";
outs() << TEE.what();
return debugCodeGeneratorCrash();
}
outs() << "*** Checking if passes caused miscompliation:\n";
try {
if (diffProgram(Filename, "", false)) {
outs() << "\n*** diffProgram returned true!\n";
debugMiscompilation();
return true;
} else {
outs() << "\n*** diff'd output matches!\n";
}
} catch (ToolExecutionError &TEE) {
errs() << TEE.what();
debugCodeGeneratorCrash();
return true;
}
sys::Path(Filename).eraseFromDisk();
outs() << "\n\n";
num++;
}
}