diff --git a/.github/workflows/dev_asan.yml b/.github/workflows/dev_asan.yml new file mode 100644 index 00000000..a2a5c3ba --- /dev/null +++ b/.github/workflows/dev_asan.yml @@ -0,0 +1,27 @@ +name: run address sanitizer +on: + push: + branches: [main] + pull_request: + branches: [main] +jobs: + Matrix-build: + runs-on: ${{ matrix.os }} + strategy: + matrix: + os: [ubuntu-latest] + python-version: [3.13] # Could add more here, but it just makes the build matrix bigger + steps: + - name: checkout + uses: actions/checkout@v4 + - name: checkout submodules + run: git submodule update --init --recursive + - name: mkdir + run: mkdir build && cd build + - name: cmake config + run: cd build && CXX=clang++ cmake .. -DCMAKE_BUILD_TYPE=Asan -DCOOLPROP_ASAN=ON -DCOOLPROP_CATCH_MODULE=ON + - name: build all Catch tests + run: cmake --build build --config Asan + - name: run all the compiled Catch tests with address sanitizer + run: cd build && ASAN_OPTIONS=detect_stack_use_after_return=1:verbosity=1 ASAN_SYMBOLIZER_PATH=`brew --prefix`/opt/llvm/bin/llvm-symbolizer ./catch_tests --benchmark-samples 1 + # ASAN CLI docs here: https://github.com/google/sanitizers/wiki/AddressSanitizerFlags \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 81dfa20c..76052341 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -78,6 +78,42 @@ option(COOLPROP_NO_EXAMPLES # In the future, we may want to force C++14 since std::make_unique is used in DataStructures.cpp set(CMAKE_CXX_STANDARD 17) + +# Define -DCOOLPROP_ASAN=ON to enable the option of using address sanitizer of clang +if (COOLPROP_ASAN) + + # https://stackoverflow.com/a/64294837 (CC BY-SA 4.0) + if(isMultiConfig) + if(NOT "Asan" IN_LIST CMAKE_CONFIGURATION_TYPES) + list(APPEND CMAKE_CONFIGURATION_TYPES Asan) + endif() + else() + set(allowedBuildTypes Asan Debug Release RelWithDebInfo MinSizeRel) + set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "${allowedBuildTypes}") + + if(CMAKE_BUILD_TYPE AND NOT CMAKE_BUILD_TYPE IN_LIST allowedBuildTypes) + message(FATAL_ERROR "Invalid build type: ${CMAKE_BUILD_TYPE}") + endif() + endif() + + set(CMAKE_C_FLAGS_ASAN + "${CMAKE_C_FLAGS_RelWithDebInfo} -fsanitize=address -fno-omit-frame-pointer" CACHE STRING + "Flags used by the C compiler for Asan build type or configuration." FORCE) + + set(CMAKE_CXX_FLAGS_ASAN + "${CMAKE_CXX_FLAGS_RelWithDebInfo} -fsanitize=address -fno-omit-frame-pointer" CACHE STRING + "Flags used by the C++ compiler for Asan build type or configuration." FORCE) + + set(CMAKE_EXE_LINKER_FLAGS_ASAN + "${CMAKE_EXE_LINKER_FLAGS_RelWithDebInfo} -fsanitize=address" CACHE STRING + "Linker flags to be used to create executables for Asan build type." FORCE) + + set(CMAKE_SHARED_LINKER_FLAGS_ASAN + "${CMAKE_SHARED_LINKER_FLAGS_RelWithDebInfo} -fsanitize=address" CACHE STRING + "Linker lags to be used to create shared libraries for Asan build type." FORCE) + +endif() + # see # https://stackoverflow.com/questions/52509602/cant-compile-c-program-on-a-mac-after-upgrade-to-mojave # https://support.enthought.com/hc/en-us/articles/204469410-OS-X-GCC-Clang-and-Cython-in-10-9-Mavericks