Skip to content

Commit 95825bd

Browse files
committed
!fixup merge reportGlobal again, adjust release notes.
1 parent 5de3195 commit 95825bd

File tree

5 files changed

+47
-53
lines changed

5 files changed

+47
-53
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1155,7 +1155,7 @@ Sanitizers
11551155
for examples.
11561156

11571157
- Introduced an experimental Type Sanitizer, activated by using the
1158-
-fsanitize=type flag. This sanitizer detects violations of C/C++ type-based
1158+
``-fsanitize=type flag. This sanitizer detects violations of C/C++ type-based
11591159
aliasing rules.
11601160
11611161
Python Binding Changes

clang/lib/CodeGen/CGDecl.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -458,8 +458,7 @@ void CodeGenFunction::EmitStaticVarDecl(const VarDecl &D,
458458
LocalDeclMap.find(&D)->second = Address(castedAddr, elemTy, alignment);
459459
CGM.setStaticLocalDeclAddress(&D, castedAddr);
460460

461-
CGM.getSanitizerMetadata()->reportGlobalToASan(var, D);
462-
CGM.getSanitizerMetadata()->reportGlobalToTySan(var, D);
461+
CGM.getSanitizerMetadata()->reportGlobal(var, D);
463462

464463
// Emit global variable debug descriptor for static vars.
465464
CGDebugInfo *DI = getDebugInfo();

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5162,7 +5162,7 @@ CodeGenModule::GetOrCreateLLVMGlobal(StringRef MangledName, llvm::Type *Ty,
51625162
}
51635163

51645164
if (D)
5165-
SanitizerMD->reportGlobalToASan(GV, *D);
5165+
SanitizerMD->reportGlobal(GV, *D);
51665166

51675167
LangAS ExpectedAS =
51685168
D ? D->getType().getAddressSpace()
@@ -5728,8 +5728,7 @@ void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D,
57285728
if (NeedsGlobalCtor || NeedsGlobalDtor)
57295729
EmitCXXGlobalVarDeclInitFunc(D, GV, NeedsGlobalCtor);
57305730

5731-
SanitizerMD->reportGlobalToASan(GV, *D, NeedsGlobalCtor);
5732-
SanitizerMD->reportGlobalToTySan(GV, *D);
5731+
SanitizerMD->reportGlobal(GV, *D, NeedsGlobalCtor);
57335732

57345733
// Emit global variable debug information.
57355734
if (CGDebugInfo *DI = getModuleDebugInfo())
@@ -6619,8 +6618,7 @@ CodeGenModule::GetAddrOfConstantStringFromLiteral(const StringLiteral *S,
66196618
if (Entry)
66206619
*Entry = GV;
66216620

6622-
SanitizerMD->reportGlobalToASan(GV, S->getStrTokenLoc(0), "<string literal>");
6623-
// FIXME: Should we also report to the TySan?
6621+
SanitizerMD->reportGlobal(GV, S->getStrTokenLoc(0), "<string literal>");
66246622

66256623
return ConstantAddress(castStringLiteralToDefaultAddressSpace(*this, GV),
66266624
GV->getValueType(), Alignment);

clang/lib/CodeGen/SanitizerMetadata.cpp

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ using namespace CodeGen;
1919

2020
SanitizerMetadata::SanitizerMetadata(CodeGenModule &CGM) : CGM(CGM) {}
2121

22-
static bool isAsanHwasanOrMemTag(const SanitizerSet &SS) {
22+
static bool isAsanHwasanMemTagOrTysan(const SanitizerSet &SS) {
2323
return SS.hasOneOf(SanitizerKind::Address | SanitizerKind::KernelAddress |
24-
SanitizerKind::HWAddress | SanitizerKind::MemTag);
24+
SanitizerKind::HWAddress | SanitizerKind::MemTag |
25+
SanitizerKind::Type);
2526
}
2627

2728
static SanitizerMask expandKernelSanitizerMasks(SanitizerMask Mask) {
@@ -31,13 +32,13 @@ static SanitizerMask expandKernelSanitizerMasks(SanitizerMask Mask) {
3132
return Mask;
3233
}
3334

34-
void SanitizerMetadata::reportGlobalToASan(llvm::GlobalVariable *GV,
35-
SourceLocation Loc, StringRef Name,
36-
QualType Ty,
37-
SanitizerMask NoSanitizeAttrMask,
38-
bool IsDynInit) {
35+
void SanitizerMetadata::reportGlobal(llvm::GlobalVariable *GV,
36+
SourceLocation Loc, StringRef Name,
37+
QualType Ty,
38+
SanitizerMask NoSanitizeAttrMask,
39+
bool IsDynInit) {
3940
SanitizerSet FsanitizeArgument = CGM.getLangOpts().Sanitize;
40-
if (!isAsanHwasanOrMemTag(FsanitizeArgument))
41+
if (!isAsanHwasanMemTagOrTysan(FsanitizeArgument))
4142
return;
4243

4344
FsanitizeArgument.Mask = expandKernelSanitizerMasks(FsanitizeArgument.Mask);
@@ -70,11 +71,32 @@ void SanitizerMetadata::reportGlobalToASan(llvm::GlobalVariable *GV,
7071
GV, Loc, Ty, "init");
7172

7273
GV->setSanitizerMetadata(Meta);
74+
75+
if (!CGM.getLangOpts().Sanitize.has(SanitizerKind::Type) ||
76+
NoSanitizeAttrMask & SanitizerKind::Type)
77+
return;
78+
79+
llvm::MDNode *TBAAInfo = CGM.getTBAATypeInfo(Ty);
80+
if (!TBAAInfo || TBAAInfo == CGM.getTBAATypeInfo(CGM.getContext().CharTy))
81+
return;
82+
83+
llvm::Metadata *GlobalMetadata[] = {llvm::ConstantAsMetadata::get(GV),
84+
TBAAInfo};
85+
86+
// Metadata for the global already registered.
87+
if (llvm::MDNode::getIfExists(CGM.getLLVMContext(), GlobalMetadata))
88+
return;
89+
90+
llvm::MDNode *ThisGlobal =
91+
llvm::MDNode::get(CGM.getLLVMContext(), GlobalMetadata);
92+
llvm::NamedMDNode *TysanGlobals =
93+
CGM.getModule().getOrInsertNamedMetadata("llvm.tysan.globals");
94+
TysanGlobals->addOperand(ThisGlobal);
7395
}
7496

75-
void SanitizerMetadata::reportGlobalToASan(llvm::GlobalVariable *GV,
76-
const VarDecl &D, bool IsDynInit) {
77-
if (!isAsanHwasanOrMemTag(CGM.getLangOpts().Sanitize))
97+
void SanitizerMetadata::reportGlobal(llvm::GlobalVariable *GV, const VarDecl &D,
98+
bool IsDynInit) {
99+
if (!isAsanHwasanMemTagOrTysan(CGM.getLangOpts().Sanitize))
78100
return;
79101
std::string QualName;
80102
llvm::raw_string_ostream OS(QualName);
@@ -91,34 +113,10 @@ void SanitizerMetadata::reportGlobalToASan(llvm::GlobalVariable *GV,
91113
return NoSanitizeMask;
92114
};
93115

94-
reportGlobalToASan(GV, D.getLocation(), QualName, D.getType(),
95-
getNoSanitizeMask(D), IsDynInit);
96-
}
97-
98-
void SanitizerMetadata::reportGlobalToTySan(llvm::GlobalVariable *GV,
99-
const VarDecl &D) {
100-
if (!CGM.getLangOpts().Sanitize.has(SanitizerKind::Type))
101-
return;
102-
103-
for (auto Attr : D.specific_attrs<NoSanitizeAttr>())
104-
if (Attr->getMask() & SanitizerKind::Type)
105-
return;
106-
107-
QualType QTy = D.getType();
108-
llvm::MDNode *TBAAInfo = CGM.getTBAATypeInfo(QTy);
109-
if (!TBAAInfo || TBAAInfo == CGM.getTBAATypeInfo(CGM.getContext().CharTy))
110-
return;
111-
112-
llvm::Metadata *GlobalMetadata[] = {llvm::ConstantAsMetadata::get(GV),
113-
TBAAInfo};
114-
115-
llvm::MDNode *ThisGlobal =
116-
llvm::MDNode::get(CGM.getLLVMContext(), GlobalMetadata);
117-
llvm::NamedMDNode *TysanGlobals =
118-
CGM.getModule().getOrInsertNamedMetadata("llvm.tysan.globals");
119-
TysanGlobals->addOperand(ThisGlobal);
116+
reportGlobal(GV, D.getLocation(), QualName, D.getType(), getNoSanitizeMask(D),
117+
IsDynInit);
120118
}
121119

122120
void SanitizerMetadata::disableSanitizerForGlobal(llvm::GlobalVariable *GV) {
123-
reportGlobalToASan(GV, SourceLocation(), "", QualType(), SanitizerKind::All);
121+
reportGlobal(GV, SourceLocation(), "", QualType(), SanitizerKind::All);
124122
}

clang/lib/CodeGen/SanitizerMetadata.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,12 @@ class SanitizerMetadata {
3737

3838
public:
3939
SanitizerMetadata(CodeGenModule &CGM);
40-
void reportGlobalToASan(llvm::GlobalVariable *GV, const VarDecl &D,
41-
bool IsDynInit = false);
42-
void reportGlobalToASan(llvm::GlobalVariable *GV, SourceLocation Loc,
43-
StringRef Name, QualType Ty = {},
44-
SanitizerMask NoSanitizeAttrMask = {},
45-
bool IsDynInit = false);
46-
void reportGlobalToTySan(llvm::GlobalVariable *GV, const VarDecl &D);
40+
void reportGlobal(llvm::GlobalVariable *GV, const VarDecl &D,
41+
bool IsDynInit = false);
42+
void reportGlobal(llvm::GlobalVariable *GV, SourceLocation Loc,
43+
StringRef Name, QualType Ty = {},
44+
SanitizerMask NoSanitizeAttrMask = {},
45+
bool IsDynInit = false);
4746
void disableSanitizerForGlobal(llvm::GlobalVariable *GV);
4847
};
4948
} // end namespace CodeGen

0 commit comments

Comments
 (0)