Commit 1f952df4 authored by Vladislav Vaintroub's avatar Vladislav Vaintroub

MDEV-22612 Fix -DWITH_ASAN=1 on Windows.

parent 9f8e5558
...@@ -63,9 +63,9 @@ IF(MINGW AND CMAKE_SIZEOF_VOID_P EQUAL 4) ...@@ -63,9 +63,9 @@ IF(MINGW AND CMAKE_SIZEOF_VOID_P EQUAL 4)
ADD_DEFINITIONS(-march=i486) ADD_DEFINITIONS(-march=i486)
ENDIF() ENDIF()
FUNCTION(ENABLE_ASAN) MACRO(ENABLE_SANITIZERS)
IF(NOT CLANG_CL) IF(NOT MSVC)
MESSAGE(FATAL_ERROR "clang-cl is necessary to enable asan") MESSAGE(FATAL_ERROR "clang-cl or MSVC necessary to enable asan/ubsan")
ENDIF() ENDIF()
# currently, asan is broken with static CRT. # currently, asan is broken with static CRT.
IF(NOT(MSVC_CRT_TYPE STREQUAL "/MD")) IF(NOT(MSVC_CRT_TYPE STREQUAL "/MD"))
...@@ -74,25 +74,38 @@ FUNCTION(ENABLE_ASAN) ...@@ -74,25 +74,38 @@ FUNCTION(ENABLE_ASAN)
ENDIF() ENDIF()
ENDIF() ENDIF()
IF(CMAKE_SIZEOF_VOID_P EQUAL 4) IF(CMAKE_SIZEOF_VOID_P EQUAL 4)
MESSAGE(FATAL_ERROR "-DWITH_ASAN on Windows requires 64bit build") SET(ASAN_ARCH i386)
ELSE()
IF(NOT CLANG_CL)
MESSAGE(FATAL_ERROR "sanitizers do not yet work on MSVC x64, try 32 bit or clang-cl")
ENDIF()
SET(ASAN_ARCH x86_64)
ENDIF() ENDIF()
# After installation, clang lib directory should be added to PATH
# After installation, clang lib directory should be added to PATH
# (e.g C:/Program Files/LLVM/lib/clang/5.0.1/lib/windows) # (e.g C:/Program Files/LLVM/lib/clang/5.0.1/lib/windows)
FIND_LIBRARY(CLANG_RT_ASAN_DYNAMIC clang_rt.asan_dynamic-x86_64.lib) SET(SANITIZER_LIBS)
IF(NOT CLANG_RT_ASAN_DYNAMIC) SET(SANITIZER_LINK_LIBRARIES)
MESSAGE(FATAL_ERROR "Can't enable ASAN : missing clang_rt.asan_dynamic-x86_64.lib") SET(SANITIZER_COMPILE_FLAGS)
IF(WITH_ASAN)
LIST(APPEND SANITIZER_LIBS
clang_rt.asan_dynamic-${ASAN_ARCH}.lib clang_rt.asan_dynamic_runtime_thunk-${ASAN_ARCH}.lib)
STRING(APPEND SANITIZER_COMPILE_FLAGS " -fsanitize=address")
ENDIF() ENDIF()
IF(WITH_UBSAN)
FIND_LIBRARY(CLANG_RT_ASAN_DYNAMIC_THUNK clang_rt.asan_dynamic_runtime_thunk-x86_64.lib) STRING(APPEND SANITIZER_COMPILE_FLAGS " -fsanitize=undefined -fno-sanitize=alignment")
IF(NOT CLANG_RT_ASAN_DYNAMIC_THUNK)
MESSAGE(FATAL_ERROR "Can't enable ASAN : missing clang_rt.asan_dynamic_runtime_thunk-x86_64.lib")
ENDIF() ENDIF()
FOREACH(lib ${SANITIZER_LIBS})
FIND_LIBRARY(${lib}_fullpath ${lib})
IF(NOT ${lib}_fullpath)
MESSAGE(FATAL_ERROR "Can't enable sanitizer : missing ${lib}")
ENDIF()
LIST(APPEND CMAKE_REQUIRED_LIBRARIES ${${lib}_fullpath})
STRING(APPEND CMAKE_C_STANDARD_LIBRARIES " \"${${lib}_fullpath}\" ")
STRING(APPEND CMAKE_CXX_STANDARD_LIBRARIES " \"${${lib}_fullpath}\" ")
ENDFOREACH()
STRING(APPEND CMAKE_C_FLAGS " -fsanitize=address") ENDMACRO()
STRING(APPEND CMAKE_CXX_FLAGS " -fsanitize=address")
LINK_LIBRARIES(${CLANG_RT_ASAN_DYNAMIC} ${CLANG_RT_ASAN_DYNAMIC_THUNK})
ENDFUNCTION()
IF(MSVC) IF(MSVC)
...@@ -120,12 +133,6 @@ IF(MSVC) ...@@ -120,12 +133,6 @@ IF(MSVC)
OPTION(DYNAMIC_UCRT_LINK "Link Universal CRT dynamically, if MSVC_CRT_TYPE=/MT" ON) OPTION(DYNAMIC_UCRT_LINK "Link Universal CRT dynamically, if MSVC_CRT_TYPE=/MT" ON)
SET(DYNAMIC_UCRT_LINKER_OPTION " /NODEFAULTLIB:libucrt.lib /DEFAULTLIB:ucrt.lib") SET(DYNAMIC_UCRT_LINKER_OPTION " /NODEFAULTLIB:libucrt.lib /DEFAULTLIB:ucrt.lib")
IF(WITH_ASAN)
# Workaround something Linux specific
SET(SECURITY_HARDENED 0 CACHE INTERNAL "" FORCE)
ENABLE_ASAN()
ENDIF()
# Enable debug info also in Release build, # Enable debug info also in Release build,
# and create PDB to be able to analyze crashes. # and create PDB to be able to analyze crashes.
FOREACH(type EXE SHARED MODULE) FOREACH(type EXE SHARED MODULE)
...@@ -141,7 +148,9 @@ IF(MSVC) ...@@ -141,7 +148,9 @@ IF(MSVC)
FOREACH(lang C CXX) FOREACH(lang C CXX)
SET(CMAKE_${lang}_FLAGS_RELEASE "${CMAKE_${lang}_FLAGS_RELEASE} /Zi") SET(CMAKE_${lang}_FLAGS_RELEASE "${CMAKE_${lang}_FLAGS_RELEASE} /Zi")
ENDFOREACH() ENDFOREACH()
FOREACH(flag FOREACH(flag
CMAKE_C_FLAGS CMAKE_CXX_FLAGS
CMAKE_C_FLAGS_INIT CMAKE_CXX_FLAGS_INIT
CMAKE_C_FLAGS_RELEASE CMAKE_C_FLAGS_RELWITHDEBINFO CMAKE_C_FLAGS_RELEASE CMAKE_C_FLAGS_RELWITHDEBINFO
CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_DEBUG_INIT CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_DEBUG_INIT
CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_RELWITHDEBINFO CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_RELWITHDEBINFO
...@@ -155,6 +164,12 @@ IF(MSVC) ...@@ -155,6 +164,12 @@ IF(MSVC)
ENDIF() ENDIF()
ENDFOREACH() ENDFOREACH()
IF(WITH_ASAN OR WITH_UBSAN)
# Workaround something Linux specific
SET(SECURITY_HARDENED 0 CACHE INTERNAL "" FORCE)
ENABLE_SANITIZERS()
ENDIF()
IF(CLANG_CL) IF(CLANG_CL)
SET(CLANG_CL_FLAGS SET(CLANG_CL_FLAGS
"-Wno-unknown-warning-option -Wno-unused-private-field \ "-Wno-unknown-warning-option -Wno-unused-private-field \
...@@ -163,8 +178,8 @@ IF(MSVC) ...@@ -163,8 +178,8 @@ IF(MSVC)
-Wno-deprecated-register -Wno-missing-braces \ -Wno-deprecated-register -Wno-missing-braces \
-Wno-unused-function -Wno-unused-local-typedef -msse4.2 " -Wno-unused-function -Wno-unused-local-typedef -msse4.2 "
) )
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CLANG_CL_FLAGS}") STRING(APPEND CMAKE_C_FLAGS " ${CLANG_CL_FLAGS} ${MSVC_CRT_TYPE}")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CLANG_CL_FLAGS}") STRING(APPEND CMAKE_CXX_FLAGS " ${CLANG_CL_FLAGS} ${MSVC_CRT_TYPE}")
ENDIF() ENDIF()
FOREACH(type EXE SHARED MODULE) FOREACH(type EXE SHARED MODULE)
......
...@@ -24,11 +24,13 @@ MACRO(BUNDLE_PCRE2) ...@@ -24,11 +24,13 @@ MACRO(BUNDLE_PCRE2)
SET(byproducts ${byproducts} BUILD_BYPRODUCTS ${file} ${file_d}) SET(byproducts ${byproducts} BUILD_BYPRODUCTS ${file} ${file_d})
SET_TARGET_PROPERTIES(${lib} PROPERTIES IMPORTED_LOCATION ${file}) SET_TARGET_PROPERTIES(${lib} PROPERTIES IMPORTED_LOCATION ${file})
ENDFOREACH() ENDFOREACH()
FOREACH(v "" "_DEBUG" "_RELWITHDEBINFO" "_RELEASE" "_MINZISEREL") FOREACH(v "" "_DEBUG" "_RELWITHDEBINFO" "_RELEASE" "_MINSIZEREL")
STRING(REPLACE "/WX" "" pcre2_flags${v} "${CMAKE_C_FLAGS${v}}") STRING(REPLACE "/WX" "" pcre2_flags${v} "${CMAKE_C_FLAGS${v}}")
IF(MSVC) IF(MSVC)
# Suppress a warning # Suppress a warning
STRING(APPEND pcre2_flags${v} " /wd4244 " ) STRING(APPEND pcre2_flags${v} " /wd4244 " )
# Need this only for ASAN support
SET(stdlibs "-DCMAKE_C_STANDARD_LIBRARIES=${CMAKE_C_STANDARD_LIBRARIES}")
ENDIF() ENDIF()
ENDFOREACH() ENDFOREACH()
ExternalProject_Add( ExternalProject_Add(
...@@ -48,6 +50,7 @@ MACRO(BUNDLE_PCRE2) ...@@ -48,6 +50,7 @@ MACRO(BUNDLE_PCRE2)
"-DCMAKE_C_FLAGS_RELEASE=${pcre2_flags_RELEASE}" "-DCMAKE_C_FLAGS_RELEASE=${pcre2_flags_RELEASE}"
"-DCMAKE_C_FLAGS_MINSIZEREL=${pcre2_flags_MINSIZEREL}" "-DCMAKE_C_FLAGS_MINSIZEREL=${pcre2_flags_MINSIZEREL}"
"-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}" "-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}"
${stdlibs}
${byproducts} ${byproducts}
) )
SET_TARGET_PROPERTIES(pcre2 PROPERTIES EXCLUDE_FROM_ALL TRUE) SET_TARGET_PROPERTIES(pcre2 PROPERTIES EXCLUDE_FROM_ALL TRUE)
......
IF(NOT MSVC) IF((NOT MSVC) OR CLANG_CL OR WITH_ASAN)
RETURN()
ENDIF()
IF(CMAKE_C_COMPILER_ID MATCHES Clang)
# MFC stuff does not compile with clang
RETURN()
ENDIF()
IF(CMAKE_USING_VC_FREE_TOOLS)
# No MFC, so it cannot be built
RETURN() RETURN()
ENDIF() ENDIF()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment