Skip to content

Commit 4ee0dec

Browse files
committed
[𝘀𝗽𝗿] initial version
Created using spr 1.3.4
2 parents 04c473b + 5883996 commit 4ee0dec

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+2327
-36
lines changed

clang/include/clang/Basic/Features.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ FEATURE(nullability_nullable_result, true)
9898
FEATURE(memory_sanitizer,
9999
LangOpts.Sanitize.hasOneOf(SanitizerKind::Memory |
100100
SanitizerKind::KernelMemory))
101+
FEATURE(type_sanitizer, LangOpts.Sanitize.has(SanitizerKind::Type))
101102
FEATURE(thread_sanitizer, LangOpts.Sanitize.has(SanitizerKind::Thread))
102103
FEATURE(dataflow_sanitizer, LangOpts.Sanitize.has(SanitizerKind::DataFlow))
103104
FEATURE(scudo, LangOpts.Sanitize.hasOneOf(SanitizerKind::Scudo))

clang/include/clang/Basic/Sanitizers.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ SANITIZER("fuzzer", Fuzzer)
7373
// libFuzzer-required instrumentation, no linking.
7474
SANITIZER("fuzzer-no-link", FuzzerNoLink)
7575

76+
// TypeSanitizer
77+
SANITIZER("type", Type)
78+
7679
// ThreadSanitizer
7780
SANITIZER("thread", Thread)
7881

