From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 DAB4C3858C3A for ; Wed, 6 Sep 2023 19:39:25 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DAB4C3858C3A 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=1694029165; 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: in-reply-to:in-reply-to:references:references; bh=u4n6CVnKQ+O5dJwOQiTbI3GQmJE2dzjzBKAK6ovnS0Q=; b=WfXzPYNRi6xxOG9ORW5YrPBWK1CaqT8JUbPsSXWWz69LRKJAOg6hMCvyT0FCMlydguRGzg OTrgpoj/SM72rlltTiqVva+9zTXReFyeeLrbWZYfIdunfhFqe5cKgQg14K0s4Fzc34OJx+ LygTCJ0dFZIEgr9NQoEWtYYPWMoQUSI= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-169-C4XXG-fbOdK-OghLfeKfOg-1; Wed, 06 Sep 2023 15:39:23 -0400 X-MC-Unique: C4XXG-fbOdK-OghLfeKfOg-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 3F426181C325; Wed, 6 Sep 2023 19:39:23 +0000 (UTC) Received: from oak (unknown [10.22.33.147]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 2301A10EA0; Wed, 6 Sep 2023 19:39:23 +0000 (UTC) Date: Wed, 6 Sep 2023 15:39:21 -0400 From: Joe Simmons-Talbott To: Adhemerval Zanella Netto Cc: libc-alpha@sourceware.org Subject: Re: [PATCH v2] libc_fatal: Get rid of alloca Message-ID: <20230906193921.GJ3849957@oak> References: <20230831202122.2239619-1-josimmon@redhat.com> <7a018de3-c2e8-f7f3-af4b-229995e683c1@linaro.org> <20230906154327.GH3849957@oak> <19c19475-24a0-08ff-0e57-6d2c7a40ffd4@linaro.org> <20230906184550.GI3849957@oak> MIME-Version: 1.0 In-Reply-To: <20230906184550.GI3849957@oak> X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=us-ascii Content-Disposition: inline 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,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 List-Id: On Wed, Sep 06, 2023 at 02:45:50PM -0400, Joe Simmons-Talbott wrote: > On Wed, Sep 06, 2023 at 01:51:12PM -0300, Adhemerval Zanella Netto wrote: > > > > > > On 06/09/23 12:43, Joe Simmons-Talbott wrote: > > > On Fri, Sep 01, 2023 at 11:23:08AM -0300, Adhemerval Zanella Netto wrote: > > >> > > >> > > >> On 31/08/23 17:20, Joe Simmons-Talbott via Libc-alpha wrote: > > >>> Use fixed size arrays in place of alloca to avoid potential stack overflow. > > >>> Limit the number of varargs to __libc_message to 10. > > >> > > >> I think we enforce the maximum number of arguments internally with some > > >> macro tricks, so there is no need to bail out to abort without printing > > >> the message. > > >> > > >>> --- > > >>> Changes to v1: > > >>> * Use a fixed size array rather than scratch_buffers since we can only > > >>> call async signal safe functions. > > >>> > > >>> sysdeps/posix/libc_fatal.c | 11 +++++++++-- > > >>> 1 file changed, 9 insertions(+), 2 deletions(-) > > >>> > > >>> diff --git a/sysdeps/posix/libc_fatal.c b/sysdeps/posix/libc_fatal.c > > >>> index 70edcc10c1..16929addab 100644 > > >>> --- a/sysdeps/posix/libc_fatal.c > > >>> +++ b/sysdeps/posix/libc_fatal.c > > >>> @@ -45,6 +45,9 @@ writev_for_fatal (int fd, const struct iovec *iov, size_t niov, size_t total) > > >>> } > > >>> #endif > > >>> > > >>> +/* The maximum number of varargs allowed in a __libc_message format string */ > > >>> +#define MAX_NLIST 10 > > >>> + > > >>> struct str_list > > >>> { > > >>> const char *str; > > >>> @@ -58,6 +61,7 @@ __libc_message (const char *fmt, ...) > > >>> { > > >>> va_list ap; > > >>> int fd = -1; > > >>> + struct str_list _newp[MAX_NLIST]; > > >> > > >> There are no need to track the string list, it is used essentially to > > >> construct the iovec struct to call writev. You can construct the > > >> iovec directly. > > >> > > >>> > > >>> va_start (ap, fmt); > > >>> > > >>> @@ -70,6 +74,7 @@ __libc_message (const char *fmt, ...) > > >>> > > >>> struct str_list *list = NULL; > > >>> int nlist = 0; > > >>> + struct iovec iov[MAX_NLIST]; > > >>> > > >>> const char *cp = fmt; > > >>> while (*cp != '\0') > > >>> @@ -100,17 +105,18 @@ __libc_message (const char *fmt, ...) > > >>> cp = next; > > >>> } > > >>> > > >>> - struct str_list *newp = alloca (sizeof (struct str_list)); > > >>> + struct str_list *newp = &_newp[nlist]; > > >>> newp->str = str; > > >>> newp->len = len; > > >>> newp->next = list; > > >>> list = newp; > > >>> ++nlist; > > >>> + if (nlist > MAX_NLIST) > > >>> + goto fail_out; > > >>> } > > >>> > > >>> if (nlist > 0) > > >>> { > > >>> - struct iovec *iov = alloca (nlist * sizeof (struct iovec)); > > >>> ssize_t total = 0; > > >>> > > >>> for (int cnt = nlist - 1; cnt >= 0; --cnt) > > >>> @@ -146,6 +152,7 @@ __libc_message (const char *fmt, ...) > > >>> > > >>> va_end (ap); > > >>> > > >>> +fail_out: > > >>> /* Kill the application. */ > > >>> abort (); > > >>> } > > >> > > >> Below is a patch on top of your which enforces the maximum number of > > >> supported variadic arguments with a similar trick I used for > > >> INLINE_SYSCALL_CALL. One will need to explicit implement a new macro > > >> for each __libc_message usage with a number of arguments larger than > > >> LIBC_MESSAGE_MAX_ARGS, but it will fail at build time if you try to > > >> use __libc_message with 5 or more arguments. > > > > > > Thanks for the patch. I applied it and during testing see failures for > > > stdlib/tst-bz20544 with the following output: > > > > > > <<< > > > Did not find expected string in error output: > > > expected: >>>assertion failed: func != NULL > > > <<< > > > actual: >>>Fatal glibc error: on_exit.c:31 (__on_exit): assertion failed: ): assertion failed: %s > > > > > > <<< > > > Did not find expected string in error output: > > > expected: >>>assertion failed: func != NULL > > > <<< > > > actual: >>>Fatal glibc error: cxa_atexit.c:41 (__internal_atexit): assertion failed: ): assertion failed: %s > > > > > > <<< > > > Did not find expected string in error output: > > > expected: >>>assertion failed: func != NULL > > > <<< > > > actual: >>>Fatal glibc error: cxa_atexit.c:41 (__internal_atexit): assertion failed: ): assertion failed: %s > > > > > > <<< > > > > That is unexpected, I have not see any regression testing here. Could > > you check a clean build with a branch I have pushed on sourceware [1]? > > > > [1] https://sourceware.org/git/?p=glibc.git;a=commit;h=742b35228f3efa25d41d14f27c8911f308514b28 > > > > I saw the same error with your branch on a clean build. I think the > issue is that iov needs to have space for the parts of the format > string that are not varargs too. I replaced it with: > > struct iovec iov[LIBC_MESSAGE_MAX_ARGS * 2 - 1]; > > and that fixed the errors. > I've posted the patch[1] Thanks, Joe [1] https://sourceware.org/pipermail/libc-alpha/2023-September/151403.html