From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id QepYBKtjAGuPeSQAWB0awg (envelope-from ) for ; Fri, 20 Nov 2026 10:13:47 -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=SSzY5IGQ; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 0E3511E023; Fri, 20 Nov 2026 10:13:47 -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 [IPv6:2620:52:6:3111::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 9613D1E023 for ; Fri, 20 Nov 2026 10:13:45 -0500 (EST) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 44F0A4AA54FA for ; Fri, 20 Nov 2026 15:13:37 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 44F0A4AA54FA 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=SSzY5IGQ Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTP id 6570A48FEDAF for ; Fri, 20 Nov 2026 15:13:10 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6570A48FEDAF 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 6570A48FEDAF Authentication-Results: sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1795187590; cv=none; b=jkqqX03C+7KGgdpjRtnQH8LNZWTrsITEKarJv2bXwpC6jYOFKihV+s6nSFk+/a9TXCSgPACKe4yiH/Dajs02NWiAnmAiuzqNd5udM73oXSquHp7eU30KQYfu5eSlPq5MyNPsFKrUlnd+f9XSRjFXpnhxAkhY52a2/WHiIAKIDgQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1795187590; c=relaxed/simple; bh=JljF7OD1/YIG6d8WP0fQxcjhamf9/Koypuk1Z8S19k0=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=R6jZs9IJTRhObIgAHV54OyB3PbXcyLV6BdFFPeBTeCwih/GmIE/D0YrfCWRHPPdzMYZyCBlV1gFWSxKFDhWONFz4ZZILMzEdLW0ZxMThMOxp2rd27YzpU0II7xcVQF0c0Ij71R+Z5UNXkgGwkdULNbzcjZP3QrauNz38WhgFKlY= 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=SSzY5IGQ DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6570A48FEDAF DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1795187590; 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=IK20VcrnXzI7NE7VFl648D/tO4mEaOHVRC3w9E2DtxM=; b=SSzY5IGQP4SYwdbNZMZ8lPzFGpErJbzqvwST2V1/22sJ8ddiK2JcpXd9YOoeSqEa9EUJzM YJkr9WFUG/kSl3NkB2In9v32k1nJLXnL0ve1PYqdK83fFjY8zFjEpKlfQvdb3g5Mdh58Hv juJ7EdprSD37k64DWPmDC7ZbKQ0CsaE= Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-201-4Fh5pjxQPpmKT_rch2uCvw-1; Fri, 20 Nov 2026 15:13:08 +0000 X-MC-Unique: 4Fh5pjxQPpmKT_rch2uCvw-1 X-Mimecast-MFC-AGG-ID: 4Fh5pjxQPpmKT_rch2uCvw_1795187587 Received: by mail-wr1-f70.google.com with SMTP id ffacd0b85a97d-48e0461e040so815172f8f.0 for ; Fri, 20 Nov 2026 07:13:08 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795187587; x=1795792387; 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=IK20VcrnXzI7NE7VFl648D/tO4mEaOHVRC3w9E2DtxM=; b=Qgfy0bqANsltSAjzgPQKg6I9WgB6/vUbTmcNnlbNpp5ubuTvMfuXfSWs0oK1ay6xil 8XEcsnOg5jj5uekLgv/eoGH9IMU5cOWf4spSPcdZT3o736dgCxCWxKoa+sSTAlBDMB3g mGj5kWIxqHAfxBNDu4mDDIy0crjkqhfXvC305R0/ObZzoosIq4v5sKxsrnS8ylqZq8LA wiy0oE71WrQ26A8CUPUtw0fWcK1562Nj89hQ2N2n6o43yfqf381StdRV6k4ZeTTp4fmO gm06FLV8IrkofLSuv6jAUSivl0pcBUf33NVjM/oycKa+orQpKMIwP2JvsF8c5QR7uUqX lR7A== X-Gm-Message-State: APuU4Z+VsICgwvFyika5M759gpu2Vm/LTK7zHQgqi/zu8toMgJnZsO6T Rw8HA/EndG/jGthSg7R4XA0GwHPieELAWfBaM0VvCluz+dCCUnzxwO9G4LH2zB+H8OV8YOOZMPB 0Jzl+ELw2PnryUU1rJfEbIT4eTukrIe4Y/DiW50UaiONDFaoaVPL+0xO34Yl+F7D+VWdj42vqQC iwdpsp41qBCfXzAx1tNf55NGYxWpM9LXk//XXNyEc2Hqbhtzk= X-Gm-Gg: AYBFou2H3arXQgHhiqfVSSAp88XmaxDpSoUayPGgLkDmIb0BX8XGKkyY+Qr7jEFRaRa xwIap3Og8ixiL1YSLUuimg9Jmniuac4iDlq3ut8ydJZ9f15ewxmi3kj/VsgmYAuqA1DKi5oHjKQ n2+2FH/BWASVaiV2afgfdybP/pfueyS5IkXQNpmT5RhaCu8hemL6UlOmTcmkIHDYQI0b7h8+6VP Acyt00NuLf55oTNPjqqyLSmUR9XH7/RVe+21YRBIyShlSagADmuyDabXsxF1ZfApualVkWqT3Vy EZV2LNmN+9HDBEWC+vmeJegNVkxulyICM45DaxcDKX6RoV3uHsgCFsIJIJWaC0gr3P7T0ZSdndd YvK/z0b890Efke238FmNsuHLwgY0lv2Z5F4GD4b1WxNsLDg== X-Received: by 2002:a05:600c:a4b:b0:4a1:dade:f049 with SMTP id 5b1f17b1804b1-4bb88806292mr77588605e9.15.1795187586758; Fri, 20 Nov 2026 07:13:06 -0800 (PST) X-Received: by 2002:a05:600c:a4b:b0:4a1:dade:f049 with SMTP id 5b1f17b1804b1-4bb88806292mr77587665e9.15.1795187586068; Fri, 20 Nov 2026 07:13:06 -0800 (PST) Received: from localhost ([83.216.138.189]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4ab69bf630esm6308700f8f.16.2026.11.20.07.13.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Nov 2026 07:13:05 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCH 2/5] gdbserver: merge look_up_one_symbol and relocate_instruction packet handling Date: Fri, 20 Nov 2026 15:12:47 +0000 Message-ID: <18919cf28001b47de5aa101126ad7a17a69a469a.1795186852.git.aburgess@redhat.com> X-Mailer: git-send-email 2.56.1 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: sSMUlH-uNnbok6cOes8t7PZ_6KHBx9U-XA4hlGPuPBs_1795187587 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 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