public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/115421] New: Multi-threaded condition_variable app throws when linking as -static on Linux
@ 2024-06-10 17:32 ilg at livius dot net
  2024-06-10 17:35 ` [Bug libstdc++/115421] " pinskia at gcc dot gnu.org
                   ` (15 more replies)
  0 siblings, 16 replies; 17+ messages in thread
From: ilg at livius dot net @ 2024-06-10 17:32 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 115421
           Summary: Multi-threaded condition_variable app throws when
                    linking as -static on Linux
           Product: gcc
           Version: 14.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ilg at livius dot net
  Target Milestone: ---

Created attachment 58398
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=58398&action=edit
The test source code

I have a test that checks C++ threads which synchronise via a
condition_variable.

The test runs fine on Linux/macOS/Windows, in various scenarios, except when I
build it as `-static` on Linux.

```
$ g++ sleepy-threads-cv.cpp -o sleepy-threads-cv -static -g -lpthread
$ ./static-sleepy-threads-cv 4
abcd
$ g++ sleepy-threads-cv.cpp -o static-sleepy-threads-cv -static -g -lpthread
$ ./static-sleepy-threads-cv 4
Segmentation fault
```

On some systems the result is an exception:

```
terminate called after throwing an instance of 'std::system_error'
  what():  Enable multithreading to use std::thread: Operation not permitted
```

I encountered the issue while building the xPack GCC 14 binaries, but later
confirmed it on GCC 13 and older. 

The source code is attached.inside 

The problem seems to be the global condition variable and/or mutex; if I move
them inside the `spawnThreads()` function and pass pointers to them to all
threads, the test is functional even with `-static`.

Is there anything wrong with my test, or is this a small bug in libstdc++?

^ permalink raw reply	[flat|nested] 17+ messages in thread

* [Bug libstdc++/115421] Multi-threaded condition_variable app throws when linking as -static on Linux
  2024-06-10 17:32 [Bug libstdc++/115421] New: Multi-threaded condition_variable app throws when linking as -static on Linux ilg at livius dot net
@ 2024-06-10 17:35 ` pinskia at gcc dot gnu.org
  2024-06-10 17:36 ` pinskia at gcc dot gnu.org
                   ` (14 subsequent siblings)
  15 siblings, 0 replies; 17+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-06-10 17:35 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
*** Bug 115422 has been marked as a duplicate of this bug. ***

^ permalink raw reply	[flat|nested] 17+ messages in thread

* [Bug libstdc++/115421] Multi-threaded condition_variable app throws when linking as -static on Linux
  2024-06-10 17:32 [Bug libstdc++/115421] New: Multi-threaded condition_variable app throws when linking as -static on Linux ilg at livius dot net
  2024-06-10 17:35 ` [Bug libstdc++/115421] " pinskia at gcc dot gnu.org
@ 2024-06-10 17:36 ` pinskia at gcc dot gnu.org
  2024-06-10 17:38 ` pinskia at gcc dot gnu.org
                   ` (13 subsequent siblings)
  15 siblings, 0 replies; 17+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-06-10 17:36 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
What version of glibc do you have?

^ permalink raw reply	[flat|nested] 17+ messages in thread

* [Bug libstdc++/115421] Multi-threaded condition_variable app throws when linking as -static on Linux
  2024-06-10 17:32 [Bug libstdc++/115421] New: Multi-threaded condition_variable app throws when linking as -static on Linux ilg at livius dot net
  2024-06-10 17:35 ` [Bug libstdc++/115421] " pinskia at gcc dot gnu.org
  2024-06-10 17:36 ` pinskia at gcc dot gnu.org
@ 2024-06-10 17:38 ` pinskia at gcc dot gnu.org
  2024-06-10 17:42 ` pinskia at gcc dot gnu.org
                   ` (12 subsequent siblings)
  15 siblings, 0 replies; 17+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-06-10 17:38 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
   Last reconfirmed|                            |2024-06-10
             Status|UNCONFIRMED                 |WAITING

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
This works fine with glibc 2.34 and above.

