blob: 8c225cb1f58492b594f92178c696e6db1f7c8beb [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001
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 Hughes3808c4e2013-04-23 17:14:56 -070020extern const char* __progname;
21
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080022void crash1(void);
23void crashnostack(void);
Elliott Hughes6f40caf2013-06-12 14:04:34 -070024static int do_action(const char* arg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080025
26static void debuggerd_connect()
27{
28 char tmp[1];
29 int s;
30 sprintf(tmp, "%d", gettid());
31 s = socket_local_client("android:debuggerd",
Elliott Hughes6f40caf2013-06-12 14:04:34 -070032 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080033 if(s >= 0) {
34 read(s, tmp, 1);
35 close(s);
36 }
37}
38
Elliott Hughes6f40caf2013-06-12 14:04:34 -070039static void maybeabort() {
40 if(time(0) != 42) {
41 abort();
42 }
43}
44
45static int smash_stack(int i) {
Elliott Hughesdf4200e2013-02-14 14:41:57 -080046 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 Hughes6f40caf2013-06-12 14:04:34 -070057__attribute__((noinline)) static void overflow_stack(void* p) {
Elliott Hughes3808c4e2013-04-23 17:14:56 -070058 fprintf(stderr, "p = %p\n", p);
59 void* buf[1];
60 buf[0] = p;
61 overflow_stack(&buf);
62}
63
Elliott Hughes6f40caf2013-06-12 14:04:34 -070064static void test_call1()
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080065{
66 *((int*) 32) = 1;
67}
68
Elliott Hughes6f40caf2013-06-12 14:04:34 -070069static void *noisy(void *x)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080070{
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 Hughes6f40caf2013-06-12 14:04:34 -070080static int ctest()
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080081{
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 Hughesaa421302012-12-10 14:15:42 -080093static void* thread_callback(void* raw_arg)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080094{
Elliott Hughesaa421302012-12-10 14:15:42 -080095 return (void*) do_action((const char*) raw_arg);
96}
97
Elliott Hughes6f40caf2013-06-12 14:04:34 -070098static int do_action_on_thread(const char* arg)
Elliott Hughesaa421302012-12-10 14:15:42 -080099{
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 Hughes6f40caf2013-06-12 14:04:34 -0700107__attribute__((noinline)) static int crash3(int a) {
108 *((int*) 0xdead) = a;
109 return a*4;
Pavel Chupinaf2cb362013-03-08 13:17:35 +0400110}
111
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700112__attribute__((noinline)) static int crash2(int a) {
113 a = crash3(a) + 2;
114 return a*3;
Pavel Chupinaf2cb362013-03-08 13:17:35 +0400115}
116
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700117__attribute__((noinline)) static int crash(int a) {
118 a = crash2(a) + 1;
119 return a*2;
Pavel Chupinaf2cb362013-03-08 13:17:35 +0400120}
121
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700122static 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
127static int do_action(const char* arg)
Elliott Hughesaa421302012-12-10 14:15:42 -0800128{
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700129 fprintf(stderr,"crasher: init pid=%d tid=%d\n", getpid(), gettid());
130
131 if (!strncmp(arg, "thread-", strlen("thread-"))) {
Elliott Hughesaa421302012-12-10 14:15:42 -0800132 return do_action_on_thread(arg + strlen("thread-"));
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700133 } 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 Hughes6f40caf2013-06-12 14:04:34 -0700147 } else if (!strcmp(arg, "heap-usage")) {
148 abuse_heap();
Elliott Hughesaa421302012-12-10 14:15:42 -0800149 }
150
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700151 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 Hughes6f40caf2013-06-12 14:04:34 -0700155 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 Hughes3808c4e2013-04-23 17:14:56 -0700157 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 Hughesaa421302012-12-10 14:15:42 -0800165}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800166
Elliott Hughesaa421302012-12-10 14:15:42 -0800167int main(int argc, char **argv)
168{
169 fprintf(stderr,"crasher: built at " __TIME__ "!@\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800170
171 if(argc > 1) {
Elliott Hughesaa421302012-12-10 14:15:42 -0800172 return do_action(argv[1]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800173 } else {
174 crash1();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800175 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800176
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700177 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800178}