From nobody Mon Aug 21 14:17:18 2023 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4RTvgg1JF6z4qqqR; Mon, 21 Aug 2023 14:17:19 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4RTvgg0hNdz3c5T; Mon, 21 Aug 2023 14:17:19 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1692627439; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=d0Fa3yP7Oo/HI7RvhTdix7rRQIBEWiGYbkfZRdGtp8A=; b=c/fxe9dcqA6pEEb3hc3gesZBd6tiL7lSg7WVGH1hJ1E23zev7lEM9MZVOWny7CwEPnUTun D5UrDonrQl2jddq3DBpqmjjTfsHnGcsIQK+hXvzDc62rfAVaShKO0wvvwlnZKEyCP7oezy e4D5Fx1nReih+km2Y8KH5WJR6q1PFyOvmS+dzi5+ppEV+O/rvxwJPwX8neynKI6os7M5Yi wsXyzysE4bmxgIq7uz6nW2MY7APoYx0cSixMg7K/dyIItMliZH8VSIRukZHD7rhe2UScrp /CvJnYpAvav1FlPPoOIUrxH/GYV3VndLpMqU6Le+olo7TdNcHDocHd8T4F5RKA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1692627439; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=d0Fa3yP7Oo/HI7RvhTdix7rRQIBEWiGYbkfZRdGtp8A=; b=MfZyv+vSTT2/gQBbq82f1bwt1n5fB/WK8tOb5hQryIsEK1FvP7lz4xnmPj9TATkm3ReK+H cqFzOoGOWTlXD6NaKXLLcUGyKt8sEtg+Fe3qMxeSlMKGyMXRYtnA/Ty8urWIeELyyV5iKN nRQUiBjPk2nopB/rV4j6gBrjXH682d7o3tOumb8MBTWU8KuyJL7bns2iCmPbDu8SoU4JQJ MVy8alVp2vEQYMC51lryXVss88UG4tk13phKAViNFHE5kVUE57DMEglIkoS3dDNhotwour 7aqP5M0OCnCH+n9xNbGUQb4vc9NAKFM74Mh5Nnneba0ClYSHKQqDO595BGiBdg== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1692627439; a=rsa-sha256; cv=none; b=ftHa+8TSem998Di2PTD/E4scaiRGiBZr4ZWX7vOnub/5HXINtqtjOYxDJVHXQvNI0vP7rE aJEWQRHQLOJjGcv1QtyiZgI49LnaxDR4FCjujXX+1aBOmk53ELtH5knZvicHJucCwavK72 +CWVIk6rKRw0QeMRMz2Vmn9FGFNGMygrEdbJVjtlYwO7zjLV4bz6Tltxufl9ARhRBtnil1 dtIKSWa6o+H9Guc10BZReI5MIO1IqpYhEL8t4lUZkD9rixd2fqIUiHUlmkmiTDIT/UzMI3 EF4pWFvjQTERf4lnpVRQ1aI8C8VppgW+IbO+8tfrlDID1sxrdPqMAUK27tjI5w== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4RTvgf6ts7z8cq; Mon, 21 Aug 2023 14:17:18 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 37LEHIuR097488; Mon, 21 Aug 2023 14:17:18 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 37LEHIOC097484; Mon, 21 Aug 2023 14:17:18 GMT (envelope-from git) Date: Mon, 21 Aug 2023 14:17:18 GMT Message-Id: <202308211417.37LEHIOC097484@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Konstantin Belousov Subject: git: feaae6ba1ace - main - rtld: switch from malloc_aligned() to __crt_aligned_alloc() List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kib X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: feaae6ba1ace0091384ac371423976cd15e59e5a Auto-Submitted: auto-generated The branch main has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=feaae6ba1ace0091384ac371423976cd15e59e5a commit feaae6ba1ace0091384ac371423976cd15e59e5a Author: Konstantin Belousov AuthorDate: 2023-07-30 01:51:52 +0000 Commit: Konstantin Belousov CommitDate: 2023-08-21 14:16:42 +0000 rtld: switch from malloc_aligned() to __crt_aligned_alloc() Use regular free(), since it works now. Reviewed by: markj Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D41150 --- libexec/rtld-elf/rtld.c | 14 +++++++------- libexec/rtld-elf/rtld.h | 3 +-- libexec/rtld-elf/xmalloc.c | 30 +++++++----------------------- 3 files changed, 15 insertions(+), 32 deletions(-) diff --git a/libexec/rtld-elf/rtld.c b/libexec/rtld-elf/rtld.c index feef5dd0bb5d..aa7e6020a085 100644 --- a/libexec/rtld-elf/rtld.c +++ b/libexec/rtld-elf/rtld.c @@ -5254,13 +5254,13 @@ allocate_tls(Obj_Entry *objs, void *oldtcb, size_t tcbsize, size_t tcbalign) tls_block_size += pre_size + tls_static_space - TLS_TCB_SIZE - post_size; /* Allocate whole TLS block */ - tls_block = malloc_aligned(tls_block_size, maxalign, 0); + tls_block = xmalloc_aligned(tls_block_size, maxalign, 0); tcb = (Elf_Addr **)(tls_block + pre_size + extra_size); if (oldtcb != NULL) { memcpy(tls_block, get_tls_block_ptr(oldtcb, tcbsize), tls_static_space); - free_aligned(get_tls_block_ptr(oldtcb, tcbsize)); + free(get_tls_block_ptr(oldtcb, tcbsize)); /* Adjust the DTV. */ dtv = tcb[0]; @@ -5324,7 +5324,7 @@ free_tls(void *tcb, size_t tcbsize, size_t tcbalign __unused) } } free(dtv); - free_aligned(get_tls_block_ptr(tcb, tcbsize)); + free(get_tls_block_ptr(tcb, tcbsize)); } #endif /* TLS_VARIANT_I */ @@ -5350,7 +5350,7 @@ allocate_tls(Obj_Entry *objs, void *oldtls, size_t tcbsize, size_t tcbalign) size = roundup(tls_static_space, ralign) + roundup(tcbsize, ralign); assert(tcbsize >= 2*sizeof(Elf_Addr)); - tls = malloc_aligned(size, ralign, 0 /* XXX */); + tls = xmalloc_aligned(size, ralign, 0 /* XXX */); dtv = xcalloc(tls_max_index + 2, sizeof(Elf_Addr)); segbase = (Elf_Addr)(tls + roundup(tls_static_space, ralign)); @@ -5429,11 +5429,11 @@ free_tls(void *tls, size_t tcbsize __unused, size_t tcbalign) for (i = 0; i < dtvsize; i++) { if (dtv[i + 2] != 0 && (dtv[i + 2] < tlsstart || dtv[i + 2] > tlsend)) { - free_aligned((void *)dtv[i + 2]); + free((void *)dtv[i + 2]); } } - free_aligned((void *)tlsstart); + free((void *)tlsstart); free((void *)dtv); } @@ -5470,7 +5470,7 @@ allocate_module_tls(int index) obj->tls_dynamic = true; - p = malloc_aligned(obj->tlssize, obj->tlsalign, obj->tlspoffset); + p = xmalloc_aligned(obj->tlssize, obj->tlsalign, obj->tlspoffset); memcpy(p, obj->tlsinit, obj->tlsinitsize); memset(p + obj->tlsinitsize, 0, obj->tlssize - obj->tlsinitsize); return (p); diff --git a/libexec/rtld-elf/rtld.h b/libexec/rtld-elf/rtld.h index 45cb722097b2..7fe9d837d6f1 100644 --- a/libexec/rtld-elf/rtld.h +++ b/libexec/rtld-elf/rtld.h @@ -361,8 +361,7 @@ Obj_Entry *map_object(int, const char *, const struct stat *); void *xcalloc(size_t, size_t); void *xmalloc(size_t); char *xstrdup(const char *); -void *malloc_aligned(size_t size, size_t align, size_t offset); -void free_aligned(void *ptr); +void *xmalloc_aligned(size_t size, size_t align, size_t offset); extern Elf_Addr _GLOBAL_OFFSET_TABLE_[]; extern Elf_Sym sym_zero; /* For resolving undefined weak refs. */ extern bool ld_bind_not; diff --git a/libexec/rtld-elf/xmalloc.c b/libexec/rtld-elf/xmalloc.c index 80246f36dd26..5f7c1b5ba10a 100644 --- a/libexec/rtld-elf/xmalloc.c +++ b/libexec/rtld-elf/xmalloc.c @@ -75,34 +75,18 @@ xstrdup(const char *str) } void * -malloc_aligned(size_t size, size_t align, size_t offset) +xmalloc_aligned(size_t size, size_t align, size_t offset) { - char *mem, *res; - uintptr_t x; + void *res; offset &= align - 1; if (align < sizeof(void *)) align = sizeof(void *); - mem = xmalloc(size + 3 * align + offset); - x = roundup((uintptr_t)mem + sizeof(void *), align); - x += offset; - res = (void *)x; - x -= sizeof(void *); - memcpy((void *)x, &mem, sizeof(mem)); + res = __crt_aligned_alloc_offset(align, size, offset); + if (res == NULL) { + rtld_fdputstr(STDERR_FILENO, "Out of memory\n"); + _exit(1); + } return (res); } - -void -free_aligned(void *ptr) -{ - void *mem; - uintptr_t x; - - if (ptr == NULL) - return; - x = (uintptr_t)ptr; - x -= sizeof(void *); - memcpy(&mem, (void *)x, sizeof(mem)); - free(mem); -}