Date: Wed, 9 May 2012 15:57:59 +0000 (UTC) From: Konstantin Belousov <kib@FreeBSD.org> To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-8@freebsd.org Subject: svn commit: r235198 - in stable/8: lib/libc/gen lib/libc/include lib/libc/stdlib lib/libthr lib/libthr/thread libexec/rtld-elf Message-ID: <201205091557.q49FvxLL091842@svn.freebsd.org>
next in thread | raw e-mail | index | archive | help
Author: kib Date: Wed May 9 15:57:59 2012 New Revision: 235198 URL: http://svn.freebsd.org/changeset/base/235198 Log: MFC r211706: On shared object unload, in __cxa_finalize, call and clear all installed atexit and __cxa_atexit handlers that are either installed by unloaded dso, or points to the functions provided by the dso. Use _rtld_addr_phdr to locate segment information from the address of private variable belonging to the dso, supplied by crtstuff.c. Provide utility function __elf_phdr_match_addr to do the match of address against dso executable segment. Call back into libthr from __cxa_finalize using weak __pthread_cxa_finalize symbol to remove any atfork handler which function points into unloaded object. The rtld needs private __pthread_cxa_finalize symbol to not require resolution of the weak undefined symbol at initialization time. This cannot work, since rtld is relocated before sym_zero is set up. MFC r211894: Do not call __pthread_cxa_finalize with invalid struct dl_phdr_info. Requested and tested by: Peter Jeremy <peter rulingia com> Added: stable/8/lib/libc/gen/elf_utils.c - copied unchanged from r211706, head/lib/libc/gen/elf_utils.c Modified: stable/8/lib/libc/gen/Makefile.inc stable/8/lib/libc/gen/Symbol.map stable/8/lib/libc/include/libc_private.h stable/8/lib/libc/stdlib/atexit.c stable/8/lib/libthr/pthread.map stable/8/lib/libthr/thread/thr_fork.c stable/8/lib/libthr/thread/thr_private.h stable/8/libexec/rtld-elf/rtld.c Directory Properties: stable/8/lib/libc/ (props changed) stable/8/lib/libthr/ (props changed) stable/8/libexec/rtld-elf/ (props changed) Modified: stable/8/lib/libc/gen/Makefile.inc ============================================================================== --- stable/8/lib/libc/gen/Makefile.inc Wed May 9 15:36:37 2012 (r235197) +++ stable/8/lib/libc/gen/Makefile.inc Wed May 9 15:57:59 2012 (r235198) @@ -10,7 +10,7 @@ SRCS+= __getosreldate.c __xuname.c \ alarm.c arc4random.c assert.c basename.c check_utility_compat.c \ clock.c closedir.c confstr.c \ crypt.c ctermid.c daemon.c devname.c dirname.c disklabel.c \ - dlfcn.c drand48.c erand48.c err.c errlst.c errno.c \ + dlfcn.c drand48.c elf_utils.c erand48.c err.c errlst.c errno.c \ exec.c fdevname.c feature_present.c fmtcheck.c fmtmsg.c fnmatch.c \ fpclassify.c frexp.c fstab.c ftok.c fts.c fts-compat.c ftw.c \ getbootfile.c getbsize.c \ Modified: stable/8/lib/libc/gen/Symbol.map ============================================================================== --- stable/8/lib/libc/gen/Symbol.map Wed May 9 15:36:37 2012 (r235197) +++ stable/8/lib/libc/gen/Symbol.map Wed May 9 15:57:59 2012 (r235198) @@ -445,6 +445,7 @@ FBSDprivate_1.0 { _rtld_atfork_post; _rtld_error; /* for private use */ _rtld_thread_init; /* for private use */ + __elf_phdr_match_addr; _err; _warn; __fmtcheck; Copied: stable/8/lib/libc/gen/elf_utils.c (from r211706, head/lib/libc/gen/elf_utils.c) ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ stable/8/lib/libc/gen/elf_utils.c Wed May 9 15:57:59 2012 (r235198, copy of r211706, head/lib/libc/gen/elf_utils.c) @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2010 Konstantin Belousov <kib@freebsd.org> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Neither the name of the author nor the names of any co-contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#include <link.h> + +int +__elf_phdr_match_addr(struct dl_phdr_info *phdr_info, void *addr) +{ + const Elf_Phdr *ph; + int i; + + for (i = 0; i < phdr_info->dlpi_phnum; i++) { + ph = &phdr_info->dlpi_phdr[i]; + if (ph->p_type != PT_LOAD || (ph->p_flags & PF_X) == 0) + continue; + if (phdr_info->dlpi_addr + ph->p_vaddr <= (uintptr_t)addr && + (uintptr_t)addr + sizeof(addr) < phdr_info->dlpi_addr + + ph->p_vaddr + ph->p_memsz) + break; + } + return (i != phdr_info->dlpi_phnum); +} Modified: stable/8/lib/libc/include/libc_private.h ============================================================================== --- stable/8/lib/libc/include/libc_private.h Wed May 9 15:36:37 2012 (r235197) +++ stable/8/lib/libc/include/libc_private.h Wed May 9 15:57:59 2012 (r235198) @@ -235,4 +235,7 @@ extern int __sys_fcntl(int, int, ...); /* execve() with PATH processing to implement posix_spawnp() */ int _execvpe(const char *, char * const *, char * const *); +struct dl_phdr_info; +int __elf_phdr_match_addr(struct dl_phdr_info *, void *); + #endif /* _LIBC_PRIVATE_H_ */ Modified: stable/8/lib/libc/stdlib/atexit.c ============================================================================== --- stable/8/lib/libc/stdlib/atexit.c Wed May 9 15:36:37 2012 (r235197) +++ stable/8/lib/libc/stdlib/atexit.c Wed May 9 15:57:59 2012 (r235198) @@ -37,6 +37,7 @@ static char sccsid[] = "@(#)atexit.c 8.2 __FBSDID("$FreeBSD$"); #include "namespace.h" +#include <link.h> #include <stddef.h> #include <stdlib.h> #include <unistd.h> @@ -146,6 +147,9 @@ __cxa_atexit(void (*func)(void *), void return (error); } +#pragma weak __pthread_cxa_finalize +void __pthread_cxa_finalize(const struct dl_phdr_info *); + /* * Call all handlers registered with __cxa_atexit for the shared * object owning 'dso'. Note: if 'dso' is NULL, then all remaining @@ -154,18 +158,28 @@ __cxa_atexit(void (*func)(void *), void void __cxa_finalize(void *dso) { + struct dl_phdr_info phdr_info; struct atexit *p; struct atexit_fn fn; - int n; + int n, has_phdr; + + if (dso != NULL) + has_phdr = _rtld_addr_phdr(dso, &phdr_info); + else + has_phdr = 0; _MUTEX_LOCK(&atexit_mutex); for (p = __atexit; p; p = p->next) { for (n = p->ind; --n >= 0;) { if (p->fns[n].fn_type == ATEXIT_FN_EMPTY) continue; /* already been called */ - if (dso != NULL && dso != p->fns[n].fn_dso) - continue; /* wrong DSO */ fn = p->fns[n]; + if (dso != NULL && dso != fn.fn_dso) { + /* wrong DSO ? */ + if (!has_phdr || !__elf_phdr_match_addr( + &phdr_info, fn.fn_ptr.cxa_func)) + continue; + } /* Mark entry to indicate that this particular handler has already been called. @@ -182,4 +196,7 @@ __cxa_finalize(void *dso) } } _MUTEX_UNLOCK(&atexit_mutex); + + if (has_phdr && &__pthread_cxa_finalize != NULL) + __pthread_cxa_finalize(&phdr_info); } Modified: stable/8/lib/libthr/pthread.map ============================================================================== --- stable/8/lib/libthr/pthread.map Wed May 9 15:36:37 2012 (r235197) +++ stable/8/lib/libthr/pthread.map Wed May 9 15:57:59 2012 (r235198) @@ -198,6 +198,7 @@ FBSDprivate_1.0 { __poll; __pthread_cond_timedwait; __pthread_cond_wait; + __pthread_cxa_finalize; __pthread_mutex_init; __pthread_mutex_lock; __pthread_mutex_timedlock; Modified: stable/8/lib/libthr/thread/thr_fork.c ============================================================================== --- stable/8/lib/libthr/thread/thr_fork.c Wed May 9 15:36:37 2012 (r235197) +++ stable/8/lib/libthr/thread/thr_fork.c Wed May 9 15:57:59 2012 (r235198) @@ -59,6 +59,7 @@ #include "namespace.h" #include <errno.h> +#include <link.h> #include <string.h> #include <stdlib.h> #include <unistd.h> @@ -94,6 +95,27 @@ _pthread_atfork(void (*prepare)(void), v return (0); } +void +__pthread_cxa_finalize(struct dl_phdr_info *phdr_info) +{ + struct pthread *curthread; + struct pthread_atfork *af, *af1; + + _thr_check_init(); + + curthread = _get_curthread(); + THR_UMUTEX_LOCK(curthread, &_thr_atfork_lock); + TAILQ_FOREACH_SAFE(af, &_thr_atfork_list, qe, af1) { + if (__elf_phdr_match_addr(phdr_info, af->prepare) || + __elf_phdr_match_addr(phdr_info, af->parent) || + __elf_phdr_match_addr(phdr_info, af->child)) { + TAILQ_REMOVE(&_thr_atfork_list, af, qe); + free(af); + } + } + THR_UMUTEX_UNLOCK(curthread, &_thr_atfork_lock); +} + __weak_reference(_fork, fork); pid_t _fork(void); Modified: stable/8/lib/libthr/thread/thr_private.h ============================================================================== --- stable/8/lib/libthr/thread/thr_private.h Wed May 9 15:36:37 2012 (r235197) +++ stable/8/lib/libthr/thread/thr_private.h Wed May 9 15:57:59 2012 (r235198) @@ -725,6 +725,9 @@ _thr_check_init(void) _libpthread_init(NULL); } +struct dl_phdr_info; +void __pthread_cxa_finalize(struct dl_phdr_info *phdr_info); + __END_DECLS #endif /* !_THR_PRIVATE_H */ Modified: stable/8/libexec/rtld-elf/rtld.c ============================================================================== --- stable/8/libexec/rtld-elf/rtld.c Wed May 9 15:36:37 2012 (r235197) +++ stable/8/libexec/rtld-elf/rtld.c Wed May 9 15:57:59 2012 (r235198) @@ -3719,3 +3719,11 @@ fetch_ventry(const Obj_Entry *obj, unsig } return NULL; } + +/* + * No unresolved symbols for rtld. + */ +void +__pthread_cxa_finalize(struct dl_phdr_info *a) +{ +}
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201205091557.q49FvxLL091842>