blob: a37df33506b58bd794a2c0b3c22d9b63d2d89144 [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
Josh Gao9c02dc52016-06-15 17:29:00 -070019#if defined(STATIC_CRASHER)
20#include "debuggerd/client.h"
21#endif
22
Mark Salyzynf1a8dfa2014-04-30 09:24:08 -070023#ifndef __unused
24#define __unused __attribute__((__unused__))
25#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080026
Elliott Hughes3808c4e2013-04-23 17:14:56 -070027extern const char* __progname;
28
Elliott Hughes23d1cad2016-05-10 13:29:58 -070029extern "C" void crash1(void);
30extern "C" void crashnostack(void);
31
Elliott Hughes6f40caf2013-06-12 14:04:34 -070032static int do_action(const char* arg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080033
Elliott Hughesda6b2e22014-04-23 14:57:32 -070034static void maybe_abort() {
35 if (time(0) != 42) {
Elliott Hughes6f40caf2013-06-12 14:04:34 -070036 abort();
37 }
38}
39
Yabin Cui2331b952014-12-11 17:46:33 -080040static char* smash_stack_dummy_buf;
41__attribute__ ((noinline)) static void smash_stack_dummy_function(volatile int* plen) {
42 smash_stack_dummy_buf[*plen] = 0;
43}
44
45// This must be marked with "__attribute__ ((noinline))", to ensure the
46// compiler generates the proper stack guards around this function.
47// Assign local array address to global variable to force stack guards.
48// Use another noinline function to corrupt the stack.
49__attribute__ ((noinline)) static int smash_stack(volatile int* plen) {
Josh Gao9c02dc52016-06-15 17:29:00 -070050 printf("%s: deliberately corrupting stack...\n", __progname);
Yabin Cui2331b952014-12-11 17:46:33 -080051
52 char buf[128];
53 smash_stack_dummy_buf = buf;
54 // This should corrupt stack guards and make process abort.
55 smash_stack_dummy_function(plen);
56 return 0;
Elliott Hughesdf4200e2013-02-14 14:41:57 -080057}
58
Stephen Hines9466bb22015-09-30 23:30:38 -070059#if defined(__clang__)
Stephen Hines18395cb2015-09-29 23:55:14 -070060#pragma clang diagnostic push
61#pragma clang diagnostic ignored "-Winfinite-recursion"
Stephen Hines9466bb22015-09-30 23:30:38 -070062#endif
Stephen Hines18395cb2015-09-29 23:55:14 -070063
Elliott Hughesb1be27e2013-07-15 17:19:02 -070064static void* global = 0; // So GCC doesn't optimize the tail recursion out of overflow_stack.
65
Elliott Hughes6f40caf2013-06-12 14:04:34 -070066__attribute__((noinline)) static void overflow_stack(void* p) {
Elliott Hughes3808c4e2013-04-23 17:14:56 -070067 void* buf[1];
68 buf[0] = p;
Elliott Hughesb1be27e2013-07-15 17:19:02 -070069 global = buf;
Elliott Hughes3808c4e2013-04-23 17:14:56 -070070 overflow_stack(&buf);
71}
72
Stephen Hines9466bb22015-09-30 23:30:38 -070073#if defined(__clang__)
Stephen Hines18395cb2015-09-29 23:55:14 -070074#pragma clang diagnostic pop
Stephen Hines9466bb22015-09-30 23:30:38 -070075#endif
Stephen Hines18395cb2015-09-29 23:55:14 -070076
Elliott Hughes6f40caf2013-06-12 14:04:34 -070077static void *noisy(void *x)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080078{
Elliott Hughes5d9fe772014-02-05 17:50:35 -080079 char c = (uintptr_t) x;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080080 for(;;) {
81 usleep(250*1000);
82 write(2, &c, 1);
Chih-Hung Hsieha1ff4752014-10-23 16:50:51 -070083 if(c == 'C') *((volatile unsigned*) 0) = 42;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080084 }
Elliott Hughes5d9fe772014-02-05 17:50:35 -080085 return NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080086}
87
Elliott Hughes6f40caf2013-06-12 14:04:34 -070088static int ctest()
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080089{
90 pthread_t thr;
91 pthread_attr_t attr;
92 pthread_attr_init(&attr);
93 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
94 pthread_create(&thr, &attr, noisy, (void*) 'A');
95 pthread_create(&thr, &attr, noisy, (void*) 'B');
96 pthread_create(&thr, &attr, noisy, (void*) 'C');
97 for(;;) ;
98 return 0;
99}
100
Elliott Hughesaa421302012-12-10 14:15:42 -0800101static void* thread_callback(void* raw_arg)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800102{
Elliott Hughes5d9fe772014-02-05 17:50:35 -0800103 return (void*) (uintptr_t) do_action((const char*) raw_arg);
Elliott Hughesaa421302012-12-10 14:15:42 -0800104}
105
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700106static int do_action_on_thread(const char* arg)
Elliott Hughesaa421302012-12-10 14:15:42 -0800107{
108 pthread_t t;
109 pthread_create(&t, NULL, thread_callback, (void*) arg);
110 void* result = NULL;
111 pthread_join(t, &result);
Elliott Hughes5d9fe772014-02-05 17:50:35 -0800112 return (int) (uintptr_t) result;
Elliott Hughesaa421302012-12-10 14:15:42 -0800113}
114
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700115__attribute__((noinline)) static int crash3(int a) {
116 *((int*) 0xdead) = a;
117 return a*4;
Pavel Chupinaf2cb362013-03-08 13:17:35 +0400118}
119
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700120__attribute__((noinline)) static int crash2(int a) {
121 a = crash3(a) + 2;
122 return a*3;
Pavel Chupinaf2cb362013-03-08 13:17:35 +0400123}
124
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700125__attribute__((noinline)) static int crash(int a) {
126 a = crash2(a) + 1;
127 return a*2;
Pavel Chupinaf2cb362013-03-08 13:17:35 +0400128}
129
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700130static void abuse_heap() {
131 char buf[16];
132 free((void*) buf); // GCC is smart enough to warn about this, but we're doing it deliberately.
133}
134
Brigid Smith7b2078e2014-06-17 14:55:47 -0700135static void sigsegv_non_null() {
136 int* a = (int *)(&do_action);
137 *a = 42;
138}
139
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700140static int do_action(const char* arg)
Elliott Hughesaa421302012-12-10 14:15:42 -0800141{
Josh Gao9c02dc52016-06-15 17:29:00 -0700142 fprintf(stderr, "%s: init pid=%d tid=%d\n", __progname, getpid(), gettid());
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700143
144 if (!strncmp(arg, "thread-", strlen("thread-"))) {
Elliott Hughesaa421302012-12-10 14:15:42 -0800145 return do_action_on_thread(arg + strlen("thread-"));
Brigid Smith7b2078e2014-06-17 14:55:47 -0700146 } else if (!strcmp(arg, "SIGSEGV-non-null")) {
147 sigsegv_non_null();
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700148 } else if (!strcmp(arg, "smash-stack")) {
Yabin Cui2331b952014-12-11 17:46:33 -0800149 volatile int len = 128;
150 return smash_stack(&len);
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700151 } else if (!strcmp(arg, "stack-overflow")) {
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700152 overflow_stack(NULL);
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700153 } else if (!strcmp(arg, "nostack")) {
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700154 crashnostack();
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700155 } else if (!strcmp(arg, "ctest")) {
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700156 return ctest();
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700157 } else if (!strcmp(arg, "exit")) {
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700158 exit(1);
Elliott Hughes855fcc32014-04-25 16:05:34 -0700159 } else if (!strcmp(arg, "crash") || !strcmp(arg, "SIGSEGV")) {
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700160 return crash(42);
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700161 } else if (!strcmp(arg, "abort")) {
162 maybe_abort();
163 } else if (!strcmp(arg, "assert")) {
164 __assert("some_file.c", 123, "false");
165 } else if (!strcmp(arg, "assert2")) {
Elliott Hughes3ecc4212014-07-15 11:38:47 -0700166 __assert2("some_file.c", 123, "some_function", "false");
Elliott Hughes23d1cad2016-05-10 13:29:58 -0700167 } else if (!strcmp(arg, "fortify")) {
168 char buf[10];
169 __read_chk(-1, buf, 32, 10);
170 while (true) pause();
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700171 } else if (!strcmp(arg, "LOG_ALWAYS_FATAL")) {
172 LOG_ALWAYS_FATAL("hello %s", "world");
173 } else if (!strcmp(arg, "LOG_ALWAYS_FATAL_IF")) {
174 LOG_ALWAYS_FATAL_IF(true, "hello %s", "world");
Elliott Hughes3ecc4212014-07-15 11:38:47 -0700175 } else if (!strcmp(arg, "SIGFPE")) {
176 raise(SIGFPE);
177 return EXIT_SUCCESS;
Elliott Hughes7e35ae82014-05-16 17:05:19 -0700178 } else if (!strcmp(arg, "SIGTRAP")) {
179 raise(SIGTRAP);
180 return EXIT_SUCCESS;
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700181 } else if (!strcmp(arg, "heap-usage")) {
182 abuse_heap();
Brigid Smith8606eaa2014-07-07 12:33:50 -0700183 } else if (!strcmp(arg, "SIGSEGV-unmapped")) {
Elliott Hughes23d1cad2016-05-10 13:29:58 -0700184 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 -0700185 munmap(map, sizeof(int));
186 map[0] = '8';
Elliott Hughesaa421302012-12-10 14:15:42 -0800187 }
188
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700189 fprintf(stderr, "%s OP\n", __progname);
190 fprintf(stderr, "where OP is:\n");
Elliott Hughes855fcc32014-04-25 16:05:34 -0700191 fprintf(stderr, " smash-stack overwrite a stack-guard canary\n");
192 fprintf(stderr, " stack-overflow recurse until the stack overflows\n");
193 fprintf(stderr, " heap-corruption cause a libc abort by corrupting the heap\n");
194 fprintf(stderr, " heap-usage cause a libc abort by abusing a heap function\n");
195 fprintf(stderr, " nostack crash with a NULL stack pointer\n");
196 fprintf(stderr, " ctest (obsoleted by thread-crash?)\n");
197 fprintf(stderr, " exit call exit(1)\n");
198 fprintf(stderr, " abort call abort()\n");
199 fprintf(stderr, " assert call assert() without a function\n");
200 fprintf(stderr, " assert2 call assert() with a function\n");
Elliott Hughes23d1cad2016-05-10 13:29:58 -0700201 fprintf(stderr, " fortify fail a _FORTIFY_SOURCE check\n");
Elliott Hughes855fcc32014-04-25 16:05:34 -0700202 fprintf(stderr, " LOG_ALWAYS_FATAL call LOG_ALWAYS_FATAL\n");
203 fprintf(stderr, " LOG_ALWAYS_FATAL_IF call LOG_ALWAYS_FATAL\n");
Elliott Hughes3ecc4212014-07-15 11:38:47 -0700204 fprintf(stderr, " SIGFPE cause a SIGFPE\n");
Brigid Smith7b2078e2014-06-17 14:55:47 -0700205 fprintf(stderr, " SIGSEGV cause a SIGSEGV at address 0x0 (synonym: crash)\n");
206 fprintf(stderr, " SIGSEGV-non-null cause a SIGSEGV at a non-zero address\n");
Brigid Smith8606eaa2014-07-07 12:33:50 -0700207 fprintf(stderr, " SIGSEGV-unmapped mmap/munmap a region of memory and then attempt to access it\n");
Elliott Hughes7e35ae82014-05-16 17:05:19 -0700208 fprintf(stderr, " SIGTRAP cause a SIGTRAP\n");
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700209 fprintf(stderr, "prefix any of the above with 'thread-' to not run\n");
210 fprintf(stderr, "on the process' main thread.\n");
211 return EXIT_SUCCESS;
Elliott Hughesaa421302012-12-10 14:15:42 -0800212}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800213
Elliott Hughesaa421302012-12-10 14:15:42 -0800214int main(int argc, char **argv)
215{
Josh Gao9c02dc52016-06-15 17:29:00 -0700216 fprintf(stderr, "%s: built at " __TIME__ "!@\n", __progname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800217
Josh Gao9c02dc52016-06-15 17:29:00 -0700218#if defined(STATIC_CRASHER)
219 debuggerd_callbacks_t callbacks = {
220 .get_abort_message = []() {
221 static struct {
222 size_t size;
223 char msg[32];
224 } msg;
225
226 msg.size = strlen("dummy abort message");
227 memcpy(msg.msg, "dummy abort message", strlen("dummy abort message"));
228 return reinterpret_cast<abort_msg_t*>(&msg);
229 },
230 .post_dump = nullptr
231 };
232 debuggerd_init(&callbacks);
233#endif
234
235 if (argc > 1) {
Elliott Hughesaa421302012-12-10 14:15:42 -0800236 return do_action(argv[1]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800237 } else {
238 crash1();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800239 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800240
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700241 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800242}