From patchwork Thu May 30 12:26:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qing Zhao X-Patchwork-Id: 1941697 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Vqlrb1ND3z20Pc for ; Thu, 30 May 2024 22:27:43 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6B0AD384F4BD for ; Thu, 30 May 2024 12:27:41 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org 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=3Acontent-transfer-encoding=3Acontent?= =?utf-8?q?-type=3Adate=3Afrom=3Ain-reply-to=3Amessage-id=3Amime-version=3Ar?= =?utf-8?q?eferences=3Asubject=3Ato=3B_s=3Dcorp-2023-11-20=3B_bh=3Dg+phD/5rk?= =?utf-8?q?EFFRDcmRJwxbNPg1M/tlX5cTORTuKJqeeM=3D=3B_b=3DdRLbwftDWKmRYfxT8cxD?= =?utf-8?q?LfBEehvR+rFA9/lHl0SEQNbe+O0tEOwKvGgG9arxeetfQNYD_iMZIUnizFBsVSnjx?= =?utf-8?q?58ZvipM5m7UAUhkIeCwrK5QGZ6iSJhA//O4KMr8meC6HEuPbjRM8_ZTNeweQip6P/?= =?utf-8?q?Ih+YZSwWUtuzHIPxxLXUvreVRndPKd20YfeXfTiJ5wjxjn0bH6N9uFXS_7arhCGL/?= =?utf-8?q?SpBFHioRI/REKN9vw5whssgmic+TBqqyuxnjkQ+qlId1a8sBiut9m6OSpMb/_Zx9i?= =?utf-8?q?BDDdC91RyFXL/kNU3Q429PevU2KxKVHHwL0BAsTKcjy9EXCOZ6qhrmLzt3E27TBX_?= =?utf-8?q?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> 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: rR06hmOScrMNScelc2Eumx8g2Ni0p4bS3MPxxP8Vk7TwbXB2rEJuQgMcT5uMdcm5juhVFyqlNgmpfhB3SHoHlg/jonDSMmWnxMTwr5WjiSHbGR42S9WDbDPKWSd3SCk1mjQ6g6sc3AbTGvsmywgLpWxJR2u1YyEAklsaBTPydkulfG2B8WXZm/HvgaA7IoV+RRaHkEjC/z/WU2Kn1NxhSbYYcRwupPIRlBgxzG3DU1xNSxnNisCfyaXng/J1tIy8A6Ldo6cw/TclxfjGHyzEbxxC3UgJU6s7GgvL3SDVwhk2XAcnS/GbdQA52pqMT8txJKd4nYFmzPI9PmTyPI5XhwFsnFHU2MeFWd9XsmWG0G+Z7O4Rk9RWFnm+idPe6n6j/nVmS+yda0HuvXa4v6NzkA7JmO4osX6K1mvS0FsDN/srlxNDBv77JUWgwffjuI/yV5Xe4DafGcwQrtMGQpM1wrWcQEixl2RYLYRhUHHve0lfB7NYtJ+zUf5AZIzaO7aSzhwmecmxHnRu6shG0EcfsM0Ti0cLx8ntxnHPaJ3jcy4vEliWOYgjSutFV2OGg+CJsF6gRb6UgE8RKFA6pK8wMMQ1Yjaw5EkIq2LBXzRmWsuL4oJUlO/+PgK1KLaK/5rDYFDtkfbMnG9AtC/ZZgam4HWucvZ7ocLSiP6wh/wGCY5hTpRCTqU49mH6YgZe2Lehv+gl2jJ+2dBegItJ2ZukENFKK2ESq8B1RQi0g6YYVwvmUY/+j0isQyAQAu10bU3bOnyRBjHJLo7shUSMn4pqXI3NyxmvSinKzpZ0Quz0Tt1Tf5maDKq4vE/eAVwLletchaNk9Q0MI7xTfWBo4NPkeuLC+pOxEESimzpR4RM8EIPYmXooMylBz49WOFPMFpJLzcQaL4qfowAjqQ3nkfY28o5OyGslOMrAvXAqbKf4Lwud322Yj3gbn915gMzIZ94GWI+aiYc93vOuT2c+uLainAVplW5/XASpV6aqmW4HZiFcdwawxGVzcoXiYMWUHyvhQPMVSd+wB5k4RlFus/BfH/NwWkX0EHxvaqd6WiGov9LhNZQvxkKVlEgcqLizkIM8nfVmsJWnQH4OuyDkK86kFtA6qlu3zunI1QcNZr3ZGVD3P2jhhPBGl8cARLjTR5HZLB/nrRZ91LU4ge3gNinKmyP170In43toU9Xn35vpJwEZrvxTigV57WT5gcqk2kWF3ol2ler5p05yRJzXPS943LCugkLOMMe1EqvF4KSMekY= 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: 4pM4ZUSrDg33xSs8z9AP7hpO2gesQKmBi9AhfTNPyRjUR2wNEVZFWX0NcW4PIFQJpK3Fmtp62rWxjCBpusIX3J605jciVu58uLhUVICoag7911upbil52KNm3dDSaUBUgYswyOVmFdIHb1uQEFFoRoApoTBEurgVmK4LgE9bDT8/9E9876VqbAHY6rl74LJqlNqGlcOz9dMNPY77bEAzxGOkF9526EMQUGp5UaLVw9tZ4ZJu9MNFO3T51BuvJ7iwAfFirK52CCCuxEku0Omh0xDQ9V9xWgRpQue6KvPHqTZ63uTV1oqaIx4LZjOsJ1jKARxeKDU/OArlYdkx+ZcKKDzPiWDytCQoOWFZrmDtdBd7XB5tgezouCUE0WIAnWI3Ex+OXg8fiuQ08irGdYGiHZAulzLA1/48VXt9SvGg7NObwSWIliknVsOm3lQe8KsRuKGGcxHAfeC29K/Gc9gtwscz9NihqSAGa50LU1HIYz3kGwGqVjKNIOVEtHFxfKkHr28360Evpf/yZQLPWNaAzqIvL3IvIBDDAyZ8i94ZGhilto+RvgEHa+PtaOyXziDlwX4EDTvZC1oYGpvIiik9m4j5cWfsybhH78qpH2ExUmUtv9e3RMi/R5h1emTXS1ZfhdozVvKx9JPQbWOXQyyCqsGsVxt2sre6JSMnQmniia4ljUVZfi6jJA3kParCvOCHzFRkkXQeIQlpDU78bqQlF8mXXByxrFONGh/+GVSz0U+JqqXPPde08UDHMaZoL7iPMRLBkNOqu7inMx+eicoJcO12dPzd1wwapaXqlOe51ISwqeSlI4V0/Z1vXDRLhKNO83ZtglbjMK+ALAfkENi8B2Sg8o2tLxIRp3XjsLRTYDxRWRIkItk3OvOlS3JODyZgg1g8WV365CZGR3hySk8KFhy1Jw0UpKbqOGLEqv2zEWMLS7DXm9+/PgCJdvF7FXiu/x885Ze8GRywgSaUpijLhPsa9oLST6+XrYpk8/lenpLaaAswjaxu13Y5f8CZaGOEIM+hzgj5ynGfXK0RWZbO2rp3kYyH05aieC/ooD4iwrvUrAzNoK4LGJt/Cai+eLmQ5Vtk4AnTX/pcIGZQ5Ka5u0eLtuPUwbe5IDthe5uXB6auC9R0Mc0qOhpEQQI7sm1Sxc9Fx3EsQ+P28E99Y6rUzZr/1qRKRw7o+oDek/apM+DVOjzbpCFOPNu7OnFodvVYVkiNdPZEj2FC/lpotdbteXGfO57kRs5nN0GWfY6UfxO5JHDhxCcGE2jDW28bapR3WjgkWCBEy3nRunAiN4WW2+2r2j5BgIfs1hz4Dxf1rULlJWjo32hCe2VO+M4MBAP31CQapRnIQjRroGmL9/R03E8kI/mBbO5HGdHRxpf45pPXpARvFCPIh/AMmcMfIbIYuuyTLxNSzkget9xyaZAWC6nlkE68WfhnfvEiln5J1VlXhfmig7SQTqCXGfqq7oMUnLJXPzsmKf0Wm9Y6+sIpc2eKrgJ8ivdxe8CbA8iTU2KozQncZzwJFN0WhA5sxh9jQCrcxgZ0WqazKCIvx60UisBvPic1wXwHIlYKnYBj2QgnzbH9KD/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 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org '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))); +}; From patchwork Thu May 30 12:26:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qing Zhao X-Patchwork-Id: 1941698 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Vqlrm4jK6z20Pc for ; Thu, 30 May 2024 22:27:52 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id EC2C9384CB95 for ; Thu, 30 May 2024 12:27:50 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by sourceware.org (Postfix) with ESMTPS id BC2DE385B532 for ; Thu, 30 May 2024 12:27:18 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org BC2DE385B532 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 BC2DE385B532 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=1717072041; cv=pass; b=yE6zbttkJ0aaTKIAVNwiOHmwKydRU25c8/azIO5i2WXKQGy6orY4P+skUEogCg9x2DAM1W5ETXsKz0ZdLVIw5zzA/8D0dR5YvIwKsv3WfD1LNG5M0BdAsJ7EsE4mdrfTm7W4iYZ6VSAHUEC0fsoQX+OspzxZv9gh3eC/uJ3zw58= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1717072041; c=relaxed/simple; bh=QEae4tbGNZnxB/71Ro8+RZa6IJ9p6vN/ggK+lZU4VwE=; h=DKIM-Signature:DKIM-Signature:From:To:Subject:Date:Message-Id: MIME-Version; b=lEzx+HRlUNVIt6ZGXEYU1Eg8dmmSoNJ+Ba46urM6XVZVzAzujwCRHIq1eNkKjrkSGaNL+cqPhh6/PVlf+aP5b3RUmoAMoLDseIYMN5UgObSa4DvhDn3EhZ3RSbGqwRlmn3NfPzBFnTm4qZn6El2s2Dnn9bjwqBs4r4/awqjF/Cc= ARC-Authentication-Results: i=2; server2.sourceware.org Received: from pps.filterd (m0333520.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 44U7mrrb019731; Thu, 30 May 2024 12:27:18 GMT DKIM-Signature: =?UTF-8?Q?v=3D1; _a=3Drsa-sha256; _c=3Drelaxed/relaxed; _d=3Doracle.com; _h?= =?utf-8?q?=3Dcc=3Acontent-transfer-encoding=3Acontent?= =?utf-8?q?-type=3Adate=3Afrom=3Ain-reply-to=3Amessage-id=3Amime-version=3Ar?= =?utf-8?q?eferences=3Asubject=3Ato=3B_s=3Dcorp-2023-11-20=3B_bh=3DN1pesWlgH?= =?utf-8?q?Cd7vBEKJaJtcT0u0YcZLE+AKt4MPRMAupc=3D=3B_b=3DQtstEJ+xH/x8oQr/T1FH?= =?utf-8?q?Wzr0ooregmNDjQVJ/DD6N9pC7qx0SPnAO/RgcqjrPonIlbVp_OB2A90ONiET0TVlv?= =?utf-8?q?iWhWmA9FrM748xzb4TYVD/10HWYM/YwaUif7oSaBKOA4wjsiFeh3_R+HM0Xp5QNOl?= =?utf-8?q?+4BP9o2gY1O91xbQJxUG3yhGe6WmPCDIIAiMPuutv9UjSlcfCW0BxOBv_MVokvYkq?= =?utf-8?q?G60xbv/I1e1RMGwrvGNhhpWBqhnx0oHbn0xPglr4eDudWv37vemBlhYaGS8Z_Rx9A?= =?utf-8?q?8LZlWSpTqnyz1EzM3lduUCqI6Afk+4/X1Ae2vtfD2Fns4o/JlqNh8MDYADkZ0oQH_?= =?utf-8?q?Tg=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 3yb8g9ryqb-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 30 May 2024 12:27:17 +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 44UC6j3x026519; Thu, 30 May 2024 12:27:16 GMT Received: from nam10-dm6-obe.outbound.protection.outlook.com (mail-dm6nam10lp2101.outbound.protection.outlook.com [104.47.58.101]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 3yc508fwwb-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 30 May 2024 12:27:16 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=QAgzha08e6OEAH4hO36O+0QjXselax2hHRdbXkOoaZ4nyu/rUUBqXm9SqdpwTgY7o5seynm0B8OtIRC5t50B4copPQ57He0h9cbpIgzym8nU+rrMQtF+2ja+WGm8baTHuYkU3SZ7TVuZcg0c46A6kvfDqiBlQs7j1WkKvkavzCykDpS1C4ySIFFJAxqCuQ1JcQ6OPQEh4j6LU1Am5xU+WcXhOBEc53pGpMv53ddslHY5vIvnZbjV/1stOMpy8uOgY7OC7ULGmjC5FQwIUu0XUCTdbbvQLMdkV1jlfiDTBzEpeZ7sMiTuwA45c/275gpumrjVN5Rev3aQvwuWN1HqrA== 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=N1pesWlgHCd7vBEKJaJtcT0u0YcZLE+AKt4MPRMAupc=; b=hLv59VgzXvbHn2PYl90mlFoLA/7DeKHcPCWe206TPCwArTCFZkDlChYUitWIFL9niRKo81+C/dRKmOqGs/rXpvuQCxkN6XU8Hr6Uu1Q0haH47atNioSC2Xu9FSFtOICCSqG8jEEU6s1E9j6SmvPHMsfQxCF/NavcAUAwO6S/HaCXe+CmaXVvObTy3NF8D0+Bq5mN+se4HcQdQs6GoxTDRvU798F+ileQt9pc2mj+C18ukSX9mvOWNHibhHAKJxuvJNxTGkCPKBGaO8Jm1T7WLigMnJxwCrybBFr/RJJKgalwZA/Vwmq4Rvb79//063MkFPxnSEOOCUZ+c6fD1sN3lA== 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=N1pesWlgHCd7vBEKJaJtcT0u0YcZLE+AKt4MPRMAupc=; b=g8p/rkq7EbjTqq+Qi2/F8YzTIW94D8YFdpfwuzxEq1HYMJb15cCQYXExZ/p+1ovbRHPfn8e4jQv3AqnuvZVFH/k0VJwAADe8Q8xKFTbzFFx20lL97Lqf95difE+fnGLh3um5cAqYmDjIb5qjLoEYn5c8oY05O4GarNE5ZF8KUxs= 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:09 +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:08 +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 2/5] Convert references with "counted_by" attributes to/from .ACCESS_WITH_SIZE. Date: Thu, 30 May 2024 12:26:57 +0000 Message-Id: <20240530122700.1516243-3-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> X-ClientProxiedBy: BL6PEPF00013E11.NAMP222.PROD.OUTLOOK.COM (2603:10b6:22e:400:0:1001:0:17) 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: 5cdcf993-4117-430f-0325-08dc80a3d314 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: +8tm8TUTzE4YrLO7ETWNtGyiEiS51UzkzLCkXLVUq7Bx0cqMPGyucecbMzsi+KZquNZjH7M3FEIn2UNISJU0XsQrXkF6yCxyjVAZju71ACWHlGR8gNy5ezSxKvnWHBYzFPlmCciTx7YpgDatTs9A3sBLt9D1Due6TZgfq16uNcMpkdFlimBG1btfCCJ3J133P9mslyl+ckD5cGjOAPhtNfmbkv/A2ZdqvVOsav84HttQgwWaN8Wi01tBoEjmfaqjwtTfrhlfmSsvJUlk3xscGMQxMzIKlvIIWadgcxqH3Rx1dA9nIUbz0brFbEbO5CETSF1kaucYZthfOJVLh99A4OtAk+Ot9YXRH1GLGBjxuFgkyshqYs5dKH1wcGpkEWgcO8CgQ7R4D0sKcQ9cA+ZBKOi+Om0r3EXODjhECcYVFFD52QCjQyrSYTYJw/kSxa08MQmErM3D1NiNRwdkjhJ0C0c7NfJyr27UMTtpz5+DvbsVzGsT8eiIRF+HNvOQKO0WheQTTuFL8XQDuN2os6xrGKHYHCxaT/krJAT2EQsucZw7MSCel1r2H7Fc9nKlCbED/J/OaLxHgdp0SkLnTrOZSolrg6BMZcjlENFv/KtQXkY3L2lHWWcufDIxHKX45YQUW3MJKJvxHjZVwOzdfAXlmd7f67lQtAuXgQ6kAD/d90eu3dwl4wHeKAGtzvXpdoeSMZjeeTEW1SePlxd7XIrg6q3bOyMvVxuP0fFyPaV08tzHkoyw4CEvxTVblirzEP/O+ZYAx7e7SZggN68vww/imNDa3xp7iFdy6sVKwe6jq2vkPXPbteTIavGUosm7/aakQdt9Tiy5La4KqSCqVQ3GG/OLoDGRhzywyO8GwVGnqaqj1/6q3zgzdRoGF731WflRS+CM/OR1ECvnMQbwyFYM/IxDeMrQLEBt4KhbQb3sjS8nbkS1QgFMmPBq9xMZUWp63EeBnVOPjvvjJKgszt0wHdDpIVNr/iX2oXrPtDj67F9SS43waj8KoTIbL4uuBPyORcueYvB970P8AG1eUzY6b3f5Dy0FczO6rdWbXLMapkhyerYUKrNCmRkzUdOqA6fk70+y5moZycIiw9RPa+lyuGdB5NV8F3R8kQjvArMFrR2k+S3UMYOyflMN/oYx+KaQROgqx+wWJLzaEEImviUe3w6U3QjBfsU/kxhw3/tR3WzoAz3jiukVeHxssBk4YZ1l2PNUhjs7LCnGY/6RsrffYg== 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: mNbH7OgnswSVL3Rfp7Cl+2E80sKXh6dkdtTDjf421iDN3K8njDuUPDRLUaF8qZdImQMiF82l8Fh06OW4/Kb4yUg/99JUqJhWiLPmPqJj96FGJMdjMtyilCl7ujJ3CwgraxCduO1J4tQ2xjaO4oSl+jHhe/FaJgWHNQ2H36MejaMu7kTy2I5uboDBeeIx0YymEqpa/jMbzR8kCtvN/xVLPWKaiYo9Kh3WEcwmN0WTIOrW+sOaM0z7wcyt0iN+RJQQk0xqFFTljgcsXWAP/6n0K8XZ7R15CkKWHoT9gGm9DcZlvada3HDNyzBEZh3JK0tKmsKQJHSbh19tXjr8dlXFcyFoVvudAqleWvgJP5I/j/URFXb6bkqNNu2JKXNQPzxhcqzi67AIiXZx+sQJ8YO6KiGQ2A578gqknohzJlVvr9YFzYGvj+pEWOuRsAbIAvsaKurB5n75i3OJ0PhM6VPLtgdb4S0lfYPD5OEOXPLO/aJR5DTc+K4SYojal/M53xTqPQyTOFsJXjva5HbE4pyOols5+bUj1ozKGkdaIW2C/JbknyLVeW0/Tp3M7mmPKiwSkticAmKkcAFHNjk10rQZXlrBMNoS0JDa+tDjy3icRbCBRicsXcjXLhI8oHKi8Y2bxDKnYGgqxahk+3zjEg1QQbwPBQj6DWzTYMe8O2SgOV+QSClSnJbMqOHu0KRaGtSrOl7sUiDmP2mcU0q5uBdqu3YyPO3Gobrzqbu7pHopatWMTlZX9CMFSC2+ZLoo75DKHkj0MTMtIXIrE5MddugyyXzAR0xYTehEmB4x6wIfKJnviKambPT9dlSWlVlgqajgQZENdDnSSTeflpPyTW2WkTNy1pJflU3w7j1XaGqy0zX7xewZnK7M9K3Z/9EvH9aQbPkX3vlHKL9+V8HJvXkJeu8KKHQ+hl4pjxUNIunse/ukpmJAU7/r/2cye87gxGJvXsU7Zhny6TK/cZQfJBvVaXVrp5qookrxCWmNPZZGmKRaauA2Smp2m5BG4g8SJ1myQfDtP1xQeKLnWFqgAqVys9VS9BMBYJJxzBynwo7SxMDyFsaKOfxWHyrcmeMS2TbnP3O5CF8SVkL2320rbTdQepkeEfS3ZftiDin5oPnkSECR35120JyRqrb5kI36BnCMSDnWXrh2C/xyG9VuIfEzPe97IKvlxCG41MwfDniYGCrx6/aUFkMZshXRF/GG56dd2wRuW7SJXAZMqpXmTxNhAkG9kOMXQnLS3ya7FG/kSjae4plFEc+M54NZugb4lKcIliudpAN3pyzGv22nMjBKbzgTEWjHSij6T7QHb0FV9+i1dwwvHSr6dy/V6obAiMY8UdjIJib/I2ogYIE5ifixuiu975mz2z4d/d4KfpB9IImTi98psWPixyfX+M/dXcKBgHxs+KK/YQdFw+b+Qnu7TMHFyxDMtI/s4fUznYdUk2cpPKzs56mtXUDQ4WLh1zB99LQJqghtANp/nSS5W43zwtvFc+nfhsYYrMqUKU8ToBRgAYkWxiIuUd//wQBaGhJ3Fe3T1N/FTzv6/zJUb0ZxW0KJtbFCUjZgUV0kkWmbJ9qqmFA2qMQ+7NyN7N4yKowt X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: i+mquzhclspN4ejzIqMFUtIEdaCtTphbWDbkaBDX8P4hOxiCqj4kVeiv8BRR5kZgCskoO2e6eiJHc9LE2mT/8E7GymFzi/kT/wgifEj0DROSaniwA9Lltz90K1yPQcGIy26BUMV3iLRTInDNeFJhJIoYYqlJu60WwrAR6+IcgVLPad2XJJ60bnv5T+u2XqWYPKYGfxG6vEcy79qz2WmAZyCJMi9YnCN7eRLUNxXmLgoHx9Jmvv5+mJWaxf3NxvUSYo8ria8QVjaAa+wJRwb++KRslT/zd1TlLGynTWuZu4EeTqri4lRY4wIpjgrnH4rYV7W994lTzWWmwQBFJcuQduD9Jy84p2O3URVc/IggROIR3T8/oMHbY5fbJyWjYBnD9KMWCulGwMoOfrmpGYy4IG9EEy8aVm18bC/E3yttlcdzzqq/NN/kY5tn2t/3i0d3ZXuqKDHM47Xvl0bHF/ej8wOyFFfpjwXYZ4zxu8yZdKfKpwyqdrFhKLDhS+iHPEqxNysTO0UrZu6UO19vD2hifcpWzU98PxIShFYcAl96Ts9pBIUm3moIErZP4tOPgsUt2UWARSdO3HGEkCtWEkLm66ghI6C2QDwp5jwftXWJR1U= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 5cdcf993-4117-430f-0325-08dc80a3d314 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:08.9743 (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: PMGpOB2UMfqE7Ht60KbymmPxFa3S2jXJfpcGoQtLh6DEruXDxFoeet/qu5K5eC7SKPXP3MxD53N6VqekqKaktw== 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=999 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-GUID: dyPt6p5QY00Ifi_Xcqc5Z50XyU2l4Ndw X-Proofpoint-ORIG-GUID: dyPt6p5QY00Ifi_Xcqc5Z50XyU2l4Ndw X-Spam-Status: No, score=-10.9 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 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Including the following changes: * The definition of the new internal function .ACCESS_WITH_SIZE in internal-fn.def. * C FE converts every reference to a FAM with a "counted_by" attribute to a call to the internal function .ACCESS_WITH_SIZE. (build_component_ref in c_typeck.cc) This includes the case when the object is statically allocated and initialized. In order to make this working, the routine digest_init in c-typeck.cc is updated to fold calls to .ACCESS_WITH_SIZE to its first argument when require_constant is TRUE. However, for the reference inside "offsetof", the "counted_by" attribute is ignored since it's not useful at all. (c_parser_postfix_expression in c/c-parser.cc) In addtion to "offsetof", for the reference inside operator "typeof" and "alignof", we ignore counted_by attribute too. When building ADDR_EXPR for the .ACCESS_WITH_SIZE in C FE, replace the call with its first argument. * Convert every call to .ACCESS_WITH_SIZE to its first argument. (expand_ACCESS_WITH_SIZE in internal-fn.cc) * Provide the utility routines to check the call is .ACCESS_WITH_SIZE and get the reference from the call to .ACCESS_WITH_SIZE. (is_access_with_size_p and get_ref_from_access_with_size in tree.cc) gcc/c/ChangeLog: * c-parser.cc (c_parser_postfix_expression): Ignore the counted-by attribute when build_component_ref inside offsetof operator. * c-tree.h (build_component_ref): Add one more parameter. * c-typeck.cc (build_counted_by_ref): New function. (build_access_with_size_for_counted_by): New function. (build_component_ref): Check the counted-by attribute and build call to .ACCESS_WITH_SIZE. (build_unary_op): When building ADDR_EXPR for .ACCESS_WITH_SIZE, use its first argument. (lvalue_p): Accept call to .ACCESS_WITH_SIZE. (digest_init): Fold call to .ACCESS_WITH_SIZE to its first argument when require_constant is TRUE. gcc/ChangeLog: * internal-fn.cc (expand_ACCESS_WITH_SIZE): New function. * internal-fn.def (ACCESS_WITH_SIZE): New internal function. * tree.cc (is_access_with_size_p): New function. (get_ref_from_access_with_size): New function. * tree.h (is_access_with_size_p): New prototype. (get_ref_from_access_with_size): New prototype. gcc/testsuite/ChangeLog: * gcc.dg/flex-array-counted-by-2.c: New test. --- gcc/c/c-parser.cc | 10 +- gcc/c/c-tree.h | 2 +- gcc/c/c-typeck.cc | 142 +++++++++++++++++- gcc/internal-fn.cc | 34 +++++ gcc/internal-fn.def | 5 + .../gcc.dg/flex-array-counted-by-2.c | 112 ++++++++++++++ gcc/tree.cc | 22 +++ gcc/tree.h | 8 + 8 files changed, 328 insertions(+), 7 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/flex-array-counted-by-2.c diff --git a/gcc/c/c-parser.cc b/gcc/c/c-parser.cc index 00f8bf4376e5..2d9e9c0969f0 100644 --- a/gcc/c/c-parser.cc +++ b/gcc/c/c-parser.cc @@ -10848,9 +10848,12 @@ c_parser_postfix_expression (c_parser *parser) if (c_parser_next_token_is (parser, CPP_NAME)) { c_token *comp_tok = c_parser_peek_token (parser); + /* Ignore the counted_by attribute for reference inside + offsetof since the information is not useful at all. */ offsetof_ref = build_component_ref (loc, offsetof_ref, comp_tok->value, - comp_tok->location, UNKNOWN_LOCATION); + comp_tok->location, UNKNOWN_LOCATION, + false); c_parser_consume_token (parser); while (c_parser_next_token_is (parser, CPP_DOT) || c_parser_next_token_is (parser, @@ -10877,11 +10880,14 @@ c_parser_postfix_expression (c_parser *parser) break; } c_token *comp_tok = c_parser_peek_token (parser); + /* Ignore the counted_by attribute for reference inside + offsetof since the information is not useful. */ offsetof_ref = build_component_ref (loc, offsetof_ref, comp_tok->value, comp_tok->location, - UNKNOWN_LOCATION); + UNKNOWN_LOCATION, + false); c_parser_consume_token (parser); } else diff --git a/gcc/c/c-tree.h b/gcc/c/c-tree.h index 531a7e8742e3..56a33b8156c6 100644 --- a/gcc/c/c-tree.h +++ b/gcc/c/c-tree.h @@ -779,7 +779,7 @@ 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); + location_t, bool = true); extern tree build_array_ref (location_t, tree, tree); extern tree build_omp_array_section (location_t, tree, tree, tree); extern tree build_external_ref (location_t, tree, bool, tree *); diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc index 2375953fdb62..0d9c7a34a0df 100644 --- a/gcc/c/c-typeck.cc +++ b/gcc/c/c-typeck.cc @@ -2584,15 +2584,116 @@ should_suggest_deref_p (tree datum_type) return false; } +/* For a SUBDATUM field of a structure or union DATUM, generate a REF to + the object that represents its counted_by per the attribute counted_by + attached to this field if it's a flexible array member field, otherwise + return NULL_TREE. + Set COUNTED_BY_TYPE to the TYPE of the counted_by field. + For example, if: + + struct P { + int k; + int x[] __attribute__ ((counted_by (k))); + } *p; + + for: + p->x + + the ref to the object that represents its element count will be: + + &(p->k) + +*/ +static tree +build_counted_by_ref (tree datum, tree subdatum, tree *counted_by_type) +{ + tree type = TREE_TYPE (datum); + if (!c_flexible_array_member_type_p (TREE_TYPE (subdatum))) + return NULL_TREE; + + tree attr_counted_by = lookup_attribute ("counted_by", + DECL_ATTRIBUTES (subdatum)); + tree counted_by_ref = NULL_TREE; + *counted_by_type = NULL_TREE; + if (attr_counted_by) + { + tree field_id = TREE_VALUE (TREE_VALUE (attr_counted_by)); + counted_by_ref + = build_component_ref (UNKNOWN_LOCATION, + datum, field_id, + UNKNOWN_LOCATION, UNKNOWN_LOCATION); + counted_by_ref = build_fold_addr_expr (counted_by_ref); + + /* Get the TYPE of the counted_by field. */ + tree counted_by_field = lookup_field (type, field_id); + gcc_assert (counted_by_field); + + do + { + *counted_by_type = TREE_TYPE (TREE_VALUE (counted_by_field)); + counted_by_field = TREE_CHAIN (counted_by_field); + } + while (counted_by_field); + } + return counted_by_ref; +} + +/* Given a COMPONENT_REF REF with the location LOC, the corresponding + COUNTED_BY_REF, and the COUNTED_BY_TYPE, generate an INDIRECT_REF + to a call to the internal function .ACCESS_WITH_SIZE. + + REF + + to: + + (*.ACCESS_WITH_SIZE (REF, COUNTED_BY_REF, 1, (TYPE_OF_SIZE)0, -1)) + + NOTE: The return type of this function is the POINTER type pointing + to the original flexible array type. + Then the type of the INDIRECT_REF is the original flexible array type. + + The type of the first argument of this function is a POINTER type + to the original flexible array type. + + The 4th argument of the call is a constant 0 with the TYPE of the + object pointed by COUNTED_BY_REF. + + */ +static tree +build_access_with_size_for_counted_by (location_t loc, tree ref, + tree counted_by_ref, + tree counted_by_type) +{ + gcc_assert (c_flexible_array_member_type_p (TREE_TYPE (ref))); + /* The result type of the call is a pointer to the flexible array type. */ + tree result_type = build_pointer_type (TREE_TYPE (ref)); + + tree call + = build_call_expr_internal_loc (loc, IFN_ACCESS_WITH_SIZE, + result_type, 5, + array_to_pointer_conversion (loc, ref), + counted_by_ref, + build_int_cst (integer_type_node, 1), + build_int_cst (counted_by_type, 0), + build_int_cst (integer_type_node, -1)); + /* Wrap the call with an INDIRECT_REF with the flexible array type. */ + call = build1 (INDIRECT_REF, TREE_TYPE (ref), call); + SET_EXPR_LOCATION (call, loc); + return call; +} + /* Make an expression to refer to the COMPONENT field of structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the location of the COMPONENT_REF. COMPONENT_LOC is the location of COMPONENT. ARROW_LOC is the location of the first -> operand if - it is from -> operator. */ + it is from -> operator. + If HANDLE_COUNTED_BY is true, check the counted_by attribute and generate + a call to .ACCESS_WITH_SIZE. Otherwise, ignore the attribute. */ tree build_component_ref (location_t loc, tree datum, tree component, - location_t component_loc, location_t arrow_loc) + location_t component_loc, location_t arrow_loc, + bool handle_counted_by) { tree type = TREE_TYPE (datum); enum tree_code code = TREE_CODE (type); @@ -2664,7 +2765,13 @@ build_component_ref (location_t loc, tree datum, tree component, int quals; tree subtype; bool use_datum_quals; - + tree counted_by_type = NULL_TREE; + /* Do not handle counted_by when in typeof and alignof operator. */ + handle_counted_by = handle_counted_by && !in_typeof && !in_alignof; + tree counted_by_ref = handle_counted_by + ? build_counted_by_ref (datum, subdatum, + &counted_by_type) + : NULL_TREE; if (TREE_TYPE (subdatum) == error_mark_node) return error_mark_node; @@ -2683,6 +2790,12 @@ build_component_ref (location_t loc, tree datum, tree component, ref = build3 (COMPONENT_REF, subtype, datum, subdatum, NULL_TREE); SET_EXPR_LOCATION (ref, loc); + + if (counted_by_ref) + ref = build_access_with_size_for_counted_by (loc, ref, + counted_by_ref, + counted_by_type); + if (TREE_READONLY (subdatum) || (use_datum_quals && TREE_READONLY (datum))) TREE_READONLY (ref) = 1; @@ -5087,7 +5200,11 @@ build_unary_op (location_t location, enum tree_code code, tree xarg, goto return_build_unary_op; } - /* Ordinary case; arg is a COMPONENT_REF or a decl. */ + /* Ordinary case; arg is a COMPONENT_REF or a decl, or a call to + .ACCESS_WITH_SIZE. */ + if (is_access_with_size_p (arg)) + arg = TREE_OPERAND (TREE_OPERAND (CALL_EXPR_ARG (arg, 0), 0), 0); + argtype = TREE_TYPE (arg); /* If the lvalue is const or volatile, merge that into the type @@ -5238,6 +5355,9 @@ lvalue_p (const_tree ref) case BIND_EXPR: return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE; + case CALL_EXPR: + return is_access_with_size_p (ref); + default: return false; } @@ -8525,6 +8645,20 @@ digest_init (location_t init_loc, tree type, tree init, tree origtype, STRIP_TYPE_NOPS (inside_init); + /* If require_constant is TRUE, when the initializer is a call to + .ACCESS_WITH_SIZE, use the first argument as the initializer. + For example: + y = (char *) .ACCESS_WITH_SIZE ((char *) &static_annotated.c,...) + will be converted to + y = &static_annotated.c. */ + + if (require_constant + && TREE_CODE (inside_init) == NOP_EXPR + && TREE_CODE (TREE_OPERAND (inside_init, 0)) == CALL_EXPR + && is_access_with_size_p (TREE_OPERAND (inside_init, 0))) + inside_init + = get_ref_from_access_with_size (TREE_OPERAND (inside_init, 0)); + if (!c_in_omp_for) { if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR) diff --git a/gcc/internal-fn.cc b/gcc/internal-fn.cc index 9c09026793fa..eb2c4cd59048 100644 --- a/gcc/internal-fn.cc +++ b/gcc/internal-fn.cc @@ -3438,6 +3438,40 @@ expand_DEFERRED_INIT (internal_fn, gcall *stmt) } } +/* Expand the IFN_ACCESS_WITH_SIZE function: + ACCESS_WITH_SIZE (REF_TO_OBJ, REF_TO_SIZE, CLASS_OF_SIZE, + TYPE_OF_SIZE, ACCESS_MODE) + which returns the REF_TO_OBJ same as the 1st argument; + + 1st argument REF_TO_OBJ: The reference to the object; + 2nd argument REF_TO_SIZE: The reference to the size of the object, + 3rd argument CLASS_OF_SIZE: The size referenced by the REF_TO_SIZE represents + 0: the number of bytes. + 1: the number of the elements of the object type; + 4th argument TYPE_OF_SIZE: A constant 0 with its TYPE being the same as the TYPE + of the object referenced by REF_TO_SIZE + 5th argument ACCESS_MODE: + -1: Unknown access semantics + 0: none + 1: read_only + 2: write_only + 3: read_write + + Both the return type and the type of the first argument of this + function have been converted from the incomplete array type to + the corresponding pointer type. + + For each call to a .ACCESS_WITH_SIZE, replace it with its 1st argument. */ + +static void +expand_ACCESS_WITH_SIZE (internal_fn, gcall *stmt) +{ + tree lhs = gimple_call_lhs (stmt); + tree ref_to_obj = gimple_call_arg (stmt, 0); + if (lhs) + expand_assignment (lhs, ref_to_obj, false); +} + /* The size of an OpenACC compute dimension. */ static void diff --git a/gcc/internal-fn.def b/gcc/internal-fn.def index 25badbb86e56..8de1fa882e95 100644 --- a/gcc/internal-fn.def +++ b/gcc/internal-fn.def @@ -512,6 +512,11 @@ DEF_INTERNAL_FN (PHI, 0, NULL) automatic variable. */ DEF_INTERNAL_FN (DEFERRED_INIT, ECF_CONST | ECF_LEAF | ECF_NOTHROW, NULL) +/* A function to associate the access size and access mode information + with the corresponding reference to an object. It only reads from the + 2nd argument. */ +DEF_INTERNAL_FN (ACCESS_WITH_SIZE, ECF_PURE | ECF_LEAF | ECF_NOTHROW, NULL) + /* DIM_SIZE and DIM_POS return the size of a particular compute dimension and the executing thread's position within that dimension. DIM_POS is pure (and not const) so that it isn't diff --git a/gcc/testsuite/gcc.dg/flex-array-counted-by-2.c b/gcc/testsuite/gcc.dg/flex-array-counted-by-2.c new file mode 100644 index 000000000000..d4899a63af3c --- /dev/null +++ b/gcc/testsuite/gcc.dg/flex-array-counted-by-2.c @@ -0,0 +1,112 @@ +/* Test the code generation for the new attribute counted_by. + And also the offsetof operator on such array. */ +/* { dg-do run } */ +/* { dg-options "-O2 -fdump-tree-original" } */ + +#include + +struct annotated { + int b; + char c[] __attribute__ ((counted_by (b))); +} *array_annotated; + +static struct annotated static_annotated = { sizeof "hello", "hello" }; +static char *y = static_annotated.c; + +struct flex { + int b; + char c[]; +}; + +struct nested_annotated { + struct { + union { + int b; + float f; + }; + int n; + }; + char c[] __attribute__ ((counted_by (b))); +} *array_nested_annotated; + +static struct nested_annotated nested_static_annotated + = { sizeof "hello1", 0, "hello1" }; +static char *nested_y = nested_static_annotated.c; + +struct nested_flex { + struct { + union { + int b; + float f; + }; + int n; + }; + char c[]; +}; + +void __attribute__((__noinline__)) setup (int normal_count, int attr_count) +{ + array_annotated + = (struct annotated *)malloc (sizeof (struct annotated) + + attr_count * sizeof (char)); + array_annotated->b = attr_count; + + array_nested_annotated + = (struct nested_annotated *)malloc (sizeof (struct nested_annotated) + + attr_count * sizeof (char)); + array_nested_annotated->b = attr_count; + + return; +} + +void __attribute__((__noinline__)) test (char a, char b) +{ + if (__builtin_offsetof (struct annotated, c[0]) + != __builtin_offsetof (struct flex, c[0])) + abort (); + if (__builtin_offsetof (struct annotated, c[1]) + != __builtin_offsetof (struct flex, c[1])) + abort (); + if (__builtin_offsetof (struct nested_annotated, c[0]) + != __builtin_offsetof (struct nested_flex, c[0])) + abort (); + if (__builtin_offsetof (struct nested_annotated, c[1]) + != __builtin_offsetof (struct nested_flex, c[1])) + abort (); + + if (__builtin_types_compatible_p (typeof (array_annotated->c), + typeof (&(array_annotated->c)[0]))) + abort (); + if (__builtin_types_compatible_p (typeof (array_nested_annotated->c), + typeof (&(array_nested_annotated->c)[0]))) + abort (); + + if (__alignof (array_annotated->c) != __alignof (char)) + abort (); + if (__alignof (array_nested_annotated->c) != __alignof (char)) + abort (); + + if ((unsigned long) array_annotated->c != (unsigned long) &array_annotated->c) + abort (); + if ((unsigned long) array_nested_annotated->c + != (unsigned long) &array_nested_annotated->c) + abort (); + + array_annotated->c[2] = a; + array_nested_annotated->c[3] = b; + + if (y[2] != 'l') abort (); + if (nested_y[4] !='o') abort (); + +} + +int main(int argc, char *argv[]) +{ + setup (10,10); + test ('A', 'B'); + if (array_annotated->c[2] != 'A') abort (); + if (array_nested_annotated->c[3] != 'B') abort (); + return 0; +} + +/* { dg-final { scan-tree-dump-times "ACCESS_WITH_SIZE" 8 "original" } } */ diff --git a/gcc/tree.cc b/gcc/tree.cc index 6564b002dc1a..01572fe70f72 100644 --- a/gcc/tree.cc +++ b/gcc/tree.cc @@ -13405,6 +13405,28 @@ component_ref_size (tree ref, special_array_member *sam /* = NULL */) ? NULL_TREE : size_zero_node); } +/* Return true if the given node CALL is a call to a .ACCESS_WITH_SIZE + function. */ +bool +is_access_with_size_p (const_tree call) +{ + if (TREE_CODE (call) != CALL_EXPR) + return false; + if (CALL_EXPR_IFN (call) == IFN_ACCESS_WITH_SIZE) + return true; + return false; +} + +/* Get the corresponding reference from the call to a .ACCESS_WITH_SIZE. + * i.e the first argument of this call. Return NULL_TREE otherwise. */ +tree +get_ref_from_access_with_size (tree call) +{ + if (is_access_with_size_p (call)) + return CALL_EXPR_ARG (call, 0); + return NULL_TREE; +} + /* Return the machine mode of T. For vectors, returns the mode of the inner type. The main use case is to feed the result to HONOR_NANS, avoiding the BLKmode that a direct TYPE_MODE (T) might return. */ diff --git a/gcc/tree.h b/gcc/tree.h index ee2aae332a41..604885641184 100644 --- a/gcc/tree.h +++ b/gcc/tree.h @@ -5772,6 +5772,14 @@ extern special_array_member component_ref_sam_type (tree); cannot be determined. */ extern tree component_ref_size (tree, special_array_member * = NULL); +/* Return true if the given node is a call to a .ACCESS_WITH_SIZE + function. */ +extern bool is_access_with_size_p (const_tree); + +/* Get the corresponding reference from the call to a .ACCESS_WITH_SIZE, + * i.e. the first argument of this call. Return NULL_TREE otherwise. */ +extern tree get_ref_from_access_with_size (tree); + extern int tree_map_base_eq (const void *, const void *); extern unsigned int tree_map_base_hash (const void *); extern bool tree_map_base_marked_p (const void *); From patchwork Thu May 30 12:26:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qing Zhao X-Patchwork-Id: 1941701 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Vqlty3YrZz20Pc for ; Thu, 30 May 2024 22:29:46 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C7930385E836 for ; Thu, 30 May 2024 12:29:44 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by sourceware.org (Postfix) with ESMTPS id D3C10385B529 for ; Thu, 30 May 2024 12:27:21 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D3C10385B529 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 D3C10385B529 Authentication-Results: server2.sourceware.org; arc=pass smtp.remote-ip=205.220.165.32 ARC-Seal: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1717072045; cv=pass; b=kpwNPd3Hl4mX98NgD/vi1bAVf1FPOcoDqeQaDS9leGLT+1GfuCRB15Iazx4fliH+oZ0ACJCkeDxsoQ5AKWp9YrLyaq5AC83kgI9XdHKIAgcX7fiMFkd8/XqzRDwF9mXS03dmJcCNGx5BQOUG/JGSzCQ/wvYZYPtJSUmBhtvv7Go= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1717072045; c=relaxed/simple; bh=k6laV2/S+lpsg60nksLhNFZR3HMSDSZace6OpIsLnys=; h=DKIM-Signature:DKIM-Signature:From:To:Subject:Date:Message-Id: MIME-Version; b=QHjgPZNCy3ztBXSDcdbqsfyh3sO8hzOW8WkMDLbbkGBaNmOFbaeiRFy/PeSgTz5tlu7HeyM9cXSzBM0uIi5Adb7gWmxGsCMQPwd5c+FhPAiMzHmgPik0yK9CHlq/8t6s5YNE/kDFtny0/RPQh/w3MWZFRGNXiWPdmIR5AYElHQU= ARC-Authentication-Results: i=2; server2.sourceware.org Received: from pps.filterd (m0246629.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 44U7o8FZ007147; Thu, 30 May 2024 12:27:19 GMT DKIM-Signature: =?UTF-8?Q?v=3D1; _a=3Drsa-sha256; _c=3Drelaxed/relaxed; _d=3Doracle.com; _h?= =?utf-8?q?=3Dcc=3Acontent-transfer-encoding=3Acontent?= =?utf-8?q?-type=3Adate=3Afrom=3Ain-reply-to=3Amessage-id=3Amime-version=3Ar?= =?utf-8?q?eferences=3Asubject=3Ato=3B_s=3Dcorp-2023-11-20=3B_bh=3DG8sePRIFL?= =?utf-8?q?0O9qIP7D5ALR/Ap+YtPr+HvCczMpVsAtug=3D=3B_b=3DWe51FCezPuMTFArYci8Y?= =?utf-8?q?CGK1SO3dZyb2JbCG5TIxphPxhYFumDbZKHdaBm0cxX7rUb8H_MCtUX6n3mZxqSpmH?= =?utf-8?q?pjW20vPZmwuWNTgsLp0msjZYIjurkjRL3lffVZyT+ZcjsZT1Z7n1_ZcwQZ9RBYNjD?= =?utf-8?q?G7OpR0Qd61183RTDMcJ6mOG6FlD7LGahNERHmaO1k8PF8TAuJFglmllt_K0pXViTL?= =?utf-8?q?Lnqqn09wcHQAVHsM1lnNpMSR1V9WMZY0e4YAfgq8SxgMHMuoR47rJz1KXge3_T5vb?= =?utf-8?q?mmJCDMgFK09IBnuIvRgqhaA189r5SuznLiGjUm9iJIEZFYH9DaJQwBJAI8Kw2+ut_?= =?utf-8?q?nw=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 3yb8p7rrhw-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 30 May 2024 12:27:18 +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 44UC6j40026519; Thu, 30 May 2024 12:27:17 GMT Received: from nam10-dm6-obe.outbound.protection.outlook.com (mail-dm6nam10lp2101.outbound.protection.outlook.com [104.47.58.101]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 3yc508fwwb-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 30 May 2024 12:27:17 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=TKNpt56LKJHD0kpgMSlioJDZizztKhu7yPxnFNKwRmdu86+XkaIuXct+j/Jb6ERqkKabomn6L3qoX+8ru3+rqViS5ZMx7vrHJF68lanMe7SDmD+IBEZDXW40UOqswokeXB/J32SAm0ZadBoH4Lv+ydBJwjpZiI5AevexjHWcjIBs28zFm8C9pWZugU3YocIj4CKGJwhPRq1vufBSg8AjEQftAgRrUDpPoJvb9bj3VLOTTz+RwOpy0R/NOFSbJjn3JFPWra+n0Umt4a2IjNU7s+4AjOCvfNHoQ1gzYpSL0YNJ3Yq7yizd1+1exa0qDPrwYQBC/qKmlraxuzqC21aw9g== 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=G8sePRIFL0O9qIP7D5ALR/Ap+YtPr+HvCczMpVsAtug=; b=Tl2qV5GVh5OBdmpS0P0V8YINiNozcJLbxHBQsenrPX2NtnwaI79WCWvw8QjbkyyuLQqkUK2dwxQp4sCKJjaOFDPTFXWnhLRKycwLfksWQ/+rRgb4ksicpdqHxhsp+JWQLYTaE2VzxIrOkDABemPofOS9BsQ2s9/DnS/zOAwc0uq54zCrqlP62QWRaOBzJcYO2q160la5vH1L7LhgxO7/M4NIPnD6iKSVTBmM7NMRsEq2AWba02gdvtnPkSr2G3PIri99afmcA/AZLHsYuKCuidM8SZpwXdy1bb2VBAvE0OQPL7WwjwpikCbmLPP2tEZA0et4N5r06QPRlSYgHzbtLA== 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=G8sePRIFL0O9qIP7D5ALR/Ap+YtPr+HvCczMpVsAtug=; b=nD1sFCgh077R7oVBI4QFDB6U+usAU+wrBX7JEyVMYcLJHJBBt6/TLLhHvCZjOoIkI+r2fUi9q4cHb6/ZA1LWnGsZGpspNS4WL7kjMA5d9Kd8zJoie8o50psdzRSU7lTEpDgRX4/WLQHD5FDCNfKMNOUoxnWwvdbmmsGGJ+X9hxc= 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:12 +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:11 +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 3/5] Use the .ACCESS_WITH_SIZE in builtin object size. Date: Thu, 30 May 2024 12:26:58 +0000 Message-Id: <20240530122700.1516243-4-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> X-ClientProxiedBy: BL6PEPF00013DF8.NAMP222.PROD.OUTLOOK.COM (2603:10b6:22e:400:0:1001:0:b) 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: 8d3ff53d-dc17-4fcf-3a45-08dc80a3d4c5 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: uqagsPGtEPjD+JiH4DQYoqfhX1UUGyPj+HJhMxniyxZWuQ/F1AOR1w89iTUXX0e7suam7BTPaGdL7C319Twpxpz1zccUTuC/asJ8FxEroI+AbSDjN8Y5TCBo+pBW2WYHECT7OAUu3B7PhsojtIu65FsrLzCr04xAwYEwX+2T12Q2bKZbqzwhDFWC+i84vfrT+pycvPbUQbL4/Zv2LZykk0D94wMsAZ7y1WiI8VAUhYleD60sqq6YgYT6D4SkpAt4KNINvxLZIoyqyNuKIMIsLkmlthhZqSHRACz8vwfC1Eucx1SYiXXsq/0fbuSAOzJnumtuegoOIJjdU/dI1q8zC8zks9YjBqdsrxGBqnapJFIjrmo/iyID53w99L2N1KRtUv/JCa2uIDCM19zB5lbj9YOaTP0MZ3MRI84VFwfjvsNe8pngpWfU4wOcl2MHCcHAHT+wgSM9YYbGO6uTdMJ+qnbLuBS3WR/r1yUs6h8zDnt+xildppxp0wEOCgSqS/fFhbGhXldiP4G/n/QE2VHVliNntGjwflu8iB8Y/uavqXvcICnkcIvUwwg18l62DMculbCtYTOCehB1kz8LUrlHiqlBkwPHjHs+YwUS+YyyzgIh23NYxBcZL4RPdfxO8FuugZTExr0NaCIkJ6mbBLd4wnYNsdvvM+SiJ9Mo7bpaf+361GH3Litqte+QN3rZQgQPF4i190cG3I5je3/65mbW4OiJL+P7KAK79kVpCFnkw7ID7JFQDQUgy5+7RBhFfqBAsjyUvyvlhrZByccKXNj5oSqX6D6bO3Z89Amq2sSv/shEwpTUFMhGLP+psnCLJywf5a2kqazQqZteJ1hDB2yRM7i6AFr2KPqdsUssop8KZiGLngz7gn/DeVb7Bhrp0VOn10ItN3XY+50GndI9O2ymlV7yYz6Z94yF0tDf3u/7GbGpEY+D7LXkQehDUuwB08mdtGP0zlUuveeV9SSsa4CM4thdg6IaA1QrM/nEuzUcHweDVyVaBhbt5GcHwrxfdvUS/G3mnVuffu6IYFe2nP3czWbNVQqZuR/rQINi+iHfu5NXC+eBwqlnpPpj3ygmvUY8OHzZSevla4Xu+ZfVOu6XG+8xKhjwBt7azeuYhUQB5dji1bZCMP3XFOc+BI2BFMPYuMQcoZt/yR+5IMbDiZN0GXe9ahIbhxFhMPkH/unEy+1RyAiXnPxztOgUYHu27vcsJlmvzoWEkMSyc6jLsMqZ5oLuArSAvbCHxOVOk0CYAv0= 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: J4Z3Nw+CDfN9v4JgV7YYKz/wVWKUppiI5kzR2cU7Y+Kj7mDR/hnkm6c/WJWf5bRBNgRGp14MHXKOK5a5dgpbm7ID1D45wOD3zVOwsOT863TvvZDbBH/xkM2/4CeE23gsrpqesh3XodMK7+5+Dg10L5CoikKdXiP5DcGzuhqjL6h53ZMi64eOQRglwy5O5KKwp3sS3I4jKM5bFXH91sxnsO+WLFjDBAf0ZNZqny2LKtTQzN0sQpqTVUP5MxtxXmE4KOdHxNhAoxf4iuD5J8asSYQbm65xbBqXx2MoRw0LN+sO3NMOo6dyLo9d0RUIEPdal6xLntVwZNkwY4CRDYI3XPV2ReOx8tlRyAbSsQ4gJJ5DlLYl/lx0Vgdtzy9sNoj5K9sVvbWZQqUAMDJnsAXatowbylo8SxX9U4WtNM68jD3h6RZGiQrNHne/0l5jLXJmEsaFDhxyvCf2OBaAgVtbU1UuvyfwOp80GucV2lOxXf9y5rqSDLuwHqwyI3LTj/vOY8PIHTRmF+ziW/iWSBUMeSgcppAoBAGb2k/S+Jz1oto0U86nW3CxAkUqMFWEhcFuDO2LA5XOkuBnkE+9RdH1f+Zv/R0cd3h5LcC0bbeoiErcoOE3sEWZpGx1/BvWhtY/duURoqc5KQNvpwq0+i7xz1NcnVqQnocAEiTM2mt7T+BO0YL8iwuv3QUOb7pJqc8c8c9DbbfJ3riqHEGSm0NjkzVuzRxgjnFXElRJkg2nSv4PVjQ0fjKbTELX+f0EVdx4IVwJYWKVlteb7Aq8N/AOco1VqJjeZEJuZyAndhh1wKrH1yFPaCxfnFbNXgzQIkQDbCpPF/H0pCjlm1/WPGKYi67O6TjbxbVkOGMou1R+HkyXLK7I69ETqVsl8jYhMz/KR7cwyx5cfGqd67ZO3mZxFsqvoZBXSL6yvsIo4bCynPN/VHPWGe710QNWTpXDtjt12CGNc94kKFT3FvBQj2dNcef4E8mV0OxkEjS+stniKASixos6G53pE5n00lHEkJtYSXr26B/9XiuflhUppE6FjcaG9Gp+l1bfIx55/NUqZYeRxqZzqq98DCcIjR/6sd4TbMRZNgCynWC2YC9NE4b8VVaVzEg2aVMdfzGdtvtoShKiA70vkTiMwKFp8duC7ys1wSUMojDV3B2fygy5DbM8DiZVdVEZtPW2VslqJ+xDsgE0cpqbU0MvI0QcIurP+URiwT+xz7nXfDpFZoDdvAlkINrPgosY++iOD4X9wAO63F9HaV/Gw13jS6AbxhHnfcbL6R6o9UQo8sqHewP1qEjYxmPxNEq5ojGCK3v5mBG/89MlRiWfjpFxEOOrkymhqfA82edWh9zzMxqlNDfTjqLmAmb99nGSA8ppV3biCzC1B7LUfGSzmHYxq8roI5PdSz5z1LXNM/2Iqq2s06U++wQfXuBu/1M/VoX/jS130VnoSJrS0XVjn63vM1067mIw+ccpYIOk9c5UptLhUVLivTuJKBn481iGBMzExxKsA17diBw+HkFgVVpr1GGCWp+JXQt/nQ5Ztd/fjPZKaVnAII/8ew92PPbPzfFyi/m0jS/8ocsyw5xJAsLBI9NgPiodv6GQ X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: xovonb/VP6SUXlwrjzcmIEY3L0/0y/12sUNarD+GgPof5Qb+jvCetkAo5Ie13wGCt/nYPeye3grZznIlLh5+CfvhPoHTCxGZ3x7ptWz6HqU347dDPdlLS/pQnkfTWJPkhRqbRINTn8cW8Ade7yyfCZ03l3iAbVUTfOMO6ipSQdLC7wwU//RYkqzkFFb7F0+s9BSDnRp9EUDkGlpctjtQfGTslAII4IKAV67oS09xQDNx28DS2GXMMitT4f0jJdF3NMZrOfP4Bfaldmhj94mNuY4k2xpWE47PBZETvy2ReCoyXmmPkSEzbtxhfEWOIWzYUkvAyvE8EZskHHKla5vezi9M3Arx9i/lOwitfZEb4Pb/7hT8wYKDWCENb/I4Fby9DPMG0azWhjK7gwFxrZpYgJND5TANt96mp0UrIaNOupYD745bwzg6W2rXEGagH6xfcvKNrn+mSz2/ndIHt2i9Kncl23vH3Iv8bJbTODMCCYUfPv4PdwsRTEgJKkAc3LhA/DOqx1kdKsmsvRcvUpuSDnQw8A9slI9xegis0l19378B9POYRQj1pDOuZVmxwRMdHMWWjEDLB5ecBuNMY/rf58JP7/jfO5VvructLXDcf8E= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 8d3ff53d-dc17-4fcf-3a45-08dc80a3d4c5 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:11.8806 (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: ZQOHxYsJmf4dwchS+5mb/ZX/+SUtoTxDhrV0zE6ZD9Xno53Db7V/ZAPCx+sziJ65mQV0hzIby8x4CfIIULRtWA== 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=999 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: 69wSFVOhsAwgtjsU0Zhznqakd8DKiKo1 X-Proofpoint-GUID: 69wSFVOhsAwgtjsU0Zhznqakd8DKiKo1 X-Spam-Status: No, score=-10.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, KAM_DMARC_QUARANTINE, KAM_SHORT, RCVD_IN_MSPIKE_H3, 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 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org gcc/ChangeLog: * tree-object-size.cc (access_with_size_object_size): New function. (call_object_size): Call the new function. gcc/testsuite/ChangeLog: * gcc.dg/builtin-object-size-common.h: Add a new macro EXPECT. * gcc.dg/flex-array-counted-by-3.c: New test. * gcc.dg/flex-array-counted-by-4.c: New test. * gcc.dg/flex-array-counted-by-5.c: New test. --- .../gcc.dg/builtin-object-size-common.h | 11 ++ .../gcc.dg/flex-array-counted-by-3.c | 63 +++++++ .../gcc.dg/flex-array-counted-by-4.c | 178 ++++++++++++++++++ .../gcc.dg/flex-array-counted-by-5.c | 48 +++++ gcc/tree-object-size.cc | 60 ++++++ 5 files changed, 360 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/flex-array-counted-by-3.c create mode 100644 gcc/testsuite/gcc.dg/flex-array-counted-by-4.c create mode 100644 gcc/testsuite/gcc.dg/flex-array-counted-by-5.c diff --git a/gcc/testsuite/gcc.dg/builtin-object-size-common.h b/gcc/testsuite/gcc.dg/builtin-object-size-common.h index 66ff7cdd953a..b677067c6e6b 100644 --- a/gcc/testsuite/gcc.dg/builtin-object-size-common.h +++ b/gcc/testsuite/gcc.dg/builtin-object-size-common.h @@ -30,3 +30,14 @@ unsigned nfails = 0; __builtin_abort (); \ return 0; \ } while (0) + +#define EXPECT(p, _v) do { \ + size_t v = _v; \ + if (p == v) \ + __builtin_printf ("ok: %s == %zd\n", #p, p); \ + else \ + { \ + __builtin_printf ("WAT: %s == %zd (expected %zd)\n", #p, p, v); \ + FAIL (); \ + } \ +} while (0); diff --git a/gcc/testsuite/gcc.dg/flex-array-counted-by-3.c b/gcc/testsuite/gcc.dg/flex-array-counted-by-3.c new file mode 100644 index 000000000000..78f50230e891 --- /dev/null +++ b/gcc/testsuite/gcc.dg/flex-array-counted-by-3.c @@ -0,0 +1,63 @@ +/* Test the attribute counted_by and its usage in + * __builtin_dynamic_object_size. */ +/* { dg-do run } */ +/* { dg-options "-O2" } */ + +#include "builtin-object-size-common.h" + +struct flex { + int b; + int c[]; +} *array_flex; + +struct annotated { + int b; + int c[] __attribute__ ((counted_by (b))); +} *array_annotated; + +struct nested_annotated { + struct { + union { + int b; + float f; + }; + int n; + }; + int c[] __attribute__ ((counted_by (b))); +} *array_nested_annotated; + +void __attribute__((__noinline__)) setup (int normal_count, int attr_count) +{ + array_flex + = (struct flex *)malloc (sizeof (struct flex) + + normal_count * sizeof (int)); + array_flex->b = normal_count; + + array_annotated + = (struct annotated *)malloc (sizeof (struct annotated) + + attr_count * sizeof (int)); + array_annotated->b = attr_count; + + array_nested_annotated + = (struct nested_annotated *)malloc (sizeof (struct nested_annotated) + + attr_count * sizeof (int)); + array_nested_annotated->b = attr_count; + + return; +} + +void __attribute__((__noinline__)) test () +{ + EXPECT(__builtin_dynamic_object_size(array_flex->c, 1), -1); + EXPECT(__builtin_dynamic_object_size(array_annotated->c, 1), + array_annotated->b * sizeof (int)); + EXPECT(__builtin_dynamic_object_size(array_nested_annotated->c, 1), + array_nested_annotated->b * sizeof (int)); +} + +int main(int argc, char *argv[]) +{ + setup (10,10); + test (); + DONE (); +} diff --git a/gcc/testsuite/gcc.dg/flex-array-counted-by-4.c b/gcc/testsuite/gcc.dg/flex-array-counted-by-4.c new file mode 100644 index 000000000000..20103d58ef51 --- /dev/null +++ b/gcc/testsuite/gcc.dg/flex-array-counted-by-4.c @@ -0,0 +1,178 @@ +/* Test the attribute counted_by and its usage in +__builtin_dynamic_object_size: what's the correct behavior when the +allocation size mismatched with the value of counted_by attribute? +We should always use the latest value that is hold by the counted_by +field. */ +/* { dg-do run } */ +/* { dg-options "-O -fstrict-flex-arrays=3" } */ + +#include "builtin-object-size-common.h" + +struct annotated { + size_t foo; + char others; + char array[] __attribute__((counted_by (foo))); +}; + +#define noinline __attribute__((__noinline__)) +#define SIZE_BUMP 10 +#define MAX(a, b) ((a) > (b) ? (a) : (b)) + +/* In general, Due to type casting, the type for the pointee of a pointer + does not say anything about the object it points to, + So, __builtin_object_size can not directly use the type of the pointee + to decide the size of the object the pointer points to. + + There are only two reliable ways: + A. observed allocations (call to the allocation functions in the routine) + B. observed accesses (read or write access to the location of the + pointer points to) + + That provide information about the type/existence of an object at + the corresponding address. + + For A, we use the "alloc_size" attribute for the corresponding allocation + functions to determine the object size; + (We treat counted_by attribute the same as the "alloc_size" attribute) + + For B, we use the SIZE info of the TYPE attached to the corresponding access. + + The only other way in C which ensures that a pointer actually points + to an object of the correct type is 'static': + + void foo(struct P *p[static 1]); + + See https://gcc.gnu.org/pipermail/gcc-patches/2023-July/624814.html + for more details. */ + +/* In the following function, malloc allocated more space than the value + of counted_by attribute. Then what's the correct behavior we expect + the __builtin_dynamic_object_size should have for each of the cases? */ + +static struct annotated * noinline alloc_buf_more (size_t index) +{ + struct annotated *p; + size_t allocated_size + = MAX (sizeof (struct annotated), + (__builtin_offsetof (struct annotated, array[0]) + + (index + SIZE_BUMP) * sizeof (char))); + p = (struct annotated *) malloc (allocated_size); + + p->foo = index; + + /* When checking the observed access p->array, we have info on both + observered allocation and observed access, + A.1 from observed allocation: + allocated_size - offsetof (struct annotated, array[0]) + + A.2 from the counted-by attribute: + p->foo * sizeof (char) + + We always use the latest value that is hold by the counted-by field. + */ + + EXPECT(__builtin_dynamic_object_size(p->array, 0), + (p->foo) * sizeof(char)); + + EXPECT(__builtin_dynamic_object_size(p->array, 1), + (p->foo) * sizeof(char)); + + EXPECT(__builtin_dynamic_object_size(p->array, 2), + (p->foo) * sizeof(char)); + + EXPECT(__builtin_dynamic_object_size(p->array, 3), + (p->foo) * sizeof(char)); + + /* When checking the pointer p, we only have info on the observed allocation. + So, the object size info can only been obtained from the call to malloc. + For both MAXIMUM and MINIMUM: A = (index + SIZE_BUMP) * sizeof (char) */ + EXPECT(__builtin_dynamic_object_size(p, 0), allocated_size); + EXPECT(__builtin_dynamic_object_size(p, 1), allocated_size); + EXPECT(__builtin_dynamic_object_size(p, 2), allocated_size); + EXPECT(__builtin_dynamic_object_size(p, 3), allocated_size); + return p; +} + +/* In the following function, malloc allocated less space than the value + of counted_by attribute. Then what's the correct behavior we expect + the __builtin_dynamic_object_size should have for each of the cases? + NOTE: this is an user error, GCC should issue warnings for such case. + This is a seperate issue we should address later. */ + +static struct annotated * noinline alloc_buf_less (size_t index) +{ + struct annotated *p; + size_t allocated_size + = MAX (sizeof (struct annotated), + (__builtin_offsetof (struct annotated, array[0]) + + (index) * sizeof (char))); + p = (struct annotated *) malloc (allocated_size); + + p->foo = index + SIZE_BUMP; + + /* When checking the observed access p->array, we have info on both + observered allocation and observed access, + A.1 from observed allocation: + allocated_size - offsetof (struct annotated, array[0]) + A.2 from the counted-by attribute: + p->foo * sizeof (char) + + We always use the latest value that is hold by the counted-by field. + */ + + EXPECT(__builtin_dynamic_object_size(p->array, 0), + (p->foo) * sizeof(char)); + + EXPECT(__builtin_dynamic_object_size(p->array, 1), + (p->foo) * sizeof(char)); + + EXPECT(__builtin_dynamic_object_size(p->array, 2), + (p->foo) * sizeof(char)); + + EXPECT(__builtin_dynamic_object_size(p->array, 3), + (p->foo) * sizeof(char)); + + /* When checking the pointer p, we only have info on the observed + allocation. So, the object size info can only been obtained from + the call to malloc. */ + EXPECT(__builtin_dynamic_object_size(p, 0), allocated_size); + EXPECT(__builtin_dynamic_object_size(p, 1), allocated_size); + EXPECT(__builtin_dynamic_object_size(p, 2), allocated_size); + EXPECT(__builtin_dynamic_object_size(p, 3), allocated_size); + return p; +} + +int main () +{ + struct annotated *p, *q; + p = alloc_buf_more (10); + q = alloc_buf_less (10); + + /* When checking the access p->array, we only have info on the counted-by + value. */ + EXPECT(__builtin_dynamic_object_size(p->array, 0), p->foo * sizeof(char)); + EXPECT(__builtin_dynamic_object_size(p->array, 1), p->foo * sizeof(char)); + EXPECT(__builtin_dynamic_object_size(p->array, 2), p->foo * sizeof(char)); + EXPECT(__builtin_dynamic_object_size(p->array, 3), p->foo * sizeof(char)); + /* When checking the pointer p, we have no observed allocation nor observed + access, therefore, we cannot determine the size info here. */ + EXPECT(__builtin_dynamic_object_size(p, 0), -1); + EXPECT(__builtin_dynamic_object_size(p, 1), -1); + EXPECT(__builtin_dynamic_object_size(p, 2), 0); + EXPECT(__builtin_dynamic_object_size(p, 3), 0); + + /* When checking the access p->array, we only have info on the counted-by + value. */ + EXPECT(__builtin_dynamic_object_size(q->array, 0), q->foo * sizeof(char)); + EXPECT(__builtin_dynamic_object_size(q->array, 1), q->foo * sizeof(char)); + EXPECT(__builtin_dynamic_object_size(q->array, 2), q->foo * sizeof(char)); + EXPECT(__builtin_dynamic_object_size(q->array, 3), q->foo * sizeof(char)); + /* When checking the pointer p, we have no observed allocation nor observed + access, therefore, we cannot determine the size info here. */ + EXPECT(__builtin_dynamic_object_size(q, 0), -1); + EXPECT(__builtin_dynamic_object_size(q, 1), -1); + EXPECT(__builtin_dynamic_object_size(q, 2), 0); + EXPECT(__builtin_dynamic_object_size(q, 3), 0); + + DONE (); +} diff --git a/gcc/testsuite/gcc.dg/flex-array-counted-by-5.c b/gcc/testsuite/gcc.dg/flex-array-counted-by-5.c new file mode 100644 index 000000000000..68f9b0f7c8d2 --- /dev/null +++ b/gcc/testsuite/gcc.dg/flex-array-counted-by-5.c @@ -0,0 +1,48 @@ +/* Test the attribute counted_by and its usage in + * __builtin_dynamic_object_size: when the counted_by field is negative. */ +/* { dg-do run } */ +/* { dg-options "-O2" } */ + +#include "builtin-object-size-common.h" + +struct annotated { + int b; + int c[] __attribute__ ((counted_by (b))); +} *array_annotated; + +struct nested_annotated { + struct { + union { + int b; + float f; + }; + int n; + }; + int c[] __attribute__ ((counted_by (b))); +} *array_nested_annotated; + +void __attribute__((__noinline__)) setup (int attr_count) +{ + array_annotated + = (struct annotated *)malloc (sizeof (struct annotated)); + array_annotated->b = attr_count; + + array_nested_annotated + = (struct nested_annotated *)malloc (sizeof (struct nested_annotated)); + array_nested_annotated->b = attr_count -1; + + return; +} + +void __attribute__((__noinline__)) test () +{ + EXPECT(__builtin_dynamic_object_size(array_annotated->c, 1), 0); + EXPECT(__builtin_dynamic_object_size(array_nested_annotated->c, 1), 0); +} + +int main(int argc, char *argv[]) +{ + setup (-10); + test (); + DONE (); +} diff --git a/gcc/tree-object-size.cc b/gcc/tree-object-size.cc index 018fbc30cbb6..8de264d1dee2 100644 --- a/gcc/tree-object-size.cc +++ b/gcc/tree-object-size.cc @@ -37,6 +37,7 @@ along with GCC; see the file COPYING3. If not see #include "attribs.h" #include "builtins.h" #include "gimplify-me.h" +#include "gimplify.h" struct object_size_info { @@ -60,6 +61,7 @@ static tree compute_object_offset (tree, const_tree); static bool addr_object_size (struct object_size_info *, const_tree, int, tree *, tree *t = NULL); static tree alloc_object_size (const gcall *, int); +static tree access_with_size_object_size (const gcall *, int); static tree pass_through_call (const gcall *); static void collect_object_sizes_for (struct object_size_info *, tree); static void expr_object_size (struct object_size_info *, tree, tree); @@ -749,6 +751,60 @@ addr_object_size (struct object_size_info *osi, const_tree ptr, return false; } +/* Compute __builtin_object_size for a CALL to .ACCESS_WITH_SIZE, + OBJECT_SIZE_TYPE is the second argument from __builtin_object_size. + The 2nd, 3rd, and the 4th parameters of the call determine the size of + the CALL: + + 2nd argument REF_TO_SIZE: The reference to the size of the object, + 3rd argument CLASS_OF_SIZE: The size referenced by the REF_TO_SIZE represents + 0: the number of bytes; + 1: the number of the elements of the object type; + 4th argument TYPE_OF_SIZE: A constant 0 with its TYPE being the same as the TYPE + of the object referenced by REF_TO_SIZE + + The size of the element can be retrived from the result type of the call, + which is the pointer to the array type. */ +static tree +access_with_size_object_size (const gcall *call, int object_size_type) +{ + /* If not for dynamic object size, return. */ + if ((object_size_type & OST_DYNAMIC) == 0) + return size_unknown (object_size_type); + + gcc_assert (gimple_call_internal_p (call, IFN_ACCESS_WITH_SIZE)); + /* Result type is a pointer type to the original flexible array type. */ + tree result_type = gimple_call_return_type (call); + gcc_assert (POINTER_TYPE_P (result_type)); + tree element_size = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (result_type))); + tree ref_to_size = gimple_call_arg (call, 1); + unsigned int class_of_size = TREE_INT_CST_LOW (gimple_call_arg (call, 2)); + tree type = TREE_TYPE (gimple_call_arg (call, 3)); + + tree size = fold_build2 (MEM_REF, type, ref_to_size, + build_int_cst (ptr_type_node, 0)); + + /* If size is negative value, treat it as zero. */ + if (!TYPE_UNSIGNED (type)) + { + tree cond_expr = fold_build2 (LT_EXPR, boolean_type_node, + unshare_expr (size), build_zero_cst (type)); + size = fold_build3 (COND_EXPR, integer_type_node, cond_expr, + build_zero_cst (type), size); + } + + if (class_of_size == 1) + size = size_binop (MULT_EXPR, + fold_convert (sizetype, size), + fold_convert (sizetype, element_size)); + else + size = fold_convert (sizetype, size); + + if (!todo) + todo = TODO_update_ssa_only_virtuals; + + return size; +} /* Compute __builtin_object_size for CALL, which is a GIMPLE_CALL. Handles calls to functions declared with attribute alloc_size. @@ -1350,8 +1406,12 @@ call_object_size (struct object_size_info *osi, tree ptr, gcall *call) bool is_strdup = gimple_call_builtin_p (call, BUILT_IN_STRDUP); bool is_strndup = gimple_call_builtin_p (call, BUILT_IN_STRNDUP); + bool is_access_with_size + = gimple_call_internal_p (call, IFN_ACCESS_WITH_SIZE); if (is_strdup || is_strndup) bytes = strdup_object_size (call, object_size_type, is_strndup); + else if (is_access_with_size) + bytes = access_with_size_object_size (call, object_size_type); else bytes = alloc_object_size (call, object_size_type); From patchwork Thu May 30 12:26:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qing Zhao X-Patchwork-Id: 1941699 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Vqlsn6qz9z20Pc for ; Thu, 30 May 2024 22:28:45 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4241B3850200 for ; Thu, 30 May 2024 12:28:44 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by sourceware.org (Postfix) with ESMTPS id 0470D3864849 for ; Thu, 30 May 2024 12:27:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0470D3864849 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 0470D3864849 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=1717072042; cv=pass; b=RtmFOoDKaUCDgLF9Bp6tZAuPafomIvtpDpNZzdOrYMlMJ02QuOIhU2ZGE0fWkdLZkgg4CNfGtwI6tqHoNUICK6stDIfCFfKNgn8T61zwosgQv0RHbVQlVc87zPfYh5Onc5z6ZlbscCwDBssufe6/YEJ7n2Dot4yYLeJ55OmcZH8= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1717072042; c=relaxed/simple; bh=VYUdRmfEB4cFFMIRemDI8zFmG/JE6raxBSU4mCyIkg0=; h=DKIM-Signature:DKIM-Signature:From:To:Subject:Date:Message-Id: MIME-Version; b=iIhLcpsg8vbeXekgUQ3lrcobhENSsaWyNTw6Ky6Xx/TQUU8RcIW92x3HUvDeOerwbKGYhAPlc+sXGnIOK8NfBOsoOPNSMduNcc0oZqv2hbo/4U0/6Lrz/eTbb7DYMp6X4ufNbZi/DABAPbbTu5bszSLn/PckVAB/apSVbqKB3yo= ARC-Authentication-Results: i=2; server2.sourceware.org Received: from pps.filterd (m0333520.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 44U7mt8c019749; Thu, 30 May 2024 12:27:19 GMT DKIM-Signature: =?UTF-8?Q?v=3D1; _a=3Drsa-sha256; _c=3Drelaxed/relaxed; _d=3Doracle.com; _h?= =?utf-8?q?=3Dcc=3Acontent-transfer-encoding=3Acontent?= =?utf-8?q?-type=3Adate=3Afrom=3Ain-reply-to=3Amessage-id=3Amime-version=3Ar?= =?utf-8?q?eferences=3Asubject=3Ato=3B_s=3Dcorp-2023-11-20=3B_bh=3DoKYSrntSY?= =?utf-8?q?5aRPtPOaaLCJLyKDz2RVKzXgLdz/F6HqBM=3D=3B_b=3DWWtxP8YDyOHyTsduGTCR?= =?utf-8?q?WAdty/UjEt20p13pxfDpmqf5aNjDsfVj0QwT6xSCLJ7Xk0f7_ve1T4Y+nunXLCYFv?= =?utf-8?q?Om3ejXosSE7hQB3E5Lzof6UQi0ZZJGqWW2cLHSPKwvk/ThEX7H74_IH7QUA/CYKgk?= =?utf-8?q?zq5fr/VihLauoHmrS8G+B4AYEsI1942dlNzSj7A/irhfp1FM9NKiL4ra_jSq+sy6X?= =?utf-8?q?CMygA982RlHLCRYbHfEoQKCadELo5VX7uWnSLoaXnKmPZk5T+BGCtC5BxiZm_1gKa?= =?utf-8?q?hjDNE83qKb/VXCx49UEwJtclogU1p+BQldgYgMaZ8CLAxYj5JuxG7kbsC7+QJsCp_?= =?utf-8?q?1g=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 3yb8g9ryqe-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 30 May 2024 12:27:19 +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 44UC6j41026519; Thu, 30 May 2024 12:27:17 GMT Received: from nam10-dm6-obe.outbound.protection.outlook.com (mail-dm6nam10lp2101.outbound.protection.outlook.com [104.47.58.101]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 3yc508fwwb-3 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 30 May 2024 12:27:17 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=PkK4ETJG41+FZ7I2IlZRKqt6cw4bSJKFbOA2ovB8CXky8mwxuaDIpwsP79tNX0o/paoEj19i8ZEkLCBy/WcJnY8BZEhfqoc8kLoicJL3gk4i2nwPgim4sK97O6bdR2/0J8rv5X/xfPqEFcTicZa2weofuDwb/rTVh16JqINfQGJ9HuD8C2g45GmgX8F1/UlZgxIT+sqvXcYa5kbw1AxiIMwSA3ruPvKrcZSMVc3dxCvY0/QSAmMPrMTJ0E9ZpZNiN5h98e7/LsiGZ+NY7fUFOADb1GK+XVPohJ61oi+OgXzrTZRqqFMKmBZpvpJWdXwQvbV9B9Vb3IU7IaplTl5nfg== 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=oKYSrntSY5aRPtPOaaLCJLyKDz2RVKzXgLdz/F6HqBM=; b=Co7mIaai/3bU0RCJwa++dD0xhDMCH33ZI0ztgAJKXfk/wR+qd8th2IVbQasUHsS/URCxTmd2XwFAPZfl01UPauT6p2XEMjRs+suvnnu/5pTahp67SJrf6MhfWF19DUGU1wuU1xm0L3v5eSBhDgAVNXjBNRupP9KTylEArqhZvC6ZgSeSjZwUlxGrDO2FQAACzK+Kg2OAwVJMxh0qMyCmLvJEUJA2XrOWYFiSAl2Uh96K48z5K4F4emmQJXKXYK1PFsbz8RhhkEIb6fO491EhJg8oBrGCNPm5u7r6DAftVA0AoYC1WgdobWQhPheSjwtTk9Q1tIJbhfVAoMDp8Js2tA== 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=oKYSrntSY5aRPtPOaaLCJLyKDz2RVKzXgLdz/F6HqBM=; b=vZiWVQTCOSUoewmh+I5hasoOVm0rPsrK7C6XAMxugmUC+OofJKmRnj44Ex4QEyTKFkWGkCIV07f29PFzvEquo2zDUXs/QAg/IaEK6JpOpOIyjBPKePAhaCQ3VBtrIWvtgKXwgqdjV7QaK0Do303rDoP6fDCObQxeVpuIZDhUN3s= 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:13 +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:13 +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 4/5] Use the .ACCESS_WITH_SIZE in bound sanitizer. Date: Thu, 30 May 2024 12:26:59 +0000 Message-Id: <20240530122700.1516243-5-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> X-ClientProxiedBy: BLAPR03CA0087.namprd03.prod.outlook.com (2603:10b6:208:329::32) 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: 20aef10e-f8a4-44cd-6bf4-08dc80a3d5e9 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: yAmSn3+iK40n0Eseo+3VO60kX4sBS7y4pA07GoSrgjYg1W2pJx0uD60YCBtiFfIXHiT6p8fjKUrlURG51XiIhzql/3ziWbUQMkqr7CShwpsMgXmFugRKmiJWMJHyJa/yJCtPqRcRHSoUeD9FRzyPo8AZH38XNE6gG3zlJWDaGejoPlQzFFykcRgJGy2jAiT5FD4rGrBAOezyuVPZWegWWuHFSD7JVcGlcKyaxtkuqgR2zz2/ivzcGfBbVLWN2CMlVmlfFf+ToHIP91TuSFfd2HljTwc480EKf2oX8iKBJjv1rwCf4qaT86pKnUt3OOT7A2SFPOjdSCbTh0tQy8Yle6Wl6Hn5kaKJwriPwkgxD6tpxnLA7CbNPZM2l7blL/lJjL//558rVMyJMUsHJ8QecMU18bMdWKZSCqRaliuHc1qWf/w7U0x+goo82KmQRzNJ63LLzvONAEScphdwJSzWJhpdyaVLUI51hhw3ombG/HWWEIhg8GbH39dHg2a+M2ksyQbQx5Pwr4SH5XmGHTe9UnqQCdL2jWRzG5LS2RgYvvFQuhaqTeNDFhgBCc+RQ1R6eWoPd4I9RtycATWW3m23tCU4fOZ1UgO1M7joxP8pfi9U3DraV97uHcp1Za3ELgNuVFdq2TLtG25FDUgQqiMtddY2IgukIgtHGjQyrW6I4/KtIGcDHa5rSnOzHZvcr8j4qG05ySrvkZlzsoZ59nKBZr60S54UTO94TLbFDTCL0ki2Q/JjM5r8JaWPILyXEiBIyiN+bB6/bGdS1XcDWpfArqKqjqnKZCcEX3s1+eipGG5mGhrunz+HjgcO13xysZ73WgphhzaZNW0q+zBRSoFD6V6R5bpOyJMAhA+SMH8IhOo3RnCJiEBQFtDFWos9/UuQOueoR4a+qWFURk3ApwKgZe+UL+flbHAWAXpx995uGJx6OD7DAfij1WaoYaPInZ2aU1w5cxL/qHGvs2m804XdYP/UP51fCUdEmBia6o+5zFKRbT03jfwLcDRZa1pUinw/Od1TiMZN9r4z8PPDmEFXxwLruQLABOhwP6rkRjFnuCgRS1kjLcwoLeWxhfuNjt20/Ekpq91fzXnELJZaJ3SVs9RVWhPUoie/aLfdCttc1rZBw7/lDHWpPn5goNkca2R/rPGhY0CLZbK5twQhRTXP49cMwuxwiAT2FWwAJIfYONeKzefijFO/ulbLZ6LapB/JgTbjOvrSEUDQxqm3fyZU9B/+xTjsL4iOhYoSWXOceQs= 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: haargBWmpLjvPgm7whCin0No5gQWHlTwJawTBwA9rNchN1frA2Jwqv4lpWx/76VyS2k+gVwKYkD1JZxNHX0WFyzCuNGy8zEbHmn6IgtUicT5sIAN76yVPVjXn/sXUdLPfb6YkU49IgtJpewqeTmlY28IYZUG+4dClJZGzrZnSebfHLpAoSzUimMQvlYgrTqrgqlJwVRGt1GZKp/ES5O1rs2NXF/zP3QspQ4cxYuLplZaWiSWrE6syanJRaXLWRlTFfOmlramrHQLBEZZhAE8pMPSCoR63eMPito1uvwHwg6ThBoI73aPUVwHyTqNrzggx/B9tzfnpoLSplJIIWqw8FNtc/Y7STe/oGF01/IZdH8xXYrrO33AXuYURd260kyLUgjG2ilEXMCGj2DG+j3XIcQLvTZjoYf3Dgntr2fNLn56Wk5Rc9n9e/dkfdvV83VXYnnJUZMOP/rkA6pFxedHBHy2kIdzC2U/+lskyV5cZYnxDFtAO8KIX8n4xvdH+Hui4cyMeA18yPvywXZWo+V+WOZMpvhoZXWwZkOSsgDSgI/35+9It4tTcD8zfOLQ+YmISAxj09LUQdd95r65Lu26hpJrmOtMLgVk7x0qT0UI9fHillFPH3KW7B+Pp+P7xrOUYL6KhNPGEuDPvZO1f86JSqYyqRtMt2h8D/SMYOw9mxNsandzRzbOuyoTZ08we0dQLCwPJgbUBCxk3xylTbG6GktXzgw5EqpjuB0nN+6+8+kXmR4Iu0rqwaSd9RYvamk2fwQ4Ni280ua8WUQIIWCo8vEMCHyysHAC+63Lufe3x9qHnPR6A1sa+8Yh/lRNR0XgLAyb/FLkwVk7RklO6ohvDGp3zSVLmTRhLhWRKFaqB4m3VhIpDFETu+qBN6naMMQEXbrefqbfb5lElujFM/5gsR+VDNNQ3/zRjpTlkrCPo6Ajb3kTRDLou+LJDPqy+SjW3H9T6PefCAJ6qZuQTXelKcnupt6ISw+fRUMXY+DCTYv6UlFjv6rqJKEAF9nbujZQ0E9JV4h8r1EDGSVu7otlElKXwPzOnf8WtK4Mrh1K4sZhDOjis9owa1QpdwQJH7DIla6nHx5suaUIaYLRUXbiGhPYup/yXL60d2hrINQxY0wZ4dm1R+m4mnIShsfFtwSH4d1SrGo1O4YtuePVnG7HEz3klpsdfgs4uUKg/LacUIlbaleTHNMAicBDF6mmLUt+MJdJkdcUH7THqfkZbZC5SWn2lN7Il4bS0tDpodV4tm6nmqSNlPXfo9jAhvnfabetpQhRwZz1e/h4THVI/e1vcAFBkT3JbsDqKE5IGbSwV0Z9lIK45aUVL1Rnea3cMU+6QiZZmnXLBEn0youfeMTqSH11w6bbuLvThjMthBzPDRr3+0gmdWJhOINPB72rn1TVwGg7U1SeJJHec4dipoCZStjGq1bruov+WEo8sELoz3J0OISEboe5synWS5GB2J6Fe+9oT0I52Ont8ocXu3Kp8F1Yng2ObGhTyA22d/K5jcr1rjNqm8qlGzz9ez3FEoPOgOdgwbPdn6MDeAeyiW+f8YC2ETbK3gTJu9EvPr2GsCEUPL9jr7vKu40B1Sv6AKs8 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: uymSkGNNtROKudMCu7oh2wQv/ctvyzq7yW4UGgtvioudYzxGmkaCLryl8WN69yaLculxgCF8QWUFFh+dhus8uTTU3m6bjI/Hvjhsa4CVXlhYh6+OrEJ+GEASkDAlgi0SYU4Z05GGf8N95KOdfWI696LXnE01+qcUk2+CwMyi68C3yxJL6Wut44ciwUDUkv13wUL9NXnAV+wBjYp27luck67DSaYhWawBuFC5S3Ac8G14aIYocZcvpagFxyfKkbQ7qniPrBm0EpvlLL3N0ca1LQLRv34tJ1G82KrMDMUF9+FLiHTKOXMeWl/t66v9k2ZlNxpkjQMDxAk6iHCDvbu79X0HsrDZGahoJwzbPqiASn33FkvuJSaUFwpRddXWnoCxCpyiE3FMiq3LrdZ/1tFytRLr2vUXy4FKfBCALPxKtmJBXzgqiTrZD6E+JV0yseop4YfTJ9QoT0Zqg2VgDKpuSnBXXVBVojjDGHLNIowvNnf9LnKhWLjtDNvqdMTI0cjJIsbAZkfgM5zy6Dq4SgNRCv4d7/wb8HfuRjazgTwZ6dHIn4Y/UPavqz1dMBXPMNGD+xntMm5AA1ISG9eAxwCPRnX8t2xuJdkgdz5WgMEtjzI= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 20aef10e-f8a4-44cd-6bf4-08dc80a3d5e9 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:13.7389 (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: +xIYgCk7FPqsY5myU/9dY7o/Z8UCo3YbLNrRi5ArethDtV9RJFdyjDoH5PLHQDixgpsNF/Wgxs97tAUO5lbpuQ== 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=999 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-GUID: 6Bj203YKVMkeku1VplCuUEiGmdBFJlBV X-Proofpoint-ORIG-GUID: 6Bj203YKVMkeku1VplCuUEiGmdBFJlBV X-Spam-Status: No, score=-10.9 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 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org gcc/c-family/ChangeLog: * c-ubsan.cc (get_bound_from_access_with_size): New function. (ubsan_instrument_bounds): Handle call to .ACCESS_WITH_SIZE. gcc/testsuite/ChangeLog: * gcc.dg/ubsan/flex-array-counted-by-bounds-2.c: New test. * gcc.dg/ubsan/flex-array-counted-by-bounds-3.c: New test. * gcc.dg/ubsan/flex-array-counted-by-bounds-4.c: New test. * gcc.dg/ubsan/flex-array-counted-by-bounds.c: New test. --- gcc/c-family/c-ubsan.cc | 42 +++++++++++++++++ .../ubsan/flex-array-counted-by-bounds-2.c | 45 ++++++++++++++++++ .../ubsan/flex-array-counted-by-bounds-3.c | 34 ++++++++++++++ .../ubsan/flex-array-counted-by-bounds-4.c | 34 ++++++++++++++ .../ubsan/flex-array-counted-by-bounds.c | 46 +++++++++++++++++++ 5 files changed, 201 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/ubsan/flex-array-counted-by-bounds-2.c create mode 100644 gcc/testsuite/gcc.dg/ubsan/flex-array-counted-by-bounds-3.c create mode 100644 gcc/testsuite/gcc.dg/ubsan/flex-array-counted-by-bounds-4.c create mode 100644 gcc/testsuite/gcc.dg/ubsan/flex-array-counted-by-bounds.c diff --git a/gcc/c-family/c-ubsan.cc b/gcc/c-family/c-ubsan.cc index 940982819ddf..7cd3c6aa5b88 100644 --- a/gcc/c-family/c-ubsan.cc +++ b/gcc/c-family/c-ubsan.cc @@ -376,6 +376,40 @@ ubsan_instrument_return (location_t loc) return build_call_expr_loc (loc, t, 1, build_fold_addr_expr_loc (loc, data)); } +/* Get the tree that represented the number of counted_by, i.e, the maximum + number of the elements of the object that the call to .ACCESS_WITH_SIZE + points to, this number will be the bound of the corresponding array. */ +static tree +get_bound_from_access_with_size (tree call) +{ + if (!is_access_with_size_p (call)) + return NULL_TREE; + + tree ref_to_size = CALL_EXPR_ARG (call, 1); + unsigned int class_of_size = TREE_INT_CST_LOW (CALL_EXPR_ARG (call, 2)); + tree type = TREE_TYPE (CALL_EXPR_ARG (call, 3)); + tree size = fold_build2 (MEM_REF, type, unshare_expr (ref_to_size), + build_int_cst (ptr_type_node, 0)); + /* If size is negative value, treat it as zero. */ + if (!TYPE_UNSIGNED (type)) + { + tree cond = fold_build2 (LT_EXPR, boolean_type_node, + unshare_expr (size), build_zero_cst (type)); + size = fold_build3 (COND_EXPR, type, cond, + build_zero_cst (type), size); + } + + /* Only when class_of_size is 1, i.e, the number of the elements of + the object type, return the size. */ + if (class_of_size != 1) + return NULL_TREE; + else + size = fold_convert (sizetype, size); + + return size; +} + + /* Instrument array bounds for ARRAY_REFs. We create special builtin, that gets expanded in the sanopt pass, and make an array dimension of it. ARRAY is the array, *INDEX is an index to the array. @@ -401,6 +435,14 @@ ubsan_instrument_bounds (location_t loc, tree array, tree *index, && COMPLETE_TYPE_P (type) && integer_zerop (TYPE_SIZE (type))) bound = build_int_cst (TREE_TYPE (TYPE_MIN_VALUE (domain)), -1); + else if (INDIRECT_REF_P (array) + && is_access_with_size_p ((TREE_OPERAND (array, 0)))) + { + bound = get_bound_from_access_with_size ((TREE_OPERAND (array, 0))); + bound = fold_build2 (MINUS_EXPR, TREE_TYPE (bound), + bound, + build_int_cst (TREE_TYPE (bound), 1)); + } else return NULL_TREE; } diff --git a/gcc/testsuite/gcc.dg/ubsan/flex-array-counted-by-bounds-2.c b/gcc/testsuite/gcc.dg/ubsan/flex-array-counted-by-bounds-2.c new file mode 100644 index 000000000000..b503320628d2 --- /dev/null +++ b/gcc/testsuite/gcc.dg/ubsan/flex-array-counted-by-bounds-2.c @@ -0,0 +1,45 @@ +/* Test the attribute counted_by and its usage in + bounds sanitizer combined with VLA. */ +/* { dg-do run } */ +/* { dg-options "-fsanitize=bounds" } */ +/* { dg-output "index 11 out of bounds for type 'int \\\[\\\*\\\]\\\[\\\*\\\]'\[^\n\r]*(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*index 20 out of bounds for type 'int \\\[\\\*\\\]\\\[\\\*\\\]\\\[\\\*\\\]'\[^\n\r]*(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*index 11 out of bounds for type 'int \\\[\\\*\\\]\\\[\\\*\\\]'\[^\n\r]*(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*index 10 out of bounds for type 'int \\\[\\\*\\\]'\[^\n\r]*(\n|\r\n|\r)" } */ + + +#include + +void __attribute__((__noinline__)) setup_and_test_vla (int n, int m) +{ + struct foo { + int n; + int p[][n] __attribute__((counted_by(n))); + } *f; + + f = (struct foo *) malloc (sizeof(struct foo) + m*sizeof(int[n])); + f->n = m; + f->p[m][n-1]=1; + return; +} + +void __attribute__((__noinline__)) setup_and_test_vla_1 (int n1, int n2, int m) +{ + struct foo { + int n; + int p[][n2][n1] __attribute__((counted_by(n))); + } *f; + + f = (struct foo *) malloc (sizeof(struct foo) + m*sizeof(int[n2][n1])); + f->n = m; + f->p[m][n2][n1]=1; + return; +} + +int main(int argc, char *argv[]) +{ + setup_and_test_vla (10, 11); + setup_and_test_vla_1 (10, 11, 20); + return 0; +} + diff --git a/gcc/testsuite/gcc.dg/ubsan/flex-array-counted-by-bounds-3.c b/gcc/testsuite/gcc.dg/ubsan/flex-array-counted-by-bounds-3.c new file mode 100644 index 000000000000..9da25644af3e --- /dev/null +++ b/gcc/testsuite/gcc.dg/ubsan/flex-array-counted-by-bounds-3.c @@ -0,0 +1,34 @@ +/* Test the attribute counted_by and its usage in bounds + sanitizer. when counted_by field is negative value. */ +/* { dg-do run } */ +/* { dg-options "-fsanitize=bounds" } */ + +#include + +struct annotated { + int b; + int c[] __attribute__ ((counted_by (b))); +} *array_annotated; + +void __attribute__((__noinline__)) setup (int annotated_count) +{ + array_annotated + = (struct annotated *)malloc (sizeof (struct annotated)); + array_annotated->b = annotated_count; + + return; +} + +void __attribute__((__noinline__)) test (int annotated_index) +{ + array_annotated->c[annotated_index] = 2; +} + +int main(int argc, char *argv[]) +{ + setup (-3); + test (2); + return 0; +} + +/* { dg-output "24:21: runtime error: index 2 out of bounds for type" } */ diff --git a/gcc/testsuite/gcc.dg/ubsan/flex-array-counted-by-bounds-4.c b/gcc/testsuite/gcc.dg/ubsan/flex-array-counted-by-bounds-4.c new file mode 100644 index 000000000000..bd7e144274fc --- /dev/null +++ b/gcc/testsuite/gcc.dg/ubsan/flex-array-counted-by-bounds-4.c @@ -0,0 +1,34 @@ +/* Test the attribute counted_by and its usage in bounds + sanitizer. when counted_by field is zero value. */ +/* { dg-do run } */ +/* { dg-options "-fsanitize=bounds" } */ + +#include + +struct annotated { + int b; + int c[] __attribute__ ((counted_by (b))); +} *array_annotated; + +void __attribute__((__noinline__)) setup (int annotated_count) +{ + array_annotated + = (struct annotated *)malloc (sizeof (struct annotated)); + array_annotated->b = annotated_count; + + return; +} + +void __attribute__((__noinline__)) test (int annotated_index) +{ + array_annotated->c[annotated_index] = 2; +} + +int main(int argc, char *argv[]) +{ + setup (0); + test (1); + return 0; +} + +/* { dg-output "24:21: runtime error: index 1 out of bounds for type" } */ diff --git a/gcc/testsuite/gcc.dg/ubsan/flex-array-counted-by-bounds.c b/gcc/testsuite/gcc.dg/ubsan/flex-array-counted-by-bounds.c new file mode 100644 index 000000000000..e2b911dde626 --- /dev/null +++ b/gcc/testsuite/gcc.dg/ubsan/flex-array-counted-by-bounds.c @@ -0,0 +1,46 @@ +/* Test the attribute counted_by and its usage in + bounds sanitizer. */ +/* { dg-do run } */ +/* { dg-options "-fsanitize=bounds" } */ + +#include + +struct flex { + int b; + int c[]; +} *array_flex; + +struct annotated { + int b; + int c[] __attribute__ ((counted_by (b))); +} *array_annotated; + +void __attribute__((__noinline__)) setup (int normal_count, int annotated_count) +{ + array_flex + = (struct flex *)malloc (sizeof (struct flex) + + normal_count * sizeof (int)); + array_flex->b = normal_count; + + array_annotated + = (struct annotated *)malloc (sizeof (struct annotated) + + annotated_count * sizeof (int)); + array_annotated->b = annotated_count; + + return; +} + +void __attribute__((__noinline__)) test (int normal_index, int annotated_index) +{ + array_flex->c[normal_index] = 1; + array_annotated->c[annotated_index] = 2; +} + +int main(int argc, char *argv[]) +{ + setup (10, 10); + test (10, 10); + return 0; +} + +/* { dg-output "36:21: runtime error: index 10 out of bounds for type" } */ From patchwork Thu May 30 12:27:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qing Zhao X-Patchwork-Id: 1941700 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Vqlss2pGsz20Pc for ; Thu, 30 May 2024 22:28:49 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A5E30384F4BA for ; Thu, 30 May 2024 12:28:47 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by sourceware.org (Postfix) with ESMTPS id 918663858432 for ; Thu, 30 May 2024 12:27:20 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 918663858432 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 918663858432 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=1717072042; cv=pass; b=qbtc5yfoHDvGeszHnyWkIeFiwQzzYPzMKd71v6/NhD/oMRGiXNvHrgUOIJ+awVmKO3QylDcsE3kwGD16JbT0JPxs0RvideN9VGNrq/YLd5ymxXJsM+QOuEoy41KQIDPJrdK3QOHLKw1D6Wx9MQMSBzdd3g41WX9wnFpJBLeRDEE= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1717072042; c=relaxed/simple; bh=A9MKK9MDDxBPckL/irt7Gzpd3jhy8MvDhhyQP1uU3c0=; h=DKIM-Signature:DKIM-Signature:From:To:Subject:Date:Message-Id: MIME-Version; b=ivyRKD/FjjXI9pXgBCJCFINVmU/pOD98G7WnOdJ+JfjDZGwQucElhO2rUfSRQTwefA4+FTIISTrIoQ3Di3bDVMv75oCZMO3ZslOfCm7aZI8BMLcAVfVTZbPb7vye04YJypVS8yhx5oIi4+MX7uTLJuUq3lDBCpa71M8W7HPNUHA= ARC-Authentication-Results: i=2; server2.sourceware.org Received: from pps.filterd (m0333520.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 44U7mt8d019749; Thu, 30 May 2024 12:27:19 GMT DKIM-Signature: =?UTF-8?Q?v=3D1; _a=3Drsa-sha256; _c=3Drelaxed/relaxed; _d=3Doracle.com; _h?= =?utf-8?q?=3Dcc=3Acontent-transfer-encoding=3Acontent?= =?utf-8?q?-type=3Adate=3Afrom=3Ain-reply-to=3Amessage-id=3Amime-version=3Ar?= =?utf-8?q?eferences=3Asubject=3Ato=3B_s=3Dcorp-2023-11-20=3B_bh=3D+uxsV82Xg?= =?utf-8?q?akOeCOah8hKCUObQtKJE64dmJMNRrdC1ZM=3D=3B_b=3Dm1Ue3hYcjeRCdjAd2dda?= =?utf-8?q?C1IpUIxTnuDDCnBC15mAhHpVskq9BrArWi/9OwrNzMJVOr4c_Sh5zzXO7kuNJK1am?= =?utf-8?q?muluLBuBFM/IH+vGnrYc+j9+i9pObAngQVUNjahF2WGa/4YzD4fQ_7e537f61yWXo?= =?utf-8?q?L1Yy6uZ8PjGIu6ytekU+jSZYNweIfK6HzuPtRI3WgQyv8kROgE/RvC3P_+n429f+1?= =?utf-8?q?lgU4TVBsJ9LWflEHDbAwi7cX8NSm7fQNsxtXQV87f2hLZFNib1UCpsVLOZP9_o90h?= =?utf-8?q?x4dphln0IVRJD9/MQGdR0HWkVp/rKBSWXa6kFXl2sMIj7vH+jtO49WcVTbRod+vP_?= =?utf-8?q?tQ=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 3yb8g9ryqf-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 30 May 2024 12:27:19 +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 44UC6j43026519; Thu, 30 May 2024 12:27:18 GMT Received: from nam10-dm6-obe.outbound.protection.outlook.com (mail-dm6nam10lp2101.outbound.protection.outlook.com [104.47.58.101]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 3yc508fwwb-4 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 30 May 2024 12:27:18 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=bqkL82nw/p+cpT0xDv+Aef9brGXuFjKmjyWo0Uq4nEUW/XxQQG6Bs08AFiYctVx6Leaww7UYW8VFqNX4yT0bKZgse013D4bD4FfZL+AJGwxC85dtY0U78OJvPpCOZo11Q6GGM1aKgrEPmSlMIiJcAMvIvP8WEr/nunk3R9CjPvNNvMQNbEFnx4z0YYYCNuMrtcTYiraJ4cc4YaH8rwxwvdUy2AaI86pK7mXKQVW4U9pUVFOhvE2PX7Kt7rF0iK9CjiRw+ILz9jwnFQcNu6rh+3PRwGIpsgm+H4eSffdicmDILGjJC5J6K6KwmigruE8XkotTv6R4jDjoeMPl6BKmMw== 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=+uxsV82XgakOeCOah8hKCUObQtKJE64dmJMNRrdC1ZM=; b=HuFF/cW2QHoimGwso0dlv/Q0YxNxq35RzTBK2vT00/uv/bSejE4WP7unMkwLTFkrq4M/bMMeNL9CrCa0O2uHKDYQ+SfdkNDvWwaESsWeFPbb1QqVsBb6NT34+E9aFalnJ9WwjAVKzldpkHjZtMJuzys8rnmbOXPkESo0Y2t1u2szgUqgo/+9zIgNC+oq1uD+3Xf9iIuCy6tWT3v5Dock5U0XqAlpQx+P+FqdgaH7EpPGb1Dhn5XV7/zVIHXJDy7LLmAcbfeis7QG002uvvwkU3rCyun3AqzPBRHdnZsCg33zzTRMzLduO+IUI7x8xK/c5VHhdBygLbUdlWJ6O0jiIA== 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=+uxsV82XgakOeCOah8hKCUObQtKJE64dmJMNRrdC1ZM=; b=DGW5Wr1RVvT1XDJRZjxn8MJHQofXtZp7HZ5Y4/P5HNwvoU8qphO0mT/qSCxT/cZkW7n2m5Mmgg5mZZWFPu1Oq2XojeXJgC+FPEOmyUj4Gm6tpcAN0LJSnCmHqwCzpTip8V/V2wgIPPIGupaIi2FPfFyR9Dp6fk622wUv9cbEWQY= 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:16 +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:15 +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 5/5] Add the 6th argument to .ACCESS_WITH_SIZE Date: Thu, 30 May 2024 12:27:00 +0000 Message-Id: <20240530122700.1516243-6-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> X-ClientProxiedBy: MN2PR03CA0008.namprd03.prod.outlook.com (2603:10b6:208:23a::13) 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: 716dd579-fe2a-425c-c0de-08dc80a3d733 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: BJYAjeJ+tFZeaSDAhfmH5oYY3DTtusHLJ6gtPTZKCM9KucmHS/Pm7TgtU6bZ2RdeCDt93ZFl+XlXKgJSxnaEkN9rBVoULfzLKGshCLlouXq95I7h3PKdfbpn02epVrfa1HvwmiuvnUrW7TQVJGFoICel965tRhQjyZg8aKbuDRktRwkyglgNErBqBDy2R+S3JLS7Y6Fv+X93wDYYwNK70lcr/UsDrVhJj5xkxStY/EPHPRGEz21qxflRL6uLoJnC8N4wC0xBNxyitXSR2D6112nJ6bpbPM6mhwRPrKQXieZIEaqvKVZXCnD5iTCsyP27IIZ87Pw7T2BI0WptBsxpFUhlmeZONiSPzlQpByrqrD+IjnQxj7FG/RXvOMfosJCww2Qxjg7QVsy0wW+yS9YfmLsj0MBCXGhxsyVOpzAg1WTKAN+qWE0n3mLlLXtrO72O79ZddMB7YdFt4n89ZiohIaQjK6tndoO1Eya5jK0Oijzkf0Y9NW15R6AS9qI2eKpE/bQXElBqj2ql8Ay3dbxeUCV4DbHpW5S938telCZ2YepYcHzRRLUQWr6xLXdv8rYl1wb05Ev1XEe9dnSL59hhuNblEHWutKG5bBFps/PsBhUhsRRobrW+4PGIJYJVrzF/WkET1BsKK8EHw2xbz70o8KCHtWRk5YBNGalpJjj8Td3mngyIeLeqKp5Xkj0//jbPfTGDZEKgEKHK5Vl+DtBnrV7QDZgmVOlKBjJk+urYYm8bMHktyADWDsmJWj/88su+eURr7JzKv824sfZapY+NT3HauQH4yp8xuvT+hNeKapXbitKWLf+xh6zWTEH978r8Zy2mRjQQYZYrtuo89z4nSvBPqUq2uDgsbgLn6b24G0+cUTp5E/nh6hxcdeOC81XLW/bsxyzhQ5kHI6u3/8BbNDTBeOwGE0k12oPhQCDRAdQzgaxr39hGvqSNzDdLK5PcTehCU8QSdrF0aY+Lp6O3I8O6d8dPy1VGp3W/R2N6hKdcVWs7Sj2ZdQ4tEGecWR0IaDrr6PGkMUx38sUbIZON3MxXx3kiZ5Ob/jhvg9QZt+qSms2zTajxsmL+qA3l8meFQHrxLDs1KWHu3GdhQXIQ4n9rjzri8fA0fXySpbvGW02J1IEHUuH49A8TK70sq6W03PKnyQ0z1pdDk3yasI4aCpJoIOC9f75tmbVRzDahPN4FWEh89ScAjXhbSTj1ZqiU4cmWnzKTQOz7892VOH204Ozof6ZiCHScn/xRmognON0= 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: q5mtUpNJdmcLPE/dEKzmJMCjvSmE+jXQUc/MyFddje7PN6MHHVhnBGeE3bn9nD9NMnHhIefb5K8MgRMwrM+eattNwMU4Xm+FqH26St9kdqjhsFdV65SXGYTiPnk6JwCM5a/39kzhytdCssVNIg87GlTJD9tm1UiRbSbzvYcwwRZc0B636fWyQNKVwIlQ1f6KsPtLLHiLJQGAqE5zJrsIbsDC7NsGNUkxSf2gwsABlRHotQh9hMkU6NNY8JHNnZD4OJcMm11Wwpd0ntvfA/CqQZgHIJahAv6bi+wEr+OgDdpoxGQvGXO3UiQooNlef2WSL8fOLGd5BE+kdwB9cXZahhfMzF7HpuiL2V4wJ8i53uuVjkI1vzFKwmBcQaNec7ZSw/69RZStXEMdZo/IgM8QYQobmDTYbWFmfQC21f2JYVcAMYSVqBXWyGB7cb/jH4okCSw7Y/AA6KJUD+2leY4kEBz852Jh2QeAE+RzqxedvVc4dZNbVQ8s02Ph6pPkUJruYhZTgXKUBHG4RvAqygF9UFT5MLPTVzyP5mSw/GkA2Kwdpdm3eI7TJXtrBE2/AKrcQ8S5+X17nLgfG0NdxQePBJFnaXMqifpykbRW5L94CdeSLBwI0/vlEm7VMGCUBIC+TyKhsnCuW9CDbwMrNvviWgllNI60QwilWibhMAVoGdogyEX9i3viLLudQ7P6GCFxqzTQELNS1/pKn3dZIiDxnOok83jq4PFl8nuR3lrTKNWbuq1Jom6rN+niyxwPxtldDqVP1LbaA6EA0bKoWBv4HYS6PoqsTZc5j7uX3gG5bFFwfdMdfUKXG614/vzB8qXiI3udksF2aO09pNGedq9fQlme0CaUsH5ezkORC44s8iH03KSmJELZqC7B69GDMA+nr+ZwhJem8yD0ejTbXvF4zxCNxuB9b1ID/szRuj21EIs8SwPVtPIo+IwC7s0y7QJgAqdkOgCS9QHv/LueTu7xTLe4e6kWIevj4yR8uxwEntDoghZAGUZphG6HmQv6DfBVklG70m5bMw+bZAazODEEfbG1IvYpnTkCMNe3DfxfPUzQT23ZxAlEP0KA5GdIuYjnsnXYPsoPkzMqvnpgwL6QTrIbOF7yKh3fREbNxLRSjDwwzmP534wSb9do5nEYC8QX8tffQnbGLRAV/HVEwBEYjrybuoP/XT0dwmplFphrLzZ9Yt2O9Rdjmnc9gd+VmR3fbpUUylxkKaYYcPo/B6tfbeVIGkj7DyMLGL68fuGOCIC2I3nr8zHGrGR/MOnyj+XIaAwv0Z1MRChmihegcqNH16pQUlYaZ6Xtx0YhP5rZPPP/qE4LbqP37zQmkfbtGFkhSbHs6uLYJVK2psi3JMN2eFrHpBLPEaD4ULYNm/r8Y18epUuAQWRI2h/s80kyZ/1f7JC8GCSPoL6D4SFTko4qvVEQuva1gkxcDfkI3vfpApignD6V24OLWk3DYZCDySBW9YFtkwA2+sYo4CuRy5AoNVumJ7FkMkqcWSdYn7cCRG3Acik+2UeAJv9D5qrSn7so0E69yf3nk7OSOfKKVg16CL5TRx8OJVqXxyrvk9+Ounphzfy4qQe6BjkLJh4NbjE8 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: biFM4zRXi3NOyAzfUm0CeReZjSLArcFVA5eK2ACAIryGHux3VIxlgA3hA5duHLLHpkeCoViAfKlGDE5vaSQXNPxdUNSsV4yOLVBuN99/+X4iYJvAtks2S+fo+MMTiQnY3e6mPny3iO/ry8QJrQ2xhxX5+09KGeAPcy8zdFidC9inMcHnUxK/z9QsNjfeSlssrGzBdYSdy4frhcK74z0bufGEsgGOAcfxrfE1ahngFO+Xuozc76BqcfhYQNv94UdtdW6KQTBR+i7N1mpve40LzQG+k4+MSmiYKOKnz+tNXzB+4NhDQQJvPvuZRUsXVTusrZuJK5pFGFa8fV2dn8ATt831M726igXHcLhujWNddHrBIg+e4b8cD0jvhZ72ujG6jvDakO1dWs2a4Whr0Vs1upEXC0t5oduoSuoHvMNjhLKbTxgTF6FgsJub+rjndnHvMTqxEhds3L7YqxCEBwlS+EhfPQTka+VIurvguDYWzGpsFs9f6JMug5yBLDqoN9Pf0Cb1jPQKcJiJDiMs0xtcxzVXoFb3Dwoinv5mUPKcy566YeLo8mM0oVzYlR6m5DP8fgwvTu4JXnlg4aScfsP07Fu6T4vNJvVaiG+K4l20xYo= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 716dd579-fe2a-425c-c0de-08dc80a3d733 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:15.9326 (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: S5CECcnNAidlIwWijn+kzLh8a3TakLSf3kZ3B82J2N25Zv3aLxTQ33lZwGR8mwmuoGyW2W5sBw1qHlnaAbL4Zw== 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=999 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-GUID: xvm3gfEYqcptux3C5H4BsB-FwalTAx-U X-Proofpoint-ORIG-GUID: xvm3gfEYqcptux3C5H4BsB-FwalTAx-U X-Spam-Status: No, score=-10.9 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 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org to carry the TYPE of the flexible array. Such information is needed during tree-object-size.cc. We cannot use the result type or the type of the 1st argument of the routine .ACCESS_WITH_SIZE to decide the element type of the original array due to possible type casting in the source code. gcc/c/ChangeLog: * c-typeck.cc (build_access_with_size_for_counted_by): Add the 6th argument to .ACCESS_WITH_SIZE. gcc/ChangeLog: * tree-object-size.cc (access_with_size_object_size): Use the type of the 6th argument for the type of the element. gcc/testsuite/ChangeLog: * gcc.dg/flex-array-counted-by-6.c: New test. --- gcc/c/c-typeck.cc | 11 +++-- gcc/internal-fn.cc | 2 + .../gcc.dg/flex-array-counted-by-6.c | 46 +++++++++++++++++++ gcc/tree-object-size.cc | 16 ++++--- 4 files changed, 66 insertions(+), 9 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/flex-array-counted-by-6.c diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc index 0d9c7a34a0df..efd111305b5a 100644 --- a/gcc/c/c-typeck.cc +++ b/gcc/c/c-typeck.cc @@ -2646,7 +2646,8 @@ build_counted_by_ref (tree datum, tree subdatum, tree *counted_by_type) to: - (*.ACCESS_WITH_SIZE (REF, COUNTED_BY_REF, 1, (TYPE_OF_SIZE)0, -1)) + (*.ACCESS_WITH_SIZE (REF, COUNTED_BY_REF, 1, (TYPE_OF_SIZE)0, -1, + (TYPE_OF_ARRAY *)0)) NOTE: The return type of this function is the POINTER type pointing to the original flexible array type. @@ -2658,6 +2659,9 @@ build_counted_by_ref (tree datum, tree subdatum, tree *counted_by_type) The 4th argument of the call is a constant 0 with the TYPE of the object pointed by COUNTED_BY_REF. + The 6th argument of the call is a constant 0 with the pointer TYPE + to the original flexible array type. + */ static tree build_access_with_size_for_counted_by (location_t loc, tree ref, @@ -2670,12 +2674,13 @@ build_access_with_size_for_counted_by (location_t loc, tree ref, tree call = build_call_expr_internal_loc (loc, IFN_ACCESS_WITH_SIZE, - result_type, 5, + result_type, 6, array_to_pointer_conversion (loc, ref), counted_by_ref, build_int_cst (integer_type_node, 1), build_int_cst (counted_by_type, 0), - build_int_cst (integer_type_node, -1)); + build_int_cst (integer_type_node, -1), + build_int_cst (result_type, 0)); /* Wrap the call with an INDIRECT_REF with the flexible array type. */ call = build1 (INDIRECT_REF, TREE_TYPE (ref), call); SET_EXPR_LOCATION (call, loc); diff --git a/gcc/internal-fn.cc b/gcc/internal-fn.cc index eb2c4cd59048..0d27f17b2834 100644 --- a/gcc/internal-fn.cc +++ b/gcc/internal-fn.cc @@ -3456,6 +3456,8 @@ expand_DEFERRED_INIT (internal_fn, gcall *stmt) 1: read_only 2: write_only 3: read_write + 6th argument: A constant 0 with the pointer TYPE to the original flexible + array type. Both the return type and the type of the first argument of this function have been converted from the incomplete array type to diff --git a/gcc/testsuite/gcc.dg/flex-array-counted-by-6.c b/gcc/testsuite/gcc.dg/flex-array-counted-by-6.c new file mode 100644 index 000000000000..65fa01443d95 --- /dev/null +++ b/gcc/testsuite/gcc.dg/flex-array-counted-by-6.c @@ -0,0 +1,46 @@ +/* Test the attribute counted_by and its usage in + * __builtin_dynamic_object_size: when the type of the flexible array member + * is casting to another type. */ +/* { dg-do run } */ +/* { dg-options "-O2" } */ + +#include "builtin-object-size-common.h" + +typedef unsigned short u16; + +struct info { + u16 data_len; + char data[] __attribute__((counted_by(data_len))); +}; + +struct foo { + int a; + int b; +}; + +static __attribute__((__noinline__)) +struct info *setup () +{ + struct info *p; + size_t bytes = 3 * sizeof(struct foo); + + p = (struct info *)malloc (sizeof (struct info) + bytes); + p->data_len = bytes; + + return p; +} + +static void +__attribute__((__noinline__)) report (struct info *p) +{ + struct foo *bar = (struct foo *)p->data; + EXPECT(__builtin_dynamic_object_size((char *)(bar + 1), 1), 16); + EXPECT(__builtin_dynamic_object_size((char *)(bar + 2), 1), 8); +} + +int main(int argc, char *argv[]) +{ + struct info *p = setup(); + report(p); + return 0; +} diff --git a/gcc/tree-object-size.cc b/gcc/tree-object-size.cc index 8de264d1dee2..4c1fa9b555fa 100644 --- a/gcc/tree-object-size.cc +++ b/gcc/tree-object-size.cc @@ -762,9 +762,11 @@ addr_object_size (struct object_size_info *osi, const_tree ptr, 1: the number of the elements of the object type; 4th argument TYPE_OF_SIZE: A constant 0 with its TYPE being the same as the TYPE of the object referenced by REF_TO_SIZE + 6th argument: A constant 0 with the pointer TYPE to the original flexible + array type. - The size of the element can be retrived from the result type of the call, - which is the pointer to the array type. */ + The size of the element can be retrived from the TYPE of the 6th argument + of the call, which is the pointer to the array type. */ static tree access_with_size_object_size (const gcall *call, int object_size_type) { @@ -773,10 +775,12 @@ access_with_size_object_size (const gcall *call, int object_size_type) return size_unknown (object_size_type); gcc_assert (gimple_call_internal_p (call, IFN_ACCESS_WITH_SIZE)); - /* Result type is a pointer type to the original flexible array type. */ - tree result_type = gimple_call_return_type (call); - gcc_assert (POINTER_TYPE_P (result_type)); - tree element_size = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (result_type))); + /* The type of the 6th argument type is the pointer TYPE to the original + flexible array type. */ + tree pointer_to_array_type = TREE_TYPE (gimple_call_arg (call, 5)); + gcc_assert (POINTER_TYPE_P (pointer_to_array_type)); + tree element_type = TREE_TYPE (TREE_TYPE (pointer_to_array_type)); + tree element_size = TYPE_SIZE_UNIT (element_type); tree ref_to_size = gimple_call_arg (call, 1); unsigned int class_of_size = TREE_INT_CST_LOW (gimple_call_arg (call, 2)); tree type = TREE_TYPE (gimple_call_arg (call, 3));