git: 0a12bf23325d - stable/13 - rtld: switch from malloc_aligned() to __crt_aligned_alloc()

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Mon, 28 Aug 2023 00:28:25 UTC
The branch stable/13 has been updated by kib:

URL: https://cgit.FreeBSD.org/src/commit/?id=0a12bf23325da9cb479ef844869200ff1b0255cf

commit 0a12bf23325da9cb479ef844869200ff1b0255cf
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2023-07-30 01:51:52 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2023-08-28 00:27:48 +0000

    rtld: switch from malloc_aligned() to __crt_aligned_alloc()
    
    (cherry picked from commit feaae6ba1ace0091384ac371423976cd15e59e5a)
---
 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 e8b901bda634..888586cf876d 100644
--- a/libexec/rtld-elf/rtld.c
+++ b/libexec/rtld-elf/rtld.c
@@ -5300,13 +5300,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];
@@ -5370,7 +5370,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 */
@@ -5396,7 +5396,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));
@@ -5475,11 +5475,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);
 }
 
@@ -5516,7 +5516,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 522638a5b1d9..e2fb3de470c3 100644
--- a/libexec/rtld-elf/rtld.h
+++ b/libexec/rtld-elf/rtld.h
@@ -367,8 +367,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);
-}