From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by sourceware.org (Postfix) with ESMTPS id D6C67385840D for ; Fri, 29 Mar 2024 16:07:25 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D6C67385840D Authentication-Results: sourceware.org; dmarc=pass (p=none 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 D6C67385840D 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=1711728448; cv=pass; b=Hkw/OU4w9f23LPLWB7guPItpGX5WDfOUAiyc9z3/ovzoEZni19cbtYwOx0PL87dCQfhR1EnjseN8i8Ud2w1lP4HHny6DTKNELOQbgTv1ZzYeoQErUpK7Vvl0S55bU+W5cxTC4xgIRrXjv0NijmCS2/4hrGPrWU/BoDE8MZzpbJo= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1711728448; c=relaxed/simple; bh=k6laV2/S+lpsg60nksLhNFZR3HMSDSZace6OpIsLnys=; h=DKIM-Signature:DKIM-Signature:From:To:Subject:Date:Message-Id: MIME-Version; b=SL9TfX2jgb+zFcuJMMwx5RA0Qa9/JYSK412z0no6jcqIMDVHPSxd6mejkXy0mOumfPVzfDR58vr8oucKnnt6eyVWxaJsw6C6yax2tZ1xGYbkv7Mmc6MNH0B15cKc6mYDQL466gIwnH9mzBSy58Chs+blV4eFPK/fxNYTN4hxvLY= 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 42TFNrwd016578; Fri, 29 Mar 2024 16:07:23 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-transfer-encoding : content-type : mime-version; s=corp-2023-11-20; bh=G8sePRIFL0O9qIP7D5ALR/Ap+YtPr+HvCczMpVsAtug=; b=KlXtvfZR/XSIOrEk09MF06YQTyj+V2KsRfEmQL6/lo4QWWRm5bnZ5P45qMLRO6LVwRu9 6Mh2tzcInyAJ6qe9mt7tq13Vt0SZokGDOJuSeeEOtYtGX9yhWBM4j9tKDg+8snFM3gnh DCAiPCCGz/8Reuy11y3vEDeuLyIjHa/3DRB8GCpYKbYcF5kwR6yqBJj3qaqmm66QYwVv pdyMuK3NY6nRoYTlkz+xwkbJz/luk8zv7CfIQZSJrJFYHBnwC+JOU9K3dgXSBx4n2Vfl HZT499qF3YFzwdCswvAH+U2Ylni5BiCoXl3S91pqxabgN/P1vCdrMPk/DSbo+n+cw7Gs aw== Received: from phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta03.appoci.oracle.com [138.1.37.129]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3x1pybuu1j-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 29 Mar 2024 16:07:23 +0000 Received: from pps.filterd (phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (8.17.1.19/8.17.1.19) with ESMTP id 42TFelmE008879; Fri, 29 Mar 2024 16:07:22 GMT Received: from nam04-bn8-obe.outbound.protection.outlook.com (mail-bn8nam04lp2040.outbound.protection.outlook.com [104.47.74.40]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 3x1nhbw9g2-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 29 Mar 2024 16:07:22 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=NAiobqwhWOR6b1Wbyi7fqg0svH59tgNir16o/Hlhm7q5I9mY717KY7CTHs7KVHhuf9jQt1qH7+RGWY/yAxZFw7kNSJ54cu0MTkBWKl9V8/FLqw5ZagZ5pve7m+3NLGl0yJ13PBWnSjYXbdv5ZrO9pTAgVbDF8Ymbx2nA/3s4rpE8VEt3Hx+6JqG8ZKMRHde2ptA7y2rXcFKvr/WaaljDThclcNFIxTLT5qGqRRzfEi7p3Z1sdMBUxxkook25P9QE6xwTMOnLE9rJueclnLI73UrCBQh6g2c63GwfprDGn10GVWVkcnG1i7iub/Y/iqH1obnlCeVChoG7w//PkUOzhw== 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=DK1h6SzptlgvdQ8Y7Ew2Ml3PXi2OkeCh60qv97Sey1DpMjTpvNTWNwP9jCcQ54tgOnlSNy2cXW0NY8I62m4NGfnG0+K5AQ0Ix91AX9PlhsJa1pUrS9V6CBnqVdv2Qf9Q3jw6fScm/wbv9AZSvjZ68CvtBJwoCKRCrPK+9BSYmI6A2B/VW5HLylIt4knoqySGh+LC2ZTCw59sijwwx3y42FBjWWRIyLjU1Flsp02PR1+cih8Fr3ap4fozupUVm6hwn+aEbS/Fw78aSE1MsVYswLDxgYqtwuHoTOGLAPk0WhuhNKuUGKqwMAz+tJ4dQc+pWvs9StnXcpbuiojOhvwVmw== 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=hIOg/4FHnvWB46RMfzJV85cjZZiycEw7LcbkZjNk+4L4Xzlt7QnkU0v1fUD5iiAyJ1tgoMiN4oTcRXZRWljVxU6bi4n33yOjlcyDmDPazzZZJRPIzN3F1xhi9tRpmqE+MHkIrgK7+M006+teEgoPvqmiWljFxspodRo8LXI4Vwk= Received: from CY8PR10MB6538.namprd10.prod.outlook.com (2603:10b6:930:5a::17) by SJ0PR10MB4638.namprd10.prod.outlook.com (2603:10b6:a03:2d8::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7409.31; Fri, 29 Mar 2024 16:07:19 +0000 Received: from CY8PR10MB6538.namprd10.prod.outlook.com ([fe80::2dae:7852:9563:b4bc]) by CY8PR10MB6538.namprd10.prod.outlook.com ([fe80::2dae:7852:9563:b4bc%6]) with mapi id 15.20.7409.039; Fri, 29 Mar 2024 16:07:19 +0000 From: Qing Zhao To: josmyers@redhat.com, richard.guenther@gmail.com, siddhesh@gotplt.org, uecker@tugraz.at Cc: keescook@chromium.org, isanbard@gmail.com, gcc-patches@gcc.gnu.org, Qing Zhao Subject: [PATCH v8 3/5] Use the .ACCESS_WITH_SIZE in builtin object size. Date: Fri, 29 Mar 2024 16:07:01 +0000 Message-Id: <20240329160703.4012941-4-qing.zhao@oracle.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20240329160703.4012941-1-qing.zhao@oracle.com> References: <20240329160703.4012941-1-qing.zhao@oracle.com> Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: SJ0PR05CA0126.namprd05.prod.outlook.com (2603:10b6:a03:33d::11) To CY8PR10MB6538.namprd10.prod.outlook.com (2603:10b6:930:5a::17) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY8PR10MB6538:EE_|SJ0PR10MB4638:EE_ X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: f46HeRYCW0sBHcTadIRKdEf2GSAiDB2JhuQfMDyGcPG1iBbgWoc/in8+PyOKqsmE9OgrrpX03E1hFDVj1kyNCXnJ9Jf5hN/ljfn2WnfgTZOVdouwiq3T0UxeV9m2T5QuDDrh1juk1ufSWuLSbmYig0oXgl3kUF9/rfJxNBsdnQRKBFvf27pSErFG4DpZSxdZf8N0sEbIPUSPwTCNBeerBeJSc4LP0PxPH4Ui4N4TjW7/k30TgeeMFoCawskW2g2+An+0COGA3iH3g0lwXoslNbwzh1pthd1WnaOmsFKIsPRTgE+CQX4YK7sXcl/lS9hjZS1D7MTq3EQbFgseo9A8LOey45gNtuIlDY6ihl9Ao6OYpuY7sCrykkQnexyOG+w0R+jcGDr/ccGfXHFTHrIj2zofPoN0xceDv0NpapphZDeX35dtnjB5ii2D5twJ+bGUvbcKvBInO7cZW4vNt8fpvNukUMxRR+JSLUHX8DbsX10lu/GNvvU/KAA9THKnZW2hbN+5RD8siH/YsoC27mxCorMiEVU2aWWQuV/C2Zw1wtdBOHlfQoFhKiEmkT5WoUYCT99HT5UgFzWlW3lMiPyLiUPsCZJ3hvs8nliaN9Js3C+a3TCVak6xk3pT1udeJMHT 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)(1800799015)(366007);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?KxoLGIrQOrxcHMaQmgB7F2jGiw25SH9FyUSR6xq+1RCzubWcGeElM+wVzD+5?= =?us-ascii?Q?w4vu4tU5VsHfSsGRK2Q09A3ESKhe8aNVtRnI8e3n/iFdTlp6mVWl8BRrAmnz?= =?us-ascii?Q?33P5zOlnAcdeYDLVgCZCH7LqMt6BEw1Y27c21x0rEB5YYp+29ajGEO2pIoAx?= =?us-ascii?Q?P7Hdz3o0qZ7+TNxb6syBk0e7k7UEM0FxiSZf4UnxVOMEQ4BydNCfc8g7RC4f?= =?us-ascii?Q?kU0Lf7Uqn+kA19a+8Kr1w2QzHD6sbv/Xamq/osYzyOSrK9RlKsb95lSttdH/?= =?us-ascii?Q?oPeCV9BVCV2qukF2TVFWMWb/X4cLeRcMSInefa67xd3gBZHjL+Js3woYEWm/?= =?us-ascii?Q?IJWVUgsBOa45u9jHmZ7qyTd/jbSu0wHZnCv6mGOqGZn0JLtafDh54qu09+pw?= =?us-ascii?Q?XCqvmrE4ZBPWQXAhXModrsR/HJ9WdalQJtM60oPGKUERJ47kEj532HPGQJcQ?= =?us-ascii?Q?6jLGPkbGITNoybmSdZGTYj3/Rf2sIv4hve9ZNSMQjMZiULcj/AnvVxyA+wxW?= =?us-ascii?Q?bxyrD5CD0vnlJoTNVBOCnFvgvbxVPaHe0ggRpYvy0huvAK1qQAJzTKpWATGv?= =?us-ascii?Q?/GGB7nng1mo8mAdQh1C4ZZQGFyVbCU4kiOzs0j3rYPXo7EG7sP3mdFM9P3Ba?= =?us-ascii?Q?AmAIkzJ8k/2pW398RtKaImPdFvaj7uQhsyVwgENIta4QMAbt4i3NsMsAd96L?= =?us-ascii?Q?vU8mO0U8ECtKAtpkjFELUBmEx7WN3gZArX5Jo4CuwKwAo3SpFB0C8JJZvX9p?= =?us-ascii?Q?We0Bl+PSQ8riIrXluAeTQQdLVzZTbZm81/ZI7Q/IlOdWuRw/ufU0S5q8U8LX?= =?us-ascii?Q?7q9h6wke/A6RXlml9AekdWAZ63H6SIal0uUjvUYVKUxrfnt9s5vGauBeRuqk?= =?us-ascii?Q?goi+0BGRtuNB+qk+LeLSqTfbuhr8N5BHSNXIa5xjbgXxMxhRg83GkNyuch4B?= =?us-ascii?Q?GakKx3l0licrRubBc9vxtRBN2se/7NDG1UWhlALc5YSCrcwq6W1NSwtKNMdN?= =?us-ascii?Q?pgqSd5At+aGC499TlvCN6ygr/gLhGj8TQdEVfgMG6R1kPAUdhoVDgpuiHfbV?= =?us-ascii?Q?1MpGMGrwyCylRN7EHQt8xk3IozMsuXq0jYph1DSeJ1HGZOkK8BeWI7SQXd7U?= =?us-ascii?Q?X70Xb6T9kh1Kq0YF9blxO2lyOYoDCPz5BKU3kDmrwv734cQc8Ork1+un80F3?= =?us-ascii?Q?I9mcUh+35azJVraxzRdwToR72eYDT8BOoRg3lgB59lcGIC8DTCTJJNbezujK?= =?us-ascii?Q?moxeNePXbYFusSL+HwRDYYyuDmPfTgRuvn3qmRn60Y3K/GVFq+/pT1wow4hQ?= =?us-ascii?Q?cVZPUAabBRGjXVcLvEuGpANhFBiFBv6MPkSnazUDPbhB6YF4cVkuvXHM46fo?= =?us-ascii?Q?lT2zzdvknZGWGnVcojzbV7j353QFZkzVcZXwhwUdao4QbqMCltSWA+oI/sK7?= =?us-ascii?Q?b/hljXL/1EkCOh5Y5BkS4cgq6/iNelFrQf9krrI7+IenX84uNogFn9C9JO5T?= =?us-ascii?Q?IZoRw1zSl+Fj/7PlR5Ru1R4CWweKi4FNbVJA/kmPC6HVo5kTjuqqkeLaSIBY?= =?us-ascii?Q?HsKhqLH+z4NpDWe75cOoNuZ/dyavVrpG7S+QoTgS?= X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: eohbdFo/wZy9rFI67h8LWSR3TEW4gA3OmiUTZx1jaUftp6fGDMxa+sRjJSrUXuo28o2De8R9gguUueXG4fIkd80hT6JL0CG8ImnvAfJwTeMm9qqHHaKuKEbSsJBNRU25+wXl4XKks+dsYqSiIKG/aZsO/Vnq2DDyO52OIqmz9uKpxtAJpwdkbuNfNOIEry1z/8mfeDxAVKkFcWA4FNueFuHV6hprs0wKvOpYLiUs/LQpHAO2v7c7w4uQyAxwTi586NuyuahkUBiS65TD3b4gGO51N+9H8qgn5lk8xmiHUztf1F7z/yvftt3zenzvs/WAAI4jDDMYSghX6eHSIGhBrjYxp2gUyOMP4lf0rk4XUus1HupP3+8HbO8KI0bFjKJ6ZFVs1rBVcgaGRJHIUVZL6vnRNpgqehAG0Gy052DMkzfLDFF+O96J2uxlXbGXo0kOzxItrnaS5N6Ht1wqefZUk9OZXPUpa9OCwllLCZydk6EulUuWxUZkDK3AAVUHiY8DA7R4Cv4AtEfw2g88ZlUTk8Igxr+ryQAdvv3QhQQxvfZw+tTEGgof5GSkRdI7pTTXbbdMBv3SoL5jdR6HZukx82AuSadWNtG/uqHe2Dl2IMY= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: b43e0c25-a3f3-4d5a-43ec-08dc500a4fa8 X-MS-Exchange-CrossTenant-AuthSource: CY8PR10MB6538.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 29 Mar 2024 16:07:19.7745 (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: fH8grsr5Qpfe3xR8AUJa/OMFFWFffrUP6tdz+KL3gSv5iW9XY+mTqlz7511UNPw/iwa8sEvJkKEO0CD6KVL92Q== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ0PR10MB4638 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.1011,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2024-03-29_13,2024-03-28_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxlogscore=999 phishscore=0 bulkscore=0 spamscore=0 adultscore=0 mlxscore=0 suspectscore=0 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2403210000 definitions=main-2403290142 X-Proofpoint-GUID: oHIN6RSuy8Ctf8ROIkWLVE1Y-GxqHKJ4 X-Proofpoint-ORIG-GUID: oHIN6RSuy8Ctf8ROIkWLVE1Y-GxqHKJ4 X-Spam-Status: No, score=-11.2 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,KAM_SHORT,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: 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); -- 2.31.1