From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by sourceware.org (Postfix) with ESMTPS id E2B1E3858427 for ; Fri, 26 Aug 2022 13:37:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org E2B1E3858427 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=oracle.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=oracle.com Received: from pps.filterd (m0246631.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 27QBOamx022902; Fri, 26 Aug 2022 13:37:17 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : references : in-reply-to : content-type : content-id : content-transfer-encoding : mime-version; s=corp-2022-7-12; bh=/N/WaziUuLFTalRQXjbHXyj88+nlCjXVSTjNVFZKvp8=; b=1drp1svNf8YZS8orPl3kSI2c+3KMnM6y3mo8Idbaxndx6jGBHlPNCqaKCLx5QzUx+UIu 5lFkvGES9otdoBgfakkIWbmggrQ5vqObFsmWG7J4XyxcfCncdifGlDyYw0aIMIAKgwlW 8knQT/RY21ztfAMIQoaY30fOCJbSe5lsVbEluO8nuFpLVeJsn9qMLG43e2pEfnI7/Sgx kiH1/kWc6/NkjpXb/Ma/RRnnZfQR5sHgycl/VCBB15kqpFU60ha+LmazCEu5MEnTgl3T 6FQjJ3xJlZ8JLqHae5X1mLBbzk0sjMo59yqEzWhBRaGrVsjAFcqTHdgdRVy1jt8RaZ5k KA== Received: from iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta03.appoci.oracle.com [130.35.103.27]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3j5awvqasx-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 26 Aug 2022 13:37:17 +0000 Received: from pps.filterd (iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (8.17.1.5/8.17.1.5) with ESMTP id 27QDDKcM028538; Fri, 26 Aug 2022 13:37:16 GMT Received: from nam10-dm6-obe.outbound.protection.outlook.com (mail-dm6nam10lp2108.outbound.protection.outlook.com [104.47.58.108]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 3j5n7d3r1q-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 26 Aug 2022 13:37:16 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Cl9BaTfSmlDytBHl/mYCCzlMC2Oa1jpZAPrnQ0q7ji+zovxoSTh5ViQE5zOENhSc9hKzCDF4/vMLSbC4233ueUqStTDt1U6jw59JVXOHujVhgDt5EiraClZCPXSWIm8Orgf9prXPxXG4PwJm7SbzgmKffwf72pYoltuuK0zdqgJUHFSl9AXDlArPZjh4WHY636baiKkcqzVb0qXE93WlV1V73y1LgDNp2jtR2EPnqkI5CmsAJCNOiyYM4VGqZemhyOtYCbEtE/pAPgymgmwQrdxFlD9twOoa3ve19Tnftq063SVCEHzLZFrZGIw6b6ttDh+1vqqNWsLVCrbVv/+o6Q== 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=/N/WaziUuLFTalRQXjbHXyj88+nlCjXVSTjNVFZKvp8=; b=Usl1bTcNpNd2H+2ECU8ZVauD5axip+pY2oEQAWigo74n205oiSR8oLxX2Wq4BwV4RCPJmENcuhnSYGyv3fT8IQxXf00sqwMnfE9aeBGnNAamgyg2kOo/1CUIJ47Cz9eTbUOhXibGoHbpbnLmHsRiuLFfaIAQ9InmFXowPwZOuiGQTfh1cnfaXweh5gidNqH00bX+FLwXQIpyDsWloPrKtxJzjZNB1Sjw60F3ahHiJxlV6OTDzLFc0rGAK5fh6wAkBX8X3KSbfqUWBDAVENoVGwbUsKiBTPl6zqlRLihIx5Hn/M8U4GEpumVDGdNP6pt7ux8DEu/Hsc/NuXo2BWD46Q== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=/N/WaziUuLFTalRQXjbHXyj88+nlCjXVSTjNVFZKvp8=; b=h7uO1s0KqJAolcnJ47jriwOuTftwWOWvJwrgVixdGoRdVrlzCGg7k85UNsxRl5LgnesQqDtGBOEDumRb0m2px0LtqNXmPnWq1Zt+obdUwhtl9oU3XCOc06HO5JUzYONWCklKjZtX3EU8MeocRE+W5GhV5FKE8kKYBO9k5cbs2Kw= Received: from CH2PR10MB4344.namprd10.prod.outlook.com (2603:10b6:610:af::19) by DM6PR10MB3579.namprd10.prod.outlook.com (2603:10b6:5:17f::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5566.15; Fri, 26 Aug 2022 13:37:14 +0000 Received: from CH2PR10MB4344.namprd10.prod.outlook.com ([fe80::84af:9275:f1d7:de1]) by CH2PR10MB4344.namprd10.prod.outlook.com ([fe80::84af:9275:f1d7:de1%6]) with mapi id 15.20.5566.016; Fri, 26 Aug 2022 13:37:14 +0000 From: Qing Zhao To: Richard Biener , Kees Cook CC: GCC Patches , jakub Jelinek , martin Sebor , "joseph@codesourcery.com" Subject: Re: [[GCC13][Patch][V3] 2/2] Use array_at_struct_end_p in __builtin_object_size [PR101836] Thread-Topic: [[GCC13][Patch][V3] 2/2] Use array_at_struct_end_p in __builtin_object_size [PR101836] Thread-Index: AQHYskdggyJoTAHI50W8Bc66arYwP63A7P6AgABQWIA= Date: Fri, 26 Aug 2022 13:37:13 +0000 Message-ID: <14B0C56E-AEBB-4C6B-B487-C6AF5DEC5505@oracle.com> References: <20220817144042.2931674-1-qing.zhao@oracle.com> <20220817144042.2931674-2-qing.zhao@oracle.com> In-Reply-To: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-mailer: Apple Mail (2.3608.120.23.2.7) x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: ca75ffde-f0e3-41f1-3a80-08da876815e2 x-ms-traffictypediagnostic: DM6PR10MB3579:EE_ x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: 1+Fd769rsqEBxAmyabdz9ttSb0PshftRwphLNgEwDyh34R3oAy22s44q6l2L5fKlB5vUiEVGiTqJYfkj0aU0av6T5wl3I8Xw4Ku3onOpvlkYp8psePPbtE8ZsNGR+vkuURmg0kcG2k/Y8Z+uyScXogWxV8wSOscJeT2lf/CZiNO7BBpl7p/0SrI7sxerHyuP1IiIFeIq2Z5IHOGDui8uheriEO08Sg/PBVY1vDt8mwX+cRhgJ2Cl3OFI97I9pDQhiakNU8H5V1lTfi/+XzDCNafuSNeHrkJ9419WMgAiv0sDFmn8YRoQCJYttlci62qISAUSdMerZE9QUeX58h5O+R2Dogl+ljLJEZwPQ/kg9lR13jxf9C0yxJDS9sOuYUTMpEPx0FZc0KSFGocJEzDlRs1qO56XIwNZ6e5WKKQZuKs8bQFfDS3L/0sXcpIuN6kaEDBSYWaXnFPIvTdJSkT+ouo/L940wLCzYmeQUEQvdO4W7fElFZHwnmPyuONrc7/m5PvyxpsRYobQNbVWTjnnECVb30ifoZ+yuOGJsp1iNY7l5m4ApxX0fR8WCwCcgDhwBkHG5ilOE1mSuhBnQ/xAVIfZK3e4eZlUhFXueyO0E+E6aKYCVMgV9UGGHMU/ci+5V73MreEOr/na05sqFchLAPLXDT88IE8a5HSTLn08moYS70OMpipzN0BXweUECqiGQbWzfEd4g7mqB+PVwrPaA9TMRi35hL7K/zVcxFVtSf8vTEtksZICjDsYEV0snn9lyw5CWc80YWITA2kqq3E07X2YYGnWjMZkTVI9by7oJKXnnVwojWARBYxDxFEjlCcNGHYI6xlXUHnczVldidFzT/8pOILIgNy2xN2EPlt+3J4= x-forefront-antispam-report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:CH2PR10MB4344.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230016)(136003)(396003)(376002)(346002)(366004)(39860400002)(2616005)(6486002)(186003)(316002)(76116006)(66556008)(66476007)(4326008)(66446008)(44832011)(83380400001)(91956017)(30864003)(66946007)(6506007)(6512007)(84970400001)(53546011)(36756003)(5660300002)(41300700001)(478600001)(71200400001)(8676002)(64756008)(33656002)(110136005)(122000001)(38070700005)(2906002)(8936002)(38100700002)(86362001)(54906003)(45980500001)(579004);DIR:OUT;SFP:1101; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?us-ascii?Q?a98NJZAzmhHpSzs6nLhWLRq4VoJIjD9wTDBnX23OUQj8RRZgStTSsrc3Oi4Y?= =?us-ascii?Q?CdvcgEpnk9aeXu/cTDj8QOplU86wHjd9LrtXlpFuli60LVLZgmLJOO3RW+PM?= =?us-ascii?Q?uUK5R5EQX3agNHkaFPxVAuZFWNLSogOZkulHyzOWfTJhE9Tu8Z6xbRWOar6p?= =?us-ascii?Q?JmFOn6l3yG9c4TlAH4sf/tIhcacD9b3zjLxYy3UMLZg502VrWFUsPc+7vKjR?= =?us-ascii?Q?RFr8zHGs/0s6eSv+3zopUrVhs4PHTD/XUEOP7LvmClXWGkwaGcsLAroBNrbl?= =?us-ascii?Q?SDnIoylbV7hcHL2Tme3Yrxd3PtrBT90gQcijwApcfwfTpzKjUTw/05B1Sjnb?= =?us-ascii?Q?JPUwAoqt4GSjqD12AMtpViy0kxYIOTyRHTwJPoFmfd9jl16C91sWLFKDLNux?= =?us-ascii?Q?sypM8pibMkOQVGvbHVZXnUrlf/qoKItq2gJYi7oK/o7dQ8zu8EgRD19RKiBy?= =?us-ascii?Q?98gGOVlMgAu1tyXKgk8qUyKvwBVrrI8xEfCJYu6n/hD9+/evVbEmdKLZAZrC?= =?us-ascii?Q?vRTqFiTYQLRsnyCXrhMJXGPr4oPfDB2rp8169RvJHyk9WEfiS/aCxhkfcLl5?= =?us-ascii?Q?iYASZNmOp2IaOG8tfONTNiDUt0PbrlL4zuNCpAbXLSmjwCqGdW5WqVoMeD6g?= =?us-ascii?Q?72ZQ6/HK9nf3h8Mlde/udgYhhRE0BTcgRAtqFjuGwJN0VNaYX2jqJUUdC+80?= =?us-ascii?Q?MXJUS4II6aIdC+MPELk0qFKnu4uC1ipocWCx3r8eobYwFGPIj+UtX4JYCqqP?= =?us-ascii?Q?+nC/Fe7B/FevJt8tgwsp/reKtzRMcB/umjuBo+FGTc7uJvdLJtscrcI2LgJ+?= =?us-ascii?Q?JD3SWmiVQbnHChvQIXP6JXlqHQkb6D4sl1W/WSMhK2aO9lxwpYQakDS6GEHW?= =?us-ascii?Q?JCRdpgo5H9ulOXPqbWVC59gELMtQJjG9S2gmnHOTJ4BAjXl9xrry6qISSFN5?= =?us-ascii?Q?eMhTIwUIVBhm34luJnnQwe89FmvvVaqhGmZU9urDAKu21SY+pNL2tTXtdM99?= =?us-ascii?Q?DNjAhUMHVYFVZNxzL1ASHCueJd3YaniaXYzglfPnCCnsidtSJtDUxFlFty13?= =?us-ascii?Q?+zjNpWJXmhYiqDXwEyuiONxCKt4ntovRBYVfcGHeRFN5BTl4ol1xtCiNNz0H?= =?us-ascii?Q?rEs6d7rzjjMG2oUl6YN2jwq7Et42F6WbDt0YyfxdvaiUni4nHSybnwHuvAXz?= =?us-ascii?Q?PH+9b+HI2M+DpyIUaygudEqk5gEoRjft3KqbYjy44m0Y/dVKYuuI3xcmj+pf?= =?us-ascii?Q?7J+GjWdhK1HFNTDrUQS0crX9+zHOIifND694COOaaOQfLRD38VU/wxr1F3Bw?= =?us-ascii?Q?IF3tn3BOF/m2IcHnrRlADQ9dBiI6eN18uy1yZJkQ4WSzdRnFzbdKYZvC2jFy?= =?us-ascii?Q?4yPs3iE7/sXHf6HvfyZtPRPRcCNosJcmZA2C2eF9caGgnIzYApKFjswLCBE5?= =?us-ascii?Q?BSAeJ+Z8cwWnvIYCX6pGipjdt5YZ2kmGAQNCsrxD8CVTpKpkaOnLSNIop+9q?= =?us-ascii?Q?QbOXbV6/d0nLjhyh/QZJPCoL6G1+HXbklyY9+m+rrtoL/urlxQmvNeciCK14?= =?us-ascii?Q?7ubWmC1zIdwX6aFT65wk6WmoQRut5JZtX2cCkxuZqxyMXTh9o6bW6X69jLnY?= =?us-ascii?Q?0i5aoFc68oSql6w2aw7Aq8k=3D?= Content-Type: text/plain; charset="us-ascii" Content-ID: Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: CH2PR10MB4344.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: ca75ffde-f0e3-41f1-3a80-08da876815e2 X-MS-Exchange-CrossTenant-originalarrivaltime: 26 Aug 2022 13:37:13.9530 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: Hg0cr0sY3/KM6gBX6cgPfmudchVbvA0MiKO8Iv82oSL1bFdyADJff+oRrh/EnvmbYWctllVWhVSeb0mcpe06Rw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR10MB3579 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.895,Hydra:6.0.517,FMLib:17.11.122.1 definitions=2022-08-26_06,2022-08-25_01,2022-06-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 bulkscore=0 suspectscore=0 phishscore=0 mlxlogscore=999 adultscore=0 mlxscore=0 malwarescore=0 spamscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2207270000 definitions=main-2208260057 X-Proofpoint-ORIG-GUID: XjpYhNJvWBBOTYa9hSspuOYBmqehLOZI X-Proofpoint-GUID: XjpYhNJvWBBOTYa9hSspuOYBmqehLOZI X-Spam-Status: No, score=-11.4 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_LOW,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,TXREP,T_SCC_BODY_TEXT_LINE 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: > On Aug 26, 2022, at 4:49 AM, Richard Biener wrote: >=20 > On Wed, 17 Aug 2022, Qing Zhao wrote: >=20 >> Use array_at_struct_end_p to determine whether the trailing array >> of a structure is flexible array member in __builtin_object_size. >=20 > With the discussion about what array_at_struct_end_p actually computes, > is this now the correct utility for __builtin_object_size or should > it use a stricter variant? My understanding is, -fstrict-flex-arrays should control all the GCC analys= is consistently, including __bultin_object_size.=20 For a standard conforming application, use -fstrict-flex-arrays=3D3 for the= most efficient code.=20 Kees, is it okay for Linux Kernel to add -fstrict-flex-arrays in order to e= nable FORTIFY_SOURCE work better for regular trailing arrays? Thanks. Qing >=20 >> gcc/ChangeLog: >>=20 >> PR tree-optimization/101836 >> * tree-object-size.cc (addr_object_size): Use array_at_struct_end_p >> to determine a flexible array member reference. >>=20 >> gcc/testsuite/ChangeLog: >>=20 >> PR tree-optimization/101836 >> * gcc.dg/pr101836.c: New test. >> * gcc.dg/pr101836_1.c: New test. >> * gcc.dg/pr101836_2.c: New test. >> * gcc.dg/pr101836_3.c: New test. >> * gcc.dg/pr101836_4.c: New test. >> * gcc.dg/pr101836_5.c: New test. >> * gcc.dg/strict-flex-array-5.c: New test. >> * gcc.dg/strict-flex-array-6.c: New test. >> --- >> gcc/testsuite/gcc.dg/pr101836.c | 60 ++++++++++++++++++++++ >> gcc/testsuite/gcc.dg/pr101836_1.c | 60 ++++++++++++++++++++++ >> gcc/testsuite/gcc.dg/pr101836_2.c | 60 ++++++++++++++++++++++ >> gcc/testsuite/gcc.dg/pr101836_3.c | 60 ++++++++++++++++++++++ >> gcc/testsuite/gcc.dg/pr101836_4.c | 60 ++++++++++++++++++++++ >> gcc/testsuite/gcc.dg/pr101836_5.c | 60 ++++++++++++++++++++++ >> gcc/testsuite/gcc.dg/strict-flex-array-5.c | 60 ++++++++++++++++++++++ >> gcc/testsuite/gcc.dg/strict-flex-array-6.c | 60 ++++++++++++++++++++++ >> gcc/tree-object-size.cc | 16 +++--- >> 9 files changed, 487 insertions(+), 9 deletions(-) >> create mode 100644 gcc/testsuite/gcc.dg/pr101836.c >> create mode 100644 gcc/testsuite/gcc.dg/pr101836_1.c >> create mode 100644 gcc/testsuite/gcc.dg/pr101836_2.c >> create mode 100644 gcc/testsuite/gcc.dg/pr101836_3.c >> create mode 100644 gcc/testsuite/gcc.dg/pr101836_4.c >> create mode 100644 gcc/testsuite/gcc.dg/pr101836_5.c >> create mode 100644 gcc/testsuite/gcc.dg/strict-flex-array-5.c >> create mode 100644 gcc/testsuite/gcc.dg/strict-flex-array-6.c >>=20 >> diff --git a/gcc/testsuite/gcc.dg/pr101836.c b/gcc/testsuite/gcc.dg/pr10= 1836.c >> new file mode 100644 >> index 000000000000..efad02cfe899 >> --- /dev/null >> +++ b/gcc/testsuite/gcc.dg/pr101836.c >> @@ -0,0 +1,60 @@ >> +/* -fstrict-flex-arrays is aliased with -ftrict-flex-arrays=3D3, which = is the >> + strictest, only [] is treated as flexible array. */=20 >> +/* PR tree-optimization/101836 */ >> +/* { dg-do run } */ >> +/* { dg-options "-O2 -fstrict-flex-arrays" } */ >> + >> +#include >> + >> +#define expect(p, _v) do { \ >> + size_t v =3D _v; \ >> + if (p =3D=3D v) \ >> + printf("ok: %s =3D=3D %zd\n", #p, p); \ >> + else \ >> + { \ >> + printf("WAT: %s =3D=3D %zd (expected %zd)\n", #p, p, v); \ >> + __builtin_abort (); \ >> + } \ >> +} while (0); >> + >> +struct trailing_array_1 { >> + int a; >> + int b; >> + int c[4]; >> +}; >> + >> +struct trailing_array_2 { >> + int a; >> + int b; >> + int c[1]; >> +}; >> + >> +struct trailing_array_3 { >> + int a; >> + int b; >> + int c[0]; >> +}; >> +struct trailing_array_4 { >> + int a; >> + int b; >> + int c[]; >> +}; >> + >> +void __attribute__((__noinline__)) stuff( >> + struct trailing_array_1 *normal, >> + struct trailing_array_2 *trailing_1, >> + struct trailing_array_3 *trailing_0, >> + struct trailing_array_4 *trailing_flex) >> +{ >> + expect(__builtin_object_size(normal->c, 1), 16); >> + expect(__builtin_object_size(trailing_1->c, 1), 4); >> + expect(__builtin_object_size(trailing_0->c, 1), 0); >> + expect(__builtin_object_size(trailing_flex->c, 1), -1); >> +} >> + >> +int main(int argc, char *argv[]) >> +{ >> + stuff((void *)argv[0], (void *)argv[0], (void *)argv[0], (void *)ar= gv[0]); >> + >> + return 0; >> +} >> diff --git a/gcc/testsuite/gcc.dg/pr101836_1.c b/gcc/testsuite/gcc.dg/pr= 101836_1.c >> new file mode 100644 >> index 000000000000..e2931ce1012e >> --- /dev/null >> +++ b/gcc/testsuite/gcc.dg/pr101836_1.c >> @@ -0,0 +1,60 @@ >> +/* -fstrict-flex-arrays=3D3 is the strictest, only [] is treated as >> + flexible array. */=20 >> +/* PR tree-optimization/101836 */ >> +/* { dg-do run } */ >> +/* { dg-options "-O2 -fstrict-flex-arrays=3D3" } */ >> + >> +#include >> + >> +#define expect(p, _v) do { \ >> + size_t v =3D _v; \ >> + if (p =3D=3D v) \ >> + printf("ok: %s =3D=3D %zd\n", #p, p); \ >> + else \ >> + { \ >> + printf("WAT: %s =3D=3D %zd (expected %zd)\n", #p, p, v); \ >> + __builtin_abort (); \ >> + } \ >> +} while (0); >> + >> +struct trailing_array_1 { >> + int a; >> + int b; >> + int c[4]; >> +}; >> + >> +struct trailing_array_2 { >> + int a; >> + int b; >> + int c[1]; >> +}; >> + >> +struct trailing_array_3 { >> + int a; >> + int b; >> + int c[0]; >> +}; >> +struct trailing_array_4 { >> + int a; >> + int b; >> + int c[]; >> +}; >> + >> +void __attribute__((__noinline__)) stuff( >> + struct trailing_array_1 *normal, >> + struct trailing_array_2 *trailing_1, >> + struct trailing_array_3 *trailing_0, >> + struct trailing_array_4 *trailing_flex) >> +{ >> + expect(__builtin_object_size(normal->c, 1), 16); >> + expect(__builtin_object_size(trailing_1->c, 1), 4); >> + expect(__builtin_object_size(trailing_0->c, 1), 0); >> + expect(__builtin_object_size(trailing_flex->c, 1), -1); >> +} >> + >> +int main(int argc, char *argv[]) >> +{ >> + stuff((void *)argv[0], (void *)argv[0], (void *)argv[0], (void *)ar= gv[0]); >> + >> + return 0; >> +} >> diff --git a/gcc/testsuite/gcc.dg/pr101836_2.c b/gcc/testsuite/gcc.dg/pr= 101836_2.c >> new file mode 100644 >> index 000000000000..78974187721f >> --- /dev/null >> +++ b/gcc/testsuite/gcc.dg/pr101836_2.c >> @@ -0,0 +1,60 @@ >> +/* When -fstrict-flex-arrays=3D2, only [] and [0] are treated as flexia= ble >> + arrays. */ >> +/* PR tree-optimization/101836 */ >> +/* { dg-do run } */ >> +/* { dg-options "-O2 -fstrict-flex-arrays=3D2" } */ >> + >> +#include >> + >> +#define expect(p, _v) do { \ >> + size_t v =3D _v; \ >> + if (p =3D=3D v) \ >> + printf("ok: %s =3D=3D %zd\n", #p, p); \ >> + else \ >> + { \ >> + printf("WAT: %s =3D=3D %zd (expected %zd)\n", #p, p, v); \ >> + __builtin_abort (); \ >> + } \ >> +} while (0); >> + >> +struct trailing_array_1 { >> + int a; >> + int b; >> + int c[4]; >> +}; >> + >> +struct trailing_array_2 { >> + int a; >> + int b; >> + int c[1]; >> +}; >> + >> +struct trailing_array_3 { >> + int a; >> + int b; >> + int c[0]; >> +}; >> +struct trailing_array_4 { >> + int a; >> + int b; >> + int c[]; >> +}; >> + >> +void __attribute__((__noinline__)) stuff( >> + struct trailing_array_1 *normal, >> + struct trailing_array_2 *trailing_1, >> + struct trailing_array_3 *trailing_0, >> + struct trailing_array_4 *trailing_flex) >> +{ >> + expect(__builtin_object_size(normal->c, 1), 16); >> + expect(__builtin_object_size(trailing_1->c, 1), 4); >> + expect(__builtin_object_size(trailing_0->c, 1), -1); >> + expect(__builtin_object_size(trailing_flex->c, 1), -1); >> +} >> + >> +int main(int argc, char *argv[]) >> +{ >> + stuff((void *)argv[0], (void *)argv[0], (void *)argv[0], (void *)ar= gv[0]); >> + >> + return 0; >> +} >> diff --git a/gcc/testsuite/gcc.dg/pr101836_3.c b/gcc/testsuite/gcc.dg/pr= 101836_3.c >> new file mode 100644 >> index 000000000000..0e69388e81fb >> --- /dev/null >> +++ b/gcc/testsuite/gcc.dg/pr101836_3.c >> @@ -0,0 +1,60 @@ >> +/* When -fstrict-flex-arrays=3D1, [], [0], and [1] are treated as flexi= ble >> + arrays. */ >> +/* PR tree-optimization/101836 */ >> +/* { dg-do run } */ >> +/* { dg-options "-O2 -fstrict-flex-arrays=3D1" } */ >> + >> +#include >> + >> +#define expect(p, _v) do { \ >> + size_t v =3D _v; \ >> + if (p =3D=3D v) \ >> + printf("ok: %s =3D=3D %zd\n", #p, p); \ >> + else \ >> + { \ >> + printf("WAT: %s =3D=3D %zd (expected %zd)\n", #p, p, v); \ >> + __builtin_abort (); \ >> + } \ >> +} while (0); >> + >> +struct trailing_array_1 { >> + int a; >> + int b; >> + int c[4]; >> +}; >> + >> +struct trailing_array_2 { >> + int a; >> + int b; >> + int c[1]; >> +}; >> + >> +struct trailing_array_3 { >> + int a; >> + int b; >> + int c[0]; >> +}; >> +struct trailing_array_4 { >> + int a; >> + int b; >> + int c[]; >> +}; >> + >> +void __attribute__((__noinline__)) stuff( >> + struct trailing_array_1 *normal, >> + struct trailing_array_2 *trailing_1, >> + struct trailing_array_3 *trailing_0, >> + struct trailing_array_4 *trailing_flex) >> +{ >> + expect(__builtin_object_size(normal->c, 1), 16); >> + expect(__builtin_object_size(trailing_1->c, 1), -1); >> + expect(__builtin_object_size(trailing_0->c, 1), -1); >> + expect(__builtin_object_size(trailing_flex->c, 1), -1); >> +} >> + >> +int main(int argc, char *argv[]) >> +{ >> + stuff((void *)argv[0], (void *)argv[0], (void *)argv[0], (void *)ar= gv[0]); >> + >> + return 0; >> +} >> diff --git a/gcc/testsuite/gcc.dg/pr101836_4.c b/gcc/testsuite/gcc.dg/pr= 101836_4.c >> new file mode 100644 >> index 000000000000..e0025aa9a7b5 >> --- /dev/null >> +++ b/gcc/testsuite/gcc.dg/pr101836_4.c >> @@ -0,0 +1,60 @@ >> +/* when -fstrict-flex-arrays=3D0, all trailing arrays are treated as >> + flexible arrays. */ >> +/* PR tree-optimization/101836 */ >> +/* { dg-do run } */ >> +/* { dg-options "-O2 -fstrict-flex-arrays=3D0" } */ >> + >> +#include >> + >> +#define expect(p, _v) do { \ >> + size_t v =3D _v; \ >> + if (p =3D=3D v) \ >> + printf("ok: %s =3D=3D %zd\n", #p, p); \ >> + else \ >> + { \ >> + printf("WAT: %s =3D=3D %zd (expected %zd)\n", #p, p, v); \ >> + __builtin_abort (); \ >> + } \ >> +} while (0); >> + >> +struct trailing_array_1 { >> + int a; >> + int b; >> + int c[4]; >> +}; >> + >> +struct trailing_array_2 { >> + int a; >> + int b; >> + int c[1]; >> +}; >> + >> +struct trailing_array_3 { >> + int a; >> + int b; >> + int c[0]; >> +}; >> +struct trailing_array_4 { >> + int a; >> + int b; >> + int c[]; >> +}; >> + >> +void __attribute__((__noinline__)) stuff( >> + struct trailing_array_1 *normal, >> + struct trailing_array_2 *trailing_1, >> + struct trailing_array_3 *trailing_0, >> + struct trailing_array_4 *trailing_flex) >> +{ >> + expect(__builtin_object_size(normal->c, 1), -1); >> + expect(__builtin_object_size(trailing_1->c, 1), -1); >> + expect(__builtin_object_size(trailing_0->c, 1), -1); >> + expect(__builtin_object_size(trailing_flex->c, 1), -1); >> +} >> + >> +int main(int argc, char *argv[]) >> +{ >> + stuff((void *)argv[0], (void *)argv[0], (void *)argv[0], (void *)ar= gv[0]); >> + >> + return 0; >> +} >> diff --git a/gcc/testsuite/gcc.dg/pr101836_5.c b/gcc/testsuite/gcc.dg/pr= 101836_5.c >> new file mode 100644 >> index 000000000000..0ad8bbf693ce >> --- /dev/null >> +++ b/gcc/testsuite/gcc.dg/pr101836_5.c >> @@ -0,0 +1,60 @@ >> +/* -fno-strict-flex-arrays is aliased to -fstrict-flex-arrays=3D0, >> + all trailing arrays are treated as flexible array. */ >> +/* PR tree-optimization/101836 */ >> +/* { dg-do run } */ >> +/* { dg-options "-O2 -fno-strict-flex-arrays" } */ >> + >> +#include >> + >> +#define expect(p, _v) do { \ >> + size_t v =3D _v; \ >> + if (p =3D=3D v) \ >> + printf("ok: %s =3D=3D %zd\n", #p, p); \ >> + else \ >> + { \ >> + printf("WAT: %s =3D=3D %zd (expected %zd)\n", #p, p, v); \ >> + __builtin_abort (); \ >> + } \ >> +} while (0); >> + >> +struct trailing_array_1 { >> + int a; >> + int b; >> + int c[4]; >> +}; >> + >> +struct trailing_array_2 { >> + int a; >> + int b; >> + int c[1]; >> +}; >> + >> +struct trailing_array_3 { >> + int a; >> + int b; >> + int c[0]; >> +}; >> +struct trailing_array_4 { >> + int a; >> + int b; >> + int c[]; >> +}; >> + >> +void __attribute__((__noinline__)) stuff( >> + struct trailing_array_1 *normal, >> + struct trailing_array_2 *trailing_1, >> + struct trailing_array_3 *trailing_0, >> + struct trailing_array_4 *trailing_flex) >> +{ >> + expect(__builtin_object_size(normal->c, 1), -1); >> + expect(__builtin_object_size(trailing_1->c, 1), -1); >> + expect(__builtin_object_size(trailing_0->c, 1), -1); >> + expect(__builtin_object_size(trailing_flex->c, 1), -1); >> +} >> + >> +int main(int argc, char *argv[]) >> +{ >> + stuff((void *)argv[0], (void *)argv[0], (void *)argv[0], (void *)ar= gv[0]); >> + >> + return 0; >> +} >> diff --git a/gcc/testsuite/gcc.dg/strict-flex-array-5.c b/gcc/testsuite/= gcc.dg/strict-flex-array-5.c >> new file mode 100644 >> index 000000000000..e474b9ec43fa >> --- /dev/null >> +++ b/gcc/testsuite/gcc.dg/strict-flex-array-5.c >> @@ -0,0 +1,60 @@ >> +/* test the combination of attribute strict_flex_arrays and option >> + -fstrict-flex-arrays: when both attribute and option specified, >> + attribute will have higher priority. */ >> +/* { dg-do run } */ >> +/* { dg-options "-O2 -fstrict-flex-arrays=3D3" } */ >> + >> +#include >> + >> +#define expect(p, _v) do { \ >> + size_t v =3D _v; \ >> + if (p =3D=3D v) \ >> + printf("ok: %s =3D=3D %zd\n", #p, p); \ >> + else \ >> + { \ >> + printf("WAT: %s =3D=3D %zd (expected %zd)\n", #p, p, v); \ >> + __builtin_abort (); \ >> + } \ >> +} while (0); >> + >> +struct trailing_array_1 { >> + int a; >> + int b; >> + int c[4] __attribute__ ((strict_flex_arrays (0))); >> +}; >> + >> +struct trailing_array_2 { >> + int a; >> + int b; >> + int c[1] __attribute__ ((strict_flex_arrays (1))); >> +}; >> + >> +struct trailing_array_3 { >> + int a; >> + int b; >> + int c[0] __attribute__ ((strict_flex_arrays (2))); >> +}; >> +struct trailing_array_4 { >> + int a; >> + int b; >> + int c[]; >> +}; >> + >> +void __attribute__((__noinline__)) stuff( >> + struct trailing_array_1 *normal, >> + struct trailing_array_2 *trailing_1, >> + struct trailing_array_3 *trailing_0, >> + struct trailing_array_4 *trailing_flex) >> +{ >> + expect(__builtin_object_size(normal->c, 1), -1); >> + expect(__builtin_object_size(trailing_1->c, 1), -1); >> + expect(__builtin_object_size(trailing_0->c, 1), -1); >> + expect(__builtin_object_size(trailing_flex->c, 1), -1); >> +} >> + >> +int main(int argc, char *argv[]) >> +{ >> + stuff((void *)argv[0], (void *)argv[0], (void *)argv[0], (void *)ar= gv[0]); >> + >> + return 0; >> +} >> diff --git a/gcc/testsuite/gcc.dg/strict-flex-array-6.c b/gcc/testsuite/= gcc.dg/strict-flex-array-6.c >> new file mode 100644 >> index 000000000000..b45e7b32f6d1 >> --- /dev/null >> +++ b/gcc/testsuite/gcc.dg/strict-flex-array-6.c >> @@ -0,0 +1,60 @@ >> +/* test the combination of attribute strict_flex_arrays and option >> + -fstrict-flex-arrays: when both attribute and option specified, >> + attribute will have higher priority. */ >> +/* { dg-do run } */ >> +/* { dg-options "-O2 -fstrict-flex-arrays=3D0" } */ >> + >> +#include >> + >> +#define expect(p, _v) do { \ >> + size_t v =3D _v; \ >> + if (p =3D=3D v) \ >> + printf("ok: %s =3D=3D %zd\n", #p, p); \ >> + else \ >> + { \ >> + printf("WAT: %s =3D=3D %zd (expected %zd)\n", #p, p, v); \ >> + __builtin_abort (); \ >> + } \ >> +} while (0); >> + >> +struct trailing_array_1 { >> + int a; >> + int b; >> + int c[4] __attribute__ ((strict_flex_arrays (1))); >> +}; >> + >> +struct trailing_array_2 { >> + int a; >> + int b; >> + int c[1] __attribute__ ((strict_flex_arrays (2))); >> +}; >> + >> +struct trailing_array_3 { >> + int a; >> + int b; >> + int c[0] __attribute__ ((strict_flex_arrays (3))); >> +}; >> +struct trailing_array_4 { >> + int a; >> + int b; >> + int c[]; >> +}; >> + >> +void __attribute__((__noinline__)) stuff( >> + struct trailing_array_1 *normal, >> + struct trailing_array_2 *trailing_1, >> + struct trailing_array_3 *trailing_0, >> + struct trailing_array_4 *trailing_flex) >> +{ >> + expect(__builtin_object_size(normal->c, 1), 16); >> + expect(__builtin_object_size(trailing_1->c, 1), 4); >> + expect(__builtin_object_size(trailing_0->c, 1), 0); >> + expect(__builtin_object_size(trailing_flex->c, 1), -1); >> +} >> + >> +int main(int argc, char *argv[]) >> +{ >> + stuff((void *)argv[0], (void *)argv[0], (void *)argv[0], (void *)ar= gv[0]); >> + >> + return 0; >> +} >> diff --git a/gcc/tree-object-size.cc b/gcc/tree-object-size.cc >> index 4eb454a4a33b..1f04cb80fd0a 100644 >> --- a/gcc/tree-object-size.cc >> +++ b/gcc/tree-object-size.cc >> @@ -604,9 +604,9 @@ addr_object_size (struct object_size_info *osi, cons= t_tree ptr, >> else if (var !=3D pt_var && TREE_CODE (pt_var) =3D=3D MEM_REF) >> { >> tree v =3D var; >> - /* For &X->fld, compute object size only if fld isn't the last >> - field, as struct { int i; char c[1]; } is often used instead >> - of flexible array member. */ >> + /* For &X->fld, compute object size if fld isn't a flexible arra= y >> + member. */ >> + bool is_flexible_array_mem_ref =3D false; >> while (v && v !=3D pt_var) >> switch (TREE_CODE (v)) >> { >> @@ -633,6 +633,7 @@ addr_object_size (struct object_size_info *osi, cons= t_tree ptr, >> v =3D NULL_TREE; >> break; >> } >> + is_flexible_array_mem_ref =3D array_at_struct_end_p (v); >> while (v !=3D pt_var && TREE_CODE (v) =3D=3D COMPONENT_REF) >> if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0))) >> !=3D UNION_TYPE >> @@ -645,12 +646,9 @@ addr_object_size (struct object_size_info *osi, con= st_tree ptr, >> && TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0))) >> =3D=3D RECORD_TYPE) >> { >> - tree fld_chain =3D DECL_CHAIN (TREE_OPERAND (v, 1)); >> - for (; fld_chain; fld_chain =3D DECL_CHAIN (fld_chain)) >> - if (TREE_CODE (fld_chain) =3D=3D FIELD_DECL) >> - break; >> - >> - if (fld_chain) >> + /* compute object size only if v is not a >> + flexible array member. */ >> + if (!is_flexible_array_mem_ref) >> { >> v =3D NULL_TREE; >> break; >>=20 >=20 > --=20 > Richard Biener > SUSE Software Solutions Germany GmbH, Frankenstrasse 146, 90461 Nuernberg= , > Germany; GF: Ivo Totev, Andrew Myers, Andrew McDonald, Boudien Moerman; > HRB 36809 (AG Nuernberg)