public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/97211] New: __cxa_guard_acquire fails to detect recursive init in multithreaded code
@ 2020-09-25 19:55 redi at gcc dot gnu.org
  0 siblings, 0 replies; only message in thread
From: redi at gcc dot gnu.org @ 2020-09-25 19:55 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97211

            Bug ID: 97211
           Summary: __cxa_guard_acquire fails to detect recursive init in
                    multithreaded code
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: redi at gcc dot gnu.org
  Target Milestone: ---

In a single-threaded program this detects recursive init:

#include <stddef.h>

int a() {
        static int xx = a();
        return 1;
}
extern "C" void* b(void*)
{
        a();
        return NULL;
}

int main() {
        b(NULL);
}

terminate called after throwing an instance of
'__gnu_cxx::recursive_init_error'
  what():  std::exception
Aborted (core dumped)


When we know there are no threads in the program we can assume that attempting
to acquire the guard variable when it's already in progress means recursive
init. Because no other thread exists, so it must have been the current thread
that started it, and we've re-entered the initialization.

But if there are multiple threads we just go to sleep waiting forever for
initialization to finish:

#include <pthread.h>
int a() {
        static int xx = a();
        return 1;
}
extern "C" void* b(void*)
{
        a();
        return NULL;
}

int main() {
        pthread_t thrd;
        pthread_create(&thrd, NULL, b, NULL);
        pthread_join(thrd, NULL);
}

Clang makes this work by storing the result of SYS_gettid in the last four
bytes of the guard variable, so it can detect when the same thread re-enters
the guard.

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2020-09-25 19:55 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-09-25 19:55 [Bug libstdc++/97211] New: __cxa_guard_acquire fails to detect recursive init in multithreaded code redi at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).