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 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:content-transfer-encoding:content-type:date:from:in-reply?= =?UTF-8?Q?-to:message-id:mime-version:references:subject:to;_s=3Dcorp-202?= =?UTF-8?Q?3-11-20;_bh=3DG8sePRIFL0O9qIP7D5ALR/Ap+YtPr+HvCczMpVsAtug=3D;_b?= =?UTF-8?Q?=3DWe51FCezPuMTFArYci8YCGK1SO3dZyb2JbCG5TIxphPxhYFumDbZKHdaBm0c?= =?UTF-8?Q?xX7rUb8H_MCtUX6n3mZxqSpmHpjW20vPZmwuWNTgsLp0msjZYIjurkjRL3lffVZ?= =?UTF-8?Q?yT+ZcjsZT1Z7n1_ZcwQZ9RBYNjDG7OpR0Qd61183RTDMcJ6mOG6FlD7LGahNERH?= =?UTF-8?Q?maO1k8PF8TAuJFglmllt_K0pXViTLLnqqn09wcHQAVHsM1lnNpMSR1V9WMZY0e4?= =?UTF-8?Q?YAfgq8SxgMHMuoR47rJz1KXge3_T5vbmmJCDMgFK09IBnuIvRgqhaA189r5Suzn?= =?UTF-8?Q?LiGjUm9iJIEZFYH9DaJQwBJAI8Kw2+ut_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> Content-Transfer-Encoding: 8bit Content-Type: text/plain 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: =?us-ascii?Q?uqagsPGtEPjD+JiH4DQYoqfhX1UUGyPj+HJhMxniyxZWuQ/F1AOR1w89iTUX?= =?us-ascii?Q?X0e7suam7BTPaGdL7C319Twpxpz1zccUTuC/asJ8FxEroI+AbSDjN8Y5TCBo?= =?us-ascii?Q?+pBW2WYHECT7OAUu3B7PhsojtIu65FsrLzCr04xAwYEwX+2T12Q2bKZbqzwh?= =?us-ascii?Q?DFWC+i84vfrT+pycvPbUQbL4/Zv2LZykk0D94wMsAZ7y1WiI8VAUhYleD60s?= =?us-ascii?Q?qq6YgYT6D4SkpAt4KNINvxLZIoyqyNuKIMIsLkmlthhZqSHRACz8vwfC1Euc?= =?us-ascii?Q?x1SYiXXsq/0fbuSAOzJnumtuegoOIJjdU/dI1q8zC8zks9YjBqdsrxGBqnap?= =?us-ascii?Q?JFIjrmo/iyID53w99L2N1KRtUv/JCa2uIDCM19zB5lbj9YOaTP0MZ3MRI84V?= =?us-ascii?Q?FwfjvsNe8pngpWfU4wOcl2MHCcHAHT+wgSM9YYbGO6uTdMJ+qnbLuBS3WR/r?= =?us-ascii?Q?1yUs6h8zDnt+xildppxp0wEOCgSqS/fFhbGhXldiP4G/n/QE2VHVliNntGjw?= =?us-ascii?Q?flu8iB8Y/uavqXvcICnkcIvUwwg18l62DMculbCtYTOCehB1kz8LUrlHiqlB?= =?us-ascii?Q?kwPHjHs+YwUS+YyyzgIh23NYxBcZL4RPdfxO8FuugZTExr0NaCIkJ6mbBLd4?= =?us-ascii?Q?wnYNsdvvM+SiJ9Mo7bpaf+361GH3Litqte+QN3rZQgQPF4i190cG3I5je3/6?= =?us-ascii?Q?5mbW4OiJL+P7KAK79kVpCFnkw7ID7JFQDQUgy5+7RBhFfqBAsjyUvyvlhrZB?= =?us-ascii?Q?yccKXNj5oSqX6D6bO3Z89Amq2sSv/shEwpTUFMhGLP+psnCLJywf5a2kqazQ?= =?us-ascii?Q?qZteJ1hDB2yRM7i6AFr2KPqdsUssop8KZiGLngz7gn/DeVb7Bhrp0VOn10It?= =?us-ascii?Q?N3XY+50GndI9O2ymlV7yYz6Z94yF0tDf3u/7GbGpEY+D7LXkQehDUuwB08md?= =?us-ascii?Q?tGP0zlUuveeV9SSsa4CM4thdg6IaA1QrM/nEuzUcHweDVyVaBhbt5GcHwrxf?= =?us-ascii?Q?dvUS/G3mnVuffu6IYFe2nP3czWbNVQqZuR/rQINi+iHfu5NXC+eBwqlnpPpj?= =?us-ascii?Q?3ygmvUY8OHzZSevla4Xu+ZfVOu6XG+8xKhjwBt7azeuYhUQB5dji1bZCMP3X?= =?us-ascii?Q?FOc+BI2BFMPYuMQcoZt/yR+5IMbDiZN0GXe9ahIbhxFhMPkH/unEy+1RyAiX?= =?us-ascii?Q?nPxztOgUYHu27vcsJlmvzoWEkMSyc6jLsMqZ5oLuArSAvbCHxOVOk0CYAv0?= =?us-ascii?Q?=3D?= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:CY8PR10MB6538.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230031)(376005)(366007)(1800799015);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?J4Z3Nw+CDfN9v4JgV7YYKz/wVWKUppiI5kzR2cU7Y+Kj7mDR/hnkm6c/WJWf?= =?us-ascii?Q?5bRBNgRGp14MHXKOK5a5dgpbm7ID1D45wOD3zVOwsOT863TvvZDbBH/xkM2/?= =?us-ascii?Q?4CeE23gsrpqesh3XodMK7+5+Dg10L5CoikKdXiP5DcGzuhqjL6h53ZMi64eO?= =?us-ascii?Q?QRglwy5O5KKwp3sS3I4jKM5bFXH91sxnsO+WLFjDBAf0ZNZqny2LKtTQzN0s?= =?us-ascii?Q?QpqTVUP5MxtxXmE4KOdHxNhAoxf4iuD5J8asSYQbm65xbBqXx2MoRw0LN+sO?= =?us-ascii?Q?3NMOo6dyLo9d0RUIEPdal6xLntVwZNkwY4CRDYI3XPV2ReOx8tlRyAbSsQ4g?= =?us-ascii?Q?JJ5DlLYl/lx0Vgdtzy9sNoj5K9sVvbWZQqUAMDJnsAXatowbylo8SxX9U4Wt?= =?us-ascii?Q?NM68jD3h6RZGiQrNHne/0l5jLXJmEsaFDhxyvCf2OBaAgVtbU1UuvyfwOp80?= =?us-ascii?Q?GucV2lOxXf9y5rqSDLuwHqwyI3LTj/vOY8PIHTRmF+ziW/iWSBUMeSgcppAo?= =?us-ascii?Q?BAGb2k/S+Jz1oto0U86nW3CxAkUqMFWEhcFuDO2LA5XOkuBnkE+9RdH1f+Zv?= =?us-ascii?Q?/R0cd3h5LcC0bbeoiErcoOE3sEWZpGx1/BvWhtY/duURoqc5KQNvpwq0+i7x?= =?us-ascii?Q?z1NcnVqQnocAEiTM2mt7T+BO0YL8iwuv3QUOb7pJqc8c8c9DbbfJ3riqHEGS?= =?us-ascii?Q?m0NjkzVuzRxgjnFXElRJkg2nSv4PVjQ0fjKbTELX+f0EVdx4IVwJYWKVlteb?= =?us-ascii?Q?7Aq8N/AOco1VqJjeZEJuZyAndhh1wKrH1yFPaCxfnFbNXgzQIkQDbCpPF/H0?= =?us-ascii?Q?pCjlm1/WPGKYi67O6TjbxbVkOGMou1R+HkyXLK7I69ETqVsl8jYhMz/KR7cw?= =?us-ascii?Q?yx5cfGqd67ZO3mZxFsqvoZBXSL6yvsIo4bCynPN/VHPWGe710QNWTpXDtjt1?= =?us-ascii?Q?2CGNc94kKFT3FvBQj2dNcef4E8mV0OxkEjS+stniKASixos6G53pE5n00lHE?= =?us-ascii?Q?kJtYSXr26B/9XiuflhUppE6FjcaG9Gp+l1bfIx55/NUqZYeRxqZzqq98DCcI?= =?us-ascii?Q?jR/6sd4TbMRZNgCynWC2YC9NE4b8VVaVzEg2aVMdfzGdtvtoShKiA70vkTiM?= =?us-ascii?Q?wKFp8duC7ys1wSUMojDV3B2fygy5DbM8DiZVdVEZtPW2VslqJ+xDsgE0cpqb?= =?us-ascii?Q?U0MvI0QcIurP+URiwT+xz7nXfDpFZoDdvAlkINrPgosY++iOD4X9wAO63F9H?= =?us-ascii?Q?aV/Gw13jS6AbxhHnfcbL6R6o9UQo8sqHewP1qEjYxmPxNEq5ojGCK3v5mBG/?= =?us-ascii?Q?89MlRiWfjpFxEOOrkymhqfA82edWh9zzMxqlNDfTjqLmAmb99nGSA8ppV3bi?= =?us-ascii?Q?CzC1B7LUfGSzmHYxq8roI5PdSz5z1LXNM/2Iqq2s06U++wQfXuBu/1M/VoX/?= =?us-ascii?Q?jS130VnoSJrS0XVjn63vM1067mIw+ccpYIOk9c5UptLhUVLivTuJKBn481iG?= =?us-ascii?Q?BMzExxKsA17diBw+HkFgVVpr1GGCWp+JXQt/nQ5Ztd/fjPZKaVnAII/8ew92?= =?us-ascii?Q?PPbPzfFyi/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 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