]> git.ipfire.org Git - thirdparty/glibc.git/blame - nptl/tst-pthread-getattr.c
Update copyright dates with scripts/update-copyrights
[thirdparty/glibc.git] / nptl / tst-pthread-getattr.c
CommitLineData
18b5e737
SP
1/* Make sure that the stackaddr returned by pthread_getattr_np is
2 reachable.
3
dff8da6b 4 Copyright (C) 2012-2024 Free Software Foundation, Inc.
18b5e737
SP
5 This file is part of the GNU C Library.
6
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
11
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; if not, see
5a82c748 19 <https://www.gnu.org/licenses/>. */
18b5e737
SP
20
21#include <stdio.h>
22#include <string.h>
23#include <sys/resource.h>
fc56c5bb 24#include <sys/param.h>
18b5e737
SP
25#include <pthread.h>
26#include <alloca.h>
fc56c5bb
SP
27#include <assert.h>
28#include <unistd.h>
29#include <inttypes.h>
30
2fe64148
DD
31#include <support/support.h>
32
fc56c5bb
SP
33/* There is an obscure bug in the kernel due to which RLIMIT_STACK is sometimes
34 returned as unlimited when it is not, which may cause this test to fail.
35 There is also the other case where RLIMIT_STACK is intentionally set as
36 unlimited or very high, which may result in a vma that is too large and again
37 results in a test case failure. To avoid these problems, we cap the stack
38 size to one less than 8M. See the following mailing list threads for more
39 information about this problem:
a306c790
SP
40 <https://sourceware.org/ml/libc-alpha/2012-06/msg00599.html>
41 <https://sourceware.org/ml/libc-alpha/2012-06/msg00713.html>. */
fc56c5bb
SP
42#define MAX_STACK_SIZE (8192 * 1024 - 1)
43
44static size_t pagesize;
45
8a814e20
FW
46/* Test that the page in which TARGET lies is accessible. This will
47 segfault if the write fails. This function has only half a page
48 of thread stack left and so should not do anything and immediately
49 return the address to which the stack reached. */
50static volatile uintptr_t
fc56c5bb 51allocate_and_test (char *target)
18b5e737 52{
fc56c5bb
SP
53 volatile char *mem = (char *) &mem;
54 /* FIXME: mem >= target for _STACK_GROWSUP. */
55 mem = alloca ((size_t) (mem - target));
56
57 *mem = 42;
8a814e20 58 return (uintptr_t) mem;
18b5e737
SP
59}
60
61static int
62get_self_pthread_attr (const char *id, void **stackaddr, size_t *stacksize)
63{
64 pthread_attr_t attr;
65 int ret;
66 pthread_t me = pthread_self ();
67
fc56c5bb 68 if ((ret = pthread_getattr_np (me, &attr)) < 0)
18b5e737
SP
69 {
70 printf ("%s: pthread_getattr_np failed: %s\n", id, strerror (ret));
71 return 1;
72 }
73
fc56c5bb 74 if ((ret = pthread_attr_getstack (&attr, stackaddr, stacksize)) < 0)
18b5e737
SP
75 {
76 printf ("%s: pthread_attr_getstack returned error: %s\n", id,
77 strerror (ret));
78 return 1;
79 }
80
81 return 0;
82}
83
84/* Verify that the stack size returned by pthread_getattr_np is usable when
85 the returned value is subject to rlimit. */
86static int
87check_stack_top (void)
88{
89 struct rlimit stack_limit;
90 void *stackaddr;
91 size_t stacksize = 0;
92 int ret;
fc56c5bb 93 uintptr_t pagemask = ~(pagesize - 1);
18b5e737
SP
94
95 puts ("Verifying that stack top is accessible");
96
97 ret = getrlimit (RLIMIT_STACK, &stack_limit);
98 if (ret)
99 {
100 perror ("getrlimit failed");
101 return 1;
102 }
103
fc56c5bb
SP
104 printf ("current rlimit_stack is %zu\n", (size_t) stack_limit.rlim_cur);
105
18b5e737
SP
106 if (get_self_pthread_attr ("check_stack_top", &stackaddr, &stacksize))
107 return 1;
108
fc56c5bb
SP
109 /* Reduce the rlimit to a page less that what is currently being returned
110 (subject to a maximum of MAX_STACK_SIZE) so that we ensure that
111 pthread_getattr_np uses rlimit. The figure is intentionally unaligned so
112 to verify that pthread_getattr_np returns an aligned stacksize that
113 correctly fits into the rlimit. We don't bother about the case where the
114 stack is limited by the vma below it and not by the rlimit because the
115 stacksize returned in that case is computed from the end of that vma and is
116 hence safe. */
117 stack_limit.rlim_cur = MIN (stacksize - pagesize + 1, MAX_STACK_SIZE);
118 printf ("Adjusting RLIMIT_STACK to %zu\n", (size_t) stack_limit.rlim_cur);
119 if ((ret = setrlimit (RLIMIT_STACK, &stack_limit)) < 0)
18b5e737
SP
120 {
121 perror ("setrlimit failed");
122 return 1;
123 }
124
125 if (get_self_pthread_attr ("check_stack_top2", &stackaddr, &stacksize))
126 return 1;
127
128 printf ("Adjusted rlimit: stacksize=%zu, stackaddr=%p\n", stacksize,
129 stackaddr);
fc56c5bb
SP
130
131 /* A lot of targets tend to write stuff on top of the user stack during
132 context switches, so we cannot possibly safely go up to the very top of
133 stack and test access there. It is however sufficient to simply check if
134 the top page is accessible, so we target our access halfway up the top
135 page. Thanks Chris Metcalf for this idea. */
8a814e20 136 uintptr_t mem = allocate_and_test (stackaddr + pagesize / 2);
fc56c5bb
SP
137
138 /* Before we celebrate, make sure we actually did test the same page. */
8a814e20 139 if (((uintptr_t) stackaddr & pagemask) != (mem & pagemask))
fc56c5bb
SP
140 {
141 printf ("We successfully wrote into the wrong page.\n"
142 "Expected %#" PRIxPTR ", but got %#" PRIxPTR "\n",
8a814e20 143 (uintptr_t) stackaddr & pagemask, mem & pagemask);
fc56c5bb
SP
144
145 return 1;
146 }
18b5e737
SP
147
148 puts ("Stack top tests done");
149
150 return 0;
151}
152
153/* TODO: Similar check for thread stacks once the thread stack sizes are
154 fixed. */
155static int
156do_test (void)
157{
2fe64148
DD
158 support_need_proc ("Reads /proc/self/maps to get stack size.");
159
fc56c5bb 160 pagesize = sysconf (_SC_PAGESIZE);
18b5e737
SP
161 return check_stack_top ();
162}
163
164
165#define TEST_FUNCTION do_test ()
166#include "../test-skeleton.c"