Merge pull request #1107 from devnexen/llvm_pass_update

LLVM passes making slightly more C++
This commit is contained in:
van Hauser
2021-10-09 18:50:35 +02:00
committed by GitHub
3 changed files with 24 additions and 29 deletions

View File

@ -250,7 +250,7 @@ class ModuleSanitizerCoverage {
Module * Mo = NULL; Module * Mo = NULL;
GlobalVariable * AFLMapPtr = NULL; GlobalVariable * AFLMapPtr = NULL;
Value * MapPtrFixed = NULL; Value * MapPtrFixed = NULL;
FILE * documentFile = NULL; std::ofstream dFile;
size_t found = 0; size_t found = 0;
// afl++ END // afl++ END
@ -446,7 +446,8 @@ bool ModuleSanitizerCoverage::instrumentModule(
if ((ptr = getenv("AFL_LLVM_DOCUMENT_IDS")) != NULL) { if ((ptr = getenv("AFL_LLVM_DOCUMENT_IDS")) != NULL) {
if ((documentFile = fopen(ptr, "a")) == NULL) dFile.open(ptr, std::ofstream::out | std::ofstream::app);
if (dFile.is_open())
WARNF("Cannot access document file %s", ptr); WARNF("Cannot access document file %s", ptr);
} }
@ -1003,12 +1004,7 @@ bool ModuleSanitizerCoverage::instrumentModule(
instrumentFunction(F, DTCallback, PDTCallback); instrumentFunction(F, DTCallback, PDTCallback);
// afl++ START // afl++ START
if (documentFile) { if (dFile.is_open()) dFile.close();
fclose(documentFile);
documentFile = NULL;
}
if (!getenv("AFL_LLVM_LTO_DONTWRITEID") || dictionary.size() || map_addr) { if (!getenv("AFL_LLVM_LTO_DONTWRITEID") || dictionary.size() || map_addr) {
@ -1509,12 +1505,11 @@ void ModuleSanitizerCoverage::InjectCoverageAtBlock(Function &F, BasicBlock &BB,
// afl++ START // afl++ START
++afl_global_id; ++afl_global_id;
if (documentFile) { if (dFile.is_open()) {
unsigned long long int moduleID = unsigned long long int moduleID =
(((unsigned long long int)(rand() & 0xffffffff)) << 32) | getpid(); (((unsigned long long int)(rand() & 0xffffffff)) << 32) | getpid();
fprintf(documentFile, "ModuleID=%llu Function=%s edgeID=%u\n", moduleID, dFile << "ModuleID=" << moduleID << " Function=" << F.getName().str() << " edgeID=" << afl_global_id << "\n";
F.getName().str().c_str(), afl_global_id);
} }

View File

@ -65,7 +65,8 @@ using namespace llvm;
namespace { namespace {
class AFLdict2filePass : public ModulePass { class AFLdict2filePass : public ModulePass {
std::ofstream of;
void dict2file(u8 *, u32);
public: public:
static char ID; static char ID;
@ -81,7 +82,7 @@ class AFLdict2filePass : public ModulePass {
} // namespace } // namespace
void dict2file(int fd, u8 *mem, u32 len) { void AFLdict2filePass::dict2file(u8 *mem, u32 len) {
u32 i, j, binary = 0; u32 i, j, binary = 0;
char line[MAX_AUTO_EXTRA * 8], tmp[8]; char line[MAX_AUTO_EXTRA * 8], tmp[8];
@ -113,9 +114,8 @@ void dict2file(int fd, u8 *mem, u32 len) {
line[j] = 0; line[j] = 0;
strcat(line, "\"\n"); strcat(line, "\"\n");
if (write(fd, line, strlen(line)) <= 0) of << line;
PFATAL("Could not write to dictionary file"); of.flush();
fsync(fd);
if (!be_quiet) fprintf(stderr, "Found dictionary token: %s", line); if (!be_quiet) fprintf(stderr, "Found dictionary token: %s", line);
@ -125,7 +125,7 @@ bool AFLdict2filePass::runOnModule(Module &M) {
DenseMap<Value *, std::string *> valueMap; DenseMap<Value *, std::string *> valueMap;
char * ptr; char * ptr;
int fd, found = 0; int found = 0;
/* Show a banner */ /* Show a banner */
setvbuf(stdout, NULL, _IONBF, 0); setvbuf(stdout, NULL, _IONBF, 0);
@ -146,7 +146,8 @@ bool AFLdict2filePass::runOnModule(Module &M) {
if (!ptr || *ptr != '/') if (!ptr || *ptr != '/')
FATAL("AFL_LLVM_DICT2FILE is not set to an absolute path: %s", ptr); FATAL("AFL_LLVM_DICT2FILE is not set to an absolute path: %s", ptr);
if ((fd = open(ptr, O_WRONLY | O_APPEND | O_CREAT | O_DSYNC, 0644)) < 0) of.open(ptr, std::ofstream::out | std::ofstream::app);
if (!of.is_open())
PFATAL("Could not open/create %s.", ptr); PFATAL("Could not open/create %s.", ptr);
/* Instrument all the things! */ /* Instrument all the things! */
@ -264,11 +265,11 @@ bool AFLdict2filePass::runOnModule(Module &M) {
} }
dict2file(fd, (u8 *)&val, len); dict2file((u8 *)&val, len);
found++; found++;
if (val2) { if (val2) {
dict2file(fd, (u8 *)&val2, len); dict2file((u8 *)&val2, len);
found++; found++;
} }
@ -630,7 +631,7 @@ bool AFLdict2filePass::runOnModule(Module &M) {
ptr = (char *)thestring.c_str(); ptr = (char *)thestring.c_str();
dict2file(fd, (u8 *)ptr, optLen); dict2file((u8 *)ptr, optLen);
found++; found++;
} }
@ -641,7 +642,7 @@ bool AFLdict2filePass::runOnModule(Module &M) {
} }
close(fd); of.close();
/* Say something nice. */ /* Say something nice. */

View File

@ -108,8 +108,8 @@ bool AFLLTOPass::runOnModule(Module &M) {
// std::vector<CallInst *> calls; // std::vector<CallInst *> calls;
DenseMap<Value *, std::string *> valueMap; DenseMap<Value *, std::string *> valueMap;
std::vector<BasicBlock *> BlockList; std::vector<BasicBlock *> BlockList;
std::ofstream dFile;
char * ptr; char * ptr;
FILE * documentFile = NULL;
size_t found = 0; size_t found = 0;
srand((unsigned int)time(NULL)); srand((unsigned int)time(NULL));
@ -137,7 +137,8 @@ bool AFLLTOPass::runOnModule(Module &M) {
if ((ptr = getenv("AFL_LLVM_DOCUMENT_IDS")) != NULL) { if ((ptr = getenv("AFL_LLVM_DOCUMENT_IDS")) != NULL) {
if ((documentFile = fopen(ptr, "a")) == NULL) dFile.open(ptr, std::ofstream::out | std::ofstream::app);
if (!dFile.is_open())
WARNF("Cannot access document file %s", ptr); WARNF("Cannot access document file %s", ptr);
} }
@ -845,10 +846,9 @@ bool AFLLTOPass::runOnModule(Module &M) {
} }
if (documentFile) { if (dFile.is_open()) {
fprintf(documentFile, "ModuleID=%llu Function=%s edgeID=%u\n", dFile << "ModuleID=" << moduleID << " Function=" << F.getName().str() << " edgeID=" << afl_global_id << "\n";
moduleID, F.getName().str().c_str(), afl_global_id);
} }
@ -920,8 +920,7 @@ bool AFLLTOPass::runOnModule(Module &M) {
} }
if (documentFile) fclose(documentFile); if (dFile.is_open()) dFile.close();
documentFile = NULL;
// save highest location ID to global variable // save highest location ID to global variable
// do this after each function to fail faster // do this after each function to fail faster