From: Andrew Burgess <aburgess@redhat.com>
To: Kevin Buettner <kevinb@redhat.com>, gdb-patches@sourceware.org
Subject: Re: [PATCH 2/5] gdbserver: merge look_up_one_symbol and relocate_instruction packet handling
Date: Thu, 26 Nov 2026 14:58:10 +0000 [thread overview]
Message-ID: <xhuyfld6fprlp.fsf@aburgess-thinkpadp1gen8.rmtgb.csb> (raw)
In-Reply-To: <93333453-b4d8-40e2-a20f-adabf34d9690@redhat.com>
Kevin Buettner <kevinb@redhat.com> writes:
> On 11/20/26 8:12 AM, Andrew Burgess wrote:
>> 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.
>
> I ran the entire series through an AI patch review. After reading
> through a very dense version of what has now been rewritten to be more
> comprehensible, it appeared to me that you were aware of these concerns.
> Nonetheless, you may wish to read what Claude thought of this patch:
>
> I have one point, and I'd class it as a nit: four 'v' packets now get
> no reply when they arrive during a qSymbol or qRelocInsn exchange.
>
> As I read handle_limited_packets_while_waiting, anything other than
> m, M, x, X, or a 'v' packet ends the loop and is handed back to the
> caller, which expects it to be the qSymbol or qRelocInsn answer.
> With this patch, vCont;, vAttach;, vRun;, and vKill; also end the
> loop instead of being carried out. I agree with the reason you give:
> carrying out any of them in the middle of the exchange could resume,
> kill, or replace the inferior, and leave the caller working on stale
> or freed state. Removing that risk fits well in this series.
>
> The side effect I noticed is that neither caller recognises these
> packets when they come back. Both print "Malformed response ..."
> and give up, and nothing ever replies to the packet itself. In the
> qSymbol case the main loop then sends "OK", so a client that sent
> vCont would get "OK" instead of a stop reply, and the two sides end
> up out of step.
>
> When I compared this with master, I found:
>
> - qSymbol path: this is new with the patch. On master these
> packets were carried out and answered (unsafely, as you point
> out).
>
> - qRelocInsn path: this was already the case. On master that loop
> ended on every 'v' packet and dropped it in the same way.
>
> - More generally, any unexpected packet (a 'g', say) was already
> handled like this in both loops. The patch just puts these four
> packets into that existing group.
>
> I don't think this needs to hold up the series. Your commit message
> covers it: these packets now end the loop, existing GDB sends nothing
> but m, M, x, X, or v during these exchanges, and you'd rather add
> filtering "once we know there's a need for it". GDB can't send vCont
> in the middle of the exchange anyway, because it is busy servicing
> the exchange. What's left is a protocol mix-up with a client that
> has already broken the protocol, not a memory-safety bug, so leaving
> it seems a reasonable choice to me.
>
> If you do want to tidy it up, one option would be to reply with an
> error rather than ending the loop:
>
> if (startswith (cs.own_buf, "vCont;")
> || startswith (cs.own_buf, "vAttach;")
> || startswith (cs.own_buf, "vRun;")
> || startswith (cs.own_buf, "vKill;"))
> write_enn (cs.own_buf);
> else
> handle_v_requests (cs.own_buf, len, &new_len);
>
> The putpkt at the bottom of the loop would send the E01 straight back
> as the reply to the refused packet, and the loop would go on waiting
> for the real qSymbol or qRelocInsn answer. The client gets a clear
> refusal, and clients already have to cope with E01 from these
> packets. The comment above the check would then say "refuse with an
> error" rather than "don't service". I'd be equally happy to see this
> as a follow-up.
I thought about this over night and I agree with you that what I have
here is not ideal. But nor do I think your proposal is the right way to
go.
My concern with you proposal is that we end up "stuck" in the secondary
packet loop. I mean, if we want to return E01 to a vKill, then surely
we should return E01 for 'g', or any other packet. So then we end with
a loop which will only exit by sending a valid qSymbol or qRelocInsn
packet. A debug client that somehow gets out of sync would then never
break from the loop.
If we take qSymbol handling as an example, what I think we should do is
this: in server.cc, in handle_query, where we handle qSymbol, we
eventually reach these lines:
if (current_thread != NULL)
the_target->look_up_symbols ();
strcpy (own_buf, "OK");
return;
We should wrap these like this:
try
{
if (current_thread != NULL)
the_target->look_up_symbols ();
strcpy (own_buf, "OK");
}
catch (const gdb_exception_error &exception)
{
sprintf (cs.own_buf, "E.%s", exception.what ());
}
return;
Then, in handle_limited_packets_while_waiting, when we see a packet that
we're not expecting, instead of printing a warning, we should raise an
error. This error would be caught above and we'd return an error packet
to GDB.
I don't want to get sidetracked fixing the above right now, so I don't
plan to address this issue in this commit.
I have however, made some changes to this commit:
- I moved the checks for the error packet, and for the expected final
packet, inside handle_limited_packets_while_waiting.
- I've added a test that exercises the current behaviour.
I don't think either of these really change the core of this patch
though, so I have applied your approved-by tag. Still, I'll wait a
while before pushing in case you wish to re-review this new version.
Thanks,
Andrew
---
commit 69d373278050dfe4f797aa5840d0cb6a484a05f8
Author: Andrew Burgess <aburgess@redhat.com>
Date: Tue Nov 17 13:57:07 2026 +0000
gdbserver: merge look_up_one_symbol and relocate_instruction packet handling
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.
During review [1] it was asked why, for things like vKill, vCont, etc,
we return from handle_limited_packets_while_waiting rather than sending
back an error packet and remaining in the loop. I think that replying
with an error is the correct way to go, but I think we also need to
return from handle_limited_packets_while_waiting. If we don't return
then there is a risk that a debug client will get stuck within the
handle_limited_packets_while_waiting loop. However, as the code is
currently written, returning from handle_limited_packets_while_waiting
means that gdbserver will send some other reply packet, either a
subsequent qSymbol request, or the final OK. If we send an error then
we will end up sending two packets in response to a single packet from
the debug client, and this will definitely cause the communication to
get out of sync.
I think the correct solution is to have
handle_limited_packets_while_waiting throw an exception which is caught
further up the stack. For qSymbol this would be in handle_query where
the initial qSymbol packet is being dealt with. At this point gdbserver
would catch the exception and return an error packet. A similar fix
would be needed for qRelocInsn. I don't plan to make this change in
this series which is focused on bugs in m, M, x, and X packet handling.
[1] https://inbox.sourceware.org/gdb-patches/93333453-b4d8-40e2-a20f-adabf34d9690@redhat.com
Approved-By: Kevin Buettner <kevinb@redhat.com>
diff --git a/gdb/testsuite/gdb.server/qSymbol-bad-reply.c b/gdb/testsuite/gdb.server/qSymbol-bad-reply.c
new file mode 100644
index 00000000000..dc3a92d8031
--- /dev/null
+++ b/gdb/testsuite/gdb.server/qSymbol-bad-reply.c
@@ -0,0 +1,63 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+ Copyright 2026 Free Software Foundation, Inc.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#include <stdlib.h>
+#include <assert.h>
+#include <limits.h>
+
+unsigned char *buffer = NULL;
+size_t buffer_length = 1024 * 1024 * 100;
+volatile int global_var = 0;
+
+/* Is C a byte that must be escaped in a reply from gdbserver to
+ GDB? Return 1 if it is, otherwise, return 0. */
+static int
+is_escapable_byte (unsigned char c)
+{
+ switch (c)
+ {
+ case 0x23:
+ case 0x24:
+ case 0x7d:
+ case 0x2a:
+ return 1;
+ default:
+ return 0;
+ }
+}
+
+int
+main ()
+{
+ buffer = malloc ( buffer_length );
+ assert (buffer != NULL);
+
+ unsigned char val = 0;
+ for (size_t i = 0; i < buffer_length; ++i)
+ {
+ buffer[i] = val;
+ do
+ {
+ val = (val + 1) % UCHAR_MAX;
+ }
+ while (is_escapable_byte (val));
+ }
+
+ global_var = 42; /* Break here. */
+
+ return 0;
+}
diff --git a/gdb/testsuite/gdb.server/qSymbol-bad-reply.exp b/gdb/testsuite/gdb.server/qSymbol-bad-reply.exp
new file mode 100644
index 00000000000..7c3dce4d9be
--- /dev/null
+++ b/gdb/testsuite/gdb.server/qSymbol-bad-reply.exp
@@ -0,0 +1,142 @@
+# This testcase is part of GDB, the GNU debugger.
+#
+# Copyright 2026 Free Software Foundation, Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+# The qSymbol and qRelocInsn handling includes a mini packet loop which only
+# handles a small subset of packets while gdbserver and GDB are finalising the
+# qSymbol or qRelocInsn conversation. The gdbserver code that handles this
+# packet subset is shared between qSymbol and qRelocInsn, but qSymbol is far
+# easier to trigger, so we use that here for testing,
+#
+# This test was added when the packet handling for qSymbol and qRelocInsn was
+# merged together into a single function, and this captures the current
+# behaviour at that time. However, this behaviour is probably not the ideal
+# behaviour, and hopefully will be improved in the future. When that happens
+# this test will need to be updated and/or rewritten. That's OK.
+#
+# To understand what's wrong with the current code, consider the case where the
+# debug client first sends 'qSymbol::'. gdbserver then replies with a qSymbol
+# packet asking for a particular symbol. If the debug client at this point
+# sends back a 'g' packet, which is not one the qSymbol loop can handle, then
+# currently gdbserver breaks from the secondary packet handling loop and
+# returns. gdbserver will then send the next qSymbol request. This is bad
+# because qSymbol is not a documented reply to a 'g' packet.
+#
+# However, fixing this will require some bigger changes to how qSymbol and
+# qRelocInsn are handled, especially the error cases, within gdbserver.
+#
+# This test uses 'maint packet ...' to trigger a qSymbol packet exchange with
+# gdbserver. Then sends unexpected packets to gdbserver and check how the
+# server handles them.
+
+load_lib gdbserver-support.exp
+
+require allow_gdbserver_tests
+
+standard_testfile
+
+save_vars { GDBFLAGS } {
+ # If GDB and GDBserver are both running locally, set the sysroot to avoid
+ # reading files via the remote protocol (the `is_remote target` check is
+ # already done above).
+ if { ![is_remote host] && ![is_remote target] } {
+ set GDBFLAGS "$GDBFLAGS -ex \"set sysroot\""
+ }
+ if {[prepare_for_testing "failed to prepare" $testfile $srcfile]} {
+ return
+ }
+}
+
+# Make sure we're disconnected, in case we're testing with an
+# extended-remote board, therefore already connected.
+gdb_test "disconnect" ".*"
+
+# Start gdbserver and connect.
+set res [gdbserver_spawn ""]
+lassign $res gdbserver_protocol gdbserver_gdbport
+set res [gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport]
+if {![gdb_assert {$res == 0} "connect"]} {
+ return
+}
+
+# Get into position for the test.
+gdb_breakpoint [gdb_get_line_number "/* Break here. */"]
+gdb_continue_to_breakpoint "test position"
+
+# Run BODY within a qSymbol packet processing loop. Send 'qSymbol::' to
+# gdbserver . It should either reply with 'qSymbol:<name>' or 'OK'.
+#
+# If we get OK then gdbserver doesn't want to know any symbols from GDB, so we
+# cannot get access to its inner packet processing loop. Issue an UNSUPPORTED
+# and return without running BODY.
+#
+# If we get back a symbol request then we can execute BODY which will send some
+# unexpected packets to gdbserver.
+proc within_qSymbol_loop { body } {
+ set first_symbol_requested ""
+ gdb_test_multiple "maint packet qSymbol::" "start qSymbol loop" {
+ -re -wrap "\r\nreceived: \"OK\"" {
+ unsupported "$gdb_test_name (no symbols requested)"
+ }
+
+ -re -wrap "\r\nreceived: \"qSymbol:(\[^\"\]+)\"" {
+ set first_symbol_requested $expect_out(1,string)
+ pass $gdb_test_name
+ }
+ }
+
+ # We got a symbol request from gdbserver then run BODY. Each BODY will run
+ # until gdbserver exits the qSymbol packet processing loop.
+ if { $first_symbol_requested ne "" } {
+ with_test_prefix "in qSymbol loop" {
+ transparent_uplevel $body
+ }
+ }
+}
+
+# After gdbserver sends a qSymbol request, send back an unexpected packet. See
+# the comments at the top of this file for the limitations of this test.
+#
+# Place a limit of 100 symbol requests. This is more than gdbserver currently
+# asks for on GNU/Linux, but should stop thi test getting stuck in a loop if
+# something unexpected goes wrong.
+foreach_with_prefix packet { E01 g vKill; } {
+ within_qSymbol_loop {
+ set limit 100
+ gdb_test_multiple "maint packet $packet" "reply with error" {
+ -i $server_spawn_id
+ -re "gdbserver: unexpected \[^\r\n\]+\r\n" {
+ exp_continue
+ }
+
+ -i $gdb_spawn_id
+ -re -wrap "received: \"qSymbol:\[^\r\n\]+\"" {
+ incr limit -1
+ if { $limit == 0 } {
+ fail $gdb_test_name
+ } else {
+ send_gdb "maint packet $packet\n"
+ exp_continue
+ }
+ }
+
+ -i $gdb_spawn_id
+ -re -wrap "received: \"OK\"" {
+ pass $gdb_test_name
+ }
+ }
+ }
+}
diff --git a/gdbserver/remote-utils.cc b/gdbserver/remote-utils.cc
index 8192e4577f2..abb1789eb76 100644
--- a/gdbserver/remote-utils.cc
+++ b/gdbserver/remote-utils.cc
@@ -1417,6 +1417,199 @@ 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 final 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.
+
+ EXPECTED_PACKET_PREFIX is the first part of the final response packet
+ that we are waiting for. This code fetches packets from the debug
+ client and then:
+
+ + If it is one of the packets this function handles, the packet is
+ handled, and then loops around to fetch and handle another packet.
+
+ + If the packet is empty, is an error (starts with 'E'), or is not one
+ this function can handle and doesn't start with EXPECTED_PACKET_PREFIX,
+ then this function returns false, and the unexpected, unhandled packet
+ is left in the client_state's OWN_BUF.
+
+ + If the packet is not one this function can handle and does start with
+ EXPECTED_PACKET_PREFIX, then this is the final packet we were waiting
+ for. This function returns true and the unhandled final packet is left
+ in the client_state's OWN_BUF.
+
+ If this function returns with either true or false, then no response will
+ have been sent to the debug client. Returning from this function will
+ result in a reply being sent, so if we send a reply before returning then
+ this would result in multiple reply packets. The debug client is not
+ expecting this and will get out of sync.
+
+ 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 (const char *expected_packet_prefix)
+{
+ 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;
+ }
+ }
+
+ if (cs.own_buf[0] == '\0')
+ {
+ warning (_("an unexpected empty packet occurred while waiting for '%s'"),
+ expected_packet_prefix);
+ return false;
+ }
+ else if (cs.own_buf[0] == 'E')
+ {
+ /* Don't describe an error as unexpected. For both qSymbol and
+ qRelocInsn, if the debug client needs to read inferior memory to
+ service the request then the read could fail and the debug client
+ might return an error. There's still not much we can do beyond
+ returning false here; we cannot continue handling the qSymbol or
+ qRelocInsn, but the warning message should not indicate that this
+ is necessarily a bug. */
+ warning (_("an error occurred while waiting for '%s': %s"),
+ expected_packet_prefix, cs.own_buf);
+ return false;
+ }
+ else if (!startswith (cs.own_buf, expected_packet_prefix))
+ {
+ /* We got an unexpected packet from the debug client. Do not send an
+ error packet back at this point. If we send back an error at this
+ point then we also need to fetch another packet from the debug
+ client, which means we need another go around this packet handling
+ loop again. There is risk that a debug client could get stuck in
+ this loop.
+
+ By instead just returning false we let our caller know that
+ something went wrong, they can choose to handle this how they want.
+ But at least gdbserver is making progress towards returning to the
+ main packet handling loop.
+
+ In the future it might be better if we threw an exception at this
+ point, but that will require a suitable catch in our call stack.
+ If the exception is allowed to propagate all the way back to
+ captured_main then this will reset the connection. */
+ warning (_("unexpected packet while waiting for '%s', ignoring: %s"),
+ expected_packet_prefix, cs.own_buf);
+ 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 +1619,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,88 +1644,10 @@ 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 ("qSymbol:"))
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);
- return -1;
- }
+ gdb_assert (startswith (cs.own_buf, "qSymbol:"));
p = cs.own_buf + strlen ("qSymbol:");
q = p;
@@ -1572,7 +1686,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,100 +1694,10 @@ 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 ("qRelocInsn:"))
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;
- }
-
- if (cs.own_buf[0] == 'E')
- {
- warning (_("An error occurred while relocating an instruction: %s"),
- cs.own_buf);
- return -1;
- }
-
- if (!startswith (cs.own_buf, "qRelocInsn:"))
- {
- warning (_("Malformed response to qRelocInsn, ignoring: %s"),
- cs.own_buf);
- return -1;
- }
+ gdb_assert (startswith (cs.own_buf, "qRelocInsn:"));
unpack_varlen_hex (cs.own_buf + strlen ("qRelocInsn:"), &written);
next prev parent reply other threads:[~2026-11-26 14:59 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 ` [PATCH 2/5] gdbserver: merge look_up_one_symbol and relocate_instruction packet handling Andrew Burgess
2026-11-25 5:55 ` Kevin Buettner
2026-11-26 14:58 ` Andrew Burgess [this message]
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=xhuyfld6fprlp.fsf@aburgess-thinkpadp1gen8.rmtgb.csb \
--to=aburgess@redhat.com \
--cc=gdb-patches@sourceware.org \
--cc=kevinb@redhat.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