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> |
Elliott Hughes | 1b13b14 | 2023-08-08 16:00:30 -0700 | [diff] [blame] | 22 | #include <error.h> |
Josh Gao | 218f7fb | 2016-10-07 16:42:05 -0700 | [diff] [blame] | 23 | #include <fcntl.h> |
Elliott Hughes | da6b2e2 | 2014-04-23 14:57:32 -0700 | [diff] [blame] | 24 | #include <pthread.h> |
Elliott Hughes | da6b2e2 | 2014-04-23 14:57:32 -0700 | [diff] [blame] | 25 | #include <signal.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
Brigid Smith | 8606eaa | 2014-07-07 12:33:50 -0700 | [diff] [blame] | 29 | #include <sys/mman.h> |
Josh Gao | 91ad653 | 2017-02-09 12:37:39 -0800 | [diff] [blame] | 30 | #include <sys/prctl.h> |
Elliott Hughes | da6b2e2 | 2014-04-23 14:57:32 -0700 | [diff] [blame] | 31 | #include <unistd.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 32 | |
Elliott Hughes | 1b13b14 | 2023-08-08 16:00:30 -0700 | [diff] [blame] | 33 | #include <android-base/file.h> |
| 34 | #include <android-base/strings.h> |
| 35 | |
Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 36 | // We test both kinds of logging. |
Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 37 | #include <android-base/logging.h> |
Mark Salyzyn | 51c33b7 | 2017-01-12 15:44:06 -0800 | [diff] [blame] | 38 | #include <log/log.h> |
Mark Salyzyn | f1a8dfa | 2014-04-30 09:24:08 -0700 | [diff] [blame] | 39 | |
Elliott Hughes | 12b7129 | 2017-03-02 19:01:20 -0800 | [diff] [blame] | 40 | #include "seccomp_policy.h" |
| 41 | |
Josh Gao | 9c02dc5 | 2016-06-15 17:29:00 -0700 | [diff] [blame] | 42 | #if defined(STATIC_CRASHER) |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 43 | #include "debuggerd/handler.h" |
Josh Gao | 9c02dc5 | 2016-06-15 17:29:00 -0700 | [diff] [blame] | 44 | #endif |
| 45 | |
Christopher Ferris | 4f600fe | 2022-04-13 14:55:36 -0700 | [diff] [blame] | 46 | extern "C" void android_set_abort_message(const char* msg); |
| 47 | |
Elliott Hughes | 0ba5359 | 2017-02-01 16:59:15 -0800 | [diff] [blame] | 48 | #if defined(__arm__) |
| 49 | // See https://www.kernel.org/doc/Documentation/arm/kernel_user_helpers.txt for details. |
| 50 | #define __kuser_helper_version (*(int32_t*) 0xffff0ffc) |
| 51 | typedef void * (__kuser_get_tls_t)(void); |
| 52 | #define __kuser_get_tls (*(__kuser_get_tls_t*) 0xffff0fe0) |
| 53 | typedef int (__kuser_cmpxchg_t)(int oldval, int newval, volatile int *ptr); |
| 54 | #define __kuser_cmpxchg (*(__kuser_cmpxchg_t*) 0xffff0fc0) |
| 55 | typedef void (__kuser_dmb_t)(void); |
| 56 | #define __kuser_dmb (*(__kuser_dmb_t*) 0xffff0fa0) |
| 57 | typedef int (__kuser_cmpxchg64_t)(const int64_t*, const int64_t*, volatile int64_t*); |
| 58 | #define __kuser_cmpxchg64 (*(__kuser_cmpxchg64_t*) 0xffff0f60) |
| 59 | #endif |
| 60 | |
Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 61 | #define noinline __attribute__((__noinline__)) |
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 | // Avoid name mangling so that stacks are more readable. |
| 64 | extern "C" { |
Elliott Hughes | 3808c4e | 2013-04-23 17:14:56 -0700 | [diff] [blame] | 65 | |
Elliott Hughes | 1b13b14 | 2023-08-08 16:00:30 -0700 | [diff] [blame] | 66 | void crash1(); |
| 67 | void crash_no_stack(); |
| 68 | void crash_bti(); |
| 69 | void crash_pac(); |
Elliott Hughes | 23d1cad | 2016-05-10 13:29:58 -0700 | [diff] [blame] | 70 | |
Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 71 | int do_action(const char* arg); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 72 | |
Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 73 | noinline void maybe_abort() { |
Elliott Hughes | da6b2e2 | 2014-04-23 14:57:32 -0700 | [diff] [blame] | 74 | if (time(0) != 42) { |
Elliott Hughes | 6f40caf | 2013-06-12 14:04:34 -0700 | [diff] [blame] | 75 | abort(); |
| 76 | } |
| 77 | } |
| 78 | |
Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 79 | char* smash_stack_dummy_buf; |
| 80 | noinline void smash_stack_dummy_function(volatile int* plen) { |
Yabin Cui | 2331b95 | 2014-12-11 17:46:33 -0800 | [diff] [blame] | 81 | smash_stack_dummy_buf[*plen] = 0; |
| 82 | } |
| 83 | |
| 84 | // This must be marked with "__attribute__ ((noinline))", to ensure the |
| 85 | // compiler generates the proper stack guards around this function. |
| 86 | // Assign local array address to global variable to force stack guards. |
| 87 | // Use another noinline function to corrupt the stack. |
Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 88 | noinline int smash_stack(volatile int* plen) { |
| 89 | printf("%s: deliberately corrupting stack...\n", getprogname()); |
Yabin Cui | 2331b95 | 2014-12-11 17:46:33 -0800 | [diff] [blame] | 90 | |
| 91 | char buf[128]; |
| 92 | smash_stack_dummy_buf = buf; |
| 93 | // This should corrupt stack guards and make process abort. |
| 94 | smash_stack_dummy_function(plen); |
| 95 | return 0; |
Elliott Hughes | df4200e | 2013-02-14 14:41:57 -0800 | [diff] [blame] | 96 | } |
| 97 | |
Stephen Hines | 18395cb | 2015-09-29 23:55:14 -0700 | [diff] [blame] | 98 | #pragma clang diagnostic push |
| 99 | #pragma clang diagnostic ignored "-Winfinite-recursion" |
| 100 | |
Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 101 | 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] | 102 | |
Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 103 | noinline void overflow_stack(void* p) { |
Elliott Hughes | 3808c4e | 2013-04-23 17:14:56 -0700 | [diff] [blame] | 104 | void* buf[1]; |
| 105 | buf[0] = p; |
Elliott Hughes | b1be27e | 2013-07-15 17:19:02 -0700 | [diff] [blame] | 106 | global = buf; |
Elliott Hughes | 3808c4e | 2013-04-23 17:14:56 -0700 | [diff] [blame] | 107 | overflow_stack(&buf); |
| 108 | } |
| 109 | |
Stephen Hines | 18395cb | 2015-09-29 23:55:14 -0700 | [diff] [blame] | 110 | #pragma clang diagnostic pop |
| 111 | |
Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 112 | noinline void* thread_callback(void* raw_arg) { |
| 113 | const char* arg = reinterpret_cast<const char*>(raw_arg); |
| 114 | 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] | 115 | } |
| 116 | |
Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 117 | noinline int do_action_on_thread(const char* arg) { |
Elliott Hughes | aa42130 | 2012-12-10 14:15:42 -0800 | [diff] [blame] | 118 | pthread_t t; |
Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 119 | pthread_create(&t, nullptr, thread_callback, const_cast<char*>(arg)); |
| 120 | void* result = nullptr; |
Elliott Hughes | aa42130 | 2012-12-10 14:15:42 -0800 | [diff] [blame] | 121 | pthread_join(t, &result); |
Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 122 | return reinterpret_cast<uintptr_t>(result); |
Elliott Hughes | aa42130 | 2012-12-10 14:15:42 -0800 | [diff] [blame] | 123 | } |
| 124 | |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 125 | noinline int crash_null() { |
| 126 | int (*null_func)() = nullptr; |
| 127 | return null_func(); |
| 128 | } |
| 129 | |
Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 130 | noinline int crash3(int a) { |
| 131 | *reinterpret_cast<int*>(0xdead) = a; |
Elliott Hughes | 6f40caf | 2013-06-12 14:04:34 -0700 | [diff] [blame] | 132 | return a*4; |
Pavel Chupin | af2cb36 | 2013-03-08 13:17:35 +0400 | [diff] [blame] | 133 | } |
| 134 | |
Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 135 | noinline int crash2(int a) { |
Elliott Hughes | 6f40caf | 2013-06-12 14:04:34 -0700 | [diff] [blame] | 136 | a = crash3(a) + 2; |
| 137 | return a*3; |
Pavel Chupin | af2cb36 | 2013-03-08 13:17:35 +0400 | [diff] [blame] | 138 | } |
| 139 | |
Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 140 | noinline int crash(int a) { |
Elliott Hughes | 6f40caf | 2013-06-12 14:04:34 -0700 | [diff] [blame] | 141 | a = crash2(a) + 1; |
| 142 | return a*2; |
Pavel Chupin | af2cb36 | 2013-03-08 13:17:35 +0400 | [diff] [blame] | 143 | } |
| 144 | |
Pirama Arumuga Nainar | 982e2a5 | 2021-03-16 16:22:52 -0700 | [diff] [blame] | 145 | #pragma clang diagnostic push |
| 146 | #pragma clang diagnostic ignored "-Wfree-nonheap-object" |
| 147 | |
Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 148 | noinline void abuse_heap() { |
Elliott Hughes | 6f40caf | 2013-06-12 14:04:34 -0700 | [diff] [blame] | 149 | char buf[16]; |
Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 150 | 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] | 151 | } |
Pirama Arumuga Nainar | 982e2a5 | 2021-03-16 16:22:52 -0700 | [diff] [blame] | 152 | #pragma clang diagnostic pop |
Elliott Hughes | 6f40caf | 2013-06-12 14:04:34 -0700 | [diff] [blame] | 153 | |
Josh Gao | 399b4ee | 2017-06-30 12:46:31 -0700 | [diff] [blame] | 154 | noinline void leak() { |
| 155 | while (true) { |
| 156 | void* mapping = |
Kelvin Zhang | 786dac3 | 2023-06-15 16:23:56 -0700 | [diff] [blame] | 157 | mmap(nullptr, getpagesize(), PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
Josh Gao | 399b4ee | 2017-06-30 12:46:31 -0700 | [diff] [blame] | 158 | static_cast<volatile char*>(mapping)[0] = 'a'; |
| 159 | } |
| 160 | } |
| 161 | |
Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 162 | noinline void sigsegv_non_null() { |
Brigid Smith | 7b2078e | 2014-06-17 14:55:47 -0700 | [diff] [blame] | 163 | int* a = (int *)(&do_action); |
| 164 | *a = 42; |
| 165 | } |
| 166 | |
Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 167 | noinline void fprintf_null() { |
zijunzhao | f5e1533 | 2023-03-03 01:33:28 +0000 | [diff] [blame] | 168 | FILE* sneaky_null = nullptr; |
| 169 | fprintf(sneaky_null, "oops"); |
Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 170 | } |
Elliott Hughes | 3808c4e | 2013-04-23 17:14:56 -0700 | [diff] [blame] | 171 | |
Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 172 | noinline void readdir_null() { |
zijunzhao | fa8037c | 2023-03-31 23:50:37 +0000 | [diff] [blame] | 173 | DIR* sneaky_null = nullptr; |
| 174 | readdir(sneaky_null); |
Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | noinline int strlen_null() { |
| 178 | char* sneaky_null = nullptr; |
| 179 | return strlen(sneaky_null); |
| 180 | } |
| 181 | |
| 182 | static int usage() { |
| 183 | fprintf(stderr, "usage: %s KIND\n", getprogname()); |
| 184 | fprintf(stderr, "\n"); |
| 185 | fprintf(stderr, "where KIND is:\n"); |
| 186 | fprintf(stderr, " smash-stack overwrite a -fstack-protector guard\n"); |
| 187 | fprintf(stderr, " stack-overflow recurse until the stack overflows\n"); |
Elliott Hughes | 0ba5359 | 2017-02-01 16:59:15 -0800 | [diff] [blame] | 188 | fprintf(stderr, " nostack crash with a NULL stack pointer\n"); |
| 189 | fprintf(stderr, "\n"); |
Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 190 | 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] | 191 | 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] | 192 | fprintf(stderr, " leak leak memory until we get OOM-killed\n"); |
Elliott Hughes | 0ba5359 | 2017-02-01 16:59:15 -0800 | [diff] [blame] | 193 | fprintf(stderr, "\n"); |
Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 194 | fprintf(stderr, " abort call abort()\n"); |
Christopher Ferris | 4f600fe | 2022-04-13 14:55:36 -0700 | [diff] [blame] | 195 | fprintf(stderr, " abort_with_msg call abort() setting an abort message\n"); |
| 196 | fprintf(stderr, " abort_with_null_msg call abort() setting a null abort message\n"); |
Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 197 | fprintf(stderr, " assert call assert() without a function\n"); |
| 198 | fprintf(stderr, " assert2 call assert() with a function\n"); |
| 199 | fprintf(stderr, " exit call exit(1)\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, " fortify fail a _FORTIFY_SOURCE check\n"); |
Josh Gao | 3fa9637 | 2018-06-01 16:35:47 -0700 | [diff] [blame] | 202 | fprintf(stderr, " fdsan_file close a file descriptor that's owned by a FILE*\n"); |
| 203 | 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] | 204 | fprintf(stderr, " seccomp fail a seccomp check\n"); |
Josh Gao | 3a9a989 | 2024-05-16 13:52:17 -0700 | [diff] [blame] | 205 | #if defined(__LP64__) |
Elliott Hughes | dd04c65 | 2019-04-15 13:03:48 -0700 | [diff] [blame] | 206 | fprintf(stderr, " xom read execute-only memory\n"); |
Josh Gao | 3a9a989 | 2024-05-16 13:52:17 -0700 | [diff] [blame] | 207 | #endif |
Elliott Hughes | 0ba5359 | 2017-02-01 16:59:15 -0800 | [diff] [blame] | 208 | fprintf(stderr, "\n"); |
Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 209 | fprintf(stderr, " LOG_ALWAYS_FATAL call liblog LOG_ALWAYS_FATAL\n"); |
| 210 | fprintf(stderr, " LOG_ALWAYS_FATAL_IF call liblog LOG_ALWAYS_FATAL_IF\n"); |
| 211 | fprintf(stderr, " LOG-FATAL call libbase LOG(FATAL)\n"); |
Elliott Hughes | 0ba5359 | 2017-02-01 16:59:15 -0800 | [diff] [blame] | 212 | fprintf(stderr, "\n"); |
Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 213 | fprintf(stderr, " SIGFPE cause a SIGFPE\n"); |
Elliott Hughes | 2baf443 | 2018-05-30 12:55:04 -0700 | [diff] [blame] | 214 | fprintf(stderr, " SIGILL cause a SIGILL\n"); |
Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 215 | fprintf(stderr, " SIGSEGV cause a SIGSEGV at address 0x0 (synonym: crash)\n"); |
| 216 | fprintf(stderr, " SIGSEGV-non-null cause a SIGSEGV at a non-zero address\n"); |
| 217 | fprintf(stderr, " SIGSEGV-unmapped mmap/munmap a region of memory and then attempt to access it\n"); |
| 218 | fprintf(stderr, " SIGTRAP cause a SIGTRAP\n"); |
Elliott Hughes | 0ba5359 | 2017-02-01 16:59:15 -0800 | [diff] [blame] | 219 | fprintf(stderr, "\n"); |
Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 220 | fprintf(stderr, " fprintf-NULL pass a null pointer to fprintf\n"); |
| 221 | fprintf(stderr, " readdir-NULL pass a null pointer to readdir\n"); |
| 222 | fprintf(stderr, " strlen-NULL pass a null pointer to strlen\n"); |
Elliott Hughes | da9e395 | 2017-02-17 10:26:48 -0800 | [diff] [blame] | 223 | 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] | 224 | fprintf(stderr, "\n"); |
Josh Gao | 91ad653 | 2017-02-09 12:37:39 -0800 | [diff] [blame] | 225 | fprintf(stderr, " no_new_privs set PR_SET_NO_NEW_PRIVS and then abort\n"); |
| 226 | fprintf(stderr, "\n"); |
Elliott Hughes | 1b13b14 | 2023-08-08 16:00:30 -0700 | [diff] [blame] | 227 | #if defined(__arm__) |
| 228 | fprintf(stderr, "Also, since this is an arm32 binary:\n"); |
| 229 | fprintf(stderr, " kuser_helper_version call kuser_helper_version\n"); |
| 230 | fprintf(stderr, " kuser_get_tls call kuser_get_tls\n"); |
| 231 | fprintf(stderr, " kuser_cmpxchg call kuser_cmpxchg\n"); |
| 232 | fprintf(stderr, " kuser_memory_barrier call kuser_memory_barrier\n"); |
| 233 | fprintf(stderr, " kuser_cmpxchg64 call kuser_cmpxchg64\n"); |
| 234 | #endif |
| 235 | #if defined(__aarch64__) |
| 236 | fprintf(stderr, "Also, since this is an arm64 binary:\n"); |
| 237 | fprintf(stderr, " bti fail a branch target identification (BTI) check\n"); |
| 238 | fprintf(stderr, " pac fail a pointer authentication (PAC) check\n"); |
| 239 | #endif |
| 240 | fprintf(stderr, "\n"); |
Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 241 | fprintf(stderr, "prefix any of the above with 'thread-' to run on a new thread\n"); |
| 242 | fprintf(stderr, "prefix any of the above with 'exhaustfd-' to exhaust\n"); |
| 243 | fprintf(stderr, "all available file descriptors before crashing.\n"); |
| 244 | fprintf(stderr, "prefix any of the above with 'wait-' to wait until input is received on stdin\n"); |
| 245 | |
| 246 | return EXIT_FAILURE; |
| 247 | } |
| 248 | |
Elliott Hughes | 1b13b14 | 2023-08-08 16:00:30 -0700 | [diff] [blame] | 249 | [[maybe_unused]] static void CheckCpuFeature(const std::string& name) { |
| 250 | std::string cpuinfo; |
| 251 | if (!android::base::ReadFileToString("/proc/cpuinfo", &cpuinfo)) { |
| 252 | error(1, errno, "couldn't read /proc/cpuinfo"); |
| 253 | } |
| 254 | std::vector<std::string> lines = android::base::Split(cpuinfo, "\n"); |
| 255 | for (std::string_view line : lines) { |
| 256 | if (!android::base::ConsumePrefix(&line, "Features\t:")) continue; |
| 257 | std::vector<std::string> features = android::base::Split(std::string(line), " "); |
| 258 | if (std::find(features.begin(), features.end(), name) == features.end()) { |
| 259 | error(1, 0, "/proc/cpuinfo does not report feature '%s'", name.c_str()); |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | |
Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 264 | noinline int do_action(const char* arg) { |
| 265 | // Prefixes. |
Josh Gao | 100ce39 | 2016-10-31 17:37:37 -0700 | [diff] [blame] | 266 | if (!strncmp(arg, "wait-", strlen("wait-"))) { |
| 267 | char buf[1]; |
Stephen Hines | 8395de6 | 2018-09-24 13:03:25 -0700 | [diff] [blame] | 268 | UNUSED(TEMP_FAILURE_RETRY(read(STDIN_FILENO, buf, sizeof(buf)))); |
Josh Gao | 100ce39 | 2016-10-31 17:37:37 -0700 | [diff] [blame] | 269 | return do_action(arg + strlen("wait-")); |
| 270 | } else if (!strncmp(arg, "exhaustfd-", strlen("exhaustfd-"))) { |
Josh Gao | 218f7fb | 2016-10-07 16:42:05 -0700 | [diff] [blame] | 271 | errno = 0; |
| 272 | while (errno != EMFILE) { |
| 273 | open("/dev/null", O_RDONLY); |
| 274 | } |
| 275 | return do_action(arg + strlen("exhaustfd-")); |
| 276 | } else if (!strncmp(arg, "thread-", strlen("thread-"))) { |
Elliott Hughes | aa42130 | 2012-12-10 14:15:42 -0800 | [diff] [blame] | 277 | return do_action_on_thread(arg + strlen("thread-")); |
Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | // Actions. |
| 281 | if (!strcasecmp(arg, "SIGSEGV-non-null")) { |
Josh Gao | 3fa9637 | 2018-06-01 16:35:47 -0700 | [diff] [blame] | 282 | sigsegv_non_null(); |
Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 283 | } else if (!strcasecmp(arg, "smash-stack")) { |
Josh Gao | 3fa9637 | 2018-06-01 16:35:47 -0700 | [diff] [blame] | 284 | volatile int len = 128; |
| 285 | return smash_stack(&len); |
Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 286 | } else if (!strcasecmp(arg, "stack-overflow")) { |
Josh Gao | 3fa9637 | 2018-06-01 16:35:47 -0700 | [diff] [blame] | 287 | overflow_stack(nullptr); |
Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 288 | } else if (!strcasecmp(arg, "nostack")) { |
Elliott Hughes | 1b13b14 | 2023-08-08 16:00:30 -0700 | [diff] [blame] | 289 | crash_no_stack(); |
Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 290 | } else if (!strcasecmp(arg, "exit")) { |
Josh Gao | 3fa9637 | 2018-06-01 16:35:47 -0700 | [diff] [blame] | 291 | exit(1); |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 292 | } else if (!strcasecmp(arg, "call-null")) { |
| 293 | return crash_null(); |
Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 294 | } else if (!strcasecmp(arg, "crash") || !strcmp(arg, "SIGSEGV")) { |
Josh Gao | 3fa9637 | 2018-06-01 16:35:47 -0700 | [diff] [blame] | 295 | return crash(42); |
Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 296 | } else if (!strcasecmp(arg, "abort")) { |
Josh Gao | 3fa9637 | 2018-06-01 16:35:47 -0700 | [diff] [blame] | 297 | maybe_abort(); |
Christopher Ferris | 4f600fe | 2022-04-13 14:55:36 -0700 | [diff] [blame] | 298 | } else if (!strcasecmp(arg, "abort_with_msg")) { |
| 299 | android_set_abort_message("Aborting due to crasher"); |
| 300 | maybe_abort(); |
| 301 | } else if (!strcasecmp(arg, "abort_with_null")) { |
| 302 | android_set_abort_message(nullptr); |
| 303 | maybe_abort(); |
Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 304 | } else if (!strcasecmp(arg, "assert")) { |
Josh Gao | 3fa9637 | 2018-06-01 16:35:47 -0700 | [diff] [blame] | 305 | __assert("some_file.c", 123, "false"); |
Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 306 | } else if (!strcasecmp(arg, "assert2")) { |
Josh Gao | 3fa9637 | 2018-06-01 16:35:47 -0700 | [diff] [blame] | 307 | __assert2("some_file.c", 123, "some_function", "false"); |
Stephen Hines | 8395de6 | 2018-09-24 13:03:25 -0700 | [diff] [blame] | 308 | #if !defined(__clang_analyzer__) |
Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 309 | } else if (!strcasecmp(arg, "fortify")) { |
Stephen Hines | 8395de6 | 2018-09-24 13:03:25 -0700 | [diff] [blame] | 310 | // FORTIFY is disabled when running clang-tidy and other tools, so this |
| 311 | // shouldn't depend on internal implementation details of it. |
Josh Gao | 3fa9637 | 2018-06-01 16:35:47 -0700 | [diff] [blame] | 312 | char buf[10]; |
| 313 | __read_chk(-1, buf, 32, 10); |
| 314 | while (true) pause(); |
Stephen Hines | 8395de6 | 2018-09-24 13:03:25 -0700 | [diff] [blame] | 315 | #endif |
Josh Gao | 3fa9637 | 2018-06-01 16:35:47 -0700 | [diff] [blame] | 316 | } else if (!strcasecmp(arg, "fdsan_file")) { |
| 317 | FILE* f = fopen("/dev/null", "r"); |
| 318 | close(fileno(f)); |
| 319 | } else if (!strcasecmp(arg, "fdsan_dir")) { |
| 320 | DIR* d = opendir("/dev/"); |
| 321 | close(dirfd(d)); |
Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 322 | } else if (!strcasecmp(arg, "LOG(FATAL)")) { |
Josh Gao | 3fa9637 | 2018-06-01 16:35:47 -0700 | [diff] [blame] | 323 | LOG(FATAL) << "hello " << 123; |
Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 324 | } else if (!strcasecmp(arg, "LOG_ALWAYS_FATAL")) { |
Josh Gao | 3fa9637 | 2018-06-01 16:35:47 -0700 | [diff] [blame] | 325 | LOG_ALWAYS_FATAL("hello %s", "world"); |
Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 326 | } else if (!strcasecmp(arg, "LOG_ALWAYS_FATAL_IF")) { |
Josh Gao | 3fa9637 | 2018-06-01 16:35:47 -0700 | [diff] [blame] | 327 | LOG_ALWAYS_FATAL_IF(true, "hello %s", "world"); |
Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 328 | } else if (!strcasecmp(arg, "SIGFPE")) { |
Josh Gao | 3fa9637 | 2018-06-01 16:35:47 -0700 | [diff] [blame] | 329 | raise(SIGFPE); |
| 330 | return EXIT_SUCCESS; |
Elliott Hughes | 2baf443 | 2018-05-30 12:55:04 -0700 | [diff] [blame] | 331 | } else if (!strcasecmp(arg, "SIGILL")) { |
| 332 | #if defined(__aarch64__) |
| 333 | __asm__ volatile(".word 0\n"); |
| 334 | #elif defined(__arm__) |
| 335 | __asm__ volatile(".word 0xe7f0def0\n"); |
| 336 | #elif defined(__i386__) || defined(__x86_64__) |
| 337 | __asm__ volatile("ud2\n"); |
Xia Lifang | b13a10b | 2022-10-12 22:27:54 +0800 | [diff] [blame] | 338 | #elif defined(__riscv) |
| 339 | __asm__ volatile("unimp\n"); |
Elliott Hughes | 2baf443 | 2018-05-30 12:55:04 -0700 | [diff] [blame] | 340 | #else |
| 341 | #error |
| 342 | #endif |
Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 343 | } else if (!strcasecmp(arg, "SIGTRAP")) { |
Josh Gao | 3fa9637 | 2018-06-01 16:35:47 -0700 | [diff] [blame] | 344 | raise(SIGTRAP); |
| 345 | return EXIT_SUCCESS; |
Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 346 | } else if (!strcasecmp(arg, "fprintf-NULL")) { |
Josh Gao | 3fa9637 | 2018-06-01 16:35:47 -0700 | [diff] [blame] | 347 | fprintf_null(); |
Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 348 | } else if (!strcasecmp(arg, "readdir-NULL")) { |
Josh Gao | 3fa9637 | 2018-06-01 16:35:47 -0700 | [diff] [blame] | 349 | readdir_null(); |
Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 350 | } else if (!strcasecmp(arg, "strlen-NULL")) { |
Josh Gao | 3fa9637 | 2018-06-01 16:35:47 -0700 | [diff] [blame] | 351 | return strlen_null(); |
Elliott Hughes | da9e395 | 2017-02-17 10:26:48 -0800 | [diff] [blame] | 352 | } else if (!strcasecmp(arg, "pthread_join-NULL")) { |
Josh Gao | 3fa9637 | 2018-06-01 16:35:47 -0700 | [diff] [blame] | 353 | return pthread_join(0, nullptr); |
Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 354 | } else if (!strcasecmp(arg, "heap-usage")) { |
Josh Gao | 3fa9637 | 2018-06-01 16:35:47 -0700 | [diff] [blame] | 355 | abuse_heap(); |
Josh Gao | 399b4ee | 2017-06-30 12:46:31 -0700 | [diff] [blame] | 356 | } else if (!strcasecmp(arg, "leak")) { |
Josh Gao | 3fa9637 | 2018-06-01 16:35:47 -0700 | [diff] [blame] | 357 | leak(); |
Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 358 | } else if (!strcasecmp(arg, "SIGSEGV-unmapped")) { |
Josh Gao | 3fa9637 | 2018-06-01 16:35:47 -0700 | [diff] [blame] | 359 | char* map = reinterpret_cast<char*>( |
| 360 | mmap(nullptr, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0)); |
| 361 | munmap(map, sizeof(int)); |
| 362 | map[0] = '8'; |
Elliott Hughes | 0ba5359 | 2017-02-01 16:59:15 -0800 | [diff] [blame] | 363 | } else if (!strcasecmp(arg, "seccomp")) { |
Josh Gao | 3fa9637 | 2018-06-01 16:35:47 -0700 | [diff] [blame] | 364 | set_system_seccomp_filter(); |
| 365 | syscall(99999); |
Elliott Hughes | dd04c65 | 2019-04-15 13:03:48 -0700 | [diff] [blame] | 366 | #if defined(__LP64__) |
| 367 | } else if (!strcasecmp(arg, "xom")) { |
| 368 | // Try to read part of our code, which will fail if XOM is active. |
| 369 | printf("*%lx = %lx\n", reinterpret_cast<long>(usage), *reinterpret_cast<long*>(usage)); |
| 370 | #endif |
Elliott Hughes | 0ba5359 | 2017-02-01 16:59:15 -0800 | [diff] [blame] | 371 | #if defined(__arm__) |
| 372 | } else if (!strcasecmp(arg, "kuser_helper_version")) { |
| 373 | return __kuser_helper_version; |
| 374 | } else if (!strcasecmp(arg, "kuser_get_tls")) { |
| 375 | return !__kuser_get_tls(); |
| 376 | } else if (!strcasecmp(arg, "kuser_cmpxchg")) { |
| 377 | return __kuser_cmpxchg(0, 0, 0); |
| 378 | } else if (!strcasecmp(arg, "kuser_memory_barrier")) { |
| 379 | __kuser_dmb(); |
| 380 | } else if (!strcasecmp(arg, "kuser_cmpxchg64")) { |
| 381 | return __kuser_cmpxchg64(0, 0, 0); |
| 382 | #endif |
Elliott Hughes | 1b13b14 | 2023-08-08 16:00:30 -0700 | [diff] [blame] | 383 | #if defined(__aarch64__) |
| 384 | } else if (!strcasecmp(arg, "bti")) { |
| 385 | CheckCpuFeature("bti"); |
| 386 | crash_bti(); |
| 387 | } else if (!strcasecmp(arg, "pac")) { |
| 388 | CheckCpuFeature("paca"); |
| 389 | crash_pac(); |
| 390 | #endif |
Josh Gao | 91ad653 | 2017-02-09 12:37:39 -0800 | [diff] [blame] | 391 | } else if (!strcasecmp(arg, "no_new_privs")) { |
| 392 | if (prctl(PR_SET_NO_NEW_PRIVS, 1) != 0) { |
| 393 | fprintf(stderr, "prctl(PR_SET_NO_NEW_PRIVS, 1) failed: %s\n", strerror(errno)); |
| 394 | return EXIT_SUCCESS; |
| 395 | } |
| 396 | abort(); |
Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 397 | } else { |
| 398 | return usage(); |
Elliott Hughes | aa42130 | 2012-12-10 14:15:42 -0800 | [diff] [blame] | 399 | } |
| 400 | |
Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 401 | fprintf(stderr, "%s: exiting normally!\n", getprogname()); |
Elliott Hughes | 3808c4e | 2013-04-23 17:14:56 -0700 | [diff] [blame] | 402 | return EXIT_SUCCESS; |
Elliott Hughes | aa42130 | 2012-12-10 14:15:42 -0800 | [diff] [blame] | 403 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 404 | |
Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 405 | int main(int argc, char** argv) { |
Josh Gao | 9c02dc5 | 2016-06-15 17:29:00 -0700 | [diff] [blame] | 406 | #if defined(STATIC_CRASHER) |
| 407 | debuggerd_callbacks_t callbacks = { |
Peter Collingbourne | f3d542f | 2020-03-05 16:46:15 -0800 | [diff] [blame] | 408 | .get_process_info = []() { |
Josh Gao | 9c02dc5 | 2016-06-15 17:29:00 -0700 | [diff] [blame] | 409 | static struct { |
| 410 | size_t size; |
| 411 | char msg[32]; |
| 412 | } msg; |
| 413 | |
| 414 | msg.size = strlen("dummy abort message"); |
| 415 | memcpy(msg.msg, "dummy abort message", strlen("dummy abort message")); |
Peter Collingbourne | f3d542f | 2020-03-05 16:46:15 -0800 | [diff] [blame] | 416 | return debugger_process_info{ |
| 417 | .abort_msg = reinterpret_cast<void*>(&msg), |
| 418 | }; |
Josh Gao | 9c02dc5 | 2016-06-15 17:29:00 -0700 | [diff] [blame] | 419 | }, |
| 420 | .post_dump = nullptr |
| 421 | }; |
| 422 | debuggerd_init(&callbacks); |
| 423 | #endif |
| 424 | |
Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 425 | if (argc == 1) crash1(); |
| 426 | else if (argc == 2) return do_action(argv[1]); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 427 | |
Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 428 | return usage(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 429 | } |
Elliott Hughes | 400628a | 2016-12-14 17:33:46 -0800 | [diff] [blame] | 430 | |
| 431 | }; |