]> git.ipfire.org Git - thirdparty/glibc.git/blob - nptl/tst-pthread-getattr.c
Merge glibc-ports into ports/ directory.
[thirdparty/glibc.git] / nptl / tst-pthread-getattr.c
1 /* Make sure that the stackaddr returned by pthread_getattr_np is
2 reachable.
3
4 Copyright (C) 2012 Free Software Foundation, Inc.
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
19 <http://www.gnu.org/licenses/>. */
20
21 #include <stdio.h>
22 #include <string.h>
23 #include <sys/resource.h>
24 #include <pthread.h>
25 #include <alloca.h>
26
27 /* Move the stack pointer so that stackaddr is accessible and then check if it
28 really is accessible. This will segfault if it fails. */
29 static void
30 allocate_and_test (void *stackaddr)
31 {
32 void *mem = &mem;
33 /* FIXME: The difference will be negative for _STACK_GROWSUP. */
34 mem = alloca ((size_t) (mem - stackaddr));
35 *(int *)(mem) = 0;
36 }
37
38 static int
39 get_self_pthread_attr (const char *id, void **stackaddr, size_t *stacksize)
40 {
41 pthread_attr_t attr;
42 int ret;
43 pthread_t me = pthread_self ();
44
45 if ((ret = pthread_getattr_np (me, &attr)))
46 {
47 printf ("%s: pthread_getattr_np failed: %s\n", id, strerror (ret));
48 return 1;
49 }
50
51 if ((ret = pthread_attr_getstack (&attr, stackaddr, stacksize)))
52 {
53 printf ("%s: pthread_attr_getstack returned error: %s\n", id,
54 strerror (ret));
55 return 1;
56 }
57
58 return 0;
59 }
60
61 /* Verify that the stack size returned by pthread_getattr_np is usable when
62 the returned value is subject to rlimit. */
63 static int
64 check_stack_top (void)
65 {
66 struct rlimit stack_limit;
67 void *stackaddr;
68 size_t stacksize = 0;
69 int ret;
70
71 puts ("Verifying that stack top is accessible");
72
73 ret = getrlimit (RLIMIT_STACK, &stack_limit);
74 if (ret)
75 {
76 perror ("getrlimit failed");
77 return 1;
78 }
79
80 if (get_self_pthread_attr ("check_stack_top", &stackaddr, &stacksize))
81 return 1;
82
83 /* Reduce the rlimit to a page less that what is currently being returned so
84 that we ensure that pthread_getattr_np uses rlimit. The figure is
85 intentionally unaligned so to verify that pthread_getattr_np returns an
86 aligned stacksize that correctly fits into the rlimit. We don't bother
87 about the case where the stack is limited by the vma below it and not by
88 the rlimit because the stacksize returned in that case is computed from
89 the end of that vma and is hence safe. */
90 stack_limit.rlim_cur = stacksize - 4095;
91 printf ("Adjusting RLIMIT_STACK to %zu\n", stack_limit.rlim_cur);
92 if ((ret = setrlimit (RLIMIT_STACK, &stack_limit)))
93 {
94 perror ("setrlimit failed");
95 return 1;
96 }
97
98 if (get_self_pthread_attr ("check_stack_top2", &stackaddr, &stacksize))
99 return 1;
100
101 printf ("Adjusted rlimit: stacksize=%zu, stackaddr=%p\n", stacksize,
102 stackaddr);
103 allocate_and_test (stackaddr);
104
105 puts ("Stack top tests done");
106
107 return 0;
108 }
109
110 /* TODO: Similar check for thread stacks once the thread stack sizes are
111 fixed. */
112 static int
113 do_test (void)
114 {
115 return check_stack_top ();
116 }
117
118
119 #define TEST_FUNCTION do_test ()
120 #include "../test-skeleton.c"