Asan: Make leak and ODR violation reporting CMake options.

pass -DLEAKS=ON or -DODR=ON to enable these features. They have an
effect only with CMAKE_BUILD_TYPE=Asan and don't work on Apple because
Apple clang doesn't enable them.
This commit is contained in:
John Ralls
2023-11-25 11:17:14 -08:00
parent dd0b72cdb5
commit a3f14759ab
5 changed files with 30 additions and 11 deletions

View File

@@ -55,6 +55,8 @@ option (WITH_PYTHON "enable python plugin and bindings" OFF)
option (ENABLE_BINRELOC "compile with binary relocation support" ON)
option (DISABLE_NLS "do not use Native Language Support" OFF)
option (COVERAGE "Instrument an Asan build for coverage reporting" OFF)
option (LEAKS "Report leaks for tests in a non-Apple Asan build." OFF)
option (ODR "Report One Definition Rule violations in tests in a non-Apple Asan build." OFF)
# ############################################################
# These are also settable from the command line in a similar way.
@@ -616,20 +618,36 @@ if (APPLE)
OUTPUT_VARIABLE ASAN_DYNAMIC_LIB
OUTPUT_STRIP_TRAILING_WHITESPACE)
set(ASAN_DYNAMIC_LIB_ENV "DYLD_INSERT_LIBRARIES=${ASAN_DYNAMIC_LIB}")
set(ASAN_BUILD_OPTIONS fast_unwind_on_malloc=0)
elseif(UNIX)
execute_process(COMMAND gcc -print-file-name=libasan.so OUTPUT_VARIABLE LIBASAN_PATH OUTPUT_STRIP_TRAILING_WHITESPACE)
execute_process(COMMAND gcc -print-file-name=libstdc++.so OUTPUT_VARIABLE LIBSTDCXX_PATH OUTPUT_STRIP_TRAILING_WHITESPACE)
set(PRELOADS "${LIBASAN_PATH} ${LIBSTDCXX_PATH}")
set(ASAN_OPTIONS "detect_leaks=0:fast_unwind_on_malloc=0")
set(ASAN_DYNAMIC_LIB_ENV "LD_PRELOAD=${PRELOADS};ASAN_OPTIONS=${ASAN_OPTIONS}")
set(PRELOADS "${LIBASAN_PATH}:${LIBSTDCXX_PATH}")
set(ASAN_BUILD_OPTIONS "detect_leaks=0:fast_unwind_on_malloc=0")
set(ASAN_DYNAMIC_LIB_ENV LD_PRELOAD=${PRELOADS})
endif ()
set(ASAN_BUILD_OPTIONS -fsanitize=address -fsanitize=undefined)
set(ASAN_LINK_OPTIONS -fsanitize=address -fsanitize=undefined)
if (COVERAGE)
list(APPEND ASAN_BUILD_OPTIONS --coverage)
list(APPEND ASAN_LINK_OPTIONS --coverage)
endif()
set(ASAN_COMPILE_OPTIONS -g ${ASAN_BUILD_OPTIONS})
set(ASAN_COMPILE_OPTIONS -g ${ASAN_LINK_OPTIONS})
add_compile_options("$<$<CONFIG:Asan>:${ASAN_COMPILE_OPTIONS}>")
add_link_options("$<$<CONFIG:Asan>:${ASAN_BUILD_OPTIONS}>")
add_link_options("$<$<CONFIG:Asan>:${ASAN_LINK_OPTIONS}>")
# See https://github.com/google/sanitizers/wiki/AddressSanitizerFlags#run-time-flags
set(ASAN_TEST_OPTIONS fast_unwind_on_malloc=0)
if (UNIX AND NOT APPLE)
if (LEAKS)
list(APPEND ASAN_TEST_OPTIONS detect_leaks=1)
else()
list(APPEND ASAN_TEST_OPTIONS detect_leaks=0)
endif()
if (ODR)
list(APPEND ASAN_TEST_OPTIONS detect_odr_violation=2)
else()
list(APPEND ASAN_TEST_OPTIONS detect_odr_violation=0)
endif()
string(REPLACE ";" ":" ASAN_TEST_OPTIONS "${ASAN_TEST_OPTIONS}")
endif()
if (APPLE AND WITH_GNUCASH)
set(CMAKE_MACOSX_RPATH ON)