<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Mon, Jul 7, 2014 at 4:34 PM, Alexey Samsonov <span dir="ltr"><<a href="mailto:vonosmas@gmail.com" target="_blank">vonosmas@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: samsonov<br>
Date: Mon Jul  7 18:34:34 2014<br>
New Revision: 212499<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=212499&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=212499&view=rev</a><br>
Log:<br>
[Sanitizer] Reduce the usage of sanitizer blacklist in CodeGenModule<br>
<br>
Get rid of cached CodeGenModule::SanOpts, which was used to turn off<br>
sanitizer codegen options if current LLVM Module is blacklisted, and use<br>
plain LangOpts.Sanitize instead.<br>
<br>
1) Some codegen decisions (turning TBAA or writable strings on/off)<br>
   shouldn't depend on the contents of blacklist.<br>
<br>
2) llvm.asan.globals should *always* be created, even if the module<br>
   is blacklisted - soon Clang's CodeGen where we read sanitizer<br>
   blacklist files, so we should properly report which globals are<br>
   blacklisted to the backend.<br>
<br>
Modified:<br>
    cfe/trunk/lib/CodeGen/CGDeclCXX.cpp<br>
    cfe/trunk/lib/CodeGen/CodeGenModule.cpp<br>
    cfe/trunk/lib/CodeGen/CodeGenModule.h<br>
    cfe/trunk/test/CodeGen/asan-globals.cpp<br>
<br>
Modified: cfe/trunk/lib/CodeGen/CGDeclCXX.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGDeclCXX.cpp?rev=212499&r1=212498&r2=212499&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGDeclCXX.cpp?rev=212499&r1=212498&r2=212499&view=diff</a><br>

==============================================================================<br>
--- cfe/trunk/lib/CodeGen/CGDeclCXX.cpp (original)<br>
+++ cfe/trunk/lib/CodeGen/CGDeclCXX.cpp Mon Jul  7 18:34:34 2014<br>
@@ -245,12 +245,14 @@ CreateGlobalInitOrDestructFunction(CodeG<br>
   if (!CGM.getLangOpts().Exceptions)<br>
     Fn->setDoesNotThrow();<br>
<br>
-  if (CGM.getSanOpts().Address)<br>
-    Fn->addFnAttr(llvm::Attribute::SanitizeAddress);<br>
-  if (CGM.getSanOpts().Thread)<br>
-    Fn->addFnAttr(llvm::Attribute::SanitizeThread);<br>
-  if (CGM.getSanOpts().Memory)<br>
-    Fn->addFnAttr(llvm::Attribute::SanitizeMemory);<br>
+  if (!CGM.getSanitizerBlacklist().isIn(*Fn)) {<br>
+    if (CGM.getLangOpts().Sanitize.Address)<br>
+      Fn->addFnAttr(llvm::Attribute::SanitizeAddress);<br>
+    if (CGM.getLangOpts().Sanitize.Thread)<br>
+      Fn->addFnAttr(llvm::Attribute::SanitizeThread);<br>
+    if (CGM.getLangOpts().Sanitize.Memory)<br>
+      Fn->addFnAttr(llvm::Attribute::SanitizeMemory);<br>
+  }<br>
<br>
   return Fn;<br>
 }<br>
<br>
Modified: cfe/trunk/lib/CodeGen/CodeGenModule.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenModule.cpp?rev=212499&r1=212498&r2=212499&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenModule.cpp?rev=212499&r1=212498&r2=212499&view=diff</a><br>

==============================================================================<br>
--- cfe/trunk/lib/CodeGen/CodeGenModule.cpp (original)<br>
+++ cfe/trunk/lib/CodeGen/CodeGenModule.cpp Mon Jul  7 18:34:34 2014<br>
@@ -89,9 +89,7 @@ CodeGenModule::CodeGenModule(ASTContext<br>
       GenericBlockLiteralType(nullptr), LifetimeStartFn(nullptr),<br>
       LifetimeEndFn(nullptr),<br>
       SanitizerBlacklist(<br>
-          llvm::SpecialCaseList::createOrDie(CGO.SanitizerBlacklistFile)),<br>
-      SanOpts(SanitizerBlacklist->isIn(M) ? SanitizerOptions::Disabled<br>
-                                          : LangOpts.Sanitize) {<br>
+          llvm::SpecialCaseList::createOrDie(CGO.SanitizerBlacklistFile)) {<br>
<br>
   // Initialize the type cache.<br>
   llvm::LLVMContext &LLVMContext = M.getContext();<br>
@@ -122,7 +120,7 @@ CodeGenModule::CodeGenModule(ASTContext<br>
     createCUDARuntime();<br>
<br>
   // Enable TBAA unless it's suppressed. ThreadSanitizer needs TBAA even at O0.<br>
-  if (SanOpts.Thread ||<br>
+  if (LangOpts.Sanitize.Thread ||<br>
       (!CodeGenOpts.RelaxedAliasing && CodeGenOpts.OptimizationLevel > 0))<br>
     TBAA = new CodeGenTBAA(Context, VMContext, CodeGenOpts, getLangOpts(),<br>
                            getCXXABI().getMangleContext());<br>
@@ -735,14 +733,13 @@ void CodeGenModule::SetLLVMFunctionAttri<br>
   if (!SanitizerBlacklist->isIn(*F)) {<br>
     // When AddressSanitizer is enabled, set SanitizeAddress attribute<br>
     // unless __attribute__((no_sanitize_address)) is used.<br>
-    if (SanOpts.Address && !D->hasAttr<NoSanitizeAddressAttr>())<br>
+    if (LangOpts.Sanitize.Address && !D->hasAttr<NoSanitizeAddressAttr>())<br>
       B.addAttribute(llvm::Attribute::SanitizeAddress);<br></blockquote><div><br></div><div>Should this be set if the module is blacklisted? (Likewise for tsan and msan attributes.)</div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

     // Same for ThreadSanitizer and __attribute__((no_sanitize_thread))<br>
-    if (SanOpts.Thread && !D->hasAttr<NoSanitizeThreadAttr>()) {<br>
+    if (LangOpts.Sanitize.Thread && !D->hasAttr<NoSanitizeThreadAttr>())<br>
       B.addAttribute(llvm::Attribute::SanitizeThread);<br>
-    }<br>
     // Same for MemorySanitizer and __attribute__((no_sanitize_memory))<br>
-    if (SanOpts.Memory && !D->hasAttr<NoSanitizeMemoryAttr>())<br>
+    if (LangOpts.Sanitize.Memory && !D->hasAttr<NoSanitizeMemoryAttr>())<br>
       B.addAttribute(llvm::Attribute::SanitizeMemory);<br>
   }<br>
<br>
@@ -1966,7 +1963,7 @@ void CodeGenModule::EmitGlobalVarDefinit<br>
<br>
 void CodeGenModule::reportGlobalToASan(llvm::GlobalVariable *GV,<br>
                                        SourceLocation Loc, bool IsDynInit) {<br>
-  if (!SanOpts.Address)<br>
+  if (!LangOpts.Sanitize.Address)<br>
     return;<br>
   IsDynInit &= !SanitizerBlacklist->isIn(*GV, "init");<br>
   bool IsBlacklisted = SanitizerBlacklist->isIn(*GV);<br>
@@ -2796,7 +2793,7 @@ CodeGenModule::GetAddrOfConstantStringFr<br>
   // Mangle the string literal if the ABI allows for it.  However, we cannot<br>
   // do this if  we are compiling with ASan or -fwritable-strings because they<br>
   // rely on strings having normal linkage.<br>
-  if (!LangOpts.WritableStrings && !SanOpts.Address &&<br>
+  if (!LangOpts.WritableStrings && !LangOpts.Sanitize.Address &&<br>
       getCXXABI().getMangleContext().shouldMangleStringLiteral(S)) {<br>
     llvm::raw_svector_ostream Out(MangledNameBuffer);<br>
     getCXXABI().getMangleContext().mangleStringLiteral(S, Out);<br>
<br>
Modified: cfe/trunk/lib/CodeGen/CodeGenModule.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenModule.h?rev=212499&r1=212498&r2=212499&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenModule.h?rev=212499&r1=212498&r2=212499&view=diff</a><br>

==============================================================================<br>
--- cfe/trunk/lib/CodeGen/CodeGenModule.h (original)<br>
+++ cfe/trunk/lib/CodeGen/CodeGenModule.h Mon Jul  7 18:34:34 2014<br>
@@ -475,8 +475,6 @@ class CodeGenModule : public CodeGenType<br>
<br>
   std::unique_ptr<llvm::SpecialCaseList> SanitizerBlacklist;<br>
<br>
-  const SanitizerOptions &SanOpts;<br>
-<br>
   /// @}<br>
 public:<br>
   CodeGenModule(ASTContext &C, const CodeGenOptions &CodeGenOpts,<br>
@@ -1014,7 +1012,10 @@ public:<br>
     return *SanitizerBlacklist;<br>
   }<br>
<br>
-  const SanitizerOptions &getSanOpts() const { return SanOpts; }<br>
+  const SanitizerOptions &getSanOpts() const {<br>
+    return SanitizerBlacklist->isIn(TheModule) ? SanitizerOptions::Disabled<br>
+                                               : LangOpts.Sanitize;<br>
+  }<br>
<br>
   void reportGlobalToASan(llvm::GlobalVariable *GV, SourceLocation Loc,<br>
                           bool IsDynInit = false);<br>
<br>
Modified: cfe/trunk/test/CodeGen/asan-globals.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/asan-globals.cpp?rev=212499&r1=212498&r2=212499&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/asan-globals.cpp?rev=212499&r1=212498&r2=212499&view=diff</a><br>

==============================================================================<br>
--- cfe/trunk/test/CodeGen/asan-globals.cpp (original)<br>
+++ cfe/trunk/test/CodeGen/asan-globals.cpp Mon Jul  7 18:34:34 2014<br>
@@ -1,5 +1,7 @@<br>
 // RUN: echo "global:*blacklisted_global*" > %t.blacklist<br>
 // RUN: %clang_cc1 -fsanitize=address -fsanitize-blacklist=%t.blacklist -emit-llvm -o - %s | FileCheck %s<br>
+// RUN: echo "src:%s" > %t.blacklist-src<br>
+// RUN: %clang_cc1 -fsanitize=address -fsanitize-blacklist=%t.blacklist-src -emit-llvm -o - %s | FileCheck %s --check-prefix=BLACKLIST-SRC<br>
 // REQUIRES: shell<br>
<br>
 int global;<br>
@@ -21,3 +23,10 @@ void func() {<br>
 // CHECK: ![[BLACKLISTED_GLOBAL]] = metadata !{{{.*}}, null, i1 false, i1 true}<br>
 // CHECK: ![[STATIC_VAR]] = metadata !{{{.*}} [[STATIC_LOC]], i1 false, i1 false}<br>
 // CHECK: ![[LITERAL]] = metadata !{{{.*}} [[LITERAL_LOC]], i1 false, i1 false}<br>
+<br>
+// BLACKLIST-SRC: !llvm.asan.globals = !{![[GLOBAL:[0-9]+]], ![[DYN_INIT_GLOBAL:[0-9]+]], ![[BLACKLISTED_GLOBAL:[0-9]+]], ![[STATIC_VAR:[0-9]+]], ![[LITERAL:[0-9]+]]}<br>
+// BLACKLIST-SRC: ![[GLOBAL]] = metadata !{{{.*}} null, i1 false, i1 true}<br>
+// BLACKLIST-SRC: ![[DYN_INIT_GLOBAL]] = metadata !{{{.*}} null, i1 true, i1 true}<br>
+// BLACKLIST-SRC: ![[BLACKLISTED_GLOBAL]] = metadata !{{{.*}}, null, i1 false, i1 true}<br>
+// BLACKLIST-SRC: ![[STATIC_VAR]] = metadata !{{{.*}} null, i1 false, i1 true}<br>
+// BLACKLIST-SRC: ![[LITERAL]] = metadata !{{{.*}} null, i1 false, i1 true}<br>
<br>
<br>
_______________________________________________<br>
cfe-commits mailing list<br>
<a href="mailto:cfe-commits@cs.uiuc.edu">cfe-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits</a><br>
</blockquote></div><br></div></div>