| 1 |
# SPDX-FileCopyrightText: 2014 Mathieu Tarral <[email protected]> |
| 2 |
# |
| 3 |
# SPDX-License-Identifier: BSD-3-Clause |
| 4 |
|
| 5 |
#[=======================================================================[.rst: |
| 6 |
ECMEnableSanitizers |
| 7 |
------------------- |
| 8 |
|
| 9 |
Enable compiler sanitizer flags. |
| 10 |
|
| 11 |
The following sanitizers are supported: |
| 12 |
|
| 13 |
- Address Sanitizer |
| 14 |
- Memory Sanitizer |
| 15 |
- Thread Sanitizer |
| 16 |
- Leak Sanitizer |
| 17 |
- Undefined Behaviour Sanitizer |
| 18 |
|
| 19 |
All of them are implemented in Clang, depending on your version, and |
| 20 |
there is an work in progress in GCC, where some of them are currently |
| 21 |
implemented. |
| 22 |
|
| 23 |
This module will check your current compiler version to see if it |
| 24 |
supports the sanitizers that you want to enable |
| 25 |
|
| 26 |
Usage |
| 27 |
===== |
| 28 |
|
| 29 |
Simply add:: |
| 30 |
|
| 31 |
include(ECMEnableSanitizers) |
| 32 |
|
| 33 |
to your ``CMakeLists.txt``. Note that this module is included in |
| 34 |
KDECompilerSettings, so projects using that module do not need to also |
| 35 |
include this one. |
| 36 |
|
| 37 |
The sanitizers are not enabled by default. Instead, you must set |
| 38 |
``ECM_ENABLE_SANITIZERS`` (either in your ``CMakeLists.txt`` or on the |
| 39 |
command line) to a semicolon-separated list of sanitizers you wish to enable. |
| 40 |
The options are: |
| 41 |
|
| 42 |
- address |
| 43 |
- memory |
| 44 |
- thread |
| 45 |
- leak |
| 46 |
- undefined |
| 47 |
- fuzzer-no-link |
| 48 |
- fuzzer |
| 49 |
|
| 50 |
The sanitizers "address", "memory" and "thread" are mutually exclusive. You |
| 51 |
cannot enable two of them in the same build. |
| 52 |
|
| 53 |
"leak" requires the "address" sanitizer. |
| 54 |
|
| 55 |
.. note:: |
| 56 |
|
| 57 |
To reduce the overhead induced by the instrumentation of the sanitizers, it |
| 58 |
is advised to enable compiler optimizations (``-O1`` or higher). |
| 59 |
|
| 60 |
Example |
| 61 |
======= |
| 62 |
|
| 63 |
This is an example of usage:: |
| 64 |
|
| 65 |
mkdir build |
| 66 |
cd build |
| 67 |
cmake -DECM_ENABLE_SANITIZERS='address;leak;undefined' .. |
| 68 |
|
| 69 |
.. note:: |
| 70 |
|
| 71 |
Most of the sanitizers will require Clang. To enable it, use:: |
| 72 |
|
| 73 |
-DCMAKE_CXX_COMPILER=clang++ |
| 74 |
|
| 75 |
Since 1.3.0. |
| 76 |
#]=======================================================================] |
| 77 |
|
| 78 |
# MACRO check_compiler_version |
| 79 |
#----------------------------- |
| 80 |
macro (check_compiler_version gcc_required_version clang_required_version msvc_required_version) |
| 81 |
if ( |
| 82 |
( |
| 83 |
CMAKE_CXX_COMPILER_ID MATCHES "GNU" |
| 84 |
AND |
| 85 |
CMAKE_CXX_COMPILER_VERSION VERSION_LESS ${gcc_required_version} |
| 86 |
) |
| 87 |
OR |
| 88 |
( |
| 89 |
CMAKE_CXX_COMPILER_ID MATCHES "Clang" |
| 90 |
AND |
| 91 |
CMAKE_CXX_COMPILER_VERSION VERSION_LESS ${clang_required_version} |
| 92 |
) |
| 93 |
OR |
| 94 |
( |
| 95 |
CMAKE_CXX_COMPILER_ID MATCHES "MSVC" |
| 96 |
AND |
| 97 |
CMAKE_CXX_COMPILER_VERSION VERSION_LESS ${msvc_required_version} |
| 98 |
) |
| 99 |
) |
| 100 |
# error ! |
| 101 |
message(FATAL_ERROR "You ask to enable the sanitizer ${CUR_SANITIZER}, |
| 102 |
but your compiler ${CMAKE_CXX_COMPILER_ID} version ${CMAKE_CXX_COMPILER_VERSION} |
| 103 |
does not support it ! |
| 104 |
You should use at least GCC ${gcc_required_version}, Clang ${clang_required_version} |
| 105 |
or MSVC ${msvc_required_version} |
| 106 |
(99.99 means not implemented yet)") |
| 107 |
endif () |
| 108 |
endmacro () |
| 109 |
|
| 110 |
# MACRO check_compiler_support |
| 111 |
#------------------------------ |
| 112 |
macro (enable_sanitizer_flags sanitize_option) |
| 113 |
if (${sanitize_option} MATCHES "address") |
| 114 |
check_compiler_version("4.8" "3.1" "19.28") |
| 115 |
if (CMAKE_CXX_COMPILER_ID MATCHES "MSVC") |
| 116 |
set(XSAN_COMPILE_FLAGS "-fsanitize=address") |
| 117 |
else() |
| 118 |
set(XSAN_COMPILE_FLAGS "-fsanitize=address -fno-omit-frame-pointer -fno-optimize-sibling-calls") |
| 119 |
set(XSAN_LINKER_FLAGS "asan") |
| 120 |
endif() |
| 121 |
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU") |
| 122 |
# fixes "note: variable tracking size limit exceeded with |
| 123 |
# -fvar-tracking-assignments, retrying without" (which is |
| 124 |
# another way of saying "I'll go ahead and compile this |
| 125 |
# thing _twice_") by making it unlimited |
| 126 |
# Reference: |
| 127 |
# https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html |
| 128 |
# -> max-vartrack-size |
| 129 |
string(APPEND XSAN_COMPILE_FLAGS " --param=max-vartrack-size=0") |
| 130 |
endif () |
| 131 |
elseif (${sanitize_option} MATCHES "thread") |
| 132 |
check_compiler_version("4.8" "3.1" "99.99") |
| 133 |
set(XSAN_COMPILE_FLAGS "-fsanitize=thread") |
| 134 |
set(XSAN_LINKER_FLAGS "tsan") |
| 135 |
elseif (${sanitize_option} MATCHES "memory") |
| 136 |
check_compiler_version("99.99" "3.1" "99.99") |
| 137 |
set(XSAN_COMPILE_FLAGS "-fsanitize=memory") |
| 138 |
elseif (${sanitize_option} MATCHES "leak") |
| 139 |
check_compiler_version("4.9" "3.4" "99.99") |
| 140 |
set(XSAN_COMPILE_FLAGS "-fsanitize=leak") |
| 141 |
set(XSAN_LINKER_FLAGS "lsan") |
| 142 |
elseif (${sanitize_option} MATCHES "undefined") |
| 143 |
check_compiler_version("4.9" "3.1" "99.99") |
| 144 |
set(XSAN_COMPILE_FLAGS "-fsanitize=undefined -fsanitize=float-divide-by-zero -fno-omit-frame-pointer -fno-optimize-sibling-calls") |
| 145 |
elseif (${sanitize_option} MATCHES "fuzzer-no-link") |
| 146 |
check_compiler_version("99.99" "6.0" "99.99") |
| 147 |
set(XSAN_COMPILE_FLAGS "-fsanitize=fuzzer-no-link") |
| 148 |
set(XSAN_LINKER_FLAGS "-fsanitize=fuzzer-no-link") |
| 149 |
elseif (${sanitize_option} MATCHES "fuzzer") |
| 150 |
check_compiler_version("99.99" "6.0" "99.99") |
| 151 |
set(XSAN_COMPILE_FLAGS "-fsanitize=fuzzer") |
| 152 |
else () |
| 153 |
message(FATAL_ERROR "Compiler sanitizer option \"${sanitize_option}\" not supported.") |
| 154 |
endif () |
| 155 |
endmacro () |
| 156 |
|
| 157 |
if (ECM_ENABLE_SANITIZERS) |
| 158 |
if (CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID MATCHES "MSVC") |
| 159 |
# for each element of the ECM_ENABLE_SANITIZERS list |
| 160 |
foreach ( CUR_SANITIZER ${ECM_ENABLE_SANITIZERS} ) |
| 161 |
# lowercase filter |
| 162 |
string(TOLOWER ${CUR_SANITIZER} CUR_SANITIZER) |
| 163 |
# check option and enable appropriate flags |
| 164 |
enable_sanitizer_flags ( ${CUR_SANITIZER} ) |
| 165 |
# TODO: GCC will not link pthread library if enabled ASan |
| 166 |
if(CMAKE_C_COMPILER_ID MATCHES "Clang") |
| 167 |
set( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${XSAN_COMPILE_FLAGS}" ) |
| 168 |
endif() |
| 169 |
set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${XSAN_COMPILE_FLAGS}" ) |
| 170 |
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU") |
| 171 |
string(JOIN "" linker_flags |
| 172 |
"$<" |
| 173 |
"$<NOT:" |
| 174 |
"$<BOOL:$<TARGET_PROPERTY:SKIP_SANITIZER>>" |
| 175 |
">:" |
| 176 |
"${XSAN_LINKER_FLAGS}" |
| 177 |
">" |
| 178 |
) |
| 179 |
link_libraries("${linker_flags}") |
| 180 |
endif() |
| 181 |
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") |
| 182 |
string(REGEX REPLACE "( |^)-Wl,--no-undefined( |$)" " " CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS}") |
| 183 |
string(REGEX REPLACE "( |^)-Wl,--no-undefined( |$)" " " CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS}") |
| 184 |
endif () |
| 185 |
endforeach() |
| 186 |
else() |
| 187 |
message(STATUS "Tried to enable sanitizers (-DECM_ENABLE_SANITIZERS=${ECM_ENABLE_SANITIZERS}), \ |
| 188 |
but compiler (${CMAKE_CXX_COMPILER_ID}) does not have sanitizer support") |
| 189 |
endif() |
| 190 |
endif() |