Bug 97485 - std::call_once crashes at runtime on glibc if not linked to libpthread: terminate called after throwing an instance of 'std::system_error': what(): Unknown error -1
Summary: std::call_once crashes at runtime on glibc if not linked to libpthread: termi...
Status: RESOLVED DUPLICATE of bug 55394
Alias: None
Product: gcc
Classification: Unclassified
Component: libstdc++ (show other bugs)
Version: 10.2.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-10-19 07:10 UTC by Sergei Trofimovich
Modified: 2020-10-19 22:07 UTC (History)
0 users

See Also:
Host: x86_64-pc-linux-gnu
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Sergei Trofimovich 2020-10-19 07:10:10 UTC
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).
Comment 1 Sergei Trofimovich 2020-10-19 07:14:14 UTC
$ LANG=C gcc-11.0.0 -v
Using built-in specs.
COLLECT_GCC=/usr/bin/gcc-11.0.0
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-pc-linux-gnu/11.0.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: /var/tmp/portage/sys-devel/gcc-11.0.0_pre9999/work/gcc-11.0.0_pre9999/configure --host=x86_64-pc-linux-gnu --build=x86_64-pc-linux-gnu --prefix=/usr --bindir=/usr/x86_64-pc-linux-gnu/gcc-bin/11.0.0 --includedir=/usr/lib/gcc/x86_64-pc-linux-gnu/11.0.0/include --datadir=/usr/share/gcc-data/x86_64-pc-linux-gnu/11.0.0 --mandir=/usr/share/gcc-data/x86_64-pc-linux-gnu/11.0.0/man --infodir=/usr/share/gcc-data/x86_64-pc-linux-gnu/11.0.0/info --with-gxx-include-dir=/usr/lib/gcc/x86_64-pc-linux-gnu/11.0.0/include/g++-v11 --with-python-dir=/share/gcc-data/x86_64-pc-linux-gnu/11.0.0/python --enable-languages=c,c++,go,jit,fortran --enable-obsolete --enable-secureplt --disable-werror --with-system-zlib --enable-nls --without-included-gettext --enable-checking=release --with-bugurl=https://bugs.gentoo.org/ --with-pkgversion='Gentoo 11.0.0_pre9999 p4, commit 6b55fa29adf4d643e61388bf01a4509b0b041d17' --disable-esp --enable-libstdcxx-time --enable-host-shared --enable-shared --enable-threads=posix --enable-__cxa_atexit --enable-clocale=gnu --enable-multilib --with-multilib-list=m32,m64 --disable-fixed-point --enable-targets=all --enable-libgomp --disable-libssp --disable-libada --disable-systemtap --enable-valgrind-annotations --enable-vtable-verify --without-zstd --enable-lto --with-isl --disable-isl-version-check --enable-default-pie --enable-default-ssp
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 11.0.0 20201015 (experimental) (Gentoo 11.0.0_pre9999 p4, commit 6b55fa29adf4d643e61388bf01a4509b0b041d17)
Comment 2 Sergei Trofimovich 2020-10-19 07:23:50 UTC
AFAIU '-1' comes from https://gcc.gnu.org/git/?p=gcc.git;a=blob;f=libgcc/gthr-posix.h;h=965247602acf11f81c5fa009c7ee48eb55cbacca;hb=HEAD#l696

 696 static inline int
 697 __gthread_once (__gthread_once_t *__once, void (*__func) (void))
 698 {
 699   if (__gthread_active_p ())
 700     return __gthrw_(pthread_once) (__once, __func);
 701   else
 702     return -1;
 703 }

where '__gthread_active_p ()' returns false:
  (gdb) call __gthread_active_p()
  $2 = 0

That is defined in https://gcc.gnu.org/git/?p=gcc.git;a=blob;f=libgcc/gthr-posix.h;h=965247602acf11f81c5fa009c7ee48eb55cbacca;hb=HEAD#l236

 236 #ifdef __GLIBC__
 237 __gthrw2(__gthrw_(__pthread_key_create),
 238          __pthread_key_create,
 239          pthread_key_create)
 240 # define GTHR_ACTIVE_PROXY      __gthrw_(__pthread_key_create)
 241 #elif defined (__BIONIC__)
 242 # define GTHR_ACTIVE_PROXY      __gthrw_(pthread_create)
 243 #else
 244 # define GTHR_ACTIVE_PROXY      __gthrw_(pthread_cancel)
 245 #endif
 246 
 247 static inline int
 248 __gthread_active_p (void)
 249 {
 250   static void *const __gthread_active_ptr
 251     = __extension__ (void *) &GTHR_ACTIVE_PROXY;
 252   return __gthread_active_ptr != 0;
 253 }

Both of symbols are weak, unbound to libpthread and unversioned (for non-working case):

$ g++-11.0.0 a.cc -o a && x86_64-pc-linux-gnu-nm -D a | fgrep -i pthread
                 w __pthread_key_create
                 w pthread_once

$ g++-11.0.0 a.cc -o a -pthread && x86_64-pc-linux-gnu-nm -D a | fgrep -i pthread
                 w __pthread_key_create@@GLIBC_2.2.5
                 w pthread_once@@GLIBC_2.2.5
Comment 3 Jonathan Wakely 2020-10-19 12:56:31 UTC
Looks like a dup of PR 55394
Comment 4 Sergei Trofimovich 2020-10-19 22:07:26 UTC
Ah, yes!

*** This bug has been marked as a duplicate of bug 55394 ***