From patchwork Fri Apr 12 13:54:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qing Zhao X-Patchwork-Id: 1923156 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=oracle.com header.i=@oracle.com header.a=rsa-sha256 header.s=corp-2023-11-20 header.b=lHAOdr/b; dkim=pass (1024-bit key; unprotected) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.a=rsa-sha256 header.s=selector2-oracle-onmicrosoft-com header.b=AkbG+N36; dkim-atps=neutral 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 4VGJ54030Vz1yYB for ; Fri, 12 Apr 2024 23:56:24 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3A3543858417 for ; Fri, 12 Apr 2024 13:56:22 +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 F2700385B532 for ; Fri, 12 Apr 2024 13:54:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org F2700385B532 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 F2700385B532 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=1712930093; cv=pass; b=VKnL3TL//ad/1S+y73Q88SLn2GNAhe3j+1Pamd0vInuSW/msjeau5NCcJOiIhhSKkl61TERe6dziczi31M7VFUrMV/L6cdqX5g9Dr8gIuC9F5hyPYyM2fcYoMDtdyszM8nt48wY0gLUSzYATuQPEdT08+vKurYejyeGJmwt2GmY= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1712930093; c=relaxed/simple; bh=k6laV2/S+lpsg60nksLhNFZR3HMSDSZace6OpIsLnys=; h=DKIM-Signature:DKIM-Signature:From:To:Subject:Date:Message-Id: MIME-Version; b=Le1GYylqLJBD3fU2A0VzOx1G0Es04Gh2miYerkHAAjJV1epQdz96mw6QSiEE+aXnZyBc5XISZZU8ERdZaa46TolB2r79MXiHQrnuUCaBEkS0re8ilK+4ob4YdOux7aQvcoLDXrDbvqhySTzAhN+LutwEme1REnq47WCaP0hqQN8= 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 43CCKoal032551; Fri, 12 Apr 2024 13:54:50 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=lHAOdr/b2IyMBKWK+pjLT7eBx1QI0y61VPmHv8c4ey87dx/25N2ZWLQaOb2SD1i0RLUi R714g2Wq2wZbCeTm8i9q9ILL5KoM1DP+EURDtkHfxLdS7hALJmiavIsEY1n0izi88tNf uJgjwkVYzSuiisosajsF2LQ+NGsBC3M7oQ1SRp/28W+S73tPvmgTX6WWXNd2lwR39dCU 0thgTGgMpnB/o7Ed6B6AH/fJkb/9EjiK52UZF+r9S1oY9bjtd4RpmLk6x5Jxm1MJy0Zs YNcMW0qqReJIESyWO+qB6zn6YakFjTisN9JroOy0CE8PXI/E83ezDQPHWjOIQXlyf5dt /Q== Received: from phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta01.appoci.oracle.com [138.1.114.2]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3xaxedv0ns-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 12 Apr 2024 13:54:49 +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 43CDlSUn007848; Fri, 12 Apr 2024 13:54:48 GMT Received: from nam11-dm6-obe.outbound.protection.outlook.com (mail-dm6nam11lp2168.outbound.protection.outlook.com [104.47.57.168]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 3xavub17s5-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 12 Apr 2024 13:54:48 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Ivx1dh+sIMmpjBmCn7Y5vcZ8xD+R6iovE9EiG1QQH4Wi52W/SOBGRRJODzYXJVVvOHVcUu4O2FlAUmaQragelWDuXnFqOAG6Ul+9EBeQnhIvC/FDLu/DkZh5vor23Sp3imkBR6pAowW5OwUiTUC4l8PYiSNdtDFazPCVA/z3s58asHLm1EeYLCjvTBWNy+3cVhfhfq+/g7jJL0rBD9NJI669NZKbu8xISNt4nZ29MHhG7HvKU6aVVDl3W1YD8AH05vvVPyggj46iHSa35QhIBZEpIQ273sJshYvamnrjZe3EaQJN8AGNz51SZ3wEOQ9U1QqyVdr9raogs+iK+gUogA== 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=U2vuTYPgKOI4zJ4+5o1WeqNPw3L8UMYm1ynbIZu2SyUfOpiqT+2E8js5JIYwRqa7Q7pmSPT1AZ06aWNFdPRwm+IITKJXdbWPpOBn/nWr7YfW1ZhTMTkPxKCAAHvkqQAYLvprKwER3FkTybmj9I0r/lI5QVIzZ3mdUI61m88sw7efG1N5sEA0a/eLUlgDPo2r25srFjJhvq3kSnvN/V0ZOSfIIj0jngFiFcqHOyzld/CoKIlIfGBjbaA+X1EPwj0nZ3jcm4amdGkY/5KpGZNtutB4gxg2dpBwhVaBQx/zuLmvuyTb2DuGevB1k0oKDmdC8ZF0zSverM/Ncg5SSQPZQg== 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=AkbG+N36T5xH1mO7eNjmzfko/dZcrBKUrHpSc/26dbfXq6qHDt4rGQR1lKr4G6eIXfW8+IGUD0DN4cHJCDD4BsaCGHbjUBaj2zjtttk8uEUl5RTtUj4jt+WrNaIxi9obWg7ammVDg8U2Tjf8vfszd4/S/KykbuNRSxDDIwAWxv0= Received: from CY8PR10MB6538.namprd10.prod.outlook.com (2603:10b6:930:5a::17) by SA2PR10MB4796.namprd10.prod.outlook.com (2603:10b6:806:115::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7409.46; Fri, 12 Apr 2024 13:54:46 +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; Fri, 12 Apr 2024 13:54:46 +0000 From: Qing Zhao To: josmyers@redhat.com, richard.guenther@gmail.com, siddhesh@gotplt.org, uecker@tugraz.at, keescook@chromium.org, isanbard@gmail.com, gcc-patches@gcc.gnu.org Cc: Qing Zhao Subject: [PATCH v9 3/5] Use the .ACCESS_WITH_SIZE in builtin object size. Date: Fri, 12 Apr 2024 13:54:28 +0000 Message-Id: <20240412135430.4122328-4-qing.zhao@oracle.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20240412135430.4122328-1-qing.zhao@oracle.com> References: <20240412135430.4122328-1-qing.zhao@oracle.com> X-ClientProxiedBy: PH7PR13CA0013.namprd13.prod.outlook.com (2603:10b6:510:174::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_|SA2PR10MB4796:EE_ X-MS-Office365-Filtering-Correlation-Id: 36b344c8-80c9-4112-84e4-08dc5af81caf X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: EmCtfs5iUEqBdM5c86sjiSf4qPsJJvA0ZiYvbbwJhH/rPX1nt227NUszg9pJvDeaynkW5oR9SlES5vYEAR2DG8v9a4XbNXJesVRWTPP1WY6qK+6vJEfYLkkI/2vrxFWAKApwu6u0k6iL7KxKlvipX6W2CHU4xTWP8o/mILs+divySgzGQXjTUnpY1OtO6ykxmI1Zet3B4Od0/LTALjhbeMNGzpRWGpiYQB1TKoHBj9s5gfALhG71/clS24gwf4cnP/f9w4YHunHk7D1zbA+BuxAG1F4Ph8l08gVyCn2T6tsNZWOBNTpVwnzqEBZNCXmltATNbLc/htIGIqGjfnDJjKK4yinGV53GUD8J5ZYGjR/fxUDOTZKI646mzcl3o9xGCh6Xh4aRPr1PMiEJaDmI6viMOOaoU49XoVWjEvOouUDesNX/9CSQdX+QegHRUIvxsS4NEuurDRq0cQQVnLx9diYZGeBFNERqfPRHB5Wou1XHNto3UZGJ1jvfk5y549yHuCj+2zPAYey1db/Tk8mQ0HSYtNJx8e7lkjcgkmcwGzuHMM5BwD4aFmh/+2E35oRaihA29Or20KCwASjG5T0HFV6B9EW1ACRmOMF7aeB0EMq0SMWJwTDpUupOwpx+H5mg 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)(366007)(1800799015)(376005); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: TW5mNqm370oDYv7yXhw4mztIj7YxCi8iMjYiYw6ZDHscsqot8h+zCPUMiG5kn4APsRsqMCzbarSyle7oBzFRSCoLm/DHuXWCy54hEpHTydyasmhhPQv+R69ps6plzzeo4gw3q1gbrseEreht6wDU9th5gEwhCN+6WLk/Icl8xezHWMwvdRPaYumF7yzDoHjl6Jplb9DvCSsVdjiuY2K9ZTT23Igdj8lAQqkHwvBQNNjNdGfu6bvkK+uxNSfHOSdfHwgMMLjre/GvVBSitiweq/hP9MTsOAprEmXUnTlku/T7NOI7jC60M3grWTohmXQiyaXUFtqzkqi8qYVHs+1l7K6AEFB9+oE0Xox1C/PGEZ21+5C2C9el3nvch10lj0l4s0XK/hsbXI7nUsFo6QaQJriok/OPz7YPR7OFmlIm2e4Df81WKdHz8hnUQEfG1x4oRW+wlpzr1M+ROFuMotK0msGR/wE8AJ/NxXR7nWsKjx7pAkHx/ITIxgADes+wnE+M2FsA0hGml65c3pkJflfP0VzFZXRgjZweIjHIWQ0m4iDYBKzGkrL2oPtPat6fYe0PwGq7GQM94TZ3idEX8G37QQP/73q2lGH8UAO4Ewx0gkOPWRJq984d+7+HSgnj5907fd40j4FlxnD3nqW1aI0ipVgGWT2DCspRv+n5SFn4cjFMsd4gcIxueJyiAxMSlIniV32+xSANY7uq2w33jLEPmMjQOh1PQ9Zzd9chhp/dJ5ngJfmv7qAacBLdaZnRADeDiHI5xFKccGsDMtnviSaI1+Isu8dskx65nBmb3bwXVTu2W3Klk/Q5+TkJaEL198CXbyfHmUt9G2dc2A8eZLbWWn5bH5wwZfroa8FUV4LCTad1pFcd2iudih1kaQXXpte5I9jYpFl6sBlMOUvnXXrDKciQG4QBjuahgJv3oKqqYVfXe0DNKD6NkPOmzhSTiU8pJK1Vb1V4VT04WjoyJ+GJF8KM5irSxkaRVOhDapjYyVOQvogS8iDE4EAzk+FHak75Z1dHUnfdez82YCkbNRZyYBtOmRQversCRH8GhoLA+bHJo9/3EP56M/ksXxWOI0czoVEr8kFy3DO1HR7xykadYmC5V+eUUwLCddm9vt8MShLnze1e2X9raPR1PCUdiv0n12x1Cr5Bw99mYhuX1fzHSrHz33xqS2wXzcy5KQK+AxKKmbnd9+GygG+APiVIytbfHacN746pOaM8CeGqV34HxFvIcNhxKUgtMGjB2XZpvgO7cekcWrsM/3Oa71a0Y3rAM6hcej2cNAf4//4URYqSbX60MpYmgGu8OqMHa+TgDLaudNneDbGv3ToTV+K2DU40JJEiUzsW5jLk8ss/FbVcq0aJOCdw2B8H0VV/V8glkQrvUdymzIVvEhchwP+PY9UHcp+vphLetguguochSUd2EW/SI1Egt92xC67TEqh6RwUarQKES8rpRwjqsa0NzJVfGZo0ON7SlgqHAUwZuKiSETKyiAbUpt4tUH1xHEdsK2GHJXhL7ZrFbxoZ2mYC4NrlrxRZIak3/W+j9LeIaaLRN5wsl38HyHVZolSOmkTANL5joHkKkvPJQlAfh/X4re8T X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: CS8mcuHCctChPPesgpuuuy03KW+33nFQH+0bXsN7AEfi5vQ9k/Zwpvg5tj3LvWK1CGGGwBOS0zdKM5KjsOJLed1M799CreMo3UylvQWiLi9l6g053dCkgSTOnJMjj4J+F4kgpnoeNSDbfJLUExPYIgN4+mhnZ7yEY9qTN5hMzLZ+c78G+IA8VUDh85JZer0PfNpm6elfYkZeRQvTBAGgkWpamkQG1lwhwbfL8ppBuCvXDVIIG7TF0EWCuaeieFNNiP9JFCX5cZQubz1TM1nAVwGlg4wOz1p4od2WGZ8GxvkIZMRHwLG7x3OyzBFmt4ePUDhr4gu5fTCexViL3IVgZcx3k8vRvL9bO9YLf3OtfAXbJR4E+trABLgCAXt74NITscqk4xUC3I7krKhr0j2ctQ0oc0ZGv4Ih1PJ/dosQc0ezUHAJiDdjjFYo3uCqaBsYdw/W94wWLCeynEIfiQOfSB1TU/UQUJLyC1gvWKS7DYTj5KLQEL1d+a59N+aGzRwnQos3GU+078XtmJFzr0UatZNYNQwqPrPHhoEPkEni/eo0Sk72YTSfOgV8DO5R0V+ZSxOLz51bZWeYQoXYvC+cEwlpRus/ajnM1CFdlseJJIA= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 36b344c8-80c9-4112-84e4-08dc5af81caf X-MS-Exchange-CrossTenant-AuthSource: CY8PR10MB6538.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Apr 2024 13:54:46.1017 (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: HawarJcgbbCNk9T1osFw1BTO1fGDM410LIGt5GPrPxj7kzCcP3gp64ayuIAd2hlEPRTWA2aVLCstm8ghuPDxXg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA2PR10MB4796 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-12_10,2024-04-09_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxlogscore=999 suspectscore=0 mlxscore=0 adultscore=0 phishscore=0 bulkscore=0 spamscore=0 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2404010000 definitions=main-2404120100 X-Proofpoint-GUID: 2wu5nS3lSNDS-gMzq4XyrlTmw6jn8vg9 X-Proofpoint-ORIG-GUID: 2wu5nS3lSNDS-gMzq4XyrlTmw6jn8vg9 X-Spam-Status: No, score=-11.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_LOW, 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 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);