From df9bc4850f3a6aef289d53c0c3d1fc78d63bdda2 Mon Sep 17 00:00:00 2001 From: kib Date: Mon, 23 Aug 2010 15:38:02 +0000 Subject: 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. Idea by: kan Reviewed by: kan (previous version) MFC after: 3 weeks --- lib/libc/gen/Makefile.inc | 2 +- lib/libc/gen/Symbol.map | 1 + lib/libc/gen/elf_utils.c | 47 +++++++++++++++++++++++++++++++++++++++++ lib/libc/include/libc_private.h | 2 ++ lib/libc/stdlib/atexit.c | 23 +++++++++++++++++--- lib/libthr/pthread.map | 1 + lib/libthr/thread/thr_fork.c | 22 +++++++++++++++++++ lib/libthr/thread/thr_private.h | 3 +++ libexec/rtld-elf/rtld.c | 12 +++++++++++ 9 files changed, 109 insertions(+), 4 deletions(-) create mode 100644 lib/libc/gen/elf_utils.c diff --git a/lib/libc/gen/Makefile.inc b/lib/libc/gen/Makefile.inc index 2c3d392..5056392 100644 --- a/lib/libc/gen/Makefile.inc +++ b/lib/libc/gen/Makefile.inc @@ -10,7 +10,7 @@ SRCS+= __getosreldate.c __xuname.c \ alarm.c arc4random.c assert.c aux.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 \ diff --git a/lib/libc/gen/Symbol.map b/lib/libc/gen/Symbol.map index c4f21b5..6bd3140 100644 --- a/lib/libc/gen/Symbol.map +++ b/lib/libc/gen/Symbol.map @@ -451,6 +451,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; diff --git a/lib/libc/gen/elf_utils.c b/lib/libc/gen/elf_utils.c new file mode 100644 index 0000000..9ee5f41 --- /dev/null +++ b/lib/libc/gen/elf_utils.c @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2010 Konstantin Belousov + * 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 + +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); +} diff --git a/lib/libc/include/libc_private.h b/lib/libc/include/libc_private.h index 570441b..c5f6eb3 100644 --- a/lib/libc/include/libc_private.h +++ b/lib/libc/include/libc_private.h @@ -214,5 +214,7 @@ extern int __sys_fcntl(int, int, ...); int _execvpe(const char *, char * const *, char * const *); int _elf_aux_info(int aux, void *buf, int buflen); +struct dl_phdr_info; +int __elf_phdr_match_addr(struct dl_phdr_info *, void *); #endif /* _LIBC_PRIVATE_H_ */ diff --git a/lib/libc/stdlib/atexit.c b/lib/libc/stdlib/atexit.c index a7a9a32..97cf234 100644 --- a/lib/libc/stdlib/atexit.c +++ b/lib/libc/stdlib/atexit.c @@ -37,6 +37,7 @@ static char sccsid[] = "@(#)atexit.c 8.2 (Berkeley) 7/3/94"; __FBSDID("$FreeBSD$"); #include "namespace.h" +#include #include #include #include @@ -147,6 +148,9 @@ __cxa_atexit(void (*func)(void *), void *arg, void *dso) 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 @@ -155,18 +159,28 @@ __cxa_atexit(void (*func)(void *), void *arg, void *dso) 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. @@ -185,4 +199,7 @@ __cxa_finalize(void *dso) _MUTEX_UNLOCK(&atexit_mutex); if (dso == NULL) _MUTEX_DESTROY(&atexit_mutex); + + if (&__pthread_cxa_finalize != NULL) + __pthread_cxa_finalize(&phdr_info); } diff --git a/lib/libthr/pthread.map b/lib/libthr/pthread.map index 6460bec..8457996 100644 --- a/lib/libthr/pthread.map +++ b/lib/libthr/pthread.map @@ -192,6 +192,7 @@ FBSDprivate_1.0 { __poll; __pthread_cond_timedwait; __pthread_cond_wait; + __pthread_cxa_finalize; __pthread_mutex_init; __pthread_mutex_lock; __pthread_mutex_timedlock; diff --git a/lib/libthr/thread/thr_fork.c b/lib/libthr/thread/thr_fork.c index 529f38f..b1c221e 100644 --- a/lib/libthr/thread/thr_fork.c +++ b/lib/libthr/thread/thr_fork.c @@ -59,6 +59,7 @@ #include "namespace.h" #include +#include #include #include #include @@ -94,6 +95,27 @@ _pthread_atfork(void (*prepare)(void), void (*parent)(void), 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); diff --git a/lib/libthr/thread/thr_private.h b/lib/libthr/thread/thr_private.h index 465f725..f0c3aa0 100644 --- a/lib/libthr/thread/thr_private.h +++ b/lib/libthr/thread/thr_private.h @@ -740,6 +740,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 */ diff --git a/libexec/rtld-elf/rtld.c b/libexec/rtld-elf/rtld.c index 77eeafb..dc5cdca 100644 --- a/libexec/rtld-elf/rtld.c +++ b/libexec/rtld-elf/rtld.c @@ -3692,6 +3692,10 @@ fetch_ventry(const Obj_Entry *obj, unsigned long symnum) return NULL; } +/* + * Overrides for libc_pic-provided functions. + */ + int __getosreldate(void) { @@ -3711,3 +3715,11 @@ __getosreldate(void) osreldate = osrel; return (osreldate); } + +/* + * No unresolved symbols for rtld. + */ +void +__pthread_cxa_finalize(struct dl_phdr_info *a) +{ +} -- cgit v1.1