移动 std::ostringstream 时出现奇怪的 clang c++ ubsan 错误

Strange clang c++ ubsan error when moving std::ostringstream

提问人:user5406764 提问时间:3/19/2023 更新时间:3/19/2023 访问量:74

问:

这可能不是发布此内容的正确论坛,因为它看起来像一个错误,我可能应该向 llvm 团队报告。如果您同意,请告诉我,我会这样做。

这是产生 ubsan 错误的代码,以防万一我错了:

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

重现步骤:

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();
}

输出:

~/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
C clang llvm clang++ ubsan

评论

0赞 user17732522 3/19/2023
无法在具有当前主干的 Linux 上使用 libc++ 或 libstdc++: godbolt.org/z/ac5novM3q 重现。您确定一切都正确构建了吗?您使用的是最新的后备箱吗?如果这是一个问题,那么看起来是特定于MacOS的。
0赞 Fabian Keßler 3/19/2023
您可能链接到错误的 ubsan/asan 库。Clang 可能会自动选择系统库(可能是 apple clang 版本)
0赞 user5406764 3/19/2023
我使用 otool -l 检查 RPATH,并使用 otool -L 来确保它与正确的 libubsan 链接。它链接到正确的(即我构建的那个)。我过去曾多次构建 clang 并且从未遇到过问题,所以我认为事实并非如此。
0赞 Fabian Keßler 3/19/2023
当它与自编译的 clang-15.07 版本一起使用时,这很可能是回归,您应该将其发布在 GitHub 上。但是即使没有那里,您也可以发布它,因为它应该可以工作。

答: 暂无答案