Skip to content

Commit 56e5ffa

Browse files
authored
Auto-detect ASan at compile time (#638)
Fixes: #636
1 parent eae9b23 commit 56e5ffa

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

CMakeLists.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,6 @@ xoption(CONFIG_UBSAN "Enable UndefinedBehaviorSanitizer (UBSan)" OFF)
123123

124124
if(CONFIG_ASAN)
125125
message(STATUS "Building with ASan")
126-
add_compile_definitions(
127-
__ASAN__=1
128-
)
129126
add_compile_options(
130127
-fsanitize=address
131128
-fno-sanitize-recover=all
@@ -168,6 +165,7 @@ endif()
168165

169166
if(CONFIG_UBSAN)
170167
message(STATUS "Building with UBSan")
168+
# __has_feature(undefined_sanitizer) or __SANITIZE_UNDEFINED__ don't exist
171169
add_compile_definitions(
172170
__UBSAN__=1
173171
)

cutils.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ extern "C" {
5454
#include <pthread.h>
5555
#endif
5656

57+
#if defined(__SANITIZE_ADDRESS__)
58+
# define __ASAN__ 1
59+
#elif defined(__has_feature)
60+
# if __has_feature(address_sanitizer)
61+
# define __ASAN__ 1
62+
# endif
63+
#endif
64+
5765
#if defined(_MSC_VER) && !defined(__clang__)
5866
# define likely(x) (x)
5967
# define unlikely(x) (x)

0 commit comments

Comments
 (0)