[PATCH] D96328: [Clang, NewPM] Add KMSan support

Vitaly Buka via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Feb 9 03:27:05 PST 2021


vitalybuka created this revision.
vitalybuka added reviewers: glider, eugenis.
vitalybuka requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Depends on D96320 <https://reviews.llvm.org/D96320>.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D96328

Files:
  clang/lib/CodeGen/BackendUtil.cpp
  clang/test/Driver/msan.c


Index: clang/test/Driver/msan.c
===================================================================
--- clang/test/Driver/msan.c
+++ clang/test/Driver/msan.c
@@ -36,14 +36,14 @@
 // RUN: %clang     -fexperimental-new-pass-manager -target x86_64-unknown-linux -fsanitize=memory %s -S -emit-llvm -flto -o - | FileCheck %s --check-prefix=CHECK-MSAN
 // RUN: %clang -O2 -fexperimental-new-pass-manager -target x86_64-unknown-linux -fsanitize=memory %s -S -emit-llvm -flto -o - | FileCheck %s --check-prefix=CHECK-MSAN
 
-// FIX: %clang     -fexperimental-new-pass-manager -target x86_64-unknown-linux -fsanitize=kernel-memory  %s -S -emit-llvm -o - | FileCheck %s --check-prefix=CHECK-KMSAN
-// FIX: %clang -O1 -fexperimental-new-pass-manager -target x86_64-unknown-linux -fsanitize=kernel-memory  %s -S -emit-llvm -o - | FileCheck %s --check-prefix=CHECK-KMSAN
-// FIX: %clang -O2 -fexperimental-new-pass-manager -target x86_64-unknown-linux -fsanitize=kernel-memory  %s -S -emit-llvm -o - | FileCheck %s --check-prefix=CHECK-KMSAN
-// FIX: %clang -O3 -fexperimental-new-pass-manager -target x86_64-unknown-linux -fsanitize=kernel-memory  %s -S -emit-llvm -o - | FileCheck %s --check-prefix=CHECK-KMSAN
-// FIX: %clang     -fexperimental-new-pass-manager -target x86_64-unknown-linux -fsanitize=kernel-memory %s -S -emit-llvm -flto=thin -o - | FileCheck %s --check-prefix=CHECK-KMSAN
+// RUN: %clang     -fexperimental-new-pass-manager -target x86_64-unknown-linux -fsanitize=kernel-memory  %s -S -emit-llvm -o - | FileCheck %s --check-prefix=CHECK-KMSAN
+// RUN: %clang -O1 -fexperimental-new-pass-manager -target x86_64-unknown-linux -fsanitize=kernel-memory  %s -S -emit-llvm -o - | FileCheck %s --check-prefix=CHECK-KMSAN
+// RUN: %clang -O2 -fexperimental-new-pass-manager -target x86_64-unknown-linux -fsanitize=kernel-memory  %s -S -emit-llvm -o - | FileCheck %s --check-prefix=CHECK-KMSAN
+// RUN: %clang -O3 -fexperimental-new-pass-manager -target x86_64-unknown-linux -fsanitize=kernel-memory  %s -S -emit-llvm -o - | FileCheck %s --check-prefix=CHECK-KMSAN
+// RUN: %clang     -fexperimental-new-pass-manager -target x86_64-unknown-linux -fsanitize=kernel-memory %s -S -emit-llvm -flto=thin -o - | FileCheck %s --check-prefix=CHECK-KMSAN
 // FIX: %clang -O2 -fexperimental-new-pass-manager -target x86_64-unknown-linux -fsanitize=kernel-memory %s -S -emit-llvm -flto=thin -o - | FileCheck %s --check-prefix=CHECK-KMSAN
-// FIX: %clang     -fexperimental-new-pass-manager -target x86_64-unknown-linux -fsanitize=kernel-memory %s -S -emit-llvm -flto -o - | FileCheck %s --check-prefix=CHECK-KMSAN
-// FIX: %clang -O2 -fexperimental-new-pass-manager -target x86_64-unknown-linux -fsanitize=kernel-memory %s -S -emit-llvm -flto -o - | FileCheck %s --check-prefix=CHECK-KMSAN
+// RUN: %clang     -fexperimental-new-pass-manager -target x86_64-unknown-linux -fsanitize=kernel-memory %s -S -emit-llvm -flto -o - | FileCheck %s --check-prefix=CHECK-KMSAN
+// RUN: %clang -O2 -fexperimental-new-pass-manager -target x86_64-unknown-linux -fsanitize=kernel-memory %s -S -emit-llvm -flto -o - | FileCheck %s --check-prefix=CHECK-KMSAN
 
 int foo(int *a) { return *a; }
 // CHECK-MSAN: __msan_init
Index: clang/lib/CodeGen/BackendUtil.cpp
===================================================================
--- clang/lib/CodeGen/BackendUtil.cpp
+++ clang/lib/CodeGen/BackendUtil.cpp
@@ -1269,17 +1269,25 @@
             });
       }
 
-      if (LangOpts.Sanitize.has(SanitizerKind::Memory)) {
-        int TrackOrigins = CodeGenOpts.SanitizeMemoryTrackOrigins;
-        bool Recover = CodeGenOpts.SanitizeRecover.has(SanitizerKind::Memory);
-        PB.registerOptimizerLastEPCallback(
-            [TrackOrigins, Recover](ModulePassManager &MPM,
-                                    PassBuilder::OptimizationLevel Level) {
-              MPM.addPass(MemorySanitizerPass({TrackOrigins, Recover, false}));
-              MPM.addPass(createModuleToFunctionPassAdaptor(
-                  MemorySanitizerPass({TrackOrigins, Recover, false})));
-            });
-      }
+      auto MSanPass = [&](SanitizerMask Mask, bool CompileKernel) {
+        if (LangOpts.Sanitize.has(Mask)) {
+          int TrackOrigins = CodeGenOpts.SanitizeMemoryTrackOrigins;
+          bool Recover = CodeGenOpts.SanitizeRecover.has(Mask);
+          PB.registerOptimizerLastEPCallback(
+              [CompileKernel, TrackOrigins,
+               Recover](ModulePassManager &MPM,
+                        PassBuilder::OptimizationLevel Level) {
+                MPM.addPass(MemorySanitizerPass(
+                    {TrackOrigins, Recover, CompileKernel}));
+                MPM.addPass(
+                    createModuleToFunctionPassAdaptor(MemorySanitizerPass(
+                        {TrackOrigins, Recover, CompileKernel})));
+              });
+        }
+      };
+      MSanPass(SanitizerKind::Memory, false);
+      MSanPass(SanitizerKind::KernelMemory, true);
+
       if (LangOpts.Sanitize.has(SanitizerKind::Thread)) {
         PB.registerOptimizerLastEPCallback(
             [](ModulePassManager &MPM, PassBuilder::OptimizationLevel Level) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D96328.322329.patch
Type: text/x-patch
Size: 5129 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210209/69b2d3c0/attachment.bin>


More information about the cfe-commits mailing list