Remove Linuxism from tst-tls-atexit
[glibc.git] / stdlib / tst-tls-atexit.c
blobcea655decc2d1d5022fda66bb9852f1c366245f8
1 /* Verify that DSO is unloaded only if its TLS objects are destroyed.
2 Copyright (C) 2013-2015 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
19 /* This test dynamically loads a DSO and spawns a thread that subsequently
20 calls into the DSO to register a destructor for an object in the DSO and
21 then calls dlclose on the handle for the DSO. When the thread exits, the
22 DSO should not be unloaded or else the destructor called during thread exit
23 will crash. Further in the main thread, the DSO is opened and closed again,
24 at which point the DSO should be unloaded. */
26 #include <dlfcn.h>
27 #include <pthread.h>
28 #include <stdio.h>
29 #include <unistd.h>
30 #include <string.h>
31 #include <errno.h>
32 #include <link.h>
34 #define DSO_NAME "$ORIGIN/tst-tls-atexit-lib.so"
36 /* Walk through the map in the _r_debug structure to see if our lib is still
37 loaded. */
38 static bool
39 is_loaded (void)
41 struct link_map *lm = (struct link_map *) _r_debug.r_map;
43 for (; lm; lm = lm->l_next)
44 if (lm->l_type == lt_loaded && lm->l_name
45 && strcmp (basename (DSO_NAME), basename (lm->l_name)) == 0)
46 return true;
47 return false;
50 /* Accept a valid handle returned by DLOPEN, load the reg_dtor symbol to
51 register a destructor and then call dlclose on the handle. The dlclose
52 should not unload the DSO since the destructor has not been called yet. */
53 static void *
54 reg_dtor_and_close (void *h)
56 void (*reg_dtor) (void) = (void (*) (void)) dlsym (h, "reg_dtor");
58 if (reg_dtor == NULL)
60 printf ("Unable to find symbol: %s\n", dlerror ());
61 return (void *) (uintptr_t) 1;
64 reg_dtor ();
66 dlclose (h);
68 return NULL;
71 static int
72 spawn_thread (void *h)
74 pthread_t t;
75 int ret;
76 void *thr_ret;
78 if ((ret = pthread_create (&t, NULL, reg_dtor_and_close, h)) != 0)
80 printf ("pthread_create failed: %s\n", strerror (ret));
81 return 1;
84 if ((ret = pthread_join (t, &thr_ret)) != 0)
86 printf ("pthread_join failed: %s\n", strerror (ret));
87 return 1;
90 if (thr_ret != NULL)
91 return 1;
93 return 0;
96 static int
97 do_test (void)
99 /* Load the DSO. */
100 void *h1 = dlopen (DSO_NAME, RTLD_LAZY);
101 if (h1 == NULL)
103 printf ("h1: Unable to load DSO: %s\n", dlerror ());
104 return 1;
107 if (spawn_thread (h1) != 0)
108 return 1;
110 /* Now this should unload the DSO. FIXME: This is a bug, calling dlclose
111 like this is actually wrong, but it works because cxa_thread_atexit_impl
112 has a bug which results in dlclose allowing this to work. */
113 dlclose (h1);
115 /* Check link maps to ensure that the DSO has unloaded. */
116 if (is_loaded ())
117 return 1;
119 return 0;
122 #define TEST_FUNCTION do_test ()
123 #include "../test-skeleton.c"