10

When I compile with -fsanitize=address, GCC/Clang implicitly make use of an ASAN dynamic library which provides runtime support for ASAN. If your built library is dynamically loaded by another application, it is necessary to set LD_PRELOAD to include this dynamic library, so that it gets run at application start up time.

It is often not obvious which copy of libasan.so GCC/Clang expects to use, because there may be multiple copies of ASAN on your system (if you have multiple compilers installed.) Is there a reliable way to determine the location of the shared library you need to load?

Edward Z. Yang
  • 26,325
  • 16
  • 80
  • 110

1 Answers1

12

You can use -print-file-name flag:

GCC_ASAN_PRELOAD=$(gcc -print-file-name=libasan.so)
CLANG_ASAN_PRELOAD=$(clang -print-file-name=libclang_rt.asan-x86_64.so)

You could also extract libasan path from the library itself via ldd:

$ echo 'void foo() {}' | gcc -x c -fPIC -shared -fsanitize=address -
$ ldd a.out | grep libasan.so | awk '{print $3}'
/usr/lib/x86_64-linux-gnu/libasan.so.4
yugr
  • 19,769
  • 3
  • 51
  • 96
  • 1
    `-print-file-name` didn't work for me under CentOS 7 or 8 with a non-default version of `gcc` because the file returned uses the [INPUT syntax](https://unix.stackexchange.com/questions/302402/) to reference another shared library, which apparently LD_PRELOAD doesn't support. The `ldd` script still worked though. Thanks! – joesdiner Apr 05 '22 at 12:52