From: Stephan Rohr <stephan.rohr@intel.com>
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 [thread overview]
Message-ID: <20261104130344.44218-2-stephan.rohr@intel.com> (raw)
In-Reply-To: <20261104130344.44218-1-stephan.rohr@intel.com>
From: Eduard Sargsyan <eduard.sargsyan@intel.com>
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 <stephan.rohr@intel.com>
---
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<const gdb_byte> 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.
next prev parent reply other threads:[~2026-11-04 13:04 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-04 13:03 [PATCH v3 0/1] " Stephan Rohr
2026-11-04 13:03 ` Stephan Rohr [this message]
2026-11-06 19:01 ` [PATCH v3 1/1] " Tom Tromey
2026-11-09 19:03 ` Tom Tromey
2026-11-06 19:06 ` [PATCH v3 0/1] " Tom Tromey
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261104130344.44218-2-stephan.rohr@intel.com \
--to=stephan.rohr@intel.com \
--cc=gdb-patches@sourceware.org \
--cc=guinevere@redhat.com \
--cc=tom@tromey.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox