public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] libstdc++/112351 - deal with __gthread_once failure during locale init
@ 2023-11-06 11:52 Richard Biener
  0 siblings, 0 replies; 7+ messages in thread
From: Richard Biener @ 2023-11-06 11:52 UTC (permalink / raw)
  To: gcc-patches; +Cc: libstdc++, jwakely

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.

Bootstrapped and tested on x86_64-unknown-linux-gnu, OK for trunk?
And GCC 13 branch?

Thanks,
Richard.

	PR libstdc++/112351
libstdc++-v3/
	* src/c++98/locale.cc (locale::facet::_S_get_c_locale):
	Always perform non-threadsafe init when threadsafe init
	failed.
---
 libstdc++-v3/src/c++98/locale.cc | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)

diff --git a/libstdc++-v3/src/c++98/locale.cc b/libstdc++-v3/src/c++98/locale.cc
index d308140bab7..e9bec1db3b6 100644
--- a/libstdc++-v3/src/c++98/locale.cc
+++ b/libstdc++-v3/src/c++98/locale.cc
@@ -216,12 +216,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 #ifdef __GTHREADS
     if (__gthread_active_p())
       __gthread_once(&_S_once, _S_initialize_once);
-    else
 #endif
-      {
-	if (!_S_c_locale)
-	  _S_initialize_once();
-      }
+    if (__builtin_expect (!_S_c_locale, 0))
+      _S_initialize_once();
     return _S_c_locale;
   }
 
-- 
2.35.3

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

* Re: [PATCH] libstdc++/112351 - deal with __gthread_once failure during locale init
  2023-11-06 12:51   ` Richard Biener
  2023-11-07 10:55     ` Richard Biener
