Gabime/tsan (#3237)

* Fixed race condition in tests

* Support for thread sanitizer
This commit is contained in:
Gabi Melman 2024-11-01 15:14:27 +02:00 committed by gabime
parent 0e95bba098
commit c122eaf749
2 changed files with 16 additions and 3 deletions

View File

@ -49,7 +49,7 @@ function(spdlog_enable_warnings target_name)
endfunction() endfunction()
# Enable address sanitizer (gcc/clang only) # Enable address sanitizer (gcc/clang only)
function(spdlog_enable_sanitizer target_name) function(spdlog_enable_addr_sanitizer target_name)
if(NOT CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang") if(NOT CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
message(FATAL_ERROR "Sanitizer supported only for gcc/clang") message(FATAL_ERROR "Sanitizer supported only for gcc/clang")
endif() endif()
@ -60,3 +60,14 @@ function(spdlog_enable_sanitizer target_name)
target_compile_options(${target_name} PRIVATE -fno-omit-frame-pointer) target_compile_options(${target_name} PRIVATE -fno-omit-frame-pointer)
target_link_libraries(${target_name} PRIVATE -fsanitize=address,undefined -fuse-ld=gold) target_link_libraries(${target_name} PRIVATE -fsanitize=address,undefined -fuse-ld=gold)
endfunction() endfunction()
# Enable thread sanitizer (gcc/clang only)
function(spdlog_enable_thread_sanitizer target_name)
if(NOT CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
message(FATAL_ERROR "Sanitizer supported only for gcc/clang")
endif()
message(STATUS "Thread sanitizer enabled")
target_compile_options(${target_name} PRIVATE -fsanitize=thread)
target_compile_options(${target_name} PRIVATE -fno-omit-frame-pointer)
target_link_libraries(${target_name} PRIVATE -fsanitize=thread -fuse-ld=gold)
endfunction()

View File

@ -80,8 +80,10 @@ function(spdlog_prepare_test test_target spdlog_lib)
endif() endif()
target_link_libraries(${test_target} PRIVATE Catch2::Catch2WithMain) target_link_libraries(${test_target} PRIVATE Catch2::Catch2WithMain)
if(SPDLOG_SANITIZE_ADDRESS) if(SPDLOG_SANITIZE_ADDRESS)
spdlog_enable_sanitizer(${test_target}) spdlog_enable_addr_sanitizer(${test_target})
endif() elseif (SPDLOG_SANITIZE_THREAD)
spdlog_enable_thread_sanitizer(${test_target})
endif ()
add_test(NAME ${test_target} COMMAND ${test_target}) add_test(NAME ${test_target} COMMAND ${test_target})
set_tests_properties(${test_target} PROPERTIES RUN_SERIAL ON) set_tests_properties(${test_target} PROPERTIES RUN_SERIAL ON)
endfunction() endfunction()