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
30void sighandler(int sig) {
31 printf("Happily ignoring signal %d\n", sig);
32}
33
34int main(int argc, char* argv[]) {
35 printf("crashtest: a return value of zero means success\n");
36
37 signal(SIGFPE, sighandler);
38 signal(SIGILL, sighandler);
39
40 // SIGFPE
41 printf("Testing SIGFPE...\n");
42 volatile int zero = 0;
43 int a = 1 / zero; // NOLINT(clang-analyzer-core.DivideZero)
44 float b = 1.0f / 0.0f;
45 double c = 1.0 / 0.0;
46 printf("Works.\n");
47
48 // SIGILL
49 printf("Testing SIGILL...\n");
50 char badops[] = {0xab, 0xcd, 0xef, 0x12};
51 void (*f)() = (void (*)())badops;
52 f();
53 printf("Works.\n");
54
55 printf("All signals handled.\n");
56
57 return 0;
58}