-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[TySan] A Type Sanitizer (Clang) #76260
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
4c3ac21
[𝘀𝗽𝗿] initial version
fhahn e5158df
!fixup address comments, thanks
fhahn 9e30a88
!fiupx address comments, thanks!
fhahn 557abfc
!fixup remove Instrumentation.h include
fhahn f035588
!fixup address latest comments, thanks!
fhahn 9e05f83
!fixup modernize check lines.
fhahn 1dd1d14
!fixup pass ShadowBase/AppMemMask by value.
fhahn 4c19928
!fixup address first set of comments, thanks!
fhahn 694cb2c
!Fixup address remaining comments, thanks!
fhahn be6f759
[TySan] A Type Sanitizer (Clang)
fhahn 9adf5be
!fixup: add test
fhahn 349c32d
!fixup formatting and add release note.
fhahn 2c3ac3d
!fixup merge reportGlobal again, adjust release notes.
fhahn bac1590
!fixup add missing ``
fhahn 8dfaca8
!fixup undo unrelated changes, fix runtimes to push
fhahn 13c4092
!fixup account for globals without types.
fhahn 916f9b9
Merge remote-tracking branch 'origin/main' into users/fhahn/tysan-a-t…
fhahn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,9 +19,10 @@ using namespace CodeGen; | |
|
||
SanitizerMetadata::SanitizerMetadata(CodeGenModule &CGM) : CGM(CGM) {} | ||
|
||
static bool isAsanHwasanOrMemTag(const SanitizerSet &SS) { | ||
static bool isAsanHwasanMemTagOrTysan(const SanitizerSet &SS) { | ||
return SS.hasOneOf(SanitizerKind::Address | SanitizerKind::KernelAddress | | ||
SanitizerKind::HWAddress | SanitizerKind::MemTag); | ||
SanitizerKind::HWAddress | SanitizerKind::MemTag | | ||
SanitizerKind::Type); | ||
} | ||
|
||
static SanitizerMask expandKernelSanitizerMasks(SanitizerMask Mask) { | ||
|
@@ -68,7 +69,7 @@ void SanitizerMetadata::reportGlobal(llvm::GlobalVariable *GV, | |
SanitizerMask NoSanitizeAttrMask, | ||
bool IsDynInit) { | ||
SanitizerSet FsanitizeArgument = CGM.getLangOpts().Sanitize; | ||
if (!isAsanHwasanOrMemTag(FsanitizeArgument)) | ||
if (!isAsanHwasanMemTagOrTysan(FsanitizeArgument)) | ||
return; | ||
|
||
FsanitizeArgument.Mask = expandKernelSanitizerMasks(FsanitizeArgument.Mask); | ||
|
@@ -105,11 +106,32 @@ void SanitizerMetadata::reportGlobal(llvm::GlobalVariable *GV, | |
GV, Loc, Ty, "init"); | ||
|
||
GV->setSanitizerMetadata(Meta); | ||
|
||
if (Ty.isNull() || !CGM.getLangOpts().Sanitize.has(SanitizerKind::Type) || | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We also need to exclude globals defined externally as well I think: #120565 |
||
NoSanitizeAttrMask & SanitizerKind::Type) | ||
return; | ||
|
||
llvm::MDNode *TBAAInfo = CGM.getTBAATypeInfo(Ty); | ||
if (!TBAAInfo || TBAAInfo == CGM.getTBAATypeInfo(CGM.getContext().CharTy)) | ||
return; | ||
|
||
llvm::Metadata *GlobalMetadata[] = {llvm::ConstantAsMetadata::get(GV), | ||
TBAAInfo}; | ||
|
||
// Metadata for the global already registered. | ||
if (llvm::MDNode::getIfExists(CGM.getLLVMContext(), GlobalMetadata)) | ||
return; | ||
|
||
llvm::MDNode *ThisGlobal = | ||
llvm::MDNode::get(CGM.getLLVMContext(), GlobalMetadata); | ||
llvm::NamedMDNode *TysanGlobals = | ||
CGM.getModule().getOrInsertNamedMetadata("llvm.tysan.globals"); | ||
TysanGlobals->addOperand(ThisGlobal); | ||
} | ||
|
||
void SanitizerMetadata::reportGlobal(llvm::GlobalVariable *GV, const VarDecl &D, | ||
bool IsDynInit) { | ||
if (!isAsanHwasanOrMemTag(CGM.getLangOpts().Sanitize)) | ||
if (!isAsanHwasanMemTagOrTysan(CGM.getLangOpts().Sanitize)) | ||
return; | ||
std::string QualName; | ||
llvm::raw_string_ostream OS(QualName); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-llvm -o - %s | FileCheck -check-prefix=WITHOUT %s | ||
// RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-llvm -o - %s -fsanitize=type | FileCheck -check-prefix=TYSAN %s | ||
// RUN: echo "src:%s" | sed -e 's/\\/\\\\/g' > %t | ||
// RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-llvm -o - %s -fsanitize=type -fsanitize-blacklist=%t | FileCheck -check-prefix=BL %s | ||
|
||
// The sanitize_type attribute should be attached to functions | ||
// when TypeSanitizer is enabled, unless no_sanitize("type") attribute | ||
// is present. | ||
|
||
// WITHOUT: NoTYSAN1{{.*}}) [[NOATTR:#[0-9]+]] | ||
// BL: NoTYSAN1{{.*}}) [[NOATTR:#[0-9]+]] | ||
// TYSAN: NoTYSAN1{{.*}}) [[NOATTR:#[0-9]+]] | ||
__attribute__((no_sanitize("type"))) int NoTYSAN1(int *a) { return *a; } | ||
|
||
// WITHOUT: NoTYSAN2{{.*}}) [[NOATTR]] | ||
// BL: NoTYSAN2{{.*}}) [[NOATTR]] | ||
// TYSAN: NoTYSAN2{{.*}}) [[NOATTR]] | ||
__attribute__((no_sanitize("type"))) int NoTYSAN2(int *a); | ||
int NoTYSAN2(int *a) { return *a; } | ||
|
||
// WITHOUT: NoTYSAN3{{.*}}) [[NOATTR:#[0-9]+]] | ||
// BL: NoTYSAN3{{.*}}) [[NOATTR:#[0-9]+]] | ||
// TYSAN: NoTYSAN3{{.*}}) [[NOATTR:#[0-9]+]] | ||
__attribute__((no_sanitize("type"))) int NoTYSAN3(int *a) { return *a; } | ||
|
||
// WITHOUT: TYSANOk{{.*}}) [[NOATTR]] | ||
// BL: TYSANOk{{.*}}) [[NOATTR]] | ||
// TYSAN: TYSANOk{{.*}}) [[WITH:#[0-9]+]] | ||
int TYSANOk(int *a) { return *a; } | ||
|
||
// WITHOUT: TemplateTYSANOk{{.*}}) [[NOATTR]] | ||
// BL: TemplateTYSANOk{{.*}}) [[NOATTR]] | ||
// TYSAN: TemplateTYSANOk{{.*}}) [[WITH]] | ||
template <int i> | ||
int TemplateTYSANOk() { return i; } | ||
|
||
// WITHOUT: TemplateNoTYSAN{{.*}}) [[NOATTR]] | ||
// BL: TemplateNoTYSAN{{.*}}) [[NOATTR]] | ||
// TYSAN: TemplateNoTYSAN{{.*}}) [[NOATTR]] | ||
template <int i> | ||
__attribute__((no_sanitize("type"))) int TemplateNoTYSAN() { return i; } | ||
|
||
int force_instance = TemplateTYSANOk<42>() + TemplateNoTYSAN<42>(); | ||
|
||
// Check that __cxx_global_var_init* get the sanitize_type attribute. | ||
int global1 = 0; | ||
int global2 = *(int *)((char *)&global1 + 1); | ||
// WITHOUT: @__cxx_global_var_init{{.*}}[[NOATTR:#[0-9]+]] | ||
// BL: @__cxx_global_var_init{{.*}}[[NOATTR:#[0-9]+]] | ||
// TYSAN: @__cxx_global_var_init{{.*}}[[WITH:#[0-9]+]] | ||
|
||
// Make sure that we don't add globals to the list for which we don't have a | ||
// specific type description. | ||
// FIXME: We now have a type description for this type and a global is added. Should it? | ||
struct SX { | ||
int a, b; | ||
}; | ||
SX sx; | ||
|
||
void consumer(const char *); | ||
|
||
void char_caller() { | ||
// TYSAN: void @_Z11char_callerv() | ||
// TYSAN-NEXT: entry: | ||
// TYSAN-NEXT: call void @_Z8consumerPKc(ptr noundef @.str) | ||
// TYSAN-NEXT: ret void | ||
|
||
consumer("foo"); | ||
} | ||
|
||
// WITHOUT: attributes [[NOATTR]] = { noinline nounwind{{.*}} } | ||
|
||
// BL: attributes [[NOATTR]] = { noinline nounwind{{.*}} } | ||
|
||
// TYSAN: attributes [[NOATTR]] = { mustprogress noinline nounwind{{.*}} } | ||
// TYSAN: attributes [[WITH]] = { noinline nounwind sanitize_type{{.*}} } | ||
|
||
// TYSAN-DAG: !llvm.tysan.globals = !{[[G1MD:![0-9]+]], [[G2MD:![0-9]+]], [[G3MD:![0-9]+]], [[SXMD:![0-9]+]]} | ||
// TYSAN-DAG: [[G1MD]] = !{ptr @force_instance, [[INTMD:![0-9]+]]} | ||
// TYSAN-DAG: [[INTMD]] = !{!"int", | ||
// TYSAN-DAG: [[G2MD]] = !{ptr @global1, [[INTMD]]} | ||
// TYSAN-DAG: [[G3MD]] = !{ptr @global2, [[INTMD]]} | ||
// TYSAN-DAG: [[SXMD]] = !{ptr @sx, [[SXTYMD:![0-9]+]]} | ||
// TYSAN-DAG: [[SXTYMD]] = !{!"_ZTS2SX", [[INTMD]], i64 0, !1, i64 4} | ||
// TYSAN-DAG: Simple C++ TBAA |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
None of these sanitizers are features. :-( I think this is the correct thing to do for consistency, but at the same time, we keep adding more sanitizers and we keep making this problem worse.
Nothing to change here, just me grumbling. :-D
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where would be the right place to add those? Might be good to at least file an issue to clean this up?