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 15BA13858D1E for ; Thu, 11 Apr 2024 13:20:00 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 15BA13858D1E 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 15BA13858D1E 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=1712841603; cv=pass; b=SPUoM1l6xP685nMegTAUIfNryuBq9wRrvDAWZEmMhMkSg/NdirfT9wiTmsuP6ireQilDG/3Tb5RYm5As61flM4vqRk4ZvsBP3SID1VOUTnDVxN9iOyA/9SywdFcd2Uiyj5NJnKvzGabSGQ50sS2Xnet7raEBer/TOyoZXqwAbl8= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1712841603; c=relaxed/simple; bh=SQNnzkfy7tlc9Q6ZCwjwp+cdL9NS3W2LYFTx7Nx9zGg=; h=DKIM-Signature:DKIM-Signature:From:To:Subject:Date:Message-ID: MIME-Version; b=shOH5t9NzxWcEsTl+YVBvvFYhzbOZe8TjSXfVb2C6H9mRwSVyCXLwaKbdFksSf99u+R0vJjXD/YwgYKUFNB4P9Xw8P8V47d9Z0HSAMfybU4sBTwnQA5moujh6P1Jqkxa4XKkrYf81tLGWbv/a4jaIJU/OIZ3jVQNsmdrHopqdog= ARC-Authentication-Results: i=2; server2.sourceware.org Received: from pps.filterd (m0246627.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 43BCPud2004063; Thu, 11 Apr 2024 13:19:58 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : references : in-reply-to : content-type : content-id : content-transfer-encoding : mime-version; s=corp-2023-11-20; bh=Xh7jV0TVuuQM3ksJluRLUN3lxZuc/LQbSZ8/+JdQAsM=; b=Ehgo6pB7asPGh2H4N2VMLJLwrVGjbk3L6AcQM0YRUZxPyvnbPXCfNpMCiSRnaOKDYP0+ RqXAVz9SkQsKdrNsf2/frmcxggJBnvCi8t45c79zfIHr0dJLgN+2BO8rU27P/d/ciJhw U6SkCRfWo6FNt1W1Ljt2C3Eg8w+2HxgwbPRSODofm1v1XW7uCmKpuwBazsM7usrXArxA nGZipd/IYrOAM4Z4gKYjA6NMc3HAFruE1n9wvmS80WC9dEIh9y3fZqkyjSSZeQdTxXDn 0x+9gUL5cDu7ZGqTy8Iv42eM93OtIv3zOaDXrY0rpAKa1JFe13q5JBMLPEeKV6vnVoKo 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 3xaw029mk7-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 11 Apr 2024 13:19:57 +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 43BBWaGD032401; Thu, 11 Apr 2024 13:19:57 GMT Received: from nam10-bn7-obe.outbound.protection.outlook.com (mail-bn7nam10lp2100.outbound.protection.outlook.com [104.47.70.100]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 3xavua408c-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 11 Apr 2024 13:19:57 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ExCrC9O2o5x/UozI989BnOlVg4Z+84BbfiJUKd2moCg3TP9f6x+73U/76AP2TCgAjTaLzdvUMFTGdQaHDziSae6QFt0ZRx4rbHfz+tQqRSnlYUpiaMLs5pVTiu1NUytY85NZDRHbnkXngrgZyx587KtLkZI7RPbSxE0CiJuTufwpSg+375Gq+2kf3uJ2QKqaC9ahLyKQu2QDv0NXQMpt+cjVcCIUyhpRfbCy+pLuEjyotI3lhgSrrWGjmVJJDJ00mbIDSucHUH6bzGEJ/TJ68ETWDsEium5EdfJLVol/9Vg71iO7xFvHptukJFizCqcCPA2NmhJLAYvtgZtHAzTdww== 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=Xh7jV0TVuuQM3ksJluRLUN3lxZuc/LQbSZ8/+JdQAsM=; b=Mxsptkf0QREoR+hkCoKxCprHHcGqjGcrvnJyjct3W283tFyMuJHsM/0IibFnlw8uOAKDnFl4ON8hQ7HqCBmzGFcyhoX5F5uoEzml00a7yU5xIxDjO3px+PKNloIjf4QOwAqoO8YROiMaj6NCXaDA9yzPMx1SMkw4nqh2t1XNyBUO6gW5I3xouM/Q4PgI6YH3RU89+Z5K6RblvN+ZRjBvTch/dyksP+GsNhd2d4+ysSk7/OnF/jqKRAahTApG8Tp+KEMCRdLC0QMWfTGR6xDmDI0psgQ1WHEKz/XYYqqSvScrMRxUNw8Gna3hgocOa7MCSgN6uzO3vYOgzlehKz+miw== 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=Xh7jV0TVuuQM3ksJluRLUN3lxZuc/LQbSZ8/+JdQAsM=; b=ETpd2B1VlY/d2nS34ToNIDdMWoazZdJAJwkgW1dZw3AcVDFPss3E0qUoPWkq5IIoI6Jz/7DXuAU+Ccq+bqDJLFv8Q2yJiaUpB5haCcF6jPBLZ+LdJW/zXwQfFuvLLcU685NlOJ1xL3uZ9P1F4sI+YVjkhrdImUZOa4IfDTuHv1U= Received: from CY8PR10MB6538.namprd10.prod.outlook.com (2603:10b6:930:5a::17) by DM4PR10MB7425.namprd10.prod.outlook.com (2603:10b6:8:180::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7409.46; Thu, 11 Apr 2024 13:19:52 +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.042; Thu, 11 Apr 2024 13:19:52 +0000 From: Qing Zhao To: Siddhesh Poyarekar , Richard Biener CC: Joseph Myers , Richard Biener , "uecker@tugraz.at" , Kees Cook , "isanbard@gmail.com" , "gcc-patches@gcc.gnu.org" Subject: Re: [PATCH v8 3/5] Use the .ACCESS_WITH_SIZE in builtin object size. Thread-Topic: [PATCH v8 3/5] Use the .ACCESS_WITH_SIZE in builtin object size. Thread-Index: AQHagfMtfIZ6PU+ej06detOGlfE4BbFiHNCAgAEFG4A= Date: Thu, 11 Apr 2024 13:19:52 +0000 Message-ID: <15A1FD7E-940F-40D2-B7C1-966DC130C87B@oracle.com> References: <20240329160703.4012941-1-qing.zhao@oracle.com> <20240329160703.4012941-4-qing.zhao@oracle.com> <857967f8-affb-4d43-b1f7-2078237a33b8@gotplt.org> In-Reply-To: <857967f8-affb-4d43-b1f7-2078237a33b8@gotplt.org> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-mailer: Apple Mail (2.3774.500.171.1.1) x-ms-publictraffictype: Email x-ms-traffictypediagnostic: CY8PR10MB6538:EE_|DM4PR10MB7425:EE_ x-ms-office365-filtering-correlation-id: 7e0e63f7-d386-42fd-dfe1-08dc5a2a1243 x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: ABOD14CIG2kRg9OCt1NYSmTDFq5A05cbFPTxRJ03i6lCDDP+qie8HmJU6ZF6jY/hgwjaQJEtGt4WLdo/CPZFbXW74tT2NZv94dGf7tU0L5H6SqiD5N7f5yhbn6RQ0gdZGW0sHVHXg/zdRMbQgfrBGtlLdaRR5Il2kg8SvzhxKb9T8gSKtIODYsCjageOsZy5dJl3w+aYUPpgWLY441jje0XvGBpqfSTwnu7HoxStL+Mx3qcFpKB2JzZga1INZusBJde4/GPd7HOlCXUd1MUui/HC99OkTpKmK712iJEgPpA7TXC2XKZp6r9iV5NmQgUGs/d5rBJgUUCR+zVGIJWv2+8l6927QzrLAYxzlZo2vIroXQkBvK7Q7pdFBzbkg3n1qDU8BMB/Z0yKajueYaI/1we2NtLA0bhzAYxtv0TqpP/VsadN2eufyxwjQqKUPqcv86COLRKo89Ri3ZSg/hR+r9bDcateX6D+jmMV9zWV8j88nCNJO4R9qnh8w3ydc9VRN0Xm7B6zf6pkXnTak/FADq7xczmCMWl/UIzmS4sf+ISxsuvNRtvtG3RIBOtRy8rceJLJ6eWVGjwtzMZPFELw+mLLVXmm4YS9sBmQP8pV5JhfC4Hf8hq6lih4kbRBn/Sk9H0qH72iBtw8ICBVlrZ7TFHI+Q8YAKHRcjPikBruwiwBhShSI6YfbOQ0ShfAEreb 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)(38070700009);DIR:OUT;SFP:1101; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?us-ascii?Q?x+OuQME/NpjJJRXRDitj8SNqUpfj+E+JnkhrIdFKEGnqsOQwa8XFbqMgOSvy?= =?us-ascii?Q?EtE65P2aBNGdwAidjiAt/7zWgwDdh89vXdgROAmbOj+F3RmGVauPLKaAXLYP?= =?us-ascii?Q?NN9AW0f3pWdX4S5zR5VVtIfZIdYe4PBKDaUQ7krq8k9H3J6q7v4XxRYYr02I?= =?us-ascii?Q?FA50TcLHrH3NVWf6sOyW6cT6OEoMs/7PHDcJhKhPozhBu1wVp4jS28u9NZb2?= =?us-ascii?Q?7PonY7LjnCMKL3OWCpfJK1BS8kFbNYp3P4JG1v7IrDL/34DW2BSny7qEb8Mj?= =?us-ascii?Q?3KP/b/979igkE0s1oRVytuNXTEWJgf1odon/ISC/lWcvsvC8WoMRhc6D9QCF?= =?us-ascii?Q?AHzX7KXR4p+hUk6IfuM/aloh60S4dPCEwpyRMxkit0R07QsZUIHNnMUTsZmC?= =?us-ascii?Q?I0gZwCwvlas6Ba0Y6Hbf7dneuninBOV6c3aK+fEB8f1mcKZATsNFFf/Ss9n0?= =?us-ascii?Q?qNBc9jUKja8tEaYX/zXdmzn5dOjE+1tqFu2bZfOHN3Axc6RhqT5Fwc4EV95q?= =?us-ascii?Q?i2rcJRUmIcyjAzSZJ316iBlmt5/fAGIsasDflquDhZqtnmLlJW/dpFx8iLLc?= =?us-ascii?Q?ELFngyHpl0AT3qKg/dOp5QXglxjoTohA2XUk3/08nZcmsVfmEA69gt1/lgc3?= =?us-ascii?Q?pUB9A/XMGyZVmsWI34EXhPUm8LEU/5vcddUgOd+auCAhmi7nV3Abd+sDJDze?= =?us-ascii?Q?InWukf1FsggB3kBYzxVIqYXdTF2tvmaR6dKEqKvVTlEWuwJAfJuIKeXjVmSO?= =?us-ascii?Q?shZH/7Dc/9HA+omMlyI5CQ9hlP4zlZKJFJI+y9QSeDHe4RfAuS/f3FSndmb+?= =?us-ascii?Q?M/bSlGNskjfrDPX3cUAq/v9ng+eqORARMaIxB2Z1zyGVAjK7EOFqZR8m1LoT?= =?us-ascii?Q?t1eq9p8ccVfXG5L1puKbPT3Mx7pKkwFZ4OXcpApFj1q93QuTEotOhmA0cjRj?= =?us-ascii?Q?Oxldrnnmeviyqb44NrBxiyt3wAXSe/0iY4YFD2516aT5yF0I+oyZlpnElDk8?= =?us-ascii?Q?5MdgwWo6eHrrBR72dBFaL5NhNcSZN6tG3B0TTcFWYjSPRYDXqldx1BfEEKWD?= =?us-ascii?Q?KlGWBtzl8F/sZ1PBmgTRHpIZpvX83CHEKkK/PpGyqzgCVR8+uX8Nl0v/gvhg?= =?us-ascii?Q?161rYkhUv4kxZZbJYINM4m7wr7ZMZ7pVQxO4LEeII7leLu+zwl2FPzmrbYdw?= =?us-ascii?Q?RSg0KsN5VZbGa4SMSFsn64XghT4hJHSDLaKMTrISHdbQt6KHnAGzibutLQMD?= =?us-ascii?Q?mWjuSPGPqgOyMdaAtGM2QfH4fZT05WWDojxBYUL+zyYEACZ11sP+yvxIcuyg?= =?us-ascii?Q?T5/y4bFKkuaEeXP6WXIQ7us30xwj+PI1bNBJo3iInzOcrGU44aHlaJhR9NiH?= =?us-ascii?Q?ujVsD9ek0Hdfr+pIYzSx56JH62/lliiB0bSkznfadcj9QWEIZKXITU3NrpIx?= =?us-ascii?Q?MwXy96pQniik4R5St9RzN2w+blpuABQGLdNKsHtiV4sq0A/JJn4TRHugCpkR?= =?us-ascii?Q?+GuSAj8pZa+irBJhnYsWm1uz8FcGZvN1+kT7YkTb6/Ev0sZjEy2S6eFBRLuP?= =?us-ascii?Q?00svzs4YpT+J5pocRM4F2kFDdMmAi4xwcdOxrnd3DyifKIo5a/qfhhlPFFK/?= =?us-ascii?Q?N9Dk7lwMK1RHvEqi7G3FvqQ=3D?= Content-Type: text/plain; charset="us-ascii" Content-ID: <985637506B948C42992EB3E78CACFFC8@namprd10.prod.outlook.com> Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: v+RGcn+/qkeH0FWXPyv4qn1DrItdJGM4hE31N/mchFmrmKX756fuEd5nd0I5n91XbmQPdNqEdOjIX1Lko3n24evmhj5iQ9yeVaktkY5crJyeCMxqfInYew/kXGS6wMLZLviKYc6+3PDOpqGboArzXvJD5AB62hj+nDK12rQc+29jCky4DgUEjHt4iv39bVAttSQSXagr/DVhEBDLtK5L+6hEkY7eyPJ43f3U74VTTnEzm21NKZkU850O5GhOJlN60qZeWfT5TIvi2X1enqtjEJN5cmOtqatsrX7ma4Uu1bEG9SFOld30NckJwx/0HfKh9TfAdDboeM4qSqKUeG5jgph/gK2G8nZ5l5PYEvSsofKFcnFTuuESXQeeekrRC5Y3nqfSJemo93sjcdi7zyxcs/hn46uURnuvv2rF2C1gVfR9fkXfKS46CYIYVN1hcCi4Ndj2jHiPBlcVsofD/PFWKTmhSbPeg3+HT41ibsobErL7deLZg9b1Yxp5DYxqYy0o2ZG+G2xvW1VhOH6VErCglT8g4iZJgLtjpzyhsqotk2vERS4xZVK2m5ljB2+fmRytDbD5kYt8SMxnZkC1BX4RdNDmtQupeWnqxhz9wI2JTCo= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: CY8PR10MB6538.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 7e0e63f7-d386-42fd-dfe1-08dc5a2a1243 X-MS-Exchange-CrossTenant-originalarrivaltime: 11 Apr 2024 13:19:52.0975 (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: ewwXqVtOlbWeM/rPztaTsRHWY/4lccir2kQGR45Eh3O1a/dYfzT1IL/NV+441wPO+gi+cgNDEkKGnuV82blwfg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM4PR10MB7425 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-04-11_06,2024-04-09_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 malwarescore=0 suspectscore=0 bulkscore=0 mlxlogscore=999 mlxscore=0 adultscore=0 phishscore=0 spamscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2404010000 definitions=main-2404110097 X-Proofpoint-ORIG-GUID: Neqtw88IQlmEDhrE8lCTaAFSHVVOlBwQ X-Proofpoint-GUID: Neqtw88IQlmEDhrE8lCTaAFSHVVOlBwQ X-Spam-Status: No, score=-11.1 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: Sid, Thanks a lot for your review! > On Apr 10, 2024, at 17:45, Siddhesh Poyarekar wrote= : >=20 > On 2024-03-29 12:07, Qing Zhao wrote: >> 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. >=20 > This version looks fine to me for stage 1, but I'm not a maintainer so yo= u'll need an ack from one to commit. Richard,=20 Could you please comment on this patch? Is this one okay for stage 1? Thanks Qing >=20 > Thanks, > Sid >=20 >> --- >> .../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/tes= tsuite/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 =3D 0; >> __builtin_abort (); \ >> return 0; \ >> } while (0) >> + >> +#define EXPECT(p, _v) do { \ >> + size_t v =3D _v; \ >> + if (p =3D=3D v) \ >> + __builtin_printf ("ok: %s =3D=3D %zd\n", #p, p); \ >> + else \ >> + { \ >> + __builtin_printf ("WAT: %s =3D=3D %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/testsu= ite/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;=09 >> + }; >> + int n; >> + }; >> + int c[] __attribute__ ((counted_by (b))); >> +} *array_nested_annotated; >> + >> +void __attribute__((__noinline__)) setup (int normal_count, int attr_co= unt) >> +{ >> + array_flex >> + =3D (struct flex *)malloc (sizeof (struct flex) >> + + normal_count * sizeof (int)); >> + array_flex->b =3D normal_count; >> + >> + array_annotated >> + =3D (struct annotated *)malloc (sizeof (struct annotated) >> + + attr_count * sizeof (int)); >> + array_annotated->b =3D attr_count; >> + >> + array_nested_annotated >> + =3D (struct nested_annotated *)malloc (sizeof (struct nested_annota= ted) >> + + attr_count * sizeof (int)); >> + array_nested_annotated->b =3D 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/testsu= ite/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=3D3" } */ >> + >> +#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 point= er >> + does not say anything about the object it points to, >> + So, __builtin_object_size can not directly use the type of the point= ee >> + 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 ro= utine) >> + B. observed accesses (read or write access to the location of th= e >> + 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 alloc= ation >> + functions to determine the object size; >> + (We treat counted_by attribute the same as the "alloc_size" attribut= e) >> + >> + For B, we use the SIZE info of the TYPE attached to the correspondin= g 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 valu= e >> + 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 >> + =3D MAX (sizeof (struct annotated), >> + (__builtin_offsetof (struct annotated, array[0]) >> + + (index + SIZE_BUMP) * sizeof (char))); >> + p =3D (struct annotated *) malloc (allocated_size); >> + >> + p->foo =3D 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 all= ocation. >> + So, the object size info can only been obtained from the call to ma= lloc. >> + For both MAXIMUM and MINIMUM: A =3D (index + SIZE_BUMP) * sizeof (c= har) */ >> + 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 valu= e >> + 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 >> + =3D MAX (sizeof (struct annotated), >> + (__builtin_offsetof (struct annotated, array[0]) >> + + (index) * sizeof (char))); >> + p =3D (struct annotated *) malloc (allocated_size); >> + >> + p->foo =3D 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 =3D alloc_buf_more (10); >> + q =3D alloc_buf_less (10); >> + >> + /* When checking the access p->array, we only have info on the counte= d-by >> + value. */ >> + EXPECT(__builtin_dynamic_object_size(p->array, 0), p->foo * sizeof(ch= ar)); >> + EXPECT(__builtin_dynamic_object_size(p->array, 1), p->foo * sizeof(ch= ar)); >> + EXPECT(__builtin_dynamic_object_size(p->array, 2), p->foo * sizeof(ch= ar)); >> + EXPECT(__builtin_dynamic_object_size(p->array, 3), p->foo * sizeof(ch= ar)); >> + /* When checking the pointer p, we have no observed allocation nor ob= served >> + 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 counte= d-by >> + value. */ >> + EXPECT(__builtin_dynamic_object_size(q->array, 0), q->foo * sizeof(ch= ar)); >> + EXPECT(__builtin_dynamic_object_size(q->array, 1), q->foo * sizeof(ch= ar)); >> + EXPECT(__builtin_dynamic_object_size(q->array, 2), q->foo * sizeof(ch= ar)); >> + EXPECT(__builtin_dynamic_object_size(q->array, 3), q->foo * sizeof(ch= ar)); >> + /* When checking the pointer p, we have no observed allocation nor ob= served >> + 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/testsu= ite/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;=09 >> + }; >> + int n; >> + }; >> + int c[] __attribute__ ((counted_by (b))); >> +} *array_nested_annotated; >> + >> +void __attribute__((__noinline__)) setup (int attr_count) >> +{ >> + array_annotated >> + =3D (struct annotated *)malloc (sizeof (struct annotated)); >> + array_annotated->b =3D attr_count; >> + >> + array_nested_annotated >> + =3D (struct nested_annotated *)malloc (sizeof (struct nested_annota= ted)); >> + array_nested_annotated->b =3D 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 =3D 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, con= st_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 r= epresents >> + 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) =3D=3D 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 =3D gimple_call_return_type (call); >> + gcc_assert (POINTER_TYPE_P (result_type)); >> + tree element_size =3D TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (result_ty= pe))); >> + tree ref_to_size =3D gimple_call_arg (call, 1); >> + unsigned int class_of_size =3D TREE_INT_CST_LOW (gimple_call_arg (cal= l, 2)); >> + tree type =3D TREE_TYPE (gimple_call_arg (call, 3)); >> + >> + tree size =3D 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 =3D fold_build2 (LT_EXPR, boolean_type_node, >> + unshare_expr (size), build_zero_cst (type)); >> + size =3D fold_build3 (COND_EXPR, integer_type_node, cond_expr, >> + build_zero_cst (type), size); >> + } >> + >> + if (class_of_size =3D=3D 1) >> + size =3D size_binop (MULT_EXPR, >> + fold_convert (sizetype, size), >> + fold_convert (sizetype, element_size)); >> + else >> + size =3D fold_convert (sizetype, size); >> + >> + if (!todo) >> + todo =3D 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, t= ree ptr, gcall *call) >> bool is_strdup =3D gimple_call_builtin_p (call, BUILT_IN_STRDUP); >> bool is_strndup =3D gimple_call_builtin_p (call, BUILT_IN_STRNDUP); >> + bool is_access_with_size >> + =3D gimple_call_internal_p (call, IFN_ACCESS_WITH_SIZE); >> if (is_strdup || is_strndup) >> bytes =3D strdup_object_size (call, object_size_type, is_strndup); >> + else if (is_access_with_size) >> + bytes =3D access_with_size_object_size (call, object_size_type); >> else >> bytes =3D alloc_object_size (call, object_size_type); >> =20