@ 2023-11-07 11:44     ` Jonathan Wakely
  1 sibling, 0 replies; 7+ messages in thread
From: Jonathan Wakely @ 2023-11-07 11:44 UTC (permalink / raw)
  To: Richard Biener; +Cc: gcc-patches, libstdc++

On Mon, 6 Nov 2023 at 12:52, Richard Biener <rguenther@suse.de> wrote:
>
> On Mon, 6 Nov 2023, Jonathan Wakely wrote:
>
> > On Mon, 6 Nov 2023 at 11:52, Richard Biener <rguenther@suse.de> wrote:
> > >
> > > 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.
> > >
> > > Bootstrapped and tested on x86_64-unknown-linux-gnu, OK for trunk?
> > > And GCC 13 branch?
> > >
> > > Thanks,
> > > Richard.
> > >
> > >         PR libstdc++/112351
> > > libstdc++-v3/
> > >         * src/c++98/locale.cc (locale::facet::_S_get_c_locale):
> > >         Always perform non-threadsafe init when threadsafe init
> > >         failed.
> > > ---
> > >  libstdc++-v3/src/c++98/locale.cc | 7 ++-----
> > >  1 file changed, 2 insertions(+), 5 deletions(-)
> > >
> > > diff --git a/libstdc++-v3/src/c++98/locale.cc b/libstdc++-v3/src/c++98/locale.cc
> > > index d308140bab7..e9bec1db3b6 100644
> > > --- a/libstdc++-v3/src/c++98/locale.cc
> > > +++ b/libstdc++-v3/src/c++98/locale.cc
> > > @@ -216,12 +216,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
> > >  #ifdef __GTHREADS
> > >      if (__gthread_active_p())
> > >        __gthread_once(&_S_once, _S_initialize_once);
> > > -    else
> > >  #endif
> > > -      {
> > > -       if (!_S_c_locale)
> > > -         _S_initialize_once();
> > > -      }
> > > +    if (__builtin_expect (!_S_c_locale, 0))
> > > +      _S_initialize_once();
> > >      return _S_c_locale;
> > >    }
> >
> >
> > I think this has a problem, which is handled correctly in
> > src/c++11/locale_init.cc by checking _S_classic inside the
> > _S_initialize_once function.
>
> We check _S_c_locale here (it's just a pointer) instead of in

It's a __locale_struct* for glibc and int* for BSD. Hopefully it's
always something pointer-ish so that works.

> _S_initialize_once (), so I think the code is equivalent, no?
>
> > If the first call to __gthread_once does fail then _S_once will not be
> > changed. We will fall through to calling _S_initialize_once directly
> > (which is not thread-safe) and set _S_c_locale.
> >
> > The next time we call _S_initialize, __gthread_once will try to run
> > again, and because _S_once was not changed, it might call
> > _S_initialize_once() again, which writes to _S_c_locale again
> > (possibly causing a data race).
>
> Ah, yeah, so in the C++11 path the check for !_S_classic in
> locale::_S_initialize is redundant.  But good spot.

Redundant, but it avoids a call to _S_initialize_once, so probably
worth it for performance.

>
> > I don't think the slightly different code in src/c++11/locale_init.cc
> > is different in order to handle __gthread_once failing, I think it's
> > different because the effects of locale::facet::_S_initialize_once()
> > and locale::_S_initialize_once() are different. One is safe to call
> > more than once, and the other isn't.
> >
> > I don't think we need to care about __gthread_once failing at all, do
> > we? There are no error conditions for pthread_once, it always returns
> > 0 (previous POSIX revisions said it could return EINVAL for an
> > uninitialized pthread_once_t but that can't happen here as it's
> > correctly initialized in src/c++11/locale.cc). Is the concern that it
> > can fail for non-posix thread models? (I didn't check if any of them
> > can actually fail)
>
> The concern is that there are actual products out that break with the
> new I/O initialization in libstdc++ for GCC13+ because they have bugs.
> It's easy enough to work around those by the proposed patch (plus
> correction for the above issue).  I suppose the comment in
> locale::_S_initialize_once holds as well for the C++98 path.
>
> The failure mode of the product is that it overrides pthread_once
> but does nothing (not even indicate failure) when its pthread_*
> override mechanism isn't initialized yet.  With libstdc++ from GCC13
> we now use pthread_once "too early" and fail to initialize the locale
> object.
>
> Adjusted patch below.
>
> OK after another round of testing?

OK, thanks.

>
> Thanks,
> Richard.
>
>
> From 4e3fa2f4426a5a10d189587b63e4d7298c347b01 Mon Sep 17 00:00:00 2001
> From: Richard Biener <rguenther@suse.de>
> Date: Mon, 6 Nov 2023 11:31:40 +0100
> Subject: [PATCH] libstdc++/112351 - deal with __gthread_once failure during
>  locale init
> To: gcc-patches@gcc.gnu.org
>
> 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.
> ---
>  libstdc++-v3/src/c++98/locale.cc | 13 ++++++++-----
>  1 file changed, 8 insertions(+), 5 deletions(-)
>
> diff --git a/libstdc++-v3/src/c++98/locale.cc b/libstdc++-v3/src/c++98/locale.cc
> index d308140bab7..1ef0c394cd7 100644
> --- a/libstdc++-v3/src/c++98/locale.cc
> +++ b/libstdc++-v3/src/c++98/locale.cc
> @@ -206,6 +206,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>    void
>    locale::facet::_S_initialize_once()
>    {
> +    // Need to check this because we could get called once from
> +    // _S_get_c_locale() when the program is single-threaded, and then again
> +    // (via __gthread_once) when it's multi-threaded.
> +    if (_S_c_locale)
> +      return;
> +
>      // Initialize the underlying locale model.
>      _S_create_c_locale(_S_c_locale, _S_c_name);
>    }
> @@ -216,12 +222,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>  #ifdef __GTHREADS
>      if (__gthread_active_p())
>        __gthread_once(&_S_once, _S_initialize_once);
> -    else
>  #endif
> -      {
> -       if (!_S_c_locale)
> -         _S_initialize_once();
> -      }
> +    if (__builtin_expect (!_S_c_locale, 0))
> +      _S_initialize_once();
>      return _S_c_locale;
>    }
>
> --
> 2.35.3
>


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

* Re: [PATCH] libstdc++/112351 - deal with __gthread_once failure during locale init
  2023-11-06 12:51   ` Richard Biener
@ 2023-11-07 10:55     ` Richard Biener
  2023-11-07 11:44     ` Jonathan Wakely
  1 sibling, 0 replies; 7+ messages in thread
From: Richard Biener @ 2023-11-07 10:55 UTC (permalink / raw)
  To: Jonathan Wakely; +Cc: gcc-patches, libstdc++

On Mon, 6 Nov 2023, Richard Biener wrote:

> On Mon, 6 Nov 2023, Jonathan Wakely wrote:
> 
> > On Mon, 6 Nov 2023 at 11:52, Richard Biener <rguenther@suse.de> wrote:
> > >
> > > 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.
> > >
> > > Bootstrapped and tested on x86_64-unknown-linux-gnu, OK for trunk?
> > > And GCC 13 branch?
> > >
> > > Thanks,
> > > Richard.

