fix typos and format

This commit is contained in:
Andrea Fioraldi
2021-03-05 15:27:10 +01:00
parent af9aeb89d4
commit c429021de1
2 changed files with 73 additions and 44 deletions

View File

@ -217,19 +217,25 @@ bool AFLCoverage::runOnModule(Module &M) {
VectorType *PrevCallerTy = NULL; VectorType *PrevCallerTy = NULL;
if (ctx_k_str) if (ctx_k_str)
if (sscanf(ctx_k_str, "%u", &ctx_k) != 1 || ctx_k < 2 || if (sscanf(ctx_k_str, "%u", &ctx_k) != 1 || ctx_k < 2 || ctx_k > CTX_MAX_K)
ctx_k > CTX_MAX_K) FATAL("Bad value of AFL_CTX_K (must be between 2 and CTX_MAX_K (%u))",
FATAL("Bad value of AFL_CTX_K (must be between 2 and CTX_MAX_K (%u))", CTX_MAX_K); CTX_MAX_K);
if (ctx_k == 1) { if (ctx_k == 1) {
ctx_k = 0; ctx_k = 0;
instrument_ctx = true; instrument_ctx = true;
caller_str = ctx_k_str; // Enable CALLER instead caller_str = ctx_k_str; // Enable CALLER instead
} }
if (ctx_k) { if (ctx_k) {
PrevCallerSize = ctx_k; PrevCallerSize = ctx_k;
instrument_ctx = true; instrument_ctx = true;
} }
#else #else
if (ngram_size_str) if (ngram_size_str)
#ifndef LLVM_VERSION_PATCH #ifndef LLVM_VERSION_PATCH
@ -274,8 +280,8 @@ bool AFLCoverage::runOnModule(Module &M) {
if (ctx_k) if (ctx_k)
PrevCallerTy = VectorType::get(IntLocTy, PrevCallerVecSize PrevCallerTy = VectorType::get(IntLocTy, PrevCallerVecSize
#if LLVM_VERSION_MAJOR >= 12 #if LLVM_VERSION_MAJOR >= 12
, ,
false false
#endif #endif
); );
#endif #endif
@ -340,12 +346,13 @@ bool AFLCoverage::runOnModule(Module &M) {
else else
#endif #endif
#if defined(__ANDROID__) || defined(__HAIKU__) #if defined(__ANDROID__) || defined(__HAIKU__)
AFLPrevCaller = new GlobalVariable( AFLPrevCaller =
M, Int32Ty, false, GlobalValue::ExternalLinkage, 0, "__afl_prev_caller"); new GlobalVariable(M, Int32Ty, false, GlobalValue::ExternalLinkage, 0,
"__afl_prev_caller");
#else #else
AFLPrevCaller = new GlobalVariable( AFLPrevCaller = new GlobalVariable(
M, Int32Ty, false, GlobalValue::ExternalLinkage, 0, "__afl_prev_caller", 0, M, Int32Ty, false, GlobalValue::ExternalLinkage, 0, "__afl_prev_caller",
GlobalVariable::GeneralDynamicTLSModel, 0, false); 0, GlobalVariable::GeneralDynamicTLSModel, 0, false);
#endif #endif
#ifdef AFL_HAVE_VECTOR_INTRINSICS #ifdef AFL_HAVE_VECTOR_INTRINSICS
@ -363,10 +370,11 @@ bool AFLCoverage::runOnModule(Module &M) {
Constant *PrevLocShuffleMask = ConstantVector::get(PrevLocShuffle); Constant *PrevLocShuffleMask = ConstantVector::get(PrevLocShuffle);
Constant *PrevCallerShuffleMask = NULL; Constant * PrevCallerShuffleMask = NULL;
SmallVector<Constant *, 32> PrevCallerShuffle = {UndefValue::get(Int32Ty)}; SmallVector<Constant *, 32> PrevCallerShuffle = {UndefValue::get(Int32Ty)};
if (ctx_k) { if (ctx_k) {
for (unsigned I = 0; I < PrevCallerSize - 1; ++I) for (unsigned I = 0; I < PrevCallerSize - 1; ++I)
PrevCallerShuffle.push_back(ConstantInt::get(Int32Ty, I)); PrevCallerShuffle.push_back(ConstantInt::get(Int32Ty, I));
@ -374,15 +382,17 @@ bool AFLCoverage::runOnModule(Module &M) {
PrevCallerShuffle.push_back(ConstantInt::get(Int32Ty, PrevCallerSize)); PrevCallerShuffle.push_back(ConstantInt::get(Int32Ty, PrevCallerSize));
PrevCallerShuffleMask = ConstantVector::get(PrevCallerShuffle); PrevCallerShuffleMask = ConstantVector::get(PrevCallerShuffle);
} }
#endif #endif
// other constants we need // other constants we need
ConstantInt *Zero = ConstantInt::get(Int8Ty, 0); ConstantInt *Zero = ConstantInt::get(Int8Ty, 0);
ConstantInt *One = ConstantInt::get(Int8Ty, 1); ConstantInt *One = ConstantInt::get(Int8Ty, 1);
Value *PrevCtx = NULL; // CTX sensitive coverage Value * PrevCtx = NULL; // CTX sensitive coverage
LoadInst *PrevCaller = NULL; // K-CTX coverage LoadInst *PrevCaller = NULL; // K-CTX coverage
/* Instrument all the things! */ /* Instrument all the things! */
@ -410,16 +420,25 @@ bool AFLCoverage::runOnModule(Module &M) {
#ifdef AFL_HAVE_VECTOR_INTRINSICS #ifdef AFL_HAVE_VECTOR_INTRINSICS
if (ctx_k) { if (ctx_k) {
PrevCaller = IRB.CreateLoad(AFLPrevCaller); PrevCaller = IRB.CreateLoad(AFLPrevCaller);
PrevCaller->setMetadata(M.getMDKindID("nosanitize"), MDNode::get(C, None)); PrevCaller->setMetadata(M.getMDKindID("nosanitize"),
PrevCtx = IRB.CreateZExt(IRB.CreateXorReduce(PrevCaller), IRB.getInt32Ty()); MDNode::get(C, None));
PrevCtx =
IRB.CreateZExt(IRB.CreateXorReduce(PrevCaller), IRB.getInt32Ty());
} else } else
#endif #endif
{ {
// load the context ID of the previous function and write to to a local variable on the stack
LoadInst* PrevCtxLoad = IRB.CreateLoad(AFLContext); // load the context ID of the previous function and write to to a
PrevCtxLoad->setMetadata(M.getMDKindID("nosanitize"), MDNode::get(C, None)); // local variable on the stack
LoadInst *PrevCtxLoad = IRB.CreateLoad(AFLContext);
PrevCtxLoad->setMetadata(M.getMDKindID("nosanitize"),
MDNode::get(C, None));
PrevCtx = PrevCtxLoad; PrevCtx = PrevCtxLoad;
} }
// 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
@ -454,19 +473,28 @@ bool AFLCoverage::runOnModule(Module &M) {
Value *NewCtx = ConstantInt::get(Int32Ty, AFL_R(map_size)); Value *NewCtx = ConstantInt::get(Int32Ty, AFL_R(map_size));
#ifdef AFL_HAVE_VECTOR_INTRINSICS #ifdef AFL_HAVE_VECTOR_INTRINSICS
if (ctx_k) { if (ctx_k) {
Value *ShuffledPrevCaller = IRB.CreateShuffleVector(
PrevCaller, UndefValue::get(PrevCallerTy), PrevCallerShuffleMask);
Value *UpdatedPrevCaller = IRB.CreateInsertElement(ShuffledPrevCaller, NewCtx, (uint64_t)0);
StoreInst * Store = IRB.CreateStore(UpdatedPrevCaller, AFLPrevCaller); Value *ShuffledPrevCaller = IRB.CreateShuffleVector(
Store->setMetadata(M.getMDKindID("nosanitize"), MDNode::get(C, None)); PrevCaller, UndefValue::get(PrevCallerTy),
PrevCallerShuffleMask);
Value *UpdatedPrevCaller = IRB.CreateInsertElement(
ShuffledPrevCaller, NewCtx, (uint64_t)0);
StoreInst *Store =
IRB.CreateStore(UpdatedPrevCaller, AFLPrevCaller);
Store->setMetadata(M.getMDKindID("nosanitize"),
MDNode::get(C, None));
} else } else
#endif #endif
{ {
if (ctx_str) NewCtx = IRB.CreateXor(PrevCtx, NewCtx); if (ctx_str) NewCtx = IRB.CreateXor(PrevCtx, NewCtx);
StoreInst *StoreCtx = IRB.CreateStore(NewCtx, AFLContext); StoreInst *StoreCtx = IRB.CreateStore(NewCtx, AFLContext);
StoreCtx->setMetadata(M.getMDKindID("nosanitize"), StoreCtx->setMetadata(M.getMDKindID("nosanitize"),
MDNode::get(C, None)); MDNode::get(C, None));
} }
} }
@ -528,15 +556,16 @@ bool AFLCoverage::runOnModule(Module &M) {
IRBuilder<> Post_IRB(Inst); IRBuilder<> Post_IRB(Inst);
StoreInst * RestoreCtx; StoreInst *RestoreCtx;
#ifdef AFL_HAVE_VECTOR_INTRINSICS #ifdef AFL_HAVE_VECTOR_INTRINSICS
if (ctx_k) if (ctx_k)
RestoreCtx = IRB.CreateStore(PrevCaller, AFLPrevCaller); RestoreCtx = IRB.CreateStore(PrevCaller, AFLPrevCaller);
else else
#endif #endif
RestoreCtx = Post_IRB.CreateStore(PrevCtx, AFLContext); RestoreCtx = Post_IRB.CreateStore(PrevCtx, AFLContext);
RestoreCtx->setMetadata(M.getMDKindID("nosanitize"), RestoreCtx->setMetadata(M.getMDKindID("nosanitize"),
MDNode::get(C, None)); MDNode::get(C, None));
} }
} }
@ -668,13 +697,13 @@ bool AFLCoverage::runOnModule(Module &M) {
IRBuilder<> Post_IRB(Inst); IRBuilder<> Post_IRB(Inst);
StoreInst * RestoreCtx; StoreInst *RestoreCtx;
#ifdef AFL_HAVE_VECTOR_INTRINSICS #ifdef AFL_HAVE_VECTOR_INTRINSICS
if (ctx_k) if (ctx_k)
RestoreCtx = IRB.CreateStore(PrevCaller, AFLPrevCaller); RestoreCtx = IRB.CreateStore(PrevCaller, AFLPrevCaller);
else else
#endif #endif
RestoreCtx = Post_IRB.CreateStore(PrevCtx, AFLContext); RestoreCtx = Post_IRB.CreateStore(PrevCtx, AFLContext);
RestoreCtx->setMetadata(M.getMDKindID("nosanitize"), RestoreCtx->setMetadata(M.getMDKindID("nosanitize"),
MDNode::get(C, None)); MDNode::get(C, None));

View File

@ -1283,7 +1283,7 @@ int main(int argc, char **argv, char **envp) {
ngram_size = atoi(getenv("AFL_LLVM_NGRAM_SIZE")); ngram_size = atoi(getenv("AFL_LLVM_NGRAM_SIZE"));
if (ngram_size < 2 || ngram_size > NGRAM_SIZE_MAX) if (ngram_size < 2 || ngram_size > NGRAM_SIZE_MAX)
FATAL( FATAL(
"K-CTX instrumentation mode must be between 2 and NGRAM_SIZE_MAX " "NGRAM instrumentation mode must be between 2 and NGRAM_SIZE_MAX "
"(%u)", "(%u)",
NGRAM_SIZE_MAX); NGRAM_SIZE_MAX);
@ -1294,8 +1294,8 @@ int main(int argc, char **argv, char **envp) {
instrument_opt_mode |= INSTRUMENT_OPT_CTX_K; instrument_opt_mode |= INSTRUMENT_OPT_CTX_K;
ctx_k = atoi(getenv("AFL_LLVM_CTX_K")); ctx_k = atoi(getenv("AFL_LLVM_CTX_K"));
if (ctx_k < 1 || ctx_k > CTX_MAX_K) if (ctx_k < 1 || ctx_k > CTX_MAX_K)
FATAL( FATAL("K-CTX instrumentation mode must be between 1 and CTX_MAX_K (%u)",
"NGRAM instrumentation mode must be between 1 and CTX_MAX_K (%u)", CTX_MAX_K); CTX_MAX_K);
} }
@ -1412,7 +1412,8 @@ int main(int argc, char **argv, char **envp) {
ctx_k = atoi(ptr3); ctx_k = atoi(ptr3);
if (ctx_k < 1 || ctx_k > CTX_MAX_K) if (ctx_k < 1 || ctx_k > CTX_MAX_K)
FATAL( FATAL(
"K-CTX instrumentation option must be between 1 and CTX_MAX_K (%u)", "K-CTX instrumentation option must be between 1 and CTX_MAX_K "
"(%u)",
CTX_MAX_K); CTX_MAX_K);
instrument_opt_mode |= (INSTRUMENT_OPT_CTX_K); instrument_opt_mode |= (INSTRUMENT_OPT_CTX_K);
u8 *ptr4 = alloc_printf("%u", ctx_k); u8 *ptr4 = alloc_printf("%u", ctx_k);
@ -1855,8 +1856,7 @@ int main(int argc, char **argv, char **envp) {
(instrument_opt_mode & INSTRUMENT_OPT_CTX) ? " + CTX" : "", (instrument_opt_mode & INSTRUMENT_OPT_CTX) ? " + CTX" : "",
(instrument_opt_mode & INSTRUMENT_OPT_CALLER) ? " + CALLER" : "", (instrument_opt_mode & INSTRUMENT_OPT_CALLER) ? " + CALLER" : "",
(instrument_opt_mode & INSTRUMENT_OPT_NGRAM) ? ptr2 : "", (instrument_opt_mode & INSTRUMENT_OPT_NGRAM) ? ptr2 : "",
(instrument_opt_mode & INSTRUMENT_OPT_CTX_K) ? ptr3 : "" (instrument_opt_mode & INSTRUMENT_OPT_CTX_K) ? ptr3 : "");
);
ck_free(ptr2); ck_free(ptr2);
ck_free(ptr3); ck_free(ptr3);