[Bf-blender-cvs] [f9eaf93d379] master: MSVC: ASAN support for VS 16.9

Ray Molenkamp noreply at git.blender.org
Tue Mar 30 03:11:24 CEST 2021


Commit: f9eaf93d37957fb29eefb720022edd988c540369
Author: Ray Molenkamp
Date:   Mon Mar 29 19:11:17 2021 -0600
Branches: master
https://developer.blender.org/rBf9eaf93d37957fb29eefb720022edd988c540369

MSVC: ASAN support for VS 16.9

This enables ASAN support when used with VS 16.9
enable as usual in cmake with the WITH_COMPILER_ASAN
option, or when using make.bat just tag on `asan'
to the invocation, ie: `make lite 2019 asan`

MSVC: Asan support for 16.9

This enables ASAN support when used with VS 16.9
enable as usual in cmake with the WITH_COMPILER_ASAN
option, or when using make.bat just tag on `asan'
to the invocation, ie: `make lite 2019 asan`

Differential Revision: https://developer.blender.org/D7794
Reviewed By: brecht, sergey

===================================================================

M	CMakeLists.txt
M	build_files/cmake/platform/platform_win32.cmake
M	build_files/windows/configure_msbuild.cmd
M	build_files/windows/configure_ninja.cmd
M	source/blender/blendthumb/CMakeLists.txt
M	source/blender/blenlib/BLI_asan.h
M	source/creator/CMakeLists.txt

===================================================================

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 4d224eaf46c..3f3057bccf1 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -522,10 +522,10 @@ if(CMAKE_COMPILER_IS_GNUCC)
   mark_as_advanced(WITH_LINKER_LLD)
 endif()
 
-if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_C_COMPILER_ID MATCHES "Clang")
-  option(WITH_COMPILER_ASAN "Build and link against address sanitizer (only for Debug & RelWithDebInfo targets)." OFF)
-  mark_as_advanced(WITH_COMPILER_ASAN)
+option(WITH_COMPILER_ASAN "Build and link against address sanitizer (only for Debug & RelWithDebInfo targets)." OFF)
+mark_as_advanced(WITH_COMPILER_ASAN)
 
+if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_C_COMPILER_ID MATCHES "Clang")
   if(WITH_COMPILER_ASAN)
     set(_asan_defaults "\
 -fsanitize=address \
diff --git a/build_files/cmake/platform/platform_win32.cmake b/build_files/cmake/platform/platform_win32.cmake
index acd6028e2ae..b6ec016aa95 100644
--- a/build_files/cmake/platform/platform_win32.cmake
+++ b/build_files/cmake/platform/platform_win32.cmake
@@ -149,11 +149,6 @@ add_definitions(-D_WIN32_WINNT=0x601)
 include(build_files/cmake/platform/platform_win32_bundle_crt.cmake)
 remove_cc_flag("/MDd" "/MD" "/Zi")
 
-if(WITH_WINDOWS_PDB)
-  set(PDB_INFO_OVERRIDE_FLAGS "/Z7")
-  set(PDB_INFO_OVERRIDE_LINKER_FLAGS "/DEBUG /OPT:REF /OPT:ICF /INCREMENTAL:NO")
-endif()
-
 if(MSVC_CLANG) # Clangs version of cl doesn't support all flags
   string(APPEND CMAKE_CXX_FLAGS " ${CXX_WARN_FLAGS} /nologo /J /Gd /EHsc -Wno-unused-command-line-argument -Wno-microsoft-enum-forward-reference ")
   set(CMAKE_C_FLAGS     "${CMAKE_C_FLAGS} /nologo /J /Gd -Wno-unused-command-line-argument -Wno-microsoft-enum-forward-reference")
@@ -162,6 +157,21 @@ else()
   set(CMAKE_C_FLAGS     "${CMAKE_C_FLAGS} /nologo /J /Gd /MP /bigobj")
 endif()
 
+# X64 ASAN is available and usable on MSVC 16.9 preview 4 and up)
+if(WITH_COMPILER_ASAN AND MSVC AND NOT MSVC_CLANG)
+  if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 19.28.29828)
+    #set a flag so we don't have to do this comparison all the time
+    SET(MSVC_ASAN On)
+    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /fsanitize=address")
+    set(CMAKE_C_FLAGS     "${CMAKE_C_FLAGS} /fsanitize=address")
+    string(APPEND CMAKE_EXE_LINKER_FLAGS_DEBUG " /INCREMENTAL:NO")
+    string(APPEND CMAKE_SHARED_LINKER_FLAGS_DEBUG " /INCREMENTAL:NO")
+  else()
+    message("-- ASAN not supported on MSVC ${CMAKE_CXX_COMPILER_VERSION}")
+  endif()
+endif()
+
+
 # C++ standards conformace (/permissive-) is available on msvc 15.5 (1912) and up
 if(MSVC_VERSION GREATER 1911 AND NOT MSVC_CLANG)
   string(APPEND CMAKE_CXX_FLAGS " /permissive-")
