public inbox for glibc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "jimb@red-bean.com" <sourceware-bugzilla@sourceware.org>
To: glibc-bugs@sourceware.org
Subject: [Bug dynamic-link/31285] New: Segfault when returning from main while a thread calls dlclose
Date: Wed, 24 Jan 2024 03:19:18 +0000	[thread overview]
Message-ID: <bug-31285-131@http.sourceware.org/bugzilla/> (raw)

https://sourceware.org/bugzilla/show_bug.cgi?id=31285

            Bug ID: 31285
           Summary: Segfault when returning from main while a thread calls
                    dlclose
           Product: glibc
           Version: 2.38
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: dynamic-link
          Assignee: unassigned at sourceware dot org
          Reporter: jimb@red-bean.com
  Target Milestone: ---

If the program's main thread returns while another thread calls dlclose on a
shared library that has a static variable with a destructor, dlclose may unmap
the shared library's code while the main thread is still executing the
destructor, causing a crash.

This can manifest as intermittent crashes at exit, as in this issue:
https://github.com/gfx-rs/wgpu/issues/5084

The bug is that if the main thread calls `__run_exit_handlers` and finds the
shared library's destructor's entry in __exit_funcs` first, changes its flavor
to `ef_free`, releases `__exit_funcs_lock`, and then calls the entry's
function, that function is arbitrary code which may take a long time to run.
While it does so, another thread may `dlclose` the shared library, skip past
the entry in `__exit_funcs` since it is marked as `ef_free`, and proceed to
unmap the shared library's code while the main thread is still working.

The attached test case reproduces the bug reliably. To reproduce, download the
attached test case as `dlclose-crash.tar.gz`, and then:

```
$ tar xf dlclose-crash.tar.gz 
$ cd dlclose-crash/
$ make
g++ -g -export-dynamic main.cpp -o main
g++ -g  -shared -fPIC solib.cpp -o solib.so
$ ./main
Segmentation fault (core dumped)
$ 
```

-- 
You are receiving this mail because:
You are on the CC list for the bug.

             reply	other threads:[~2024-01-24  3:19 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-24  3:19 jimb@red-bean.com [this message]
2024-01-24  3:20 ` [Bug dynamic-link/31285] " jimb@red-bean.com

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-31285-131@http.sourceware.org/bugzilla/ \
    --to=sourceware-bugzilla@sourceware.org \
    --cc=glibc-bugs@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).