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 639ED3858018 for ; Wed, 5 Oct 2022 19:13:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 639ED3858018 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 295Hdaa1014270; Wed, 5 Oct 2022 19:13:34 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=ny18nXdpfZevo9PceM4PPaxKIdWOHb7mhOKjGeALwCQs+HzE6nX8+6uHu5aQB1CcTWhN 0qEdyANzvoeiejpP9H1PiGJOQsYtj8EiMjOW8zgv8/ebxRWvdvIKAdQ3P0unIvQ05kAJ HST4VIEXIzBX81CKbSxi4UoLHLFTMiMHzfThe2DKEWSqFPsQeW7W75IOhucdJn1rTrXI RFPJVXJoXZKiElLGwUvOo5iqO/68J8b90YP96yAIowwPJySbUeIPNkfV1qfrHNRsVNtz lDNWDuFPn10nyyfA0klL3LdlhhWrEypOM5sSym1026O3tMJ4CV7Mznxzvc1XEGFqh8sk CQ== Received: from iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta02.appoci.oracle.com [147.154.18.20]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3jxcb2t8cg-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 05 Oct 2022 19:13:34 +0000 Received: from pps.filterd (iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (8.17.1.5/8.17.1.5) with ESMTP id 295HJq3v037600; Wed, 5 Oct 2022 19:13:33 GMT Received: from nam12-bn8-obe.outbound.protection.outlook.com (mail-bn8nam12lp2176.outbound.protection.outlook.com [104.47.55.176]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 3jxc05dr0u-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 05 Oct 2022 19:13:33 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=JoVp6/qeVVJ6IDiQPLGt08xMAulv2vItJZRDbm88pl/Bk6fzwgO6+rpATVI0qPMqSoLzkrWS3w5cMXzWMsdUo6FsX4tyP6HHxDx8nrOeIuSBOSPrcre51mE904BoJykc+jwoNUxFabbqIukrv+46+cglVlVINxGHBu9ok9+8T2/cGxyihtB1km1CAnCRj/a2x+lUWR154YjTgL5ZiCaW3dpTLakm4569Bor8lZdmHS2E7qNwRNLD8yjbk4d4FLIf9X8+u2I0TufGupe/IizJHzYxrAZ1ojTqoERU3KhuGGAxJ0ku2UJwC3FeqIFH1nECjf61eYwU56en++bWnyYQaQ== 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=l/pcr31UDU4mYeAufuTNuvxQSQ09uXkbDx0AxJN/uDdKQl6A4nh1Esj4sLQ9ve0n3BeaPKlNYF5/SDIm7vaF9pn0Elt4a0QRtn+cljUnbH8njc+LkpcCOJGo7z1nFvwIXa1hWb3EMkSfCx68HZP7DC/WEuZotoLo+oda+FVULNo9cpezbFLxly/A5LiHeixsgQ6YdvRBPS3WVyolTozDfCW7Ba1rGw9aGxOqVapHzH3qU53im5AkzQLYeLvkb+a6ChIhY2fwfJqz/UFtPEhmACJ6C2b5oISnAIUGY4Csrgy0+qMovZnxXMOGieT8Pd/+emKdue/tzXJ42Wa/jpnlAA== 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=DwknQkgsCUOWL4XQTpGTGJhXt3VOMgtr6Mq5kWUwcJmC8ES2sGzm4Dj3F8/6NGn6W7SlzPyLDzCa4aqr7HihsRs1aQjRaFi1OISB17ETyn3uW2zq6RU/X8vl+TPghqXEBeor50g4+bLlsSwc7y2u0dlLDWbSHbtaJiqVg9ZRUPM= Received: from CH2PR10MB4344.namprd10.prod.outlook.com (2603:10b6:610:af::19) by BY5PR10MB4338.namprd10.prod.outlook.com (2603:10b6:a03:207::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5676.28; Wed, 5 Oct 2022 19:13:30 +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.032; Wed, 5 Oct 2022 19:13:30 +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][V6][PATCH 2/2] Use array_at_struct_end_p in __builtin_object_size [PR101836] Date: Wed, 5 Oct 2022 19:13:20 +0000 Message-Id: <20221005191320.2087486-3-qing.zhao@oracle.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20221005191320.2087486-1-qing.zhao@oracle.com> References: <20221005191320.2087486-1-qing.zhao@oracle.com> Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: MN2PR18CA0016.namprd18.prod.outlook.com (2603:10b6:208:23c::21) To CH2PR10MB4344.namprd10.prod.outlook.com (2603:10b6:610:af::19) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH2PR10MB4344:EE_|BY5PR10MB4338:EE_ X-MS-Office365-Filtering-Correlation-Id: e57e3aad-82bd-4431-52d4-08daa705b040 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: a0FHYuw8c4Cp2tvazQWdenF0cAGzstWC9Oput/jvZnTlrHLZIAReyNhG2nyLhh26ZikIDMJhr9M0fMmJ5CNUDpKgpSt/q7KmurIEYnlL6sBmH8JlMEsIHVOMV+bQ/kJGbnnYJoRfI4FyOQRyVuyYE4WHN7MqycBx4rvGrS7NugetFSLTWxHhtE+2ZRSKvQlm0mzpzxryvimi1DkgZlks7ji8NaVYWggFtaF2yqMmKmFAxGT67tjdjvZxXmJrduwqps5tx+5748/uFnb8052Y0aQm4nBwj8goVmDwE2AvRYNvgFFETtVcwlB1/UbyarI/ECkhN/EvF9JJTLryak+Meza1p6v6ZkXoFnYUDN1gY2KYJoziP8Hi1XlEbnxfgFIZJzusw9xuB68BekHDcsy91TRMFnNvPaUPiw0lh+ZJPi3rj+4T26puwss0qd7zuyYSxDRdcJVzBPaREa9daE0RvUPFQJqkhgxEXqrc+SDmAUe5Tm2SNT4w0QoSSVErGbjGXVjwmCmXr5QZEcbnzRKcbIhjyzffTR4PDJuyog7eq+TYvXgpmAMSojcFo6hYxMbAPQ0wvSVLKldqEbw4084soAbN9/luhq0mRVC3o+uMSsQAiJ49jhKKF+4+FpleU0N4ng1+Pa3ubdZXXUUP5l66QThAsa73MI9AJBRRYkcY291zZbRapSMDrmDEAoqpobrvV0xa09OEGpo/OS/DYS4NPqf0Az+YrZyx48j0GSGuDv8= 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)(376002)(346002)(396003)(136003)(366004)(39860400002)(451199015)(2906002)(6512007)(41300700001)(6506007)(26005)(36756003)(30864003)(5660300002)(478600001)(6486002)(38100700002)(6666004)(107886003)(8936002)(86362001)(1076003)(44832011)(186003)(2616005)(83380400001)(316002)(66556008)(66476007)(66946007)(8676002)(4326008)(84970400001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?TdXh/F8gmsDwN/cd+hSgJhOZ5ceWTmvEypgiNWs6H76Lfa+VYS4PpwyTgTxT?= =?us-ascii?Q?pKB7tYOf9vZjpxRAAn2TtEk7ZB/8VViykPmr3SCPddMQ9OI+dI/+rIHoHDG+?= =?us-ascii?Q?lmZGHOIIjQOdv0JHNY5XaaGoJCM6DYcz34CUBRoYJamDBeE8zhj3aU/F5QlL?= =?us-ascii?Q?kxHt55aA8juSToUB2n0DZtW6ruLARCVFVQJ3T0Y8e/uc/al5Dvm9QvABlfp4?= =?us-ascii?Q?LB/jRqtX4WZIN6JY4FqrZZRJ3xtHiBsgvjGVY7mX6zL9czuX/jSYnDK5g0xV?= =?us-ascii?Q?/rsvePkrJQG+PvhS+SQ2Gd6/Zn63Fm86YB/D1c525vyF1/Zq0qll1eq/9a9L?= =?us-ascii?Q?1nBU56oQ1mZq8THy2to0UYkclHrWOYmicNljFXdqsvvMs+w+BYjwQaQyC65k?= =?us-ascii?Q?WkTnbT7ZeeQeUCER23zEgKQzbA3VRjs8+RwIEJUmLtnvjTu9eQEDZ1BFf/a6?= =?us-ascii?Q?bHKaqyXsSmDZ7YsG+HysDoZ+bCrYqusp47Vl6cKBWuQVITJLXR5znJrX0RcE?= =?us-ascii?Q?re0rErcXEODcz7DlG8ca53SPJJR52KOTwRYuq1eIKHx8rq+DftdT0p4p0qXY?= =?us-ascii?Q?l8BSck21ihNteVK8rLQJQAVWkWvM1VNtbYt9j6Ls1UX+XxYcJSbuxtILPdx7?= =?us-ascii?Q?R74jYRDnW3j+uOTfEEk9SM0DOnOSK68eEjLpLvNwM1wZg+LXaRrMslg4R4xK?= =?us-ascii?Q?4k81Y4L2WPeVZiC9SfGCHPKrUjgl9WwZ6hJNPF2uwR+NWQRWv7TpQiL59MG7?= =?us-ascii?Q?IWRERAySDV0F2NqeyacKWTmxuX6a5lAvWK7j60hYEWzOMZcfwGNB5cXNjXkz?= =?us-ascii?Q?RlY7BV9lGn2XNxCZ23qRHp4JCxT/fHW0uSYz37ApJfv+iwtnHbR7Nf9JEpm8?= =?us-ascii?Q?vCSm051aNaJP2NfHYk/zip78LSCusfuud4+DOAF+HX5pZddRuvhCQ5JPNzKA?= =?us-ascii?Q?95gmF1671eCpraT4EC8+OCGpeE0OiSHKTzktLnSMyrOq3XF+WEscEhVgnuvP?= =?us-ascii?Q?xGRaNTbvAImZypqHwy0qL2PPiOGbLkDpayuT0cElNWb0xNEqPgXLrbhYD4wc?= =?us-ascii?Q?AjClHkiohOhQKH8FBjOpwSxpXLD1ALZNrQjaYEO61Mu6a8tUeOhnFQnVgieU?= =?us-ascii?Q?rkrHJGbzXrFYX8ACwIlR45kILkhgGz4dYx69LmyVDFG5fQhnI6vSd9HNlMNN?= =?us-ascii?Q?Xa6N5Z5nE3MOavgYpuw1nh0PbpbgxJlh7QSBakjFWu5VQd4TL71xZ5uGYiL1?= =?us-ascii?Q?LoeiqahDvrqs6XIF6rvoMw4d6VfqNKfFW4nmoypvLbuv8BdwTGvXYp+Hr5Gf?= =?us-ascii?Q?MHLO05/Z/NEBeD7F6oQneU51ooJRPo3/ZP+k2ILthtkHD7mtiKkyWxWH2vxn?= =?us-ascii?Q?8THcI+/e/qxhw6GLRXoePTx7DTDbp4R+keY5g5LQGIA171WpRLToiZd2j6UW?= =?us-ascii?Q?uNRpQPus0Rjtp+J0t+B2ANUjIl7HkYQq2yKsYDuVIZ0mQBn3L03J3BWcOFwH?= =?us-ascii?Q?nV7HggKzAkiP/lyJR3nizRjqVwZMFsi6/H/D8oTLxeOqwPa+4Fkvy4PDzqaE?= =?us-ascii?Q?Ivy7hbi5xpJWn40ShRIX7/fD4R2ffPTe+SbslKHp?= X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: CKvoyzRUz7BmxFul9qeb7ih5gJ0c1ydHLBsI8Gxz2ZVoiCS2cRxHFIh4A28rfNJZ8+gbIi/qYykYtmSwircEsIYS72GINQVKNBqz7DHz3XaGQzYi7fSD5Aeov13EtSDJoe/N+MnWU+2w2c69DokI7KGpTKizBULb7bQNrz4jhm4NAgmgyHIuOaOFxW7LP6thAVEI+a5ntCsRi24vv643DbGnP0vJun/NnNRFeC+gjvNnZmqyrBGKq6xCAhw+V5ybvLPSTf/qDHnYiWKmGbdFWFPF1IP7UVVbOOzXp04gNT072FI7UNNr1KN5/kx5fF72KDI6vRcNkDs04JuWg041BbPIBZ38O7eUPmFQdIZFmqn7kX6NaCjHLkI57pt15EusvZUb0nBN7TZvVJL/083pltseZO7Iocu0znfoD/vJr9YCYnN46EOWYNoiMOdu/gNFBt+gYcnTiz55B6Zztc53E+ri2yp2ArA/fm0XXJOdNbQk7msYAeWA1XfMqcsq1zVm5XpUIfcsdLcPzV28+AjwTY5lFsOj8UdohmPBhS6Pi/RK+84UAEctVy/WX6L7jUKwvJq8+dXK9SxTP83JhowCUsr/IulZW0vbJgX2/CYGqQGNiW8NP4Jox6M+POvXvDmClNR0kVfD9UnY6E4sogaEySdm5FNnCj98mSIBLnV1PrYPy7Do305yQekrkf5JRK3zRlGFmncfd6y4SPxXU1ENz79aPgIa+Z8ys8y5bgsBHBmj0DxTBijyOzFyIRzU0JaoNnjonqMAgm21TV7GpudXmE3SUlWeIIp9VOYdijChO/45ag8GniMtn51gBgDc/ZZBGZW0JeeciM9eLM0c1E5zGJew95N+p9sA2Cb5N5c7tUEr99c9D1eXLp3ElDFD6uXdzwTzBzzrpOrKTdFB3srXig== X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: e57e3aad-82bd-4431-52d4-08daa705b040 X-MS-Exchange-CrossTenant-AuthSource: CH2PR10MB4344.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 05 Oct 2022 19:13:30.1120 (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: p5aTQPDvoYOLzGSjqg1EnIaS8mlh4kWdrfJ297mHJ3vOzVoO+/dVRH4A9YCUhDs9cBkvrFBZjoorsqFiJiOLxw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR10MB4338 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-05_05,2022-10-05_01,2022-06-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 malwarescore=0 phishscore=0 bulkscore=0 suspectscore=0 mlxlogscore=964 mlxscore=0 spamscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2209130000 definitions=main-2210050120 X-Proofpoint-GUID: RHle4QvUA0ZINOtrtX-ZDE5bXaaDQJGh X-Proofpoint-ORIG-GUID: RHle4QvUA0ZINOtrtX-ZDE5bXaaDQJGh 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