From patchwork Mon Sep 11 12:53:25 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: 1832281 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=hXxZX0YU; 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 4Rkmqn3hkdz1yhX for ; Mon, 11 Sep 2023 22:53:57 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3E3D63858C1F for ; Mon, 11 Sep 2023 12:53:55 +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 05BB43858D28 for ; Mon, 11 Sep 2023 12:53:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 05BB43858D28 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=1694436825; 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=Xi6NWw9ipDOBt1MPO35Z0zLKhUaYFYp6YOBTv++saY8=; b=hXxZX0YUBuJ13OUU+FfkOKbQAFye+dHOpSwPAYZKAH0FdJpRV0kPSmkGc1vPODMZ2jZRRr rkuRPm1AYcJiMfoiKyxXTmhGXr5mDLP2eILP/olWnOK8Sw3jj6IO6923vgcw3nSgR54w8C l+a016bZAPxKLkrbHuAtheERgDij76U= Received: from mail-qv1-f72.google.com (mail-qv1-f72.google.com [209.85.219.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-57-lFaVpSdOOQCP0gfLjX9I_w-1; Mon, 11 Sep 2023 08:53:44 -0400 X-MC-Unique: lFaVpSdOOQCP0gfLjX9I_w-1 Received: by mail-qv1-f72.google.com with SMTP id 6a1803df08f44-63d0c38e986so44512366d6.1 for ; Mon, 11 Sep 2023 05:53:44 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694436824; x=1695041624; 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=Xi6NWw9ipDOBt1MPO35Z0zLKhUaYFYp6YOBTv++saY8=; b=N26fk77KRLM2+a6/JwG0ESnvg/Ukad0FYPxyruicc3NPaib6abfjZIVQQyrVrRVsjW 6UpfNh0vZV05xLJ0FJZQzacGkGq8czhNqf1rWY/gHExPUYYA7RsTD/0SxH4PMYt+v2nF /sKP3GAasVdumciXLRlH5N+kmRzsbpD+pP9HozFs26f7pc2uJpd53W5IOLzg2mlu1FuC H5CTmoBSo8+/OLmk9ueEkqA61HsvBedUyLPw2hm4IsJcskPPEKrowDcKHknO7Mt4zSpR juuwVtfpjbyMzOi/OmTAwU3BaUUUU8QOd5yEMbZcSShUOJ0VnwEws8Lhbil/wpLdU1E6 OIeA== X-Gm-Message-State: AOJu0Yz5ks1C1nk6H2FrMenEazPIhccmGG67xZxaAtz/TRxpEqdwPEdq KU8OxFElJSvkV7j1VFHE+b01S40mHHdwzUV7G7m71+shjktXGRVZ+iLji8QD0eKTJjXBsI/OewH viOG8LsXHV5UyFcZ24uMDlHFgZw7DqQyyEK4npo7OzSC6MK+hDqvFhr5PU4fz1F9dN6Awl1wZGb GJecc2 X-Received: by 2002:ad4:5683:0:b0:63c:fac8:ec0a with SMTP id bd3-20020ad45683000000b0063cfac8ec0amr7421164qvb.21.1694436823820; Mon, 11 Sep 2023 05:53:43 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF+54M7o4FGsSv1Y5KljXmwPqK+VXZQBIjv72wyqeznTIFmqa4TsUTZQMD7BPBLXc4vIzKwBg== X-Received: by 2002:ad4:5683:0:b0:63c:fac8:ec0a with SMTP id bd3-20020ad45683000000b0063cfac8ec0amr7421151qvb.21.1694436823523; Mon, 11 Sep 2023 05:53:43 -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 b14-20020a0cb3ce000000b0062def68f75csm2929480qvf.124.2023.09.11.05.53.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Sep 2023 05:53:43 -0700 (PDT) From: Joe Simmons-Talbott To: libc-alpha@sourceware.org Cc: Joe Simmons-Talbott , Adhemerval Zanella Subject: [PATCH v2] stdio: Remove __libc_message alloca usage Date: Mon, 11 Sep 2023 08:53:25 -0400 Message-ID: <20230911125341.3231008-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 Reviewed-by: Adhemerval Zanella --- Changes to v1: * Add a printf attribute to help catch mismatched arguments as well. include/stdio.h | 29 ++++++++++++++++++++++- sysdeps/posix/libc_fatal.c | 47 +++++++++++--------------------------- 2 files changed, 41 insertions(+), 35 deletions(-) diff --git a/include/stdio.h b/include/stdio.h index 6755877911..7e70f95c6d 100644 --- a/include/stdio.h +++ b/include/stdio.h @@ -172,10 +172,37 @@ 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 + __attribute__ ((__format__ (__printf__, 1, 2))); + +#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 (); }