https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97485
Bug ID: 97485
Summary: std::call_once crashes at runtime on glibc if not
linked to libpthread:
Product: gcc
Version: 10.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: libstdc++
Assignee: unassigned at gcc dot gnu.org
Reporter: slyfox at gcc dot gnu.org
Target Milestone: ---
Initially bug was reported as a crash of llvm-tblgen tool:
https://bugs.gentoo.org/749162.
Small reproducer:
$ cat a.cc
#include <mutex>
static std::once_flag of;
static std::recursive_mutex * pm = nullptr;
int main() {
std::call_once(of, []() { pm = new std::recursive_mutex; });
}
$ g++-10.2.0 a.cc -o a -ggdb3 && ./a
terminate called after throwing an instance of 'std::system_error'
what(): Unknown error -1
Aborted (core dumped)
$ g++-10.2.0 a.cc -o a -pthread && ./a
All gcc versions are affected (at least 6.5.0 and above).
Backtrace:
"""
Reading symbols from ./a...
(gdb) run
Starting program: /tmp/a
terminate called after throwing an instance of 'std::system_error'
what(): Unknown error -1
Program received signal SIGABRT, Aborted.
__GI_raise (sig=<optimized out>) at ../sysdeps/unix/sysv/linux/raise.c:49
49 return ret;
(gdb) bt
#0 __GI_raise (sig=<optimized out>) at ../sysdeps/unix/sysv/linux/raise.c:49
#1 0x00007ffff7a1c536 in __GI_abort () at abort.c:79
#2 0x00007ffff7dad961 in __gnu_cxx::__verbose_terminate_handler ()
at
/usr/src/debug/sys-devel/gcc-11.0.0_pre9999/gcc-11.0.0_pre9999/libstdc++-v3/libsupc++/vterminate.cc:95
#3 0x00007ffff7ddcd9a in __cxxabiv1::__terminate (handler=<optimized out>)
at
/usr/src/debug/sys-devel/gcc-11.0.0_pre9999/gcc-11.0.0_pre9999/libstdc++-v3/libsupc++/eh_terminate.cc:48
#4 0x00007ffff7ddce05 in std::terminate () at
/usr/src/debug/sys-devel/gcc-11.0.0_pre9999/gcc-11.0.0_pre9999/libstdc++-v3/libsupc++/eh_terminate.cc:58
#5 0x00007ffff7ddd098 in __cxxabiv1::__cxa_throw (obj=<optimized out>,
tinfo=0x7ffff7f6f948 <typeinfo for std::system_error>,
dest=0x7ffff7e0a570 <std::system_error::~system_error()>)
at
/usr/src/debug/sys-devel/gcc-11.0.0_pre9999/gcc-11.0.0_pre9999/libstdc++-v3/libsupc++/eh_throw.cc:95
#6 0x00007ffff7db0c7f in std::__throw_system_error (__i=-1)
at
/usr/src/debug/sys-devel/gcc-11.0.0_pre9999/build/x86_64-pc-linux-gnu/libstdc++-v3/include/ext/new_allocator.h:89
#7 0x000055555555531d in std::call_once<main()::<lambda()> >(std::once_flag &,
struct {...} &&) (__once=..., __f=...)
at /usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/include/g++-v10/mutex:743
#8 0x000055555555521a in main () at a.cc:7
"""
https://stackoverflow.com/questions/8649828/what-are-the-correct-link-options-to-use-stdthread-in-gcc-under-linux
suggests -pthread has to be used.
Would it be feasible to convert this error as any of:
1. Link-time error that would complain about missing pthread_once symbol.
2. Successful run if libpthread is not present (probably not easy as libpthread
can be loaded dynamically at a later point in program's runtime).