Blame stdlib/tst-tls-atexit.c

Packit 6c4009
/* Verify that DSO is unloaded only if its TLS objects are destroyed.
Packit 6c4009
   Copyright (C) 2013-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
/* For the default case, i.e. NO_DELETE not defined, the test dynamically loads
Packit 6c4009
   a DSO and spawns a thread that subsequently calls into the DSO to register a
Packit 6c4009
   destructor for an object in the DSO and then calls dlclose on the handle for
Packit 6c4009
   the DSO.  When the thread exits, the DSO should not be unloaded or else the
Packit 6c4009
   destructor called during thread exit will crash.  Further in the main
Packit 6c4009
   thread, the DSO is opened and closed again, at which point the DSO should be
Packit 6c4009
   unloaded.
Packit 6c4009
Packit 6c4009
   When NO_DELETE is defined, the DSO is loaded twice, once with just RTLD_LAZY
Packit 6c4009
   flag and the second time with the RTLD_NODELETE flag set.  The thread is
Packit 6c4009
   spawned, destructor registered and then thread exits without closing the
Packit 6c4009
   DSO.  In the main thread, the first handle is then closed, followed by the
Packit 6c4009
   second handle.  In the end, the DSO should remain loaded due to the
Packit 6c4009
   RTLD_NODELETE flag being set in the second dlopen call.  */
Packit 6c4009
Packit 6c4009
#include <pthread.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <link.h>
Packit 6c4009
#include <stdbool.h>
Packit 6c4009
#include <support/xdlfcn.h>
Packit 6c4009
Packit 6c4009
#ifndef NO_DELETE
Packit 6c4009
# define LOADED_IS_GOOD false
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
#ifndef H2_RTLD_FLAGS
Packit 6c4009
# define H2_RTLD_FLAGS (RTLD_LAZY)
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
#define DSO_NAME "$ORIGIN/tst-tls-atexit-lib.so"
Packit 6c4009
Packit 6c4009
/* Walk through the map in the _r_debug structure to see if our lib is still
Packit 6c4009
   loaded.  */
Packit 6c4009
static bool
Packit 6c4009
is_loaded (void)
Packit 6c4009
{
Packit 6c4009
  struct link_map *lm = (struct link_map *) _r_debug.r_map;
Packit 6c4009
Packit 6c4009
  for (; lm; lm = lm->l_next)
Packit 6c4009
    if (lm->l_type == lt_loaded && lm->l_name
Packit 6c4009
	&& strcmp (basename (DSO_NAME), basename (lm->l_name)) == 0)
Packit 6c4009
      {
Packit 6c4009
	printf ("%s is still loaded\n", lm->l_name);
Packit 6c4009
	return true;
Packit 6c4009
      }
Packit 6c4009
  return false;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Accept a valid handle returned by DLOPEN, load the reg_dtor symbol to
Packit 6c4009
   register a destructor and then call dlclose on the handle.  The dlclose
Packit 6c4009
   should not unload the DSO since the destructor has not been called yet.  */
Packit 6c4009
static void *
Packit 6c4009
reg_dtor_and_close (void *h)
Packit 6c4009
{
Packit 6c4009
  void (*reg_dtor) (void) = (void (*) (void)) xdlsym (h, "reg_dtor");
Packit 6c4009
Packit 6c4009
  reg_dtor ();
Packit 6c4009
Packit 6c4009
#ifndef NO_DELETE
Packit 6c4009
  xdlclose (h);
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  return NULL;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
spawn_thread (void *h)
Packit 6c4009
{
Packit 6c4009
  pthread_t t;
Packit 6c4009
  int ret;
Packit 6c4009
  void *thr_ret;
Packit 6c4009
Packit 6c4009
  if ((ret = pthread_create (&t, NULL, reg_dtor_and_close, h)) != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("pthread_create failed: %s\n", strerror (ret));
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if ((ret = pthread_join (t, &thr_ret)) != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("pthread_join failed: %s\n", strerror (ret));
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (thr_ret != NULL)
Packit 6c4009
    return 1;
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  /* Load the DSO.  */
Packit 6c4009
  void *h1 = xdlopen (DSO_NAME, RTLD_LAZY);
Packit 6c4009
Packit 6c4009
#ifndef NO_DELETE
Packit 6c4009
  if (spawn_thread (h1) != 0)
Packit 6c4009
    return 1;
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  void *h2 = xdlopen (DSO_NAME, H2_RTLD_FLAGS);
Packit 6c4009
Packit 6c4009
#ifdef NO_DELETE
Packit 6c4009
  if (spawn_thread (h1) != 0)
Packit 6c4009
    return 1;
Packit 6c4009
Packit 6c4009
  xdlclose (h1);
Packit 6c4009
#endif
Packit 6c4009
  xdlclose (h2);
Packit 6c4009
Packit 6c4009
  /* Check link maps to ensure that the DSO has unloaded.  In the normal case,
Packit 6c4009
     the DSO should be unloaded if there are no uses.  However, if one of the
Packit 6c4009
     dlopen calls were with RTLD_NODELETE, the DSO should remain loaded.  */
Packit 6c4009
  return is_loaded () == LOADED_IS_GOOD ? 0 : 1;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#define TEST_FUNCTION do_test ()
Packit 6c4009
#include "../test-skeleton.c"