clang: -fsanitize=undefined with 128 integer operations (undefined reference to `__muloti4')
Asked Answered
H

1

6

In clang, I run into linking error if the Undefined Behavior Sanitizer (-fsanitize=undefined) when the program uses 128 bit integer. The linking errors complain about __muloti4:

$ cat example.c
__int128_t a;
int main (void) {
  a = a * a;
  return 0;
}

$ clang -fsanitize=undefined example.c 
/tmp/example-df4873.o: In function `main':
example.c:(.text+0x4c): undefined reference to `__muloti4'
clang: error: linker command failed with exit code 1 (use -v to see invocation)

(Tested on Ubuntu 17.10 with clang 4.0.1.)

With gcc it works out of the box (gcc -fsanitize=undefined example.c).

What does work with clang is the following call, but I neither understand it fully (--rtlib=compiler-rt), nor does it look like to me:

clang -lgcc_s -lubsan --rtlib=compiler-rt -fsanitize=undefined /tmp/example.c

I found it by trial and error, but it feels wrong to use clang and link against some gcc library. Also explicitly linking against ubsan should not be necessary according to the documentation.

Is this the correct way to get rid of the error, or is there a more robust solution?

Henhouse answered 12/4, 2018 at 10:0 Comment(0)
S
5

This is a known problem (also see this). Libgcc (which clang links against by default) does not provide necessary symbols to sanitize 128-bit types so you need to ask clang to use compiler-rt runtime library instead.

Swell answered 12/4, 2018 at 11:32 Comment(2)
When using --rtlib=compiler-rt I now get an error with missing libunwind symbols, is there a fix that works for both these issues?Ictinus
@Ictinus you could try linking both runtimes by specifying both --rtlib and -lgcc -lgcc_eh but this may be unsafe. I'd check with compiler-rt maintainers why you unwind symbols are missing.Swell

© 2022 - 2024 — McMap. All rights reserved.