melver created this revision. melver added a reviewer: dvyukov. Herald added subscribers: Enna1, hiraditya. Herald added a project: All. melver requested review of this revision. Herald added projects: clang, LLVM. Herald added subscribers: llvm-commits, cfe-commits.
To avoid false positives, respect no_sanitize("thread") when atomics metadata is enabled. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D148694 Files: clang/lib/CodeGen/CodeGenFunction.cpp clang/test/CodeGen/sanitize-metadata-nosanitize.c llvm/lib/Transforms/Instrumentation/SanitizerBinaryMetadata.cpp
Index: llvm/lib/Transforms/Instrumentation/SanitizerBinaryMetadata.cpp =================================================================== --- llvm/lib/Transforms/Instrumentation/SanitizerBinaryMetadata.cpp +++ llvm/lib/Transforms/Instrumentation/SanitizerBinaryMetadata.cpp @@ -90,6 +90,11 @@ "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 @@ RequiresCovered |= runOn(I, MIS, MDB, FeatureMask); } + if (ClNoSanitize && F.hasFnAttribute("no_sanitize_thread")) + FeatureMask &= ~kSanitizerBinaryMetadataAtomics; if (F.isVarArg()) FeatureMask &= ~kSanitizerBinaryMetadataUAR; if (FeatureMask & kSanitizerBinaryMetadataUAR) { Index: clang/test/CodeGen/sanitize-metadata-nosanitize.c =================================================================== --- /dev/null +++ clang/test/CodeGen/sanitize-metadata-nosanitize.c @@ -0,0 +1,113 @@ +// 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: !0 = !{i32 1, !"wchar_size", i32 4} +// CHECK: !1 = !{!"clang version 17.0.0 (https://github.com/llvm/llvm-project.git a5306da3a31f76bcf967beaa0f58b9fb5a9d78de)"} +// 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} +//. Index: clang/lib/CodeGen/CodeGenFunction.cpp =================================================================== --- clang/lib/CodeGen/CodeGenFunction.cpp +++ clang/lib/CodeGen/CodeGenFunction.cpp @@ -730,31 +730,38 @@ 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()) {
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits