静态链接pthread在ubuntu导致未初始化的值跳转(valgrind)



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

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

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

我链接pthread库的方式是从这里借鉴的。否则我将得到一个段错误。

我得到的错误样本:

==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)

,它还没完没了地说未初始化的值

如果我不静态链接库,则不会出现错误或警告。

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

也就是说,这里解释了您得到Valgrind错误的原因。

相关内容

  • 没有找到相关文章

最新更新