blob: af86fe9c8fb8b185dc4f1368397861e947e4aee9 [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>
Brigid Smith8606eaa2014-07-07 12:33:50 -070010#include <sys/mman.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080011#include <sys/ptrace.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080012#include <sys/socket.h>
Elliott Hughesda6b2e22014-04-23 14:57:32 -070013#include <sys/wait.h>
14#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080015
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080016#include <cutils/sockets.h>
Mark Salyzynf1a8dfa2014-04-30 09:24:08 -070017#include <log/log.h>
18
19#ifndef __unused
20#define __unused __attribute__((__unused__))
21#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080022
Elliott Hughes3808c4e2013-04-23 17:14:56 -070023extern const char* __progname;
24
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080025void crash1(void);
26void crashnostack(void);
Elliott Hughes6f40caf2013-06-12 14:04:34 -070027static int do_action(const char* arg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080028
Elliott Hughesda6b2e22014-04-23 14:57:32 -070029static void maybe_abort() {
30 if (time(0) != 42) {
Elliott Hughes6f40caf2013-06-12 14:04:34 -070031 abort();
32 }
33}
34
Yabin Cui2331b952014-12-11 17:46:33 -080035static char* smash_stack_dummy_buf;
36__attribute__ ((noinline)) static void smash_stack_dummy_function(volatile int* plen) {
37 smash_stack_dummy_buf[*plen] = 0;
38}
39
40// This must be marked with "__attribute__ ((noinline))", to ensure the
41// compiler generates the proper stack guards around this function.
42// Assign local array address to global variable to force stack guards.
43// Use another noinline function to corrupt the stack.
44__attribute__ ((noinline)) static int smash_stack(volatile int* plen) {
Elliott Hughesdf4200e2013-02-14 14:41:57 -080045 printf("crasher: deliberately corrupting stack...\n");
Yabin Cui2331b952014-12-11 17:46:33 -080046
47 char buf[128];
48 smash_stack_dummy_buf = buf;
49 // This should corrupt stack guards and make process abort.
50 smash_stack_dummy_function(plen);
51 return 0;
Elliott Hughesdf4200e2013-02-14 14:41:57 -080052}
53
Elliott Hughesb1be27e2013-07-15 17:19:02 -070054static void* global = 0; // So GCC doesn't optimize the tail recursion out of overflow_stack.
55
Elliott Hughes6f40caf2013-06-12 14:04:34 -070056__attribute__((noinline)) static void overflow_stack(void* p) {
Elliott Hughes3808c4e2013-04-23 17:14:56 -070057 void* buf[1];
58 buf[0] = p;
Elliott Hughesb1be27e2013-07-15 17:19:02 -070059 global = buf;
Elliott Hughes3808c4e2013-04-23 17:14:56 -070060 overflow_stack(&buf);
61}
62
Elliott Hughes6f40caf2013-06-12 14:04:34 -070063static void *noisy(void *x)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080064{
Elliott Hughes5d9fe772014-02-05 17:50:35 -080065 char c = (uintptr_t) x;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080066 for(;;) {
67 usleep(250*1000);
68 write(2, &c, 1);
Chih-Hung Hsieha1ff4752014-10-23 16:50:51 -070069 if(c == 'C') *((volatile unsigned*) 0) = 42;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080070 }
Elliott Hughes5d9fe772014-02-05 17:50:35 -080071 return NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080072}
73
Elliott Hughes6f40caf2013-06-12 14:04:34 -070074static int ctest()
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080075{
76 pthread_t thr;
77 pthread_attr_t attr;
78 pthread_attr_init(&attr);
79 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
80 pthread_create(&thr, &attr, noisy, (void*) 'A');
81 pthread_create(&thr, &attr, noisy, (void*) 'B');
82 pthread_create(&thr, &attr, noisy, (void*) 'C');
83 for(;;) ;
84 return 0;
85}
86
Elliott Hughesaa421302012-12-10 14:15:42 -080087static void* thread_callback(void* raw_arg)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080088{
Elliott Hughes5d9fe772014-02-05 17:50:35 -080089 return (void*) (uintptr_t) do_action((const char*) raw_arg);
Elliott Hughesaa421302012-12-10 14:15:42 -080090}
91
Elliott Hughes6f40caf2013-06-12 14:04:34 -070092static int do_action_on_thread(const char* arg)
Elliott Hughesaa421302012-12-10 14:15:42 -080093{
94 pthread_t t;
95 pthread_create(&t, NULL, thread_callback, (void*) arg);
96 void* result = NULL;
97 pthread_join(t, &result);
Elliott Hughes5d9fe772014-02-05 17:50:35 -080098 return (int) (uintptr_t) result;
Elliott Hughesaa421302012-12-10 14:15:42 -080099}
100
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700101__attribute__((noinline)) static int crash3(int a) {
102 *((int*) 0xdead) = a;
103 return a*4;
Pavel Chupinaf2cb362013-03-08 13:17:35 +0400104}
105
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700106__attribute__((noinline)) static int crash2(int a) {
107 a = crash3(a) + 2;
108 return a*3;
Pavel Chupinaf2cb362013-03-08 13:17:35 +0400109}
110
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700111__attribute__((noinline)) static int crash(int a) {
112 a = crash2(a) + 1;
113 return a*2;
Pavel Chupinaf2cb362013-03-08 13:17:35 +0400114}
115
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700116static void abuse_heap() {
117 char buf[16];
118 free((void*) buf); // GCC is smart enough to warn about this, but we're doing it deliberately.
119}
120
Brigid Smith7b2078e2014-06-17 14:55:47 -0700121static void sigsegv_non_null() {
122 int* a = (int *)(&do_action);
123 *a = 42;
124}
125
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700126static int do_action(const char* arg)
Elliott Hughesaa421302012-12-10 14:15:42 -0800127{
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700128 fprintf(stderr,"crasher: init pid=%d tid=%d\n", getpid(), gettid());
129
130 if (!strncmp(arg, "thread-", strlen("thread-"))) {
Elliott Hughesaa421302012-12-10 14:15:42 -0800131 return do_action_on_thread(arg + strlen("thread-"));
Brigid Smith7b2078e2014-06-17 14:55:47 -0700132 } else if (!strcmp(arg, "SIGSEGV-non-null")) {
133 sigsegv_non_null();
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700134 } else if (!strcmp(arg, "smash-stack")) {
Yabin Cui2331b952014-12-11 17:46:33 -0800135 volatile int len = 128;
136 return smash_stack(&len);
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700137 } else if (!strcmp(arg, "stack-overflow")) {
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700138 overflow_stack(NULL);
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700139 } else if (!strcmp(arg, "nostack")) {
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700140 crashnostack();
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700141 } else if (!strcmp(arg, "ctest")) {
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700142 return ctest();
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700143 } else if (!strcmp(arg, "exit")) {
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700144 exit(1);
Elliott Hughes855fcc32014-04-25 16:05:34 -0700145 } else if (!strcmp(arg, "crash") || !strcmp(arg, "SIGSEGV")) {
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700146 return crash(42);
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700147 } else if (!strcmp(arg, "abort")) {
148 maybe_abort();
149 } else if (!strcmp(arg, "assert")) {
150 __assert("some_file.c", 123, "false");
151 } else if (!strcmp(arg, "assert2")) {
Elliott Hughes3ecc4212014-07-15 11:38:47 -0700152 __assert2("some_file.c", 123, "some_function", "false");
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700153 } else if (!strcmp(arg, "LOG_ALWAYS_FATAL")) {
154 LOG_ALWAYS_FATAL("hello %s", "world");
155 } else if (!strcmp(arg, "LOG_ALWAYS_FATAL_IF")) {
156 LOG_ALWAYS_FATAL_IF(true, "hello %s", "world");
Elliott Hughes3ecc4212014-07-15 11:38:47 -0700157 } else if (!strcmp(arg, "SIGFPE")) {
158 raise(SIGFPE);
159 return EXIT_SUCCESS;
Elliott Hughes855fcc32014-04-25 16:05:34 -0700160 } else if (!strcmp(arg, "SIGPIPE")) {
161 int pipe_fds[2];
162 pipe(pipe_fds);
163 close(pipe_fds[0]);
164 write(pipe_fds[1], "oops", 4);
165 return EXIT_SUCCESS;
Elliott Hughes7e35ae82014-05-16 17:05:19 -0700166 } else if (!strcmp(arg, "SIGTRAP")) {
167 raise(SIGTRAP);
168 return EXIT_SUCCESS;
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700169 } else if (!strcmp(arg, "heap-usage")) {
170 abuse_heap();
Brigid Smith8606eaa2014-07-07 12:33:50 -0700171 } else if (!strcmp(arg, "SIGSEGV-unmapped")) {
172 char* map = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
173 munmap(map, sizeof(int));
174 map[0] = '8';
Elliott Hughesaa421302012-12-10 14:15:42 -0800175 }
176
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700177 fprintf(stderr, "%s OP\n", __progname);
178 fprintf(stderr, "where OP is:\n");
Elliott Hughes855fcc32014-04-25 16:05:34 -0700179 fprintf(stderr, " smash-stack overwrite a stack-guard canary\n");
180 fprintf(stderr, " stack-overflow recurse until the stack overflows\n");
181 fprintf(stderr, " heap-corruption cause a libc abort by corrupting the heap\n");
182 fprintf(stderr, " heap-usage cause a libc abort by abusing a heap function\n");
183 fprintf(stderr, " nostack crash with a NULL stack pointer\n");
184 fprintf(stderr, " ctest (obsoleted by thread-crash?)\n");
185 fprintf(stderr, " exit call exit(1)\n");
186 fprintf(stderr, " abort call abort()\n");
187 fprintf(stderr, " assert call assert() without a function\n");
188 fprintf(stderr, " assert2 call assert() with a function\n");
189 fprintf(stderr, " LOG_ALWAYS_FATAL call LOG_ALWAYS_FATAL\n");
190 fprintf(stderr, " LOG_ALWAYS_FATAL_IF call LOG_ALWAYS_FATAL\n");
Elliott Hughes3ecc4212014-07-15 11:38:47 -0700191 fprintf(stderr, " SIGFPE cause a SIGFPE\n");
Elliott Hughes855fcc32014-04-25 16:05:34 -0700192 fprintf(stderr, " SIGPIPE cause a SIGPIPE\n");
Brigid Smith7b2078e2014-06-17 14:55:47 -0700193 fprintf(stderr, " SIGSEGV cause a SIGSEGV at address 0x0 (synonym: crash)\n");
194 fprintf(stderr, " SIGSEGV-non-null cause a SIGSEGV at a non-zero address\n");
Brigid Smith8606eaa2014-07-07 12:33:50 -0700195 fprintf(stderr, " SIGSEGV-unmapped mmap/munmap a region of memory and then attempt to access it\n");
Elliott Hughes7e35ae82014-05-16 17:05:19 -0700196 fprintf(stderr, " SIGTRAP cause a SIGTRAP\n");
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700197 fprintf(stderr, "prefix any of the above with 'thread-' to not run\n");
198 fprintf(stderr, "on the process' main thread.\n");
199 return EXIT_SUCCESS;
Elliott Hughesaa421302012-12-10 14:15:42 -0800200}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800201
Elliott Hughesaa421302012-12-10 14:15:42 -0800202int main(int argc, char **argv)
203{
204 fprintf(stderr,"crasher: built at " __TIME__ "!@\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800205
206 if(argc > 1) {
Elliott Hughesaa421302012-12-10 14:15:42 -0800207 return do_action(argv[1]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800208 } else {
209 crash1();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800210 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800211
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700212 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800213}