tst-tls-atexit.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* Verify that DSO is unloaded only if its TLS objects are destroyed.
  2. Copyright (C) 2013-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. /* For the default case, i.e. NO_DELETE not defined, the test dynamically loads
  16. a DSO and spawns a thread that subsequently calls into the DSO to register a
  17. destructor for an object in the DSO and then calls dlclose on the handle for
  18. the DSO. When the thread exits, the DSO should not be unloaded or else the
  19. destructor called during thread exit will crash. Further in the main
  20. thread, the DSO is opened and closed again, at which point the DSO should be
  21. unloaded.
  22. When NO_DELETE is defined, the DSO is loaded twice, once with just RTLD_LAZY
  23. flag and the second time with the RTLD_NODELETE flag set. The thread is
  24. spawned, destructor registered and then thread exits without closing the
  25. DSO. In the main thread, the first handle is then closed, followed by the
  26. second handle. In the end, the DSO should remain loaded due to the
  27. RTLD_NODELETE flag being set in the second dlopen call. */
  28. #include <pthread.h>
  29. #include <stdio.h>
  30. #include <unistd.h>
  31. #include <string.h>
  32. #include <errno.h>
  33. #include <link.h>
  34. #include <stdbool.h>
  35. #include <support/xdlfcn.h>
  36. #ifndef NO_DELETE
  37. # define LOADED_IS_GOOD false
  38. #endif
  39. #ifndef H2_RTLD_FLAGS
  40. # define H2_RTLD_FLAGS (RTLD_LAZY)
  41. #endif
  42. #define DSO_NAME "$ORIGIN/tst-tls-atexit-lib.so"
  43. /* Walk through the map in the _r_debug structure to see if our lib is still
  44. loaded. */
  45. static bool
  46. is_loaded (void)
  47. {
  48. struct link_map *lm = (struct link_map *) _r_debug.r_map;
  49. for (; lm; lm = lm->l_next)
  50. if (lm->l_type == lt_loaded && lm->l_name
  51. && strcmp (basename (DSO_NAME), basename (lm->l_name)) == 0)
  52. {
  53. printf ("%s is still loaded\n", lm->l_name);
  54. return true;
  55. }
  56. return false;
  57. }
  58. /* Accept a valid handle returned by DLOPEN, load the reg_dtor symbol to
  59. register a destructor and then call dlclose on the handle. The dlclose
  60. should not unload the DSO since the destructor has not been called yet. */
  61. static void *
  62. reg_dtor_and_close (void *h)
  63. {
  64. void (*reg_dtor) (void) = (void (*) (void)) xdlsym (h, "reg_dtor");
  65. reg_dtor ();
  66. #ifndef NO_DELETE
  67. xdlclose (h);
  68. #endif
  69. return NULL;
  70. }
  71. static int
  72. spawn_thread (void *h)
  73. {
  74. pthread_t t;
  75. int ret;
  76. void *thr_ret;
  77. if ((ret = pthread_create (&t, NULL, reg_dtor_and_close, h)) != 0)
  78. {
  79. printf ("pthread_create failed: %s\n", strerror (ret));
  80. return 1;
  81. }
  82. if ((ret = pthread_join (t, &thr_ret)) != 0)
  83. {
  84. printf ("pthread_join failed: %s\n", strerror (ret));
  85. return 1;
  86. }
  87. if (thr_ret != NULL)
  88. return 1;
  89. return 0;
  90. }
  91. static int
  92. do_test (void)
  93. {
  94. /* Load the DSO. */
  95. void *h1 = xdlopen (DSO_NAME, RTLD_LAZY);
  96. #ifndef NO_DELETE
  97. if (spawn_thread (h1) != 0)
  98. return 1;
  99. #endif
  100. void *h2 = xdlopen (DSO_NAME, H2_RTLD_FLAGS);
  101. #ifdef NO_DELETE
  102. if (spawn_thread (h1) != 0)
  103. return 1;
  104. xdlclose (h1);
  105. #endif
  106. xdlclose (h2);
  107. /* Check link maps to ensure that the DSO has unloaded. In the normal case,
  108. the DSO should be unloaded if there are no uses. However, if one of the
  109. dlopen calls were with RTLD_NODELETE, the DSO should remain loaded. */
  110. return is_loaded () == LOADED_IS_GOOD ? 0 : 1;
  111. }
  112. #define TEST_FUNCTION do_test ()
  113. #include "../test-skeleton.c"