From patchwork Wed Jan 24 00:29:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qing Zhao X-Patchwork-Id: 84640 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 20E72386181D for ; Wed, 24 Jan 2024 00:33:15 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by sourceware.org (Postfix) with ESMTPS id 2DFCA386182C for ; Wed, 24 Jan 2024 00:30:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2DFCA386182C 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 2DFCA386182C 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=1706056231; cv=pass; b=nxqv8uJSzeE3JC0qCcAqdtcJGFCLdhkEmkxOX0q2co0rtzJeoFLT3FWlS3d19FTMvcbQO8QqyT2i+b5Vi6bDEHSkQMmLni8V2vFCmtx1YF0Nfix27vh1NavZ/u4woqx/d1x0Z5SAF/K7WocC4uXpecxx7UZVyahqXA0yGG2m+jQ= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1706056231; c=relaxed/simple; bh=/6xKl1KBwKY6WYrnPV3i5SlauTJcG9gzS8hQtW/kPok=; h=DKIM-Signature:DKIM-Signature:From:To:Subject:Date:Message-Id: MIME-Version; b=I+B9/CNt07e4PFk+ud6RbbAHyrldvMNXQIR8s9FrW5+qNvlQkGtqAf5j7JK69KTLMeTo4rfBzUc7QRvP3Xofxhb1FlE8b3HZFxsrCrqSf0oxGNfskF97W8GlLzbNNFoM8mcKEgrkhICcUeeUsm9eVSH0eg4Q+wovJCoqOK19e+8= ARC-Authentication-Results: i=2; server2.sourceware.org Received: from pps.filterd (m0246617.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 40NMcgvf025439; Wed, 24 Jan 2024 00:30:25 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=kxReP94QyGknp+VMmJbR7KCPZyXGLc8N2AkInosVMFw=; b=bu2Zxm3XN+tVTB8AoDqlCjJoVmcXkDIEBtxAYozu50eAQkMqKQwpS5Vz4bhXpSVk+Nxv QXy6nQ6/UoAPSG8AJC5+KRr4msPvm+PMpBLF3rhzng/hr81asJrsxIxCM3EwCwcbRiNT uMQzqyFEGw9HFveTAUM2cJ14MIBfMFVgrJKIgWysrori2Po4h8/QrxiMF9tzOJDh+K/G B47Q4K6ADCCqrgxJpzYESlaLOJUTmhh8maLIp1/EkcUxyobUvWSSolC1qotY41gtR55h gPye17MrjzAGvlCobfh7DD9lqw/eVx2y14SaysSRgvHmMZ71pEu7A6+hr2fL2v7WhxHC 4A== Received: from phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta03.appoci.oracle.com [138.1.37.129]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3vr7abyu2w-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 24 Jan 2024 00:30:25 +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 40O0Aqwt014895; Wed, 24 Jan 2024 00:30:24 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 3vs33ttkfg-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 24 Jan 2024 00:30:24 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=naBJrjKU1kvFI7B49Nh/Oo4lOtM5WzaXj8kGeNlB0jKcoMjrpFF+2cPJvcNyxm56NajC3DksNKVR1YaEBSnCbVcNHlDqdFRBFoy00+ujFRedawi/fKO3xo1653Y0TxgamkDX2ZYjrY9UVXRjaP0tJZbE7xvdOacs1gkrgx/miXJIGpxX/zR8e72F9hMFEISRFLKC8Uzhrc0kGa9NXaMAY7zaTvizQOJ9eQh60/MK+1a/Xq+3n69yD0xqtAbUtsgPyyoysyu+L+zuQA7SVqb0OysoYBzs4QEzldPWkjGcqvbjaQ6ZAzgzxsvoerY/f6kZb46UMbR+dW9KBk0sz/socA== 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=kxReP94QyGknp+VMmJbR7KCPZyXGLc8N2AkInosVMFw=; b=RNR6ZenJH7/2PGDd3Ov0UpoGz0jA9eqZJVq4TXUsij0BpcjLAXLYwOmZtMU/i7aNCCRRLhJv9duKrwfzsGLQvI6XpU2YPbHzS3fk8sP4pUlkNUM3yw+5IyBtKQzBrAQKMpAuMJxyaXDsVnb98HzSW65El2GpVfxFzm7D9x8AkGs41gNWFTp33g23DdzvwmN+Fomy8MnIotyzozAx+3yHRHEOpQLQqdiRQMEHoqnlag2MPYGG3kT0XIho47nUsQbqlumCV35ELT0ay5RQX+ZqaA3+sLnaxfcvdc+IPlNkN0+vrM/x2deIFwGXg6HaMj7zkKMEMw0A3XagOyFftEG14Q== 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=kxReP94QyGknp+VMmJbR7KCPZyXGLc8N2AkInosVMFw=; b=xCgWlGJGUiVRVXbATi7Jykp9cSEY9Qpjdo5kBhXoyt/nVZogTGR0kuVI/rcda+c/x1xs3FzzYbzyfG3pwX0YIvVP4Jh5vfUrZWV9lJU0ReZfhcDjoDuog6X+pOgOfzukcf2phy411ZAsxRZMotBNfN4bztko3S3x6q4FZhaJeKw= Received: from CH3PR10MB7957.namprd10.prod.outlook.com (2603:10b6:610:1bf::19) by IA1PR10MB5994.namprd10.prod.outlook.com (2603:10b6:208:3ee::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.22; Wed, 24 Jan 2024 00:30:21 +0000 Received: from CH3PR10MB7957.namprd10.prod.outlook.com ([fe80::6234:e7a1:b517:c44]) by CH3PR10MB7957.namprd10.prod.outlook.com ([fe80::6234:e7a1:b517:c44%6]) with mapi id 15.20.7202.035; Wed, 24 Jan 2024 00:30:21 +0000 From: Qing Zhao To: joseph@codesourcery.com, richard.guenther@gmail.com, jakub@redhat.com, siddhesh@gotplt.org, uecker@tugraz.at Cc: keescook@chromium.org, isanbard@gmail.com, gcc-patches@gcc.gnu.org, Qing Zhao Subject: [PATCH v4 3/4] Use the .ACCESS_WITH_SIZE in builtin object size. Date: Wed, 24 Jan 2024 00:29:54 +0000 Message-Id: <20240124002955.3387096-4-qing.zhao@oracle.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20240124002955.3387096-1-qing.zhao@oracle.com> References: <20240124002955.3387096-1-qing.zhao@oracle.com> X-ClientProxiedBy: BL1PR13CA0205.namprd13.prod.outlook.com (2603:10b6:208:2be::30) To CH3PR10MB7957.namprd10.prod.outlook.com (2603:10b6:610:1bf::19) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH3PR10MB7957:EE_|IA1PR10MB5994:EE_ X-MS-Office365-Filtering-Correlation-Id: 3ad00228-cae8-4462-2699-08dc1c73a64f X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: wFrAeRlVJXM2RuBRBDmOB+rXZJoU7DLwuoRbO+Za25bcP1Qw7te66j7qP4RdoN3+Bf8EzUESsYjlgtUT7BDDcufI4TFubFy0xvFn/FyWJSl1O5+0YFW6dt4K84hxd8DANbzWTeSNs4oGU2x+QH64O+eE5pagASigsXVclVgEVVTaGN8540yLWbbLk98TukXwZdjAIvYhjUA3niZ2Dl6/El2pn/qrQKAJ/E7YaFNbBiwqNbr3MXQ/79IXDdAWia0UlAlGTksjxp41hRZZJXbFplnoPuOZx76AMIM2OFIc10/8tLVDDuUVMWR2g8RbbLKdj9PTdcFgEYlf5ZYg1ShE6IaZqp621voG68U+J7xfm4h49Oe58uQylLt7tCbg1LaQmPzqQlP/9vMvJemyYMKzEPFqVD+4Pq0gbb8wrWtbG24VhVuThyupVsLLNMnJ6hSoyBhWmhlzLrVYBj1otdYnfxxAg6EPo51K3C0Qmn4Z4X4gvdc+ZAqd++XNHVb8I9Yk21H7BnQcWaWOTQjek+ujnx+5bIO4lPFzbICEDbe2cKIBQ6/FvDBoNSJGw5KkYyjgho/G8bDtJJqLDasH3VC8GPdnwurxMVoD54hmafjdg101e6pWXkjug8/2vm4bjwCCjdwQBa9dWZWDpyenN9Ubvg== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:CH3PR10MB7957.namprd10.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230031)(376002)(136003)(346002)(39860400002)(366004)(396003)(230922051799003)(230173577357003)(230273577357003)(451199024)(186009)(64100799003)(1800799012)(84970400001)(6666004)(6486002)(966005)(478600001)(38100700002)(26005)(2616005)(83380400001)(6512007)(6506007)(316002)(44832011)(5660300002)(30864003)(2906002)(8936002)(8676002)(4326008)(36756003)(86362001)(66556008)(66476007)(66946007)(1076003)(107886003)(41300700001)(142923001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 87NgcMISz7t6324+3nLxIhDYuxx3CWLe7hhtpLsQndi9dE5GKISymXZfzdx8NgYhZT43JegOZ91Mn5QUMYYJE9q2y2q5AkOx5reX/tLsQmfMANSRrOBcmHoouyjbyw2Ds1df+ZpBmEzzHNleLVpuQs+3UmDumBnL6N8Hf5NbvJuKZIG7Be+/cewnm0w0TCk815CqhHTDmevaeP4hqf8SDIW9TeJFOOojzb8dm15xGMHoNNzf11dTcx6b5Q+unKqsbGBZ+u1Y2EyvIsgfgNiDmWyY28BgjZ4B5Pj/cUOk+2MwVa1W3dGjplYBsvqdXWDo3SKfPABWbmk4FuNzM7aN7+4KArfoItJY4ntjiLDCaltKeNJA4DEpI+u5VqFXOrKERj6tzi+DZuArB0sZaddNB0wvpdMcK8WJGkpjBTN6fQrZDMho3qp/K06nV52V0qBaSv0nwFeKHHJUHyA4sGkBZLvSSVLh0r1KJzy+pOFu1IxOuD+eo4ogAOE1OSIEtr18mlY8620TErMwbA7Xg1j+JaCPA7blvtMVRSYHP8fPI9NZtYNPBgi5haKdYgbn5jbq2mTJgxzvjjBf34VSLUE/uMnbsZgv8UE2hkSg2RlyNAGdv5g61F9wAXLz6rWFrHfRYgI8B3Z7gmRQjMDcNs5TdLhsDoGx5VEq2mm/msMH/Z3vo1BHD030RjbodLtS9/bQWao0ojuTTjCWofZFL+2qz2MXgoHPX6blGhWv45TX/+sOpcyVWZrO0fBu/4xjJyrjnh7AaCAw2QEv1RX14q/UktdExa4/PfWjtGB4h6KKIz5mVF8ENg5ba9Ca2xn4avvsFmDoXw35RncaSMulKybpuzMkzAaQWJsDGx6fCoRZhuFgTKian8S3bp9p4xqqv8Mb+4wds+h2L9Shg646tHOx1LsnoGrKpjdO88aCSzr9hmz9QjiLfyk1q71anU4IkxMJJXhSCn9u1Uqr6TKWuW1eigbtXpnVHM+TNQA6hX2wSFZ1YhUHOkjRwokvHUTgRiiyLHB3g3fgQAGA9YgDKfOKe1SxAChKtSLB12DwNYJXxobDjAWlAmoov9I6ZOxnE2ihPcufzMSitG5PBzoGdb1gKF3q0tjQXVfEBUse6DuGkuoUWBRvHceS5qljdxeK/MPUenCi2LEu6Czq35OSNT1VYee5BcCIVjQueTctoNhGyegrJkf2ehwtqSed2yYrikk6ifLpK8fNM/T9p4t3AAoBzrCOA3ZxlOBXxW0DD3vrn+IZKwo0vIyETBrMxqA8pUeZTvpIJQbSGBd9Nf1Az1kBAiun5zDYHQ9OMdvrcMJzIH7fxvN5wdJVvP5dyaTFX4FXqANf8WTHcQOXl0w2MyCZKf4YkIALejdw33QreFaCPHZfWq97xLqTkPUjf6gN7MC7lyplfdLwnexFAZF4Y8XjNvKxjiZ+e74016RvOSx0Qf+UuaAbHQqkFTpO5PhdtpWPOykZ1fNIkMcMc3pGakpwtM6SXMt2I+L7EiBf/TtPCA9O2rbjYDL7geEzE1VtlVEa9oWbCZhQEmp409UMmA0XzNLO9AZ1oBmPdEZMwgdGYR0pB7Y/sCyRTPQBDtZzorxi X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: SOzghq4q7P9hsoaJ83K2Xr0NxWXDpCH+1X+qZq8majo5lGOSwZBVkyy5ifFb5z7ctfQaR1KWSJGCsSBd6/uOZzzYDllJy+PPSSjaH+oeX+vnY2uMl/KVnBi2FIDlUH7yYNXBwRcJfIwQm8eb2yGA1RmXlOwYNsXKxQlbLBBHcdMWWQmmxHC7Pp8F3wqt2ZojNBf1swCPHPRQAfBsbDXwFDjWQU/yECuVOyZRmY3qqx0/aHdHC6SFn+4znO0VRMpQq16CGCuxueA+7lOAr7v5V1njnXUgmsxW9PWqT5Ee3W093qzR37MaEGF4t0j/9CYLNj/1emeIBsjIyRCFO9V5yWxkDNprMrG4mzOSn/4o2wI48ReU2h+MkBExM/y6tVItDNzdTvWHPq1gzB1AsYAChKSDYwV9wQz1rqtE2s74ULZL5qsr4Zx2wUTRGwr0mhCnTVOd0E+fGrokxc/eYrSjI5JHDOYGGBJmKo4YZqeOSMjGwUv+TzY91yJ+zI8C/F0xFhnsiS3pIhLecuq5exWHaBr5AS6eE6lURGPfiReTTTbT86J1g1ea/xf+E6+DE6YjFhRSIaBl8eF4F4ykQ1bKEllWR4Nsd05RyA0nmFzgRxw= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 3ad00228-cae8-4462-2699-08dc1c73a64f X-MS-Exchange-CrossTenant-AuthSource: CH3PR10MB7957.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Jan 2024 00:30:21.7784 (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: +/gYGQmUEHkVBnBaypVzbp/p6xQ/YPsS7ilQusHQOG2GxjsCuC/YgKxUH5DXX3utAH9CZ7OCvlo/0B9ixR7g/g== X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR10MB5994 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-01-23_14,2024-01-23_02,2023-05-22_02 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 phishscore=0 spamscore=0 malwarescore=0 mlxlogscore=999 suspectscore=0 bulkscore=0 adultscore=0 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2311290000 definitions=main-2401240001 X-Proofpoint-GUID: ifeYumqyJ0myrOFB09gwG51MzbFzUTkk X-Proofpoint-ORIG-GUID: ifeYumqyJ0myrOFB09gwG51MzbFzUTkk X-Spam-Status: No, score=-10.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+patchwork=sourceware.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/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/tree-object-size.cc | 47 +++++ 4 files changed, 299 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 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..0066c32ca808 --- /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..3ce7f3545549 --- /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/tree-object-size.cc b/gcc/tree-object-size.cc index 018fbc30cbb6..74d778f71eed 100644 --- a/gcc/tree-object-size.cc +++ b/gcc/tree-object-size.cc @@ -60,6 +60,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 +750,48 @@ 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 TYPE_OF_SIZE: The size referenced by the REF_TO_SIZE represents + 0: unknown; + 1: the number of the elements of the object type; + 2: the number of bytes; + 4th argument PREC_OF_SIZE: The precision of the object refed 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 type of element. */ +static tree +access_with_size_object_size (const gcall *call, int object_size_type) +{ + gcc_assert (gimple_call_internal_p (call, IFN_ACCESS_WITH_SIZE)); + tree result_type = gimple_call_return_type (call); + gcc_assert (POINTER_TYPE_P (result_type)); + tree element_size = TYPE_SIZE_UNIT (TREE_TYPE (result_type)); + tree ref_to_size = gimple_call_arg (call, 1); + unsigned int type_of_size = TREE_INT_CST_LOW (gimple_call_arg (call, 2)); + unsigned int prec_of_size = TREE_INT_CST_LOW (gimple_call_arg (call, 3)); + tree type = build_nonstandard_integer_type (prec_of_size, 1); + tree size = fold_build2 (MEM_REF, type, ref_to_size, + build_int_cst (ptr_type_node, 0)); + + if (type_of_size == 0) + return size_unknown (object_size_type); + else if (type_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 +1393,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);