[binutils-gdb] gdbserver: don't leak trace variable names if reusing variable numbers

Andrew Burgess aburgess@sourceware.org
Tue Dec 1 10:40:07 GMT 2026


https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=3f4b242dc854d490effe4dd331c98c7e454e6112

commit 3f4b242dc854d490effe4dd331c98c7e454e6112
Author: Andrew Burgess <aburgess@redhat.com>
Date:   Thu Nov 12 16:40:48 2026 +0000

    gdbserver: don't leak trace variable names if reusing variable numbers
    
    In the next commit I will close a memory leak relating to strings
    parsed from some trace packets.
    
    While working on this I realised that a QTDV packet, which creates
    trace variables, can leak memory if a trace variable number is reused.
    
    For example two packets:
    
      QTDV:2:6:0:646566
      QTDV:2:6:0:676869
    
    The first creates trace variable #2 called "def" and the second
    creates a trace variable #2 called "ghi".  When the "ghi" variable is
    created the string "def" will be leaked.
    
    It is not clear from the documentation if a client should be able to
    reuse trace variable numbers like this, but as this is the existing
    behaviour, I have retained it while closing off the leak.
    
    There is one tiny issue which adds some extra complexity.  We
    currently have a single built-in trace state variable
    'trace_timestamp' which is created in the function
    initialize_tracepoint.  The name of this trace variable is not heap
    allocated, and so should not be freed.
    
    Initially I thought the solution to this problem was to just allocate
    the 'trace_timestamp' string on the heap and so allow it to be freed,
    but the situation is more complex.
    
    The trace_timestamp variable sets a getter function by calling
    set_trace_state_variable_getter, and this getter is not reset in
    cmd_qtdv if an existing trace_state_variable is returned from
    create_trace_state_variable.  What this means is that if a client
    creates a trace state variable and assigns it to number 1 then the
    name of the variable will change from trace_timestamp, but its value,
    as returned by the getter function will still act like
    trace_timestamp.
    
    To me, this feels like clients should not be reusing existing trace
    state variable numbers at all, but definitely not those of built-in
    variables.
    
    Initially I changed cmd_qtdv to reject any QTDV packet that used the
    number of an existing builtin; however the test gdb.trace/actions.exp
    started failing.  What's happening is that when GDB starts tracing
    (with `tstart`), all the trace state variables, including the existing
    builtins, are sent to gdbserver.  By rejecting any QTDV that touched a
    builtin I ended up rejecting an expected QTDV packet.
    
    So my check became a little more specific; if a QTDV packet is for a
    builtin, then the builtin flag must be non-zero and the variable name
    needs to match the existing builtin.  Anything else indicates that GDB
    is trying to change a builtin trace state variable, and that is not
    allowed.  Sending back the current variable details is fine.
    
    Otherwise, if the QTDV is not overriding a known builtin, we do allow
    the variable to be overridden; a client can override its own
    variables.  In this case we free the old name to avoid leaking.
    
    Finally, while looking at the name management I spotted that
    cmd_qtinit was also failing to free the names.  So I have updated the
    function to free the name.
    
    I've added new tests, including tests that exercise both the
    rejection of an attempt to replace built-in variable #1,
    trace_timestamp, and the acceptance of GDB re-sending the builtin's
    definition unchanged.
    
    Reviewed-By: Keith Seitz <keiths@redhat.com>

Diff:
---
 .../gdb.server/invalid-tracepoint-packets.exp      | 32 ++++++++++++++
 gdbserver/tracepoint.cc                            | 50 ++++++++++++++++++++--
 2 files changed, 79 insertions(+), 3 deletions(-)

diff --git a/gdb/testsuite/gdb.server/invalid-tracepoint-packets.exp b/gdb/testsuite/gdb.server/invalid-tracepoint-packets.exp
index 6554331e0af..7aeac9e7465 100644
--- a/gdb/testsuite/gdb.server/invalid-tracepoint-packets.exp
+++ b/gdb/testsuite/gdb.server/invalid-tracepoint-packets.exp
@@ -121,6 +121,34 @@ proc_with_prefix test_qtdv {} {
 	return
     }
 
+    # Try to assign a new trace state variable to number 1.  Number 1
+    # is already in use by the builtin 'trace_timestamp' variable, so
+    # this will be rejected.
+    gdb_test "maint packet QTDV:1:5:1:616263" $::error_re \
+	"QTDV packet rejected for number 1, name changed"
+
+    # Try to assign a new trace state variable to number 1.  This time the
+    # name is correct, but the builtin flag is wrong; we are trying to
+    # create this as a non-builtin.
+    gdb_test "maint packet QTDV:1:5:0:74726163655f74696d657374616d70" \
+	$::error_re "QTDV packet rejected for number 1, builtin flag changed"
+
+    # However, if we echo back the builtin trace_timestamp definition,
+    # then gdbserver will accept this.  The hex-encoded string here is
+    # 'trace_timestamp', the name of the builtin variable.
+    gdb_test \
+	"maint packet QTDV:1:0:1:74726163655f74696d657374616d70" \
+	$::ok_re "QTDV packet accepted for built-in definition"
+
+    # There's no pre-existing variable 2.  Create a new trace state
+    # variable.
+    gdb_test "maint packet QTDV:2:6:0:646566" $::ok_re \
+	"valid QTDV packet accepted for number 2"
+
+    # Immediately replace our variable 2 with a new one.
+    gdb_test "maint packet QTDV:2:7:0:676869" $::ok_re \
+	"valid QTDV packet accepted to replace variable number 2"
+
     # Tests with missing fields of the packet.
     gdb_test "maint packet QTDV:2" \
 	$::error_re "missing colon after number"
