From patchwork Fri Aug 18 03:36:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Akihiko Odaki X-Patchwork-Id: 1822681 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=daynix-com.20221208.gappssmtp.com header.i=@daynix-com.20221208.gappssmtp.com header.a=rsa-sha256 header.s=20221208 header.b=07/qLFH4; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=patchwork.ozlabs.org) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4RRnlQ084fz1yNm for ; Fri, 18 Aug 2023 13:43:14 +1000 (AEST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qWqIx-00027G-OZ; Thu, 17 Aug 2023 23:37:43 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qWqIw-000262-RT for qemu-devel@nongnu.org; Thu, 17 Aug 2023 23:37:42 -0400 Received: from mail-pg1-x52b.google.com ([2607:f8b0:4864:20::52b]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qWqIu-0005vb-JS for qemu-devel@nongnu.org; Thu, 17 Aug 2023 23:37:42 -0400 Received: by mail-pg1-x52b.google.com with SMTP id 41be03b00d2f7-564b6276941so385971a12.3 for ; Thu, 17 Aug 2023 20:37:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20221208.gappssmtp.com; s=20221208; t=1692329859; x=1692934659; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ikhJZPeSQ6af3opQ0NADT01t876hnrjxBLN83G442nU=; b=07/qLFH4ytt09lwKN3MM+/4qpzm+PpCi43k8YLpGDfxHos2q59/uycnTdHNxGwyn0L AwYP2yiBQYlmgc7gRsMtlJ2uTTaqoPCjKnpX9iMr4z2X7EDbHkl5e4mh4oegjYZlXKBE 0XHFt5j/nvzlQioOgiAvdbqrJDvk9vf/+fOpLwTBR0VKaAi5gf+F83ylHuR6ZfEiZCTH lwFwZAI63wsr4tVvWIhIURkDhpShmrXdo1uc/VqpsoVQT71q0E6Jde4zdxM7AkNchQCZ Vsgh2E4E8NCxokEWMU7+vouDk/4M8sEvhQh6+UiX4dvcSZpwz+dC6bNWbuGmY/J9Byx2 aP2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692329859; x=1692934659; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ikhJZPeSQ6af3opQ0NADT01t876hnrjxBLN83G442nU=; b=K3Qw6WXQe4X9OnXVuOs8u1gkZrPDxCdwNcaaDJnrI8RkbarIzvy5d5MbJZIyVwW3eW ABGyrhFRaUnFT+yUobzoQ3dI8eNJvo/3rcMqE8Saqen3saEZf9uknpas7/TcPpQmNtP2 +pitCWr5tKiaNs4qdHCaEa4pqqCxl9T5n+9Pe+XGZs/5/Ej8muiH6pmClvsSKZhy184j 589DmZodmcKYayK+pJzCAtJbOoJAWonLAZ+gkN+TSdIw6XItCmKxknCcGcnvUpZ3WX9O 93wB/wxUOG5d+/eW+9qRzRQ7a/qJS5UOpl+DUCxXoXVacZmZ/ZTdO0ifp3Gig0YLu4rg LFmw== X-Gm-Message-State: AOJu0YwEsouZJ7Ioon3I5cBRIeX6KlPJpMY/AIwgnNRGwJlfDm5xtlHb +3fiKpM6hjl3Xj/cR+PiXmuwlO0StfSV6rsD2Fs= X-Google-Smtp-Source: AGHT+IGS42Ndf01oG6kR1iAseklLAnXwXbodzf33NhT7/1xNCdD+rcTjSD1QdmXBXoCDqswH39e38Q== X-Received: by 2002:a05:6a21:3387:b0:138:1980:1837 with SMTP id yy7-20020a056a21338700b0013819801837mr2154534pzb.13.1692329859178; Thu, 17 Aug 2023 20:37:39 -0700 (PDT) Received: from alarm.flets-east.jp ([2400:4050:a840:1e00:78d2:b862:10a7:d486]) by smtp.gmail.com with ESMTPSA id c24-20020aa781d8000000b00686a80f431dsm484135pfn.126.2023.08.17.20.37.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Aug 2023 20:37:38 -0700 (PDT) From: Akihiko Odaki To: Cc: =?utf-8?q?Alex_Benn=C3=A9e?= , Mikhail Tyutin , Aleksandr Anenkov , qemu-devel@nongnu.org, Akihiko Odaki , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= Subject: [PATCH RESEND v5 14/26] gdbstub: Dynamically allocate target.xml buffer Date: Fri, 18 Aug 2023 12:36:33 +0900 Message-ID: <20230818033648.8326-15-akihiko.odaki@daynix.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230818033648.8326-1-akihiko.odaki@daynix.com> References: <20230818033648.8326-1-akihiko.odaki@daynix.com> MIME-Version: 1.0 Received-SPF: none client-ip=2607:f8b0:4864:20::52b; envelope-from=akihiko.odaki@daynix.com; helo=mail-pg1-x52b.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_NONE=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org There is no guarantee that target.xml fits in 1024 bytes, and the fixed buffer length requires tedious buffer overflow check. Dynamically allocate the target.xml buffer to resolve these problems. Suggested-by: Alex Bennée Signed-off-by: Akihiko Odaki --- gdbstub/internals.h | 2 +- gdbstub/gdbstub.c | 44 ++++++++++++++++++++++++-------------------- gdbstub/softmmu.c | 2 +- 3 files changed, 26 insertions(+), 22 deletions(-) diff --git a/gdbstub/internals.h b/gdbstub/internals.h index f2b46cce41..4876ebd74f 100644 --- a/gdbstub/internals.h +++ b/gdbstub/internals.h @@ -33,7 +33,7 @@ typedef struct GDBProcess { uint32_t pid; bool attached; - char target_xml[1024]; + char *target_xml; } GDBProcess; enum RSState { diff --git a/gdbstub/gdbstub.c b/gdbstub/gdbstub.c index 5656a44970..031ad89c7d 100644 --- a/gdbstub/gdbstub.c +++ b/gdbstub/gdbstub.c @@ -366,33 +366,37 @@ static const char *get_feature_xml(const char *p, const char **newp, name = NULL; if (strncmp(p, "target.xml", len) == 0) { - char *buf = process->target_xml; - const size_t buf_sz = sizeof(process->target_xml); - /* Generate the XML description for this CPU. */ - if (!buf[0]) { + if (!process->target_xml) { + g_autoptr(GPtrArray) a = g_ptr_array_new_with_free_func(g_free); GDBRegisterState *r; - pstrcat(buf, buf_sz, - "" - "" - ""); + g_ptr_array_add( + a, + g_strdup("" + "" + "")); if (cc->gdb_arch_name) { - pstrcat(buf, buf_sz, ""); - pstrcat(buf, buf_sz, cc->gdb_arch_name(cpu)); - pstrcat(buf, buf_sz, ""); + g_ptr_array_add( + a, + g_markup_printf_escaped("%s", + cc->gdb_arch_name(cpu))); } - pstrcat(buf, buf_sz, "gdb_core_feature->xmlname); - pstrcat(buf, buf_sz, "\"/>"); + g_ptr_array_add( + a, + g_markup_printf_escaped("", + cc->gdb_core_feature->xmlname)); for (r = cpu->gdb_regs; r; r = r->next) { - pstrcat(buf, buf_sz, "feature->xmlname); - pstrcat(buf, buf_sz, "\"/>"); + g_ptr_array_add( + a, + g_markup_printf_escaped("", + r->feature->xmlname)); } - pstrcat(buf, buf_sz, ""); + g_ptr_array_add(a, g_strdup("")); + g_ptr_array_add(a, NULL); + process->target_xml = g_strjoinv(NULL, (void *)a->pdata); } - return buf; + return process->target_xml; } if (cc->gdb_get_dynamic_xml) { char *xmlname = g_strndup(p, len); @@ -2270,6 +2274,6 @@ void gdb_create_default_process(GDBState *s) process = &s->processes[s->process_num - 1]; process->pid = pid; process->attached = false; - process->target_xml[0] = '\0'; + process->target_xml = NULL; } diff --git a/gdbstub/softmmu.c b/gdbstub/softmmu.c index f509b7285d..5282324764 100644 --- a/gdbstub/softmmu.c +++ b/gdbstub/softmmu.c @@ -293,7 +293,7 @@ static int find_cpu_clusters(Object *child, void *opaque) assert(cluster->cluster_id != UINT32_MAX); process->pid = cluster->cluster_id + 1; process->attached = false; - process->target_xml[0] = '\0'; + process->target_xml = NULL; return 0; }