[.. old patch ..]

> > The next time we call _S_initialize, __gthread_once will try to run
> > again, and because _S_once was not changed, it might call
> > _S_initialize_once() again, which writes to _S_c_locale again
> > (possibly causing a data race).
> 
> Ah, yeah, so in the C++11 path the check for !_S_classic in
> locale::_S_initialize is redundant.  But good spot.
> 
> > I don't think the slightly different code in src/c++11/locale_init.cc
> > is different in order to handle __gthread_once failing, I think it's
> > different because the effects of locale::facet::_S_initialize_once()
> > and locale::_S_initialize_once() are different. One is safe to call
> > more than once, and the other isn't.
> > 
> > I don't think we need to care about __gthread_once failing at all, do
> > we? There are no error conditions for pthread_once, it always returns
> > 0 (previous POSIX revisions said it could return EINVAL for an
> > uninitialized pthread_once_t but that can't happen here as it's
> > correctly initialized in src/c++11/locale.cc). Is the concern that it
> > can fail for non-posix thread models? (I didn't check if any of them
> > can actually fail)
> 
> The concern is that there are actual products out that break with the
> new I/O initialization in libstdc++ for GCC13+ because they have bugs.
> It's easy enough to work around those by the proposed patch (plus
> correction for the above issue).  I suppose the comment in
> locale::_S_initialize_once holds as well for the C++98 path.
> 
> The failure mode of the product is that it overrides pthread_once
> but does nothing (not even indicate failure) when its pthread_*
> override mechanism isn't initialized yet.  With libstdc++ from GCC13
> we now use pthread_once "too early" and fail to initialize the locale
> object.
> 
> Adjusted patch below.
> 
> OK after another round of testing?

Bootstrapped and tested on x86_64-unknown-linux-gnu.

OK?

Thanks,
Richard.

> Thanks,
> Richard.
> 
> 
> From 4e3fa2f4426a5a10d189587b63e4d7298c347b01 Mon Sep 17 00:00:00 2001
> From: Richard Biener <rguenther@suse.de>
> Date: Mon, 6 Nov 2023 11:31:40 +0100
> Subject: [PATCH] libstdc++/112351 - deal with __gthread_once failure during
>  locale init
> To: gcc-patches@gcc.gnu.org
> 
> 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.
> ---
>  libstdc++-v3/src/c++98/locale.cc | 13 ++++++++-----
>  1 file changed, 8 insertions(+), 5 deletions(-)
> 
> diff --git a/libstdc++-v3/src/c++98/locale.cc b/libstdc++-v3/src/c++98/locale.cc
> index d308140bab7..1ef0c394cd7 100644
> --- a/libstdc++-v3/src/c++98/locale.cc
> +++ b/libstdc++-v3/src/c++98/locale.cc
> @@ -206,6 +206,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>    void
>    locale::facet::_S_initialize_once()
>    {
> +    // Need to check this because we could get called once from
> +    // _S_get_c_locale() when the program is single-threaded, and then again
> +    // (via __gthread_once) when it's multi-threaded.
> +    if (_S_c_locale)
> +      return;
> +
>      // Initialize the underlying locale model.
>      _S_create_c_locale(_S_c_locale, _S_c_name);
>    }
> @@ -216,12 +222,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>  #ifdef __GTHREADS
>      if (__gthread_active_p())
>        __gthread_once(&_S_once, _S_initialize_once);
> -    else
>  #endif
> -      {
> -	if (!_S_c_locale)
> -	  _S_initialize_once();
> -      }
> +    if (__builtin_expect (!_S_c_locale, 0))
> +      _S_initialize_once();
>      return _S_c_locale;
>    }
>  
> 

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

* Re: [PATCH] libstdc++/112351 - deal with __gthread_once failure during locale init
  2023-11-06 12:15 ` Jonathan Wakely
@ 2023-11-06 12:51   ` Richard Biener
  2023-11-07 10:55     ` Richard Biener
  2023-11-07 11:44     ` Jonathan Wakely
  0 siblings, 2 replies; 7+ messages in thread
From: Richard Biener @ 2023-11-06 12:51 UTC (permalink / raw)
  To: Jonathan Wakely; +Cc: gcc-patches, libstdc++

On Mon, 6 Nov 2023, Jonathan Wakely wrote:

