2018-08-08 03:37:35 +00:00
option ( SANITIZE "Enable sanitizer: address, memory, thread, undefined" "" )
2018-02-26 18:34:22 +00:00
2018-08-08 03:37:35 +00:00
set ( SAN_FLAGS "${SAN_FLAGS} -g -fno-omit-frame-pointer -DSANITIZER" )
2018-02-26 18:34:22 +00:00
2018-08-08 03:37:35 +00:00
if ( SANITIZE )
if ( SANITIZE STREQUAL "address" )
2018-09-02 19:44:34 +00:00
set ( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${SAN_FLAGS} -fsanitize=address -fsanitize-address-use-after-scope" )
set ( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${SAN_FLAGS} -fsanitize=address -fsanitize-address-use-after-scope" )
2018-12-28 23:42:39 +00:00
if ( CMAKE_CXX_COMPILER_ID STREQUAL "GNU" )
set ( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=address -fsanitize-address-use-after-scope" )
endif ( )
2018-08-08 03:37:35 +00:00
if ( MAKE_STATIC_LIBRARIES AND CMAKE_CXX_COMPILER_ID STREQUAL "GNU" )
set ( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libasan" )
endif ( )
2018-12-28 23:42:39 +00:00
2018-08-08 03:37:35 +00:00
elseif ( SANITIZE STREQUAL "memory" )
2018-12-28 04:40:28 +00:00
set ( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${SAN_FLAGS} -fsanitize=memory -fsanitize-memory-track-origins" )
set ( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${SAN_FLAGS} -fsanitize=memory -fsanitize-memory-track-origins" )
2018-12-28 23:42:39 +00:00
if ( CMAKE_CXX_COMPILER_ID STREQUAL "GNU" )
set ( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=memory" )
endif ( )
2018-08-08 03:37:35 +00:00
if ( MAKE_STATIC_LIBRARIES AND CMAKE_CXX_COMPILER_ID STREQUAL "GNU" )
set ( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libmsan" )
endif ( )
2018-12-28 23:42:39 +00:00
2018-08-08 03:37:35 +00:00
elseif ( SANITIZE STREQUAL "thread" )
set ( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${SAN_FLAGS} -fsanitize=thread" )
set ( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${SAN_FLAGS} -fsanitize=thread" )
2018-12-28 23:42:39 +00:00
if ( CMAKE_CXX_COMPILER_ID STREQUAL "GNU" )
set ( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=thread" )
endif ( )
2018-08-08 03:37:35 +00:00
if ( MAKE_STATIC_LIBRARIES AND CMAKE_CXX_COMPILER_ID STREQUAL "GNU" )
set ( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libtsan" )
endif ( )
2018-12-28 23:42:39 +00:00
2018-08-08 03:37:35 +00:00
elseif ( SANITIZE STREQUAL "undefined" )
2018-09-02 19:47:04 +00:00
set ( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${SAN_FLAGS} -fsanitize=undefined -fno-sanitize-recover=all" )
set ( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${SAN_FLAGS} -fsanitize=undefined -fno-sanitize-recover=all" )
2018-12-28 23:42:39 +00:00
if ( CMAKE_CXX_COMPILER_ID STREQUAL "GNU" )
set ( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=undefined" )
endif ( )
2018-08-08 03:37:35 +00:00
if ( MAKE_STATIC_LIBRARIES AND CMAKE_CXX_COMPILER_ID STREQUAL "GNU" )
set ( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libubsan" )
endif ( )
2019-09-22 10:34:09 +00:00
elseif ( SANITIZE STREQUAL "libfuzzer" )
2019-09-22 21:21:41 +00:00
# NOTE: Eldar Zaitov decided to name it "libfuzzer" instead of "fuzzer" to keep in mind another possible fuzzer backends.
# NOTE: no-link means that all the targets are built with instrumentation for fuzzer, but only some of them (tests) have entry point for fuzzer and it's not checked.
set ( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${SAN_FLAGS} -fsanitize=fuzzer-no-link,address,undefined -fsanitize-address-use-after-scope" )
set ( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${SAN_FLAGS} -fsanitize=fuzzer-no-link,address,undefined -fsanitize-address-use-after-scope" )
2019-09-22 10:34:09 +00:00
if ( CMAKE_CXX_COMPILER_ID STREQUAL "GNU" )
2019-09-22 21:22:22 +00:00
set ( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=fuzzer-no-link,address,undefined -fsanitize-address-use-after-scope" )
2019-09-22 10:34:09 +00:00
endif ( )
if ( MAKE_STATIC_LIBRARIES AND CMAKE_CXX_COMPILER_ID STREQUAL "GNU" )
2019-09-22 21:22:22 +00:00
set ( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libasan -static-libubsan" )
2019-09-22 10:34:09 +00:00
endif ( )
2019-09-22 21:22:22 +00:00
set ( LIBFUZZER_CMAKE_CXX_FLAGS "-fsanitize=fuzzer,address,undefined -fsanitize-address-use-after-scope" )
2018-08-08 03:37:35 +00:00
else ( )
message ( FATAL_ERROR "Unknown sanitizer type: ${SANITIZE}" )
endif ( )
endif ( )