@@ -63,6 +63,7 @@ class AngoraLLVMPass : public ModulePass {
6363 u32 CidCounter;
6464 unsigned long int RandSeed = 1 ;
6565 bool is_bc;
66+ unsigned int inst_ratio = 100 ;
6667
6768 // Const Variables
6869 DenseSet<u32 > UniqCidSet;
@@ -117,6 +118,7 @@ class AngoraLLVMPass : public ModulePass {
117118 bool runOnModule (Module &M) override ;
118119 u32 getInstructionId (Instruction *Inst);
119120 u32 getRandomBasicBlockId ();
121+ bool skipBasicBlock ();
120122 u32 getRandomNum ();
121123 void setRandomNumSeed (u32 seed);
122124 u32 getRandomContextId ();
@@ -145,6 +147,8 @@ char AngoraLLVMPass::ID = 0;
145147
146148u32 AngoraLLVMPass::getRandomBasicBlockId () { return random () % MAP_SIZE; }
147149
150+ bool AngoraLLVMPass::skipBasicBlock () { return (random () % 100 ) >= inst_ratio; }
151+
148152// http://pubs.opengroup.org/onlinepubs/009695399/functions/rand.html
149153u32 AngoraLLVMPass::getRandomNum () {
150154 RandSeed = RandSeed * 1103515245 + 12345 ;
@@ -222,6 +226,14 @@ void AngoraLLVMPass::initVariables(Module &M) {
222226 errs () << " Input is LLVM bitcode\n " ;
223227 }
224228
229+ char * inst_ratio_str = getenv (" ANGORA_INST_RATIO" );
230+ if (inst_ratio_str) {
231+ if (sscanf (inst_ratio_str, " %u" , &inst_ratio) != 1 || !inst_ratio ||
232+ inst_ratio > 100 )
233+ FATAL (" Bad value of ANGORA_INST_RATIO (must be between 1 and 100)" );
234+ }
235+ errs () << " inst_ratio: " << inst_ratio << " \n " ;
236+
225237 // set seed
226238 srandom (ModId);
227239 setRandomNumSeed (ModId);
@@ -339,8 +351,8 @@ void AngoraLLVMPass::initVariables(Module &M) {
339351 char * custom_fn_ctx = getenv (CUSTOM_FN_CTX);
340352 if (custom_fn_ctx) {
341353 num_fn_ctx = atoi (custom_fn_ctx);
342- if (num_fn_ctx < 0 || num_fn_ctx > 32 ) {
343- errs () << " custom context should be: >= 0 && <= 32 \n " ;
354+ if (num_fn_ctx < 0 || num_fn_ctx >= 32 ) {
355+ errs () << " custom context should be: >= 0 && < 32 \n " ;
344356 exit (1 );
345357 }
346358 }
@@ -365,9 +377,9 @@ void AngoraLLVMPass::initVariables(Module &M) {
365377// Coverage statistics: AFL's Branch count
366378// Angora enable function-call context.
367379void AngoraLLVMPass::countEdge (Module &M, BasicBlock &BB) {
368- if (!FastMode)
380+ if (!FastMode || skipBasicBlock () )
369381 return ;
370-
382+
371383 // LLVMContext &C = M.getContext();
372384 unsigned int cur_loc = getRandomBasicBlockId ();
373385 ConstantInt *CurLoc = ConstantInt::get (Int32Ty, cur_loc);
@@ -394,13 +406,25 @@ void AngoraLLVMPass::countEdge(Module &M, BasicBlock &BB) {
394406 LoadInst *Counter = IRB.CreateLoad (MapPtrIdx);
395407 setInsNonSan (Counter);
396408
397- // Avoid overflow
398- Value *CmpOF = IRB.CreateICmpNE (Counter, ConstantInt::get (Int8Ty, -1 ));
399- setValueNonSan (CmpOF);
400-
401- Value *IncVal = IRB.CreateZExt (CmpOF, Int8Ty);
409+ // Implementation of saturating counter.
410+ // Value *CmpOF = IRB.CreateICmpNE(Counter, ConstantInt::get(Int8Ty, -1));
411+ // setValueNonSan(CmpOF);
412+ // Value *IncVal = IRB.CreateZExt(CmpOF, Int8Ty);
413+ // setValueNonSan(IncVal);
414+ // Value *IncRet = IRB.CreateAdd(Counter, IncVal);
415+ // setValueNonSan(IncRet);
416+
417+ // Implementation of Never-zero counter
418+ // The idea is from Marc and Heiko in AFLPlusPlus
419+ // Reference: : https://github.com/vanhauser-thc/AFLplusplus/blob/master/llvm_mode/README.neverzero and https://github.com/vanhauser-thc/AFLplusplus/issues/10
420+
421+ Value *IncRet = IRB.CreateAdd (Counter, ConstantInt::get (Int8Ty, 1 ));
422+ setValueNonSan (IncRet);
423+ Value *IsZero = IRB.CreateICmpEQ (IncRet, ConstantInt::get (Int8Ty, 0 ));
424+ setValueNonSan (IsZero);
425+ Value *IncVal = IRB.CreateZExt (IsZero, Int8Ty);
402426 setValueNonSan (IncVal);
403- Value * IncRet = IRB.CreateAdd (Counter , IncVal);
427+ IncRet = IRB.CreateAdd (IncRet , IncVal);
404428 setValueNonSan (IncRet);
405429
406430 // Store Back Map[idx]
@@ -607,10 +631,12 @@ void AngoraLLVMPass::processCmp(Instruction *Cond, Constant *Cid,
607631 OpArg[1] = castArgType(IRB, OpArg[1]);
608632 Value *CondExt = IRB.CreateZExt(Cond, Int32Ty);
609633 setValueNonSan(CondExt);
634+ LoadInst *CurCtx = IRB.CreateLoad(AngoraContext);
635+ setInsNonSan(CurCtx);
610636 CallInst *ProxyCall =
611- IRB.CreateCall(TraceCmp, {CondExt, Cid, OpArg[0], OpArg[1]});
637+ IRB.CreateCall(TraceCmp, {CondExt, Cid, CurCtx, OpArg[0], OpArg[1]});
612638 setInsNonSan(ProxyCall);
613- */
639+ */
614640 LoadInst *CurCid = IRB.CreateLoad (AngoraCondId);
615641 setInsNonSan (CurCid);
616642 Value *CmpEq = IRB.CreateICmpEQ (Cid, CurCid);
@@ -841,7 +867,7 @@ bool AngoraLLVMPass::runOnModule(Module &M) {
841867 return true ;
842868
843869 for (auto &F : M) {
844- if (F.isDeclaration ())
870+ if (F.isDeclaration () || F. getName (). startswith ( StringRef ( " asan.module " )) )
845871 continue ;
846872
847873 addFnWrap (F);
0 commit comments