diff --git a/cmake/compiler.cmake b/cmake/compiler.cmake index dd6f71c8..1d805ea0 100644 --- a/cmake/compiler.cmake +++ b/cmake/compiler.cmake @@ -348,6 +348,8 @@ endif() if(MSVC) check_compiler_flag("/WX" CC_HAS_WERROR) + check_compiler_flag("/fsanitize=address" CC_HAS_ASAN) + check_compiler_flag("/fsanitize=undefined" CC_HAS_UBSAN) else() # # GCC started to warn for unused result starting from 4.2, and @@ -839,19 +841,26 @@ macro(setup_compile_flags) endif() if(ENABLE_ASAN) - add_compile_flags("C;CXX" "-fsanitize=address") + if(NOT MSVC) + add_compile_flags("C;CXX" "-fsanitize=address") + else() + add_compile_flags("C;CXX" "/fsanitize=address") + endif() add_definitions(-DASAN_ENABLED=1) endif() if(ENABLE_UBSAN) - add_compile_flags("C;CXX" "-fsanitize=undefined" "-fsanitize-undefined-trap-on-error") + if(NOT MSVC) + add_compile_flags("C;CXX" "-fsanitize=undefined" "-fsanitize-undefined-trap-on-error") + else() + add_compile_flags("C;CXX" "/fsanitize=undefined") + endif() add_definitions(-DUBSAN_ENABLED=1) endif() if(ENABLE_GCOV) if(NOT HAVE_GCOV) - message(FATAL_ERROR - "ENABLE_GCOV option requested but gcov library is not found") + message(FATAL_ERROR "ENABLE_GCOV option requested but gcov library is not found") endif() add_compile_flags("C;CXX" "-fprofile-arcs" "-ftest-coverage") diff --git a/src/core.c b/src/core.c index 040217ad..3f52d4e2 100644 --- a/src/core.c +++ b/src/core.c @@ -25654,7 +25654,10 @@ __dll_export }; #ifdef __SANITIZE_ADDRESS__ -LIBMDBX_API __attribute__((__weak__)) const char *__asan_default_options(void) { +#if !defined(_MSC_VER) || __has_attribute(weak) +LIBMDBX_API __attribute__((__weak__)) +#endif +const char *__asan_default_options(void) { return "symbolize=1:allow_addr2line=1:" #if MDBX_DEBUG "debug=1:" diff --git a/src/mdbx.c++ b/src/mdbx.c++ index 8a74e412..590cc07d 100644 --- a/src/mdbx.c++ +++ b/src/mdbx.c++ @@ -14,6 +14,12 @@ #define __USE_MINGW_ANSI_STDIO 1 #endif /* MinGW */ +/* Workaround for MSVC' header `extern "C"` vs `std::` redefinition bug */ +#if defined(_MSC_VER) && defined(__SANITIZE_ADDRESS__) && \ + !defined(_DISABLE_VECTOR_ANNOTATION) +#define _DISABLE_VECTOR_ANNOTATION +#endif /* _DISABLE_VECTOR_ANNOTATION */ + #include "../mdbx.h++" #include "internals.h" diff --git a/test/base.h++ b/test/base.h++ index f4e083bd..f3a7701e 100644 --- a/test/base.h++ +++ b/test/base.h++ @@ -30,6 +30,10 @@ #define _WIN32_WINNT 0x0601 /* Windows 7 */ #endif #ifdef _MSC_VER +/* Workaround for MSVC' header `extern "C"` vs `std::` redefinition bug */ +#if defined(__SANITIZE_ADDRESS__) && !defined(_DISABLE_VECTOR_ANNOTATION) +#define _DISABLE_VECTOR_ANNOTATION +#endif /* _DISABLE_VECTOR_ANNOTATION */ #ifndef _CRT_SECURE_NO_WARNINGS #define _CRT_SECURE_NO_WARNINGS #endif /* _CRT_SECURE_NO_WARNINGS */