Make sanitizer options more fine-grained
authorStephan Beyer <s-beyer@gmx.net>
Fri, 15 May 2020 23:52:29 +0000 (01:52 +0200)
committerStephan Beyer <s-beyer@gmx.net>
Tue, 19 May 2020 08:57:02 +0000 (10:57 +0200)
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>
CMakeLists.txt
cmake/modules/SanitizerFlags.cmake

index 55e8464935f3714398be8143cf0ba6bfaa725ef1..5df23883895d25f5427d686ed877882986f7845e 100644 (file)
@@ -228,11 +228,7 @@ if (APPLE)
     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")
 endif()
 
-option(SANITIZE_ADDRESS "Enable address sanitizer in tests" OFF)
-if (SANITIZE_ADDRESS)
-    include(SanitizerFlags)
-    enable_sanitizer()
-endif ()
+include(SanitizerFlags)
 
 # Handle Translations, pick all client_* files from trans directory.
 file( GLOB TRANS_FILES ${CMAKE_SOURCE_DIR}/translations/client_*.ts)
index 9d5c9ebdafebd4ad6349a2272a7a99be76819925..43319018257a570d3d098666afaec20e21dce863 100644 (file)
@@ -1,17 +1,31 @@
-
 # Enable address sanitizer (gcc/clang only)
-macro(ENABLE_SANITIZER)
-
-  if (NOT CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
-    message(FATAL_ERROR "Sanitizer supported only for gcc/clang")
-  endif()
-
-  set(SANITIZER_FLAGS "-fsanitize=address  -fsanitize=leak -g")
-  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${SANITIZER_FLAGS}")
-  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${SANITIZER_FLAGS}")
+if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
+    set(SANITIZERS)
 
-  set(LINKER_FLAGS "-fsanitize=address,undefined -fuse-ld=gold")
-  set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${LINKER_FLAGS}")
+    macro(add_sanitizer_option variable flag help)
+        option(${variable} "Enable ${help}" OFF)
+        if(${variable})
+            list(APPEND SANITIZERS ${flag})
+        endif()
+        mark_as_advanced(${variable})
+    endmacro()
 
-endmacro()
+    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"
+        "UndefinedBehaviorSanitizer (detects undefined behavior)")
+    add_sanitizer_option(SANITIZE_THREAD "thread"
+        "ThreadSanitizer (detects data races)")
 
+    if(SANITIZERS)
+        string(REPLACE ";" "," SANITIZER_FLAGS "${SANITIZERS}")
+        set(SANITIZER_FLAGS "-fsanitize=${SANITIZER_FLAGS}")
+        string(APPEND CMAKE_CXX_FLAGS " ${SANITIZER_FLAGS}")
+        string(APPEND CMAKE_C_FLAGS " ${SANITIZER_FLAGS}")
+        string(APPEND CMAKE_EXE_LINKER_FLAGS " ${SANITIZER_FLAGS}")
+    endif()
+endif()