The Pedigree Project  0.1
crashtest/main.c
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 // crashtest: tests exception handling in the host POSIX subsystem
21 
22 #include <signal.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 
27 // Don't give a warning for division by zero
28 #pragma GCC diagnostic ignored "-Wdiv-by-zero"
29 
30 void sighandler(int sig)
31 {
32  printf("Happily ignoring signal %d\n", sig);
33 }
34 
35 int main(int argc, char *argv[])
36 {
37  printf("crashtest: a return value of zero means success\n");
38 
39  signal(SIGFPE, sighandler);
40  signal(SIGILL, sighandler);
41 
42  // SIGFPE
43  printf("Testing SIGFPE...\n");
44  int a = 1 / 0;
45  float b = 1.0f / 0.0f;
46  double c = 1.0 / 0.0;
47  printf("Works.\n");
48 
49  // SIGILL
50  printf("Testing SIGILL...\n");
51  char badops[] = {0xab, 0xcd, 0xef, 0x12};
52  void (*f)() = (void (*)()) badops;
53  f();
54  printf("Works.\n");
55 
56  printf("All signals handled.\n");
57 
58  return 0;
59 }