This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [PATCH] PR libstdc++/91910 fix data race in Debug Mode destructors
- From: Jonathan Wakely <jwakely at redhat dot com>
- To: libstdc++ at gcc dot gnu dot org, gcc-patches at gcc dot gnu dot org
- Cc: leon zadorin <leonleon77 at gmail dot com>, François Dumont <frs dot dumont at gmail dot com>
- Date: Thu, 26 Sep 2019 17:25:19 +0100
- Subject: Re: [PATCH] PR libstdc++/91910 fix data race in Debug Mode destructors
- References: <20190926131959.GA3393@redhat.com>
On 26/09/19 14:20 +0100, Jonathan Wakely wrote:
Fix data race when _Safe_iterator_base::_M_detach() runs concurrently with
the _Safe_container_base destructor.
PR libstdc++/91910
* src/c++11/debug.cc (_Safe_iterator_base::_M_detach()): Load pointer
atomically and lock the mutex before accessing the sequence.
(_Safe_iterator_base::_M_reset()): Clear _M_sequence atomically.
Tested x86_64-linux. I plan to commit this to trunk unless somebody
sees a problem with it.
We need the same change in the _Safe_local_iterator_base::_M_detach()
function, as in this new patch.
commit 3ec426b2f46e65eb6e4ce7ec05863a6f9c05dffc
Author: Jonathan Wakely <jwakely@redhat.com>
Date: Thu Sep 26 13:08:48 2019 +0100
PR libstdc++/91910 fix data race in Debug Mode destructors
Fix data race when _Safe_iterator_base::_M_detach() runs concurrently with
the _Safe_container_base destructor.
PR libstdc++/91910
* src/c++11/debug.cc (_Safe_iterator_base::_M_detach()): Load pointer
atomically and lock the mutex before accessing the sequence.
(_Safe_local_iterator_base::_M_detach()): Likewise.
(_Safe_iterator_base::_M_reset()): Clear _M_sequence atomically.
diff --git a/libstdc++-v3/src/c++11/debug.cc b/libstdc++-v3/src/c++11/debug.cc
index f5a49992efa..efd1a9e0254 100644
--- a/libstdc++-v3/src/c++11/debug.cc
+++ b/libstdc++-v3/src/c++11/debug.cc
@@ -381,10 +381,17 @@ namespace __gnu_debug
_Safe_iterator_base::
_M_detach()
{
- if (_M_sequence)
+ // This function can run concurrently with the sequence destructor,
+ // so there is a TOCTTOU race here: the sequence could be destroyed
+ // after we check that _M_sequence is not null. Use the pointer value
+ // to acquire the mutex (rather than via _M_sequence->_M_get_mutex()).
+ // If the sequence destructor runs between loading the pointer and
+ // locking the mutex, it will detach this iterator and set _M_sequence
+ // to null, and then _M_detach_single() will do nothing.
+ if (auto seq = __atomic_load_n(&_M_sequence, __ATOMIC_ACQUIRE))
{
- _M_sequence->_M_detach(this);
- _M_reset();
+ __gnu_cxx::__scoped_lock sentry(get_safe_base_mutex(seq));
+ _M_detach_single();
}
}
@@ -403,7 +410,7 @@ namespace __gnu_debug
_Safe_iterator_base::
_M_reset() throw ()
{
- _M_sequence = 0;
+ __atomic_store_n(&_M_sequence, (_Safe_sequence_base*)0, __ATOMIC_RELEASE);
_M_version = 0;
_M_prior = 0;
_M_next = 0;
@@ -466,10 +473,10 @@ namespace __gnu_debug
_Safe_local_iterator_base::
_M_detach()
{
- if (_M_sequence)
+ if (auto seq = __atomic_load_n(&_M_sequence, __ATOMIC_ACQUIRE))
{
- _M_get_container()->_M_detach_local(this);
- _M_reset();
+ __gnu_cxx::__scoped_lock sentry(get_safe_base_mutex(seq));
+ _M_detach_single();
}
}