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 E7A5E3858C5E for ; Fri, 22 Dec 2023 16:36:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E7A5E3858C5E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=simark.ca Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=simark.ca ARC-Filter: OpenARC Filter v1.0.0 sourceware.org E7A5E3858C5E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=158.69.221.121 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1703263000; cv=none; b=suqyloh7LIVBBpG2N/azk81oyF05DHl8ZHretiAIuwrnISjmngY+C2rR0WZJyII5XWuxabyhghr2cQImPHI/wa/H0DJZx7bVVqhva8EY1OQV8WbMXnhsP76AC4Z8SGiV0BbEabzD17/Gti+hd7XrsEAo8Rz/evrgB05FMlMrL9Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1703263000; c=relaxed/simple; bh=35xAyCsUpFaO7gKwAhHTl/b1kWWd9JBc3HS4nh+Mlag=; h=DKIM-Signature:Message-ID:Date:MIME-Version:Subject:To:From; b=djuUDyui0ro2MtW6QrlPJTidYU61v8yLVaOY8cYwK6pEXjL194SRbIsokENohoxynxUXn6os7xuwurT+evwoIgQw2OBKEtzvCfb13IZAc8NH7sfO0xMObueQdFVFij3RzASRpOBNn/WiEBUCqBxoag20qiqzqLVBl25T5OVDrhA= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=simark.ca; s=mail; t=1703262998; bh=35xAyCsUpFaO7gKwAhHTl/b1kWWd9JBc3HS4nh+Mlag=; h=Date:Subject:To:References:From:In-Reply-To:From; b=lEZnHdG7YR9kaye586HAxz9VhuwZZ5RdTm+VHRm1YqBxLpWYPct/wXOz0oVkYA7OW +2ZNcjYk+SQ0nJ38sD6KfiL2a3j6roY/1VKvYyW7DblkEj8ntGCJ3rK25Ws38/YEKi I7mt9j1jZgscPf7l3Q3CPGBRcc0FmkknbmsvOgp4= Received: from [10.0.0.11] (modemcable238.237-201-24.mc.videotron.ca [24.201.237.238]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPSA id 564CB1E0AC; Fri, 22 Dec 2023 11:36:38 -0500 (EST) Message-ID: <2f09d501-14c8-40be-a43e-a3fc45fef0c2@simark.ca> Date: Fri, 22 Dec 2023 11:36:37 -0500 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 1/1] gdb: fix assert after register write failure Content-Language: en-US To: Eduard Sargsyan , gdb-patches@sourceware.org References: <20231219151448.23519-1-eduard.sargsyan@intel.com> <20231219151448.23519-2-eduard.sargsyan@intel.com> From: Simon Marchi In-Reply-To: <20231219151448.23519-2-eduard.sargsyan@intel.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-10.8 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,SPF_HELO_PASS,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 List-Id: On 2023-12-19 10:14, Eduard Sargsyan wrote: > GDB tries to invalidate register cache if write to register fails. In case > when write was requested to be done for a remote inferior and connection with > remote is dead (so write request will fail) GDB cleans register caches and as > result setting register status fails with segmentation fault, because internal > pointers are not valid anymore and the unique_ptr with register status is > null. > > This change adds check if register status cache is still valid before trying to > modify (set status to UNKNOWN). > > gdb/ChangeLog: > 2021-03-18 Eduard Sargsyan > > * regcache.c (regcache::raw_write): Add check for pointer validity. > --- > gdb/regcache.c | 7 +++++-- > 1 file changed, 5 insertions(+), 2 deletions(-) > > diff --git a/gdb/regcache.c b/gdb/regcache.c > index 6140a05f02b..94ca89a58de 100644 > --- a/gdb/regcache.c > +++ b/gdb/regcache.c > @@ -882,8 +882,11 @@ regcache::raw_write (int regnum, gdb::array_view src) > > /* Invalidate the register after it is written, in case of a > failure. */ > - auto invalidator > - = make_scope_exit ([&] { this->invalidate (regnum); }); > + auto invalidator = make_scope_exit ([&] > + { > + if (this->m_register_status != nullptr) > + this->invalidate (regnum); > + }); > > target_store_registers (this, regnum); > So, there's no place I can see where the regcache will reset its m_register_status field to nullptr. So my guess of what happens is that when something wrong happens with the network connection inside target_store_registers, the remote target closes, and somewhere in the process, it destroys all regcaches bound to it (including the regcache we are "in" right now). This calls m_register_status' destructor, which resets the internal pointer to nullptr. When you reach the invalidator code, the regcache has been deleted, so even accessing this->m_register_status would be invalid, *this has been deleted. If you run this with an ASan-enabled build, do you get a use-after-free error? Simon