@@ -34,11 +34,11 @@ SanitizerMask expandKernelSanitizerMasks(SanitizerMask Mask) {
3434 return Mask;
3535}
3636
37- void SanitizerMetadata::reportGlobal (llvm::GlobalVariable *GV,
38- SourceLocation Loc, StringRef Name,
39- QualType Ty,
40- SanitizerMask NoSanitizeAttrMask,
41- bool IsDynInit) {
37+ void SanitizerMetadata::reportGlobalToASan (llvm::GlobalVariable *GV,
38+ SourceLocation Loc, StringRef Name,
39+ QualType Ty,
40+ SanitizerMask NoSanitizeAttrMask,
41+ bool IsDynInit) {
4242 SanitizerSet FsanitizeArgument = CGM.getLangOpts ().Sanitize ;
4343 if (!isAsanHwasanOrMemTag (FsanitizeArgument))
4444 return ;
@@ -75,8 +75,8 @@ void SanitizerMetadata::reportGlobal(llvm::GlobalVariable *GV,
7575 GV->setSanitizerMetadata (Meta);
7676}
7777
78- void SanitizerMetadata::reportGlobal (llvm::GlobalVariable *GV, const VarDecl &D ,
79- bool IsDynInit) {
78+ void SanitizerMetadata::reportGlobalToASan (llvm::GlobalVariable *GV,
79+ const VarDecl &D, bool IsDynInit) {
8080 if (!isAsanHwasanOrMemTag (CGM.getLangOpts ().Sanitize ))
8181 return ;
8282 std::string QualName;
@@ -94,10 +94,34 @@ void SanitizerMetadata::reportGlobal(llvm::GlobalVariable *GV, const VarDecl &D,
9494 return NoSanitizeMask;
9595 };
9696
97- reportGlobal (GV, D.getLocation (), OS.str (), D.getType (), getNoSanitizeMask (D),
98- IsDynInit);
97+ reportGlobalToASan (GV, D.getLocation (), OS.str (), D.getType (),
98+ getNoSanitizeMask (D), IsDynInit);
99+ }
100+
101+ void SanitizerMetadata::reportGlobalToTySan (llvm::GlobalVariable *GV,
102+ const VarDecl &D) {
103+ if (!CGM.getLangOpts ().Sanitize .has (SanitizerKind::Type))
104+ return ;
105+
106+ for (auto Attr : D.specific_attrs <NoSanitizeAttr>())
107+ if (Attr->getMask () & SanitizerKind::Type)
108+ return ;
109+
110+ QualType QTy = D.getType ();
111+ llvm::MDNode *TBAAInfo = CGM.getTBAATypeInfo (QTy);
112+ if (!TBAAInfo || TBAAInfo == CGM.getTBAATypeInfo (CGM.getContext ().CharTy ))
113+ return ;
114+
115+ llvm::Metadata *GlobalMetadata[] = {llvm::ConstantAsMetadata::get (GV),
116+ TBAAInfo};
117+
118+ llvm::MDNode *ThisGlobal =
119+ llvm::MDNode::get (CGM.getLLVMContext (), GlobalMetadata);
120+ llvm::NamedMDNode *TysanGlobals =
121+ CGM.getModule ().getOrInsertNamedMetadata (" llvm.tysan.globals" );
122+ TysanGlobals->addOperand (ThisGlobal);
99123}
100124
101125void SanitizerMetadata::disableSanitizerForGlobal (llvm::GlobalVariable *GV) {
102- reportGlobal (GV, SourceLocation (), " " , QualType (), SanitizerKind::All);
126+ reportGlobalToASan (GV, SourceLocation (), " " , QualType (), SanitizerKind::All);
103127}
0 commit comments