From patchwork Wed Sep 6 19:33:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joe Simmons-Talbott X-Patchwork-Id: 1830523 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=RVe4VAga; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Rgsxk2Q4Gz1yh1 for ; Thu, 7 Sep 2023 05:34:02 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8B1FF385842A for ; Wed, 6 Sep 2023 19:33:59 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@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 087153858C78 for ; Wed, 6 Sep 2023 19:33:49 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 087153858C78 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1694028829; 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; bh=9mX2VzhdfTwxV9oNBYqmYhFgBVXL0ltJEyHiBZ2+No8=; b=RVe4VAgaONLxTkn5gKvPdgqhNesoWAZc/BmML55NuLe4Wn/Qrc86KMg7uM1YGbIuzk1fnb v0mrhZwTkU44FyH9xDa7qB4aQc5B54KdREBevxDR4PgmjW2RPQHHyC3+MKVm4A5y8blNnZ pSj/yzwG+qxuocZ47wL+NbwUUSMZduc= Received: from mail-ot1-f71.google.com (mail-ot1-f71.google.com [209.85.210.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-503-DJTEgrO7N_mKiBgjbWy41A-1; Wed, 06 Sep 2023 15:33:48 -0400 X-MC-Unique: DJTEgrO7N_mKiBgjbWy41A-1 Received: by mail-ot1-f71.google.com with SMTP id 46e09a7af769-6b9cf208fb5so228307a34.3 for ; Wed, 06 Sep 2023 12:33:48 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1694028827; x=1694633627; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=9mX2VzhdfTwxV9oNBYqmYhFgBVXL0ltJEyHiBZ2+No8=; b=XepFrqHM3b+xgz3V7vw8AT7+sWQoTIIKgqsy717rC+90jeoa3QfRhZUJklS3swi6tr hOUmxrsF/3MhxCFEviOK07yBfRhj5Mk7PZuPkbZtgC7j1xUSDL9EgY11KZmBcEDYSK6z n5bWGjooYRLNzK2gq38d7TWap250qdB2JRKTdWmmn0w7IYh2KTKOHt2bO4PqcpnAmLle gJ9M1icSLoQye5q4AYyc8Y/4ULKok8m9Wjqfch/hdPg+p6Fyjls+LQqKjTU1dyDpRZqN YeN0OY/V/ruxg2XbUxdFwlJ5w4IR5Q0FhfFWdhox1DZyNyeanygX2Z7D7chWh2zXKo3P afpA== X-Gm-Message-State: AOJu0YwuBNsqqI2uNoCqYqkRqpIxEYG04t9VDP8GzLWF3ouu14/kGIZf 8zjcN7h+6WRYsOt2eRd9DR5yQBENv6EJ3sMwe8HGjJBQVZkgzbu2yUxkmswkg+IrobLGW9HO3XR q+clQVGxG/YmU3HrkVELmr1t9w6PqmOz7ztRrzLhfr/jyynVLGZp/KD1AZ02l8pqwi6DAxfIPuw uWO6jk X-Received: by 2002:a9d:695a:0:b0:6bd:be5:daa2 with SMTP id p26-20020a9d695a000000b006bd0be5daa2mr19546553oto.33.1694028826938; Wed, 06 Sep 2023 12:33:46 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFwOpjHujqunOJexTaCxAfLNe/VnpzH3aI4HJgkQgFWiNdeb8KgNbJZS3HgS4mo3EsYJ/2Uzw== X-Received: by 2002:a9d:695a:0:b0:6bd:be5:daa2 with SMTP id p26-20020a9d695a000000b006bd0be5daa2mr19546538oto.33.1694028826645; Wed, 06 Sep 2023 12:33:46 -0700 (PDT) Received: from oak.redhat.com (c-71-206-142-238.hsd1.va.comcast.net. [71.206.142.238]) by smtp.gmail.com with ESMTPSA id pi16-20020a05620a379000b0076f0744ff50sm5146723qkn.136.2023.09.06.12.33.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Sep 2023 12:33:45 -0700 (PDT) From: Joe Simmons-Talbott To: libc-alpha@sourceware.org Cc: Joe Simmons-Talbott , Adhemerval Zanella Subject: [PATCH] stdio: Remove __libc_message alloca usage Date: Wed, 6 Sep 2023 15:33:22 -0400 Message-ID: <20230906193339.2134909-1-josimmon@redhat.com> X-Mailer: git-send-email 2.41.0 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.6 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, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP 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: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org Use a fixed size array instead. The maximum number of arguments is set by macro tricks. Co-authored-by: Adhemerval Zanella --- include/stdio.h | 28 ++++++++++++++++++++++- sysdeps/posix/libc_fatal.c | 47 +++++++++++--------------------------- 2 files changed, 40 insertions(+), 35 deletions(-) diff --git a/include/stdio.h b/include/stdio.h index 6755877911..e2100066ed 100644 --- a/include/stdio.h +++ b/include/stdio.h @@ -172,10 +172,36 @@ extern int __gen_tempname (char *__tmpl, int __suffixlen, int __flags, and abort. */ extern void __libc_fatal (const char *__message) __attribute__ ((__noreturn__)); -_Noreturn void __libc_message (const char *__fnt, ...) attribute_hidden; extern void __fortify_fail (const char *msg) __attribute__ ((__noreturn__)); libc_hidden_proto (__fortify_fail) +/* The maximum number of varargs allowed in a __libc_message format string */ +#define LIBC_MESSAGE_MAX_ARGS 4 + +_Noreturn void __libc_message_impl (const char *__fnt, ...) attribute_hidden; + +#define __libc_message0(fmt) \ + __libc_message_impl (fmt) +#define __libc_message1(fmt, a1) \ + __libc_message_impl (fmt, a1) +#define __libc_message2(fmt, a1, a2) \ + __libc_message_impl (fmt, a1, a2) +#define __libc_message3(fmt, a1, a2, a3) \ + __libc_message_impl (fmt, a1, a2, a3) +#define __libc_message4(fmt, a1, a2, a3, a4) \ + __libc_message_impl (fmt, a1, a2, a3, a4) + +#define __libc_message_concat_x(a,b) a##b +#define __libc_message_concat(a,b) __libc_message_concat_x (a, b) + +#define __libc_message_nargs_x(a0,a1,a2,a3,a4,a5,a6,...) a6 +#define __libc_message_nargs(b, ...) \ + __libc_message_nargs_x (__VA_ARGS__,6,5,4,3,2,1,0,) +#define __libc_message_disp(b, ...) \ + __libc_message_concat (b, __libc_message_nargs (__VA_ARGS__))(__VA_ARGS__) +#define __libc_message(...) \ + __libc_message_disp (__libc_message, __VA_ARGS__) + /* Acquire ownership of STREAM. */ extern void __flockfile (FILE *__stream) attribute_hidden; diff --git a/sysdeps/posix/libc_fatal.c b/sysdeps/posix/libc_fatal.c index 70edcc10c1..cf28387ee6 100644 --- a/sysdeps/posix/libc_fatal.c +++ b/sysdeps/posix/libc_fatal.c @@ -45,22 +45,13 @@ writev_for_fatal (int fd, const struct iovec *iov, size_t niov, size_t total) } #endif -struct str_list -{ - const char *str; - size_t len; - struct str_list *next; -}; - /* Abort with an error message. */ void -__libc_message (const char *fmt, ...) +__libc_message_impl (const char *fmt, ...) { va_list ap; int fd = -1; - va_start (ap, fmt); - #ifdef FATAL_PREPARE FATAL_PREPARE; #endif @@ -68,9 +59,11 @@ __libc_message (const char *fmt, ...) if (fd == -1) fd = STDERR_FILENO; - struct str_list *list = NULL; - int nlist = 0; + struct iovec iov[LIBC_MESSAGE_MAX_ARGS * 2 - 1]; + int iovcnt = 0; + ssize_t total = 0; + va_start (ap, fmt); const char *cp = fmt; while (*cp != '\0') { @@ -100,28 +93,16 @@ __libc_message (const char *fmt, ...) cp = next; } - struct str_list *newp = alloca (sizeof (struct str_list)); - newp->str = str; - newp->len = len; - newp->next = list; - list = newp; - ++nlist; + iov[iovcnt].iov_base = (char *) str; + iov[iovcnt].iov_len = len; + total += len; + iovcnt++; } + va_end (ap); - if (nlist > 0) + if (iovcnt > 0) { - struct iovec *iov = alloca (nlist * sizeof (struct iovec)); - ssize_t total = 0; - - for (int cnt = nlist - 1; cnt >= 0; --cnt) - { - iov[cnt].iov_base = (char *) list->str; - iov[cnt].iov_len = list->len; - total += list->len; - list = list->next; - } - - WRITEV_FOR_FATAL (fd, iov, nlist, total); + WRITEV_FOR_FATAL (fd, iov, iovcnt, total); total = (total + 1 + GLRO(dl_pagesize) - 1) & ~(GLRO(dl_pagesize) - 1); struct abort_msg_s *buf = __mmap (NULL, total, @@ -131,7 +112,7 @@ __libc_message (const char *fmt, ...) { buf->size = total; char *wp = buf->msg; - for (int cnt = 0; cnt < nlist; ++cnt) + for (int cnt = 0; cnt < iovcnt; ++cnt) wp = mempcpy (wp, iov[cnt].iov_base, iov[cnt].iov_len); *wp = '\0'; @@ -144,8 +125,6 @@ __libc_message (const char *fmt, ...) } } - va_end (ap); - /* Kill the application. */ abort (); }