Author: Enna1 Date: 2023-05-19T19:18:57+08:00 New Revision: e4e6c6510b183f478df48a92feb6ef1a5e20aa0a
URL: https://github.com/llvm/llvm-project/commit/e4e6c6510b183f478df48a92feb6ef1a5e20aa0a DIFF: https://github.com/llvm/llvm-project/commit/e4e6c6510b183f478df48a92feb6ef1a5e20aa0a.diff LOG: [IR] Adds Instruction::setNoSanitizeMetadata() This patch adds a new method setNoSanitizeMetadata() for Instruction, and use it in SanitizerMetadata and SanitizerCoverage. Reviewed By: nickdesaulniers, MaskRay Differential Revision: https://reviews.llvm.org/D150632 Added: Modified: clang/lib/CodeGen/CodeGenFunction.cpp clang/lib/CodeGen/ItaniumCXXABI.cpp clang/lib/CodeGen/SanitizerMetadata.cpp clang/lib/CodeGen/SanitizerMetadata.h llvm/include/llvm/IR/Instruction.h llvm/lib/IR/Metadata.cpp llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp Removed: ################################################################################ diff --git a/clang/lib/CodeGen/CodeGenFunction.cpp b/clang/lib/CodeGen/CodeGenFunction.cpp index f6f92ebd85cf2..5c8b8821cd28d 100644 --- a/clang/lib/CodeGen/CodeGenFunction.cpp +++ b/clang/lib/CodeGen/CodeGenFunction.cpp @@ -2541,7 +2541,7 @@ void CodeGenFunction::InsertHelper(llvm::Instruction *I, llvm::BasicBlock::iterator InsertPt) const { LoopStack.InsertHelper(I); if (IsSanitizerScope) - CGM.getSanitizerMetadata()->disableSanitizerForInstruction(I); + I->setNoSanitizeMetadata(); } void CGBuilderInserter::InsertHelper( diff --git a/clang/lib/CodeGen/ItaniumCXXABI.cpp b/clang/lib/CodeGen/ItaniumCXXABI.cpp index bf78d7495df1a..f4f722a1be063 100644 --- a/clang/lib/CodeGen/ItaniumCXXABI.cpp +++ b/clang/lib/CodeGen/ItaniumCXXABI.cpp @@ -2189,7 +2189,7 @@ Address ItaniumCXXABI::InitializeArrayCookie(CodeGenFunction &CGF, (expr->getOperatorNew()->isReplaceableGlobalAllocationFunction() || CGM.getCodeGenOpts().SanitizeAddressPoisonCustomArrayCookie)) { // The store to the CookiePtr does not need to be instrumented. - CGM.getSanitizerMetadata()->disableSanitizerForInstruction(SI); + SI->setNoSanitizeMetadata(); llvm::FunctionType *FTy = llvm::FunctionType::get(CGM.VoidTy, NumElementsPtr.getType(), false); llvm::FunctionCallee F = diff --git a/clang/lib/CodeGen/SanitizerMetadata.cpp b/clang/lib/CodeGen/SanitizerMetadata.cpp index 554f1ea2a47d5..53161c316c58a 100644 --- a/clang/lib/CodeGen/SanitizerMetadata.cpp +++ b/clang/lib/CodeGen/SanitizerMetadata.cpp @@ -101,8 +101,3 @@ void SanitizerMetadata::reportGlobal(llvm::GlobalVariable *GV, const VarDecl &D, void SanitizerMetadata::disableSanitizerForGlobal(llvm::GlobalVariable *GV) { reportGlobal(GV, SourceLocation(), "", QualType(), SanitizerKind::All); } - -void SanitizerMetadata::disableSanitizerForInstruction(llvm::Instruction *I) { - I->setMetadata(llvm::LLVMContext::MD_nosanitize, - llvm::MDNode::get(CGM.getLLVMContext(), std::nullopt)); -} diff --git a/clang/lib/CodeGen/SanitizerMetadata.h b/clang/lib/CodeGen/SanitizerMetadata.h index f5dd0e503cc00..000f02cf8dcf1 100644 --- a/clang/lib/CodeGen/SanitizerMetadata.h +++ b/clang/lib/CodeGen/SanitizerMetadata.h @@ -44,7 +44,6 @@ class SanitizerMetadata { SanitizerMask NoSanitizeAttrMask = {}, bool IsDynInit = false); void disableSanitizerForGlobal(llvm::GlobalVariable *GV); - void disableSanitizerForInstruction(llvm::Instruction *I); }; } // end namespace CodeGen } // end namespace clang diff --git a/llvm/include/llvm/IR/Instruction.h b/llvm/include/llvm/IR/Instruction.h index 732f6df133576..7de412873b97b 100644 --- a/llvm/include/llvm/IR/Instruction.h +++ b/llvm/include/llvm/IR/Instruction.h @@ -350,6 +350,9 @@ class Instruction : public User, /// Sets the AA metadata on this instruction from the AAMDNodes structure. void setAAMetadata(const AAMDNodes &N); + /// Sets the nosanitize metadata on this instruction. + void setNoSanitizeMetadata(); + /// Retrieve total raw weight values of a branch. /// Returns true on success with profile total weights filled in. /// Returns false if no metadata was found. diff --git a/llvm/lib/IR/Metadata.cpp b/llvm/lib/IR/Metadata.cpp index face1ba58cfbb..e2a89e2b6300b 100644 --- a/llvm/lib/IR/Metadata.cpp +++ b/llvm/lib/IR/Metadata.cpp @@ -1612,6 +1612,11 @@ void Instruction::setAAMetadata(const AAMDNodes &N) { setMetadata(LLVMContext::MD_noalias, N.NoAlias); } +void Instruction::setNoSanitizeMetadata() { + setMetadata(llvm::LLVMContext::MD_nosanitize, + llvm::MDNode::get(getContext(), std::nullopt)); +} + MDNode *Instruction::getMetadataImpl(unsigned KindID) const { // Handle 'dbg' as a special case since it is not stored in the hash table. if (KindID == LLVMContext::MD_dbg) diff --git a/llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp b/llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp index 046ab5650abe7..1f21f9efa9265 100644 --- a/llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp +++ b/llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp @@ -248,10 +248,6 @@ class ModuleSanitizerCoverage { std::pair<Value *, Value *> CreateSecStartEnd(Module &M, const char *Section, Type *Ty); - void SetNoSanitizeMetadata(Instruction *I) { - I->setMetadata(LLVMContext::MD_nosanitize, MDNode::get(*C, std::nullopt)); - } - std::string getSectionName(const std::string &Section) const; std::string getSectionStart(const std::string &Section) const; std::string getSectionEnd(const std::string &Section) const; @@ -992,8 +988,8 @@ void ModuleSanitizerCoverage::InjectCoverageAtBlock(Function &F, BasicBlock &BB, auto Load = IRB.CreateLoad(Int8Ty, CounterPtr); auto Inc = IRB.CreateAdd(Load, ConstantInt::get(Int8Ty, 1)); auto Store = IRB.CreateStore(Inc, CounterPtr); - SetNoSanitizeMetadata(Load); - SetNoSanitizeMetadata(Store); + Load->setNoSanitizeMetadata(); + Store->setNoSanitizeMetadata(); } if (Options.InlineBoolFlag) { auto FlagPtr = IRB.CreateGEP( @@ -1004,8 +1000,8 @@ void ModuleSanitizerCoverage::InjectCoverageAtBlock(Function &F, BasicBlock &BB, SplitBlockAndInsertIfThen(IRB.CreateIsNull(Load), &*IP, false); IRBuilder<> ThenIRB(ThenTerm); auto Store = ThenIRB.CreateStore(ConstantInt::getTrue(Int1Ty), FlagPtr); - SetNoSanitizeMetadata(Load); - SetNoSanitizeMetadata(Store); + Load->setNoSanitizeMetadata(); + Store->setNoSanitizeMetadata(); } if (Options.StackDepth && IsEntryBB && !IsLeafFunc) { // Check stack depth. If it's the deepest so far, record it. @@ -1021,8 +1017,8 @@ void ModuleSanitizerCoverage::InjectCoverageAtBlock(Function &F, BasicBlock &BB, auto ThenTerm = SplitBlockAndInsertIfThen(IsStackLower, &*IP, false); IRBuilder<> ThenIRB(ThenTerm); auto Store = ThenIRB.CreateStore(FrameAddrInt, SanCovLowestStack); - SetNoSanitizeMetadata(LowestStack); - SetNoSanitizeMetadata(Store); + LowestStack->setNoSanitizeMetadata(); + Store->setNoSanitizeMetadata(); } } _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits