[clang] Honor -fno-sanitize-link-runtime for libclang_rt.asan_static (PR #66414)

via cfe-commits cfe-commits at lists.llvm.org
Thu Sep 14 11:50:34 PDT 2023


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-driver
            
<details>
<summary>Changes</summary>
https://reviews.llvm.org/D122407 added a static link against libclang_rt.asan_static whenever ASAN is linked, but ignored the -fno-sanitize-link-runtime flag.  Every other conditional in collectSanitizerRuntimes is already checking for SanArgs.linkRuntimes(), move it to the top of the function so that newly added conditionals don't need to remember to check it.
--
Full diff: https://github.com/llvm/llvm-project/pull/66414.diff

2 Files Affected:

- (modified) clang/lib/Driver/ToolChains/CommonArgs.cpp (+27-27) 
- (modified) clang/test/Driver/sanitizer-ld.c (+2) 


<pre>
diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp
index 1db05b47e11ba6a..1768280e05b336f 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -1071,28 +1071,28 @@ collectSanitizerRuntimes(const ToolChain &amp;TC, const ArgList &amp;Args,
   const SanitizerArgs &amp;SanArgs = TC.getSanitizerArgs(Args);
   // Collect shared runtimes.
   if (SanArgs.needsSharedRt()) {
-    if (SanArgs.needsAsanRt() &amp;&amp; SanArgs.linkRuntimes()) {
+    if (SanArgs.needsAsanRt()) {
       SharedRuntimes.push_back(&quot;asan&quot;);
       if (!Args.hasArg(options::OPT_shared) &amp;&amp; !TC.getTriple().isAndroid())
         HelperStaticRuntimes.push_back(&quot;asan-preinit&quot;);
     }
-    if (SanArgs.needsMemProfRt() &amp;&amp; SanArgs.linkRuntimes()) {
+    if (SanArgs.needsMemProfRt()) {
       SharedRuntimes.push_back(&quot;memprof&quot;);
       if (!Args.hasArg(options::OPT_shared) &amp;&amp; !TC.getTriple().isAndroid())
         HelperStaticRuntimes.push_back(&quot;memprof-preinit&quot;);
     }
-    if (SanArgs.needsUbsanRt() &amp;&amp; SanArgs.linkRuntimes()) {
+    if (SanArgs.needsUbsanRt()) {
       if (SanArgs.requiresMinimalRuntime())
         SharedRuntimes.push_back(&quot;ubsan_minimal&quot;);
       else
         SharedRuntimes.push_back(&quot;ubsan_standalone&quot;);
     }
-    if (SanArgs.needsScudoRt() &amp;&amp; SanArgs.linkRuntimes()) {
+    if (SanArgs.needsScudoRt()) {
       SharedRuntimes.push_back(&quot;scudo_standalone&quot;);
     }
-    if (SanArgs.needsTsanRt() &amp;&amp; SanArgs.linkRuntimes())
+    if (SanArgs.needsTsanRt())
       SharedRuntimes.push_back(&quot;tsan&quot;);
-    if (SanArgs.needsHwasanRt() &amp;&amp; SanArgs.linkRuntimes()) {
+    if (SanArgs.needsHwasanRt()) {
       if (SanArgs.needsHwasanAliasesRt())
         SharedRuntimes.push_back(&quot;hwasan_aliases&quot;);
       else
@@ -1103,7 +1103,7 @@ collectSanitizerRuntimes(const ToolChain &amp;TC, const ArgList &amp;Args,
   }
 
   // The stats_client library is also statically linked into DSOs.
-  if (SanArgs.needsStatsRt() &amp;&amp; SanArgs.linkRuntimes())
+  if (SanArgs.needsStatsRt())
     StaticRuntimes.push_back(&quot;stats_client&quot;);
 
   // Always link the static runtime regardless of DSO or executable.
@@ -1119,20 +1119,19 @@ collectSanitizerRuntimes(const ToolChain &amp;TC, const ArgList &amp;Args,
   // Each static runtime that has a DSO counterpart above is excluded below,
   // but runtimes that exist only as static are not affected by needsSharedRt.
 
-  if (!SanArgs.needsSharedRt() &amp;&amp; SanArgs.needsAsanRt() &amp;&amp; SanArgs.linkRuntimes()) {
+  if (!SanArgs.needsSharedRt() &amp;&amp; SanArgs.needsAsanRt()) {
     StaticRuntimes.push_back(&quot;asan&quot;);
     if (SanArgs.linkCXXRuntimes())
       StaticRuntimes.push_back(&quot;asan_cxx&quot;);
   }
 
-  if (!SanArgs.needsSharedRt() &amp;&amp; SanArgs.needsMemProfRt() &amp;&amp;
-      SanArgs.linkRuntimes()) {
+  if (!SanArgs.needsSharedRt() &amp;&amp; SanArgs.needsMemProfRt()) {
     StaticRuntimes.push_back(&quot;memprof&quot;);
     if (SanArgs.linkCXXRuntimes())
       StaticRuntimes.push_back(&quot;memprof_cxx&quot;);
   }
 
-  if (!SanArgs.needsSharedRt() &amp;&amp; SanArgs.needsHwasanRt() &amp;&amp; SanArgs.linkRuntimes()) {
+  if (!SanArgs.needsSharedRt() &amp;&amp; SanArgs.needsHwasanRt()) {
     if (SanArgs.needsHwasanAliasesRt()) {
       StaticRuntimes.push_back(&quot;hwasan_aliases&quot;);
       if (SanArgs.linkCXXRuntimes())
@@ -1143,22 +1142,21 @@ collectSanitizerRuntimes(const ToolChain &amp;TC, const ArgList &amp;Args,
         StaticRuntimes.push_back(&quot;hwasan_cxx&quot;);
     }
   }
-  if (SanArgs.needsDfsanRt() &amp;&amp; SanArgs.linkRuntimes())
+  if (SanArgs.needsDfsanRt())
     StaticRuntimes.push_back(&quot;dfsan&quot;);
-  if (SanArgs.needsLsanRt() &amp;&amp; SanArgs.linkRuntimes())
+  if (SanArgs.needsLsanRt())
     StaticRuntimes.push_back(&quot;lsan&quot;);
-  if (SanArgs.needsMsanRt() &amp;&amp; SanArgs.linkRuntimes()) {
+  if (SanArgs.needsMsanRt()) {
     StaticRuntimes.push_back(&quot;msan&quot;);
     if (SanArgs.linkCXXRuntimes())
       StaticRuntimes.push_back(&quot;msan_cxx&quot;);
   }
-  if (!SanArgs.needsSharedRt() &amp;&amp; SanArgs.needsTsanRt() &amp;&amp;
-      SanArgs.linkRuntimes()) {
+  if (!SanArgs.needsSharedRt() &amp;&amp; SanArgs.needsTsanRt()) {
     StaticRuntimes.push_back(&quot;tsan&quot;);
     if (SanArgs.linkCXXRuntimes())
       StaticRuntimes.push_back(&quot;tsan_cxx&quot;);
   }
-  if (!SanArgs.needsSharedRt() &amp;&amp; SanArgs.needsUbsanRt() &amp;&amp; SanArgs.linkRuntimes()) {
+  if (!SanArgs.needsSharedRt() &amp;&amp; SanArgs.needsUbsanRt()) {
     if (SanArgs.requiresMinimalRuntime()) {
       StaticRuntimes.push_back(&quot;ubsan_minimal&quot;);
     } else {
@@ -1167,24 +1165,24 @@ collectSanitizerRuntimes(const ToolChain &amp;TC, const ArgList &amp;Args,
         StaticRuntimes.push_back(&quot;ubsan_standalone_cxx&quot;);
     }
   }
-  if (SanArgs.needsSafeStackRt() &amp;&amp; SanArgs.linkRuntimes()) {
+  if (SanArgs.needsSafeStackRt()) {
     NonWholeStaticRuntimes.push_back(&quot;safestack&quot;);
     RequiredSymbols.push_back(&quot;__safestack_init&quot;);
   }
-  if (!(SanArgs.needsSharedRt() &amp;&amp; SanArgs.needsUbsanRt() &amp;&amp; SanArgs.linkRuntimes())) {
-    if (SanArgs.needsCfiRt() &amp;&amp; SanArgs.linkRuntimes())
+  if (!(SanArgs.needsSharedRt() &amp;&amp; SanArgs.needsUbsanRt())) {
+    if (SanArgs.needsCfiRt())
       StaticRuntimes.push_back(&quot;cfi&quot;);
-    if (SanArgs.needsCfiDiagRt() &amp;&amp; SanArgs.linkRuntimes()) {
+    if (SanArgs.needsCfiDiagRt()) {
       StaticRuntimes.push_back(&quot;cfi_diag&quot;);
       if (SanArgs.linkCXXRuntimes())
         StaticRuntimes.push_back(&quot;ubsan_standalone_cxx&quot;);
     }
   }
-  if (SanArgs.needsStatsRt() &amp;&amp; SanArgs.linkRuntimes()) {
+  if (SanArgs.needsStatsRt()) {
     NonWholeStaticRuntimes.push_back(&quot;stats&quot;);
     RequiredSymbols.push_back(&quot;__sanitizer_stats_register&quot;);
   }
-  if (!SanArgs.needsSharedRt() &amp;&amp; SanArgs.needsScudoRt() &amp;&amp; SanArgs.linkRuntimes()) {
+  if (!SanArgs.needsSharedRt() &amp;&amp; SanArgs.needsScudoRt()) {
     StaticRuntimes.push_back(&quot;scudo_standalone&quot;);
     if (SanArgs.linkCXXRuntimes())
       StaticRuntimes.push_back(&quot;scudo_standalone_cxx&quot;);
@@ -1195,13 +1193,15 @@ collectSanitizerRuntimes(const ToolChain &amp;TC, const ArgList &amp;Args,
 // C runtime, etc). Returns true if sanitizer system deps need to be linked in.
 bool tools::addSanitizerRuntimes(const ToolChain &amp;TC, const ArgList &amp;Args,
                                  ArgStringList &amp;CmdArgs) {
+  const SanitizerArgs &amp;SanArgs = TC.getSanitizerArgs(Args);
   SmallVector&lt;StringRef, 4&gt; SharedRuntimes, StaticRuntimes,
       NonWholeStaticRuntimes, HelperStaticRuntimes, RequiredSymbols;
-  collectSanitizerRuntimes(TC, Args, SharedRuntimes, StaticRuntimes,
-                           NonWholeStaticRuntimes, HelperStaticRuntimes,
-                           RequiredSymbols);
+  if (SanArgs.linkRuntimes()) {
+    collectSanitizerRuntimes(TC, Args, SharedRuntimes, StaticRuntimes,
+                             NonWholeStaticRuntimes, HelperStaticRuntimes,
+                             RequiredSymbols);
+  }
 
-  const SanitizerArgs &amp;SanArgs = TC.getSanitizerArgs(Args);
   // Inject libfuzzer dependencies.
   if (SanArgs.needsFuzzer() &amp;&amp; SanArgs.linkRuntimes() &amp;&amp;
       !Args.hasArg(options::OPT_shared)) {
diff --git a/clang/test/Driver/sanitizer-ld.c b/clang/test/Driver/sanitizer-ld.c
index eacd5c4698842e6..2831c2dad8d24cc 100644
--- a/clang/test/Driver/sanitizer-ld.c
+++ b/clang/test/Driver/sanitizer-ld.c
@@ -23,6 +23,7 @@
 // RUN:     --sysroot=%S/Inputs/basic_linux_tree \
 // RUN:   | FileCheck --check-prefix=CHECK-ASAN-NO-LINK-RUNTIME-LINUX %s
 //
+// CHECK-ASAN-NO-LINK-RUNTIME-LINUX-NOT: libclang_rt.asan_static-x86_64
 // CHECK-ASAN-NO-LINK-RUNTIME-LINUX-NOT: libclang_rt.asan-x86_64
 
 // RUN: %clang -fsanitize=address -fno-sanitize-link-runtime -### %s 2&gt;&amp;1 \
@@ -31,6 +32,7 @@
 // RUN:     --sysroot=%S/Inputs/basic_linux_tree \
 // RUN:   | FileCheck --check-prefix=CHECK-ASAN-NO-LINK-RUNTIME-DARWIN %s
 //
+// CHECK-ASAN-NO-LINK-RUNTIME-DARWIN-NOT: libclang_rt.asan_static
 // CHECK-ASAN-NO-LINK-RUNTIME-DARWIN-NOT: libclang_rt.asan
 
 // RUN: %clang -fsanitize=address -### %s 2&gt;&amp;1 \
</pre>
</details>


https://github.com/llvm/llvm-project/pull/66414


More information about the cfe-commits mailing list