I have several versions of a project checkout out and compiled. If I spot an error, I compare the versions to narrow the problem down. Sometimes I enable sanitizers like the AddressSanitizer. If I re-use an executable, I don't remember whether it was compiled with the sanitizers or not. If the executable works fine, I am not sure whether the bug is not there or whether I did not include the sanitizer in this build. So I have to reconfigure and rebuild to make sure I have the sanitizer included.
Is there a way to check whether an executable has been compiled with a sanitizer?
Address sanitizer can also be compiled statically with the -static-libasan
option in GCC. Statically compiling address sanitizer is the default mode in Clang.
If you compile address sanitizer statically then it is obviously not possible to use ldd
to verify if your binary is sanitized or not. In this case I use nm
and check if there are sanitizer symbols in the binary:
nm -an <executable> | grep asan
You need to use __has_feature(address_sanitizer), see http://clang.llvm.org/docs/AddressSanitizer.html (same for other sanitizers).
From man ldd
:
ldd prints the shared libraries required by each program or shared library specified on the command line.
As long as address sanitizer requires to link with libasan.so library (actual implementation of sanitizer) you can assume:
If ldd
won't print shared lib libasan.so
it definitely means that address sanitizer is turned off.
If ldd
will print shared lib libasan.so
it means that your linker flags includes -lasan
, otherwise you`ll get unresolved symbol error during linking. Highly likely that address sanitizer is enabled, unless you have a bug in building system.
Third option if you have bug in your building system. ldd
will print libasan.so
but address sanitizer will be turned off if you passed to linker -lasan
, but didn't pass -fsanitize=address
. It means that you linked your executable with address sanitizer but didn't include checks into your executable.
Or you can do objdump -p to see if libasan.so
is needed in dymanic sections: NEEDED libasan.so.0
. objdump
could give the same(and more) information as ldd
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With