From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id cXQuFXot62rk8RgAWB0awg (envelope-from ) for ; Wed, 04 Nov 2026 08:04:58 -0500 Authentication-Results: simark.ca; dkim=pass (2048-bit key; unprotected) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=QLSAsrkd; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 50F31378002; Wed, 04 Nov 2026 08:04:58 -0500 (EST) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-6.4 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham autolearn_force=no version=4.0.1 Received: from vm01.sourceware.org (vm01.sourceware.org [IPv6:2620:52:6:3111::32]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 751E71E052 for ; Wed, 04 Nov 2026 08:04:57 -0500 (EST) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id BDB754BA2E27 for ; Wed, 4 Nov 2026 13:04:55 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BDB754BA2E27 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=QLSAsrkd Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.16]) by sourceware.org (Postfix) with ESMTPS id F2E234BA2E33 for ; Wed, 4 Nov 2026 13:04:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org F2E234BA2E33 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=intel.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org F2E234BA2E33 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=198.175.65.16 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1793797465; cv=none; b=YfV0i55Z6g650IP8UcGuaTo/3EqSbTJ6uAk9sfJEVaDN6hva16X0RvlEAQy5I34LIDNx5YWfz6cf0ovrZstdlW5N2fgc934dmmygiLhenxsdIMmI9C+Yb5ByWpPktCZpIHV9klZMqGY5AgEBqrSYKPy2JYMbUh/fkY6kYrQkYI4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1793797465; c=relaxed/simple; bh=AgSUyYd/qscBuYhgl3dNGfVc3hCLGqYFglly2J18Xis=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=RWF+mVKXjK6Z//r9Fu/EhsnfNtGMUuU0NUncgANRR0/ET2DwebPWmrYITvnD5duIj+C/+fgWs+I4z+Ap6GbSJpglsWEUXEhmZXSxQZZSf4tfKbgGJRPGVyr1riddQBbJYKXXrCDLeZg9+6PLUahYr5SJM252QS5HlH/lI754cMQ= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=QLSAsrkd DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org F2E234BA2E33 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1793797465; x=1825333465; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=AgSUyYd/qscBuYhgl3dNGfVc3hCLGqYFglly2J18Xis=; b=QLSAsrkdLMIumULWseJiGlM0sqpCYDtL9v0pXr9Pi89z1Zar7CNm1b7H w53fwyMXOpt14dLJlzG0dFIQ5XadcR3fhd0/CnZQ9MX89WCrahbdvBqRf aVEt0wsVv+ijUprR8JZ5wHhHOKP6dfgevd5Mn54ZxRxcPekZkSmTDh4nw 1th+SLtop/j/FVzztiNc1nXQrpnI1+9Qqav9wyHh8D8NHk5k2h/HYABrE j9gUVD15S3wJjDNXQxV2P129tmfbTcmWpOfs/ROiHKxnFvTA3/5ZJAqlB PQvfLVIHe8dXeJ52T0wH3kdGNkWXTYfBoFdTrDuA7ql/GCLUiwwvgfOXs w==; X-CSE-ConnectionGUID: Gg0YsgU+Siui26j0tXp68A== X-CSE-MsgGUID: a4N/pJ0rTDK21ZaYT1aYxg== X-IronPort-AV: E=McAfee;i="6800,10657,11955"; a="2543941" X-IronPort-AV: E=Sophos;i="6.27,195,1787036400"; d="scan'208";a="2543941" Received: from fmviesa003.fm.intel.com ([10.60.135.143]) by orvoesa108.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Nov 2026 05:04:07 -0800 X-CSE-ConnectionGUID: rv9xqoQdSq6b2FxGYYYe/Q== X-CSE-MsgGUID: DuqZCq95Q3KevH+HU0/BQA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.27,195,1787036400"; d="scan'208";a="6854965" Received: from dut1048pvc.igk.intel.com (HELO localhost) ([10.211.177.228]) by smtpauth.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Nov 2026 05:04:06 -0800 From: Stephan Rohr To: gdb-patches@sourceware.org Cc: tom@tromey.com, guinevere@redhat.com Subject: [PATCH v3 1/1] gdb: fix assert after register write failure Date: Wed, 4 Nov 2026 13:03:44 +0000 Message-ID: <20261104130344.44218-2-stephan.rohr@intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261104130344.44218-1-stephan.rohr@intel.com> References: <20261104130344.44218-1-stephan.rohr@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit 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: , Errors-To: gdb-patches-bounces~public-inbox=simark.ca@sourceware.org From: Eduard Sargsyan GDB tries to invalidate the register cache if a write to a register fails. In case the connection to a remote inferior is closed during the write, the remote target is unpushed, which deletes the regcache. GDB receives a TARGET_CLOSE_ERROR. With this, the regcache is deleted and GDB runs into a use-after-free bug; the previously set up make_scope_exit invalidator writes to the already deleted regcache. Fix this by catching the exception and only invalidating the regcache if the error is not a TARGET_CLOSE_ERROR. Co-Authored-By: Stephan Rohr --- gdb/regcache.c | 22 ++++++++++++---------- gdb/testsuite/gdb.server/server-kill.exp | 20 ++++++++++++++++++++ 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/gdb/regcache.c b/gdb/regcache.c index 773e8ac37d0..abdd49190ee 100644 --- a/gdb/regcache.c +++ b/gdb/regcache.c @@ -881,16 +881,18 @@ regcache::raw_write (int regnum, gdb::array_view src) target_prepare_to_store (this); raw_supply (regnum, src); - /* Invalidate the register after it is written, in case of a - failure. */ - auto invalidator - = make_scope_exit ([&] { this->invalidate (regnum); }); - - target_store_registers (this, regnum); - - /* The target did not throw an error so we can discard invalidating - the register. */ - invalidator.release (); + try + { + target_store_registers (this, regnum); + } + catch (const gdb_exception_error &ex) + { + /* The write failed. If the target is closed, the regcache is + destroyed and is not accessible. */ + if (ex.error != TARGET_CLOSE_ERROR) + invalidate (regnum); + throw; + } } void diff --git a/gdb/testsuite/gdb.server/server-kill.exp b/gdb/testsuite/gdb.server/server-kill.exp index d4bbb466f04..a03e04ad63e 100644 --- a/gdb/testsuite/gdb.server/server-kill.exp +++ b/gdb/testsuite/gdb.server/server-kill.exp @@ -148,7 +148,27 @@ proc_with_prefix test_stepi {} { gdb_test "stepi" "(Target disconnected|Remote connection closed|Remote communication error).*" } +# Regression test for a use-after-free bug in the regcache when the remote +# connection is dropped during register write. The regcache is destroyed in +# this case. + +proc_with_prefix test_register_write {} { + if {![prepare]} { + return + } + + # Ensure the frame-id is cached before killing gdbserver. Otherwise, + # GDB notices the dead connection when computing the frame-id. + gdb_test "info frame" "Stack level 0, .*" + kill_server + + # Write a register to the already killed remote connection. + gdb_test "set \$pc = \$pc + 1" \ + "(Target disconnected|Remote (connection closed|communication error)).*" +} + test_tstatus test_unwind_nosyms test_unwind_syms test_stepi +test_register_write -- 2.43.0 ________________________________________ Intel Deutschland GmbH Registered Address: Dornacher Strasse 1, 85622 Feldkirchen, Germany Tel: +49 (89) 99143-0 www.intel.de Managing Directors: Candice Moore, Jeffrey Schneiderman, Ramachandran Sitaraman Chairperson of the Supervisory Board: Sonja Pierer Registered Seat: Munich Commercial Register B: Amtsgericht Munich HRB 186928 This e-mail and any attachments may contain confidential material for the sole use of the intended recipient(s). Any review or distribution by others is strictly prohibited. If you are not the intended recipient, please contact the sender and delete all copies.