diff options
author | Florian Hahn <flo@fhahn.com> | 2024-04-18 22:52:16 +0100 |
---|---|---|
committer | Florian Hahn <flo@fhahn.com> | 2024-04-18 22:53:03 +0100 |
commit | eb97b07ade4dd02f8a66a5a6b914c2645af2461b (patch) | |
tree | 8a99b088d4e699a2caaee2cf1a839a475ecbca7c | |
parent | 29460f76a3aa9b413b9d6ef9e8c557fe311f8c57 (diff) |
!fixup address comments, thanksupstream/users/fhahn/tysan-a-type-sanitizer-llvm
-rw-r--r-- | llvm/include/llvm/Analysis/TypeBasedAliasAnalysis.h | 8 | ||||
-rw-r--r-- | llvm/lib/Analysis/TypeBasedAliasAnalysis.cpp | 26 | ||||
-rw-r--r-- | llvm/lib/Transforms/Instrumentation/TypeSanitizer.cpp | 22 | ||||
-rw-r--r-- | llvm/unittests/Analysis/AliasSetTrackerTest.cpp | 2 |
4 files changed, 24 insertions, 34 deletions
diff --git a/llvm/include/llvm/Analysis/TypeBasedAliasAnalysis.h b/llvm/include/llvm/Analysis/TypeBasedAliasAnalysis.h index 36dd39c033aa..0ec84a461a3d 100644 --- a/llvm/include/llvm/Analysis/TypeBasedAliasAnalysis.h +++ b/llvm/include/llvm/Analysis/TypeBasedAliasAnalysis.h @@ -29,7 +29,15 @@ class MemoryLocation; /// A simple AA result that uses TBAA metadata to answer queries. class TypeBasedAAResult : public AAResultBase { + /// True if type sanitizer is enabled. When TypeSanitizer is used, don't use + /// TBAA information for alias analysis as this might cause us to remove + /// memory accesses that we need to verify at runtime. + bool UsingTypeSanitizer; + public: + TypeBasedAAResult(bool UsingTypeSanitizer) + : UsingTypeSanitizer(UsingTypeSanitizer) {} + /// Handle invalidation events from the new pass manager. /// /// By definition, this result is stateless and so remains valid. diff --git a/llvm/lib/Analysis/TypeBasedAliasAnalysis.cpp b/llvm/lib/Analysis/TypeBasedAliasAnalysis.cpp index f7e6b0d393f3..1278151a5cca 100644 --- a/llvm/lib/Analysis/TypeBasedAliasAnalysis.cpp +++ b/llvm/lib/Analysis/TypeBasedAliasAnalysis.cpp @@ -371,26 +371,10 @@ static bool isStructPathTBAA(const MDNode *MD) { return isa<MDNode>(MD->getOperand(0)) && MD->getNumOperands() >= 3; } -// When using the TypeSanitizer, don't use TBAA information for alias analysis. -// This might cause us to remove memory accesses that we need to verify at -// runtime. -static bool usingSanitizeType(const Value *V) { - const Function *F; - - if (auto *I = dyn_cast<Instruction>(V)) - F = I->getParent()->getParent(); - else if (auto *A = dyn_cast<Argument>(V)) - F = A->getParent(); - else - return false; - - return F->hasFnAttribute(Attribute::SanitizeType); -} - AliasResult TypeBasedAAResult::alias(const MemoryLocation &LocA, const MemoryLocation &LocB, AAQueryInfo &AAQI, const Instruction *) { - if (!EnableTBAA || usingSanitizeType(LocA.Ptr) || usingSanitizeType(LocB.Ptr)) + if (!EnableTBAA || UsingTypeSanitizer || UsingTypeSanitizer) return AAResultBase::alias(LocA, LocB, AAQI, nullptr); if (Aliases(LocA.AATags.TBAA, LocB.AATags.TBAA)) @@ -441,7 +425,7 @@ MemoryEffects TypeBasedAAResult::getMemoryEffects(const Function *F) { ModRefInfo TypeBasedAAResult::getModRefInfo(const CallBase *Call, const MemoryLocation &Loc, AAQueryInfo &AAQI) { - if (!EnableTBAA || usingSanitizeType(Call)) + if (!EnableTBAA || UsingTypeSanitizer) return AAResultBase::getModRefInfo(Call, Loc, AAQI); if (const MDNode *L = Loc.AATags.TBAA) @@ -455,7 +439,7 @@ ModRefInfo TypeBasedAAResult::getModRefInfo(const CallBase *Call, ModRefInfo TypeBasedAAResult::getModRefInfo(const CallBase *Call1, const CallBase *Call2, AAQueryInfo &AAQI) { - if (!EnableTBAA || usingSanitizeType(Call1)) + if (!EnableTBAA || UsingTypeSanitizer) return AAResultBase::getModRefInfo(Call1, Call2, AAQI); if (const MDNode *M1 = Call1->getMetadata(LLVMContext::MD_tbaa)) @@ -722,7 +706,7 @@ bool TypeBasedAAResult::Aliases(const MDNode *A, const MDNode *B) const { AnalysisKey TypeBasedAA::Key; TypeBasedAAResult TypeBasedAA::run(Function &F, FunctionAnalysisManager &AM) { - return TypeBasedAAResult(); + return TypeBasedAAResult(F.hasFnAttribute(Attribute::SanitizeType)); } char TypeBasedAAWrapperPass::ID = 0; @@ -738,7 +722,7 @@ TypeBasedAAWrapperPass::TypeBasedAAWrapperPass() : ImmutablePass(ID) { } bool TypeBasedAAWrapperPass::doInitialization(Module &M) { - Result.reset(new TypeBasedAAResult()); + Result.reset(new TypeBasedAAResult(false)); return false; } diff --git a/llvm/lib/Transforms/Instrumentation/TypeSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/TypeSanitizer.cpp index ed4aba4ad612..2ca9b8a8d8ce 100644 --- a/llvm/lib/Transforms/Instrumentation/TypeSanitizer.cpp +++ b/llvm/lib/Transforms/Instrumentation/TypeSanitizer.cpp @@ -110,6 +110,7 @@ private: TypeNameMapTy &TypeNames, Module &M); const Triple TargetTriple; + Regex AnonNameRegex; Type *IntptrTy; uint64_t PtrShift; IntegerType *OrdTy; @@ -122,7 +123,8 @@ private: } // namespace TypeSanitizer::TypeSanitizer(Module &M) - : TargetTriple(Triple(M.getTargetTriple())) { + : TargetTriple(Triple(M.getTargetTriple())), + AnonNameRegex("^_ZTS.*N[1-9][0-9]*_GLOBAL__N") { const DataLayout &DL = M.getDataLayout(); IntptrTy = DL.getIntPtrType(M.getContext()); PtrShift = countr_zero(IntptrTy->getPrimitiveSizeInBits() / 8); @@ -237,16 +239,6 @@ static std::string encodeName(StringRef Name) { return Output; } -static bool isAnonymousNamespaceName(StringRef Name) { - // Types that are in an anonymous namespace are local to this module. - // FIXME: This should really be marked by the frontend in the metadata - // instead of having us guess this from the mangled name. Moreover, the regex - // here can pick up (unlikely) names in the non-reserved namespace (because - // it needs to search into the type to pick up cases where the type in the - // anonymous namespace is a template parameter, etc.). - return AnonNameRegex.match(Name); -} - std::string TypeSanitizer::getAnonymousStructIdentifier(const MDNode *MD, TypeNameMapTy &TypeNames) { @@ -352,7 +344,13 @@ bool TypeSanitizer::generateBaseTypeDescriptor( TDSubTys.push_back(IntptrTy); TDSubData.push_back(ConstantInt::get(IntptrTy, Members.size())); - bool ShouldBeComdat = !isAnonymousNamespaceName(NameNode->getString()); + // Types that are in an anonymous namespace are local to this module. + // FIXME: This should really be marked by the frontend in the metadata + // instead of having us guess this from the mangled name. Moreover, the regex + // here can pick up (unlikely) names in the non-reserved namespace (because + // it needs to search into the type to pick up cases where the type in the + // anonymous namespace is a template parameter, etc.). + bool ShouldBeComdat = !AnonNameRegex.match(NameNode->getString()); for (auto &Member : Members) { TDSubTys.push_back(Member.first->getType()); TDSubData.push_back(Member.first); diff --git a/llvm/unittests/Analysis/AliasSetTrackerTest.cpp b/llvm/unittests/Analysis/AliasSetTrackerTest.cpp index 68bd41a1e858..e784e6eefb79 100644 --- a/llvm/unittests/Analysis/AliasSetTrackerTest.cpp +++ b/llvm/unittests/Analysis/AliasSetTrackerTest.cpp @@ -62,7 +62,7 @@ TEST(AliasSetTracker, AliasUnknownInst) { TargetLibraryInfoImpl TLII(Trip); TargetLibraryInfo TLI(TLII); AAResults AA(TLI); - TypeBasedAAResult TBAAR; + TypeBasedAAResult TBAAR(false); AA.addAAResult(TBAAR); // Initialize the alias set tracker for the @test function. |