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