blob: 9946faa5982a99ece73c769aea4b65ae9c7686a6 [file] [log] [blame]
Elliott Hughesda6b2e22014-04-23 14:57:32 -07001#include <assert.h>
2#include <errno.h>
3#include <pthread.h>
4#include <sched.h>
5#include <signal.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08006#include <stdio.h>
7#include <stdlib.h>
8#include <string.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08009#include <sys/ptrace.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080010#include <sys/socket.h>
Elliott Hughesda6b2e22014-04-23 14:57:32 -070011#include <sys/wait.h>
12#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080013
Elliott Hughesda6b2e22014-04-23 14:57:32 -070014#include <cutils/log.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080015#include <cutils/sockets.h>
16
Elliott Hughes3808c4e2013-04-23 17:14:56 -070017extern const char* __progname;
18
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080019void crash1(void);
20void crashnostack(void);
Elliott Hughes6f40caf2013-06-12 14:04:34 -070021static int do_action(const char* arg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080022
Elliott Hughesda6b2e22014-04-23 14:57:32 -070023static void maybe_abort() {
24 if (time(0) != 42) {
Elliott Hughes6f40caf2013-06-12 14:04:34 -070025 abort();
26 }
27}
28
29static int smash_stack(int i) {
Elliott Hughesdf4200e2013-02-14 14:41:57 -080030 printf("crasher: deliberately corrupting stack...\n");
31 // Unless there's a "big enough" buffer on the stack, gcc
32 // doesn't bother inserting checks.
33 char buf[8];
Elliott Hughesb1be27e2013-07-15 17:19:02 -070034 // If we don't write something relatively unpredictable
Elliott Hughesdf4200e2013-02-14 14:41:57 -080035 // into the buffer and then do something with it, gcc
36 // optimizes everything away and just returns a constant.
37 *(int*)(&buf[7]) = (uintptr_t) &buf[0];
38 return *(int*)(&buf[0]);
39}
40
Elliott Hughesb1be27e2013-07-15 17:19:02 -070041static void* global = 0; // So GCC doesn't optimize the tail recursion out of overflow_stack.
42
Elliott Hughes6f40caf2013-06-12 14:04:34 -070043__attribute__((noinline)) static void overflow_stack(void* p) {
Elliott Hughes3808c4e2013-04-23 17:14:56 -070044 void* buf[1];
45 buf[0] = p;
Elliott Hughesb1be27e2013-07-15 17:19:02 -070046 global = buf;
Elliott Hughes3808c4e2013-04-23 17:14:56 -070047 overflow_stack(&buf);
48}
49
Elliott Hughes6f40caf2013-06-12 14:04:34 -070050static void test_call1()
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080051{
52 *((int*) 32) = 1;
53}
54
Elliott Hughes6f40caf2013-06-12 14:04:34 -070055static void *noisy(void *x)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080056{
Elliott Hughes5d9fe772014-02-05 17:50:35 -080057 char c = (uintptr_t) x;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080058 for(;;) {
59 usleep(250*1000);
60 write(2, &c, 1);
61 if(c == 'C') *((unsigned*) 0) = 42;
62 }
Elliott Hughes5d9fe772014-02-05 17:50:35 -080063 return NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080064}
65
Elliott Hughes6f40caf2013-06-12 14:04:34 -070066static int ctest()
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080067{
68 pthread_t thr;
69 pthread_attr_t attr;
70 pthread_attr_init(&attr);
71 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
72 pthread_create(&thr, &attr, noisy, (void*) 'A');
73 pthread_create(&thr, &attr, noisy, (void*) 'B');
74 pthread_create(&thr, &attr, noisy, (void*) 'C');
75 for(;;) ;
76 return 0;
77}
78
Elliott Hughesaa421302012-12-10 14:15:42 -080079static void* thread_callback(void* raw_arg)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080080{
Elliott Hughes5d9fe772014-02-05 17:50:35 -080081 return (void*) (uintptr_t) do_action((const char*) raw_arg);
Elliott Hughesaa421302012-12-10 14:15:42 -080082}
83
Elliott Hughes6f40caf2013-06-12 14:04:34 -070084static int do_action_on_thread(const char* arg)
Elliott Hughesaa421302012-12-10 14:15:42 -080085{
86 pthread_t t;
87 pthread_create(&t, NULL, thread_callback, (void*) arg);
88 void* result = NULL;
89 pthread_join(t, &result);
Elliott Hughes5d9fe772014-02-05 17:50:35 -080090 return (int) (uintptr_t) result;
Elliott Hughesaa421302012-12-10 14:15:42 -080091}
92
Elliott Hughes6f40caf2013-06-12 14:04:34 -070093__attribute__((noinline)) static int crash3(int a) {
94 *((int*) 0xdead) = a;
95 return a*4;
Pavel Chupinaf2cb362013-03-08 13:17:35 +040096}
97
Elliott Hughes6f40caf2013-06-12 14:04:34 -070098__attribute__((noinline)) static int crash2(int a) {
99 a = crash3(a) + 2;
100 return a*3;
Pavel Chupinaf2cb362013-03-08 13:17:35 +0400101}
102
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700103__attribute__((noinline)) static int crash(int a) {
104 a = crash2(a) + 1;
105 return a*2;
Pavel Chupinaf2cb362013-03-08 13:17:35 +0400106}
107
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700108static void abuse_heap() {
109 char buf[16];
110 free((void*) buf); // GCC is smart enough to warn about this, but we're doing it deliberately.
111}
112
113static int do_action(const char* arg)
Elliott Hughesaa421302012-12-10 14:15:42 -0800114{
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700115 fprintf(stderr,"crasher: init pid=%d tid=%d\n", getpid(), gettid());
116
117 if (!strncmp(arg, "thread-", strlen("thread-"))) {
Elliott Hughesaa421302012-12-10 14:15:42 -0800118 return do_action_on_thread(arg + strlen("thread-"));
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700119 } else if (!strcmp(arg, "smash-stack")) {
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700120 return smash_stack(42);
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700121 } else if (!strcmp(arg, "stack-overflow")) {
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700122 overflow_stack(NULL);
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700123 } else if (!strcmp(arg, "nostack")) {
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700124 crashnostack();
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700125 } else if (!strcmp(arg, "ctest")) {
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700126 return ctest();
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700127 } else if (!strcmp(arg, "exit")) {
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700128 exit(1);
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700129 } else if (!strcmp(arg, "crash")) {
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700130 return crash(42);
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700131 } else if (!strcmp(arg, "abort")) {
132 maybe_abort();
133 } else if (!strcmp(arg, "assert")) {
134 __assert("some_file.c", 123, "false");
135 } else if (!strcmp(arg, "assert2")) {
136 __assert2("some_file.c", 123, "some_function", "false");
137 } else if (!strcmp(arg, "LOG_ALWAYS_FATAL")) {
138 LOG_ALWAYS_FATAL("hello %s", "world");
139 } else if (!strcmp(arg, "LOG_ALWAYS_FATAL_IF")) {
140 LOG_ALWAYS_FATAL_IF(true, "hello %s", "world");
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700141 } else if (!strcmp(arg, "heap-usage")) {
142 abuse_heap();
Elliott Hughesaa421302012-12-10 14:15:42 -0800143 }
144
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700145 fprintf(stderr, "%s OP\n", __progname);
146 fprintf(stderr, "where OP is:\n");
147 fprintf(stderr, " smash-stack overwrite a stack-guard canary\n");
148 fprintf(stderr, " stack-overflow recurse until the stack overflows\n");
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700149 fprintf(stderr, " heap-corruption cause a libc abort by corrupting the heap\n");
150 fprintf(stderr, " heap-usage cause a libc abort by abusing a heap function\n");
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700151 fprintf(stderr, " nostack crash with a NULL stack pointer\n");
152 fprintf(stderr, " ctest (obsoleted by thread-crash?)\n");
153 fprintf(stderr, " exit call exit(1)\n");
154 fprintf(stderr, " crash cause a SIGSEGV\n");
155 fprintf(stderr, " abort call abort()\n");
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700156 fprintf(stderr, " assert call assert() without a function\n");
157 fprintf(stderr, " assert2 call assert() with a function\n");
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700158 fprintf(stderr, "prefix any of the above with 'thread-' to not run\n");
159 fprintf(stderr, "on the process' main thread.\n");
160 return EXIT_SUCCESS;
Elliott Hughesaa421302012-12-10 14:15:42 -0800161}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800162
Elliott Hughesaa421302012-12-10 14:15:42 -0800163int main(int argc, char **argv)
164{
165 fprintf(stderr,"crasher: built at " __TIME__ "!@\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800166
167 if(argc > 1) {
Elliott Hughesaa421302012-12-10 14:15:42 -0800168 return do_action(argv[1]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800169 } else {
170 crash1();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800171 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800172
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700173 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800174}