> On Mon, 6 Nov 2023 at 11:52, Richard Biener <rguenther@suse.de> wrote:
> >
> > 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.
> >
> > Bootstrapped and tested on x86_64-unknown-linux-gnu, OK for trunk?
> > And GCC 13 branch?
> >
> > Thanks,
> > Richard.
> >
> >         PR libstdc++/112351
> > libstdc++-v3/
> >         * src/c++98/locale.cc (locale::facet::_S_get_c_locale):
> >         Always perform non-threadsafe init when threadsafe init
> >         failed.
> > ---
> >  libstdc++-v3/src/c++98/locale.cc | 7 ++-----
> >  1 file changed, 2 insertions(+), 5 deletions(-)
> >
> > diff --git a/libstdc++-v3/src/c++98/locale.cc b/libstdc++-v3/src/c++98/locale.cc
> > index d308140bab7..e9bec1db3b6 100644
> > --- a/libstdc++-v3/src/c++98/locale.cc
> > +++ b/libstdc++-v3/src/c++98/locale.cc
> > @@ -216,12 +216,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
> >  #ifdef __GTHREADS
> >      if (__gthread_active_p())
> >        __gthread_once(&_S_once, _S_initialize_once);
> > -    else
> >  #endif
> > -      {
> > -       if (!_S_c_locale)
> > -         _S_initialize_once();
> > -      }
> > +    if (__builtin_expect (!_S_c_locale, 0))
> > +      _S_initialize_once();
> >      return _S_c_locale;
> >    }
> 
> 
> I think this has a problem, which is handled correctly in
> src/c++11/locale_init.cc by checking _S_classic inside the
> _S_initialize_once function.

We check _S_c_locale here (it's just a pointer) instead of in
_S_initialize_once (), so I think the code is equivalent, no?

> If the first call to __gthread_once does fail then _S_once will not be
> changed. We will fall through to calling _S_initialize_once directly
> (which is not thread-safe) and set _S_c_locale.
> 
> The next time we call _S_initialize, __gthread_once will try to run
> again, and because _S_once was not changed, it might call
> _S_initialize_once() again, which writes to _S_c_locale again
> (possibly causing a data race).

Ah, yeah, so in the C++11 path the check for !_S_classic in
locale::_S_initialize is redundant.  But good spot.

> I don't think the slightly different code in src/c++11/locale_init.cc
> is different in order to handle __gthread_once failing, I think it's
> different because the effects of locale::facet::_S_initialize_once()
> and locale::_S_initialize_once() are different. One is safe to call
> more than once, and the other isn't.
> 
> I don't think we need to care about __gthread_once failing at all, do
> we? There are no error conditions for pthread_once, it always returns
> 0 (previous POSIX revisions said it could return EINVAL for an
> uninitialized pthread_once_t but that can't happen here as it's
> correctly initialized in src/c++11/locale.cc). Is the concern that it
> can fail for non-posix thread models? (I didn't check if any of them
> can actually fail)

The concern is that there are actual products out that break with the
new I/O initialization in libstdc++ for GCC13+ because they have bugs.
It's easy enough to work around those by the proposed patch (plus
correction for the above issue).  I suppose the comment in
locale::_S_initialize_once holds as well for the C++98 path.

The failure mode of the product is that it overrides pthread_once
but does nothing (not even indicate failure) when its pthread_*
override mechanism isn't initialized yet.  With libstdc++ from GCC13
we now use pthread_once "too early" and fail to initialize the locale
object.

Adjusted patch below.

OK after another round of testing?

Thanks,
Richard.


From 4e3fa2f4426a5a10d189587b63e4d7298c347b01 Mon Sep 17 00:00:00 2001
From: Richard Biener <rguenther@suse.de>
Date: Mon, 6 Nov 2023 11:31:40 +0100
Subject: [PATCH] libstdc++/112351 - deal with __gthread_once failure during
 locale init
To: gcc-patches@gcc.gnu.org

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.
---
 libstdc++-v3/src/c++98/locale.cc | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/libstdc++-v3/src/c++98/locale.cc b/libstdc++-v3/src/c++98/locale.cc
index d308140bab7..1ef0c394cd7 100644
--- a/libstdc++-v3/src/c++98/locale.cc
+++ b/libstdc++-v3/src/c++98/locale.cc
@@ -206,6 +206,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
   void
   locale::facet::_S_initialize_once()
   {
+    // Need to check this because we could get called once from
+    // _S_get_c_locale() when the program is single-threaded, and then again
+    // (via __gthread_once) when it's multi-threaded.
+    if (_S_c_locale)
+      return;
+
     // Initialize the underlying locale model.
     _S_create_c_locale(_S_c_locale, _S_c_name);
   }
