Interesting behaviour with UBSAN+libc++

I got some very simple code, dumbed down from a larger one:

[~]> cat wc.cpp
#include

int wc() {
return 0;
}

int main()
{
std::cout << wc() << std::endl;
}

[~]> /opt/clang/bin/clang++ -stdlib=libc++ -fsanitize=undefined wc.cpp

[~]> ./a.out
/opt/clang/bin/…/include/c++/v1/ostream:1034:14: runtime error: member call on address 0x000000ef9858 which does not point to an object of type ‘std::__1::basic_ios’
0x000000ef9850: note: object is base class subobject at offset 8 within object of type ‘std::__1::basic_ostream<char, std::__1::char_traits >’
00 00 00 00 40 a3 b9 c6 b1 7f 00 00 68 a3 b9 c6 b1 7f 00 00 02 10 00 00 00 00 00 00 06 00 00 00
^ ~~~~~~~~~~~~~~~~~~~~~~~
vptr for ‘’ base class of ‘std::__1::basic_ostream<char, std::__1::char_traits >’
/opt/clang/bin/…/include/c++/v1/ios:734:41: runtime error: member call on address 0x000000ef9858 which does not point to an object of type ‘std::__1::ios_base’
0x000000ef9850: note: object is base class subobject at offset 8 within object of type ‘std::__1::basic_ostream<char, std::__1::char_traits >’
00 00 00 00 40 a3 b9 c6 b1 7f 00 00 68 a3 b9 c6 b1 7f 00 00 02 10 00 00 00 00 00 00 06 00 00 00
^ ~~~~~~~~~~~~~~~~~~~~~~~
vptr for ‘’ base class of ‘std::__1::basic_ostream<char, std::__1::char_traits >’
0

Works fine if I link to libstdc++. clang is r223341.

Did you compile libc++ with UBSAN?

/Eric

LLVM and libc++ always seem to use '-fno-sanitize=vptr,function' and
'-fno-sanitize-recover' with UBSAN. I imagine adding these will remove
the error.
I would ask the compiler-rt people about the error on llvm-dev.

/Eric

The UBSan runtime’s support for type checks of virtual bases is new and possibly incorrect.

...which reminds me of <http://lists.cs.uiuc.edu/pipermail/llvm-commits/Week-of-Mon-20141201/247604.html&gt; "[PATCH] [comiler-rt/ubsan] getVtablePrefix must not sanity-check on Prefix->Offset > 0"

Ok so just to confirm to use ubsan against c++ programs I do not
necessarily recompile libc++ with ubsan support right?

Regards.

I successfully use UBSan on LibreOffice (lots of C++) against a non-instrumented libstdc++ (on Linux) at least.

(It is MemorySanitizer that needs /all/ code to be instrumented, I think.)

Same for TSAN I guess. Thanks for the confirmation!

Correct.

— Marshall