17
17
#include " llvm/ADT/SmallVector.h"
18
18
#include " llvm/ADT/StringExtras.h"
19
19
#include " llvm/ADT/StringRef.h"
20
+ #include " llvm/Analysis/DomTreeUpdater.h"
20
21
#include " llvm/Analysis/GlobalsModRef.h"
21
22
#include " llvm/Analysis/PostDominators.h"
22
23
#include " llvm/Analysis/StackSafetyAnalysis.h"
@@ -303,17 +304,20 @@ class HWAddressSanitizer {
303
304
Value *memToShadow (Value *Shadow, IRBuilder<> &IRB);
304
305
305
306
int64_t getAccessInfo (bool IsWrite, unsigned AccessSizeIndex);
306
- ShadowTagCheckInfo insertShadowTagCheck (Value *Ptr,
307
- Instruction *InsertBefore );
307
+ ShadowTagCheckInfo insertShadowTagCheck (Value *Ptr, Instruction *InsertBefore,
308
+ DomTreeUpdater &DTU, LoopInfo &LI );
308
309
void instrumentMemAccessOutline (Value *Ptr, bool IsWrite,
309
310
unsigned AccessSizeIndex,
310
- Instruction *InsertBefore);
311
+ Instruction *InsertBefore,
312
+ DomTreeUpdater &DTU, LoopInfo &LI);
311
313
void instrumentMemAccessInline (Value *Ptr, bool IsWrite,
312
314
unsigned AccessSizeIndex,
313
- Instruction *InsertBefore);
315
+ Instruction *InsertBefore, DomTreeUpdater &DTU,
316
+ LoopInfo &LI);
314
317
bool ignoreMemIntrinsic (MemIntrinsic *MI);
315
318
void instrumentMemIntrinsic (MemIntrinsic *MI);
316
- bool instrumentMemAccess (InterestingMemoryOperand &O);
319
+ bool instrumentMemAccess (InterestingMemoryOperand &O, DomTreeUpdater &DTU,
320
+ LoopInfo &LI);
317
321
bool ignoreAccess (Instruction *Inst, Value *Ptr);
318
322
void getInterestingMemoryOperands (
319
323
Instruction *I, SmallVectorImpl<InterestingMemoryOperand> &Interesting);
@@ -434,6 +438,12 @@ PreservedAnalyses HWAddressSanitizerPass::run(Module &M,
434
438
HWASan.sanitizeFunction (F, FAM);
435
439
436
440
PreservedAnalyses PA = PreservedAnalyses::none ();
441
+ // DominatorTreeAnalysis, PostDominatorTreeAnalysis, and LoopAnalysis
442
+ // are incrementally updated throughout this pass whenever
443
+ // SplitBlockAndInsertIfThen is called.
444
+ PA.preserve <DominatorTreeAnalysis>();
445
+ PA.preserve <PostDominatorTreeAnalysis>();
446
+ PA.preserve <LoopAnalysis>();
437
447
// GlobalsAA is considered stateless and does not get invalidated unless
438
448
// explicitly invalidated; PreservedAnalyses::none() is not enough. Sanitizers
439
449
// make changes that require GlobalsAA to be invalidated.
@@ -861,8 +871,8 @@ int64_t HWAddressSanitizer::getAccessInfo(bool IsWrite,
861
871
}
862
872
863
873
HWAddressSanitizer::ShadowTagCheckInfo
864
- HWAddressSanitizer::insertShadowTagCheck (Value *Ptr,
865
- Instruction *InsertBefore ) {
874
+ HWAddressSanitizer::insertShadowTagCheck (Value *Ptr, Instruction *InsertBefore,
875
+ DomTreeUpdater &DTU, LoopInfo &LI ) {
866
876
ShadowTagCheckInfo R;
867
877
868
878
IRBuilder<> IRB (InsertBefore);
@@ -881,21 +891,24 @@ HWAddressSanitizer::insertShadowTagCheck(Value *Ptr,
881
891
TagMismatch = IRB.CreateAnd (TagMismatch, TagNotIgnored);
882
892
}
883
893
884
- R.TagMismatchTerm =
885
- SplitBlockAndInsertIfThen ( TagMismatch, InsertBefore, false ,
886
- MDBuilder (*C).createBranchWeights (1 , 100000 ));
894
+ R.TagMismatchTerm = SplitBlockAndInsertIfThen (
895
+ TagMismatch, InsertBefore, false ,
896
+ MDBuilder (*C).createBranchWeights (1 , 100000 ), &DTU, &LI );
887
897
888
898
return R;
889
899
}
890
900
891
901
void HWAddressSanitizer::instrumentMemAccessOutline (Value *Ptr, bool IsWrite,
892
902
unsigned AccessSizeIndex,
893
- Instruction *InsertBefore) {
903
+ Instruction *InsertBefore,
904
+ DomTreeUpdater &DTU,
905
+ LoopInfo &LI) {
894
906
assert (!UsePageAliases);
895
907
const int64_t AccessInfo = getAccessInfo (IsWrite, AccessSizeIndex);
896
908
897
909
if (InlineFastPath)
898
- InsertBefore = insertShadowTagCheck (Ptr, InsertBefore).TagMismatchTerm ;
910
+ InsertBefore =
911
+ insertShadowTagCheck (Ptr, InsertBefore, DTU, LI).TagMismatchTerm ;
899
912
900
913
IRBuilder<> IRB (InsertBefore);
901
914
Module *M = IRB.GetInsertBlock ()->getParent ()->getParent ();
@@ -909,38 +922,38 @@ void HWAddressSanitizer::instrumentMemAccessOutline(Value *Ptr, bool IsWrite,
909
922
910
923
void HWAddressSanitizer::instrumentMemAccessInline (Value *Ptr, bool IsWrite,
911
924
unsigned AccessSizeIndex,
912
- Instruction *InsertBefore) {
925
+ Instruction *InsertBefore,
926
+ DomTreeUpdater &DTU,
927
+ LoopInfo &LI) {
913
928
assert (!UsePageAliases);
914
929
const int64_t AccessInfo = getAccessInfo (IsWrite, AccessSizeIndex);
915
930
916
- ShadowTagCheckInfo TCI = insertShadowTagCheck (Ptr, InsertBefore);
931
+ ShadowTagCheckInfo TCI = insertShadowTagCheck (Ptr, InsertBefore, DTU, LI );
917
932
918
933
IRBuilder<> IRB (TCI.TagMismatchTerm );
919
934
Value *OutOfShortGranuleTagRange =
920
935
IRB.CreateICmpUGT (TCI.MemTag , ConstantInt::get (Int8Ty, 15 ));
921
936
Instruction *CheckFailTerm = SplitBlockAndInsertIfThen (
922
937
OutOfShortGranuleTagRange, TCI.TagMismatchTerm , !Recover,
923
- MDBuilder (*C).createBranchWeights (1 , 100000 ));
938
+ MDBuilder (*C).createBranchWeights (1 , 100000 ), &DTU, &LI );
924
939
925
940
IRB.SetInsertPoint (TCI.TagMismatchTerm );
926
941
Value *PtrLowBits = IRB.CreateTrunc (IRB.CreateAnd (TCI.PtrLong , 15 ), Int8Ty);
927
942
PtrLowBits = IRB.CreateAdd (
928
943
PtrLowBits, ConstantInt::get (Int8Ty, (1 << AccessSizeIndex) - 1 ));
929
944
Value *PtrLowBitsOOB = IRB.CreateICmpUGE (PtrLowBits, TCI.MemTag );
930
945
SplitBlockAndInsertIfThen (PtrLowBitsOOB, TCI.TagMismatchTerm , false ,
931
- MDBuilder (*C).createBranchWeights (1 , 100000 ),
932
- (DomTreeUpdater *)nullptr , nullptr ,
933
- CheckFailTerm->getParent ());
946
+ MDBuilder (*C).createBranchWeights (1 , 100000 ), &DTU,
947
+ &LI, CheckFailTerm->getParent ());
934
948
935
949
IRB.SetInsertPoint (TCI.TagMismatchTerm );
936
950
Value *InlineTagAddr = IRB.CreateOr (TCI.AddrLong , 15 );
937
951
InlineTagAddr = IRB.CreateIntToPtr (InlineTagAddr, Int8PtrTy);
938
952
Value *InlineTag = IRB.CreateLoad (Int8Ty, InlineTagAddr);
939
953
Value *InlineTagMismatch = IRB.CreateICmpNE (TCI.PtrTag , InlineTag);
940
954
SplitBlockAndInsertIfThen (InlineTagMismatch, TCI.TagMismatchTerm , false ,
941
- MDBuilder (*C).createBranchWeights (1 , 100000 ),
942
- (DomTreeUpdater *)nullptr , nullptr ,
943
- CheckFailTerm->getParent ());
955
+ MDBuilder (*C).createBranchWeights (1 , 100000 ), &DTU,
956
+ &LI, CheckFailTerm->getParent ());
944
957
945
958
IRB.SetInsertPoint (CheckFailTerm);
946
959
InlineAsm *Asm;
@@ -1015,7 +1028,9 @@ void HWAddressSanitizer::instrumentMemIntrinsic(MemIntrinsic *MI) {
1015
1028
MI->eraseFromParent ();
1016
1029
}
1017
1030
1018
- bool HWAddressSanitizer::instrumentMemAccess (InterestingMemoryOperand &O) {
1031
+ bool HWAddressSanitizer::instrumentMemAccess (InterestingMemoryOperand &O,
1032
+ DomTreeUpdater &DTU,
1033
+ LoopInfo &LI) {
1019
1034
Value *Addr = O.getPtr ();
1020
1035
1021
1036
LLVM_DEBUG (dbgs () << " Instrumenting: " << O.getInsn () << " \n " );
@@ -1036,9 +1051,11 @@ bool HWAddressSanitizer::instrumentMemAccess(InterestingMemoryOperand &O) {
1036
1051
IRB.CreateCall (HwasanMemoryAccessCallback[O.IsWrite ][AccessSizeIndex],
1037
1052
Args);
1038
1053
} else if (OutlinedChecks) {
1039
- instrumentMemAccessOutline (Addr, O.IsWrite , AccessSizeIndex, O.getInsn ());
1054
+ instrumentMemAccessOutline (Addr, O.IsWrite , AccessSizeIndex, O.getInsn (),
1055
+ DTU, LI);
1040
1056
} else {
1041
- instrumentMemAccessInline (Addr, O.IsWrite , AccessSizeIndex, O.getInsn ());
1057
+ instrumentMemAccessInline (Addr, O.IsWrite , AccessSizeIndex, O.getInsn (),
1058
+ DTU, LI);
1042
1059
}
1043
1060
} else {
1044
1061
SmallVector<Value *, 3 > Args{
@@ -1542,8 +1559,13 @@ void HWAddressSanitizer::sanitizeFunction(Function &F,
1542
1559
}
1543
1560
}
1544
1561
1562
+ DominatorTree *DT = FAM.getCachedResult <DominatorTreeAnalysis>(F);
1563
+ PostDominatorTree *PDT = FAM.getCachedResult <PostDominatorTreeAnalysis>(F);
1564
+ LoopInfo *LI = FAM.getCachedResult <LoopAnalysis>(F);
1565
+ DomTreeUpdater DTU (DT, PDT, DomTreeUpdater::UpdateStrategy::Lazy);
1545
1566
for (auto &Operand : OperandsToInstrument)
1546
- instrumentMemAccess (Operand);
1567
+ instrumentMemAccess (Operand, DTU, *LI);
1568
+ DTU.flush ();
1547
1569
1548
1570
if (ClInstrumentMemIntrinsics && !IntrinToInstrument.empty ()) {
1549
1571
for (auto *Inst : IntrinToInstrument)
0 commit comments