@@ -216,12 +222,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 #ifdef __GTHREADS
     if (__gthread_active_p())
       __gthread_once(&_S_once, _S_initialize_once);
-    else
 #endif
-      {
-	if (!_S_c_locale)
-	  _S_initialize_once();
-      }
+    if (__builtin_expect (!_S_c_locale, 0))
+      _S_initialize_once();
     return _S_c_locale;
   }
 
-- 
2.35.3


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

* Re: [PATCH] libstdc++/112351 - deal with __gthread_once failure during locale init
  2023-11-06 12:16 ` Jakub Jelinek
@ 2023-11-06 12:17   ` Jonathan Wakely
  0 siblings, 0 replies; 7+ messages in thread
From: Jonathan Wakely @ 2023-11-06 12:17 UTC (permalink / raw)
  To: Jakub Jelinek; +Cc: Richard Biener, gcc-patches, libstdc++

On Mon, 6 Nov 2023 at 12:16, Jakub Jelinek <jakub@redhat.com> wrote:
>
> On Mon, Nov 06, 2023 at 11:52:08AM +0000, Richard Biener wrote:
> > 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.
> >
> > Bootstrapped and tested on x86_64-unknown-linux-gnu, OK for trunk?
> > And GCC 13 branch?
> >
> > Thanks,
> > Richard.
> >
> >       PR libstdc++/112351
> > libstdc++-v3/
> >       * src/c++98/locale.cc (locale::facet::_S_get_c_locale):
> >       Always perform non-threadsafe init when threadsafe init
> >       failed.
> > ---
> >  libstdc++-v3/src/c++98/locale.cc | 7 ++-----
> >  1 file changed, 2 insertions(+), 5 deletions(-)
> >
> > diff --git a/libstdc++-v3/src/c++98/locale.cc b/libstdc++-v3/src/c++98/locale.cc
> > index d308140bab7..e9bec1db3b6 100644
> > --- a/libstdc++-v3/src/c++98/locale.cc
> > +++ b/libstdc++-v3/src/c++98/locale.cc
> > @@ -216,12 +216,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
> >  #ifdef __GTHREADS
> >      if (__gthread_active_p())
> >        __gthread_once(&_S_once, _S_initialize_once);
> > -    else
> >  #endif
> > -      {
> > -     if (!_S_c_locale)
> > -       _S_initialize_once();
> > -      }
> > +    if (__builtin_expect (!_S_c_locale, 0))
> > +      _S_initialize_once();
> >      return _S_c_locale;
>
> Wouldn't it be better to just test __gthread_once return value
> #ifdef __THREADS
>   if ((!__gthread_active_p()
>        || __gthread_once(&_S_once, _S_initialize_once)))
> #endif
>     if (!_S_c_locale)
>       _S_initialize_once();
> ?


This still has the problem of calling the function twice, once because
__gthread_once fails and one because it succeeds.


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

* Re: [PATCH] libstdc++/112351 - deal with __gthread_once failure during locale init
       [not found] <20231106115212.6DCC23858C00@sourceware.org>
@ 2023-11-06 12:16 ` Jakub Jelinek
  2023-11-06 12:17   ` Jonathan Wakely
  0 siblings, 1 reply; 7+ messages in thread
From: Jakub Jelinek @ 2023-11-06 12:16 UTC (permalink / raw)
  To: Richard Biener; +Cc: gcc-patches, libstdc++, jwakely

On Mon, Nov 06, 2023 at 11:52:08AM +0000, Richard Biener wrote:
> 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.
> 
> Bootstrapped and tested on x86_64-unknown-linux-gnu, OK for trunk?
> And GCC 13 branch?
> 
> Thanks,
> Richard.
> 
> 	PR libstdc++/112351
> libstdc++-v3/
> 	* src/c++98/locale.cc (locale::facet::_S_get_c_locale):
> 	Always perform non-threadsafe init when threadsafe init
> 	failed.
> ---
>  libstdc++-v3/src/c++98/locale.cc | 7 ++-----
>  1 file changed, 2 insertions(+), 5 deletions(-)
> 
> diff --git a/libstdc++-v3/src/c++98/locale.cc b/libstdc++-v3/src/c++98/locale.cc
> index d308140bab7..e9bec1db3b6 100644
> --- a/libstdc++-v3/src/c++98/locale.cc
> +++ b/libstdc++-v3/src/c++98/locale.cc
> @@ -216,12 +216,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>  #ifdef __GTHREADS
>      if (__gthread_active_p())
>        __gthread_once(&_S_once, _S_initialize_once);
> -    else
>  #endif
> -      {
> -	if (!_S_c_locale)
> -	  _S_initialize_once();
> -      }
> +    if (__builtin_expect (!_S_c_locale, 0))
> +      _S_initialize_once();
>      return _S_c_locale;

