Elliott Hughes | da6b2e2 | 2014-04-23 14:57:32 -0700 | [diff] [blame] | 1 | #include <assert.h> |
| 2 | #include <errno.h> |
| 3 | #include <pthread.h> |
| 4 | #include <sched.h> |
| 5 | #include <signal.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 6 | #include <stdio.h> |
| 7 | #include <stdlib.h> |
| 8 | #include <string.h> |
Mark Salyzyn | f1a8dfa | 2014-04-30 09:24:08 -0700 | [diff] [blame] | 9 | #include <sys/cdefs.h> |
Brigid Smith | 8606eaa | 2014-07-07 12:33:50 -0700 | [diff] [blame] | 10 | #include <sys/mman.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 11 | #include <sys/ptrace.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 12 | #include <sys/socket.h> |
Elliott Hughes | da6b2e2 | 2014-04-23 14:57:32 -0700 | [diff] [blame] | 13 | #include <sys/wait.h> |
| 14 | #include <unistd.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 15 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 16 | #include <cutils/sockets.h> |
Mark Salyzyn | f1a8dfa | 2014-04-30 09:24:08 -0700 | [diff] [blame] | 17 | #include <log/log.h> |
| 18 | |
| 19 | #ifndef __unused |
| 20 | #define __unused __attribute__((__unused__)) |
| 21 | #endif |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 22 | |
Elliott Hughes | 3808c4e | 2013-04-23 17:14:56 -0700 | [diff] [blame] | 23 | extern const char* __progname; |
| 24 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 25 | void crash1(void); |
| 26 | void crashnostack(void); |
Elliott Hughes | 6f40caf | 2013-06-12 14:04:34 -0700 | [diff] [blame] | 27 | static int do_action(const char* arg); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 28 | |
Elliott Hughes | da6b2e2 | 2014-04-23 14:57:32 -0700 | [diff] [blame] | 29 | static void maybe_abort() { |
| 30 | if (time(0) != 42) { |
Elliott Hughes | 6f40caf | 2013-06-12 14:04:34 -0700 | [diff] [blame] | 31 | abort(); |
| 32 | } |
| 33 | } |
| 34 | |
Mark Salyzyn | f1a8dfa | 2014-04-30 09:24:08 -0700 | [diff] [blame] | 35 | static int smash_stack(int i __unused) { |
Elliott Hughes | df4200e | 2013-02-14 14:41:57 -0800 | [diff] [blame] | 36 | 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 Hughes | b1be27e | 2013-07-15 17:19:02 -0700 | [diff] [blame] | 40 | // If we don't write something relatively unpredictable |
Elliott Hughes | df4200e | 2013-02-14 14:41:57 -0800 | [diff] [blame] | 41 | // 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 Hughes | b1be27e | 2013-07-15 17:19:02 -0700 | [diff] [blame] | 47 | static void* global = 0; // So GCC doesn't optimize the tail recursion out of overflow_stack. |
| 48 | |
Elliott Hughes | 6f40caf | 2013-06-12 14:04:34 -0700 | [diff] [blame] | 49 | __attribute__((noinline)) static void overflow_stack(void* p) { |
Elliott Hughes | 3808c4e | 2013-04-23 17:14:56 -0700 | [diff] [blame] | 50 | void* buf[1]; |
| 51 | buf[0] = p; |
Elliott Hughes | b1be27e | 2013-07-15 17:19:02 -0700 | [diff] [blame] | 52 | global = buf; |
Elliott Hughes | 3808c4e | 2013-04-23 17:14:56 -0700 | [diff] [blame] | 53 | overflow_stack(&buf); |
| 54 | } |
| 55 | |
Elliott Hughes | 6f40caf | 2013-06-12 14:04:34 -0700 | [diff] [blame] | 56 | static void *noisy(void *x) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 57 | { |
Elliott Hughes | 5d9fe77 | 2014-02-05 17:50:35 -0800 | [diff] [blame] | 58 | char c = (uintptr_t) x; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 59 | for(;;) { |
| 60 | usleep(250*1000); |
| 61 | write(2, &c, 1); |
Chih-Hung Hsieh | a1ff475 | 2014-10-23 16:50:51 -0700 | [diff] [blame^] | 62 | if(c == 'C') *((volatile unsigned*) 0) = 42; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 63 | } |
Elliott Hughes | 5d9fe77 | 2014-02-05 17:50:35 -0800 | [diff] [blame] | 64 | return NULL; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 65 | } |
| 66 | |
Elliott Hughes | 6f40caf | 2013-06-12 14:04:34 -0700 | [diff] [blame] | 67 | static int ctest() |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 68 | { |
| 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 Hughes | aa42130 | 2012-12-10 14:15:42 -0800 | [diff] [blame] | 80 | static void* thread_callback(void* raw_arg) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 81 | { |
Elliott Hughes | 5d9fe77 | 2014-02-05 17:50:35 -0800 | [diff] [blame] | 82 | return (void*) (uintptr_t) do_action((const char*) raw_arg); |
Elliott Hughes | aa42130 | 2012-12-10 14:15:42 -0800 | [diff] [blame] | 83 | } |
| 84 | |
Elliott Hughes | 6f40caf | 2013-06-12 14:04:34 -0700 | [diff] [blame] | 85 | static int do_action_on_thread(const char* arg) |
Elliott Hughes | aa42130 | 2012-12-10 14:15:42 -0800 | [diff] [blame] | 86 | { |
| 87 | pthread_t t; |
| 88 | pthread_create(&t, NULL, thread_callback, (void*) arg); |
| 89 | void* result = NULL; |
| 90 | pthread_join(t, &result); |
Elliott Hughes | 5d9fe77 | 2014-02-05 17:50:35 -0800 | [diff] [blame] | 91 | return (int) (uintptr_t) result; |
Elliott Hughes | aa42130 | 2012-12-10 14:15:42 -0800 | [diff] [blame] | 92 | } |
| 93 | |
Elliott Hughes | 6f40caf | 2013-06-12 14:04:34 -0700 | [diff] [blame] | 94 | __attribute__((noinline)) static int crash3(int a) { |
| 95 | *((int*) 0xdead) = a; |
| 96 | return a*4; |
Pavel Chupin | af2cb36 | 2013-03-08 13:17:35 +0400 | [diff] [blame] | 97 | } |
| 98 | |
Elliott Hughes | 6f40caf | 2013-06-12 14:04:34 -0700 | [diff] [blame] | 99 | __attribute__((noinline)) static int crash2(int a) { |
| 100 | a = crash3(a) + 2; |
| 101 | return a*3; |
Pavel Chupin | af2cb36 | 2013-03-08 13:17:35 +0400 | [diff] [blame] | 102 | } |
| 103 | |
Elliott Hughes | 6f40caf | 2013-06-12 14:04:34 -0700 | [diff] [blame] | 104 | __attribute__((noinline)) static int crash(int a) { |
| 105 | a = crash2(a) + 1; |
| 106 | return a*2; |
Pavel Chupin | af2cb36 | 2013-03-08 13:17:35 +0400 | [diff] [blame] | 107 | } |
| 108 | |
Elliott Hughes | 6f40caf | 2013-06-12 14:04:34 -0700 | [diff] [blame] | 109 | static 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 Smith | 7b2078e | 2014-06-17 14:55:47 -0700 | [diff] [blame] | 114 | static void sigsegv_non_null() { |
| 115 | int* a = (int *)(&do_action); |
| 116 | *a = 42; |
| 117 | } |
| 118 | |
Elliott Hughes | 6f40caf | 2013-06-12 14:04:34 -0700 | [diff] [blame] | 119 | static int do_action(const char* arg) |
Elliott Hughes | aa42130 | 2012-12-10 14:15:42 -0800 | [diff] [blame] | 120 | { |
Elliott Hughes | 3808c4e | 2013-04-23 17:14:56 -0700 | [diff] [blame] | 121 | fprintf(stderr,"crasher: init pid=%d tid=%d\n", getpid(), gettid()); |
| 122 | |
| 123 | if (!strncmp(arg, "thread-", strlen("thread-"))) { |
Elliott Hughes | aa42130 | 2012-12-10 14:15:42 -0800 | [diff] [blame] | 124 | return do_action_on_thread(arg + strlen("thread-")); |
Brigid Smith | 7b2078e | 2014-06-17 14:55:47 -0700 | [diff] [blame] | 125 | } else if (!strcmp(arg, "SIGSEGV-non-null")) { |
| 126 | sigsegv_non_null(); |
Elliott Hughes | da6b2e2 | 2014-04-23 14:57:32 -0700 | [diff] [blame] | 127 | } else if (!strcmp(arg, "smash-stack")) { |
Elliott Hughes | 3808c4e | 2013-04-23 17:14:56 -0700 | [diff] [blame] | 128 | return smash_stack(42); |
Elliott Hughes | da6b2e2 | 2014-04-23 14:57:32 -0700 | [diff] [blame] | 129 | } else if (!strcmp(arg, "stack-overflow")) { |
Elliott Hughes | 3808c4e | 2013-04-23 17:14:56 -0700 | [diff] [blame] | 130 | overflow_stack(NULL); |
Elliott Hughes | da6b2e2 | 2014-04-23 14:57:32 -0700 | [diff] [blame] | 131 | } else if (!strcmp(arg, "nostack")) { |
Elliott Hughes | 3808c4e | 2013-04-23 17:14:56 -0700 | [diff] [blame] | 132 | crashnostack(); |
Elliott Hughes | da6b2e2 | 2014-04-23 14:57:32 -0700 | [diff] [blame] | 133 | } else if (!strcmp(arg, "ctest")) { |
Elliott Hughes | 3808c4e | 2013-04-23 17:14:56 -0700 | [diff] [blame] | 134 | return ctest(); |
Elliott Hughes | da6b2e2 | 2014-04-23 14:57:32 -0700 | [diff] [blame] | 135 | } else if (!strcmp(arg, "exit")) { |
Elliott Hughes | 3808c4e | 2013-04-23 17:14:56 -0700 | [diff] [blame] | 136 | exit(1); |
Elliott Hughes | 855fcc3 | 2014-04-25 16:05:34 -0700 | [diff] [blame] | 137 | } else if (!strcmp(arg, "crash") || !strcmp(arg, "SIGSEGV")) { |
Elliott Hughes | 3808c4e | 2013-04-23 17:14:56 -0700 | [diff] [blame] | 138 | return crash(42); |
Elliott Hughes | da6b2e2 | 2014-04-23 14:57:32 -0700 | [diff] [blame] | 139 | } 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 Hughes | 3ecc421 | 2014-07-15 11:38:47 -0700 | [diff] [blame] | 144 | __assert2("some_file.c", 123, "some_function", "false"); |
Elliott Hughes | da6b2e2 | 2014-04-23 14:57:32 -0700 | [diff] [blame] | 145 | } 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 Hughes | 3ecc421 | 2014-07-15 11:38:47 -0700 | [diff] [blame] | 149 | } else if (!strcmp(arg, "SIGFPE")) { |
| 150 | raise(SIGFPE); |
| 151 | return EXIT_SUCCESS; |
Elliott Hughes | 855fcc3 | 2014-04-25 16:05:34 -0700 | [diff] [blame] | 152 | } 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 Hughes | 7e35ae8 | 2014-05-16 17:05:19 -0700 | [diff] [blame] | 158 | } else if (!strcmp(arg, "SIGTRAP")) { |
| 159 | raise(SIGTRAP); |
| 160 | return EXIT_SUCCESS; |
Elliott Hughes | 6f40caf | 2013-06-12 14:04:34 -0700 | [diff] [blame] | 161 | } else if (!strcmp(arg, "heap-usage")) { |
| 162 | abuse_heap(); |
Brigid Smith | 8606eaa | 2014-07-07 12:33:50 -0700 | [diff] [blame] | 163 | } 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 Hughes | aa42130 | 2012-12-10 14:15:42 -0800 | [diff] [blame] | 167 | } |
| 168 | |
Elliott Hughes | 3808c4e | 2013-04-23 17:14:56 -0700 | [diff] [blame] | 169 | fprintf(stderr, "%s OP\n", __progname); |
| 170 | fprintf(stderr, "where OP is:\n"); |
Elliott Hughes | 855fcc3 | 2014-04-25 16:05:34 -0700 | [diff] [blame] | 171 | 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 Hughes | 3ecc421 | 2014-07-15 11:38:47 -0700 | [diff] [blame] | 183 | fprintf(stderr, " SIGFPE cause a SIGFPE\n"); |
Elliott Hughes | 855fcc3 | 2014-04-25 16:05:34 -0700 | [diff] [blame] | 184 | fprintf(stderr, " SIGPIPE cause a SIGPIPE\n"); |
Brigid Smith | 7b2078e | 2014-06-17 14:55:47 -0700 | [diff] [blame] | 185 | 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 Smith | 8606eaa | 2014-07-07 12:33:50 -0700 | [diff] [blame] | 187 | fprintf(stderr, " SIGSEGV-unmapped mmap/munmap a region of memory and then attempt to access it\n"); |
Elliott Hughes | 7e35ae8 | 2014-05-16 17:05:19 -0700 | [diff] [blame] | 188 | fprintf(stderr, " SIGTRAP cause a SIGTRAP\n"); |
Elliott Hughes | 3808c4e | 2013-04-23 17:14:56 -0700 | [diff] [blame] | 189 | 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 Hughes | aa42130 | 2012-12-10 14:15:42 -0800 | [diff] [blame] | 192 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 193 | |
Elliott Hughes | aa42130 | 2012-12-10 14:15:42 -0800 | [diff] [blame] | 194 | int main(int argc, char **argv) |
| 195 | { |
| 196 | fprintf(stderr,"crasher: built at " __TIME__ "!@\n"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 197 | |
| 198 | if(argc > 1) { |
Elliott Hughes | aa42130 | 2012-12-10 14:15:42 -0800 | [diff] [blame] | 199 | return do_action(argv[1]); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 200 | } else { |
| 201 | crash1(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 202 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 203 | |
Elliott Hughes | 6f40caf | 2013-06-12 14:04:34 -0700 | [diff] [blame] | 204 | return 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 205 | } |