public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] [libstdc++] ensure mutex_pool survives _Safe_sequence_base
@ 2023-02-17  7:44 Alexandre Oliva
  2023-02-17  8:01 ` Alexandre Oliva
  0 siblings, 1 reply; 4+ messages in thread
From: Alexandre Oliva @ 2023-02-17  7:44 UTC (permalink / raw)
  To: gcc-patches, libstdc++


On vxworks, after destroying the semaphore used to implement a mutex,
__gthread_mutex_lock fails and __gnu_cxx::__mutex::lock calls
__throw_concurrence_lock_error.  Nothing ensures the mutex_pool
mutexes survive init-once objects containing _Safe_sequence_base.  If
such an object completes construction before mutex_pool
initialization, it will be registered for atexit destruction after the
mutex_pool mutexes, so the _M_detach_all() call in the
_Safe_sequence_base dtor will use already-destructed mutexes, and
basic_string/requirements/citerators_cc fails calling terminate.

This patch fixes this problem by ensuring the mutex pool completes
construction before any _Safe_sequence_base-containing object, so that
the mutex pool survives them all.

Regstrapped on x86_64-linux-gnu.
Tested on arm-vxworks7 (gcc-12) and arm-eabi (trunk).  Ok to install?

for  libstdc++-v3/ChangeLog

	* include/debug/safe_base.h (_Safe_sequence_base): Ensure
	the mutex pool survives *this.
---
 libstdc++-v3/include/debug/safe_base.h |   10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/libstdc++-v3/include/debug/safe_base.h b/libstdc++-v3/include/debug/safe_base.h
index 1dfa9f68b65b5..d4ba404cdac6e 100644
--- a/libstdc++-v3/include/debug/safe_base.h
+++ b/libstdc++-v3/include/debug/safe_base.h
@@ -203,7 +203,15 @@ namespace __gnu_debug
     // Initialize with a version number of 1 and no iterators
     _Safe_sequence_base() _GLIBCXX_NOEXCEPT
     : _M_iterators(0), _M_const_iterators(0), _M_version(1)
-    { }
+    {
+      // Make sure the mutex_pool machinery is initialized before any
+      // full object containing a _Safe_sequence_base completes
+      // construction, so that any local static mutexes in the mutex
+      // pool won't be destructed before our destructor runs;
+      // _M_detach_all could fail otherwise, on targets whose mutexes
+      // stop working after being destroyed.
+      (void)this->_M_get_mutex();
+    }
 
 #if __cplusplus >= 201103L
     _Safe_sequence_base(const _Safe_sequence_base&) noexcept

-- 
Alexandre Oliva, happy hacker                https://FSFLA.org/blogs/lxo/
   Free Software Activist                       GNU Toolchain Engineer
Disinformation flourishes because many people care deeply about injustice
but very few check the facts.  Ask me about <https://stallmansupport.org>

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

* Re: [PATCH] [libstdc++] ensure mutex_pool survives _Safe_sequence_base
  2023-02-17  7:44 [PATCH] [libstdc++] ensure mutex_pool survives _Safe_sequence_base Alexandre Oliva
@ 2023-02-17  8:01 ` Alexandre Oliva
  2023-02-17 11:03   ` Jonathan Wakely
  2023-02-17 20:33   ` François Dumont
  0 siblings, 2 replies; 4+ messages in thread
From: Alexandre Oliva @ 2023-02-17  8:01 UTC (permalink / raw)
  To: gcc-patches; +Cc: libstdc++

On Feb 17, 2023, Alexandre Oliva <oliva@adacore.com> wrote:

> On vxworks, after destroying the semaphore used to implement a mutex,
> __gthread_mutex_lock fails and __gnu_cxx::__mutex::lock calls
> __throw_concurrence_lock_error.  Nothing ensures the mutex_pool
> mutexes survive init-once objects containing _Safe_sequence_base.  If
> such an object completes construction before mutex_pool
> initialization, it will be registered for atexit destruction after the
> mutex_pool mutexes, so the _M_detach_all() call in the
> _Safe_sequence_base dtor will use already-destructed mutexes, and
> basic_string/requirements/citerators_cc fails calling terminate.

Here's an alternative approach, with zero runtime overhead.  Negative
overhead, if you count the time it would have taken to destruct the
mutex pool :-) But it fails to destruct them, which is presumably of no
consequence.

[libstdc++] do not destruct mutex_pool mutexes

[Copy of the paragraph quoted above omitted here]

This patch fixes this problem by ensuring the mutex pool mutexes are
constructed on demand, on a statically-allocated buffer, but never
destructed.

