From owner-svn-src-head@freebsd.org Sat Apr 4 22:37:59 2020 Return-Path: Delivered-To: svn-src-head@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 66CAA2A5387; Sat, 4 Apr 2020 22:37:59 +0000 (UTC) (envelope-from kib@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) server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 48vsBt1YJNz4dCM; Sat, 4 Apr 2020 22:37:57 +0000 (UTC) (envelope-from kib@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4FB0711CB; Sat, 4 Apr 2020 22:37:51 +0000 (UTC) (envelope-from kib@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id 034MboOa022793; Sat, 4 Apr 2020 22:37:50 GMT (envelope-from kib@FreeBSD.org) Received: (from kib@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id 034MboGg022789; Sat, 4 Apr 2020 22:37:50 GMT (envelope-from kib@FreeBSD.org) Message-Id: <202004042237.034MboGg022789@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: kib set sender to kib@FreeBSD.org using -f From: Konstantin Belousov Date: Sat, 4 Apr 2020 22:37:50 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r359634 - head/libexec/rtld-elf X-SVN-Group: head X-SVN-Commit-Author: kib X-SVN-Commit-Paths: head/libexec/rtld-elf X-SVN-Commit-Revision: 359634 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 04 Apr 2020 22:37:59 -0000 Author: kib Date: Sat Apr 4 22:37:50 2020 New Revision: 359634 URL: https://svnweb.freebsd.org/changeset/base/359634 Log: Make p_vaddr % p_align == p_offset % p_align for (some) TLS segments. See https://sourceware.org/bugzilla/show_bug.cgi?id=24606 for the test case. See https://reviews.llvm.org/D64930 for the background and more discussion. Also this fixes another bug in malloc_aligned() where total size of the allocated memory might be not enough to fit the aligned requested block after the initial pointer is incremented by the pointer size. Reviewed by: bdragon Tested by: antoine (exp-run PR 244866), bdragon, emaste Sponsored by: The FreeBSD Foundation MFC after: 2 weeks Differential revision: https://reviews.freebsd.org/D21163 Modified: head/libexec/rtld-elf/map_object.c head/libexec/rtld-elf/rtld.c head/libexec/rtld-elf/rtld.h head/libexec/rtld-elf/xmalloc.c Modified: head/libexec/rtld-elf/map_object.c ============================================================================== --- head/libexec/rtld-elf/map_object.c Sat Apr 4 21:38:00 2020 (r359633) +++ head/libexec/rtld-elf/map_object.c Sat Apr 4 22:37:50 2020 (r359634) @@ -313,6 +313,7 @@ map_object(int fd, const char *path, const struct stat obj->tlsindex = ++tls_max_index; obj->tlssize = phtls->p_memsz; obj->tlsalign = phtls->p_align; + obj->tlspoffset = phtls->p_offset; obj->tlsinitsize = phtls->p_filesz; obj->tlsinit = mapbase + phtls->p_vaddr; } Modified: head/libexec/rtld-elf/rtld.c ============================================================================== --- head/libexec/rtld-elf/rtld.c Sat Apr 4 21:38:00 2020 (r359633) +++ head/libexec/rtld-elf/rtld.c Sat Apr 4 22:37:50 2020 (r359634) @@ -1501,6 +1501,7 @@ digest_phdr(const Elf_Phdr *phdr, int phnum, caddr_t e obj->tlsalign = ph->p_align; obj->tlsinitsize = ph->p_filesz; obj->tlsinit = (void*)(ph->p_vaddr + obj->relocbase); + obj->tlspoffset = ph->p_offset; break; case PT_GNU_STACK: @@ -4868,7 +4869,7 @@ allocate_tls(Obj_Entry *objs, void *oldtcb, size_t tcb Elf_Addr addr; Elf_Addr i; size_t extra_size, maxalign, post_size, pre_size, tls_block_size; - size_t tls_init_align; + size_t tls_init_align, tls_init_offset; if (oldtcb != NULL && tcbsize == TLS_TCB_SIZE) return (oldtcb); @@ -4885,7 +4886,7 @@ allocate_tls(Obj_Entry *objs, void *oldtcb, size_t tcb 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); + tls_block = malloc_aligned(tls_block_size, maxalign, 0); tcb = (Elf_Addr **)(tls_block + pre_size + extra_size); if (oldtcb != NULL) { @@ -4909,15 +4910,21 @@ allocate_tls(Obj_Entry *objs, void *oldtcb, size_t tcb for (obj = globallist_curr(objs); obj != NULL; obj = globallist_next(obj)) { - if (obj->tlsoffset > 0) { - addr = (Elf_Addr)tcb + obj->tlsoffset; - if (obj->tlsinitsize > 0) - memcpy((void*) addr, obj->tlsinit, obj->tlsinitsize); - if (obj->tlssize > obj->tlsinitsize) - memset((void*)(addr + obj->tlsinitsize), 0, - obj->tlssize - obj->tlsinitsize); - dtv[obj->tlsindex + 1] = addr; + if (obj->tlsoffset == 0) + continue; + tls_init_offset = obj->tlspoffset & (obj->tlsalign - 1); + addr = (Elf_Addr)tcb + obj->tlsoffset; + if (tls_init_offset > 0) + memset((void *)addr, 0, tls_init_offset); + if (obj->tlsinitsize > 0) { + memcpy((void *)(addr + tls_init_offset), obj->tlsinit, + obj->tlsinitsize); } + if (obj->tlssize > obj->tlsinitsize) { + memset((void *)(addr + tls_init_offset + obj->tlsinitsize), + 0, obj->tlssize - obj->tlsinitsize - tls_init_offset); + } + dtv[obj->tlsindex + 1] = addr; } } @@ -4975,7 +4982,7 @@ allocate_tls(Obj_Entry *objs, void *oldtls, size_t tcb size = round(tls_static_space, ralign) + round(tcbsize, ralign); assert(tcbsize >= 2*sizeof(Elf_Addr)); - tls = malloc_aligned(size, ralign); + tls = malloc_aligned(size, ralign, 0 /* XXX */); dtv = xcalloc(tls_max_index + 2, sizeof(Elf_Addr)); segbase = (Elf_Addr)(tls + round(tls_static_space, ralign)); @@ -5068,25 +5075,24 @@ free_tls(void *tls, size_t tcbsize __unused, size_t t void * allocate_module_tls(int index) { - Obj_Entry* obj; - char* p; + Obj_Entry *obj; + char *p; - TAILQ_FOREACH(obj, &obj_list, next) { - if (obj->marker) - continue; - if (obj->tlsindex == index) - break; - } - if (!obj) { - _rtld_error("Can't find module with TLS index %d", index); - rtld_die(); - } + TAILQ_FOREACH(obj, &obj_list, next) { + if (obj->marker) + continue; + if (obj->tlsindex == index) + break; + } + if (obj == NULL) { + _rtld_error("Can't find module with TLS index %d", index); + rtld_die(); + } - p = malloc_aligned(obj->tlssize, obj->tlsalign); - memcpy(p, obj->tlsinit, obj->tlsinitsize); - memset(p + obj->tlsinitsize, 0, obj->tlssize - obj->tlsinitsize); - - return p; + p = malloc_aligned(obj->tlssize, obj->tlsalign, obj->tlspoffset); + memcpy(p, obj->tlsinit, obj->tlsinitsize); + memset(p + obj->tlsinitsize, 0, obj->tlssize - obj->tlsinitsize); + return (p); } bool Modified: head/libexec/rtld-elf/rtld.h ============================================================================== --- head/libexec/rtld-elf/rtld.h Sat Apr 4 21:38:00 2020 (r359633) +++ head/libexec/rtld-elf/rtld.h Sat Apr 4 22:37:50 2020 (r359634) @@ -163,6 +163,7 @@ typedef struct Struct_Obj_Entry { size_t tlssize; /* Size of TLS block for this module */ size_t tlsoffset; /* Offset of static TLS block for this module */ size_t tlsalign; /* Alignment of static TLS block */ + size_t tlspoffset; /* p_offset of the static TLS block */ caddr_t relro_page; size_t relro_size; @@ -361,7 +362,7 @@ Obj_Entry *map_object(int, const char *, const struct void *xcalloc(size_t, size_t); void *xmalloc(size_t); char *xstrdup(const char *); -void *malloc_aligned(size_t size, size_t align); +void *malloc_aligned(size_t size, size_t align, size_t offset); void free_aligned(void *ptr); extern Elf_Addr _GLOBAL_OFFSET_TABLE_[]; extern Elf_Sym sym_zero; /* For resolving undefined weak refs. */ Modified: head/libexec/rtld-elf/xmalloc.c ============================================================================== --- head/libexec/rtld-elf/xmalloc.c Sat Apr 4 21:38:00 2020 (r359633) +++ head/libexec/rtld-elf/xmalloc.c Sat Apr 4 22:37:50 2020 (r359634) @@ -27,6 +27,7 @@ * $FreeBSD$ */ +#include #include #include #include @@ -76,16 +77,21 @@ xstrdup(const char *str) } void * -malloc_aligned(size_t size, size_t align) +malloc_aligned(size_t size, size_t align, size_t offset) { - void *mem, *res; + char *mem, *res; + uintptr_t x; + offset &= align - 1; if (align < sizeof(void *)) align = sizeof(void *); - mem = xmalloc(size + sizeof(void *) + align - 1); - res = (void *)round((uintptr_t)mem + sizeof(void *), align); - *(void **)((uintptr_t)res - sizeof(void *)) = mem; + 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)); return (res); } @@ -99,6 +105,6 @@ free_aligned(void *ptr) return; x = (uintptr_t)ptr; x -= sizeof(void *); - mem = *(void **)x; + memcpy(&mem, (void *)x, sizeof(mem)); free(mem); }