Message ID | 20240418205821.366065-1-cupertino.miranda@oracle.com |
---|---|
State | New |
Headers |
Return-Path: <gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org> 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 6F2CC384AB4A for <patchwork@sourceware.org>; Thu, 18 Apr 2024 20:59:05 +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 1D2613858D33 for <gcc-patches@gcc.gnu.org>; Thu, 18 Apr 2024 20:58:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1D2613858D33 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 1D2613858D33 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=1713473920; cv=pass; b=LrMLIyU4Yxru/HJqypEaasDhnXdhQl28VPC7w4SEAqrtrTh4cwKpDUklycdvozeFK8GMByq6i1JHNkVFrMKd+a7k5EEvnDAy6GFRWfmkfIdY+SdA3woT9Q+cVVC12BTfNcGyn25gbceGDc7+/xz/wuFnS/2LfIKXbiJN0I/0H1M= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1713473920; c=relaxed/simple; bh=zuHSJWfXyJ/WEzyC1M+9z1hgViEHvVnQPIjaq73id/M=; h=DKIM-Signature:DKIM-Signature:From:To:Subject:Date:Message-Id: MIME-Version; b=UjBBJorISLz7FdC1mqFSfByf2KlUKCMnVu4Q6TjhJdI9EniQMfGMu7K6QwDyuTn7ecaPu9Z95r/9Nkh6DIQQmL3nYUse8DJLmiejW59guuFbjIeOlZEvUhk6DkONDVIBuHxW8jgyCu+5Xpjllp53ietuyM+ctnf6ckRaoEmVhkY= 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 43IJiMP2004555 for <gcc-patches@gcc.gnu.org>; Thu, 18 Apr 2024 20:58:38 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : content-transfer-encoding : content-type : mime-version; s=corp-2023-11-20; bh=KdhKuKiilWoJZ2+tQ2CRUEBqjM0o8I904ltRvW5dwOU=; b=fyPQnhBY+UT6t0aWSXqoPFAIuTHe/akm0c163i88bPiooWzNk5hmxnyMQnM6Y7fbxK8o wNvNXiRYI60ac45SJuTfjPESm2/X73abaBTHQjAdhYYa6ocT3sWBG9CgS5Ub9ElC84M+ fUPW0P9ijFovzd+1+z1Y8H0TgwMSbNVltt6iodrIgqlPJPrZF+5+f024AX0tdt/HIRd5 kBU4AGampc7KdZ4YqSCpUmN64XLUDuw09iglWw+XovqvXv/zp9HI/6IArMuXHJU4WZiY ZDl3sc92FWGfzxPbg+gthk86MQQ0eg4ywkWZbYDlSUhsZ0Srcz9bL/x7NiL2hJcVgjym Qw== Received: from iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta02.appoci.oracle.com [147.154.18.20]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3xfjkvbhtb-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for <gcc-patches@gcc.gnu.org>; Thu, 18 Apr 2024 20:58:37 +0000 Received: from pps.filterd (iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (8.17.1.19/8.17.1.19) with ESMTP id 43IK1087022364 for <gcc-patches@gcc.gnu.org>; Thu, 18 Apr 2024 20:58:36 GMT Received: from nam11-bn8-obe.outbound.protection.outlook.com (mail-bn8nam11lp2168.outbound.protection.outlook.com [104.47.58.168]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 3xfgghajec-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for <gcc-patches@gcc.gnu.org>; Thu, 18 Apr 2024 20:58:36 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=NrtrmRK8NF1NeBFI0SkWx4GeFZ/fSYeSfJCaShxbvIguLF56YITX4jfc9csdc+IuCfaAd+G3/s1MlpkvAbvr+NRQe5e0+w+77jityS1NF+4xwqelww4r0C2mO+QnEpq7KcEwfQ228rGMiohMOcOmYcn6w4Zxz4bjTm8YNwtMbMB9s5l04V1gGqyM4WTHp7xyk14sE33l5pu5hPmAX2hjBXTZ6VLfBvARZPZ8m6b8KCU1mcdMW1VlOe8Mm4SUc8LBuca7hLe7TeRpRbXRaWkCzntWkxDTDcVP3eIjgeVMXjDwFMFsjgkKeEFVy8p3yD19EYen9W8GqqyymMBkrou/cQ== 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=KdhKuKiilWoJZ2+tQ2CRUEBqjM0o8I904ltRvW5dwOU=; b=Szm9/CxJZlJdbnPOW6NtXbKb9UI9uQX95GsaY6wMr4r7e6NWJDyQSdxHmOAstj3YuwWJmrJLjQET5WZwcV+tgKW7hMKFsEcrl9v/45T83/r9jVlz4/wHqcfbVEhi7NCITuw3hzV0EjdeXvXh/Nbyt9P/OGLYIRRerI2snW3adsFGCCQFet/SLJ2OWZhUExDvrUICa3wZXhusN0W9/WhM6RFPL135DciUcmdNsHA2YhlZolx5s3sUWLdmEU6DA2YHgsdyMFfwTBhSbrurGWE7bHrXPycZNoNGZjjQ2u89VevQnlUlGilU0XUHcK1F4jF9iepVk2DAkwJKEom7HpTrCg== 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=KdhKuKiilWoJZ2+tQ2CRUEBqjM0o8I904ltRvW5dwOU=; b=XpSC4MywzZaSMUx2NmU9dS3EKvCtZgFKnfWMIpzoGF3X3hEPLwX7zjffbncjZGcyNdflLHRqWQc9zQxEkydJYeKtC9GoaYPxGmVHBd74qIXLVF/8rOL/Vsl8RNEMsNtpAAFmFlvygnuQEjvPVhq1I7x+A2jdx9H4EeRuz1tff0g= Received: from MN2PR10MB4382.namprd10.prod.outlook.com (2603:10b6:208:1d7::13) by DS7PR10MB4846.namprd10.prod.outlook.com (2603:10b6:5:38c::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7472.41; Thu, 18 Apr 2024 20:58:34 +0000 Received: from MN2PR10MB4382.namprd10.prod.outlook.com ([fe80::1e11:7917:d2c:e44c]) by MN2PR10MB4382.namprd10.prod.outlook.com ([fe80::1e11:7917:d2c:e44c%4]) with mapi id 15.20.7472.037; Thu, 18 Apr 2024 20:58:34 +0000 From: Cupertino Miranda <cupertino.miranda@oracle.com> To: gcc-patches@gcc.gnu.org Cc: jose.marchesi@oracle.com, david.faust@oracle.com, elena.zannoni@oracle.com, Cupertino Miranda <cupertino.miranda@oracle.com> Subject: [PATCH v3] bpf: remove huge memory waste with string allocation. Date: Thu, 18 Apr 2024 21:58:21 +0100 Message-Id: <20240418205821.366065-1-cupertino.miranda@oracle.com> X-Mailer: git-send-email 2.30.2 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: LO2P265CA0471.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:a2::27) To MN2PR10MB4382.namprd10.prod.outlook.com (2603:10b6:208:1d7::13) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: MN2PR10MB4382:EE_|DS7PR10MB4846:EE_ X-MS-Office365-Filtering-Correlation-Id: 01e7a824-0fba-4999-0fe4-08dc5fea4f94 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: jxMosXCyCGfwsZOnPox7HiuVeyytgwrRT0GASv/BexUz48jlYbw8EDrZTXRCrlrApRegi4NxdNuV7LE/L7CqGcsLSNXX+MXSZsUCVGaMTob2yUoo6kRSkZTgPIKIWCw3Hb1h6ikVdQjiFeXpfrm0lyJrU7nI8Cvpxq/R+//p1NuxPPfK8EewG3PHhAC7QlYwx1yEjK+XM+796VgpbMTb7CFFUMfTp0BzO1rWc3LUxAVeoc3y9+nWDFtd/BhoOFu4ARfzBebOZR56OVNxr5fqAteC8y1tKETVUqzc/JgK5Ua89R0Alqd64tu9t0nM+t/3JQ0MECamzxK8YwSmGO5eaWjOv/Yyz9dPZNTWtA8cl2+s2u53XhaWtSEBV5zmsRYy9/LrVmzBwZFs0RiqIxWWVs+XWjvaqAmychZWzwsso27bmJlA15hVowdq6UyiEWiweyUQ+T4EZctWm3OE5n/xRAEqPyTuFMnw3ufUtmfX9LZ2tAB4e+zRcd8wi2twP7kgLluy6VTypFF+K+/mmx6iGBX62ddouD0fHKBP4VBynjegC12n/ZJmiUuBxDAK//N5RSy/1Qis+JqbLQA9CPZ4p5l67ykRiBFbDKisYX1AFcc01Lqi1g6qF6DkqH0nJTDQtap7CxjtRCNhsjFF+q2tMzLXCPUN4LkltoFwcHsR/l8= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:MN2PR10MB4382.namprd10.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230031)(1800799015)(366007)(376005); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 27W0L+mu77HFuWhnBMWNbNjrGY/fgKbA8cEdorQOZ9OuI6ES3diO94X5th2EKfKHcyQBkHVGfPtC8FiVUyI7OxyEyvU1vc1UQIKdo9eXkWZ1bUvW6sALwBJevRWARroia81QLbnbSplBXFo8Y8wDrmHnSioMAnzer3fjskmhU3BwIbZ4p/+tUKBUau8n2zGD1K+KUSRa89FpDZmhcuZCSSQjwAhqNxxKf8cva2Xm0JZqO632/Hm2AaW9+7P6sCaMie1kFbGHfAfEWdt7DlnvKlAnGfmr/H5wjJ6QcO2LinSLaF3T5ev722l5sl2hzQ+lXKzV0EelhlTO1ICpw0iZOdwI62ToPtNfrqExHl/dJtl0obgpNKWiG1GhDhalwruOGloFazBPfMt0hrS46V2QyYhrE4pCOSIyfL0iRwGopylwppSvfxGuVpStnf7mD9AiNJ/d2x2VXRBGHMglzBjSZOdZfP72pEYBJhwn0Z8pJK6hkrN76aUzNfFoDJfq0i4uOpfaZZPJfziBt+JjTKR6JA1E8kAFJ8xr+kwCaEyE1hqjeaZFPUGS2rKrl92bfwM2hVo/Yf5EG+DRjHNaQbAcnma69ITThhxRv/T7YrnWQPqcE2TnhfLlnxAFQBuJ25F9YpTkrgTMCGs4RRe4v4iwRZqPJ+9eP+KMOAjRzpPuRfSjUGH52fPYDlpwJ/68LQPcGevBNZsqKzI+R0KZaHRmH8+Jlf7Twmuhgjj4muD8WkMrp93NE5srporCvVgSfn/Xo6Qh1EnV9NtSyash/ZbWBJdJaOcwLa9M5b6Mys9rXri8qwQPJ0TPVL00LkwwZFTfsufe5c04uy78a1SMaulEuMWjPTAcE88n9kqFM9vPvjMco9aoPX8okZAzJYq8/6uNz29L94Qi6xkzA0Z4f35Op56mjn1pdn7NF4S9C7ioHOakvKcx9hkQSMRO1mWFiinXvn4MZYVPWg4FYU7YbS70J5EjzlS9lAlg2QboXU+khBI9BRQjQs8BS1hfAOMHad5SXokVsUvmqyYqmnDNEquy7IEpSC8dU8u7TeiW6PMA9ipUtrMpaJVRN1qzD/wBu1E+RpIe8JAnShcqFKkqjzSrkWXwqJ3cyqSFK5+0v/xDTShKBF/lhyy/XSTj/NimOgCJpb8CV8PFAXhl1BDmpjM7RHYWwKzP2T53JosLSwWjrwtHLC5O0JLYVAHznaAB7wvJauVrP8sQRFAi8nN6YMukIL0fllDLqq5AzBM6H0H2d6QfvyR6NOMtmvlvqSyqHwZMrHJ35De8HEdxry/HQ9qRjWBzp05u7qUN1J2rQcVV0b4JETjw6S9rAtJE/zIfmZoAV2IYw4RIsVW5araub+GEbw14fIOWMcYS9LeUplMREWrNTJyCpj7zbY6PM6IPW1rPVjVijcmRdlEmqWRt3zujtwCRa6w1XGHfR8pE653OXANYNaXKESetTVhomE85HKH5nKyHnXX0CUtL5RMjmc4ECN+W3/RDh8YrRmSzX0L3nCSQKmdvGOoKzwjk9z6oZxwAsUF9w3qRkyonHdwmsXLDNQ6VOPcqUamJxtP2ph3wVVjVwFgAITanAhBv/p0JGftOngdsgg9pGJ7ZaJPbJBhUBwb1AdE1JN6rWIzvJOkpvxw= X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: P3AxNGod6aghB+ZaxKwPskj16D0b9ch50BdXND5x2LzLw/LvXqCes+bjJMe3QJBUw1x71JE4gUJM1Ib5biGqWpsmHC5/E0LinvOjspbrS4idSD/7Hbysg9UiAwUfM1ceqtdt2pC1INv7jHSyfxEFBx5rgkkD/mOKt8ntU1gBUGM6nAkbWihZLoKbVq5u1bKM/pvTcs33Mplxt/y9J+GXi9IS2TdJL5A/EgXqJT8VSct+oh93JXcZ91sy2ppgX28VYJzcYPyEUP572ZCgDeAH5e57Y/pq5bENSwQFOezOW3jZm3e2FG74ZEj8NtVMt+FPnTwR2Iiqob6W3Wnm7z7KZhhHn8b5sjrdZaQ6eSbE5weaWdanlcKocrDCZBE2hect4ZL09F8vp8NgbTsRJx7MZ96QEmTg1FXQDQ1byWcincs5VSEl76g1DAZHy0xQV9tTmtsmHWl/ud5nnPXYvJ1b30RI00lDhvxZAaggml0FpQoqAYwv1EnxD4txu5ERAUs8xP76MxCnXighknv+Sy7DlLGo+ZPDESwLQ2RSaOBJtAZWXXdehwh8WcZCpJc4GWzh8TbTQ6+zDFWXWTlgaJnQz3CED3XMk7aaWAJYlTGPsdk= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 01e7a824-0fba-4999-0fe4-08dc5fea4f94 X-MS-Exchange-CrossTenant-AuthSource: MN2PR10MB4382.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 Apr 2024 20:58:34.4044 (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: mJXDW+bUhWJHQXt3PGB/uyfyAcZbt8p2z2JNBBfCy99ZXY+VDieENgVOTwFSCtQe2TOiIIZqZveeG6wIWilGWPmBAY/2k46p++Dlleyn54E= X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS7PR10MB4846 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-18_19,2024-04-17_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 mlxscore=0 mlxlogscore=999 suspectscore=0 adultscore=0 phishscore=0 bulkscore=0 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2404010000 definitions=main-2404180153 X-Proofpoint-ORIG-GUID: p2fsW5lrXeo5z46yplxCNgpTiwRpLJR6 X-Proofpoint-GUID: p2fsW5lrXeo5z46yplxCNgpTiwRpLJR6 X-Spam-Status: No, score=-12.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, 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 <gcc-patches.gcc.gnu.org> List-Unsubscribe: <https://gcc.gnu.org/mailman/options/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=unsubscribe> List-Archive: <https://gcc.gnu.org/pipermail/gcc-patches/> List-Post: <mailto:gcc-patches@gcc.gnu.org> List-Help: <mailto:gcc-patches-request@gcc.gnu.org?subject=help> List-Subscribe: <https://gcc.gnu.org/mailman/listinfo/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=subscribe> Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org |
Series |
[v3] bpf: remove huge memory waste with string allocation.
|
|
Checks
Context | Check | Description |
---|---|---|
linaro-tcwg-bot/tcwg_gcc_build--master-aarch64 | success | Testing passed |
linaro-tcwg-bot/tcwg_gcc_check--master-aarch64 | success | Testing passed |
linaro-tcwg-bot/tcwg_gcc_build--master-arm | success | Testing passed |
linaro-tcwg-bot/tcwg_gcc_check--master-arm | success | Testing passed |
Commit Message
Cupertino Miranda
April 18, 2024, 8:58 p.m. UTC
Hi David, everyone, Following Davids last review I decided to properly detect error cases, as suggested. The error however should be reported earlier in compilation in pack_enum_valud function, where all the errors are reported. Thanks for the quick and detailed reviews. Regards, Cupertino The BPF backend was allocating an unnecessarily large string when constructing CO-RE relocations for enum types. This patch further verifies if an enumerator is valid for CO-RE representability and returns an error in those cases. gcc/ChangeLog: * config/bpf/core-builtins.cc (get_index_for_enum_value): Create function. (pack_enum_value): Check for enumerator and error out. (process_enum_value): Correct string allocation. --- gcc/config/bpf/core-builtins.cc | 57 ++++++++++++++++++++++----------- 1 file changed, 38 insertions(+), 19 deletions(-)
Comments
Hi Cupertino, On 4/18/24 13:58, Cupertino Miranda wrote: > Hi David, everyone, > > Following Davids last review I decided to properly detect error cases, > as suggested. > The error however should be reported earlier in compilation in > pack_enum_valud function, where all the errors are reported. > > Thanks for the quick and detailed reviews. > > Regards, > Cupertino Thanks for taking the time on this. This version is nice, just one little comment: > > The BPF backend was allocating an unnecessarily large string when > constructing CO-RE relocations for enum types. > This patch further verifies if an enumerator is valid for CO-RE > representability and returns an error in those cases. The second sentence is a little awkward and seems to imply the error is returned when the enumerator is valid :) Perhaps "...verifies that an enumerator is valid for CO-RE, and returns an error if it is not" or similar would be more clear? Otherwise, OK. Thanks! > > gcc/ChangeLog: > * config/bpf/core-builtins.cc (get_index_for_enum_value): Create > function. > (pack_enum_value): Check for enumerator and error out. > (process_enum_value): Correct string allocation. > --- > gcc/config/bpf/core-builtins.cc | 57 ++++++++++++++++++++++----------- > 1 file changed, 38 insertions(+), 19 deletions(-) > > diff --git a/gcc/config/bpf/core-builtins.cc b/gcc/config/bpf/core-builtins.cc > index e03e986e2c1..829acea98f7 100644 > --- a/gcc/config/bpf/core-builtins.cc > +++ b/gcc/config/bpf/core-builtins.cc > @@ -795,6 +795,23 @@ process_field_expr (struct cr_builtins *data) > static GTY(()) hash_map<tree, tree> *bpf_enum_mappings; > tree enum_value_type = NULL_TREE; > > +static int > +get_index_for_enum_value (tree type, tree expr) > +{ > + gcc_assert (TREE_CODE (expr) == CONST_DECL > + && TREE_CODE (type) == ENUMERAL_TYPE); > + > + unsigned int index = 0; > + for (tree l = TYPE_VALUES (type); l; l = TREE_CHAIN (l)) > + { > + gcc_assert (index < (1 << 16)); > + if (TREE_VALUE (l) == expr) > + return index; > + index++; > + } > + return -1; > +} > + > /* Pack helper for the __builtin_preserve_enum_value. */ > > static struct cr_local > @@ -846,6 +863,16 @@ pack_enum_value_fail: > ret.reloc_data.default_value = integer_one_node; > } > > + if (ret.fail == false ) > + { > + int index = get_index_for_enum_value (type, tmp); > + if (index == -1 || index >= (1 << 16)) > + { > + bpf_error ("enum value in CO-RE builtin cannot be represented"); > + ret.fail = true; > + } > + } > + > ret.reloc_data.type = type; > ret.reloc_data.kind = kind; > return ret; > @@ -864,25 +891,17 @@ process_enum_value (struct cr_builtins *data) > > struct cr_final ret = { NULL, type, data->kind }; > > - if (TREE_CODE (expr) == CONST_DECL > - && TREE_CODE (type) == ENUMERAL_TYPE) > - { > - unsigned int index = 0; > - for (tree l = TYPE_VALUES (type); l; l = TREE_CHAIN (l)) > - { > - if (TREE_VALUE (l) == expr) > - { > - char *tmp = (char *) ggc_alloc_atomic ((index / 10) + 1); > - sprintf (tmp, "%d", index); > - ret.str = (const char *) tmp; > - > - break; > - } > - index++; > - } > - } > - else > - gcc_unreachable (); > + gcc_assert (TREE_CODE (expr) == CONST_DECL > + && TREE_CODE (type) == ENUMERAL_TYPE); > + > + int index = get_index_for_enum_value (type, expr); > + gcc_assert (index != -1 && index < (1 << 16)); > + > + /* Index can only be a value up to 2^16. Should always fit > + in 6 chars. */ > + char tmp[6]; > + sprintf (tmp, "%u", index); > + ret.str = CONST_CAST (char *, ggc_strdup(tmp)); > > return ret; > }
David Faust writes: > Hi Cupertino, > > On 4/18/24 13:58, Cupertino Miranda wrote: >> Hi David, everyone, >> >> Following Davids last review I decided to properly detect error cases, >> as suggested. >> The error however should be reported earlier in compilation in >> pack_enum_valud function, where all the errors are reported. >> >> Thanks for the quick and detailed reviews. >> >> Regards, >> Cupertino > > Thanks for taking the time on this. > This version is nice, just one little comment: > >> >> The BPF backend was allocating an unnecessarily large string when >> constructing CO-RE relocations for enum types. >> This patch further verifies if an enumerator is valid for CO-RE >> representability and returns an error in those cases. > > The second sentence is a little awkward and seems to imply the error is > returned when the enumerator is valid :) > Perhaps "...verifies that an enumerator is valid for CO-RE, and returns > an error if it is not" or similar would be more clear? Thanks for all the suggestions. > > Otherwise, OK. > Thanks! Pushed! > > >> >> gcc/ChangeLog: >> * config/bpf/core-builtins.cc (get_index_for_enum_value): Create >> function. >> (pack_enum_value): Check for enumerator and error out. >> (process_enum_value): Correct string allocation. >> --- >> gcc/config/bpf/core-builtins.cc | 57 ++++++++++++++++++++++----------- >> 1 file changed, 38 insertions(+), 19 deletions(-) >> >> diff --git a/gcc/config/bpf/core-builtins.cc b/gcc/config/bpf/core-builtins.cc >> index e03e986e2c1..829acea98f7 100644 >> --- a/gcc/config/bpf/core-builtins.cc >> +++ b/gcc/config/bpf/core-builtins.cc >> @@ -795,6 +795,23 @@ process_field_expr (struct cr_builtins *data) >> static GTY(()) hash_map<tree, tree> *bpf_enum_mappings; >> tree enum_value_type = NULL_TREE; >> >> +static int >> +get_index_for_enum_value (tree type, tree expr) >> +{ >> + gcc_assert (TREE_CODE (expr) == CONST_DECL >> + && TREE_CODE (type) == ENUMERAL_TYPE); >> + >> + unsigned int index = 0; >> + for (tree l = TYPE_VALUES (type); l; l = TREE_CHAIN (l)) >> + { >> + gcc_assert (index < (1 << 16)); >> + if (TREE_VALUE (l) == expr) >> + return index; >> + index++; >> + } >> + return -1; >> +} >> + >> /* Pack helper for the __builtin_preserve_enum_value. */ >> >> static struct cr_local >> @@ -846,6 +863,16 @@ pack_enum_value_fail: >> ret.reloc_data.default_value = integer_one_node; >> } >> >> + if (ret.fail == false ) >> + { >> + int index = get_index_for_enum_value (type, tmp); >> + if (index == -1 || index >= (1 << 16)) >> + { >> + bpf_error ("enum value in CO-RE builtin cannot be represented"); >> + ret.fail = true; >> + } >> + } >> + >> ret.reloc_data.type = type; >> ret.reloc_data.kind = kind; >> return ret; >> @@ -864,25 +891,17 @@ process_enum_value (struct cr_builtins *data) >> >> struct cr_final ret = { NULL, type, data->kind }; >> >> - if (TREE_CODE (expr) == CONST_DECL >> - && TREE_CODE (type) == ENUMERAL_TYPE) >> - { >> - unsigned int index = 0; >> - for (tree l = TYPE_VALUES (type); l; l = TREE_CHAIN (l)) >> - { >> - if (TREE_VALUE (l) == expr) >> - { >> - char *tmp = (char *) ggc_alloc_atomic ((index / 10) + 1); >> - sprintf (tmp, "%d", index); >> - ret.str = (const char *) tmp; >> - >> - break; >> - } >> - index++; >> - } >> - } >> - else >> - gcc_unreachable (); >> + gcc_assert (TREE_CODE (expr) == CONST_DECL >> + && TREE_CODE (type) == ENUMERAL_TYPE); >> + >> + int index = get_index_for_enum_value (type, expr); >> + gcc_assert (index != -1 && index < (1 << 16)); >> + >> + /* Index can only be a value up to 2^16. Should always fit >> + in 6 chars. */ >> + char tmp[6]; >> + sprintf (tmp, "%u", index); >> + ret.str = CONST_CAST (char *, ggc_strdup(tmp)); >> >> return ret; >> }
diff --git a/gcc/config/bpf/core-builtins.cc b/gcc/config/bpf/core-builtins.cc index e03e986e2c1..829acea98f7 100644 --- a/gcc/config/bpf/core-builtins.cc +++ b/gcc/config/bpf/core-builtins.cc @@ -795,6 +795,23 @@ process_field_expr (struct cr_builtins *data) static GTY(()) hash_map<tree, tree> *bpf_enum_mappings; tree enum_value_type = NULL_TREE; +static int +get_index_for_enum_value (tree type, tree expr) +{ + gcc_assert (TREE_CODE (expr) == CONST_DECL + && TREE_CODE (type) == ENUMERAL_TYPE); + + unsigned int index = 0; + for (tree l = TYPE_VALUES (type); l; l = TREE_CHAIN (l)) + { + gcc_assert (index < (1 << 16)); + if (TREE_VALUE (l) == expr) + return index; + index++; + } + return -1; +} + /* Pack helper for the __builtin_preserve_enum_value. */ static struct cr_local @@ -846,6 +863,16 @@ pack_enum_value_fail: ret.reloc_data.default_value = integer_one_node; } + if (ret.fail == false ) + { + int index = get_index_for_enum_value (type, tmp); + if (index == -1 || index >= (1 << 16)) + { + bpf_error ("enum value in CO-RE builtin cannot be represented"); + ret.fail = true; + } + } + ret.reloc_data.type = type; ret.reloc_data.kind = kind; return ret; @@ -864,25 +891,17 @@ process_enum_value (struct cr_builtins *data) struct cr_final ret = { NULL, type, data->kind }; - if (TREE_CODE (expr) == CONST_DECL - && TREE_CODE (type) == ENUMERAL_TYPE) - { - unsigned int index = 0; - for (tree l = TYPE_VALUES (type); l; l = TREE_CHAIN (l)) - { - if (TREE_VALUE (l) == expr) - { - char *tmp = (char *) ggc_alloc_atomic ((index / 10) + 1); - sprintf (tmp, "%d", index); - ret.str = (const char *) tmp; - - break; - } - index++; - } - } - else - gcc_unreachable (); + gcc_assert (TREE_CODE (expr) == CONST_DECL + && TREE_CODE (type) == ENUMERAL_TYPE); + + int index = get_index_for_enum_value (type, expr); + gcc_assert (index != -1 && index < (1 << 16)); + + /* Index can only be a value up to 2^16. Should always fit + in 6 chars. */ + char tmp[6]; + sprintf (tmp, "%u", index); + ret.str = CONST_CAST (char *, ggc_strdup(tmp)); return ret; }