Regstrapped on x86_64-linux-gnu.
Tested on arm-vxworks7 (gcc-12) and arm-eabi (trunk).  Ok to install?

for  libstdc++-v3/ChangeLog

	* src/c++11/shared_ptr.cc (__gnu_internal::get_mutex):
	Avoid destruction of the mutex pool.
---
 libstdc++-v3/src/c++11/shared_ptr.cc |    6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/libstdc++-v3/src/c++11/shared_ptr.cc b/libstdc++-v3/src/c++11/shared_ptr.cc
index bc70134359c87..74e879e582896 100644
--- a/libstdc++-v3/src/c++11/shared_ptr.cc
+++ b/libstdc++-v3/src/c++11/shared_ptr.cc
@@ -36,7 +36,11 @@ namespace __gnu_internal _GLIBCXX_VISIBILITY(hidden)
   {
     // increase alignment to put each lock on a separate cache line
     struct alignas(64) M : __gnu_cxx::__mutex { };
-    static M m[mask + 1];
+    // Use a static buffer, so that the mutexes are not destructed
+    // before potential users (or at all)
+    static __attribute__ ((aligned(__alignof__(M))))
+      char buffer[(sizeof (M)) * (mask + 1)];
+    static M *m = new (buffer) M[mask + 1];
     return m[i];
   }
 }

-- 
Alexandre Oliva, happy hacker                https://FSFLA.org/blogs/lxo/
   Free Software Activist                       GNU Toolchain Engineer
Disinformation flourishes because many people care deeply about injustice
but very few check the facts.  Ask me about <https://stallmansupport.org>

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

* Re: [PATCH] [libstdc++] ensure mutex_pool survives _Safe_sequence_base
  2023-02-17  8:01 ` Alexandre Oliva
@ 2023-02-17 11:03   ` Jonathan Wakely
  2023-02-17 20:33   ` François Dumont
  1 sibling, 0 replies; 4+ messages in thread
From: Jonathan Wakely @ 2023-02-17 11:03 UTC (permalink / raw)
  To: Alexandre Oliva; +Cc: gcc-patches, libstdc++

[-- Attachment #1: Type: text/plain, Size: 2785 bytes --]

On Fri, 17 Feb 2023, 08:02 Alexandre Oliva via Libstdc++, <
libstdc++@gcc.gnu.org> wrote:

> On Feb 17, 2023, Alexandre Oliva <oliva@adacore.com> wrote:
>
> > On vxworks, after destroying the semaphore used to implement a mutex,
> > __gthread_mutex_lock fails and __gnu_cxx::__mutex::lock calls
> > __throw_concurrence_lock_error.  Nothing ensures the mutex_pool
> > mutexes survive init-once objects containing _Safe_sequence_base.  If
> > such an object completes construction before mutex_pool
> > initialization, it will be registered for atexit destruction after the
> > mutex_pool mutexes, so the _M_detach_all() call in the
> > _Safe_sequence_base dtor will use already-destructed mutexes, and
> > basic_string/requirements/citerators_cc fails calling terminate.
>
> Here's an alternative approach, with zero runtime overhead.  Negative
> overhead, if you count the time it would have taken to destruct the
> mutex pool :-) But it fails to destruct them, which is presumably of no
> consequence.
>

Agreed, I was going to suggest we immortalise them like this.



> [libstdc++] do not destruct mutex_pool mutexes
>
> [Copy of the paragraph quoted above omitted here]
>
> This patch fixes this problem by ensuring the mutex pool mutexes are
> constructed on demand, on a statically-allocated buffer, but never
> destructed.
>
> Regstrapped on x86_64-linux-gnu.
> Tested on arm-vxworks7 (gcc-12) and arm-eabi (trunk).  Ok to install?
>


OK, thanks.



> for  libstdc++-v3/ChangeLog
>
>         * src/c++11/shared_ptr.cc (__gnu_internal::get_mutex):
>         Avoid destruction of the mutex pool.
> ---
>  libstdc++-v3/src/c++11/shared_ptr.cc |    6 +++++-
>  1 file changed, 5 insertions(+), 1 deletion(-)
>
> diff --git a/libstdc++-v3/src/c++11/shared_ptr.cc
> b/libstdc++-v3/src/c++11/shared_ptr.cc
> index bc70134359c87..74e879e582896 100644
> --- a/libstdc++-v3/src/c++11/shared_ptr.cc
> +++ b/libstdc++-v3/src/c++11/shared_ptr.cc
> @@ -36,7 +36,11 @@ namespace __gnu_internal _GLIBCXX_VISIBILITY(hidden)
>    {
>      // increase alignment to put each lock on a separate cache line
>      struct alignas(64) M : __gnu_cxx::__mutex { };
> -    static M m[mask + 1];
> +    // Use a static buffer, so that the mutexes are not destructed
> +    // before potential users (or at all)
> +    static __attribute__ ((aligned(__alignof__(M))))
> +      char buffer[(sizeof (M)) * (mask + 1)];
> +    static M *m = new (buffer) M[mask + 1];
>      return m[i];
>    }
>  }
>
> --
> Alexandre Oliva, happy hacker                https://FSFLA.org/blogs/lxo/
>    Free Software Activist                       GNU Toolchain Engineer
> Disinformation flourishes because many people care deeply about injustice
> but very few check the facts.  Ask me about <https://stallmansupport.org>
>

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

* Re: [PATCH] [libstdc++] ensure mutex_pool survives _Safe_sequence_base
  2023-02-17  8:01 ` Alexandre Oliva
  2023-02-17 11:03   ` Jonathan Wakely
@ 2023-02-17 20:33   ` François Dumont
  1 sibling, 0 replies; 4+ messages in thread
