From: Nathan Moinvaziri Date: Fri, 18 Mar 2022 23:05:18 +0000 (-0700) Subject: Added common sanitizer flags for getting optimal stack traces. X-Git-Tag: 2.1.0-beta1~318 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=85f9709137a88c546e5dd188f7cb196b80f84253;p=thirdparty%2Fzlib-ng.git Added common sanitizer flags for getting optimal stack traces. --- diff --git a/.github/workflows/cmake.yml b/.github/workflows/cmake.yml index e7a099a4..f9e97d31 100644 --- a/.github/workflows/cmake.yml +++ b/.github/workflows/cmake.yml @@ -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 diff --git a/cmake/detect-sanitizer.cmake b/cmake/detect-sanitizer.cmake index 965ddca8..ae3d3731 100644 --- a/cmake/detect-sanitizer.cmake +++ b/cmake/detect-sanitizer.cmake @@ -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()