Clang++ LSAN and UBSAN causes undefined reference to `__ubsan_handle_add_overflow'

989 views Asked by At

When I was playing with compiler sanitizers, I was confused by this linking error when only LSAN and UBSAN are enabled on clang++.

Note that the linking problem disappeared when I removed -fsanitize=leak or added these flags together: -fsanitize=leak -fsanitize-trap=undefined.

Also note that this strange linking error seemed to only occur when using clang++. g++ worked very well on all of the cases shown below.

$ clang++-11 -fsanitize=undefined ../main.cpp && ./a.out
../main.cpp:17:5: runtime error: signed integer overflow: 2147483647 + 1 cannot be represented in type 'int'
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior ../main.cpp:17:5 in

$ clang++-11 -fsanitize=undefined -fsanitize=leak ../main.cpp && ./a.out
/usr/bin/ld: /tmp/main-920ee1.o: in function `main':
main.cpp:(.text+0x6a): undefined reference to `__ubsan_handle_add_overflow'
clang: error: linker command failed with exit code 1 (use -v to see invocation)

$ clang++-11 -fsanitize=undefined -fsanitize=leak -fsanitize-trap=undefined ../main.cpp && ./a.out
Illegal instruction

$ clang++-11 -v
Debian clang version 11.0.0-2
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/10
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/10
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/10
Candidate multilib: .;@m64
Selected multilib: .;@m64

Any clue may help. Thx.

In case you need the source code of main.cpp (which you probably never do):

int main(int argc, char **argv) {
  int k = 0x7fffffff;
  k += argc;
  return 0;
}
0

There are 0 answers