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