0002-fix-condvar.patch 3.3 KB

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