From patchwork Thu Jun 1 09:27:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 70438 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5B0853857739 for ; Thu, 1 Jun 2023 09:28:35 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5B0853857739 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1685611715; bh=U+wjB5kfHv92vGXbc+EDlVvgLFohzLbpVXu7qzssSK8=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=DmYd1bHGvVGR4aT8SFPMhamQovnPnjmoalEklBNRqFb6/TPmvY/BTdbtVTNQBAt2O YNQN9KpZCYb/qeWpqlJUL2XjgdOyXje1FcnO29MwJoUBcsMn/DsDRkTeVX3SQ6OBaf jKpGrkIqTEffcsdMwIS6RplRhSgQ2JIbeKjYE50A= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 57DC53857707 for ; Thu, 1 Jun 2023 09:28:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 57DC53857707 Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-39-x_RD9Dg6OCS36qxJezPqkQ-1; Thu, 01 Jun 2023 05:28:04 -0400 X-MC-Unique: x_RD9Dg6OCS36qxJezPqkQ-1 Received: by mail-wm1-f72.google.com with SMTP id 5b1f17b1804b1-3f6089a9689so4343305e9.1 for ; Thu, 01 Jun 2023 02:28:04 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685611683; x=1688203683; 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=U+wjB5kfHv92vGXbc+EDlVvgLFohzLbpVXu7qzssSK8=; b=Hw/PUtAFo2bQHe6KGWROimo8qcUqsmZKaMZ31wMUgVA4G/NL0VVD1FI7ZZWI2Rw0Nm Tj8dBKpmO+OCfa8WW+AMBCH0o0obmBWyeQ+TwWy2AfcRnKwtkPyDiO34cB5XEKmUzftR 8aS82oy7kbEzo/f4ZxvpaG0SpKm2M7V/leiLKr6PSiIA/Jlztb3lBP4WBFVQvKn2e6Tm m/dmLbIVZKVA9k/MdKw2o8mXDxmMmUxcq3gnG/mL2QdwWh+0qGx/RzgAx9+Ag+k2YHWu xWcroOZ4I1sivprAj3kF7eUfqsIB6NgsY16UtGUoMELTO62eJ9jD1zYOtbT49foMVV0+ ZXqA== X-Gm-Message-State: AC+VfDwH+pMBtGbEdKj5rDcoLj2A9XoY4U9w2THiliTiFvNmEMEPM+7p 3ZoEqPmzG8F54OGT7J/cofv4+I/Mh6zUeoRBlWMcgVDxLl8+3Cu01sfoSv9iX3WCam/CMx2ZPu1 +2Pcr52ButhYjrQdR5LFJ7PH0u5BX2k8FeiRBT4o1bjhtML2AkIffRsNATr3KOynGylaI8Fz/lV hGBY+GeA== X-Received: by 2002:a7b:ce0a:0:b0:3f4:d18f:b2fb with SMTP id m10-20020a7bce0a000000b003f4d18fb2fbmr1374114wmc.8.1685611683482; Thu, 01 Jun 2023 02:28:03 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ73KXcvloiPMJ/bRpvWAzbk8sozc7n/YLERaoY9hOR67CPaMzK0q+eJn3R8dncW5QEzJDXcHA== X-Received: by 2002:a7b:ce0a:0:b0:3f4:d18f:b2fb with SMTP id m10-20020a7bce0a000000b003f4d18fb2fbmr1374096wmc.8.1685611683206; Thu, 01 Jun 2023 02:28:03 -0700 (PDT) Received: from localhost (11.72.115.87.dyn.plus.net. [87.115.72.11]) by smtp.gmail.com with ESMTPSA id t4-20020a5d5344000000b00307acec258esm9632973wrv.3.2023.06.01.02.28.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Jun 2023 02:28:02 -0700 (PDT) To: gdb-patches@sourceware.org Cc: Simon Marchi , Andrew Burgess Subject: [PATCH 2/4] gdb: remove two uses of alloca from printcmd.c Date: Thu, 1 Jun 2023 10:27:51 +0100 Message-Id: X-Mailer: git-send-email 2.25.4 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Andrew Burgess via Gdb-patches From: Andrew Burgess Reply-To: Andrew Burgess Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" Remove a couple of uses of alloca from printcmd.c, and replace them with gdb::byte_vector. An earlier variant of this patch was proposed in this thread: https://inbox.sourceware.org/gdb-patches/cover.1677533215.git.aburgess@redhat.com/ however, there was push back on that thread due to it adding extra dynamic allocation, i.e. moving the memory buffers off the stack on to the heap. However, of all the patches originally proposed, I think in these two cases moving off the stack is the correct thing to do. Unlike all the other patches in the original series, where the data being read was (mostly) small in size, a register, or a couple of registers, in this case we are reading an arbitrary string from the inferior. This could be any size, and so should not be placed on the stack. So in this commit I replace the use of alloca with std::byte_vector and simplify the logic a little (I think) to take advantage of the ability of std::byte_vector to dynamically grow in size. Of course, really, we should probably be checking the max-value-size setting as we load the string to stop GDB crashing if a corrupted inferior causes GDB to try read a stupidly large amount of memory... but I'm leaving that for a follow on patch. There should be no user visible changes after this commit. --- gdb/printcmd.c | 49 ++++++++++++++++++++++--------------------------- 1 file changed, 22 insertions(+), 27 deletions(-) diff --git a/gdb/printcmd.c b/gdb/printcmd.c index f9517e6e086..6f8a7f1420a 100644 --- a/gdb/printcmd.c +++ b/gdb/printcmd.c @@ -2447,7 +2447,7 @@ static void printf_c_string (struct ui_file *stream, const char *format, struct value *value) { - const gdb_byte *str; + gdb::byte_vector str; if (((value->type ()->code () != TYPE_CODE_PTR && value->lval () == lval_internalvar) || value->type ()->code () == TYPE_CODE_ARRAY) @@ -2459,11 +2459,10 @@ printf_c_string (struct ui_file *stream, const char *format, character. This protects against corrupted C-style strings that lack the terminating null char. It also allows Ada-style strings (not null terminated) to be printed without problems. */ - gdb_byte *tem_str = (gdb_byte *) alloca (len + 1); + str.resize (len + 1); - memcpy (tem_str, value->contents ().data (), len); - tem_str [len] = 0; - str = tem_str; + memcpy (str.data (), value->contents ().data (), len); + str [len] = 0; } else { @@ -2478,31 +2477,30 @@ printf_c_string (struct ui_file *stream, const char *format, return; } - /* This is a %s argument. Find the length of the string. */ - size_t len; - - for (len = 0;; len++) + /* This is a %s argument. Build the string in STR which is + currently empty. */ + gdb_assert (str.size () == 0); + for (size_t len = 0;; len++) { gdb_byte c; QUIT; read_memory (tem + len, &c, 1); + str.push_back (c); if (c == 0) break; } - /* Copy the string contents into a string inside GDB. */ - gdb_byte *tem_str = (gdb_byte *) alloca (len + 1); - - if (len != 0) - read_memory (tem, tem_str, len); - tem_str[len] = 0; - str = tem_str; + /* We will have passed through the above loop at least once, and will + only exit the loop when we have pushed a zero byte onto the end of + STR. */ + gdb_assert (str.size () > 0); + gdb_assert (str.back () == 0); } DIAGNOSTIC_PUSH DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL - gdb_printf (stream, format, (char *) str); + gdb_printf (stream, format, (char *) str.data ()); DIAGNOSTIC_POP } @@ -2521,6 +2519,7 @@ printf_wide_c_string (struct ui_file *stream, const char *format, struct type *wctype = lookup_typename (current_language, "wchar_t", NULL, 0); int wcwidth = wctype->length (); + gdb::optional tem_str; if (value->lval () == lval_internalvar && c_is_string_type_p (value->type ())) @@ -2543,23 +2542,19 @@ printf_wide_c_string (struct ui_file *stream, const char *format, /* This is a %s argument. Find the length of the string. */ enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); - gdb_byte *buf = (gdb_byte *) alloca (wcwidth); + tem_str.emplace (); for (len = 0;; len += wcwidth) { QUIT; - read_memory (tem + len, buf, wcwidth); - if (extract_unsigned_integer (buf, wcwidth, byte_order) == 0) + tem_str->resize (tem_str->size () + wcwidth); + gdb_byte *dst = tem_str->data () + len; + read_memory (tem + len, dst, wcwidth); + if (extract_unsigned_integer (dst, wcwidth, byte_order) == 0) break; } - /* Copy the string contents into a string inside GDB. */ - gdb_byte *tem_str = (gdb_byte *) alloca (len + wcwidth); - - if (len != 0) - read_memory (tem, tem_str, len); - memset (&tem_str[len], 0, wcwidth); - str = tem_str; + str = tem_str->data (); } auto_obstack output;