^ permalink raw reply	[flat|nested] 17+ messages in thread

* [Bug libstdc++/115421] Multi-threaded condition_variable app throws when linking as -static on Linux
  2024-06-10 17:32 [Bug libstdc++/115421] New: Multi-threaded condition_variable app throws when linking as -static on Linux ilg at livius dot net
                   ` (2 preceding siblings ...)
  2024-06-10 17:38 ` pinskia at gcc dot gnu.org
@ 2024-06-10 17:42 ` pinskia at gcc dot gnu.org
  2024-06-10 17:56 ` ilg at livius dot net
                   ` (11 subsequent siblings)
  15 siblings, 0 replies; 17+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-06-10 17:42 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Andrew Pinski from comment #3)
> This works fine with glibc 2.34 and above.

I should say with glibc 2.34 and above and GCC 13+.
Other combinations are know to fail since you need to link in the whole
libpthread as there are weak symbols used to detect if the program was single
threaded or not.

^ permalink raw reply	[flat|nested] 17+ messages in thread

* [Bug libstdc++/115421] Multi-threaded condition_variable app throws when linking as -static on Linux
  2024-06-10 17:32 [Bug libstdc++/115421] New: Multi-threaded condition_variable app throws when linking as -static on Linux ilg at livius dot net
                   ` (3 preceding siblings ...)
  2024-06-10 17:42 ` pinskia at gcc dot gnu.org
@ 2024-06-10 17:56 ` ilg at livius dot net
  2024-06-10 18:00 ` pinskia at gcc dot gnu.org
                   ` (10 subsequent siblings)
  15 siblings, 0 replies; 17+ messages in thread
From: ilg at livius dot net @ 2024-06-10 17:56 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Liviu Ionescu <ilg at livius dot net> ---
Here is a run on Arch, with 2.39:

```
Tests summary for gcc 13.3.0-1 on linux-x64 (Arch rolling)

394 test(s) passed, 4 failed:

- fail: static-sleepy-threads-cv-64
- fail: static-gc-sleepy-threads-cv-64
- fail: static-lto-sleepy-threads-cv-64
- fail: static-gc-lto-sleepy-threads-cv-64

Verdict: tests reluctantly accepted

[uname -a]
Linux f27625f70135 6.5.0-1021-azure #22~22.04.1-Ubuntu SMP Tue Apr 30 16:08:18
UTC 2024 x86_64 GNU/Linux

[lsb_release -a]
LSB Version:    n/a
Distributor ID: Arch
Description:    Arch Linux
Release:        rolling
Codename:       n/a

[ldd --version]
ldd (GNU libc) 2.39
Copyright (C) 2024 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Written by Roland McGrath and Ulrich Drepper.
```

^ permalink raw reply	[flat|nested] 17+ messages in thread

* [Bug libstdc++/115421] Multi-threaded condition_variable app throws when linking as -static on Linux
  2024-06-10 17:32 [Bug libstdc++/115421] New: Multi-threaded condition_variable app throws when linking as -static on Linux ilg at livius dot net
                   ` (4 preceding siblings ...)
  2024-06-10 17:56 ` ilg at livius dot net
