[PATCH] D151511: Enable building with UBSAN + enable_ubsan_clang_cl on windows

Duo Wang via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu May 25 15:40:06 PDT 2023


dwang created this revision.
Herald added a project: All.
dwang requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D151511

Files:
  llvm/cmake/modules/HandleLLVMOptions.cmake


Index: llvm/cmake/modules/HandleLLVMOptions.cmake
===================================================================
--- llvm/cmake/modules/HandleLLVMOptions.cmake
+++ llvm/cmake/modules/HandleLLVMOptions.cmake
@@ -846,7 +846,7 @@
 endif()
 
 macro(append_common_sanitizer_flags)
-  if (NOT MSVC)
+  if (NOT MSVC OR CLANG_CL)
     # Append -fno-omit-frame-pointer and turn on debug info to get better
     # stack traces.
     add_flag_if_supported("-fno-omit-frame-pointer" FNO_OMIT_FRAME_POINTER)
@@ -925,33 +925,45 @@
       message(FATAL_ERROR "This sanitizer not yet supported in a MinGW environment: ${LLVM_USE_SANITIZER}")
     endif()
   elseif(MSVC)
+    # Prepare ASAN runtime if needed
+    if (LLVM_USE_SANITIZER MATCHES ".*Address.*" AND LINKER_IS_LLD_LINK)
+      if (LLVM_HOST_TRIPLE MATCHES "i[2-6]86-.*")
+        set(arch "i386")
+      else()
+        set(arch "x86_64")
+      endif()
+      if (${LLVM_USE_CRT_${uppercase_CMAKE_BUILD_TYPE}} MATCHES "^(MT|MTd)$")
+        append("/wholearchive:clang_rt.asan-${arch}.lib /wholearchive:clang_rt.asan_cxx-${arch}.lib"
+          CMAKE_EXE_LINKER_FLAGS)
+        append("/wholearchive:clang_rt.asan_dll_thunk-${arch}.lib"
+          CMAKE_MODULE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS)
+      else()
+        append("clang_rt.asan_dynamic-${arch}.lib /wholearchive:clang_rt.asan_dynamic_runtime_thunk-${arch}.lib"
+          CMAKE_EXE_LINKER_FLAGS CMAKE_MODULE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS)
+      endif()
+    endif()
     if (LLVM_USE_SANITIZER STREQUAL "Address")
       append_common_sanitizer_flags()
-      append("/fsanitize=address" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
       if (NOT CLANG_CL)
+        append("/fsanitize=address" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
         # Not compatible with /RTC flags.
         foreach (flags_opt_to_scrub
             CMAKE_CXX_FLAGS_${uppercase_CMAKE_BUILD_TYPE} CMAKE_C_FLAGS_${uppercase_CMAKE_BUILD_TYPE})
           string (REGEX REPLACE "(^| )/RTC[1csu]*($| )" " "
             "${flags_opt_to_scrub}" "${${flags_opt_to_scrub}}")
         endforeach()
+      else()
+        append("-fsanitize=address" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
       endif()
-      if (LINKER_IS_LLD_LINK)
-        if (LLVM_HOST_TRIPLE MATCHES "i[2-6]86-.*")
-          set(arch "i386")
-        else()
-          set(arch "x86_64")
-        endif()
-        if (${LLVM_USE_CRT_${uppercase_CMAKE_BUILD_TYPE}} MATCHES "^(MT|MTd)$")
-          append("/wholearchive:clang_rt.asan-${arch}.lib /wholearchive:clang_rt.asan_cxx-${arch}.lib"
-            CMAKE_EXE_LINKER_FLAGS)
-          append("/wholearchive:clang_rt.asan_dll_thunk-${arch}.lib"
-            CMAKE_MODULE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS)
-        else()
-          append("clang_rt.asan_dynamic-${arch}.lib /wholearchive:clang_rt.asan_dynamic_runtime_thunk-${arch}.lib"
-            CMAKE_EXE_LINKER_FLAGS CMAKE_MODULE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS)
-        endif()
-      endif()
+    elseif (CLANG_CL AND LLVM_USE_SANITIZER STREQUAL "Undefined")
+      append_common_sanitizer_flags()
+      append("-D_ITERATOR_DEBUG_LEVEL=0" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
+      append(${LLVM_UBSAN_FLAGS} CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
+    elseif (CLANG_CL AND (LLVM_USE_SANITIZER STREQUAL "Undefined;Address" OR LLVM_USE_SANITIZER STREQUAL "Address;Undefined"))
+      append_common_sanitizer_flags()
+      append("-fsanitize=address" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
+      append("-D_ITERATOR_DEBUG_LEVEL=0" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
+      append(${LLVM_UBSAN_FLAGS} CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
     else()
       message(FATAL_ERROR "This sanitizer not yet supported in the MSVC environment: ${LLVM_USE_SANITIZER}")
     endif()


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D151511.525844.patch
Type: text/x-patch
Size: 3708 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230525/a0b71722/attachment.bin>


More information about the llvm-commits mailing list