[PATCH] D115236: [msan] Implement -msan-no-sanitize-whole-file.

Alexander Potapenko via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 7 04:32:29 PST 2021


glider created this revision.
glider added reviewers: eugenis, vitalybuka, melver.
Herald added a subscriber: hiraditya.
glider requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

To ease the deployment of KMSAN, we need a way to apply
__attribute__((no_sanitize("kernel-memory"))) to the whole source file.
Passing -msan-no-sanitize-whole-file=1 to the compiler will make it
treat every function in the file as if it was lacking the
sanitize_memory attribute.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D115236

Files:
  llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp
  llvm/test/Instrumentation/MemorySanitizer/msan_no_sanitize_whole_file.ll


Index: llvm/test/Instrumentation/MemorySanitizer/msan_no_sanitize_whole_file.ll
===================================================================
--- /dev/null
+++ llvm/test/Instrumentation/MemorySanitizer/msan_no_sanitize_whole_file.ll
@@ -0,0 +1,50 @@
+; Test for -msan-no-sanitize-whole-file, which should treat every function in the file
+; as if it didn't have the sanitize_memory attribute.
+; RUN: opt < %s -msan-check-access-address=0 -S -passes='module(msan-module),function(msan)' 2>&1 | FileCheck -allow-deprecated-dag-overlap -check-prefixes=CHECK,INSTR %s
+; RUN: opt < %s -msan-check-access-address=0 -S -passes='module(msan-module),function(msan)' -msan-no-sanitize-whole-file=1 2>&1 | FileCheck -allow-deprecated-dag-overlap -check-prefixes=CHECK,NOINSTR %s
+
+target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+declare void @bar()
+
+define i32 @SanitizeFn(i32 %x) uwtable sanitize_memory {
+entry:
+  %tobool = icmp eq i32 %x, 0
+  br i1 %tobool, label %if.end, label %if.then
+
+if.then:                                          ; preds = %entry
+  tail call void @bar()
+  br label %if.end
+
+if.end:                                           ; preds = %entry, %if.then
+  ret i32 %x
+}
+
+; CHECK-LABEL: @SanitizeFn
+; INSTR: @__msan_warning
+; NOINSTR-NOT: @__msan_warning
+; NOINSTR: store i32 0, {{.*}} @__msan_retval_tls
+; CHECK: ret i32
+
+
+define i32 @NoSanitizeFn(i32 %x) uwtable {
+entry:
+  %tobool = icmp eq i32 %x, 0
+  br i1 %tobool, label %if.end, label %if.then
+
+if.then:                                          ; preds = %entry
+  tail call void @bar()
+  br label %if.end
+
+if.end:                                           ; preds = %entry, %if.then
+  ret i32 %x
+}
+
+
+; CHECK-LABEL: @NoSanitizeFn
+; INSTR-NOT: @__msan_warning
+; NOINSTR-NOT: @__msan_warning
+; NOINSTR: store i32 0, {{.*}} @__msan_retval_tls
+; CHECK: ret i32
+
Index: llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp
===================================================================
--- llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp
+++ llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp
@@ -307,6 +307,12 @@
                   cl::desc("Enable KernelMemorySanitizer instrumentation"),
                   cl::Hidden, cl::init(false));
 
+static cl::opt<bool> ClNoSanitizeWholeFile("msan-no-sanitize-whole-file",
+                                           cl::desc("Apply __no_sanitize(("
+                                                    "memory"
+                                                    ")) to the whole file"),
+                                           cl::Hidden, cl::init(false));
+
 // This is an experiment to enable handling of cases where shadow is a non-zero
 // compile-time constant. For some unexplainable reason they were silently
 // ignored in the instrumentation.
@@ -1095,7 +1101,8 @@
   MemorySanitizerVisitor(Function &F, MemorySanitizer &MS,
                          const TargetLibraryInfo &TLI)
       : F(F), MS(MS), VAHelper(CreateVarArgHelper(F, MS, *this)), TLI(&TLI) {
-    bool SanitizeFunction = F.hasFnAttribute(Attribute::SanitizeMemory);
+    bool SanitizeFunction =
+        F.hasFnAttribute(Attribute::SanitizeMemory) && !ClNoSanitizeWholeFile;
     InsertChecks = SanitizeFunction;
     PropagateShadow = SanitizeFunction;
     PoisonStack = SanitizeFunction && ClPoisonStack;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D115236.392351.patch
Type: text/x-patch
Size: 3538 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20211207/22e25d78/attachment.bin>


More information about the llvm-commits mailing list