blob: bdeaf0b331c0718b20c1ca26b42e5aa072ae9f86 [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
Elliott Hughes23d1cad2016-05-10 13:29:58 -070025extern "C" void crash1(void);
26extern "C" void crashnostack(void);
27
Elliott Hughes6f40caf2013-06-12 14:04:34 -070028static int do_action(const char* arg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080029
Elliott Hughesda6b2e22014-04-23 14:57:32 -070030static void maybe_abort() {
31 if (time(0) != 42) {
Elliott Hughes6f40caf2013-06-12 14:04:34 -070032 abort();
33 }
34}
35
Yabin Cui2331b952014-12-11 17:46:33 -080036static char* smash_stack_dummy_buf;
37__attribute__ ((noinline)) static void smash_stack_dummy_function(volatile int* plen) {
38 smash_stack_dummy_buf[*plen] = 0;
39}
40
41// This must be marked with "__attribute__ ((noinline))", to ensure the
42// compiler generates the proper stack guards around this function.
43// Assign local array address to global variable to force stack guards.
44// Use another noinline function to corrupt the stack.
45__attribute__ ((noinline)) static int smash_stack(volatile int* plen) {
Elliott Hughesdf4200e2013-02-14 14:41:57 -080046 printf("crasher: deliberately corrupting stack...\n");
Yabin Cui2331b952014-12-11 17:46:33 -080047
48 char buf[128];
49 smash_stack_dummy_buf = buf;
50 // This should corrupt stack guards and make process abort.
51 smash_stack_dummy_function(plen);
52 return 0;
Elliott Hughesdf4200e2013-02-14 14:41:57 -080053}
54
Stephen Hines9466bb22015-09-30 23:30:38 -070055#if defined(__clang__)
Stephen Hines18395cb2015-09-29 23:55:14 -070056#pragma clang diagnostic push
57#pragma clang diagnostic ignored "-Winfinite-recursion"
Stephen Hines9466bb22015-09-30 23:30:38 -070058#endif
Stephen Hines18395cb2015-09-29 23:55:14 -070059
Elliott Hughesb1be27e2013-07-15 17:19:02 -070060static void* global = 0; // So GCC doesn't optimize the tail recursion out of overflow_stack.
61
Elliott Hughes6f40caf2013-06-12 14:04:34 -070062__attribute__((noinline)) static void overflow_stack(void* p) {
Elliott Hughes3808c4e2013-04-23 17:14:56 -070063 void* buf[1];
64 buf[0] = p;
Elliott Hughesb1be27e2013-07-15 17:19:02 -070065 global = buf;
Elliott Hughes3808c4e2013-04-23 17:14:56 -070066 overflow_stack(&buf);
67}
68
Stephen Hines9466bb22015-09-30 23:30:38 -070069#if defined(__clang__)
Stephen Hines18395cb2015-09-29 23:55:14 -070070#pragma clang diagnostic pop
Stephen Hines9466bb22015-09-30 23:30:38 -070071#endif
Stephen Hines18395cb2015-09-29 23:55:14 -070072
Elliott Hughes6f40caf2013-06-12 14:04:34 -070073static void *noisy(void *x)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080074{
Elliott Hughes5d9fe772014-02-05 17:50:35 -080075 char c = (uintptr_t) x;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080076 for(;;) {
77 usleep(250*1000);
78 write(2, &c, 1);
Chih-Hung Hsieha1ff4752014-10-23 16:50:51 -070079 if(c == 'C') *((volatile unsigned*) 0) = 42;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080080 }
Elliott Hughes5d9fe772014-02-05 17:50:35 -080081 return NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080082}
83
Elliott Hughes6f40caf2013-06-12 14:04:34 -070084static int ctest()
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080085{
86 pthread_t thr;
87 pthread_attr_t attr;
88 pthread_attr_init(&attr);
89 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
90 pthread_create(&thr, &attr, noisy, (void*) 'A');
91 pthread_create(&thr, &attr, noisy, (void*) 'B');
92 pthread_create(&thr, &attr, noisy, (void*) 'C');
93 for(;;) ;
94 return 0;
95}
96
Elliott Hughesaa421302012-12-10 14:15:42 -080097static void* thread_callback(void* raw_arg)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080098{
Elliott Hughes5d9fe772014-02-05 17:50:35 -080099 return (void*) (uintptr_t) do_action((const char*) raw_arg);
Elliott Hughesaa421302012-12-10 14:15:42 -0800100}
101
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700102static int do_action_on_thread(const char* arg)
Elliott Hughesaa421302012-12-10 14:15:42 -0800103{
104 pthread_t t;
105 pthread_create(&t, NULL, thread_callback, (void*) arg);
106 void* result = NULL;
107 pthread_join(t, &result);
Elliott Hughes5d9fe772014-02-05 17:50:35 -0800108 return (int) (uintptr_t) result;
Elliott Hughesaa421302012-12-10 14:15:42 -0800109}
110
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700111__attribute__((noinline)) static int crash3(int a) {
112 *((int*) 0xdead) = a;
113 return a*4;
Pavel Chupinaf2cb362013-03-08 13:17:35 +0400114}
115
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700116__attribute__((noinline)) static int crash2(int a) {
117 a = crash3(a) + 2;
118 return a*3;
Pavel Chupinaf2cb362013-03-08 13:17:35 +0400119}
120
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700121__attribute__((noinline)) static int crash(int a) {
122 a = crash2(a) + 1;
123 return a*2;
Pavel Chupinaf2cb362013-03-08 13:17:35 +0400124}
125
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700126static void abuse_heap() {
127 char buf[16];
128 free((void*) buf); // GCC is smart enough to warn about this, but we're doing it deliberately.
129}
130
Brigid Smith7b2078e2014-06-17 14:55:47 -0700131static void sigsegv_non_null() {
132 int* a = (int *)(&do_action);
133 *a = 42;
134}
135
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700136static int do_action(const char* arg)
Elliott Hughesaa421302012-12-10 14:15:42 -0800137{
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700138 fprintf(stderr,"crasher: init pid=%d tid=%d\n", getpid(), gettid());
139
140 if (!strncmp(arg, "thread-", strlen("thread-"))) {
Elliott Hughesaa421302012-12-10 14:15:42 -0800141 return do_action_on_thread(arg + strlen("thread-"));
Brigid Smith7b2078e2014-06-17 14:55:47 -0700142 } else if (!strcmp(arg, "SIGSEGV-non-null")) {
143 sigsegv_non_null();
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700144 } else if (!strcmp(arg, "smash-stack")) {
Yabin Cui2331b952014-12-11 17:46:33 -0800145 volatile int len = 128;
146 return smash_stack(&len);
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700147 } else if (!strcmp(arg, "stack-overflow")) {
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700148 overflow_stack(NULL);
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700149 } else if (!strcmp(arg, "nostack")) {
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700150 crashnostack();
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700151 } else if (!strcmp(arg, "ctest")) {
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700152 return ctest();
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700153 } else if (!strcmp(arg, "exit")) {
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700154 exit(1);
Elliott Hughes855fcc32014-04-25 16:05:34 -0700155 } else if (!strcmp(arg, "crash") || !strcmp(arg, "SIGSEGV")) {
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700156 return crash(42);
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700157 } else if (!strcmp(arg, "abort")) {
158 maybe_abort();
159 } else if (!strcmp(arg, "assert")) {
160 __assert("some_file.c", 123, "false");
161 } else if (!strcmp(arg, "assert2")) {
Elliott Hughes3ecc4212014-07-15 11:38:47 -0700162 __assert2("some_file.c", 123, "some_function", "false");
Elliott Hughes23d1cad2016-05-10 13:29:58 -0700163 } else if (!strcmp(arg, "fortify")) {
164 char buf[10];
165 __read_chk(-1, buf, 32, 10);
166 while (true) pause();
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700167 } else if (!strcmp(arg, "LOG_ALWAYS_FATAL")) {
168 LOG_ALWAYS_FATAL("hello %s", "world");
169 } else if (!strcmp(arg, "LOG_ALWAYS_FATAL_IF")) {
170 LOG_ALWAYS_FATAL_IF(true, "hello %s", "world");
Elliott Hughes3ecc4212014-07-15 11:38:47 -0700171 } else if (!strcmp(arg, "SIGFPE")) {
172 raise(SIGFPE);
173 return EXIT_SUCCESS;
Elliott Hughes7e35ae82014-05-16 17:05:19 -0700174 } else if (!strcmp(arg, "SIGTRAP")) {
175 raise(SIGTRAP);
176 return EXIT_SUCCESS;
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700177 } else if (!strcmp(arg, "heap-usage")) {
178 abuse_heap();
Brigid Smith8606eaa2014-07-07 12:33:50 -0700179 } else if (!strcmp(arg, "SIGSEGV-unmapped")) {
Elliott Hughes23d1cad2016-05-10 13:29:58 -0700180 char* map = reinterpret_cast<char*>(mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0));
Brigid Smith8606eaa2014-07-07 12:33:50 -0700181 munmap(map, sizeof(int));
182 map[0] = '8';
Elliott Hughesaa421302012-12-10 14:15:42 -0800183 }
184
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700185 fprintf(stderr, "%s OP\n", __progname);
186 fprintf(stderr, "where OP is:\n");
Elliott Hughes855fcc32014-04-25 16:05:34 -0700187 fprintf(stderr, " smash-stack overwrite a stack-guard canary\n");
188 fprintf(stderr, " stack-overflow recurse until the stack overflows\n");
189 fprintf(stderr, " heap-corruption cause a libc abort by corrupting the heap\n");
190 fprintf(stderr, " heap-usage cause a libc abort by abusing a heap function\n");
191 fprintf(stderr, " nostack crash with a NULL stack pointer\n");
192 fprintf(stderr, " ctest (obsoleted by thread-crash?)\n");
193 fprintf(stderr, " exit call exit(1)\n");
194 fprintf(stderr, " abort call abort()\n");
195 fprintf(stderr, " assert call assert() without a function\n");
196 fprintf(stderr, " assert2 call assert() with a function\n");
Elliott Hughes23d1cad2016-05-10 13:29:58 -0700197 fprintf(stderr, " fortify fail a _FORTIFY_SOURCE check\n");
Elliott Hughes855fcc32014-04-25 16:05:34 -0700198 fprintf(stderr, " LOG_ALWAYS_FATAL call LOG_ALWAYS_FATAL\n");
199 fprintf(stderr, " LOG_ALWAYS_FATAL_IF call LOG_ALWAYS_FATAL\n");
Elliott Hughes3ecc4212014-07-15 11:38:47 -0700200 fprintf(stderr, " SIGFPE cause a SIGFPE\n");
Brigid Smith7b2078e2014-06-17 14:55:47 -0700201 fprintf(stderr, " SIGSEGV cause a SIGSEGV at address 0x0 (synonym: crash)\n");
202 fprintf(stderr, " SIGSEGV-non-null cause a SIGSEGV at a non-zero address\n");
Brigid Smith8606eaa2014-07-07 12:33:50 -0700203 fprintf(stderr, " SIGSEGV-unmapped mmap/munmap a region of memory and then attempt to access it\n");
Elliott Hughes7e35ae82014-05-16 17:05:19 -0700204 fprintf(stderr, " SIGTRAP cause a SIGTRAP\n");
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700205 fprintf(stderr, "prefix any of the above with 'thread-' to not run\n");
206 fprintf(stderr, "on the process' main thread.\n");
207 return EXIT_SUCCESS;
Elliott Hughesaa421302012-12-10 14:15:42 -0800208}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800209
Elliott Hughesaa421302012-12-10 14:15:42 -0800210int main(int argc, char **argv)
211{
212 fprintf(stderr,"crasher: built at " __TIME__ "!@\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800213
214 if(argc > 1) {
Elliott Hughesaa421302012-12-10 14:15:42 -0800215 return do_action(argv[1]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800216 } else {
217 crash1();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800218 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800219
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700220 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800221}