Skip to content

Add ASAN to root CMake#109

Open
nicholasbl wants to merge 3 commits intoNatLabRockies:developfrom
nicholasbl:develop
Open

Add ASAN to root CMake#109
nicholasbl wants to merge 3 commits intoNatLabRockies:developfrom
nicholasbl:develop

Conversation

@nicholasbl
Copy link
Contributor

@nicholasbl nicholasbl commented Mar 3, 2026

AddressSanitizer is a great feature that helps catch memory safety issues early (heap/stack buffer overflows, use-after-free, etc.), and makes debugging crashes far easier during development.

This PR adds an opt-in AddressSanitizer build configuration controlled by a new CMake option:

  • SOLTRACE_ENABLE_ASAN (by default, this is OFF)

When enabled and the project is built with Clang or GCC, the build will compile and link with -fsanitize=address. Note that this is not supported on MSVC; in this case, CMake will skip the flags. This is added to the root CMake so that all code (including third-party) can be covered. ASan is not added to the CI matrix in this PR, but can be, later on.

To enable ASan at configure time:

cmake -S . -B build-asan -DSOLTRACE_ENABLE_ASAN=ON -DCMAKE_BUILD_TYPE=Debug
cmake --build build-asan
ctest --test-dir build-asan

Note that this will inflate execution time as it adds additional instrumentation to the code.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds an opt-in AddressSanitizer (ASan) configuration at the root CMake level so the entire build (including third-party) can be instrumented when using supported compilers.

Changes:

  • Introduces SOLTRACE_ENABLE_ASAN CMake option (default OFF).
  • When enabled, applies -fsanitize=address (and -fno-omit-frame-pointer) for supported compilers, with warnings for unsupported toolchains (e.g., MSVC).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

CMakeLists.txt Outdated
Comment on lines +27 to +28
elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU")
message(STATUS "Enabling address sanitizer")
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU" excludes AppleClang, so ASan won't be enabled on macOS even though AppleClang supports -fsanitize=address. Consider including AppleClang in the check (or using a more robust compiler-ID test) to match the PR description (“Clang or GCC”).

Copilot uses AI. Check for mistakes.
Copy link
Member

@jmaack24 jmaack24 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would there be any objections to adding AppleClang to the compiler check? I tried this on my Mac and had issues using this with the GCC compiler, but AppleClang worked just fine.

@nicholasbl
Copy link
Contributor Author

Sure. It's probably a solid idea to just restrict this to Clang + AppleClang at the moment as Clang does a much better job at keeping the runtimes OK with ASAN active. Let me update the branch...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants