From patchwork Sun Sep 3 04:30:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Akihiko Odaki X-Patchwork-Id: 1829161 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.20230601.gappssmtp.com header.i=@daynix-com.20230601.gappssmtp.com header.a=rsa-sha256 header.s=20230601 header.b=0npzz2Ob; 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 4Rdf6W2ff3z1yhH for ; Sun, 3 Sep 2023 14:33:55 +1000 (AEST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qcem2-0002Eu-9n; Sun, 03 Sep 2023 00:31:46 -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 1qcelu-0002DJ-UW for qemu-devel@nongnu.org; Sun, 03 Sep 2023 00:31:42 -0400 Received: from mail-pj1-x1035.google.com ([2607:f8b0:4864:20::1035]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qcelr-0006va-4K for qemu-devel@nongnu.org; Sun, 03 Sep 2023 00:31:38 -0400 Received: by mail-pj1-x1035.google.com with SMTP id 98e67ed59e1d1-26f3975ddd4so270213a91.1 for ; Sat, 02 Sep 2023 21:31:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1693715493; x=1694320293; darn=nongnu.org; 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=z/aZ16AuCEo7wpubooP4JHdg7IKFA98mYu/To/TblDc=; b=0npzz2ObQNRkZi4Eh38hHblL59dVrtwiw2oQClBOrUmpDWuqBo/47ygOCJ3Ab9J5O5 DX2SZMekHBClpSlGIR5qOQPqAbpsXGz4QXS16Y2gVG5GTbzmd2AVfd878z0goGzkGW2V IrhpQ5BmTILRwMDlHtMsIDl77O7+FmE7BcTvwSmr1GZRfY94cqVKKqqbW1d7oGw7cnSf 8BENLVoMPQDJSurPCEraZzg6IIZs/wBcyvsaOseLkuintGhHpBrgc5wbSwV8ONdPORit 8uL5kx1sBmy1mbvpfTNzZXcUy49PsF8U6yYRA27cHpBNRFeEE8W/IVMZW/s7CmKcJsLu 301w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693715493; x=1694320293; 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=z/aZ16AuCEo7wpubooP4JHdg7IKFA98mYu/To/TblDc=; b=XKBLeM8el1JH7GXPcZPUvXiGcuimPe4FuYuOnSAGRHn6K+eLeFwPSZiChTtnfHOVCy 5pQfAy5QIIljurO/0+u9xLG+YM05+HdlEgEefYfbZ32xp4W8cSubbC9/ETUnxHH7SBJh PKJoZUXGtvUx2WcRDfBvjZjIP6Zkb/v6yLiKMA+mLc7CpcDncGr05xeec3bEdT/lbkGC 4YIB76o4/X3fyVb10sqUD0coOlHp8Z+/RWWGdwuBq4rQ7agXDY7HbsrUnUBJk43vp3CM EQrbikzUdneMyTpICOO1Fu9rI6XRyhN9kTZ7XxTz1nDi4jMDk769NdhJas31/PXj2wj+ 0nqQ== X-Gm-Message-State: AOJu0YwPksWtG4YzI7LIRcwfZ2kWVE+AfN6jR/te02L3RQuB7HZICr55 /pdFJWRKKt4fAZQrefuBUC6+Ew== X-Google-Smtp-Source: AGHT+IGpUlqXdUGU543EtzTNpwbZyZkf86NKdkwWU8r8yW+vJCJ61u2R3VbTHYN/X8Ykc9YUKIDPgg== X-Received: by 2002:a17:90b:33d1:b0:25e:a8ab:9157 with SMTP id lk17-20020a17090b33d100b0025ea8ab9157mr5715886pjb.22.1693715493660; Sat, 02 Sep 2023 21:31:33 -0700 (PDT) Received: from alarm.flets-east.jp ([2400:4050:a840:1e00:78d2:b862:10a7:d486]) by smtp.gmail.com with ESMTPSA id iz2-20020a170902ef8200b001bb9f104328sm5295792plb.146.2023.09.02.21.31.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 02 Sep 2023 21:31:33 -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 5/8] gdbstub: Dynamically allocate target.xml buffer Date: Sun, 3 Sep 2023 13:30:15 +0900 Message-ID: <20230903043030.20708-6-akihiko.odaki@daynix.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20230903043030.20708-1-akihiko.odaki@daynix.com> References: <20230903043030.20708-1-akihiko.odaki@daynix.com> MIME-Version: 1.0 Received-SPF: none client-ip=2607:f8b0:4864:20::1035; envelope-from=akihiko.odaki@daynix.com; helo=mail-pj1-x1035.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 78711801db..3f97dc66e5 100644 --- a/gdbstub/gdbstub.c +++ b/gdbstub/gdbstub.c @@ -367,33 +367,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_xml_file); - pstrcat(buf, buf_sz, "\"/>"); + g_ptr_array_add( + a, + g_markup_printf_escaped("", + cc->gdb_core_xml_file)); for (r = cpu->gdb_regs; r; r = r->next) { - pstrcat(buf, buf_sz, "xml); - pstrcat(buf, buf_sz, "\"/>"); + g_ptr_array_add( + a, + g_markup_printf_escaped("", + r->xml)); } - 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); @@ -2199,6 +2203,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; }