1 /* Some auxiliary stuff for defining an alternate stack.
2 Copyright (C) 2010-2024 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Eric Blake and Bruno Haible. */
19 #include <stdint.h> /* uintptr_t */
20 #include <string.h> /* for memset */
22 #define MYSTACK_SIZE (1 << 24)
24 /* glibc says: Users should use SIGSTKSZ as the size of user-supplied
25 buffers. We want to detect stack overflow of the alternate stack
26 in a nicer manner than just crashing, so we overallocate in
27 comparison to what we hand libsigsegv. Also, we intentionally hand
28 an unaligned pointer, to ensure the alternate stack still ends up
30 #define MYSTACK_CRUMPLE_ZONE 8192
31 static char mystack_storage
[MYSTACK_SIZE
+ 2 * MYSTACK_CRUMPLE_ZONE
+ 31];
32 static char *mystack
; /* MYSTACK_SIZE bytes in the middle of storage. */
35 prepare_alternate_stack (void)
38 if (MYSTACK_SIZE
< SIGSTKSZ
)
40 size_t size
= SIGSTKSZ
;
41 printf ("SIGSTKSZ=%zu exceeds MYSTACK_SIZE=%d\n", size
, MYSTACK_SIZE
);
45 memset (mystack_storage
, 's', sizeof mystack_storage
);
46 mystack
= (char *) ((uintptr_t) (mystack_storage
+ MYSTACK_CRUMPLE_ZONE
) | 31);
50 check_alternate_stack_no_overflow (void)
54 for (i
= MYSTACK_CRUMPLE_ZONE
; i
> 0; i
--)
55 if (*(mystack
- i
) != 's')
57 printf ("Alternate stack was exceeded by %u bytes!!\n", i
);
60 for (i
= MYSTACK_CRUMPLE_ZONE
; i
> 0; i
--)
61 if (*(mystack
+ MYSTACK_SIZE
- 1 + i
) != 's')
63 printf ("Alternate stack was exceeded by %u bytes!!\n", i
);