@@ -664,8 +664,50 @@ void ModuleSanitizerCoverageAFL::instrumentFunction(
664
664
// InjectTraceForCmp(F, CmpTraceTargets);
665
665
// InjectTraceForSwitch(F, SwitchTraceTargets);
666
666
667
- if (dump_cc) { calcCyclomaticComplexity (&F, LI); }
668
- if (dump_vc) { calcVulnerabilityScore (&F, LI, DT, PDT); }
667
+ unsigned int score = 0 ;
668
+
669
+ if (dump_cc) { score = calcCyclomaticComplexity (&F, LI); }
670
+ if (dump_vc) { score = calcVulnerabilityScore (&F, LI, DT, PDT); }
671
+
672
+ if (score) {
673
+
674
+ BasicBlock::iterator IP = F.getEntryBlock ().getFirstInsertionPt ();
675
+ IRBuilder<> builder (&*IP);
676
+
677
+ // Access the int32 value at u8 offset 1 (unaligned access)
678
+ LoadInst *MapPtr =
679
+ builder.CreateLoad (PointerType::get (Int8Ty, 0 ), AFLMapPtr);
680
+ llvm::Value *CastToInt8Ptr =
681
+ builder.CreateBitCast (MapPtr, llvm::PointerType::get (Int8Ty, 0 ));
682
+ llvm::Value *Int32Ptr = builder.CreateGEP (
683
+ Int8Ty, CastToInt8Ptr, llvm::ConstantInt::get (Int32Ty, 1 ));
684
+ llvm::Value *CastToInt32Ptr =
685
+ builder.CreateBitCast (Int32Ptr, llvm::PointerType::get (Int32Ty, 0 ));
686
+
687
+ // Load the unaligned int32 value
688
+ llvm::LoadInst *Load = builder.CreateLoad (Int32Ty, CastToInt32Ptr);
689
+ Load->setAlignment (llvm::Align (1 ));
690
+
691
+ // Value to add
692
+ llvm::Value *ValueToAdd = llvm::ConstantInt::get (Int32Ty, score);
693
+
694
+ // Perform addition and check for wrap around
695
+ llvm::Value *Add =
696
+ builder.CreateAdd (Load, ValueToAdd, " addValue" , true , true );
697
+
698
+ // Check if addition wrapped (unsigned)
699
+ llvm::Value *DidWrap = builder.CreateICmpULT (Add, Load, " didWrap" );
700
+
701
+ // Select the maximum value if there was a wrap, otherwise use the result
702
+ llvm::Value *MaxInt32 = llvm::ConstantInt::get (Int32Ty, UINT32_MAX);
703
+ llvm::Value *Result =
704
+ builder.CreateSelect (DidWrap, MaxInt32, Add, " selectMaxOrResult" );
705
+
706
+ // Store the result back at the same unaligned offset
707
+ llvm::StoreInst *Store = builder.CreateStore (Result, CastToInt32Ptr);
708
+ Store->setAlignment (llvm::Align (1 ));
709
+
710
+ }
669
711
670
712
}
671
713
0 commit comments