1

This might not be the correct forum to post this, as it looks like an error that I should report to the llvm team perhaps. Please let me know if you agree and I'll do just that.

Here's the code that produces the ubsan error, just in case I'm wrong:

OS = MacOS Ventura
Compiler = Clang 16.0.0 (Manually built LLVM with default options)

Steps to reproduce:

clang++ -fsanitize=undefined main.cpp
./a.out

main.cpp

#include <sstream>

struct MyStruct
{
    static MyStruct create() noexcept
    {
        return MyStruct{};
    }

private:
    std::ostringstream oss_;
};

int main()
{
    MyStruct::create();
}

Output:

~/usr/bin/../include/c++/v1/sstream:730:43: runtime error: member access within address 0x7ff7b265a298 which does not point to an object of type 'std::ostringstream'
0x7ff7b265a298: note: object is of type 'std::__1::basic_ostringstream<char, std::__1::char_traits<char>, std::__1::allocator<char>>'
 f7 7f 00 00  40 11 8b 0d 01 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00
              ^~~~~~~~~~~~~~~~~~~~~~~
              vptr for 'std::__1::basic_ostringstream<char, std::__1::char_traits<char>, std::__1::allocator<char>>'
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior ~/usr/bin/../include/c++/v1/sstream:730:43 in
~/usr/bin/../include/c++/v1/ostream:198:7: runtime error: cast to virtual base of address 0x7ff7b265a298 which does not point to an object of type 'std::ostream'
0x7ff7b265a298: note: object is of type 'std::__1::basic_ostream<char, std::__1::char_traits<char>>'
 f7 7f 00 00  c8 11 8b 0d 01 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00
              ^~~~~~~~~~~~~~~~~~~~~~~
              vptr for 'std::__1::basic_ostream<char, std::__1::char_traits<char>>'
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior ~/usr/bin/../include/c++/v1/ostream:198:7 in
~/usr/bin/../include/c++/v1/ostream:198:13: runtime error: member call on address 0x7ff7b265a308 which does not point to an object of type 'std::ios'
0x7ff7b265a298: note: object is base class subobject at offset 112 within object of type 'std::__1::basic_ostream<char, std::__1::char_traits<char>>'
 f7 7f 00 00  c8 11 8b 0d 01 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00
              ^
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior ~/usr/bin/../include/c++/v1/ostream:198:13 in
~/usr/bin/../include/c++/v1/ios:711:15: runtime error: member call on address 0x7ff7b265a308 which does not point to an object of type 'std::ios_base'
0x7ff7b265a298: note: object is base class subobject at offset 112 within object of type 'std::__1::basic_ostream<char, std::__1::char_traits<char>>'
 f7 7f 00 00  c8 11 8b 0d 01 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00
              ^
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior ~/usr/bin/../include/c++/v1/ios:711:15 in
~/usr/bin/../include/c++/v1/ios:712:5: runtime error: member access within address 0x7ff7b265a308 which does not point to an object of type 'std::ios'
0x7ff7b265a298: note: object is base class subobject at offset 112 within object of type 'std::__1::basic_ostream<char, std::__1::char_traits<char>>'
 f7 7f 00 00  c8 11 8b 0d 01 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00
              ^
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior ~/usr/bin/../include/c++/v1/ios:712:5 in
~/usr/bin/../include/c++/v1/ios:713:5: runtime error: member access within address 0x7ff7b265a308 which does not point to an object of type 'std::ios'
0x7ff7b265a298: note: object is base class subobject at offset 112 within object of type 'std::__1::basic_ostream<char, std::__1::char_traits<char>>'
 f7 7f 00 00  c8 11 8b 0d 01 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00
              ^
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior ~/usr/bin/../include/c++/v1/ios:713:5 in
user5406764
  • 1,627
  • 2
  • 16
  • 23
  • Can't reproduce on Linux with current trunk with either libc++ or libstdc++: https://godbolt.org/z/ac5novM3q. Are you sure everything is built correctly? Are you using the latest trunk? If it is a problem, then looks MacOS specific. – user17732522 Mar 18 '23 at 23:05
  • You probably link to the wrong ubsan/asan lib. Clang might automatically chose the system lib(probably the apple clang version) – Fabian Keßler Mar 18 '23 at 23:30
  • I used otool -l to check the RPATH and otool -L to make sure it's linking against the correct libubsan. It is linking against the correct one (i.e., the one I built). I've built clang many times in the past and never had issues, so I think that's not what it is. – user5406764 Mar 18 '23 at 23:48
  • When it works with a self compiled clang-15.07 version, this is most likely a regression and you should post it at GitHub. But you can post this even without there, since it should work. – Fabian Keßler Mar 19 '23 at 00:00

0 Answers0