[cfe-commits] [PATCH] Clang support for MemorySanitizer

Evgeniy Stepanov eugenis at google.com
Fri Nov 30 04:19:47 PST 2012


Hi chandlerc, kcc, nicholas,

http://llvm-reviews.chandlerc.com/D146

CHANGE SINCE LAST DIFF
  http://llvm-reviews.chandlerc.com/D146?vs=379&id=380#toc

Files:
  llvm/tools/clang/include/clang/Basic/DiagnosticDriverKinds.td
  llvm/tools/clang/include/clang/Basic/Sanitizers.def
  llvm/tools/clang/lib/CodeGen/BackendUtil.cpp
  llvm/tools/clang/lib/Driver/SanitizerArgs.h
  llvm/tools/clang/lib/Driver/Tools.cpp

Index: llvm/tools/clang/include/clang/Basic/DiagnosticDriverKinds.td
===================================================================
--- llvm/tools/clang/include/clang/Basic/DiagnosticDriverKinds.td
+++ llvm/tools/clang/include/clang/Basic/DiagnosticDriverKinds.td
@@ -101,6 +101,8 @@
   "option '-MG' requires '-M' or '-MM'">;
 def err_drv_asan_android_requires_pie : Error<
   "AddressSanitizer on Android requires '-pie'">;
+def err_drv_sanitizer_requires_pie : Error<
+  "%select{Thread|Memory}0Sanitizer requires '-pie'">;
 def err_drv_unknown_objc_runtime : Error<
   "unknown or ill-formed Objective-C runtime '%0'">;
 
Index: llvm/tools/clang/include/clang/Basic/Sanitizers.def
===================================================================
--- llvm/tools/clang/include/clang/Basic/Sanitizers.def
+++ llvm/tools/clang/include/clang/Basic/Sanitizers.def
@@ -41,6 +41,9 @@
 // AddressSanitizer
 SANITIZER("address", Address)
 
+// MemorySanitizer
+SANITIZER("memory", Memory)
+
 // ThreadSanitizer
 SANITIZER("thread", Thread)
 
Index: llvm/tools/clang/lib/CodeGen/BackendUtil.cpp
===================================================================
--- llvm/tools/clang/lib/CodeGen/BackendUtil.cpp
+++ llvm/tools/clang/lib/CodeGen/BackendUtil.cpp
@@ -163,6 +163,11 @@
   PM.add(createAddressSanitizerModulePass());
 }
 
+static void addMemorySanitizerPass(const PassManagerBuilder &Builder,
+                                   PassManagerBase &PM) {
+  PM.add(createMemorySanitizerPass());
+}
+
 static void addThreadSanitizerPass(const PassManagerBuilder &Builder,
                                    PassManagerBase &PM) {
   PM.add(createThreadSanitizerPass());
@@ -211,6 +216,13 @@
                            addAddressSanitizerPass);
   }
 
+  if (LangOpts.SanitizeMemory) {
+    PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
+                           addMemorySanitizerPass);
+    PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
+                           addMemorySanitizerPass);
+  }
+
   if (LangOpts.SanitizeThread) {
     PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
                            addThreadSanitizerPass);
Index: llvm/tools/clang/lib/Driver/SanitizerArgs.h
===================================================================
--- llvm/tools/clang/lib/Driver/SanitizerArgs.h
+++ llvm/tools/clang/lib/Driver/SanitizerArgs.h
@@ -30,6 +30,7 @@
 #include "clang/Basic/Sanitizers.def"
     NeedsAsanRt = Address,
     NeedsTsanRt = Thread,
+    NeedsMsanRt = Memory,
     NeedsUbsanRt = (Undefined & ~Bounds) | Integer
   };
   unsigned Kind;
@@ -41,6 +42,7 @@
 
   bool needsAsanRt() const { return Kind & NeedsAsanRt; }
   bool needsTsanRt() const { return Kind & NeedsTsanRt; }
+  bool needsMsanRt() const { return Kind & NeedsMsanRt; }
   bool needsUbsanRt() const { return Kind & NeedsUbsanRt; }
 
   bool sanitizesVptr() const { return Kind & Vptr; }
Index: llvm/tools/clang/lib/Driver/Tools.cpp
===================================================================
--- llvm/tools/clang/lib/Driver/Tools.cpp
+++ llvm/tools/clang/lib/Driver/Tools.cpp
@@ -1512,6 +1512,9 @@
 static void addTsanRTLinux(const ToolChain &TC, const ArgList &Args,
                            ArgStringList &CmdArgs) {
   if (!Args.hasArg(options::OPT_shared)) {
+    if (!Args.hasArg(options::OPT_pie))
+      TC.getDriver().Diag(diag::err_drv_sanitizer_requires_pie) <<
+        /* Thread */ 0;
     // LibTsan is "libclang_rt.tsan-<ArchName>.a" in the Linux library
     // resource directory.
     SmallString<128> LibTsan(TC.getDriver().ResourceDir);
@@ -1525,6 +1528,27 @@
   }
 }
 
+/// If MemorySanitizer is enabled, add appropriate linker flags (Linux).
+/// This needs to be called before we add the C run-time (malloc, etc).
+static void addMsanRTLinux(const ToolChain &TC, const ArgList &Args,
+                           ArgStringList &CmdArgs) {
+  if (!Args.hasArg(options::OPT_shared)) {
+    if (!Args.hasArg(options::OPT_pie))
+      TC.getDriver().Diag(diag::err_drv_sanitizer_requires_pie) <<
+        /* Memory */ 1;
+    // LibMsan is "libclang_rt.msan-<ArchName>.a" in the Linux library
+    // resource directory.
+    SmallString<128> LibMsan(TC.getDriver().ResourceDir);
+    llvm::sys::path::append(LibMsan, "lib", "linux",
+                            (Twine("libclang_rt.msan-") +
+                             TC.getArchName() + ".a"));
+    CmdArgs.push_back(Args.MakeArgString(LibMsan));
+    CmdArgs.push_back("-lpthread");
+    CmdArgs.push_back("-ldl");
+    CmdArgs.push_back("-export-dynamic");
+  }
+}
+
 /// If UndefinedBehaviorSanitizer is enabled, add appropriate linker flags
 /// (Linux).
 static void addUbsanRTLinux(const ToolChain &TC, const ArgList &Args,
@@ -5445,6 +5469,8 @@
     addAsanRTLinux(getToolChain(), Args, CmdArgs);
   if (Sanitize.needsTsanRt())
     addTsanRTLinux(getToolChain(), Args, CmdArgs);
+  if (Sanitize.needsMsanRt())
+    addMsanRTLinux(getToolChain(), Args, CmdArgs);
 
   if (!Args.hasArg(options::OPT_nostdlib)) {
     if (!Args.hasArg(options::OPT_nodefaultlibs)) {
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D146.4.patch
Type: text/x-patch
Size: 5111 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20121130/94f8fe63/attachment.bin>


More information about the cfe-commits mailing list