Showing posts with label valgrind. Show all posts
Showing posts with label valgrind. Show all posts

Wednesday, March 19, 2014

ASAN and gcc: How to get line numbers in the stacktrace

ASAN or Address Sanitizer is a nice project by Google that is a memory error detector for C/C++. According to their web it finds:
  • Use after free (dangling pointer dereference)
  • buffer overflow
  • Stack buffer overflow
  • Global buffer overflow
  • Use after return
  • Initialization order bugs

Also it's much faster than valgrind :-) Unfortunately it's not as easy to use as valgrind since it requires a rebuild of your code.

The basic instructions to use ASAN are:
Use the -fsanitize=address switch
together with the suggestion of
To get nicer stack traces in error messages add -fno-omit-frame-pointer.

As a simple example let's see what happens when we run
int main(int, char **)
{
    int a[3];
    a[3] = 4;
    return 0;
}
compiled with
g++ -fno-omit-frame-pointer -fsanitize=address main.cpp

The result:
==8403== ERROR: AddressSanitizer: stack-buffer-overflow
on address 0x7fffbe7c7d8c at pc 0x400779
bp 0x7fffbe7c7d40 sp 0x7fffbe7c7d38
WRITE of size 4 at 0x7fffbe7c7d8c thread T0
    #0 0x400778 (/home/tsdgeos_work/test/a.out+0x400778)
    #1 0x7f2f5cfa6ec4 (/lib/x86_64-linux-gnu/libc-2.19.so+0x21ec4)
    #2 0x400638 (/home/tsdgeos_work/test/a.out+0x400638)
Address 0x7fffbe7c7d8c is located at offset 44 in frame 
of T0's stack: This frame has 1 object(s): [32, 44) 'a'

In this case it's pretty obvious what's wrong with just looking at the code and ASAN is even telling us it is a bad write just after the 'a' object, but note that we are not getting the line number of where the error happens. You'll say, that's why you didn't use the -g switch! So let's see what do we get after compiling with
g++ -g3 -fno-omit-frame-pointer -fsanitize=address main.cpp

The result:
==8467== ERROR: AddressSanitizer: stack-buffer-overflow
on address 0x7fff59ccaa2c at pc 0x400779
bp 0x7fff59cca9e0 sp 0x7fff59cca9d8
WRITE of size 4 at 0x7fff59ccaa2c thread T0
    #0 0x400778 (/home/tsdgeos_work/test/a.out+0x400778)
    #1 0x7f6f75601ec4 (/lib/x86_64-linux-gnu/libc-2.19.so+0x21ec4)
    #2 0x400638 (/home/tsdgeos_work/test/a.out+0x400638)
Address 0x7fff59ccaa2c is located at offset 44 in frame 
of T0's stack: This frame has 1 object(s): [32, 44) 'a'

Same thing :-/

Here is where people may give up or think to switch to clang and try there, but since clang is sometimes more picky with the code (not bad per se) it may be a lot of work to get your code to compile under clang, if you keep digging you'll read some news about GCC not having a ASAN symbolizer [yet], but clang has one and you can still use it with GCC, so if you
export ASAN_SYMBOLIZER_PATH=/usr/bin/llvm-symbolizer-3.4
export ASAN_OPTIONS=symbolize=1
and then run the binary again (the one we just compiled with -g3)

The result:
WRITE of size 4 at 0x7fff4f5f0ebc thread T0
    #0 0x400778 in main /home/tsdgeos_work/test/main.cpp:5
    #1 0x7f3d4dce2ec4 (/lib/x86_64-linux-gnu/libc.so.6+0x21ec4)
    #2 0x400638 in _start (/home/tsdgeos_work/test/a.out+0x400638)
Address 0x7fff4f5f0ebc is located at offset 44 in frame 
of T0's stack: This frame has 1 object(s): [32, 44) 'a'

And now we have line numbers :-)

Wednesday, December 15, 2010

My valgrind talk at OSSBarcamp 2010

The 26th of September I gave a talk about valgrind at OSSBarcamp 2010 in Dublin. In case you want to listen/see it feel free to hit http://vimeo.com/15483105. Warning not good English is included for free in the package

Wednesday, September 22, 2010

Speaking at OSS Barcamp this weekend

This weekend i will be giving two talks at OSS Barcamp in Dublin. The first talk (Saturday 12:00 Room 2) is "An introduction to Qt Quick" (really an introduction as i am just a beginner in Qt Quick) and the second talk (Sunday 11:10 Room 1) is an introduction to Valgrind (I'm not an expert but i think i qualify as seasoned user here). Of course I'll be around a while wearing my KDE gear so if you have questions or wanna have a chat just say "Hi!"

Friday, December 14, 2007

Valgrind 3.3.0

The cool valgrind crew has released a new version, from the NEWS file:

*********
3.3.0 is a feature release with many significant improvements and the
usual collection of bug fixes. This release supports X86/Linux,
AMD64/Linux, PPC32/Linux and PPC64/Linux. Support for recent distros
(using gcc 4.3, glibc 2.6 and 2.7) has been added.

The main excitement in 3.3.0 is new and improved tools. Helgrind
works again, Massif has been completely overhauled and much improved,
Cachegrind now does branch-misprediction profiling, and a new category
of experimental tools has been created, containing two new tools:
Omega and DRD.
*********

Omega is cool it actually tells you when a leak just happened instead of having to wait to the end of program execution to know you have a leak :D

And i already blogged on how useful helgrind can be to detect problems in thread programs, but i'll say it again, helgrind rocks too!

Thursday, November 15, 2007

helgrind is back!

Today i was having a hard time debugging some code of mine that used threads and i thought of helgrind, as of valgrind 3.2.3 trying to use it gives you

Helgrind is currently not working, because:
(a) it is not yet ready to handle the Vex IR and the use with 64-bit
platforms introduced in Valgrind 3.0.0
(b) we need to get thread operation tracking working again after
the changes added in Valgrind 2.4.0
If you want to use Helgrind, you'll have to use Valgrind 2.2.0, which is
the most recent Valgrind release that contains a working Helgrind.

Sorry for the inconvenience. Let us know if this is a problem for you.

So i downloaded valgrind 2.2.0, but it complained about me having a too new glibc, so i downloaded valgrind from svn and YES! it's there :-) It made me realize i needed to add QDeepCopy to some QStrings and it is working now :-) Yet again valgrind to the rescue!