1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
|
/* Basic tests for pthread guard area.
Copyright (C) 2025 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */
#include <array_length.h>
#include <pthreaddef.h>
#include <setjmp.h>
#include <stackinfo.h>
#include <stdio.h>
#include <support/check.h>
#include <support/test-driver.h>
#include <support/xsignal.h>
#include <support/xthread.h>
#include <support/xunistd.h>
#include <sys/mman.h>
#include <stdlib.h>
static long int pagesz;
/* To check if the guard region is inaccessible, the thread tries read/writes
on it and checks if a SIGSEGV is generated. */
static volatile sig_atomic_t signal_jump_set;
static sigjmp_buf signal_jmp_buf;
static void
sigsegv_handler (int sig)
{
if (signal_jump_set == 0)
return;
siglongjmp (signal_jmp_buf, sig);
}
static bool
try_access_buf (char *ptr, bool write)
{
signal_jump_set = true;
bool failed = sigsetjmp (signal_jmp_buf, 0) != 0;
if (!failed)
{
if (write)
*(volatile char *)(ptr) = 'x';
else
*(volatile char *)(ptr);
}
signal_jump_set = false;
return !failed;
}
static bool
try_read_buf (char *ptr)
{
return try_access_buf (ptr, false);
}
static bool
try_write_buf (char *ptr)
{
return try_access_buf (ptr, true);
}
static bool
try_read_write_buf (char *ptr)
{
return try_read_buf (ptr) && try_write_buf(ptr);
}
/* Return the guard region of the current thread (it only makes sense on
a thread created by pthread_created). */
struct stack_t
{
char *stack;
size_t stacksize;
char *guard;
size_t guardsize;
};
static inline size_t
adjust_stacksize (size_t stacksize)
{
/* For some ABIs, The guard page depends of the thread descriptor, which in
turn rely on the require static TLS. The only supported _STACK_GROWS_UP
ABI, hppa, defines TLS_DTV_AT_TP and it is not straightforward to
calculate the guard region with current pthread APIs. So to get a
correct stack size assumes an extra page after the guard area. */
#if _STACK_GROWS_DOWN
return stacksize;
#elif _STACK_GROWS_UP
return stacksize - pagesz;
#endif
}
struct stack_t
get_current_stack_info (void)
{
pthread_attr_t attr;
TEST_VERIFY_EXIT (pthread_getattr_np (pthread_self (), &attr) == 0);
void *stack;
size_t stacksize;
TEST_VERIFY_EXIT (pthread_attr_getstack (&attr, &stack, &stacksize) == 0);
size_t guardsize;
TEST_VERIFY_EXIT (pthread_attr_getguardsize (&attr, &guardsize) == 0);
/* The guardsize is reported as the current page size, although it might
be adjusted to a larger value (aarch64 for instance). */
if (guardsize != 0 && guardsize < ARCH_MIN_GUARD_SIZE)
guardsize = ARCH_MIN_GUARD_SIZE;
#if _STACK_GROWS_DOWN
void *guard = guardsize ? stack - guardsize : 0;
#elif _STACK_GROWS_UP
stacksize = adjust_stacksize (stacksize);
void *guard = guardsize ? stack + stacksize : 0;
#endif
pthread_attr_destroy (&attr);
return (struct stack_t) { stack, stacksize, guard, guardsize };
}
struct thread_args_t
{
size_t stacksize;
size_t guardsize;
};
struct thread_args_t
get_thread_args (const pthread_attr_t *attr)
{
size_t stacksize;
size_t guardsize;
TEST_COMPARE (pthread_attr_getstacksize (attr, &stacksize), 0);
TEST_COMPARE (pthread_attr_getguardsize (attr, &guardsize), 0);
if (guardsize < ARCH_MIN_GUARD_SIZE)
guardsize = ARCH_MIN_GUARD_SIZE;
return (struct thread_args_t) { stacksize, guardsize };
}
static void
set_thread_args (pthread_attr_t *attr, const struct thread_args_t *args)
{
xpthread_attr_setstacksize (attr, args->stacksize);
xpthread_attr_setguardsize (attr, args->guardsize);
}
static void *
tf (void *closure)
{
struct thread_args_t *args = closure;
struct stack_t s = get_current_stack_info ();
if (test_verbose)
printf ("debug: [tid=%jd] stack = { .stack=%p, stacksize=%#zx, guard=%p, "
"guardsize=%#zx }\n",
(intmax_t) gettid (),
s.stack,
s.stacksize,
s.guard,
s.guardsize);
if (args != NULL)
{
TEST_COMPARE (adjust_stacksize (args->stacksize), s.stacksize);
TEST_COMPARE (args->guardsize, s.guardsize);
}
/* Ensure we can access the stack area. */
TEST_COMPARE (try_read_buf (s.stack), true);
TEST_COMPARE (try_read_buf (&s.stack[s.stacksize / 2]), true);
TEST_COMPARE (try_read_buf (&s.stack[s.stacksize - 1]), true);
/* Check if accessing the guard area results in SIGSEGV. */
if (s.guardsize > 0)
{
TEST_COMPARE (try_read_write_buf (s.guard), false);
TEST_COMPARE (try_read_write_buf (&s.guard[s.guardsize / 2]), false);
TEST_COMPARE (try_read_write_buf (&s.guard[s.guardsize] - 1), false);
}
return NULL;
}
/* Test 1: caller provided stack without guard. */
static void
do_test1 (void)
{
pthread_attr_t attr;
xpthread_attr_init (&attr);
size_t stacksize = support_small_thread_stack_size ();
void *stack = xmmap (0,
stacksize,
PROT_READ | PROT_WRITE,
MAP_PRIVATE|MAP_ANONYMOUS|MAP_STACK,
-1);
xpthread_attr_setstack (&attr, stack, stacksize);
xpthread_attr_setguardsize (&attr, 0);
struct thread_args_t args = { stacksize, 0 };
pthread_t t = xpthread_create (&attr, tf, &args);
void *status = xpthread_join (t);
TEST_VERIFY (status == 0);
xpthread_attr_destroy (&attr);
xmunmap (stack, stacksize);
}
/* Test 2: same as 1., but with a guard area. */
static void
do_test2 (void)
{
pthread_attr_t attr;
xpthread_attr_init (&attr);
size_t stacksize = support_small_thread_stack_size ();
void *stack = xmmap (0,
stacksize,
PROT_READ | PROT_WRITE,
MAP_PRIVATE|MAP_ANONYMOUS|MAP_STACK,
-1);
xpthread_attr_setstack (&attr, stack, stacksize);
xpthread_attr_setguardsize (&attr, pagesz);
struct thread_args_t args = { stacksize, 0 };
pthread_t t = xpthread_create (&attr, tf, &args);
void *status = xpthread_join (t);
TEST_VERIFY (status == 0);
xpthread_attr_destroy (&attr);
xmunmap (stack, stacksize);
}
/* Test 3: pthread_create with default values. */
static void
do_test3 (void)
{
pthread_t t = xpthread_create (NULL, tf, NULL);
void *status = xpthread_join (t);
TEST_VERIFY (status == 0);
}
/* Test 4: pthread_create without a guard area. */
static void
do_test4 (void)
{
pthread_attr_t attr;
xpthread_attr_init (&attr);
struct thread_args_t args = get_thread_args (&attr);
args.stacksize += args.guardsize;
args.guardsize = 0;
set_thread_args (&attr, &args);
pthread_t t = xpthread_create (&attr, tf, &args);
void *status = xpthread_join (t);
TEST_VERIFY (status == 0);
xpthread_attr_destroy (&attr);
}
/* Test 5: pthread_create with non default stack and guard size value. */
static void
do_test5 (void)
{
pthread_attr_t attr;
xpthread_attr_init (&attr);
struct thread_args_t args = get_thread_args (&attr);
args.guardsize += pagesz;
args.stacksize += pagesz;
set_thread_args (&attr, &args);
pthread_t t = xpthread_create (&attr, tf, &args);
void *status = xpthread_join (t);
TEST_VERIFY (status == 0);
xpthread_attr_destroy (&attr);
}
/* Test 6: thread with the required size (stack + guard) that matches the
test 3, but with a larger guard area. The pthread_create will need to
increase the guard area. */
static void
do_test6 (void)
{
pthread_attr_t attr;
xpthread_attr_init (&attr);
struct thread_args_t args = get_thread_args (&attr);
args.guardsize += pagesz;
args.stacksize -= pagesz;
set_thread_args (&attr, &args);
pthread_t t = xpthread_create (&attr, tf, &args);
void *status = xpthread_join (t);
TEST_VERIFY (status == 0);
xpthread_attr_destroy (&attr);
}
/* Test 7: pthread_create with default values, the requires size matches the
one from test 3 and 6 (but with a reduced guard ares). The
pthread_create should use the cached stack from previous tests, but it
would require to reduce the guard area. */
static void
do_test7 (void)
{
pthread_t t = xpthread_create (NULL, tf, NULL);
void *status = xpthread_join (t);
TEST_VERIFY (status == 0);
}
static int
do_test (void)
{
pagesz = sysconf (_SC_PAGESIZE);
{
struct sigaction sa = {
.sa_handler = sigsegv_handler,
.sa_flags = SA_NODEFER,
};
sigemptyset (&sa.sa_mask);
xsigaction (SIGSEGV, &sa, NULL);
/* Some system generates SIGBUS accessing the guard area when it is
setup with madvise. */
xsigaction (SIGBUS, &sa, NULL);
}
static const struct {
const char *descr;
void (*test)(void);
} tests[] = {
{ "user provided stack without guard", do_test1 },
{ "user provided stack with guard", do_test2 },
{ "default attribute", do_test3 },
{ "default attribute without guard", do_test4 },
{ "non default stack and guard sizes", do_test5 },
{ "reused stack with larger guard", do_test6 },
{ "reused stack with smaller guard", do_test7 },
};
for (int i = 0; i < array_length (tests); i++)
{
printf ("debug: test%01d: %s\n", i, tests[i].descr);
tests[i].test();
}
return 0;
}
#include <support/test-driver.c>
|