From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by sourceware.org (Postfix) with ESMTPS id CB8F73858286 for ; Tue, 4 Oct 2022 15:22:13 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org CB8F73858286 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 (m0246629.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 294DPVKn000370; Tue, 4 Oct 2022 15:22:11 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-transfer-encoding : content-type : mime-version; s=corp-2022-7-12; bh=QwXpOxYbbC1krXIi2IBMfMtZ7FQo9WSQrnt/axz9iuw=; b=aCGOQIpQrP0DAj53o7XETSHC6rzAwkjxd6WZKA+Zv5so7b4WFK+PcGfu4HcpDASpOnHD EjB46K/4xOEywVf9IwvwDjLTMWBr3/0zmacbgOhSORzyMQ4JGuo0XsnTBwR627c8SpYf c9VopP8xNUpVAXubJX90NDpJJkhvVGKIuUwLQQFAu2b3x8mBwsATQFaIvdXdjC1JzK0K TySOgY3OdeMf6YLEPKRe8Wokt6Uv2MSpOG0cG1Qtnd5hERba8DxXXo99k6fJR6wKOiaJ wrEGBUXwmAxNZzgwxkP7deKp9rF02EkZoT95d+YDJmtZsiB1ERuRjAp+G6DnpJtiavxZ Pg== Received: from phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta03.appoci.oracle.com [138.1.37.129]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3jxdea6vx6-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 04 Oct 2022 15:22:10 +0000 Received: from pps.filterd (phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (8.17.1.5/8.17.1.5) with ESMTP id 294EH80W001099; Tue, 4 Oct 2022 15:22:10 GMT Received: from nam12-bn8-obe.outbound.protection.outlook.com (mail-bn8nam12lp2177.outbound.protection.outlook.com [104.47.55.177]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 3jxc04hmbx-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 04 Oct 2022 15:22:10 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=D1et2OKDJki9lgIvwK7cgG4JI/25iZEjNHjsRJCaqz1VmDjsSPyJ1G3L6Ahd+/0/MVikBhMcUvk+i50cfSyaMQu4MSo9WpQ2KM+ESqiixYWpNKDaYdpk7oi+N3Or2dbQKBChh5iqlX5tj1D1kG6ahSvgQOXPmI6OaMbreJfxI4FpF7EQ36yRSk8koAyypaEDU3d71CQ7c9IGjS2hwPrl80dUU3Bfg3qN/0cBg6GJkONs4zszGnceMDrSlxHHWHT9MaVi1nla1Scrn95uLnKlrtv0VRkWY8nqIfAFc+aGN280nvUP9W+QL1/ElVQmWgIaYN8MknYcrmx+yn8VFZKOPQ== 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=QwXpOxYbbC1krXIi2IBMfMtZ7FQo9WSQrnt/axz9iuw=; b=XaDs5g2/eDc7HEm4o+BRstlILva6YzLj5nQuNLCF1pwwyuiBFW0FxZltXkuaYlR2M5cSmjaqGgb3LKowomqbRIM3tdek4gXYFZ9EStzm/BEP84QNQgLyA8wVQI4V98Lb+USver53PPeXjHBkqEKwTpLX8M9IhNv/dxuidyF0LuVOdBp0Ss43D68P750PjNPqpR7XN9NQqPG0fQYJVLQiWkxqC8sCgrb1xEhfEXK/ujs4Uyk1nAEbt1E1N3ERgtHrz0QxhuDdRZy7kLoWI258acL48l7/Xr/uImPPGSXEOMmz4TZD+kO4C209Lf/Ma+70RXj62GtN6lfGJyxEGT3lGA== 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=QwXpOxYbbC1krXIi2IBMfMtZ7FQo9WSQrnt/axz9iuw=; b=IGmKbYy34+pqPsYm/1Hj2l038pR+Liv63BN7B9wssvF/Gby8gxAolIJjCdC8k7/q29lwvXnolSsAIMBdirlAXRi1U1OYN3KKFY+cNpqORq4PgU3IUzrJL3qPgE4L2SpSh9X5HOKQQu04849KAm2jBBisqt91LwdYDjgnkSPSdRs= Received: from CH2PR10MB4344.namprd10.prod.outlook.com (2603:10b6:610:af::19) by SA1PR10MB6448.namprd10.prod.outlook.com (2603:10b6:806:29e::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5676.20; Tue, 4 Oct 2022 15:22:07 +0000 Received: from CH2PR10MB4344.namprd10.prod.outlook.com ([fe80::cbf8:6682:721e:835c]) by CH2PR10MB4344.namprd10.prod.outlook.com ([fe80::cbf8:6682:721e:835c%6]) with mapi id 15.20.5676.031; Tue, 4 Oct 2022 15:22:07 +0000 From: Qing Zhao To: gcc-patches@gcc.gnu.org, joseph@codesourcery.com, msebor@gmail.com Cc: rguenther@suse.de, jakub@redhat.com, keescook@chromium.org, Qing Zhao Subject: [GCC13][Patch][V5][PATCH 2/2] Use array_at_struct_end_p in __builtin_object_size [PR101836] Date: Tue, 4 Oct 2022 15:21:53 +0000 Message-Id: <20221004152154.1665626-3-qing.zhao@oracle.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20221004152154.1665626-1-qing.zhao@oracle.com> References: <20221004152154.1665626-1-qing.zhao@oracle.com> Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: SJ0PR13CA0108.namprd13.prod.outlook.com (2603:10b6:a03:2c5::23) To CH2PR10MB4344.namprd10.prod.outlook.com (2603:10b6:610:af::19) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH2PR10MB4344:EE_|SA1PR10MB6448:EE_ X-MS-Office365-Filtering-Correlation-Id: 312e19c7-d61e-4090-833e-08daa61c3314 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: AENvZ/enBJPHXf1cZjoesLOzsjwF0rEqzcjblBi+wWcDQ/wlZWte9shPrba/9WStzogkvs6uD0TQY54VFDJSTvqihmAga4DmDgfn2flWXp7NDHOYWwrvWXBDWL/UX+X0hVMfRxMrhoIace8od/hNc2pJxBWiU6Zy2dNHJuK7Xbhbv3HZ7q2JahBG7vsDp7mqqXPK84WOqV7+A6rMPdfF2AN+wV7rA7N5JPT8EFerXjR06wzj8oPvB5kAG697vXHZuBQ83IOCWbAU2e9PYIQgveqShE1vO7Yba0LMNlyIOiLG/CtS2DraqWwmX2t96Djt1Ogxsl0LAd1G7vgz1KBCY7dof/el7/iAJm/Pfk1QAOVtg52Sbdt99iSvoWCESAADA5bV9Pu1N34zfa5hrDWC713tk6wlSMkd5lBffa0KoD5862Y7APoewhCnwx7KmOU2GrdXa5T3c+fg1cJ9mE2aZ3i5lnV8V3LltduWmc/eRvuzVq5+BWwne/Zbw1dyCGG6LRe/WSiSngTk9/6cj8Q60eo5eEaklFKVQKqfvcBhQPPto0QSHJmu/RLS4C0JEwCZDWZXdljZW/WIF3fHuG4fsdaz5plqOYw8BjY97pSFOrKBuWP7itNqZCZ4U5t4rPnwMF+ILkTOUA6/vqFAJ1Nq0/0xMFgWvN+HvPdzdotYitUJcSeXsICJe7peFByQpIf+D9v+yuaf5mt4zTzwsAQA4KBivpQxrYb7WQlJc9Qe+p4= 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:(13230022)(346002)(39860400002)(366004)(376002)(396003)(136003)(451199015)(66946007)(36756003)(83380400001)(107886003)(186003)(6666004)(66476007)(66556008)(6486002)(2906002)(84970400001)(478600001)(38100700002)(30864003)(2616005)(86362001)(6506007)(8676002)(44832011)(316002)(1076003)(4326008)(5660300002)(6512007)(26005)(41300700001)(8936002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?lyHNlK4l55toRUUOFIO0ekxBUf6TKfiK3jllO5irggiIU6JwOY5/zrUDng99?= =?us-ascii?Q?CRNMgXogQ3xXdWbh9ySJoKlosoU07+JJNUlpJdrV8QMFsgqZaXM2qeyUCk+c?= =?us-ascii?Q?HiS0Brq/QMgKsbhNYlMnXuiHX4uEFuI6BmJCFIxLSNDzJu0VC03W8YuAK5+Y?= =?us-ascii?Q?E9ljEKar/3pdKbG01k0aZh0N9TMZdiocinCjzedGV3tfcPNz9QyWKRDAV7Mn?= =?us-ascii?Q?LWz1VLUkmffZMb61CsDq/qW2r+BbkQMxdXnDn1IkihsR+eQmB5WHcUQXzTYm?= =?us-ascii?Q?cnpLCh3E2FJmh5eT045NzD1n+B9BNUj7K8rir/1ZGomK1lqo2KAGopd3gN8M?= =?us-ascii?Q?1boomFoW/lFJN6ywoDq4WOwzriDh6E7L5PnLHYPLFjPGjqlfhUP7CRuVUhOQ?= =?us-ascii?Q?b8/EVukTAGYvQpil0sh3BUBcWMEuytSzR5vsWg3Lb26fs7guQx4/3nryTTTz?= =?us-ascii?Q?cCJKH1MhHiSh6en+W0fAHgjEg/U5LO2cG1Olc4n24/koq8Bvehjoy6hXugDj?= =?us-ascii?Q?yHpgH6mJ11K1YWYSJ5pTKWGQspuKww9s9lOeCchywaam8/0eqJQl2ZSbWZk0?= =?us-ascii?Q?gqXrDr5duPUb0JOTB0MyGQIH63aNFsMVVC/Kz0Wrycwak0T67SM6wge1Cbhp?= =?us-ascii?Q?HoEU7a0GT2d5RHwkRPyxJPPh0rd2ToR6h//3UjTuu7KxJwooOLDB7JEt46BI?= =?us-ascii?Q?KdSdPjrOps5PtvxUz0XjesmE2xmwxOX8pavi6Uj/HFyX7Qagj1Rn2iwdNWVp?= =?us-ascii?Q?QSdHkzBQ8G/XzLsMwcCaSA48mz/VSzPJQVFIduQRejaETo9oOdtxgER5BLZQ?= =?us-ascii?Q?SzhGap83V95rtwdvx3OEDs6iUDbnCD//0U+X/22yH73KJtK7rRS0m74L64N1?= =?us-ascii?Q?hDB60i9h0UTpvfeY1bLWXXpMR155m+0gQWdkEcrYe6RSWxC+gEnqXDXO/7Nu?= =?us-ascii?Q?KlDrEXLwCxNYQdEIq7kjVo8fv0p7XGOe+vcqhjXljAAH1PXCx5gh35qQWqF2?= =?us-ascii?Q?98Yi4euA72XufNyWAuK0CPeRfG2o6j79/X9eRR5YhFmAnhDXQPdlTJdnjUzg?= =?us-ascii?Q?LuQ83Gamppwp0JlplKnFBl8bvgAwI/tdT9/qos7ALjtDOp8SbZOJMo669OjV?= =?us-ascii?Q?gGUdvoXo8/1bvwZYdxM/X+poXw7H/0T8E4Xh5yNE/bFXreb/a3GQbMnzdh8X?= =?us-ascii?Q?B9951WXBYNF++UNUlgE0X0asAAgjR/+hnFemlAlemlE2ySmvCFkiNwTzP/ox?= =?us-ascii?Q?wquEmFaV5lStvK+w7wfZ/W8T78M18hWhj6WowhmI/8Xzba60wj1VnvG4dfJy?= =?us-ascii?Q?WPqlOfmyRrUKZe9GHJcWkYh0V3cptEchStwvQq/WpY9Oqh5N9zIAG+kr1Vwe?= =?us-ascii?Q?Cf2E826HYVxMIX1CEjrCPC7dUopWEcklmoc5kj0039XGiHwmZmWBd/wB8NNB?= =?us-ascii?Q?i7MVhpuVXDgitmasS2SRzNRsU02iWFw4X+3TzeFIPwIKmkzpeR2cfiN2hlrT?= =?us-ascii?Q?BgSPI6VH4khf8+tBB/ssQyQHSbH0HU6RDymbs9rycBvsty79DMmwxxOX6f1K?= =?us-ascii?Q?PWcSI05kpdjC+9QUuJsbJ+tEp6UeNv2ouoVBlahh?= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 312e19c7-d61e-4090-833e-08daa61c3314 X-MS-Exchange-CrossTenant-AuthSource: CH2PR10MB4344.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 04 Oct 2022 15:22:07.3561 (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: WUwc0WCHa3tZK+hOJA8qCXL4KRLtI21naJ0I/Dng1m6p2gTurDZ0l4N0Vf5fXL1vBIzVj9t87WLHGIJzhQD21Q== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA1PR10MB6448 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.895,Hydra:6.0.528,FMLib:17.11.122.1 definitions=2022-10-04_06,2022-09-29_03,2022-06-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 mlxscore=0 mlxlogscore=963 bulkscore=0 malwarescore=0 adultscore=0 phishscore=0 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2209130000 definitions=main-2210040099 X-Proofpoint-GUID: OOEKajTjk9s7TO_ItS6ERDhWHlwDMx61 X-Proofpoint-ORIG-GUID: OOEKajTjk9s7TO_ItS6ERDhWHlwDMx61 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 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: Use array_at_struct_end_p 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 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 | 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-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..efad02cfe89 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr101836.c @@ -0,0 +1,60 @@ +/* -fstrict-flex-arrays is aliased with -ftrict-flex-arrays=3, which is the + strictest, only [] is treated as flexible array. */ +/* PR tree-optimization/101836 */ +/* { dg-do run } */ +/* { dg-options "-O2 -fstrict-flex-arrays" } */ + +#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..e2931ce1012 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr101836_1.c @@ -0,0 +1,60 @@ +/* -fstrict-flex-arrays=3 is the strictest, only [] is treated as + flexible array. */ +/* PR tree-optimization/101836 */ +/* { dg-do run } */ +/* { dg-options "-O2 -fstrict-flex-arrays=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..78974187721 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr101836_2.c @@ -0,0 +1,60 @@ +/* When -fstrict-flex-arrays=2, only [] and [0] are treated as flexiable + arrays. */ +/* PR tree-optimization/101836 */ +/* { dg-do run } */ +/* { dg-options "-O2 -fstrict-flex-arrays=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..0e69388e81f --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr101836_3.c @@ -0,0 +1,60 @@ +/* When -fstrict-flex-arrays=1, [], [0], and [1] are treated as flexible + arrays. */ +/* PR tree-optimization/101836 */ +/* { dg-do run } */ +/* { dg-options "-O2 -fstrict-flex-arrays=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..e0025aa9a7b --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr101836_4.c @@ -0,0 +1,60 @@ +/* when -fstrict-flex-arrays=0, all trailing arrays are treated as + flexible arrays. */ +/* PR tree-optimization/101836 */ +/* { dg-do run } */ +/* { dg-options "-O2 -fstrict-flex-arrays=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..0ad8bbf693c --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr101836_5.c @@ -0,0 +1,60 @@ +/* -fno-strict-flex-arrays is aliased to -fstrict-flex-arrays=0, + 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 = _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..2b80c2365f3 --- /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-arrays: when both attribute and option specified, + attribute will have higher priority. */ +/* { dg-do run } */ +/* { dg-options "-O2 -fstrict-flex-arrays=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..602f99dc79a --- /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-arrays: when both attribute and option specified, + attribute will have higher priority. */ +/* { dg-do run } */ +/* { dg-options "-O2 -fstrict-flex-arrays=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..1f04cb80fd0 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. */ + /* For &X->fld, compute object size if fld isn't a flexible array + member. */ + bool is_flexible_array_mem_ref = false; while (v && v != pt_var) switch (TREE_CODE (v)) { @@ -633,6 +633,7 @@ 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); while (v != pt_var && TREE_CODE (v) == COMPONENT_REF) if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0))) != UNION_TYPE @@ -645,12 +646,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; -- 2.31.1