xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame^] | 1 | /* Make sure that the stackaddr returned by pthread_getattr_np is |
| 2 | reachable. |
| 3 | |
| 4 | Copyright (C) 2012-2016 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 <sys/param.h> |
| 25 | #include <pthread.h> |
| 26 | #include <alloca.h> |
| 27 | #include <assert.h> |
| 28 | #include <unistd.h> |
| 29 | #include <inttypes.h> |
| 30 | |
| 31 | /* There is an obscure bug in the kernel due to which RLIMIT_STACK is sometimes |
| 32 | returned as unlimited when it is not, which may cause this test to fail. |
| 33 | There is also the other case where RLIMIT_STACK is intentionally set as |
| 34 | unlimited or very high, which may result in a vma that is too large and again |
| 35 | results in a test case failure. To avoid these problems, we cap the stack |
| 36 | size to one less than 8M. See the following mailing list threads for more |
| 37 | information about this problem: |
| 38 | <http://sourceware.org/ml/libc-alpha/2012-06/msg00599.html> |
| 39 | <http://sourceware.org/ml/libc-alpha/2012-06/msg00713.html>. */ |
| 40 | #define MAX_STACK_SIZE (8192 * 1024 - 1) |
| 41 | |
| 42 | static size_t pagesize; |
| 43 | |
| 44 | /* Check if the page in which TARGET lies is accessible. This will segfault |
| 45 | if it fails. */ |
| 46 | static volatile char * |
| 47 | allocate_and_test (char *target) |
| 48 | { |
| 49 | volatile char *mem = (char *) &mem; |
| 50 | /* FIXME: mem >= target for _STACK_GROWSUP. */ |
| 51 | mem = alloca ((size_t) (mem - target)); |
| 52 | |
| 53 | *mem = 42; |
| 54 | return mem; |
| 55 | } |
| 56 | |
| 57 | static int |
| 58 | get_self_pthread_attr (const char *id, void **stackaddr, size_t *stacksize) |
| 59 | { |
| 60 | pthread_attr_t attr; |
| 61 | int ret; |
| 62 | pthread_t me = pthread_self (); |
| 63 | |
| 64 | if ((ret = pthread_getattr_np (me, &attr)) < 0) |
| 65 | { |
| 66 | printf ("%s: pthread_getattr_np failed: %s\n", id, strerror (ret)); |
| 67 | return 1; |
| 68 | } |
| 69 | |
| 70 | if ((ret = pthread_attr_getstack (&attr, stackaddr, stacksize)) < 0) |
| 71 | { |
| 72 | printf ("%s: pthread_attr_getstack returned error: %s\n", id, |
| 73 | strerror (ret)); |
| 74 | return 1; |
| 75 | } |
| 76 | |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | /* Verify that the stack size returned by pthread_getattr_np is usable when |
| 81 | the returned value is subject to rlimit. */ |
| 82 | static int |
| 83 | check_stack_top (void) |
| 84 | { |
| 85 | struct rlimit stack_limit; |
| 86 | void *stackaddr; |
| 87 | volatile void *mem; |
| 88 | size_t stacksize = 0; |
| 89 | int ret; |
| 90 | uintptr_t pagemask = ~(pagesize - 1); |
| 91 | |
| 92 | puts ("Verifying that stack top is accessible"); |
| 93 | |
| 94 | ret = getrlimit (RLIMIT_STACK, &stack_limit); |
| 95 | if (ret) |
| 96 | { |
| 97 | perror ("getrlimit failed"); |
| 98 | return 1; |
| 99 | } |
| 100 | |
| 101 | printf ("current rlimit_stack is %zu\n", (size_t) stack_limit.rlim_cur); |
| 102 | |
| 103 | if (get_self_pthread_attr ("check_stack_top", &stackaddr, &stacksize)) |
| 104 | return 1; |
| 105 | |
| 106 | /* Reduce the rlimit to a page less that what is currently being returned |
| 107 | (subject to a maximum of MAX_STACK_SIZE) so that we ensure that |
| 108 | pthread_getattr_np uses rlimit. The figure is intentionally unaligned so |
| 109 | to verify that pthread_getattr_np returns an aligned stacksize that |
| 110 | correctly fits into the rlimit. We don't bother about the case where the |
| 111 | stack is limited by the vma below it and not by the rlimit because the |
| 112 | stacksize returned in that case is computed from the end of that vma and is |
| 113 | hence safe. */ |
| 114 | stack_limit.rlim_cur = MIN (stacksize - pagesize + 1, MAX_STACK_SIZE); |
| 115 | printf ("Adjusting RLIMIT_STACK to %zu\n", (size_t) stack_limit.rlim_cur); |
| 116 | if ((ret = setrlimit (RLIMIT_STACK, &stack_limit)) < 0) |
| 117 | { |
| 118 | perror ("setrlimit failed"); |
| 119 | return 1; |
| 120 | } |
| 121 | |
| 122 | if (get_self_pthread_attr ("check_stack_top2", &stackaddr, &stacksize)) |
| 123 | return 1; |
| 124 | |
| 125 | printf ("Adjusted rlimit: stacksize=%zu, stackaddr=%p\n", stacksize, |
| 126 | stackaddr); |
| 127 | |
| 128 | /* A lot of targets tend to write stuff on top of the user stack during |
| 129 | context switches, so we cannot possibly safely go up to the very top of |
| 130 | stack and test access there. It is however sufficient to simply check if |
| 131 | the top page is accessible, so we target our access halfway up the top |
| 132 | page. Thanks Chris Metcalf for this idea. */ |
| 133 | mem = allocate_and_test (stackaddr + pagesize / 2); |
| 134 | |
| 135 | /* Before we celebrate, make sure we actually did test the same page. */ |
| 136 | if (((uintptr_t) stackaddr & pagemask) != ((uintptr_t) mem & pagemask)) |
| 137 | { |
| 138 | printf ("We successfully wrote into the wrong page.\n" |
| 139 | "Expected %#" PRIxPTR ", but got %#" PRIxPTR "\n", |
| 140 | (uintptr_t) stackaddr & pagemask, (uintptr_t) mem & pagemask); |
| 141 | |
| 142 | return 1; |
| 143 | } |
| 144 | |
| 145 | puts ("Stack top tests done"); |
| 146 | |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | /* TODO: Similar check for thread stacks once the thread stack sizes are |
| 151 | fixed. */ |
| 152 | static int |
| 153 | do_test (void) |
| 154 | { |
| 155 | pagesize = sysconf (_SC_PAGESIZE); |
| 156 | return check_stack_top (); |
| 157 | } |
| 158 | |
| 159 | |
| 160 | #define TEST_FUNCTION do_test () |
| 161 | #include "../test-skeleton.c" |