Avoid polluting cygwin namespace.
[libsigsegv/ericb.git] / tests / efault3.c
blobde842f4f40497239bacd06295b84e9f160e215d6
1 /* Test that libsigsegv does not interfere with fault handling inside
2 system calls.
3 Copyright (C) 2009-2010 Eric Blake <ebb9@byu.net>
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 #ifndef _MSC_VER
20 # include <config.h>
21 #endif
23 #include "sigsegv.h"
25 #if HAVE_STACK_OVERFLOW_RECOVERY && HAVE_SIGSEGV_RECOVERY && HAVE_EFAULT_SUPPORT
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <unistd.h>
33 #include "altstack.h"
35 /* A NULL pointer.
36 If we were to use a literal NULL, gcc would give a warning on glibc systems:
37 "warning: null argument where non-null required". */
38 const char *null_pointer = NULL;
40 static int
41 sigsegv_handler (void *fault_address, int serious)
43 /* This test is only enabled when ENABLE_EFAULT is true. If we get here,
44 the ENABLE_EFAULT handling in libsigsegv is incomplete. */
45 abort ();
48 static void
49 stackoverflow_handler (int emergency, stackoverflow_context_t scp)
51 /* This test is only enabled when ENABLE_EFAULT is true. If we get here,
52 the ENABLE_EFAULT handling in libsigsegv is incomplete. */
53 abort ();
56 int
57 main ()
59 /* Test whether the OS handles some faults by itself. */
60 if (open (null_pointer, O_RDONLY) != -1 || errno != EFAULT)
62 fprintf (stderr, "EFAULT not detected alone.\n");
63 exit (1);
66 /* Prepare the storage for the alternate stack. */
67 prepare_alternate_stack ();
69 /* Install the stack overflow handler. */
70 if (stackoverflow_install_handler (&stackoverflow_handler,
71 mystack, SIGSTKSZ)
72 < 0)
73 exit (2);
75 /* Install the SIGSEGV handler. */
76 if (sigsegv_install_handler (&sigsegv_handler) < 0)
77 exit (2);
79 /* Test that the library does not interfere with OS faults. */
80 if (open (null_pointer, O_RDONLY) != -1 || errno != EFAULT)
82 fprintf (stderr, "EFAULT not detected with handler.\n");
83 exit (1);
86 /* Validate that the alternate stack did not overflow. */
87 check_alternate_stack_no_overflow ();
89 /* Test passed! */
90 printf ("Test passed.\n");
91 return 0;
94 #else
96 int
97 main ()
99 return 77;
102 #endif