]> git.ipfire.org Git - thirdparty/glibc.git/blob - stdlib/test-dlclose-exit-race.c
d23650e765b7cb953d91a63861e468a85418253c
[thirdparty/glibc.git] / stdlib / test-dlclose-exit-race.c
1 /* Test for exit/dlclose race (Bug 22180).
2 Copyright (C) 2017-2019 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 /* This file must be run from within a directory called "stdlib". */
20
21 /* This test verifies that when dlopen in one thread races against exit
22 in another thread, we don't call registered destructor twice.
23
24 Expected result:
25 second
26 first
27 ... clean termination
28 */
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <semaphore.h>
33 #include <support/check.h>
34 #include <support/xdlfcn.h>
35 #include <support/xthread.h>
36
37 /* Semaphore to ensure we have a happens-before between the first function
38 starting and exit being called. */
39 sem_t order1;
40
41 /* Semaphore to ensure we have a happens-before between the second function
42 starting and the first function returning. */
43 sem_t order2;
44
45 void *
46 exit_thread (void *arg)
47 {
48 /* Wait for the dlclose to start... */
49 sem_wait (&order1);
50 /* Then try to run the exit sequence which should call all
51 __cxa_atexit registered functions and in parallel with
52 the executing dlclose(). */
53 exit (0);
54 }
55
56
57 void
58 last (void)
59 {
60 /* Let dlclose thread proceed. */
61 sem_post (&order2);
62 }
63
64 int
65 main (void)
66 {
67 void *dso;
68 pthread_t thread;
69
70 atexit (last);
71
72 dso = xdlopen ("$ORIGIN/test-dlclose-exit-race-helper.so",
73 RTLD_NOW|RTLD_GLOBAL);
74 thread = xpthread_create (NULL, exit_thread, NULL);
75
76 xdlclose (dso);
77 xpthread_join (thread);
78
79 FAIL_EXIT1 ("Did not terminate via exit(0) in exit_thread() as expected.");
80 }