Avoid polluting cygwin namespace.
[libsigsegv/ericb.git] / tests / altstack.h
blobbba4818332c1ad0c0a66e6d92e6c92c9875cb7c7
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)
8 any later version.
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 */
21 /* Get uintptr_t. */
22 #if HAVE_STDINT_H
23 # include <stdint.h>
24 #endif
25 #if HAVE_INTTYPES_H
26 # include <inttypes.h>
27 #endif
29 #ifndef SIGSTKSZ
30 # define SIGSTKSZ 16384
31 #endif
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
38 aligned. */
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. */
43 static void
44 prepare_alternate_stack (void)
46 memset (mystack_storage, 's', sizeof mystack_storage);
47 mystack = (char *) ((uintptr_t) (mystack_storage + MYSTACK_CRUMPLE_ZONE) | 31);
50 static void
51 check_alternate_stack_no_overflow (void)
53 unsigned int i;
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);
59 exit (1);
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);
65 exit (1);