From patchwork Thu Mar 23 10:56:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luis Machado X-Patchwork-Id: 66793 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 992A1384D1AA for ; Thu, 23 Mar 2023 10:58:49 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 992A1384D1AA DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1679569129; bh=75Czw2BHyAj44OMkZnDkhxIrooLjnXMHztC6Q8GLm2s=; h=To:CC:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=uwG80ecm9AxZg4wqEhw0xI0ecsb0tK6Fb4jcRV8uNHgf+9JcVAPn0whN5JXr0fB4b 24mIkyBfWIffNNktScjLNAG2QAanBRR2cPXNsu/jy2JQlpJsjIVlkBj3+0zisZw+wJ 3zaVTzp7I9L7dAVJ0mN6eiOum0rVm/fG0PuHQM2k= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from EUR04-DB3-obe.outbound.protection.outlook.com (mail-db3eur04on2088.outbound.protection.outlook.com [40.107.6.88]) by sourceware.org (Postfix) with ESMTPS id AB8E1384F032 for ; Thu, 23 Mar 2023 10:58:21 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org AB8E1384F032 Received: from DU2PR04CA0259.eurprd04.prod.outlook.com (2603:10a6:10:28e::24) by PR3PR08MB5596.eurprd08.prod.outlook.com (2603:10a6:102:88::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6178.38; Thu, 23 Mar 2023 10:58:11 +0000 Received: from DBAEUR03FT005.eop-EUR03.prod.protection.outlook.com (2603:10a6:10:28e:cafe::50) by DU2PR04CA0259.outlook.office365.com (2603:10a6:10:28e::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6178.38 via Frontend Transport; Thu, 23 Mar 2023 10:58:11 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 63.35.35.123) smtp.mailfrom=arm.com; dkim=pass (signature was verified) header.d=armh.onmicrosoft.com;dmarc=pass action=none header.from=arm.com; Received-SPF: Pass (protection.outlook.com: domain of arm.com designates 63.35.35.123 as permitted sender) receiver=protection.outlook.com; client-ip=63.35.35.123; helo=64aa7808-outbound-1.mta.getcheckrecipient.com; pr=C Received: from 64aa7808-outbound-1.mta.getcheckrecipient.com (63.35.35.123) by DBAEUR03FT005.mail.protection.outlook.com (100.127.142.81) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6222.17 via Frontend Transport; Thu, 23 Mar 2023 10:58:11 +0000 Received: ("Tessian outbound 2ba0ed2ebb9f:v135"); Thu, 23 Mar 2023 10:58:11 +0000 X-CheckRecipientChecked: true X-CR-MTA-CID: eacf02692ff56ec8 X-CR-MTA-TID: 64aa7808 Received: from 7c654c281342.1 by 64aa7808-outbound-1.mta.getcheckrecipient.com id 7903C1EF-5657-414F-83D2-5B978621A12D.1; Thu, 23 Mar 2023 10:56:33 +0000 Received: from EUR04-VI1-obe.outbound.protection.outlook.com by 64aa7808-outbound-1.mta.getcheckrecipient.com with ESMTPS id 7c654c281342.1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384); Thu, 23 Mar 2023 10:56:33 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=iRC22uPLyb67DnCvHxwbdJSr9cgEg8YCMx5midpLykJNrU9zzsRbdqZo7it1mT3sObIML64ZXijfusB088A1h3MXYNc7z5laoisRinDFdiYzJRATelItO1pL9ZOYtDaeaUErF5ArsqwDx4jiW2AhJ55tXEaBz5QoF9uIfk7YdJtQZG00oBIgOPtNIfeWnkn/IVjBUK/JCFSzQPVYzGbkJxUg0v9Yis5IRNJpd+YU1ucGgwCwsrCF9XcbDOy9ScF26T+/8RTl3m8YopbSw+G/7DBXJpGBNctIkTpbDwA11ODpllEgx+wKTYgQWBpx4Xu1/52xiNMN4BJkLivcE5om/w== 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=75Czw2BHyAj44OMkZnDkhxIrooLjnXMHztC6Q8GLm2s=; b=nh+D+NHvAMwsZfJII9hCVery7lidlFRNgHHauRUTSWkt/ps1FJW7HUT6bB0tkX7ZEdB0ySTVUbNpNZL1rmCiRPUq60EDLQLlAXwCZyPqAQMJuWU2Ad7ZbYyT/udwZrGtsnxTj5M/JndGgl1Dl6UANiV5HA0IM9yoZdHZofhjv9u5NhdXpxo4dYnT9c96xatFzK+/tbNSPc0qWW2gbAS0rqQ4l7n9B+9hjV6QW63wBnsnyF1Sh/OoQRmoHwca83fUTt8R20abM5o86mcDhHxq3Q1wbhAswv3JnUPYmJUz8z6YJ4N+czki5bMEvsZ6a3wnvsJfFDHthqFdTauz3QksFw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 40.67.248.234) smtp.rcpttodomain=sourceware.org smtp.mailfrom=arm.com; dmarc=pass (p=none sp=none pct=100) action=none header.from=arm.com; dkim=none (message not signed); arc=none Received: from DUZPR01CA0317.eurprd01.prod.exchangelabs.com (2603:10a6:10:4ba::27) by AS2PR08MB9367.eurprd08.prod.outlook.com (2603:10a6:20b:595::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6178.38; Thu, 23 Mar 2023 10:56:30 +0000 Received: from DBAEUR03FT051.eop-EUR03.prod.protection.outlook.com (2603:10a6:10:4ba:cafe::21) by DUZPR01CA0317.outlook.office365.com (2603:10a6:10:4ba::27) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6178.38 via Frontend Transport; Thu, 23 Mar 2023 10:56:30 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 40.67.248.234) smtp.mailfrom=arm.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=arm.com; Received-SPF: Pass (protection.outlook.com: domain of arm.com designates 40.67.248.234 as permitted sender) receiver=protection.outlook.com; client-ip=40.67.248.234; helo=nebula.arm.com; pr=C Received: from nebula.arm.com (40.67.248.234) by DBAEUR03FT051.mail.protection.outlook.com (100.127.142.148) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6222.21 via Frontend Transport; Thu, 23 Mar 2023 10:56:30 +0000 Received: from AZ-NEU-EX04.Arm.com (10.251.24.32) by AZ-NEU-EX03.Arm.com (10.251.24.31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.17; Thu, 23 Mar 2023 10:56:30 +0000 Received: from e129171.cambridge.arm.com (10.1.30.48) by mail.arm.com (10.251.24.32) with Microsoft SMTP Server id 15.1.2507.17 via Frontend Transport; Thu, 23 Mar 2023 10:56:30 +0000 To: , , , CC: Subject: [PATCH, v2] aarch64: Check for valid inferior thread/regcache before reading pauth registers Date: Thu, 23 Mar 2023 10:56:19 +0000 Message-ID: <20230323105619.9151-1-luis.machado@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230316103904.1947447-1-luis.machado@arm.com> References: <20230316103904.1947447-1-luis.machado@arm.com> MIME-Version: 1.0 X-EOPAttributedMessage: 1 X-MS-TrafficTypeDiagnostic: DBAEUR03FT051:EE_|AS2PR08MB9367:EE_|DBAEUR03FT005:EE_|PR3PR08MB5596:EE_ X-MS-Office365-Filtering-Correlation-Id: e95c37fd-bfaf-41d9-fb67-08db2b8d7e5d x-checkrecipientrouted: true NoDisclaimer: true X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam-Untrusted: BCL:0; X-Microsoft-Antispam-Message-Info-Original: VcHAKiCQUi85bFcXXlJfys8CtuOjRrXfDZEHNbvh7fiejuU45ePkYyGWu32PCvkT4x6+eyXml4+Vn630jMYzWvhp9mh/0yYRIlTlBajDRSal3Oe4Htj6c3vY43HU/tJlqZvKE2zr2/Zs1gUML2TG0bfUbrnNA/8AIYZloYx284IT2XeRTAHIbDL9bCvuqnDMSm/f+8l8WkrI+ZUc0zVLEt5Bg5EICsRiR8FtxxL/cQKwYCVceezitOzCtV/YJ3kg1XN1649nfWm4iHKZqMqlW8vhm/0b33ADxCZSMInGj9K74hnV/mCmcTh4Y9ztc4/pGqKvnEV07zuJn0qJrz8tRTJN1kXv6OGowPddZRawfQ9rJzYlajJPiirpxxmTAWkHRtyaZPOckZu4brZtNFDJXRS/n/PXDuugHPzYAPg7OfgG/xi4eAMHu8HSWW6O7bR06+jFT6OKSEdbUXIKXQua9pJx39fd9RclRFmxgsD1vl0fUJcAera+qonGEhoHCrVoDhMNW0gClF2xUdxuVYZ9npPOyULOZZ+9FzUiDKhL8ozUKbQz4VIboq7TrPn9XaYhhEJpD3OJ4Yy2o+H2qRJMMs8uGzsb7aYC8k7eOWRXKoyWd2pSFfeS8F/P/XCvE62Ie7VoQsFcSHA84bw6RX4XYDDJpATKoOyF/xHfswqoioJ/GexB4QM+Q8V64DH/bJEFHButvYf68lBeFgKQCtjlKkH7jv7vi/FqJDbxRDe9/BhVefS8E4gUCiH4VADqHVoa67b1FFbM3U0Ax1ke63v10w== X-Forefront-Antispam-Report-Untrusted: CIP:40.67.248.234; CTRY:IE; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:nebula.arm.com; PTR:InfoDomainNonexistent; CAT:NONE; SFS:(13230025)(4636009)(396003)(346002)(376002)(136003)(39860400002)(451199018)(36840700001)(40470700004)(46966006)(356005)(40480700001)(2906002)(84970400001)(40460700003)(83380400001)(478600001)(2616005)(336012)(186003)(7696005)(86362001)(82310400005)(36756003)(316002)(19627235002)(110136005)(70206006)(8676002)(36860700001)(30864003)(4326008)(70586007)(8936002)(426003)(47076005)(6666004)(1076003)(26005)(44832011)(81166007)(5660300002)(41300700001)(82740400003)(36900700001); DIR:OUT; SFP:1101; X-MS-Exchange-Transport-CrossTenantHeadersStamped: AS2PR08MB9367 X-MS-Exchange-Transport-CrossTenantHeadersStripped: DBAEUR03FT005.eop-EUR03.prod.protection.outlook.com X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id-Prvs: 0f922746-6a5b-494e-3f97-08db2b8d4263 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: OmZ3Y+RbkiAjwzVHkr16xdMvGgBBB92CH8jvQ4E23llFdB08P34w8Rx6vMhkqtSyHa6FDvC1QtbpsytBcg2GpcT5Fcujnf16C78+Gd4sj8W2cG4/+3kiv8sbT6qKU9ILCCxgOmXY4t+J8KfuDjh5Yz8wMn/Oj9n8zmsZrNdRux8kuHreLDH5q3hcmG3KFNE3LJub3axr1ser7q1Wo0hRh2KbYJ3YgYXKK1qCFCPPQpOAxVZZYyG+4DHz4FbO1VEz9S1l18qZSMxY+SsjvFtum/b7wHeeFCfp+uRDHtwgHGAMt7QmVblT1uErpWSzdPth14f65VHSZz0dAZS5QYM3o4VPmoE9v54j8PUMxq7rhojO2zccWlCUeqL2x4bcagZx82oAFr2NFNFJosV1hvm9icHVjD566CbYpGkOiZgWZoCelkvpmMmKFx/3NomwtcUrcweFdfxEq8Z9lj/DGVNFAntbJsjW9kfzOIggVBIaXBiPnlkK0prYTsfMyR5H/6hGqz6M4WDlGoWolGKwBVw0xsi3Q00QwHUEq/l9SC8UGEHSKQNtTzWICaHJIZ+7OzWkNjkO7ILFR0SUal7Ovamic/0s/W/BA9x787QgL20MtzOE9PVBbIloo4gdKt/d88eRfMNcbqHKznLo1zA+SiiEmUyQ+1MiVV1Jm+ODuh0LfmPs+wTfgD/MPU1PYonLkr8tUn1JcJvu/jO8x98AV2uttK0Z666TXGgkIyH2YxmVkYA= X-Forefront-Antispam-Report: CIP:63.35.35.123; CTRY:IE; LANG:en; SCL:1; SRV:; IPV:CAL; SFV:NSPM; H:64aa7808-outbound-1.mta.getcheckrecipient.com; PTR:ec2-63-35-35-123.eu-west-1.compute.amazonaws.com; CAT:NONE; SFS:(13230025)(4636009)(136003)(376002)(396003)(346002)(39860400002)(451199018)(36840700001)(46966006)(40470700004)(41300700001)(40460700003)(83380400001)(7696005)(36860700001)(2906002)(19627235002)(5660300002)(110136005)(82740400003)(86362001)(81166007)(44832011)(30864003)(40480700001)(478600001)(8936002)(26005)(316002)(1076003)(82310400005)(107886003)(6666004)(36756003)(2616005)(4326008)(8676002)(70586007)(336012)(70206006)(186003)(426003)(47076005)(84970400001); DIR:OUT; SFP:1101; X-OriginatorOrg: arm.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 23 Mar 2023 10:58:11.3372 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: e95c37fd-bfaf-41d9-fb67-08db2b8d7e5d X-MS-Exchange-CrossTenant-Id: f34e5979-57d9-4aaa-ad4d-b122a662184d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=f34e5979-57d9-4aaa-ad4d-b122a662184d; Ip=[63.35.35.123]; Helo=[64aa7808-outbound-1.mta.getcheckrecipient.com] X-MS-Exchange-CrossTenant-AuthSource: DBAEUR03FT005.eop-EUR03.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PR3PR08MB5596 X-Spam-Status: No, score=-12.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, FORGED_SPF_HELO, GIT_PATCH_0, KAM_DMARC_NONE, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_PASS, SPF_NONE, TXREP, UNPARSEABLE_RELAY, WEIRD_PORT 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: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Luis Machado via Gdb-patches From: Luis Machado Reply-To: Luis Machado Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" Changes in v2: - Dropped helper functions and used inferior_ptid/current_inferior () instead. Changes are now aarch64-specific. There were reports of gdb throwing internal errors when calling inferior_thread ()/get_current_regcache () on a system with Pointer Authentication enabled. In such cases, gdb produces the following backtrace: ../../../repos/binutils-gdb/gdb/thread.c:86: internal-error: inferior_thread: Assertion `current_thread_ != nullptr' failed. A problem internal to GDB has been detected, further debugging may prove unreliable. ----- Backtrace ----- 0xaaaae04a571f gdb_internal_backtrace_1 ../../../repos/binutils-gdb/gdb/bt-utils.c:122 0xaaaae04a57f3 _Z22gdb_internal_backtracev ../../../repos/binutils-gdb/gdb/bt-utils.c:168 0xaaaae0b52ccf internal_vproblem ../../../repos/binutils-gdb/gdb/utils.c:401 0xaaaae0b5310b _Z15internal_verrorPKciS0_St9__va_list ../../../repos/binutils-gdb/gdb/utils.c:481 0xaaaae0e24b8f _Z18internal_error_locPKciS0_z ../../../repos/binutils-gdb/gdbsupport/errors.cc:58 0xaaaae0a88983 _Z15inferior_threadv ../../../repos/binutils-gdb/gdb/thread.c:86 0xaaaae0956c87 _Z20get_current_regcachev ../../../repos/binutils-gdb/gdb/regcache.c:428 0xaaaae035223f aarch64_remove_non_address_bits ../../../repos/binutils-gdb/gdb/aarch64-tdep.c:3572 0xaaaae03e8abb _Z31gdbarch_remove_non_address_bitsP7gdbarchm ../../../repos/binutils-gdb/gdb/gdbarch.c:3109 0xaaaae0a692d7 memory_xfer_partial ../../../repos/binutils-gdb/gdb/target.c:1620 0xaaaae0a695e3 _Z19target_xfer_partialP10target_ops13target_objectPKcPhPKhmmPm ../../../repos/binutils-gdb/gdb/target.c:1684 0xaaaae0a69e9f target_read_partial ../../../repos/binutils-gdb/gdb/target.c:1937 0xaaaae0a69fdf _Z11target_readP10target_ops13target_objectPKcPhml ../../../repos/binutils-gdb/gdb/target.c:1977 0xaaaae0a69937 _Z18target_read_memorymPhl ../../../repos/binutils-gdb/gdb/target.c:1773 0xaaaae08be523 ps_xfer_memory ../../../repos/binutils-gdb/gdb/proc-service.c:90 0xaaaae08be6db ps_pdread ../../../repos/binutils-gdb/gdb/proc-service.c:124 0x40001ed7c3b3 _td_fetch_value /build/glibc-RIFKjK/glibc-2.31/nptl_db/fetch-value.c:115 0x40001ed791ef td_ta_map_lwp2thr /build/glibc-RIFKjK/glibc-2.31/nptl_db/td_ta_map_lwp2thr.c:194 0xaaaae07f4473 thread_from_lwp ../../../repos/binutils-gdb/gdb/linux-thread-db.c:413 0xaaaae07f6d6f _ZN16thread_db_target4waitE6ptid_tP17target_waitstatus10enum_flagsI16target_wait_flagE ../../../repos/binutils-gdb/gdb/linux-thread-db.c:1420 0xaaaae0a6b33b _Z11target_wait6ptid_tP17target_waitstatus10enum_flagsI16target_wait_flagE ../../../repos/binutils-gdb/gdb/target.c:2586 0xaaaae0789cf7 do_target_wait_1 ../../../repos/binutils-gdb/gdb/infrun.c:3825 0xaaaae0789e6f operator() ../../../repos/binutils-gdb/gdb/infrun.c:3884 0xaaaae078a167 do_target_wait ../../../repos/binutils-gdb/gdb/infrun.c:3903 0xaaaae078b0af _Z20fetch_inferior_eventv ../../../repos/binutils-gdb/gdb/infrun.c:4314 0xaaaae076652f _Z22inferior_event_handler19inferior_event_type ../../../repos/binutils-gdb/gdb/inf-loop.c:41 0xaaaae07dc68b handle_target_event ../../../repos/binutils-gdb/gdb/linux-nat.c:4206 0xaaaae0e25fbb handle_file_event ../../../repos/binutils-gdb/gdbsupport/event-loop.cc:573 0xaaaae0e264f3 gdb_wait_for_event ../../../repos/binutils-gdb/gdbsupport/event-loop.cc:694 0xaaaae0e24f9b _Z16gdb_do_one_eventi ../../../repos/binutils-gdb/gdbsupport/event-loop.cc:217 0xaaaae080f033 start_event_loop ../../../repos/binutils-gdb/gdb/main.c:411 0xaaaae080f1b7 captured_command_loop ../../../repos/binutils-gdb/gdb/main.c:475 0xaaaae0810b97 captured_main ../../../repos/binutils-gdb/gdb/main.c:1318 0xaaaae0810c1b _Z8gdb_mainP18captured_main_args ../../../repos/binutils-gdb/gdb/main.c:1337 0xaaaae0338453 main ../../../repos/binutils-gdb/gdb/gdb.c:32 --------------------- ../../../repos/binutils-gdb/gdb/thread.c:86: internal-error: inferior_thread: Assertion `current_thread_ != nullptr' failed. A problem internal to GDB has been detected, further debugging may prove unreliable. Quit this debugging session? (y or n) We also see failures across the testsuite if the tests get executed on a target that has native support for the pointer authentication feature. But gdb.base/break.exp and gdb.base/access-mem-running.exp are two examples of tests that run into errors and internal errors. This issue started after commit d88cb738e6a7a7179dfaff8af78d69250c852af1, which enabled more broad use of pointer authentication masks to remove non-address bits of pointers, but wasn't immediately detected because systems with native support for pointer authentication are not that common yet. The above crash happens because gdb is in the middle of handling an event, and do_target_wait_1 calls switch_to_inferior_no_thread, nullifying the current thread. This means a call to inferior_thread () will assert, and attempting to call get_current_regcache () will also call inferior_thread (), resulting in an assertion as well. target_has_registers was one function that seemed useful for detecting these types of situation where we don't have a register cache. The problem with that is the inconsistent state of inferior_ptid, which is used by target_has_registers. Despite the call to switch_to_no_thread in switch_to_inferior_no_thread from do_target_wait_1 in the backtrace above clearing inferior_ptid, the call to ps_xfer_memory sets inferior_ptid momentarily before reading memory: static ps_err_e ps_xfer_memory (const struct ps_prochandle *ph, psaddr_t addr, gdb_byte *buf, size_t len, int write) { scoped_restore_current_inferior restore_inferior; set_current_inferior (ph->thread->inf); scoped_restore_current_program_space restore_current_progspace; set_current_program_space (ph->thread->inf->pspace); scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid); inferior_ptid = ph->thread->ptid; CORE_ADDR core_addr = ps_addr_to_core_addr (addr); int ret; if (write) ret = target_write_memory (core_addr, buf, len); else ret = target_read_memory (core_addr, buf, len); return (ret == 0 ? PS_OK : PS_ERR); } Maybe this shouldn't happen, or maybe it is just an unfortunate state to be in. But this prevents the use of target_has_registers to guard against the lack of registers, since, although current_thread_ is still nullptr, inferior_ptid is valid and is not null_ptid. There is another crash scenario after we kill a previously active inferior, in which case the gdbarch will still say we support pointer authentication but we will also have no current thread (inferior_thread () will assert etc). If the target has support for pointer authentication, gdb needs to use a couple (or 4, for bare-metal) mask registers to mask off some bits of pointers, and for that it needs to access the registers. At some points, like the one from the backtrace above, there is no active thread/current regcache because gdb is in the middle of doing event handling and switching between threads. Simon suggested the use of inferior_ptid to fetch the register cache, as opposed to relying on the current register cache. Though we need to make sure inferior_ptid is valid (not null_ptid), I think this works nicely. With inferior_ptid, we can do safety checks along the way, making sure we have a thread to fetch a register cache from and checking if the thread is actually stopped or running. The following patch implements this idea with safety checks to make sure we don't run into assertions or errors. If any of the checks fail, we fallback to using a default mask to remove non-address bits of a pointer. I discussed with Pedro the possibility of caching the mask register values (which are per-process and can change mid-execution), but there isn't a good spot to cache those values. Besides, the mask registers can change constantly for bare-metal debugging when switching between exception levels. In some cases, it is just not possible to get access to these mask registers, like the case where threads are running. In those cases, using a default mask to remove the non-address bits should be enough. This can happen when we let threads run in the background and then we attempt to access a memory address (now that gdb is capable of reading memory even with threads running). Thus gdb will attempt to remove non-address bits of that memory access, will attempt to access registers, running into errors. Regression-tested on aarch64-linux Ubuntu 20.04. --- gdb/aarch64-tdep.c | 88 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 62 insertions(+), 26 deletions(-) diff --git a/gdb/aarch64-tdep.c b/gdb/aarch64-tdep.c index 5b1b9921f87..d11d8320799 100644 --- a/gdb/aarch64-tdep.c +++ b/gdb/aarch64-tdep.c @@ -55,6 +55,9 @@ #include #include +/* For inferior_ptid and current_inferior (). */ +#include "inferior.h" + /* A Homogeneous Floating-Point or Short-Vector Aggregate may have at most four members. */ #define HA_MAX_NUM_FLDS 4 @@ -3556,40 +3559,73 @@ aarch64_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc) static CORE_ADDR aarch64_remove_non_address_bits (struct gdbarch *gdbarch, CORE_ADDR pointer) { - aarch64_gdbarch_tdep *tdep = gdbarch_tdep (gdbarch); - /* By default, we assume TBI and discard the top 8 bits plus the VA range - select bit (55). */ + select bit (55). Below we try to fetch information about pointer + authentication masks in order to make non-address removal more + precise. */ CORE_ADDR mask = AARCH64_TOP_BITS_MASK; - if (tdep->has_pauth ()) + /* Check if we have an inferior first. If not, just use the default + mask. + + We use the inferior_ptid here because the pointer authentication masks + should be the same across threads of a process. Since we may not have + access to the current thread (gdb may have switched to no inferiors + momentarily), we use the inferior ptid. */ + if (inferior_ptid != null_ptid) { - /* Fetch the PAC masks. These masks are per-process, so we can just - fetch data from whatever thread we have at the moment. - - Also, we have both a code mask and a data mask. For now they are the - same, but this may change in the future. */ - struct regcache *regs = get_current_regcache (); - CORE_ADDR cmask, dmask; - int dmask_regnum = AARCH64_PAUTH_DMASK_REGNUM (tdep->pauth_reg_base); - int cmask_regnum = AARCH64_PAUTH_CMASK_REGNUM (tdep->pauth_reg_base); - - /* If we have a kernel address and we have kernel-mode address mask - registers, use those instead. */ - if (tdep->pauth_reg_count > 2 - && pointer & VA_RANGE_SELECT_BIT_MASK) + /* If we do have an inferior, attempt to fetch its thread's thread_info + struct. */ + thread_info *thread + = find_thread_ptid (current_inferior ()->process_target (), + inferior_ptid); + + /* If the thread is running, we will not be able to fetch the mask + registers. */ + if (thread != nullptr && thread->state != THREAD_RUNNING) { - dmask_regnum = AARCH64_PAUTH_DMASK_HIGH_REGNUM (tdep->pauth_reg_base); - cmask_regnum = AARCH64_PAUTH_CMASK_HIGH_REGNUM (tdep->pauth_reg_base); - } + /* Otherwise, fetch the register cache and the masks. */ + struct regcache *regs + = get_thread_regcache (current_inferior ()->process_target (), + inferior_ptid); + + /* Use the gdbarch from the register cache to check for pointer + authentication support, as it matches the features found in + that particular thread. */ + aarch64_gdbarch_tdep *tdep + = gdbarch_tdep (regs->arch ()); - if (regs->cooked_read (dmask_regnum, &dmask) != REG_VALID) - dmask = mask; + /* Is there pointer authentication support? */ + if (tdep->has_pauth ()) + { + CORE_ADDR cmask, dmask; + int dmask_regnum + = AARCH64_PAUTH_DMASK_REGNUM (tdep->pauth_reg_base); + int cmask_regnum + = AARCH64_PAUTH_CMASK_REGNUM (tdep->pauth_reg_base); + + /* If we have a kernel address and we have kernel-mode address + mask registers, use those instead. */ + if (tdep->pauth_reg_count > 2 + && pointer & VA_RANGE_SELECT_BIT_MASK) + { + dmask_regnum + = AARCH64_PAUTH_DMASK_HIGH_REGNUM (tdep->pauth_reg_base); + cmask_regnum + = AARCH64_PAUTH_CMASK_HIGH_REGNUM (tdep->pauth_reg_base); + } - if (regs->cooked_read (cmask_regnum, &cmask) != REG_VALID) - cmask = mask; + /* We have both a code mask and a data mask. For now they are + the same, but this may change in the future. */ + if (regs->cooked_read (dmask_regnum, &dmask) != REG_VALID) + dmask = mask; - mask |= aarch64_mask_from_pac_registers (cmask, dmask); + if (regs->cooked_read (cmask_regnum, &cmask) != REG_VALID) + cmask = mask; + + mask |= aarch64_mask_from_pac_registers (cmask, dmask); + } + } } return aarch64_remove_top_bits (pointer, mask);