Wouldn't it be better to just test __gthread_once return value
#ifdef __THREADS
  if ((!__gthread_active_p()
       || __gthread_once(&_S_once, _S_initialize_once)))
#endif
    if (!_S_c_locale)
      _S_initialize_once();
?

	Jakub


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

* Re: [PATCH] libstdc++/112351 - deal with __gthread_once failure during locale init
       [not found] <11125.123110606520900751@us-mta-255.us.mimecast.lan>
@ 2023-11-06 12:15 ` Jonathan Wakely
  2023-11-06 12:51   ` Richard Biener
  0 siblings, 1 reply; 7+ messages in thread
From: Jonathan Wakely @ 2023-11-06 12:15 UTC (permalink / raw)
  To: Richard Biener; +Cc: gcc-patches, libstdc++

On Mon, 6 Nov 2023 at 11:52, Richard Biener <rguenther@suse.de> wrote:
>
> 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.
>
> Bootstrapped and tested on x86_64-unknown-linux-gnu, OK for trunk?
> And GCC 13 branch?
>
> Thanks,
> Richard.
>
>         PR libstdc++/112351
> libstdc++-v3/
>         * src/c++98/locale.cc (locale::facet::_S_get_c_locale):
>         Always perform non-threadsafe init when threadsafe init
>         failed.
> ---
>  libstdc++-v3/src/c++98/locale.cc | 7 ++-----
>  1 file changed, 2 insertions(+), 5 deletions(-)
>
> diff --git a/libstdc++-v3/src/c++98/locale.cc b/libstdc++-v3/src/c++98/locale.cc
> index d308140bab7..e9bec1db3b6 100644
> --- a/libstdc++-v3/src/c++98/locale.cc
> +++ b/libstdc++-v3/src/c++98/locale.cc
> @@ -216,12 +216,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>  #ifdef __GTHREADS
>      if (__gthread_active_p())
>        __gthread_once(&_S_once, _S_initialize_once);
> -    else
>  #endif
> -      {
> -       if (!_S_c_locale)
> -         _S_initialize_once();
> -      }
> +    if (__builtin_expect (!_S_c_locale, 0))
> +      _S_initialize_once();
>      return _S_c_locale;
>    }


I think this has a problem, which is handled correctly in
src/c++11/locale_init.cc by checking _S_classic inside the
_S_initialize_once function.

If the first call to __gthread_once does fail then _S_once will not be
changed. We will fall through to calling _S_initialize_once directly
(which is not thread-safe) and set _S_c_locale.

The next time we call _S_initialize, __gthread_once will try to run
again, and because _S_once was not changed, it might call
_S_initialize_once() again, which writes to _S_c_locale again
(possibly causing a data race).

I don't think the slightly different code in src/c++11/locale_init.cc
is different in order to handle __gthread_once failing, I think it's
different because the effects of locale::facet::_S_initialize_once()
and locale::_S_initialize_once() are different. One is safe to call
more than once, and the other isn't.

I don't think we need to care about __gthread_once failing at all, do
we? There are no error conditions for pthread_once, it always returns
0 (previous POSIX revisions said it could return EINVAL for an
uninitialized pthread_once_t but that can't happen here as it's
correctly initialized in src/c++11/locale.cc). Is the concern that it
can fail for non-posix thread models? (I didn't check if any of them
can actually fail)


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

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

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-06 11:52 [PATCH] libstdc++/112351 - deal with __gthread_once failure during locale init Richard Biener
     [not found] <11125.123110606520900751@us-mta-255.us.mimecast.lan>
2023-11-06 12:15 ` Jonathan Wakely
2023-11-06 12:51   ` Richard Biener
2023-11-07 10:55     ` Richard Biener
2023-11-07 11:44     ` Jonathan Wakely
     [not found] <20231106115212.6DCC23858C00@sourceware.org>
2023-11-06 12:16 ` Jakub Jelinek
2023-11-06 12:17   ` Jonathan Wakely

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).