blob: cfcc26ef5bd2349e16474af9a9c5da8e28dbc86d [file] [log] [blame]
Elliott Hughesda6b2e22014-04-23 14:57:32 -07001#include <assert.h>
2#include <errno.h>
Josh Gao218f7fb2016-10-07 16:42:05 -07003#include <fcntl.h>
Elliott Hughesda6b2e22014-04-23 14:57:32 -07004#include <pthread.h>
5#include <sched.h>
6#include <signal.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08007#include <stdio.h>
8#include <stdlib.h>
9#include <string.h>
Mark Salyzynf1a8dfa2014-04-30 09:24:08 -070010#include <sys/cdefs.h>
Brigid Smith8606eaa2014-07-07 12:33:50 -070011#include <sys/mman.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080012#include <sys/ptrace.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080013#include <sys/socket.h>
Elliott Hughesda6b2e22014-04-23 14:57:32 -070014#include <sys/wait.h>
15#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080016
Mark Salyzynff2dcd92016-09-28 15:54:45 -070017#include <android/log.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080018#include <cutils/sockets.h>
Mark Salyzynf1a8dfa2014-04-30 09:24:08 -070019
Josh Gao9c02dc52016-06-15 17:29:00 -070020#if defined(STATIC_CRASHER)
21#include "debuggerd/client.h"
22#endif
23
Mark Salyzynf1a8dfa2014-04-30 09:24:08 -070024#ifndef __unused
25#define __unused __attribute__((__unused__))
26#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080027
Elliott Hughes3808c4e2013-04-23 17:14:56 -070028extern const char* __progname;
29
Elliott Hughes23d1cad2016-05-10 13:29:58 -070030extern "C" void crash1(void);
31extern "C" void crashnostack(void);
32
Elliott Hughes6f40caf2013-06-12 14:04:34 -070033static int do_action(const char* arg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080034
Elliott Hughesda6b2e22014-04-23 14:57:32 -070035static void maybe_abort() {
36 if (time(0) != 42) {
Elliott Hughes6f40caf2013-06-12 14:04:34 -070037 abort();
38 }
39}
40
Yabin Cui2331b952014-12-11 17:46:33 -080041static char* smash_stack_dummy_buf;
42__attribute__ ((noinline)) static void smash_stack_dummy_function(volatile int* plen) {
43 smash_stack_dummy_buf[*plen] = 0;
44}
45
46// This must be marked with "__attribute__ ((noinline))", to ensure the
47// compiler generates the proper stack guards around this function.
48// Assign local array address to global variable to force stack guards.
49// Use another noinline function to corrupt the stack.
50__attribute__ ((noinline)) static int smash_stack(volatile int* plen) {
Josh Gao9c02dc52016-06-15 17:29:00 -070051 printf("%s: deliberately corrupting stack...\n", __progname);
Yabin Cui2331b952014-12-11 17:46:33 -080052
53 char buf[128];
54 smash_stack_dummy_buf = buf;
55 // This should corrupt stack guards and make process abort.
56 smash_stack_dummy_function(plen);
57 return 0;
Elliott Hughesdf4200e2013-02-14 14:41:57 -080058}
59
Stephen Hines9466bb22015-09-30 23:30:38 -070060#if defined(__clang__)
Stephen Hines18395cb2015-09-29 23:55:14 -070061#pragma clang diagnostic push
62#pragma clang diagnostic ignored "-Winfinite-recursion"
Stephen Hines9466bb22015-09-30 23:30:38 -070063#endif
Stephen Hines18395cb2015-09-29 23:55:14 -070064
Elliott Hughesb1be27e2013-07-15 17:19:02 -070065static void* global = 0; // So GCC doesn't optimize the tail recursion out of overflow_stack.
66
Elliott Hughes6f40caf2013-06-12 14:04:34 -070067__attribute__((noinline)) static void overflow_stack(void* p) {
Elliott Hughes3808c4e2013-04-23 17:14:56 -070068 void* buf[1];
69 buf[0] = p;
Elliott Hughesb1be27e2013-07-15 17:19:02 -070070 global = buf;
Elliott Hughes3808c4e2013-04-23 17:14:56 -070071 overflow_stack(&buf);
72}
73
Stephen Hines9466bb22015-09-30 23:30:38 -070074#if defined(__clang__)
Stephen Hines18395cb2015-09-29 23:55:14 -070075#pragma clang diagnostic pop
Stephen Hines9466bb22015-09-30 23:30:38 -070076#endif
Stephen Hines18395cb2015-09-29 23:55:14 -070077
Elliott Hughes6f40caf2013-06-12 14:04:34 -070078static void *noisy(void *x)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080079{
Elliott Hughes5d9fe772014-02-05 17:50:35 -080080 char c = (uintptr_t) x;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080081 for(;;) {
82 usleep(250*1000);
83 write(2, &c, 1);
Chih-Hung Hsieha1ff4752014-10-23 16:50:51 -070084 if(c == 'C') *((volatile unsigned*) 0) = 42;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080085 }
Elliott Hughes5d9fe772014-02-05 17:50:35 -080086 return NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080087}
88
Elliott Hughes6f40caf2013-06-12 14:04:34 -070089static int ctest()
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080090{
91 pthread_t thr;
92 pthread_attr_t attr;
93 pthread_attr_init(&attr);
94 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
95 pthread_create(&thr, &attr, noisy, (void*) 'A');
96 pthread_create(&thr, &attr, noisy, (void*) 'B');
97 pthread_create(&thr, &attr, noisy, (void*) 'C');
98 for(;;) ;
99 return 0;
100}
101
Elliott Hughesaa421302012-12-10 14:15:42 -0800102static void* thread_callback(void* raw_arg)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800103{
Elliott Hughes5d9fe772014-02-05 17:50:35 -0800104 return (void*) (uintptr_t) do_action((const char*) raw_arg);
Elliott Hughesaa421302012-12-10 14:15:42 -0800105}
106
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700107static int do_action_on_thread(const char* arg)
Elliott Hughesaa421302012-12-10 14:15:42 -0800108{
109 pthread_t t;
110 pthread_create(&t, NULL, thread_callback, (void*) arg);
111 void* result = NULL;
112 pthread_join(t, &result);
Elliott Hughes5d9fe772014-02-05 17:50:35 -0800113 return (int) (uintptr_t) result;
Elliott Hughesaa421302012-12-10 14:15:42 -0800114}
115
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700116__attribute__((noinline)) static int crash3(int a) {
117 *((int*) 0xdead) = a;
118 return a*4;
Pavel Chupinaf2cb362013-03-08 13:17:35 +0400119}
120
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700121__attribute__((noinline)) static int crash2(int a) {
122 a = crash3(a) + 2;
123 return a*3;
Pavel Chupinaf2cb362013-03-08 13:17:35 +0400124}
125
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700126__attribute__((noinline)) static int crash(int a) {
127 a = crash2(a) + 1;
128 return a*2;
Pavel Chupinaf2cb362013-03-08 13:17:35 +0400129}
130
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700131static void abuse_heap() {
132 char buf[16];
133 free((void*) buf); // GCC is smart enough to warn about this, but we're doing it deliberately.
134}
135
Brigid Smith7b2078e2014-06-17 14:55:47 -0700136static void sigsegv_non_null() {
137 int* a = (int *)(&do_action);
138 *a = 42;
139}
140
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700141static int do_action(const char* arg)
Elliott Hughesaa421302012-12-10 14:15:42 -0800142{
Josh Gao9c02dc52016-06-15 17:29:00 -0700143 fprintf(stderr, "%s: init pid=%d tid=%d\n", __progname, getpid(), gettid());
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700144
Josh Gao218f7fb2016-10-07 16:42:05 -0700145 if (!strncmp(arg, "exhaustfd-", strlen("exhaustfd-"))) {
146 errno = 0;
147 while (errno != EMFILE) {
148 open("/dev/null", O_RDONLY);
149 }
150 return do_action(arg + strlen("exhaustfd-"));
151 } else if (!strncmp(arg, "thread-", strlen("thread-"))) {
Elliott Hughesaa421302012-12-10 14:15:42 -0800152 return do_action_on_thread(arg + strlen("thread-"));
Brigid Smith7b2078e2014-06-17 14:55:47 -0700153 } else if (!strcmp(arg, "SIGSEGV-non-null")) {
154 sigsegv_non_null();
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700155 } else if (!strcmp(arg, "smash-stack")) {
Yabin Cui2331b952014-12-11 17:46:33 -0800156 volatile int len = 128;
157 return smash_stack(&len);
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700158 } else if (!strcmp(arg, "stack-overflow")) {
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700159 overflow_stack(NULL);
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700160 } else if (!strcmp(arg, "nostack")) {
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700161 crashnostack();
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700162 } else if (!strcmp(arg, "ctest")) {
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700163 return ctest();
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700164 } else if (!strcmp(arg, "exit")) {
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700165 exit(1);
Elliott Hughes855fcc32014-04-25 16:05:34 -0700166 } else if (!strcmp(arg, "crash") || !strcmp(arg, "SIGSEGV")) {
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700167 return crash(42);
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700168 } else if (!strcmp(arg, "abort")) {
169 maybe_abort();
170 } else if (!strcmp(arg, "assert")) {
171 __assert("some_file.c", 123, "false");
172 } else if (!strcmp(arg, "assert2")) {
Elliott Hughes3ecc4212014-07-15 11:38:47 -0700173 __assert2("some_file.c", 123, "some_function", "false");
Elliott Hughes23d1cad2016-05-10 13:29:58 -0700174 } else if (!strcmp(arg, "fortify")) {
175 char buf[10];
176 __read_chk(-1, buf, 32, 10);
177 while (true) pause();
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700178 } else if (!strcmp(arg, "LOG_ALWAYS_FATAL")) {
179 LOG_ALWAYS_FATAL("hello %s", "world");
180 } else if (!strcmp(arg, "LOG_ALWAYS_FATAL_IF")) {
181 LOG_ALWAYS_FATAL_IF(true, "hello %s", "world");
Elliott Hughes3ecc4212014-07-15 11:38:47 -0700182 } else if (!strcmp(arg, "SIGFPE")) {
183 raise(SIGFPE);
184 return EXIT_SUCCESS;
Elliott Hughes7e35ae82014-05-16 17:05:19 -0700185 } else if (!strcmp(arg, "SIGTRAP")) {
186 raise(SIGTRAP);
187 return EXIT_SUCCESS;
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700188 } else if (!strcmp(arg, "heap-usage")) {
189 abuse_heap();
Brigid Smith8606eaa2014-07-07 12:33:50 -0700190 } else if (!strcmp(arg, "SIGSEGV-unmapped")) {
Elliott Hughes23d1cad2016-05-10 13:29:58 -0700191 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 -0700192 munmap(map, sizeof(int));
193 map[0] = '8';
Elliott Hughesaa421302012-12-10 14:15:42 -0800194 }
195
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700196 fprintf(stderr, "%s OP\n", __progname);
197 fprintf(stderr, "where OP is:\n");
Elliott Hughes855fcc32014-04-25 16:05:34 -0700198 fprintf(stderr, " smash-stack overwrite a stack-guard canary\n");
199 fprintf(stderr, " stack-overflow recurse until the stack overflows\n");
200 fprintf(stderr, " heap-corruption cause a libc abort by corrupting the heap\n");
201 fprintf(stderr, " heap-usage cause a libc abort by abusing a heap function\n");
202 fprintf(stderr, " nostack crash with a NULL stack pointer\n");
203 fprintf(stderr, " ctest (obsoleted by thread-crash?)\n");
204 fprintf(stderr, " exit call exit(1)\n");
205 fprintf(stderr, " abort call abort()\n");
206 fprintf(stderr, " assert call assert() without a function\n");
207 fprintf(stderr, " assert2 call assert() with a function\n");
Elliott Hughes23d1cad2016-05-10 13:29:58 -0700208 fprintf(stderr, " fortify fail a _FORTIFY_SOURCE check\n");
Elliott Hughes855fcc32014-04-25 16:05:34 -0700209 fprintf(stderr, " LOG_ALWAYS_FATAL call LOG_ALWAYS_FATAL\n");
210 fprintf(stderr, " LOG_ALWAYS_FATAL_IF call LOG_ALWAYS_FATAL\n");
Elliott Hughes3ecc4212014-07-15 11:38:47 -0700211 fprintf(stderr, " SIGFPE cause a SIGFPE\n");
Brigid Smith7b2078e2014-06-17 14:55:47 -0700212 fprintf(stderr, " SIGSEGV cause a SIGSEGV at address 0x0 (synonym: crash)\n");
213 fprintf(stderr, " SIGSEGV-non-null cause a SIGSEGV at a non-zero address\n");
Brigid Smith8606eaa2014-07-07 12:33:50 -0700214 fprintf(stderr, " SIGSEGV-unmapped mmap/munmap a region of memory and then attempt to access it\n");
Elliott Hughes7e35ae82014-05-16 17:05:19 -0700215 fprintf(stderr, " SIGTRAP cause a SIGTRAP\n");
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700216 fprintf(stderr, "prefix any of the above with 'thread-' to not run\n");
217 fprintf(stderr, "on the process' main thread.\n");
Josh Gao218f7fb2016-10-07 16:42:05 -0700218 fprintf(stderr, "prefix any of the above with 'exhaustfd-' to exhaust\n");
219 fprintf(stderr, "all available file descriptors before crashing.\n");
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700220 return EXIT_SUCCESS;
Elliott Hughesaa421302012-12-10 14:15:42 -0800221}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800222
Elliott Hughesaa421302012-12-10 14:15:42 -0800223int main(int argc, char **argv)
224{
Josh Gao9c02dc52016-06-15 17:29:00 -0700225 fprintf(stderr, "%s: built at " __TIME__ "!@\n", __progname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800226
Josh Gao9c02dc52016-06-15 17:29:00 -0700227#if defined(STATIC_CRASHER)
228 debuggerd_callbacks_t callbacks = {
229 .get_abort_message = []() {
230 static struct {
231 size_t size;
232 char msg[32];
233 } msg;
234
235 msg.size = strlen("dummy abort message");
236 memcpy(msg.msg, "dummy abort message", strlen("dummy abort message"));
237 return reinterpret_cast<abort_msg_t*>(&msg);
238 },
239 .post_dump = nullptr
240 };
241 debuggerd_init(&callbacks);
242#endif
243
244 if (argc > 1) {
Elliott Hughesaa421302012-12-10 14:15:42 -0800245 return do_action(argv[1]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800246 } else {
247 crash1();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800248 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800249
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700250 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800251}