Skip to content

Commit

Permalink
build: check the combination of Sanitizers
Browse files Browse the repository at this point in the history
we are going to add a build with ThreadSanitizer enabled, but
ThreadSanitizer cannot be used along with AddressSanitizer. the
existing sanitizer detection checks each sanitizer separately, so
even if the sanitizers combination does not work, the detection
still passes.

also, we hardwire two sanitizers in `FindSanitizers.cmake`, this
is not extensible to the use case where we only need to selectively
find a certain (combination) of sanitizers.

in order to address these problems, in this change

* find specified component(s) in FindSanitizers.cmake, to prepare for
  the change which selectively specifies a subset of sanitizers
* check if the compiler supports the combination of compile options
  required by all specified sanitizers
* explicitly specify the used sanitizers in `SeastarDependencies.cmake`.

Signed-off-by: Kefu Chai <[email protected]>
  • Loading branch information
tchaikov committed Aug 28, 2024
1 parent ec5da7a commit c50d312
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 40 deletions.
83 changes: 43 additions & 40 deletions cmake/FindSanitizers.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -20,54 +20,57 @@
# Copyright (C) 2018 Scylladb, Ltd.
#

include (CheckCXXSourceCompiles)

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)
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 ()
4 changes: 4 additions & 0 deletions cmake/SeastarDependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ macro (seastar_find_dependencies)
seastar_set_dep_args (rt REQUIRED)
seastar_set_dep_args (numactl
OPTION ${Seastar_NUMA})
seastar_set_dep_args (Sanitizers
COMPONENTS
address
undefined_behavior)
seastar_set_dep_args (ucontext REQUIRED)
seastar_set_dep_args (yaml-cpp REQUIRED
VERSION 0.5.1)
Expand Down

0 comments on commit c50d312

Please sign in to comment.