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 758F83858D28 for ; Mon, 3 Jul 2023 18:25:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 758F83858D28 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=1688408717; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=SHYtOvMIUNg5POStRG4uWD6ls7Rrld9i0LgIxskGHtE=; b=JPcbU9VbMaahnJEdBTyL+3xo7LLVev2JnCoBgtBXJWIRSblU1wdCkX3iLByzVL+i5CS7OY oVIdf5XNNFLRFTN9ZNfv5IYkcubZqh5bwfpmTc2QvN8ov7Q49dR3rdynwDNbjU30ZeHCQJ D13gZOM+iuWxgD2riQ3Dcw1Rs8MCQQI= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-37-UXXl-w1LMdKzHCmrU9FFiw-1; Mon, 03 Jul 2023 14:25:15 -0400 X-MC-Unique: UXXl-w1LMdKzHCmrU9FFiw-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 4ABAA3C0F663 for ; Mon, 3 Jul 2023 18:25:15 +0000 (UTC) Received: from oak (unknown [10.22.8.210]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 33AD24087C6A for ; Mon, 3 Jul 2023 18:25:15 +0000 (UTC) Date: Mon, 3 Jul 2023 14:25:13 -0400 From: Joe Simmons-Talbott To: libc-alpha@sourceware.org Subject: Re: [PATCH] msort: Get rid of alloca. Message-ID: <20230703182513.GV6392@oak> References: <20230630172636.384922-1-josimmon@redhat.com> MIME-Version: 1.0 In-Reply-To: <20230630172636.384922-1-josimmon@redhat.com> X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 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=-12.0 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,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 List-Id: On Fri, Jun 30, 2023 at 01:26:36PM -0400, Joe Simmons-Talbott wrote: > Use a scratch_buffer rather than alloca/malloc to avoid potential stack > overflow. This patch failed the check on arm[1] but I'm unable to replicate that locally with build-many-glibcs.py. Does anyone have any suggestions for replicating these testcase failures? Thanks, Joe [1] https://patchwork.sourceware.org/project/glibc/patch/20230630172636.384922-1-josimmon@redhat.com/ > --- > stdlib/msort.c | 94 +++++++++++++++++++++++--------------------------- > 1 file changed, 43 insertions(+), 51 deletions(-) > > diff --git a/stdlib/msort.c b/stdlib/msort.c > index bbaa5e9f82..237dbb444f 100644 > --- a/stdlib/msort.c > +++ b/stdlib/msort.c > @@ -24,6 +24,7 @@ > #include > #include > #include > +#include > > struct msort_param > { > @@ -164,71 +165,62 @@ void > __qsort_r (void *b, size_t n, size_t s, __compar_d_fn_t cmp, void *arg) > { > size_t size = n * s; > - char *tmp = NULL; > struct msort_param p; > + struct scratch_buffer buf; > + scratch_buffer_init (&buf); > > /* For large object sizes use indirect sorting. */ > if (s > 32) > size = 2 * n * sizeof (void *) + s; > > - if (size < 1024) > - /* The temporary array is small, so put it on the stack. */ > - p.t = __alloca (size); > - else > - { > - /* We should avoid allocating too much memory since this might > - have to be backed up by swap space. */ > - static long int phys_pages; > - static int pagesize; > + /* We should avoid allocating too much memory since this might > + have to be backed up by swap space. */ > + static long int phys_pages; > + static int pagesize; > > - if (pagesize == 0) > - { > - phys_pages = __sysconf (_SC_PHYS_PAGES); > + if (pagesize == 0) > + { > + phys_pages = __sysconf (_SC_PHYS_PAGES); > > - if (phys_pages == -1) > - /* Error while determining the memory size. So let's > - assume there is enough memory. Otherwise the > - implementer should provide a complete implementation of > - the `sysconf' function. */ > - phys_pages = (long int) (~0ul >> 1); > + if (phys_pages == -1) > + /* Error while determining the memory size. So let's > + assume there is enough memory. Otherwise the > + implementer should provide a complete implementation of > + the `sysconf' function. */ > + phys_pages = (long int) (~0ul >> 1); > > - /* The following determines that we will never use more than > - a quarter of the physical memory. */ > - phys_pages /= 4; > + /* The following determines that we will never use more than > + a quarter of the physical memory. */ > + phys_pages /= 4; > > - /* Make sure phys_pages is written to memory. */ > - atomic_write_barrier (); > + /* Make sure phys_pages is written to memory. */ > + atomic_write_barrier (); > > - pagesize = __sysconf (_SC_PAGESIZE); > - } > + pagesize = __sysconf (_SC_PAGESIZE); > + } > > - /* Just a comment here. We cannot compute > - phys_pages * pagesize > - and compare the needed amount of memory against this value. > - The problem is that some systems might have more physical > - memory then can be represented with a `size_t' value (when > - measured in bytes. */ > + /* Just a comment here. We cannot compute > + phys_pages * pagesize > + and compare the needed amount of memory against this value. > + The problem is that some systems might have more physical > + memory then can be represented with a `size_t' value (when > + measured in bytes. */ > > - /* If the memory requirements are too high don't allocate memory. */ > - if (size / pagesize > (size_t) phys_pages) > - { > - _quicksort (b, n, s, cmp, arg); > - return; > - } > + /* If the memory requirements are too high don't allocate memory. */ > + if (size / pagesize > (size_t) phys_pages) > + { > + _quicksort (b, n, s, cmp, arg); > + return; > + } > > - /* It's somewhat large, so malloc it. */ > - int save = errno; > - tmp = malloc (size); > - __set_errno (save); > - if (tmp == NULL) > - { > - /* Couldn't get space, so use the slower algorithm > - that doesn't need a temporary array. */ > - _quicksort (b, n, s, cmp, arg); > - return; > - } > - p.t = tmp; > + if (!scratch_buffer_set_array_size (&buf, 1, size)) > + { > + /* Couldn't get space, so use the slower algorithm > + that doesn't need a temporary array. */ > + _quicksort (b, n, s, cmp, arg); > + return; > } > + p.t = buf.data; > > p.s = s; > p.var = 4; > @@ -295,7 +287,7 @@ __qsort_r (void *b, size_t n, size_t s, __compar_d_fn_t cmp, void *arg) > } > msort_with_tmp (&p, b, n); > } > - free (tmp); > + scratch_buffer_free (&buf); > } > libc_hidden_def (__qsort_r) > weak_alias (__qsort_r, qsort_r) > -- > 2.39.2 >