1*da0073e9SAndroid Build Coastguard Worker# Find sanitizers 2*da0073e9SAndroid Build Coastguard Worker# 3*da0073e9SAndroid Build Coastguard Worker# This module sets the following targets: 4*da0073e9SAndroid Build Coastguard Worker# Sanitizer::address 5*da0073e9SAndroid Build Coastguard Worker# Sanitizer::thread 6*da0073e9SAndroid Build Coastguard Worker# Sanitizer::undefined 7*da0073e9SAndroid Build Coastguard Worker# Sanitizer::leak 8*da0073e9SAndroid Build Coastguard Worker# Sanitizer::memory 9*da0073e9SAndroid Build Coastguard Workerinclude_guard(GLOBAL) 10*da0073e9SAndroid Build Coastguard Worker 11*da0073e9SAndroid Build Coastguard Workeroption(UBSAN_FLAGS "additional UBSAN flags" OFF) 12*da0073e9SAndroid Build Coastguard Worker 13*da0073e9SAndroid Build Coastguard Workerget_property(languages GLOBAL PROPERTY ENABLED_LANGUAGES) 14*da0073e9SAndroid Build Coastguard Worker 15*da0073e9SAndroid Build Coastguard Workerset(_source_code 16*da0073e9SAndroid Build Coastguard Worker [==[ 17*da0073e9SAndroid Build Coastguard Worker #include <stdio.h> 18*da0073e9SAndroid Build Coastguard Worker int main() { 19*da0073e9SAndroid Build Coastguard Worker printf("hello world!"); 20*da0073e9SAndroid Build Coastguard Worker return 0; 21*da0073e9SAndroid Build Coastguard Worker } 22*da0073e9SAndroid Build Coastguard Worker ]==]) 23*da0073e9SAndroid Build Coastguard Worker 24*da0073e9SAndroid Build Coastguard Workerinclude(CMakePushCheckState) 25*da0073e9SAndroid Build Coastguard Workercmake_push_check_state(RESET) 26*da0073e9SAndroid Build Coastguard Workerforeach(sanitizer_name IN ITEMS address thread undefined leak memory) 27*da0073e9SAndroid Build Coastguard Worker if(TARGET Sanitizer::${sanitizer_name}) 28*da0073e9SAndroid Build Coastguard Worker continue() 29*da0073e9SAndroid Build Coastguard Worker endif() 30*da0073e9SAndroid Build Coastguard Worker 31*da0073e9SAndroid Build Coastguard Worker set(CMAKE_REQUIRED_FLAGS 32*da0073e9SAndroid Build Coastguard Worker "-fsanitize=${sanitizer_name};-fno-omit-frame-pointer") 33*da0073e9SAndroid Build Coastguard Worker if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC" OR CMAKE_C_COMPILER_ID STREQUAL 34*da0073e9SAndroid Build Coastguard Worker "MSVC") 35*da0073e9SAndroid Build Coastguard Worker if(sanitizer_name STREQUAL "address") 36*da0073e9SAndroid Build Coastguard Worker set(CMAKE_REQUIRED_FLAGS "/fsanitize=${sanitizer_name}") 37*da0073e9SAndroid Build Coastguard Worker else() 38*da0073e9SAndroid Build Coastguard Worker continue() 39*da0073e9SAndroid Build Coastguard Worker endif() 40*da0073e9SAndroid Build Coastguard Worker endif() 41*da0073e9SAndroid Build Coastguard Worker if(sanitizer_name STREQUAL "address") 42*da0073e9SAndroid Build Coastguard Worker if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR CMAKE_C_COMPILER_ID STREQUAL 43*da0073e9SAndroid Build Coastguard Worker "Clang") 44*da0073e9SAndroid Build Coastguard Worker list(APPEND CMAKE_REQUIRED_FLAGS "-shared-libasan") 45*da0073e9SAndroid Build Coastguard Worker endif() 46*da0073e9SAndroid Build Coastguard Worker endif() 47*da0073e9SAndroid Build Coastguard Worker if(sanitizer_name STREQUAL "undefined" AND UBSAN_FLAGS) 48*da0073e9SAndroid Build Coastguard Worker list(APPEND CMAKE_REQUIRED_FLAGS "${UBSAN_FLAGS}") 49*da0073e9SAndroid Build Coastguard Worker endif() 50*da0073e9SAndroid Build Coastguard Worker if(sanitizer_name STREQUAL "memory") 51*da0073e9SAndroid Build Coastguard Worker list(APPEND CMAKE_REQUIRED_FLAGS "-fsanitize-memory-track-origins=2") 52*da0073e9SAndroid Build Coastguard Worker endif() 53*da0073e9SAndroid Build Coastguard Worker 54*da0073e9SAndroid Build Coastguard Worker set(CMAKE_REQUIRED_QUIET ON) 55*da0073e9SAndroid Build Coastguard Worker set(_run_res 0) 56*da0073e9SAndroid Build Coastguard Worker include(CheckCSourceRuns) 57*da0073e9SAndroid Build Coastguard Worker include(CheckCXXSourceRuns) 58*da0073e9SAndroid Build Coastguard Worker foreach(lang IN LISTS languages) 59*da0073e9SAndroid Build Coastguard Worker if(lang STREQUAL C) 60*da0073e9SAndroid Build Coastguard Worker check_c_source_runs("${_source_code}" 61*da0073e9SAndroid Build Coastguard Worker __${lang}_${sanitizer_name}_res) 62*da0073e9SAndroid Build Coastguard Worker if(__${lang}_${sanitizer_name}_res) 63*da0073e9SAndroid Build Coastguard Worker set(_run_res 1) 64*da0073e9SAndroid Build Coastguard Worker endif() 65*da0073e9SAndroid Build Coastguard Worker endif() 66*da0073e9SAndroid Build Coastguard Worker if(lang STREQUAL CXX) 67*da0073e9SAndroid Build Coastguard Worker check_cxx_source_runs("${_source_code}" 68*da0073e9SAndroid Build Coastguard Worker __${lang}_${sanitizer_name}_res) 69*da0073e9SAndroid Build Coastguard Worker if(__${lang}_${sanitizer_name}_res) 70*da0073e9SAndroid Build Coastguard Worker set(_run_res 1) 71*da0073e9SAndroid Build Coastguard Worker endif() 72*da0073e9SAndroid Build Coastguard Worker endif() 73*da0073e9SAndroid Build Coastguard Worker endforeach() 74*da0073e9SAndroid Build Coastguard Worker if(_run_res) 75*da0073e9SAndroid Build Coastguard Worker add_library(Sanitizer::${sanitizer_name} INTERFACE IMPORTED GLOBAL) 76*da0073e9SAndroid Build Coastguard Worker target_compile_options( 77*da0073e9SAndroid Build Coastguard Worker Sanitizer::${sanitizer_name} 78*da0073e9SAndroid Build Coastguard Worker INTERFACE 79*da0073e9SAndroid Build Coastguard Worker $<$<AND:$<COMPILE_LANGUAGE:CXX>,$<BOOL:$__CXX_${sanitizer_name}_res>>:${CMAKE_REQUIRED_FLAGS}> 80*da0073e9SAndroid Build Coastguard Worker $<$<AND:$<COMPILE_LANGUAGE:C>,$<BOOL:$__C_${sanitizer_name}_res>>:${CMAKE_REQUIRED_FLAGS}> 81*da0073e9SAndroid Build Coastguard Worker ) 82*da0073e9SAndroid Build Coastguard Worker if(NOT CMAKE_CXX_COMPILER_ID STREQUAL "MSVC" AND NOT CMAKE_C_COMPILER_ID 83*da0073e9SAndroid Build Coastguard Worker STREQUAL "MSVC") 84*da0073e9SAndroid Build Coastguard Worker target_link_options( 85*da0073e9SAndroid Build Coastguard Worker Sanitizer::${sanitizer_name} 86*da0073e9SAndroid Build Coastguard Worker INTERFACE 87*da0073e9SAndroid Build Coastguard Worker $<$<AND:$<COMPILE_LANGUAGE:CXX>,$<BOOL:$__CXX_${sanitizer_name}_res>>:${CMAKE_REQUIRED_FLAGS}> 88*da0073e9SAndroid Build Coastguard Worker $<$<AND:$<COMPILE_LANGUAGE:C>,$<BOOL:$__C_${sanitizer_name}_res>>:${CMAKE_REQUIRED_FLAGS}> 89*da0073e9SAndroid Build Coastguard Worker ) 90*da0073e9SAndroid Build Coastguard Worker else() 91*da0073e9SAndroid Build Coastguard Worker target_link_options( 92*da0073e9SAndroid Build Coastguard Worker Sanitizer::${sanitizer_name} 93*da0073e9SAndroid Build Coastguard Worker INTERFACE 94*da0073e9SAndroid Build Coastguard Worker $<$<AND:$<COMPILE_LANGUAGE:CXX>,$<BOOL:$__CXX_${sanitizer_name}_res>>:/INCREMENTAL:NO> 95*da0073e9SAndroid Build Coastguard Worker $<$<AND:$<COMPILE_LANGUAGE:C>,$<BOOL:$__C_${sanitizer_name}_res>>:/INCREMENTAL:NO> 96*da0073e9SAndroid Build Coastguard Worker ) 97*da0073e9SAndroid Build Coastguard Worker endif() 98*da0073e9SAndroid Build Coastguard Worker 99*da0073e9SAndroid Build Coastguard Worker if(sanitizer_name STREQUAL "address") 100*da0073e9SAndroid Build Coastguard Worker target_compile_definitions( 101*da0073e9SAndroid Build Coastguard Worker Sanitizer::${sanitizer_name} 102*da0073e9SAndroid Build Coastguard Worker INTERFACE 103*da0073e9SAndroid Build Coastguard Worker $<$<AND:$<COMPILE_LANGUAGE:CXX>,$<BOOL:$__CXX_${sanitizer_name}_res>>:_GLIBCXX_SANITIZE_VECTOR> 104*da0073e9SAndroid Build Coastguard Worker $<$<AND:$<COMPILE_LANGUAGE:CXX>,$<BOOL:$__CXX_${sanitizer_name}_res>>:_GLIBCXX_SANITIZE_STD_ALLOCATOR> 105*da0073e9SAndroid Build Coastguard Worker ) 106*da0073e9SAndroid Build Coastguard Worker target_link_options( 107*da0073e9SAndroid Build Coastguard Worker Sanitizer::${sanitizer_name} 108*da0073e9SAndroid Build Coastguard Worker INTERFACE 109*da0073e9SAndroid Build Coastguard Worker $<$<AND:$<COMPILE_LANGUAGE:CXX>,$<BOOL:$__CXX_${sanitizer_name}_res>,$<CXX_COMPILER_ID:GNU>>:-lasan> 110*da0073e9SAndroid Build Coastguard Worker $<$<AND:$<COMPILE_LANGUAGE:C>,$<BOOL:$__C_${sanitizer_name}_res>,$<C_COMPILER_ID:GNU>>:-lasan> 111*da0073e9SAndroid Build Coastguard Worker ) 112*da0073e9SAndroid Build Coastguard Worker endif() 113*da0073e9SAndroid Build Coastguard Worker if(sanitizer_name STREQUAL "undefined") 114*da0073e9SAndroid Build Coastguard Worker target_link_options( 115*da0073e9SAndroid Build Coastguard Worker Sanitizer::${sanitizer_name} 116*da0073e9SAndroid Build Coastguard Worker INTERFACE 117*da0073e9SAndroid Build Coastguard Worker $<$<AND:$<COMPILE_LANGUAGE:CXX>,$<BOOL:$__CXX_${sanitizer_name}_res>,$<CXX_COMPILER_ID:GNU>>:-lubsan> 118*da0073e9SAndroid Build Coastguard Worker $<$<AND:$<COMPILE_LANGUAGE:C>,$<BOOL:$__C_${sanitizer_name}_res>,$<C_COMPILER_ID:GNU>>:-lubsan> 119*da0073e9SAndroid Build Coastguard Worker ) 120*da0073e9SAndroid Build Coastguard Worker endif() 121*da0073e9SAndroid Build Coastguard Worker endif() 122*da0073e9SAndroid Build Coastguard Workerendforeach() 123*da0073e9SAndroid Build Coastguard Worker 124*da0073e9SAndroid Build Coastguard Workercmake_pop_check_state() 125