Author: kib
Date: Mon Aug 23 15:38:02 2010
New Revision: 211706
URL: http://svn.freebsd.org/changeset/base/211706

Log:
  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

Added:
  head/lib/libc/gen/elf_utils.c   (contents, props changed)
Modified:
  head/lib/libc/gen/Makefile.inc
  head/lib/libc/gen/Symbol.map
  head/lib/libc/include/libc_private.h
  head/lib/libc/stdlib/atexit.c
  head/lib/libthr/pthread.map
  head/lib/libthr/thread/thr_fork.c
  head/lib/libthr/thread/thr_private.h
  head/libexec/rtld-elf/rtld.c

Modified: head/lib/libc/gen/Makefile.inc
==============================================================================
--- head/lib/libc/gen/Makefile.inc      Mon Aug 23 15:27:03 2010        
(r211705)
+++ head/lib/libc/gen/Makefile.inc      Mon Aug 23 15:38:02 2010        
(r211706)
@@ -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 \

Modified: head/lib/libc/gen/Symbol.map
==============================================================================
--- head/lib/libc/gen/Symbol.map        Mon Aug 23 15:27:03 2010        
(r211705)
+++ head/lib/libc/gen/Symbol.map        Mon Aug 23 15:38:02 2010        
(r211706)
@@ -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;

Added: head/lib/libc/gen/elf_utils.c
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ head/lib/libc/gen/elf_utils.c       Mon Aug 23 15:38:02 2010        
(r211706)
@@ -0,0 +1,47 @@
+/*
+ * Copyright (c) 2010 Konstantin Belousov <k...@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: head/lib/libc/include/libc_private.h
==============================================================================
--- head/lib/libc/include/libc_private.h        Mon Aug 23 15:27:03 2010        
(r211705)
+++ head/lib/libc/include/libc_private.h        Mon Aug 23 15:38:02 2010        
(r211706)
@@ -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_ */

Modified: head/lib/libc/stdlib/atexit.c
==============================================================================
--- head/lib/libc/stdlib/atexit.c       Mon Aug 23 15:27:03 2010        
(r211705)
+++ head/lib/libc/stdlib/atexit.c       Mon Aug 23 15:38:02 2010        
(r211706)
@@ -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>
@@ -147,6 +148,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
@@ -155,18 +159,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.
@@ -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);
 }

Modified: head/lib/libthr/pthread.map
==============================================================================
--- head/lib/libthr/pthread.map Mon Aug 23 15:27:03 2010        (r211705)
+++ head/lib/libthr/pthread.map Mon Aug 23 15:38:02 2010        (r211706)
@@ -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;

Modified: head/lib/libthr/thread/thr_fork.c
==============================================================================
--- head/lib/libthr/thread/thr_fork.c   Mon Aug 23 15:27:03 2010        
(r211705)
+++ head/lib/libthr/thread/thr_fork.c   Mon Aug 23 15:38:02 2010        
(r211706)
@@ -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: head/lib/libthr/thread/thr_private.h
==============================================================================
--- head/lib/libthr/thread/thr_private.h        Mon Aug 23 15:27:03 2010        
(r211705)
+++ head/lib/libthr/thread/thr_private.h        Mon Aug 23 15:38:02 2010        
(r211706)
@@ -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 */

Modified: head/libexec/rtld-elf/rtld.c
==============================================================================
--- head/libexec/rtld-elf/rtld.c        Mon Aug 23 15:27:03 2010        
(r211705)
+++ head/libexec/rtld-elf/rtld.c        Mon Aug 23 15:38:02 2010        
(r211706)
@@ -3692,6 +3692,10 @@ fetch_ventry(const Obj_Entry *obj, unsig
     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)
+{
+}
_______________________________________________
svn-src-head@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/svn-src-head
To unsubscribe, send any mail to "svn-src-head-unsubscr...@freebsd.org"

Reply via email to