blob: d0c39127f499dbf0c46dcba2d592c6e3ef647756 [file] [log] [blame]
Elliott Hughesda6b2e22014-04-23 14:57:32 -07001#include <assert.h>
2#include <errno.h>
3#include <pthread.h>
4#include <sched.h>
5#include <signal.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08006#include <stdio.h>
7#include <stdlib.h>
8#include <string.h>
Mark Salyzynf1a8dfa2014-04-30 09:24:08 -07009#include <sys/cdefs.h>
Brigid Smith8606eaa2014-07-07 12:33:50 -070010#include <sys/mman.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080011#include <sys/ptrace.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080012#include <sys/socket.h>
Elliott Hughesda6b2e22014-04-23 14:57:32 -070013#include <sys/wait.h>
14#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080015
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080016#include <cutils/sockets.h>
Mark Salyzynf1a8dfa2014-04-30 09:24:08 -070017#include <log/log.h>
18
19#ifndef __unused
20#define __unused __attribute__((__unused__))
21#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080022
Elliott Hughes3808c4e2013-04-23 17:14:56 -070023extern const char* __progname;
24
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080025void crash1(void);
26void crashnostack(void);
Elliott Hughes6f40caf2013-06-12 14:04:34 -070027static int do_action(const char* arg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080028
Elliott Hughesda6b2e22014-04-23 14:57:32 -070029static void maybe_abort() {
30 if (time(0) != 42) {
Elliott Hughes6f40caf2013-06-12 14:04:34 -070031 abort();
32 }
33}
34
Mark Salyzynf1a8dfa2014-04-30 09:24:08 -070035static int smash_stack(int i __unused) {
Elliott Hughesdf4200e2013-02-14 14:41:57 -080036 printf("crasher: deliberately corrupting stack...\n");
37 // Unless there's a "big enough" buffer on the stack, gcc
38 // doesn't bother inserting checks.
39 char buf[8];
Elliott Hughesb1be27e2013-07-15 17:19:02 -070040 // If we don't write something relatively unpredictable
Elliott Hughesdf4200e2013-02-14 14:41:57 -080041 // into the buffer and then do something with it, gcc
42 // optimizes everything away and just returns a constant.
43 *(int*)(&buf[7]) = (uintptr_t) &buf[0];
44 return *(int*)(&buf[0]);
45}
46
Elliott Hughesb1be27e2013-07-15 17:19:02 -070047static void* global = 0; // So GCC doesn't optimize the tail recursion out of overflow_stack.
48
Elliott Hughes6f40caf2013-06-12 14:04:34 -070049__attribute__((noinline)) static void overflow_stack(void* p) {
Elliott Hughes3808c4e2013-04-23 17:14:56 -070050 void* buf[1];
51 buf[0] = p;
Elliott Hughesb1be27e2013-07-15 17:19:02 -070052 global = buf;
Elliott Hughes3808c4e2013-04-23 17:14:56 -070053 overflow_stack(&buf);
54}
55
Elliott Hughes6f40caf2013-06-12 14:04:34 -070056static void *noisy(void *x)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080057{
Elliott Hughes5d9fe772014-02-05 17:50:35 -080058 char c = (uintptr_t) x;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080059 for(;;) {
60 usleep(250*1000);
61 write(2, &c, 1);
Chih-Hung Hsieha1ff4752014-10-23 16:50:51 -070062 if(c == 'C') *((volatile unsigned*) 0) = 42;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080063 }
Elliott Hughes5d9fe772014-02-05 17:50:35 -080064 return NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080065}
66
Elliott Hughes6f40caf2013-06-12 14:04:34 -070067static int ctest()
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080068{
69 pthread_t thr;
70 pthread_attr_t attr;
71 pthread_attr_init(&attr);
72 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
73 pthread_create(&thr, &attr, noisy, (void*) 'A');
74 pthread_create(&thr, &attr, noisy, (void*) 'B');
75 pthread_create(&thr, &attr, noisy, (void*) 'C');
76 for(;;) ;
77 return 0;
78}
79
Elliott Hughesaa421302012-12-10 14:15:42 -080080static void* thread_callback(void* raw_arg)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080081{
Elliott Hughes5d9fe772014-02-05 17:50:35 -080082 return (void*) (uintptr_t) do_action((const char*) raw_arg);
Elliott Hughesaa421302012-12-10 14:15:42 -080083}
84
Elliott Hughes6f40caf2013-06-12 14:04:34 -070085static int do_action_on_thread(const char* arg)
Elliott Hughesaa421302012-12-10 14:15:42 -080086{
87 pthread_t t;
88 pthread_create(&t, NULL, thread_callback, (void*) arg);
89 void* result = NULL;
90 pthread_join(t, &result);
Elliott Hughes5d9fe772014-02-05 17:50:35 -080091 return (int) (uintptr_t) result;
Elliott Hughesaa421302012-12-10 14:15:42 -080092}
93
Elliott Hughes6f40caf2013-06-12 14:04:34 -070094__attribute__((noinline)) static int crash3(int a) {
95 *((int*) 0xdead) = a;
96 return a*4;
Pavel Chupinaf2cb362013-03-08 13:17:35 +040097}
98
Elliott Hughes6f40caf2013-06-12 14:04:34 -070099__attribute__((noinline)) static int crash2(int a) {
100 a = crash3(a) + 2;
101 return a*3;
Pavel Chupinaf2cb362013-03-08 13:17:35 +0400102}
103
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700104__attribute__((noinline)) static int crash(int a) {
105 a = crash2(a) + 1;
106 return a*2;
Pavel Chupinaf2cb362013-03-08 13:17:35 +0400107}
108
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700109static void abuse_heap() {
110 char buf[16];
111 free((void*) buf); // GCC is smart enough to warn about this, but we're doing it deliberately.
112}
113
Brigid Smith7b2078e2014-06-17 14:55:47 -0700114static void sigsegv_non_null() {
115 int* a = (int *)(&do_action);
116 *a = 42;
117}
118
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700119static int do_action(const char* arg)
Elliott Hughesaa421302012-12-10 14:15:42 -0800120{
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700121 fprintf(stderr,"crasher: init pid=%d tid=%d\n", getpid(), gettid());
122
123 if (!strncmp(arg, "thread-", strlen("thread-"))) {
Elliott Hughesaa421302012-12-10 14:15:42 -0800124 return do_action_on_thread(arg + strlen("thread-"));
Brigid Smith7b2078e2014-06-17 14:55:47 -0700125 } else if (!strcmp(arg, "SIGSEGV-non-null")) {
126 sigsegv_non_null();
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700127 } else if (!strcmp(arg, "smash-stack")) {
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700128 return smash_stack(42);
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700129 } else if (!strcmp(arg, "stack-overflow")) {
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700130 overflow_stack(NULL);
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700131 } else if (!strcmp(arg, "nostack")) {
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700132 crashnostack();
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700133 } else if (!strcmp(arg, "ctest")) {
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700134 return ctest();
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700135 } else if (!strcmp(arg, "exit")) {
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700136 exit(1);
Elliott Hughes855fcc32014-04-25 16:05:34 -0700137 } else if (!strcmp(arg, "crash") || !strcmp(arg, "SIGSEGV")) {
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700138 return crash(42);
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700139 } else if (!strcmp(arg, "abort")) {
140 maybe_abort();
141 } else if (!strcmp(arg, "assert")) {
142 __assert("some_file.c", 123, "false");
143 } else if (!strcmp(arg, "assert2")) {
Elliott Hughes3ecc4212014-07-15 11:38:47 -0700144 __assert2("some_file.c", 123, "some_function", "false");
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700145 } else if (!strcmp(arg, "LOG_ALWAYS_FATAL")) {
146 LOG_ALWAYS_FATAL("hello %s", "world");
147 } else if (!strcmp(arg, "LOG_ALWAYS_FATAL_IF")) {
148 LOG_ALWAYS_FATAL_IF(true, "hello %s", "world");
Elliott Hughes3ecc4212014-07-15 11:38:47 -0700149 } else if (!strcmp(arg, "SIGFPE")) {
150 raise(SIGFPE);
151 return EXIT_SUCCESS;
Elliott Hughes855fcc32014-04-25 16:05:34 -0700152 } else if (!strcmp(arg, "SIGPIPE")) {
153 int pipe_fds[2];
154 pipe(pipe_fds);
155 close(pipe_fds[0]);
156 write(pipe_fds[1], "oops", 4);
157 return EXIT_SUCCESS;
Elliott Hughes7e35ae82014-05-16 17:05:19 -0700158 } else if (!strcmp(arg, "SIGTRAP")) {
159 raise(SIGTRAP);
160 return EXIT_SUCCESS;
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700161 } else if (!strcmp(arg, "heap-usage")) {
162 abuse_heap();
Brigid Smith8606eaa2014-07-07 12:33:50 -0700163 } else if (!strcmp(arg, "SIGSEGV-unmapped")) {
164 char* map = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
165 munmap(map, sizeof(int));
166 map[0] = '8';
Elliott Hughesaa421302012-12-10 14:15:42 -0800167 }
168
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700169 fprintf(stderr, "%s OP\n", __progname);
170 fprintf(stderr, "where OP is:\n");
Elliott Hughes855fcc32014-04-25 16:05:34 -0700171 fprintf(stderr, " smash-stack overwrite a stack-guard canary\n");
172 fprintf(stderr, " stack-overflow recurse until the stack overflows\n");
173 fprintf(stderr, " heap-corruption cause a libc abort by corrupting the heap\n");
174 fprintf(stderr, " heap-usage cause a libc abort by abusing a heap function\n");
175 fprintf(stderr, " nostack crash with a NULL stack pointer\n");
176 fprintf(stderr, " ctest (obsoleted by thread-crash?)\n");
177 fprintf(stderr, " exit call exit(1)\n");
178 fprintf(stderr, " abort call abort()\n");
179 fprintf(stderr, " assert call assert() without a function\n");
180 fprintf(stderr, " assert2 call assert() with a function\n");
181 fprintf(stderr, " LOG_ALWAYS_FATAL call LOG_ALWAYS_FATAL\n");
182 fprintf(stderr, " LOG_ALWAYS_FATAL_IF call LOG_ALWAYS_FATAL\n");
Elliott Hughes3ecc4212014-07-15 11:38:47 -0700183 fprintf(stderr, " SIGFPE cause a SIGFPE\n");
Elliott Hughes855fcc32014-04-25 16:05:34 -0700184 fprintf(stderr, " SIGPIPE cause a SIGPIPE\n");
Brigid Smith7b2078e2014-06-17 14:55:47 -0700185 fprintf(stderr, " SIGSEGV cause a SIGSEGV at address 0x0 (synonym: crash)\n");
186 fprintf(stderr, " SIGSEGV-non-null cause a SIGSEGV at a non-zero address\n");
Brigid Smith8606eaa2014-07-07 12:33:50 -0700187 fprintf(stderr, " SIGSEGV-unmapped mmap/munmap a region of memory and then attempt to access it\n");
Elliott Hughes7e35ae82014-05-16 17:05:19 -0700188 fprintf(stderr, " SIGTRAP cause a SIGTRAP\n");
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700189 fprintf(stderr, "prefix any of the above with 'thread-' to not run\n");
190 fprintf(stderr, "on the process' main thread.\n");
191 return EXIT_SUCCESS;
Elliott Hughesaa421302012-12-10 14:15:42 -0800192}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800193
Elliott Hughesaa421302012-12-10 14:15:42 -0800194int main(int argc, char **argv)
195{
196 fprintf(stderr,"crasher: built at " __TIME__ "!@\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800197
198 if(argc > 1) {
Elliott Hughesaa421302012-12-10 14:15:42 -0800199 return do_action(argv[1]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800200 } else {
201 crash1();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800202 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800203
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700204 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800205}