Skip to content

Commit

Permalink
[IR] Adds Instruction::setNoSanitizeMetadata()
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Enna1 committed May 19, 2023
1 parent 6a95042 commit e4e6c65
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 18 deletions.
2 changes: 1 addition & 1 deletion clang/lib/CodeGen/CodeGenFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/CodeGen/ItaniumCXXABI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
5 changes: 0 additions & 5 deletions clang/lib/CodeGen/SanitizerMetadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
1 change: 0 additions & 1 deletion clang/lib/CodeGen/SanitizerMetadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions llvm/include/llvm/IR/Instruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions llvm/lib/IR/Metadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 6 additions & 10 deletions llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand All @@ -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.
Expand All @@ -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();
}
}

Expand Down

0 comments on commit e4e6c65

Please sign in to comment.