From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id m4PVKcDk+mp4/xIAWB0awg (envelope-from ) for ; Mon, 16 Nov 2026 06:10:56 -0500 Authentication-Results: simark.ca; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=Y8ntNMFn; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 9B1D91E043; Mon, 16 Nov 2026 06:10:56 -0500 (EST) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-6.4 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham autolearn_force=no version=4.0.1 Received: from vm01.sourceware.org (vm01.sourceware.org [38.145.34.32]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id C7B6A1E043 for ; Mon, 16 Nov 2026 06:10:55 -0500 (EST) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 362354B99F4D for ; Mon, 16 Nov 2026 11:10:55 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 362354B99F4D Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=Y8ntNMFn Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTP id 4D8434BA23F1 for ; Mon, 16 Nov 2026 11:10:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4D8434BA23F1 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 4D8434BA23F1 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1794827424; cv=none; b=nGHspAx6WaNT6ACDCGZVG2pSWZ+r7pW3t/dc8CHyH8F+n3KPeovDF1SS8XoYmwUE58f7mcKYC9vNkvfROo7ZxRKVH8uAyEZtZ8rkudGRKa9x7HDMzR8U5SWAOXgyp+i7lkjFYSFyfGtOKEdgdMzVr+A0DJCGJ9s5v33YmV4lyi0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1794827424; c=relaxed/simple; bh=0HJ/0ZRcnUZ02PQCvi+NX3IKyg+RnVYi/dTCeipe9JI=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=EZM65Uxp2sI9UF0fRZyXCn4J6h5QLo7WRU5jS5ArLzxI8SNMmluu8IATDOlwbnnirdGYB3o5Kq4cY1QmzVfw7jA2nLM8BkjBvjKVRIyGY2G5AqLwmb12McarUKQoYLno/ZtvVRDcRnqkAT7FabqRmrVhe8ks7XmBp5jSUD7YcgI= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=Y8ntNMFn DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4D8434BA23F1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1794827424; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=zoW7rP1MyaWTtBbPvo7B1h6fsD4ga/b81iHS+ZL+hdc=; b=Y8ntNMFna8FXX40Ns0zzStqiIyiS1GGEMHRydWGKYWOibzUVmxX5kfG9C5aBnfpusD+ISa Y7XS55EegAVmdP6oRFUmdacDIFloaVIOgbNEdZzUa+TEOmI7n1gyk/4/9ODeMc4rw2zltD 9F2/DDpHidapmmQl4dAMavq6meM4Exo= Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-686-QJifuooPPRuK7USNLgJdzw-1; Mon, 16 Nov 2026 06:10:22 -0500 X-MC-Unique: QJifuooPPRuK7USNLgJdzw-1 X-Mimecast-MFC-AGG-ID: QJifuooPPRuK7USNLgJdzw_1794827421 Received: by mail-wr1-f72.google.com with SMTP id ffacd0b85a97d-48e1d839ddaso2079148f8f.3 for ; Mon, 16 Nov 2026 03:10:22 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1794827421; x=1795432221; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=zoW7rP1MyaWTtBbPvo7B1h6fsD4ga/b81iHS+ZL+hdc=; b=m4ke+TgdoBub5E4YK+v8uiVuJ8rLMyHXCgH718D5VpWFR8jxLWxfw3ugiH6Hq5C54S kEQEULOrxf55zHqPKxEI1KDGONW4v2wS9bn5h3mFDRhJkN7Jj3yCLiG/q5R/biQOsxpy d1K14EP+vy23sNF9OqFE5/Bp+1MAXXEE8YDUHAeU9Yte+wgXPyQm+FFU3CzPnIBlfvb3 pi3Xgbc/k9FjlSNiSMpVb9q8q2P7v/hmdh+aRIB0Ml7LLCixs/XQSP5Z/t0StIR3rd0r xwoW61aLwTZriGVHV1NhXUjt4qyFbFBXu/yx/Zod9Ah1lmmFQ75f/VNTvMYRsl+AIUEO eJIg== X-Gm-Message-State: APuU4Z/Fo9emVPwIZMLuLyF9FbF6MXxSjWWKgN//aPdVVq22WEgsjTso Id3OD0fVePaTdqfOqrcAt+OhHY53yVZypmPjP5Vl48N6voQUw6WXD/Y5hxLi3GUiU64HkiypIy+ +628J9md9rSDHLLqfE15mkvLfzM1sOFqfQPvLdlv3xlqVRcwbKGv2gDAWtaZPxbO7s1I1kMT+5a nWhwQkmzdqXKish46YcrgrVn/azZT5Hin/wFisFqytyvxbR70= X-Gm-Gg: AYBFou091837qSudaVCSh+Z0rPpXqc4H35SOXqRXAiBmJk+2/4Vs1Vw3KccVnFIAmzK 8M4rmQa9L4g5KPQTO2IwCU3uGE92o9z/aLtgKGVmhNzWDdl+2F6KgDO7ZXr8Tawf5Ut87LZkoeh Ji/pBNN36fm8zaa2nkbdqYG1rEUtWepiiWbc+ELdaQOo35rHHDDzxBPaQOD5EFRJfv5ke6yfy41 KnJLkonSOGsHAQ7ftzeNzTC2Ot1N9OU2mEOPVjnbHcimCADlkFC22Or5vN5zpMDf9I6aKXTPX7a TjjGK9612Z1OeB4IL5lmctNgs82A8H3ffJqr5bNdr7ixckjjw53l6CCjFDFCv3CuCk70KKi7TDx C3lDvPhwePbGdtfLKCxYHX6Kc9RDLhSb2W2PoZ86/IJZC X-Received: by 2002:a05:600c:34d6:b0:4a8:f2ad:a9e5 with SMTP id 5b1f17b1804b1-4bb559f609cmr198127425e9.3.1794827421281; Mon, 16 Nov 2026 03:10:21 -0800 (PST) X-Received: by 2002:a05:600c:34d6:b0:4a8:f2ad:a9e5 with SMTP id 5b1f17b1804b1-4bb559f609cmr198126335e9.3.1794827420266; Mon, 16 Nov 2026 03:10:20 -0800 (PST) Received: from localhost ([83.216.138.189]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4bb7292c04csm63762685e9.11.2026.11.16.03.10.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Nov 2026 03:10:19 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCH 4/5] gdbserver: don't leak trace variable names if reusing variable numbers Date: Mon, 16 Nov 2026 11:10:10 +0000 Message-Id: <3e98b6277e442950b1980af0e0f930c1a41b4824.1794827178.git.aburgess@redhat.com> X-Mailer: git-send-email 2.25.4 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: -MeCohpYIhusyN-Qnsa-WaBIG3XALLz6SHteWTblXvg_1794827421 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit content-type: text/plain; charset="US-ASCII"; x-default=true X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces~public-inbox=simark.ca@sourceware.org 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 details from GDB need to exactly match the existing builtin. The builtin flag must be set to non-zero, and the name must be correct. Anything else indicates that GDB is trying to change a builtin trace state variable, and that is not allowed. But 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. --- .../gdb.server/invalid-tracepoint-packets.exp | 26 ++++++++++++++ gdbserver/tracepoint.cc | 36 +++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/gdb/testsuite/gdb.server/invalid-tracepoint-packets.exp b/gdb/testsuite/gdb.server/invalid-tracepoint-packets.exp index de37d657b57..cbaddfd7e7b 100644 --- a/gdb/testsuite/gdb.server/invalid-tracepoint-packets.exp +++ b/gdb/testsuite/gdb.server/invalid-tracepoint-packets.exp @@ -122,6 +122,28 @@ 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:0:616263" $::error_re \ + "QTDV packet rejected for number 1" + + # 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" @@ -136,6 +158,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 7f0b233fa43..1e68b19d9d5 100644 --- a/gdbserver/tracepoint.cc +++ b/gdbserver/tracepoint.cc @@ -2269,6 +2269,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. */ + free (tsv->name); free (tsv); tsv = next; } @@ -2673,6 +2678,37 @@ 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 + re-declaring a builtin unchanged, but reject any attempt to change + it. */ + if (tsv->getter != nullptr) + { + if (builtin != 0 && strcmp (varname, tsv->name) == 0) + { + /* The client is re-sending the definition of this builtin trace + state variable unchanged. There's nothing to do. */ + write_ok (own_buf); + xfree (varname); + return; + } + + /* The client tried to change this builtin state variable. As the + getter function is not being updated this will not do what the + client expects, so reject it. */ + 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; -- 2.25.4