commit 4a8eb80: [Minor] Allow to specify multiple sanitize targets and slightly rework

Vsevolod Stakhov vsevolod at rspamd.com
Sat Sep 17 16:14:03 UTC 2022


Author: Vsevolod Stakhov
Date: 2022-09-17 14:39:25 +0100
URL: https://github.com/rspamd/rspamd/commit/4a8eb80e888f200cc76918b20db7766c236c5e48

[Minor] Allow to specify multiple sanitize targets and slightly rework

---
 cmake/Sanitizer.cmake | 73 +++++++++++++++++++++++++++++----------------------
 1 file changed, 42 insertions(+), 31 deletions(-)

diff --git a/cmake/Sanitizer.cmake b/cmake/Sanitizer.cmake
index 74d6a4b1c..58a240f6f 100644
--- a/cmake/Sanitizer.cmake
+++ b/cmake/Sanitizer.cmake
@@ -1,6 +1,6 @@
 # Ported from Clickhouse: https://github.com/ClickHouse/ClickHouse/blob/master/cmake/sanitize.cmake
 
-option (SANITIZE "Enable sanitizer: address, memory, undefined" "")
+option (SANITIZE "Enable sanitizer: address, memory, undefined, leak (comma separated list)" "")
 set (SAN_FLAGS "${SAN_FLAGS} -g -fno-omit-frame-pointer -DSANITIZER")
 # O1 is normally set by clang, and -Og by gcc
 if (COMPILER_GCC)
@@ -13,42 +13,53 @@ if (SANITIZE)
         message (STATUS "Jemalloc support is useless in case of build with sanitizers")
         set (ENABLE_JEMALLOC "OFF")
     endif ()
-    if (SANITIZE STREQUAL "address")
-        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")
-        set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=address -fsanitize-address-use-after-scope")
-        if (COMPILER_GCC)
-            set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -static-libasan")
-            set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libasan")
-            set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libasan")
-        endif ()
 
-    elseif (SANITIZE STREQUAL "memory")
-        set (MSAN_FLAGS "-fsanitize=memory -fsanitize-memory-track-origins -fno-optimize-sibling-calls")
+    string(REPLACE "," ";" SANITIZE_LIST ${SANITIZE})
+    foreach(SANITIZE ${SANITIZE_LIST})
+        if (SANITIZE STREQUAL "address")
+            set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fsanitize-address-use-after-scope")
+            set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address -fsanitize-address-use-after-scope")
+            set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=address -fsanitize-address-use-after-scope")
+            if (COMPILER_GCC)
+                set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -static-libasan")
+                set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libasan")
+                set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libasan")
+            endif ()
 
-        set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${SAN_FLAGS} ${MSAN_FLAGS}")
-        set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${SAN_FLAGS} ${MSAN_FLAGS}")
-        set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=memory")
+        elseif (SANITIZE STREQUAL "leak")
+            set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=leak")
+            set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=leak")
+            set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=leak")
 
-        if (COMPILER_GCC)
-            set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -static-libmsan")
-            set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libmsan")
-            set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libmsan")
-        endif ()
+        elseif (SANITIZE STREQUAL "memory")
+            set (MSAN_FLAGS "-fsanitize=memory -fsanitize-memory-track-origins -fno-optimize-sibling-calls")
+
+            set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${MSAN_FLAGS}")
+            set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${MSAN_FLAGS}")
+            set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=memory")
 
-    elseif (SANITIZE STREQUAL "undefined")
-        set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${SAN_FLAGS} -fsanitize=undefined -fno-sanitize-recover=all")
-        set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${SAN_FLAGS} -fsanitize=undefined -fno-sanitize-recover=all")
-        set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=undefined")
+            if (COMPILER_GCC)
+                set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -static-libmsan")
+                set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libmsan")
+                set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libmsan")
+            endif ()
 
-        if (COMPILER_GCC)
-            set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -static-libubsan")
-            set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libubsan")
-            set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libubsan")
+        elseif (SANITIZE STREQUAL "undefined")
+            set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined -fno-sanitize-recover=all")
+            set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=undefined -fno-sanitize-recover=all")
+            set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=undefined")
+
+            if (COMPILER_GCC)
+                set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -static-libubsan")
+                set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libubsan")
+                set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libubsan")
+            endif ()
+        else ()
+            message (FATAL_ERROR "Unknown sanitizer type: ${SANITIZE}")
         endif ()
-    else ()
-        message (FATAL_ERROR "Unknown sanitizer type: ${SANITIZE}")
-    endif ()
+    endforeach ()
+    set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${SAN_FLAGS}")
+    set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${SAN_FLAGS}")
     message (STATUS "Add sanitizer: ${SANITIZE}")
     # Disable sanitizing on make stage e.g. for snowball compiler
     set (ENV{ASAN_OPTIONS} "detect_leaks=0")


More information about the Commits mailing list