From 24977861a66de5b474bcda7bcf2899f5dcc99772 Mon Sep 17 00:00:00 2001 From: Razish Date: Fri, 2 Sep 2022 22:07:46 +1000 Subject: [PATCH 1/2] [Shared] provide option to use AddressSanitizer --- CMakeLists.txt | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index d32ed22ab8..48554796ef 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -49,6 +49,7 @@ option(BuildJK2SPGame "Whether to create projects for the jk2 sp gamecode mod (j option(BuildJK2SPRdVanilla "Whether to create projects for the jk2 sp renderer (rdjosp-vanilla_x86.dll)" OFF) option(BuildTests "Whether to build automatic unit tests (requires Boost)" OFF) +option(UseSanitizer "Whether to enable runtime sanitizers (e.g. AddressSanitizer)" OFF) include(CMakeDependentOption) cmake_dependent_option(BuildSymbolServer "Build WIP Windows Symbol Server (experimental and unused)" OFF "NOT WIN32 OR NOT MSVC" OFF) @@ -234,6 +235,11 @@ elseif (("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU") OR ("${CMAKE_C_COMPILER_ID}" M set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "") set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "") + if(CMAKE_BUILD_TYPE MATCHES "DEBUG" OR CMAKE_BUILD_TYPE MATCHES "Debug" AND UseSanitizer) + add_compile_options(-fsanitize=address) + add_link_options(-fsanitize=address) + endif() + # additional flags for debug configuration set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -ggdb") set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -ggdb") From b5a309387a68baf302bce900a8994b14c503854e Mon Sep 17 00:00:00 2001 From: Razish Date: Fri, 7 Apr 2023 21:48:18 +1000 Subject: [PATCH 2/2] split UseSanitizer into UseAddressSanitizer and UseUndefinedSanitizer --- CMakeLists.txt | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 48554796ef..56fc5cf1e2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -49,7 +49,8 @@ option(BuildJK2SPGame "Whether to create projects for the jk2 sp gamecode mod (j option(BuildJK2SPRdVanilla "Whether to create projects for the jk2 sp renderer (rdjosp-vanilla_x86.dll)" OFF) option(BuildTests "Whether to build automatic unit tests (requires Boost)" OFF) -option(UseSanitizer "Whether to enable runtime sanitizers (e.g. AddressSanitizer)" OFF) +option(UseAddressSanitizer "Whether to enable runtime address sanitizer" OFF) +option(UseUndefinedSanitizer "Whether to enable runtime Undefined Behavior sanitizer" OFF) include(CMakeDependentOption) cmake_dependent_option(BuildSymbolServer "Build WIP Windows Symbol Server (experimental and unused)" OFF "NOT WIN32 OR NOT MSVC" OFF) @@ -235,9 +236,14 @@ elseif (("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU") OR ("${CMAKE_C_COMPILER_ID}" M set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "") set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "") - if(CMAKE_BUILD_TYPE MATCHES "DEBUG" OR CMAKE_BUILD_TYPE MATCHES "Debug" AND UseSanitizer) + if(UseAddressSanitizer) + # also raise stack size drastically (to 64MiB), since the sanitizer adds overhead to stack frames add_compile_options(-fsanitize=address) - add_link_options(-fsanitize=address) + add_link_options(-fsanitize=address -z stack-size=4000000) + endif() + if(UseUndefinedSanitizer) + add_compile_options(-fsanitize=undefined) + add_link_options(-fsanitize=undefined) endif() # additional flags for debug configuration