I'm compiling with Address Sanitizer, and I'm trying to get leak sanitizer reports, but it is only producing an Address Sanitizer report and not producing a LeakSanitizer report after program exit for some reason.
I'm compiling on Centos 7.9 with G++ 9.3.1, Cmake version 3.16.2.
I'm compiling with
list(APPEND SAN_FLAGS "-fsanitize=address" "-fno-omit-frame-pointer" "-g3")
to enable leak sanitizer (which should be enabled with address sanitizer on Linux according to the docs)
I set
Environment="ASAN_OPTIONS=log_path=/var/log/foo_asan.log:detect_leaks=1"
in the service file, then ran my program with systemd (also on Centos 7.9). Stopping the program with systemctl creates a foo_asan. log file as expected, but the log file only has an AddressSanitizer report, but no LeakSanitizer report. How do I get the Leaksanitizer report?
By default, AddressSanitizer aborts on error. LeakSanitizer only runs on
exit
, and since your program doesn'texit
, you get no leak report.Here is an example program which has both a memory leak and a buffer overflow:
If I build it with
clang -g -fsanitize=address leaky.c -o leaky
and run it withASAN_OPTIONS=detect_leaks=1
, I only get the error report:This answer shows how to make the program not abort on errors. When I do that, I get both the AddressSanitizer and the LeakSanitizer reports: