Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Andrew Burgess <aburgess@redhat.com>
To: gdb-patches@sourceware.org
Cc: Andrew Burgess <aburgess@redhat.com>
Subject: [PATCH 2/5] gdbserver: merge look_up_one_symbol and relocate_instruction packet handling
Date: Fri, 20 Nov 2026 15:12:47 +0000	[thread overview]
Message-ID: <18919cf28001b47de5aa101126ad7a17a69a469a.1795186852.git.aburgess@redhat.com> (raw)
In-Reply-To: <cover.1795186852.git.aburgess@redhat.com>

Both the look_up_one_symbol and relocate_instruction functions contain a
loop which handles a limited number of packets received from GDB.  This
commit merges these loops in order to reduce code duplication.  A
follow-up commit fixes a buffer overflow bug in this code.  The bug was
present at both locations, merging the code means only one site needs
fixing.  The new merged code is added in a new function
handle_limited_packets_while_waiting.

There is a potentially user-visible change in this commit though.
Previously look_up_one_symbol handled m, x, and v packets only, while
relocate_instruction handled m, M, x, and X packets.  By merging the
code both functions now handle the superset of these packets, m, M, x,
X, and v.  Both functions contained a comment saying something along the
lines of "we should really handle all packets here, but for now these
will do", I assume the current lists are based on what GDB happens to
send.  Supporting more packets in each case should not have any negative
consequences.  If it turns out that there are problems caused by
accepting this wider set of packets then we can add a packet filter
argument to handle_limited_packets_while_waiting, but I'd prefer to only
do this once we know there's a need for it.

As part of the commit, the result of sending the reply to a 'v' packet
is now error checked; previously, the look_up_one_symbol loop ignored a
failure to send this reply.

Additionally, while thinking about the 'v' packet handling, I wondered
what would happen if we received a vCont or vKill while we were within
look_up_one_symbol or relocate_instruction, and I suspect the outcome
would not be good.  If the inferior disappeared then these functions
might end up trying to operate on freed process state.  The 'v' packet
handling was added in commit:

  commit 28170b88cc8b40fdea2b065dafe6e1872a47ee4e
  Date:   Sat Mar 12 14:03:26 2016 +0100

    gdbserver: Handle 'v' packet while processing qSymbol.

The commit message talks about needing vFile support.  So, while making
this merge I have made the 'v' packet handling stricter.  We now drop
out of the handle_limited_packets_while_waiting loop for vCont, vAttach,
vRun, or vKill.  All other 'v' packets (including vFile) are handled as
before.

To the best of my knowledge, existing GDB does not send any packets
other than m, M, x, X, or v during these two exchanges.  If it did, the
old code would have rejected the packet as a malformed response, and the
resulting symbol lookup or instruction relocation failure would most
likely have shown up as a failing test somewhere.  There should
therefore be no visible changes after this commit.
---
 gdbserver/remote-utils.cc | 294 ++++++++++++++++++--------------------
 1 file changed, 138 insertions(+), 156 deletions(-)

diff --git a/gdbserver/remote-utils.cc b/gdbserver/remote-utils.cc
index 8192e4577f2..d1b17a33e2b 100644
--- a/gdbserver/remote-utils.cc
+++ b/gdbserver/remote-utils.cc
@@ -1417,6 +1417,139 @@ clear_symbol_cache (struct sym_cache **symcache_p)
   *symcache_p = NULL;
 }
 
