c++ - 当 filesystem::path 被破坏时程序崩溃

标签 c++ c++17 libstdc++ std-filesystem

以下程序崩溃:

#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;
int main()
{
    fs::path p1 = "/usr/lib/sendmail.cf";
 
    std::cout << "p1 = " << p1 << '\n';
}
汇编:
$ g++ -std=c++17 pathExistsTest.cpp
$ ./a.out 
p1 = "/usr/lib/sendmail.cf"
[1]    35688 segmentation fault (core dumped)  ./a.out

在 Ubuntu 20.04 上测试,编译器为 GCC 8.4.0。
Valgrind,这是切割输出:
==30078==    by 0x4AE5034: QAbstractButton::mouseReleaseEvent(QMouseEvent*) (in /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5.12.8)
==30078==    by 0x4A312B5: QWidget::event(QEvent*) (in /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5.12.8)
==30078==  Address 0x2b is not stack'd, malloc'd or (recently) free'd
==30078== 
==30078== 
==30078== Process terminating with default action of signal 11 (SIGSEGV)
==30078==  Access not within mapped region at address 0x2B
==30078==    at 0x13AD9B: std::vector<std::filesystem::__cxx11::path::_Cmpt, std::allocator<std::filesystem::__cxx11::path::_Cmpt> >::~vector() (in /home/(me)/src/tomato/build-src-Desktop-Release/TomatoLauncher)

Full Output
我什至不知道为什么要调用 vector dtor?我只创建了一个 path变量,否 vector<path> .

最佳答案

TL;博士
您正在使用 GCC 8.4.0 进行编译,因此您需要明确链接 -lstdc++fs .
由于您使用的是 GCC 8.4.0,因此您使用的是 GCC 8.4.0 版的 GNU C++ 标准库,即 libstdc++ header 。但是你的系统(Ubuntu 20.04)只包含 libstdc++.so.6.0.28来自 GCC 9。如果你没有明确链接到 -lstdc++fs ,那么你不小心吃了一个 std::filesystem符号来自 GCC 9(通过 libstdc++.so )而不是来自 GCC 8(通过 libstdc++fs.a )。
GCC 8 和 GCC 9 不兼容 std::filesystem类型。更具体地说,它们的二进制布局是不同的。这基本上是一个非常隐蔽的 ODR 违规。您的对象是为 GCC 8 布局分配的,但使用 GCC 9 布局构造。当您尝试销毁它时,析构函数使用 GCC 8 布局并崩溃,因为数据不是它所期望的。

