blob: 689f4d4c41b4a7a58242758301f551abb8dd34aa [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>
Elliott Hughes400628a2016-12-14 17:33:46 -080020#include <dirent.h>
Elliott Hughesda6b2e22014-04-23 14:57:32 -070021#include <errno.h>
Josh Gao218f7fb2016-10-07 16:42:05 -070022#include <fcntl.h>
Elliott Hughesda6b2e22014-04-23 14:57:32 -070023#include <pthread.h>
Elliott Hughesda6b2e22014-04-23 14:57:32 -070024#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>
Brigid Smith8606eaa2014-07-07 12:33:50 -070028#include <sys/mman.h>
Elliott Hughesda6b2e22014-04-23 14:57:32 -070029#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080030
Elliott Hughes400628a2016-12-14 17:33:46 -080031// We test both kinds of logging.
Elliott Hughes400628a2016-12-14 17:33:46 -080032#include <android-base/logging.h>
Mark Salyzyn51c33b72017-01-12 15:44:06 -080033#include <log/log.h>
Mark Salyzynf1a8dfa2014-04-30 09:24:08 -070034
Josh Gao9c02dc52016-06-15 17:29:00 -070035#if defined(STATIC_CRASHER)
36#include "debuggerd/client.h"
37#endif
38
Elliott Hughes400628a2016-12-14 17:33:46 -080039#define noinline __attribute__((__noinline__))
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080040
Elliott Hughes400628a2016-12-14 17:33:46 -080041// Avoid name mangling so that stacks are more readable.
42extern "C" {
Elliott Hughes3808c4e2013-04-23 17:14:56 -070043
Elliott Hughes400628a2016-12-14 17:33:46 -080044void crash1(void);
45void crashnostack(void);
Elliott Hughes23d1cad2016-05-10 13:29:58 -070046
Elliott Hughes400628a2016-12-14 17:33:46 -080047int do_action(const char* arg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080048
Elliott Hughes400628a2016-12-14 17:33:46 -080049noinline void maybe_abort() {
Elliott Hughesda6b2e22014-04-23 14:57:32 -070050 if (time(0) != 42) {
Elliott Hughes6f40caf2013-06-12 14:04:34 -070051 abort();
52 }
53}
54
Elliott Hughes400628a2016-12-14 17:33:46 -080055char* smash_stack_dummy_buf;
56noinline void smash_stack_dummy_function(volatile int* plen) {
Yabin Cui2331b952014-12-11 17:46:33 -080057 smash_stack_dummy_buf[*plen] = 0;
58}
59
60// This must be marked with "__attribute__ ((noinline))", to ensure the
61// compiler generates the proper stack guards around this function.
62// Assign local array address to global variable to force stack guards.
63// Use another noinline function to corrupt the stack.
Elliott Hughes400628a2016-12-14 17:33:46 -080064noinline int smash_stack(volatile int* plen) {
65 printf("%s: deliberately corrupting stack...\n", getprogname());
Yabin Cui2331b952014-12-11 17:46:33 -080066
67 char buf[128];
68 smash_stack_dummy_buf = buf;
69 // This should corrupt stack guards and make process abort.
70 smash_stack_dummy_function(plen);
71 return 0;
Elliott Hughesdf4200e2013-02-14 14:41:57 -080072}
73
Stephen Hines18395cb2015-09-29 23:55:14 -070074#pragma clang diagnostic push
75#pragma clang diagnostic ignored "-Winfinite-recursion"
76
Elliott Hughes400628a2016-12-14 17:33:46 -080077void* global = 0; // So GCC doesn't optimize the tail recursion out of overflow_stack.
Elliott Hughesb1be27e2013-07-15 17:19:02 -070078
Elliott Hughes400628a2016-12-14 17:33:46 -080079noinline void overflow_stack(void* p) {
Elliott Hughes3808c4e2013-04-23 17:14:56 -070080 void* buf[1];
81 buf[0] = p;
Elliott Hughesb1be27e2013-07-15 17:19:02 -070082 global = buf;
Elliott Hughes3808c4e2013-04-23 17:14:56 -070083 overflow_stack(&buf);
84}
85
Stephen Hines18395cb2015-09-29 23:55:14 -070086#pragma clang diagnostic pop
87
Elliott Hughes400628a2016-12-14 17:33:46 -080088noinline void* thread_callback(void* raw_arg) {
89 const char* arg = reinterpret_cast<const char*>(raw_arg);
90 return reinterpret_cast<void*>(static_cast<uintptr_t>(do_action(arg)));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080091}
92
Elliott Hughes400628a2016-12-14 17:33:46 -080093noinline int do_action_on_thread(const char* arg) {
Elliott Hughesaa421302012-12-10 14:15:42 -080094 pthread_t t;
Elliott Hughes400628a2016-12-14 17:33:46 -080095 pthread_create(&t, nullptr, thread_callback, const_cast<char*>(arg));
96 void* result = nullptr;
Elliott Hughesaa421302012-12-10 14:15:42 -080097 pthread_join(t, &result);
Elliott Hughes400628a2016-12-14 17:33:46 -080098 return reinterpret_cast<uintptr_t>(result);
Elliott Hughesaa421302012-12-10 14:15:42 -080099}
100
Elliott Hughes400628a2016-12-14 17:33:46 -0800101noinline int crash3(int a) {
102 *reinterpret_cast<int*>(0xdead) = a;
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700103 return a*4;
Pavel Chupinaf2cb362013-03-08 13:17:35 +0400104}
105
Elliott Hughes400628a2016-12-14 17:33:46 -0800106noinline int crash2(int a) {
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700107 a = crash3(a) + 2;
108 return a*3;
Pavel Chupinaf2cb362013-03-08 13:17:35 +0400109}
110
Elliott Hughes400628a2016-12-14 17:33:46 -0800111noinline int crash(int a) {
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700112 a = crash2(a) + 1;
113 return a*2;
Pavel Chupinaf2cb362013-03-08 13:17:35 +0400114}
115
Elliott Hughes400628a2016-12-14 17:33:46 -0800116noinline void abuse_heap() {
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700117 char buf[16];
Elliott Hughes400628a2016-12-14 17:33:46 -0800118 free(buf); // GCC is smart enough to warn about this, but we're doing it deliberately.
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700119}
120
Elliott Hughes400628a2016-12-14 17:33:46 -0800121noinline void sigsegv_non_null() {
Brigid Smith7b2078e2014-06-17 14:55:47 -0700122 int* a = (int *)(&do_action);
123 *a = 42;
124}
125
Elliott Hughes400628a2016-12-14 17:33:46 -0800126noinline void fprintf_null() {
127 fprintf(nullptr, "oops");
128}
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700129
Elliott Hughes400628a2016-12-14 17:33:46 -0800130noinline void readdir_null() {
131 readdir(nullptr);
132}
133
134noinline int strlen_null() {
135 char* sneaky_null = nullptr;
136 return strlen(sneaky_null);
137}
138
139static int usage() {
140 fprintf(stderr, "usage: %s KIND\n", getprogname());
141 fprintf(stderr, "\n");
142 fprintf(stderr, "where KIND is:\n");
143 fprintf(stderr, " smash-stack overwrite a -fstack-protector guard\n");
144 fprintf(stderr, " stack-overflow recurse until the stack overflows\n");
145 fprintf(stderr, " heap-corruption cause a libc abort by corrupting the heap\n");
146 fprintf(stderr, " heap-usage cause a libc abort by abusing a heap function\n");
147 fprintf(stderr, " nostack crash with a NULL stack pointer\n");
148 fprintf(stderr, " abort call abort()\n");
149 fprintf(stderr, " assert call assert() without a function\n");
150 fprintf(stderr, " assert2 call assert() with a function\n");
151 fprintf(stderr, " exit call exit(1)\n");
152 fprintf(stderr, " fortify fail a _FORTIFY_SOURCE check\n");
153 fprintf(stderr, " LOG_ALWAYS_FATAL call liblog LOG_ALWAYS_FATAL\n");
154 fprintf(stderr, " LOG_ALWAYS_FATAL_IF call liblog LOG_ALWAYS_FATAL_IF\n");
155 fprintf(stderr, " LOG-FATAL call libbase LOG(FATAL)\n");
156 fprintf(stderr, " SIGFPE cause a SIGFPE\n");
157 fprintf(stderr, " SIGSEGV cause a SIGSEGV at address 0x0 (synonym: crash)\n");
158 fprintf(stderr, " SIGSEGV-non-null cause a SIGSEGV at a non-zero address\n");
159 fprintf(stderr, " SIGSEGV-unmapped mmap/munmap a region of memory and then attempt to access it\n");
160 fprintf(stderr, " SIGTRAP cause a SIGTRAP\n");
161 fprintf(stderr, " fprintf-NULL pass a null pointer to fprintf\n");
162 fprintf(stderr, " readdir-NULL pass a null pointer to readdir\n");
163 fprintf(stderr, " strlen-NULL pass a null pointer to strlen\n");
164 fprintf(stderr, "\n");
165 fprintf(stderr, "prefix any of the above with 'thread-' to run on a new thread\n");
166 fprintf(stderr, "prefix any of the above with 'exhaustfd-' to exhaust\n");
167 fprintf(stderr, "all available file descriptors before crashing.\n");
168 fprintf(stderr, "prefix any of the above with 'wait-' to wait until input is received on stdin\n");
169
170 return EXIT_FAILURE;
171}
172
173noinline int do_action(const char* arg) {
174 // Prefixes.
Josh Gao100ce392016-10-31 17:37:37 -0700175 if (!strncmp(arg, "wait-", strlen("wait-"))) {
176 char buf[1];
177 TEMP_FAILURE_RETRY(read(STDIN_FILENO, buf, sizeof(buf)));
178 return do_action(arg + strlen("wait-"));
179 } else if (!strncmp(arg, "exhaustfd-", strlen("exhaustfd-"))) {
Josh Gao218f7fb2016-10-07 16:42:05 -0700180 errno = 0;
181 while (errno != EMFILE) {
182 open("/dev/null", O_RDONLY);
183 }
184 return do_action(arg + strlen("exhaustfd-"));
185 } else if (!strncmp(arg, "thread-", strlen("thread-"))) {
Elliott Hughesaa421302012-12-10 14:15:42 -0800186 return do_action_on_thread(arg + strlen("thread-"));
Elliott Hughes400628a2016-12-14 17:33:46 -0800187 }
188
189 // Actions.
190 if (!strcasecmp(arg, "SIGSEGV-non-null")) {
Brigid Smith7b2078e2014-06-17 14:55:47 -0700191 sigsegv_non_null();
Elliott Hughes400628a2016-12-14 17:33:46 -0800192 } else if (!strcasecmp(arg, "smash-stack")) {
Yabin Cui2331b952014-12-11 17:46:33 -0800193 volatile int len = 128;
194 return smash_stack(&len);
Elliott Hughes400628a2016-12-14 17:33:46 -0800195 } else if (!strcasecmp(arg, "stack-overflow")) {
196 overflow_stack(nullptr);
197 } else if (!strcasecmp(arg, "nostack")) {
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700198 crashnostack();
Elliott Hughes400628a2016-12-14 17:33:46 -0800199 } else if (!strcasecmp(arg, "exit")) {
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700200 exit(1);
Elliott Hughes400628a2016-12-14 17:33:46 -0800201 } else if (!strcasecmp(arg, "crash") || !strcmp(arg, "SIGSEGV")) {
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700202 return crash(42);
Elliott Hughes400628a2016-12-14 17:33:46 -0800203 } else if (!strcasecmp(arg, "abort")) {
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700204 maybe_abort();
Elliott Hughes400628a2016-12-14 17:33:46 -0800205 } else if (!strcasecmp(arg, "assert")) {
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700206 __assert("some_file.c", 123, "false");
Elliott Hughes400628a2016-12-14 17:33:46 -0800207 } else if (!strcasecmp(arg, "assert2")) {
Elliott Hughes3ecc4212014-07-15 11:38:47 -0700208 __assert2("some_file.c", 123, "some_function", "false");
Elliott Hughes400628a2016-12-14 17:33:46 -0800209 } else if (!strcasecmp(arg, "fortify")) {
Elliott Hughes23d1cad2016-05-10 13:29:58 -0700210 char buf[10];
211 __read_chk(-1, buf, 32, 10);
212 while (true) pause();
Elliott Hughes400628a2016-12-14 17:33:46 -0800213 } else if (!strcasecmp(arg, "LOG(FATAL)")) {
214 LOG(FATAL) << "hello " << 123;
215 } else if (!strcasecmp(arg, "LOG_ALWAYS_FATAL")) {
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700216 LOG_ALWAYS_FATAL("hello %s", "world");
Elliott Hughes400628a2016-12-14 17:33:46 -0800217 } else if (!strcasecmp(arg, "LOG_ALWAYS_FATAL_IF")) {
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700218 LOG_ALWAYS_FATAL_IF(true, "hello %s", "world");
Elliott Hughes400628a2016-12-14 17:33:46 -0800219 } else if (!strcasecmp(arg, "SIGFPE")) {
Elliott Hughes3ecc4212014-07-15 11:38:47 -0700220 raise(SIGFPE);
221 return EXIT_SUCCESS;
Elliott Hughes400628a2016-12-14 17:33:46 -0800222 } else if (!strcasecmp(arg, "SIGTRAP")) {
Elliott Hughes7e35ae82014-05-16 17:05:19 -0700223 raise(SIGTRAP);
224 return EXIT_SUCCESS;
Elliott Hughes400628a2016-12-14 17:33:46 -0800225 } else if (!strcasecmp(arg, "fprintf-NULL")) {
226 fprintf_null();
227 } else if (!strcasecmp(arg, "readdir-NULL")) {
228 readdir_null();
229 } else if (!strcasecmp(arg, "strlen-NULL")) {
230 return strlen_null();
231 } else if (!strcasecmp(arg, "heap-usage")) {
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700232 abuse_heap();
Elliott Hughes400628a2016-12-14 17:33:46 -0800233 } else if (!strcasecmp(arg, "SIGSEGV-unmapped")) {
234 char* map = reinterpret_cast<char*>(mmap(nullptr, sizeof(int), PROT_READ | PROT_WRITE,
235 MAP_SHARED | MAP_ANONYMOUS, -1, 0));
Brigid Smith8606eaa2014-07-07 12:33:50 -0700236 munmap(map, sizeof(int));
237 map[0] = '8';
Elliott Hughes400628a2016-12-14 17:33:46 -0800238 } else {
239 return usage();
Elliott Hughesaa421302012-12-10 14:15:42 -0800240 }
241
Elliott Hughes400628a2016-12-14 17:33:46 -0800242 fprintf(stderr, "%s: exiting normally!\n", getprogname());
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700243 return EXIT_SUCCESS;
Elliott Hughesaa421302012-12-10 14:15:42 -0800244}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800245
Elliott Hughes400628a2016-12-14 17:33:46 -0800246int main(int argc, char** argv) {
Josh Gao9c02dc52016-06-15 17:29:00 -0700247#if defined(STATIC_CRASHER)
248 debuggerd_callbacks_t callbacks = {
249 .get_abort_message = []() {
250 static struct {
251 size_t size;
252 char msg[32];
253 } msg;
254
255 msg.size = strlen("dummy abort message");
256 memcpy(msg.msg, "dummy abort message", strlen("dummy abort message"));
257 return reinterpret_cast<abort_msg_t*>(&msg);
258 },
259 .post_dump = nullptr
260 };
261 debuggerd_init(&callbacks);
262#endif
263
Elliott Hughes400628a2016-12-14 17:33:46 -0800264 if (argc == 1) crash1();
265 else if (argc == 2) return do_action(argv[1]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800266
Elliott Hughes400628a2016-12-14 17:33:46 -0800267 return usage();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800268}
Elliott Hughes400628a2016-12-14 17:33:46 -0800269
270};