Saturday, March 22, 2014

ASAN and plugins

In ASAN and libraries Milian asked if the reasoning for libraries also applied for plugins. Since I had no idea, I had to try it.

Here comes the output

#include <QDebug>
#include <QLibrary>

int main(int, char **)
    QLibrary l("libshared");
    qDebug() << l.load();

    return 0;
#include "shared.h"

static Foo f;

    int *a = 0;
    *a = 33;
class Foo
export ASAN_SYMBOLIZER_PATH=/usr/bin/llvm-symbolizer-3.4
export ASAN_OPTIONS=symbolize=1
g++ -shared -o shared.cpp  -g3 -fPIC
g++ -fsanitize=address main.cpp -g3 -I /usr/include/qt4/QtCore/ \
    -I /usr/include/qt4/ -lQtCore
And then we run it!
$ LD_LIBRARY_PATH=. ./a.out 
==7048== ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000
(pc 0x7f199c1326aa sp 0x7fff37e557c0 bp 0x7fff37e557c0 T0)
AddressSanitizer can not provide additional info.
 #0 0x7f199c1326a9 in Foo::Foo() /home/tsdgeos/test/shared.cpp:8
 #1 0x7f199c1326da in __static_initialization_and_destruction_0(int, int) 
 #2 0x7f199c1326ef in _GLOBAL__sub_I_shared.cpp /home/tsdgeos/test/shared.cpp:9
 #3 0x7f19a132b139 (/lib64/
 #4 0x7f19a132b222 (/lib64/
 #5 0x7f19a132fc6f (/lib64/
 #6 0x7f19a132aff3 (/lib64/
 #7 0x7f19a132f3ba (/lib64/
 #8 0x7f199d1a602a (/lib/x86_64-linux-gnu/
 #9 0x7f19a132aff3 (/lib64/
 #10 0x7f199d1a662c (/lib/x86_64-linux-gnu/
 #11 0x7f199d1a60c0 (/lib/x86_64-linux-gnu/
 #12 0x7f199e0156b7 (/usr/lib/x86_64-linux-gnu/
 #13 0x7f199e010599 (/usr/lib/x86_64-linux-gnu/
 #14 0x4011c0 in main /home/tsdgeos/test/main.cpp:8
 #15 0x7f199d5e8ec4 (/lib/x86_64-linux-gnu/
 #16 0x401078 in _start (/home/tsdgeos/test/a.out+0x401078)
SUMMARY: AddressSanitizer: SEGV /home/tsdgeos/test/shared.cpp:8 Foo::Foo()
==7048== ABORTING

So it seems that "plugins are just libraries" applies here :)


Milian said...

Thanks Albert :)

zecke said...

You apply ASAN to a clear NULL pointer dereference leading to a segmentation fault. This is something that the catchsegv command handled for years.

Wouldn't a more realistic test be:

int a[1];
a[2] = 3;

In case you link to the library you have stack corruption. In case you compile the library with ASAN you will get the ASAN warning?

Albert Astals Cid said...

You are right, need to compile the library with ASAN in that case to catch that, otherwise it won't work.

Albert Astals Cid said...

Updated at

afiestas said...

Just used this to get some stuff fixed, thanks :)!