Add UNSUPPORTED check in elf/tst-pldd.
[glibc.git] / support / xsigstack.c
blob599c393402c61b0f305aaece8d85199082a0f041
1 /* sigaltstack wrappers.
2 Copyright (C) 2019 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #include <support/xsignal.h>
20 #include <support/support.h>
21 #include <support/xunistd.h>
22 #include <support/check.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/mman.h>
27 #include <sys/param.h> /* roundup, MAX */
29 #ifndef MAP_NORESERVE
30 # define MAP_NORESERVE 0
31 #endif
32 #ifndef MAP_STACK
33 # define MAP_STACK 0
34 #endif
36 /* The "cookie" returned by xalloc_sigstack points to one of these
37 structures. */
38 struct sigstack_desc
40 void *alloc_base; /* Base address of the complete allocation. */
41 size_t alloc_size; /* Size of the complete allocation. */
42 stack_t alt_stack; /* The address and size of the stack itself. */
43 stack_t old_stack; /* The previous signal stack. */
46 void *
47 xalloc_sigstack (size_t size)
49 size_t pagesize = sysconf (_SC_PAGESIZE);
50 if (pagesize == -1)
51 FAIL_EXIT1 ("sysconf (_SC_PAGESIZE): %m\n");
53 /* Always supply at least MINSIGSTKSZ space; passing 0 as size means
54 only that much space. No matter what the number is, round it up
55 to a whole number of pages. */
56 size_t stacksize = roundup (size + MINSIGSTKSZ, pagesize);
58 /* The guard bands need to be large enough to intercept offset
59 accesses from a stack address that might otherwise hit another
60 mapping. Make them at least twice as big as the stack itself, to
61 defend against an offset by the entire size of a large
62 stack-allocated array. The minimum is 1MiB, which is arbitrarily
63 chosen to be larger than any "typical" wild pointer offset.
64 Again, no matter what the number is, round it up to a whole
65 number of pages. */
66 size_t guardsize = roundup (MAX (2 * stacksize, 1024 * 1024), pagesize);
68 struct sigstack_desc *desc = xmalloc (sizeof (struct sigstack_desc));
69 desc->alloc_size = guardsize + stacksize + guardsize;
70 /* Use MAP_NORESERVE so that RAM will not be wasted on the guard
71 bands; touch all the pages of the actual stack before returning,
72 so we know they are allocated. */
73 desc->alloc_base = xmmap (0,
74 desc->alloc_size,
75 PROT_READ|PROT_WRITE,
76 MAP_PRIVATE|MAP_ANONYMOUS|MAP_NORESERVE|MAP_STACK,
77 -1);
79 xmprotect (desc->alloc_base, guardsize, PROT_NONE);
80 xmprotect (desc->alloc_base + guardsize + stacksize, guardsize, PROT_NONE);
81 memset (desc->alloc_base + guardsize, 0xA5, stacksize);
83 desc->alt_stack.ss_sp = desc->alloc_base + guardsize;
84 desc->alt_stack.ss_flags = 0;
85 desc->alt_stack.ss_size = stacksize;
87 if (sigaltstack (&desc->alt_stack, &desc->old_stack))
88 FAIL_EXIT1 ("sigaltstack (new stack: sp=%p, size=%zu, flags=%u): %m\n",
89 desc->alt_stack.ss_sp, desc->alt_stack.ss_size,
90 desc->alt_stack.ss_flags);
92 return desc;
95 void
96 xfree_sigstack (void *stack)
98 struct sigstack_desc *desc = stack;
100 if (sigaltstack (&desc->old_stack, 0))
101 FAIL_EXIT1 ("sigaltstack (restore old stack: sp=%p, size=%zu, flags=%u): "
102 "%m\n", desc->old_stack.ss_sp, desc->old_stack.ss_size,
103 desc->old_stack.ss_flags);
104 xmunmap (desc->alloc_base, desc->alloc_size);
105 free (desc);
108 void
109 xget_sigstack_location (const void *stack, unsigned char **addrp, size_t *sizep)
111 const struct sigstack_desc *desc = stack;
112 *addrp = desc->alt_stack.ss_sp;
113 *sizep = desc->alt_stack.ss_size;