Hacker News new | ask | show | jobs
by kbeckmann 3218 days ago
With clang you can enable run time UB checks using the -fsanitize=undefined flag[0]. But in this case, the sanitizer doesn't detect undefined behavior.

I tried to build the example (and changing the system to an echo) using "clang a.cpp -o a -Os -std=c++11 -lstdc++ -fsanitize=undefined", but the sanitizer didn't detect the UB.

[0] https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html

2 comments

I can't build it with fsanitize=undefined at all with clang 3.4:

    > clang -Os -std=c++11 -Wall -o boom -fsanitize=undefined boom.cpp 
    /tmp/boom-428713.o:(.rodata._ZTIFvvE[_ZTIFvvE]+0x0): undefined reference to `vtable for __cxxabiv1::__function_type_info'
    /tmp/boom-428713.o:(.rodata._ZTIFivE[_ZTIFivE]+0x0): undefined reference to `vtable for __cxxabiv1::__function_type_info'
    clang: error: linker command failed with exit code 1 (use -v to see invocation)
> but the sanitizer didn't detect the UB.

Isn't this because the sanitizer works with the instrumented "optimized" code, which does no longer invoke UB? Or is the clang toolchain intelligent enough to not perform UB dependent optimizations when the sanitizer is specified?

What would it even mean for compiled, machine language code to invoke UB or not?

UB is mostly a C concept, not a lower-level one. The semantics of assembly language programs are well-defined much more often (the only counterexample I can think of is stuff involving using threading, memory barriers, etc. incorrect)