From patchwork Wed May 25 02:29:17 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakob Hasse X-Patchwork-Id: 1635254 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=Fjux0FEk; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4L7FQR6M45z9sGS for ; Wed, 25 May 2022 12:29:50 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A5442382C5C5 for ; Wed, 25 May 2022 02:29:45 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A5442382C5C5 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1653445785; bh=qqq+pByGrsqmBAUXPh37eqJd634l11G6n1MySGYmWrI=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=Fjux0FEktx5f28VYXgkFuAWczaAotGDHv1Hw11jELgmgUvPlIEaXma5q1uAQTYQh/ WGgV8VD4tqJoPBVcykbPo7da4+grN6i7UBTAaR5Bap8WB8jYiFANySXOr8XkBI6U3Q 97uXXtKlw1Gn5FR5GfuphoYJo46jhv3p4oU38F1s= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from APC01-SG2-obe.outbound.protection.outlook.com (mail-sgaapc01on2130.outbound.protection.outlook.com [40.107.215.130]) by sourceware.org (Postfix) with ESMTPS id C9ECB3856082; Wed, 25 May 2022 02:29:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org C9ECB3856082 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=h05ebETas6DdPaE4mBpbjTDaKbPumrrsfvjFhdZS1OggOiCUC4pNoH9VvV340o/cjLKQV683q18LuIG7ctTzf+GxqC6yvh6dZQee1sBiynM9i9KYnQbtkKKUDH3SCKvmSsmLd7Ed3J/kFr2ihTPEf7gsoNhQ0GU9MJHZeLEEKLyMg4HgrXlweH73bC7epyd3pK+TLdzjk5gSgmCZpF3iVmPRRKDZp3KZGW+fjwLcul5K5nHklk0RH36r+XKfCM5awtLI/JI5AS9N4lcgvg8UcycsrQYv4cBNt3s93AkYt0xfJJF9ZdJkMw2CRCG3MCYL4y/tdizE/oP3X/EYNybWhQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=kLhob6t0pHZNqt0+tC9TynA4ko2Kb4HywTAoaCFcCwQ=; b=QpaolUGEbYjocKt4DWXKYTErwKhcWkgsP+0cpGR9cr2U9nr9leaAziVwTR6zm3LWPJ/+Ce/rmAccn7OENw+w5a/nf1CddkXWsR/qaX9AQ5nJu8iFnhUsZvNCNqzJg9YnU/p/2/Nt97ds5E/iwdSnbvK74Fyrk7lXU29RKVI+GibjTLKZG32tDhHnsooG0Yi8HyxK6FBNzBMRF9y4tZIwtKZBDpyR6c8sSh873k33jJm0awxXsdQRjB/zF56sHYmSDI2AxYPWzm3EXufWadJ7M/jYHQFtIhYWAJnC1oiSJ1NAsIyuWvo2ApjJO4mBgo97h99twNZkjkXwCZfA14iD8w== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=espressif.com; dmarc=pass action=none header.from=espressif.com; dkim=pass header.d=espressif.com; arc=none Received: from HK0PR04MB2530.apcprd04.prod.outlook.com (2603:1096:203:4e::12) by SL2PR04MB3388.apcprd04.prod.outlook.com (2603:1096:100:3e::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5293.13; Wed, 25 May 2022 02:29:17 +0000 Received: from HK0PR04MB2530.apcprd04.prod.outlook.com ([fe80::d052:f10c:f5e5:fc2]) by HK0PR04MB2530.apcprd04.prod.outlook.com ([fe80::d052:f10c:f5e5:fc2%3]) with mapi id 15.20.5273.023; Wed, 25 May 2022 02:29:17 +0000 To: "libstdc++@gcc.gnu.org" , gcc-patches Subject: [PATCH v3] libstdc++: fix pointer type exception catch [PR105387] Thread-Topic: [PATCH v3] libstdc++: fix pointer type exception catch [PR105387] Thread-Index: AQHYb9taULT9QOzJEkuXLkdY29/fXA== Date: Wed, 25 May 2022 02:29:17 +0000 Message-ID: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: yes X-MS-TNEF-Correlator: suggested_attachment_session_id: dcb33088-e7aa-ef35-609e-584e655adc4d x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: afd01c96-56e3-4b12-2d60-08da3df65dc7 x-ms-traffictypediagnostic: SL2PR04MB3388:EE_ x-microsoft-antispam-prvs: x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: 5V2tGrgjKukzeLhRV49F3uG1CXUXI+b3b6gYkLvy8Giau8p7hd6oyOUdytizfAcjZMCwGdNiapWQJtg8lQUcfJH6PwgTd+XoVVl0iEY0wiu9Aiydw1uroAlh7qDTHmGv5jgH9f3KvWduUTBcjuxHwtWvsH39ZvkK7VDIuL89bVdwE7A6WRz7O7WQjz1Z5szSVufoq/dS0D4tlV+KTggxL/EF2sY90JYbnyHqdKP8Hd2xLzbB6x6CETWTiE2omJHop229ls+DB57jAJoJtN1UtbWeGXRqqtznHgcGK/YvNN6yzQCZVqLqa3hlEfHJlhPre3IgJJ8gemsNPL4vwhMsWhO1xMVp39abvo3n99Qh7EFOQT8FbhtgkaieE8XYabGIYoVq6nCHXAbkW8p9iA/cdQ5LHmMZYDxGpHit9N+1PvDHtTRLkrbcX8bSY3NNjG6n9f+bXN64vm/vB6jN2I2b+KYXjAlVJmwJuqKkCl9KbJL0Nf1s4MGN46Qyqx6m+s7r+NNH+A5AN1qeYH2R56v0q66n+QbHSAsRIU96+Xon/5OXfeZrWIWQNMaz6z5+wzR0jgfJDzo9lAT7b1jdaApMpucDu7EimNRN0Is7ldXTmvMF5xXim9Igl5ZPO4gZaNiI6/nrNsemdDWswRJgLd6N4kqTeofGoNgP6Vrtlg2zPBKZLEZtqCqqiq9v10mNYbSv7eSA9qCsR2lJMyFGex1Pvg== x-forefront-antispam-report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:HK0PR04MB2530.apcprd04.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230001)(4636009)(366004)(9686003)(7696005)(316002)(33656002)(52536014)(6506007)(71200400001)(54906003)(107886003)(186003)(110136005)(86362001)(8936002)(2906002)(122000001)(38070700005)(508600001)(38100700002)(55016003)(99936003)(5660300002)(83380400001)(450100002)(66946007)(76116006)(8676002)(66446008)(64756008)(66476007)(4326008)(91956017)(66556008)(19627405001)(44832011); DIR:OUT; SFP:1102; x-ms-exchange-antispam-messagedata-chunkcount: 2 x-ms-exchange-antispam-messagedata-0: =?iso-8859-1?q?xoxeF9wgvTkDOIHhHUPL6vx?= =?iso-8859-1?q?WtA/nIQM6N1tjp0K5RI6vDXibbFXne1KR/hTitEGweEkGgrXdUKDJ9OCBc+G?= =?iso-8859-1?q?GJuw8V7H+UQgA+mDRmUkAG9peEAztcnFJTKgTwlaPOnMnQWc/Jrv3buXvNWY?= =?iso-8859-1?q?4zv5cTCjeY7NoiFBuZApsX175ELIBH6UqQDK0o+5xY0fZ61EgTfNSdQ9r1fI?= =?iso-8859-1?q?lkOxyH7M5+YVxIMro77rUAUU3b6wi5tGj7KzDV/E3a9eJzUs4ef5C88pAoLV?= =?iso-8859-1?q?QF2XdH8zMeri9xpd8zhtMpMaPKBHc0YbZPeOHRiPajX7QD/ntdP4Ic+UweDQ?= =?iso-8859-1?q?ZubkmQM3TPuvX+Rhe3Z89djo75wdf7+nCeSIbN7qufLwjyqJ6qkbyxYHltV0?= =?iso-8859-1?q?uua01fIffrtbt2yfMlRz7YRdGVg2Ssr4rxq45h8G2yV+fQtRfc6qpoTDtuTg?= =?iso-8859-1?q?C5UEjZFckWWkAL0xeOvqRHFoooHE159TO1colEeTeeRGVRSmo+WF2153TJjv?= =?iso-8859-1?q?O6pRH5IjeQhxg6+AI3NS92E/hCpRZFfobUe9DV09zb8R2ddqa0acqUVKEggD?= =?iso-8859-1?q?PjujEHnNd97um+eV5lw1NFPX7rN33/VfIvh1K+XFpnmCKCTMOiFIsBKqJNVG?= =?iso-8859-1?q?uoVsUqEXoSF9rWTAT6fNczzAD1ki28NqTxaOZ6tOB2Bf67HjqNBWEIOoJo5j?= =?iso-8859-1?q?dP0M/f1caIJGcVNaxvjIHfb8kptu9qJj3FkDqgJGxMCoqrygftOM9RKN9nA9?= =?iso-8859-1?q?R759pSFPjgfeaDU7sX9XRBudeTTnMJfvZhaQhqzjIZnPXm6xu4+nVzXwZ9AK?= =?iso-8859-1?q?Ius8I1PEAIMYBDXWJBf+P51I4facEslsLxJHGjeTAn0RGNq526xFqZPdki3E?= =?iso-8859-1?q?9xXJCBFojkSdW6zPqCjrsTbz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GyAs4t842?= =?iso-8859-1?q?y/Ov91LxqPI4Res75P4lBDhGydZlHV/acfoH8?= x-ms-exchange-antispam-messagedata-1: IQXjhCZ9OHpEdg== MIME-Version: 1.0 X-OriginatorOrg: espressif.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: HK0PR04MB2530.apcprd04.prod.outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: afd01c96-56e3-4b12-2d60-08da3df65dc7 X-MS-Exchange-CrossTenant-originalarrivaltime: 25 May 2022 02:29:17.0461 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 5faf27fd-3557-4294-9545-8ea74a409f39 X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: ApYQTN5mkvgapFhlsvIeThEl9mjgdNqakxPEl1w/VTSGL/IOa15GVfSHpWpHB5gi4rfeCRRieXGRQW+4CBWCIDoOjYzAgL18LH7w8Tjpqig= X-MS-Exchange-Transport-CrossTenantHeadersStamped: SL2PR04MB3388 X-Spam-Status: No, score=-11.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, HTML_MESSAGE, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_PASS, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE, URI_HEX autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-Content-Filtered-By: Mailman/MimeDel 2.1.29 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jakob Hasse via Gcc-patches From: Jakob Hasse Reply-To: Jakob Hasse Cc: Anton Maklakov , Ivan Grokhotkov Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" Hello, two weeks ago I submitted the second version of the patch PR105387 for the bug 105387. Now I added a pointer-to-member exception test just to make sure that it doesn't break in case RTTI is enabled. The test is disabled if RTTI is disabled. I didn't receive any feedback so far regarding the second version of the patch. Is there any issue preventing acceptance? I ran the conformance tests on libstdc++v3 by running make -j 18 check RUNTESTFLAGS=conformance.exp Results for the current version (only difference is the added pointer-to-member test): Without RTTI before applying patch: === libstdc++ Summary === # of expected passes 14560 # of unexpected failures 5 # of expected failures 95 # of unsupported tests 702 Without RTTI after applying patch: === libstdc++ Summary === # of expected passes 14562 # of unexpected failures 5 # of expected failures 95 # of unsupported tests 703 With RTTI before applying patch: === libstdc++ Summary === # of expected passes 14598 # of unexpected failures 2 # of expected failures 95 # of unsupported tests 683 With RTTI after applying patch: === libstdc++ Summary === # of expected passes 14602 # of unexpected failures 2 # of expected failures 95 # of unsupported tests 683 Given that the pointer-to-member test is disabled when RTTI is disabled, the results look logical to me. From 26004c6f26f4b2f3e664184767d861c7291f3a16 Mon Sep 17 00:00:00 2001 From: Jakob Hasse <0xjakob@users.noreply.github.com> Date: Tue, 26 Apr 2022 12:03:47 +0800 Subject: [PATCH] [PATCH] libstdc++: fix pointer type exception catch (no RTTI) [PR105387] PR libstdc++/105387 __pbase_type_info::__do_catch(), used to catch pointer type exceptions, did not check if the type info object to compare against is a pointer type info object before doing a static down-cast to a pointer type info object. If RTTI is disabled, this leads to the following situation: Since a pointer type info object has additional fields, they would end up being undefined if the actual type info object was not a pointer type info object. A simple check has been added before the down-cast happens. Note that a consequence of this check is that exceptions of type pointer-to-member cannot be caught anymore. In case RTTI is enabled, this does not seem to be a problem because RTTI-based checks would run before and prevent running into the bad down-cast. Hence, the fix is disabled if RTTI is enabled and exceptions of type pointer-to-member can still be caught. libstdc++-v3/ChangeLog: * libsupc++/pbase_type_info.cc (__do_catch): * testsuite/18_support/105387.cc: New test. Signed-off-by: Jakob Hasse --- libstdc++-v3/libsupc++/pbase_type_info.cc | 7 ++- libstdc++-v3/testsuite/18_support/105387.cc | 61 +++++++++++++++++++ .../18_support/exception_ptr/ptr_to_member.cc | 25 ++++++++ 3 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 libstdc++-v3/testsuite/18_support/105387.cc create mode 100644 libstdc++-v3/testsuite/18_support/exception_ptr/ptr_to_member.cc diff --git a/libstdc++-v3/libsupc++/pbase_type_info.cc b/libstdc++-v3/libsupc++/pbase_type_info.cc index 7e5720b84a3..934e049a4e0 100644 --- a/libstdc++-v3/libsupc++/pbase_type_info.cc +++ b/libstdc++-v3/libsupc++/pbase_type_info.cc @@ -74,7 +74,12 @@ __do_catch (const type_info *thr_type, // Therefore there must at least be a qualification conversion involved // But for that to be valid, our outer pointers must be const qualified. return false; - + +#if !__cpp_rtti + if (!thr_type->__is_pointer_p ()) + return false; +#endif + const __pbase_type_info *thrown_type = static_cast (thr_type); diff --git a/libstdc++-v3/testsuite/18_support/105387.cc b/libstdc++-v3/testsuite/18_support/105387.cc new file mode 100644 index 00000000000..5cec222c334 --- /dev/null +++ b/libstdc++-v3/testsuite/18_support/105387.cc @@ -0,0 +1,61 @@ +#include +#include +#include + +// Test cases for PR libstdc++/105387 + +// This test is to trigger undefined behavior if the bug 105387 is present +// in the code. Note, however, given that the bug is present, this test runs +// into undefined behavior which can also mean that it passes. +// It has been observed to fail quite reliably on x86_64-linux-gnu but only +// fail sporadically on Xtensa, depending on the code placement. +void portable_test() +{ + bool exception_thrown = false; + try { + throw std::runtime_error("test"); + } catch (const char *e) { + VERIFY(false); + } catch (const std::exception &e) { + exception_thrown = true; + } + VERIFY(exception_thrown); +} + +// This test relies on the types defined in the files typeinfo and cxxabi.h +// It is therefore less portable then the test case above but should be +// guaranteed to fail if the implementation has the bug 105387. +// +// This test case checks that __pbase_type_info::__do_catch() behaves +// correctly when called with a non-pointer type info object as argument. +// In particular, __pbase_type_info::__do_catch() should not cast +// the given type object into a pointer type and try to access the +// extended fields. + +void non_portable_test() +{ + // Create a zero-initialized buffer for allocation of the type object + unsigned char buffer [sizeof(__cxxabiv1::__fundamental_type_info) * 2] = {}; + + // Use placement-new to create the fundamental type info object in the + // first half of the buffer. Whenever that type info object will be + // casted to a pointer type info object, the extended fields of the + // pointer type info object will be in the second half of the buffer + // and hence be guaranteed zero. + __cxxabiv1::__fundamental_type_info *p_fund_info = + new(buffer) __cxxabiv1::__fundamental_type_info("fund_type"); + + __cxxabiv1::__pointer_type_info ptr_info("ptr_type", 0, p_fund_info); + + // __do_catch is declared protected in __pointer_type_info, but public in + // type_info, so we upcast it here + std::type_info *abstract_ptr_info = static_cast(&ptr_info); + VERIFY(abstract_ptr_info->__do_catch(p_fund_info, nullptr, 1) == false); +} + +int main() +{ + portable_test(); + non_portable_test(); + return 0; +} diff --git a/libstdc++-v3/testsuite/18_support/exception_ptr/ptr_to_member.cc b/libstdc++-v3/testsuite/18_support/exception_ptr/ptr_to_member.cc new file mode 100644 index 00000000000..dbe2dafebf0 --- /dev/null +++ b/libstdc++-v3/testsuite/18_support/exception_ptr/ptr_to_member.cc @@ -0,0 +1,25 @@ +#include + +// Test related to PR libstdc++/105387 +// Check that pointer-to-member type exceptions can still be caught with -frtti. +// { dg-require-effective-target rtti } + +void test_catch_ptr_to_member() +{ + bool exception_thrown = false; + struct X { int i; }; + try { + throw &X::i; + } + catch (const int X::*) { + exception_thrown = true; + } + + VERIFY(exception_thrown); +} + +int main() +{ + test_catch_ptr_to_member(); + return 0; +} -- 2.25.1