From patchwork Fri Sep 29 08:15:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luis Machado X-Patchwork-Id: 76873 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 73AEE386192A for ; Fri, 29 Sep 2023 08:15:54 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 73AEE386192A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1695975354; bh=VOuIY9zzvKzGW+YhvPkI3pEwq0EgG4Af/LkO841WyVk=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=kway5yE5c/S8AA3snQglPdf8GyZCTbaC6ckaL9pCO3S4Antn6N93FkGAdYTccluc1 PtWk1IW07gMnYopOD90CMuGz+XXr5i/fFbB9JGpWs6z1cDCBc6E4RD8n9l21ErT1q8 1WEKTZ+LQK6v9prNsA9C+6RasUk39X2f6g9n1KIc= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from EUR05-DB8-obe.outbound.protection.outlook.com (mail-db8eur05on2084.outbound.protection.outlook.com [40.107.20.84]) by sourceware.org (Postfix) with ESMTPS id 1D230387103A for ; Fri, 29 Sep 2023 08:15:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1D230387103A Received: from AS9PR06CA0432.eurprd06.prod.outlook.com (2603:10a6:20b:49e::28) by PA4PR08MB6238.eurprd08.prod.outlook.com (2603:10a6:102:e8::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6838.25; Fri, 29 Sep 2023 08:15:20 +0000 Received: from AM7EUR03FT032.eop-EUR03.prod.protection.outlook.com (2603:10a6:20b:49e:cafe::15) by AS9PR06CA0432.outlook.office365.com (2603:10a6:20b:49e::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6838.26 via Frontend Transport; Fri, 29 Sep 2023 08:15:19 +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 AM7EUR03FT032.mail.protection.outlook.com (100.127.140.65) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6838.19 via Frontend Transport; Fri, 29 Sep 2023 08:15:19 +0000 Received: ("Tessian outbound 9aeaca65ec26:v211"); Fri, 29 Sep 2023 08:15:19 +0000 X-CheckRecipientChecked: true X-CR-MTA-CID: d19a3444df290824 X-CR-MTA-TID: 64aa7808 Received: from 957bd6d8a67c.1 by 64aa7808-outbound-1.mta.getcheckrecipient.com id 79FA2322-720F-4117-AD18-BD2FEE6FA38F.1; Fri, 29 Sep 2023 08:15:12 +0000 Received: from EUR04-DB3-obe.outbound.protection.outlook.com by 64aa7808-outbound-1.mta.getcheckrecipient.com with ESMTPS id 957bd6d8a67c.1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384); Fri, 29 Sep 2023 08:15:12 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=CHhgcgEKI9G5LLMsyUVOklrJ66yQnr9hdlxMUtEJa4JSr2qyp1JOBxgBiX7ddtZV7ROKrY4/EY1+fjXWeoXo6N2tewmVraRTUk6LYQRoaTt54qTVkTRjsFnGcigcQ0jBaFNVYgRT9Xcr5rgzyerW5Tb++sUOh+5fg+RRjE03f6TLjQzhF5LUrJpA4HxI9C/xmrWb15/U8dpP8GdboJPw6Y5uEA4mJsYqwgUA2SQGZUhowoXdC0pj4QuI2AaHqqiZQyDyNqPStBZI8wymBH3SYhzTtnGcFj6hDUEjjAevYVFA4+V49coOybFqoMYaem8PzEWTMmuBTJUOMg93be29KA== 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=VOuIY9zzvKzGW+YhvPkI3pEwq0EgG4Af/LkO841WyVk=; b=igD3Cko8fLovMoM0yHpMX+nWo9FJ4o8EYtBzyAa3keMAW/RQd+VsmU0WUqEsL/3JuqGEs/UVDQgc+kmCyjS4Tarq/h5P3IuJhiIJW8ly36qfnahDZDrt3xDdKNRVV53+Bmzsi2Vtmf89UyJWFPZVzZ1dvOOneK08lj43HmK/ttq5Hp0ZVRuNCsjqUCc+u8w/zScTqBLsk35s5hRh5Jtze/pd0LQs5L3N0HkMQpEf/Or63MDFVLbWLaFk1vP0FhA6hrFSNGEs+MmCM6qQ3dASQKqpmSBWl5HHsF577dls3VOdgnGTdOLIfgKcl4Kr7X9s6ZhTDuhcLo34zqbVVNpcxg== 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 DUZPR01CA0284.eurprd01.prod.exchangelabs.com (2603:10a6:10:4b7::22) by AM9PR08MB6051.eurprd08.prod.outlook.com (2603:10a6:20b:2d6::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6792.28; Fri, 29 Sep 2023 08:15:07 +0000 Received: from DBAEUR03FT026.eop-EUR03.prod.protection.outlook.com (2603:10a6:10:4b7:cafe::5e) by DUZPR01CA0284.outlook.office365.com (2603:10a6:10:4b7::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6838.26 via Frontend Transport; Fri, 29 Sep 2023 08:15:07 +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 DBAEUR03FT026.mail.protection.outlook.com (100.127.142.242) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6838.25 via Frontend Transport; Fri, 29 Sep 2023 08:15:07 +0000 Received: from AZ-NEU-EX02.Emea.Arm.com (10.251.26.5) by AZ-NEU-EX04.Arm.com (10.251.24.32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.27; Fri, 29 Sep 2023 08:15:07 +0000 Received: from AZ-NEU-EX04.Arm.com (10.251.24.32) by AZ-NEU-EX02.Emea.Arm.com (10.251.26.5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.27; Fri, 29 Sep 2023 08:15:06 +0000 Received: from e129171.arm.com (10.57.64.234) by mail.arm.com (10.251.24.32) with Microsoft SMTP Server id 15.1.2507.27 via Frontend Transport; Fri, 29 Sep 2023 08:15:06 +0000 To: Subject: [PATCH] [gdb/arm] Only allow closure lookup by address if there are threads displaced-stepping Date: Fri, 29 Sep 2023 09:15:03 +0100 Message-ID: <20230929081503.4014732-1-luis.machado@arm.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-EOPAttributedMessage: 1 X-MS-TrafficTypeDiagnostic: DBAEUR03FT026:EE_|AM9PR08MB6051:EE_|AM7EUR03FT032:EE_|PA4PR08MB6238:EE_ X-MS-Office365-Filtering-Correlation-Id: a7901792-1a69-44be-a0b5-08dbc0c43873 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: kC9Ozt7PpiunuE2cdQfl8B9xMntgy/Kb1ao7ggmCxcLIZkLWaDdwGxkrpaUmlUvnfn0xPvr2l7LhYuecyW3qcKIa9jueJB3YOuyci8f3HIWqtAgaKcUe5tsqMaaQ5v4k1VEo9zc+8jPwlxEbKuRUo63MfqjK5PvBwYLVEE0lcrgAldesITu3HdnqVTC+DMReNV8ehriNKqLVTUVLlSu7ezlWhed2v72yxAkvp4gXw9MNADPG1MtQJQhe9sRti6TJRembn4CWjijhp+DMrgnCRCqsmoRaCtaO373KYkuCd7Of5t1DjFg8tVHsGf6//m2z3cW6zFEV3dqSpvLbjNhxKWBBTohIhtpgs7TZdjyM07QJwN8kl3hE617jWdy61EKkXqSh7zjuwqxZ5N3SbBTZLKYCzelYvoSf5jHU7NjkgQc11hIeBMIs8Ol8/t1gZWb2+GhNINVO4uXmjl5r10QL9FJs/VLmIa5jruLl9EGoOysEJx7lgHVs3bt2Kxd2PMUFrXix60YfvDCPWlaBylv3koInyTWSKrRaFSDxYoZCIiXnJZjGhCveXhZf7+GF28kzyqf+gGinkXyzJMRdGQCB3VAiPlJqHQUa1ZWTRdL6cTUd64kzNMnK7ct79AbbS6NG4HQ+5GGXk3TUQDQJQyf7GeLEU5sL0uwiQKZ+1KTnX1zEY4itKE1hq6Eql+aNh/0vP40dx7d9KT78kkQdT3KamvHKcJJ4/PxBKYRcoTtP4rYnzPe9XLB8/34H/6LgpjS342RJ4ukVxyOkceDpxCybqd0/nr3/FV4M+ZrFqyebbxVRYuSElywP2MtBhHvXdO5w 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:(13230031)(4636009)(376002)(39860400002)(346002)(136003)(396003)(230922051799003)(82310400011)(1800799009)(186009)(64100799003)(451199024)(46966006)(40470700004)(36840700001)(40460700003)(6666004)(7696005)(1076003)(356005)(82740400003)(81166007)(86362001)(36860700001)(83380400001)(47076005)(36756003)(2616005)(26005)(40480700001)(426003)(2906002)(336012)(41300700001)(316002)(44832011)(70586007)(6916009)(8936002)(70206006)(966005)(8676002)(478600001)(5660300002)(2004002)(36900700001); DIR:OUT; SFP:1101; X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM9PR08MB6051 X-MS-Exchange-Transport-CrossTenantHeadersStripped: AM7EUR03FT032.eop-EUR03.prod.protection.outlook.com X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id-Prvs: 5d801d58-e382-4722-56d0-08dbc0c4314d X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: FCntxdKW+oylZvBvd4zTitYX50O73kYdSOzJqZmkRZon3z5PHAXhaEafFUQwqU364VnNLFRqHM7aYEchOpHvWQHEjLJ16gW8/LCd1tpfNiix019DOcuv3vmMHorV+ySLDsXXMhMJz/wU0qzSFQvM+Z2WDtNr2YKeWp9IIZrFM2q0IuzCuaqaLIq7cI7Z84yf9VyyiBt/OobBeFSyRSJGBu3gbZAPnOEbATQTHccdiFpqrPNgiSddMzvD+KRLVlWyleZTRvHB8elA56/O/ysaCqGkiCN0PfA2cpU2NWvIxbU2tY8xY7/rk5+Y7ZHtyRfuL0jJ/B7PU9Fdq9x+gNc8UrNkF5/CymI4hhAKm0Bt0Rmxcpffd3MBJBesczCxMoftpLRPFFFgtZOa24mRr4rKTOyTUPT3DK2euQtFkGwpAF3Ft+KVKGOKmx4yWrKdFZs+UTSgt0wUEIq+vc4uxO5lc9CdRvFwhHxwX8W4qNsXaHjFL9VvB4DbwBc4Tq2yKmTcL5I6eNasPba2Bh5inbeAsBwOz4eCLw1zjI3nM1tHZlCfOVLFdDq4w5U/QLeVB87JadZ378IjzPT13m/tN/8a/SFu0+hwkMTtWRwErOME5lo/RcGcuqj6brFJ+EbR3pucQdefcJRaWf+IF8Q/pLfsgGiDJge6uLtXK2sOw/78N19zEAuRwIc7iw69Dt6GE15KoIWLhK4SALC9r0hrMoMEmIuEIg8bh8+T0qnl16sXL/a9GXHkF9KlGwOzMFthey5odx33yz/1ccVSGNtVSJiCXE0htgN6+Qcoj/NA82t37dw= 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:(13230031)(4636009)(376002)(396003)(346002)(39860400002)(136003)(230922051799003)(64100799003)(1800799009)(186009)(451199024)(82310400011)(46966006)(40470700004)(36840700001)(7696005)(2616005)(40460700003)(86362001)(36756003)(40480700001)(81166007)(82740400003)(36860700001)(1076003)(426003)(26005)(336012)(2906002)(966005)(83380400001)(6666004)(478600001)(47076005)(316002)(8936002)(8676002)(41300700001)(5660300002)(44832011)(70206006)(70586007)(6916009)(2004002); DIR:OUT; SFP:1101; X-OriginatorOrg: arm.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 29 Sep 2023 08:15:19.5657 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: a7901792-1a69-44be-a0b5-08dbc0c43873 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: AM7EUR03FT032.eop-EUR03.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PA4PR08MB6238 X-Spam-Status: No, score=-12.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, FORGED_SPF_HELO, GIT_PATCH_0, KAM_DMARC_NONE, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_PASS, SPF_NONE, TXREP, UNPARSEABLE_RELAY 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.30 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" Since commit 1e5ccb9c5ff4fd8ade4a8694676f99f4abf2d679, we have an assertion in displaced_step_buffers::copy_insn_closure_by_addr that makes sure a closure is available whenever we have a match between the provided address argument and the buffer address. That is fine, but the report in PR30872 shows this assertion triggering when it really shouldn't. After some investigation, here's what I found out. The 32-bit Arm architecture is the only one that calls gdbarch_displaced_step_copy_insn_closure_by_addr directly, and that's because 32-bit Arm needs to figure out the thumb state of the original instruction that we displaced-stepped through the displaced-step buffer. Before the assertion was put in place by commit 1e5ccb9c5ff4fd8ade4a8694676f99f4abf2d679, there was the possibility of getting nullptr back, which meant we were not doing a displaced-stepping operation. Now, with the assertion in place, this is running into issues. It looks like displaced_step_buffers::copy_insn_closure_by_addr is being used to return a couple different answers depending on the state we're in: 1 - If we are actively displaced-stepping, then copy_insn_closure_by_addr is supposed to return a valid closure for us, so we can determine the thumb mode. 2 - If we are not actively displaced-stepping, then copy_insn_closure_by_addr should return nullptr to signal that there isn't any displaced-step buffers in use, because we don't have a valid closure (but we should always have this). Since the displaced-step buffers are always allocated, but not always used, that means the buffers will always contain data. In particular, the buffer addr field cannot be used to determine if the buffer is active or not. For instance, we cannot set the buffer addr field to 0x0, as that can be a valid PC in some cases. My understanding is that the current_thread field should be a good candidate to signal that a particular displaced-step buffer is active or not. If it is nullptr, we have no threads using that buffer to displaced-step. Otherwise, it is an active buffer in use by a particular thread. The following fix modifies the displaced_step_buffers::copy_insn_closure_by_addr function so we only attempt to return a closure if the buffer has an assigned current_thread and if the buffer address matches the address argument. Alternatively, I think we could use a function to answer the question of whether we're actively displaced-stepping (so we have an active buffer) or not. I've also added a testcase that exercises the problem, restricted to 32-bit Arm, as that is the only architecture that faces this problem. Regression-tested on Ubuntu 20.04. OK? Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30872 --- gdb/displaced-stepping.c | 3 +- .../gdb.arch/arm-displaced-step-closure.c | 5 +++ .../gdb.arch/arm-displaced-step-closure.exp | 41 +++++++++++++++++++ 3 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 gdb/testsuite/gdb.arch/arm-displaced-step-closure.c create mode 100644 gdb/testsuite/gdb.arch/arm-displaced-step-closure.exp diff --git a/gdb/displaced-stepping.c b/gdb/displaced-stepping.c index bc59ef01478..41c3c999d1e 100644 --- a/gdb/displaced-stepping.c +++ b/gdb/displaced-stepping.c @@ -277,7 +277,8 @@ displaced_step_buffers::copy_insn_closure_by_addr (CORE_ADDR addr) { for (const displaced_step_buffer &buffer : m_buffers) { - if (addr == buffer.addr) + /* Make sure we have active buffers to compare to. */ + if (buffer.current_thread != nullptr && addr == buffer.addr) { /* The closure information should always be available. */ gdb_assert (buffer.copy_insn_closure.get () != nullptr); diff --git a/gdb/testsuite/gdb.arch/arm-displaced-step-closure.c b/gdb/testsuite/gdb.arch/arm-displaced-step-closure.c new file mode 100644 index 00000000000..085e682be50 --- /dev/null +++ b/gdb/testsuite/gdb.arch/arm-displaced-step-closure.c @@ -0,0 +1,5 @@ +int main (int argc, char **argv) + +{ + return 0; +} diff --git a/gdb/testsuite/gdb.arch/arm-displaced-step-closure.exp b/gdb/testsuite/gdb.arch/arm-displaced-step-closure.exp new file mode 100644 index 00000000000..ddac04ebe76 --- /dev/null +++ b/gdb/testsuite/gdb.arch/arm-displaced-step-closure.exp @@ -0,0 +1,41 @@ +# Copyright 2023 Free Software Foundation, Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +# This file is part of the gdb testsuite. +# +# Test a displaced stepping closure management bug, where a closure lookup +# by address returns a match even if no displaced stepping is currently +# taking place. + +require is_aarch32_target + +standard_testfile +if { [prepare_for_testing "failed to prepare" ${testfile} ${srcfile}] } { + return -1 +} + +if ![runto_main] { + return -1 +} + +# We have a breakpoint at the current pc (from stopping at main). Step over +# the breakpoint. +gdb_test "stepi" ".*" "step-over breakpoint" + +# Now attempt to disassemble the entry point function, where the displaced +# stepping buffer is. With the bug, gdb will crash when we attempt to list +# the PC that was used to displaced-step the previous instruction. +gdb_test "disassemble _start" ".*End of assembler dump\." \ + "disassemble through displaced-step buffer"