有两段代码使用了 path 的不同且不兼容的布局。类型。
第一段代码来自 libstdc++.so.6.0.28 : 它包含 path::_M_split_cmpts() 的定义,通过内联构造函数调用 path::path(string_type&&, format) .由于构造函数是内联的,构造函数本身的代码会生成到您的可执行文件中。因此,您的可执行文件包含对 path::_M_split_cmpts 的调用。 .
第二段代码在您自己的可执行文件中:它为内联(默认)析构函数生成指令 path::~path() ,以及它调用的内联函数;一直到std::filesystem::__cxx11::path::path<char [21], std::filesystem::__cxx11::path>(char const (&) [21], std::filesystem::__cxx11::path::path>(char const (&) [21], std::filesystem::__cxx11::path::format) .

我们怎样才能找到这个?
使用调试器:
逐步检查 ctor 中的可疑函数会发现:

0x5569716498ed <std::filesystem::__cxx11::path::path<char [21], std::filesystem::__cxx11::path>(char const (&) [21], std::filesystem::__cxx11::path::path>(char const (&) [21], std::filesystem::__cxx11::path::format)+112>       callq  0x5569716491e0 <_ZNSt10filesystem7__cxx114path14_M_split_cmptsEv@plt>
这是通过 PLT 的调用(因此,可能来自共享对象,并且绝对不是内联的)。我们进入它并:
(gdb) bt
#0  0x00007f102c60f260 in std::filesystem::__cxx11::path::_M_split_cmpts() () from /lib/x86_64-linux-gnu/libstdc++.so.6
#1  0x00005569716498ed in std::filesystem::__cxx11::path::path<char [21], std::filesystem::__cxx11::path> (this=0x7ffe1a07ad60, __source=...)
    at /usr/include/c++/8/bits/fs_path.h:185
#2  0x00005569716493fd in main () at blub.cpp:6
所以,我们可以看到它确实来自/lib/x86_64-linux-gnu/libstdc++.so.6 ,这是到 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.28 的符号链接(symbolic link).
我们可以看到的 dtor,例如在 OP 的 Valgrind 输出中:
==30078== Invalid read of size 8
==30078==    at 0x13AD9B: std::vector<std::filesystem::__cxx11::path::_Cmpt, std::allocator<std::filesystem::__cxx11::path::_Cmpt> >::~vector() (in /home/(me)/src/tomato/build-src-Desktop-Release/TomatoLauncher)
它是内联的,因此在可执行文件中。

现在,真正有趣的部分是包含 path 的内联函数的头文件和 path::_M_split_cmpts函数来自 GNU C++ 标准库 (libstdc++)。
他们怎么可能不相容?
为了回答这个问题,让我们来看看确切的版本。我们正在使用 GCC 8.4.0 进行编译。它包含在包含路径中,它们指的是 Ubuntu 20.04 的 gcc-8 包中提供的标准库头文件。它们完美匹配,您必须更改默认设置以使 GCC 使用不同的、不匹配的标准库头文件。因此 header 是 GCC 8.4.0 的 header 。
共享对象呢libstdc++.so ?我们正在运行 libstdc++.so.6.0.28根据 ldd和调试器。根据 libstdc++ ABI Policy and Guidelines , 那是 海湾合作委员会 >= 9.3 .
libstdc++.so.6.0.28 确实包含 _ZNSt10filesystem7__cxx114path14_M_split_cmptsEv 的定义:
$ objdump -T /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.28 | grep _ZNSt10filesystem7__cxx114path14_M_split_cmptsEv
000000000016a260 g    DF .text  00000000000005f3  GLIBCXX_3.4.26 _ZNSt10filesystem7__cxx114path14_M_split_cmptsEv
根据 ABI 文档,这是

GCC 9.1.0: GLIBCXX_3.4.26, CXXABI_1.3.12


所以这是一个在 GCC 8.4.0 中不可用的符号。

为什么编译器/链接器不提示?
当我们使用 gcc-8 进行编译时,为什么编译器或链接器不会提示我们使用了 GCC 9 中的符号?
如果我们用 -v 编译,我们看到链接器调用:
COLLECT_GCC_OPTIONS='-v' '-std=c++17' '-g' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
/usr/lib/gcc/x86_64-linux-gnu/8/collect2 -plugin /usr/lib/gcc/x86_64-linux-gnu/8/liblto_plugin.so -plugin-opt=/usr/lib/gcc/x86_64-linux-gnu/8/lto-wrapper -plugin-opt=-fresolution=/tmp/cceJgWPt.res -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lc -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lgcc --build-id --eh-frame-hdr -m elf_x86_64 --hash-style=gnu --as-needed -dynamic-linker /lib64/ld-linux-x86-64.so.2 -pie -z now -z relro /usr/lib/gcc/x86_64-linux-gnu/8/../../../x86_64-linux-gnu/Scrt1.o /usr/lib/gcc/x86_64-linux-gnu/8/../../../x86_64-linux-gnu/crti.o /usr/lib/gcc/x86_64-linux-gnu/8/crtbeginS.o -L/usr/lib/gcc/x86_64-linux-gnu/8 -L/usr/lib/gcc/x86_64-linux-gnu/8/../../../x86_64-linux-gnu -L/usr/lib/gcc/x86_64-linux-gnu/8/../../../../lib -L/lib/x86_64-linux-gnu -L/lib/../lib -L/usr/lib/x86_64-linux-gnu -L/usr/lib/../lib -L/usr/lib/gcc/x86_64-linux-gnu/8/../../.. /tmp/ccTNph3u.o -lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc /usr/lib/gcc/x86_64-linux-gnu/8/crtendS.o /usr/lib/gcc/x86_64-linux-gnu/8/../../../x86_64-linux-gnu/crtn.o                           COLLECT_GCC_OPTIONS='-v' '-std=c++17' '-g' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
在那里,我们有 -L/usr/lib/gcc/x86_64-linux-gnu/8和其他查找标准库的路径。在那里,我们找到 libstdc++.so -> ../../../x86_64-linux-gnu/libstdc++.so.6 ,最后指向 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.28 (!!!)。
所以链接器被赋予 GCC 9 的 libstdc++.so , 并且它不会从编译器 (*) 接收有关符号的任何版本信息。编译器只知道源代码,在这种情况下,源代码不包含符号版本(GCC 8.4.0 的文件系统头文件)。然而,符号版本存在于 ELF 二进制文件中 libstdc++.so .链接器看到 GLIBCXX_3.4.26对于编译器请求的符号 _ZNSt10filesystem7__cxx114path14_M_split_cmptsEv并对此感到满意。让你想知道是否有一个链接器开关来告诉链接器“如果我请求一个未版本化的符号,不要使用一个版本化的符号”。
(*) 链接器不会从编译器接收有关该未解析符号的任何符号信息,因为编译器没有来自源代码的此类信息。您can add info to your source code .我不知道 libstdc++ 通常是怎么做的——或者它对头文件中符号版本的策略。看来filesystem根本没有做.
ELF 符号版本控制机制通常应该防止此类不兼容:如果存在布局不兼容的更改,则创建一个名称相同但版本不同的新符号,并将其添加到 libstdc++.so。 ,然后包含旧版本和新版本。
针对 libstdc++.so 编译的二进制文件指定它想要的符号版本,动态加载器根据匹配名称和版本的符号正确解析 undefined symbol 。请注意,动态链接器不知道要搜索哪个共享库(在 Windows/PE 上,这是不同的)。任何“符号请求”都只是一个 undefined symbol ,并且有一个完全独立的所需库列表来提供那些 undefined symbol 。但是二进制文件中没有映射哪个符号应该来自哪个库。
因为 ELF 符号版本控制机制允许向后兼容添加符号,所以我们可以维护一个 libstdc++.so对于多个版本的编译器。这就是为什么你会看到到处都是符号链接(symbolic link),导致所有文件都指向同一个文件。后缀.6.0.28是另一种允许向后的正交版本控制方案- 不兼容 更改:您的二进制文件可以指定它需要 libstdc++.so.6你可以添加一个不兼容的 libstdc++.so.7对于其他二进制文件。
有趣的事实:如果您将库与 libstdc++.so 的纯 GCC 8 版本链接起来,你会看到 a linker error .链接共享库对二进制文件没有太大作用;然而,它确实修复了未解析符号的符号版本,并且可以在查看所有库后检查是否没有留下未解析的符号。我们可以看到你的二进制文件实际上请求了 _ZNSt10filesystem7__cxx114path14_M_split_cmptsEv@GLIBCXX_3.4.26当您将其链接到 libstdc++.so.6.0.28 时.
有趣的事实 2:如果您针对 libstdc++.so 的纯 GCC 8 版本运行您的库,您会收到动态链接器错误,因为它找不到 _ZNSt10filesystem7__cxx114path14_M_split_cmptsEv@GLIBCXX_3.4.26 .

实际上应该发生什么?
您实际上应该链接到 libstdc++fs.a .它还提供了 _ZNSt10filesystem7__cxx114path14_M_split_cmptsEv 的定义,它不是符号链接(symbolic link),而是特定于此 GCC 版本:/usr/lib/gcc/x86_64-linux-gnu/8/libstdc++fs.a .
当您链接到 -lstdc++fs ,您可以将其符号直接包含在可执行文件中(因为它是一个静态库)。可执行文件中的符号优先于共享对象中的符号。因此,_ZNSt10filesystem7__cxx114path14_M_split_cmptsEv来自 libstdc++fs.a用来。

path中的布局不兼容实际上是什么? ?
GCC 9 引入了一种不同的类型来保存路径的组件。使用 clang++ -cc1 -fdump-record-layouts ,我们可以看到左侧的偏移量,右侧的成员和类型名称:
海湾合作委员会 8.4.0:
 0 | class std::filesystem::__cxx11::path
 0 |   class std::__cxx11::basic_string<char, struct std::char_traits<char>, class std::allocator<char> > _M_pathname
 0 |     struct std::__cxx11::basic_string<char, struct std::char_traits<char>, class std::allocator<char> >::_Alloc_hider _M_dataplus
 0 |       class std::allocator<char> (base) (empty)
 0 |         class __gnu_cxx::new_allocator<char> (base) (empty)
 0 |       std::__cxx11::basic_string<char, struct std::char_traits<char>, class std::allocator<char> >::pointer _M_p
 8 |     std::__cxx11::basic_string<char, struct std::char_traits<char>, class std::allocator<char> >::size_type _M_string_length
16 |     union std::__cxx11::basic_string<char, struct std::char_traits<char>, class std::allocator<char> >::(anonymous at /usr/include/c++/8/bits/basic_string.h:160:7) 
16 |       char [16] _M_local_buf
16 |       std::__cxx11::basic_string<char, struct std::char_traits<char>, class std::allocator<char> >::size_type _M_allocated_capacity
32 |   class std::vector<struct std::filesystem::__cxx11::path::_Cmpt, class std::allocator<struct std::filesystem::__cxx11::path::_Cmpt> > _M_cmpts
32 |     struct std::_Vector_base<struct std::filesystem::__cxx11::path::_Cmpt, class std::allocator<struct std::filesystem::__cxx11::path::_Cmpt> > (base)
32 |       struct std::_Vector_base<struct std::filesystem::__cxx11::path::_Cmpt, class std::allocator<struct std::filesystem::__cxx11::path::_Cmpt> >::_Vector_impl _M_impl
32 |         class std::allocator<struct std::filesystem::__cxx11::path::_Cmpt> (base) (empty)
32 |           class __gnu_cxx::new_allocator<struct std::filesystem::__cxx11::path::_Cmpt> (base) (empty)
32 |         std::_Vector_base<struct std::filesystem::__cxx11::path::_Cmpt, class std::allocator<struct std::filesystem::__cxx11::path::_Cmpt> >::pointer _M_start
40 |         std::_Vector_base<struct std::filesystem::__cxx11::path::_Cmpt, class std::allocator<struct std::filesystem::__cxx11::path::_Cmpt> >::pointer _M_finish
48 |         std::_Vector_base<struct std::filesystem::__cxx11::path::_Cmpt, class std::allocator<struct std::filesystem::__cxx11::path::_Cmpt> >::pointer _M_end_of_storage
56 |   enum std::filesystem::__cxx11::path::_Type _M_type
   | [sizeof=64, dsize=57, align=8,
   |  nvsize=57, nvalign=8]
海湾合作委员会 9.3.0:
 0 | class std::filesystem::__cxx11::path
 0 |   class std::__cxx11::basic_string<char> _M_pathname
 0 |     struct std::__cxx11::basic_string<char, struct std::char_traits<char>, class std::allocator<char> >::_Alloc_hider _M_dataplus
 0 |       class std::allocator<char> (base) (empty)
 0 |         class __gnu_cxx::new_allocator<char> (base) (empty)
 0 |       std::__cxx11::basic_string<char, struct std::char_traits<char>, class std::allocator<char> >::pointer _M_p
 8 |     std::__cxx11::basic_string<char, struct std::char_traits<char>, class std::allocator<char> >::size_type _M_string_length
16 |     union std::__cxx11::basic_string<char, struct std::char_traits<char>, class std::allocator<char> >::(anonymous at /usr/include/c++/9/bits/basic_string.h:171:7) 
16 |       char [16] _M_local_buf
16 |       std::__cxx11::basic_string<char, struct std::char_traits<char>, class std::allocator<char> >::size_type _M_allocated_capacity
32 |   struct std::filesystem::__cxx11::path::_List _M_cmpts
32 |     class std::unique_ptr<struct std::filesystem::__cxx11::path::_List::_Impl, struct std::filesystem::__cxx11::path::_List::_Impl_deleter> _M_impl
32 |       class std::__uniq_ptr_impl<struct std::filesystem::__cxx11::path::_List::_Impl, struct std::filesystem::__cxx11::path::_List::_Impl_deleter> _M_t
32 |         class std::tuple<struct std::filesystem::__cxx11::path::_List::_Impl *, struct std::filesystem::__cxx11::path::_List::_Impl_deleter> _M_t
32 |           struct std::_Tuple_impl<0, struct std::filesystem::__cxx11::path::_List::_Impl *, struct std::filesystem::__cxx11::path::_List::_Impl_deleter> (base)
32 |             struct std::_Tuple_impl<1, struct std::filesystem::__cxx11::path::_List::_Impl_deleter> (base) (empty)
32 |               struct std::_Head_base<1, struct std::filesystem::__cxx11::path::_List::_Impl_deleter, true> (base) (empty)
32 |                 struct std::filesystem::__cxx11::path::_List::_Impl_deleter (base) (empty)
32 |             struct std::_Head_base<0, struct std::filesystem::__cxx11::path::_List::_Impl *, false> (base)
32 |               struct std::filesystem::__cxx11::path::_List::_Impl * _M_head_impl
   | [sizeof=40, dsize=40, align=8,
   |  nvsize=40, nvalign=8]
区别在于path::_M_cmpts :
// GCC 8
class std::vector<
  struct std::filesystem::__cxx11::path::_Cmpt,
  class std::allocator<struct std::filesystem::__cxx11::path::_Cmpt>
> _M_cmpts

// GCC 9
struct std::filesystem::__cxx11::path::_List _M_cmpts
你也可以看到path::_List的结构在上面的记录转储中。它与 GCC 8 非常不兼容 vector .
请记住,我们正在调用 path::_M_split_cmpts通过来自 GCC 9 的 libstdc++.so,我们在 vector 中崩溃了这个的析构函数_M_cmpts数据成员。
这是从 vector 更改的提交至 _List :
commit 4f87bb8d6e8dec21a07f1fba641a78a127281349
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Thu Dec 13 20:33:55 2018 +0000

PR libstdc++/71044 optimize std::filesystem::path construction

This new implementation has a smaller footprint than the previous
implementation, due to replacing std::vector<_Cmpt> with a custom pimpl
type that only needs a single pointer. The _M_type enumeration is also
combined with the pimpl type, by using a tagged pointer, reducing
sizeof(path) further still.

Construction and modification of paths is now done more efficiently, by
splitting the input into a stack-based buffer of string_view objects
instead of a dynamically-allocated vector containing strings. Once the
final size is known only a single allocation is needed to reserve space
for it.  The append and concat operations no longer require constructing
temporary path objects, nor re-parsing the entire native pathname.
This results in algorithmic improvements to path construction, and
working with large paths is much faster.

关于c++ - 当 filesystem::path 被破坏时程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63902528/

相关文章:

c++ - gcc 4.8 或更早版本是否存在关于正则表达式的问题?

c++ boost进程间交换(复制)非共享和共享字符串 vector

c++ - 为什么要使用 std::forward?

c++ - emplace_back 导致静态 constexpr 成员上的链接错误

c++ - 自定义元素的迭代器在 vector 中

c++ - 为什么内存分配器不主动将释放的内存返回给操作系统?

Java 等同于 C++ 复制赋值运算符

C++模板成员函数错误

c++ - 是否可以从std::any使用std::reference_wrapper创建std::any?

c++ - g++ libstdc++.so.6 : version `CXXABI_1.3.9' not found after upgrade to gcc version 7. 3.0 从 4.8.5