blob: 4721da9cf7a5041f18d477369ef24771562b8b09 [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>
Mark Salyzynf1a8dfa2014-04-30 09:24:08 -07009#include <sys/cdefs.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080010#include <sys/ptrace.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080011#include <sys/socket.h>
Elliott Hughesda6b2e22014-04-23 14:57:32 -070012#include <sys/wait.h>
13#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080014
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080015#include <cutils/sockets.h>
Mark Salyzynf1a8dfa2014-04-30 09:24:08 -070016#include <log/log.h>
17
18#ifndef __unused
19#define __unused __attribute__((__unused__))
20#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080021
Elliott Hughes3808c4e2013-04-23 17:14:56 -070022extern const char* __progname;
23
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080024void crash1(void);
25void crashnostack(void);
Elliott Hughes6f40caf2013-06-12 14:04:34 -070026static int do_action(const char* arg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080027
Elliott Hughesda6b2e22014-04-23 14:57:32 -070028static void maybe_abort() {
29 if (time(0) != 42) {
Elliott Hughes6f40caf2013-06-12 14:04:34 -070030 abort();
31 }
32}
33
Mark Salyzynf1a8dfa2014-04-30 09:24:08 -070034static int smash_stack(int i __unused) {
Elliott Hughesdf4200e2013-02-14 14:41:57 -080035 printf("crasher: deliberately corrupting stack...\n");
36 // Unless there's a "big enough" buffer on the stack, gcc
37 // doesn't bother inserting checks.
38 char buf[8];
Elliott Hughesb1be27e2013-07-15 17:19:02 -070039 // If we don't write something relatively unpredictable
Elliott Hughesdf4200e2013-02-14 14:41:57 -080040 // into the buffer and then do something with it, gcc
41 // optimizes everything away and just returns a constant.
42 *(int*)(&buf[7]) = (uintptr_t) &buf[0];
43 return *(int*)(&buf[0]);
44}
45
Elliott Hughesb1be27e2013-07-15 17:19:02 -070046static void* global = 0; // So GCC doesn't optimize the tail recursion out of overflow_stack.
47
Elliott Hughes6f40caf2013-06-12 14:04:34 -070048__attribute__((noinline)) static void overflow_stack(void* p) {
Elliott Hughes3808c4e2013-04-23 17:14:56 -070049 void* buf[1];
50 buf[0] = p;
Elliott Hughesb1be27e2013-07-15 17:19:02 -070051 global = buf;
Elliott Hughes3808c4e2013-04-23 17:14:56 -070052 overflow_stack(&buf);
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 Hughes855fcc32014-04-25 16:05:34 -0700129 } else if (!strcmp(arg, "crash") || !strcmp(arg, "SIGSEGV")) {
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 Hughes855fcc32014-04-25 16:05:34 -0700141 } else if (!strcmp(arg, "SIGPIPE")) {
142 int pipe_fds[2];
143 pipe(pipe_fds);
144 close(pipe_fds[0]);
145 write(pipe_fds[1], "oops", 4);
146 return EXIT_SUCCESS;
Elliott Hughes7e35ae82014-05-16 17:05:19 -0700147 } else if (!strcmp(arg, "SIGTRAP")) {
148 raise(SIGTRAP);
149 return EXIT_SUCCESS;
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700150 } else if (!strcmp(arg, "heap-usage")) {
151 abuse_heap();
Elliott Hughesaa421302012-12-10 14:15:42 -0800152 }
153
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700154 fprintf(stderr, "%s OP\n", __progname);
155 fprintf(stderr, "where OP is:\n");
Elliott Hughes855fcc32014-04-25 16:05:34 -0700156 fprintf(stderr, " smash-stack overwrite a stack-guard canary\n");
157 fprintf(stderr, " stack-overflow recurse until the stack overflows\n");
158 fprintf(stderr, " heap-corruption cause a libc abort by corrupting the heap\n");
159 fprintf(stderr, " heap-usage cause a libc abort by abusing a heap function\n");
160 fprintf(stderr, " nostack crash with a NULL stack pointer\n");
161 fprintf(stderr, " ctest (obsoleted by thread-crash?)\n");
162 fprintf(stderr, " exit call exit(1)\n");
163 fprintf(stderr, " abort call abort()\n");
164 fprintf(stderr, " assert call assert() without a function\n");
165 fprintf(stderr, " assert2 call assert() with a function\n");
166 fprintf(stderr, " LOG_ALWAYS_FATAL call LOG_ALWAYS_FATAL\n");
167 fprintf(stderr, " LOG_ALWAYS_FATAL_IF call LOG_ALWAYS_FATAL\n");
168 fprintf(stderr, " SIGPIPE cause a SIGPIPE\n");
169 fprintf(stderr, " SIGSEGV cause a SIGSEGV (synonym: crash)\n");
Elliott Hughes7e35ae82014-05-16 17:05:19 -0700170 fprintf(stderr, " SIGTRAP cause a SIGTRAP\n");
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700171 fprintf(stderr, "prefix any of the above with 'thread-' to not run\n");
172 fprintf(stderr, "on the process' main thread.\n");
173 return EXIT_SUCCESS;
Elliott Hughesaa421302012-12-10 14:15:42 -0800174}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800175
Elliott Hughesaa421302012-12-10 14:15:42 -0800176int main(int argc, char **argv)
177{
178 fprintf(stderr,"crasher: built at " __TIME__ "!@\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800179
180 if(argc > 1) {
Elliott Hughesaa421302012-12-10 14:15:42 -0800181 return do_action(argv[1]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800182 } else {
183 crash1();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800184 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800185
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700186 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800187}