Skip to content

Commit 7ab44b5

Browse files
[msan] Allow KMSAN to use -fsanitize-memory-param-retval
Let -fsanitize-memory-param-retval be used together with -fsanitize=kernel-memory, so that it can be applied when building the Linux kernel. Also add clang/test/CodeGen/kmsan-param-retval.c to ensure that -fsanitize-memory-param-retval eliminates shadow accesses for parameters marked as undef. Reviewed By: eugenis, vitalybuka Differential Revision: https://reviews.llvm.org/D127860
1 parent 2d9c891 commit 7ab44b5

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

clang/lib/Driver/SanitizerArgs.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,11 @@ SanitizerArgs::SanitizerArgs(const ToolChain &TC,
648648
options::OPT_fno_sanitize_memory_param_retval, MsanParamRetval);
649649
NeedPIE |= !(TC.getTriple().isOSLinux() &&
650650
TC.getTriple().getArch() == llvm::Triple::x86_64);
651+
} else if (AllAddedKinds & SanitizerKind::KernelMemory) {
652+
MsanUseAfterDtor = false;
653+
MsanParamRetval = Args.hasFlag(
654+
options::OPT_fsanitize_memory_param_retval,
655+
options::OPT_fno_sanitize_memory_param_retval, MsanParamRetval);
651656
} else {
652657
MsanUseAfterDtor = false;
653658
MsanParamRetval = false;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// RUN: %clang_cc1 -triple x86_64-linux-gnu -S -emit-llvm -O2 -fsanitize=kernel-memory -no-enable-noundef-analysis -o - %s | \
2+
// RUN: FileCheck %s --check-prefix=CLEAN
3+
// RUN: %clang_cc1 -triple x86_64-linux-gnu -S -emit-llvm -O2 -fsanitize=kernel-memory -o - %s | \
4+
// RUN: FileCheck %s --check-prefixes=NOUNDEF,NOUNDEF_ONLY
5+
// RUN: %clang_cc1 -triple x86_64-linux-gnu -S -emit-llvm -O2 -fsanitize=kernel-memory -mllvm -msan-eager-checks -o - %s | \
6+
// RUN: FileCheck %s --check-prefixes=NOUNDEF,EAGER
7+
// RUN: %clang_cc1 -triple x86_64-linux-gnu -S -emit-llvm -O2 -fsanitize=kernel-memory -no-enable-noundef-analysis -fsanitize-memory-param-retval -o - %s | \
8+
// RUN: FileCheck %s --check-prefixes=CLEAN
9+
// RUN: %clang_cc1 -triple x86_64-linux-gnu -S -emit-llvm -O2 -fsanitize=kernel-memory -fsanitize-memory-param-retval -o - %s | \
10+
// RUN: FileCheck %s --check-prefixes=NOUNDEF,EAGER
11+
12+
void foo();
13+
14+
void bar(int x) {
15+
if (x)
16+
foo();
17+
}
18+
19+
20+
// CLEAN: define dso_local void @bar(i32 %x)
21+
// NOUNDEF: define dso_local void @bar(i32 noundef %x)
22+
//
23+
// %param_shadow assignment gets optimized away with -O2, because it is at the beginning of the
24+
// struct returned by __msan_get_context_state(). Use %param_origin as a sign that the shadow of
25+
// the first argument is being used.
26+
//
27+
// Without noundef analysis, KMSAN emits metadata checks for the function parameter.
28+
// CLEAN: load i32, ptr %param_origin
29+
//
30+
// With noundef analysis enabled, but without eager checks, KMSAN still emits metadata checks,
31+
// although the parameter is known to be defined.
32+
// NOUNDEF_ONLY: load i32, ptr %param_origin
33+
//
34+
// With noundef analysis and eager checks enabled, KMSAN won't emit metadata checks for function
35+
// parameters.
36+
// EAGER-NOT: load i32, ptr %param_origin

clang/test/Driver/fsanitize-memory-param-retval.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// RUN: %clang -target aarch64-linux-gnu %s -fsanitize=memory -fsanitize-memory-param-retval -c -### 2>&1 | FileCheck %s
44
// RUN: %clang -target riscv32-linux-gnu %s -fsanitize=memory -fsanitize-memory-param-retval -c -### 2>&1 | FileCheck %s
55
// RUN: %clang -target riscv64-linux-gnu %s -fsanitize=memory -fsanitize-memory-param-retval -c -### 2>&1 | FileCheck %s
6+
// RUN: %clang -target x86_64-linux-gnu %s -fsanitize=kernel-memory -fsanitize-memory-param-retval -c -### 2>&1 | FileCheck %s
7+
68
// CHECK: "-fsanitize-memory-param-retval"
79

810
// RUN: %clang -target aarch64-linux-gnu -fsyntax-only %s -fsanitize=memory -fsanitize-memory-param-retval -c -### 2>&1 | FileCheck --check-prefix=11 %s

0 commit comments

Comments
 (0)