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