From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca (simark.ca [158.69.221.121]) by sourceware.org (Postfix) with ESMTPS id 4B1D63858295 for ; Wed, 2 Nov 2022 17:44:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 4B1D63858295 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=simark.ca Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=simark.ca Received: from [172.16.0.64] (192-222-180-24.qc.cable.ebox.net [192.222.180.24]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPSA id BDE741E0D3; Wed, 2 Nov 2022 13:44:50 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=simark.ca; s=mail; t=1667411091; bh=06MigEQVN/yesZEc+ZE9oINUTw7UaMMFwZgcD11do8o=; h=Date:Subject:To:References:From:In-Reply-To:From; b=utVvN0jov4UnIazd2VUM0Yn2NIL/IeRkZNt06z92wyNkqYjk1Yn6iROIuja2E5ejG t/k4OAqDXjKGk2pjKgGMoeYiob//88B/UxoOB+jdkYnhfqO/tNfh9Q8IO87d5CPvXN qRTBKa8YbnX9fVNOQm3rDJozfyoZjQRlVD+KqujU= Message-ID: <376afc60-6910-56b2-bad5-b9850d2999b5@simark.ca> Date: Wed, 2 Nov 2022 13:44:50 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.4.1 Subject: Re: [PATCH, v2] [PR gdb/29272] Make sure a copy_insn_closure is available when we have a match in copy_insn_closure_by_addr Content-Language: fr To: Luis Machado , gdb-patches@sourceware.org, simon.marchi@efficios.com References: <20221026084100.28009-1-luis.machado@arm.com> <20221102143341.2807182-1-luis.machado@arm.com> From: Simon Marchi In-Reply-To: <20221102143341.2807182-1-luis.machado@arm.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-11.2 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,NICE_REPLY_A,SPF_HELO_PASS,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: 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