17

When linking my code compiled with the AddressSanitizer tool, I'm getting many errors of the kind undefined reference to '_asan_init_v4'.

clang -fPIC -g -fno-omit-frame-pointer -DNDEBUG -Wl,-z,defs \
  -shared -Wl,-soname,libqpid-dispatch.so -o libqpid-dispatch.so \
  CMakeFiles/qpid-dispatch.dir/alloc_pool.c.o \
  CMakeFiles/qpid-dispatch.dir/amqp.c.o \
  [...]
  -lpthread -lrt -ldl -lpython3.7m -lwebsockets -fsanitize=address

Some examples of the errors

/nix/store/1zf4cnaaidjajwb4gx4mnkqc5dypkcdy-binutils-2.31.1/bin/ld: CMakeFiles/qpid-dispatch.dir/http-libwebsockets.c.o: in function `qd_http_server_free':
/home/jdanek/repos/qpid/qpid-dispatch/src/http-libwebsockets.c:824: undefined reference to `__ubsan_handle_type_mismatch_v1'
/nix/store/1zf4cnaaidjajwb4gx4mnkqc5dypkcdy-binutils-2.31.1/bin/ld: /home/jdanek/repos/qpid/qpid-dispatch/src/http-libwebsockets.c:825: undefined reference to `__ubsan_handle_type_mismatch_v1'
/nix/store/1zf4cnaaidjajwb4gx4mnkqc5dypkcdy-binutils-2.31.1/bin/ld: /home/jdanek/repos/qpid/qpid-dispatch/src/http-libwebsockets.c:825: undefined reference to `__ubsan_handle_type_mismatch_v1'
/nix/store/1zf4cnaaidjajwb4gx4mnkqc5dypkcdy-binutils-2.31.1/bin/ld: /home/jdanek/repos/qpid/qpid-dispatch/src/http-libwebsockets.c:825: undefined reference to `__ubsan_handle_type_mismatch_v1'
/nix/store/1zf4cnaaidjajwb4gx4mnkqc5dypkcdy-binutils-2.31.1/bin/ld: /home/jdanek/repos/qpid/qpid-dispatch/src/http-libwebsockets.c:825: undefined reference to `__ubsan_handle_type_mismatch_v1'
/nix/store/1zf4cnaaidjajwb4gx4mnkqc5dypkcdy-binutils-2.31.1/bin/ld: /home/jdanek/repos/qpid/qpid-dispatch/src/http-libwebsockets.c:825: undefined reference to `__asan_report_load8'
/nix/store/1zf4cnaaidjajwb4gx4mnkqc5dypkcdy-binutils-2.31.1/bin/ld: /home/jdanek/repos/qpid/qpid-dispatch/src/http-libwebsockets.c:825: undefined reference to `__asan_report_load8'

Note: I am using NixOS 19.09 and Clang 10.

user7610
  • 25,267
  • 15
  • 124
  • 150
vamshi
  • 183
  • 1
  • 1
  • 8
  • Can you provide more details about how you compile your code (e.g. compile and link line). – yugr Feb 27 '17 at 14:35
  • Compile falgs: -Wall ,-g ,Wextra, -Wno-missing-field-initializers,-Wformat=2,-Wpointer-arith,-fno-omit-frame-pointer,-m64,-D_LARGEFILE_SOURCE,-D_FILE_OFFSET_BITS=64,-fsanitize=address,-fno-stack-protector,-fno-omit-frame-pointer,-fsanitize-recover=address,-fsanitize=undefined,-D_GLIBCXX_USE_CXX11_ABI=0,-fgnu89-inline – vamshi Feb 28 '17 at 08:48
  • link flags:-m64,-rdynamic,-fsanitize=address,-fsanitize-recover=address,-fsanitize=undefined,-D_GLIBCXX_USE_CXX11_ABI=0 – vamshi Feb 28 '17 at 08:49
  • Hm, the only idea coming to my mind is that GCC and libasan on your machine are out of synch. Are you using custom GCC? Could you run `readelf -sW FILENAME | grep asan_init` with `FILENAME` set to one of your object files compiled with `-fsanitize=address` and to `path/to/libasan.so`? – yugr Feb 28 '17 at 11:50
  • 1
    Possible duplicate of [How to use AddressSanitizer in gcc?](https://stackoverflow.com/questions/37970758/how-to-use-addresssanitizer-in-gcc) – yugr Jun 15 '19 at 15:56
  • 2
    This could be manifestation of https://github.com/google/sanitizers/issues/380. Try removing `-Wl,-z,defs` from your linker command line. – user7610 Jul 19 '20 at 10:50

4 Answers4

10

You should be using the compile flag -fsanitize=address: https://github.com/google/sanitizers/wiki/AddressSanitizer

Note that -fsanitize=address = -lasan + some add'l options. And using -lasan has been discouraged by ASan developers.

BoltzmannBrain
  • 5,082
  • 11
  • 46
  • 79
  • 5
    `-fsanitize=address` isn't enough with GCC. – jhasse Dec 13 '18 at 14:27
  • 7
    @jhasse: Do you have a prooflink for this? Note that you need `-fsanitize=address` both in `CFLAGS`/`CXXFLAGS` and in `LDFLAGS` as described [here](https://stackoverflow.com/a/40215639/2170527). – yugr Jun 10 '19 at 09:06
  • 1
    actually in my case, adding `-fsanitize=address` makes the whole thing break but removing it makes it work. – Juan Carlos Ortiz Nov 18 '21 at 05:14
2

(I see @user7610 basically said this in their comment)

You should not use -z defs, since it is incompatible with asan.

Source: https://clang.llvm.org/docs/AddressSanitizer.html

"When linking shared libraries, the AddressSanitizer run-time is not linked, so -Wl,-z,defs may cause link errors (don’t use it with AddressSanitizer)."

domen
  • 1,819
  • 12
  • 19
-1

I'm not sure that this was the problem, but for me, adding -lasan to the linker options was enough to get my program to build and run.

Vincent Fourmond
  • 3,038
  • 1
  • 22
  • 24
  • 8
    This answer is wrong, adding `-lasan` has been discouraged by Asan developers many times e.g. in [this thread](https://groups.google.com/d/msg/address-sanitizer/SD590XDinfQ/NMUPj_G0BgAJ). The only supported approach is to use `-fsanitize=address` both in `CFLAGS`/`CXXFLAGS` and `LDFLAGS` (see [this answer](https://stackoverflow.com/a/40215639/2170527)). – yugr Jun 10 '19 at 09:11
-1

Adding -static-libasan to linker flags will solves this problem.

rashok
  • 12,790
  • 16
  • 88
  • 100
  • 1
    `-static-libasan` is not recommended on GCC though (not very well supported see e.g. [PR 64234](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64234)). – yugr Jun 10 '19 at 09:08