Skip to content

Add CMake options for turning on ASAN and HWASAN #1106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 13, 2024

Conversation

BenWibking
Copy link
Collaborator

@BenWibking BenWibking commented Jun 13, 2024

PR Summary

Add CMake options ENABLE_ASAN and ENABLE_HWASAN to build with AddressSanitizer and HWAddressSanitizer, respectively, turned on.

ASAN is supported in both clang and gcc. HWASAN is support by clang and (in theory) gcc.

Then the app is run as normal. It will stop and print an error if a memory bug is detected. This should detect almost all out of bounds access, uninitalized values, and other memory errors for CPU builds.

Usage notes:

  • Miscellaneous useful env vars to configure its behavior when running:

    ASAN_OPTIONS=abort_on_error=1:fast_unwind_on_malloc=1:detect_leaks=0 UBSAN_OPTIONS=print_stacktrace=0 LSAN_OPTIONS=suppressions=leak_suppress.txt
    
  • HWAddressSanitizer only works on Linux ARM platforms but runs significantly faster than AddressSanitizer. In my testing, additional flags are required for HWAsan:

    -DCMAKE_CXX_FLAGS="-fPIC" -DCMAKE_EXE_LINKER_FLAGS="-fuse-ld=lld" -DCMAKE_SHARED_LINKER_FLAGS="-fuse-ld=lld" -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++
    

    (I was not able to get it working with gcc, since building HWASAN support in gcc is optional and distros don't do it.)

PR Checklist

  • Code passes cpplint
  • New features are documented.
  • Adds a test for any bugs fixed. Adds tests for new features.
  • Code is formatted
  • Changes are summarized in CHANGELOG.md
  • Change is breaking (API, behavior, ...)
    • Change is additionally added to CHANGELOG.md in the breaking section
    • PR is marked as breaking
    • Short summary API changes at the top of the PR (plus optionally with an automated update/fix script)
  • CI has been triggered on Darwin for performance regression tests.
  • Docs build
  • (@lanl.gov employees) Update copyright on changed files

Add CMake options to build with AddressSanitizer/HWAddressSanitizer turned on.

HWAddressSanitizer only works on ARM platforms due to hardware support but runs faster than AddressSanitizer.
Copy link
Collaborator

@pgrete pgrete left a comment

Choose a reason for hiding this comment

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

I think it's a good idea to add those.
How much slower does the code run with those options?
I'm asking wrt to whether it's worth to activate this by default to one of the short or extended tests (once the issue reported in #1107 are fixed).

PS: Changelog entry is missing;)

Comment on lines +298 to +299
add_compile_options(-fsanitize=address -fsanitize=undefined -fno-sanitize-recover=all -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow -fno-sanitize=null -fno-sanitize=alignment)
add_link_options(-fsanitize=address -fsanitize=undefined)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This will then also be forwarded to all submodules, we include, isn't it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, it should be.

Copy link
Collaborator

@lroberts36 lroberts36 left a comment

Choose a reason for hiding this comment

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

LGTM, this will definitely be helpful to have. I am going to take a look at the logical location bit shift issue, I think it will be straightforward to fix.

@lroberts36
Copy link
Collaborator

I'm asking wrt to whether it's worth to activate this by default to one of the short or extended tests (once the issue reported in #1107 are fixed).

I definitely support having it run on one of the extended tests (unless it takes hours to run with these options on).

@BenWibking BenWibking changed the title add CMake options for turning on ASAN and HWASAN Add CMake options for turning on ASAN and HWASAN Jun 13, 2024
@BenWibking
Copy link
Collaborator Author

Running the test suite with this turned on is only about a factor of 2-3 slower on my laptop, so I think it should be fine to enable for the extended CI.

@BenWibking BenWibking enabled auto-merge June 13, 2024 14:23
@BenWibking BenWibking merged commit 6268f2c into develop Jun 13, 2024
50 checks passed
@BenWibking BenWibking deleted the BenWibking/add-asan-options branch June 13, 2024 16:09
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.

3 participants