Add installation information for the tarball users and from a git checkout.
[libsigsegv.git] / tests / efault2.c
blob6cda5009d175cc6638e08777dc826d25df119d76
1 /* Test that libsigsegv does not interfere with fault handling inside
2 system calls.
3 Copyright (C) 2009-2010 Eric Blake <ebb9@byu.net>
4 Copyright (C) 2021 Bruno Haible <bruno@clisp.org>
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <https://www.gnu.org/licenses/>. */
19 #ifndef _MSC_VER
20 # include <config.h>
21 #endif
23 #include "sigsegv.h"
25 #if HAVE_STACK_OVERFLOW_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-util.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 void
41 handler (int emergency, stackoverflow_context_t scp)
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 int
49 main ()
51 /* Test whether the OS handles some faults by itself. */
52 if (open (null_pointer, O_RDONLY) != -1 || errno != EFAULT)
54 fprintf (stderr, "EFAULT not detected alone.\n");
55 exit (1);
58 /* Prepare the storage for the alternate stack. */
59 prepare_alternate_stack ();
61 /* Install the stack overflow handler. */
62 if (stackoverflow_install_handler (&handler, mystack, SIGSTKSZ) < 0)
63 exit (2);
65 /* Test that the library does not interfere with OS faults. */
66 if (open (null_pointer, O_RDONLY) != -1 || errno != EFAULT)
68 fprintf (stderr, "EFAULT not detected with handler.\n");
69 exit (1);
72 /* Validate that the alternate stack did not overflow. */
73 check_alternate_stack_no_overflow ();
75 /* Test passed! */
76 printf ("Test passed.\n");
77 return 0;
80 #else
82 int
83 main ()
85 return 77;
88 #endif