Skip to content

Commit 693a1b7

Browse files
committed
[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
1 parent 514dddb commit 693a1b7

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed

llvm/lib/Transforms/Instrumentation/GCOVProfiling.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -919,15 +919,21 @@ bool GCOVProfiler::emitProfileNotes(
919919
IRBuilder<> Builder(E.Place, E.Place->getFirstInsertionPt());
920920
Value *V = Builder.CreateConstInBoundsGEP2_64(
921921
Counters->getValueType(), Counters, 0, I);
922+
// Disable sanitizers to decrease size bloat. We don't expect
923+
// sanitizers to catch interesting issues.
924+
Instruction *Inst;
922925
if (Options.Atomic) {
923-
Builder.CreateAtomicRMW(AtomicRMWInst::Add, V, Builder.getInt64(1),
924-
MaybeAlign(), AtomicOrdering::Monotonic);
926+
Inst = Builder.CreateAtomicRMW(AtomicRMWInst::Add, V,
927+
Builder.getInt64(1), MaybeAlign(),
928+
AtomicOrdering::Monotonic);
925929
} else {
926-
Value *Count =
930+
LoadInst *OldCount =
927931
Builder.CreateLoad(Builder.getInt64Ty(), V, "gcov_ctr");
928-
Count = Builder.CreateAdd(Count, Builder.getInt64(1));
929-
Builder.CreateStore(Count, V);
932+
OldCount->setNoSanitizeMetadata();
933+
Value *NewCount = Builder.CreateAdd(OldCount, Builder.getInt64(1));
934+
Inst = Builder.CreateStore(NewCount, V);
930935
}
936+
Inst->setNoSanitizeMetadata();
931937
}
932938
}
933939
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
;; Ensure that the loads, stores, and atomicrmw adds for gcov have nosanitize metadata.
2+
; RUN: rm -rf %t && mkdir %t && cd %t
3+
; RUN: opt < %s -S -passes=insert-gcov-profiling | FileCheck %s
4+
; RUN: opt < %s -S -passes=insert-gcov-profiling -gcov-atomic-counter | FileCheck %s --check-prefixes=CHECK-ATOMIC-COUNTER
5+
6+
; CHECK-LABEL: void @empty()
7+
; CHECK-NEXT: entry:
8+
; CHECK-NEXT: %gcov_ctr = load i64, ptr @__llvm_gcov_ctr, align 4, !dbg [[DBG:![0-9]+]], !nosanitize [[NOSANITIZE:![0-9]+]]
9+
; CHECK-NEXT: %0 = add i64 %gcov_ctr, 1, !dbg [[DBG]]
10+
; CHECK-NEXT: store i64 %0, ptr @__llvm_gcov_ctr, align 4, !dbg [[DBG]], !nosanitize [[NOSANITIZE]]
11+
; CHECK-NEXT: ret void, !dbg [[DBG]]
12+
13+
; CHECK-ATOMIC-COUNTER-LABEL: void @empty()
14+
; CHECK-ATOMIC-COUNTER-NEXT: entry:
15+
; CHECK-ATOMIC-COUNTER-NEXT: %0 = atomicrmw add ptr @__llvm_gcov_ctr, i64 1 monotonic, align 8, !dbg [[DBG:![0-9]+]], !nosanitize [[NOSANITIZE:![0-9]+]]
16+
; CHECK-ATOMIC-COUNTER-NEXT: ret void, !dbg [[DBG]]
17+
18+
define dso_local void @empty() !dbg !5 {
19+
entry:
20+
ret void, !dbg !8
21+
}
22+
23+
!llvm.dbg.cu = !{!0}
24+
!llvm.module.flags = !{!3, !4}
25+
26+
!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, emissionKind: FullDebug, enums: !2)
27+
!1 = !DIFile(filename: "a.c", directory: "")
28+
!2 = !{}
29+
!3 = !{i32 7, !"Dwarf Version", i32 5}
30+
!4 = !{i32 2, !"Debug Info Version", i32 3}
31+
!5 = distinct !DISubprogram(name: "empty", scope: !1, file: !1, line: 1, type: !6, scopeLine: 1, flags: DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !2)
32+
!6 = !DISubroutineType(types: !7)
33+
!7 = !{null}
34+
!8 = !DILocation(line: 2, column: 1, scope: !5)

0 commit comments

Comments
 (0)