ubuntu 上的静态 link pthread 导致未初始化的值跳转 (valgrind)

static link pthread on ubuntu causes uninitialised value jumps (valgrind)

当我使用 gcc 11.1 编译器在 ubuntu 20.04 上静态 linking pthread 库时,在其 运行 期间,我的程序完全按预期工作。然而,当我尝试在调试模式下 运行 它并使用 valgrind 检查任何问题时,我很快发现 linking pthread 库会导致弹出很多警告。为什么会这样,这是我应该担心的事情吗? (std::thread 和 std::jthread 都显示相同的错误)。

如果我不 link 静态库,则没有错误或警告。

main.cpp:

#include <iostream>
#include <thread>

void foo() {
    std::cout << "Hello world" << std::endl;
}

int main() {
    std::jthread t(foo);
    return 0;
}

编译:

g++ main.cpp -std=c++20 -static -pthread -Wl,--whole-archive -lpthread -Wl,--no-whole-archive

我 link 使用 pthread 库的方式 took from here。否则我会遇到段错误。

我遇到的错误示例:

==9674== Syscall param set_robust_list(head) points to uninitialised byte(s)
==9674==    at 0x404E65: __pthread_initialize_minimal (nptl-init.c:272)
==9674==    by 0x4B858E: (below main) (in /home/example)
==9674==  Address 0x4000bf0 is in the brk data segment 0x4000000-0x400123f
==9674== 
==9674== Conditional jump or move depends on uninitialised value(s)
==9674==    at 0x4F5780: __register_atfork (in /home/example)
==9674==    by 0x4F574C: __libc_pthread_init (in /home/example)
==9674==    by 0x405056: __pthread_initialize_minimal (nptl-init.c:368)
==9674==    by 0x4B858E: (below main) (in /home/example)
==9674== 
==9674== Conditional jump or move depends on uninitialised value(s)
==9674==    at 0x4F5804: __register_atfork (in /home/example)
==9674==    by 0x4F574C: __libc_pthread_init (in /home/example)
==9674==    by 0x405056: __pthread_initialize_minimal (nptl-init.c:368)
==9674==    by 0x4B858E: (below main) (in /home/example)
==9674== 
==9674== Conditional jump or move depends on uninitialised value(s)
==9674==    at 0x4FA870: malloc_hook_ini (in /home/example)
==9674==    by 0x5759DA: _dl_get_origin (in /home/example)
==9674==    by 0x5495F4: _dl_non_dynamic_init (in /home/example)
==9674==    by 0x54BF45: __libc_init_first (in /home/example)
==9674==    by 0x4B85C8: (below main) (in /home/example)

关于未初始化的值,它一直在继续。

If i don't link the library statically, there are no errors or warnings.

在 Linux 上静态链接 libpthreadlibc 几乎总是错误的做法。您的可执行文件可能会以许多细微的方式损坏,如果不是现在,那么将来。

就是说,您得到 Valgrind 错误的原因已解释 here