From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id D24043858D28; Fri, 6 Jan 2023 11:53:38 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D24043858D28 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1673006018; bh=rxdA4F0RIyUZvkR5tThp72tvEVi4uObBw2YQxt9r9Xw=; h=From:To:Subject:Date:From; b=C7KxVQ3kL8BPC0tPnim99vfO9XU6qe7wEwakqC9pJAgs7KW5ukJ/yRYfSujtXRqIi /0OCSsl5xhL2i5RjGMVymMt+7CPdRbohAh1+afPW3vpBnVbjExN+3yLNc3QmfXkj5y PPmQe65iFcFnUwF/P1wru6CT+tWJhAKYcrDOx3R8= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r13-5044] libstdc++: Fix deadlock in debug iterator increment [PR108288] X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/master X-Git-Oldrev: b990e80e39b41ec028d018eac7d2d040a537b681 X-Git-Newrev: b9479ddc7a28fb672ca67304a67d66524d8200a4 Message-Id: <20230106115338.D24043858D28@sourceware.org> Date: Fri, 6 Jan 2023 11:53:38 +0000 (GMT) List-Id: https://gcc.gnu.org/g:b9479ddc7a28fb672ca67304a67d66524d8200a4 commit r13-5044-gb9479ddc7a28fb672ca67304a67d66524d8200a4 Author: Jonathan Wakely Date: Thu Jan 5 16:23:51 2023 +0000 libstdc++: Fix deadlock in debug iterator increment [PR108288] With -fno-elide-constructors the debug iterator post-increment and post-decrement operators are susceptible to deadlock. They take a mutex lock and then return a temporary, which also attempts to take a lock to attach itself to the sequence. If the return value and *this happen to collide and use the same mutex from the pool, then you get a deadlock trying to lock a mutex that is already held by the current thread. The solution is to construct the return value before taking the lock. The copy constructor and pre-inc/pre-dec operators already manage locks correctly, without deadlock, so just implement post-inc/post-dec in the conventional way, taking a copy then modifying *this, then returning the copy. libstdc++-v3/ChangeLog: PR libstdc++/108288 * include/debug/safe_iterator.h (_Safe_iterator::operator++(int)) (_Safe_iterator::operator--(int)): Do not hold lock around construction of return value. Diff: --- libstdc++-v3/include/debug/safe_iterator.h | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/libstdc++-v3/include/debug/safe_iterator.h b/libstdc++-v3/include/debug/safe_iterator.h index 117dc93de60..f9068eaf8d6 100644 --- a/libstdc++-v3/include/debug/safe_iterator.h +++ b/libstdc++-v3/include/debug/safe_iterator.h @@ -761,12 +761,9 @@ namespace __gnu_debug _Safe_iterator operator++(int) _GLIBCXX_NOEXCEPT { - _GLIBCXX_DEBUG_VERIFY(this->_M_incrementable(), - _M_message(__msg_bad_inc) - ._M_iterator(*this, "this")); - __gnu_cxx::__scoped_lock __l(this->_M_get_mutex()); - return _Safe_iterator(this->base()++, this->_M_sequence, - _Attach_single()); + _Safe_iterator __ret = *this; + ++*this; + return __ret; } // ------ Bidirectional iterator requirements ------ @@ -788,12 +785,9 @@ namespace __gnu_debug _Safe_iterator operator--(int) _GLIBCXX_NOEXCEPT { - _GLIBCXX_DEBUG_VERIFY(this->_M_decrementable(), - _M_message(__msg_bad_dec) - ._M_iterator(*this, "this")); - __gnu_cxx::__scoped_lock __l(this->_M_get_mutex()); - return _Safe_iterator(this->base()--, this->_M_sequence, - _Attach_single()); + _Safe_iterator __ret = *this; + --*this; + return __ret; } // ------ Random access iterator requirements ------