From c7fd8d5ad159152da25ceec19da4dc3d639dcf4c Mon Sep 17 00:00:00 2001 From: Ray Molenkamp Date: Thu, 27 Feb 2025 16:56:18 -0700 Subject: [PATCH] Fix: CMake: Windows: Make ASAN usable again on debug configurations Seems like ASAN has been broken on debug builds for MSVC ever since d5e50460e7 (1.5 years ago) the core reason is it's incompatible with fastlink, it'll appear to work, but in the asan reports no symbols will be resolved. While looking into this, some other problems like the symbol format being not inline with the table in the comment above it, and a warning coming out of /extern were noted. All of these issues are addressed in this commit. --- build_files/cmake/platform/platform_win32.cmake | 11 ++++++++--- extern/CMakeLists.txt | 16 +++++++++++----- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/build_files/cmake/platform/platform_win32.cmake b/build_files/cmake/platform/platform_win32.cmake index 5493df5e30d..3cbaa0de5ed 100644 --- a/build_files/cmake/platform/platform_win32.cmake +++ b/build_files/cmake/platform/platform_win32.cmake @@ -267,8 +267,8 @@ else() unset(CMAKE_C_COMPILER_LAUNCHER) unset(CMAKE_CXX_COMPILER_LAUNCHER) if(MSVC_ASAN) - set(SYMBOL_FORMAT /Z7) - set(SYMBOL_FORMAT_RELEASE /Z7) + set(SYMBOL_FORMAT /Zi) + set(SYMBOL_FORMAT_RELEASE /Zi) else() set(SYMBOL_FORMAT /ZI) set(SYMBOL_FORMAT_RELEASE /Zi) @@ -298,7 +298,12 @@ endif() string(APPEND PLATFORM_LINKFLAGS " /SUBSYSTEM:CONSOLE /STACK:2097152") set(PLATFORM_LINKFLAGS_RELEASE "/NODEFAULTLIB:libcmt.lib /NODEFAULTLIB:libcmtd.lib /NODEFAULTLIB:msvcrtd.lib") -string(APPEND PLATFORM_LINKFLAGS_DEBUG "/debug:fastlink /IGNORE:4099 /NODEFAULTLIB:libcmt.lib /NODEFAULTLIB:msvcrt.lib /NODEFAULTLIB:libcmtd.lib") + +if(NOT WITH_COMPILER_ASAN) + # Asan is incompatible with fastlink, it will appear to work, but will not resolve symbols which makes it somewhat useless + string(APPEND PLATFORM_LINKFLAGS_DEBUG "/debug:fastlink ") +endif() +string(APPEND PLATFORM_LINKFLAGS_DEBUG " /IGNORE:4099 /NODEFAULTLIB:libcmt.lib /NODEFAULTLIB:msvcrt.lib /NODEFAULTLIB:libcmtd.lib") # Ignore meaningless for us linker warnings. string(APPEND PLATFORM_LINKFLAGS " /ignore:4049 /ignore:4217 /ignore:4221") diff --git a/extern/CMakeLists.txt b/extern/CMakeLists.txt index 418d2255a75..6af24c092cc 100644 --- a/extern/CMakeLists.txt +++ b/extern/CMakeLists.txt @@ -21,11 +21,17 @@ elseif(CMAKE_C_COMPILER_ID MATCHES "Clang") endif() if(WITH_COMPILER_ASAN AND NOT WITH_COMPILER_ASAN_EXTERN) - # Disable ASAN for extern dependencies, as it can lead to linking issues due to too large binaries. - string(APPEND CMAKE_CXX_FLAGS_RELWITHDEBINFO " -fno-sanitize=all") - string(APPEND CMAKE_C_FLAGS_RELWITHDEBINFO " -fno-sanitize=all") - string(APPEND CMAKE_CXX_FLAGS_DEBUG " -fno-sanitize=all") - string(APPEND CMAKE_C_FLAGS_DEBUG " -fno-sanitize=all") + # Not only does MSVC not have an -fno-sanitize=all option, if you remove the /fsanitize=address + # flag at this point, it will give a linker error as it generates an ODR violation for some + # vector classes, for details see : + # https://learn.microsoft.com/en-us/cpp/sanitizers/error-container-overflow?view=msvc-170 + if(NOT MSVC) + # Disable ASAN for extern dependencies, as it can lead to linking issues due to too large binaries. + string(APPEND CMAKE_CXX_FLAGS_RELWITHDEBINFO " -fno-sanitize=all") + string(APPEND CMAKE_C_FLAGS_RELWITHDEBINFO " -fno-sanitize=all") + string(APPEND CMAKE_CXX_FLAGS_DEBUG " -fno-sanitize=all") + string(APPEND CMAKE_C_FLAGS_DEBUG " -fno-sanitize=all") + endif() endif()