]> git.ipfire.org Git - thirdparty/zlib-ng.git/commitdiff
Added common sanitizer flags for getting optimal stack traces.
authorNathan Moinvaziri <nathan@nathanm.com>
Fri, 18 Mar 2022 23:05:18 +0000 (16:05 -0700)
committerHans Kristian Rosbach <hk-github@circlestorm.org>
Mon, 21 Mar 2022 10:33:08 +0000 (11:33 +0100)
.github/workflows/cmake.yml
cmake/detect-sanitizer.cmake

index e7a099a437374eab2c4de33f0ee5dd91b0fd95cd..f9e97d314c42b68747f47545269014d23ec83039 100644 (file)
@@ -41,22 +41,22 @@ jobs:
             cmake-args: -DZLIB_COMPAT=ON -DZLIB_SYMBOL_PREFIX=zTest_
             codecov: ubuntu_gcc_compat_sprefix
 
-          - name: Ubuntu GCC -O1 OSB
+          - name: Ubuntu GCC -O3 OSB
             os: ubuntu-latest
             compiler: gcc
             cxx-compiler: g++
             build-dir: ../build
             build-src-dir: ../zlib-ng
             codecov: ubuntu_gcc_osb
-            cflags: -O1 -g3
+            cflags: -O3
 
-          - name: Ubuntu GCC -O3 No Unaligned UBSAN
+          - name: Ubuntu GCC -O1 No Unaligned UBSAN
             os: ubuntu-latest
             compiler: gcc
             cxx-compiler: g++
             cmake-args: -DWITH_UNALIGNED=OFF -DWITH_SANITIZER=Undefined
-            codecov: ubuntu_gcc_o3
-            cflags: -O3
+            codecov: ubuntu_gcc_o1
+            cflags: -O1
 
           - name: Ubuntu GCC 32-bit
             os: ubuntu-latest
@@ -347,7 +347,6 @@ jobs:
             cmake-args: -GNinja -DWITH_SANITIZER=Memory
             packages:  ninja-build llvm-11-tools
             gcov-exec: llvm-cov-11 gcov
-            cflags: -g3 -fno-omit-frame-pointer -fno-optimize-sibling-calls -fsanitize-memory-track-origins
 
           - name: Windows MSVC Win32
             os: windows-latest
index 965ddca8d0e68a50a912edadd3644c4951c384a3..ae3d37317c80d6bcac87a3201792331bd9ea2c5f 100644 (file)
@@ -1,6 +1,22 @@
 # detect-sanitizer.cmake -- Detect supported compiler sanitizer flags
 # Licensed under the Zlib license, see LICENSE.md for details
 
+macro(add_common_sanitizer_flags)
+    if(CMAKE_C_COMPILER_ID MATCHES "GNU" OR CMAKE_C_COMPILER_ID MATCHES "Clang")
+        add_compile_options(-g3)
+    endif()
+    check_c_compiler_flag(-fno-omit-frame-pointer HAVE_NO_OMIT_FRAME_POINTER)
+    if(HAVE_NO_OMIT_FRAME_POINTER)
+        add_compile_options(-fno-omit-frame-pointer)
+        add_link_options(-fno-omit-frame-pointer)
+    endif()
+    check_c_compiler_flag(-fno-optimize-sibling-calls HAVE_NO_OPTIMIZE_SIBLING_CALLS)
+    if(HAVE_NO_OPTIMIZE_SIBLING_CALLS)
+        add_compile_options(-fno-optimize-sibling-calls)
+        add_link_options(-fno-optimize-sibling-calls)
+    endif()
+endmacro()
+
 macro(check_sanitizer_support known_checks supported_checks)
     set(available_checks "")
 
@@ -41,6 +57,7 @@ macro(add_address_sanitizer)
         message(STATUS "Address sanitizer is enabled: ${supported_checks}")
         add_compile_options(-fsanitize=${supported_checks})
         add_link_options(-fsanitize=${supported_checks})
+        add_common_sanitizer_flags()
     else()
         message(STATUS "Address sanitizer is not supported")
     endif()
@@ -55,6 +72,7 @@ macro(add_address_sanitizer)
             message(STATUS "Leak sanitizer is enabled: ${supported_checks}")
             add_compile_options(-fsanitize=${supported_checks})
             add_link_options(-fsanitize=${supported_checks})
+            add_common_sanitizer_flags()
         else()
             message(STATUS "Leak sanitizer is not supported")
         endif()
@@ -67,6 +85,13 @@ macro(add_memory_sanitizer)
         message(STATUS "Memory sanitizer is enabled: ${supported_checks}")
         add_compile_options(-fsanitize=${supported_checks})
         add_link_options(-fsanitize=${supported_checks})
+        add_common_sanitizer_flags()
+
+        check_c_compiler_flag(-fsanitize-memory-track-origins HAVE_MEMORY_TRACK_ORIGINS)
+        if(HAVE_MEMORY_TRACK_ORIGINS)
+            add_compile_options(-fsanitize-memory-track-origins)
+            add_link_options(-fsanitize-memory-track-origins)
+        endif()
     else()
         message(STATUS "Memory sanitizer is not supported")
     endif()
@@ -78,6 +103,7 @@ macro(add_thread_sanitizer)
         message(STATUS "Thread sanitizer is enabled: ${supported_checks}")
         add_compile_options(-fsanitize=${supported_checks})
         add_link_options(-fsanitize=${supported_checks})
+        add_common_sanitizer_flags()
     else()
         message(STATUS "Thread sanitizer is not supported")
     endif()
@@ -132,6 +158,8 @@ macro(add_undefined_sanitizer)
         if(WITH_UNALIGNED)
             add_compile_options(-fno-sanitize=alignment)
         endif()
+
+        add_common_sanitizer_flags()
     else()
         message(STATUS "Undefined behavior sanitizer is not supported")
     endif()