cmake-sanitizers/cmake/FindSanitizers.cmake

132 lines
4.3 KiB
CMake
Raw Normal View History

# The MIT License (MIT)
#
# Copyright (c)
# 2013 Matthew Arsenault
# 2015-2016 RWTH Aachen University, Federal Republic of Germany
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# If any of the used compiler is a GNU compiler, add a second option to static
# link against the sanitizers.
option(SANITIZE_LINK_STATIC "Try to link static against sanitizers." Off)
set(FIND_QUIETLY_FLAG "")
if (DEFINED Sanitizers_FIND_QUIETLY)
2019-08-10 07:33:41 +00:00
set(FIND_QUIETLY_FLAG "QUIET")
endif ()
find_package(ASan ${FIND_QUIETLY_FLAG})
2016-04-06 17:15:25 +00:00
find_package(TSan ${FIND_QUIETLY_FLAG})
find_package(MSan ${FIND_QUIETLY_FLAG})
find_package(UBSan ${FIND_QUIETLY_FLAG})
2019-07-02 06:47:11 +00:00
find_package(LeakSan ${FIND_QUIETLY_FLAG})
find_package(CFI ${FIND_QUIETLY_FLAG})
find_package(SS ${FIND_QUIETLY_FLAG})
set(Sanitizers_COMPONENTS "")
if(ASan_FOUND)
2019-08-10 07:33:41 +00:00
set(Sanitizers_ASan_FOUND TRUE)
list(APPEND Sanitizers_COMPONENTS ASan)
endif()
if(TSan_FOUND)
2019-08-10 07:33:41 +00:00
set(Sanitizers_TSan_FOUND TRUE)
list(APPEND Sanitizers_COMPONENTS TSan)
endif()
if(MSan_FOUND)
2019-08-10 07:33:41 +00:00
set(Sanitizers_MSan_FOUND TRUE)
list(APPEND Sanitizers_COMPONENTS MSan)
endif()
if(UBSan_FOUND)
2019-08-10 07:33:41 +00:00
set(Sanitizers_UBSan_FOUND TRUE)
list(APPEND Sanitizers_COMPONENTS UBSan)
endif()
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(
2019-08-10 07:33:41 +00:00
Sanitizers
FOUND_VAR Sanitizers_FOUND
REQUIRED_VARS Sanitizers_COMPONENTS
HANDLE_COMPONENTS
)
function(sanitizer_add_blacklist_file FILE)
2019-08-10 07:33:41 +00:00
if(NOT IS_ABSOLUTE ${FILE})
set(FILE "${CMAKE_CURRENT_SOURCE_DIR}/${FILE}")
endif()
get_filename_component(FILE "${FILE}" REALPATH)
2019-08-10 07:33:41 +00:00
sanitizer_check_compiler_flags("-fsanitize-blacklist=${FILE}"
"SanitizerBlacklist" "SanBlist")
endfunction()
function(add_sanitizers ...)
2019-08-10 07:33:41 +00:00
# If no sanitizer is enabled, return immediately.
if (NOT (SANITIZE_ADDRESS OR SANITIZE_MEMORY OR SANITIZE_THREAD OR
SANITIZE_UNDEFINED OR SANITIZE_CFI OR SANITIZE_SS))
message(STATUS "No sanitizer selected.")
2019-08-10 07:33:41 +00:00
return()
endif ()
foreach (TARGET ${ARGV})
# Check if this target will be compiled by exactly one compiler. Other-
# wise sanitizers can't be used and a warning should be printed once.
get_target_property(TARGET_TYPE ${TARGET} TYPE)
if (TARGET_TYPE STREQUAL "INTERFACE_LIBRARY")
message(STATUS "Can't use any sanitizers for target ${TARGET}, "
2019-08-10 07:33:41 +00:00
"because it is an interface library and cannot be "
"compiled directly.")
return()
endif ()
sanitizer_target_compilers(${TARGET} TARGET_COMPILER)
list(LENGTH TARGET_COMPILER NUM_COMPILERS)
if (NUM_COMPILERS GREATER 1)
message(STATUS "Can't use any sanitizers for target ${TARGET}, "
2019-08-10 07:33:41 +00:00
"because it will be compiled by incompatible compilers. "
"Target will be compiled without sanitizers.")
return()
# If the target is compiled by no or no known compiler, give a warning.
elseif (NUM_COMPILERS EQUAL 0)
message(STATUS "Sanitizers for target ${TARGET} may not be"
2019-08-10 07:33:41 +00:00
" usable, because it uses no or an unknown compiler. "
"This is a false warning for targets using only "
"object lib(s) as input.")
endif ()
2019-08-10 07:33:41 +00:00
# Add sanitizers for target.
add_sanitize_address(${TARGET})
add_sanitize_thread(${TARGET})
add_sanitize_memory(${TARGET})
add_sanitize_undefined(${TARGET})
add_sanitize_leak(${TARGET})
add_sanitize_cfi(${TARGET})
add_sanitize_ss(${TARGET})
2019-08-10 07:33:41 +00:00
endforeach ()
endfunction(add_sanitizers)