@@ -174,14 +184,41 @@ if(WITH_WINDOWS_SCCACHE AND CMAKE_VS_MSBUILD_COMMAND)
     set(WITH_WINDOWS_SCCACHE Off)
 endif()
 
+# Debug Symbol format
+# sccache # MSVC_ASAN # format # why
+# On      # On        # Z7     # sccache will only play nice with Z7
+# On      # Off       # Z7     # sccache will only play nice with Z7
+# Off     # On        # Zi     # Asan will not play nice with Edit and Continue
+# Off     # Off       # ZI     # Neither asan nor sscache is enabled Edit and Continue is available
+
+# Release Symbol format
+# sccache # MSVC_ASAN # format # why
+# On      # On        # Z7     # sccache will only play nice with Z7
+# On      # Off       # Z7     # sccache will only play nice with Z7
+# Off     # On        # Zi     # Asan will not play nice with Edit and Continue
+# Off     # Off       # Zi     # Edit and Continue disables some optimizations
+
+
 if(WITH_WINDOWS_SCCACHE)
     set(CMAKE_C_COMPILER_LAUNCHER sccache)
     set(CMAKE_CXX_COMPILER_LAUNCHER sccache)
     set(SYMBOL_FORMAT /Z7)
+    set(SYMBOL_FORMAT_RELEASE /Z7)
 else()
     unset(CMAKE_C_COMPILER_LAUNCHER)
     unset(CMAKE_CXX_COMPILER_LAUNCHER)
-    set(SYMBOL_FORMAT /ZI)
+    if(MSVC_ASAN)
+      set(SYMBOL_FORMAT /Z7)
+      set(SYMBOL_FORMAT_RELEASE /Z7)
+    else()
+      set(SYMBOL_FORMAT /ZI)
+      set(SYMBOL_FORMAT_RELEASE /Zi)
+    endif()
+endif()
+
+if(WITH_WINDOWS_PDB)
+	set(PDB_INFO_OVERRIDE_FLAGS "${SYMBOL_FORMAT_RELEASE}")
+	set(PDB_INFO_OVERRIDE_LINKER_FLAGS "/DEBUG /OPT:REF /OPT:ICF /INCREMENTAL:NO")
 endif()
 
 string(APPEND CMAKE_CXX_FLAGS_DEBUG " /MDd ${SYMBOL_FORMAT}")
@@ -190,9 +227,11 @@ string(APPEND CMAKE_CXX_FLAGS_RELEASE " /MD ${PDB_INFO_OVERRIDE_FLAGS}")
 string(APPEND CMAKE_C_FLAGS_RELEASE " /MD ${PDB_INFO_OVERRIDE_FLAGS}")
 string(APPEND CMAKE_CXX_FLAGS_MINSIZEREL " /MD ${PDB_INFO_OVERRIDE_FLAGS}")
 string(APPEND CMAKE_C_FLAGS_MINSIZEREL " /MD ${PDB_INFO_OVERRIDE_FLAGS}")
-string(APPEND CMAKE_CXX_FLAGS_RELWITHDEBINFO " /MD ${SYMBOL_FORMAT}")
-string(APPEND CMAKE_C_FLAGS_RELWITHDEBINFO " /MD ${SYMBOL_FORMAT}")
+string(APPEND CMAKE_CXX_FLAGS_RELWITHDEBINFO " /MD ${SYMBOL_FORMAT_RELEASE}")
+string(APPEND CMAKE_C_FLAGS_RELWITHDEBINFO " /MD ${SYMBOL_FORMAT_RELEASE}")
 unset(SYMBOL_FORMAT)
+unset(SYMBOL_FORMAT_RELEASE)
+
 # JMC is available on msvc 15.8 (1915) and up
 if(MSVC_VERSION GREATER 1914 AND NOT MSVC_CLANG)
   string(APPEND CMAKE_CXX_FLAGS_DEBUG " /JMC")