@ 2024-06-10 18:00 ` pinskia at gcc dot gnu.org
  2024-06-10 18:08 ` pinskia at gcc dot gnu.org
                   ` (9 subsequent siblings)
  15 siblings, 0 replies; 17+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-06-10 18:00 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
apinski@xeond:~/src/upstream-gcc-match$ /lib/x86_64-linux-gnu/libc.so.6
GNU C Library (Ubuntu GLIBC 2.35-0ubuntu3.8) stable release version 2.35.
Copyright (C) 2022 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
Compiled by GNU CC version 11.4.0.
libc ABIs: UNIQUE IFUNC ABSOLUTE
For bug reporting instructions, please see:
<https://bugs.launchpad.net/ubuntu/+source/glibc/+bugs>.
apinski@xeond:~/src/upstream-gcc-match$ ~/upstream-gcc-match/bin/g++ -static -g
-lpthread t.cc
apinski@xeond:~/src/upstream-gcc-match$ ./a.out
Usage: sleepy-thread N
apinski@xeond:~/src/upstream-gcc-match$ ./a.out  4
abcd

^ permalink raw reply	[flat|nested] 17+ messages in thread

* [Bug libstdc++/115421] Multi-threaded condition_variable app throws when linking as -static on Linux
  2024-06-10 17:32 [Bug libstdc++/115421] New: Multi-threaded condition_variable app throws when linking as -static on Linux ilg at livius dot net
                   ` (5 preceding siblings ...)
  2024-06-10 18:00 ` pinskia at gcc dot gnu.org
@ 2024-06-10 18:08 ` pinskia at gcc dot gnu.org
  2024-06-10 18:13 ` ilg at livius dot net
                   ` (8 subsequent siblings)
  15 siblings, 0 replies; 17+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-06-10 18:08 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Are you sure that arch does not patch glibc to make it broken?

^ permalink raw reply	[flat|nested] 17+ messages in thread

* [Bug libstdc++/115421] Multi-threaded condition_variable app throws when linking as -static on Linux
  2024-06-10 17:32 [Bug libstdc++/115421] New: Multi-threaded condition_variable app throws when linking as -static on Linux ilg at livius dot net
                   ` (6 preceding siblings ...)
  2024-06-10 18:08 ` pinskia at gcc dot gnu.org
@ 2024-06-10 18:13 ` ilg at livius dot net
  2024-06-10 18:16 ` sjames at gcc dot gnu.org
                   ` (7 subsequent siblings)
  15 siblings, 0 replies; 17+ messages in thread
From: ilg at livius dot net @ 2024-06-10 18:13 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #8 from Liviu Ionescu <ilg at livius dot net> ---
A small correction, the first run should be without -static:

```
$ g++ sleepy-threads-cv.cpp -o sleepy-threads-cv -g -lpthread
$ ./sleepy-threads-cv 4
abcd
$ g++ sleepy-threads-cv.cpp -o static-sleepy-threads-cv -static -g -lpthread
$ ./static-sleepy-threads-cv 4
Segmentation fault
```

^ permalink raw reply	[flat|nested] 17+ messages in thread

* [Bug libstdc++/115421] Multi-threaded condition_variable app throws when linking as -static on Linux
  2024-06-10 17:32 [Bug libstdc++/115421] New: Multi-threaded condition_variable app throws when linking as -static on Linux ilg at livius dot net
                   ` (7 preceding siblings ...)
  2024-06-10 18:13 ` ilg at livius dot net
@ 2024-06-10 18:16 ` sjames at gcc dot gnu.org
  2024-06-10 18:22 ` ilg at livius dot net
                   ` (6 subsequent siblings)
  15 siblings, 0 replies; 17+ messages in thread
From: sjames at gcc dot gnu.org @ 2024-06-10 18:16 UTC (permalink / raw)
  To: gcc-bugs

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

Sam James <sjames at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |sjames at gcc dot gnu.org

--- Comment #9 from Sam James <sjames at gcc dot gnu.org> ---
I can't reproduce the crash with Gentoo's glibc (`ld.so (Gentoo 2.39-r7
(patchset 7)) stable release version 2.39.`) and gcc 14/15 (tip of both
branches). I get 'abcd' both times.

^ permalink raw reply	[flat|nested] 17+ messages in thread

* [Bug libstdc++/115421] Multi-threaded condition_variable app throws when linking as -static on Linux
  2024-06-10 17:32 [Bug libstdc++/115421] New: Multi-threaded condition_variable app throws when linking as -static on Linux ilg at livius dot net
                   ` (8 preceding siblings ...)
  2024-06-10 18:16 ` sjames at gcc dot gnu.org
