1
0
mirror of https://git.FreeBSD.org/src.git synced 2024-11-26 07:55:01 +00:00

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
This commit is contained in:
Konstantin Belousov 2010-08-23 15:38:02 +00:00
parent e048186a62
commit ea246b6369
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=211706
9 changed files with 109 additions and 4 deletions

View File

@ -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 \

View File

@ -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;

47
lib/libc/gen/elf_utils.c Normal file
View File

@ -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);
}

View File

@ -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_ */

View File

@ -37,6 +37,7 @@ static char sccsid[] = "@(#)atexit.c 8.2 (Berkeley) 7/3/94";
__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 *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);
}

View File

@ -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;

View File

@ -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), 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);

View File

@ -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 */

View File

@ -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)
{
}