blob: b0e8b175fc85d1969ba0b67eb044c38911c43a93 [file] [log] [blame]
Mark Salyzyncfd5b082016-10-17 14:28:00 -07001/*
2 * Copyright 2006, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "crasher"
18
Elliott Hughesda6b2e22014-04-23 14:57:32 -070019#include <assert.h>
20#include <errno.h>
Josh Gao218f7fb2016-10-07 16:42:05 -070021#include <fcntl.h>
Elliott Hughesda6b2e22014-04-23 14:57:32 -070022#include <pthread.h>
23#include <sched.h>
24#include <signal.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080025#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
Mark Salyzynf1a8dfa2014-04-30 09:24:08 -070028#include <sys/cdefs.h>
Brigid Smith8606eaa2014-07-07 12:33:50 -070029#include <sys/mman.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080030#include <sys/ptrace.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080031#include <sys/socket.h>
Elliott Hughesda6b2e22014-04-23 14:57:32 -070032#include <sys/wait.h>
33#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080034
Mark Salyzynff2dcd92016-09-28 15:54:45 -070035#include <android/log.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080036#include <cutils/sockets.h>
Mark Salyzynf1a8dfa2014-04-30 09:24:08 -070037
Josh Gao9c02dc52016-06-15 17:29:00 -070038#if defined(STATIC_CRASHER)
39#include "debuggerd/client.h"
40#endif
41
Mark Salyzynf1a8dfa2014-04-30 09:24:08 -070042#ifndef __unused
43#define __unused __attribute__((__unused__))
44#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080045
Elliott Hughes3808c4e2013-04-23 17:14:56 -070046extern const char* __progname;
47
Elliott Hughes23d1cad2016-05-10 13:29:58 -070048extern "C" void crash1(void);
49extern "C" void crashnostack(void);
50
Elliott Hughes6f40caf2013-06-12 14:04:34 -070051static int do_action(const char* arg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080052
Elliott Hughesda6b2e22014-04-23 14:57:32 -070053static void maybe_abort() {
54 if (time(0) != 42) {
Elliott Hughes6f40caf2013-06-12 14:04:34 -070055 abort();
56 }
57}
58
Yabin Cui2331b952014-12-11 17:46:33 -080059static char* smash_stack_dummy_buf;
60__attribute__ ((noinline)) static void smash_stack_dummy_function(volatile int* plen) {
61 smash_stack_dummy_buf[*plen] = 0;
62}
63
64// This must be marked with "__attribute__ ((noinline))", to ensure the
65// compiler generates the proper stack guards around this function.
66// Assign local array address to global variable to force stack guards.
67// Use another noinline function to corrupt the stack.
68__attribute__ ((noinline)) static int smash_stack(volatile int* plen) {
Josh Gao9c02dc52016-06-15 17:29:00 -070069 printf("%s: deliberately corrupting stack...\n", __progname);
Yabin Cui2331b952014-12-11 17:46:33 -080070
71 char buf[128];
72 smash_stack_dummy_buf = buf;
73 // This should corrupt stack guards and make process abort.
74 smash_stack_dummy_function(plen);
75 return 0;
Elliott Hughesdf4200e2013-02-14 14:41:57 -080076}
77
Stephen Hines9466bb22015-09-30 23:30:38 -070078#if defined(__clang__)
Stephen Hines18395cb2015-09-29 23:55:14 -070079#pragma clang diagnostic push
80#pragma clang diagnostic ignored "-Winfinite-recursion"
Stephen Hines9466bb22015-09-30 23:30:38 -070081#endif
Stephen Hines18395cb2015-09-29 23:55:14 -070082
Elliott Hughesb1be27e2013-07-15 17:19:02 -070083static void* global = 0; // So GCC doesn't optimize the tail recursion out of overflow_stack.
84
Elliott Hughes6f40caf2013-06-12 14:04:34 -070085__attribute__((noinline)) static void overflow_stack(void* p) {
Elliott Hughes3808c4e2013-04-23 17:14:56 -070086 void* buf[1];
87 buf[0] = p;
Elliott Hughesb1be27e2013-07-15 17:19:02 -070088 global = buf;
Elliott Hughes3808c4e2013-04-23 17:14:56 -070089 overflow_stack(&buf);
90}
91
Stephen Hines9466bb22015-09-30 23:30:38 -070092#if defined(__clang__)
Stephen Hines18395cb2015-09-29 23:55:14 -070093#pragma clang diagnostic pop
Stephen Hines9466bb22015-09-30 23:30:38 -070094#endif
Stephen Hines18395cb2015-09-29 23:55:14 -070095
Elliott Hughes6f40caf2013-06-12 14:04:34 -070096static void *noisy(void *x)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080097{
Elliott Hughes5d9fe772014-02-05 17:50:35 -080098 char c = (uintptr_t) x;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080099 for(;;) {
100 usleep(250*1000);
101 write(2, &c, 1);
Chih-Hung Hsieha1ff4752014-10-23 16:50:51 -0700102 if(c == 'C') *((volatile unsigned*) 0) = 42;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800103 }
Elliott Hughes5d9fe772014-02-05 17:50:35 -0800104 return NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800105}
106
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700107static int ctest()
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800108{
109 pthread_t thr;
110 pthread_attr_t attr;
111 pthread_attr_init(&attr);
112 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
113 pthread_create(&thr, &attr, noisy, (void*) 'A');
114 pthread_create(&thr, &attr, noisy, (void*) 'B');
115 pthread_create(&thr, &attr, noisy, (void*) 'C');
116 for(;;) ;
117 return 0;
118}
119
Elliott Hughesaa421302012-12-10 14:15:42 -0800120static void* thread_callback(void* raw_arg)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800121{
Elliott Hughes5d9fe772014-02-05 17:50:35 -0800122 return (void*) (uintptr_t) do_action((const char*) raw_arg);
Elliott Hughesaa421302012-12-10 14:15:42 -0800123}
124
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700125static int do_action_on_thread(const char* arg)
Elliott Hughesaa421302012-12-10 14:15:42 -0800126{
127 pthread_t t;
128 pthread_create(&t, NULL, thread_callback, (void*) arg);
129 void* result = NULL;
130 pthread_join(t, &result);
Elliott Hughes5d9fe772014-02-05 17:50:35 -0800131 return (int) (uintptr_t) result;
Elliott Hughesaa421302012-12-10 14:15:42 -0800132}
133
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700134__attribute__((noinline)) static int crash3(int a) {
135 *((int*) 0xdead) = a;
136 return a*4;
Pavel Chupinaf2cb362013-03-08 13:17:35 +0400137}
138
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700139__attribute__((noinline)) static int crash2(int a) {
140 a = crash3(a) + 2;
141 return a*3;
Pavel Chupinaf2cb362013-03-08 13:17:35 +0400142}
143
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700144__attribute__((noinline)) static int crash(int a) {
145 a = crash2(a) + 1;
146 return a*2;
Pavel Chupinaf2cb362013-03-08 13:17:35 +0400147}
148
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700149static void abuse_heap() {
150 char buf[16];
151 free((void*) buf); // GCC is smart enough to warn about this, but we're doing it deliberately.
152}
153
Brigid Smith7b2078e2014-06-17 14:55:47 -0700154static void sigsegv_non_null() {
155 int* a = (int *)(&do_action);
156 *a = 42;
157}
158
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700159static int do_action(const char* arg)
Elliott Hughesaa421302012-12-10 14:15:42 -0800160{
Josh Gao9c02dc52016-06-15 17:29:00 -0700161 fprintf(stderr, "%s: init pid=%d tid=%d\n", __progname, getpid(), gettid());
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700162
Josh Gao100ce392016-10-31 17:37:37 -0700163 if (!strncmp(arg, "wait-", strlen("wait-"))) {
164 char buf[1];
165 TEMP_FAILURE_RETRY(read(STDIN_FILENO, buf, sizeof(buf)));
166 return do_action(arg + strlen("wait-"));
167 } else if (!strncmp(arg, "exhaustfd-", strlen("exhaustfd-"))) {
Josh Gao218f7fb2016-10-07 16:42:05 -0700168 errno = 0;
169 while (errno != EMFILE) {
170 open("/dev/null", O_RDONLY);
171 }
172 return do_action(arg + strlen("exhaustfd-"));
173 } else if (!strncmp(arg, "thread-", strlen("thread-"))) {
Elliott Hughesaa421302012-12-10 14:15:42 -0800174 return do_action_on_thread(arg + strlen("thread-"));
Brigid Smith7b2078e2014-06-17 14:55:47 -0700175 } else if (!strcmp(arg, "SIGSEGV-non-null")) {
176 sigsegv_non_null();
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700177 } else if (!strcmp(arg, "smash-stack")) {
Yabin Cui2331b952014-12-11 17:46:33 -0800178 volatile int len = 128;
179 return smash_stack(&len);
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700180 } else if (!strcmp(arg, "stack-overflow")) {
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700181 overflow_stack(NULL);
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700182 } else if (!strcmp(arg, "nostack")) {
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700183 crashnostack();
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700184 } else if (!strcmp(arg, "ctest")) {
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700185 return ctest();
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700186 } else if (!strcmp(arg, "exit")) {
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700187 exit(1);
Elliott Hughes855fcc32014-04-25 16:05:34 -0700188 } else if (!strcmp(arg, "crash") || !strcmp(arg, "SIGSEGV")) {
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700189 return crash(42);
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700190 } else if (!strcmp(arg, "abort")) {
191 maybe_abort();
192 } else if (!strcmp(arg, "assert")) {
193 __assert("some_file.c", 123, "false");
194 } else if (!strcmp(arg, "assert2")) {
Elliott Hughes3ecc4212014-07-15 11:38:47 -0700195 __assert2("some_file.c", 123, "some_function", "false");
Elliott Hughes23d1cad2016-05-10 13:29:58 -0700196 } else if (!strcmp(arg, "fortify")) {
197 char buf[10];
198 __read_chk(-1, buf, 32, 10);
199 while (true) pause();
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700200 } else if (!strcmp(arg, "LOG_ALWAYS_FATAL")) {
201 LOG_ALWAYS_FATAL("hello %s", "world");
202 } else if (!strcmp(arg, "LOG_ALWAYS_FATAL_IF")) {
203 LOG_ALWAYS_FATAL_IF(true, "hello %s", "world");
Elliott Hughes3ecc4212014-07-15 11:38:47 -0700204 } else if (!strcmp(arg, "SIGFPE")) {
205 raise(SIGFPE);
206 return EXIT_SUCCESS;
Elliott Hughes7e35ae82014-05-16 17:05:19 -0700207 } else if (!strcmp(arg, "SIGTRAP")) {
208 raise(SIGTRAP);
209 return EXIT_SUCCESS;
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700210 } else if (!strcmp(arg, "heap-usage")) {
211 abuse_heap();
Brigid Smith8606eaa2014-07-07 12:33:50 -0700212 } else if (!strcmp(arg, "SIGSEGV-unmapped")) {
Elliott Hughes23d1cad2016-05-10 13:29:58 -0700213 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 -0700214 munmap(map, sizeof(int));
215 map[0] = '8';
Elliott Hughesaa421302012-12-10 14:15:42 -0800216 }
217
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700218 fprintf(stderr, "%s OP\n", __progname);
219 fprintf(stderr, "where OP is:\n");
Elliott Hughes855fcc32014-04-25 16:05:34 -0700220 fprintf(stderr, " smash-stack overwrite a stack-guard canary\n");
221 fprintf(stderr, " stack-overflow recurse until the stack overflows\n");
222 fprintf(stderr, " heap-corruption cause a libc abort by corrupting the heap\n");
223 fprintf(stderr, " heap-usage cause a libc abort by abusing a heap function\n");
224 fprintf(stderr, " nostack crash with a NULL stack pointer\n");
225 fprintf(stderr, " ctest (obsoleted by thread-crash?)\n");
226 fprintf(stderr, " exit call exit(1)\n");
227 fprintf(stderr, " abort call abort()\n");
228 fprintf(stderr, " assert call assert() without a function\n");
229 fprintf(stderr, " assert2 call assert() with a function\n");
Elliott Hughes23d1cad2016-05-10 13:29:58 -0700230 fprintf(stderr, " fortify fail a _FORTIFY_SOURCE check\n");
Elliott Hughes855fcc32014-04-25 16:05:34 -0700231 fprintf(stderr, " LOG_ALWAYS_FATAL call LOG_ALWAYS_FATAL\n");
232 fprintf(stderr, " LOG_ALWAYS_FATAL_IF call LOG_ALWAYS_FATAL\n");
Elliott Hughes3ecc4212014-07-15 11:38:47 -0700233 fprintf(stderr, " SIGFPE cause a SIGFPE\n");
Brigid Smith7b2078e2014-06-17 14:55:47 -0700234 fprintf(stderr, " SIGSEGV cause a SIGSEGV at address 0x0 (synonym: crash)\n");
235 fprintf(stderr, " SIGSEGV-non-null cause a SIGSEGV at a non-zero address\n");
Brigid Smith8606eaa2014-07-07 12:33:50 -0700236 fprintf(stderr, " SIGSEGV-unmapped mmap/munmap a region of memory and then attempt to access it\n");
Elliott Hughes7e35ae82014-05-16 17:05:19 -0700237 fprintf(stderr, " SIGTRAP cause a SIGTRAP\n");
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700238 fprintf(stderr, "prefix any of the above with 'thread-' to not run\n");
239 fprintf(stderr, "on the process' main thread.\n");
Josh Gao218f7fb2016-10-07 16:42:05 -0700240 fprintf(stderr, "prefix any of the above with 'exhaustfd-' to exhaust\n");
241 fprintf(stderr, "all available file descriptors before crashing.\n");
Josh Gao100ce392016-10-31 17:37:37 -0700242 fprintf(stderr, "prefix any of the above with 'wait-' to wait until input is received on stdin\n");
243
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700244 return EXIT_SUCCESS;
Elliott Hughesaa421302012-12-10 14:15:42 -0800245}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800246
Elliott Hughesaa421302012-12-10 14:15:42 -0800247int main(int argc, char **argv)
248{
Josh Gao9c02dc52016-06-15 17:29:00 -0700249 fprintf(stderr, "%s: built at " __TIME__ "!@\n", __progname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800250
Josh Gao9c02dc52016-06-15 17:29:00 -0700251#if defined(STATIC_CRASHER)
252 debuggerd_callbacks_t callbacks = {
253 .get_abort_message = []() {
254 static struct {
255 size_t size;
256 char msg[32];
257 } msg;
258
259 msg.size = strlen("dummy abort message");
260 memcpy(msg.msg, "dummy abort message", strlen("dummy abort message"));
261 return reinterpret_cast<abort_msg_t*>(&msg);
262 },
263 .post_dump = nullptr
264 };
265 debuggerd_init(&callbacks);
266#endif
267
268 if (argc > 1) {
Elliott Hughesaa421302012-12-10 14:15:42 -0800269 return do_action(argv[1]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800270 } else {
271 crash1();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800272 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800273
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700274 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800275}