From patchwork Fri May 19 12:40:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Vrany X-Patchwork-Id: 69685 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 2753F385840D for ; Fri, 19 May 2023 12:41:01 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2753F385840D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1684500061; bh=ZFwdPrUniFGJ8inU9oWBoEDcHcH7+16t6s/uzm4rnmI=; 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=yVOMV3+//UtZoYEx46+RYaEJyLtHuGfmAtYezqx0BN74Vo+EMjoXgemMAre5hJs52 Sb4Qj2H1O1mRi29f+/In1J2bzxi5Hwlswkm4MjCKew5fHXI4Xf/W/yp265EwnlK2Dr L+qZaVJjom6Qt6coDkocL/Ts1Xu4I7OEWXR5CihM= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from us-smtp-delivery-114.mimecast.com (us-smtp-delivery-114.mimecast.com [170.10.129.114]) by sourceware.org (Postfix) with ESMTPS id 35D513858421 for ; Fri, 19 May 2023 12:40:35 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 35D513858421 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10lp2105.outbound.protection.outlook.com [104.47.58.105]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-623-ZYsyFBU9NpSWW5Y7Vqp6qg-1; Fri, 19 May 2023 08:40:33 -0400 X-MC-Unique: ZYsyFBU9NpSWW5Y7Vqp6qg-1 Received: from SJ2PR17MB6568.namprd17.prod.outlook.com (2603:10b6:a03:4f7::10) by SJ0PR17MB5126.namprd17.prod.outlook.com (2603:10b6:a03:390::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6411.21; Fri, 19 May 2023 12:40:28 +0000 Received: from SJ2PR17MB6568.namprd17.prod.outlook.com ([fe80::fa1f:c830:6d52:12ab]) by SJ2PR17MB6568.namprd17.prod.outlook.com ([fe80::fa1f:c830:6d52:12ab%6]) with mapi id 15.20.6411.019; Fri, 19 May 2023 12:40:28 +0000 To: gdb-patches@sourceware.org CC: Wenyan.Xin@windriver.com, Jan Vrany , Tom Tromey Subject: [pushed] gdb: fix post-hook execution for remote targets Date: Fri, 19 May 2023 13:40:07 +0100 Message-ID: <20230519124007.386152-1-jan.vrany@labware.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <871qje23e0.fsf@tromey.com> References: <871qje23e0.fsf@tromey.com> X-ClientProxiedBy: LO4P123CA0453.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:1aa::8) To SJ2PR17MB6568.namprd17.prod.outlook.com (2603:10b6:a03:4f7::10) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SJ2PR17MB6568:EE_|SJ0PR17MB5126:EE_ X-MS-Office365-Filtering-Correlation-Id: e0444f0e-a36b-44c8-fd39-08db586639a7 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0 X-Microsoft-Antispam-Message-Info: dp1WJBb6I1Ddbcv5ImfLzrMgpDvm4gpNYdp0ms/GkeeUB720dKLGKAYv6yuxq/8IHOiR8TPgNFDPhaj1qGAex4W+R9zgKNBbsSZRJDozUb8NKAbRLmX3rTGuSO6iE0OEV9TTHlhoNVCQX8lpVsMJcfHVuzKJbZ6yypmcTmA08WOFKdUSEYJ3D/Yg8+zCuxPEfnFncbqilb+6A9jgckdaBf5xj9dTML6cy6q36WoHH+L+Xdbafs8z1qlTugIxYahlKxggU3Wgveaep/ezP51SzDTMgzhcUQ/KvfHJiHMM9JYQftet0TktztolWKVp4YYwWwvc87jrSonQ3Wnvy70T2jWbqcvXEdLq4qCk8c1UbvInM33qomdohCSeJysRFK5DuwhmQlMgo+rVN1ZTMK31epottl/B1j0jO8oHjGxUO2GQnP3C0SUmawjlfLeUaL2/KkJlhEhhGW46S3EcYf9x89knv7QAtRt7WwDZYBIkmnrHI4labw0TUXBuCNccBGCP1Hw4LO64ARWSR84Et13PfjvtxxB8T37NglLr+Cx4MNM= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:SJ2PR17MB6568.namprd17.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230028)(366004)(396003)(136003)(39840400004)(376002)(346002)(451199021)(66556008)(66476007)(66946007)(6916009)(4326008)(84970400001)(8936002)(8676002)(316002)(54906003)(478600001)(41300700001)(6666004)(966005)(2906002)(86362001)(6512007)(6506007)(26005)(1076003)(6486002)(44832011)(38100700002)(186003)(83380400001)(5660300002)(36756003)(2616005); DIR:OUT; SFP:1101 X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: GXMJmQwVyCQmWxdrNq/s+7gSqzSm2RUdrEQuCDSrIRzlkErcXAJB4MebA8/xlN02rSE9QBYXSoFRcX8YHh7T1rhLOjKwlpVtuaiImr5n0tMirHx0Lsedoaig7W9FV6FasiBksJ8JJfyvZYehf07BM5el1p7sK3PSwN9nhf/WaZG2IyduLr5psdeE0SRE2SR1IvQ9xmHdokC+hh+P4xevO4aM/hCVXVYcK61cU3NKlkNvr1yfMd3zZ+1e/pY9G/o26bEk2vOad6SvUIUIe1Q+MNgPFG9x94D25DNIRblqO8PqM33NlXqdt2IFtBFSytobnwihGg2p2v3+9Na3PdQ/9yk94EwQ++JCCEPG9bh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X71sOVLjnhmGJWtuMrD2LcQO/CU/YlMhcYbBVzIfYZSru2DzIKMvgz3TYtRFiTgk00vSl5yvxt8rY5kQp+IWfVCgiYxYhP5bjd0BcqGI03MDzcJ4q7YZ7WOm2WJwHT25tESwK0YZp3N75p6JUGF1gNvhDj9BYZuzUW6ua3Wp6wf+D0Kow5B X-OriginatorOrg: labware.com X-MS-Exchange-CrossTenant-Network-Message-Id: e0444f0e-a36b-44c8-fd39-08db586639a7 X-MS-Exchange-CrossTenant-AuthSource: SJ2PR17MB6568.namprd17.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 May 2023 12:40:28.1497 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: b5db0322-1aa0-4c0a-859c-ad0f96966f4c X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: RvZlZbLy9lowcE0GD8E0xuxrvprWqx9ZuMKpZYrOeBigKkw9GN8yyWcxArKJo31K9dvAujA/OCtgp+6G76P8vQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ0PR17MB5126 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: labware.com X-Spam-Status: No, score=-12.8 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE 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: Jan Vrany via Gdb-patches From: Jan Vrany Reply-To: Jan Vrany Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" Commit b5661ff2 ("gdb: fix possible use-after-free when executing commands") attempted to fix possible use-after-free in case command redefines itself. Commit 37e5833d ("gdb: fix command lookup in execute_command ()") updated the previous fix to handle subcommands as well by using the original command string to lookup the command again after its execution. This fixed the test in gdb.base/define.exp but it turned out that it does not work (at least) for "target remote" and "target extended-remote". The problem is that the command buffer P passed to execute_command () gets overwritten in dont_repeat () while executing "target remote" command itself: #0 dont_repeat () at top.c:822 #1 0x000055555730982a in target_preopen (from_tty=1) at target.c:2483 #2 0x000055555711e911 in remote_target::open_1 (name=0x55555881c7fe ":1234", from_tty=1, extended_p=0) at remote.c:5946 #3 0x000055555711d577 in remote_target::open (name=0x55555881c7fe ":1234", from_tty=1) at remote.c:5272 #4 0x00005555573062f2 in open_target (args=0x55555881c7fe ":1234", from_tty=1, command=0x5555589d0490) at target.c:853 #5 0x0000555556ad22fa in cmd_func (cmd=0x5555589d0490, args=0x55555881c7fe ":1234", from_tty=1) at cli/cli-decode.c:2737 #6 0x00005555573487fd in execute_command (p=0x55555881c802 "4", from_tty=1) at top.c:688 Therefore the second call to lookup_cmd () at line 697 fails to find command because the original command string is gone. This commit addresses this particular problem by creating a *copy* of original command string for the sole purpose of using it after command execution to lookup the command again. It may not be the most efficient way but it's safer given that command buffer is shared and overwritten in hard-to-foresee situations. Tested on x86_64-linux. PR 30249 Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30249 Approved-By: Tom Tromey --- gdb/top.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/gdb/top.c b/gdb/top.c index 0b819091d11..92de30a1472 100644 --- a/gdb/top.c +++ b/gdb/top.c @@ -471,6 +471,8 @@ execute_command (const char *p, int from_tty) return; } + std::string cmd_copy = p; + target_log_command (p); while (*p == ' ' || *p == '\t') @@ -577,7 +579,7 @@ execute_command (const char *p, int from_tty) We need to lookup the command again since during its execution, a command may redefine itself. In this case, C pointer becomes invalid so we need to look it up again. */ - const char *cmd2 = cmd_start; + const char *cmd2 = cmd_copy.c_str (); c = lookup_cmd (&cmd2, cmdlist, "", nullptr, 1, 1); if (c != nullptr) execute_cmd_post_hook (c);