blob: d88ef880144d98b5a9e2eff1dcd3d5dd75ef4842 [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);
Bruce Beare84924902010-10-13 14:21:30 -070024void maybeabort(void);
Elliott Hughesaa421302012-12-10 14:15:42 -080025int do_action(const char* arg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080026
27static 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 Hughesdf4200e2013-02-14 14:41:57 -080040int 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 Hughes3808c4e2013-04-23 17:14:56 -070052__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 Projectdd7bc332009-03-03 19:32:55 -080059void test_call1()
60{
61 *((int*) 32) = 1;
62}
63
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080064void *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
75int 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 Hughesaa421302012-12-10 14:15:42 -080088static void* thread_callback(void* raw_arg)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080089{
Elliott Hughesaa421302012-12-10 14:15:42 -080090 return (void*) do_action((const char*) raw_arg);
91}
92
93int 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 Chupinaf2cb362013-03-08 13:17:35 +0400102__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 Hughesaa421302012-12-10 14:15:42 -0800117int do_action(const char* arg)
118{
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700119 fprintf(stderr,"crasher: init pid=%d tid=%d\n", getpid(), gettid());
120
121 if (!strncmp(arg, "thread-", strlen("thread-"))) {
Elliott Hughesaa421302012-12-10 14:15:42 -0800122 return do_action_on_thread(arg + strlen("thread-"));
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700123 } 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 Hughesaa421302012-12-10 14:15:42 -0800137 }
138
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700139 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 Hughesaa421302012-12-10 14:15:42 -0800151}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800152
Elliott Hughesaa421302012-12-10 14:15:42 -0800153int main(int argc, char **argv)
154{
155 fprintf(stderr,"crasher: built at " __TIME__ "!@\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800156
157 if(argc > 1) {
Elliott Hughesaa421302012-12-10 14:15:42 -0800158 return do_action(argv[1]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800159 } else {
160 crash1();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800161 }
162
163 return 0;
164}
165
166void maybeabort()
167{
168 if(time(0) != 42) abort();
169}