public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/112351] New: libstdc++ locale init doesn't handle __gthread_once failure
@ 2023-11-02 15:07 rguenth at gcc dot gnu.org
  2023-11-02 15:48 ` [Bug libstdc++/112351] " redi at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-11-02 15:07 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 112351
           Summary: libstdc++ locale init doesn't handle __gthread_once
                    failure
           Product: gcc
           Version: 13.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: rguenth at gcc dot gnu.org
  Target Milestone: ---

pthread_once called through gthread_once can return "an error number" but the
locale init code isn't prepared for that, instead leaving the object
uninitialized:

src/c++98/locale.cc:

  __c_locale
  locale::facet::_S_get_c_locale()
  {
#ifdef __GTHREADS
    if (__gthread_active_p())
      __gthread_once(&_S_once, _S_initialize_once);
    else
#endif
      {
        if (!_S_c_locale)
          _S_initialize_once();
      }
    return _S_c_locale;
  }

OTOH similar code exists for C++11 locales which doesn't specifically handle
errors but instead executes the non-threaded fallback code in that case:

src/c++11/locale_init.cc:

  void
  locale::_S_initialize()
  {
#ifdef __GTHREADS
    if (!__gnu_cxx::__is_single_threaded())
      __gthread_once(&_S_once, _S_initialize_once);
#endif
    if (__builtin_expect(!_S_classic, 0))
      _S_initialize_once();
  }

Imagine an application overriding pthread_once (and other thread functions)
with an implementation that "fails" before some intitialization is done
(also imagine, just for curiosity, it wouldn't signal failure but success
and do nothing in that case...).

It would be nice to handle the situation consistently and either do the
single-threaded "fallback" or abort on failure.

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

* [Bug libstdc++/112351] libstdc++ locale init doesn't handle __gthread_once failure
  2023-11-02 15:07 [Bug libstdc++/112351] New: libstdc++ locale init doesn't handle __gthread_once failure rguenth at gcc dot gnu.org
@ 2023-11-02 15:48 ` redi at gcc dot gnu.org
  2023-11-06 10:00 ` rguenth at gcc dot gnu.org
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: redi at gcc dot gnu.org @ 2023-11-02 15:48 UTC (permalink / raw)
  To: gcc-bugs

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

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2023-11-02

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

* [Bug libstdc++/112351] libstdc++ locale init doesn't handle __gthread_once failure
  2023-11-02 15:07 [Bug libstdc++/112351] New: libstdc++ locale init doesn't handle __gthread_once failure rguenth at gcc dot gnu.org
  2023-11-02 15:48 ` [Bug libstdc++/112351] " redi at gcc dot gnu.org
@ 2023-11-06 10:00 ` rguenth at gcc dot gnu.org
  2023-11-06 10:47 ` redi at gcc dot gnu.org
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-11-06 10:00 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Richard Biener <rguenth at gcc dot gnu.org> ---
The question is also how much of explicit thread safety is needed when the init
is now happening inside libstdc++ (thus from _init which should be already
ensured to execute exactly once)?  Is that (_init time) done for all
targets/configurations btw?

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

* [Bug libstdc++/112351] libstdc++ locale init doesn't handle __gthread_once failure
  2023-11-02 15:07 [Bug libstdc++/112351] New: libstdc++ locale init doesn't handle __gthread_once failure rguenth at gcc dot gnu.org
  2023-11-02 15:48 ` [Bug libstdc++/112351] " redi at gcc dot gnu.org
  2023-11-06 10:00 ` rguenth at gcc dot gnu.org
@ 2023-11-06 10:47 ` redi at gcc dot gnu.org
  2023-11-07 12:55 ` cvs-commit at gcc dot gnu.org
  2023-11-07 12:57 ` rguenth at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: redi at gcc dot gnu.org @ 2023-11-06 10:47 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Ah yes, that's a good point. Patrick's improvement affects this initialization.

It's not done for all targets though, as not all targets have linker support
for the init_priority attribute (notably, darwin doesn't).

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

* [Bug libstdc++/112351] libstdc++ locale init doesn't handle __gthread_once failure
  2023-11-02 15:07 [Bug libstdc++/112351] New: libstdc++ locale init doesn't handle __gthread_once failure rguenth at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2023-11-06 10:47 ` redi at gcc dot gnu.org
@ 2023-11-07 12:55 ` cvs-commit at gcc dot gnu.org
  2023-11-07 12:57 ` rguenth at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2023-11-07 12:55 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Richard Biener <rguenth@gcc.gnu.org>:

https://gcc.gnu.org/g:7562f089a190953b8ef615b90b7b0520e812a930

commit r14-5220-g7562f089a190953b8ef615b90b7b0520e812a930
Author: Richard Biener <rguenther@suse.de>
Date:   Mon Nov 6 11:31:40 2023 +0100

    libstdc++/112351 - deal with __gthread_once failure during locale init

    The following makes the C++98 locale init path follow the way the
    C++11 performs initialization.  This way we deal with pthread_once
    failing, falling back to non-threadsafe initialization which, given we
    initialize from the library, should be serialized by the dynamic
    loader already.

            PR libstdc++/112351
    libstdc++-v3/
            * src/c++98/locale.cc (locale::facet::_S_initialize_once):
            Check whether _S_c_locale is already initialized.
            (locale::facet::_S_get_c_locale): Always perform non-threadsafe
            init when threadsafe init failed.

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

* [Bug libstdc++/112351] libstdc++ locale init doesn't handle __gthread_once failure
  2023-11-02 15:07 [Bug libstdc++/112351] New: libstdc++ locale init doesn't handle __gthread_once failure rguenth at gcc dot gnu.org
                   ` (3 preceding siblings ...)
  2023-11-07 12:55 ` cvs-commit at gcc dot gnu.org
@ 2023-11-07 12:57 ` rguenth at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-11-07 12:57 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
      Known to work|                            |14.0
           Assignee|unassigned at gcc dot gnu.org      |rguenth at gcc dot gnu.org

--- Comment #4 from Richard Biener <rguenth at gcc dot gnu.org> ---
Fixed on trunk sofar.

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

end of thread, other threads:[~2023-11-07 12:57 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-02 15:07 [Bug libstdc++/112351] New: libstdc++ locale init doesn't handle __gthread_once failure rguenth at gcc dot gnu.org
2023-11-02 15:48 ` [Bug libstdc++/112351] " redi at gcc dot gnu.org
2023-11-06 10:00 ` rguenth at gcc dot gnu.org
2023-11-06 10:47 ` redi at gcc dot gnu.org
2023-11-07 12:55 ` cvs-commit at gcc dot gnu.org
2023-11-07 12:57 ` 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).