[v2,PR,gdb/29272] Make sure a copy_insn_closure is available when we have a match in copy_insn_closure_by_addr
Message ID | 20221102143341.2807182-1-luis.machado@arm.com |
---|---|
State | New |
Headers |
Return-Path: <gdb-patches-bounces+patchwork=sourceware.org@sourceware.org> X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 26A663858403 for <patchwork@sourceware.org>; Wed, 2 Nov 2022 14:34:22 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 26A663858403 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1667399662; bh=YKOM46UdlbBBIrrRwV2LLqMRCfzuimPCThakG2jf1Sc=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=ZEjqPM+lCFlNPEoQ7tOGfS6wW/dCnWx/xdwk9KSaif7JD00KZp4LLaPo2lSO43nAV Ze7plmRo4v4S4B6gG9IZR7XXpSHKlgT0sdLznJqwDxGV4ej0P8wi0Pd7LqasBzpBm8 ZSYNb5t70MuHBUWKeFtBYAKqUPjUyS9agZtC0FS4= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from EUR05-AM6-obe.outbound.protection.outlook.com (mail-am6eur05on2076.outbound.protection.outlook.com [40.107.22.76]) by sourceware.org (Postfix) with ESMTPS id 1AE593858D1E for <gdb-patches@sourceware.org>; Wed, 2 Nov 2022 14:33:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 1AE593858D1E ARC-Seal: i=2; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=pass; b=X4qqIMP7mV4eRWSyO55w+rUQM0n3yQ/orjPOm4acVgM+7EEEtWg1zKvRd+W3GyUF8AaHLDsgI2Vm1HfTgiKJySpTMPFkmPA9UWDZVwVJihl0tCts1cXqy/4gbnSvPEyJZ0+BsFeWFx6yIY2MqvTYVdTsVcIz+Mnvob07AUz1gUkzGglPr/Mc079JgOqzsukZ1tKJKutiGw3oIn4itm3yDl9y4TLQSD3uC83E0FovocVN2wjOQ2HvIxka44la4onts5GyVGI5/VgZnT/ZXRluKzw/eqG00oOGGUnOCXxXq+3X4D2SfAQOeo01dGkbxZDw2zGDVTdslA4Z5udraf3L2Q== ARC-Message-Signature: i=2; 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=YKOM46UdlbBBIrrRwV2LLqMRCfzuimPCThakG2jf1Sc=; b=Aa5ks6H8KdMrz+3I0jbh+Bdmt9rV/74F3xZrDfBaygdJDFjIWQgh1ykyVH296+Mqvwtg/L0uSVG+rO2zwGiX9IwR9US2UQ8Q4kks7mjL6j5cAAt2FkqdraMImlgf3VrC4h1RGD+A+fYi7DtPU2XcQNcfKxmT4jsSqYRUr3ikYRydmhN224Ut7gJUh41QZYWfvfwxH4HPmShTH6GZVXfmRClfEiLdjCXxnkGAOEKcSdeT7lFIBmrAsQf+hSNTighJyktO4xqsaGLjMoctNVgfL8qVnJymIRazvGvhNBlnWk7mP/Suq+gOHbchkg4af6vJmhBBfuetq+RsY17Smhj1nw== ARC-Authentication-Results: i=2; mx.microsoft.com 1; spf=pass (sender ip is 63.35.35.123) smtp.rcpttodomain=sourceware.org smtp.mailfrom=arm.com; dmarc=pass (p=none sp=none pct=100) action=none header.from=arm.com; dkim=pass (signature was verified) header.d=armh.onmicrosoft.com; arc=pass (0 oda=1 ltdi=1 spf=[1, 1, smtp.mailfrom=arm.com] dmarc=[1, 1, header.from=arm.com]) Received: from DU2PR04CA0043.eurprd04.prod.outlook.com (2603:10a6:10:234::18) by DB9PR08MB7471.eurprd08.prod.outlook.com (2603:10a6:10:36d::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5769.16; Wed, 2 Nov 2022 14:33:50 +0000 Received: from DBAEUR03FT054.eop-EUR03.prod.protection.outlook.com (2603:10a6:10:234:cafe::f1) by DU2PR04CA0043.outlook.office365.com (2603:10a6:10:234::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5791.22 via Frontend Transport; Wed, 2 Nov 2022 14:33:50 +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 DBAEUR03FT054.mail.protection.outlook.com (100.127.142.218) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5791.20 via Frontend Transport; Wed, 2 Nov 2022 14:33:50 +0000 Received: ("Tessian outbound f394866f3f2b:v130"); Wed, 02 Nov 2022 14:33:50 +0000 X-CheckRecipientChecked: true X-CR-MTA-CID: ce4a96e4435994f1 X-CR-MTA-TID: 64aa7808 Received: from 694458bef2b7.1 by 64aa7808-outbound-1.mta.getcheckrecipient.com id BABA87BD-C43B-4221-8FBC-65DACB471809.1; Wed, 02 Nov 2022 14:33:44 +0000 Received: from EUR05-VI1-obe.outbound.protection.outlook.com by 64aa7808-outbound-1.mta.getcheckrecipient.com with ESMTPS id 694458bef2b7.1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384); Wed, 02 Nov 2022 14:33:44 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=NkleyA0xz6jX3zxng7nd4fl86+cQDrKyCIW20kZKDkEwR+8Ysz1OOY7HTJ/I+GH+npAIyWl9fRj2GCzxRPCFQq9cFCHj/gM5/7YohdXm2PJfMMXrsukM6cmFTMQQwiRaeg4NK+CHABr4xEpwSY6BMJVcty8akGg7qHmXiRtgJbHA+w9lkROSvWqlZ8AIeOMAAu+TkupacZjcY+P+XVzXISpA7MolZ683MI11zhS7ahjevHZoebuuCWrW+Pw3mC9XCTWn+MjJnrKj3IIMh2jQ640pkTkqouyDmPGWlJHH4qf4MY+1aizHRQ/j0Cpy0+3A84dl/W/M3UFyNeu3u+ZwUw== 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=YKOM46UdlbBBIrrRwV2LLqMRCfzuimPCThakG2jf1Sc=; b=Ziz0cj+1AHLEbh/jp2iA489Tl6LzJ3hL8AA0W19V4E6ANyn1mH4Vxfh2IEk3qwm250PuvFdQxk2g+mNSMGh7X467Qf3f8GUOaoU0VxfCQxmvpenpDJUZ1btJllLKASan+xlbfVwELgibkkr6W9H/el824NXCnL6IRBQxriowYDZ7hguWIdksAjz2bJCmruiCJRNFNxMDTXD1WEvAzUDzDGrhW2oTmiJkZLIJiwdbnKPGgYD8V3HmaBOggXV26zlddcg0QZ17303Ekq+RMx5oduBo1aTkjftNAhQpGxIyGwOP0RQTt7j9SktpwdZV0dEm/YwWV5ZBAlizx5VZ6XnXXQ== 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 DU2PR04CA0010.eurprd04.prod.outlook.com (2603:10a6:10:3b::15) by AS4PR08MB7805.eurprd08.prod.outlook.com (2603:10a6:20b:518::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5746.23; Wed, 2 Nov 2022 14:33:42 +0000 Received: from DBAEUR03FT036.eop-EUR03.prod.protection.outlook.com (2603:10a6:10:3b:cafe::38) by DU2PR04CA0010.outlook.office365.com (2603:10a6:10:3b::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5769.15 via Frontend Transport; Wed, 2 Nov 2022 14:33:42 +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 DBAEUR03FT036.mail.protection.outlook.com (100.127.142.193) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5791.20 via Frontend Transport; Wed, 2 Nov 2022 14:33:42 +0000 Received: from AZ-NEU-EX04.Arm.com (10.251.24.32) 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.12; Wed, 2 Nov 2022 14:33:41 +0000 Received: from e129171.arm.com (10.57.68.244) by mail.arm.com (10.251.24.32) with Microsoft SMTP Server id 15.1.2507.12 via Frontend Transport; Wed, 2 Nov 2022 14:33:41 +0000 To: <gdb-patches@sourceware.org>, <simon.marchi@efficios.com> Subject: [PATCH, v2] [PR gdb/29272] Make sure a copy_insn_closure is available when we have a match in copy_insn_closure_by_addr Date: Wed, 2 Nov 2022 14:33:41 +0000 Message-ID: <20221102143341.2807182-1-luis.machado@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221026084100.28009-1-luis.machado@arm.com> References: <20221026084100.28009-1-luis.machado@arm.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-EOPAttributedMessage: 1 X-MS-TrafficTypeDiagnostic: DBAEUR03FT036:EE_|AS4PR08MB7805:EE_|DBAEUR03FT054:EE_|DB9PR08MB7471:EE_ X-MS-Office365-Filtering-Correlation-Id: ce7079ba-8f45-4c47-1a49-08dabcdf4262 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: LYBdCofDk5ndaFjfqIBDBWWQBvt3KLAOwtpyYbiZZAmAQw3ADm1SFRCfdwsG5oK6A8hS9/i4hDFQlG7gb1h4QmNyqmcaq0Y+4j4ohYJpEEkLoHFhpBK5+ixO/frXt7AYg9ZVdSAvzhyvVk/LZtPt5tB+pXgiI6f2vZAjSvtV98vJcjwpgXZ3xUCnXGsLAU6f3BCVgAeCxCH0etmpSGJDbkJEy1lj9bTg35v5hMQH9btV7fO75sdkMoNXkUZfWTQr/HNUF/uKeOvKqMITXChvBiGuQufL2uMHRCcz8+X2x2u0QFGD4Ff5hWfYtoSelABotEpqzgx5CEmC5ThQRpxWr2pHlWsDzT4ybyqdVqM0yGmaV78ptsdz5tR24c13f4vrL/uvYfQAVHF8z+HfSSnaEq9dH7YKfrity0wUkd2J7A2jfMCyLlCDB+kvv59T9J9IG8g9cOoBmzWRxLHykxQvUAhun90qlDGiNuEd2L63Bmx45k5gPbzKXGSqGuAh0vnDqhQ5JDPzp/LnxnRnQwYOYkqZvs2XQL6EhI13jzUhcpJSx+2Y/IdsD/ZhqInsFirhxHSxK6c9917mzjvihrgMLRAOOne73JgzMLqjI4+f6bU5KGDubQny7YI7RTNJAVzFKBECRnzYPWed52XZesJGsCv55PxIF6zeoJ1+sxxQ6g1I++iZss4UyfBM27/o+BOdg1c4Ksxn2b+/BxVypzKvmug+jqmV07FXjvBegHVDqX1A1dhBiMvZLiHbYpj/6o4t2uEPN2vBzZHI56z5KXqGJJwRQloh30IwjuspiT5EAy1fGBVPhBBgIzrmc5et5FtZxgSBCtM55R7MMNvtAVj9mA== 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:(13230022)(4636009)(39860400002)(376002)(396003)(346002)(136003)(451199015)(36840700001)(46966006)(40470700004)(110136005)(2906002)(81166007)(47076005)(356005)(1076003)(36860700001)(44832011)(426003)(83380400001)(70206006)(86362001)(5660300002)(82740400003)(8936002)(82310400005)(40480700001)(26005)(966005)(70586007)(40460700003)(316002)(8676002)(7696005)(336012)(186003)(2616005)(41300700001)(36756003)(478600001)(36900700001); DIR:OUT; SFP:1101; X-MS-Exchange-Transport-CrossTenantHeadersStamped: AS4PR08MB7805 X-MS-Exchange-Transport-CrossTenantHeadersStripped: DBAEUR03FT054.eop-EUR03.prod.protection.outlook.com X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id-Prvs: 6bc6adf7-ffa2-4a3a-4455-08dabcdf3d69 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 4fPNdW8a8RGbilstUev2V7MvyBioPXSYq6JRSkCIDFhlQyXZ5/mY6AvBruKWq5JfW2DOEXshBvEHvtmU//Du5xJbo50QxaCDeD7Mqg5ZqoDcE68LmyCX7zyM8JNPSwYuwLIL1GIYTxo7srZmvojDy4BxQi9eQ6ykVok5C81efh2CtNJlU5m13qj6Cfd8DIBZ5JSPUP5LS8AYSdzu9IcnVJA/FXJ7vClXyo14Bk3lnWaQLxaAZVVdBpSdY3BKU21oP/7E+svRhsdOUZrT1X0brqZpkxMaGbCN6qeEMRwN3WVEFtWs1o1HTNqED0qyF6yPe7ywLnxFedd+KpnLjXaij+jyaHETNOczMkjAkPQsbg7ZfGcQsbwkZcuNYoK5AAWy0EQkKK/tsH+RiPdj+ZL4WWCDKI3h+Bp7Vgezdczd4E00tI1Q3cI8FE5ml6ExHKZ3MIdg73cxLU2OVytJ3a+XuvJNKSNnAyQG1hFtPf0c78zfZdMixviQTqzxOx1L9gkQIk7nqSivD/33Pj7GqxMTjUxUhCO3hf3tvGTcdDPcbKumT9cbQYKCR2kWsFrMC60KS/ywT/HDyg/4TcQqF8L8Izqh3f94svUpKQ6pFBegPcK09S23iN3UfX6fu0GS8AcJsyBYyCVpor/DbgLpt4/YvW0hAFzyVZTMUDVaMM8UGcamAlP07IKT5+JlQY56lQyUmShcg0djcpHda1MX1Jz9djC9MVc6FuvmuVjlRBFnlpkSanww1eoVj5yEMTIW9jKBE+WBUUCRTrfiT3FVkoiaKbLLPepmdn7FszVbJwdsX0g= 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:(13230022)(4636009)(346002)(136003)(396003)(376002)(39860400002)(451199015)(46966006)(40470700004)(36840700001)(8676002)(36756003)(81166007)(7696005)(86362001)(40460700003)(82310400005)(40480700001)(70206006)(70586007)(316002)(110136005)(186003)(83380400001)(1076003)(5660300002)(44832011)(8936002)(478600001)(426003)(336012)(2616005)(2906002)(36860700001)(966005)(41300700001)(82740400003)(26005)(47076005); DIR:OUT; SFP:1101; X-OriginatorOrg: arm.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Nov 2022 14:33:50.3644 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: ce7079ba-8f45-4c47-1a49-08dabcdf4262 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: DBAEUR03FT054.eop-EUR03.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DB9PR08MB7471 X-Spam-Status: No, score=-12.3 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 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 <gdb-patches.sourceware.org> List-Unsubscribe: <https://sourceware.org/mailman/options/gdb-patches>, <mailto:gdb-patches-request@sourceware.org?subject=unsubscribe> List-Archive: <https://sourceware.org/pipermail/gdb-patches/> List-Post: <mailto:gdb-patches@sourceware.org> List-Help: <mailto:gdb-patches-request@sourceware.org?subject=help> List-Subscribe: <https://sourceware.org/mailman/listinfo/gdb-patches>, <mailto:gdb-patches-request@sourceware.org?subject=subscribe> From: Luis Machado via Gdb-patches <gdb-patches@sourceware.org> Reply-To: Luis Machado <luis.machado@arm.com> Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" <gdb-patches-bounces+patchwork=sourceware.org@sourceware.org> |
Series |
[v2,PR,gdb/29272] Make sure a copy_insn_closure is available when we have a match in copy_insn_closure_by_addr
|
|
Commit Message
Luis Machado
Nov. 2, 2022, 2:33 p.m. UTC
v2: Add try/catch block Investigating PR29272, it was mentioned a particular test used to work on GDB 10, but it started failing with GDB 11 onwards. I tracked it down to some displaced stepping improvements on commit 187b041e2514827b9d86190ed2471c4c7a352874. In particular, one of the corner cases using copy_insn_closure_by_addr got silently broken. It is hard to spot because it doesn't have any good tests for it, and the situation is quite specific to the Arm target. Essentially, the change from the displaced stepping improvements made it so we could still invoke copy_insn_closure_by_addr correctly to return the pointer to a copy_insn_closure, but it always returned nullptr due to the order of the statements in displaced_step_buffer::prepare. The way it is now, we first write the address of the displaced step buffer to PC and then save the copy_insn_closure pointer. The problem is that writing to PC for the Arm target requires figuring out if the new PC is thumb mode or not. With no copy_insn_closure data, the logic to determine the thumb mode during displaced stepping doesn't work, and gives random results that are difficult to track (SIGILL, SIGSEGV etc). Fix this by reordering the PC write in displaced_step_buffer::prepare and, for safety, add an assertion to displaced_step_buffer::copy_insn_closure_by_addr so GDB stops right when it sees this invalid situation. If this gets broken again in the future, it will be easier to spot. Guard the code in a try/catch block to handle the case where we can't write the PC, so as to not leave partial state in the displaced step machinery. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=29272 --- gdb/displaced-stepping.c | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-)
Comments
On 11/2/22 10:33, Luis Machado via Gdb-patches wrote: > v2: Add try/catch block > > Investigating PR29272, it was mentioned a particular test used to work on > GDB 10, but it started failing with GDB 11 onwards. I tracked it down to > some displaced stepping improvements on commit > 187b041e2514827b9d86190ed2471c4c7a352874. > > In particular, one of the corner cases using copy_insn_closure_by_addr got > silently broken. It is hard to spot because it doesn't have any good tests > for it, and the situation is quite specific to the Arm target. > > Essentially, the change from the displaced stepping improvements made it so > we could still invoke copy_insn_closure_by_addr correctly to return the > pointer to a copy_insn_closure, but it always returned nullptr due to > the order of the statements in displaced_step_buffer::prepare. > > The way it is now, we first write the address of the displaced step buffer > to PC and then save the copy_insn_closure pointer. > > The problem is that writing to PC for the Arm target requires figuring > out if the new PC is thumb mode or not. > > With no copy_insn_closure data, the logic to determine the thumb mode > during displaced stepping doesn't work, and gives random results that > are difficult to track (SIGILL, SIGSEGV etc). > > Fix this by reordering the PC write in displaced_step_buffer::prepare > and, for safety, add an assertion to > displaced_step_buffer::copy_insn_closure_by_addr so GDB stops right > when it sees this invalid situation. If this gets broken again in the > future, it will be easier to spot. > > Guard the code in a try/catch block to handle the case where we can't > write the PC, so as to not leave partial state in the displaced step > machinery. > > Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=29272 > --- > gdb/displaced-stepping.c | 26 +++++++++++++++++++++++--- > 1 file changed, 23 insertions(+), 3 deletions(-) > > diff --git a/gdb/displaced-stepping.c b/gdb/displaced-stepping.c > index eac2c5dab94..3b5376cf31b 100644 > --- a/gdb/displaced-stepping.c > +++ b/gdb/displaced-stepping.c > @@ -139,15 +139,31 @@ displaced_step_buffers::prepare (thread_info *thread, CORE_ADDR &displaced_pc) > return DISPLACED_STEP_PREPARE_STATUS_CANT; > } > > - /* Resume execution at the copy. */ > - regcache_write_pc (regcache, buffer->addr); > - > /* This marks the buffer as being in use. */ > buffer->current_thread = thread; > > /* Save this, now that we know everything went fine. */ > buffer->copy_insn_closure = std::move (copy_insn_closure); > > + /* Adjust the PC so it points to the displaced step buffer address that will > + be used. This needs to be done after we save the copy_insn_closure, as > + some architectures (Arm, for one) need that information so they can adjust > + other data as needed. In particular, Arm needs to know if the instruction > + being executed in the displaced step buffer is thumb or not. Without that > + information, things will be very wrong in a random way. */ > + try > + { > + regcache_write_pc (regcache, buffer->addr); > + } > + catch (const gdb_exception_error &except) > + { > + /* Reset the displaced step buffer state if we failed to write PC. > + Otherwise we will prevent this buffer from being used, as it will > + always have a thread in buffer->current_thread. */ > + buffer->current_thread = nullptr; > + copy_insn_closure = std::move (buffer->copy_insn_closure); The intention would be clearer by just doing: buffer->copy_insn_closure.reset () > + return DISPLACED_STEP_PREPARE_STATUS_CANT; I think we should just let the exception escape, DISPLACED_STEP_PREPARE_STATUS_CANT isn't meant to convey an error. Would this work, using make_scope_exit? /* Reset the displaced step buffer state if we failed to write PC. Otherwise we will prevent this buffer from being used, as it will always have a thread in buffer->current_thread. */ auto reset_buffer = make_scope_exit ([buffer] () { buffer->current_thread = nullptr; buffer->copy_insn_closure.reset (); }); /* Adjust the PC so it points to the displaced step buffer address that will be used. This needs to be done after we save the copy_insn_closure, as some architectures (Arm, for one) need that information so they can adjust other data as needed. In particular, Arm needs to know if the instruction being executed in the displaced step buffer is thumb or not. Without that information, things will be very wrong in a random way. */ regcache_write_pc (regcache, buffer->addr); reset_buffer.release (); Simon
On 11/2/22 17:44, Simon Marchi wrote: > On 11/2/22 10:33, Luis Machado via Gdb-patches wrote: >> v2: Add try/catch block >> >> Investigating PR29272, it was mentioned a particular test used to work on >> GDB 10, but it started failing with GDB 11 onwards. I tracked it down to >> some displaced stepping improvements on commit >> 187b041e2514827b9d86190ed2471c4c7a352874. >> >> In particular, one of the corner cases using copy_insn_closure_by_addr got >> silently broken. It is hard to spot because it doesn't have any good tests >> for it, and the situation is quite specific to the Arm target. >> >> Essentially, the change from the displaced stepping improvements made it so >> we could still invoke copy_insn_closure_by_addr correctly to return the >> pointer to a copy_insn_closure, but it always returned nullptr due to >> the order of the statements in displaced_step_buffer::prepare. >> >> The way it is now, we first write the address of the displaced step buffer >> to PC and then save the copy_insn_closure pointer. >> >> The problem is that writing to PC for the Arm target requires figuring >> out if the new PC is thumb mode or not. >> >> With no copy_insn_closure data, the logic to determine the thumb mode >> during displaced stepping doesn't work, and gives random results that >> are difficult to track (SIGILL, SIGSEGV etc). >> >> Fix this by reordering the PC write in displaced_step_buffer::prepare >> and, for safety, add an assertion to >> displaced_step_buffer::copy_insn_closure_by_addr so GDB stops right >> when it sees this invalid situation. If this gets broken again in the >> future, it will be easier to spot. >> >> Guard the code in a try/catch block to handle the case where we can't >> write the PC, so as to not leave partial state in the displaced step >> machinery. >> >> Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=29272 >> --- >> gdb/displaced-stepping.c | 26 +++++++++++++++++++++++--- >> 1 file changed, 23 insertions(+), 3 deletions(-) >> >> diff --git a/gdb/displaced-stepping.c b/gdb/displaced-stepping.c >> index eac2c5dab94..3b5376cf31b 100644 >> --- a/gdb/displaced-stepping.c >> +++ b/gdb/displaced-stepping.c >> @@ -139,15 +139,31 @@ displaced_step_buffers::prepare (thread_info *thread, CORE_ADDR &displaced_pc) >> return DISPLACED_STEP_PREPARE_STATUS_CANT; >> } >> >> - /* Resume execution at the copy. */ >> - regcache_write_pc (regcache, buffer->addr); >> - >> /* This marks the buffer as being in use. */ >> buffer->current_thread = thread; >> >> /* Save this, now that we know everything went fine. */ >> buffer->copy_insn_closure = std::move (copy_insn_closure); >> >> + /* Adjust the PC so it points to the displaced step buffer address that will >> + be used. This needs to be done after we save the copy_insn_closure, as >> + some architectures (Arm, for one) need that information so they can adjust >> + other data as needed. In particular, Arm needs to know if the instruction >> + being executed in the displaced step buffer is thumb or not. Without that >> + information, things will be very wrong in a random way. */ >> + try >> + { >> + regcache_write_pc (regcache, buffer->addr); >> + } >> + catch (const gdb_exception_error &except) >> + { >> + /* Reset the displaced step buffer state if we failed to write PC. >> + Otherwise we will prevent this buffer from being used, as it will >> + always have a thread in buffer->current_thread. */ >> + buffer->current_thread = nullptr; >> + copy_insn_closure = std::move (buffer->copy_insn_closure); > > The intention would be clearer by just doing: > > buffer->copy_insn_closure.reset () > >> + return DISPLACED_STEP_PREPARE_STATUS_CANT; > > I think we should just let the exception escape, > DISPLACED_STEP_PREPARE_STATUS_CANT isn't meant to convey an error. Wouldn't letting it escape completely abort the single-stepping operation? I was expecting a return of DISPLACED_STEP_PREPARE_STATUS_CANT to have a fallback of stepping in-place. Isn't that the case? > Would this work, using make_scope_exit? Ah, possibly. Let me try that. Thanks for the suggestion. > > /* Reset the displaced step buffer state if we failed to write PC. > Otherwise we will prevent this buffer from being used, as it will > always have a thread in buffer->current_thread. */ > auto reset_buffer = make_scope_exit > ([buffer] () > { > buffer->current_thread = nullptr; > buffer->copy_insn_closure.reset (); > }); > > /* Adjust the PC so it points to the displaced step buffer address that will > be used. This needs to be done after we save the copy_insn_closure, as > some architectures (Arm, for one) need that information so they can adjust > other data as needed. In particular, Arm needs to know if the instruction > being executed in the displaced step buffer is thumb or not. Without that > information, things will be very wrong in a random way. */ > regcache_write_pc (regcache, buffer->addr); > > reset_buffer.release (); > > Simon
On 11/2/22 14:06, Luis Machado wrote: > On 11/2/22 17:44, Simon Marchi wrote: >> On 11/2/22 10:33, Luis Machado via Gdb-patches wrote: >>> v2: Add try/catch block >>> >>> Investigating PR29272, it was mentioned a particular test used to work on >>> GDB 10, but it started failing with GDB 11 onwards. I tracked it down to >>> some displaced stepping improvements on commit >>> 187b041e2514827b9d86190ed2471c4c7a352874. >>> >>> In particular, one of the corner cases using copy_insn_closure_by_addr got >>> silently broken. It is hard to spot because it doesn't have any good tests >>> for it, and the situation is quite specific to the Arm target. >>> >>> Essentially, the change from the displaced stepping improvements made it so >>> we could still invoke copy_insn_closure_by_addr correctly to return the >>> pointer to a copy_insn_closure, but it always returned nullptr due to >>> the order of the statements in displaced_step_buffer::prepare. >>> >>> The way it is now, we first write the address of the displaced step buffer >>> to PC and then save the copy_insn_closure pointer. >>> >>> The problem is that writing to PC for the Arm target requires figuring >>> out if the new PC is thumb mode or not. >>> >>> With no copy_insn_closure data, the logic to determine the thumb mode >>> during displaced stepping doesn't work, and gives random results that >>> are difficult to track (SIGILL, SIGSEGV etc). >>> >>> Fix this by reordering the PC write in displaced_step_buffer::prepare >>> and, for safety, add an assertion to >>> displaced_step_buffer::copy_insn_closure_by_addr so GDB stops right >>> when it sees this invalid situation. If this gets broken again in the >>> future, it will be easier to spot. >>> >>> Guard the code in a try/catch block to handle the case where we can't >>> write the PC, so as to not leave partial state in the displaced step >>> machinery. >>> >>> Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=29272 >>> --- >>> gdb/displaced-stepping.c | 26 +++++++++++++++++++++++--- >>> 1 file changed, 23 insertions(+), 3 deletions(-) >>> >>> diff --git a/gdb/displaced-stepping.c b/gdb/displaced-stepping.c >>> index eac2c5dab94..3b5376cf31b 100644 >>> --- a/gdb/displaced-stepping.c >>> +++ b/gdb/displaced-stepping.c >>> @@ -139,15 +139,31 @@ displaced_step_buffers::prepare (thread_info *thread, CORE_ADDR &displaced_pc) >>> return DISPLACED_STEP_PREPARE_STATUS_CANT; >>> } >>> - /* Resume execution at the copy. */ >>> - regcache_write_pc (regcache, buffer->addr); >>> - >>> /* This marks the buffer as being in use. */ >>> buffer->current_thread = thread; >>> /* Save this, now that we know everything went fine. */ >>> buffer->copy_insn_closure = std::move (copy_insn_closure); >>> + /* Adjust the PC so it points to the displaced step buffer address that will >>> + be used. This needs to be done after we save the copy_insn_closure, as >>> + some architectures (Arm, for one) need that information so they can adjust >>> + other data as needed. In particular, Arm needs to know if the instruction >>> + being executed in the displaced step buffer is thumb or not. Without that >>> + information, things will be very wrong in a random way. */ >>> + try >>> + { >>> + regcache_write_pc (regcache, buffer->addr); >>> + } >>> + catch (const gdb_exception_error &except) >>> + { >>> + /* Reset the displaced step buffer state if we failed to write PC. >>> + Otherwise we will prevent this buffer from being used, as it will >>> + always have a thread in buffer->current_thread. */ >>> + buffer->current_thread = nullptr; >>> + copy_insn_closure = std::move (buffer->copy_insn_closure); >> >> The intention would be clearer by just doing: >> >> buffer->copy_insn_closure.reset () >> >>> + return DISPLACED_STEP_PREPARE_STATUS_CANT; >> >> I think we should just let the exception escape, >> DISPLACED_STEP_PREPARE_STATUS_CANT isn't meant to convey an error. > > Wouldn't letting it escape completely abort the single-stepping operation? I was expecting a return of > DISPLACED_STEP_PREPARE_STATUS_CANT to have a fallback of stepping in-place. Isn't that the case? Yeah, but I think that's what we want. Failing to write the PC is an "abort mission" kind of failure, IMO. Something is very broken. DISPLACED_STEP_PREPARE_STATUS_CANT is not equivalent to an errorp, it's "we have successfully analyzed the instruction and concluded it can't be displaced-step". If we wanted to return a status code, I would suggest to introduce a new one (e.g. DISPLACED_STEP_PREPARE_STATUS_ERROR). But I think the exception is fine, this is how other kinds of failure that happen when resuming are reported, like when we fail to insert breakpoints. We arguably are not very good at handling those gracefully, but that's the problem of this code here. Simon
On 11/2/22 18:22, Simon Marchi wrote: > On 11/2/22 14:06, Luis Machado wrote: >> On 11/2/22 17:44, Simon Marchi wrote: >>> On 11/2/22 10:33, Luis Machado via Gdb-patches wrote: >>>> v2: Add try/catch block >>>> >>>> Investigating PR29272, it was mentioned a particular test used to work on >>>> GDB 10, but it started failing with GDB 11 onwards. I tracked it down to >>>> some displaced stepping improvements on commit >>>> 187b041e2514827b9d86190ed2471c4c7a352874. >>>> >>>> In particular, one of the corner cases using copy_insn_closure_by_addr got >>>> silently broken. It is hard to spot because it doesn't have any good tests >>>> for it, and the situation is quite specific to the Arm target. >>>> >>>> Essentially, the change from the displaced stepping improvements made it so >>>> we could still invoke copy_insn_closure_by_addr correctly to return the >>>> pointer to a copy_insn_closure, but it always returned nullptr due to >>>> the order of the statements in displaced_step_buffer::prepare. >>>> >>>> The way it is now, we first write the address of the displaced step buffer >>>> to PC and then save the copy_insn_closure pointer. >>>> >>>> The problem is that writing to PC for the Arm target requires figuring >>>> out if the new PC is thumb mode or not. >>>> >>>> With no copy_insn_closure data, the logic to determine the thumb mode >>>> during displaced stepping doesn't work, and gives random results that >>>> are difficult to track (SIGILL, SIGSEGV etc). >>>> >>>> Fix this by reordering the PC write in displaced_step_buffer::prepare >>>> and, for safety, add an assertion to >>>> displaced_step_buffer::copy_insn_closure_by_addr so GDB stops right >>>> when it sees this invalid situation. If this gets broken again in the >>>> future, it will be easier to spot. >>>> >>>> Guard the code in a try/catch block to handle the case where we can't >>>> write the PC, so as to not leave partial state in the displaced step >>>> machinery. >>>> >>>> Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=29272 >>>> --- >>>> gdb/displaced-stepping.c | 26 +++++++++++++++++++++++--- >>>> 1 file changed, 23 insertions(+), 3 deletions(-) >>>> >>>> diff --git a/gdb/displaced-stepping.c b/gdb/displaced-stepping.c >>>> index eac2c5dab94..3b5376cf31b 100644 >>>> --- a/gdb/displaced-stepping.c >>>> +++ b/gdb/displaced-stepping.c >>>> @@ -139,15 +139,31 @@ displaced_step_buffers::prepare (thread_info *thread, CORE_ADDR &displaced_pc) >>>> return DISPLACED_STEP_PREPARE_STATUS_CANT; >>>> } >>>> - /* Resume execution at the copy. */ >>>> - regcache_write_pc (regcache, buffer->addr); >>>> - >>>> /* This marks the buffer as being in use. */ >>>> buffer->current_thread = thread; >>>> /* Save this, now that we know everything went fine. */ >>>> buffer->copy_insn_closure = std::move (copy_insn_closure); >>>> + /* Adjust the PC so it points to the displaced step buffer address that will >>>> + be used. This needs to be done after we save the copy_insn_closure, as >>>> + some architectures (Arm, for one) need that information so they can adjust >>>> + other data as needed. In particular, Arm needs to know if the instruction >>>> + being executed in the displaced step buffer is thumb or not. Without that >>>> + information, things will be very wrong in a random way. */ >>>> + try >>>> + { >>>> + regcache_write_pc (regcache, buffer->addr); >>>> + } >>>> + catch (const gdb_exception_error &except) >>>> + { >>>> + /* Reset the displaced step buffer state if we failed to write PC. >>>> + Otherwise we will prevent this buffer from being used, as it will >>>> + always have a thread in buffer->current_thread. */ >>>> + buffer->current_thread = nullptr; >>>> + copy_insn_closure = std::move (buffer->copy_insn_closure); >>> >>> The intention would be clearer by just doing: >>> >>> buffer->copy_insn_closure.reset () >>> >>>> + return DISPLACED_STEP_PREPARE_STATUS_CANT; >>> >>> I think we should just let the exception escape, >>> DISPLACED_STEP_PREPARE_STATUS_CANT isn't meant to convey an error. >> >> Wouldn't letting it escape completely abort the single-stepping operation? I was expecting a return of >> DISPLACED_STEP_PREPARE_STATUS_CANT to have a fallback of stepping in-place. Isn't that the case? > > Yeah, but I think that's what we want. Failing to write the PC is an > "abort mission" kind of failure, IMO. Something is very broken. > > DISPLACED_STEP_PREPARE_STATUS_CANT is not equivalent to an errorp, it's "we have > successfully analyzed the instruction and concluded it can't be > displaced-step". If we wanted to return a status code, I would suggest > to introduce a new one (e.g. DISPLACED_STEP_PREPARE_STATUS_ERROR). But > I think the exception is fine, this is how other kinds of failure that > happen when resuming are reported, like when we fail to insert > breakpoints. We arguably are not very good at handling those > gracefully, but that's the problem of this code here. Yeah. That's a reasonable point. I was hoping to salvage something from this bad situation and at least let the user complete a single-stepping. Let me get a v3 going. > > Simon
diff --git a/gdb/displaced-stepping.c b/gdb/displaced-stepping.c index eac2c5dab94..3b5376cf31b 100644 --- a/gdb/displaced-stepping.c +++ b/gdb/displaced-stepping.c @@ -139,15 +139,31 @@ displaced_step_buffers::prepare (thread_info *thread, CORE_ADDR &displaced_pc) return DISPLACED_STEP_PREPARE_STATUS_CANT; } - /* Resume execution at the copy. */ - regcache_write_pc (regcache, buffer->addr); - /* This marks the buffer as being in use. */ buffer->current_thread = thread; /* Save this, now that we know everything went fine. */ buffer->copy_insn_closure = std::move (copy_insn_closure); + /* Adjust the PC so it points to the displaced step buffer address that will + be used. This needs to be done after we save the copy_insn_closure, as + some architectures (Arm, for one) need that information so they can adjust + other data as needed. In particular, Arm needs to know if the instruction + being executed in the displaced step buffer is thumb or not. Without that + information, things will be very wrong in a random way. */ + try + { + regcache_write_pc (regcache, buffer->addr); + } + catch (const gdb_exception_error &except) + { + /* Reset the displaced step buffer state if we failed to write PC. + Otherwise we will prevent this buffer from being used, as it will + always have a thread in buffer->current_thread. */ + buffer->current_thread = nullptr; + copy_insn_closure = std::move (buffer->copy_insn_closure); + return DISPLACED_STEP_PREPARE_STATUS_CANT; + } /* Tell infrun not to try preparing a displaced step again for this inferior if all buffers are taken. */ thread->inf->displaced_step_state.unavailable = true; @@ -264,7 +280,11 @@ displaced_step_buffers::copy_insn_closure_by_addr (CORE_ADDR addr) for (const displaced_step_buffer &buffer : m_buffers) { if (addr == buffer.addr) + { + /* The closure information should always be available. */ + gdb_assert (buffer.copy_insn_closure.get () != nullptr); return buffer.copy_insn_closure.get (); + } } return nullptr;