blob: 5a2bc3c73e167026916223c34519c7db8f9df32a [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
Elliott Hughes6f40caf2013-06-12 14:04:34 -070026static void maybeabort() {
27 if(time(0) != 42) {
28 abort();
29 }
30}
31
32static int smash_stack(int i) {
Elliott Hughesdf4200e2013-02-14 14:41:57 -080033 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 Hughesb1be27e2013-07-15 17:19:02 -070037 // If we don't write something relatively unpredictable
Elliott Hughesdf4200e2013-02-14 14:41:57 -080038 // 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 Hughesb1be27e2013-07-15 17:19:02 -070044static void* global = 0; // So GCC doesn't optimize the tail recursion out of overflow_stack.
45
Elliott Hughes6f40caf2013-06-12 14:04:34 -070046__attribute__((noinline)) static void overflow_stack(void* p) {
Elliott Hughes3808c4e2013-04-23 17:14:56 -070047 void* buf[1];
48 buf[0] = p;
Elliott Hughesb1be27e2013-07-15 17:19:02 -070049 global = buf;
Elliott Hughes3808c4e2013-04-23 17:14:56 -070050 overflow_stack(&buf);
51}
52
Elliott Hughes6f40caf2013-06-12 14:04:34 -070053static void test_call1()
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080054{
55 *((int*) 32) = 1;
56}
57
Elliott Hughes6f40caf2013-06-12 14:04:34 -070058static void *noisy(void *x)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080059{
Elliott Hughes5d9fe772014-02-05 17:50:35 -080060 char c = (uintptr_t) x;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080061 for(;;) {
62 usleep(250*1000);
63 write(2, &c, 1);
64 if(c == 'C') *((unsigned*) 0) = 42;
65 }
Elliott Hughes5d9fe772014-02-05 17:50:35 -080066 return NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080067}
68
Elliott Hughes6f40caf2013-06-12 14:04:34 -070069static int ctest()
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080070{
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 Hughesaa421302012-12-10 14:15:42 -080082static void* thread_callback(void* raw_arg)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080083{
Elliott Hughes5d9fe772014-02-05 17:50:35 -080084 return (void*) (uintptr_t) do_action((const char*) raw_arg);
Elliott Hughesaa421302012-12-10 14:15:42 -080085}
86
Elliott Hughes6f40caf2013-06-12 14:04:34 -070087static int do_action_on_thread(const char* arg)
Elliott Hughesaa421302012-12-10 14:15:42 -080088{
89 pthread_t t;
90 pthread_create(&t, NULL, thread_callback, (void*) arg);
91 void* result = NULL;
92 pthread_join(t, &result);
Elliott Hughes5d9fe772014-02-05 17:50:35 -080093 return (int) (uintptr_t) result;
Elliott Hughesaa421302012-12-10 14:15:42 -080094}
95
Elliott Hughes6f40caf2013-06-12 14:04:34 -070096__attribute__((noinline)) static int crash3(int a) {
97 *((int*) 0xdead) = a;
98 return a*4;
Pavel Chupinaf2cb362013-03-08 13:17:35 +040099}
100
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700101__attribute__((noinline)) static int crash2(int a) {
102 a = crash3(a) + 2;
103 return a*3;
Pavel Chupinaf2cb362013-03-08 13:17:35 +0400104}
105
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700106__attribute__((noinline)) static int crash(int a) {
107 a = crash2(a) + 1;
108 return a*2;
Pavel Chupinaf2cb362013-03-08 13:17:35 +0400109}
110
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700111static 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
116static int do_action(const char* arg)
Elliott Hughesaa421302012-12-10 14:15:42 -0800117{
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700118 fprintf(stderr,"crasher: init pid=%d tid=%d\n", getpid(), gettid());
119
120 if (!strncmp(arg, "thread-", strlen("thread-"))) {
Elliott Hughesaa421302012-12-10 14:15:42 -0800121 return do_action_on_thread(arg + strlen("thread-"));
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700122 } 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 Hughes6f40caf2013-06-12 14:04:34 -0700136 } else if (!strcmp(arg, "heap-usage")) {
137 abuse_heap();
Elliott Hughesaa421302012-12-10 14:15:42 -0800138 }
139
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700140 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 Hughes6f40caf2013-06-12 14:04:34 -0700144 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 Hughes3808c4e2013-04-23 17:14:56 -0700146 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 Hughesaa421302012-12-10 14:15:42 -0800154}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800155
Elliott Hughesaa421302012-12-10 14:15:42 -0800156int main(int argc, char **argv)
157{
158 fprintf(stderr,"crasher: built at " __TIME__ "!@\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800159
160 if(argc > 1) {
Elliott Hughesaa421302012-12-10 14:15:42 -0800161 return do_action(argv[1]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800162 } else {
163 crash1();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800164 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800165
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700166 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800167}