* [PATCH] Avoid possible use-after-free in Ravenscar code
@ 2026-11-09 19:35 Tom Tromey
0 siblings, 0 replies; only message in thread
From: Tom Tromey @ 2026-11-09 19:35 UTC (permalink / raw)
To: gdb-patches; +Cc: Tom Tromey
In the discussion for commit 45460a21309 ("gdb: fix assert after
register write failure"), Stephan pointed out that a similar bug
affects the Ravenscar thread layer.
This patch fixes the problem. I don't have a new test for this -- I
don't believe we have any Ravenscar thread tests in the tree, and I
don't think they can be written without a certain runtime.
However, I did test this using a Ravenscar-enabled target and the
internal AdaCore test suite.
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=34973
---
gdb/ravenscar-thread.c | 67 ++++++++++++++++++++++++------------------
1 file changed, 38 insertions(+), 29 deletions(-)
diff --git a/gdb/ravenscar-thread.c b/gdb/ravenscar-thread.c
index b5e0979a7f1..aa2da3a14a7 100644
--- a/gdb/ravenscar-thread.c
+++ b/gdb/ravenscar-thread.c
@@ -602,33 +602,33 @@ ravenscar_arch_ops::store_register (struct regcache *regcache,
stack_address);
}
-/* Temporarily set the ptid of a regcache to some other value. When
- this object is destroyed, the regcache's original ptid is
- restored. */
+/* A wrapper for various target methods that temporarily sets the ptid
+ of a regcache to some other value. If the target does not exit,
+ the regcache ptid is then reset. */
-class temporarily_change_regcache_ptid
+static void
+temporarily_change_regcache_ptid (struct regcache *regcache, ptid_t new_ptid,
+ gdb::function_view<void ()> callback)
{
-public:
-
- temporarily_change_regcache_ptid (struct regcache *regcache, ptid_t new_ptid)
- : m_regcache (regcache),
- m_save_ptid (regcache->ptid ())
- {
- m_regcache->set_ptid (new_ptid);
- }
-
- ~temporarily_change_regcache_ptid ()
- {
- m_regcache->set_ptid (m_save_ptid);
- }
+ ptid_t save = regcache->ptid ();
+ regcache->set_ptid (new_ptid);
-private:
+ try
+ {
+ callback ();
+ }
+ catch (const gdb_exception &ex)
+ {
+ /* Note here we want to catch QUIT as well, just in case. If
+ the target has closed, then the regcache is destroyed and we
+ don't want to update it. */
+ if (ex.error != TARGET_CLOSE_ERROR)
+ regcache->set_ptid (save);
+ throw;
+ }
- /* The regcache. */
- struct regcache *m_regcache;
- /* The saved ptid. */
- ptid_t m_save_ptid;
-};
+ regcache->set_ptid (save);
+}
ravenscar_thread_target::fpu_state
ravenscar_thread_target::get_fpu_state (struct regcache *regcache,
@@ -709,8 +709,11 @@ ravenscar_thread_target::fetch_registers (struct regcache *regcache,
if (use_beneath)
{
- temporarily_change_regcache_ptid changer (regcache, base);
- beneath ()->fetch_registers (regcache, i);
+ temporarily_change_regcache_ptid (regcache, base,
+ [=] ()
+ {
+ beneath ()->fetch_registers (regcache, i);
+ });
}
else
arch_ops->fetch_register (regcache, i);
@@ -755,8 +758,11 @@ ravenscar_thread_target::store_registers (struct regcache *regcache,
if (use_beneath)
{
- temporarily_change_regcache_ptid changer (regcache, base);
- beneath ()->store_registers (regcache, i);
+ temporarily_change_regcache_ptid (regcache, base,
+ [=] ()
+ {
+ beneath ()->store_registers (regcache, i);
+ });
}
else
arch_ops->store_register (regcache, i);
@@ -776,8 +782,11 @@ ravenscar_thread_target::prepare_to_store (struct regcache *regcache)
if (task_is_currently_active (ptid))
{
ptid_t base = get_base_thread_from_ravenscar_task (ptid);
- temporarily_change_regcache_ptid changer (regcache, base);
- beneath ()->prepare_to_store (regcache);
+ temporarily_change_regcache_ptid (regcache, base,
+ [=] ()
+ {
+ beneath ()->prepare_to_store (regcache);
+ });
}
else
{
base-commit: 96248aa4173886a47a7aa1798861267edde28801
--
2.56.0
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2026-11-09 19:35 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-09 19:35 [PATCH] Avoid possible use-after-free in Ravenscar code Tom Tromey
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox