Skip to content

build: check the combination of Sanitizers #2408

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 48 additions & 39 deletions cmake/FindSanitizers.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -20,54 +20,63 @@
# Copyright (C) 2018 Scylladb, Ltd.
#

include (CheckCXXSourceCompiles)
if(NOT Sanitizers_FIND_COMPONENTS)
set(Sanitizers_FIND_COMPONENTS
address
undefined_behavior)
endif()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this preserves the backward compatibility if some parent projects use find_package(Sanitizers) without specifying the COMPONENTS parameter.


set (CMAKE_REQUIRED_FLAGS -fsanitize=address)
check_cxx_source_compiles ("int main() {}" Sanitizers_ADDRESS_FOUND)

if (Sanitizers_ADDRESS_FOUND)
set (Sanitizers_ADDRESS_COMPILER_OPTIONS -fsanitize=address)
endif ()
foreach (component ${Sanitizers_FIND_COMPONENTS})
string (TOUPPER ${component} COMPONENT)
set (compile_options "Sanitizers_${COMPONENT}_COMPILE_OPTIONS")
if (component STREQUAL "address")
list (APPEND ${compile_options} -fsanitize=address)
elseif (component STREQUAL "undefined_behavior")
list (APPEND ${compile_options} -fsanitize=undefined)
# Disable vptr because of https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88684
list (APPEND ${compile_options} -fno-sanitize=vptr)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: this was fixed in gcc 10 (or perhaps 9?) - gcc-mirror/gcc@c24847a so we can drop it later.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure. will do in a separate PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

created #2430

else ()
message (FATAL_ERROR "Unsupported sanitizer: ${component}")
endif ()
list(APPEND Sanitizers_COMPILE_OPTIONS "${${compile_options}}")
endforeach ()

set (CMAKE_REQUIRED_FLAGS -fsanitize=undefined)
check_cxx_source_compiles ("int main() {}" Sanitizers_UNDEFINED_BEHAVIOR_FOUND)
include(CheckCXXSourceCompiles)
include(CMakePushCheckState)

if (Sanitizers_UNDEFINED_BEHAVIOR_FOUND)
# Disable vptr because of https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88684
set (Sanitizers_UNDEFINED_BEHAVIOR_COMPILER_OPTIONS "-fsanitize=undefined;-fno-sanitize=vptr")
# -fsanitize=address cannot be combined with -fsanitize=thread, so let's test
# the combination of the compiler options.
cmake_push_check_state()
string (REPLACE ";" " " CMAKE_REQUIRED_FLAGS "${Sanitizers_COMPILE_OPTIONS}")
set(CMAKE_REQUIRED_FLAGS ${Sanitizers_COMPILE_OPTIONS})
check_cxx_source_compiles("int main() {}"
Sanitizers_SUPPORTED)
if (Sanitizers_SUPPORTED)
if ("address" IN_LIST Sanitizers_FIND_COMPONENTS)
file (READ ${CMAKE_CURRENT_LIST_DIR}/code_tests/Sanitizers_fiber_test.cc _sanitizers_fiber_test_code)
check_cxx_source_compiles ("${_sanitizers_fiber_test_code}"
Sanitizers_FIBER_SUPPORT)
endif ()
endif ()

set (Sanitizers_COMPILER_OPTIONS
${Sanitizers_ADDRESS_COMPILER_OPTIONS}
${Sanitizers_UNDEFINED_BEHAVIOR_COMPILER_OPTIONS})

file (READ ${CMAKE_CURRENT_LIST_DIR}/code_tests/Sanitizers_fiber_test.cc _sanitizers_fiber_test_code)
set (CMAKE_REQUIRED_FLAGS ${Sanitizers_COMPILER_OPTIONS})
check_cxx_source_compiles ("${_sanitizers_fiber_test_code}" Sanitizers_FIBER_SUPPORT)
cmake_pop_check_state()

include (FindPackageHandleStandardArgs)

find_package_handle_standard_args (Sanitizers
REQUIRED_VARS
Sanitizers_ADDRESS_COMPILER_OPTIONS
Sanitizers_UNDEFINED_BEHAVIOR_COMPILER_OPTIONS)
Sanitizers_COMPILE_OPTIONS
Sanitizers_SUPPORTED)

if (Sanitizers_FOUND)
if (NOT (TARGET Sanitizers::address))
add_library (Sanitizers::address INTERFACE IMPORTED)

set_target_properties (Sanitizers::address
PROPERTIES
INTERFACE_COMPILE_OPTIONS ${Sanitizers_ADDRESS_COMPILER_OPTIONS}
INTERFACE_LINK_LIBRARIES ${Sanitizers_ADDRESS_COMPILER_OPTIONS})
endif ()

if (NOT (TARGET Sanitizers::undefined_behavior))
add_library (Sanitizers::undefined_behavior INTERFACE IMPORTED)

set_target_properties (Sanitizers::undefined_behavior
PROPERTIES
INTERFACE_COMPILE_OPTIONS "${Sanitizers_UNDEFINED_BEHAVIOR_COMPILER_OPTIONS}"
INTERFACE_LINK_LIBRARIES "${Sanitizers_UNDEFINED_BEHAVIOR_COMPILER_OPTIONS}")
endif ()
foreach (component ${Sanitizers_FIND_COMPONENTS})
string (TOUPPER ${component} COMPONENT)
set (library Sanitizers::${component})
if (NOT TARGET ${library})
add_library (${library} INTERFACE IMPORTED)
set_target_properties (${library}
PROPERTIES
INTERFACE_COMPILE_OPTIONS "${Sanitizers_${COMPONENT}_COMPILE_OPTIONS}"
INTERFACE_LINK_LIBRARIES "${Sanitizers_${COMPONENT}_COMPILE_OPTIONS}")
endif ()
endforeach ()
endif ()