Instrumenting an application with sanitizers
LLVM comes with a couple of sanitizers. These are passes that instrument the intermediate representation (IR) to check for certain misbehavior of an application. Usually, they require library support, which is part of the compiler-rt
project. The sanitizers can be enabled in clang, which makes them very comfortable to use. To build the compiler-rt
project, we can simply add the -DLLVM_ENABLE_RUNTIMES=compiler-rt
CMake variable to the initial CMake configuration step when building LLVM.
In the following sections, we will look at the address
, memory
, and thread
sanitizers. First, we’ll look at the address
sanitizer.
Detecting memory access problems with the address sanitizer
You can use the address
sanitizer to detect different types of memory access bugs within an application. This includes common errors such as using dynamically allocated memory after freeing it or writing to dynamically allocated memory outside the boundaries...