[binutils-gdb] gdb: fix assert after register write failure
Stephan Rohr
srohr@sourceware.org
Mon Nov 9 12:52:32 GMT 2026
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=45460a213097f19fb10bc5798f127d460f94b00a
commit 45460a213097f19fb10bc5798f127d460f94b00a
Author: Eduard Sargsyan <eduard.sargsyan@intel.com>
Date: Fri Mar 18 16:06:31 2022 +0100
gdb: fix assert after register write failure
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>
Approved-By: Tom Tromey <tom@tromey.com>
Diff:
---
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
More information about the Gdb-cvs
mailing list