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 AE4C8385840F for ; Thu, 30 May 2024 12:27:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org AE4C8385840F Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=oracle.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=oracle.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org AE4C8385840F Authentication-Results: server2.sourceware.org; arc=pass smtp.remote-ip=205.220.177.32 ARC-Seal: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1717072037; cv=pass; b=afQy/WSwrmab3uheAHD5iAd3ukgJyyZWuz2fVnLDV5c7Sfc9VuG4M571WwVyTBZefXeFHGLWt6zUdEtsCVRNU2kv09b6c9eYUnNgHDxjHRjI/Fm0kR44rpSCLqHQr9YtSC8X0AMtOUpQnvW1Yc7DsrYjqTRTx9OeupuIgo0I1PE= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1717072037; c=relaxed/simple; bh=lrGZ/ryE3aufE9qdNDRmb1a+EdxXr6rcgUk6L/H7WWg=; h=DKIM-Signature:DKIM-Signature:From:To:Subject:Date:Message-Id: MIME-Version; b=Kj7+ME9CPelC1IiXb9g5wr5NtYCJv9UeLdCrwJqTZHk4wIoEPiEKh6YHBFMpPp6GM5q1VC55K9PAWRU7Ev+9iC3+HwbNsxUQevI3NxgzrgsjUXgDk5SvofqodjKdK75CIcUgjyjTWGOS43OGMyZuUK/2QmjRpb/wzfY0Bh7872M= ARC-Authentication-Results: i=2; server2.sourceware.org Received: from pps.filterd (m0246630.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 44U7mwIm018664; Thu, 30 May 2024 12:27:11 GMT DKIM-Signature: =?UTF-8?Q?v=3D1;_a=3Drsa-sha256;_c=3Drelaxed/relaxed;_d=3Doracle.com;_h?= =?UTF-8?Q?=3Dcc:content-transfer-encoding:content-type:date:from:in-reply?= =?UTF-8?Q?-to:message-id:mime-version:references:subject:to;_s=3Dcorp-202?= =?UTF-8?Q?3-11-20;_bh=3Dg+phD/5rkEFFRDcmRJwxbNPg1M/tlX5cTORTuKJqeeM=3D;_b?= =?UTF-8?Q?=3DdRLbwftDWKmRYfxT8cxDLfBEehvR+rFA9/lHl0SEQNbe+O0tEOwKvGgG9arx?= =?UTF-8?Q?eetfQNYD_iMZIUnizFBsVSnjx58ZvipM5m7UAUhkIeCwrK5QGZ6iSJhA//O4KMr?= =?UTF-8?Q?8meC6HEuPbjRM8_ZTNeweQip6P/Ih+YZSwWUtuzHIPxxLXUvreVRndPKd20YfeX?= =?UTF-8?Q?fTiJ5wjxjn0bH6N9uFXS_7arhCGL/SpBFHioRI/REKN9vw5whssgmic+TBqqyux?= =?UTF-8?Q?njkQ+qlId1a8sBiut9m6OSpMb/_Zx9iBDDdC91RyFXL/kNU3Q429PevU2KxKVHH?= =?UTF-8?Q?wL0BAsTKcjy9EXCOZ6qhrmLzt3E27TBX_KQ=3D=3D_?= Received: from phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta01.appoci.oracle.com [138.1.114.2]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3yb8j88r1n-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 30 May 2024 12:27:10 +0000 Received: from pps.filterd (phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (8.17.1.19/8.17.1.19) with ESMTP id 44UC6pC7026523; Thu, 30 May 2024 12:27:09 GMT Received: from nam10-dm6-obe.outbound.protection.outlook.com (mail-dm6nam10lp2100.outbound.protection.outlook.com [104.47.58.100]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 3yc508fwvb-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 30 May 2024 12:27:09 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=bd1btPfaXI4lCiBD4rB7GrsONnzcJqH6eg8noDdwCOA1QyC5xb4Vah/ywB7mQrj9C47pXc5TJxZmdW1Yt/THaMjufe18bbb1+9UmDHXH+NihttxvKn1A/X0PldArs0/taRfwgcZEgIhhtBWMK6BTcnNZ43tqFe4VT3qyhhtg4s9Ym3isGUvhspAV7jSFlBCbEdFlNJDJucepGcwJffe4DXniveJkFjqwKBrYXzg0q9dpJndFJB4MpAr27OsYZrTQ76PAxywnewfPSftj1GKY8zMaqS6a8lLpCSksHRrK6cj+g2S+wZUZPh8BV/pIfoRmHCljaaX3rVXf8GCxS3Brew== 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=g+phD/5rkEFFRDcmRJwxbNPg1M/tlX5cTORTuKJqeeM=; b=YaSnweY3Leb36k5FiRqUTk1+WN+283m9NG18CuAm6EuN8e1q/vlPtZcKGTM1aSzUFEAYbQkq8f3Eo14Ogsz3/gf2kjP7dKTps2fq/RflKEAD62fxbj9RsFS7BDA7WZIyHTTOg/0qHN3+p8pTxsOlKGpVyuJgwkCrFsJbz2E87axkQwJQKtvF/crCujfqYU4w7UMVmgJ80fn5wdXNfdP8bh5hcnQyzfMThFvIh8cSeIdWuhHlTMiOKtD6snGNUDAuIC2bkaYAQkME3Nv54iQmcLcl5rkp6a1/lOZpbpyzS4zFQIoq0gDmzP99uliq/yRv8UEDgCOP6grUsILu1uF2Zw== 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=g+phD/5rkEFFRDcmRJwxbNPg1M/tlX5cTORTuKJqeeM=; b=KJcoxj+qnrvwDreGi5VS221a5TR2ZsM22FpZzOvlF9ZfqllXjLRHG1JsF5Y8xsR5rHuHhxya8YKbFTZ7iifBzR+H1UOZx0Q82w4M/klD7b9O9/mY+wq9hy1bb9nmdflKOtOtPu2ZSwM+5tNI2aAEzdfirahn/0SqVTfsAAsd5Vw= Received: from CY8PR10MB6538.namprd10.prod.outlook.com (2603:10b6:930:5a::17) by PH7PR10MB6155.namprd10.prod.outlook.com (2603:10b6:510:1f4::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7633.21; Thu, 30 May 2024 12:27:07 +0000 Received: from CY8PR10MB6538.namprd10.prod.outlook.com ([fe80::8a:61cd:764a:e47]) by CY8PR10MB6538.namprd10.prod.outlook.com ([fe80::8a:61cd:764a:e47%5]) with mapi id 15.20.7633.021; Thu, 30 May 2024 12:27:06 +0000 From: Qing Zhao To: rguenther@suse.de, josmyers@redhat.com Cc: siddhesh@gotplt.org, uecker@tugraz.at, keescook@chromium.org, isanbard@gmail.com, gcc-patches@gcc.gnu.org, Qing Zhao Subject: [PATCH v10 1/5] Provide counted_by attribute to flexible array member field (PR108896) Date: Thu, 30 May 2024 12:26:56 +0000 Message-Id: <20240530122700.1516243-2-qing.zhao@oracle.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20240530122700.1516243-1-qing.zhao@oracle.com> References: <20240530122700.1516243-1-qing.zhao@oracle.com> Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: BL6PEPF00013E06.NAMP222.PROD.OUTLOOK.COM (2603:10b6:22e:400:0:1001:0:7) To CY8PR10MB6538.namprd10.prod.outlook.com (2603:10b6:930:5a::17) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY8PR10MB6538:EE_|PH7PR10MB6155:EE_ X-MS-Office365-Filtering-Correlation-Id: 6795880d-5a39-4b8e-ba08-08dc80a3d1c7 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230031|376005|366007|1800799015; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?rR06hmOScrMNScelc2Eumx8g2Ni0p4bS3MPxxP8Vk7TwbXB2rEJuQgMcT5uM?= =?us-ascii?Q?dcm5juhVFyqlNgmpfhB3SHoHlg/jonDSMmWnxMTwr5WjiSHbGR42S9WDbDPK?= =?us-ascii?Q?WSd3SCk1mjQ6g6sc3AbTGvsmywgLpWxJR2u1YyEAklsaBTPydkulfG2B8WXZ?= =?us-ascii?Q?m/HvgaA7IoV+RRaHkEjC/z/WU2Kn1NxhSbYYcRwupPIRlBgxzG3DU1xNSxnN?= =?us-ascii?Q?isCfyaXng/J1tIy8A6Ldo6cw/TclxfjGHyzEbxxC3UgJU6s7GgvL3SDVwhk2?= =?us-ascii?Q?XAcnS/GbdQA52pqMT8txJKd4nYFmzPI9PmTyPI5XhwFsnFHU2MeFWd9XsmWG?= =?us-ascii?Q?0G+Z7O4Rk9RWFnm+idPe6n6j/nVmS+yda0HuvXa4v6NzkA7JmO4osX6K1mvS?= =?us-ascii?Q?0FsDN/srlxNDBv77JUWgwffjuI/yV5Xe4DafGcwQrtMGQpM1wrWcQEixl2RY?= =?us-ascii?Q?LYRhUHHve0lfB7NYtJ+zUf5AZIzaO7aSzhwmecmxHnRu6shG0EcfsM0Ti0cL?= =?us-ascii?Q?x8ntxnHPaJ3jcy4vEliWOYgjSutFV2OGg+CJsF6gRb6UgE8RKFA6pK8wMMQ1?= =?us-ascii?Q?Yjaw5EkIq2LBXzRmWsuL4oJUlO/+PgK1KLaK/5rDYFDtkfbMnG9AtC/ZZgam?= =?us-ascii?Q?4HWucvZ7ocLSiP6wh/wGCY5hTpRCTqU49mH6YgZe2Lehv+gl2jJ+2dBegItJ?= =?us-ascii?Q?2ZukENFKK2ESq8B1RQi0g6YYVwvmUY/+j0isQyAQAu10bU3bOnyRBjHJLo7s?= =?us-ascii?Q?hUSMn4pqXI3NyxmvSinKzpZ0Quz0Tt1Tf5maDKq4vE/eAVwLletchaNk9Q0M?= =?us-ascii?Q?I7xTfWBo4NPkeuLC+pOxEESimzpR4RM8EIPYmXooMylBz49WOFPMFpJLzcQa?= =?us-ascii?Q?L4qfowAjqQ3nkfY28o5OyGslOMrAvXAqbKf4Lwud322Yj3gbn915gMzIZ94G?= =?us-ascii?Q?WI+aiYc93vOuT2c+uLainAVplW5/XASpV6aqmW4HZiFcdwawxGVzcoXiYMWU?= =?us-ascii?Q?HyvhQPMVSd+wB5k4RlFus/BfH/NwWkX0EHxvaqd6WiGov9LhNZQvxkKVlEgc?= =?us-ascii?Q?qLizkIM8nfVmsJWnQH4OuyDkK86kFtA6qlu3zunI1QcNZr3ZGVD3P2jhhPBG?= =?us-ascii?Q?l8cARLjTR5HZLB/nrRZ91LU4ge3gNinKmyP170In43toU9Xn35vpJwEZrvxT?= =?us-ascii?Q?igV57WT5gcqk2kWF3ol2ler5p05yRJzXPS943LCugkLOMMe1EqvF4KSMekY?= =?us-ascii?Q?=3D?= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:CY8PR10MB6538.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230031)(376005)(366007)(1800799015);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?4pM4ZUSrDg33xSs8z9AP7hpO2gesQKmBi9AhfTNPyRjUR2wNEVZFWX0NcW4P?= =?us-ascii?Q?IFQJpK3Fmtp62rWxjCBpusIX3J605jciVu58uLhUVICoag7911upbil52KNm?= =?us-ascii?Q?3dDSaUBUgYswyOVmFdIHb1uQEFFoRoApoTBEurgVmK4LgE9bDT8/9E9876Vq?= =?us-ascii?Q?bAHY6rl74LJqlNqGlcOz9dMNPY77bEAzxGOkF9526EMQUGp5UaLVw9tZ4ZJu?= =?us-ascii?Q?9MNFO3T51BuvJ7iwAfFirK52CCCuxEku0Omh0xDQ9V9xWgRpQue6KvPHqTZ6?= =?us-ascii?Q?3uTV1oqaIx4LZjOsJ1jKARxeKDU/OArlYdkx+ZcKKDzPiWDytCQoOWFZrmDt?= =?us-ascii?Q?dBd7XB5tgezouCUE0WIAnWI3Ex+OXg8fiuQ08irGdYGiHZAulzLA1/48VXt9?= =?us-ascii?Q?SvGg7NObwSWIliknVsOm3lQe8KsRuKGGcxHAfeC29K/Gc9gtwscz9NihqSAG?= =?us-ascii?Q?a50LU1HIYz3kGwGqVjKNIOVEtHFxfKkHr28360Evpf/yZQLPWNaAzqIvL3Iv?= =?us-ascii?Q?IBDDAyZ8i94ZGhilto+RvgEHa+PtaOyXziDlwX4EDTvZC1oYGpvIiik9m4j5?= =?us-ascii?Q?cWfsybhH78qpH2ExUmUtv9e3RMi/R5h1emTXS1ZfhdozVvKx9JPQbWOXQyyC?= =?us-ascii?Q?qsGsVxt2sre6JSMnQmniia4ljUVZfi6jJA3kParCvOCHzFRkkXQeIQlpDU78?= =?us-ascii?Q?bqQlF8mXXByxrFONGh/+GVSz0U+JqqXPPde08UDHMaZoL7iPMRLBkNOqu7in?= =?us-ascii?Q?Mx+eicoJcO12dPzd1wwapaXqlOe51ISwqeSlI4V0/Z1vXDRLhKNO83Ztglbj?= =?us-ascii?Q?MK+ALAfkENi8B2Sg8o2tLxIRp3XjsLRTYDxRWRIkItk3OvOlS3JODyZgg1g8?= =?us-ascii?Q?WV365CZGR3hySk8KFhy1Jw0UpKbqOGLEqv2zEWMLS7DXm9+/PgCJdvF7FXiu?= =?us-ascii?Q?/x885Ze8GRywgSaUpijLhPsa9oLST6+XrYpk8/lenpLaaAswjaxu13Y5f8CZ?= =?us-ascii?Q?aGOEIM+hzgj5ynGfXK0RWZbO2rp3kYyH05aieC/ooD4iwrvUrAzNoK4LGJt/?= =?us-ascii?Q?Cai+eLmQ5Vtk4AnTX/pcIGZQ5Ka5u0eLtuPUwbe5IDthe5uXB6auC9R0Mc0q?= =?us-ascii?Q?OhpEQQI7sm1Sxc9Fx3EsQ+P28E99Y6rUzZr/1qRKRw7o+oDek/apM+DVOjzb?= =?us-ascii?Q?pCFOPNu7OnFodvVYVkiNdPZEj2FC/lpotdbteXGfO57kRs5nN0GWfY6UfxO5?= =?us-ascii?Q?JHDhxCcGE2jDW28bapR3WjgkWCBEy3nRunAiN4WW2+2r2j5BgIfs1hz4Dxf1?= =?us-ascii?Q?rULlJWjo32hCe2VO+M4MBAP31CQapRnIQjRroGmL9/R03E8kI/mBbO5HGdHR?= =?us-ascii?Q?xpf45pPXpARvFCPIh/AMmcMfIbIYuuyTLxNSzkget9xyaZAWC6nlkE68Wfhn?= =?us-ascii?Q?fvEiln5J1VlXhfmig7SQTqCXGfqq7oMUnLJXPzsmKf0Wm9Y6+sIpc2eKrgJ8?= =?us-ascii?Q?ivdxe8CbA8iTU2KozQncZzwJFN0WhA5sxh9jQCrcxgZ0WqazKCIvx60UisBv?= =?us-ascii?Q?Pic1wXwHIlYKnYBj2QgnzbH9KD/0SpHRmytIeb7d?= X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: 2HLdUy4jjHmTwM7h8BLKHPhL1ztse8IvGAhHJTh0d8Nw+buIwPCtAkQl2vVY7zlfFYI89qz0w5hKY51A5kPUvpvhR24JSTjyNt154wh2wsAeS39gSSKGMvt8fiW0yzcQE7pqCBPlbPjtkOEyKnUZy2x+bTcSzdp72sith4vSTD+PQfJf0V+9LsfiHFkfCs1gC6tgDJLM25ds0XgyG0wJTxU56Z1PYTfyGF+Vd2pKQI7syFClHz+f7GCJlMoOwFG2A+GptoGHRSt4/HVXXLhabGelNQXJ3rI1mB3Vn8BXAEB74NlxZo4TBwaquocZ6FNks/Wcwd2fKSRehqXUhhA8OgnpCzwTRRBZOl6HQx0JqrjfZ0ooyzyL6oPMQUy38VH2YOnUv74MtbYMNzLnaaYi1EGXiJFdcTGWrmduAW0TQx9hjrBqFLxMjpb3gZ6v1OfiH6lHt535ciCGUXxW9TgUFazFg48lXV+USG81Sg63SHB6ZeWrw08kXnUlT7/JOJS1zO+bgh7JccM9WvU+Pj97IghelofgIrQ5j8/7ZRuiF+BJGBouF0job674fIvDmJzvRnvclzGjEF2RESdtHr8tURBNS4kEOi2k4aweEGIFHIE= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 6795880d-5a39-4b8e-ba08-08dc80a3d1c7 X-MS-Exchange-CrossTenant-AuthSource: CY8PR10MB6538.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 May 2024 12:27:06.8411 (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: P/CKuMW2rr7YW4k+IB6QTdk+Nm48DFHve2Bh9p95yNy3ewfLe2RCZzrCEMgwZ/vLWkGqta+o7p/BTn7ROWJ5Fw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR10MB6155 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1039,Hydra:6.0.650,FMLib:17.12.28.16 definitions=2024-05-30_09,2024-05-28_01,2024-05-17_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxlogscore=956 mlxscore=0 adultscore=0 suspectscore=0 malwarescore=0 phishscore=0 bulkscore=0 spamscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2405010000 definitions=main-2405300094 X-Proofpoint-ORIG-GUID: bl2khOQXPFOSt66TJFiqMgOl8uXkZyXK X-Proofpoint-GUID: bl2khOQXPFOSt66TJFiqMgOl8uXkZyXK X-Spam-Status: No, score=-10.8 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,GIT_PATCH_0,KAM_DMARC_QUARANTINE,RCVD_IN_DNSWL_LOW,RCVD_IN_MSPIKE_H4,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: 'counted_by (COUNT)' The 'counted_by' attribute may be attached to the C99 flexible array member of a structure. It indicates that the number of the elements of the array is given by the field "COUNT" in the same structure as the flexible array member. GCC may use this information to improve detection of object size information for such structures and provide better results in compile-time diagnostics and runtime features like the array bound sanitizer and the '__builtin_dynamic_object_size'. For instance, the following code: struct P { size_t count; char other; char array[] __attribute__ ((counted_by (count))); } *p; specifies that the 'array' is a flexible array member whose number of elements is given by the field 'count' in the same structure. The field that represents the number of the elements should have an integer type. Otherwise, the compiler reports an error and ignores the attribute. When the field that represents the number of the elements is assigned a negative integer value, the compiler treats the value as zero. An explicit 'counted_by' annotation defines a relationship between two objects, 'p->array' and 'p->count', and there are the following requirementthat on the relationship between this pair: * 'p->count' must be initialized before the first reference to 'p->array'; * 'p->array' has _at least_ 'p->count' number of elements available all the time. This relationship must hold even after any of these related objects are updated during the program. It's the user's responsibility to make sure the above requirements to be kept all the time. Otherwise the compiler reports warnings, at the same time, the results of the array bound sanitizer and the '__builtin_dynamic_object_size' is undefined. One important feature of the attribute is, a reference to the flexible array member field uses the latest value assigned to the field that represents the number of the elements before that reference. For example, p->count = val1; p->array[20] = 0; // ref1 to p->array p->count = val2; p->array[30] = 0; // ref2 to p->array in the above, 'ref1' uses 'val1' as the number of the elements in 'p->array', and 'ref2' uses 'val2' as the number of elements in 'p->array'. gcc/c-family/ChangeLog: PR C/108896 * c-attribs.cc (handle_counted_by_attribute): New function. (attribute_takes_identifier_p): Add counted_by attribute to the list. * 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_counted_by_attribute): New function. (finish_struct): Use renamed function and verify counted_by attribute. * c-tree.h (lookup_field): New prototype. * c-typeck.cc (lookup_field): Expose as extern function. (tagged_types_tu_compatible_p): Check counted_by attribute for structure type. gcc/ChangeLog: PR C/108896 * doc/extend.texi: Document attribute counted_by. gcc/testsuite/ChangeLog: PR C/108896 * gcc.dg/flex-array-counted-by.c: New test. * gcc.dg/flex-array-counted-by-7.c: New test. * gcc.dg/flex-array-counted-by-8.c: New test. --- gcc/c-family/c-attribs.cc | 68 +++++++++- gcc/c-family/c-common.cc | 13 ++ gcc/c-family/c-common.h | 1 + gcc/c/c-decl.cc | 80 ++++++++--- gcc/c/c-tree.h | 1 + gcc/c/c-typeck.cc | 37 ++++- gcc/doc/extend.texi | 68 ++++++++++ .../gcc.dg/flex-array-counted-by-7.c | 8 ++ .../gcc.dg/flex-array-counted-by-8.c | 127 ++++++++++++++++++ gcc/testsuite/gcc.dg/flex-array-counted-by.c | 62 +++++++++ 10 files changed, 444 insertions(+), 21 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/flex-array-counted-by-7.c create mode 100644 gcc/testsuite/gcc.dg/flex-array-counted-by-8.c create mode 100644 gcc/testsuite/gcc.dg/flex-array-counted-by.c diff --git a/gcc/c-family/c-attribs.cc b/gcc/c-family/c-attribs.cc index 04e39b41bdf3..9d562ea8548e 100644 --- a/gcc/c-family/c-attribs.cc +++ b/gcc/c-family/c-attribs.cc @@ -105,6 +105,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_counted_by_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 *); @@ -412,6 +414,8 @@ const struct attribute_spec c_common_gnu_attributes[] = handle_warn_if_not_aligned_attribute, NULL }, { "strict_flex_array", 1, 1, true, false, false, false, handle_strict_flex_array_attribute, NULL }, + { "counted_by", 1, 1, true, false, false, false, + handle_counted_by_attribute, NULL }, { "weak", 0, 0, true, false, false, false, handle_weak_attribute, NULL }, { "noplt", 0, 0, true, false, false, false, @@ -659,7 +663,8 @@ attribute_takes_identifier_p (const_tree attr_id) else if (!strcmp ("mode", spec->name) || !strcmp ("format", spec->name) || !strcmp ("cleanup", spec->name) - || !strcmp ("access", spec->name)) + || !strcmp ("access", spec->name) + || !strcmp ("counted_by", spec->name)) return true; else return targetm.attribute_takes_identifier_p (attr_id); @@ -2806,6 +2811,67 @@ handle_strict_flex_array_attribute (tree *node, tree name, return NULL_TREE; } +/* Handle a "counted_by" attribute; arguments as in + struct attribute_spec.handler. */ + +static tree +handle_counted_by_attribute (tree *node, tree name, + tree args, int ARG_UNUSED (flags), + bool *no_add_attrs) +{ + tree decl = *node; + tree argval = TREE_VALUE (args); + tree old_counted_by = lookup_attribute ("counted_by", DECL_ATTRIBUTES (decl)); + + /* This attribute only applies to field decls of a structure. */ + if (TREE_CODE (decl) != FIELD_DECL) + { + error_at (DECL_SOURCE_LOCATION (decl), + "%qE attribute is not allowed for a 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 is not allowed 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 is not allowed for a non-flexible" + " array member field", name); + *no_add_attrs = true; + } + /* The argument should be an identifier. */ + else if (TREE_CODE (argval) != IDENTIFIER_NODE) + { + error_at (DECL_SOURCE_LOCATION (decl), + "% argument is not an identifier"); + *no_add_attrs = true; + } + /* Issue error when there is a counted_by attribute with a different + field as the argument for the same flexible array member field. */ + else if (old_counted_by != NULL_TREE) + { + tree old_fieldname = TREE_VALUE (TREE_VALUE (old_counted_by)); + if (strcmp (IDENTIFIER_POINTER (old_fieldname), + IDENTIFIER_POINTER (argval)) != 0) + { + error_at (DECL_SOURCE_LOCATION (decl), + "% argument %qE conflicts with" + " previous declaration %qE", argval, old_fieldname); + *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 aae998d0f738..0341c44a2cd9 100644 --- a/gcc/c-family/c-common.cc +++ b/gcc/c-family/c-common.cc @@ -9942,6 +9942,19 @@ c_common_finalize_early_debug (void) (*debug_hooks->early_global_decl) (cnode->decl); } +/* Determine whether TYPE is an ISO C99 flexible array member 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 2d5f53998855..3e0eed0548b0 100644 --- a/gcc/c-family/c-common.h +++ b/gcc/c-family/c-common.h @@ -904,6 +904,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); extern tree c_hardbool_type_attr_1 (tree, tree *, tree *); diff --git a/gcc/c/c-decl.cc b/gcc/c/c-decl.cc index b691b91b3db4..9c2213dadeaa 100644 --- a/gcc/c/c-decl.cc +++ b/gcc/c/c-decl.cc @@ -5303,19 +5303,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) @@ -5353,7 +5340,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); /* For a structure, add the size of the initializer to the DECL's @@ -9340,7 +9327,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); @@ -9410,6 +9397,55 @@ c_update_type_canonical (tree t) } } +/* Verify the argument of the counted_by attribute of the flexible array + member FIELD_DECL is a valid field of the containing structure, + STRUCT_TYPE, Report error and remove this attribute when it's not. */ + +static void +verify_counted_by_attribute (tree struct_type, tree field_decl) +{ + tree attr_counted_by = lookup_attribute ("counted_by", + DECL_ATTRIBUTES (field_decl)); + + if (!attr_counted_by) + return; + + /* If there is an counted_by attribute attached to the field, + verify it. */ + + tree fieldname = TREE_VALUE (TREE_VALUE (attr_counted_by)); + + /* Verify the argument of the attrbute is a valid field of the + containing structure. */ + + tree counted_by_field = lookup_field (struct_type, fieldname); + + /* Error when the field is not found in the containing structure. */ + if (!counted_by_field) + error_at (DECL_SOURCE_LOCATION (field_decl), + "argument %qE to the %qE attribute is not a field declaration" + " in the same structure as %qD", fieldname, + (get_attribute_name (attr_counted_by)), + field_decl); + + else + /* Error when the field is not with an integer type. */ + { + while (TREE_CHAIN (counted_by_field)) + counted_by_field = TREE_CHAIN (counted_by_field); + tree real_field = TREE_VALUE (counted_by_field); + + if (!INTEGRAL_TYPE_P (TREE_TYPE (real_field))) + error_at (DECL_SOURCE_LOCATION (field_decl), + "argument %qE to the %qE attribute is not a field declaration" + " with an integer type", fieldname, + (get_attribute_name (attr_counted_by))); + + } + + 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. FIELDLIST is a chain of FIELD_DECL nodes for the fields. @@ -9470,6 +9506,7 @@ finish_struct (location_t loc, tree t, tree fieldlist, tree attributes, until now.) */ bool saw_named_field = false; + tree counted_by_fam_field = NULL_TREE; for (x = fieldlist; x; x = DECL_CHAIN (x)) { /* Whether this field is the last field of the structure or union. @@ -9530,7 +9567,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) pedwarn (DECL_SOURCE_LOCATION (x), OPT_Wpedantic, @@ -9545,6 +9582,12 @@ finish_struct (location_t loc, tree t, tree fieldlist, tree attributes, pedwarn (DECL_SOURCE_LOCATION (x), OPT_Wpedantic, "flexible array member in a struct with no named " "members is a GCC extension"); + + /* If there is a counted_by attribute attached to this field, + record it here and do more verification later after the + whole structure is complete. */ + if (lookup_attribute ("counted_by", DECL_ATTRIBUTES (x))) + counted_by_fam_field = x; } if (pedantic && TREE_CODE (t) == RECORD_TYPE @@ -9559,7 +9602,7 @@ finish_struct (location_t loc, tree t, tree fieldlist, tree attributes, when x is an array and is the last field. */ if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE) TYPE_INCLUDES_FLEXARRAY (t) - = is_last_field && flexible_array_member_type_p (TREE_TYPE (x)); + = is_last_field && c_flexible_array_member_type_p (TREE_TYPE (x)); /* Recursively set TYPE_INCLUDES_FLEXARRAY for the context of x, t when x is an union or record and is the last field. */ else if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (x))) @@ -9816,6 +9859,9 @@ finish_struct (location_t loc, tree t, tree fieldlist, tree attributes, struct_parse_info->struct_types.safe_push (t); } + if (counted_by_fam_field) + verify_counted_by_attribute (t, counted_by_fam_field); + return t; } diff --git a/gcc/c/c-tree.h b/gcc/c/c-tree.h index 22b0009874b5..531a7e8742e3 100644 --- a/gcc/c/c-tree.h +++ b/gcc/c/c-tree.h @@ -777,6 +777,7 @@ extern struct c_expr convert_lvalue_to_rvalue (location_t, struct c_expr, extern tree decl_constant_value_1 (tree, bool); extern void mark_exp_read (tree); extern tree composite_type (tree, tree); +extern tree lookup_field (const_tree, tree); extern tree build_component_ref (location_t, tree, tree, location_t, location_t); extern tree build_array_ref (location_t, tree, tree); diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc index 2d092357e0f9..2375953fdb62 100644 --- a/gcc/c/c-typeck.cc +++ b/gcc/c/c-typeck.cc @@ -101,7 +101,6 @@ static bool function_types_compatible_p (const_tree, const_tree, struct comptypes_data *); static bool type_lists_compatible_p (const_tree, const_tree, struct comptypes_data *); -static tree lookup_field (tree, tree); static int convert_arguments (location_t, vec, tree, vec *, vec *, tree, tree); @@ -1623,6 +1622,38 @@ tagged_types_tu_compatible_p (const_tree t1, const_tree t2, && st2 && TREE_CODE (st2) == INTEGER_CST && !tree_int_cst_equal (st1, st2)) return false; + + tree counted_by1 = lookup_attribute ("counted_by", + DECL_ATTRIBUTES (s1)); + tree counted_by2 = lookup_attribute ("counted_by", + DECL_ATTRIBUTES (s2)); + /* If there is no counted_by attribute for both fields. */ + if (!counted_by1 && !counted_by2) + continue; + + /* If only one field has counted_by attribute. */ + if ((counted_by1 && !counted_by2) + || (!counted_by1 && counted_by2)) + return false; + + /* Now both s1 and s2 have counted_by attributes, check + whether they are the same. */ + + tree counted_by_field1 + = lookup_field (t1, TREE_VALUE (TREE_VALUE (counted_by1))); + tree counted_by_field2 + = lookup_field (t2, TREE_VALUE (TREE_VALUE (counted_by2))); + + gcc_assert (counted_by_field1 && counted_by_field2); + + while (TREE_CHAIN (counted_by_field1)) + counted_by_field1 = TREE_CHAIN (counted_by_field1); + while (TREE_CHAIN (counted_by_field2)) + counted_by_field2 = TREE_CHAIN (counted_by_field2); + + if (DECL_NAME (TREE_VALUE (counted_by_field1)) + != DECL_NAME (TREE_VALUE (counted_by_field2))) + return false; } return true; @@ -2381,8 +2412,8 @@ default_conversion (tree exp) the component is embedded within (nested) anonymous structures or unions, the list steps down the chain to the component. */ -static tree -lookup_field (tree type, tree component) +tree +lookup_field (const_tree type, tree component) { tree field; diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi index 00449bd96304..799a36586dc9 100644 --- a/gcc/doc/extend.texi +++ b/gcc/doc/extend.texi @@ -7788,6 +7788,74 @@ align them on any target. The @code{aligned} attribute can also be used for functions (@pxref{Common Function Attributes}.) +@cindex @code{counted_by} variable attribute +@item counted_by (@var{count}) +The @code{counted_by} attribute may be attached to the C99 flexible array +member of a structure. It indicates that the number of the elements of the +array is given by the field "@var{count}" in the same structure as the +flexible array member. +GCC may use this information to improve detection of object size information +for such structures and provide better results in compile-time diagnostics +and runtime features like the array bound sanitizer and +the @code{__builtin_dynamic_object_size}. + +For instance, the following code: + +@smallexample +struct P @{ + size_t count; + char other; + char array[] __attribute__ ((counted_by (count))); +@} *p; +@end smallexample + +@noindent +specifies that the @code{array} is a flexible array member whose number of +elements is given by the field @code{count} in the same structure. + +The field that represents the number of the elements should have an +integer type. Otherwise, the compiler reports an error and ignores +the attribute. + +When the field that represents the number of the elements is assigned a +negative integer value, the compiler treats the value as zero. + +An explicit @code{counted_by} annotation defines a relationship between +two objects, @code{p->array} and @code{p->count}, and there are the +following requirementthat on the relationship between this pair: + +@itemize @bullet +@item +@code{p->count} must be initialized before the first reference to +@code{p->array}; + +@item +@code{p->array} has @emph{at least} @code{p->count} number of elements +available all the time. This relationship must hold even after any of +these related objects are updated during the program. +@end itemize + +It's the user's responsibility to make sure the above requirements to +be kept all the time. Otherwise the compiler reports warnings, +at the same time, the results of the array bound sanitizer and the +@code{__builtin_dynamic_object_size} is undefined. + +One important feature of the attribute is, a reference to the flexible +array member field uses the latest value assigned to the field that +represents the number of the elements before that reference. For example, + +@smallexample + p->count = val1; + p->array[20] = 0; // ref1 to p->array + p->count = val2; + p->array[30] = 0; // ref2 to p->array +@end smallexample + +@noindent +in the above, @code{ref1} uses @code{val1} as the number of the elements in +@code{p->array}, and @code{ref2} uses @code{val2} as the number of elements +in @code{p->array}. + @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-counted-by-7.c b/gcc/testsuite/gcc.dg/flex-array-counted-by-7.c new file mode 100644 index 000000000000..fcb6f1b79690 --- /dev/null +++ b/gcc/testsuite/gcc.dg/flex-array-counted-by-7.c @@ -0,0 +1,8 @@ +/* Testing the correct usage of attribute counted_by: _BitInt */ +/* { dg-do compile { target bitint } } */ +/* { dg-options "-O2 -std=c23" } */ + +struct trailing_array { + _BitInt(24) count; + int array[] __attribute ((counted_by (count))); +}; diff --git a/gcc/testsuite/gcc.dg/flex-array-counted-by-8.c b/gcc/testsuite/gcc.dg/flex-array-counted-by-8.c new file mode 100644 index 000000000000..058d58fb2931 --- /dev/null +++ b/gcc/testsuite/gcc.dg/flex-array-counted-by-8.c @@ -0,0 +1,127 @@ + /* Testing the correct usage of attribute counted_by in c23, multiple + * definitions of the same tag in same or different scopes. + * { dg-do compile } + * { dg-options "-std=c23" } + */ + +/* Allowed redefinitions of the same struct in the same scope, with the + same counted_by attribute. */ +struct f { + int b; + int c; + int a[] __attribute__ ((counted_by (b))); }; +struct f { + int b; + int c; + int a[] __attribute__ ((counted_by (b))); }; +struct f { + int b; + int c; + int a[]; }; /* { dg-error "redefinition of struct or union" } */ + +/* Error when the counted_by attribute is defined differently. */ +struct f { + int b; + int c; + int a[] __attribute__ ((counted_by (c))); }; /* { dg-error "redefinition of struct or union" } */ + +struct h { + int b; + int c; + int a[] __attribute__ ((counted_by (b))); } p; + +void test (void) +{ + struct h { + int b; + int c; + int a[] __attribute__ ((counted_by (b))); } x; + + p = x; +} + +void test1 (void) +{ + struct h { + int b; + int c; + int a[] __attribute__ ((counted_by (c))); } y; + + p = y; /* { dg-error "incompatible types when assigning to type" } */ +} + +struct nested_f { + struct { + union { + int b; + float f; + }; + int n; + }; + char c[] __attribute__ ((counted_by (b))); +}; + +struct nested_f { + struct { + union { + int b; + float f; + }; + int n; + }; + char c[] __attribute__ ((counted_by (b))); +}; + +struct nested_f { + struct { + union { + int b; + float f; + }; + int n; + }; + char c[] __attribute__ ((counted_by (n))); +}; /* { dg-error "redefinition of struct or union" } */ + +struct nested_h { + struct { + union { + int b; + float f; + }; + int n; + }; + char c[] __attribute__ ((counted_by (b))); +} nested_p; + +void test_2 (void) +{ +struct nested_h { + struct { + union { + int b; + float f; + }; + int n; + }; + char c[] __attribute__ ((counted_by (b))); +} nested_x; + + nested_p = nested_x; +} + +void test_3 (void) +{ +struct nested_h { + struct { + union { + int b; + float f; + }; + int n; + }; + char c[] __attribute__ ((counted_by (n))); +} nested_y; + + nested_p = nested_y; /* { dg-error "incompatible types when assigning to type" } */ +} diff --git a/gcc/testsuite/gcc.dg/flex-array-counted-by.c b/gcc/testsuite/gcc.dg/flex-array-counted-by.c new file mode 100644 index 000000000000..e8b54c2de1c0 --- /dev/null +++ b/gcc/testsuite/gcc.dg/flex-array-counted-by.c @@ -0,0 +1,62 @@ +/* Testing the correct usage of attribute counted_by. */ +/* { dg-do compile } */ +/* { dg-options "-O2" } */ + +#include + +int size; +int x __attribute ((counted_by (size))); /* { dg-error "attribute is not allowed for a non-field declaration" } */ + +struct trailing { + int count; + int field __attribute ((counted_by (count))); /* { dg-error "attribute is not allowed for a non-array field" } */ +}; + +struct trailing_1 { + int count; + int array_1[0] __attribute ((counted_by (count))); /* { dg-error "attribute is not allowed for a non-flexible array member field" } */ +}; + +int count; +struct trailing_array_2 { + int count; + int array_2[] __attribute ((counted_by ("count"))); /* { dg-error "argument is not an identifier" } */ +}; + +struct trailing_array_3 { + int other; + int array_3[] __attribute ((counted_by (L"count"))); /* { dg-error "argument is not an identifier" } */ +}; + +struct trailing_array_4 { + int other; + int array_4[] __attribute ((counted_by (count))); /* { dg-error "attribute is not a field declaration in the same structure as" } */ +}; + +int count; +struct trailing_array_5 { + float count; + int array_5[] __attribute ((counted_by (count))); /* { dg-error "attribute is not a field declaration with an integer type" } */ +}; + +struct trailing_array_6 { + int count; + int array_6[] __attribute ((counted_by (count))) __attribute ((counted_by (count))); +}; + +struct trailing_array_7 { + int count1; + int count2; + int array_7[] __attribute ((counted_by (count1))) __attribute ((counted_by (count2))); /* { dg-error "conflicts with previous declaration" } */ +}; + +struct trailing_array_8 { + _Bool count; + int array_8[] __attribute ((counted_by (count))); +}; + +enum week {Mon, Tue, Wed}; +struct trailing_array_9 { + enum week days; + int array_9[] __attribute ((counted_by (days))); +}; -- 2.31.1