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 827DE3858C2F for ; Wed, 17 Aug 2022 14:41:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 827DE3858C2F 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 27HEdmxC004963; Wed, 17 Aug 2022 14:41:08 GMT Received: from iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta03.appoci.oracle.com [130.35.103.27]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3j0nt91m57-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 17 Aug 2022 14:41:07 +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 27HCOv4j027008; Wed, 17 Aug 2022 14:41:07 GMT Received: from nam04-mw2-obe.outbound.protection.outlook.com (mail-mw2nam04lp2176.outbound.protection.outlook.com [104.47.73.176]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 3hx2d9m1rq-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 17 Aug 2022 14:41:07 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=lnoCvgM8CISdMWna5N8PtubqTUlrn6v0oRKq2MBZrKaoXrjR94TRJYUgmA+6kaISTTlnsqdKQlWYVWiBixnDIu5nB6rT+aa4MoKe0skZqbT/484p3yXWy3/9X7/sW4odxe8bc37IpsPLnp57kH6oK1v4aytOY66DeYGxto97Z9Mm1dNEMFpGCIliN8+OsOzL4z0fG4DgZr3nyVoswHvwC9h4N7DLLVzY/+3rBt5R7uJQCKw4b9gReug+0EE8sRSe0mVWHN0JsMD03xLRSp4ROOjuEYXr/kMFA8BDsZ7ue0lFv5XYlT85/cqbHz7hm97/GvekzD2R4iM9at/4bwU9gw== 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=oowX1TBhS/McrYANrIkcr4OmSgxifwEZ39wHATSlplk=; b=hplJwV7UZGy45wYm40flwr9PjGm7bzyK0v6IsNGiUkeg+Rgnq9W6hGHqt7nzXQ2s4Qz2iWZdVUvuSWLyGf2PoCvH/oG++Wg3OdnsudN0Fli3Y0fkcWAfLw1tHI0eJ98YxTz3qBa/EjRY8ghw2isWRLkMqzSv0yGoElq0jEHZe4HIGOfJ+AY1xAxQ3uGvgPVPcf0hISEJAC+HY7KpLEZuqE2nXHWBhFxhedae2rxBJuxodDF5Q23CfLgCadtR1oe/JOwtOC+7NSi0XJ1KUAHd3FULRCCTXgvwXL5RvEjN36/iPwTtQjxEYC12fQ4VkPorDIsSGps8q3QwpuXMvH8ARA== 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 MN2PR10MB3759.namprd10.prod.outlook.com (2603:10b6:208:185::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5525.19; Wed, 17 Aug 2022 14:41:04 +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.5525.019; Wed, 17 Aug 2022 14:41:04 +0000 From: Qing Zhao To: gcc-patches@gcc.gnu.org Cc: rguenther@suse.de, jakub@redhat.com, msebor@gmail.com, keescook@chromium.org, joseph@codesourcery.com, Qing Zhao Subject: [[GCC13][Patch][V3] 2/2] Use array_at_struct_end_p in __builtin_object_size [PR101836] Date: Wed, 17 Aug 2022 14:40:42 +0000 Message-Id: <20220817144042.2931674-2-qing.zhao@oracle.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220817144042.2931674-1-qing.zhao@oracle.com> References: <20220817144042.2931674-1-qing.zhao@oracle.com> Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: BY5PR04CA0009.namprd04.prod.outlook.com (2603:10b6:a03:1d0::19) To CH2PR10MB4344.namprd10.prod.outlook.com (2603:10b6:610:af::19) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: f417e4cf-4750-43c9-90cc-08da805e832a X-MS-TrafficTypeDiagnostic: MN2PR10MB3759:EE_ X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: aHjt2/d4YJRNrdJK/CZclpxmy1YEL4ZhsnrQcbYvhYNwNdLmWu08mIUoqPOvTV9zBNgRKa23ybnJvwGoOcvADQ4tZnZrUx5jCmt6EJG4AM4sfsy/dXgEmH/qv175odso4nvMTi6OnUIq6Td6yR54+ivPcThqTl63ZTv/MR3RmfLcIUK5tNB4D29D9jEn9sTuDJOoQB0aomyoKygyGLEVCh3SJKFlpxeUaxURiD+Xan2bR3Xb04+obwlSXLlaTI1KMzve7iG1KQIh8KKMzCOLRt00Gm2RWCUbX0v312aaToeazouWgnJ5qQuvvLYc6mzSKSt4H5Gue09SGHYgCCBeoH2ayGQE77rM9rmwpQw/S6ms+RSnHXPdf5bMfpPcBRkBA1bxCZYd58eNKttY0v0Vuwd2OH5KIv0fmwaX7DdQYJNu0rnvf52l9uMipaMcdsusqffU6KCKpvShD7JdRHrciI0Bc9+GjuO2HfRyMy0CVeU3+2ybBaQPX5lrkiWulQkbnW747kKkEW+K3SS9wnxB9LLB0Fx0q9xr2WPyJ6DAaMwslu0DmsvrNOwgywjfX1YrjVFBQGqn13a1139Mu4PHIMHFYtuZTMhQU0cmQt2Hzw7hi9upGpVkVnobER7r07dkkoY6+HB+GaMQRO+LHZYNDnV+nwhH8RB2lKqsiLkvGa7NLj6wkYZbU83gweNbdkVHMVC/EWfMD/8i67+YwcT2RNlhKqlAugcfygd0lUkFWvaS5/x4VTTPlNSa8rbqkM2hivrZ1WBAG+RnldS+F+I5BLfutTCtsm4EUMdNxum/bL4= 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)(366004)(39860400002)(346002)(376002)(396003)(6512007)(84970400001)(6506007)(26005)(86362001)(2616005)(1076003)(186003)(38100700002)(83380400001)(4326008)(30864003)(5660300002)(44832011)(478600001)(36756003)(66946007)(8936002)(8676002)(66476007)(66556008)(2906002)(6486002)(107886003)(41300700001)(6666004)(6916009)(316002); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?Bjal+GjoVsLDzfDcwDBwdokpGLq1T9j0xV34dmpw+pXuP0PwYWelDr5pWQsv?= =?us-ascii?Q?WjwJR0+NEsBdn9xAT7QPUMrB5eFr8HHedhpm5BGANm+tSIJqZenLJvVxU2St?= =?us-ascii?Q?YbBcVD3D6TpJLwpaEkxVkbg95lLlQFdRvL3e8RCx+J8QKf0t6Nwk1MQGKTFV?= =?us-ascii?Q?V3ugM7jTNCXBO90+SXrdH+8c4RThnPIwvkn6mrWAapRvBlerkYwo8XvNlYev?= =?us-ascii?Q?xErRg6VDIwlfevE2nuDuD3/KTO7fgtVvlkF+oIcPmKZsojfrcpzJ0wmj28yX?= =?us-ascii?Q?ltB5y/I5guN2zGJ9jXbvzvwKTXwLL7t+Jbu81ZkLdGiD1OX32tObhU5qFq2G?= =?us-ascii?Q?ixpgvv7q5/VoBecO1lYjuPxNarBYs/sZVEYPilFTkcD1UhSLl6FuaOrWP0Aa?= =?us-ascii?Q?9JHD8+fHp9T29FV+gfC7hNBOxIfGaMEngVWBEH23HUP0E91dnzxOq2+Cj1xP?= =?us-ascii?Q?4DK/i2HUedkeIXWPbMJDvZzlnsoNHSUWeDBl0jm8UC10vj5detf8pGLoeaQJ?= =?us-ascii?Q?eks2pPdi1ACb4XM30NtziV1dE1FGsuXgQLP7NvfxIjM5lT5Ny9zJGouZrUQ3?= =?us-ascii?Q?Me3LQcOUbUzd72W7GmKOa3efl9lskjwNzISY8vcYOwhJocnvoWQBrATTLlp3?= =?us-ascii?Q?fnbx56VKYwkPcBe7nK4cwTUZmmh7AZUkkpCRBF51+LTeWFWa8oMzmfTi0P3O?= =?us-ascii?Q?dT9D+zPD0xm0rZ+JnwQ4Z9Ma9YGK0knGVt8CAPryzw/sWBcJof9vzjpfvJ7l?= =?us-ascii?Q?sc7WoIRov5Nw/jkvsjyRQlAJkM0UutSKcM+BRN52NrdUcYcEtu6ZNPThJ8UT?= =?us-ascii?Q?MFLvh1e3fjCHxMROi1ADJsg3H+FUPDF2DEdYYvNk9GakUxiKhAMrPPgD4lTJ?= =?us-ascii?Q?78UrEgmgArKb3VUgQ+gxOjv52t2dhBN/myWzQIWM6QyOqRpCvv0jB2s/Rjd4?= =?us-ascii?Q?Bfb5fZCk9QXcQdgFVC7SO4j2p9HkmUUfgJaJrLo/Yu12lKKZAZ4u8eikkvGa?= =?us-ascii?Q?uHZVbVh9PIJLJEYtO4KeWY7eIXGNz09jwa5Yz5rEPaR0Zl/X3ON8TiN7490X?= =?us-ascii?Q?03SpKo/ZxCkz3Iw4R9chybA+cQuTR11iD7B5sfAPzgqfwH8HCN9ggiiIjSty?= =?us-ascii?Q?WZ/WAeQrP73wmUHqqG69XYRRQDELQINMPS5GQ1ZEt3rHiRr47OXsg/9Y9Gzo?= =?us-ascii?Q?dpXZaQzYFWwOFrZXQvyH0eSMOPgpmM99q60CeKa1J/Soe5DZsf7C2KmuS+9l?= =?us-ascii?Q?AhebqVajJzyrmVjgqR/FGgzedaiuJo/bICVpWS+O8ic6davHftaHLa3VXHeF?= =?us-ascii?Q?5bRaJ/x1cCv+1j0/zr2132re93Y1r/z6t/LClHNlmEqDWfMplwU/XeGxtdqr?= =?us-ascii?Q?hR3WVkX/C7B2NfWGdx3MA+oyYRIE9KippluLxSlN16VRvSB+dHz+geP0iU17?= =?us-ascii?Q?QrikEaX3/8dCn60oXh94uA8xMEqw+C6U5DcxrLKJv2L1zB3egLoRFfhebo5K?= =?us-ascii?Q?qFjXeZFIyTqTVbkea0dmIaY8taIdmlz8BBmLx+0AGzQTJch4VVTrHa47IQjb?= =?us-ascii?Q?YBOlqrlzTvkm5Ma/GI7VMLv39Rwud+GbmwO3wMMU?= X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: =?us-ascii?Q?O0WHSKsOBgOHY+Wojf+ZeLI1QNRRE+2yC9ieWHOHe17C1+iVNffrVRMlYHJD?= =?us-ascii?Q?0YPW2xTBC8BYf2rkbSSycxXh3T5xw23omierCr6hnpNfSy1q3dQDDHJNHKRZ?= =?us-ascii?Q?Fuvi0fF9NvfSyD0+xTMQqmJW5mVe+cBWXIZWGSpe1otqKWgyDxZ4bTw+qeLZ?= =?us-ascii?Q?u4bwl9InNIM0CJ1wb4o8/WVuKJ0senHe90WW8wJNgpzw5ygXL/Ch4ePLXk3W?= =?us-ascii?Q?47avv7wqHEoDdvhRVyTPS5VpA7IEIBSp6g0Za/M3jleBg6rafTmdR/K8StGr?= =?us-ascii?Q?YTV4rhN4J98cbuEwxuSRuPJwLhNmTzASN5HdOJKzfFGuyXrE5d1lXqwDii2C?= =?us-ascii?Q?2oSRPD5ZlBne6X/IheM6JBWqQp+uQnlCvSn+ZiagXuagV2EcW3cEShSmIasC?= =?us-ascii?Q?82r7gFcK9K1CgC2CVSPFQuaNRKaZkz8x9sGMfsl/6Gn5I5lmajNeSD6ZDt2w?= =?us-ascii?Q?N6FHLzjPLX53FzRtNfAYkh5IMTlK9MEKzrDbZbVfW+CA9YbzwvV1I4ywe6Ej?= =?us-ascii?Q?WaY+GNNWtLyW6dpjT0qwElrhaReYaTyIL5nM4qV2rMQr8i3NIUdT54zQwd6A?= =?us-ascii?Q?ueaV+TjjWsrFohwoUOKOCC8ttgob4+vfIro6yObZTRHP4qJjhcjj49Rcw7ZC?= =?us-ascii?Q?Fq7jyJvDRDbvgE/H62TWBdaKDCzPZLm06k3jKWjuENgkipyclhXyhSIrz02J?= =?us-ascii?Q?6QxUwZ+V5n8nx+yxWHU6PelhGMCi99DmGvNrwHMTLVYUj5cHlnJeZbUDJMIh?= =?us-ascii?Q?LI6qhnrNEudCkUp/CxUYPu8iHWQlZcZeBnPhOWB1p4YbfoWmGMtNBG0DmGe4?= =?us-ascii?Q?yAatttaRnEtuhrEwhsD11NwUrF576Yq8OIWRo/IMQX3/CZ6EH68QbCPJ46aD?= =?us-ascii?Q?C8Admqk6R8sHfayUJtGFO1fmzrpgAl1FambyXqSNzPG3xBoj3jSGuvEsKaSQ?= =?us-ascii?Q?EpsaULC9vJS5Vpr/ooePafd3eWPXsxuzwOaI/rJokHI=3D?= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: f417e4cf-4750-43c9-90cc-08da805e832a X-MS-Exchange-CrossTenant-AuthSource: CH2PR10MB4344.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 17 Aug 2022 14:41:04.4432 (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: FbN6HJfEp331o3GCuXCJXBBEab0XoCtGhNDGsts9jzfZwu/6Yvqx9CslKULvOuhoHmf7Ptmmp5T3kmU0V2Np0Q== X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN2PR10MB3759 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-08-17_09,2022-08-16_02,2022-06-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxscore=0 suspectscore=0 malwarescore=0 phishscore=0 bulkscore=0 spamscore=0 mlxlogscore=888 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2207270000 definitions=main-2208170057 X-Proofpoint-GUID: Ck8VmM3pCYcxrveszRTpzjIBer7thZCm X-Proofpoint-ORIG-GUID: Ck8VmM3pCYcxrveszRTpzjIBer7thZCm X-Spam-Status: No, score=-11.3 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 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-List-Received-Date: Wed, 17 Aug 2022 14:41:12 -0000 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-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 diff --git a/gcc/testsuite/gcc.dg/pr101836.c b/gcc/testsuite/gcc.dg/pr101836.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=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 000000000000..e2931ce1012e --- /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 000000000000..78974187721f --- /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 000000000000..0e69388e81fb --- /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 000000000000..e0025aa9a7b5 --- /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 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=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-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=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_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 *)argv[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=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_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 *)argv[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, 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