From patchwork Tue Jul 19 14:11:19 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Li, Pan2 via Gcc-patches" X-Patchwork-Id: 59 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a98:d5ce:0:b0:178:cc93:bf7d with SMTP id g14csp2475566eik; Tue, 19 Jul 2022 07:12:37 -0700 (PDT) X-Google-Smtp-Source: AGRyM1tmPmJhfX2g1fKvGmBrimQRoYluvMUm0EkPUc/WJ645OzKi5TEzmpLGg0hl1gR4HJeGliHG X-Received: by 2002:a17:907:7290:b0:72b:3799:624c with SMTP id dt16-20020a170907729000b0072b3799624cmr30784548ejc.108.1658239957025; Tue, 19 Jul 2022 07:12:37 -0700 (PDT) Received: from sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id dk25-20020a0564021d9900b0042dc61e68d0si17930827edb.137.2022.07.19.07.12.36 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Jul 2022 07:12:37 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=pvePZugt; arc=fail (signature failed); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 1B66F3860002 for ; Tue, 19 Jul 2022 14:12:28 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1B66F3860002 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1658239948; bh=A4IWMsdLN24GJLPa75uFfku3iR21WAU01uUVtE61m+4=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=pvePZugtDiYvy0mjHC+pztQbWMin2Bfy6uu5n+P/RJv/HKpoIZ1IlQshp0Yv9LjWV hRAy1EcmnvdSorkz5A/v3MergTRGMuO9KNYWrHje9+qVRxOXHbsO1gdTgmzbV6lZuV 2+TEqrUKWysM3lLX5Jdj71pYCEYk3Qy9CMkBXaiI= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by sourceware.org (Postfix) with ESMTPS id 8972C3858439 for ; Tue, 19 Jul 2022 14:11:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 8972C3858439 Received: from pps.filterd (m0246627.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 26JDBFSd017914; Tue, 19 Jul 2022 14:11:24 GMT Received: from phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta01.appoci.oracle.com [138.1.114.2]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3hbkx0xju5-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 19 Jul 2022 14:11:23 +0000 Received: from pps.filterd (phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (8.17.1.5/8.17.1.5) with ESMTP id 26JC9xMT016422; Tue, 19 Jul 2022 14:11:22 GMT Received: from nam04-bn8-obe.outbound.protection.outlook.com (mail-bn8nam04lp2040.outbound.protection.outlook.com [104.47.74.40]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 3hc1emk55h-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 19 Jul 2022 14:11:22 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=mPuXS1kyedIW+Yl1v+CXgnG+nrYhWafAtuWAtioqgNuA9u/ec+tdRMgPr54lF2UGJ8jxz2PY7uTE/viq5Y4IlbeoNzsPtOSV0VTfM+uk5Fgms3UBeQJSCGQzRJue3GchQ936Wfn9ZGXc1URrNH5R8Lf0Iw2RGjKqhbJkwnCsWwNEqdfRNWqQ0A2lgi69qxY+r0RTBycJRhx3CDo+Sgt633ThApfAZSSiveqyGM6HfKtXM4YONl60+13/CFuEyKY7sTZgXlfmRkWKFRXWdyG/Rz/tA3Idc2N0P3w/+rYNkrQFxz2XxNyLUtNyjDz/+ps+d4RPA/LSPh9AoPuA7pUdsg== 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=A4IWMsdLN24GJLPa75uFfku3iR21WAU01uUVtE61m+4=; b=DSMuqUBy0n1F+hm3D81jlW0SbLYW3Ssngu+ZdpZFgki6hLBzR4lZEJ/+3W/p8Q34JAj0Ld1HYRdF7BwLyzYYyNRWvGLXwlJ8wuMQG8wonGi7MNW7aZtdAN58pnatgT8RQHdFLKesVp/1WsIH0pJxglchScRhM48MnpVT8mWgq6rH2YW87KXHyZz6jI+5NsaWY3kYL2XUYv2uftOB4cBg8mAcDpADMbF0bybU6UiNdyQC7nIXfLQwtYzUOMB8k5unOuhuX43fGesJdVWs7sz8rEcPSIlFCfkJBHANP+/X6o8gdbI8L5+gULJaJmc/Gc1dpfvV5mR9I9OTAPlXaCgBfA== 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 Received: from CH2PR10MB4344.namprd10.prod.outlook.com (2603:10b6:610:af::19) by MWHPR10MB1886.namprd10.prod.outlook.com (2603:10b6:300:10e::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5438.17; Tue, 19 Jul 2022 14:11:19 +0000 Received: from CH2PR10MB4344.namprd10.prod.outlook.com ([fe80::2d14:50e4:3bb:4e30]) by CH2PR10MB4344.namprd10.prod.outlook.com ([fe80::2d14:50e4:3bb:4e30%9]) with mapi id 15.20.5438.023; Tue, 19 Jul 2022 14:11:19 +0000 To: gcc-patches Paul A Clarke via Subject: [GCC13][Patch][V2][2/2]Add a new option -fstrict-flex-array[=n] and attribute strict_flex_array(n) and use it in PR101836 Thread-Topic: [GCC13][Patch][V2][2/2]Add a new option -fstrict-flex-array[=n] and attribute strict_flex_array(n) and use it in PR101836 Thread-Index: AQHYm3lqhCG5z7mY8Eyr0tWPjjd+9w== Date: Tue, 19 Jul 2022 14:11:19 +0000 Message-ID: <1D72918C-4D6A-4583-AF14-288757C04DD0@oracle.com> 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: 6d217f46-64f4-4238-f43c-08da69908d6b x-ms-traffictypediagnostic: MWHPR10MB1886:EE_ x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: 6vU8d4ox8KXrGY7Hja3awvm4LZTXzdRfBIVqPo6EpKaVnbKd+eBmJIKskh0uuC+2DgA1McWQKyUr3Sg7khw4gf13DTr67oGQ7P0tVGUhSXxAiBqz2e8aageD157F4+Qbs1zhAKMI2gi6tiu6KA+Ir2MQGyfTbeSGXbug3oZK+Ehvjtrg3WhbiIrvKWzt9B0uJPMPmS+LRHpSCr0YSt7G3xO6uN+CjSk2uBioQxGdCpVaZqzcvSvLhx7RpR5Hri3xhsILHfY3/04coxlv/pmX4x0+WMj8F0mS+Bl6oEl7vV4VCI8r1vLnlnAwP7sAoPSxw5bQsG1W8eenvqpkxSyBtJcr9HuN6RiFvdeoN2WL7QsJ1MZ5rvcYMEzJfbvVVS+Muo/DLpjMLr2gMWGphTqi2fH2rHlfs0DOp0M2iv+HCi7CIGtTuq/i34UoEOk19kwLKXe3TLvBDNtIsExd0WDEA9GkP4Y+T3t8nayK7ueK6dogx/WNuVDJBIjlZWZqLSRahiA3Ugfg+tIGUIV5Jyi8F7ev4otUJiiiYEbqsiqx51qkA8/3Hh9l76XaRmHCcEqqkbHWXCtM2ChODSa6FTjxgnHnwZamOyUBSroN5GL2edpKPswCc4GiwwAd7cig5RVV9oK84d9TIPgvUXeUfRiMA2YX4w7MEGaI+Lwc86j6sy0kR235pWlA1taILewfhU7X16OOUld9qGzbOJ+y+iY/zRctSHUIgyvH7H5s3Nw57kGrOfOK3N5tMNInaNsb1IceX3l/6MSaWJp1JP+1w5xZpZv0YGKdnOl49VBzoAubyakF56wxUz1jXpCSKp4qLqA+IsV2+jPRgWXbt05vPLro0uxMo+MF8do+al8roPnl2W8FbrW15XcDkqMa6dvvo5xcTMahztIi4Kva9//+0FEQvw== 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)(376002)(366004)(136003)(39860400002)(396003)(346002)(38100700002)(83380400001)(71200400001)(186003)(91956017)(6916009)(54906003)(66946007)(76116006)(36756003)(316002)(66446008)(8676002)(64756008)(4326008)(66556008)(66476007)(478600001)(84970400001)(8936002)(5660300002)(53546011)(6512007)(2616005)(6506007)(44832011)(122000001)(6486002)(33656002)(41300700001)(2906002)(38070700005)(86362001)(30864003)(45980500001); DIR:OUT; SFP:1101; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: Nvskd5VXMVyRDzzEbXPu3THNUfCtnvAZ+J7EsfaWGbiEnN/F5/q6+j5/X2vEnyftPm0UHFasq+/Rqe2B0v5Euk+LyjMViwoZitf0NbpuWfTRRXuIP/9rVu7okkiqYdDf31TXbCUoiMbLvoCpsFhe21ZuXaM7guDgtgm1z/GDcWcnLKmmHFFmidh2snU1uy7DudVKoitOEH7XbV65dMVSyWPuch+xr6ysknsY7gWuIrNade4Z9IZ/qSdbuJdbrWtRaJKmr9h2QXV4aN3pJeKGw2pQs2f5DJce19V0BZhiRfUADxMYQjcdDT+Mmkw46OahhaaZ9VJzL8RdlBYta6OCP1oCfC5UKBTK9hBsdCiOs0Xoay6LRuMPOGHm3PwDvAqcdBgc6Ctx0+mLw3mkwWj0gglCVCLxKMvQJ2rwMSsT9hYJKrry4hOFvgZqyPdn9HZsinQUv97v+zwF6Vofqr9mzFz55tPuWjIAN7iJDaMAOb4C0omhAljyBtKiaz2NZ7G0hc75dIPLTgmt+RKIm++Y/D1Ydaf6M2uH4WUTlr9m4EDul38oIAP3UHmUdRw4SLFLMTKVaNGv69eTKAp9Q1dz9ZEj+mUJdacr8PZY3Q/SzvAwvcyaAwJD9VuoocxO3chpa3DWefxnS1WT5CRUxX9hYfzgf9Zb8ymoyUezfAAhmCN/qLQ8h38oaBCvaILX94wJw0igGTwFENzec3+1JIc1rdQmT20Xt1beqGR9wUsve4WaID5tmC6Qf4ibQpm7GaYXRBJOJNFi/RnX8KOQ7Ymp1hkVWr7Mw6Gu4DmFhNbP6ewltTByZTHdum01NfRAMtklKL8eL0GZxajSGzwR4aSYeRt/OPFDk74pG2WWBopcPU784xUNwSOBOw0aNYyTFyzv9oh5oryPmLEwYQImmn5zD/gjNu6NbOmtNcn2tcBTPzYeZusZojHPyiyUjG0rbEpLadyKdv8PbiSqqdwo9DFuzgzsE6qHRy1wL8nlorD8BbGHBGHC2xFxYwPMvjxtW8yotfJ2VeqrbRjwHbYaFnpRr79PhTiqCepNvmDitGo0RD2nfwpeYH/JKT4Zfra1zIUlcvqSipDf80kXFHH0w9a1a0XSZ6LrD+5z4B95c2GpXpr+PxDPHH46X/USlR/PnZ+daT0YBce2ISm3Gjlvmpqaz0iku3zuu5w41GsNHEOvpm64HEtFVSp7Tw+wTRl+FBLE1+lG8SRx9ifWizfNalC4ENvTbeFfGGy8hblQfErI4usIhiU3pjSPRa3TRgHybdtzo/O3CXATTkeIv8IFpl1/rJbgr8Q9Z4ZohGo1M1AnIyoJnVYjNWHRuAvripMXP9Qg66ArMmbLsr09DS+Vx46zyf1Xml9LZxUf1qFgi6vUTwwLus5Te17M8UmhBhBl4blo9pFHUwIN+EthPF2bCtNg2zx1bm8uBZ60tYSbfuP/y5HxSpdZsdB4AgCY1tWepQndplLdXdAHz8uo3cdcoOauywXzSEJLbJYg3/5NBciouraArIL9FuFPSHv7sby/tGJ5HDC33N6mrBf72oclEweybbH8Wy/zrUZbxjivObBYxoRwkOMDpoCxJTW6dpy65LFmX76auQT1gc3yJfIe+Q7HU0PqIuTvyyDAs7QaOXkiinI= Content-ID: 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: 6d217f46-64f4-4238-f43c-08da69908d6b X-MS-Exchange-CrossTenant-originalarrivaltime: 19 Jul 2022 14:11:19.4903 (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: e5mcvBKIgdp4q7NIOt5ijghWh6JiNEAe8HqksEWldy4H4J2NkkSjk/1XWvZnA9wDb42pHa3uWdktY6+LmJZ6FA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: MWHPR10MB1886 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.883,Hydra:6.0.517,FMLib:17.11.122.1 definitions=2022-07-19_02,2022-07-19_01,2022-06-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 bulkscore=0 mlxscore=0 mlxlogscore=999 suspectscore=0 phishscore=0 adultscore=0 spamscore=0 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2206140000 definitions=main-2207190060 X-Proofpoint-GUID: 63H6BUkE2lcHIgPJVg9nY4k9j9bMcKBD X-Proofpoint-ORIG-GUID: 63H6BUkE2lcHIgPJVg9nY4k9j9bMcKBD X-Spam-Status: No, score=-11.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_NUMSUBJECT, RCVD_IN_DNSWL_LOW, 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 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: Qing Zhao via Gcc-patches From: "Li, Pan2 via Gcc-patches" Reply-To: Qing Zhao Cc: jakub Jelinek , richard Biener , martin Sebor , "joseph@codesourcery.com" , kees Cook Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1738790621588880679?= X-GMAIL-MSGID: =?utf-8?q?1738790621588880679?= From a09f39ded462611286a44d9e8273de8342673ba2 Mon Sep 17 00:00:00 2001 From: Qing Zhao Date: Mon, 18 Jul 2022 18:12:26 +0000 Subject: [PATCH 2/2] Use new flag DECL_NOT_FLEXARRAY in __builtin_object_size [PR101836] Use new flag DECL_NOT_FLEXARRAY to determine whether the trailing array of a structure is flexible array member in __builtin_object_size. gcc/ChangeLog: PR tree-optimization/101836 * tree-object-size.cc (addr_object_size): Use array_at_struct_end_p and DECL_NOT_FLEXARRAY to determine a flexible array member reference. gcc/testsuite/ChangeLog: 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-2.c: New test. * gcc.dg/strict-flex-array-3.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-2.c | 60 ++++++++++++++++++++++ gcc/testsuite/gcc.dg/strict-flex-array-3.c | 60 ++++++++++++++++++++++ gcc/tree-object-size.cc | 18 +++---- 9 files changed, 489 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-2.c create mode 100644 gcc/testsuite/gcc.dg/strict-flex-array-3.c diff --git a/gcc/testsuite/gcc.dg/pr101836.c b/gcc/testsuite/gcc.dg/pr101836.c new file mode 100644 index 00000000000..e5b4e5160a4 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr101836.c @@ -0,0 +1,60 @@ +/* -fstrict-flex-array is aliased with -ftrict-flex-array=3, which is the + strictest, only [] is treated as flexible array. */ +/* PR tree-optimization/101836 */ +/* { dg-do run } */ +/* { dg-options "-O2 -fstrict-flex-array" } */ + +#include + +#define expect(p, _v) do { \ + size_t v = _v; \ + if (p == v) \ + printf("ok: %s == %zd\n", #p, p); \ + else \ + { \ + printf("WAT: %s == %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 *)argv[0]); + + return 0; +} diff --git a/gcc/testsuite/gcc.dg/pr101836_1.c b/gcc/testsuite/gcc.dg/pr101836_1.c new file mode 100644 index 00000000000..30ea20427a5 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr101836_1.c @@ -0,0 +1,60 @@ +/* -fstrict-flex-array=3 is the strictest, only [] is treated as + flexible array. */ +/* PR tree-optimization/101836 */ +/* { dg-do run } */ +/* { dg-options "-O2 -fstrict-flex-array=3" } */ + +#include + +#define expect(p, _v) do { \ + size_t v = _v; \ + if (p == v) \ + printf("ok: %s == %zd\n", #p, p); \ + else \ + { \ + printf("WAT: %s == %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 *)argv[0]); + + return 0; +} diff --git a/gcc/testsuite/gcc.dg/pr101836_2.c b/gcc/testsuite/gcc.dg/pr101836_2.c new file mode 100644 index 00000000000..ebbe88f433c --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr101836_2.c @@ -0,0 +1,60 @@ +/* When -fstrict-flex-array=2, only [] and [0] are treated as flexiable + arrays. */ +/* PR tree-optimization/101836 */ +/* { dg-do run } */ +/* { dg-options "-O2 -fstrict-flex-array=2" } */ + +#include + +#define expect(p, _v) do { \ + size_t v = _v; \ + if (p == v) \ + printf("ok: %s == %zd\n", #p, p); \ + else \ + { \ + printf("WAT: %s == %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 *)argv[0]); + + return 0; +} diff --git a/gcc/testsuite/gcc.dg/pr101836_3.c b/gcc/testsuite/gcc.dg/pr101836_3.c new file mode 100644 index 00000000000..d4ba0afe5bc --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr101836_3.c @@ -0,0 +1,60 @@ +/* When -fstrict-flex-array=1, [], [0], and [1] are treated as flexible + arrays. */ +/* PR tree-optimization/101836 */ +/* { dg-do run } */ +/* { dg-options "-O2 -fstrict-flex-array=1" } */ + +#include + +#define expect(p, _v) do { \ + size_t v = _v; \ + if (p == v) \ + printf("ok: %s == %zd\n", #p, p); \ + else \ + { \ + printf("WAT: %s == %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 *)argv[0]); + + return 0; +} diff --git a/gcc/testsuite/gcc.dg/pr101836_4.c b/gcc/testsuite/gcc.dg/pr101836_4.c new file mode 100644 index 00000000000..b10d3ce312d --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr101836_4.c @@ -0,0 +1,60 @@ +/* when -fstrict-flex-array=0, all trailing arrays are treated as + flexible arrays. */ +/* PR tree-optimization/101836 */ +/* { dg-do run } */ +/* { dg-options "-O2 -fstrict-flex-array=0" } */ + +#include + +#define expect(p, _v) do { \ + size_t v = _v; \ + if (p == v) \ + printf("ok: %s == %zd\n", #p, p); \ + else \ + { \ + printf("WAT: %s == %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 *)argv[0]); + + return 0; +} diff --git a/gcc/testsuite/gcc.dg/pr101836_5.c b/gcc/testsuite/gcc.dg/pr101836_5.c new file mode 100644 index 00000000000..2f6b5f7ae1f --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr101836_5.c @@ -0,0 +1,60 @@ +/* -fno-strict-flex-array is aliased to -fstrict-flex-array=0, + all trailing arrays are treated as flexible array. */ +/* PR tree-optimization/101836 */ +/* { dg-do run } */ +/* { dg-options "-O2 -fno-strict-flex-array" } */ + +#include + +#define expect(p, _v) do { \ + size_t v = _v; \ + if (p == v) \ + printf("ok: %s == %zd\n", #p, p); \ + else \ + { \ + printf("WAT: %s == %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 *)argv[0]); + + return 0; +} diff --git a/gcc/testsuite/gcc.dg/strict-flex-array-2.c b/gcc/testsuite/gcc.dg/strict-flex-array-2.c new file mode 100644 index 00000000000..326ddcfeda5 --- /dev/null +++ b/gcc/testsuite/gcc.dg/strict-flex-array-2.c @@ -0,0 +1,60 @@ +/* test the combination of attribute strict_flex_array and option + -fstrict-flex-array: when both attribute and option specified, + attribute will have higher priority. */ +/* { dg-do run } */ +/* { dg-options "-O2 -fstrict-flex-array=3" } */ + +#include + +#define expect(p, _v) do { \ + size_t v = _v; \ + if (p == v) \ + printf("ok: %s == %zd\n", #p, p); \ + else \ + { \ + printf("WAT: %s == %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_array (0))); +}; + +struct trailing_array_2 { + int a; + int b; + int c[1] __attribute__ ((strict_flex_array (1))); +}; + +struct trailing_array_3 { + int a; + int b; + int c[0] __attribute__ ((strict_flex_array (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 *)argv[0]); + + return 0; +} diff --git a/gcc/testsuite/gcc.dg/strict-flex-array-3.c b/gcc/testsuite/gcc.dg/strict-flex-array-3.c new file mode 100644 index 00000000000..990c5bb6223 --- /dev/null +++ b/gcc/testsuite/gcc.dg/strict-flex-array-3.c @@ -0,0 +1,60 @@ +/* test the combination of attribute strict_flex_array and option + -fstrict-flex-array: when both attribute and option specified, + attribute will have higher priority. */ +/* { dg-do run } */ +/* { dg-options "-O2 -fstrict-flex-array=0" } */ + +#include + +#define expect(p, _v) do { \ + size_t v = _v; \ + if (p == v) \ + printf("ok: %s == %zd\n", #p, p); \ + else \ + { \ + printf("WAT: %s == %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_array (1))); +}; + +struct trailing_array_2 { + int a; + int b; + int c[1] __attribute__ ((strict_flex_array (2))); +}; + +struct trailing_array_3 { + int a; + int b; + int c[0] __attribute__ ((strict_flex_array (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 *)argv[0]); + + return 0; +} diff --git a/gcc/tree-object-size.cc b/gcc/tree-object-size.cc index 4eb454a4a33..90710ecba72 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, const_tree ptr, else if (var != pt_var && TREE_CODE (pt_var) == MEM_REF) { tree v = 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. */ + bool is_flexible_array_mem_ref = false; + /* For &X->fld, compute object size if fld isn't a flexible array + member. */ while (v && v != pt_var) switch (TREE_CODE (v)) { @@ -633,6 +633,9 @@ addr_object_size (struct object_size_info *osi, const_tree ptr, v = NULL_TREE; break; } + is_flexible_array_mem_ref + = array_at_struct_end_p (v) + && !DECL_NOT_FLEXARRAY (TREE_OPERAND (v, 1)); while (v != pt_var && TREE_CODE (v) == COMPONENT_REF) if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0))) != UNION_TYPE @@ -645,12 +648,9 @@ addr_object_size (struct object_size_info *osi, const_tree ptr, && TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0))) == RECORD_TYPE) { - tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1)); - for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain)) - if (TREE_CODE (fld_chain) == 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 = NULL_TREE; break;