summary refs log tree commit diff
path: root/fail.c
blob: a08253adcd5e4ebc03d002b6fb90c73089e59a4d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/* fail - crash in various possible ways */

#include <linux/seccomp.h>

#include <sys/prctl.h>
#include <sys/ptrace.h>
#include <sys/stat.h>
#include <sys/types.h>

#include <fcntl.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>

void
segfault()
{
	volatile int *nullp = 0;

	*nullp = 42;
}

// can lockup your machine
void
oom()
{
	long c = 0;
	static long last = 0;

	int fd;
	fd = open("/proc/self/oom_score_adj", O_WRONLY);
	write(fd, "1000\n", 5);
	close(fd);

	while (1) {
		long *m = malloc(4096*4096);
		m[0] = last;
		m[1] = c++;
		last = (long)m;
		write(1, ".", 1);
	}
}

void
recurse(char *n)
{
	char m[1024];
	recurse(m);
	if (n)
		m[0] = n[0] = 42;
}

void
abortme()
{
	abort();
}

void
killme()
{
	raise(SIGKILL);
}

void illegalins()
{
#if defined(__x86_64__) || defined(__i386__)
        __asm__ __volatile__( "ud1" : : : "memory" );
#elif defined(__arm__)
        __asm__ __volatile__(
	#ifndef __thumb__
		".word 0xe7f000f0"
	#else
		".short 0xdeff"
	#endif
                : : : "memory");
#elif defined(__aarch64__)
	__asm__ __volatile__( ".word 0x00800011" : : : "memory" );
#else
	#error implement illegalins for this architecture
#endif
}

void trap()
{
	__builtin_trap();
}

int zero = 0;
void divtrap()
{
	zero = 1/zero;
}

void
violate_seccomp()
{
	prctl(PR_SET_SECCOMP, SECCOMP_MODE_STRICT);
	chdir("/");
}

int
main(int argc, char *argv[])
{
	int c;

	while ((c = getopt(argc, argv, "123Oacdikrst")) != -1) {
        	switch (c) {
		case '1': exit(-1); break;
		case '2': exit(2); break;
		case '3': exit(111); break;
                case 'c': violate_seccomp(); break;
		case 'd': divtrap(); break;
		case 'i': illegalins(); break;
		case 't': trap(); break;
                case 'O': oom(); break;
                case 'a': abortme(); break;
                case 'k': killme(); break;
                case 'r': recurse(0); break;
                case 's': segfault(); break;                
		}
        }

	write(2, "Usage: fail [-123Oacdikrst]\n", 28);
	exit(1);
}