2020-12-15 19:59:07 +00:00
|
|
|
# Possible values:
|
|
|
|
# - `address` (ASan)
|
|
|
|
# - `memory` (MSan)
|
|
|
|
# - `thread` (TSan)
|
|
|
|
# - `undefined` (UBSan)
|
|
|
|
# - "" (no sanitizing)
|
2020-09-17 15:37:23 +00:00
|
|
|
option (SANITIZE "Enable one of the code sanitizers" "")
|
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")
|
2023-05-08 20:39:49 +00:00
|
|
|
set (ASAN_FLAGS "-fsanitize=address -fsanitize-address-use-after-scope")
|
2023-05-09 18:32:03 +00:00
|
|
|
if (COMPILER_CLANG)
|
|
|
|
if (${CMAKE_CXX_COMPILER_VERSION} VERSION_GREATER_EQUAL 15 AND ${CMAKE_CXX_COMPILER_VERSION} VERSION_LESS 16)
|
|
|
|
# LLVM-15 has a bug in Address Sanitizer, preventing the usage
|
|
|
|
# of 'sanitize-address-use-after-scope', see [1].
|
|
|
|
#
|
|
|
|
# [1]: https://github.com/llvm/llvm-project/issues/58633
|
|
|
|
set (ASAN_FLAGS "${ASAN_FLAGS} -fno-sanitize-address-use-after-scope")
|
|
|
|
endif()
|
|
|
|
endif()
|
2020-12-15 19:59:07 +00:00
|
|
|
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${SAN_FLAGS} ${ASAN_FLAGS}")
|
|
|
|
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${SAN_FLAGS} ${ASAN_FLAGS}")
|
|
|
|
|
2018-08-08 03:37:35 +00:00
|
|
|
elseif (SANITIZE STREQUAL "memory")
|
2019-09-24 19:00:05 +00:00
|
|
|
# MemorySanitizer flags are set according to the official documentation:
|
|
|
|
# https://clang.llvm.org/docs/MemorySanitizer.html#usage
|
2023-04-25 12:25:14 +00:00
|
|
|
|
|
|
|
# Linking can fail due to relocation overflows (see #49145), caused by too big object files / libraries.
|
|
|
|
# Work around this with position-independent builds (-fPIC and -fpie), this is slightly slower than non-PIC/PIE but that's okay.
|
2023-04-25 13:45:23 +00:00
|
|
|
set (MSAN_FLAGS "-fsanitize=memory -fsanitize-memory-use-after-dtor -fsanitize-memory-track-origins -fno-optimize-sibling-calls -fPIC -fpie -fsanitize-blacklist=${CMAKE_SOURCE_DIR}/tests/msan_suppressions.txt")
|
2019-09-23 18:00:13 +00:00
|
|
|
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${SAN_FLAGS} ${MSAN_FLAGS}")
|
|
|
|
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${SAN_FLAGS} ${MSAN_FLAGS}")
|
|
|
|
|
2018-08-08 03:37:35 +00:00
|
|
|
elseif (SANITIZE STREQUAL "thread")
|
2020-09-09 23:05:41 +00:00
|
|
|
set (TSAN_FLAGS "-fsanitize=thread")
|
|
|
|
if (COMPILER_CLANG)
|
|
|
|
set (TSAN_FLAGS "${TSAN_FLAGS} -fsanitize-blacklist=${CMAKE_SOURCE_DIR}/tests/tsan_suppressions.txt")
|
|
|
|
endif()
|
|
|
|
|
2020-05-11 07:05:46 +00:00
|
|
|
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${SAN_FLAGS} ${TSAN_FLAGS}")
|
|
|
|
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${SAN_FLAGS} ${TSAN_FLAGS}")
|
2018-12-28 23:42:39 +00:00
|
|
|
|
2018-08-08 03:37:35 +00:00
|
|
|
elseif (SANITIZE STREQUAL "undefined")
|
2020-12-14 23:19:37 +00:00
|
|
|
set (UBSAN_FLAGS "-fsanitize=undefined -fno-sanitize-recover=all -fno-sanitize=float-divide-by-zero")
|
2021-11-27 14:39:13 +00:00
|
|
|
if (ENABLE_FUZZING)
|
|
|
|
# Unsigned integer overflow is well defined behaviour from a perspective of C++ standard,
|
|
|
|
# compilers or CPU. We use in hash functions like SipHash and many other places in our codebase.
|
|
|
|
# This flag is needed only because fuzzers are run inside oss-fuzz infrastructure
|
|
|
|
# and they have a bunch of flags not halt the program if UIO happend and even to silence that warnings.
|
|
|
|
# But for unknown reason that flags don't work with ClickHouse or we don't understand how to properly use them,
|
|
|
|
# that's why we often receive reports about UIO. The simplest way to avoid this is just set this flag here.
|
2022-10-24 21:01:06 +00:00
|
|
|
set(UBSAN_FLAGS "${UBSAN_FLAGS} -fno-sanitize=unsigned-integer-overflow")
|
2021-11-27 14:39:13 +00:00
|
|
|
endif()
|
2020-12-14 23:19:37 +00:00
|
|
|
if (COMPILER_CLANG)
|
|
|
|
set (UBSAN_FLAGS "${UBSAN_FLAGS} -fsanitize-blacklist=${CMAKE_SOURCE_DIR}/tests/ubsan_suppressions.txt")
|
|
|
|
endif()
|
|
|
|
|
|
|
|
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${SAN_FLAGS} ${UBSAN_FLAGS}")
|
|
|
|
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${SAN_FLAGS} ${UBSAN_FLAGS}")
|
2019-09-22 10:34:09 +00:00
|
|
|
|
2019-12-18 15:30:23 +00:00
|
|
|
# llvm-tblgen, that is used during LLVM build, doesn't work with UBSan.
|
|
|
|
set (ENABLE_EMBEDDED_COMPILER 0 CACHE BOOL "")
|
|
|
|
|
2018-08-08 03:37:35 +00:00
|
|
|
else ()
|
|
|
|
message (FATAL_ERROR "Unknown sanitizer type: ${SANITIZE}")
|
|
|
|
endif ()
|
|
|
|
endif()
|