2020-09-19 16:42:36 +00:00
|
|
|
# Possible values: `address` (ASan), `memory` (MSan), `thread` (TSan), `undefined` (UBSan), and "" (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")
|
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")
|
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
|
|
|
|
#
|
|
|
|
# For now, it compiles with `cmake -DSANITIZE=memory -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_CXX_FLAGS_ADD="-O1" -DCMAKE_C_FLAGS_ADD="-O1"`
|
|
|
|
# Compiling with -DCMAKE_BUILD_TYPE=Debug leads to ld.lld failures because
|
|
|
|
# of large files (was not tested with ld.gold). This is why we compile with
|
|
|
|
# RelWithDebInfo, and downgrade optimizations to -O1 but not to -Og, to
|
|
|
|
# keep the binary size down.
|
|
|
|
# TODO: try compiling with -Og and with ld.gold.
|
2020-04-01 23:51:21 +00:00
|
|
|
set (MSAN_FLAGS "-fsanitize=memory -fsanitize-memory-track-origins -fno-optimize-sibling-calls -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-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")
|
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")
|
|
|
|
else()
|
|
|
|
message (WARNING "TSAN suppressions was not passed to the compiler (since the compiler is not clang)")
|
|
|
|
message (WARNING "Use the following command to pass them manually:")
|
|
|
|
message (WARNING " export TSAN_OPTIONS=\"$TSAN_OPTIONS suppressions=${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
|
|
|
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")
|
2019-09-28 14:36:56 +00:00
|
|
|
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${SAN_FLAGS} -fsanitize=undefined -fno-sanitize-recover=all -fno-sanitize=float-divide-by-zero")
|
|
|
|
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${SAN_FLAGS} -fsanitize=undefined -fno-sanitize-recover=all -fno-sanitize=float-divide-by-zero")
|
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
|
|
|
|
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()
|