@@ -135,6 +163,10 @@ proc_with_prefix test_qtdv {} {
     gdb_test "maint packet QTDV:2:7:0:616263" $::ok_re \
 	"valid QTDV packet is accepted"
 
+    # Send a QTinit packet which frees all the trace state variables.
+    gdb_test "maint packet QTinit" $::ok_re \
+	"valid QTinit packet accepted"
+
     gdbserver_exit false
 }
 
diff --git a/gdbserver/tracepoint.cc b/gdbserver/tracepoint.cc
index e14ecc8c8be..e8d182e299a 100644
--- a/gdbserver/tracepoint.cc
+++ b/gdbserver/tracepoint.cc
@@ -1098,6 +1098,15 @@ IP_AGENT_EXPORT_VAR int traceframes_created;
 
 #ifndef IN_PROCESS_AGENT
 
+/* Return true if TSV is a builtin; false otherwise.  */
+
+static bool
+tsv_is_builtin (const struct trace_state_variable *tsv)
+{
+  gdb_assert (tsv != nullptr);
+  return tsv->getter != nullptr;
+}
+
 /* Read-only regions are address ranges whose contents don't change,
    and so can be read from target memory even while looking at a trace
    frame.  Without these, disassembly for instance will likely fail,
@@ -2270,7 +2279,7 @@ cmd_qtinit (char *packet)
   while (tsv != nullptr)
     {
       trace_debug ("Looking at var %d", tsv->number);
-      if (tsv->getter == nullptr)
+      if (!tsv_is_builtin (tsv))
 	{
 	  struct trace_state_variable *next = tsv->next;
 	  if (prev != nullptr)
@@ -2278,6 +2287,11 @@ cmd_qtinit (char *packet)
 	  else
 	    trace_state_variables = next;
 	  trace_debug ("Deleting var %d", tsv->number);
+
+	  /* Trace state variables without a getter originated from the
+	     debugger client; the name is heap allocated, and should be
+	     freed now.  */
+	  xfree (tsv->name);
 	  free (tsv);
 	  tsv = next;
 	}
@@ -2697,6 +2711,36 @@ cmd_qtdv (char *own_buf)
   varname[nbytes] = '\0';
 
   struct trace_state_variable *tsv = create_trace_state_variable (num, 1);
+
+  /* If the getter is set then this is a builtin trace state variable, and
+     should not be overridden by the debug client.  However, GDB will upload
+     the target's builtin variables at connection time, and then send them
+     back to the target when starting a trace run.  Accept the debug client
+     redeclaring a builtin unchanged, but reject any attempt to change
+     it.  */
+  if (tsv_is_builtin (tsv))
+    {
+      /* The name of a builtin is always set before the getter is set.  */
+      gdb_assert (tsv->name != nullptr);
+
+      /* If the client is re-sending this builtin with its builtin flag set
+	 and the same name then accept it without updating anything.
+	 Otherwise the client is trying to change the name of this builtin,
+	 or stop this being a builtin, neither of which are allowed.  */
+      if (builtin != 0 && strcmp (varname, tsv->name) == 0)
+	write_ok (own_buf);
+      else
+	write_enn (own_buf);
+
+      xfree (varname);
+      return;
+    }
+
+  /* If NUM is a pre-existing variable then tsv->name will already be set.
+     Additionally, as the getter is NULL, the name is heap allocated and
+     should be freed now to avoid leaking.  */
+  xfree (tsv->name);
+
   tsv->initial_value = (LONGEST) val;
   tsv->name = varname;
 
@@ -5927,7 +5971,7 @@ download_trace_state_variables (void)
 
       /* TSV's with a getter have been initialized equally in both the
 	 inferior and GDBserver.  Skip them.  */
-      if (tsv->getter != NULL)
+      if (tsv_is_builtin (tsv))
 	continue;
 
       target_tsv = *tsv;
@@ -5970,7 +6014,7 @@ download_trace_state_variables (void)
 				       name_addr);
 	}
 
-      gdb_assert (tsv->getter == NULL);
+      gdb_assert (!tsv_is_builtin (tsv));
     }
 
   if (prev_ptr != 0)


More information about the Gdb-cvs mailing list