From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id 302C73858D35; Wed, 18 Jan 2023 15:14:55 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 302C73858D35 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1674054895; bh=FZ5A9WMqLLYNEvccqciNQQZVvvLzgElxy8SGtzeapJA=; h=From:To:Subject:Date:From; b=DARVCP+qdBGvOP00cP+iI7xmCEq0yfehPAlZKLackuVFy8gRBqTU8W7SW/kePCvfI Bxcjf3mAQPmUvRKOuVtVemyWWyZb1Wb8klpI/7RZwki1StfRmiLBqoW8uvedWEdQeg ekNHeMKPlE4R3e/PEhtBUs8NwiBhgKemal04lJWc= 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 r12-9051] libstdc++: Avoid recursion in __nothrow_wait_cv::wait [PR105730] X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/releases/gcc-12 X-Git-Oldrev: e80bb4015dd814e4cb3a9249a521df3f1cc79fc5 X-Git-Newrev: 937b0f05910bd814459ba9af0972041514274cd7 Message-Id: <20230118151455.302C73858D35@sourceware.org> Date: Wed, 18 Jan 2023 15:14:55 +0000 (GMT) List-Id: https://gcc.gnu.org/g:937b0f05910bd814459ba9af0972041514274cd7 commit r12-9051-g937b0f05910bd814459ba9af0972041514274cd7 Author: Jonathan Wakely Date: Thu Dec 22 09:56:47 2022 +0000 libstdc++: Avoid recursion in __nothrow_wait_cv::wait [PR105730] The commit r12-5877-g9e18a25331fa25 removed the incorrect noexcept-specifier from std::condition_variable::wait and gave the new symbol version @@GLIBCXX_3.4.30. It also redefined the original symbol std::condition_variable::wait(unique_lock&)@GLIBCXX_3.4.11 as an alias for a new symbol, __gnu_cxx::__nothrow_wait_cv::wait, which still has the incorrect noexcept guarantee. That __nothrow_wait_cv::wait is just a wrapper around the real condition_variable::wait which adds noexcept and so terminates on a __forced_unwind exception. This doesn't work on uclibc, possibly due to a dynamic linker bug. When __nothrow_wait_cv::wait calls the condition_variable::wait function it binds to the alias symbol, which means it just calls itself recursively until the stack overflows. This change avoids the possibility of a recursive call by changing the __nothrow_wait_cv::wait function so that instead of calling condition_variable::wait it re-implements it. This requires accessing the private _M_cond member of condition_variable, so we need to use the trick of instantiating a template with the member-pointer of the private member. libstdc++-v3/ChangeLog: PR libstdc++/105730 * src/c++11/compatibility-condvar.cc (__nothrow_wait_cv::wait): Access private data member of base class and call its wait member. (cherry picked from commit ee4af2ed0b7322884ec4ff537564683c3749b813) Diff: --- libstdc++-v3/src/c++11/compatibility-condvar.cc | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/libstdc++-v3/src/c++11/compatibility-condvar.cc b/libstdc++-v3/src/c++11/compatibility-condvar.cc index ea3e11efeda..45339c58aef 100644 --- a/libstdc++-v3/src/c++11/compatibility-condvar.cc +++ b/libstdc++-v3/src/c++11/compatibility-condvar.cc @@ -63,6 +63,24 @@ _GLIBCXX_END_NAMESPACE_VERSION && defined(_GLIBCXX_HAVE_SYMVER_SYMBOL_RENAMING_RUNTIME_SUPPORT) namespace __gnu_cxx _GLIBCXX_VISIBILITY(default) { +namespace +{ + // Pointer-to-member for private std::condition_variable::_M_cond member. + std::__condvar std::condition_variable::* __base_member; + + template + struct cracker + { static std::__condvar std::condition_variable::* value; }; + + // Initializer for this static member also initializes __base_member. + template + std::__condvar std::condition_variable::* + cracker::value = __base_member = X; + + // Explicit instantiation is allowed to access the private member. + template class cracker<&std::condition_variable::_M_cond>; +} + struct __nothrow_wait_cv : std::condition_variable { void wait(std::unique_lock&) noexcept; @@ -72,7 +90,9 @@ __attribute__((used)) void __nothrow_wait_cv::wait(std::unique_lock& lock) noexcept { - this->condition_variable::wait(lock); + // In theory this could be simply this->std::condition_variable::wait(lock) + // but with uclibc that binds to the @GLIBCXX_3.4.11 symbol, see PR 105730. + (this->*__base_member).wait(*lock.mutex()); } } // namespace __gnu_cxx