From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1720) id 09C383858D32; Sun, 15 Jan 2023 16:07:01 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 09C383858D32 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1673798821; bh=xR/fEzBytf7JwcrW+Xt3X09G9MnPfWJwQHLmwcuyMDg=; h=From:To:Subject:Date:From; b=tZ/wevq/Apizsl+5usl9PC7xNtqcIEfvjZvSfUGLdtLCzJU29O+GTtpjTWPFPchET cc7o/4/sfzcRH4anfvUhBH2bbDVt/LMQj9/OXaPLqo0Z5Uo/jI7K+JccHWWZLWKsWe FHVJw8QNXHs/XwvD0ls7bOMhsUx2Y5+S3e1X8rcs= MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="utf-8" From: Francois Dumont To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r13-5180] libstdc++: [_GLIBCXX_DEBUG] Complete deadlock fix on safe iterators [PR108288] X-Act-Checkin: gcc X-Git-Author: =?utf-8?q?Fran=C3=A7ois_Dumont?= X-Git-Refname: refs/heads/master X-Git-Oldrev: 844190af178c210a6aff6b7eb4dd8c6a49210ff9 X-Git-Newrev: 5c9833d878602de20043a5b74e4c6acd33d5fb3e Message-Id: <20230115160701.09C383858D32@sourceware.org> Date: Sun, 15 Jan 2023 16:07:01 +0000 (GMT) List-Id: https://gcc.gnu.org/g:5c9833d878602de20043a5b74e4c6acd33d5fb3e commit r13-5180-g5c9833d878602de20043a5b74e4c6acd33d5fb3e Author: François Dumont Date: Mon Jan 9 18:48:30 2023 +0100 libstdc++: [_GLIBCXX_DEBUG] Complete deadlock fix on safe iterators [PR108288] Complete fix on all _Safe_iterator post-increment and post-decrement implementations and on _Safe_local_iterator. libstdc++-v3/ChangeLog: PR libstdc++/108288 * include/debug/safe_iterator.h (_Safe_iterator<>::operator++(int)): Extend deadlock fix to other iterator category. (_Safe_iterator<>::operator--(int)): Likewise. * include/debug/safe_local_iterator.h (_Safe_local_iterator<>::operator++(int)): Fix deadlock. * testsuite/util/debug/unordered_checks.h (invalid_local_iterator_pre_increment): New. (invalid_local_iterator_post_increment): New. * testsuite/23_containers/unordered_map/debug/invalid_local_iterator_post_increment_neg.cc: New test. * testsuite/23_containers/unordered_map/debug/invalid_local_iterator_pre_increment_neg.cc: New test. Diff: --- libstdc++-v3/include/debug/safe_iterator.h | 44 ++++++++-------------- libstdc++-v3/include/debug/safe_local_iterator.h | 14 ++----- .../invalid_local_iterator_post_increment_neg.cc | 16 ++++++++ .../invalid_local_iterator_pre_increment_neg.cc | 16 ++++++++ .../testsuite/util/debug/unordered_checks.h | 34 +++++++++++++++++ 5 files changed, 84 insertions(+), 40 deletions(-) diff --git a/libstdc++-v3/include/debug/safe_iterator.h b/libstdc++-v3/include/debug/safe_iterator.h index f9068eaf8d6..755da7dffe5 100644 --- a/libstdc++-v3/include/debug/safe_iterator.h +++ b/libstdc++-v3/include/debug/safe_iterator.h @@ -129,14 +129,6 @@ namespace __gnu_debug typename _Sequence::_Base::iterator, typename _Sequence::_Base::const_iterator>::__type _OtherIterator; - struct _Attach_single - { }; - - _Safe_iterator(_Iterator __i, _Safe_sequence_base* __seq, _Attach_single) - _GLIBCXX_NOEXCEPT - : _Iter_base(__i) - { _M_attach_single(__seq); } - public: typedef _Iterator iterator_type; typedef typename _Traits::iterator_category iterator_category; @@ -347,8 +339,9 @@ namespace __gnu_debug _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(base()++, this->_M_sequence, _Attach_single()); + _Safe_iterator __ret = *this; + ++*this; + return __ret; } // ------ Utilities ------ @@ -520,12 +513,6 @@ namespace __gnu_debug protected: typedef typename _Safe_base::_OtherIterator _OtherIterator; - typedef typename _Safe_base::_Attach_single _Attach_single; - - _Safe_iterator(_Iterator __i, _Safe_sequence_base* __seq, _Attach_single) - _GLIBCXX_NOEXCEPT - : _Safe_base(__i, __seq, _Attach_single()) - { } public: /// @post the iterator is singular and unattached @@ -609,9 +596,9 @@ namespace __gnu_debug _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 ------ @@ -640,9 +627,9 @@ namespace __gnu_debug _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; } // ------ Utilities ------ @@ -666,13 +653,6 @@ namespace __gnu_debug typedef _Safe_iterator<_OtherIterator, _Sequence, std::random_access_iterator_tag> _OtherSelf; - typedef typename _Safe_base::_Attach_single _Attach_single; - - _Safe_iterator(_Iterator __i, _Safe_sequence_base* __seq, _Attach_single) - _GLIBCXX_NOEXCEPT - : _Safe_base(__i, __seq, _Attach_single()) - { } - public: typedef typename _Safe_base::difference_type difference_type; typedef typename _Safe_base::reference reference; @@ -761,6 +741,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")); _Safe_iterator __ret = *this; ++*this; return __ret; @@ -785,6 +768,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")); _Safe_iterator __ret = *this; --*this; return __ret; diff --git a/libstdc++-v3/include/debug/safe_local_iterator.h b/libstdc++-v3/include/debug/safe_local_iterator.h index 6e3c4eb1505..1079ee8dc4f 100644 --- a/libstdc++-v3/include/debug/safe_local_iterator.h +++ b/libstdc++-v3/include/debug/safe_local_iterator.h @@ -84,14 +84,6 @@ namespace __gnu_debug typedef _Safe_local_iterator _Self; typedef _Safe_local_iterator<_OtherIterator, _Sequence> _OtherSelf; - struct _Attach_single - { }; - - _Safe_local_iterator(_Iterator __i, _Safe_sequence_base* __cont, - _Attach_single) noexcept - : _Iter_base(__i) - { _M_attach_single(__cont); } - public: typedef _Iterator iterator_type; typedef typename _Traits::iterator_category iterator_category; @@ -290,9 +282,9 @@ namespace __gnu_debug _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_local_iterator(base()++, this->_M_sequence, - _Attach_single()); + _Safe_local_iterator __ret = *this; + ++*this; + return __ret; } // ------ Utilities ------ diff --git a/libstdc++-v3/testsuite/23_containers/unordered_map/debug/invalid_local_iterator_post_increment_neg.cc b/libstdc++-v3/testsuite/23_containers/unordered_map/debug/invalid_local_iterator_post_increment_neg.cc new file mode 100644 index 00000000000..74005c3ec69 --- /dev/null +++ b/libstdc++-v3/testsuite/23_containers/unordered_map/debug/invalid_local_iterator_post_increment_neg.cc @@ -0,0 +1,16 @@ +// { dg-do run { target c++11 xfail *-*-* } } +// { dg-require-debug-mode "" } + +#include +#include + +void test01() +{ + __gnu_test::invalid_local_iterator_post_increment>(); +} + +int main() +{ + test01(); + return 0; +} diff --git a/libstdc++-v3/testsuite/23_containers/unordered_map/debug/invalid_local_iterator_pre_increment_neg.cc b/libstdc++-v3/testsuite/23_containers/unordered_map/debug/invalid_local_iterator_pre_increment_neg.cc new file mode 100644 index 00000000000..016cd1c6947 --- /dev/null +++ b/libstdc++-v3/testsuite/23_containers/unordered_map/debug/invalid_local_iterator_pre_increment_neg.cc @@ -0,0 +1,16 @@ +// { dg-do run { target c++11 xfail *-*-* } } +// { dg-require-debug-mode "" } + +#include +#include + +void test01() +{ + __gnu_test::invalid_local_iterator_pre_increment>(); +} + +int main() +{ + test01(); + return 0; +} diff --git a/libstdc++-v3/testsuite/util/debug/unordered_checks.h b/libstdc++-v3/testsuite/util/debug/unordered_checks.h index 655f16f199f..76ae05bbf8b 100644 --- a/libstdc++-v3/testsuite/util/debug/unordered_checks.h +++ b/libstdc++-v3/testsuite/util/debug/unordered_checks.h @@ -125,6 +125,40 @@ namespace __gnu_test VERIFY( *it == val ); } + template + void invalid_local_iterator_pre_increment() + { + typedef _Tp cont_type; + typedef typename cont_type::value_type cont_val_type; + typedef typename CopyableValueType::value_type val_type; + generate_unique gu; + + cont_type c; + for (size_t i = 0; i != 5; ++i) + c.insert(gu.build()); + + auto lit = c.begin(0); + for (size_t i = 0; i != 6; ++i) + ++lit; + } + + template + void invalid_local_iterator_post_increment() + { + typedef _Tp cont_type; + typedef typename cont_type::value_type cont_val_type; + typedef typename CopyableValueType::value_type val_type; + generate_unique gu; + + cont_type c; + for (size_t i = 0; i != 5; ++i) + c.insert(gu.build()); + + auto lit = c.begin(0); + for (size_t i = 0; i != 6; ++i) + lit++; + } + template void invalid_local_iterator_compare() {