+/* In some cases a packet from the debug client triggers a prolonged
+   packet exchange between gdbserver and the client.  For example,
+   gdbserver can send a qRelocInsn request in response to some
+   packets.  When the debug client receives the qRelocInsn request it
+   will perform a series of memory accesses using x, X, m, or M
+   packets before finally sending its own qRelocInsn reply.
+
+   Similarly, the debug client can send 'qSymbol::'; gdbserver will
+   then request symbol values, which the debug client provides with
+   another qSymbol packet.  Before sending the qSymbol packet, the
+   debug client might access memory or send 'v' packets (for vFile
+   transfers).
+
+   This function takes care of handling the limited set of packets
+   that might arrive while waiting for the final response packet.
+   Returns false if there is a communication error; otherwise returns
+   true, and the first packet that is not one of those handled here is
+   left, unprocessed, in the client_state's OWN_BUF for the caller to
+   handle.
+
+   We ought to handle pretty much any packet at this point while we
+   wait for the final response, but that requires re-entering the main
+   loop.  For now, this is an adequate approximation.  */
+
+static bool
+handle_limited_packets_while_waiting ()
+{
+  client_state &cs = get_client_state ();
+
+  bool leave_packet_handling_loop = false;
+  while (!leave_packet_handling_loop)
+    {
+      int len = getpkt (cs.own_buf);
+      if (len < 0)
+	return false;
+
+      CORE_ADDR mem_addr;
+      unsigned char *mem_buf = nullptr;
+      unsigned int mem_len;
+      int new_len = -1;
+
+      switch (cs.own_buf[0])
+	{
+	case 'm':
+	  {
+	    decode_m_packet (&cs.own_buf[1], &mem_addr, &mem_len);
+	    mem_buf = (unsigned char *) xmalloc (mem_len);
+	    if (read_inferior_memory (mem_addr, mem_buf, mem_len) == 0)
+	      bin2hex (mem_buf, cs.own_buf, mem_len);
+	    else
+	      write_enn (cs.own_buf);
+	  }
+	  break;
+	case 'x':
+	  {
+	    decode_x_packet (&cs.own_buf[1], &mem_addr, &mem_len);
+	    mem_buf = (unsigned char *) xmalloc (mem_len);
+	    if (read_inferior_memory (mem_addr, mem_buf, mem_len) == 0)
+	      {
+		gdb_byte *buffer = (gdb_byte *) cs.own_buf;
+		*buffer++ = 'b';
+
+		int out_len_units;
+		new_len = remote_escape_output (mem_buf, mem_len, 1,
+						buffer,
+						&out_len_units,
+						PBUFSIZ);
+		new_len++; /* For the 'b' marker.  */
+
+		if (out_len_units != mem_len)
+		  {
+		    write_enn (cs.own_buf);
+		    new_len = -1;
+		  }
+		else
+		  suppress_next_putpkt_log ();
+	      }
+	    else
+	      write_enn (cs.own_buf);
+	  }
+	  break;
+	case 'X':
+	  {
+	    if (decode_X_packet (&cs.own_buf[1], len - 1, &mem_addr,
+				 &mem_len, &mem_buf) < 0
+		|| target_write_memory (mem_addr, mem_buf, mem_len) != 0)
+	      write_enn (cs.own_buf);
+	    else
+	      write_ok (cs.own_buf);
+	  }
+	  break;
+	case 'M':
+	  {
+	    decode_M_packet (&cs.own_buf[1], &mem_addr, &mem_len, &mem_buf);
+	    if (target_write_memory (mem_addr, mem_buf, mem_len) == 0)
+	      write_ok (cs.own_buf);
+	    else
+	      write_enn (cs.own_buf);
+	  }
+	  break;
+	case 'v':
+	  /* Don't service packets that change which process we are
+	     debugging, or its execution state, while we are in the middle
+	     of another operation.  Handling these here could leave the
+	     caller operating on freed, or stale, state.  Any other 'v'
+	     packets, e.g. vFile, are fine to handle.  */
+	  if (startswith (cs.own_buf, "vCont;")
+	      || startswith (cs.own_buf, "vAttach;")
+	      || startswith (cs.own_buf, "vRun;")
+	      || startswith (cs.own_buf, "vKill;"))
+	    leave_packet_handling_loop = true;
+	  else
+	    handle_v_requests (cs.own_buf, len, &new_len);
+	  break;
+	default:
+	  leave_packet_handling_loop = true;
+	  break;
+	}
+      free (mem_buf);
+
+      if (!leave_packet_handling_loop)
+	{
+	  int res = ((new_len == -1)
+		     ? putpkt (cs.own_buf)
+		     : putpkt_binary (cs.own_buf, new_len));
+	  if (res < 0)
+	    return false;
+	}
+    }
+
+  return true;
+}
+
 /* Get the address of NAME, and return it in ADDRP if found.  if
    MAY_ASK_GDB is false, assume symbol cache misses are failures.
    Returns 1 if the symbol is found, 0 if it is not, -1 on error.  */