@ 2024-06-10 18:22 ` ilg at livius dot net
  2024-06-10 18:26 ` ilg at livius dot net
                   ` (5 subsequent siblings)
  15 siblings, 0 replies; 17+ messages in thread
From: ilg at livius dot net @ 2024-06-10 18:22 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #10 from Liviu Ionescu <ilg at livius dot net> ---
> Are you sure that arch does not patch glibc to make it broken?

That run was using docker://archlinux:latest, I don't know if glibc is broken.

^ permalink raw reply	[flat|nested] 17+ messages in thread

* [Bug libstdc++/115421] Multi-threaded condition_variable app throws when linking as -static on Linux
  2024-06-10 17:32 [Bug libstdc++/115421] New: Multi-threaded condition_variable app throws when linking as -static on Linux ilg at livius dot net
                   ` (9 preceding siblings ...)
  2024-06-10 18:22 ` ilg at livius dot net
@ 2024-06-10 18:26 ` ilg at livius dot net
  2024-06-10 18:49 ` ilg at livius dot net
                   ` (4 subsequent siblings)
  15 siblings, 0 replies; 17+ messages in thread
From: ilg at livius dot net @ 2024-06-10 18:26 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #11 from Liviu Ionescu <ilg at livius dot net> ---
I did two more runs, one on latest Raspberry Pi OS and one with latest Manjaro,
and they also passed.

So the problem seems indeed related to older systems.

^ permalink raw reply	[flat|nested] 17+ messages in thread

* [Bug libstdc++/115421] Multi-threaded condition_variable app throws when linking as -static on Linux
  2024-06-10 17:32 [Bug libstdc++/115421] New: Multi-threaded condition_variable app throws when linking as -static on Linux ilg at livius dot net
                   ` (10 preceding siblings ...)
  2024-06-10 18:26 ` ilg at livius dot net
@ 2024-06-10 18:49 ` ilg at livius dot net
  2024-06-10 18:52 ` pinskia at gcc dot gnu.org
                   ` (3 subsequent siblings)
  15 siblings, 0 replies; 17+ messages in thread
From: ilg at livius dot net @ 2024-06-10 18:49 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #12 from Liviu Ionescu <ilg at livius dot net> ---
However Debian 12 is not that old, what might be wrong with it? The initial
report is based on a Debian 12 run. I did another run on a physical Debian 12
and the issue is reproducible on my machine.

Isn't it possible to fix libstdc++ in order to run static multi-threaded apps
on older systems too?

^ permalink raw reply	[flat|nested] 17+ messages in thread

* [Bug libstdc++/115421] Multi-threaded condition_variable app throws when linking as -static on Linux
  2024-06-10 17:32 [Bug libstdc++/115421] New: Multi-threaded condition_variable app throws when linking as -static on Linux ilg at livius dot net
                   ` (11 preceding siblings ...)
  2024-06-10 18:49 ` ilg at livius dot net
@ 2024-06-10 18:52 ` pinskia at gcc dot gnu.org
  2024-06-11  9:26 ` redi at gcc dot gnu.org
                   ` (2 subsequent siblings)
  15 siblings, 0 replies; 17+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-06-10 18:52 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #13 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Liviu Ionescu from comment #12)
> Isn't it possible to fix libstdc++ in order to run static multi-threaded
> apps on older systems too?

Well it depends. glibc changed how libpthread works in 2.34 and above. So
someone with the specific versions that you are testing with need to debug it
to see what is going wrong. It might need `-Wl,--whole-archive -lpthread
-Wl,--no-whole-archive` as mentioned in PR 58909 or some other changes.

^ permalink raw reply	[flat|nested] 17+ messages in thread

