[PATCH] D127860: [msan] Allow KMSAN to use -fsanitize-memory-param-retval

Alexander Potapenko via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Jun 17 01:54:26 PDT 2022


This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG7ab44b5c2155: [msan] Allow KMSAN to use -fsanitize-memory-param-retval (authored by glider).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D127860/new/

https://reviews.llvm.org/D127860

Files:
  clang/lib/Driver/SanitizerArgs.cpp
  clang/test/CodeGen/kmsan-param-retval.c
  clang/test/Driver/fsanitize-memory-param-retval.c


Index: clang/test/Driver/fsanitize-memory-param-retval.c
===================================================================
--- clang/test/Driver/fsanitize-memory-param-retval.c
+++ clang/test/Driver/fsanitize-memory-param-retval.c
@@ -3,6 +3,8 @@
 // RUN: %clang -target aarch64-linux-gnu %s -fsanitize=memory -fsanitize-memory-param-retval -c -### 2>&1 | FileCheck %s
 // RUN: %clang -target riscv32-linux-gnu %s -fsanitize=memory -fsanitize-memory-param-retval -c -### 2>&1 | FileCheck %s
 // RUN: %clang -target riscv64-linux-gnu %s -fsanitize=memory -fsanitize-memory-param-retval -c -### 2>&1 | FileCheck %s
+// RUN: %clang -target x86_64-linux-gnu %s -fsanitize=kernel-memory -fsanitize-memory-param-retval -c -### 2>&1 | FileCheck %s
+
 // CHECK: "-fsanitize-memory-param-retval"
 
 // RUN: %clang -target aarch64-linux-gnu -fsyntax-only %s -fsanitize=memory -fsanitize-memory-param-retval -c -### 2>&1 | FileCheck --check-prefix=11 %s
Index: clang/test/CodeGen/kmsan-param-retval.c
===================================================================
--- /dev/null
+++ clang/test/CodeGen/kmsan-param-retval.c
@@ -0,0 +1,36 @@
+// RUN: %clang_cc1 -triple x86_64-linux-gnu -S -emit-llvm -O2 -fsanitize=kernel-memory -no-enable-noundef-analysis -o - %s | \
+// RUN:     FileCheck %s --check-prefix=CLEAN
+// RUN: %clang_cc1 -triple x86_64-linux-gnu -S -emit-llvm -O2 -fsanitize=kernel-memory -o - %s | \
+// RUN:     FileCheck %s --check-prefixes=NOUNDEF,NOUNDEF_ONLY
+// RUN: %clang_cc1 -triple x86_64-linux-gnu -S -emit-llvm -O2 -fsanitize=kernel-memory -mllvm -msan-eager-checks -o - %s | \
+// RUN:     FileCheck %s --check-prefixes=NOUNDEF,EAGER
+// 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 | \
+// RUN:     FileCheck %s --check-prefixes=CLEAN
+// RUN: %clang_cc1 -triple x86_64-linux-gnu -S -emit-llvm -O2 -fsanitize=kernel-memory -fsanitize-memory-param-retval -o - %s | \
+// RUN:     FileCheck %s --check-prefixes=NOUNDEF,EAGER
+
+void foo();
+
+void bar(int x) {
+  if (x)
+    foo();
+}
+
+
+// CLEAN:   define dso_local void @bar(i32 %x)
+// NOUNDEF: define dso_local void @bar(i32 noundef %x)
+//
+// %param_shadow assignment gets optimized away with -O2, because it is at the beginning of the
+// struct returned by __msan_get_context_state(). Use %param_origin as a sign that the shadow of
+// the first argument is being used.
+//
+// Without noundef analysis, KMSAN emits metadata checks for the function parameter.
+// CLEAN:        load i32, ptr %param_origin
+//
+// With noundef analysis enabled, but without eager checks, KMSAN still emits metadata checks,
+// although the parameter is known to be defined.
+// NOUNDEF_ONLY: load i32, ptr %param_origin
+//
+// With noundef analysis and eager checks enabled, KMSAN won't emit metadata checks for function
+// parameters.
+// EAGER-NOT:    load i32, ptr %param_origin
Index: clang/lib/Driver/SanitizerArgs.cpp
===================================================================
--- clang/lib/Driver/SanitizerArgs.cpp
+++ clang/lib/Driver/SanitizerArgs.cpp
@@ -648,6 +648,11 @@
         options::OPT_fno_sanitize_memory_param_retval, MsanParamRetval);
     NeedPIE |= !(TC.getTriple().isOSLinux() &&
                  TC.getTriple().getArch() == llvm::Triple::x86_64);
+  } else if (AllAddedKinds & SanitizerKind::KernelMemory) {
+    MsanUseAfterDtor = false;
+    MsanParamRetval = Args.hasFlag(
+        options::OPT_fsanitize_memory_param_retval,
+        options::OPT_fno_sanitize_memory_param_retval, MsanParamRetval);
   } else {
     MsanUseAfterDtor = false;
     MsanParamRetval = false;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D127860.437818.patch
Type: text/x-patch
Size: 3685 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220617/c961192c/attachment.bin>


More information about the cfe-commits mailing list