@@ -1426,7 +1559,6 @@ look_up_one_symbol (const char *name, CORE_ADDR *addrp, int may_ask_gdb)
 {
   client_state &cs = get_client_state ();
   char *p, *q;
-  int len;
   struct sym_cache *sym;
   struct process_info *proc;
 
@@ -1452,83 +1584,9 @@ look_up_one_symbol (const char *name, CORE_ADDR *addrp, int may_ask_gdb)
   if (putpkt (cs.own_buf) < 0)
     return -1;
 
-  len = getpkt (cs.own_buf);
-  if (len < 0)
+  if (!handle_limited_packets_while_waiting ())
     return -1;
 
-  /* We ought to handle pretty much any packet at this point while we
-     wait for the qSymbol "response".  That requires re-entering the
-     main loop.  For now, this is an adequate approximation; allow
-     GDB to read from memory and handle 'v' packets (for vFile transfers)
-     while it figures out the address of the symbol.  */
-  while (1)
-    {
-      CORE_ADDR mem_addr;
-      unsigned char *mem_buf;
-      unsigned int mem_len;
-      int new_len = -1;
-
-      if (cs.own_buf[0] == 'm')
-	{
-	  decode_m_packet (&cs.own_buf[1], &mem_addr, &mem_len);
-	  mem_buf = (unsigned char *) xmalloc (mem_len);
-	  if (read_inferior_memory (mem_addr, mem_buf, mem_len) == 0)
-	    bin2hex (mem_buf, cs.own_buf, mem_len);
-	  else
-	    write_enn (cs.own_buf);
-	  free (mem_buf);
-	  if (putpkt (cs.own_buf) < 0)
-	    return -1;
-	}
-      else if (cs.own_buf[0] == 'x')
-	{
-	  decode_x_packet (&cs.own_buf[1], &mem_addr, &mem_len);
-	  mem_buf = (unsigned char *) xmalloc (mem_len);
-	  if (read_inferior_memory (mem_addr, mem_buf, mem_len) == 0)
-	    {
-	      gdb_byte *buffer = (gdb_byte *) cs.own_buf;
-	      *buffer++ = 'b';
-
-	      int out_len_units;
-	      new_len = remote_escape_output (mem_buf, mem_len, 1,
-					      buffer,
-					      &out_len_units,
-					      PBUFSIZ);
-	      new_len++; /* For the 'b' marker.  */
-
-	      if (out_len_units != mem_len)
-		{
-		  write_enn (cs.own_buf);
-		  new_len = -1;
-		}
-	      else
-		suppress_next_putpkt_log ();
-	    }
-	  else
-	    write_enn (cs.own_buf);
-
-	  free (mem_buf);
-	  int res = ((new_len == -1)
-		     ? putpkt (cs.own_buf)
-		     : putpkt_binary (cs.own_buf, new_len));
-	  if (res < 0)
-	    return -1;
-	}
-      else if (cs.own_buf[0] == 'v')
-	{
-	  handle_v_requests (cs.own_buf, len, &new_len);
-	  if (new_len != -1)
-	    putpkt_binary (cs.own_buf, new_len);
-	  else
-	    putpkt (cs.own_buf);
-	}
-      else
-	break;
-      len = getpkt (cs.own_buf);
-      if (len < 0)
-	return -1;
-    }
-
   if (!startswith (cs.own_buf, "qSymbol:"))
     {
       warning (_("Malformed response to qSymbol, ignoring: %s"), cs.own_buf);
@@ -1572,7 +1630,6 @@ int
 relocate_instruction (CORE_ADDR *to, CORE_ADDR oldloc)
 {
   client_state &cs = get_client_state ();
-  int len;
   ULONGEST written = 0;
 
   /* Send the request.  */
@@ -1581,87 +1638,12 @@ relocate_instruction (CORE_ADDR *to, CORE_ADDR oldloc)
   if (putpkt (cs.own_buf) < 0)
     return -1;
 
-  len = getpkt (cs.own_buf);
-  if (len < 0)
+  if (!handle_limited_packets_while_waiting ())
     return -1;
 
-  /* We ought to handle pretty much any packet at this point while we
-     wait for the qRelocInsn "response".  That requires re-entering
-     the main loop.  For now, this is an adequate approximation; allow
-     GDB to access memory.  */
-  while (cs.own_buf[0] == 'm' || cs.own_buf[0] == 'M'
-	 || cs.own_buf[0] == 'X' || cs.own_buf[0] == 'x')
-    {
-      CORE_ADDR mem_addr;
-      unsigned char *mem_buf = NULL;
-      unsigned int mem_len;
-      int new_len = -1;
-
-      if (cs.own_buf[0] == 'm')
-	{
-	  decode_m_packet (&cs.own_buf[1], &mem_addr, &mem_len);
-	  mem_buf = (unsigned char *) xmalloc (mem_len);
-	  if (read_inferior_memory (mem_addr, mem_buf, mem_len) == 0)
-	    bin2hex (mem_buf, cs.own_buf, mem_len);
-	  else
-	    write_enn (cs.own_buf);
-	}
-      else if (cs.own_buf[0] == 'x')
-	{
-	  decode_x_packet (&cs.own_buf[1], &mem_addr, &mem_len);
-	  mem_buf = (unsigned char *) xmalloc (mem_len);
-	  if (read_inferior_memory (mem_addr, mem_buf, mem_len) == 0)
-	    {
-	      gdb_byte *buffer = (gdb_byte *) cs.own_buf;
-	      *buffer++ = 'b';
-
-	      int out_len_units;
-	      new_len = remote_escape_output (mem_buf, mem_len, 1,
-					      buffer,
-					      &out_len_units,
-					      PBUFSIZ);
-	      new_len++; /* For the 'b' marker.  */
-
-	      if (out_len_units != mem_len)
-		{
-		  write_enn (cs.own_buf);
-		  new_len = -1;
-		}
-	      else
-		suppress_next_putpkt_log ();
-	    }
-	  else
-	    write_enn (cs.own_buf);
-	}
-      else if (cs.own_buf[0] == 'X')
-	{
-	  if (decode_X_packet (&cs.own_buf[1], len - 1, &mem_addr,
-			       &mem_len, &mem_buf) < 0
-	      || target_write_memory (mem_addr, mem_buf, mem_len) != 0)
-	    write_enn (cs.own_buf);
-	  else
-	    write_ok (cs.own_buf);
-	}
-      else
-	{
-	  decode_M_packet (&cs.own_buf[1], &mem_addr, &mem_len, &mem_buf);
-	  if (target_write_memory (mem_addr, mem_buf, mem_len) == 0)
-	    write_ok (cs.own_buf);
-	  else
-	    write_enn (cs.own_buf);
-	}
-      free (mem_buf);
-
-      int res = ((new_len == -1)
-		 ? putpkt (cs.own_buf)
-		 : putpkt_binary (cs.own_buf, new_len));
-      if (res < 0)
-	return -1;
-      len = getpkt (cs.own_buf);
-      if (len < 0)
-	return -1;
-    }
-
+  /* When gdbserver sends qRelocInsn back to GDB, GDB itself can reply
+     with an error packet, starting with 'E'.  If this happens, warn
+     the user and return.  */
   if (cs.own_buf[0] == 'E')
     {
       warning (_("An error occurred while relocating an instruction: %s"),
-- 
2.56.0


  parent reply	other threads:[~2026-11-20 15:13 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-20 15:12 [PATCH 0/5] gdbserver: fix memory bugs in x, X, m, and M " Andrew Burgess
2026-11-20 15:12 ` [PATCH 1/5] gdbserver: avoid buffer overflow from 'm' and 'x' packets Andrew Burgess
2026-11-20 15:37   ` Eli Zaretskii
2026-11-23  5:26   ` Kevin Buettner
2026-11-20 15:12 ` Andrew Burgess [this message]
2026-11-25  5:55   ` [PATCH 2/5] gdbserver: merge look_up_one_symbol and relocate_instruction packet handling Kevin Buettner
2026-11-26 14:58     ` Andrew Burgess
2026-11-20 15:12 ` [PATCH 3/5] gdbserver: fix buffer overflow in more m/x " Andrew Burgess
2026-11-25  7:24   ` Kevin Buettner
2026-11-20 15:12 ` [PATCH 4/5] gdbserver: reject X packets with an invalid declared length Andrew Burgess
2026-11-25  7:48   ` Kevin Buettner
2026-11-25  7:48   ` Kevin Buettner
2026-11-20 15:12 ` [PATCH 5/5] gdbserver: reject M " Andrew Burgess
2026-11-25  8:03   ` Kevin Buettner

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=18919cf28001b47de5aa101126ad7a17a69a469a.1795186852.git.aburgess@redhat.com \
    --to=aburgess@redhat.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