clang/include/clang/Driver/SanitizerArgs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ class SanitizerArgs {
8686
bool needsHwasanAliasesRt() const {
8787
return needsHwasanRt() && HwasanUseAliases;
8888
}
89+
bool needsTysanRt() const { return Sanitizers.has(SanitizerKind::Type); }
8990
bool needsTsanRt() const { return Sanitizers.has(SanitizerKind::Thread); }
9091
bool needsMsanRt() const { return Sanitizers.has(SanitizerKind::Memory); }
9192
bool needsFuzzer() const { return Sanitizers.has(SanitizerKind::Fuzzer); }

clang/lib/CodeGen/BackendUtil.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
#include "llvm/Transforms/Instrumentation/SanitizerBinaryMetadata.h"
8080
#include "llvm/Transforms/Instrumentation/SanitizerCoverage.h"
8181
#include "llvm/Transforms/Instrumentation/ThreadSanitizer.h"
82+
#include "llvm/Transforms/Instrumentation/TypeSanitizer.h"
8283
#include "llvm/Transforms/ObjCARC.h"
8384
#include "llvm/Transforms/Scalar/EarlyCSE.h"
8485
#include "llvm/Transforms/Scalar/GVN.h"
@@ -685,6 +686,11 @@ static void addSanitizers(const Triple &TargetTriple,
685686
MPM.addPass(createModuleToFunctionPassAdaptor(ThreadSanitizerPass()));
686687
}
687688

689+
if (LangOpts.Sanitize.has(SanitizerKind::Type)) {
690+
MPM.addPass(ModuleTypeSanitizerPass());
691+
MPM.addPass(createModuleToFunctionPassAdaptor(TypeSanitizerPass()));
692+
}
693+
688694
auto ASanPass = [&](SanitizerMask Mask, bool CompileKernel) {
689695
if (LangOpts.Sanitize.has(Mask)) {
690696
bool UseGlobalGC = asanUseGlobalsGC(TargetTriple, CodeGenOpts);

clang/lib/CodeGen/CGDecl.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,8 @@ void CodeGenFunction::EmitStaticVarDecl(const VarDecl &D,
481481
LocalDeclMap.find(&D)->second = Address(castedAddr, elemTy, alignment);
482482
CGM.setStaticLocalDeclAddress(&D, castedAddr);
483483

484-
CGM.getSanitizerMetadata()->reportGlobal(var, D);
484+
CGM.getSanitizerMetadata()->reportGlobalToASan(var, D);
485+
CGM.getSanitizerMetadata()->reportGlobalToTySan(var, D);
485486

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

clang/lib/CodeGen/CGDeclCXX.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,10 @@ llvm::Function *CodeGenModule::CreateGlobalInitOrCleanUpFunction(
472472
!isInNoSanitizeList(SanitizerKind::MemtagStack, Fn, Loc))
473473
Fn->addFnAttr(llvm::Attribute::SanitizeMemTag);
474474

475+
if (getLangOpts().Sanitize.has(SanitizerKind::Type) &&
476+
!isInNoSanitizeList(SanitizerKind::Type, Fn, Loc))
477+
Fn->addFnAttr(llvm::Attribute::SanitizeType);
478+
475479
if (getLangOpts().Sanitize.has(SanitizerKind::Thread) &&
476480
!isInNoSanitizeList(SanitizerKind::Thread, Fn, Loc))
477481
Fn->addFnAttr(llvm::Attribute::SanitizeThread);

clang/lib/CodeGen/CodeGenFunction.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,8 @@ void CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy,
790790
Fn->addFnAttr(llvm::Attribute::SanitizeMemTag);
791791
if (SanOpts.has(SanitizerKind::Thread))
792792
Fn->addFnAttr(llvm::Attribute::SanitizeThread);
793+
if (SanOpts.has(SanitizerKind::Type))
794+
Fn->addFnAttr(llvm::Attribute::SanitizeType);
793795
if (SanOpts.hasOneOf(SanitizerKind::Memory | SanitizerKind::KernelMemory))
794796
Fn->addFnAttr(llvm::Attribute::SanitizeMemory);
795797
}

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -391,8 +391,8 @@ CodeGenModule::CodeGenModule(ASTContext &C,
391391
if (LangOpts.HLSL)
392392
createHLSLRuntime();
393393

394-
// Enable TBAA unless it's suppressed. ThreadSanitizer needs TBAA even at O0.
395-
if (LangOpts.Sanitize.has(SanitizerKind::Thread) ||
394+
// Enable TBAA unless it's suppressed. TSan and TySan need TBAA even at O0.
395+
if (LangOpts.Sanitize.hasOneOf(SanitizerKind::Thread | SanitizerKind::Type) ||
396396
(!CodeGenOpts.RelaxedAliasing && CodeGenOpts.OptimizationLevel > 0))
397397
TBAA.reset(new CodeGenTBAA(Context, TheModule, CodeGenOpts, getLangOpts(),
398398
getCXXABI().getMangleContext()));
@@ -4924,7 +4924,7 @@ CodeGenModule::GetOrCreateLLVMGlobal(StringRef MangledName, llvm::Type *Ty,
49244924
}
49254925

49264926
if (D)
4927-
SanitizerMD->reportGlobal(GV, *D);
4927+
SanitizerMD->reportGlobalToASan(GV, *D);
49284928

49294929
LangAS ExpectedAS =
49304930
D ? D->getType().getAddressSpace()
@@ -5465,7 +5465,8 @@ void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D,
54655465
if (NeedsGlobalCtor || NeedsGlobalDtor)
54665466
EmitCXXGlobalVarDeclInitFunc(D, GV, NeedsGlobalCtor);
54675467

5468-
SanitizerMD->reportGlobal(GV, *D, NeedsGlobalCtor);
5468+
SanitizerMD->reportGlobalToASan(GV, *D, NeedsGlobalCtor);
5469+
SanitizerMD->reportGlobalToTySan(GV, *D);
54695470

54705471
// Emit global variable debug information.
54715472
if (CGDebugInfo *DI = getModuleDebugInfo())
@@ -6341,7 +6342,8 @@ CodeGenModule::GetAddrOfConstantStringFromLiteral(const StringLiteral *S,
63416342
if (Entry)
63426343
*Entry = GV;
63436344

6344-
SanitizerMD->reportGlobal(GV, S->getStrTokenLoc(0), "<string literal>");
6345+
SanitizerMD->reportGlobalToASan(GV, S->getStrTokenLoc(0), "<string literal>");
6346+
// FIXME: Should we also report to the TySan?
63456347

63466348
return ConstantAddress(castStringLiteralToDefaultAddressSpace(*this, GV),
63476349
GV->getValueType(), Alignment);

clang/lib/CodeGen/CodeGenTBAA.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,10 @@ llvm::MDNode *CodeGenTBAA::getTypeInfoHelper(const Type *Ty) {
226226
}
227227

228228
llvm::MDNode *CodeGenTBAA::getTypeInfo(QualType QTy) {
229-
// At -O0 or relaxed aliasing, TBAA is not emitted for regular types.
230-
if (CodeGenOpts.OptimizationLevel == 0 || CodeGenOpts.RelaxedAliasing)
229+
// At -O0 or relaxed aliasing, TBAA is not emitted for regular types (unless
230+
// we're running TypeSanitizer).
231+
if (!Features.Sanitize.has(SanitizerKind::Type) &&
232+
(CodeGenOpts.OptimizationLevel == 0 || CodeGenOpts.RelaxedAliasing))
231233
return nullptr;
232234

233235
// If the type has the may_alias attribute (even on a typedef), it is

clang/lib/CodeGen/SanitizerMetadata.cpp

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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

101125
void SanitizerMetadata::disableSanitizerForGlobal(llvm::GlobalVariable *GV) {
102-
reportGlobal(GV, SourceLocation(), "", QualType(), SanitizerKind::All);
126+
reportGlobalToASan(GV, SourceLocation(), "", QualType(), SanitizerKind::All);
103127
}

0 commit comments

Comments
 (0)