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