2013-02-18 13:38:21 +00:00
|
|
|
/* Verify that DSO is unloaded only if its TLS objects are destroyed.
|
2024-01-01 18:12:26 +00:00
|
|
|
Copyright (C) 2013-2024 Free Software Foundation, Inc.
|
2013-02-18 13:38:21 +00:00
|
|
|
This file is part of the GNU C Library.
|
|
|
|
|
|
|
|
The GNU C Library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
The GNU C Library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with the GNU C Library; if not, see
|
Prefer https to http for gnu.org and fsf.org URLs
Also, change sources.redhat.com to sourceware.org.
This patch was automatically generated by running the following shell
script, which uses GNU sed, and which avoids modifying files imported
from upstream:
sed -ri '
s,(http|ftp)(://(.*\.)?(gnu|fsf|sourceware)\.org($|[^.]|\.[^a-z])),https\2,g
s,(http|ftp)(://(.*\.)?)sources\.redhat\.com($|[^.]|\.[^a-z]),https\2sourceware.org\4,g
' \
$(find $(git ls-files) -prune -type f \
! -name '*.po' \
! -name 'ChangeLog*' \
! -path COPYING ! -path COPYING.LIB \
! -path manual/fdl-1.3.texi ! -path manual/lgpl-2.1.texi \
! -path manual/texinfo.tex ! -path scripts/config.guess \
! -path scripts/config.sub ! -path scripts/install-sh \
! -path scripts/mkinstalldirs ! -path scripts/move-if-change \
! -path INSTALL ! -path locale/programs/charmap-kw.h \
! -path po/libc.pot ! -path sysdeps/gnu/errlist.c \
! '(' -name configure \
-execdir test -f configure.ac -o -f configure.in ';' ')' \
! '(' -name preconfigure \
-execdir test -f preconfigure.ac ';' ')' \
-print)
and then by running 'make dist-prepare' to regenerate files built
from the altered files, and then executing the following to cleanup:
chmod a+x sysdeps/unix/sysv/linux/riscv/configure
# Omit irrelevant whitespace and comment-only changes,
# perhaps from a slightly-different Autoconf version.
git checkout -f \
sysdeps/csky/configure \
sysdeps/hppa/configure \
sysdeps/riscv/configure \
sysdeps/unix/sysv/linux/csky/configure
# Omit changes that caused a pre-commit check to fail like this:
# remote: *** error: sysdeps/powerpc/powerpc64/ppc-mcount.S: trailing lines
git checkout -f \
sysdeps/powerpc/powerpc64/ppc-mcount.S \
sysdeps/unix/sysv/linux/s390/s390-64/syscall.S
# Omit change that caused a pre-commit check to fail like this:
# remote: *** error: sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S: last line does not end in newline
git checkout -f sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S
2019-09-07 05:40:42 +00:00
|
|
|
<https://www.gnu.org/licenses/>. */
|
2013-02-18 13:38:21 +00:00
|
|
|
|
2015-07-23 05:46:18 +00:00
|
|
|
/* For the default case, i.e. NO_DELETE not defined, the test dynamically loads
|
|
|
|
a DSO and spawns a thread that subsequently calls into the DSO to register a
|
|
|
|
destructor for an object in the DSO and then calls dlclose on the handle for
|
|
|
|
the DSO. When the thread exits, the DSO should not be unloaded or else the
|
|
|
|
destructor called during thread exit will crash. Further in the main
|
|
|
|
thread, the DSO is opened and closed again, at which point the DSO should be
|
|
|
|
unloaded.
|
|
|
|
|
|
|
|
When NO_DELETE is defined, the DSO is loaded twice, once with just RTLD_LAZY
|
|
|
|
flag and the second time with the RTLD_NODELETE flag set. The thread is
|
|
|
|
spawned, destructor registered and then thread exits without closing the
|
|
|
|
DSO. In the main thread, the first handle is then closed, followed by the
|
|
|
|
second handle. In the end, the DSO should remain loaded due to the
|
|
|
|
RTLD_NODELETE flag being set in the second dlopen call. */
|
2013-02-18 13:38:21 +00:00
|
|
|
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
2015-07-21 01:44:16 +00:00
|
|
|
#include <link.h>
|
2017-02-16 22:31:42 +00:00
|
|
|
#include <stdbool.h>
|
2017-09-21 02:37:45 +00:00
|
|
|
#include <support/xdlfcn.h>
|
2013-02-18 13:38:21 +00:00
|
|
|
|
2015-07-23 05:46:18 +00:00
|
|
|
#ifndef NO_DELETE
|
|
|
|
# define LOADED_IS_GOOD false
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef H2_RTLD_FLAGS
|
|
|
|
# define H2_RTLD_FLAGS (RTLD_LAZY)
|
|
|
|
#endif
|
|
|
|
|
2015-07-21 01:44:16 +00:00
|
|
|
#define DSO_NAME "$ORIGIN/tst-tls-atexit-lib.so"
|
2013-02-18 13:38:21 +00:00
|
|
|
|
2015-07-21 01:44:16 +00:00
|
|
|
/* Walk through the map in the _r_debug structure to see if our lib is still
|
|
|
|
loaded. */
|
|
|
|
static bool
|
|
|
|
is_loaded (void)
|
2013-02-18 13:38:21 +00:00
|
|
|
{
|
2015-07-21 01:44:16 +00:00
|
|
|
struct link_map *lm = (struct link_map *) _r_debug.r_map;
|
2013-02-18 13:38:21 +00:00
|
|
|
|
2015-07-21 01:44:16 +00:00
|
|
|
for (; lm; lm = lm->l_next)
|
|
|
|
if (lm->l_type == lt_loaded && lm->l_name
|
|
|
|
&& strcmp (basename (DSO_NAME), basename (lm->l_name)) == 0)
|
2015-07-23 05:46:18 +00:00
|
|
|
{
|
|
|
|
printf ("%s is still loaded\n", lm->l_name);
|
|
|
|
return true;
|
|
|
|
}
|
2015-07-21 01:44:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Accept a valid handle returned by DLOPEN, load the reg_dtor symbol to
|
|
|
|
register a destructor and then call dlclose on the handle. The dlclose
|
|
|
|
should not unload the DSO since the destructor has not been called yet. */
|
|
|
|
static void *
|
|
|
|
reg_dtor_and_close (void *h)
|
|
|
|
{
|
2017-09-21 02:37:45 +00:00
|
|
|
void (*reg_dtor) (void) = (void (*) (void)) xdlsym (h, "reg_dtor");
|
2013-02-18 13:38:21 +00:00
|
|
|
|
2015-07-21 01:44:16 +00:00
|
|
|
reg_dtor ();
|
2013-02-18 13:38:21 +00:00
|
|
|
|
2015-07-23 05:46:18 +00:00
|
|
|
#ifndef NO_DELETE
|
2017-09-21 02:37:45 +00:00
|
|
|
xdlclose (h);
|
2015-07-23 05:46:18 +00:00
|
|
|
#endif
|
2013-02-18 13:38:21 +00:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-11-05 09:54:08 +00:00
|
|
|
static int
|
2015-07-21 01:44:16 +00:00
|
|
|
spawn_thread (void *h)
|
2013-02-18 13:38:21 +00:00
|
|
|
{
|
|
|
|
pthread_t t;
|
|
|
|
int ret;
|
|
|
|
void *thr_ret;
|
|
|
|
|
2015-07-21 01:44:16 +00:00
|
|
|
if ((ret = pthread_create (&t, NULL, reg_dtor_and_close, h)) != 0)
|
2013-02-18 13:38:21 +00:00
|
|
|
{
|
|
|
|
printf ("pthread_create failed: %s\n", strerror (ret));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((ret = pthread_join (t, &thr_ret)) != 0)
|
|
|
|
{
|
2015-07-14 14:57:09 +00:00
|
|
|
printf ("pthread_join failed: %s\n", strerror (ret));
|
2013-02-18 13:38:21 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (thr_ret != NULL)
|
|
|
|
return 1;
|
|
|
|
|
2015-07-21 01:44:16 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2013-02-18 13:38:21 +00:00
|
|
|
|
2015-07-21 01:44:16 +00:00
|
|
|
static int
|
|
|
|
do_test (void)
|
|
|
|
{
|
|
|
|
/* Load the DSO. */
|
2017-09-21 02:37:45 +00:00
|
|
|
void *h1 = xdlopen (DSO_NAME, RTLD_LAZY);
|
2013-02-18 13:38:21 +00:00
|
|
|
|
2015-07-23 05:46:18 +00:00
|
|
|
#ifndef NO_DELETE
|
2015-07-21 01:44:16 +00:00
|
|
|
if (spawn_thread (h1) != 0)
|
|
|
|
return 1;
|
2015-07-23 05:46:18 +00:00
|
|
|
#endif
|
2015-07-21 01:44:16 +00:00
|
|
|
|
2017-09-21 02:37:45 +00:00
|
|
|
void *h2 = xdlopen (DSO_NAME, H2_RTLD_FLAGS);
|
2015-07-21 01:44:16 +00:00
|
|
|
|
2015-07-23 05:46:18 +00:00
|
|
|
#ifdef NO_DELETE
|
|
|
|
if (spawn_thread (h1) != 0)
|
2015-07-21 01:44:16 +00:00
|
|
|
return 1;
|
2013-02-18 13:38:21 +00:00
|
|
|
|
2017-09-21 02:37:45 +00:00
|
|
|
xdlclose (h1);
|
2015-07-23 05:46:18 +00:00
|
|
|
#endif
|
2017-09-21 02:37:45 +00:00
|
|
|
xdlclose (h2);
|
2015-07-23 05:46:18 +00:00
|
|
|
|
|
|
|
/* Check link maps to ensure that the DSO has unloaded. In the normal case,
|
|
|
|
the DSO should be unloaded if there are no uses. However, if one of the
|
|
|
|
dlopen calls were with RTLD_NODELETE, the DSO should remain loaded. */
|
|
|
|
return is_loaded () == LOADED_IS_GOOD ? 0 : 1;
|
2013-02-18 13:38:21 +00:00
|
|
|
}
|
2014-11-05 09:54:08 +00:00
|
|
|
|
|
|
|
#define TEST_FUNCTION do_test ()
|
|
|
|
#include "../test-skeleton.c"
|