1 /* Some auxiliary stuff for defining an alternate stack.
2 Copyright (C) 2010 Eric Blake <eblake@redhat.com>
3 Copyright (C) 2010 Bruno Haible <bruno@clisp.org>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
10 This program 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
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19 #include <string.h> /* for memset */
26 # include <inttypes.h>
30 # define SIGSTKSZ 16384
33 /* glibc says: Users should use SIGSTKSZ as the size of user-supplied
34 buffers. We want to detect stack overflow of the alternate stack
35 in a nicer manner than just crashing, so we overallocate in
36 comparison to what we hand libsigsegv. Also, we intentionally hand
37 an unaligned pointer, to ensure the alternate stack still ends up
39 #define MYSTACK_CRUMPLE_ZONE 8192
40 char mystack_storage
[SIGSTKSZ
+ 2 * MYSTACK_CRUMPLE_ZONE
+ 31];
41 char *mystack
; /* SIGSTKSZ bytes in the middle of storage. */
44 prepare_alternate_stack (void)
46 memset (mystack_storage
, 's', sizeof mystack_storage
);
47 mystack
= (char *) ((uintptr_t) (mystack_storage
+ MYSTACK_CRUMPLE_ZONE
) | 31);
51 check_alternate_stack_no_overflow (void)
55 for (i
= MYSTACK_CRUMPLE_ZONE
; i
> 0; i
--)
56 if (*(mystack
- i
) != 's')
58 printf ("Alternate stack was exceeded by %u bytes!!\n", i
);
61 for (i
= MYSTACK_CRUMPLE_ZONE
; i
> 0; i
--)
62 if (*(mystack
+ SIGSTKSZ
- 1 + i
) != 's')
64 printf ("Alternate stack was exceeded by %u bytes!!\n", i
);