Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Tom Tromey <tromey@adacore.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tromey@adacore.com>
Subject: [PATCH] Avoid possible use-after-free in Ravenscar code
Date: Mon,  9 Nov 2026 12:35:05 -0700	[thread overview]
Message-ID: <20261109193505.1693202-1-tromey@adacore.com> (raw)

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


                 reply	other threads:[~2026-11-09 19:35 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261109193505.1693202-1-tromey@adacore.com \
    --to=tromey@adacore.com \
    --cc=gdb-patches@sourceware.org \
    /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