From patchwork Fri Dec 10 02:52:52 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Guillermo E. Martinez" X-Patchwork-Id: 48745 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 128F73857C43 for ; Fri, 10 Dec 2021 02:53:22 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 128F73857C43 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1639104802; bh=j1BIGc/4vuSD4f3rcigy7qfbxEZgctqflpsIPoEhNjM=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Help: List-Subscribe:From:Reply-To:From; b=vMKO3WROR+4cqZDIJSPCaV2J9TLu0L94vGLVR46fJqMApXZimXNVcugbRYmP9uIF6 YVID3bcaTkjNgOFmqevEpPs7FCGT44XgI1wx2vEVTBTutVs57M0R0hH8zD9L0tlX3k ODvv7NKvIllCt+nm6mY+Pf/2XYNo+vnNACxkVc/Q= X-Original-To: libabigail@sourceware.org Delivered-To: libabigail@sourceware.org Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by sourceware.org (Postfix) with ESMTPS id DBB003858404 for ; Fri, 10 Dec 2021 02:53:13 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org DBB003858404 Received: from pps.filterd (m0246617.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 1BA0dV5I023711 for ; Fri, 10 Dec 2021 02:53:13 GMT Received: from aserp3020.oracle.com (aserp3020.oracle.com [141.146.126.70]) by mx0b-00069f02.pphosted.com with ESMTP id 3ctup54rqw-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Fri, 10 Dec 2021 02:53:12 +0000 Received: from pps.filterd (aserp3020.oracle.com [127.0.0.1]) by aserp3020.oracle.com (8.16.1.2/8.16.1.2) with SMTP id 1BA2kE16053437 for ; Fri, 10 Dec 2021 02:53:11 GMT Received: from nam11-bn8-obe.outbound.protection.outlook.com (mail-bn8nam11lp2175.outbound.protection.outlook.com [104.47.58.175]) by aserp3020.oracle.com with ESMTP id 3cr058qwt1-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Fri, 10 Dec 2021 02:53:10 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=gh5UR8LwiO0vP7jimZwZk3GoBTftuIJtfUIysFnaUU/JC1Fkqhm3X2WE/gt+yj2DGCts1yCOaO0itYSUvxmulJJOOJ0qjRD1ufR1RHwHy9F7ckOkelimpFAmmyLzNd3u4xm8jjS+IxiHjl5/Mt+O/eRMatMsu8cMBXla8KH++nnEzITHJhwkGNtxDg4XlECURO0M8A07n/uiwlwq9myZgqVSuqVXM2Dlz0hcuhGCRjytCdSx3zvQ0vYbZ9lmNF3EVe1HBE0FNJH0bQSufiTomo2gJ2dKOMSLjdjLXMO9FSIfoKgG1/lPr70klT7HoD2nfz4aFFDCF5RCGDghX2bTcw== 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=j1BIGc/4vuSD4f3rcigy7qfbxEZgctqflpsIPoEhNjM=; b=WcU4CeROJz/6bxFuyYfXc/FtNR+iydn0mlLus/xBOacya8tF4rmqGd9/7kIKgrOWWWHtYCdMC5fB6Rw/aQ4vs4iQckNyfSgUm/bqmN1guW0pKgWBJf80Jaljre3WJ9LSg4XRLE8YHn861gi5mH0ZY4EyJcSm8aJNz1Kw37XvO9LZf3lndUGvLK/QE7FKx9du69D1khLut23aDcDx7/25APDGC7ivb6gtwl4OwSW4mFU20VaDr3uJbkTHMK83Q2RV4cdZjomatiyifl4WVYGNVmJdZzK8G7jJC43GOlAsCzI9lZ/PwqSCof3ghXXI2gFRfQ+iIrMYE1nEvg3xFU5T/g== 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 Received: from BL0PR10MB2852.namprd10.prod.outlook.com (2603:10b6:208:76::22) by BL0PR10MB2931.namprd10.prod.outlook.com (2603:10b6:208:73::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4778.12; Fri, 10 Dec 2021 02:53:08 +0000 Received: from BL0PR10MB2852.namprd10.prod.outlook.com ([fe80::6927:5e6b:31ff:14bd]) by BL0PR10MB2852.namprd10.prod.outlook.com ([fe80::6927:5e6b:31ff:14bd%3]) with mapi id 15.20.4755.025; Fri, 10 Dec 2021 02:53:08 +0000 To: libabigail@sourceware.org Subject: [PATCH] ctf-reader: Assert on ir::hash_as_canonical_type_or_constant Date: Thu, 9 Dec 2021 20:52:52 -0600 Message-Id: <20211210025252.2309157-1-guillermo.e.martinez@oracle.com> X-Mailer: git-send-email 2.33.0 X-ClientProxiedBy: SJ0PR13CA0238.namprd13.prod.outlook.com (2603:10b6:a03:2c1::33) To BL0PR10MB2852.namprd10.prod.outlook.com (2603:10b6:208:76::22) MIME-Version: 1.0 Received: from sali.us.oracle.com (2606:b400:8004:44::27) by SJ0PR13CA0238.namprd13.prod.outlook.com (2603:10b6:a03:2c1::33) with Microsoft SMTP Server (version=TLS1_2, cipher=) via Frontend Transport; Fri, 10 Dec 2021 02:53:07 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: a3cfbdd4-65aa-4dc7-7d0a-08d9bb883214 X-MS-TrafficTypeDiagnostic: BL0PR10MB2931:EE_ X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:9508; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: bJIHUFGm2C5aSB6g9SQfASxz+WV5p1JxUzczuz/zA4kXjhHed7aqRr/kRNZPzSlT0t5yuxF97DRet3xw7gxAhfk2C7jH9tA+CT0Z24wDl3j0bNHSYXaOTZmZaHixVSnQvZgcM/XhivDBRoZXecpwiyhScsZLPPUE905d0R+Bpvh4JkSV4V97HcHH3Yk/bxaDPgRA6eEdRqhsuSaAU4wVwzpecLSwUCJUqudEd02PZ7oW0Mhn2xpJP1D65W22PM/No3//sQESqnbAp4/GDHq9lU2suJjX1jNqoYK6pHep0B62bJJfSm4KZRr0e0V/mqWWIukAqdX9EqYzQ1cea9OEDrVpBBaObi4GYA4Qh5lb5wJe6zI7r1nqNO0DBhRy+VnKTmvsi6YbWSuGjuwjyigOBgaP+kwXeAiJ9NptWKp8pIjOuVbyJKb5yVNEnw7lDBcB5WblmOQlx0XOd1lMXaz/LfFBId7MrqaLba1fwzz7fC4WRgUhMg68MQMVZJnkD/elPeaT3qTh3iK4FpmqjZnzk9/BZMCWOPng4NDlleofyNY33dxEyeRqv2XnMfHRHC8wflTzPzvrn1nFGpVfLzmOhRKghRGUq4LKx6r0TfW7gFY7Gh+bA3xULXdb0V0CJh5eyetx5SJjMqDDTVoxR2RuyHJLmPJIKpqPv1FfEyboxgjc1XlS9kZ6eXYl/lzsQ+a+djV6iGgk1AF2Wm04C0Ofj810+IQn6+pgcOAIOjfi19E= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:BL0PR10MB2852.namprd10.prod.outlook.com; PTR:; CAT:NONE; SFS:(366004)(36756003)(103116003)(966005)(508600001)(8936002)(4326008)(2906002)(7696005)(86362001)(107886003)(83380400001)(316002)(52116002)(66476007)(6486002)(66556008)(6666004)(1076003)(66946007)(5660300002)(30864003)(38100700002)(2616005)(8676002)(6916009)(186003); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: sJYpuMTzthistQ3tbTh7h8TMSc0fmDzvu0kS1P/hRAFQ4x1HTjT0ZWVkRfKKBM8f4YEt65Qq/o6vwPMRKtp71UnP9Tga8DqSvbbXKBhs9SGTerhUQ8qGZgQblE/gzX0WTs0g1LYs2KCDE+9rCFzPUN+tDrpmWaDQjkXDaOqhicPFhWJf54PdZ1dcXB8gocrxoefs19gotYmHSPE1gJLUcHJvtj2F0EZ7R1tqSr5k9WAORFE6ABlQlRs41D1rVyjXU7Apv5rFVIxF8/7KImBiMK0r4x4B4T3TS4cdWWaoyZw5RlKO65WqGbUL23CRwMiBIo1Vjk7+ejRy22W9SyKKbHU+oobErbiX9GK8W1VPJ5gb42vVu+wkxp+DU+nMioFmTz462JSCk4L3pZYqaI08MbRCbziEUekSd6BitsyLHMkFThZgqg1lSkoQsfGv5lz9dV8puDBLhxSXZ6ZY3bi4uNg72jtHmgBNXlhUwiNEbzi7F8Wew3TlOzIUUOaDwrRj/KEwt0mTy4qTTCZh32eVNF3tn1LoyjeeHFZvTXpAm1Iy5c1BM2gvfpAe3cQfSU05K7fDVIacw1lBTJCmjTxr3HKUtO22bgcm8RYI06g9Ctxn3L2aFmZaVTbPrPE6BcqLWugv/zAWgl2EWNHxBG3qe1EM7irZcQMQYoFTuY/95orJzw+vgLGQ/s9GFpgwHrTG+0ymdmQqUOmij0iCuFZhk2noJaDMKfUSlwyVl1G6WkdSRRjipLmwjjXfJUVz4g1tg13CPy01C9OlU5C8FY4TsDF5sODtBS6qVmpnaKjbCkxY88bOnBBjctV2G3rjkrcPTGt6hbwDUp32KTUGLEBBtLCT3tB+WgHArLkgLoIxJVIxfq8Vrp54/v7T/p+p0btrrLEe2ziTZ8fjC9H7smYa8elqhKQ6t994BmPWzXi0V0VuYtTcuD0OENTitsmIjgErsE6fRBn7HeULoD+hCcpkA8z0o/hoXfMe9Uj41VeSeJoHmoScczkC1IatUT6jpijfhmPac0zlRv3jPhYxGgXtQO2KWpU+qnOMvsrKadKeB3Dd5K1VC6duOkGsVDbmOLwUuXeupGNaGySWfv6M6Jk1Kf7mcJYI8z19ifenPabj0a5pEw+xElVavrem4LuEGm3s/HJsfdc3o04fu+KXE7x6FeLMzZHB7ztt6pAaD5K8ljr2E6obTKvau1AAjeQBCRFNegTmcYVUUdduBrHjNmeFepXtogE1uZq+ZJOOOlgb3J8IS8v0nUP9aFL/ePri43Xf+9jADHVlfYxaHlz7hEVcW97N1fUcLZNblD3cbjJyHjw03rkbydOjOFd8VDFURSNhD/s5HGTHUt0aCl6SNpFpnpUChWVyTzwW29asbx5Cn4wbobmGfCf4NJVb8EryG2RZWammSiwF+za32XBZ57xcttuFJhaENJymAn4kQGQdJvmosR0XZPE4FQs9CRyoQxHWO2vmuvr/vZhdQUmfSY5ez4DnU/kN58bwNlDTx9oxycoEYLXv5qPpCvhvFC2xQ4vY6WwOEyBx99e0Ot8ajK4iqLv99JQgZtkQvNGB+k1whZBztJFLJYKdVh74/z281UPiXeMj+OQTOJ8xd+639DIdlxR3Q0WE8t+LkrzbAVZvhh8PhFs46+5I5BL3QvChln+uU8QjGN+N+A+cJRQIrCYQFpENwmskEMy2PQDCcwRzYASixKktOlqtt5dJd0OzOre7 X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: a3cfbdd4-65aa-4dc7-7d0a-08d9bb883214 X-MS-Exchange-CrossTenant-AuthSource: BL0PR10MB2852.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Dec 2021 02:53:08.3773 (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: wTfy4Ry1SeZcmB60L20ZVMPlKELmEFlzZ8shGyroH+38po9R0Cc/E5BlasDSVugGHQf+yxqhGRwNw8K+Me6cdUvdRi6/6sJ2icZt6GuFGWM= X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL0PR10MB2931 X-Proofpoint-Virus-Version: vendor=nai engine=6300 definitions=10193 signatures=668683 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 mlxscore=0 phishscore=0 malwarescore=0 spamscore=0 mlxlogscore=999 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2110150000 definitions=main-2112100011 X-Proofpoint-GUID: k0n7oy3VFahJtawZCsm4IzeXOn6KrzrD X-Proofpoint-ORIG-GUID: k0n7oy3VFahJtawZCsm4IzeXOn6KrzrD X-Spam-Status: No, score=-13.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, MSGID_FROM_MTA_HEADER, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libabigail@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Mailing list of the Libabigail project List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-Patchwork-Original-From: "Guillermo E. Martinez via Libabigail" From: "Guillermo E. Martinez" Reply-To: "Guillermo E. Martinez" Errors-To: libabigail-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libabigail" Hello libabigail team, This patch fix assertions in ctf reader back-end. Dependencies/limitations: * It works on the top of the following patch: https://sourceware.org/pipermail/libabigail/2021q4/003918.html I'll appreciate your comments. Kind Regards, Guillermo In some scenarios where we declare same data types `recursively' such as: like linked list, functions that accept the same pointer to function as arguments, forward types declarations to build structures with member fields with mutual dependencies, etc., an assertion is trigger: abidw: ../../../libabigail-upstream/src/abg-ir.cc:25251: size_t abigail::ir::hash_as_canonical_type_or_constant(const abigail::ir::type_base*): Assertion `__abg_cond__' failed. It is happening because the recursively behavior of `process_ctf_type' and `process_ctf_*' used to register ctf types doesn't verify when a ctf_type was processed and registered before by their subsequence _recursive_ calls, so `type_base' object is built more than once and the second time when it is inserted in `types_maps', it refuses in a silent way, being that the key was already inserted, however `add_decl_to_scope', `bind_function_type_life_time' successfully registered the ctf type object. In this patch `process_ctf_type' delegates register types task to `process_ctf_*' functions guaranteeing a single ctf type registration, also it improves the performance looking for the type before start to build it again. * src/abg-ctf-reader.cc (process_ctf_base_type): Add new `translation_unit_sptr' parameter. Add condition to validate success 'base_type' construction and register type object. (process_ctf_typedef): Add `lookup_type' to get a `type_base' object when this was previously created, if this is not the case, register ctf type. Add condition to validate success 'base_type' construction and register type object. (process_ctf_function_type): Likewise. (process_ctf_array_type): Likewise. (process_ctf_qualified_type): Likewise. (process_ctf_pointer_type): Likewise. (process_ctf_struct_type): Add `add_decl_to_scope'. (process_ctf_union_type): Likewise. (process_ctf_type): Add `lookup_type' to get a `type_base' object when this was previously created. Delegate register type object to `process_ctf_*'. * tests/data/test-read-ctf/test-array-of-pointers.c: New testcase. * tests/data/test-read-ctf/test-list-struct.c: Likewise. * tests/data/test-read-ctf/test-callback.c: Likewise. * tests/data/test-read-ctf/test-callback2.c: Likewise. * tests/data/test-read-ctf/test-functions-declaration.c: Likewise. * tests/data/test-read-ctf/test-forward-type-decl.c: Likewise. * tests/data/test-read-ctf/test-array-of-pointers.abi: Expected test output. * tests/data/test-read-ctf/test-callback.abi: Likewise. * tests/data/test-read-ctf/test-callback2.abi: Likewise. * tests/data/test-read-ctf/test-forward-type-decl.abi: Likewise. * tests/data/test-read-ctf/test-functions-declaration.abi: Likewise. * tests/data/test-read-ctf/test-list-struct.abi: Likewise. * tests/data/test-read-ctf/test6.c: Likewise. * tests/data/test-read-ctf/test6.o.abi: Likewise. * tests/test-read-ctf.cc: Add testcases to CTF test harness. Signed-off-by: Guillermo E. Martinez --- src/abg-ctf-reader.cc | 151 ++++++++++-------- .../test-read-ctf/test-array-of-pointers.abi | 36 +++++ .../test-read-ctf/test-array-of-pointers.c | 20 +++ tests/data/test-read-ctf/test-callback.abi | 31 ++++ tests/data/test-read-ctf/test-callback.c | 16 ++ tests/data/test-read-ctf/test-callback2.abi | 20 +++ tests/data/test-read-ctf/test-callback2.c | 11 ++ .../test-read-ctf/test-forward-type-decl.abi | 29 ++++ .../test-read-ctf/test-forward-type-decl.c | 23 +++ .../test-functions-declaration.abi | 25 +++ .../test-functions-declaration.c | 16 ++ tests/data/test-read-ctf/test-list-struct.abi | 20 +++ tests/data/test-read-ctf/test-list-struct.c | 11 ++ tests/test-read-ctf.cc | 48 ++++++ 14 files changed, 389 insertions(+), 68 deletions(-) create mode 100644 tests/data/test-read-ctf/test-array-of-pointers.abi create mode 100644 tests/data/test-read-ctf/test-array-of-pointers.c create mode 100644 tests/data/test-read-ctf/test-callback.abi create mode 100644 tests/data/test-read-ctf/test-callback.c create mode 100644 tests/data/test-read-ctf/test-callback2.abi create mode 100644 tests/data/test-read-ctf/test-callback2.c create mode 100644 tests/data/test-read-ctf/test-forward-type-decl.abi create mode 100644 tests/data/test-read-ctf/test-forward-type-decl.c create mode 100644 tests/data/test-read-ctf/test-functions-declaration.abi create mode 100644 tests/data/test-read-ctf/test-functions-declaration.c create mode 100644 tests/data/test-read-ctf/test-list-struct.abi create mode 100644 tests/data/test-read-ctf/test-list-struct.c diff --git a/src/abg-ctf-reader.cc b/src/abg-ctf-reader.cc index fbf6baef..bc49f49c 100644 --- a/src/abg-ctf-reader.cc +++ b/src/abg-ctf-reader.cc @@ -15,6 +15,7 @@ #include /* For open(3) */ #include +#include #include "ctf-api.h" @@ -39,6 +40,7 @@ namespace abigail { namespace ctf_reader { +using std::dynamic_pointer_cast; class read_context { @@ -157,6 +159,10 @@ process_ctf_typedef(read_context *ctxt, if (!utype) return result; + result = dynamic_pointer_cast(ctxt->lookup_type(ctf_type)); + if (result) + return result; + result.reset(new typedef_decl(typedef_name, utype, location(), typedef_name /* mangled_name */)); @@ -171,6 +177,12 @@ process_ctf_typedef(read_context *ctxt, decl->set_naming_typedef(result); } + if (result) + { + add_decl_to_scope(result, tunit->get_global_scope()); + ctxt->add_type(ctf_type, result); + } + return result; } @@ -187,6 +199,7 @@ process_ctf_typedef(read_context *ctxt, static type_decl_sptr process_ctf_base_type(read_context *ctxt, corpus_sptr corp, + translation_unit_sptr tunit, ctf_dict_t *ctf_dictionary, ctf_id_t ctf_type) { @@ -226,6 +239,12 @@ process_ctf_base_type(read_context *ctxt, } + if (result) + { + add_decl_to_scope(result, tunit->get_global_scope()); + ctxt->add_type(ctf_type, result); + } + return result; } @@ -284,6 +303,9 @@ process_ctf_function_type(read_context *ctxt, function_parms.push_back(parm); } + result = dynamic_pointer_cast(ctxt->lookup_type(ctf_type)); + if (result) + return result; /* Ok now the function type itself. */ result.reset(new function_type(ret_type, @@ -291,8 +313,15 @@ process_ctf_function_type(read_context *ctxt, tunit->get_address_size(), ctf_type_align(ctf_dictionary, ctf_type))); - tunit->bind_function_type_life_time(result); - result->set_is_artificial(true); + if (result) + { + tunit->bind_function_type_life_time(result); + result->set_is_artificial(true); + decl_base_sptr function_type_decl = get_type_declaration(result); + add_decl_to_scope(function_type_decl, tunit->get_global_scope()); + ctxt->add_type(ctf_type, result); + } + return result; } @@ -394,6 +423,7 @@ process_ctf_struct_type(read_context *ctxt, refer to this struct, we have to make it available in the cache at this point even if the members haven't been added to the IR node yet. */ + add_decl_to_scope(result, tunit->get_global_scope()); ctxt->add_type(ctf_type, result); /* Now add the struct members as specified in the CTF type description. @@ -442,6 +472,7 @@ process_ctf_union_type(read_context *ctxt, refer to this union, we have to make it available in the cache at this point even if the members haven't been added to the IR node yet. */ + add_decl_to_scope(result, tunit->get_global_scope()); ctxt->add_type(ctf_type, result); /* Now add the union members as specified in the CTF type description. @@ -498,6 +529,10 @@ process_ctf_array_type(read_context *ctxt, if (!index_type) return result; + result = dynamic_pointer_cast(ctxt->lookup_type(ctf_type)); + if (result) + return result; + /* The number of elements of the array determines the IR subranges type to build. */ array_type_def::subranges_type subranges; @@ -529,6 +564,13 @@ process_ctf_array_type(read_context *ctxt, /* Finally build the IR for the array type and return it. */ result.reset(new array_type_def(element_type, subranges, location())); + if (result) + { + decl_base_sptr array_type_decl = get_type_declaration(result); + add_decl_to_scope(array_type_decl, tunit->get_global_scope()); + ctxt->add_type(ctf_type, result); + } + return result; } @@ -566,6 +608,14 @@ process_ctf_qualified_type(read_context *ctxt, ABG_ASSERT_NOT_REACHED; result.reset(new qualified_type_def(utype, qualifiers, location())); + + if (result) + { + decl_base_sptr qualified_type_decl = get_type_declaration(result); + add_decl_to_scope(qualified_type_decl, tunit->get_global_scope()); + ctxt->add_type(ctf_type, result); + } + return result; } @@ -597,10 +647,20 @@ process_ctf_pointer_type(read_context *ctxt, if (!target_type) return result; + result = dynamic_pointer_cast(ctxt->lookup_type(ctf_type)); + if (result) + return result; + result.reset(new pointer_type_def(target_type, ctf_type_size(ctf_dictionary, ctf_type) * 8, ctf_type_align(ctf_dictionary, ctf_type) * 8, location())); + if (result) + { + add_decl_to_scope(result, tunit->get_global_scope()); + ctxt->add_type(ctf_type, result); + } + return result; } @@ -657,6 +717,12 @@ process_ctf_enum_type(read_context *ctxt, const char *enum_name = ctf_type_name_raw(ctf_dictionary, ctf_type); result.reset(new enum_type_decl(enum_name, location(), utype, enms, enum_name)); + if (result) + { + add_decl_to_scope(result, tunit->get_global_scope()); + ctxt->add_type(ctf_type, result); + } + return result; } @@ -683,43 +749,31 @@ process_ctf_type(read_context *ctxt, int type_kind = ctf_type_kind(ctf_dictionary, ctf_type); type_base_sptr result; + if ((result = ctxt->lookup_type(ctf_type))) + return result; + switch (type_kind) { case CTF_K_INTEGER: case CTF_K_FLOAT: { type_decl_sptr type_decl - = process_ctf_base_type(ctxt, corp, ctf_dictionary, ctf_type); - - if (type_decl) - { - add_decl_to_scope(type_decl, tunit->get_global_scope()); - result = is_type(type_decl); - } + = process_ctf_base_type(ctxt, corp, tunit, ctf_dictionary, ctf_type); + result = is_type(type_decl); break; } case CTF_K_TYPEDEF: { typedef_decl_sptr typedef_decl = process_ctf_typedef(ctxt, corp, tunit, ctf_dictionary, ctf_type); - - if (typedef_decl) - { - add_decl_to_scope(typedef_decl, tunit->get_global_scope()); - result = is_type(typedef_decl); - } + result = is_type(typedef_decl); break; } case CTF_K_POINTER: { pointer_type_def_sptr pointer_type = process_ctf_pointer_type(ctxt, corp, tunit, ctf_dictionary, ctf_type); - - if (pointer_type) - { - add_decl_to_scope(pointer_type, tunit->get_global_scope()); - result = pointer_type; - } + result = pointer_type; break; } case CTF_K_CONST: @@ -728,79 +782,42 @@ process_ctf_type(read_context *ctxt, { type_base_sptr qualified_type = process_ctf_qualified_type(ctxt, corp, tunit, ctf_dictionary, ctf_type); - - if (qualified_type) - { - decl_base_sptr qualified_type_decl = get_type_declaration(qualified_type); - - add_decl_to_scope(qualified_type_decl, tunit->get_global_scope()); - result = qualified_type; - } + result = qualified_type; break; } case CTF_K_ARRAY: { array_type_def_sptr array_type = process_ctf_array_type(ctxt, corp, tunit, ctf_dictionary, ctf_type); - - if (array_type) - { - decl_base_sptr array_type_decl = get_type_declaration(array_type); - - add_decl_to_scope(array_type_decl, tunit->get_global_scope()); - result = array_type; - } + result = array_type; break; } case CTF_K_ENUM: { enum_type_decl_sptr enum_type = process_ctf_enum_type(ctxt, tunit, ctf_dictionary, ctf_type); - - if (enum_type) - { - add_decl_to_scope(enum_type, tunit->get_global_scope()); - result = enum_type; - } - + result = enum_type; break; } case CTF_K_FUNCTION: { function_type_sptr function_type = process_ctf_function_type(ctxt, corp, tunit, ctf_dictionary, ctf_type); - - if (function_type) - { - decl_base_sptr function_type_decl = get_type_declaration(function_type); - - add_decl_to_scope(function_type_decl, tunit->get_global_scope()); - result = function_type; - } + result = function_type; break; } case CTF_K_STRUCT: { class_decl_sptr struct_decl = process_ctf_struct_type(ctxt, corp, tunit, ctf_dictionary, ctf_type); - - if (struct_decl) - { - add_decl_to_scope(struct_decl, tunit->get_global_scope()); - result = is_type(struct_decl); - } + result = is_type(struct_decl); break; } case CTF_K_UNION: { union_decl_sptr union_decl = process_ctf_union_type(ctxt, corp, tunit, ctf_dictionary, ctf_type); - - if (union_decl) - { - add_decl_to_scope(union_decl, tunit->get_global_scope()); - result = is_type(union_decl); - } + result = is_type(union_decl); break; } case CTF_K_UNKNOWN: @@ -809,10 +826,8 @@ process_ctf_type(read_context *ctxt, break; } - if (result) - ctxt->add_type(ctf_type, result); - else - fprintf(stderr, "NOT PROCESSED TYPE %lu\n", ctf_type); + // if (!result) + // fprintf(stderr, "NOT PROCESSED TYPE %lu\n", ctf_type); return result; } diff --git a/tests/data/test-read-ctf/test-array-of-pointers.abi b/tests/data/test-read-ctf/test-array-of-pointers.abi new file mode 100644 index 00000000..920da28b --- /dev/null +++ b/tests/data/test-read-ctf/test-array-of-pointers.abi @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/data/test-read-ctf/test-array-of-pointers.c b/tests/data/test-read-ctf/test-array-of-pointers.c new file mode 100644 index 00000000..560ce354 --- /dev/null +++ b/tests/data/test-read-ctf/test-array-of-pointers.c @@ -0,0 +1,20 @@ +/* Test a array of pointer definition as struct member + * gcc -gctf -c test-array-of-pointers.c -o test-array-of-pointer.o + */ +struct task { + struct css_set *s; +}; + +struct state { + struct cgroup *cg; +}; + +struct css_set { + struct state *s0[4]; +}; + +struct cgroup { + struct state *s1[4]; +}; + +struct task *t; diff --git a/tests/data/test-read-ctf/test-callback.abi b/tests/data/test-read-ctf/test-callback.abi new file mode 100644 index 00000000..704c4971 --- /dev/null +++ b/tests/data/test-read-ctf/test-callback.abi @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/data/test-read-ctf/test-callback.c b/tests/data/test-read-ctf/test-callback.c new file mode 100644 index 00000000..a5fc7df2 --- /dev/null +++ b/tests/data/test-read-ctf/test-callback.c @@ -0,0 +1,16 @@ +/* Test a simple callback as a struct member + * gcc -gctf -c test-callback.c -o test-callback.o + */ +struct test { + void (*fn1)(int, long); +}; + +void f2(int a, long b) +{ +} + +void assign() +{ + struct test *tt; + tt->fn1 = f2; +} diff --git a/tests/data/test-read-ctf/test-callback2.abi b/tests/data/test-read-ctf/test-callback2.abi new file mode 100644 index 00000000..bdd4ad33 --- /dev/null +++ b/tests/data/test-read-ctf/test-callback2.abi @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/tests/data/test-read-ctf/test-callback2.c b/tests/data/test-read-ctf/test-callback2.c new file mode 100644 index 00000000..6d33780f --- /dev/null +++ b/tests/data/test-read-ctf/test-callback2.c @@ -0,0 +1,11 @@ +/* Test a simple callback as a struct member + * that takes a pointer to a struct parent as + * argument + * gcc -gctf -c test-callback2.c -o test-callback2.o + */ +struct s0 +{ + int (*mem_fun)(struct s0 *); +}; + +struct s0 *s0; diff --git a/tests/data/test-read-ctf/test-forward-type-decl.abi b/tests/data/test-read-ctf/test-forward-type-decl.abi new file mode 100644 index 00000000..21bb45c8 --- /dev/null +++ b/tests/data/test-read-ctf/test-forward-type-decl.abi @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/data/test-read-ctf/test-forward-type-decl.c b/tests/data/test-read-ctf/test-forward-type-decl.c new file mode 100644 index 00000000..742b24ad --- /dev/null +++ b/tests/data/test-read-ctf/test-forward-type-decl.c @@ -0,0 +1,23 @@ +/* Test a forward type declaration as a struct member + * to exercise circular dependencies. + * gcc -gctf -c test-forward-type-decl.c -o \ + * test-forward-type-decl.o + */ +typedef struct page *page_t; + +struct rb_node { + struct rb_node *rb_left; +}; + +struct address_space; + +struct page { + struct address_space *mapping; +}; + +struct address_space { + struct rb_node *rb_root; + struct page *page; +}; + +struct address_space *addr; diff --git a/tests/data/test-read-ctf/test-functions-declaration.abi b/tests/data/test-read-ctf/test-functions-declaration.abi new file mode 100644 index 00000000..9ef05e44 --- /dev/null +++ b/tests/data/test-read-ctf/test-functions-declaration.abi @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/data/test-read-ctf/test-functions-declaration.c b/tests/data/test-read-ctf/test-functions-declaration.c new file mode 100644 index 00000000..36776bbb --- /dev/null +++ b/tests/data/test-read-ctf/test-functions-declaration.c @@ -0,0 +1,16 @@ +/* Test declaring twice a function pinter a struct member + * gcc -gctf -c test-functions-declaration.c -o \ + * test-functions-declaration.o + */ +void +attribute_container_add_device( + void (*fn1)(int, long)) +{ +} + +void +attribute_container_device_trigger( + void (*fn2)(int , long)) +{ + +} diff --git a/tests/data/test-read-ctf/test-list-struct.abi b/tests/data/test-read-ctf/test-list-struct.abi new file mode 100644 index 00000000..196a79a1 --- /dev/null +++ b/tests/data/test-read-ctf/test-list-struct.abi @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/tests/data/test-read-ctf/test-list-struct.c b/tests/data/test-read-ctf/test-list-struct.c new file mode 100644 index 00000000..a1161923 --- /dev/null +++ b/tests/data/test-read-ctf/test-list-struct.c @@ -0,0 +1,11 @@ +/* Test a ADT where a struct member is a pointer to + * itself. + * gcc -gctf -c test-callback.c -o test-callback.o + */ + +struct rb_node_b { + struct rb_node_b *this; + int a; +}; + +struct rb_node_b n1, n2; diff --git a/tests/test-read-ctf.cc b/tests/test-read-ctf.cc index b448ddc5..7f5381f6 100644 --- a/tests/test-read-ctf.cc +++ b/tests/test-read-ctf.cc @@ -220,6 +220,54 @@ static InOutSpec in_out_specs[] = "data/test-read-ctf/PR27700/test-PR27700.abi", "output/test-read-ctf/PR27700/test-PR27700.abi", }, + { + "data/test-read-ctf/test-callback.o", + "", + "", + SEQUENCE_TYPE_ID_STYLE, + "data/test-read-ctf/test-callback.abi", + "output/test-read-ctf/test-callback.abi", + }, + { + "data/test-read-ctf/test-array-of-pointers.o", + "", + "", + SEQUENCE_TYPE_ID_STYLE, + "data/test-read-ctf/test-array-of-pointers.abi", + "output/test-read-ctf/test-array-of-pointers.abi", + }, + { + "data/test-read-ctf/test-functions-declaration.o", + "", + "", + SEQUENCE_TYPE_ID_STYLE, + "data/test-read-ctf/test-functions-declaration.abi", + "output/test-read-ctf/test-functions-declaration.abi", + }, + { + "data/test-read-ctf/test-forward-type-decl.o", + "", + "", + SEQUENCE_TYPE_ID_STYLE, + "data/test-read-ctf/test-forward-type-decl.abi", + "output/test-read-ctf/test-forward-type-decl.abi", + }, + { + "data/test-read-ctf/test-list-struct.o", + "", + "", + SEQUENCE_TYPE_ID_STYLE, + "data/test-read-ctf/test-list-struct.abi", + "output/test-read-ctf/test-list-struct.abi", + }, + { + "data/test-read-ctf/test-callback2.o", + "", + "", + SEQUENCE_TYPE_ID_STYLE, + "data/test-read-ctf/test-callback2.abi", + "output/test-read-ctf/test-callback2.abi", + }, // This should be the last entry. {NULL, NULL, NULL, SEQUENCE_TYPE_ID_STYLE, NULL, NULL} };