Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LeakSanitizer not working under gdb in Ubuntu 18.04?

I've upgraded my Linux development VM from Ubuntu 16.04 to 18.04 recently, and noticed one thing that has changed. This is on x86-64. With 16.04, I've always had this workflow where I'd build the project I'm working on with gcc (5.4, the stock version in 16.04) and -fsanitize=address and -O0 -g, and then run the executable through gdb (7.11.1, also the version that came with Ubuntu). This worked fine, and at the end, LeakSanitizer would produce a leak report if it detected memory leaks.

In 18.04, this doesn't seem to work anymore; LeakSanitizer complains about running under ptrace:

==5820==LeakSanitizer has encountered a fatal error.
==5820==HINT: For debugging, try setting environment variable LSAN_OPTIONS=verbosity=1:log_threads=1
==5820==HINT: LeakSanitizer does not work under ptrace (strace, gdb, etc)

Then the program crashes:

Thread 1 "spyglass" received signal SIGABRT, Aborted.
__GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51

I'm not sure what is causing the new behavior. On 18.04 I'm building with the default gcc shipped (7.3.0), using -fsanitize=address -O0 -g and debugging with the default gdb (8.1.0). Can the old behavior be somehow re-enabled? Or do I need to change my workflow and detach from the program before killing it to get a leak report?

like image 946
fencekicker Avatar asked Jan 03 '19 13:01

fencekicker


1 Answers

LeakSanitizer internally uses ptrace, probably to suspend all threads such that it can scan for leaks without false positives (see issue 9). Only one application can use ptrace, so if you run your application under gdb or strace, then LeakSanitizer won't be able to attach via ptrace.

If you are not interested in leak debugging, disable it:

export ASAN_OPTIONS=detect_leaks=0

If you do want to enable leak debugging, you must detach the debugger before LeakSanitizer starts scanning. To be able to attach a debugger shortly afterwards, sleep a bit (for example, 10 seconds):

export ASAN_OPTIONS=sleep_before_dying=10
./program

Then in another shell, attach to the application again:

gdb -q -p $(pidof program)

For more a description of the above (and other) options, see https://github.com/google/sanitizers/wiki/AddressSanitizerFlags.

like image 176
Lekensteyn Avatar answered Oct 28 '22 07:10

Lekensteyn