| Mark Salyzyn | cfd5b08 | 2016-10-17 14:28:00 -0700 | [diff] [blame] | 1 | /* | 
|  | 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 Hughes | da6b2e2 | 2014-04-23 14:57:32 -0700 | [diff] [blame] | 19 | #include <assert.h> | 
| Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 20 | #include <dirent.h> | 
| Elliott Hughes | da6b2e2 | 2014-04-23 14:57:32 -0700 | [diff] [blame] | 21 | #include <errno.h> | 
| Josh Gao | 218f7fb | 2016-10-07 16:42:05 -0700 | [diff] [blame] | 22 | #include <fcntl.h> | 
| Elliott Hughes | da6b2e2 | 2014-04-23 14:57:32 -0700 | [diff] [blame] | 23 | #include <pthread.h> | 
| Elliott Hughes | da6b2e2 | 2014-04-23 14:57:32 -0700 | [diff] [blame] | 24 | #include <signal.h> | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 25 | #include <stdio.h> | 
|  | 26 | #include <stdlib.h> | 
|  | 27 | #include <string.h> | 
| Brigid Smith | 8606eaa | 2014-07-07 12:33:50 -0700 | [diff] [blame] | 28 | #include <sys/mman.h> | 
| Josh Gao | 91ad653 | 2017-02-09 12:37:39 -0800 | [diff] [blame] | 29 | #include <sys/prctl.h> | 
| Elliott Hughes | da6b2e2 | 2014-04-23 14:57:32 -0700 | [diff] [blame] | 30 | #include <unistd.h> | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 31 |  | 
| Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 32 | // We test both kinds of logging. | 
| Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 33 | #include <android-base/logging.h> | 
| Mark Salyzyn | 51c33b7 | 2017-01-12 15:44:06 -0800 | [diff] [blame] | 34 | #include <log/log.h> | 
| Mark Salyzyn | f1a8dfa | 2014-04-30 09:24:08 -0700 | [diff] [blame] | 35 |  | 
| Josh Gao | 9c02dc5 | 2016-06-15 17:29:00 -0700 | [diff] [blame] | 36 | #if defined(STATIC_CRASHER) | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 37 | #include "debuggerd/handler.h" | 
| Josh Gao | 9c02dc5 | 2016-06-15 17:29:00 -0700 | [diff] [blame] | 38 | #endif | 
|  | 39 |  | 
| Elliott Hughes | 0ba5359 | 2017-02-01 16:59:15 -0800 | [diff] [blame] | 40 | #if defined(__arm__) | 
|  | 41 | // See https://www.kernel.org/doc/Documentation/arm/kernel_user_helpers.txt for details. | 
|  | 42 | #define __kuser_helper_version (*(int32_t*) 0xffff0ffc) | 
|  | 43 | typedef void * (__kuser_get_tls_t)(void); | 
|  | 44 | #define __kuser_get_tls (*(__kuser_get_tls_t*) 0xffff0fe0) | 
|  | 45 | typedef int (__kuser_cmpxchg_t)(int oldval, int newval, volatile int *ptr); | 
|  | 46 | #define __kuser_cmpxchg (*(__kuser_cmpxchg_t*) 0xffff0fc0) | 
|  | 47 | typedef void (__kuser_dmb_t)(void); | 
|  | 48 | #define __kuser_dmb (*(__kuser_dmb_t*) 0xffff0fa0) | 
|  | 49 | typedef int (__kuser_cmpxchg64_t)(const int64_t*, const int64_t*, volatile int64_t*); | 
|  | 50 | #define __kuser_cmpxchg64 (*(__kuser_cmpxchg64_t*) 0xffff0f60) | 
|  | 51 | #endif | 
|  | 52 |  | 
| Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 53 | #define noinline __attribute__((__noinline__)) | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 54 |  | 
| Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 55 | // Avoid name mangling so that stacks are more readable. | 
|  | 56 | extern "C" { | 
| Elliott Hughes | 3808c4e | 2013-04-23 17:14:56 -0700 | [diff] [blame] | 57 |  | 
| Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 58 | void crash1(void); | 
|  | 59 | void crashnostack(void); | 
| Elliott Hughes | 23d1cad | 2016-05-10 13:29:58 -0700 | [diff] [blame] | 60 |  | 
| Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 61 | int do_action(const char* arg); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 62 |  | 
| Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 63 | noinline void maybe_abort() { | 
| Elliott Hughes | da6b2e2 | 2014-04-23 14:57:32 -0700 | [diff] [blame] | 64 | if (time(0) != 42) { | 
| Elliott Hughes | 6f40caf | 2013-06-12 14:04:34 -0700 | [diff] [blame] | 65 | abort(); | 
|  | 66 | } | 
|  | 67 | } | 
|  | 68 |  | 
| Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 69 | char* smash_stack_dummy_buf; | 
|  | 70 | noinline void smash_stack_dummy_function(volatile int* plen) { | 
| Yabin Cui | 2331b95 | 2014-12-11 17:46:33 -0800 | [diff] [blame] | 71 | smash_stack_dummy_buf[*plen] = 0; | 
|  | 72 | } | 
|  | 73 |  | 
|  | 74 | // This must be marked with "__attribute__ ((noinline))", to ensure the | 
|  | 75 | // compiler generates the proper stack guards around this function. | 
|  | 76 | // Assign local array address to global variable to force stack guards. | 
|  | 77 | // Use another noinline function to corrupt the stack. | 
| Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 78 | noinline int smash_stack(volatile int* plen) { | 
|  | 79 | printf("%s: deliberately corrupting stack...\n", getprogname()); | 
| Yabin Cui | 2331b95 | 2014-12-11 17:46:33 -0800 | [diff] [blame] | 80 |  | 
|  | 81 | char buf[128]; | 
|  | 82 | smash_stack_dummy_buf = buf; | 
|  | 83 | // This should corrupt stack guards and make process abort. | 
|  | 84 | smash_stack_dummy_function(plen); | 
|  | 85 | return 0; | 
| Elliott Hughes | df4200e | 2013-02-14 14:41:57 -0800 | [diff] [blame] | 86 | } | 
|  | 87 |  | 
| Stephen Hines | 18395cb | 2015-09-29 23:55:14 -0700 | [diff] [blame] | 88 | #pragma clang diagnostic push | 
|  | 89 | #pragma clang diagnostic ignored "-Winfinite-recursion" | 
|  | 90 |  | 
| Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 91 | void* global = 0; // So GCC doesn't optimize the tail recursion out of overflow_stack. | 
| Elliott Hughes | b1be27e | 2013-07-15 17:19:02 -0700 | [diff] [blame] | 92 |  | 
| Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 93 | noinline void overflow_stack(void* p) { | 
| Elliott Hughes | 3808c4e | 2013-04-23 17:14:56 -0700 | [diff] [blame] | 94 | void* buf[1]; | 
|  | 95 | buf[0] = p; | 
| Elliott Hughes | b1be27e | 2013-07-15 17:19:02 -0700 | [diff] [blame] | 96 | global = buf; | 
| Elliott Hughes | 3808c4e | 2013-04-23 17:14:56 -0700 | [diff] [blame] | 97 | overflow_stack(&buf); | 
|  | 98 | } | 
|  | 99 |  | 
| Stephen Hines | 18395cb | 2015-09-29 23:55:14 -0700 | [diff] [blame] | 100 | #pragma clang diagnostic pop | 
|  | 101 |  | 
| Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 102 | noinline void* thread_callback(void* raw_arg) { | 
|  | 103 | const char* arg = reinterpret_cast<const char*>(raw_arg); | 
|  | 104 | return reinterpret_cast<void*>(static_cast<uintptr_t>(do_action(arg))); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 105 | } | 
|  | 106 |  | 
| Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 107 | noinline int do_action_on_thread(const char* arg) { | 
| Elliott Hughes | aa42130 | 2012-12-10 14:15:42 -0800 | [diff] [blame] | 108 | pthread_t t; | 
| Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 109 | pthread_create(&t, nullptr, thread_callback, const_cast<char*>(arg)); | 
|  | 110 | void* result = nullptr; | 
| Elliott Hughes | aa42130 | 2012-12-10 14:15:42 -0800 | [diff] [blame] | 111 | pthread_join(t, &result); | 
| Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 112 | return reinterpret_cast<uintptr_t>(result); | 
| Elliott Hughes | aa42130 | 2012-12-10 14:15:42 -0800 | [diff] [blame] | 113 | } | 
|  | 114 |  | 
| Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 115 | noinline int crash3(int a) { | 
|  | 116 | *reinterpret_cast<int*>(0xdead) = a; | 
| Elliott Hughes | 6f40caf | 2013-06-12 14:04:34 -0700 | [diff] [blame] | 117 | return a*4; | 
| Pavel Chupin | af2cb36 | 2013-03-08 13:17:35 +0400 | [diff] [blame] | 118 | } | 
|  | 119 |  | 
| Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 120 | noinline int crash2(int a) { | 
| Elliott Hughes | 6f40caf | 2013-06-12 14:04:34 -0700 | [diff] [blame] | 121 | a = crash3(a) + 2; | 
|  | 122 | return a*3; | 
| Pavel Chupin | af2cb36 | 2013-03-08 13:17:35 +0400 | [diff] [blame] | 123 | } | 
|  | 124 |  | 
| Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 125 | noinline int crash(int a) { | 
| Elliott Hughes | 6f40caf | 2013-06-12 14:04:34 -0700 | [diff] [blame] | 126 | a = crash2(a) + 1; | 
|  | 127 | return a*2; | 
| Pavel Chupin | af2cb36 | 2013-03-08 13:17:35 +0400 | [diff] [blame] | 128 | } | 
|  | 129 |  | 
| Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 130 | noinline void abuse_heap() { | 
| Elliott Hughes | 6f40caf | 2013-06-12 14:04:34 -0700 | [diff] [blame] | 131 | char buf[16]; | 
| Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 132 | free(buf); // GCC is smart enough to warn about this, but we're doing it deliberately. | 
| Elliott Hughes | 6f40caf | 2013-06-12 14:04:34 -0700 | [diff] [blame] | 133 | } | 
|  | 134 |  | 
| Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 135 | noinline void sigsegv_non_null() { | 
| Brigid Smith | 7b2078e | 2014-06-17 14:55:47 -0700 | [diff] [blame] | 136 | int* a = (int *)(&do_action); | 
|  | 137 | *a = 42; | 
|  | 138 | } | 
|  | 139 |  | 
| Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 140 | noinline void fprintf_null() { | 
|  | 141 | fprintf(nullptr, "oops"); | 
|  | 142 | } | 
| Elliott Hughes | 3808c4e | 2013-04-23 17:14:56 -0700 | [diff] [blame] | 143 |  | 
| Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 144 | noinline void readdir_null() { | 
|  | 145 | readdir(nullptr); | 
|  | 146 | } | 
|  | 147 |  | 
|  | 148 | noinline int strlen_null() { | 
|  | 149 | char* sneaky_null = nullptr; | 
|  | 150 | return strlen(sneaky_null); | 
|  | 151 | } | 
|  | 152 |  | 
|  | 153 | static int usage() { | 
|  | 154 | fprintf(stderr, "usage: %s KIND\n", getprogname()); | 
|  | 155 | fprintf(stderr, "\n"); | 
|  | 156 | fprintf(stderr, "where KIND is:\n"); | 
|  | 157 | fprintf(stderr, "  smash-stack           overwrite a -fstack-protector guard\n"); | 
|  | 158 | fprintf(stderr, "  stack-overflow        recurse until the stack overflows\n"); | 
| Elliott Hughes | 0ba5359 | 2017-02-01 16:59:15 -0800 | [diff] [blame] | 159 | fprintf(stderr, "  nostack               crash with a NULL stack pointer\n"); | 
|  | 160 | fprintf(stderr, "\n"); | 
| Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 161 | fprintf(stderr, "  heap-corruption       cause a libc abort by corrupting the heap\n"); | 
|  | 162 | fprintf(stderr, "  heap-usage            cause a libc abort by abusing a heap function\n"); | 
| Elliott Hughes | 0ba5359 | 2017-02-01 16:59:15 -0800 | [diff] [blame] | 163 | fprintf(stderr, "\n"); | 
| Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 164 | fprintf(stderr, "  abort                 call abort()\n"); | 
|  | 165 | fprintf(stderr, "  assert                call assert() without a function\n"); | 
|  | 166 | fprintf(stderr, "  assert2               call assert() with a function\n"); | 
|  | 167 | fprintf(stderr, "  exit                  call exit(1)\n"); | 
| Elliott Hughes | 0ba5359 | 2017-02-01 16:59:15 -0800 | [diff] [blame] | 168 | fprintf(stderr, "\n"); | 
| Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 169 | fprintf(stderr, "  fortify               fail a _FORTIFY_SOURCE check\n"); | 
| Elliott Hughes | 0ba5359 | 2017-02-01 16:59:15 -0800 | [diff] [blame] | 170 | fprintf(stderr, "  seccomp               fail a seccomp check\n"); | 
|  | 171 | #if defined(__arm__) | 
|  | 172 | fprintf(stderr, "  kuser_helper_version  call kuser_helper_version\n"); | 
|  | 173 | fprintf(stderr, "  kuser_get_tls         call kuser_get_tls\n"); | 
|  | 174 | fprintf(stderr, "  kuser_cmpxchg         call kuser_cmpxchg\n"); | 
|  | 175 | fprintf(stderr, "  kuser_memory_barrier  call kuser_memory_barrier\n"); | 
|  | 176 | fprintf(stderr, "  kuser_cmpxchg64       call kuser_cmpxchg64\n"); | 
|  | 177 | #endif | 
|  | 178 | fprintf(stderr, "\n"); | 
| Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 179 | fprintf(stderr, "  LOG_ALWAYS_FATAL      call liblog LOG_ALWAYS_FATAL\n"); | 
|  | 180 | fprintf(stderr, "  LOG_ALWAYS_FATAL_IF   call liblog LOG_ALWAYS_FATAL_IF\n"); | 
|  | 181 | fprintf(stderr, "  LOG-FATAL             call libbase LOG(FATAL)\n"); | 
| Elliott Hughes | 0ba5359 | 2017-02-01 16:59:15 -0800 | [diff] [blame] | 182 | fprintf(stderr, "\n"); | 
| Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 183 | fprintf(stderr, "  SIGFPE                cause a SIGFPE\n"); | 
|  | 184 | fprintf(stderr, "  SIGSEGV               cause a SIGSEGV at address 0x0 (synonym: crash)\n"); | 
|  | 185 | fprintf(stderr, "  SIGSEGV-non-null      cause a SIGSEGV at a non-zero address\n"); | 
|  | 186 | fprintf(stderr, "  SIGSEGV-unmapped      mmap/munmap a region of memory and then attempt to access it\n"); | 
|  | 187 | fprintf(stderr, "  SIGTRAP               cause a SIGTRAP\n"); | 
| Elliott Hughes | 0ba5359 | 2017-02-01 16:59:15 -0800 | [diff] [blame] | 188 | fprintf(stderr, "\n"); | 
| Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 189 | fprintf(stderr, "  fprintf-NULL          pass a null pointer to fprintf\n"); | 
|  | 190 | fprintf(stderr, "  readdir-NULL          pass a null pointer to readdir\n"); | 
|  | 191 | fprintf(stderr, "  strlen-NULL           pass a null pointer to strlen\n"); | 
|  | 192 | fprintf(stderr, "\n"); | 
| Josh Gao | 91ad653 | 2017-02-09 12:37:39 -0800 | [diff] [blame] | 193 | fprintf(stderr, "  no_new_privs          set PR_SET_NO_NEW_PRIVS and then abort\n"); | 
|  | 194 | fprintf(stderr, "\n"); | 
| Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 195 | fprintf(stderr, "prefix any of the above with 'thread-' to run on a new thread\n"); | 
|  | 196 | fprintf(stderr, "prefix any of the above with 'exhaustfd-' to exhaust\n"); | 
|  | 197 | fprintf(stderr, "all available file descriptors before crashing.\n"); | 
|  | 198 | fprintf(stderr, "prefix any of the above with 'wait-' to wait until input is received on stdin\n"); | 
|  | 199 |  | 
|  | 200 | return EXIT_FAILURE; | 
|  | 201 | } | 
|  | 202 |  | 
|  | 203 | noinline int do_action(const char* arg) { | 
|  | 204 | // Prefixes. | 
| Josh Gao | 100ce39 | 2016-10-31 17:37:37 -0700 | [diff] [blame] | 205 | if (!strncmp(arg, "wait-", strlen("wait-"))) { | 
|  | 206 | char buf[1]; | 
|  | 207 | TEMP_FAILURE_RETRY(read(STDIN_FILENO, buf, sizeof(buf))); | 
|  | 208 | return do_action(arg + strlen("wait-")); | 
|  | 209 | } else if (!strncmp(arg, "exhaustfd-", strlen("exhaustfd-"))) { | 
| Josh Gao | 218f7fb | 2016-10-07 16:42:05 -0700 | [diff] [blame] | 210 | errno = 0; | 
|  | 211 | while (errno != EMFILE) { | 
|  | 212 | open("/dev/null", O_RDONLY); | 
|  | 213 | } | 
|  | 214 | return do_action(arg + strlen("exhaustfd-")); | 
|  | 215 | } else if (!strncmp(arg, "thread-", strlen("thread-"))) { | 
| Elliott Hughes | aa42130 | 2012-12-10 14:15:42 -0800 | [diff] [blame] | 216 | return do_action_on_thread(arg + strlen("thread-")); | 
| Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 217 | } | 
|  | 218 |  | 
|  | 219 | // Actions. | 
|  | 220 | if (!strcasecmp(arg, "SIGSEGV-non-null")) { | 
| Brigid Smith | 7b2078e | 2014-06-17 14:55:47 -0700 | [diff] [blame] | 221 | sigsegv_non_null(); | 
| Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 222 | } else if (!strcasecmp(arg, "smash-stack")) { | 
| Yabin Cui | 2331b95 | 2014-12-11 17:46:33 -0800 | [diff] [blame] | 223 | volatile int len = 128; | 
|  | 224 | return smash_stack(&len); | 
| Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 225 | } else if (!strcasecmp(arg, "stack-overflow")) { | 
|  | 226 | overflow_stack(nullptr); | 
|  | 227 | } else if (!strcasecmp(arg, "nostack")) { | 
| Elliott Hughes | 3808c4e | 2013-04-23 17:14:56 -0700 | [diff] [blame] | 228 | crashnostack(); | 
| Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 229 | } else if (!strcasecmp(arg, "exit")) { | 
| Elliott Hughes | 3808c4e | 2013-04-23 17:14:56 -0700 | [diff] [blame] | 230 | exit(1); | 
| Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 231 | } else if (!strcasecmp(arg, "crash") || !strcmp(arg, "SIGSEGV")) { | 
| Elliott Hughes | 3808c4e | 2013-04-23 17:14:56 -0700 | [diff] [blame] | 232 | return crash(42); | 
| Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 233 | } else if (!strcasecmp(arg, "abort")) { | 
| Elliott Hughes | da6b2e2 | 2014-04-23 14:57:32 -0700 | [diff] [blame] | 234 | maybe_abort(); | 
| Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 235 | } else if (!strcasecmp(arg, "assert")) { | 
| Elliott Hughes | da6b2e2 | 2014-04-23 14:57:32 -0700 | [diff] [blame] | 236 | __assert("some_file.c", 123, "false"); | 
| Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 237 | } else if (!strcasecmp(arg, "assert2")) { | 
| Elliott Hughes | 3ecc421 | 2014-07-15 11:38:47 -0700 | [diff] [blame] | 238 | __assert2("some_file.c", 123, "some_function", "false"); | 
| Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 239 | } else if (!strcasecmp(arg, "fortify")) { | 
| Elliott Hughes | 23d1cad | 2016-05-10 13:29:58 -0700 | [diff] [blame] | 240 | char buf[10]; | 
|  | 241 | __read_chk(-1, buf, 32, 10); | 
|  | 242 | while (true) pause(); | 
| Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 243 | } else if (!strcasecmp(arg, "LOG(FATAL)")) { | 
|  | 244 | LOG(FATAL) << "hello " << 123; | 
|  | 245 | } else if (!strcasecmp(arg, "LOG_ALWAYS_FATAL")) { | 
| Elliott Hughes | da6b2e2 | 2014-04-23 14:57:32 -0700 | [diff] [blame] | 246 | LOG_ALWAYS_FATAL("hello %s", "world"); | 
| Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 247 | } else if (!strcasecmp(arg, "LOG_ALWAYS_FATAL_IF")) { | 
| Elliott Hughes | da6b2e2 | 2014-04-23 14:57:32 -0700 | [diff] [blame] | 248 | LOG_ALWAYS_FATAL_IF(true, "hello %s", "world"); | 
| Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 249 | } else if (!strcasecmp(arg, "SIGFPE")) { | 
| Elliott Hughes | 3ecc421 | 2014-07-15 11:38:47 -0700 | [diff] [blame] | 250 | raise(SIGFPE); | 
|  | 251 | return EXIT_SUCCESS; | 
| Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 252 | } else if (!strcasecmp(arg, "SIGTRAP")) { | 
| Elliott Hughes | 7e35ae8 | 2014-05-16 17:05:19 -0700 | [diff] [blame] | 253 | raise(SIGTRAP); | 
|  | 254 | return EXIT_SUCCESS; | 
| Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 255 | } else if (!strcasecmp(arg, "fprintf-NULL")) { | 
|  | 256 | fprintf_null(); | 
|  | 257 | } else if (!strcasecmp(arg, "readdir-NULL")) { | 
|  | 258 | readdir_null(); | 
|  | 259 | } else if (!strcasecmp(arg, "strlen-NULL")) { | 
|  | 260 | return strlen_null(); | 
|  | 261 | } else if (!strcasecmp(arg, "heap-usage")) { | 
| Elliott Hughes | 6f40caf | 2013-06-12 14:04:34 -0700 | [diff] [blame] | 262 | abuse_heap(); | 
| Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 263 | } else if (!strcasecmp(arg, "SIGSEGV-unmapped")) { | 
|  | 264 | char* map = reinterpret_cast<char*>(mmap(nullptr, sizeof(int), PROT_READ | PROT_WRITE, | 
|  | 265 | MAP_SHARED | MAP_ANONYMOUS, -1, 0)); | 
| Brigid Smith | 8606eaa | 2014-07-07 12:33:50 -0700 | [diff] [blame] | 266 | munmap(map, sizeof(int)); | 
|  | 267 | map[0] = '8'; | 
| Elliott Hughes | 0ba5359 | 2017-02-01 16:59:15 -0800 | [diff] [blame] | 268 | } else if (!strcasecmp(arg, "seccomp")) { | 
|  | 269 | syscall(99999); | 
|  | 270 | #if defined(__arm__) | 
|  | 271 | } else if (!strcasecmp(arg, "kuser_helper_version")) { | 
|  | 272 | return __kuser_helper_version; | 
|  | 273 | } else if (!strcasecmp(arg, "kuser_get_tls")) { | 
|  | 274 | return !__kuser_get_tls(); | 
|  | 275 | } else if (!strcasecmp(arg, "kuser_cmpxchg")) { | 
|  | 276 | return __kuser_cmpxchg(0, 0, 0); | 
|  | 277 | } else if (!strcasecmp(arg, "kuser_memory_barrier")) { | 
|  | 278 | __kuser_dmb(); | 
|  | 279 | } else if (!strcasecmp(arg, "kuser_cmpxchg64")) { | 
|  | 280 | return __kuser_cmpxchg64(0, 0, 0); | 
|  | 281 | #endif | 
| Josh Gao | 91ad653 | 2017-02-09 12:37:39 -0800 | [diff] [blame] | 282 | } else if (!strcasecmp(arg, "no_new_privs")) { | 
|  | 283 | if (prctl(PR_SET_NO_NEW_PRIVS, 1) != 0) { | 
|  | 284 | fprintf(stderr, "prctl(PR_SET_NO_NEW_PRIVS, 1) failed: %s\n", strerror(errno)); | 
|  | 285 | return EXIT_SUCCESS; | 
|  | 286 | } | 
|  | 287 | abort(); | 
| Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 288 | } else { | 
|  | 289 | return usage(); | 
| Elliott Hughes | aa42130 | 2012-12-10 14:15:42 -0800 | [diff] [blame] | 290 | } | 
|  | 291 |  | 
| Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 292 | fprintf(stderr, "%s: exiting normally!\n", getprogname()); | 
| Elliott Hughes | 3808c4e | 2013-04-23 17:14:56 -0700 | [diff] [blame] | 293 | return EXIT_SUCCESS; | 
| Elliott Hughes | aa42130 | 2012-12-10 14:15:42 -0800 | [diff] [blame] | 294 | } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 295 |  | 
| Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 296 | int main(int argc, char** argv) { | 
| Josh Gao | 9c02dc5 | 2016-06-15 17:29:00 -0700 | [diff] [blame] | 297 | #if defined(STATIC_CRASHER) | 
|  | 298 | debuggerd_callbacks_t callbacks = { | 
|  | 299 | .get_abort_message = []() { | 
|  | 300 | static struct { | 
|  | 301 | size_t size; | 
|  | 302 | char msg[32]; | 
|  | 303 | } msg; | 
|  | 304 |  | 
|  | 305 | msg.size = strlen("dummy abort message"); | 
|  | 306 | memcpy(msg.msg, "dummy abort message", strlen("dummy abort message")); | 
|  | 307 | return reinterpret_cast<abort_msg_t*>(&msg); | 
|  | 308 | }, | 
|  | 309 | .post_dump = nullptr | 
|  | 310 | }; | 
|  | 311 | debuggerd_init(&callbacks); | 
|  | 312 | #endif | 
|  | 313 |  | 
| Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 314 | if (argc == 1) crash1(); | 
|  | 315 | else if (argc == 2) return do_action(argv[1]); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 316 |  | 
| Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 317 | return usage(); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 318 | } | 
| Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 319 |  | 
|  | 320 | }; |