xref: /OK3568_Linux_fs/buildroot/package/gcc/12.2.0/0002-fix-condvar.patch (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1From ee4af2ed0b7322884ec4ff537564683c3749b813 Mon Sep 17 00:00:00 2001
2From: Jonathan Wakely <jwakely@redhat.com>
3Date: Thu, 22 Dec 2022 09:56:47 +0000
4Subject: [PATCH] libstdc++: Avoid recursion in __nothrow_wait_cv::wait
5 [PR105730]
6
7The commit r12-5877-g9e18a25331fa25 removed the incorrect
8noexcept-specifier from std::condition_variable::wait and gave the new
9symbol version @@GLIBCXX_3.4.30. It also redefined the original symbol
10std::condition_variable::wait(unique_lock<mutex>&)@GLIBCXX_3.4.11 as an
11alias for a new symbol, __gnu_cxx::__nothrow_wait_cv::wait, which still
12has the incorrect noexcept guarantee. That __nothrow_wait_cv::wait is
13just a wrapper around the real condition_variable::wait which adds
14noexcept and so terminates on a __forced_unwind exception.
15
16This doesn't work on uclibc, possibly due to a dynamic linker bug. When
17__nothrow_wait_cv::wait calls the condition_variable::wait function it
18binds to the alias symbol, which means it just calls itself recursively
19until the stack overflows.
20
21This change avoids the possibility of a recursive call by changing the
22__nothrow_wait_cv::wait function so that instead of calling
23condition_variable::wait it re-implements it. This requires accessing
24the private _M_cond member of condition_variable, so we need to use the
25trick of instantiating a template with the member-pointer of the private
26member.
27
28libstdc++-v3/ChangeLog:
29
30	PR libstdc++/105730
31	* src/c++11/compatibility-condvar.cc (__nothrow_wait_cv::wait):
32	Access private data member of base class and call its wait
33	member.
34
35Signed-off-by: Gleb Mazovetskiy <glex.spb@gmail.com>
36---
37 .../src/c++11/compatibility-condvar.cc        | 22 ++++++++++++++++++-
38 1 file changed, 21 insertions(+), 1 deletion(-)
39
40diff --git a/libstdc++-v3/src/c++11/compatibility-condvar.cc b/libstdc++-v3/src/c++11/compatibility-condvar.cc
41index e3a8b8403ca..3cef3bc0714 100644
42--- a/libstdc++-v3/src/c++11/compatibility-condvar.cc
43+++ b/libstdc++-v3/src/c++11/compatibility-condvar.cc
44@@ -67,6 +67,24 @@ _GLIBCXX_END_NAMESPACE_VERSION
45     && defined(_GLIBCXX_HAVE_SYMVER_SYMBOL_RENAMING_RUNTIME_SUPPORT)
46 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
47 {
48+namespace
49+{
50+  // Pointer-to-member for private std::condition_variable::_M_cond member.
51+  std::__condvar std::condition_variable::* __base_member;
52+
53+  template<std::__condvar std::condition_variable::*X>
54+    struct cracker
55+    { static std::__condvar std::condition_variable::* value; };
56+
57+  // Initializer for this static member also initializes __base_member.
58+  template<std::__condvar std::condition_variable::*X>
59+    std::__condvar std::condition_variable::*
60+      cracker<X>::value = __base_member = X;
61+
62+  // Explicit instantiation is allowed to access the private member.
63+  template class cracker<&std::condition_variable::_M_cond>;
64+}
65+
66 struct __nothrow_wait_cv : std::condition_variable
67 {
68   void wait(std::unique_lock<std::mutex>&) noexcept;
69@@ -76,7 +94,9 @@ __attribute__((used))
70 void
71 __nothrow_wait_cv::wait(std::unique_lock<std::mutex>& lock) noexcept
72 {
73-  this->condition_variable::wait(lock);
74+  // In theory this could be simply this->std::condition_variable::wait(lock)
75+  // but with uclibc that binds to the @GLIBCXX_3.4.11 symbol, see PR 105730.
76+  (this->*__base_member).wait(*lock.mutex());
77 }
78 } // namespace __gnu_cxx
79
80--
812.31.1
82
83