From patchwork Wed Nov 3 15:35:39 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 46999 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6A8C13858005 for ; Wed, 3 Nov 2021 15:36:07 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6A8C13858005 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1635953767; bh=cQso0v+XlbleVq+a2ADb+XcjfQBzyRkN0kZTNnT0pLw=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=n0Mneih3wJI5VyG52Cvws8y9J0Kxw5VsHpQvaOIA+v6yk3G7NvEJWge07FWsJI+41 Xw5HrlNMnxLpOlMepaYiTDPBRAlexE5CwaW90c1Z1oB44dG0cqiCTF/YqCWrTNc/J2 jqcUeNMm5o+fU9H41lvjMNK/lX9xQvbpBQYNjKu4= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qt1-x829.google.com (mail-qt1-x829.google.com [IPv6:2607:f8b0:4864:20::829]) by sourceware.org (Postfix) with ESMTPS id AC6E53858D28 for ; Wed, 3 Nov 2021 15:35:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org AC6E53858D28 Received: by mail-qt1-x829.google.com with SMTP id h14so1235841qtb.3 for ; Wed, 03 Nov 2021 08:35:44 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=cQso0v+XlbleVq+a2ADb+XcjfQBzyRkN0kZTNnT0pLw=; b=v2xwK3OpbaSc+FDSSR8CRRohjQ67kgZijzOegSsyB0HlZc/J3vayLoREdTVCx4OLul PP5R1hGcSdAZ624QxHZXhp8U+xcsgp9HaxVY/O+2mIwbMPH8QK3ZOUdeAqCE2fBAi90r j5EwlCnqKQ8rohSWeTliNie9ZAjTPwxX6PmT0ePr25McUCH4Nor5b3aq1+cBByrfA7ip TkX/DJ2SrBqpt8tYcvpR16sb/pg3fPsxSJAy3qPW1y8ta7ix3W5LMT2aEf9/5nfa8+RC Tbc3mYGHX+tdHI3QfnoAaoFce/2OZP/029Ge/tMswuFCPHA0+rD4463ZrdgIkGRZRq4P Q1Xg== X-Gm-Message-State: AOAM532h++9a2B/emubpmaYtnLw37OMQuuBrhyJhcIJyP76fSgBDEA8X FQshzPOcq+lsHVy+VXUCIe4Ni+10a3m2mQ== X-Google-Smtp-Source: ABdhPJzL/1Y3QqbExbgcoz7rLTb++oYDYrKeOcw3FEFug/PK1vyUIcUtETsYoRJ10P99bd9Qoa54dA== X-Received: by 2002:a05:622a:13:: with SMTP id x19mr46019639qtw.83.1635953743518; Wed, 03 Nov 2021 08:35:43 -0700 (PDT) Received: from birita.. ([2804:431:c7cb:b64f:c336:789a:89cc:169]) by smtp.gmail.com with ESMTPSA id 13sm1641782qkc.40.2021.11.03.08.35.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Nov 2021 08:35:43 -0700 (PDT) To: libc-alpha@sourceware.org, Siddhesh Poyarekar , DJ Delorie Subject: [PATCH] elf: Use the minimal malloc on tunables_strdup Date: Wed, 3 Nov 2021 12:35:39 -0300 Message-Id: <20211103153539.1077895-1-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.32.0 MIME-Version: 1.0 X-Spam-Status: No, score=-13.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=unavailable autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" The rtld_malloc functions are moved to its own file so it can be used on csu code. Also, the functiosn are renamed to __minimal_* (since there are now used not only on loader code). Using the __minimal_malloc on tunables_strdup() avoids potential issues with sbrk() calls while processing the tunables (I see sporadic elf/tst-dso-ordering9 on powerpc64le with different tests failing due ASLR). Also, using __minimal_malloc over plain mmap optimizes the memory allocation on both static and dynamic case (since it will any unused space in either the last page of data segments, avoiding mmap() call, or from the previous mmap() call). Checked on x86_64-linux-gnu, i686-linux-gnu, and powerpc64le-linux-gnu. --- elf/Makefile | 7 +- elf/dl-minimal-malloc.c | 112 +++++++++++++++++++++++++ elf/dl-minimal.c | 122 ++-------------------------- elf/dl-tunables.c | 5 +- sysdeps/generic/dl-minimal-malloc.h | 28 +++++++ 5 files changed, 156 insertions(+), 118 deletions(-) create mode 100644 elf/dl-minimal-malloc.c create mode 100644 sysdeps/generic/dl-minimal-malloc.h diff --git a/elf/Makefile b/elf/Makefile index 7e4f0c3121..7245309516 100644 --- a/elf/Makefile +++ b/elf/Makefile @@ -36,7 +36,7 @@ dl-routines = $(addprefix dl-,load lookup object reloc deps \ exception sort-maps lookup-direct \ call-libc-early-init write \ thread_gscope_wait tls_init_tp \ - debug-symbols) + debug-symbols minimal-malloc) ifeq (yes,$(use-ldconfig)) dl-routines += dl-cache endif @@ -75,6 +75,11 @@ CFLAGS-dl-runtime.c += -fexceptions -fasynchronous-unwind-tables CFLAGS-dl-lookup.c += -fexceptions -fasynchronous-unwind-tables CFLAGS-dl-iteratephdr.c += $(uses-callbacks) +# Called during static library initialization, so turn stack-protection +# off for non-shared builds. +CFLAGS-dl-minimal-malloc.o = $(no-stack-protector) +CFLAGS-dl-minimal-malloc.op = $(no-stack-protector) + # On targets without __builtin_memset, rtld.c uses a hand-coded loop # in _dl_start. Make sure this isn't turned into a call to regular memset. ifeq (yes,$(have-loop-to-function)) diff --git a/elf/dl-minimal-malloc.c b/elf/dl-minimal-malloc.c new file mode 100644 index 0000000000..939b5271ca --- /dev/null +++ b/elf/dl-minimal-malloc.c @@ -0,0 +1,112 @@ +/* Minimal malloc implementation for dynamic linker and static + initialization. + Copyright (C) 1995-2021 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include +#include + +static void *alloc_ptr, *alloc_end, *alloc_last_block; + +/* Allocate an aligned memory block. */ +void * +__minimal_malloc (size_t n) +{ + if (alloc_end == 0) + { + /* Consume any unused space in the last page of our data segment. */ + extern int _end attribute_hidden; + alloc_ptr = &_end; + alloc_end = (void *) 0 + (((alloc_ptr - (void *) 0) + + GLRO(dl_pagesize) - 1) + & ~(GLRO(dl_pagesize) - 1)); + } + + /* Make sure the allocation pointer is ideally aligned. */ + alloc_ptr = (void *) 0 + (((alloc_ptr - (void *) 0) + MALLOC_ALIGNMENT - 1) + & ~(MALLOC_ALIGNMENT - 1)); + + if (alloc_ptr + n >= alloc_end || n >= -(uintptr_t) alloc_ptr) + { + /* Insufficient space left; allocate another page plus one extra + page to reduce number of mmap calls. */ + caddr_t page; + size_t nup = (n + GLRO(dl_pagesize) - 1) & ~(GLRO(dl_pagesize) - 1); + if (__glibc_unlikely (nup == 0 && n != 0)) + return NULL; + nup += GLRO(dl_pagesize); + page = __mmap (0, nup, PROT_READ|PROT_WRITE, + MAP_ANON|MAP_PRIVATE, -1, 0); + if (page == MAP_FAILED) + return NULL; + if (page != alloc_end) + alloc_ptr = page; + alloc_end = page + nup; + } + + alloc_last_block = (void *) alloc_ptr; + alloc_ptr += n; + return alloc_last_block; +} + +/* We use this function occasionally since the real implementation may + be optimized when it can assume the memory it returns already is + set to NUL. */ +void * +__minimal_calloc (size_t nmemb, size_t size) +{ + /* New memory from the trivial malloc above is always already cleared. + (We make sure that's true in the rare occasion it might not be, + by clearing memory in free, below.) */ + size_t bytes = nmemb * size; + +#define HALF_SIZE_T (((size_t) 1) << (8 * sizeof (size_t) / 2)) + if (__builtin_expect ((nmemb | size) >= HALF_SIZE_T, 0) + && size != 0 && bytes / size != nmemb) + return NULL; + + return malloc (bytes); +} + +/* This will rarely be called. */ +void +__minimal_free (void *ptr) +{ + /* We can free only the last block allocated. */ + if (ptr == alloc_last_block) + { + /* Since this is rare, we clear the freed block here + so that calloc can presume malloc returns cleared memory. */ + memset (alloc_last_block, '\0', alloc_ptr - alloc_last_block); + alloc_ptr = alloc_last_block; + } +} + +/* This is only called with the most recent block returned by malloc. */ +void * +__minimal_realloc (void *ptr, size_t n) +{ + if (ptr == NULL) + return malloc (n); + assert (ptr == alloc_last_block); + size_t old_size = alloc_ptr - alloc_last_block; + alloc_ptr = alloc_last_block; + void *new = malloc (n); + return new != ptr ? memcpy (new, ptr, old_size) : new; +} diff --git a/elf/dl-minimal.c b/elf/dl-minimal.c index 4e7f11aeab..152192d451 100644 --- a/elf/dl-minimal.c +++ b/elf/dl-minimal.c @@ -16,23 +16,14 @@ License along with the GNU C Library; if not, see . */ -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include #include #include #include #include #include <_itoa.h> -#include +#include -#include /* The rtld startup code calls __rtld_malloc_init_stubs after the first self-relocation to adjust the pointers to the minimal @@ -44,19 +35,13 @@ __typeof (free) *__rtld_free attribute_relro; __typeof (malloc) *__rtld_malloc attribute_relro; __typeof (realloc) *__rtld_realloc attribute_relro; -/* Defined below. */ -static __typeof (calloc) rtld_calloc; -static __typeof (free) rtld_free; -static __typeof (malloc) rtld_malloc; -static __typeof (realloc) rtld_realloc; - void __rtld_malloc_init_stubs (void) { - __rtld_calloc = &rtld_calloc; - __rtld_free = &rtld_free; - __rtld_malloc = &rtld_malloc; - __rtld_realloc = &rtld_realloc; + __rtld_calloc = &__minimal_calloc; + __rtld_free = &__minimal_free; + __rtld_malloc = &__minimal_malloc; + __rtld_realloc = &__minimal_realloc; } bool @@ -64,7 +49,7 @@ __rtld_malloc_is_complete (void) { /* The caller assumes that there is an active malloc. */ assert (__rtld_malloc != NULL); - return __rtld_malloc != &rtld_malloc; + return __rtld_malloc != &__minimal_malloc; } /* Lookup NAME at VERSION in the scope of MATCH. */ @@ -115,99 +100,6 @@ __rtld_malloc_init_real (struct link_map *main_map) __rtld_realloc = new_realloc; } -/* Minimal malloc allocator for used during initial link. After the - initial link, a full malloc implementation is interposed, either - the one in libc, or a different one supplied by the user through - interposition. */ - -static void *alloc_ptr, *alloc_end, *alloc_last_block; - -/* Allocate an aligned memory block. */ -static void * -rtld_malloc (size_t n) -{ - if (alloc_end == 0) - { - /* Consume any unused space in the last page of our data segment. */ - extern int _end attribute_hidden; - alloc_ptr = &_end; - alloc_end = (void *) 0 + (((alloc_ptr - (void *) 0) - + GLRO(dl_pagesize) - 1) - & ~(GLRO(dl_pagesize) - 1)); - } - - /* Make sure the allocation pointer is ideally aligned. */ - alloc_ptr = (void *) 0 + (((alloc_ptr - (void *) 0) + MALLOC_ALIGNMENT - 1) - & ~(MALLOC_ALIGNMENT - 1)); - - if (alloc_ptr + n >= alloc_end || n >= -(uintptr_t) alloc_ptr) - { - /* Insufficient space left; allocate another page plus one extra - page to reduce number of mmap calls. */ - caddr_t page; - size_t nup = (n + GLRO(dl_pagesize) - 1) & ~(GLRO(dl_pagesize) - 1); - if (__glibc_unlikely (nup == 0 && n != 0)) - return NULL; - nup += GLRO(dl_pagesize); - page = __mmap (0, nup, PROT_READ|PROT_WRITE, - MAP_ANON|MAP_PRIVATE, -1, 0); - if (page == MAP_FAILED) - return NULL; - if (page != alloc_end) - alloc_ptr = page; - alloc_end = page + nup; - } - - alloc_last_block = (void *) alloc_ptr; - alloc_ptr += n; - return alloc_last_block; -} - -/* We use this function occasionally since the real implementation may - be optimized when it can assume the memory it returns already is - set to NUL. */ -static void * -rtld_calloc (size_t nmemb, size_t size) -{ - /* New memory from the trivial malloc above is always already cleared. - (We make sure that's true in the rare occasion it might not be, - by clearing memory in free, below.) */ - size_t bytes = nmemb * size; - -#define HALF_SIZE_T (((size_t) 1) << (8 * sizeof (size_t) / 2)) - if (__builtin_expect ((nmemb | size) >= HALF_SIZE_T, 0) - && size != 0 && bytes / size != nmemb) - return NULL; - - return malloc (bytes); -} - -/* This will rarely be called. */ -void -rtld_free (void *ptr) -{ - /* We can free only the last block allocated. */ - if (ptr == alloc_last_block) - { - /* Since this is rare, we clear the freed block here - so that calloc can presume malloc returns cleared memory. */ - memset (alloc_last_block, '\0', alloc_ptr - alloc_last_block); - alloc_ptr = alloc_last_block; - } -} - -/* This is only called with the most recent block returned by malloc. */ -void * -rtld_realloc (void *ptr, size_t n) -{ - if (ptr == NULL) - return malloc (n); - assert (ptr == alloc_last_block); - size_t old_size = alloc_ptr - alloc_last_block; - alloc_ptr = alloc_last_block; - void *new = malloc (n); - return new != ptr ? memcpy (new, ptr, old_size) : new; -} /* Avoid signal frobnication in setjmp/longjmp. Keeps things smaller. */ diff --git a/elf/dl-tunables.c b/elf/dl-tunables.c index 1666736bc1..ee5b315e6a 100644 --- a/elf/dl-tunables.c +++ b/elf/dl-tunables.c @@ -31,6 +31,7 @@ #include #include #include +#include #define TUNABLES_INTERNAL 1 #include "dl-tunables.h" @@ -48,12 +49,12 @@ tunables_strdup (const char *in) size_t i = 0; while (in[i++] != '\0'); - char *out = __sbrk (i); + char *out = __minimal_malloc (i + 1); /* For most of the tunables code, we ignore user errors. However, this is a system error - and running out of memory at program startup should be reported, so we do. */ - if (out == (void *)-1) + if (out == NULL) _dl_fatal_printf ("sbrk() failure while processing tunables\n"); while (i-- > 0) diff --git a/sysdeps/generic/dl-minimal-malloc.h b/sysdeps/generic/dl-minimal-malloc.h new file mode 100644 index 0000000000..cba5ed3225 --- /dev/null +++ b/sysdeps/generic/dl-minimal-malloc.h @@ -0,0 +1,28 @@ +/* Minimal malloc implementation for dynamic linker and static + initialization. + Copyright (C) 1995-2021 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#ifndef _DL_MINIMAL_MALLOC_H +#define _DL_MINIMAL_MALLOC_H + +void *__minimal_malloc (size_t n) attribute_hidden; +void *__minimal_calloc (size_t nmemb, size_t size) attribute_hidden; +void __minimal_free (void *ptr) attribute_hidden; +void *__minimal_realloc (void *ptr, size_t n) attribute_hidden; + +#endif