introduce

Fast Memory Detector can detect the following bugs:

  1. Out-of-bounds access to heap memory, stack memory and global variables
  2. Use-after-free Memory reuse
  3. Use after return (use-after-return)
  4. Double free

Compile build

See the build guide of Clang. CMake is not supported

Method of use

You only need to add when compiling and linking the program-faddress-sanitizeroptions

For reasonable performance, add-O1Or higher optimization levels

For better stack tracing in error messages, add -fno-omit-frame-pointeroptions

To get a perfect stack trace, you might want to disable inline (just use -O1) and tail call elimination-fno-optimize-sibling-calls

__has_feature(address_sanitizer)

In some cases, depending on whether AddressSanitizer is enabled, the _has_feature can be used to do this:

#if defined(__has_feature) && __has_feature(address_sanitizer)
  code that runs only under AddressSanitizer
#else
  code that does not run under AddressSanitizer
#endif
Copy the code

limited

AddressSanitizer uses more real memory than a local run, depending on the size allocated

On 64-bit platforms, AddresSanitizer maps 16+ TERabytes of virtual address space, which means tools like ULimit don’t work

Static linking is not supported

Attached link address:

ASAN document