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