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