@@ -64,11 +64,9 @@ static cl::opt<bool>
6464
6565STATISTIC (NumInstrumentedAccesses, " Number of instrumented accesses" );
6666
67- static Regex AnonNameRegex (" ^_ZTS.*N[1-9][0-9]*_GLOBAL__N" );
68-
6967namespace {
7068
71- // / TypeSanitizer: instrument the code in module to find type-based aliasing
69+ // / TypeSanitizer: instrument the code in module to find type-based aliasing
7270// / violations.
7371struct TypeSanitizer {
7472 TypeSanitizer (Module &M);
@@ -92,11 +90,9 @@ struct TypeSanitizer {
9290 bool SanitizeFunction,
9391 TypeDescriptorsMapTy &TypeDescriptors,
9492 const DataLayout &DL);
95- bool instrumentMemoryAccess (Instruction *I, MemoryLocation &MLoc,
96- Value *ShadowBase, Value *AppMemMask,
97- bool SanitizeFunction,
98- TypeDescriptorsMapTy &TypeDescriptors,
99- const DataLayout &DL);
93+
94+ // / Memory-related intrinsics/instructions reset the type of the destination
95+ // / memory (including allocas and byval arguments).
10096 bool instrumentMemInst (Value *I, Value *&ShadowBase, Value *&AppMemMask,
10197 const DataLayout &DL);
10298
@@ -150,9 +146,8 @@ void TypeSanitizer::initializeCallbacks(Module &M) {
150146 OrdTy // Flags.
151147 );
152148
153- TysanCtorFunction = cast<Function>(
154- M.getOrInsertFunction (kTysanModuleCtorName , Attr, IRB.getVoidTy ())
155- .getCallee ());
149+ TysanCtorFunction =
150+ M.getOrInsertFunction (kTysanModuleCtorName , Attr, IRB.getVoidTy ());
156151}
157152
158153void TypeSanitizer::instrumentGlobals (Module &M) {
@@ -535,7 +530,6 @@ bool TypeSanitizer::run(Function &F, const TargetLibraryInfo &TLI) {
535530 if (A.hasByValAttr ())
536531 MemTypeResetInsts.push_back (&A);
537532
538-
539533 Module &M = *F.getParent ();
540534 TypeDescriptorsMapTy TypeDescriptors;
541535 TypeNameMapTy TypeNames;
@@ -552,11 +546,22 @@ bool TypeSanitizer::run(Function &F, const TargetLibraryInfo &TLI) {
552546
553547 const DataLayout &DL = F.getParent ()->getDataLayout ();
554548 bool SanitizeFunction = F.hasFnAttribute (Attribute::SanitizeType);
555- Value *ShadowBase = MemoryAccesses.empty () ? nullptr : getShadowBase (F);
556- Value *AppMemMask = MemoryAccesses.empty () ? nullptr : getAppMemMask (F);
557- for (auto &MA : MemoryAccesses)
558- Res |= instrumentMemoryAccess (MA.first , MA.second , ShadowBase, AppMemMask,
559- SanitizeFunction, TypeDescriptors, DL);
549+ bool NeedsInstrumentation =
550+ MemTypeResetInsts.empty () && MemoryAccesses.empty ();
551+ Value *ShadowBase = NeedsInstrumentation ? nullptr : getShadowBase (F);
552+ Value *AppMemMask = NeedsInstrumentation ? nullptr : getAppMemMask (F);
553+ for (const auto &[I, MLoc] : MemoryAccesses) {
554+ IRBuilder<> IRB (I);
555+ assert (MLoc.Size .isPrecise ());
556+ if (instrumentWithShadowUpdate (
557+ IRB, MLoc.AATags .TBAA , const_cast <Value *>(MLoc.Ptr ),
558+ MLoc.Size .getValue (), I->mayReadFromMemory (), I->mayWriteToMemory (),
559+ ShadowBase, AppMemMask, false , SanitizeFunction, TypeDescriptors,
560+ DL)) {
561+ ++NumInstrumentedAccesses;
562+ Res = true ;
563+ }
564+ }
560565
561566 for (auto Inst : MemTypeResetInsts)
562567 Res |= instrumentMemInst (Inst, ShadowBase, AppMemMask, DL);
@@ -712,26 +717,6 @@ bool TypeSanitizer::instrumentWithShadowUpdate(
712717 return true ;
713718}
714719
715- bool TypeSanitizer::instrumentMemoryAccess (
716- Instruction *I, MemoryLocation &MLoc, Value *ShadowBase, Value *AppMemMask,
717- bool SanitizeFunction, TypeDescriptorsMapTy &TypeDescriptors,
718- const DataLayout &DL) {
719- IRBuilder<> IRB (I);
720- assert (MLoc.Size .isPrecise ());
721- if (instrumentWithShadowUpdate (
722- IRB, MLoc.AATags .TBAA , const_cast <Value *>(MLoc.Ptr ),
723- MLoc.Size .getValue (), I->mayReadFromMemory (), I->mayWriteToMemory (),
724- ShadowBase, AppMemMask, false , SanitizeFunction, TypeDescriptors,
725- DL)) {
726- ++NumInstrumentedAccesses;
727- return true ;
728- }
729-
730- return false ;
731- }
732-
733- // Memory-related intrinsics/instructions reset the type of the destination
734- // memory (including allocas and byval arguments).
735720bool TypeSanitizer::instrumentMemInst (Value *V, Value *&ShadowBase,
736721 Value *&AppMemMask,
737722 const DataLayout &DL) {
0 commit comments