[PATCH] D119711: Add asan support for MSVC debug runtimes

Petr Mikhalitsyn via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Feb 14 06:18:38 PST 2022


lo1ol created this revision.
lo1ol added a reviewer: rnk.
lo1ol requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Use special debug asan versions (prefixed with asan_dbg) for MSVC debug runtimes (/MTd, /MDd, /LDd).

Resolves: https://github.com/llvm/llvm-project/compare/main...lo1ol:asan_dbg?expand=1


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D119711

Files:
  clang/lib/Driver/SanitizerArgs.cpp
  clang/lib/Driver/ToolChains/MSVC.cpp


Index: clang/lib/Driver/ToolChains/MSVC.cpp
===================================================================
--- clang/lib/Driver/ToolChains/MSVC.cpp
+++ clang/lib/Driver/ToolChains/MSVC.cpp
@@ -553,13 +553,20 @@
     CmdArgs.push_back(Args.MakeArgString("-incremental:no"));
   }
 
+  bool DebugRuntime = Args.hasArg(options::OPT__SLASH_MTd) ||
+                      Args.hasArg(options::OPT__SLASH_MDd) ||
+                      Args.hasArg(options::OPT__SLASH_LDd);
+
+  std::string AsanPrefix = DebugRuntime ? "asan_dbg" : "asan";
+  std::string AsanCXX = DebugRuntime ? "asan_cxx_dbg" : "asan_cxx";
+
   if (TC.getSanitizerArgs(Args).needsAsanRt()) {
     CmdArgs.push_back(Args.MakeArgString("-debug"));
     CmdArgs.push_back(Args.MakeArgString("-incremental:no"));
     if (TC.getSanitizerArgs(Args).needsSharedRt() ||
         Args.hasArg(options::OPT__SLASH_MD, options::OPT__SLASH_MDd)) {
-      for (const auto &Lib : {"asan_dynamic", "asan_dynamic_runtime_thunk"})
-        CmdArgs.push_back(TC.getCompilerRTArgString(Args, Lib));
+      for (const auto &Lib : {"_dynamic", "_dynamic_runtime_thunk"})
+        CmdArgs.push_back(TC.getCompilerRTArgString(Args, AsanPrefix + Lib));
       // Make sure the dynamic runtime thunk is not optimized out at link time
       // to ensure proper SEH handling.
       CmdArgs.push_back(Args.MakeArgString(
@@ -568,12 +575,14 @@
               : "-include:__asan_seh_interceptor"));
       // Make sure the linker consider all object files from the dynamic runtime
       // thunk.
-      CmdArgs.push_back(Args.MakeArgString(std::string("-wholearchive:") +
-          TC.getCompilerRT(Args, "asan_dynamic_runtime_thunk")));
+      CmdArgs.push_back(Args.MakeArgString(
+          std::string("-wholearchive:") +
+          TC.getCompilerRT(Args, AsanPrefix + "_dynamic_runtime_thunk")));
     } else if (DLL) {
-      CmdArgs.push_back(TC.getCompilerRTArgString(Args, "asan_dll_thunk"));
+      CmdArgs.push_back(
+          TC.getCompilerRTArgString(Args, AsanPrefix + "_dll_thunk"));
     } else {
-      for (const auto &Lib : {"asan", "asan_cxx"}) {
+      for (const auto &Lib : {AsanPrefix, AsanCXX}) {
         CmdArgs.push_back(TC.getCompilerRTArgString(Args, Lib));
         // Make sure the linker consider all object files from the static lib.
         // This is necessary because instrumented dlls need access to all the
Index: clang/lib/Driver/SanitizerArgs.cpp
===================================================================
--- clang/lib/Driver/SanitizerArgs.cpp
+++ clang/lib/Driver/SanitizerArgs.cpp
@@ -814,23 +814,6 @@
         }
     }
 
-    if (Arg *WindowsDebugRTArg =
-            Args.getLastArg(options::OPT__SLASH_MTd, options::OPT__SLASH_MT,
-                            options::OPT__SLASH_MDd, options::OPT__SLASH_MD,
-                            options::OPT__SLASH_LDd, options::OPT__SLASH_LD)) {
-      switch (WindowsDebugRTArg->getOption().getID()) {
-      case options::OPT__SLASH_MTd:
-      case options::OPT__SLASH_MDd:
-      case options::OPT__SLASH_LDd:
-        if (DiagnoseErrors) {
-          D.Diag(clang::diag::err_drv_argument_not_allowed_with)
-              << WindowsDebugRTArg->getAsString(Args)
-              << lastArgumentForMask(D, Args, SanitizerKind::Address);
-          D.Diag(clang::diag::note_drv_address_sanitizer_debug_runtime);
-        }
-      }
-    }
-
     AsanUseAfterScope = Args.hasFlag(
         options::OPT_fsanitize_address_use_after_scope,
         options::OPT_fno_sanitize_address_use_after_scope, AsanUseAfterScope);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D119711.408392.patch
Type: text/x-patch
Size: 3562 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220214/0f7d5096/attachment-0001.bin>


More information about the cfe-commits mailing list