llvm_mode ctx: move function terminator instrumentation before skipping blocks

This commit is contained in:
van Hauser
2020-04-08 17:57:11 +02:00
parent ae05f5c4b0
commit 698caed9c0
2 changed files with 19 additions and 18 deletions

View File

@ -317,6 +317,7 @@ bool AFLCoverage::runOnModule(Module &M) {
for (auto &F : M) { for (auto &F : M) {
int has_calls = 0;
if (debug) if (debug)
fprintf(stderr, "FUNCTION: %s (%zu)\n", F.getName().str().c_str(), fprintf(stderr, "FUNCTION: %s (%zu)\n", F.getName().str().c_str(),
F.size()); F.size());
@ -336,7 +337,7 @@ bool AFLCoverage::runOnModule(Module &M) {
// does the function have calls? and is any of the calls larger than one // does the function have calls? and is any of the calls larger than one
// basic block? // basic block?
int has_calls = 0; has_calls = 0;
for (auto &BB : F) { for (auto &BB : F) {
if (has_calls) break; if (has_calls) break;
@ -487,6 +488,22 @@ bool AFLCoverage::runOnModule(Module &M) {
} }
// in CTX mode we have to restore the original context for the caller -
// she might be calling other functions which need the correct CTX
if (ctx_str && has_calls) {
Instruction *Inst = BB.getTerminator();
if (isa<ReturnInst>(Inst) || isa<ResumeInst>(Inst)) {
IRBuilder<> Post_IRB(Inst);
StoreInst * RestoreCtx = Post_IRB.CreateStore(PrevCtx, AFLContext);
RestoreCtx->setMetadata(M.getMDKindID("nosanitize"),
MDNode::get(C, None));
}
}
if (AFL_R(100) >= inst_ratio) continue; if (AFL_R(100) >= inst_ratio) continue;
/* Make up cur_loc */ /* Make up cur_loc */
@ -682,22 +699,6 @@ bool AFLCoverage::runOnModule(Module &M) {
} }
// in CTX mode we have to restore the original context for the caller -
// she might be calling other functions which need the correct CTX
if (ctx_str) {
Instruction *Inst = BB.getTerminator();
if (isa<ReturnInst>(Inst) || isa<ResumeInst>(Inst)) {
IRBuilder<> Post_IRB(Inst);
StoreInst * RestoreCtx = Post_IRB.CreateStore(PrevCtx, AFLContext);
RestoreCtx->setMetadata(M.getMDKindID("nosanitize"),
MDNode::get(C, None));
}
}
inst_blocks++; inst_blocks++;
} }

View File

@ -78,7 +78,7 @@ __thread u32 __afl_prev_ctx;
__thread u32 __afl_cmp_counter; __thread u32 __afl_cmp_counter;
#endif #endif
struct cmp_map *__afl_cmp_map; struct cmp_map *__afl_cmp_map;
/* Running in persistent mode? */ /* Running in persistent mode? */