From cb5d3ab4e1bfc883d833894f21852b0012e9fd64 Mon Sep 17 00:00:00 2001 From: Alug Date: Wed, 4 Nov 2026 01:38:15 +0100 Subject: [PATCH] add compile flag for building with thread sanitizer indev noticed thats a thing that exists wouldve been quite useful a bunch of times before lol also some fixes for the other sanitizer flags --- src/CMakeLists.txt | 2 ++ src/Makefile | 23 ++++++++++++++++++----- src/dedicated/CMakeLists.txt | 11 ++++++++--- src/sdl/CMakeLists.txt | 11 ++++++++--- 4 files changed, 36 insertions(+), 11 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2618d4077..d4296d362 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -246,6 +246,8 @@ set(SRB2_CONFIG_ASAN OFF CACHE BOOL "Compile with AddressSanitizer (libasan).") set(SRB2_CONFIG_UBSAN OFF CACHE BOOL "Compile with UndefinedBehaviorSanitizer (libubsan).") +set(SRB2_CONFIG_TSAN OFF CACHE BOOL + "Compile with ThreadSanitizer (libtsan).") set(SRB2_CONFIG_MUMBLE ON CACHE BOOL "Enable Mumble support") diff --git a/src/Makefile b/src/Makefile index 27831946a..159d3f7d0 100644 --- a/src/Makefile +++ b/src/Makefile @@ -75,6 +75,7 @@ # PROFILEMODE=1 - Enable performance profiling (gprof). # USEASAN=1 - Enable building with Address Sanitizer. # USEUBSAN=1 - Enable building with Undefined Behavior Sanitizer. +# USETSAN=1 - Enable building with Thread Sanitizer. # NOLIBBACKTRACE=1 - Disable using Libbacktrace for crashlog traces. # # General flags for building: @@ -261,23 +262,35 @@ debug_opts=-D_DEBUG else # build a normal optimized version debug_opts=-DNDEBUG opts+=-O3 -ifndef NOLTO -# compile with LTO -opts+=-flto=auto -libs+=-flto=auto -endif endif ifdef USEASAN # compile with asan opts+=-O1 -fsanitize=address -fno-omit-frame-pointer -fno-common libs+=-fsanitize=address +NOLTO=1 endif ifdef USEUBSAN # compile with ubsan opts+=-O1 -fsanitize=undefined -fno-omit-frame-pointer libs+=-fsanitize=undefined +NOLTO=1 +endif + +ifdef USETSAN +# compile with tsan +opts+=-O1 -fsanitize=thread -fno-omit-frame-pointer +libs+=-fsanitize=thread +NOLTO=1 +endif + +ifndef DEBUGMODE +ifndef NOLTO +# compile with LTO +opts+=-flto=auto +libs+=-flto=auto +endif endif ifdef USEJEMALLOC diff --git a/src/dedicated/CMakeLists.txt b/src/dedicated/CMakeLists.txt index 38fb179f0..923f55596 100644 --- a/src/dedicated/CMakeLists.txt +++ b/src/dedicated/CMakeLists.txt @@ -119,20 +119,25 @@ target_compile_definitions(SRB2SDL2 PRIVATE -DHAVE_THREADS ) -if(SRB2_CONFIG_LTO AND NOT SRB2_CONFIG_DEBUGMODE) +if(SRB2_CONFIG_LTO AND NOT SRB2_CONFIG_DEBUGMODE AND NOT SRB2_CONFIG_ASAN AND NOT SRB2_CONFIG_UBSAN AND NOT SRB2_CONFIG_TSAN) set_property(TARGET SRB2SDL2 PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE) endif() if(SRB2_CONFIG_ASAN) - target_compile_options(SRB2SDL2 PRIVATE -fsanitize=address) + target_compile_options(SRB2SDL2 PRIVATE -O1 -fsanitize=address -fno-omit-frame-pointer -fno-common) target_link_options(SRB2SDL2 PRIVATE -fsanitize=address) endif() if(SRB2_CONFIG_UBSAN) - target_compile_options(SRB2SDL2 PRIVATE -fsanitize=undefined) + target_compile_options(SRB2SDL2 PRIVATE -O1 -fsanitize=undefined -fno-omit-frame-pointer) target_link_options(SRB2SDL2 PRIVATE -fsanitize=undefined) endif() +if(SRB2_CONFIG_TSAN) + target_compile_options(SRB2SDL2 PRIVATE -O1 -fsanitize=thread -fno-omit-frame-pointer) + target_link_options(SRB2SDL2 PRIVATE -fsanitize=thread) +endif() + ## strip debug symbols into separate file when using gcc # if(CMAKE_COMPILER_IS_GNUCC) # if(${CMAKE_BUILD_TYPE} MATCHES Debug) diff --git a/src/sdl/CMakeLists.txt b/src/sdl/CMakeLists.txt index 664010072..866d2bc55 100644 --- a/src/sdl/CMakeLists.txt +++ b/src/sdl/CMakeLists.txt @@ -233,20 +233,25 @@ if(${SDL2_FOUND}) -DHAVE_THREADS ) - if(SRB2_CONFIG_LTO AND NOT SRB2_CONFIG_DEBUGMODE) + if(SRB2_CONFIG_LTO AND NOT SRB2_CONFIG_DEBUGMODE AND NOT SRB2_CONFIG_ASAN AND NOT SRB2_CONFIG_UBSAN AND NOT SRB2_CONFIG_TSAN) set_property(TARGET SRB2SDL2 PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE) endif() if(SRB2_CONFIG_ASAN) - target_compile_options(SRB2SDL2 PRIVATE -fsanitize=address) + target_compile_options(SRB2SDL2 PRIVATE -O1 -fsanitize=address -fno-omit-frame-pointer -fno-common) target_link_options(SRB2SDL2 PRIVATE -fsanitize=address) endif() if(SRB2_CONFIG_UBSAN) - target_compile_options(SRB2SDL2 PRIVATE -fsanitize=undefined) + target_compile_options(SRB2SDL2 PRIVATE -O1 -fsanitize=undefined -fno-omit-frame-pointer) target_link_options(SRB2SDL2 PRIVATE -fsanitize=undefined) endif() + if(SRB2_CONFIG_TSAN) + target_compile_options(SRB2SDL2 PRIVATE -O1 -fsanitize=thread -fno-omit-frame-pointer) + target_link_options(SRB2SDL2 PRIVATE -fsanitize=thread) + endif() + if(NOT SRB2_CONFIG_MUMBLE) target_compile_options(SRB2SDL2 PRIVATE -DNOMUMBLE) endif()