1 /* Test DTV size oveflow when pthread_create reuses old DTV and TLS is
2 used by dlopened shared object.
3 Copyright (C) 2014-2015 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 <http://www.gnu.org/licenses/>. */
26 /* The choices of thread count, and file counts are arbitary.
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
36 pthread_mutex_t g_lock;
38 typedef void (*function) (void);
41 dso_invoke(void *dso_fun)
43 function *fun_vec = (function *) dso_fun;
46 for (dso = 0; dso < DSO_SHARED_FILES; dso++)
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];
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);
71 fun_vec[dso] = (function) dlsym (handle[dso], "function");
72 assert (fun_vec[dso]);
74 pthread_mutex_unlock (&g_lock);
78 pthread_t thread[DSO_EXEC_THREADS];
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);
87 printf ("pthread_create failed: %d\n", ret);
90 pthread_mutex_unlock (&g_lock);
94 for (i = 0; i < DSO_EXEC_THREADS; i++)
96 ret = pthread_join (thread[i], NULL);
99 printf ("pthread_join failed: %d\n", ret);
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);
113 pthread_exit ((void *) result);
119 pthread_t thread[DSO_OPEN_THREADS];
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);
135 printf ("pthread_create failed: %d\n", ret);
143 for (i = 0; i < DSO_OPEN_THREADS; i++)
145 ret = pthread_join (thread[i], NULL);
148 printf ("pthread_join failed: %d\n", ret);
157 #define TEST_FUNCTION do_test ()
159 #include "../test-skeleton.c"