diff --git a/llvm/cmake/modules/HandleLLVMOptions.cmake b/llvm/cmake/modules/HandleLLVMOptions.cmake --- a/llvm/cmake/modules/HandleLLVMOptions.cmake +++ b/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,23 +925,18 @@ message(FATAL_ERROR "This sanitizer not yet supported in a MinGW environment: ${LLVM_USE_SANITIZER}") endif() elseif(MSVC) - if (LLVM_USE_SANITIZER STREQUAL "Address") - append_common_sanitizer_flags() - append("/fsanitize=address" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) - if (NOT CLANG_CL) - # 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() + if (NOT LLVM_USE_SANITIZER MATCHES "^(Address|Undefined|Address;Undefined|Undefined;Address)$") + message(FATAL_ERROR "This sanitizer not yet supported in the MSVC environment: ${LLVM_USE_SANITIZER}") + endif() + append_common_sanitizer_flags() + if (LINKER_IS_LLD_LINK) + if (LLVM_HOST_TRIPLE MATCHES "i[2-6]86-.*") + set(arch "i386") + else() + set(arch "x86_64") endif() - if (LINKER_IS_LLD_LINK) - if (LLVM_HOST_TRIPLE MATCHES "i[2-6]86-.*") - set(arch "i386") - else() - set(arch "x86_64") - endif() + # Prepare ASAN runtime if needed + if (LLVM_USE_SANITIZER MATCHES ".*Address.*") 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) @@ -952,8 +947,26 @@ CMAKE_EXE_LINKER_FLAGS CMAKE_MODULE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS) endif() endif() - else() - message(FATAL_ERROR "This sanitizer not yet supported in the MSVC environment: ${LLVM_USE_SANITIZER}") + endif() + if (LLVM_USE_SANITIZER MATCHES ".*Address.*") + 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() + endif() + if (LLVM_USE_SANITIZER MATCHES ".*Undefined.*") + if (NOT CLANG_CL) + message(FATAL_ERROR "This sanitizer only supported in the clang-cl: Undefined") + endif() + append("-D_ITERATOR_DEBUG_LEVEL=0" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) + append(${LLVM_UBSAN_FLAGS} CMAKE_C_FLAGS CMAKE_CXX_FLAGS) endif() else() message(FATAL_ERROR "LLVM_USE_SANITIZER is not supported on this platform.") diff --git a/llvm/utils/sanitizers/ubsan_ignorelist.txt b/llvm/utils/sanitizers/ubsan_ignorelist.txt --- a/llvm/utils/sanitizers/ubsan_ignorelist.txt +++ b/llvm/utils/sanitizers/ubsan_ignorelist.txt @@ -14,3 +14,10 @@ # libstdc++ 8.0.1 casts an under-aligned pointer to type type_info: # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85930 src:*bits/shared_ptr_base.h + +# UBSAN reports false-positive for 0-sized objects +# https://github.com/llvm/llvm-project/issues/61775 +src:*/ADT/StringMap.h +src:*/Support/AllocatorBase.h +src:*/ADT/StringMapEntry.h +src:*/ADT/SmallPtrSet.h \ No newline at end of file