Skip to content

Commit 45726c1

Browse files
[LLVM] Make sanitizers respect the disable_santizer_instrumentation attribute. (#91732)
`disable_sanitizer_instrumetation` is attached to functions that shall not be instrumented e.g. ifunc resolver because those run before everything is initialised. Some sanitizer already handles this attribute, this patch adds it to DataFLow and Coverage too.
1 parent d6ee7e8 commit 45726c1

File tree

4 files changed

+97
-1
lines changed

4 files changed

+97
-1
lines changed

llvm/lib/Transforms/Instrumentation/DataFlowSanitizer.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -1546,7 +1546,8 @@ bool DataFlowSanitizer::runImpl(
15461546
SmallPtrSet<Constant *, 1> PersonalityFns;
15471547
for (Function &F : M)
15481548
if (!F.isIntrinsic() && !DFSanRuntimeFunctions.contains(&F) &&
1549-
!LibAtomicFunction(F)) {
1549+
!LibAtomicFunction(F) &&
1550+
!F.hasFnAttribute(Attribute::DisableSanitizerInstrumentation)) {
15501551
FnsToInstrument.push_back(&F);
15511552
if (F.hasPersonalityFn())
15521553
PersonalityFns.insert(F.getPersonalityFn()->stripPointerCasts());

llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,8 @@ void ModuleSanitizerCoverage::instrumentFunction(Function &F) {
631631
return;
632632
if (F.hasFnAttribute(Attribute::NoSanitizeCoverage))
633633
return;
634+
if (F.hasFnAttribute(Attribute::DisableSanitizerInstrumentation))
635+
return;
634636
if (Options.CoverageType >= SanitizerCoverageOptions::SCK_Edge) {
635637
SplitAllCriticalEdges(
636638
F, CriticalEdgeSplittingOptions().setIgnoreUnreachableDests());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2+
; This test checks that we are not instrumenting sanitizer code.
3+
; RUN: opt < %s -passes='module(msan)' -S | FileCheck %s
4+
5+
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
6+
target triple = "x86_64-unknown-linux-gnu"
7+
8+
; Function with sanitize_memory is instrumented.
9+
; Function Attrs: nounwind uwtable
10+
define void @instr_sa(ptr %a) sanitize_memory {
11+
entry:
12+
%tmp1 = load i32, ptr %a, align 4
13+
%tmp2 = add i32 %tmp1, 1
14+
store i32 %tmp2, ptr %a, align 4
15+
ret void
16+
}
17+
18+
; CHECK-LABEL: @instr_sa
19+
; CHECK: %0 = load i64, ptr @__msan_param_tls
20+
21+
22+
; Function with disable_sanitizer_instrumentation is not instrumented.
23+
; Function Attrs: nounwind uwtable
24+
define void @noinstr_dsi(ptr %a) disable_sanitizer_instrumentation {
25+
entry:
26+
%tmp1 = load i32, ptr %a, align 4
27+
%tmp2 = add i32 %tmp1, 1
28+
store i32 %tmp2, ptr %a, align 4
29+
ret void
30+
}
31+
32+
; CHECK-LABEL: @noinstr_dsi
33+
; CHECK-NOT: %0 = load i64, ptr @__msan_param_tls
34+
35+
36+
; disable_sanitizer_instrumentation takes precedence over sanitize_memory.
37+
; Function Attrs: nounwind uwtable
38+
define void @noinstr_dsi_sa(ptr %a) disable_sanitizer_instrumentation sanitize_memory {
39+
entry:
40+
%tmp1 = load i32, ptr %a, align 4
41+
%tmp2 = add i32 %tmp1, 1
42+
store i32 %tmp2, ptr %a, align 4
43+
ret void
44+
}
45+
46+
; CHECK-LABEL: @noinstr_dsi_sa
47+
; CHECK-NOT: %0 = load i64, ptr @__msan_param_tls
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
; This test checks that we are not instrumenting sanitizer code.
2+
; RUN: opt < %s -passes='module(sancov-module)' -sanitizer-coverage-level=3 -sanitizer-coverage-control-flow -S | FileCheck %s
3+
4+
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
5+
target triple = "x86_64-unknown-linux-gnu"
6+
7+
; Function with sanitize_address is instrumented.
8+
; Function Attrs: nounwind uwtable
9+
define void @instr_sa(ptr %a) sanitize_address {
10+
entry:
11+
%tmp1 = load i32, ptr %a, align 4
12+
%tmp2 = add i32 %tmp1, 1
13+
store i32 %tmp2, ptr %a, align 4
14+
ret void
15+
}
16+
17+
; CHECK-LABEL: @instr_sa
18+
; CHECK: call void @__sanitizer_cov_trace_pc_guard(
19+
20+
21+
; Function with disable_sanitizer_instrumentation is not instrumented.
22+
; Function Attrs: nounwind uwtable
23+
define void @noinstr_dsi(ptr %a) disable_sanitizer_instrumentation {
24+
entry:
25+
%tmp1 = load i32, ptr %a, align 4
26+
%tmp2 = add i32 %tmp1, 1
27+
store i32 %tmp2, ptr %a, align 4
28+
ret void
29+
}
30+
31+
; CHECK-LABEL: @noinstr_dsi
32+
; CHECK-NOT: call void @__sanitizer_cov_trace_pc_guard(
33+
34+
35+
; disable_sanitizer_instrumentation takes precedence over sanitize_address.
36+
; Function Attrs: nounwind uwtable
37+
define void @noinstr_dsi_sa(ptr %a) disable_sanitizer_instrumentation sanitize_address {
38+
entry:
39+
%tmp1 = load i32, ptr %a, align 4
40+
%tmp2 = add i32 %tmp1, 1
41+
store i32 %tmp2, ptr %a, align 4
42+
ret void
43+
}
44+
45+
; CHECK-LABEL: @noinstr_dsi_sa
46+
; CHECK-NOT: call void @__sanitizer_cov_trace_pc_guard(

0 commit comments

Comments
 (0)