Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check whether sanitizer like AddressSanitizer is active

Tags:

sanitizer

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?

like image 612
usr1234567 Avatar asked Jan 26 '16 10:01

usr1234567


3 Answers

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

like image 86
Perennialista Avatar answered Oct 02 '22 23:10

Perennialista


You need to use __has_feature(address_sanitizer), see http://clang.llvm.org/docs/AddressSanitizer.html (same for other sanitizers).

like image 29
Glider Avatar answered Oct 02 '22 21:10

Glider


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.

like image 35
Jurasic Avatar answered Oct 02 '22 21:10

Jurasic