modern-cpp-template
modern-cpp-template copied to clipboard
[FEATURE] add AddressSanitizer as an option
Is your feature request related to a problem? Please describe.
AddressSanitizer is a fast memory error detector. It consists of a compiler instrumentation module and a run-time library. see https://github.com/google/sanitizers/wiki/AddressSanitizer. The tool can detect the following types of bugs:
- Out-of-bounds accesses to heap, stack and globals
- Use-after-free
- Use-after-return (clang flag -fsanitize-address-use-after-return=(never|runtime|always) default: runtime)
- Enable with: ASAN_OPTIONS=detect_stack_use_after_return=1 (already enabled on Linux).
- Disable with: ASAN_OPTIONS=detect_stack_use_after_return=0.
- Use-after-scope (clang flag -fsanitize-address-use-after-scope)
- Double-free, invalid free
- Memory leaks (experimental)
This tool is very fast. The average slowdown of the instrumented program is ~2x (see
And I think it's quite useful in C/C++ projects. You can simply enable it by compiling and linking your program with -fsanitize=address flag.
Describe the solution you'd like
Maybe we can add it as an option in cmake/StandardSettings.cmake ?
For example, like this:
option(${PROJECT_NAME}_ENABLE_ASAN "Enable Address Sanitize to detect memory error." ON)
if(${PROJECT_NAME}_ENABLE_ASAN)
add_compile_options(-fsanitize=address)
add_link_options(-fsanitize=address)
endif()