* [Bug libstdc++/115421] Multi-threaded condition_variable app throws when linking as -static on Linux
  2024-06-10 17:32 [Bug libstdc++/115421] New: Multi-threaded condition_variable app throws when linking as -static on Linux ilg at livius dot net
                   ` (12 preceding siblings ...)
  2024-06-10 18:52 ` pinskia at gcc dot gnu.org
@ 2024-06-11  9:26 ` redi at gcc dot gnu.org
  2024-06-11  9:27 ` redi at gcc dot gnu.org
  2024-06-11  9:39 ` rguenth at gcc dot gnu.org
  15 siblings, 0 replies; 17+ messages in thread
From: redi at gcc dot gnu.org @ 2024-06-11  9:26 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #14 from Jonathan Wakely <redi at gcc dot gnu.org> ---
This is just a dup of PR 58909

^ permalink raw reply	[flat|nested] 17+ messages in thread

* [Bug libstdc++/115421] Multi-threaded condition_variable app throws when linking as -static on Linux
  2024-06-10 17:32 [Bug libstdc++/115421] New: Multi-threaded condition_variable app throws when linking as -static on Linux ilg at livius dot net
                   ` (13 preceding siblings ...)
  2024-06-11  9:26 ` redi at gcc dot gnu.org
@ 2024-06-11  9:27 ` redi at gcc dot gnu.org
  2024-06-11  9:39 ` rguenth at gcc dot gnu.org
  15 siblings, 0 replies; 17+ messages in thread
From: redi at gcc dot gnu.org @ 2024-06-11  9:27 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #15 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Liviu Ionescu from comment #12)
> Isn't it possible to fix libstdc++ in order to run static multi-threaded
> apps on older systems too?

Just link all of libpthread.a as Andrew said.

^ permalink raw reply	[flat|nested] 17+ messages in thread

* [Bug libstdc++/115421] Multi-threaded condition_variable app throws when linking as -static on Linux
  2024-06-10 17:32 [Bug libstdc++/115421] New: Multi-threaded condition_variable app throws when linking as -static on Linux ilg at livius dot net
                   ` (14 preceding siblings ...)
  2024-06-11  9:27 ` redi at gcc dot gnu.org
@ 2024-06-11  9:39 ` rguenth at gcc dot gnu.org
  15 siblings, 0 replies; 17+ messages in thread
From: rguenth at gcc dot gnu.org @ 2024-06-11  9:39 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|WAITING                     |RESOLVED
         Resolution|---                         |DUPLICATE

--- Comment #16 from Richard Biener <rguenth at gcc dot gnu.org> ---
dup then

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

^ permalink raw reply	[flat|nested] 17+ messages in thread

end of thread, other threads:[~2024-06-11  9:39 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-06-10 17:32 [Bug libstdc++/115421] New: Multi-threaded condition_variable app throws when linking as -static on Linux ilg at livius dot net
2024-06-10 17:35 ` [Bug libstdc++/115421] " pinskia at gcc dot gnu.org
2024-06-10 17:36 ` pinskia at gcc dot gnu.org
2024-06-10 17:38 ` pinskia at gcc dot gnu.org
2024-06-10 17:42 ` pinskia at gcc dot gnu.org
2024-06-10 17:56 ` ilg at livius dot net
2024-06-10 18:00 ` pinskia at gcc dot gnu.org
2024-06-10 18:08 ` pinskia at gcc dot gnu.org
2024-06-10 18:13 ` ilg at livius dot net
2024-06-10 18:16 ` sjames at gcc dot gnu.org
2024-06-10 18:22 ` ilg at livius dot net
2024-06-10 18:26 ` ilg at livius dot net
2024-06-10 18:49 ` ilg at livius dot net
2024-06-10 18:52 ` pinskia at gcc dot gnu.org
2024-06-11  9:26 ` redi at gcc dot gnu.org
2024-06-11  9:27 ` redi at gcc dot gnu.org
2024-06-11  9:39 ` rguenth 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).