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")
|
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-05-13 15:47:40 +00:00
|
|
|
set (MSAN_FLAGS "-fsanitize=memory -fsanitize-memory-use-after-dtor -fsanitize-memory-track-origins -fno-optimize-sibling-calls -fPIC -fpie")
|
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")
|
2024-05-07 18:58:19 +00:00
|
|
|
set (TSAN_FLAGS "${TSAN_FLAGS} -fsanitize-ignorelist=${PROJECT_SOURCE_DIR}/tests/tsan_ignorelist.txt")
|
2020-09-09 23:05:41 +00:00
|
|
|
|
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()
|
2024-05-07 18:58:19 +00:00
|
|
|
set (UBSAN_FLAGS "${UBSAN_FLAGS} -fsanitize-ignorelist=${PROJECT_SOURCE_DIR}/tests/ubsan_ignorelist.txt")
|
2020-12-14 23:19:37 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2018-08-08 03:37:35 +00:00
|
|
|
else ()
|
|
|
|
message (FATAL_ERROR "Unknown sanitizer type: ${SANITIZE}")
|
|
|
|
endif ()
|
|
|
|
endif()
|
2023-10-29 01:07:24 +00:00
|
|
|
|
|
|
|
# Default coverage instrumentation (dumping the coverage map on exit)
|
|
|
|
option(WITH_COVERAGE "Instrumentation for code coverage with default implementation" OFF)
|
|
|
|
|
|
|
|
if (WITH_COVERAGE)
|
2024-01-18 11:47:18 +00:00
|
|
|
message (STATUS "Enabled instrumentation for code coverage")
|
2023-10-29 01:07:24 +00:00
|
|
|
set(COVERAGE_FLAGS "-fprofile-instr-generate -fcoverage-mapping")
|
|
|
|
endif()
|
|
|
|
|
|
|
|
option (SANITIZE_COVERAGE "Instrumentation for code coverage with custom callbacks" OFF)
|
|
|
|
|
|
|
|
if (SANITIZE_COVERAGE)
|
2024-01-18 11:47:18 +00:00
|
|
|
message (STATUS "Enabled instrumentation for code coverage")
|
2023-10-29 16:21:45 +00:00
|
|
|
|
|
|
|
# We set this define for whole build to indicate that at least some parts are compiled with coverage.
|
|
|
|
# And to expose it in system.build_options.
|
|
|
|
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DSANITIZE_COVERAGE=1")
|
|
|
|
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DSANITIZE_COVERAGE=1")
|
|
|
|
|
|
|
|
# But the actual coverage will be enabled on per-library basis: for ClickHouse code, but not for 3rd-party.
|
2023-10-30 02:22:49 +00:00
|
|
|
set (COVERAGE_FLAGS "-fsanitize-coverage=trace-pc-guard,pc-table")
|
2023-10-29 01:07:24 +00:00
|
|
|
|
2024-01-29 16:33:53 +00:00
|
|
|
set (WITHOUT_COVERAGE_FLAGS "-fno-profile-instr-generate -fno-coverage-mapping -fno-sanitize-coverage=trace-pc-guard,pc-table")
|
|
|
|
set (WITHOUT_COVERAGE_FLAGS_LIST -fno-profile-instr-generate -fno-coverage-mapping -fno-sanitize-coverage=trace-pc-guard,pc-table)
|
|
|
|
else()
|
|
|
|
set (WITHOUT_COVERAGE_FLAGS "")
|
|
|
|
set (WITHOUT_COVERAGE_FLAGS_LIST "")
|
|
|
|
endif()
|