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 13CF1385840B for ; Wed, 17 Apr 2024 20:20:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 13CF1385840B 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 13CF1385840B 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=1713385260; cv=pass; b=pqj67/1H8+i1msRXNUhnWeWoijSHXHC9d+9K9HROvNkCMzpevVq7mpDUeMRX/haFWL5muHqEQuE338OpD66hAZKAaVtc3CMyMDFonhqe/Aq3Nq225MFITW3ii+4Ig9ecrmkRA0f0xz+TQVmB3cRYkTKPvPVBks+ldICHr4sPaLs= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1713385260; c=relaxed/simple; bh=kEOaDZr0xkEJnQq5+7RO3cCf3dQEa8ZiiQDhkm2a4hU=; h=DKIM-Signature:DKIM-Signature:From:To:Subject:Date:Message-ID: MIME-Version; b=dQybst/d0FiZAPylLqyl7vanDKu7GTHJaRZSJRnETzikkyvn8YP1qN31HhvdHs9Rj967mEo2AvvGvtFv170nhkJxhduIgKDIXNEolkOY8LuRvQWTFjUZFOQcArkQfSbYWQlrj02iGI5DABbgh/rChPv+MWU6lxxTmYJJ6gDApAc= 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 43HHi2uF009051 for ; Wed, 17 Apr 2024 20:20:57 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : subject : date : message-id : in-reply-to : references : content-transfer-encoding : content-type : mime-version; s=corp-2023-11-20; bh=GcFB13Wp/6pOQtQM8lm99WCrdoRlPyhP2Dnh0fjblHU=; b=L3xGp+NgUhbz6Fts1/Wa8bExnVv0J1HAJo/wt1gtSeExIEXF5r6N4xHo+sTQFKAWPpvk 7XLN96sZM0QrtMAE0zOSI+4IX1o1gsJRFSbZM4hIB2w1khLTJpAVEKs7y3NwlyxF925j 7lGhpeq65fqCo1VnqQZCXrIgn/4BdVELzmwS3AUPGY1IDEZiLvj5IgulOmK+Ye6bMH63 7XyA/DQNCPv57nLt7gw/fzxPvrCXrgch+0OWRwCSEBdFhjW/tK1M2IYi/DlDqVwCsGEu lgPDAS725bQqRv60L9ayNoHPwkySit2jUj47hjN/SnHDp8avJQl6GLgdq7ZTIa+4dOK+ AA== Received: from iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta02.appoci.oracle.com [147.154.18.20]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3xfgn2ruqb-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Wed, 17 Apr 2024 20:20:56 +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 43HJ5TqQ021596 for ; Wed, 17 Apr 2024 20:20:55 GMT Received: from nam12-mw2-obe.outbound.protection.outlook.com (mail-mw2nam12lp2040.outbound.protection.outlook.com [104.47.66.40]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 3xfggfpe5c-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Wed, 17 Apr 2024 20:20:55 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=WO9vvscAOq+RMhF5snjDbIdfoTbmfzmPomLu2ILYMsRo/Pb9FLtrghmmcogqXVxyKxXGahiG5MPSfc3zfEWqUIyuasFXsOtEsjVmhTohU1YtxWKEYR7cIl4eez+Ww7Re3mNfHjnDPrIUxV7ffaCg6aXUodCwzCe2f7SPNp4szqhgt6xJh79NAbvlYM35PUapaLyKxdLQbRRod/+PYAFEuUpPkrEx7veUi1/+QifuneD37eaB50Ahe97YRyLoKPYAgvObMuljsiPVaZ3RkbFnEn5sZ/IgXcBUSt/dhJgRQaYSurMvfPR8Fpt1NmoNPAsq+hPHhfVSibHIadksmbobHg== 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=GcFB13Wp/6pOQtQM8lm99WCrdoRlPyhP2Dnh0fjblHU=; b=Vv+BsQQdO/bxG00dt/mne//dbdyIFD9TTAlrKMGqCwE+r8fHA6w5/sj40w6Rf3bn2EqZLVlTjEjqVUnwwkC6qrpbEZtGZVZPc8iecegxyIaiSwC3AprTgeR6ve6OUvoBFkbUAJx66a7G+2YxJoIWX4+3w+4uEck5tRobLRvTVWYN8mlMS/QUdVU3pZAA5ZOOAU9w96M8BmGMxn5NfmPY9M9ZP5LfXk/jSHvKzyk/j8q6xAbCyZeoqOp4vm02Ve74EMTCbqR4Q6jzVcTon7c0rYXsPih0t1J2u1yyIYSuAMiQc356fJjQbPGFGDj3+Ix6va+kfS4Zr0lmv4gOKXRoEg== 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=GcFB13Wp/6pOQtQM8lm99WCrdoRlPyhP2Dnh0fjblHU=; b=kiLj1/DWJAggNdOjm4qkT/f45ZHq5lgDufvEl4ok6ae2kUPcV2T95bNLu7PwuXPKLPReA/9YqqwjB00JtTD4m+uViLcs6d4S28M0T+yEW5LbT/MAtu2P3tNxAlAj5o3UxriPzcQCF7/7oc3od08X58oTnNB9aMMn1d5TXtUal+0= Received: from MW4PR10MB6608.namprd10.prod.outlook.com (2603:10b6:303:22e::8) by SA1PR10MB5821.namprd10.prod.outlook.com (2603:10b6:806:232::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7452.50; Wed, 17 Apr 2024 20:20:50 +0000 Received: from MW4PR10MB6608.namprd10.prod.outlook.com ([fe80::67a4:9544:7249:7751]) by MW4PR10MB6608.namprd10.prod.outlook.com ([fe80::67a4:9544:7249:7751%7]) with mapi id 15.20.7472.037; Wed, 17 Apr 2024 20:20:46 +0000 From: Nick Alcock To: binutils@sourceware.org Subject: [PATCH 04/22] libctf: fix name lookup in dicts containing base-type bitfields Date: Wed, 17 Apr 2024 21:20:00 +0100 Message-ID: <20240417202018.34966-5-nick.alcock@oracle.com> X-Mailer: git-send-email 2.44.0.273.ge0bd14271f In-Reply-To: <20240417202018.34966-1-nick.alcock@oracle.com> References: <20240417202018.34966-1-nick.alcock@oracle.com> Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: LO4P123CA0452.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:1aa::7) To MW4PR10MB6608.namprd10.prod.outlook.com (2603:10b6:303:22e::8) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: MW4PR10MB6608:EE_|SA1PR10MB5821:EE_ X-MS-Office365-Filtering-Correlation-Id: ece59496-642d-4781-2a3e-08dc5f1bdda2 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 2juSPY30hxI2KKQquZ6HXSYl6tGRMSF1odxTcWrdyQK3wmUX9lFXMFam/wyCj1XyPu5EDLee+lOQc38UiJya/bxEhAet61BYBNLkEkNyU1j6fP3O4mzOzN2BuQH2nn/ELzZwAt+IJoY3YdzOOJuJruMIuBLmrlHSHPzD60LzLZh7OmfBOuhJIjJm8gHyzwas4RpCjJjmcxn6o/APvxr5YEhorjB17YZlm7IBDC5O6rFOfOFuZjADyb4ZnLWLeOuDnkmXBPXdm9eiKSQuozKeRI7D1MQ35xWAuqFsJ/jlnDFnvXSwX3Wr8DDKXSyRAu1iFU/Xe8/eCsrw5tIWQrpbt56Je85/eM+LUIm11c7IdpKIqROMiNUd9ZEqXjLj+W86hWZ0uO9SR+G7CHYigdRQNH/icQq2lu5FrSwh+ZpzW/1yn51czFyyztBUvwEzctopBIgJHP+0DViqle3jdxT943tf6zoikNitx/JJNi9dXubVuc8amPuO0SnheZ26LyNI56N4eTznuxbP3IcGZyGDlfzrDNJKGhXls1OD2iRVHijxwQNbXG17+tMuowvck9A66N3/iAIDVcQ+VMvjY6U+mWXW7LA8nxjKELrx3Rexom7l8raeUMcOTk1fJL2mrNdt9SUpulBkjAzE7BouKnV81trDlWoL7NxRVA1ed4jBxCo= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:MW4PR10MB6608.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230031)(366007)(376005)(1800799015);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?9hbfYIV2AGbjjCszJPSGpSEZ/WnCh1lTSFHhLrZWHBVNCK5szeQpX91fUYr/?= =?us-ascii?Q?eIcA+q++Kr//HMZc1+/+PnQ4vQ7wnsg6XkidU3EMC8eBiXpsZFp9JFnk99nx?= =?us-ascii?Q?Z8nwV25FnGad/TCKICFfjizKLT5W6dXmf4bsh2x+IVU85F9FoLdwpVakvsb3?= =?us-ascii?Q?dI1fuRg6t0TfKXnphwmvnf1fJ92SWNQO00PNxGXKBNqLYkdIt89XfKP963ie?= =?us-ascii?Q?Q0EjCpIeDvgp9cXVB+k/j68lbhA7BZKQrO8AROAMgHYS6BI7MW3vBOKdKtPO?= =?us-ascii?Q?T/WyWbVosdn+CG2CZO+h8S+QiVw1RU9opiydI1szNp0oUC0jCE8lQ6enEEMe?= =?us-ascii?Q?9oAx7g4m9rSsTuWAZHGw1oRwA0LaJgkHBSpF3FuJ4wpWg0K3SssBkU6Pg3wj?= =?us-ascii?Q?dd5yo3O3Toz0bcQmAhGITNM/wMQUgb1gKimJLtrcNglpOYVdusD3MMsHxwYO?= =?us-ascii?Q?uy3KaWM+IIsAj1V7XPTT9Bg6i7A8RIBbwCDQadMVb2dh20+MCXeDLTj3Vk7U?= =?us-ascii?Q?U4tvtwnIKqFYm9Ip8JuBAE6tvPn2Yz66qw9eQcFHp7156xlPNzm13K255urM?= =?us-ascii?Q?OXrA6+gAyxwOnzh/dRBjFVTGbh+VU9t3hcNuFL2Z9fjsiM5pyAbLBB1LOg1E?= =?us-ascii?Q?gzW329esgWC3XOSHwIhvXRvrd7I88KvYzI+PiBXT65U/aLFXIdvPNRTubXS1?= =?us-ascii?Q?3QgsWXox7egU68Bnbm/lyRIapZcUxoXDhTfFM/A/gVFGk3XPpt2ZAG7CrHMQ?= =?us-ascii?Q?cQu8tsZfX1Y4FGrvVBXEhYm87tX6E2+7NCBLCwbgpIzpD+k7hCBjoSFIoM8p?= =?us-ascii?Q?CTz0ckCBBmS78R2mmYlpsmEKjbXKtr1qWMIHzoKv+vVlM+mzcwUwtzzRrecH?= =?us-ascii?Q?JTDpC9M5sd6ktK8Iw0v09KYvm/oriCWZpUjFK9vv+BX5aN1at6cxvUHMTzS9?= =?us-ascii?Q?hQoFH+SBiYy54EhQ+b7cehZodxg733f2mdYwLMZBZXMberhy4nE+6/RKk4g0?= =?us-ascii?Q?122EdMM/0ewP1HB0ApLKOJf4Y7oketK5Aw+4fL4zaeZluMTJFNU04R25YxCq?= =?us-ascii?Q?xVFoDjmmqKTl3fy9Cls0hB1/swnedZ+S2Ywl+U81ELHmCbt24qieexYlbjDL?= =?us-ascii?Q?hpqMKKTSgV0+G/QqAFPnOPvawE+Vn5s/h05cI64jMeJbF0QtcdK24HAIjjGJ?= =?us-ascii?Q?mi+kr7/c74ZbzkkqZYlvPB/C7EYSKvcVKakP9eSkLMpZG2QNpKNVhojLp+VL?= =?us-ascii?Q?a4egKuQUo4vcG6CGi8a1KqYeGN47x3phyWo1g4Rq1Kzyjs9ENVKSPgRwRMrp?= =?us-ascii?Q?Iuaz8rPEL5FbW3DmyXLUSseuRHVkKwXmB72YrQiSweCSWr78yWiGRXoTha7L?= =?us-ascii?Q?+Qnj7yLFyXaPnrz9EkJpTDmJkH1z07+8qdvRuEf94Nak9W3DUlSrHG3dt4db?= =?us-ascii?Q?q2k1AzCSig6hOjTmmvtpXimut0gcyk4Lli0TYqOykiiIaZMqCckxvNSafuS0?= =?us-ascii?Q?/TJH4vRZzlVZ4e54u61HboEUuf6WpHmbMVGa4wP1nWT9VQV+RMh0saevoRcb?= =?us-ascii?Q?U5shex3l+WhZPIloUDrR1iacPB0BdHmqID1CYdTeOoNmBwHipH+a57VrZbol?= =?us-ascii?Q?XA=3D=3D?= X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: j9bcReqyg/4Tm0gdzd5e68WBv8gnrM9ZVagUrsZKzpakyr1+J0jyPFrpl4Eq+MSTXrkOExw3wMzX7ds3H6eM9D+cHkW4A/E7YEZUiuLvNO+CPeYn4ZYd1uo8gwRRFSRHys+GkjRm0/XKPmjPi1hNenQMN1tN0T9CXWYR0IMN5S2/qDgxxwu7fmH1nu1OaUtZVwgnaHC16sgkyYMkhZltNpteeUV+VzNKlzXl3sjQUgrVKAB2XE0Auli8M+pzyL8yuwf/uCRNE19R5VQTtb1B69w6ssiiiJLhondToLwr/dbnyNRvRCIc52L+QG+4tNax5mEYppC3JxyRAqLBGjmtPDXd9pu7BY/YFcZYUlmp7B0zY5PA2pzIcVbuzavFomIIYQ/9xFfGclU6L+0jIBr3nvjlgUPHictj7xaESkIorypWwZTM89kyvZEdErOD8s4MDIytp40Fj8JrNGJstGa2AM0dDdlk/RSjcD8TD0SrwDUX0HSosNu3FRwFE5gFXixP9K5tr46UWRPeA0bFN+obl52+qWChVEeq6KE08Li6bmybW3gMLXz8ABtHxZ7Bf6XkiV24LC/h13hAaAbPRdFACcu34+flCq1ziklWfxXcb24= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: ece59496-642d-4781-2a3e-08dc5f1bdda2 X-MS-Exchange-CrossTenant-AuthSource: MW4PR10MB6608.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 17 Apr 2024 20:20:46.9126 (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: JY1RN9wg789VvZdUkdgATJGKYQE149W8uD/hl1RqNd12aPY26bhfglRn7IVbdsA0TYeafjXjXu8ov4hneyCU5Q== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA1PR10MB5821 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-17_17,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-2404170143 X-Proofpoint-ORIG-GUID: S3Wzjha2I_tBHKvLNNtzvEdML5lTDmmk X-Proofpoint-GUID: S3Wzjha2I_tBHKvLNNtzvEdML5lTDmmk X-Spam-Status: No, score=-11.7 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 List-Id: The intent of the name lookup code was for lookups to yield non-bitfield basic types except if none existed with a given name, and only then return bitfield types with that name. Unfortunately, the code as written only does this if the base type has a type ID higher than all bitfield types, which is most unlikely (the opposite is almost always the case). Adjust it so that what ends up in the name table is the highest-width zero-offset type with a given name, if any such exist, and failing that the first type with that name we see, no matter its offset. (We don't define *which* bitfield type you get, after all, so we might as well just stuff in the first we find.) Reported by Stephen Brennan . libctf/ * ctf-open.c (init_types): Modify to allow some lookups during open; detect bitfield name reuse and prefer less bitfieldy types. * testsuite/libctf-writable/libctf-bitfield-name-lookup.*: New test. --- libctf/ctf-open.c | 73 ++++++---- .../libctf-bitfield-name-lookup.c | 136 ++++++++++++++++++ .../libctf-bitfield-name-lookup.lk | 1 + 3 files changed, 186 insertions(+), 24 deletions(-) create mode 100644 libctf/testsuite/libctf-writable/libctf-bitfield-name-lookup.c create mode 100644 libctf/testsuite/libctf-writable/libctf-bitfield-name-lookup.lk diff --git a/libctf/ctf-open.c b/libctf/ctf-open.c index 2945228ff2a..87b0f74367a 100644 --- a/libctf/ctf-open.c +++ b/libctf/ctf-open.c @@ -685,6 +685,7 @@ init_types (ctf_dict_t *fp, ctf_header_t *cth) const ctf_type_t *tp; uint32_t id; uint32_t *xp; + unsigned long typemax = 0; /* We determine whether the dict is a child or a parent based on the value of cth_parname. */ @@ -708,7 +709,7 @@ init_types (ctf_dict_t *fp, ctf_header_t *cth) /* We make two passes through the entire type section. In this first pass, we count the number of each type and the total number of types. */ - for (tp = tbuf; tp < tend; fp->ctf_typemax++) + for (tp = tbuf; tp < tend; typemax++) { unsigned short kind = LCTF_INFO_KIND (fp, tp->ctt_info); unsigned long vlen = LCTF_INFO_VLEN (fp, tp->ctt_info); @@ -769,8 +770,8 @@ init_types (ctf_dict_t *fp, ctf_header_t *cth) ctf_hash_eq_string, NULL, NULL)) == NULL) return ENOMEM; - fp->ctf_txlate = malloc (sizeof (uint32_t) * (fp->ctf_typemax + 1)); - fp->ctf_ptrtab_len = fp->ctf_typemax + 1; + fp->ctf_txlate = malloc (sizeof (uint32_t) * (typemax + 1)); + fp->ctf_ptrtab_len = typemax + 1; fp->ctf_ptrtab = malloc (sizeof (uint32_t) * fp->ctf_ptrtab_len); if (fp->ctf_txlate == NULL || fp->ctf_ptrtab == NULL) @@ -779,13 +780,17 @@ init_types (ctf_dict_t *fp, ctf_header_t *cth) xp = fp->ctf_txlate; *xp++ = 0; /* Type id 0 is used as a sentinel value. */ - memset (fp->ctf_txlate, 0, sizeof (uint32_t) * (fp->ctf_typemax + 1)); - memset (fp->ctf_ptrtab, 0, sizeof (uint32_t) * (fp->ctf_typemax + 1)); + memset (fp->ctf_txlate, 0, sizeof (uint32_t) * (typemax + 1)); + memset (fp->ctf_ptrtab, 0, sizeof (uint32_t) * (typemax + 1)); /* In the second pass through the types, we fill in each entry of the - type and pointer tables and add names to the appropriate hashes. */ + type and pointer tables and add names to the appropriate hashes. - for (id = 1, tp = tbuf; tp < tend; xp++, id++) + Bump ctf_typemax as we go, but keep it one higher than normal, so that + the type being read in is considered a valid type and it is at least + barely possible to run simple lookups on it. */ + + for (id = 1, fp->ctf_typemax = 1, tp = tbuf; tp < tend; xp++, id++, fp->ctf_typemax++) { unsigned short kind = LCTF_INFO_KIND (fp, tp->ctt_info); unsigned short isroot = LCTF_INFO_ISROOT (fp, tp->ctt_info); @@ -799,27 +804,47 @@ init_types (ctf_dict_t *fp, ctf_header_t *cth) /* Cannot fail: shielded by call in loop above. */ vbytes = LCTF_VBYTES (fp, kind, size, vlen); + *xp = (uint32_t) ((uintptr_t) tp - (uintptr_t) fp->ctf_buf); + switch (kind) { case CTF_K_UNKNOWN: case CTF_K_INTEGER: case CTF_K_FLOAT: - /* Names are reused by bit-fields, which are differentiated by their - encodings, and so typically we'd record only the first instance of - a given intrinsic. However, we replace an existing type with a - root-visible version so that we can be sure to find it when - checking for conflicting definitions in ctf_add_type(). */ + { + ctf_id_t existing; + ctf_encoding_t existing_en; + ctf_encoding_t this_en; - if (((ctf_dynhash_lookup_type (fp->ctf_names, name)) == 0) - || isroot) - { - err = ctf_dynhash_insert_type (fp, fp->ctf_names, - LCTF_INDEX_TO_TYPE (fp, id, child), - tp->ctt_name); - if (err != 0) - return err; - } - break; + if (!isroot) + break; + + /* Names are reused by bitfields, which are differentiated by + their encodings. So check for the type already existing, and + iff the new type is a root-visible non-bitfield, replace the + old one. It's a little hard to figure out whether a type is + a non-bitfield without already knowing that type's native + width, but we can converge on it by replacing an existing + type as long as the new type is zero-offset and has a + bit-width wider than the existing one, since the native type + must necessarily have a bit-width at least as wide as any + bitfield based on it. */ + + if (((existing = ctf_dynhash_lookup_type (fp->ctf_names, name)) == 0) + || ctf_type_encoding (fp, existing, &existing_en) != 0 + || (ctf_type_encoding (fp, LCTF_INDEX_TO_TYPE (fp, id, child), &this_en) == 0 + && this_en.cte_offset == 0 + && (existing_en.cte_offset != 0 + || existing_en.cte_bits < this_en.cte_bits))) + { + err = ctf_dynhash_insert_type (fp, fp->ctf_names, + LCTF_INDEX_TO_TYPE (fp, id, child), + tp->ctt_name); + if (err != 0) + return err; + } + break; + } /* These kinds have no name, so do not need interning into any hashtables. */ @@ -938,10 +963,10 @@ init_types (ctf_dict_t *fp, ctf_header_t *cth) _("init_static_types(): unhandled CTF kind: %x"), kind); return ECTF_CORRUPT; } - - *xp = (uint32_t) ((uintptr_t) tp - (uintptr_t) fp->ctf_buf); tp = (ctf_type_t *) ((uintptr_t) tp + increment + vbytes); } + fp->ctf_typemax--; + assert (fp->ctf_typemax == typemax); ctf_dprintf ("%lu total types processed\n", fp->ctf_typemax); ctf_dprintf ("%zu enum names hashed\n", diff --git a/libctf/testsuite/libctf-writable/libctf-bitfield-name-lookup.c b/libctf/testsuite/libctf-writable/libctf-bitfield-name-lookup.c new file mode 100644 index 00000000000..1554ca2d626 --- /dev/null +++ b/libctf/testsuite/libctf-writable/libctf-bitfield-name-lookup.c @@ -0,0 +1,136 @@ +/* Verify that name lookup of basic types including old-style bitfield types + yields the non-bitfield. */ + +#include +#include +#include +#include + +int bitfieldery (int count, int up, int pos) +{ + unsigned char *ctf_written; + size_t size; + ctf_dict_t *dict; + const char *err = "opening"; + int open_err; + ctf_encoding_t en; + ctf_encoding_t basic; + ctf_id_t type; + size_t i; + + /* This is rendered annoying by two factors: old-style bitfields are not + generated by current compilers, so we need to build a suitable dict by + hand; and this is an open-time bug, so we need to serialize it and then + load it back in again. */ + + if ((dict = ctf_create (&open_err)) == NULL) + goto open_err; + + /* Populate with a pile of bitfields of increasing/decreasing size, with a + single basic type dropped in at position POS. Oscillate the offset + between 0 and 1. */ + + basic.cte_bits = count; + basic.cte_offset = 0; + basic.cte_format = CTF_INT_SIGNED; + + en.cte_bits = up ? 0 : count - 1; + en.cte_offset = 0; + en.cte_format = CTF_INT_SIGNED; + + for (i = 0; i < count; i++) + { + if (i == pos) + { + err = "populating with basic type"; + if (ctf_add_integer (dict, CTF_ADD_ROOT, "int", &basic) < 0) + goto err; + } + + err = "populating"; + if (ctf_add_integer (dict, CTF_ADD_ROOT, "int", &en) < 0) + goto err; + + en.cte_bits += up ? 1 : -1; + if (en.cte_offset == 0) + en.cte_offset = 1; + else + en.cte_offset = 0; + } + + /* Possibly populate with at-end basic type. */ + if (i == pos) + { + err = "populating with basic type"; + if (ctf_add_integer (dict, CTF_ADD_ROOT, "int", &basic) < 0) + goto err; + } + + err = "writing"; + if ((ctf_written = ctf_write_mem (dict, &size, 4096)) == NULL) + goto err; + ctf_dict_close (dict); + + err = "opening"; + if ((dict = ctf_simple_open ((char *) ctf_written, size, NULL, 0, + 0, NULL, 0, &open_err)) == NULL) + goto open_err; + + err = "looking up"; + if ((type = ctf_lookup_by_name (dict, "int")) == CTF_ERR) + goto err; + + err = "encoding check"; + if (ctf_type_encoding (dict, type, &en) < 0) + goto err; + + if (en.cte_bits < count || en.cte_offset != 0) { + fprintf (stderr, "Name lookup with count %i, pos %i, counting %s " + "gave bitfield ID %lx with bits %i, offset %i\n", count, pos, + up ? "up" : "down", type, en.cte_bits, en.cte_offset); + return 1; + } + ctf_dict_close (dict); + free (ctf_written); + + return 0; + + open_err: + fprintf (stdout, "Error %s: %s\n", err, ctf_errmsg (open_err)); + return 1; + + err: + fprintf (stdout, "Error %s: %s\n", err, ctf_errmsg (ctf_errno (dict))); + return 1; +} + +/* Do a bunch of tests with a type of a given size: up and down, basic type + at and near the start and end, and in the middle. */ + +void mass_bitfieldery (long size) +{ + size *= 8; + bitfieldery (size, 1, 0); + bitfieldery (size, 0, 0); + bitfieldery (size, 1, 1); + bitfieldery (size, 0, 1); + bitfieldery (size, 1, size / 2); + bitfieldery (size, 0, size / 2); + bitfieldery (size, 1, size - 1); + bitfieldery (size, 0, size - 1); + bitfieldery (size, 1, size); + bitfieldery (size, 0, size); +} + +int main (void) +{ + mass_bitfieldery (sizeof (char)); + mass_bitfieldery (sizeof (short)); + mass_bitfieldery (sizeof (int)); + mass_bitfieldery (sizeof (long)); + mass_bitfieldery (sizeof (uint64_t)); + + printf ("All done.\n"); + + return 0; +} diff --git a/libctf/testsuite/libctf-writable/libctf-bitfield-name-lookup.lk b/libctf/testsuite/libctf-writable/libctf-bitfield-name-lookup.lk new file mode 100644 index 00000000000..b944f73d013 --- /dev/null +++ b/libctf/testsuite/libctf-writable/libctf-bitfield-name-lookup.lk @@ -0,0 +1 @@ +All done. -- 2.44.0.273.ge0bd14271f