diff --git a/build_files/windows/configure_msbuild.cmd b/build_files/windows/configure_msbuild.cmd
index 4956f1e3ea1..be69f99a8e8 100644
--- a/build_files/windows/configure_msbuild.cmd
+++ b/build_files/windows/configure_msbuild.cmd
@@ -9,14 +9,10 @@ if "%BUILD_WITH_SCCACHE%"=="1" (
 
 if "%WITH_CLANG%"=="1" (
 	set CLANG_CMAKE_ARGS=-T"llvm"
-	if "%WITH_ASAN%"=="1" (
+)
+
+if "%WITH_ASAN%"=="1" (
 		set ASAN_CMAKE_ARGS=-DWITH_COMPILER_ASAN=On
-	)
-) else (
-	if "%WITH_ASAN%"=="1" (
-		echo ASAN is only supported with clang.
-		exit /b 1 
-	)
 )
 
 if "%WITH_PYDEBUG%"=="1" (
diff --git a/build_files/windows/configure_ninja.cmd b/build_files/windows/configure_ninja.cmd
index d68d94a3bcf..90085feb2bd 100644
--- a/build_files/windows/configure_ninja.cmd
+++ b/build_files/windows/configure_ninja.cmd
@@ -46,16 +46,10 @@ set LLVM_DIR=
 		set CFLAGS=-m64 -fmsc-version=1914
 		set CXXFLAGS=-m64 -fmsc-version=1914
 	)
-	if "%WITH_ASAN%"=="1" (
-		set BUILD_CMAKE_ARGS=%BUILD_CMAKE_ARGS% -DWITH_COMPILER_ASAN=On
-	)	
 )
 
 if "%WITH_ASAN%"=="1" (
-	if "%WITH_CLANG%" == "" (
-		echo ASAN is only supported with clang.
-		exit /b 1 
-	)
+	set BUILD_CMAKE_ARGS=%BUILD_CMAKE_ARGS% -DWITH_COMPILER_ASAN=On
 )
 
 if NOT "%verbose%" == "" (
diff --git a/source/blender/blendthumb/CMakeLists.txt b/source/blender/blendthumb/CMakeLists.txt
index cb121cb9c8d..b42ca284ecb 100644
--- a/source/blender/blendthumb/CMakeLists.txt
+++ b/source/blender/blendthumb/CMakeLists.txt
@@ -31,6 +31,7 @@ set(SRC
 string(APPEND CMAKE_SHARED_LINKER_FLAGS_DEBUG " /nodefaultlib:MSVCRT.lib")
 
 add_library(BlendThumb SHARED ${SRC})
+setup_platform_linker_flags(BlendThumb)
 target_link_libraries(BlendThumb ${ZLIB_LIBRARIES})
 
 install(
diff --git a/source/blender/blenlib/BLI_asan.h b/source/blender/blenlib/BLI_asan.h
index a2a44e164ab..c38ad6b39d0 100644
--- a/source/blender/blenlib/BLI_asan.h
+++ b/source/blender/blenlib/BLI_asan.h
@@ -21,7 +21,7 @@
 #  define __has_feature(x) 0
 #endif
 
-#if defined(__SANITIZE_ADDRESS__) || __has_feature(address_sanitizer)
+#if (defined(__SANITIZE_ADDRESS__) || __has_feature(address_sanitizer)) && !defined(_MSC_VER)
 #  include "sanitizer/asan_interface.h"
 #else
 /* Ensure return value is used. Just using UNUSED_VARS results in a warning. */
diff --git a/source/creator/CMakeLists.txt b/source/creator/CMakeLists.txt
index d9064682203..90840aa8358 100644
--- a/source/creator/CMakeLists.txt
+++ b/source/creator/CMakeLists.txt
@@ -693,6 +693,26 @@ elseif(WIN32)
           DESTINATION "."
     )
   endif()
+  if(MSVC_ASAN)
+    # The asan dll's can be found in the same folder as the compiler, this is the easiest way to find these.
+    string(REPLACE "cl.exe" "clang_rt.asan_dynamic-x86_64.dll" ASAN_DLL ${CMAKE_C_COMPILER})
+    string(REPLACE "cl.exe" "clang_rt.asan_dbg_dynamic-x86_64.dll" ASAN_DEBUG_DLL ${CMAKE_C_COMPILER})
+    if(NOT EXISTS "${ASAN_DLL}")
+      message(FATAL_ERROR "Asan is enabled, but the ASAN runtime is not detected, this is an optional component during the MSVC install, please install it")
+    endif()
+    install(
+          FILES ${ASAN_DLL}
+          DESTINATION "."
+          CONFIGURATIONS Release;RelWithDebInfo;MinSizeRel
+    )
+    install(
+          FILES ${ASAN_DEBUG_DLL}
+          DESTINATION "."
+          CONFIGURATIONS Debug
+    )
+    unset(ASAN_DLL)
+    unset(ASAN_DEBUG_DLL)
+  endif()
 
   if(WITH_GMP)
     install(



More information about the Bf-blender-cvs mailing list