| 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> | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 10 | #include <sys/ptrace.h> | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 11 | #include <sys/socket.h> | 
| Elliott Hughes | da6b2e2 | 2014-04-23 14:57:32 -0700 | [diff] [blame] | 12 | #include <sys/wait.h> | 
|  | 13 | #include <unistd.h> | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 14 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 15 | #include <cutils/sockets.h> | 
| Mark Salyzyn | f1a8dfa | 2014-04-30 09:24:08 -0700 | [diff] [blame] | 16 | #include <log/log.h> | 
|  | 17 |  | 
|  | 18 | #ifndef __unused | 
|  | 19 | #define __unused __attribute__((__unused__)) | 
|  | 20 | #endif | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 21 |  | 
| Elliott Hughes | 3808c4e | 2013-04-23 17:14:56 -0700 | [diff] [blame] | 22 | extern const char* __progname; | 
|  | 23 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 24 | void crash1(void); | 
|  | 25 | void crashnostack(void); | 
| Elliott Hughes | 6f40caf | 2013-06-12 14:04:34 -0700 | [diff] [blame] | 26 | static int do_action(const char* arg); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 27 |  | 
| Elliott Hughes | da6b2e2 | 2014-04-23 14:57:32 -0700 | [diff] [blame] | 28 | static void maybe_abort() { | 
|  | 29 | if (time(0) != 42) { | 
| Elliott Hughes | 6f40caf | 2013-06-12 14:04:34 -0700 | [diff] [blame] | 30 | abort(); | 
|  | 31 | } | 
|  | 32 | } | 
|  | 33 |  | 
| Mark Salyzyn | f1a8dfa | 2014-04-30 09:24:08 -0700 | [diff] [blame] | 34 | static int smash_stack(int i __unused) { | 
| Elliott Hughes | df4200e | 2013-02-14 14:41:57 -0800 | [diff] [blame] | 35 | printf("crasher: deliberately corrupting stack...\n"); | 
|  | 36 | // Unless there's a "big enough" buffer on the stack, gcc | 
|  | 37 | // doesn't bother inserting checks. | 
|  | 38 | char buf[8]; | 
| Elliott Hughes | b1be27e | 2013-07-15 17:19:02 -0700 | [diff] [blame] | 39 | // If we don't write something relatively unpredictable | 
| Elliott Hughes | df4200e | 2013-02-14 14:41:57 -0800 | [diff] [blame] | 40 | // into the buffer and then do something with it, gcc | 
|  | 41 | // optimizes everything away and just returns a constant. | 
|  | 42 | *(int*)(&buf[7]) = (uintptr_t) &buf[0]; | 
|  | 43 | return *(int*)(&buf[0]); | 
|  | 44 | } | 
|  | 45 |  | 
| Elliott Hughes | b1be27e | 2013-07-15 17:19:02 -0700 | [diff] [blame] | 46 | static void* global = 0; // So GCC doesn't optimize the tail recursion out of overflow_stack. | 
|  | 47 |  | 
| Elliott Hughes | 6f40caf | 2013-06-12 14:04:34 -0700 | [diff] [blame] | 48 | __attribute__((noinline)) static void overflow_stack(void* p) { | 
| Elliott Hughes | 3808c4e | 2013-04-23 17:14:56 -0700 | [diff] [blame] | 49 | void* buf[1]; | 
|  | 50 | buf[0] = p; | 
| Elliott Hughes | b1be27e | 2013-07-15 17:19:02 -0700 | [diff] [blame] | 51 | global = buf; | 
| Elliott Hughes | 3808c4e | 2013-04-23 17:14:56 -0700 | [diff] [blame] | 52 | overflow_stack(&buf); | 
|  | 53 | } | 
|  | 54 |  | 
| Elliott Hughes | 6f40caf | 2013-06-12 14:04:34 -0700 | [diff] [blame] | 55 | static void *noisy(void *x) | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 56 | { | 
| Elliott Hughes | 5d9fe77 | 2014-02-05 17:50:35 -0800 | [diff] [blame] | 57 | char c = (uintptr_t) x; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 58 | for(;;) { | 
|  | 59 | usleep(250*1000); | 
|  | 60 | write(2, &c, 1); | 
|  | 61 | if(c == 'C') *((unsigned*) 0) = 42; | 
|  | 62 | } | 
| Elliott Hughes | 5d9fe77 | 2014-02-05 17:50:35 -0800 | [diff] [blame] | 63 | return NULL; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 64 | } | 
|  | 65 |  | 
| Elliott Hughes | 6f40caf | 2013-06-12 14:04:34 -0700 | [diff] [blame] | 66 | static int ctest() | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 67 | { | 
|  | 68 | pthread_t thr; | 
|  | 69 | pthread_attr_t attr; | 
|  | 70 | pthread_attr_init(&attr); | 
|  | 71 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); | 
|  | 72 | pthread_create(&thr, &attr, noisy, (void*) 'A'); | 
|  | 73 | pthread_create(&thr, &attr, noisy, (void*) 'B'); | 
|  | 74 | pthread_create(&thr, &attr, noisy, (void*) 'C'); | 
|  | 75 | for(;;) ; | 
|  | 76 | return 0; | 
|  | 77 | } | 
|  | 78 |  | 
| Elliott Hughes | aa42130 | 2012-12-10 14:15:42 -0800 | [diff] [blame] | 79 | static void* thread_callback(void* raw_arg) | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 80 | { | 
| Elliott Hughes | 5d9fe77 | 2014-02-05 17:50:35 -0800 | [diff] [blame] | 81 | return (void*) (uintptr_t) do_action((const char*) raw_arg); | 
| Elliott Hughes | aa42130 | 2012-12-10 14:15:42 -0800 | [diff] [blame] | 82 | } | 
|  | 83 |  | 
| Elliott Hughes | 6f40caf | 2013-06-12 14:04:34 -0700 | [diff] [blame] | 84 | static int do_action_on_thread(const char* arg) | 
| Elliott Hughes | aa42130 | 2012-12-10 14:15:42 -0800 | [diff] [blame] | 85 | { | 
|  | 86 | pthread_t t; | 
|  | 87 | pthread_create(&t, NULL, thread_callback, (void*) arg); | 
|  | 88 | void* result = NULL; | 
|  | 89 | pthread_join(t, &result); | 
| Elliott Hughes | 5d9fe77 | 2014-02-05 17:50:35 -0800 | [diff] [blame] | 90 | return (int) (uintptr_t) result; | 
| Elliott Hughes | aa42130 | 2012-12-10 14:15:42 -0800 | [diff] [blame] | 91 | } | 
|  | 92 |  | 
| Elliott Hughes | 6f40caf | 2013-06-12 14:04:34 -0700 | [diff] [blame] | 93 | __attribute__((noinline)) static int crash3(int a) { | 
|  | 94 | *((int*) 0xdead) = a; | 
|  | 95 | return a*4; | 
| Pavel Chupin | af2cb36 | 2013-03-08 13:17:35 +0400 | [diff] [blame] | 96 | } | 
|  | 97 |  | 
| Elliott Hughes | 6f40caf | 2013-06-12 14:04:34 -0700 | [diff] [blame] | 98 | __attribute__((noinline)) static int crash2(int a) { | 
|  | 99 | a = crash3(a) + 2; | 
|  | 100 | return a*3; | 
| Pavel Chupin | af2cb36 | 2013-03-08 13:17:35 +0400 | [diff] [blame] | 101 | } | 
|  | 102 |  | 
| Elliott Hughes | 6f40caf | 2013-06-12 14:04:34 -0700 | [diff] [blame] | 103 | __attribute__((noinline)) static int crash(int a) { | 
|  | 104 | a = crash2(a) + 1; | 
|  | 105 | return a*2; | 
| Pavel Chupin | af2cb36 | 2013-03-08 13:17:35 +0400 | [diff] [blame] | 106 | } | 
|  | 107 |  | 
| Elliott Hughes | 6f40caf | 2013-06-12 14:04:34 -0700 | [diff] [blame] | 108 | static void abuse_heap() { | 
|  | 109 | char buf[16]; | 
|  | 110 | free((void*) buf); // GCC is smart enough to warn about this, but we're doing it deliberately. | 
|  | 111 | } | 
|  | 112 |  | 
| Brigid Smith | 7b2078e | 2014-06-17 14:55:47 -0700 | [diff] [blame] | 113 | static void sigsegv_non_null() { | 
|  | 114 | int* a = (int *)(&do_action); | 
|  | 115 | *a = 42; | 
|  | 116 | } | 
|  | 117 |  | 
| Elliott Hughes | 6f40caf | 2013-06-12 14:04:34 -0700 | [diff] [blame] | 118 | static int do_action(const char* arg) | 
| Elliott Hughes | aa42130 | 2012-12-10 14:15:42 -0800 | [diff] [blame] | 119 | { | 
| Elliott Hughes | 3808c4e | 2013-04-23 17:14:56 -0700 | [diff] [blame] | 120 | fprintf(stderr,"crasher: init pid=%d tid=%d\n", getpid(), gettid()); | 
|  | 121 |  | 
|  | 122 | if (!strncmp(arg, "thread-", strlen("thread-"))) { | 
| Elliott Hughes | aa42130 | 2012-12-10 14:15:42 -0800 | [diff] [blame] | 123 | return do_action_on_thread(arg + strlen("thread-")); | 
| Brigid Smith | 7b2078e | 2014-06-17 14:55:47 -0700 | [diff] [blame] | 124 | } else if (!strcmp(arg, "SIGSEGV-non-null")) { | 
|  | 125 | sigsegv_non_null(); | 
| Elliott Hughes | da6b2e2 | 2014-04-23 14:57:32 -0700 | [diff] [blame] | 126 | } else if (!strcmp(arg, "smash-stack")) { | 
| Elliott Hughes | 3808c4e | 2013-04-23 17:14:56 -0700 | [diff] [blame] | 127 | return smash_stack(42); | 
| Elliott Hughes | da6b2e2 | 2014-04-23 14:57:32 -0700 | [diff] [blame] | 128 | } else if (!strcmp(arg, "stack-overflow")) { | 
| Elliott Hughes | 3808c4e | 2013-04-23 17:14:56 -0700 | [diff] [blame] | 129 | overflow_stack(NULL); | 
| Elliott Hughes | da6b2e2 | 2014-04-23 14:57:32 -0700 | [diff] [blame] | 130 | } else if (!strcmp(arg, "nostack")) { | 
| Elliott Hughes | 3808c4e | 2013-04-23 17:14:56 -0700 | [diff] [blame] | 131 | crashnostack(); | 
| Elliott Hughes | da6b2e2 | 2014-04-23 14:57:32 -0700 | [diff] [blame] | 132 | } else if (!strcmp(arg, "ctest")) { | 
| Elliott Hughes | 3808c4e | 2013-04-23 17:14:56 -0700 | [diff] [blame] | 133 | return ctest(); | 
| Elliott Hughes | da6b2e2 | 2014-04-23 14:57:32 -0700 | [diff] [blame] | 134 | } else if (!strcmp(arg, "exit")) { | 
| Elliott Hughes | 3808c4e | 2013-04-23 17:14:56 -0700 | [diff] [blame] | 135 | exit(1); | 
| Elliott Hughes | 855fcc3 | 2014-04-25 16:05:34 -0700 | [diff] [blame] | 136 | } else if (!strcmp(arg, "crash") || !strcmp(arg, "SIGSEGV")) { | 
| Elliott Hughes | 3808c4e | 2013-04-23 17:14:56 -0700 | [diff] [blame] | 137 | return crash(42); | 
| Elliott Hughes | da6b2e2 | 2014-04-23 14:57:32 -0700 | [diff] [blame] | 138 | } else if (!strcmp(arg, "abort")) { | 
|  | 139 | maybe_abort(); | 
|  | 140 | } else if (!strcmp(arg, "assert")) { | 
|  | 141 | __assert("some_file.c", 123, "false"); | 
|  | 142 | } else if (!strcmp(arg, "assert2")) { | 
|  | 143 | __assert2("some_file.c", 123, "some_function", "false"); | 
|  | 144 | } else if (!strcmp(arg, "LOG_ALWAYS_FATAL")) { | 
|  | 145 | LOG_ALWAYS_FATAL("hello %s", "world"); | 
|  | 146 | } else if (!strcmp(arg, "LOG_ALWAYS_FATAL_IF")) { | 
|  | 147 | LOG_ALWAYS_FATAL_IF(true, "hello %s", "world"); | 
| Elliott Hughes | 855fcc3 | 2014-04-25 16:05:34 -0700 | [diff] [blame] | 148 | } else if (!strcmp(arg, "SIGPIPE")) { | 
|  | 149 | int pipe_fds[2]; | 
|  | 150 | pipe(pipe_fds); | 
|  | 151 | close(pipe_fds[0]); | 
|  | 152 | write(pipe_fds[1], "oops", 4); | 
|  | 153 | return EXIT_SUCCESS; | 
| Elliott Hughes | 7e35ae8 | 2014-05-16 17:05:19 -0700 | [diff] [blame] | 154 | } else if (!strcmp(arg, "SIGTRAP")) { | 
|  | 155 | raise(SIGTRAP); | 
|  | 156 | return EXIT_SUCCESS; | 
| Elliott Hughes | 6f40caf | 2013-06-12 14:04:34 -0700 | [diff] [blame] | 157 | } else if (!strcmp(arg, "heap-usage")) { | 
|  | 158 | abuse_heap(); | 
| Elliott Hughes | aa42130 | 2012-12-10 14:15:42 -0800 | [diff] [blame] | 159 | } | 
|  | 160 |  | 
| Elliott Hughes | 3808c4e | 2013-04-23 17:14:56 -0700 | [diff] [blame] | 161 | fprintf(stderr, "%s OP\n", __progname); | 
|  | 162 | fprintf(stderr, "where OP is:\n"); | 
| Elliott Hughes | 855fcc3 | 2014-04-25 16:05:34 -0700 | [diff] [blame] | 163 | fprintf(stderr, "  smash-stack           overwrite a stack-guard canary\n"); | 
|  | 164 | fprintf(stderr, "  stack-overflow        recurse until the stack overflows\n"); | 
|  | 165 | fprintf(stderr, "  heap-corruption       cause a libc abort by corrupting the heap\n"); | 
|  | 166 | fprintf(stderr, "  heap-usage            cause a libc abort by abusing a heap function\n"); | 
|  | 167 | fprintf(stderr, "  nostack               crash with a NULL stack pointer\n"); | 
|  | 168 | fprintf(stderr, "  ctest                 (obsoleted by thread-crash?)\n"); | 
|  | 169 | fprintf(stderr, "  exit                  call exit(1)\n"); | 
|  | 170 | fprintf(stderr, "  abort                 call abort()\n"); | 
|  | 171 | fprintf(stderr, "  assert                call assert() without a function\n"); | 
|  | 172 | fprintf(stderr, "  assert2               call assert() with a function\n"); | 
|  | 173 | fprintf(stderr, "  LOG_ALWAYS_FATAL      call LOG_ALWAYS_FATAL\n"); | 
|  | 174 | fprintf(stderr, "  LOG_ALWAYS_FATAL_IF   call LOG_ALWAYS_FATAL\n"); | 
|  | 175 | fprintf(stderr, "  SIGPIPE               cause a SIGPIPE\n"); | 
| Brigid Smith | 7b2078e | 2014-06-17 14:55:47 -0700 | [diff] [blame] | 176 | fprintf(stderr, "  SIGSEGV               cause a SIGSEGV at address 0x0 (synonym: crash)\n"); | 
|  | 177 | fprintf(stderr, "  SIGSEGV-non-null      cause a SIGSEGV at a non-zero address\n"); | 
| Elliott Hughes | 7e35ae8 | 2014-05-16 17:05:19 -0700 | [diff] [blame] | 178 | fprintf(stderr, "  SIGTRAP               cause a SIGTRAP\n"); | 
| Elliott Hughes | 3808c4e | 2013-04-23 17:14:56 -0700 | [diff] [blame] | 179 | fprintf(stderr, "prefix any of the above with 'thread-' to not run\n"); | 
|  | 180 | fprintf(stderr, "on the process' main thread.\n"); | 
|  | 181 | return EXIT_SUCCESS; | 
| Elliott Hughes | aa42130 | 2012-12-10 14:15:42 -0800 | [diff] [blame] | 182 | } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 183 |  | 
| Elliott Hughes | aa42130 | 2012-12-10 14:15:42 -0800 | [diff] [blame] | 184 | int main(int argc, char **argv) | 
|  | 185 | { | 
|  | 186 | fprintf(stderr,"crasher: built at " __TIME__ "!@\n"); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 187 |  | 
|  | 188 | if(argc > 1) { | 
| Elliott Hughes | aa42130 | 2012-12-10 14:15:42 -0800 | [diff] [blame] | 189 | return do_action(argv[1]); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 190 | } else { | 
|  | 191 | crash1(); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 192 | } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 193 |  | 
| Elliott Hughes | 6f40caf | 2013-06-12 14:04:34 -0700 | [diff] [blame] | 194 | return 0; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 195 | } |