stdlib: Add more qsort{_r} coverage
[glibc.git] / nptl / tst-stack4.c
bloba27cfacb9c36400c7f6d1b466a1e77b0a06bccbb
1 /* Test DTV size overflow when pthread_create reuses old DTV and TLS is
2 used by dlopened shared object.
3 Copyright (C) 2014-2023 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <https://www.gnu.org/licenses/>. */
20 #include <stdio.h>
21 #include <stdint.h>
22 #include <dlfcn.h>
23 #include <assert.h>
24 #include <pthread.h>
26 /* The choices of thread count, and file counts are arbitrary.
27 The point is simply to run enough threads that an exiting
28 thread has it's stack reused by another thread at the same
29 time as new libraries have been loaded. */
30 #define DSO_SHARED_FILES 20
31 #define DSO_OPEN_THREADS 20
32 #define DSO_EXEC_THREADS 2
34 /* Used to make sure that only one thread is calling dlopen and dlclose
35 at a time. */
36 pthread_mutex_t g_lock;
38 typedef void (*function) (void);
40 void *
41 dso_invoke(void *dso_fun)
43 function *fun_vec = (function *) dso_fun;
44 int dso;
46 for (dso = 0; dso < DSO_SHARED_FILES; dso++)
47 (*fun_vec[dso]) ();
49 pthread_exit (NULL);
52 void *
53 dso_process (void * p)
55 void *handle[DSO_SHARED_FILES];
56 function fun_vec[DSO_SHARED_FILES];
57 char dso_path[DSO_SHARED_FILES][100];
58 int dso;
59 int t = (int) (uintptr_t) p;
61 /* Open DSOs and get a function. */
62 for (dso = 0; dso < DSO_SHARED_FILES; dso++)
64 sprintf (dso_path[dso], "tst-stack4mod-%i-%i.so", t, dso);
66 pthread_mutex_lock (&g_lock);
68 handle[dso] = dlopen (dso_path[dso], RTLD_NOW);
69 assert (handle[dso]);
71 fun_vec[dso] = (function) dlsym (handle[dso], "function");
72 assert (fun_vec[dso]);
74 pthread_mutex_unlock (&g_lock);
77 /* Spawn workers. */
78 pthread_t thread[DSO_EXEC_THREADS];
79 int i, ret;
80 uintptr_t result = 0;
81 for (i = 0; i < DSO_EXEC_THREADS; i++)
83 pthread_mutex_lock (&g_lock);
84 ret = pthread_create (&thread[i], NULL, dso_invoke, (void *) fun_vec);
85 if (ret != 0)
87 printf ("pthread_create failed: %d\n", ret);
88 result = 1;
90 pthread_mutex_unlock (&g_lock);
93 if (!result)
94 for (i = 0; i < DSO_EXEC_THREADS; i++)
96 ret = pthread_join (thread[i], NULL);
97 if (ret != 0)
99 printf ("pthread_join failed: %d\n", ret);
100 result = 1;
104 /* Close all DSOs. */
105 for (dso = 0; dso < DSO_SHARED_FILES; dso++)
107 pthread_mutex_lock (&g_lock);
108 dlclose (handle[dso]);
109 pthread_mutex_unlock (&g_lock);
112 /* Exit. */
113 pthread_exit ((void *) result);
116 static int
117 do_test (void)
119 pthread_t thread[DSO_OPEN_THREADS];
120 int i,j;
121 int ret;
122 int result = 0;
124 pthread_mutex_init (&g_lock, NULL);
126 /* 100 is arbitrary here and is known to trigger PR 13862. */
127 for (j = 0; j < 100; j++)
129 for (i = 0; i < DSO_OPEN_THREADS; i++)
131 ret = pthread_create (&thread[i], NULL, dso_process,
132 (void *) (uintptr_t) i);
133 if (ret != 0)
135 printf ("pthread_create failed: %d\n", ret);
136 result = 1;
140 if (result)
141 break;
143 for (i = 0; i < DSO_OPEN_THREADS; i++)
145 ret = pthread_join (thread[i], NULL);
146 if (ret != 0)
148 printf ("pthread_join failed: %d\n", ret);
149 result = 1;
154 return result;
157 #define TEST_FUNCTION do_test ()
158 #define TIMEOUT 100
159 #include "../test-skeleton.c"