2013-02-18 19:08:21 +05:30
|
|
|
/* Verify that DSO is unloaded only if its TLS objects are destroyed.
|
2015-01-02 16:28:19 +00:00
|
|
|
Copyright (C) 2013-2015 Free Software Foundation, Inc.
|
2013-02-18 19:08:21 +05:30
|
|
|
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
|
|
|
|
<http://www.gnu.org/licenses/>. */
|
|
|
|
|
2015-07-21 07:14:16 +05:30
|
|
|
/* This 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. */
|
2013-02-18 19:08:21 +05:30
|
|
|
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
2015-07-21 07:14:16 +05:30
|
|
|
#include <link.h>
|
2013-02-18 19:08:21 +05:30
|
|
|
|
2015-07-21 07:14:16 +05:30
|
|
|
#define DSO_NAME "$ORIGIN/tst-tls-atexit-lib.so"
|
2013-02-18 19:08:21 +05:30
|
|
|
|
2015-07-21 07:14:16 +05:30
|
|
|
/* 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 19:08:21 +05:30
|
|
|
{
|
2015-07-21 07:14:16 +05:30
|
|
|
struct link_map *lm = (struct link_map *) _r_debug.r_map;
|
2013-02-18 19:08:21 +05:30
|
|
|
|
2015-07-21 07:14:16 +05:30
|
|
|
for (; lm; lm = lm->l_next)
|
|
|
|
if (lm->l_type == lt_loaded && lm->l_name
|
|
|
|
&& strcmp (basename (DSO_NAME), basename (lm->l_name)) == 0)
|
|
|
|
return true;
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
void (*reg_dtor) (void) = (void (*) (void)) dlsym (h, "reg_dtor");
|
2013-02-18 19:08:21 +05:30
|
|
|
|
2015-07-21 07:14:16 +05:30
|
|
|
if (reg_dtor == NULL)
|
2013-02-18 19:08:21 +05:30
|
|
|
{
|
|
|
|
printf ("Unable to find symbol: %s\n", dlerror ());
|
2015-07-21 07:14:16 +05:30
|
|
|
return (void *) (uintptr_t) 1;
|
2013-02-18 19:08:21 +05:30
|
|
|
}
|
|
|
|
|
2015-07-21 07:14:16 +05:30
|
|
|
reg_dtor ();
|
2013-02-18 19:08:21 +05:30
|
|
|
|
2015-07-21 07:14:16 +05:30
|
|
|
dlclose (h);
|
2013-02-18 19:08:21 +05:30
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-11-05 15:24:08 +05:30
|
|
|
static int
|
2015-07-21 07:14:16 +05:30
|
|
|
spawn_thread (void *h)
|
2013-02-18 19:08:21 +05:30
|
|
|
{
|
|
|
|
pthread_t t;
|
|
|
|
int ret;
|
|
|
|
void *thr_ret;
|
|
|
|
|
2015-07-21 07:14:16 +05:30
|
|
|
if ((ret = pthread_create (&t, NULL, reg_dtor_and_close, h)) != 0)
|
2013-02-18 19:08:21 +05:30
|
|
|
{
|
|
|
|
printf ("pthread_create failed: %s\n", strerror (ret));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((ret = pthread_join (t, &thr_ret)) != 0)
|
|
|
|
{
|
2015-07-14 20:27:09 +05:30
|
|
|
printf ("pthread_join failed: %s\n", strerror (ret));
|
2013-02-18 19:08:21 +05:30
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (thr_ret != NULL)
|
|
|
|
return 1;
|
|
|
|
|
2015-07-21 07:14:16 +05:30
|
|
|
return 0;
|
|
|
|
}
|
2013-02-18 19:08:21 +05:30
|
|
|
|
2015-07-21 07:14:16 +05:30
|
|
|
static int
|
|
|
|
do_test (void)
|
|
|
|
{
|
|
|
|
/* Load the DSO. */
|
|
|
|
void *h1 = dlopen (DSO_NAME, RTLD_LAZY);
|
|
|
|
if (h1 == NULL)
|
2013-02-18 19:08:21 +05:30
|
|
|
{
|
2015-07-21 07:14:16 +05:30
|
|
|
printf ("h1: Unable to load DSO: %s\n", dlerror ());
|
|
|
|
return 1;
|
2013-02-18 19:08:21 +05:30
|
|
|
}
|
|
|
|
|
2015-07-21 07:14:16 +05:30
|
|
|
if (spawn_thread (h1) != 0)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
/* Now this should unload the DSO. FIXME: This is a bug, calling dlclose
|
|
|
|
like this is actually wrong, but it works because cxa_thread_atexit_impl
|
|
|
|
has a bug which results in dlclose allowing this to work. */
|
|
|
|
dlclose (h1);
|
|
|
|
|
|
|
|
/* Check link maps to ensure that the DSO has unloaded. */
|
|
|
|
if (is_loaded ())
|
|
|
|
return 1;
|
2013-02-18 19:08:21 +05:30
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2014-11-05 15:24:08 +05:30
|
|
|
|
|
|
|
#define TEST_FUNCTION do_test ()
|
|
|
|
#include "../test-skeleton.c"
|