[llvm] 693a1b7 - [gcov] Add nosanitize metadata to memory access instructions inserted by emitProfileNotes()

via llvm-commits llvm-commits at lists.llvm.org
Wed May 24 19:15:07 PDT 2023


Author: Enna1
Date: 2023-05-25T10:13:52+08:00
New Revision: 693a1b7024b81655760e209b73b3ef339e161d76

URL: https://github.com/llvm/llvm-project/commit/693a1b7024b81655760e209b73b3ef339e161d76
DIFF: https://github.com/llvm/llvm-project/commit/693a1b7024b81655760e209b73b3ef339e161d76.diff

LOG: [gcov] Add nosanitize metadata to memory access instructions inserted by emitProfileNotes()

This patch adds nosantize metadata to memory access instructions inserted by gcov emitProfileNotes(), making sanitizers skip these instructions when gcov and sanitizer are used together.

Reviewed By: nickdesaulniers

Differential Revision: https://reviews.llvm.org/D150460

Added: 
    llvm/test/Transforms/GCOVProfiling/nosanitize.ll

Modified: 
    llvm/lib/Transforms/Instrumentation/GCOVProfiling.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Instrumentation/GCOVProfiling.cpp b/llvm/lib/Transforms/Instrumentation/GCOVProfiling.cpp
index acb9e8e3a775b..21f0b1a922938 100644
--- a/llvm/lib/Transforms/Instrumentation/GCOVProfiling.cpp
+++ b/llvm/lib/Transforms/Instrumentation/GCOVProfiling.cpp
@@ -919,15 +919,21 @@ bool GCOVProfiler::emitProfileNotes(
           IRBuilder<> Builder(E.Place, E.Place->getFirstInsertionPt());
           Value *V = Builder.CreateConstInBoundsGEP2_64(
               Counters->getValueType(), Counters, 0, I);
+          // Disable sanitizers to decrease size bloat. We don't expect
+          // sanitizers to catch interesting issues.
+          Instruction *Inst;
           if (Options.Atomic) {
-            Builder.CreateAtomicRMW(AtomicRMWInst::Add, V, Builder.getInt64(1),
-                                    MaybeAlign(), AtomicOrdering::Monotonic);
+            Inst = Builder.CreateAtomicRMW(AtomicRMWInst::Add, V,
+                                           Builder.getInt64(1), MaybeAlign(),
+                                           AtomicOrdering::Monotonic);
           } else {
-            Value *Count =
+            LoadInst *OldCount =
                 Builder.CreateLoad(Builder.getInt64Ty(), V, "gcov_ctr");
-            Count = Builder.CreateAdd(Count, Builder.getInt64(1));
-            Builder.CreateStore(Count, V);
+            OldCount->setNoSanitizeMetadata();
+            Value *NewCount = Builder.CreateAdd(OldCount, Builder.getInt64(1));
+            Inst = Builder.CreateStore(NewCount, V);
           }
+          Inst->setNoSanitizeMetadata();
         }
       }
     }

diff  --git a/llvm/test/Transforms/GCOVProfiling/nosanitize.ll b/llvm/test/Transforms/GCOVProfiling/nosanitize.ll
new file mode 100644
index 0000000000000..4694b95b99d4a
--- /dev/null
+++ b/llvm/test/Transforms/GCOVProfiling/nosanitize.ll
@@ -0,0 +1,34 @@
+;; Ensure that the loads, stores, and atomicrmw adds for gcov have nosanitize metadata.
+; RUN: rm -rf %t && mkdir %t && cd %t
+; RUN: opt < %s -S -passes=insert-gcov-profiling | FileCheck %s
+; RUN: opt < %s -S -passes=insert-gcov-profiling -gcov-atomic-counter | FileCheck %s --check-prefixes=CHECK-ATOMIC-COUNTER
+
+; CHECK-LABEL: void @empty()
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    %gcov_ctr = load i64, ptr @__llvm_gcov_ctr, align 4, !dbg [[DBG:![0-9]+]], !nosanitize [[NOSANITIZE:![0-9]+]]
+; CHECK-NEXT:    %0 = add i64 %gcov_ctr, 1, !dbg [[DBG]]
+; CHECK-NEXT:    store i64 %0, ptr @__llvm_gcov_ctr, align 4, !dbg [[DBG]], !nosanitize [[NOSANITIZE]]
+; CHECK-NEXT:    ret void, !dbg [[DBG]]
+
+; CHECK-ATOMIC-COUNTER-LABEL: void @empty()
+; CHECK-ATOMIC-COUNTER-NEXT:  entry:
+; CHECK-ATOMIC-COUNTER-NEXT:    %0 = atomicrmw add ptr @__llvm_gcov_ctr, i64 1 monotonic, align 8, !dbg [[DBG:![0-9]+]], !nosanitize [[NOSANITIZE:![0-9]+]]
+; CHECK-ATOMIC-COUNTER-NEXT:    ret void, !dbg [[DBG]]
+
+define dso_local void @empty() !dbg !5 {
+entry:
+  ret void, !dbg !8
+}
+
+!llvm.dbg.cu = !{!0}
+!llvm.module.flags = !{!3, !4}
+
+!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, emissionKind: FullDebug, enums: !2)
+!1 = !DIFile(filename: "a.c", directory: "")
+!2 = !{}
+!3 = !{i32 7, !"Dwarf Version", i32 5}
+!4 = !{i32 2, !"Debug Info Version", i32 3}
+!5 = distinct !DISubprogram(name: "empty", scope: !1, file: !1, line: 1, type: !6, scopeLine: 1, flags: DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !2)
+!6 = !DISubroutineType(types: !7)
+!7 = !{null}
+!8 = !DILocation(line: 2, column: 1, scope: !5)


        


More information about the llvm-commits mailing list