]> git.ipfire.org Git - thirdparty/glibc.git/blob - nptl/tst-tls4.c
Further harden glibc malloc metadata against 1-byte overflows.
[thirdparty/glibc.git] / nptl / tst-tls4.c
1 /* Copyright (C) 2003-2017 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Jakub Jelinek <jakub@redhat.com>, 2003.
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 #include <dlfcn.h>
20 #include <errno.h>
21 #include <pthread.h>
22 #include <stdint.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26
27 #define N 3
28
29 void (*test1) (void), (*test2) (void);
30
31 pthread_barrier_t b2, b3;
32
33 static void *
34 tf (void *arg)
35 {
36 int i;
37
38 for (i = 0; i <= (uintptr_t) arg; ++i)
39 {
40 int r = pthread_barrier_wait (&b3);
41 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
42 {
43 puts ("tf: barrier_wait failed");
44 exit (1);
45 }
46 }
47
48 test1 ();
49
50 for (i = 0; i < 3; ++i)
51 {
52 int r = pthread_barrier_wait (&b3);
53 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
54 {
55 puts ("tf: barrier_wait failed");
56 exit (1);
57 }
58 }
59
60 test2 ();
61
62 for (i = 0; i < 3 - (uintptr_t) arg; ++i)
63 {
64 int r = pthread_barrier_wait (&b3);
65 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
66 {
67 puts ("tf: barrier_wait failed");
68 exit (1);
69 }
70 }
71
72 return NULL;
73 }
74
75 static void *
76 tf2 (void *arg)
77 {
78 int r = pthread_barrier_wait (&b2);
79 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
80 {
81 puts ("tf2: barrier_wait failed");
82 exit (1);
83 }
84
85 int i;
86 for (i = 0; i < N; ++i)
87 tf (arg);
88 return NULL;
89 }
90
91 int
92 do_test (void)
93 {
94 pthread_t th[2];
95 const char *modules[N]
96 = { "tst-tls4moda.so", "tst-tls4moda.so", "tst-tls4modb.so" };
97
98 if (pthread_barrier_init (&b2, NULL, 2) != 0)
99 {
100 puts ("barrier_init failed");
101 return 1;
102 }
103
104 if (pthread_barrier_init (&b3, NULL, 3) != 0)
105 {
106 puts ("barrier_init failed");
107 return 1;
108 }
109
110 if (pthread_create (&th[0], NULL, tf2, (void *) (uintptr_t) 1))
111 {
112 puts ("pthread_create failed");
113 return 1;
114 }
115
116 int r = pthread_barrier_wait (&b2);
117 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
118 {
119 puts ("barrier_wait failed");
120 return 1;
121 }
122
123 int i;
124 for (i = 0; i < N; ++i)
125 {
126 void *h = dlopen (modules[i], RTLD_LAZY);
127 if (h == NULL)
128 {
129 printf ("dlopen failed %s\n", dlerror ());
130 return 1;
131 }
132
133 test1 = dlsym (h, "test1");
134 if (test1 == NULL)
135 {
136 printf ("dlsym for test1 failed %s\n", dlerror ());
137 return 1;
138 }
139
140 test2 = dlsym (h, "test2");
141 if (test2 == NULL)
142 {
143 printf ("dlsym for test2 failed %s\n", dlerror ());
144 return 1;
145 }
146
147 if (pthread_create (&th[1], NULL, tf, (void *) (uintptr_t) 2))
148 {
149 puts ("pthread_create failed");
150 return 1;
151 }
152
153 tf ((void *) (uintptr_t) 0);
154
155 if (pthread_join (th[1], NULL) != 0)
156 {
157 puts ("join failed");
158 return 1;
159 }
160
161 if (dlclose (h))
162 {
163 puts ("dlclose failed");
164 return 1;
165 }
166
167 printf ("test %d with %s succeeded\n", i, modules[i]);
168 }
169
170 if (pthread_join (th[0], NULL) != 0)
171 {
172 puts ("join failed");
173 return 1;
174 }
175
176 return 0;
177 }
178
179 #define TIMEOUT 5
180 #define TEST_FUNCTION do_test ()
181 #include "../test-skeleton.c"