OxfordRSE / template-project-cpp

An template C++ project using CMake for build configuration and Catch/TravisCI for automated testing
Other
25 stars 8 forks source link

Investigate adding address sanitizer #8

Closed fcooper8472 closed 6 years ago

fcooper8472 commented 6 years ago

This seems to be the current best practice for detecting memory errors in C++ code, and it seems very straightforward to use.

AddressSanitizer is a fast memory error detector. It consists of a compiler instrumentation module and a run-time library. The tool can detect the following types of bugs:

Out-of-bounds accesses to heap, stack and globals Use-after-free Use-after-return (runtime flag ASAN_OPTIONS=detect_stack_use_after_return=1) Use-after-scope (clang flag -fsanitize-address-use-after-scope) Double-free, invalid free Memory leaks (experimental) Typical slowdown introduced by AddressSanitizer is 2x.

@martinjrobins what's the modern-CMake-approved method of passing

-O1 -g -fsanitize=address -fno-omit-frame-pointer

to the (clang) compiler, and

-g -fsanitize=address

to the (clang) linker?

martinjrobins commented 6 years ago

have a look at how I did the openmp compile and link flags using target_compile_options and target_link_libraries:

# OpenMP as an optional component for tests
option(Template_USE_OPENMP "Use OpenMP for shared memory parallism" OFF)
if (Template_USE_OPENMP)
    find_package(OpenMP REQUIRED)
    target_compile_options(mytest PRIVATE "${OpenMP_CXX_FLAGS}")
    target_link_libraries(mytest PRIVATE "${OpenMP_EXE_LINKER_FLAGS}")
endif()

You will also need to filter out only the clang compiler as well If you need these flags for both the library and test executables, best way is to add them to mylib as PUBLIC, then they will automatically be transferred to mytest

fcooper8472 commented 6 years ago

I have an implementation ready to go, just want your opinion @martinjrobins:

If someone runs cmake -DTemplate_MEMCHECK=ON and they are using gcc rather than clang, would you prefer a CMake WARNING or a CMake FATAL_ERROR?

I think if the user specifically requests functionality it's OK to make it a FATAL_ERROR?

martinjrobins commented 6 years ago

I agree, make it a FATAL_ERROR. If I request a feature and it can't be satisfied, that is an error.

fcooper8472 commented 6 years ago

Happy with that changeset @martinjrobins ?

martinjrobins commented 6 years ago

looks good. We might also want to change the way that the coverage option is handled so that it is consistent with the memcheck

fcooper8472 commented 6 years ago

Do we just want coverage on GCC, or Clang too?

martinjrobins commented 6 years ago

well, probably want the ability to check coverage on both gcc and clang, but travis only do it for one of them.

On 9 May 2018 at 16:24, Fergus Cooper notifications@github.com wrote:

Do we just want coverage on GCC, or Clang too?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/OxfordRSE/template-project-cpp/issues/8#issuecomment-387777102, or mute the thread https://github.com/notifications/unsubscribe-auth/ABGF9LDTe_mNRnlNwYSQLoj-zXOTMrggks5twwowgaJpZM4T4Lv9 .

fcooper8472 commented 6 years ago

OK - I'll close this issue and open a new one for sorting out coverage.