public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely@redhat.com>
To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org
Subject: [committed] libstdc++: Avoid recursion in __nothrow_wait_cv::wait [PR105730]
Date: Thu, 22 Dec 2022 23:41:12 +0000	[thread overview]
Message-ID: <20221222234112.772515-1-jwakely@redhat.com> (raw)

Tested x86_64-linux. Pushed to trunk. Backport to gcc-12 needed too.

-- >8 --

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<mutex>&)@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.
---
 .../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 e3a8b8403ca..3cef3bc0714 100644
--- a/libstdc++-v3/src/c++11/compatibility-condvar.cc
+++ b/libstdc++-v3/src/c++11/compatibility-condvar.cc
@@ -67,6 +67,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<std::__condvar std::condition_variable::*X>
+    struct cracker
+    { static std::__condvar std::condition_variable::* value; };
+
+  // Initializer for this static member also initializes __base_member.
+  template<std::__condvar std::condition_variable::*X>
+    std::__condvar std::condition_variable::*
+      cracker<X>::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<std::mutex>&) noexcept;
@@ -76,7 +94,9 @@ __attribute__((used))
 void
 __nothrow_wait_cv::wait(std::unique_lock<std::mutex>& 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
 
-- 
2.38.1


                 reply	other threads:[~2022-12-22 23:41 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221222234112.772515-1-jwakely@redhat.com \
    --to=jwakely@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=libstdc++@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).