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)

View File

@@ -96,6 +96,7 @@ if(WITH_PYTHON)
add_test(NAME sqlite3test COMMAND sqlite3test)
add_dependencies(check sqlite3test)
set_tests_properties(sqlite3test PROPERTIES ENVIRONMENT "$<$<CONFIG:Asan>:;ASAN_OPTIONS=${ASAN_TEST_OPTIONS}>")
install(TARGETS gnucash_core_c
LIBRARY DESTINATION ${PYTHON_SYSCONFIG_OUTPUT}/gnucash

View File

@@ -9,7 +9,7 @@ if (WITH_PYTHON)
add_dependencies(check test-python-bindings)
add_test(NAME python-bindings COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/runTests.py.in)
set(PYTHON_ENV "GNC_UNINSTALLED=1;GNC_BUILDDIR=${CMAKE_BINARY_DIR};PYTHONPATH=${PYTHON_SYSCONFIG_BUILD}:${LIBDIR_BUILD}/gnucash:${test_core_dir}")
set(ASAN_ENV "${ASAN_DYNAMIC_LIB_ENV};ASAN_OPTIONS=fast_unwind_on_malloc=0")
set(ASAN_ENV "${ASAN_DYNAMIC_LIB_ENV};ASAN_OPTIONS=${ASAN_TEST_OPTIONS}")
set_tests_properties(python-bindings PROPERTIES ENVIRONMENT "$<IF:$<CONFIG:Asan>,${PYTHON_ENV};${ASAN_ENV},${PYTHON_ENV}>")
endif()

View File

@@ -278,7 +278,7 @@ function(gnc_add_scheme_targets _TARGET)
add_custom_command(
OUTPUT ${output_file}
COMMAND ${CMAKE_COMMAND} -E env
"${GUILE_ENV}$<$<CONFIG:Asan>:;${ASAN_DYNAMIC_LIB_ENV}>"
"${GUILE_ENV}$<$<CONFIG:Asan>:;${ASAN_DYNAMIC_LIB_ENV};ASAN_OPTIONS=${ASAN_BUILD_OPTIONS}>"
${GUILE_EXECUTABLE} -e "\(@@ \(guild\) main\)" -s ${GUILD_EXECUTABLE} compile -o ${output_file} ${source_file_abs_path}
DEPENDS ${guile_depends}
MAIN_DEPENDENCY ${source_file_abs_path}

View File

@@ -92,7 +92,7 @@ function(gnc_add_test _TARGET _SOURCE_FILES TEST_INCLUDE_VAR_NAME TEST_LIBS_VAR_
add_executable(${_TARGET} EXCLUDE_FROM_ALL ${_SOURCE_FILES})
target_link_libraries(${_TARGET} PRIVATE ${TEST_LIBS})
target_include_directories(${_TARGET} PRIVATE ${TEST_INCLUDE_DIRS})
set_tests_properties(${_TARGET} PROPERTIES ENVIRONMENT "$<IF:$<CONFIG:Asan>,${ENVVARS};ASAN_OPTIONS=fast_unwind_on_malloc=0,${ENVVARS}>")
set_tests_properties(${_TARGET} PROPERTIES ENVIRONMENT "${ENVVARS}$<$<CONFIG:Asan>:;ASAN_OPTIONS=${ASAN_TEST_OPTIONS}>")
add_dependencies(check ${_TARGET})
endfunction()
@@ -118,7 +118,7 @@ function(gnc_add_scheme_test _TARGET _SOURCE_FILE)
(exit (run-test))"
)
get_guile_env()
set_tests_properties(${_TARGET} PROPERTIES ENVIRONMENT "$<IF:$<CONFIG:Asan>,${GUILE_ENV};${ASAN_DYNAMIC_LIB_ENV};ASAN_OPTIONS=fast_unwind_on_malloc=0;${ARGN},${GUILE_ENV};${ARGN}>")
set_tests_properties(${_TARGET} PROPERTIES ENVIRONMENT "${GUILE_ENV}$<$<CONFIG:Asan>:;${ASAN_DYNAMIC_LIB_ENV};ASAN_OPTIONS=${ASAN_TEST_OPTIONS}>;${ARGN}>")
endfunction()
function(gnc_add_scheme_tests _SOURCE_FILES)