]>
git.ipfire.org Git - thirdparty/glibc.git/blob - nptl/tst-pthread-getattr.c
1 /* Make sure that the stackaddr returned by pthread_getattr_np is
4 Copyright (C) 2012 Free Software Foundation, Inc.
5 This file is part of the GNU C Library.
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.
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.
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/>. */
23 #include <sys/resource.h>
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. */
30 allocate_and_test (void *stackaddr
)
33 /* FIXME: The difference will be negative for _STACK_GROWSUP. */
34 mem
= alloca ((size_t) (mem
- stackaddr
));
39 get_self_pthread_attr (const char *id
, void **stackaddr
, size_t *stacksize
)
43 pthread_t me
= pthread_self ();
45 if ((ret
= pthread_getattr_np (me
, &attr
)))
47 printf ("%s: pthread_getattr_np failed: %s\n", id
, strerror (ret
));
51 if ((ret
= pthread_attr_getstack (&attr
, stackaddr
, stacksize
)))
53 printf ("%s: pthread_attr_getstack returned error: %s\n", id
,
61 /* Verify that the stack size returned by pthread_getattr_np is usable when
62 the returned value is subject to rlimit. */
64 check_stack_top (void)
66 struct rlimit stack_limit
;
71 puts ("Verifying that stack top is accessible");
73 ret
= getrlimit (RLIMIT_STACK
, &stack_limit
);
76 perror ("getrlimit failed");
80 if (get_self_pthread_attr ("check_stack_top", &stackaddr
, &stacksize
))
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
)))
94 perror ("setrlimit failed");
98 if (get_self_pthread_attr ("check_stack_top2", &stackaddr
, &stacksize
))
101 printf ("Adjusted rlimit: stacksize=%zu, stackaddr=%p\n", stacksize
,
103 allocate_and_test (stackaddr
);
105 puts ("Stack top tests done");
110 /* TODO: Similar check for thread stacks once the thread stack sizes are
115 return check_stack_top ();
119 #define TEST_FUNCTION do_test ()
120 #include "../test-skeleton.c"