From 14d1a581a18c022c777ec70c8f060bd695fd2f0f Mon Sep 17 00:00:00 2001 From: "Stephen E. Baker" Date: Sun, 6 Apr 2025 13:06:15 -0400 Subject: [PATCH] Add support for AddressSanitizer, drop VLD (#2813) * Add support for AddressSanitizer, drop VLD VLD is no longer maintained so it's removed. Added a convenience option for enabling AddressSanitizer: https://github.com/google/sanitizers/wiki/AddressSanitizer The options provided here work in gcc and llvm. Address sanitizer is available in MSVC but uses a slightly different flag. Undefined Behavior sanitizer in not available for MSVC at this time. I have not enabled it in the windows dev preset because I cannot test it. --- CMakeLists.txt | 36 +++++++++++++++++++----------------- CMakePresets.json | 2 ++ CorsixTH/CMakeLists.txt | 11 ----------- 3 files changed, 21 insertions(+), 28 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 65bc0753..11ace2d1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,21 +1,27 @@ # Cmake File for CorsixTH # OPTIONS AVAILABLE: # Any of the following: (default) + +# CorsixTH Features and behaviour # - WITH_AUDIO : Activate sound (yes) # - WITH_FREETYPE2 : Active support for non-Latin script alphabets (yes) # - WITH_MOVIES : Activate movies (requires with_audio, FFmpeg) (yes) -# - WITH_LUAJIT : Use LuaJIT instead of Lua (must specify library path) (no) # - WITH_UPDATE_CHECK : Activates support to check for new version on launch (requires libcurl) (yes) # - USE_SOURCE_DATADIRS : Use the source directory for loading resources. Incompatible with the install target (no) # - SEARCH_LOCAL_DATADIRS : Search resources in the working directory and the # program directory where the executable stores. Makes the executable # portable (yes for Apple) -# - WITH_VLD : Build with Visual Leak Detector (requires Visual Studio) (no) +# Dependency resolution +# - WITH_LUAJIT : Use LuaJIT instead of Lua (must specify library path) (no) # - USE_PRECOMPILED_DEPS : Use precompiled dependencies on *nix (no) # - WITH_LUAROCKS : Install required luarocks in the macOS app (requires luarocks) +# Tests and debug options # - ENABLE_UNIT_TESTS : Enable Unit Testing Target (requires Catch2) (no) +# - ENABLE_SANITIZERS : Build with AddressSanitizer (ASan) and UndefinedBehaviorSanitizer (UBSan) enabled (no) + +# Additional build targets # - BUILD_ANIMVIEW : Generate AnimView build files (no) # - BUILD_TOOLS : Generate cli tools @@ -32,6 +38,7 @@ option(USE_SOURCE_DATADIRS "Use the source directory for loading resources. Inco option(ENABLE_UNIT_TESTS "Enables Unit Testing Targets" OFF) option(BUILD_ANIMVIEW "Build the animation viewer as part of the build process" OFF) option(BUILD_TOOLS "Build additional CLI tools (rnc_decode)" OFF) +option(ENABLE_SANITIZERS "Build with ASan and UBSan enabled" OFF) set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/CMake) @@ -60,6 +67,16 @@ if(MINGW) set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++") endif() +if(ENABLE_SANITIZERS) + if(MSVC) + # https://learn.microsoft.com/en-us/cpp/build/reference/fsanitize?view=msvc-170 + add_compile_options(/fsanitize=address) + else() + add_compile_options(-fsanitize=address,undefined) + add_link_options(-fsanitize=address,undefined) + endif() +endif() + include(CheckIncludeFiles) set(CORSIX_TH_DONE_TOP_LEVEL_CMAKE ON) @@ -74,9 +91,6 @@ option(SEARCH_LOCAL_DATADIRS "Search resources in the working directory and the program directory where the executable stores." ${SEARCH_LOCAL_DATADIRS_DEFAULT}) -if(MSVC) - option(WITH_VLD "Build with Visual Leak Detector for Visual Studio" OFF) -endif() # Dependency management if(UNIX AND CMAKE_COMPILER_IS_GNU) option(USE_PRECOMPILED_DEPS "Use Precompiled Dependencies" OFF) # Make *nix systems opt in @@ -127,18 +141,6 @@ else() set(CORSIX_TH_LINK_LUA_MODULES OFF) endif() -if(MSVC) - if(WITH_VLD) - set(CORSIX_TH_USE_VLD ON) - message("Note: Visual Leak Detector is enabled") - else() - set(CORSIX_TH_USE_VLD OFF) - message("Note: Visual Leak Detector is disabled (default)") - endif() -else() - set(CORSIX_TH_USE_VLD OFF) -endif() - # Get precompiled dependencies before running the various find modules if(USE_PRECOMPILED_DEPS) message("Note: Using precompiled dependencies.") diff --git a/CMakePresets.json b/CMakePresets.json index e109a959..d5325ac7 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -8,6 +8,7 @@ "cacheVariables": { "USE_SOURCE_DATADIRS": "ON", "ENABLE_UNIT_TESTS": "ON", + "ENABLE_SANITIZERS": "ON", "BUILD_ANIMVIEW": "ON", "BUILD_TOOLS": "ON", "CMAKE_EXPORT_COMPILE_COMMANDS": "ON", @@ -22,6 +23,7 @@ "CMAKE_TOOLCHAIN_FILE": "$env{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake", "USE_SOURCE_DATADIRS": "ON", "ENABLE_UNIT_TESTS": "ON", + "ENABLE_SANITIZERS": "ON", "BUILD_ANIMVIEW": "ON", "BUILD_TOOLS": "ON", "CMAKE_BUILD_TYPE": "Debug" diff --git a/CorsixTH/CMakeLists.txt b/CorsixTH/CMakeLists.txt index a3c30d4a..6eb72805 100644 --- a/CorsixTH/CMakeLists.txt +++ b/CorsixTH/CMakeLists.txt @@ -245,17 +245,6 @@ else() message("Lua modules are not linked and will be loaded at runtime") endif() -if(MSVC AND CORSIX_TH_USE_VLD) - find_package(VLD REQUIRED) - if(VLD_FOUND) - target_link_libraries(CorsixTH PRIVATE ${VLD_LIBRARY}) - include_directories(CorsixTH PRIVATE ${VLD_INCLUDE_DIR}) - message(" VLD found") - else() - message(FATAL_ERROR "Error: VLD Library not found, it is required to build when USE_VLD is set") - endif() -endif() - # Launch script to facilitate out of source builds if(USE_SOURCE_DATADIRS) # Do not generate launch script. The default is fine for this case.