[clang] 5f605e2 - [SanitizerBinaryMetadata] Respect no_sanitize("thread") function attribute
Marco Elver via cfe-commits
cfe-commits at lists.llvm.org
Wed Apr 19 05:50:18 PDT 2023
Author: Marco Elver
Date: 2023-04-19T14:49:56+02:00
New Revision: 5f605e254a0f81a41fd69025c572d597f3059ebc
URL: https://github.com/llvm/llvm-project/commit/5f605e254a0f81a41fd69025c572d597f3059ebc
DIFF: https://github.com/llvm/llvm-project/commit/5f605e254a0f81a41fd69025c572d597f3059ebc.diff
LOG: [SanitizerBinaryMetadata] Respect no_sanitize("thread") function attribute
To avoid false positives, respect no_sanitize("thread") when atomics
metadata is enabled.
Reviewed By: dvyukov
Differential Revision: https://reviews.llvm.org/D148694
Added:
clang/test/CodeGen/sanitize-metadata-nosanitize.c
Modified:
clang/lib/CodeGen/CodeGenFunction.cpp
llvm/lib/Transforms/Instrumentation/SanitizerBinaryMetadata.cpp
Removed:
################################################################################
diff --git a/clang/lib/CodeGen/CodeGenFunction.cpp b/clang/lib/CodeGen/CodeGenFunction.cpp
index a5cbf7223ad65..f6f92ebd85cf2 100644
--- a/clang/lib/CodeGen/CodeGenFunction.cpp
+++ b/clang/lib/CodeGen/CodeGenFunction.cpp
@@ -730,31 +730,38 @@ void CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy,
if (D) {
const bool SanitizeBounds = SanOpts.hasOneOf(SanitizerKind::Bounds);
+ SanitizerMask no_sanitize_mask;
bool NoSanitizeCoverage = false;
for (auto *Attr : D->specific_attrs<NoSanitizeAttr>()) {
- // Apply the no_sanitize* attributes to SanOpts.
- SanitizerMask mask = Attr->getMask();
- SanOpts.Mask &= ~mask;
- if (mask & SanitizerKind::Address)
- SanOpts.set(SanitizerKind::KernelAddress, false);
- if (mask & SanitizerKind::KernelAddress)
- SanOpts.set(SanitizerKind::Address, false);
- if (mask & SanitizerKind::HWAddress)
- SanOpts.set(SanitizerKind::KernelHWAddress, false);
- if (mask & SanitizerKind::KernelHWAddress)
- SanOpts.set(SanitizerKind::HWAddress, false);
-
+ no_sanitize_mask |= Attr->getMask();
// SanitizeCoverage is not handled by SanOpts.
if (Attr->hasCoverage())
NoSanitizeCoverage = true;
}
+ // Apply the no_sanitize* attributes to SanOpts.
+ SanOpts.Mask &= ~no_sanitize_mask;
+ if (no_sanitize_mask & SanitizerKind::Address)
+ SanOpts.set(SanitizerKind::KernelAddress, false);
+ if (no_sanitize_mask & SanitizerKind::KernelAddress)
+ SanOpts.set(SanitizerKind::Address, false);
+ if (no_sanitize_mask & SanitizerKind::HWAddress)
+ SanOpts.set(SanitizerKind::KernelHWAddress, false);
+ if (no_sanitize_mask & SanitizerKind::KernelHWAddress)
+ SanOpts.set(SanitizerKind::HWAddress, false);
+
if (SanitizeBounds && !SanOpts.hasOneOf(SanitizerKind::Bounds))
Fn->addFnAttr(llvm::Attribute::NoSanitizeBounds);
if (NoSanitizeCoverage && CGM.getCodeGenOpts().hasSanitizeCoverage())
Fn->addFnAttr(llvm::Attribute::NoSanitizeCoverage);
+
+ // Some passes need the non-negated no_sanitize attribute. Pass them on.
+ if (CGM.getCodeGenOpts().hasSanitizeBinaryMetadata()) {
+ if (no_sanitize_mask & SanitizerKind::Thread)
+ Fn->addFnAttr("no_sanitize_thread");
+ }
}
if (ShouldSkipSanitizerInstrumentation()) {
diff --git a/clang/test/CodeGen/sanitize-metadata-nosanitize.c b/clang/test/CodeGen/sanitize-metadata-nosanitize.c
new file mode 100644
index 0000000000000..488714fe6078e
--- /dev/null
+++ b/clang/test/CodeGen/sanitize-metadata-nosanitize.c
@@ -0,0 +1,111 @@
+// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py UTC_ARGS: --check-attributes --check-globals --version 2
+// RUN: %clang_cc1 -O -fexperimental-sanitize-metadata=covered -fexperimental-sanitize-metadata=atomics -fexperimental-sanitize-metadata=uar -triple x86_64-gnu-linux -x c -S -emit-llvm %s -o - | FileCheck %s --check-prefixes=CHECK
+
+//.
+// CHECK: @__start_sanmd_covered = extern_weak hidden global ptr
+// CHECK: @__stop_sanmd_covered = extern_weak hidden global ptr
+// CHECK: @__start_sanmd_atomics = extern_weak hidden global ptr
+// CHECK: @__stop_sanmd_atomics = extern_weak hidden global ptr
+// CHECK: @llvm.used = appending global [4 x ptr] [ptr @__sanitizer_metadata_covered.module_ctor, ptr @__sanitizer_metadata_covered.module_dtor, ptr @__sanitizer_metadata_atomics.module_ctor, ptr @__sanitizer_metadata_atomics.module_dtor], section "llvm.metadata"
+// CHECK: @llvm.global_ctors = appending global [2 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 2, ptr @__sanitizer_metadata_covered.module_ctor, ptr @__sanitizer_metadata_covered.module_ctor }, { i32, ptr, ptr } { i32 2, ptr @__sanitizer_metadata_atomics.module_ctor, ptr @__sanitizer_metadata_atomics.module_ctor }]
+// CHECK: @llvm.global_dtors = appending global [2 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 2, ptr @__sanitizer_metadata_covered.module_dtor, ptr @__sanitizer_metadata_covered.module_dtor }, { i32, ptr, ptr } { i32 2, ptr @__sanitizer_metadata_atomics.module_dtor, ptr @__sanitizer_metadata_atomics.module_dtor }]
+//.
+// CHECK: Function Attrs: mustprogress nofree noinline norecurse nosync nounwind willreturn memory(write, argmem: none, inaccessiblemem: none)
+// CHECK-LABEL: define dso_local void @escape
+// CHECK-SAME: (ptr noundef [[P:%.*]]) local_unnamed_addr #[[ATTR0:[0-9]+]] !pcsections !2 {
+// CHECK-NEXT: entry:
+// CHECK-NEXT: ret void
+//
+__attribute__((noinline, not_tail_called)) void escape(const volatile void *p) {
+ static const volatile void *sink;
+ sink = p;
+}
+
+// CHECK: Function Attrs: mustprogress nofree norecurse nounwind willreturn memory(write, argmem: readwrite, inaccessiblemem: none)
+// CHECK-LABEL: define dso_local i32 @normal_function
+// CHECK-SAME: (ptr noundef [[X:%.*]], ptr nocapture noundef readonly [[Y:%.*]]) local_unnamed_addr #[[ATTR1:[0-9]+]] !pcsections !4 {
+// CHECK-NEXT: entry:
+// CHECK-NEXT: [[X_ADDR:%.*]] = alloca ptr, align 8
+// CHECK-NEXT: store ptr [[X]], ptr [[X_ADDR]], align 8, !tbaa [[TBAA6:![0-9]+]]
+// CHECK-NEXT: store atomic i32 1, ptr [[X]] monotonic, align 4, !pcsections !10
+// CHECK-NEXT: notail call void @escape(ptr noundef nonnull [[X_ADDR]])
+// CHECK-NEXT: [[TMP0:%.*]] = load i32, ptr [[Y]], align 4, !tbaa [[TBAA11:![0-9]+]]
+// CHECK-NEXT: ret i32 [[TMP0]]
+//
+int normal_function(int *x, int *y) {
+ __atomic_store_n(x, 1, __ATOMIC_RELAXED);
+ escape(&x);
+ return *y;
+}
+
+// CHECK: Function Attrs: disable_sanitizer_instrumentation mustprogress nofree norecurse nounwind willreturn memory(write, argmem: readwrite, inaccessiblemem: none)
+// CHECK-LABEL: define dso_local i32 @test_disable_sanitize_instrumentation
+// CHECK-SAME: (ptr noundef [[X:%.*]], ptr nocapture noundef readonly [[Y:%.*]]) local_unnamed_addr #[[ATTR2:[0-9]+]] {
+// CHECK-NEXT: entry:
+// CHECK-NEXT: [[X_ADDR:%.*]] = alloca ptr, align 8
+// CHECK-NEXT: store ptr [[X]], ptr [[X_ADDR]], align 8, !tbaa [[TBAA6]]
+// CHECK-NEXT: store atomic i32 1, ptr [[X]] monotonic, align 4
+// CHECK-NEXT: notail call void @escape(ptr noundef nonnull [[X_ADDR]])
+// CHECK-NEXT: [[TMP0:%.*]] = load i32, ptr [[Y]], align 4, !tbaa [[TBAA11]]
+// CHECK-NEXT: ret i32 [[TMP0]]
+//
+__attribute__((disable_sanitizer_instrumentation)) int test_disable_sanitize_instrumentation(int *x, int *y) {
+ __atomic_store_n(x, 1, __ATOMIC_RELAXED);
+ escape(&x);
+ return *y;
+}
+
+// CHECK: Function Attrs: mustprogress nofree norecurse nounwind willreturn memory(write, argmem: readwrite, inaccessiblemem: none)
+// CHECK-LABEL: define dso_local i32 @test_no_sanitize_thread
+// CHECK-SAME: (ptr noundef [[X:%.*]], ptr nocapture noundef readonly [[Y:%.*]]) local_unnamed_addr #[[ATTR3:[0-9]+]] !pcsections !13 {
+// CHECK-NEXT: entry:
+// CHECK-NEXT: [[X_ADDR:%.*]] = alloca ptr, align 8
+// CHECK-NEXT: store ptr [[X]], ptr [[X_ADDR]], align 8, !tbaa [[TBAA6]]
+// CHECK-NEXT: store atomic i32 1, ptr [[X]] monotonic, align 4, !pcsections !10
+// CHECK-NEXT: notail call void @escape(ptr noundef nonnull [[X_ADDR]])
+// CHECK-NEXT: [[TMP0:%.*]] = load i32, ptr [[Y]], align 4, !tbaa [[TBAA11]]
+// CHECK-NEXT: ret i32 [[TMP0]]
+//
+__attribute__((no_sanitize("thread"))) int test_no_sanitize_thread(int *x, int *y) {
+ __atomic_store_n(x, 1, __ATOMIC_RELAXED);
+ escape(&x);
+ return *y;
+}
+
+// CHECK: Function Attrs: mustprogress nofree norecurse nounwind willreturn memory(write, argmem: readwrite, inaccessiblemem: none)
+// CHECK-LABEL: define dso_local i32 @test_no_sanitize_all
+// CHECK-SAME: (ptr noundef [[X:%.*]], ptr nocapture noundef readonly [[Y:%.*]]) local_unnamed_addr #[[ATTR3]] !pcsections !13 {
+// CHECK-NEXT: entry:
+// CHECK-NEXT: [[X_ADDR:%.*]] = alloca ptr, align 8
+// CHECK-NEXT: store ptr [[X]], ptr [[X_ADDR]], align 8, !tbaa [[TBAA6]]
+// CHECK-NEXT: store atomic i32 1, ptr [[X]] monotonic, align 4, !pcsections !10
+// CHECK-NEXT: notail call void @escape(ptr noundef nonnull [[X_ADDR]])
+// CHECK-NEXT: [[TMP0:%.*]] = load i32, ptr [[Y]], align 4, !tbaa [[TBAA11]]
+// CHECK-NEXT: ret i32 [[TMP0]]
+//
+__attribute__((no_sanitize("all"))) int test_no_sanitize_all(int *x, int *y) {
+ __atomic_store_n(x, 1, __ATOMIC_RELAXED);
+ escape(&x);
+ return *y;
+}
+//.
+// CHECK: attributes #0 = { mustprogress nofree noinline norecurse nosync nounwind willreturn memory(write, argmem: none, inaccessiblemem: none) "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-features"="+cx8,+mmx,+sse,+sse2,+x87" }
+// CHECK: attributes #1 = { mustprogress nofree norecurse nounwind willreturn memory(write, argmem: readwrite, inaccessiblemem: none) "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-features"="+cx8,+mmx,+sse,+sse2,+x87" }
+// CHECK: attributes #2 = { disable_sanitizer_instrumentation mustprogress nofree norecurse nounwind willreturn memory(write, argmem: readwrite, inaccessiblemem: none) "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-features"="+cx8,+mmx,+sse,+sse2,+x87" }
+// CHECK: attributes #3 = { mustprogress nofree norecurse nounwind willreturn memory(write, argmem: readwrite, inaccessiblemem: none) "min-legal-vector-width"="0" "no-trapping-math"="true" "no_sanitize_thread" "stack-protector-buffer-size"="8" "target-features"="+cx8,+mmx,+sse,+sse2,+x87" }
+// CHECK: attributes #4 = { nounwind }
+//.
+// CHECK: !2 = !{!"sanmd_covered!C", !3}
+// CHECK: !3 = !{i64 0}
+// CHECK: !4 = !{!"sanmd_covered!C", !5}
+// CHECK: !5 = !{i64 3}
+// CHECK: !6 = !{!7, !7, i64 0}
+// CHECK: !7 = !{!"any pointer", !8, i64 0}
+// CHECK: !8 = !{!"omnipotent char", !9, i64 0}
+// CHECK: !9 = !{!"Simple C/C++ TBAA"}
+// CHECK: !10 = !{!"sanmd_atomics!C"}
+// CHECK: !11 = !{!12, !12, i64 0}
+// CHECK: !12 = !{!"int", !8, i64 0}
+// CHECK: !13 = !{!"sanmd_covered!C", !14}
+// CHECK: !14 = !{i64 2}
+//.
diff --git a/llvm/lib/Transforms/Instrumentation/SanitizerBinaryMetadata.cpp b/llvm/lib/Transforms/Instrumentation/SanitizerBinaryMetadata.cpp
index a8ba0b73446e5..d83a3a991c89b 100644
--- a/llvm/lib/Transforms/Instrumentation/SanitizerBinaryMetadata.cpp
+++ b/llvm/lib/Transforms/Instrumentation/SanitizerBinaryMetadata.cpp
@@ -90,6 +90,11 @@ cl::opt<bool> ClWeakCallbacks(
"sanitizer-metadata-weak-callbacks",
cl::desc("Declare callbacks extern weak, and only call if non-null."),
cl::Hidden, cl::init(true));
+cl::opt<bool>
+ ClNoSanitize("sanitizer-metadata-nosanitize-attr",
+ cl::desc("Mark some metadata features uncovered in functions "
+ "with associated no_sanitize attributes."),
+ cl::Hidden, cl::init(true));
cl::opt<bool> ClEmitCovered("sanitizer-metadata-covered",
cl::desc("Emit PCs for covered functions."),
@@ -268,6 +273,8 @@ void SanitizerBinaryMetadata::runOn(Function &F, MetadataInfoSet &MIS) {
RequiresCovered |= runOn(I, MIS, MDB, FeatureMask);
}
+ if (ClNoSanitize && F.hasFnAttribute("no_sanitize_thread"))
+ FeatureMask &= ~kSanitizerBinaryMetadataAtomics;
if (F.isVarArg())
FeatureMask &= ~kSanitizerBinaryMetadataUAR;
if (FeatureMask & kSanitizerBinaryMetadataUAR) {
More information about the cfe-commits
mailing list