From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 44F4138493CE for ; Thu, 22 Dec 2022 23:41:30 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 44F4138493CE Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1671752489; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=C+IHknN6J/KkEKTY2gz7GoofMAT+70tK+uw4YwVBqdQ=; b=TnY2RqPQZkkx0zvC82KJ8Wuqgtjijx2j0Q6pfsXSW0u5ncrJ/EA0vnDoIxpEGmc5rDAlEg 6O6TrFZpayTuwwmlfJCS7JnwyIs/FJDLmzyrBWyO552NiAcBacRZ1XhTjJOFxP3Mjpmd+K wRpc0koLbKP7EXokoswCGJqKxMa5CO0= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-171-EH6ffQ4YPwGGO94qjCIGOQ-1; Thu, 22 Dec 2022 18:41:13 -0500 X-MC-Unique: EH6ffQ4YPwGGO94qjCIGOQ-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 535343C0E200; Thu, 22 Dec 2022 23:41:13 +0000 (UTC) Received: from localhost (unknown [10.33.36.34]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1B2C42026D4B; Thu, 22 Dec 2022 23:41:13 +0000 (UTC) From: Jonathan Wakely 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 Message-Id: <20221222234112.772515-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-11.5 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,KAM_STOCKGEN,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: 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&)@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 + 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; @@ -76,7 +94,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 -- 2.38.1