2019-10-22 00:01:22 +03:00
|
|
|
# Enable address sanitizer (gcc/clang only)
|
Make sanitizer options more fine-grained
The SANITIZE_ADDRESS option of our CMake configuration activates the
AddressSanitizer (and UBSan in a non-working way) for the whole project
(although, by the way, its documentation pretends that it is only enabled
for tests).
This commit introduces new options SANITIZE_LEAK, SANITIZE_MEMORY,
SANITIZE_UNDEFINED, SANITIZE_THREAD. Each of these options (including
SANITIZE_ADDRESS) enables only the corresponding sanitizer.
Moreover, we mark all sanitizer options as advanced options, because these
options are only interesting for developers.
Note that some sanitizers are conflicting, that is, not all options can
be enabled simultaneously. Also, not all sanitizers are available for
all compilers and versions. We, however, do not check for this, instead
we let the compiler throw its errors in such cases.
The explicit usage of the Google Linker is removed, because it is not
necessary and can lead to problems with clang.
The commit can be considered a rewrite of cmake/modules/SanitizerFlags.cmake.
Signed-off-by: Stephan Beyer <s-beyer@gmx.net>
2020-05-16 02:52:29 +03:00
|
|
|
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
|
|
|
|
set(SANITIZERS)
|
2020-05-18 20:54:09 +03:00
|
|
|
set(SANITIZER_EXTRA_FLAGS " -g")
|
2019-10-22 00:01:22 +03:00
|
|
|
|
Make sanitizer options more fine-grained
The SANITIZE_ADDRESS option of our CMake configuration activates the
AddressSanitizer (and UBSan in a non-working way) for the whole project
(although, by the way, its documentation pretends that it is only enabled
for tests).
This commit introduces new options SANITIZE_LEAK, SANITIZE_MEMORY,
SANITIZE_UNDEFINED, SANITIZE_THREAD. Each of these options (including
SANITIZE_ADDRESS) enables only the corresponding sanitizer.
Moreover, we mark all sanitizer options as advanced options, because these
options are only interesting for developers.
Note that some sanitizers are conflicting, that is, not all options can
be enabled simultaneously. Also, not all sanitizers are available for
all compilers and versions. We, however, do not check for this, instead
we let the compiler throw its errors in such cases.
The explicit usage of the Google Linker is removed, because it is not
necessary and can lead to problems with clang.
The commit can be considered a rewrite of cmake/modules/SanitizerFlags.cmake.
Signed-off-by: Stephan Beyer <s-beyer@gmx.net>
2020-05-16 02:52:29 +03:00
|
|
|
macro(add_sanitizer_option variable flag help)
|
|
|
|
option(${variable} "Enable ${help}" OFF)
|
|
|
|
if(${variable})
|
|
|
|
list(APPEND SANITIZERS ${flag})
|
2020-05-18 20:54:09 +03:00
|
|
|
string(REPLACE ";" " " optional_args "${ARGN}")
|
|
|
|
if(optional_args)
|
|
|
|
string(APPEND SANITIZER_EXTRA_FLAGS " ${optional_args}")
|
|
|
|
endif()
|
Make sanitizer options more fine-grained
The SANITIZE_ADDRESS option of our CMake configuration activates the
AddressSanitizer (and UBSan in a non-working way) for the whole project
(although, by the way, its documentation pretends that it is only enabled
for tests).
This commit introduces new options SANITIZE_LEAK, SANITIZE_MEMORY,
SANITIZE_UNDEFINED, SANITIZE_THREAD. Each of these options (including
SANITIZE_ADDRESS) enables only the corresponding sanitizer.
Moreover, we mark all sanitizer options as advanced options, because these
options are only interesting for developers.
Note that some sanitizers are conflicting, that is, not all options can
be enabled simultaneously. Also, not all sanitizers are available for
all compilers and versions. We, however, do not check for this, instead
we let the compiler throw its errors in such cases.
The explicit usage of the Google Linker is removed, because it is not
necessary and can lead to problems with clang.
The commit can be considered a rewrite of cmake/modules/SanitizerFlags.cmake.
Signed-off-by: Stephan Beyer <s-beyer@gmx.net>
2020-05-16 02:52:29 +03:00
|
|
|
endif()
|
|
|
|
mark_as_advanced(${variable})
|
|
|
|
endmacro()
|
2019-10-22 00:01:22 +03:00
|
|
|
|
Make sanitizer options more fine-grained
The SANITIZE_ADDRESS option of our CMake configuration activates the
AddressSanitizer (and UBSan in a non-working way) for the whole project
(although, by the way, its documentation pretends that it is only enabled
for tests).
This commit introduces new options SANITIZE_LEAK, SANITIZE_MEMORY,
SANITIZE_UNDEFINED, SANITIZE_THREAD. Each of these options (including
SANITIZE_ADDRESS) enables only the corresponding sanitizer.
Moreover, we mark all sanitizer options as advanced options, because these
options are only interesting for developers.
Note that some sanitizers are conflicting, that is, not all options can
be enabled simultaneously. Also, not all sanitizers are available for
all compilers and versions. We, however, do not check for this, instead
we let the compiler throw its errors in such cases.
The explicit usage of the Google Linker is removed, because it is not
necessary and can lead to problems with clang.
The commit can be considered a rewrite of cmake/modules/SanitizerFlags.cmake.
Signed-off-by: Stephan Beyer <s-beyer@gmx.net>
2020-05-16 02:52:29 +03:00
|
|
|
add_sanitizer_option(SANITIZE_ADDRESS "address"
|
|
|
|
"AddressSanitizer (detects memory violations, buffer overflows, memory leaks)")
|
|
|
|
add_sanitizer_option(SANITIZE_LEAK "leak"
|
|
|
|
"standalone LeakSanitizer (detects memory leaks only)")
|
|
|
|
add_sanitizer_option(SANITIZE_MEMORY "memory"
|
|
|
|
"MemorySanitizer (detects reads in uninitialized memory)")
|
|
|
|
add_sanitizer_option(SANITIZE_UNDEFINED "undefined"
|
2020-05-18 20:54:09 +03:00
|
|
|
"UndefinedBehaviorSanitizer (detects undefined behavior)"
|
|
|
|
"-fno-sanitize=vptr")
|
Make sanitizer options more fine-grained
The SANITIZE_ADDRESS option of our CMake configuration activates the
AddressSanitizer (and UBSan in a non-working way) for the whole project
(although, by the way, its documentation pretends that it is only enabled
for tests).
This commit introduces new options SANITIZE_LEAK, SANITIZE_MEMORY,
SANITIZE_UNDEFINED, SANITIZE_THREAD. Each of these options (including
SANITIZE_ADDRESS) enables only the corresponding sanitizer.
Moreover, we mark all sanitizer options as advanced options, because these
options are only interesting for developers.
Note that some sanitizers are conflicting, that is, not all options can
be enabled simultaneously. Also, not all sanitizers are available for
all compilers and versions. We, however, do not check for this, instead
we let the compiler throw its errors in such cases.
The explicit usage of the Google Linker is removed, because it is not
necessary and can lead to problems with clang.
The commit can be considered a rewrite of cmake/modules/SanitizerFlags.cmake.
Signed-off-by: Stephan Beyer <s-beyer@gmx.net>
2020-05-16 02:52:29 +03:00
|
|
|
add_sanitizer_option(SANITIZE_THREAD "thread"
|
|
|
|
"ThreadSanitizer (detects data races)")
|
2019-10-22 00:01:22 +03:00
|
|
|
|
Make sanitizer options more fine-grained
The SANITIZE_ADDRESS option of our CMake configuration activates the
AddressSanitizer (and UBSan in a non-working way) for the whole project
(although, by the way, its documentation pretends that it is only enabled
for tests).
This commit introduces new options SANITIZE_LEAK, SANITIZE_MEMORY,
SANITIZE_UNDEFINED, SANITIZE_THREAD. Each of these options (including
SANITIZE_ADDRESS) enables only the corresponding sanitizer.
Moreover, we mark all sanitizer options as advanced options, because these
options are only interesting for developers.
Note that some sanitizers are conflicting, that is, not all options can
be enabled simultaneously. Also, not all sanitizers are available for
all compilers and versions. We, however, do not check for this, instead
we let the compiler throw its errors in such cases.
The explicit usage of the Google Linker is removed, because it is not
necessary and can lead to problems with clang.
The commit can be considered a rewrite of cmake/modules/SanitizerFlags.cmake.
Signed-off-by: Stephan Beyer <s-beyer@gmx.net>
2020-05-16 02:52:29 +03:00
|
|
|
if(SANITIZERS)
|
|
|
|
string(REPLACE ";" "," SANITIZER_FLAGS "${SANITIZERS}")
|
2020-05-18 20:54:09 +03:00
|
|
|
set(SANITIZER_FLAGS "-fsanitize=${SANITIZER_FLAGS}${SANITIZER_EXTRA_FLAGS}")
|
Make sanitizer options more fine-grained
The SANITIZE_ADDRESS option of our CMake configuration activates the
AddressSanitizer (and UBSan in a non-working way) for the whole project
(although, by the way, its documentation pretends that it is only enabled
for tests).
This commit introduces new options SANITIZE_LEAK, SANITIZE_MEMORY,
SANITIZE_UNDEFINED, SANITIZE_THREAD. Each of these options (including
SANITIZE_ADDRESS) enables only the corresponding sanitizer.
Moreover, we mark all sanitizer options as advanced options, because these
options are only interesting for developers.
Note that some sanitizers are conflicting, that is, not all options can
be enabled simultaneously. Also, not all sanitizers are available for
all compilers and versions. We, however, do not check for this, instead
we let the compiler throw its errors in such cases.
The explicit usage of the Google Linker is removed, because it is not
necessary and can lead to problems with clang.
The commit can be considered a rewrite of cmake/modules/SanitizerFlags.cmake.
Signed-off-by: Stephan Beyer <s-beyer@gmx.net>
2020-05-16 02:52:29 +03:00
|
|
|
string(APPEND CMAKE_CXX_FLAGS " ${SANITIZER_FLAGS}")
|
|
|
|
string(APPEND CMAKE_C_FLAGS " ${SANITIZER_FLAGS}")
|
|
|
|
string(APPEND CMAKE_EXE_LINKER_FLAGS " ${SANITIZER_FLAGS}")
|
|
|
|
endif()
|
|
|
|
endif()
|