From: Stephan Rohr <stephan.rohr@intel.com>
To: gdb-patches@sourceware.org
Cc: tom@tromey.com, guinevere@redhat.com
Subject: [PATCH v3 0/1] gdb: fix assert after register write failure
Date: Wed, 4 Nov 2026 13:03:43 +0000 [thread overview]
Message-ID: <20261104130344.44218-1-stephan.rohr@intel.com> (raw)
From: "Rohr, Stephan" <stephan.rohr@intel.com>
Hi Tom, all,
thanks for the feedback.
I can confirm the use-after-free issue you found. The proposed patch
now wraps the target_store_registers call in a try-catch block.
It invalidates the register only if the error is not a TARGET_CLOSE_ERROR.
I checked the "store_registers" implementations for other targets. As
far as I can tell from reading the code, the ravenscar target has the
same use-after-free bug when the target beneath is a remote target,
see ravenscar_thread_target::store_registers ():
temporarily_change_regcache_ptid changer (regcache, base);
beneath ()->store_registers (regcache, i);
If the remote write fails in beneath ()->store_registers (), the
destructor of temporarily_change_regcache_ptid calls set_ptid on the
already destroyed regcache. fetch_registers and prepare_to_store use
the same pattern. I think this should be fixed separately.
IIUC, on the store_registers path, the regcache can only be deleted if
the remote target is unpushed, which is always followed by a
TARGET_CLOSE_ERROR. If we want to be on the safe side, we could add a
lifetime token to the regcache:
std::shared_ptr<int> m_lifetime = std::make_shared<int> (0);
and take a weak reference to it in raw_write before storing:
std::weak_ptr<int> alive = m_lifetime;
try
{
target_store_registers (this, regnum);
}
catch (const gdb_exception &)
{
if (!alive.expired ())
invalidate (regnum);
throw;
}
We have no evidence that this is needed. I think adding the try-catch
block and checking for a TARGET_CLOSE_ERROR suffices. If you prefer, I
can also add the token.
You can find v2 of the patch at:
https://sourceware.org/pipermail/gdb-patches/2026-October/231112.html
and Tom's feedback:
https://sourceware.org/pipermail/gdb-patches/2026-October/231275.html
Changes since v2:
- Removed the nullptr check and replaced with try-catch in
regcache::raw_write ().
- Added a testcase to gdb.server/server-kill.exp.
- Reworded the commit message.
Thanks
Stephan
Eduard Sargsyan (1):
gdb: fix assert after register write failure
gdb/regcache.c | 22 ++++++++++++----------
gdb/testsuite/gdb.server/server-kill.exp | 20 ++++++++++++++++++++
2 files changed, 32 insertions(+), 10 deletions(-)
--
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 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 Stephan Rohr [this message]
2026-11-04 13:03 ` [PATCH v3 1/1] " Stephan Rohr
2026-11-06 19:01 ` 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-1-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