Link error compiling with -faddress-sanitizer (___asan_init undefined)

Hi all,

I'm trying to exactly follow the instructions here:
<http://clang.llvm.org/docs/AddressSanitizer.html>

Using the same test file and the same invocation:

clang -O1 -g -faddress-sanitizer -fno-omit-frame-pointer example_UseAfterFree.cc

But I get:

Undefined symbols:
  "___asan_report_load4", referenced from:
      _main in test-w3FGxl.o
  "___asan_init", referenced from:
      _asan.module_ctor in test-w3FGxl.o

This is on OS X 10.6.8 with clang r152633. I also tried with -arch i386 and -arch x86_64, no difference. Is there some lib I'm supposed to link with... the docs don't seem to say so.

Thanks,

Sean,

Could you please run the same command line with “-v” to see what libraries get linked in?

–kcc

Kostya,

Thanks for the fast reply. Here is the whole thing:

builder9:~ builder$ clang -O1 -g -faddress-sanitizer -fno-omit-frame-pointer ~/Desktop/test.c -v
clang version 3.1 (trunk 152633)
Target: x86_64-apple-darwin10.8.0
Thread model: posix
"/Users/builder/llvm/llvm-rel-bin-install/bin/clang-3.1" -cc1 -triple x86_64-apple-macosx10.6.0 -emit-obj -disable-free -disable-llvm-verifier -main-file-name test.c -pic-level 1 -mdisable-fp-elim -masm-verbose -munwind-tables -target-cpu core2 -v -g -resource-dir /Users/builder/llvm/llvm-rel-bin-install/bin/../lib/clang/3.1 -fmodule-cache-path /var/folders/So/SoDaKVGNF1GdaUhl-VCLoE+++TI/-Tmp-/clang-module-cache -O1 -fdebug-compilation-dir /Users/builder -ferror-limit 19 -fmessage-length 179 -faddress-sanitizer -stack-protector 1 -mstackrealign -fblocks -fobjc-dispatch-method=mixed -fobjc-default-synthesize-properties -fdiagnostics-show-option -fcolor-diagnostics -o /var/folders/So/SoDaKVGNF1GdaUhl-VCLoE+++TI/-Tmp-/test-cnlcGw.o -x c /Users/builder/Desktop/test.c
clang -cc1 version 3.1 based upon LLVM 3.1svn default target x86_64-apple-darwin10.8.0
#include "..." search starts here:
#include <...> search starts here:
/usr/local/include
/Users/builder/llvm/llvm-rel-bin-install/bin/../lib/clang/3.1/include
/usr/include
/System/Library/Frameworks (framework directory)
/Library/Frameworks (framework directory)
End of search list.
"/usr/llvm-gcc-4.2/libexec/gcc/i686-apple-darwin10/4.2.1/ld" -dynamic -arch x86_64 -macosx_version_min 10.6.0 -o a.out -lcrt1.10.6.o /var/folders/So/SoDaKVGNF1GdaUhl-VCLoE+++TI/-Tmp-/test-cnlcGw.o /usr/lib/libstdc++.6.dylib -framework CoreFoundation -lSystem
Undefined symbols:
  "___asan_report_load4", referenced from:
      _main in test-cnlcGw.o
  "___asan_init", referenced from:
      _asan.module_ctor in test-cnlcGw.o
ld: symbol(s) not found
clang-3: error: linker command failed with exit code 1 (use -v to see invocation)

For some reason the asan run-time library does not get added to the link command.
It worked for me yesterday, but today my only Mac box is down, so I can’t check immediately.

The code that should add the run-time library is in tools/clang/lib/Driver/ToolChains.cpp (search for address_sanitizer),
I’ll try to check it later today once my Mac box is up.

Does your clang build contain library called “libclang_rt.asan_osx.a”? You can probably try adding it manually.

–kcc

Sean, can you try to compile and link your program in two separate steps, i.e.:

clang -faddress-sanitizer test.c -c -v
clang -faddress-sanitizer test.o -v

Did you check out and build compiler-rt as described on
http://clang.llvm.org/get_started.html ? (It's a recent addition, so
if you skipped that page because you already had a clang checkout, you
might have missed it.)

Nico

Nico,

Exactly right. If I follow those instructions and build with configure/make then I get a clang that accepts -faddress-sanitizer. Thanks.

But if I build with CMake, which is what I usually do, it still doesn't seem to work. Should it? Is there a special option to turn on? LLVM_BUILD_RUNTIME and LLVM_INCLUDE_RUNTIME are on by default, if they're related.

Cheers,

Did you check out and build compiler-rt as described on
http://clang.llvm.org/get_started.html ? (It’s a recent addition, so
if you skipped that page because you already had a clang checkout, you
might have missed it.)

Nico,

Exactly right. If I follow those instructions and build with configure/make then I get a clang that accepts -faddress-sanitizer. Thanks.

But if I build with CMake, which is what I usually do, it still doesn’t seem to work. Should it? Is there a special option to turn on? LLVM_BUILD_RUNTIME and LLVM_INCLUDE_RUNTIME are on by default, if they’re related.

Ah, CMake… afaict, compiler-rt (at least, asan) does not build with cmake :frowning:

–kcc

FWIW, this is on my queue in the near term.

I don't see a bug about that, so created one here:
<http://llvm.org/bugs/show_bug.cgi?id=12272>

Also, the web page says that support for 10.7 is "in progress". Is that out of date, or should I really only be playing with this only on 10.6?

Cheers,

Our tests should work on 10.7, although we haven't tested ASan on
larger applications on Lion.
Most probably you won't have any problems, but if you do, feel free to
email address-sanitizer@googlegroups.com

Our tests should work on 10.7, although we haven’t tested ASan on
larger applications on Lion.

The main problem with 10.7 is that we don’t test it regularly on 10.7.
Once we get a 10.7 machine for regular testing (soon-ish) we will remove the “in progress” bit.

–kcc