t_stack.c revision 1.1 1 1.1 riastrad /* $NetBSD: t_stack.c,v 1.1 2023/11/24 16:21:17 riastradh Exp $ */
2 1.1 riastrad
3 1.1 riastrad /*-
4 1.1 riastrad * Copyright (c) 2023 The NetBSD Foundation, Inc.
5 1.1 riastrad * All rights reserved.
6 1.1 riastrad *
7 1.1 riastrad * Redistribution and use in source and binary forms, with or without
8 1.1 riastrad * modification, are permitted provided that the following conditions
9 1.1 riastrad * are met:
10 1.1 riastrad * 1. Redistributions of source code must retain the above copyright
11 1.1 riastrad * notice, this list of conditions and the following disclaimer.
12 1.1 riastrad * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 riastrad * notice, this list of conditions and the following disclaimer in the
14 1.1 riastrad * documentation and/or other materials provided with the distribution.
15 1.1 riastrad *
16 1.1 riastrad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 riastrad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 riastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 riastrad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 riastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 riastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 riastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 riastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 riastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 riastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 riastrad * POSSIBILITY OF SUCH DAMAGE.
27 1.1 riastrad */
28 1.1 riastrad
29 1.1 riastrad #define _KMEMUSER /* __MACHINE_STACK_GROWS_UP */
30 1.1 riastrad
31 1.1 riastrad #include <sys/cdefs.h>
32 1.1 riastrad __RCSID("$NetBSD: t_stack.c,v 1.1 2023/11/24 16:21:17 riastradh Exp $");
33 1.1 riastrad
34 1.1 riastrad #include <sys/mman.h>
35 1.1 riastrad #include <sys/types.h>
36 1.1 riastrad
37 1.1 riastrad #include <atf-c.h>
38 1.1 riastrad #include <pthread.h>
39 1.1 riastrad #include <setjmp.h>
40 1.1 riastrad #include <signal.h>
41 1.1 riastrad #include <string.h>
42 1.1 riastrad #include <unistd.h>
43 1.1 riastrad
44 1.1 riastrad #include "h_macros.h"
45 1.1 riastrad
46 1.1 riastrad struct jmp_ctx {
47 1.1 riastrad jmp_buf buf;
48 1.1 riastrad };
49 1.1 riastrad
50 1.1 riastrad /*
51 1.1 riastrad * State used by various tests.
52 1.1 riastrad */
53 1.1 riastrad struct ctx {
54 1.1 riastrad size_t size; /* default stack size */
55 1.1 riastrad void *addr; /* user-allocated stack */
56 1.1 riastrad pthread_key_t jmp_key; /* jmp_ctx to return from SIGSEGV handler */
57 1.1 riastrad } ctx, *C = &ctx;
58 1.1 riastrad
59 1.1 riastrad /*
60 1.1 riastrad * getdefaultstacksize()
61 1.1 riastrad *
62 1.1 riastrad * Return the default stack size for threads created with
63 1.1 riastrad * pthread_create.
64 1.1 riastrad */
65 1.1 riastrad static size_t
66 1.1 riastrad getdefaultstacksize(void)
67 1.1 riastrad {
68 1.1 riastrad pthread_attr_t attr;
69 1.1 riastrad size_t stacksize;
70 1.1 riastrad
71 1.1 riastrad /*
72 1.1 riastrad * When called from the main thread, this returns the default
73 1.1 riastrad * stack size (pthread__stacksize) used for pthreads.
74 1.1 riastrad */
75 1.1 riastrad RZ(pthread_getattr_np(pthread_self(), &attr));
76 1.1 riastrad RZ(pthread_attr_getstacksize(&attr, &stacksize));
77 1.1 riastrad RZ(pthread_attr_destroy(&attr));
78 1.1 riastrad
79 1.1 riastrad /*
80 1.1 riastrad * Verify that the assumption above holds.
81 1.1 riastrad */
82 1.1 riastrad extern size_t pthread__stacksize; /* pthread_int.h */
83 1.1 riastrad ATF_CHECK_EQ_MSG(stacksize, pthread__stacksize,
84 1.1 riastrad "stacksize=%zu pthread__stacksize=%zu",
85 1.1 riastrad stacksize, pthread__stacksize);
86 1.1 riastrad
87 1.1 riastrad return stacksize;
88 1.1 riastrad }
89 1.1 riastrad
90 1.1 riastrad /*
91 1.1 riastrad * getnondefaultstacksize()
92 1.1 riastrad *
93 1.1 riastrad * Return a stack size that is not the default stack size for
94 1.1 riastrad * threads created with pthread_create.
95 1.1 riastrad */
96 1.1 riastrad static size_t
97 1.1 riastrad getnondefaultstacksize(void)
98 1.1 riastrad {
99 1.1 riastrad
100 1.1 riastrad return getdefaultstacksize() + sysconf(_SC_PAGESIZE);
101 1.1 riastrad }
102 1.1 riastrad
103 1.1 riastrad /*
104 1.1 riastrad * alloc(nbytes)
105 1.1 riastrad *
106 1.1 riastrad * Allocate an nbytes-long page-aligned read/write region and
107 1.1 riastrad * return a pointer to it. Abort the test if allocation fails, so
108 1.1 riastrad * if this function returns it succeeds.
109 1.1 riastrad */
110 1.1 riastrad static void *
111 1.1 riastrad alloc(size_t nbytes)
112 1.1 riastrad {
113 1.1 riastrad void *ptr;
114 1.1 riastrad
115 1.1 riastrad REQUIRE_LIBC((ptr = mmap(/*hint*/NULL, nbytes,
116 1.1 riastrad PROT_READ|PROT_WRITE, MAP_ANON, /*fd*/-1, /*offset*/0)),
117 1.1 riastrad MAP_FAILED);
118 1.1 riastrad
119 1.1 riastrad return ptr;
120 1.1 riastrad }
121 1.1 riastrad
122 1.1 riastrad /*
123 1.1 riastrad * init(stacksize)
124 1.1 riastrad *
125 1.1 riastrad * Initialize state used by various tests with the specified
126 1.1 riastrad * stacksize.
127 1.1 riastrad */
128 1.1 riastrad static void
129 1.1 riastrad init(size_t stacksize)
130 1.1 riastrad {
131 1.1 riastrad
132 1.1 riastrad C->size = stacksize;
133 1.1 riastrad C->addr = alloc(C->size);
134 1.1 riastrad RZ(pthread_key_create(&C->jmp_key, NULL));
135 1.1 riastrad }
136 1.1 riastrad
137 1.1 riastrad /*
138 1.1 riastrad * sigsegv_ok(signo)
139 1.1 riastrad *
140 1.1 riastrad * Signal handler for SIGSEGV to return to the jmp ctx, to verify
141 1.1 riastrad * that SIGSEGV happened without crashing.
142 1.1 riastrad */
143 1.1 riastrad static void
144 1.1 riastrad sigsegv_ok(int signo)
145 1.1 riastrad {
146 1.1 riastrad struct jmp_ctx *j = pthread_getspecific(C->jmp_key);
147 1.1 riastrad
148 1.1 riastrad longjmp(j->buf, 1);
149 1.1 riastrad }
150 1.1 riastrad
151 1.1 riastrad /*
152 1.1 riastrad * checksigsegv(p)
153 1.1 riastrad *
154 1.1 riastrad * Verify that reading *p triggers SIGSEGV. Fails test nonfatally
155 1.1 riastrad * if SIGSEGV doesn't happen.
156 1.1 riastrad */
157 1.1 riastrad static void
158 1.1 riastrad checksigsegv(const char *p)
159 1.1 riastrad {
160 1.1 riastrad struct jmp_ctx j;
161 1.1 riastrad struct sigaction act, oact;
162 1.1 riastrad volatile struct sigaction oactsave;
163 1.1 riastrad volatile char v;
164 1.1 riastrad
165 1.1 riastrad memset(&act, 0, sizeof(act));
166 1.1 riastrad act.sa_handler = &sigsegv_ok;
167 1.1 riastrad
168 1.1 riastrad pthread_setspecific(C->jmp_key, &j);
169 1.1 riastrad if (setjmp(j.buf) == 0) {
170 1.1 riastrad RL(sigaction(SIGSEGV, &act, &oact));
171 1.1 riastrad oactsave = oact;
172 1.1 riastrad v = *p; /* trigger SIGSEGV */
173 1.1 riastrad atf_tc_fail_nonfatal("failed to trigger SIGSEGV at %p", p);
174 1.1 riastrad } else {
175 1.1 riastrad /* return from SIGSEGV handler */
176 1.1 riastrad oact = oactsave;
177 1.1 riastrad }
178 1.1 riastrad RL(sigaction(SIGSEGV, &oact, NULL));
179 1.1 riastrad pthread_setspecific(C->jmp_key, NULL);
180 1.1 riastrad
181 1.1 riastrad (void)v; /* suppress unused variable warnings */
182 1.1 riastrad }
183 1.1 riastrad
184 1.1 riastrad /*
185 1.1 riastrad * checknosigsegv(p)
186 1.1 riastrad *
187 1.1 riastrad * Verify that reading *p does not trigger SIGSEGV. Fails test
188 1.1 riastrad * nonfatally if SIGSEGV happens.
189 1.1 riastrad */
190 1.1 riastrad static void
191 1.1 riastrad checknosigsegv(const char *p)
192 1.1 riastrad {
193 1.1 riastrad struct jmp_ctx j;
194 1.1 riastrad struct sigaction act, oact;
195 1.1 riastrad volatile struct sigaction oactsave;
196 1.1 riastrad volatile char v;
197 1.1 riastrad
198 1.1 riastrad memset(&act, 0, sizeof(act));
199 1.1 riastrad act.sa_handler = &sigsegv_ok;
200 1.1 riastrad
201 1.1 riastrad pthread_setspecific(C->jmp_key, &j);
202 1.1 riastrad if (setjmp(j.buf) == 0) {
203 1.1 riastrad RL(sigaction(SIGSEGV, &act, &oact));
204 1.1 riastrad oactsave = oact;
205 1.1 riastrad v = *p; /* better not trigger SIGSEGV */
206 1.1 riastrad } else {
207 1.1 riastrad /* return from SIGSEGV handler */
208 1.1 riastrad atf_tc_fail_nonfatal("spuriously triggered SIGSEGV at %p", p);
209 1.1 riastrad oact = oactsave;
210 1.1 riastrad }
211 1.1 riastrad RL(sigaction(SIGSEGV, &oact, NULL));
212 1.1 riastrad pthread_setspecific(C->jmp_key, NULL);
213 1.1 riastrad
214 1.1 riastrad (void)v; /* suppress unused variable warnings */
215 1.1 riastrad }
216 1.1 riastrad
217 1.1 riastrad /*
218 1.1 riastrad * checkguardaccessthread(cookie)
219 1.1 riastrad *
220 1.1 riastrad * Thread start routine that verifies it has access to the start
221 1.1 riastrad * and end of its stack, according to pthread_attr_getstack, and
222 1.1 riastrad * _does not_ have access to the start or end of its stack guard,
223 1.1 riastrad * above the stack (in stack growth direction) by
224 1.1 riastrad * pthread_attr_getguardsize bytes.
225 1.1 riastrad */
226 1.1 riastrad static void *
227 1.1 riastrad checkguardaccessthread(void *cookie)
228 1.1 riastrad {
229 1.1 riastrad pthread_t t = pthread_self();
230 1.1 riastrad pthread_attr_t attr;
231 1.1 riastrad void *addr, *guard;
232 1.1 riastrad size_t size, guardsize;
233 1.1 riastrad
234 1.1 riastrad /*
235 1.1 riastrad * Get the the stack and stack guard parameters.
236 1.1 riastrad */
237 1.1 riastrad RZ(pthread_getattr_np(t, &attr));
238 1.1 riastrad RZ(pthread_attr_getstack(&attr, &addr, &size));
239 1.1 riastrad RZ(pthread_attr_getguardsize(&attr, &guardsize));
240 1.1 riastrad
241 1.1 riastrad /*
242 1.1 riastrad * Determine where the guard starts in virtual address space
243 1.1 riastrad * (not in stack growth direction).
244 1.1 riastrad */
245 1.1 riastrad #ifdef __MACHINE_STACK_GROWS_UP
246 1.1 riastrad guard = (char *)addr + size;
247 1.1 riastrad #else
248 1.1 riastrad guard = (char *)addr - guardsize;
249 1.1 riastrad #endif
250 1.1 riastrad
251 1.1 riastrad /*
252 1.1 riastrad * Verify access to the start and end of the stack itself.
253 1.1 riastrad */
254 1.1 riastrad checknosigsegv(addr);
255 1.1 riastrad checknosigsegv((char *)addr + size - 1);
256 1.1 riastrad
257 1.1 riastrad /*
258 1.1 riastrad * Verify no access to the start or end of the stack guard.
259 1.1 riastrad */
260 1.1 riastrad checksigsegv(guard);
261 1.1 riastrad checksigsegv((char *)guard + guardsize - 1);
262 1.1 riastrad
263 1.1 riastrad return NULL;
264 1.1 riastrad }
265 1.1 riastrad
266 1.1 riastrad /*
267 1.1 riastrad * checkaddraccessthread(cookie)
268 1.1 riastrad *
269 1.1 riastrad * Thread start routine that verifies its stack is [C->addr,
270 1.1 riastrad * C->addr + C->size), according to pthread_attr_getstack and
271 1.1 riastrad * pthread_addr_getstacksize, and verifies it has access to that
272 1.1 riastrad * whole range.
273 1.1 riastrad */
274 1.1 riastrad static void *
275 1.1 riastrad checkaddraccessthread(void *cookie)
276 1.1 riastrad {
277 1.1 riastrad pthread_t t = pthread_self();
278 1.1 riastrad pthread_attr_t attr;
279 1.1 riastrad void *addr;
280 1.1 riastrad size_t size, size0;
281 1.1 riastrad
282 1.1 riastrad /*
283 1.1 riastrad * Get the stack parameters -- both via pthread_attr_getstack
284 1.1 riastrad * and via pthread_attr_getstacksize, to make sure they agree
285 1.1 riastrad * -- and verify that they are what we expect from the caller.
286 1.1 riastrad */
287 1.1 riastrad RZ(pthread_getattr_np(t, &attr));
288 1.1 riastrad RZ(pthread_attr_getstack(&attr, &addr, &size));
289 1.1 riastrad RZ(pthread_attr_getstacksize(&attr, &size0));
290 1.1 riastrad ATF_CHECK_EQ_MSG(C->addr, addr, "expected %p actual %p",
291 1.1 riastrad C->addr, addr);
292 1.1 riastrad ATF_CHECK_EQ_MSG(C->size, size, "expected %zu actual %zu",
293 1.1 riastrad C->size, size);
294 1.1 riastrad ATF_CHECK_EQ_MSG(C->size, size0, "expected %zu actual %zu",
295 1.1 riastrad C->size, size0);
296 1.1 riastrad
297 1.1 riastrad /*
298 1.1 riastrad * Verify that we have access to what we expect the stack to
299 1.1 riastrad * be.
300 1.1 riastrad */
301 1.1 riastrad checknosigsegv(C->addr);
302 1.1 riastrad checknosigsegv((char *)C->addr + C->size - 1);
303 1.1 riastrad
304 1.1 riastrad return NULL;
305 1.1 riastrad }
306 1.1 riastrad
307 1.1 riastrad ATF_TC(stack1);
308 1.1 riastrad ATF_TC_HEAD(stack1, tc)
309 1.1 riastrad {
310 1.1 riastrad atf_tc_set_md_var(tc, "descr",
311 1.1 riastrad "Test allocating and reallocating a thread with a user stack");
312 1.1 riastrad }
313 1.1 riastrad ATF_TC_BODY(stack1, tc)
314 1.1 riastrad {
315 1.1 riastrad pthread_attr_t attr;
316 1.1 riastrad pthread_t t, t2;
317 1.1 riastrad
318 1.1 riastrad /*
319 1.1 riastrad * Allocate a stack with a non-default size to verify
320 1.1 riastrad * libpthread didn't choose the stack size for us.
321 1.1 riastrad */
322 1.1 riastrad init(getnondefaultstacksize());
323 1.1 riastrad
324 1.1 riastrad
325 1.1 riastrad /*
326 1.1 riastrad * Create a thread with user-allocated stack of a non-default
327 1.1 riastrad * size to verify the stack size and access.
328 1.1 riastrad */
329 1.1 riastrad RZ(pthread_attr_init(&attr));
330 1.1 riastrad RZ(pthread_attr_setstack(&attr, C->addr, C->size));
331 1.1 riastrad atf_tc_expect_fail("PR lib/57721: pthread_attr_setstack"
332 1.1 riastrad " incorrectly adjusts address as if for guard page");
333 1.1 riastrad RZ(pthread_create(&t, &attr, &checkaddraccessthread, C));
334 1.1 riastrad RZ(pthread_join(t, NULL));
335 1.1 riastrad
336 1.1 riastrad /*
337 1.1 riastrad * Create another thread with the same parameters, and verify
338 1.1 riastrad * that (a) it was recycled, and (b) it works the same way.
339 1.1 riastrad */
340 1.1 riastrad RZ(pthread_create(&t2, &attr, &checkaddraccessthread, C));
341 1.1 riastrad ATF_CHECK_EQ_MSG(t, t2, "t=%p t2=%p", t, t2); /* NetBSD recycles */
342 1.1 riastrad RZ(pthread_join(t2, NULL));
343 1.1 riastrad }
344 1.1 riastrad
345 1.1 riastrad ATF_TC(stack2);
346 1.1 riastrad ATF_TC_HEAD(stack2, tc)
347 1.1 riastrad {
348 1.1 riastrad atf_tc_set_md_var(tc, "descr",
349 1.1 riastrad "Test reallocating a thread with a newly self-allocated stack");
350 1.1 riastrad }
351 1.1 riastrad ATF_TC_BODY(stack2, tc)
352 1.1 riastrad {
353 1.1 riastrad pthread_attr_t attr, attr2;
354 1.1 riastrad size_t size, size2;
355 1.1 riastrad pthread_t t, t2;
356 1.1 riastrad
357 1.1 riastrad /*
358 1.1 riastrad * Allocate a stack with the default size so that we verify
359 1.1 riastrad * when libpthread reuses the thread, it doesn't inadvertently
360 1.1 riastrad * reuse the libpthread-allocated stack too and instead
361 1.1 riastrad * correctly uses our user-allocated stack.
362 1.1 riastrad */
363 1.1 riastrad init(getdefaultstacksize());
364 1.1 riastrad
365 1.1 riastrad /*
366 1.1 riastrad * Create a thread with a libpthread-allocated stack that
367 1.1 riastrad * verifies
368 1.1 riastrad * (a) access to its own stack, and
369 1.1 riastrad * (b) no access to its own guard pages;
370 1.1 riastrad * then get its attributes and wait for it to complete.
371 1.1 riastrad */
372 1.1 riastrad RZ(pthread_create(&t, NULL, &checkguardaccessthread, C));
373 1.1 riastrad RZ(pthread_getattr_np(t, &attr));
374 1.1 riastrad RZ(pthread_join(t, NULL));
375 1.1 riastrad
376 1.1 riastrad /*
377 1.1 riastrad * Create a thread with a user-allocated stack that verifies
378 1.1 riastrad * (a) stack addr/size match request, and
379 1.1 riastrad * (b) access to the requested stack,
380 1.1 riastrad * and confirm that the first thread was recycled -- not part
381 1.1 riastrad * of POSIX semantics, but part of NetBSD's implementation;
382 1.1 riastrad * this way, we verify that, even though the thread is
383 1.1 riastrad * recycled, the thread's stack is set to the user-allocated
384 1.1 riastrad * stack and access to it works as expected. Then wait for it
385 1.1 riastrad * to complete.
386 1.1 riastrad */
387 1.1 riastrad RZ(pthread_attr_init(&attr2));
388 1.1 riastrad RZ(pthread_attr_setstack(&attr2, C->addr, C->size));
389 1.1 riastrad atf_tc_expect_fail("PR lib/57721: pthread_attr_setstack"
390 1.1 riastrad " incorrectly adjusts address as if for guard page");
391 1.1 riastrad RZ(pthread_create(&t2, &attr2, &checkaddraccessthread, C));
392 1.1 riastrad ATF_CHECK_EQ_MSG(t, t2, "t=%p t2=%p", t, t2); /* NetBSD recycles */
393 1.1 riastrad RZ(pthread_join(t2, NULL));
394 1.1 riastrad
395 1.1 riastrad /*
396 1.1 riastrad * Verify that the libpthread-allocated stack and
397 1.1 riastrad * user-allocated stack had the same size, since we chose the
398 1.1 riastrad * default size.
399 1.1 riastrad *
400 1.1 riastrad * Note: We can't say anything about the guard size, because
401 1.1 riastrad * with pthread_attr_setstack, the guard size is ignored, and
402 1.1 riastrad * it's not clear from POSIX whether any meaningful guard size
403 1.1 riastrad * is stored for retrieval with pthread_attr_getguardsize in
404 1.1 riastrad * attributes with pthread_attr_setstack.
405 1.1 riastrad */
406 1.1 riastrad RZ(pthread_attr_getstacksize(&attr, &size));
407 1.1 riastrad RZ(pthread_attr_getstacksize(&attr2, &size2));
408 1.1 riastrad ATF_CHECK_EQ_MSG(size, size2, "size=%zu size2=%zu", size, size2);
409 1.1 riastrad }
410 1.1 riastrad
411 1.1 riastrad ATF_TP_ADD_TCS(tp)
412 1.1 riastrad {
413 1.1 riastrad
414 1.1 riastrad ATF_TP_ADD_TC(tp, stack1);
415 1.1 riastrad ATF_TP_ADD_TC(tp, stack2);
416 1.1 riastrad
417 1.1 riastrad return atf_no_error();
418 1.1 riastrad }
419