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 A6F743858413 for ; Wed, 6 Sep 2023 15:43:31 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A6F743858413 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=1694015011; 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=gNwb03ePhdK0ULwFle/jFrmBqBB+xApSprNdyIUCqgI=; b=PhnEB9WQH8Aht0Hw+XFfSHA3EPVnnKefPsr/MzkdPIASj3qoUEC5Ss6/x0Vi+tGYL5S3Nr rp1TuSVSOFTH2Zn/DUaLzRko+GUANnKvooC5zmKcuHuSU6aGdRyZUjD8bMjqTv8lIa+Rx7 nlV2h74gBHLznJk230dD0op0VqDNh2Q= 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-695-7V3pDyiQPpS0pxs9nobDuQ-1; Wed, 06 Sep 2023 11:43:29 -0400 X-MC-Unique: 7V3pDyiQPpS0pxs9nobDuQ-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id D54911837180; Wed, 6 Sep 2023 15:43:28 +0000 (UTC) Received: from oak (unknown [10.22.33.147]) by smtp.corp.redhat.com (Postfix) with ESMTPS id B942E140E950; Wed, 6 Sep 2023 15:43:28 +0000 (UTC) Date: Wed, 6 Sep 2023 11:43:27 -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: <20230906154327.GH3849957@oak> References: <20230831202122.2239619-1-josimmon@redhat.com> <7a018de3-c2e8-f7f3-af4b-229995e683c1@linaro.org> MIME-Version: 1.0 In-Reply-To: <7a018de3-c2e8-f7f3-af4b-229995e683c1@linaro.org> X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 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 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 <<< Thanks, Joe