From: François Dumont @ 2023-02-17 20:33 UTC (permalink / raw)
  To: Alexandre Oliva, gcc-patches; +Cc: libstdc++

On 17/02/23 09:01, Alexandre Oliva via Libstdc++ wrote:
> On Feb 17, 2023, Alexandre Oliva <oliva@adacore.com> wrote:
>
>> On vxworks, after destroying the semaphore used to implement a mutex,
>> __gthread_mutex_lock fails and __gnu_cxx::__mutex::lock calls
>> __throw_concurrence_lock_error.  Nothing ensures the mutex_pool
>> mutexes survive init-once objects containing _Safe_sequence_base.  If
>> such an object completes construction before mutex_pool
>> initialization, it will be registered for atexit destruction after the
>> mutex_pool mutexes, so the _M_detach_all() call in the
>> _Safe_sequence_base dtor will use already-destructed mutexes, and
>> basic_string/requirements/citerators_cc fails calling terminate.
> Here's an alternative approach, with zero runtime overhead.  Negative
> overhead, if you count the time it would have taken to destruct the
> mutex pool :-) But it fails to destruct them, which is presumably of no
> consequence.
>
> [libstdc++] do not destruct mutex_pool mutexes
>
> [Copy of the paragraph quoted above omitted here]
>
> This patch fixes this problem by ensuring the mutex pool mutexes are
> constructed on demand, on a statically-allocated buffer, but never
> destructed.
>
> Regstrapped on x86_64-linux-gnu.
> Tested on arm-vxworks7 (gcc-12) and arm-eabi (trunk).  Ok to install?
>
> for  libstdc++-v3/ChangeLog
>
> 	* src/c++11/shared_ptr.cc (__gnu_internal::get_mutex):
> 	Avoid destruction of the mutex pool.
> ---
>   libstdc++-v3/src/c++11/shared_ptr.cc |    6 +++++-
>   1 file changed, 5 insertions(+), 1 deletion(-)
>
> diff --git a/libstdc++-v3/src/c++11/shared_ptr.cc b/libstdc++-v3/src/c++11/shared_ptr.cc
> index bc70134359c87..74e879e582896 100644
> --- a/libstdc++-v3/src/c++11/shared_ptr.cc
> +++ b/libstdc++-v3/src/c++11/shared_ptr.cc
> @@ -36,7 +36,11 @@ namespace __gnu_internal _GLIBCXX_VISIBILITY(hidden)
>     {
>       // increase alignment to put each lock on a separate cache line
>       struct alignas(64) M : __gnu_cxx::__mutex { };
> -    static M m[mask + 1];
> +    // Use a static buffer, so that the mutexes are not destructed
> +    // before potential users (or at all)
I guess you meant 'before potential use'
> +    static __attribute__ ((aligned(__alignof__(M))))
> +      char buffer[(sizeof (M)) * (mask + 1)];
> +    static M *m = new (buffer) M[mask + 1];
>       return m[i];
>     }
>   }
>


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

end of thread, other threads:[~2023-02-17 20:33 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-17  7:44 [PATCH] [libstdc++] ensure mutex_pool survives _Safe_sequence_base Alexandre Oliva
2023-02-17  8:01 ` Alexandre Oliva
2023-02-17 11:03   ` Jonathan Wakely
2023-02-17 20:33   ` François Dumont

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