[PATCH] D150460: [gcov] Add nosanitize metadata to memory access instructions inserted by emitProfileNotes()
Mingjie Xu via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Fri May 12 10:50:10 PDT 2023
Enna1 created this revision.
Herald added a subscriber: hiraditya.
Herald added a project: All.
Enna1 added reviewers: MaskRay, nickdesaulniers.
Enna1 added a subscriber: MTC.
Enna1 published this revision for review.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
This patch adds nosantize metadata to memory access instructions inserted by gcov emitProfileNotes(), making sanitizers skip these instructions when gocv and sanitizer are used together.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D150460
Files:
llvm/lib/Transforms/Instrumentation/GCOVProfiling.cpp
llvm/test/Transforms/GCOVProfiling/nosanitize.ll
Index: llvm/test/Transforms/GCOVProfiling/nosanitize.ll
===================================================================
--- /dev/null
+++ llvm/test/Transforms/GCOVProfiling/nosanitize.ll
@@ -0,0 +1,33 @@
+; RUN: mkdir -p %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)
Index: llvm/lib/Transforms/Instrumentation/GCOVProfiling.cpp
===================================================================
--- llvm/lib/Transforms/Instrumentation/GCOVProfiling.cpp
+++ llvm/lib/Transforms/Instrumentation/GCOVProfiling.cpp
@@ -147,6 +147,10 @@
enum class GCovFileType { GCNO, GCDA };
std::string mangleName(const DICompileUnit *CU, GCovFileType FileType);
+ void setNoSanitizeMetadata(Instruction *I) {
+ I->setMetadata(LLVMContext::MD_nosanitize, MDNode::get(*Ctx, std::nullopt));
+ }
+
GCOVOptions Options;
support::endianness Endian;
raw_ostream *os;
@@ -920,13 +924,17 @@
Value *V = Builder.CreateConstInBoundsGEP2_64(
Counters->getValueType(), Counters, 0, I);
if (Options.Atomic) {
- Builder.CreateAtomicRMW(AtomicRMWInst::Add, V, Builder.getInt64(1),
- MaybeAlign(), AtomicOrdering::Monotonic);
+ auto *RMW = Builder.CreateAtomicRMW(
+ AtomicRMWInst::Add, V, Builder.getInt64(1), MaybeAlign(),
+ AtomicOrdering::Monotonic);
+ setNoSanitizeMetadata(RMW);
} else {
- Value *Count =
+ auto *OldCount =
Builder.CreateLoad(Builder.getInt64Ty(), V, "gcov_ctr");
- Count = Builder.CreateAdd(Count, Builder.getInt64(1));
- Builder.CreateStore(Count, V);
+ auto *NewCount = Builder.CreateAdd(OldCount, Builder.getInt64(1));
+ auto *Store = Builder.CreateStore(NewCount, V);
+ setNoSanitizeMetadata(OldCount);
+ setNoSanitizeMetadata(Store);
}
}
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D150460.521699.patch
Type: text/x-patch
Size: 3457 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230512/f81dd850/attachment-0001.bin>
More information about the llvm-commits
mailing list