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 885A2385842A for ; Thu, 25 May 2023 16:15:10 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 885A2385842A 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 (m0246632.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 34PGEmvL031880; Thu, 25 May 2023 16:15:09 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-2023-03-30; bh=9YlfsMla0gcP75BzBj+zsz44iYPjc18CeFarUyCO4hw=; b=rSnbPUTEvwSLEPfDVEMfBLGJKKomwxJ03ghOU7CgkQBgIRdtYtccn394O2Xj0nZN+IOo V+rak5YPq8wdUskBXTr8nSlvfEx2/FhlR+LjTwhieFUT5cJUMH1N+Uu7hRIy791Z1WgW 32FE1+xuqCf+T1smHM+eItp6qB8xjq2I/IesV3eMXHqgokfzCBwyZS+v99nCsUSFpjWa ptydC24pTlcrOUVwHQiTpta6FA0iCy+piPGBydAs0qPuPPiem1t2VwZruHUe8q3xe74q cnGZMwxmN0UcsETXKiqv3P0lRrB5Os3R/ggR2D7p3ckbabyaEBbzNgYtPPlJhfvWajAn ig== Received: from iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta03.appoci.oracle.com [130.35.103.27]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3qtb0y803m-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 25 May 2023 16:15:08 +0000 Received: from pps.filterd (iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (8.17.1.19/8.17.1.19) with ESMTP id 34PFwihk015873; Thu, 25 May 2023 16:15:07 GMT Received: from nam11-co1-obe.outbound.protection.outlook.com (mail-co1nam11lp2171.outbound.protection.outlook.com [104.47.56.171]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 3qqk6nccmp-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 25 May 2023 16:15:07 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Mf4eusveSlVkWsOYoZ9nWMyNofV4ksqMYDn8IRgxJ2XQGhI2cIA0me28J+IplO4RUu7hXJAL8WBYnb6IKYwJxnXl0ZLa2FbzyAx/aibHK6R4XoryhXn276l1FtfoN4zXazwlotJKNgjOqotxo1gXyF05J1UgwuwWQrMPJXx1HVVlxLIutocig4cJbPIPZnoeXOlw188ntAOY/rXbZpZeTGsAG9hp/Ji5N6FcmzT0DcscV5bcYzHvTLo4Dx765LPjUH9JyIWgQoTndVtb87L3gufoloRLEO9dWIFP7u1qQLCNcBXeF2tNBTqcWMH1Zoj1KB7VLxw+lY09oTi8qWv1bA== 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=9YlfsMla0gcP75BzBj+zsz44iYPjc18CeFarUyCO4hw=; b=cZPi2dduLnPRNL07HZXzIXCwANo8s32X7RYitOCzd3g5wU6v7R1p3yzgnkpsR9KlRL5AwPYsya9t8RrF929jbJkOLYbJuzOq7/U1NxHrzA6zSEKBECS2DBQ2aalj+DHIj4hc8JVVULqkFt4AMibh55bO0FwkX5TLlR99R4qWJ7VsJx9GTT0nfoO2Sv9NXjBaOcYcIZWMqpTMhCU7x8+OQpq6VWHG1IW8TCFnodQA4EFlZEFJY5xzL1uZMKQ91xGd8sAm9RFZkiDaI0g+frNQtusm49MAuzbNVChZvtkmdbHayEfU4Vw2uLU5heSuWhFqHTYu+Wb1xLTmyA6JIlLoBg== 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=9YlfsMla0gcP75BzBj+zsz44iYPjc18CeFarUyCO4hw=; b=FKBHG1RWoOX3nS5iI9gVi4rX124CnWSD1xOqEExvMdpHcDLEWVnAeqvH8zTLA/1JrNfToQ8qZ6KrbTSUzJWjNmbeUAt8E9Jplhe6dUkGL8XhqJ9haMpDBP0FCI6YrMS2hWgzmMN9VMRYu/v3felC0kSXmlKC+adO4yM3kmh9R0U= Received: from CH2PR10MB4344.namprd10.prod.outlook.com (2603:10b6:610:af::19) by CH0PR10MB7440.namprd10.prod.outlook.com (2603:10b6:610:18c::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6433.15; Thu, 25 May 2023 16:15:04 +0000 Received: from CH2PR10MB4344.namprd10.prod.outlook.com ([fe80::7aa1:df38:105a:21fa]) by CH2PR10MB4344.namprd10.prod.outlook.com ([fe80::7aa1:df38:105a:21fa%7]) with mapi id 15.20.6411.028; Thu, 25 May 2023 16:15:04 +0000 From: Qing Zhao To: joseph@codesourcery.com, richard.guenther@gmail.com, jakub@redhat.com, gcc-patches@gcc.gnu.org Cc: keescook@chromium.org, siddhesh@gotplt.org, uecker@tugraz.at, isanbard@gmail.com, Qing Zhao Subject: [V1][PATCH 1/3] Provide element_count attribute to flexible array member field (PR108896) Date: Thu, 25 May 2023 16:14:48 +0000 Message-Id: <20230525161450.3704901-2-qing.zhao@oracle.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230525161450.3704901-1-qing.zhao@oracle.com> References: <20230525161450.3704901-1-qing.zhao@oracle.com> Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: LO2P123CA0017.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:a6::29) To CH2PR10MB4344.namprd10.prod.outlook.com (2603:10b6:610:af::19) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH2PR10MB4344:EE_|CH0PR10MB7440:EE_ X-MS-Office365-Filtering-Correlation-Id: 3b3c5961-688e-48d3-591c-08db5d3b3300 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: JUNoQurT43HnKwWyfZqdtxF4VlNXYmmSjq/gwa9EdalAK+Zr4kv1dPeBzI5ZneAVendbQ7X5UvMbp+Ri7ByurXfiW3xjoD2vciz/Uteuv2wua8V/iWSDj/twMSo8qJCkTjzsaGbn/tFWW02jMqVvAXJDZkZFOQFE1oQzceRR2KJaLmSbIM3P1maZdNw1zP7gAvsBmkZYtAL9j8mPQWzdMgoBmCne9hg8alQ3KhZwS2XpywRdiSGy1Jidn9dvSrSGKVoAFUYXpEjluMA9yBhxuWzdtCrKDGqUQZtMDl3zoS3+4Y89/E/kwtW1DL9MTWbGNB0E4+HTdexYBCIn+XY86PRtL8cPKQpE1L2rUz7bImpUvRJ31oN6F+8zoxM7bhaTlRYkfYnUlN1m8UuVgYA7qNUXwJMj2JODvdDA/nJw8Jg2HQ/UeJMojvv0wHW72DKJTGtFw6k1f3hh/wD3JP5eLGXse8gztxrRIzyZSw0TUKshvAooForbqPlPDBEnNyF9XswlOvaqDTO8FqB0tm3gdRyJigfqfFpD8KZHAPvpF9Q= 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:(13230028)(346002)(376002)(39860400002)(366004)(396003)(136003)(451199021)(6666004)(4326008)(478600001)(107886003)(316002)(41300700001)(66476007)(66556008)(84970400001)(66946007)(6486002)(38100700002)(5660300002)(8936002)(8676002)(6506007)(26005)(44832011)(6512007)(1076003)(2616005)(83380400001)(30864003)(2906002)(186003)(36756003)(86362001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?W3EHgEzL0B8U4E+obHBcMWKm2ZcU5WJv9PQFlFDUEdqFMx2q1/feZSi51m3j?= =?us-ascii?Q?3S6a31HjHWfj4Jy7RG27VBZxQV6Gv6gY/ApcvrQvnBxlN4Q5OH0p50JerVvw?= =?us-ascii?Q?Fvs9aDfOfmuMLnWs+4/KBVbepK7lOPZZrWfkP3W7tg/tQp3E4qcyYTcBth79?= =?us-ascii?Q?rgsBHKQHMosPN6kB8MU3Bdx+Hq0F5UkFpZ/NmBMKgBDhxUWOOCsoN8l0moQJ?= =?us-ascii?Q?GM3gMXmVR7QUAvj21x88O2bP9UovzS4tkbSlmTyRF3rqLHmzsajasfI0qJwA?= =?us-ascii?Q?n29Gri/r5WEST1VkNqDxkL6mNJr2MK3BI82Se3m5gd0z0DArJjwbrnE7RWSm?= =?us-ascii?Q?f0A+UwDBscKx1fWKF8jampqhXcyAlWzcR/gglBMffvrJkFfyaJzVpBEVPLXY?= =?us-ascii?Q?mqfUnixX4RPVm6F+p/pVr4Wh18bN5bRuCcJa2tqjXuiniNvkM5Gx25dIrEfZ?= =?us-ascii?Q?SsLydLrJACe2WiF6UnZ0edFxO9TmvMs1t6q3IWV86BKgb5/oIaLBKxrw4RlQ?= =?us-ascii?Q?TVWoGW1HWYjSi0jx4+x9sSB+ZPDtGDoZUjpxfdT0XoXMz5omjyLp0HA2PQ5k?= =?us-ascii?Q?887c9ncO8oqbCUDGj+wLUuebhnpAX5WENs54rT28L72xTbJJfwd+oB3aC0rV?= =?us-ascii?Q?hjZaznWuKsrXs1+8gQMDwJA54Br4HJME6bEOvWGtzrM+OeyugrpeWkwU5e6B?= =?us-ascii?Q?j2M3d1g6aC4Fo63dXO9S4AQJ3n3ZYAcAUMGI+MbCm47a+y7i8zKQorryv69D?= =?us-ascii?Q?cg0AH4Beb8Zeuve3CNGF1X7TlTflsVovx8vKb9/ekfR3eZUX+lpbH1VXqCQk?= =?us-ascii?Q?aMtOgQp/QW/RLuV0H+5D4mkRR0IzSgdoX7lAm3ZU7kCAFEM/2nxKkQKllNQo?= =?us-ascii?Q?y/VsSCe88OOG/rDXnlf+Oh50RcjUjC98JYBV14zbpxvHE+QtNJdkm4f0RtZ7?= =?us-ascii?Q?Cgzzs5mxdb1zWZXEGYlAG31WYwOZVkui1s6eFoTAyWyO5L/vbLqTmh2t0Z4S?= =?us-ascii?Q?MjzWFdWIMUJrqjMzstjuiWCPUDbqHotbNbilJuGKes9erSeWYrJFBFLB5ERv?= =?us-ascii?Q?hDWCC4MMtiYBlBHkZyN0Vmz7cK5o6sj8Va4Pakw6fflPCCWowDw84qpfm/mX?= =?us-ascii?Q?xQnMKeNBpEkdkvaaFhgdEEck66/Nd5SudmqGNyEiS/Mtr1LA4bdv5XnykfcJ?= =?us-ascii?Q?TTF7rKBq3Ox+93XmbGb2Ygcucaogy+UacxiBy3VQFMbWtYniW85EpZae4y6y?= =?us-ascii?Q?D2KmNoRrWPQxe4FBWhKLN/V9Y3abNgiSX60PavL+/mlSYr86Sqg0bdgHLqzc?= =?us-ascii?Q?tJOjXk3aWafZe6dPwlz3TcglQMeSB23deEJB2UAPQk9iz4+V9whqTwRc4O9f?= =?us-ascii?Q?UmbxotTt3Pc6dpZG9E39hSo/jXhr53jNOOLFTOy5vJ+xBxZ5hiUwXPEIy3M7?= =?us-ascii?Q?2QuCxP4rzkewtm35xZpu2Ni/GF6UMvVbnRlCUxhZEkqeOkKZqZCduP4TL6aP?= =?us-ascii?Q?tD0fYmQ1JME2yQwHyBLEi8ouiQyWCPP+Uy9HwT5A5U/a5mchCd2GkJPvR+Dq?= =?us-ascii?Q?yADsbMR48/J9WHtRwV95RE8GF6nWA30omhGrlQ7w?= X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: =?us-ascii?Q?2iaJ+ts4HoJciruDdUm23xNadxFYokSTeX6oNNHsKMIQxH7jduQp5KB6UYiL?= =?us-ascii?Q?J7uIQ2FDhoAbx3t/D0kM4f4iN27yKCTcm9L+ee9AhCwSOEZD5YXBE7rRlrsH?= =?us-ascii?Q?qwFVSzUGA8olNfIiQGT5nBpMUKdfYoiJSwmIwuH+uRWflpssviCBja7ahfoJ?= =?us-ascii?Q?OQu8X1gXVthN6JtVneIOTjys4rCYOeCdKAVpqeFILSG7GDQ923ih1dUi9OOT?= =?us-ascii?Q?Hp00yMp5eSVA12/rJTxaDa1iAsX1t5sC1qHsXqh5+HQxJ/ty4itmxrPNCA8L?= =?us-ascii?Q?nSmMSDZ44yoZJUf21pQcX3Jmt6C0PGGjnKeqhH9/DgCd1dYs7buKXWPgqx5B?= =?us-ascii?Q?4DYkb4/sTnyaBEsiMQPtcm2/TJrrm5IfuNfM+j9UwaZ1sJYH86qAcQ2gaFvl?= =?us-ascii?Q?Cygyoqvw/JC3dXA6t5EyTPWzmYxsrR11B3pKt3L0XFOYdhgcdeGrOZvRPODE?= =?us-ascii?Q?g1W76vamLNmYkXEg6EAS2qUTYHFXddIAaLYak4VW1gV6muZxtOMKOCVLfT5W?= =?us-ascii?Q?d0W5LtcrZtmc9mk9CqOxPhJbKwrHEOfLnsZbhgoIAoTu8262mRnDqGLzF/uK?= =?us-ascii?Q?gHUcl08jeOFZRcxqnsiVf1szxR6ZSSxaID9s1MbMDjJ7iuXnXVBcOoj/8Qk8?= =?us-ascii?Q?eVO3ky8mv7DBYFpHPVSa2cDrjTiDmpIL+jFAxsWEHQcy4EarsXapI/QQYrex?= =?us-ascii?Q?tJhoHnJ00bVgkHDhopeZExsPDg4tEw9BikBtrx43OCzjkgMSQ8qN6MWMt7De?= =?us-ascii?Q?0LLpHqcrmF6J2AuAM5tfS6f5DdmrL8CsPvu7WsN7ZLeJO8hvkDWCHmdMY8Kh?= =?us-ascii?Q?hPknHyZq8vUsIO29I38oC3uRTje9xB7JHuhcII/gSA6gcz/SW3LPeyl7d9iW?= =?us-ascii?Q?GORpfhZNC5E3L+8hEFenbN3A91ImjdimlmwY21fYG2K4JQTJbhoxqtysC/0x?= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 3b3c5961-688e-48d3-591c-08db5d3b3300 X-MS-Exchange-CrossTenant-AuthSource: CH2PR10MB4344.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 May 2023 16:15:04.5369 (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: lOK6IE+wUTChdDtlmRtdZvT+agg4cCDfh3j2/VFCp019uDBRJ4w6KJ3NYmtZB0Vrch9jPvl6BTAAU16o+cFAIA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH0PR10MB7440 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.254,Aquarius:18.0.957,Hydra:6.0.573,FMLib:17.11.176.26 definitions=2023-05-25_09,2023-05-25_03,2023-05-22_02 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 phishscore=0 malwarescore=0 bulkscore=0 mlxlogscore=999 mlxscore=0 adultscore=0 spamscore=0 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2304280000 definitions=main-2305250134 X-Proofpoint-GUID: FuoNRDHPWfBhuiBzCzft75QGuOGJp7vO X-Proofpoint-ORIG-GUID: FuoNRDHPWfBhuiBzCzft75QGuOGJp7vO 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_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: 'element_count ("COUNT")' The 'element_count' attribute may be attached to the flexible array member of a structure. It indicates that the number of the elements of the array is given by the field named "COUNT" in the same structure as the flexible array member. GCC uses this information to improve the results of '__builtin_dynamic_object_size' and array bound sanitizer. For instance, the following declaration: struct P { size_t count; int array[] __attribute__ ((element_count ("count"))); }; specify that 'array' is a flexible array member whose number of element is given by the field "'count'" in the same structure. The number of elements information provided by this attribute can be used by __builtin_dynamic_object_size and array bound sanitizer to detect out-of-bound errors for flexible array member references. 2023-05-17 Qing Zhao gcc/c-family/ChangeLog: PR C/108896 * c-attribs.cc (handle_element_count_attribute): New function. * c-common.cc (c_flexible_array_member_type_p): To this. * c-common.h (c_flexible_array_member_type_p): New prototype. gcc/c/ChangeLog: PR C/108896 * c-decl.cc (flexible_array_member_type_p): Renamed and moved to... (add_flexible_array_elts_to_size): Use renamed function. (is_flexible_array_member_p): Use renamed function. (verify_element_count_attribute): New function. (finish_struct): Use renamed function and verify element count attribute. gcc/ChangeLog: PR C/108896 * doc/extend.texi: Document attribute element_count. gcc/testsuite/ChangeLog: PR C/108896 * gcc.dg/flex-array-element-count.c: New test. --- gcc/c-family/c-attribs.cc | 51 ++++++++++++++++ gcc/c-family/c-common.cc | 13 ++++ gcc/c-family/c-common.h | 1 + gcc/c/c-decl.cc | 61 ++++++++++++++----- gcc/doc/extend.texi | 21 +++++++ .../gcc.dg/flex-array-element-count.c | 27 ++++++++ 6 files changed, 158 insertions(+), 16 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/flex-array-element-count.c diff --git a/gcc/c-family/c-attribs.cc b/gcc/c-family/c-attribs.cc index 072cfb69147..d45d11077c3 100644 --- a/gcc/c-family/c-attribs.cc +++ b/gcc/c-family/c-attribs.cc @@ -103,6 +103,8 @@ static tree handle_warn_if_not_aligned_attribute (tree *, tree, tree, int, bool *); static tree handle_strict_flex_array_attribute (tree *, tree, tree, int, bool *); +static tree handle_element_count_attribute (tree *, tree, tree, + int, bool *); static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ; static tree handle_noplt_attribute (tree *, tree, tree, int, bool *) ; static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *); @@ -373,6 +375,8 @@ const struct attribute_spec c_common_attribute_table[] = handle_warn_if_not_aligned_attribute, NULL }, { "strict_flex_array", 1, 1, true, false, false, false, handle_strict_flex_array_attribute, NULL }, + { "element_count", 1, 1, true, false, false, false, + handle_element_count_attribute, NULL }, { "weak", 0, 0, true, false, false, false, handle_weak_attribute, NULL }, { "noplt", 0, 0, true, false, false, false, @@ -2555,6 +2559,53 @@ handle_strict_flex_array_attribute (tree *node, tree name, return NULL_TREE; } +/* Handle a "element_count" attribute; arguments as in + struct attribute_spec.handler. */ + +static tree +handle_element_count_attribute (tree *node, tree name, + tree args, int ARG_UNUSED (flags), + bool *no_add_attrs) +{ + tree decl = *node; + tree argval = TREE_VALUE (args); + + /* This attribute only applies to field decls of a structure. */ + if (TREE_CODE (decl) != FIELD_DECL) + { + error_at (DECL_SOURCE_LOCATION (decl), + "%qE attribute may not be specified for non-field" + " declaration %q+D", name, decl); + *no_add_attrs = true; + } + /* This attribute only applies to field with array type. */ + else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE) + { + error_at (DECL_SOURCE_LOCATION (decl), + "%qE attribute may not be specified for a non-array field", + name); + *no_add_attrs = true; + } + /* This attribute only applies to a C99 flexible array member type. */ + else if (! c_flexible_array_member_type_p (TREE_TYPE (decl))) + { + error_at (DECL_SOURCE_LOCATION (decl), + "%qE attribute may not be specified for a non" + " flexible array member field", + name); + *no_add_attrs = true; + } + /* The argument of the attribute should be a string. */ + else if (TREE_CODE (argval) != STRING_CST) + { + error_at (DECL_SOURCE_LOCATION (decl), + "%qE attribute argument not a string", name); + *no_add_attrs = true; + } + + return NULL_TREE; +} + /* Handle a "weak" attribute; arguments as in struct attribute_spec.handler. */ diff --git a/gcc/c-family/c-common.cc b/gcc/c-family/c-common.cc index 2b4c82facf7..b2bf7a230e2 100644 --- a/gcc/c-family/c-common.cc +++ b/gcc/c-family/c-common.cc @@ -9516,6 +9516,19 @@ c_common_finalize_early_debug (void) (*debug_hooks->early_global_decl) (cnode->decl); } +/* Determine whether TYPE is a ISO C99 flexible array memeber type "[]". */ +bool +c_flexible_array_member_type_p (const_tree type) +{ + if (TREE_CODE (type) == ARRAY_TYPE + && TYPE_SIZE (type) == NULL_TREE + && TYPE_DOMAIN (type) != NULL_TREE + && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE) + return true; + + return false; +} + /* Get the LEVEL of the strict_flex_array for the ARRAY_FIELD based on the values of attribute strict_flex_array and the flag_strict_flex_arrays. */ unsigned int diff --git a/gcc/c-family/c-common.h b/gcc/c-family/c-common.h index f96350b64af..77f6633b862 100644 --- a/gcc/c-family/c-common.h +++ b/gcc/c-family/c-common.h @@ -907,6 +907,7 @@ extern tree fold_for_warn (tree); extern tree c_common_get_narrower (tree, int *); extern bool get_attribute_operand (tree, unsigned HOST_WIDE_INT *); extern void c_common_finalize_early_debug (void); +extern bool c_flexible_array_member_type_p (const_tree); extern unsigned int c_strict_flex_array_level_of (tree); extern bool c_option_is_from_cpp_diagnostics (int); diff --git a/gcc/c/c-decl.cc b/gcc/c/c-decl.cc index f8ede362bfd..0f25fe0be0d 100644 --- a/gcc/c/c-decl.cc +++ b/gcc/c/c-decl.cc @@ -5187,19 +5187,6 @@ set_array_declarator_inner (struct c_declarator *decl, return decl; } -/* Determine whether TYPE is a ISO C99 flexible array memeber type "[]". */ -static bool -flexible_array_member_type_p (const_tree type) -{ - if (TREE_CODE (type) == ARRAY_TYPE - && TYPE_SIZE (type) == NULL_TREE - && TYPE_DOMAIN (type) != NULL_TREE - && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE) - return true; - - return false; -} - /* Determine whether TYPE is a one-element array type "[1]". */ static bool one_element_array_type_p (const_tree type) @@ -5236,7 +5223,7 @@ add_flexible_array_elts_to_size (tree decl, tree init) elt = CONSTRUCTOR_ELTS (init)->last ().value; type = TREE_TYPE (elt); - if (flexible_array_member_type_p (type)) + if (c_flexible_array_member_type_p (type)) { complete_array_type (&type, elt, false); DECL_SIZE (decl) @@ -9087,7 +9074,7 @@ is_flexible_array_member_p (bool is_last_field, bool is_zero_length_array = zero_length_array_type_p (TREE_TYPE (x)); bool is_one_element_array = one_element_array_type_p (TREE_TYPE (x)); - bool is_flexible_array = flexible_array_member_type_p (TREE_TYPE (x)); + bool is_flexible_array = c_flexible_array_member_type_p (TREE_TYPE (x)); unsigned int strict_flex_array_level = c_strict_flex_array_level_of (x); @@ -9117,6 +9104,45 @@ is_flexible_array_member_p (bool is_last_field, return false; } +/* Verify the argument of the element_count attribute of the flexible array + member FIELD_DECL is a valid field of the containing structure's fieldlist, + FIELDLIST, Report error when it's not. */ +static void +verify_element_count_attribute (tree fieldlist, tree field_decl) +{ + tree attr_element_count = lookup_attribute ("element_count", + DECL_ATTRIBUTES (field_decl)); + + if (!attr_element_count) + return; + + /* If there is an element_count attribute attached to the field, + verify it. */ + + const char *fieldname + = TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr_element_count))); + + /* Verify the argument of the attrbute is a valid field of the + containing structure. */ + + tree element_count_field = NULL_TREE; + for (tree field = fieldlist; field; field = DECL_CHAIN (field)) + if (TREE_CODE (field) == FIELD_DECL + && DECL_NAME (field) != NULL + && strcmp (IDENTIFIER_POINTER (DECL_NAME (field)), fieldname) == 0) + { + element_count_field = field; + break; + } + + /* Error when the field is not found in the containing structure. */ + if (!element_count_field) + error_at (DECL_SOURCE_LOCATION (field_decl), + "%qE attribute argument not a field declaration" + " in the same structure", + (get_attribute_name (attr_element_count))); + return; +} /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T. LOC is the location of the RECORD_TYPE or UNION_TYPE's definition. @@ -9237,7 +9263,7 @@ finish_struct (location_t loc, tree t, tree fieldlist, tree attributes, DECL_PACKED (x) = 1; /* Detect flexible array member in an invalid context. */ - if (flexible_array_member_type_p (TREE_TYPE (x))) + if (c_flexible_array_member_type_p (TREE_TYPE (x))) { if (TREE_CODE (t) == UNION_TYPE) { @@ -9258,6 +9284,9 @@ finish_struct (location_t loc, tree t, tree fieldlist, tree attributes, "members"); TREE_TYPE (x) = error_mark_node; } + /* if there is an element_count attribute attached to this field, + verify it. */ + verify_element_count_attribute (fieldlist, x); } if (pedantic && TREE_CODE (t) == RECORD_TYPE diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi index 69b21a75e62..cdbe3923ef1 100644 --- a/gcc/doc/extend.texi +++ b/gcc/doc/extend.texi @@ -7501,6 +7501,27 @@ When both the attribute and the option present at the same time, the level of the strictness for the specific trailing array field is determined by the attribute. +@cindex @code{element_count} variable attribute +@item element_count ("@var{count}") +The @code{element_count} attribute may be attached to the flexible array +member of a structure. It indicates that the number of the elements of the +array is given by the field named "@var{count}" in the same structure as the +flexible array member. GCC uses this information to improve the results of +@code{__builtin_dynamic_object_size} and array bound sanitizer. + +For instance, the following declaration: + +@smallexample +struct P @{ + size_t count; + int array[] __attribute__ ((element_count ("count"))); +@}; +@end smallexample + +@noindent +specify that @code{array} is a flexible array member whose number of element +is given by the field "@code{count}" in the same structure. + @cindex @code{alloc_size} variable attribute @item alloc_size (@var{position}) @itemx alloc_size (@var{position-1}, @var{position-2}) diff --git a/gcc/testsuite/gcc.dg/flex-array-element-count.c b/gcc/testsuite/gcc.dg/flex-array-element-count.c new file mode 100644 index 00000000000..988f41e9f5e --- /dev/null +++ b/gcc/testsuite/gcc.dg/flex-array-element-count.c @@ -0,0 +1,27 @@ +/* testing the correct usage of attribute element_count. */ +/* { dg-do compile } */ +/* { dg-options "-O2" } */ + +int size; +int x __attribute ((element_count ("size"))); /* { dg-error "attribute may not be specified for non-field declaration" } */ + +struct trailing { + int count; + int field __attribute ((element_count ("count"))); /* { dg-error "attribute may not be specified for a non-array field" } */ +}; + +struct trailing_1 { + int count; + int array_1[0] __attribute ((element_count ("count"))); /* { dg-error "attribute may not be specified for a non flexible array member field" } */ +}; + +int count; +struct trailing_array_2 { + int count; + int array_2[] __attribute ((element_count (count))); /* { dg-error "attribute argument not a string" } */ +}; + +struct trailing_array_3 { + int other; + int array_3[] __attribute ((element_count ("count"))); /* { dg-error "attribute argument not a field declaration in the same structure" } */ +}; -- 2.31.1