* [PATCH] gdbserver: fix memory leak in vRun handling
@ 2026-11-18 11:24 Andrew Burgess
2026-11-19 20:23 ` Keith Seitz
2026-11-19 20:38 ` Tom Tromey
0 siblings, 2 replies; 4+ messages in thread
From: Andrew Burgess @ 2026-11-18 11:24 UTC (permalink / raw)
To: gdb-patches; +Cc: Andrew Burgess
In decode_v_run_arg (gdbserver/server.cc) a buffer is allocated in
which the inferior argument will be decoded. hex2bin is then used to
decode the inferior argument.
If hex2bin throws an exception (on seeing a non-hex character), we
return from decode_v_run_arg without freeing the previously allocated
buffer.
Fix this by creating the buffer as gdb::unique_xmalloc_ptr<char>. The
decode_v_run_arg function already returned its results as a
gdb::unique_xmalloc_ptr<char>, but this was previously created at the
return point, now we just create it earlier.
Currently the decode_v_run_arg function injects a null character after
the decoded argument string. I wondered if hex2bin could ever decode
less than LEN bytes, however I don't believe this is possible due to
the checks performed early in decode_v_run_arg, and based on the value
of LEN passed in. To reflect this I have added an assert within
decode_v_run_arg.
Other than the memory leak being fixed, there should be no user
visible changes after this commit.
I have added a test that exercises the memory leak code path, however, the
memory leak isn't detected by default. Run the test by hand with gdbserver
running under valgrind or using the address sanitizer and the leak will show up.
---
gdb/testsuite/gdb.server/invalid-vrun.exp | 60 +++++++++++++++++++++++
gdbserver/server.cc | 17 +++++--
2 files changed, 73 insertions(+), 4 deletions(-)
create mode 100644 gdb/testsuite/gdb.server/invalid-vrun.exp
diff --git a/gdb/testsuite/gdb.server/invalid-vrun.exp b/gdb/testsuite/gdb.server/invalid-vrun.exp
new file mode 100644
index 00000000000..5f189b7feb9
--- /dev/null
+++ b/gdb/testsuite/gdb.server/invalid-vrun.exp
@@ -0,0 +1,60 @@
+# 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/>.
+
+# Test handling of vRun packet where argument fields contains invalid
+# non-hex characters.
+
+load_lib gdbserver-support.exp
+
+require allow_gdbserver_tests
+
+standard_testfile normal.c
+
+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] } {
+ 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. Must start in extended mode so that we
+# can use vRun.
+set target_exec [gdbserver_download_current_prog]
+
+set res [gdbserver_start "--multi" $target_exec]
+set gdbserver_protocol "extended-remote"
+set gdbserver_gdbport [lindex $res 1]
+gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport
+
+# Send a valid vRun packet to prove that gdbserver isn't rejecting all
+# packets.
+gdb_test "maint packet vRun;;6162" "\r\nreceived: \"T.*" \
+ "valid vRun packet accepted"
+
+# Send an invalid packet. Once upon a time this used to leak memory
+# inside gdbserver.
+gdb_test "maint packet vRun;;xxxx" "\r\nreceived: \"E01\"" \
+ "non-hex inferior argument rejected"
diff --git a/gdbserver/server.cc b/gdbserver/server.cc
index 74774fcde7c..f01497bdd06 100644
--- a/gdbserver/server.cc
+++ b/gdbserver/server.cc
@@ -3412,7 +3412,7 @@ decode_v_run_arg (const char *ptr, size_t len)
/* Buffer to decode the argument into. The '+ 1' is for the null
terminator we will add. */
- char *arg = (char *) xmalloc (len + 1);
+ gdb::unique_xmalloc_ptr<char> arg ((char *) xmalloc (len + 1));
/* Decode the argument from the packet and add a null terminator. We do
this within a try block as invalid characters within the PTR buffer
@@ -3420,15 +3420,24 @@ decode_v_run_arg (const char *ptr, size_t len)
returning nullptr in order to clean up some memory allocations. */
try
{
- hex2bin (ptr, (gdb_byte *) arg, len);
- arg[len] = '\0';
+ size_t res = hex2bin (ptr, (gdb_byte *) arg.get (), len);
+
+ /* hex2bin can return early if it encounters a null character,
+ but this will not happen in this case as LEN is based on a
+ scan of the PTR buffer and will be capped at any null
+ character within. As such, as we already check for an even
+ number of bytes above, we expect hex2bin to parse exactly LEN
+ bytes. */
+ gdb_assert (res == len);
+
+ arg.get ()[len] = '\0';
}
catch (const gdb_exception_error &exception)
{
return nullptr;
}
- return gdb::unique_xmalloc_ptr<char> (arg);
+ return arg;
}
/* Run a new program. */
base-commit: 5678a7bfceb3a6ea18bcde1ae68f7e96c43c998b
--
2.25.4
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH] gdbserver: fix memory leak in vRun handling
2026-11-18 11:24 [PATCH] gdbserver: fix memory leak in vRun handling Andrew Burgess
@ 2026-11-19 20:23 ` Keith Seitz
2026-11-23 14:45 ` Andrew Burgess
2026-11-19 20:38 ` Tom Tromey
1 sibling, 1 reply; 4+ messages in thread
From: Keith Seitz @ 2026-11-19 20:23 UTC (permalink / raw)
To: Andrew Burgess, gdb-patches
Hi,
On 11/18/26 3:24 AM, Andrew Burgess wrote:
> diff --git a/gdb/testsuite/gdb.server/invalid-vrun.exp b/gdb/testsuite/gdb.server/invalid-vrun.exp
> new file mode 100644
> index 00000000000..5f189b7feb9
> --- /dev/null
> +++ b/gdb/testsuite/gdb.server/invalid-vrun.exp
> @@ -0,0 +1,60 @@
> +# 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/>.
> +
> +# Test handling of vRun packet where argument fields contains invalid
> +# non-hex characters.
> +
> +load_lib gdbserver-support.exp
> +
> +require allow_gdbserver_tests
> +
> +standard_testfile normal.c
> +
> +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).
Maybe I'm daft, but where is the "is_remote target" check done? I'm not
seeing. AFAICT, this file follows an idiom used in other gdbserver
tests, but they all check both "host" and "target".
Otherwise, LGTM.
Reviewed-By: Keith Seitz <keiths@redhat.com>
Keith
> + if { ![is_remote host] } {
> + set GDBFLAGS "$GDBFLAGS -ex \"set sysroot\""
> + }
> + if {[prepare_for_testing "failed to prepare" $testfile $srcfile]} {
> + return
> + }
> +}
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH] gdbserver: fix memory leak in vRun handling
2026-11-19 20:23 ` Keith Seitz
@ 2026-11-23 14:45 ` Andrew Burgess
0 siblings, 0 replies; 4+ messages in thread
From: Andrew Burgess @ 2026-11-23 14:45 UTC (permalink / raw)
To: Keith Seitz, gdb-patches
Keith Seitz <keiths@redhat.com> writes:
> Hi,
>
> On 11/18/26 3:24 AM, Andrew Burgess wrote:
>> diff --git a/gdb/testsuite/gdb.server/invalid-vrun.exp b/gdb/testsuite/gdb.server/invalid-vrun.exp
>> new file mode 100644
>> index 00000000000..5f189b7feb9
>> --- /dev/null
>> +++ b/gdb/testsuite/gdb.server/invalid-vrun.exp
>> @@ -0,0 +1,60 @@
>> +# 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/>.
>> +
>> +# Test handling of vRun packet where argument fields contains invalid
>> +# non-hex characters.
>> +
>> +load_lib gdbserver-support.exp
>> +
>> +require allow_gdbserver_tests
>> +
>> +standard_testfile normal.c
>> +
>> +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).
>
> Maybe I'm daft, but where is the "is_remote target" check done? I'm not
> seeing. AFAICT, this file follows an idiom used in other gdbserver
> tests, but they all check both "host" and "target".
You are totally correct. I've updated this to check host and target,
and pushed this patch.
Thanks,
Andrew
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] gdbserver: fix memory leak in vRun handling
2026-11-18 11:24 [PATCH] gdbserver: fix memory leak in vRun handling Andrew Burgess
2026-11-19 20:23 ` Keith Seitz
@ 2026-11-19 20:38 ` Tom Tromey
1 sibling, 0 replies; 4+ messages in thread
From: Tom Tromey @ 2026-11-19 20:38 UTC (permalink / raw)
To: Andrew Burgess; +Cc: gdb-patches
>>>>> "Andrew" == Andrew Burgess <aburgess@redhat.com> writes:
Andrew> Fix this by creating the buffer as gdb::unique_xmalloc_ptr<char>. The
Andrew> decode_v_run_arg function already returned its results as a
Andrew> gdb::unique_xmalloc_ptr<char>, but this was previously created at the
Andrew> return point, now we just create it earlier.
Makes sense to me.
FWIW this is one kind of patch I'd approve without a test case.
I suppose my reason is that the patch is pretty obviously correct; and
regression testing for any leak fix is probably both prohibitively
difficult and also low-value in a way.
To be clear, I'm not saying I mind the test, either. One area gdb is
somewhat deficient is coverage of the error() calls.
thanks,
Tom
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-11-23 14:46 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-18 11:24 [PATCH] gdbserver: fix memory leak in vRun handling Andrew Burgess
2026-11-19 20:23 ` Keith Seitz
2026-11-23 14:45 ` Andrew Burgess
2026-11-19 20:38 ` Tom Tromey
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox