blob: 3b52776d7b32e4be64dd5ca865ccf98855730b09 [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>
Elliott Hughes1b13b142023-08-08 16:00:30 -070022#include <error.h>
Josh Gao218f7fb2016-10-07 16:42:05 -070023#include <fcntl.h>
Elliott Hughesda6b2e22014-04-23 14:57:32 -070024#include <pthread.h>
Elliott Hughesda6b2e22014-04-23 14:57:32 -070025#include <signal.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080026#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
Brigid Smith8606eaa2014-07-07 12:33:50 -070029#include <sys/mman.h>
Josh Gao91ad6532017-02-09 12:37:39 -080030#include <sys/prctl.h>
Elliott Hughesda6b2e22014-04-23 14:57:32 -070031#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080032
Elliott Hughes1b13b142023-08-08 16:00:30 -070033#include <android-base/file.h>
34#include <android-base/strings.h>
35
Elliott Hughes400628a2016-12-14 17:33:46 -080036// We test both kinds of logging.
Elliott Hughes400628a2016-12-14 17:33:46 -080037#include <android-base/logging.h>
Mark Salyzyn51c33b72017-01-12 15:44:06 -080038#include <log/log.h>
Mark Salyzynf1a8dfa2014-04-30 09:24:08 -070039
Elliott Hughes12b71292017-03-02 19:01:20 -080040#include "seccomp_policy.h"
41
Josh Gao9c02dc52016-06-15 17:29:00 -070042#if defined(STATIC_CRASHER)
Josh Gaocbe70cb2016-10-18 18:17:52 -070043#include "debuggerd/handler.h"
Josh Gao9c02dc52016-06-15 17:29:00 -070044#endif
45
Christopher Ferris4f600fe2022-04-13 14:55:36 -070046extern "C" void android_set_abort_message(const char* msg);
47
Elliott Hughes0ba53592017-02-01 16:59:15 -080048#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)
51typedef void * (__kuser_get_tls_t)(void);
52#define __kuser_get_tls (*(__kuser_get_tls_t*) 0xffff0fe0)
53typedef int (__kuser_cmpxchg_t)(int oldval, int newval, volatile int *ptr);
54#define __kuser_cmpxchg (*(__kuser_cmpxchg_t*) 0xffff0fc0)
55typedef void (__kuser_dmb_t)(void);
56#define __kuser_dmb (*(__kuser_dmb_t*) 0xffff0fa0)
57typedef 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 Hughes400628a2016-12-14 17:33:46 -080061#define noinline __attribute__((__noinline__))
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080062
Elliott Hughes400628a2016-12-14 17:33:46 -080063// Avoid name mangling so that stacks are more readable.
64extern "C" {
Elliott Hughes3808c4e2013-04-23 17:14:56 -070065
Elliott Hughes1b13b142023-08-08 16:00:30 -070066void crash1();
67void crash_no_stack();
68void crash_bti();
69void crash_pac();
Elliott Hughes23d1cad2016-05-10 13:29:58 -070070
Elliott Hughes400628a2016-12-14 17:33:46 -080071int do_action(const char* arg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080072
Elliott Hughes400628a2016-12-14 17:33:46 -080073noinline void maybe_abort() {
Elliott Hughesda6b2e22014-04-23 14:57:32 -070074 if (time(0) != 42) {
Elliott Hughes6f40caf2013-06-12 14:04:34 -070075 abort();
76 }
77}
78
Elliott Hughes400628a2016-12-14 17:33:46 -080079char* smash_stack_dummy_buf;
80noinline void smash_stack_dummy_function(volatile int* plen) {
Yabin Cui2331b952014-12-11 17:46:33 -080081 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 Hughes400628a2016-12-14 17:33:46 -080088noinline int smash_stack(volatile int* plen) {
89 printf("%s: deliberately corrupting stack...\n", getprogname());
Yabin Cui2331b952014-12-11 17:46:33 -080090
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 Hughesdf4200e2013-02-14 14:41:57 -080096}
97
Stephen Hines18395cb2015-09-29 23:55:14 -070098#pragma clang diagnostic push
99#pragma clang diagnostic ignored "-Winfinite-recursion"
100
Elliott Hughes400628a2016-12-14 17:33:46 -0800101void* global = 0; // So GCC doesn't optimize the tail recursion out of overflow_stack.
Elliott Hughesb1be27e2013-07-15 17:19:02 -0700102
Elliott Hughes400628a2016-12-14 17:33:46 -0800103noinline void overflow_stack(void* p) {
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700104 void* buf[1];
105 buf[0] = p;
Elliott Hughesb1be27e2013-07-15 17:19:02 -0700106 global = buf;
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700107 overflow_stack(&buf);
108}
109
Stephen Hines18395cb2015-09-29 23:55:14 -0700110#pragma clang diagnostic pop
111
Elliott Hughes400628a2016-12-14 17:33:46 -0800112noinline 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 Projectdd7bc332009-03-03 19:32:55 -0800115}
116
Elliott Hughes400628a2016-12-14 17:33:46 -0800117noinline int do_action_on_thread(const char* arg) {
Elliott Hughesaa421302012-12-10 14:15:42 -0800118 pthread_t t;
Elliott Hughes400628a2016-12-14 17:33:46 -0800119 pthread_create(&t, nullptr, thread_callback, const_cast<char*>(arg));
120 void* result = nullptr;
Elliott Hughesaa421302012-12-10 14:15:42 -0800121 pthread_join(t, &result);
Elliott Hughes400628a2016-12-14 17:33:46 -0800122 return reinterpret_cast<uintptr_t>(result);
Elliott Hughesaa421302012-12-10 14:15:42 -0800123}
124
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700125noinline int crash_null() {
126 int (*null_func)() = nullptr;
127 return null_func();
128}
129
Elliott Hughes400628a2016-12-14 17:33:46 -0800130noinline int crash3(int a) {
131 *reinterpret_cast<int*>(0xdead) = a;
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700132 return a*4;
Pavel Chupinaf2cb362013-03-08 13:17:35 +0400133}
134
Elliott Hughes400628a2016-12-14 17:33:46 -0800135noinline int crash2(int a) {
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700136 a = crash3(a) + 2;
137 return a*3;
Pavel Chupinaf2cb362013-03-08 13:17:35 +0400138}
139
Elliott Hughes400628a2016-12-14 17:33:46 -0800140noinline int crash(int a) {
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700141 a = crash2(a) + 1;
142 return a*2;
Pavel Chupinaf2cb362013-03-08 13:17:35 +0400143}
144
Pirama Arumuga Nainar982e2a52021-03-16 16:22:52 -0700145#pragma clang diagnostic push
146#pragma clang diagnostic ignored "-Wfree-nonheap-object"
147
Elliott Hughes400628a2016-12-14 17:33:46 -0800148noinline void abuse_heap() {
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700149 char buf[16];
Elliott Hughes400628a2016-12-14 17:33:46 -0800150 free(buf); // GCC is smart enough to warn about this, but we're doing it deliberately.
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700151}
Pirama Arumuga Nainar982e2a52021-03-16 16:22:52 -0700152#pragma clang diagnostic pop
Elliott Hughes6f40caf2013-06-12 14:04:34 -0700153
Josh Gao399b4ee2017-06-30 12:46:31 -0700154noinline void leak() {
155 while (true) {
156 void* mapping =
Kelvin Zhang786dac32023-06-15 16:23:56 -0700157 mmap(nullptr, getpagesize(), PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Josh Gao399b4ee2017-06-30 12:46:31 -0700158 static_cast<volatile char*>(mapping)[0] = 'a';
159 }
160}
161
Elliott Hughes400628a2016-12-14 17:33:46 -0800162noinline void sigsegv_non_null() {
Brigid Smith7b2078e2014-06-17 14:55:47 -0700163 int* a = (int *)(&do_action);
164 *a = 42;
165}
166
Elliott Hughes400628a2016-12-14 17:33:46 -0800167noinline void fprintf_null() {
zijunzhaof5e15332023-03-03 01:33:28 +0000168 FILE* sneaky_null = nullptr;
169 fprintf(sneaky_null, "oops");
Elliott Hughes400628a2016-12-14 17:33:46 -0800170}
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700171
Elliott Hughes400628a2016-12-14 17:33:46 -0800172noinline void readdir_null() {
zijunzhaofa8037c2023-03-31 23:50:37 +0000173 DIR* sneaky_null = nullptr;
174 readdir(sneaky_null);
Elliott Hughes400628a2016-12-14 17:33:46 -0800175}
176
177noinline int strlen_null() {
178 char* sneaky_null = nullptr;
179 return strlen(sneaky_null);
180}
181
182static 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 Hughes0ba53592017-02-01 16:59:15 -0800188 fprintf(stderr, " nostack crash with a NULL stack pointer\n");
189 fprintf(stderr, "\n");
Elliott Hughes400628a2016-12-14 17:33:46 -0800190 fprintf(stderr, " heap-usage cause a libc abort by abusing a heap function\n");
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700191 fprintf(stderr, " call-null cause a crash by calling through a nullptr\n");
Josh Gao399b4ee2017-06-30 12:46:31 -0700192 fprintf(stderr, " leak leak memory until we get OOM-killed\n");
Elliott Hughes0ba53592017-02-01 16:59:15 -0800193 fprintf(stderr, "\n");
Elliott Hughes400628a2016-12-14 17:33:46 -0800194 fprintf(stderr, " abort call abort()\n");
Christopher Ferris4f600fe2022-04-13 14:55:36 -0700195 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 Hughes400628a2016-12-14 17:33:46 -0800197 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 Hughes0ba53592017-02-01 16:59:15 -0800200 fprintf(stderr, "\n");
Elliott Hughes400628a2016-12-14 17:33:46 -0800201 fprintf(stderr, " fortify fail a _FORTIFY_SOURCE check\n");
Josh Gao3fa96372018-06-01 16:35:47 -0700202 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 Hughes0ba53592017-02-01 16:59:15 -0800204 fprintf(stderr, " seccomp fail a seccomp check\n");
Elliott Hughesdd04c652019-04-15 13:03:48 -0700205 fprintf(stderr, " xom read execute-only memory\n");
Elliott Hughes0ba53592017-02-01 16:59:15 -0800206 fprintf(stderr, "\n");
Elliott Hughes400628a2016-12-14 17:33:46 -0800207 fprintf(stderr, " LOG_ALWAYS_FATAL call liblog LOG_ALWAYS_FATAL\n");
208 fprintf(stderr, " LOG_ALWAYS_FATAL_IF call liblog LOG_ALWAYS_FATAL_IF\n");
209 fprintf(stderr, " LOG-FATAL call libbase LOG(FATAL)\n");
Elliott Hughes0ba53592017-02-01 16:59:15 -0800210 fprintf(stderr, "\n");
Elliott Hughes400628a2016-12-14 17:33:46 -0800211 fprintf(stderr, " SIGFPE cause a SIGFPE\n");
Elliott Hughes2baf4432018-05-30 12:55:04 -0700212 fprintf(stderr, " SIGILL cause a SIGILL\n");
Elliott Hughes400628a2016-12-14 17:33:46 -0800213 fprintf(stderr, " SIGSEGV cause a SIGSEGV at address 0x0 (synonym: crash)\n");
214 fprintf(stderr, " SIGSEGV-non-null cause a SIGSEGV at a non-zero address\n");
215 fprintf(stderr, " SIGSEGV-unmapped mmap/munmap a region of memory and then attempt to access it\n");
216 fprintf(stderr, " SIGTRAP cause a SIGTRAP\n");
Elliott Hughes0ba53592017-02-01 16:59:15 -0800217 fprintf(stderr, "\n");
Elliott Hughes400628a2016-12-14 17:33:46 -0800218 fprintf(stderr, " fprintf-NULL pass a null pointer to fprintf\n");
219 fprintf(stderr, " readdir-NULL pass a null pointer to readdir\n");
220 fprintf(stderr, " strlen-NULL pass a null pointer to strlen\n");
Elliott Hughesda9e3952017-02-17 10:26:48 -0800221 fprintf(stderr, " pthread_join-NULL pass a null pointer to pthread_join\n");
Elliott Hughes400628a2016-12-14 17:33:46 -0800222 fprintf(stderr, "\n");
Josh Gao91ad6532017-02-09 12:37:39 -0800223 fprintf(stderr, " no_new_privs set PR_SET_NO_NEW_PRIVS and then abort\n");
224 fprintf(stderr, "\n");
Elliott Hughes1b13b142023-08-08 16:00:30 -0700225#if defined(__arm__)
226 fprintf(stderr, "Also, since this is an arm32 binary:\n");
227 fprintf(stderr, " kuser_helper_version call kuser_helper_version\n");
228 fprintf(stderr, " kuser_get_tls call kuser_get_tls\n");
229 fprintf(stderr, " kuser_cmpxchg call kuser_cmpxchg\n");
230 fprintf(stderr, " kuser_memory_barrier call kuser_memory_barrier\n");
231 fprintf(stderr, " kuser_cmpxchg64 call kuser_cmpxchg64\n");
232#endif
233#if defined(__aarch64__)
234 fprintf(stderr, "Also, since this is an arm64 binary:\n");
235 fprintf(stderr, " bti fail a branch target identification (BTI) check\n");
236 fprintf(stderr, " pac fail a pointer authentication (PAC) check\n");
237#endif
238 fprintf(stderr, "\n");
Elliott Hughes400628a2016-12-14 17:33:46 -0800239 fprintf(stderr, "prefix any of the above with 'thread-' to run on a new thread\n");
240 fprintf(stderr, "prefix any of the above with 'exhaustfd-' to exhaust\n");
241 fprintf(stderr, "all available file descriptors before crashing.\n");
242 fprintf(stderr, "prefix any of the above with 'wait-' to wait until input is received on stdin\n");
243
244 return EXIT_FAILURE;
245}
246
Elliott Hughes1b13b142023-08-08 16:00:30 -0700247[[maybe_unused]] static void CheckCpuFeature(const std::string& name) {
248 std::string cpuinfo;
249 if (!android::base::ReadFileToString("/proc/cpuinfo", &cpuinfo)) {
250 error(1, errno, "couldn't read /proc/cpuinfo");
251 }
252 std::vector<std::string> lines = android::base::Split(cpuinfo, "\n");
253 for (std::string_view line : lines) {
254 if (!android::base::ConsumePrefix(&line, "Features\t:")) continue;
255 std::vector<std::string> features = android::base::Split(std::string(line), " ");
256 if (std::find(features.begin(), features.end(), name) == features.end()) {
257 error(1, 0, "/proc/cpuinfo does not report feature '%s'", name.c_str());
258 }
259 }
260}
261
Elliott Hughes400628a2016-12-14 17:33:46 -0800262noinline int do_action(const char* arg) {
263 // Prefixes.
Josh Gao100ce392016-10-31 17:37:37 -0700264 if (!strncmp(arg, "wait-", strlen("wait-"))) {
265 char buf[1];
Stephen Hines8395de62018-09-24 13:03:25 -0700266 UNUSED(TEMP_FAILURE_RETRY(read(STDIN_FILENO, buf, sizeof(buf))));
Josh Gao100ce392016-10-31 17:37:37 -0700267 return do_action(arg + strlen("wait-"));
268 } else if (!strncmp(arg, "exhaustfd-", strlen("exhaustfd-"))) {
Josh Gao218f7fb2016-10-07 16:42:05 -0700269 errno = 0;
270 while (errno != EMFILE) {
271 open("/dev/null", O_RDONLY);
272 }
273 return do_action(arg + strlen("exhaustfd-"));
274 } else if (!strncmp(arg, "thread-", strlen("thread-"))) {
Elliott Hughesaa421302012-12-10 14:15:42 -0800275 return do_action_on_thread(arg + strlen("thread-"));
Elliott Hughes400628a2016-12-14 17:33:46 -0800276 }
277
278 // Actions.
279 if (!strcasecmp(arg, "SIGSEGV-non-null")) {
Josh Gao3fa96372018-06-01 16:35:47 -0700280 sigsegv_non_null();
Elliott Hughes400628a2016-12-14 17:33:46 -0800281 } else if (!strcasecmp(arg, "smash-stack")) {
Josh Gao3fa96372018-06-01 16:35:47 -0700282 volatile int len = 128;
283 return smash_stack(&len);
Elliott Hughes400628a2016-12-14 17:33:46 -0800284 } else if (!strcasecmp(arg, "stack-overflow")) {
Josh Gao3fa96372018-06-01 16:35:47 -0700285 overflow_stack(nullptr);
Elliott Hughes400628a2016-12-14 17:33:46 -0800286 } else if (!strcasecmp(arg, "nostack")) {
Elliott Hughes1b13b142023-08-08 16:00:30 -0700287 crash_no_stack();
Elliott Hughes400628a2016-12-14 17:33:46 -0800288 } else if (!strcasecmp(arg, "exit")) {
Josh Gao3fa96372018-06-01 16:35:47 -0700289 exit(1);
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700290 } else if (!strcasecmp(arg, "call-null")) {
291 return crash_null();
Elliott Hughes400628a2016-12-14 17:33:46 -0800292 } else if (!strcasecmp(arg, "crash") || !strcmp(arg, "SIGSEGV")) {
Josh Gao3fa96372018-06-01 16:35:47 -0700293 return crash(42);
Elliott Hughes400628a2016-12-14 17:33:46 -0800294 } else if (!strcasecmp(arg, "abort")) {
Josh Gao3fa96372018-06-01 16:35:47 -0700295 maybe_abort();
Christopher Ferris4f600fe2022-04-13 14:55:36 -0700296 } else if (!strcasecmp(arg, "abort_with_msg")) {
297 android_set_abort_message("Aborting due to crasher");
298 maybe_abort();
299 } else if (!strcasecmp(arg, "abort_with_null")) {
300 android_set_abort_message(nullptr);
301 maybe_abort();
Elliott Hughes400628a2016-12-14 17:33:46 -0800302 } else if (!strcasecmp(arg, "assert")) {
Josh Gao3fa96372018-06-01 16:35:47 -0700303 __assert("some_file.c", 123, "false");
Elliott Hughes400628a2016-12-14 17:33:46 -0800304 } else if (!strcasecmp(arg, "assert2")) {
Josh Gao3fa96372018-06-01 16:35:47 -0700305 __assert2("some_file.c", 123, "some_function", "false");
Stephen Hines8395de62018-09-24 13:03:25 -0700306#if !defined(__clang_analyzer__)
Elliott Hughes400628a2016-12-14 17:33:46 -0800307 } else if (!strcasecmp(arg, "fortify")) {
Stephen Hines8395de62018-09-24 13:03:25 -0700308 // FORTIFY is disabled when running clang-tidy and other tools, so this
309 // shouldn't depend on internal implementation details of it.
Josh Gao3fa96372018-06-01 16:35:47 -0700310 char buf[10];
311 __read_chk(-1, buf, 32, 10);
312 while (true) pause();
Stephen Hines8395de62018-09-24 13:03:25 -0700313#endif
Josh Gao3fa96372018-06-01 16:35:47 -0700314 } else if (!strcasecmp(arg, "fdsan_file")) {
315 FILE* f = fopen("/dev/null", "r");
316 close(fileno(f));
317 } else if (!strcasecmp(arg, "fdsan_dir")) {
318 DIR* d = opendir("/dev/");
319 close(dirfd(d));
Elliott Hughes400628a2016-12-14 17:33:46 -0800320 } else if (!strcasecmp(arg, "LOG(FATAL)")) {
Josh Gao3fa96372018-06-01 16:35:47 -0700321 LOG(FATAL) << "hello " << 123;
Elliott Hughes400628a2016-12-14 17:33:46 -0800322 } else if (!strcasecmp(arg, "LOG_ALWAYS_FATAL")) {
Josh Gao3fa96372018-06-01 16:35:47 -0700323 LOG_ALWAYS_FATAL("hello %s", "world");
Elliott Hughes400628a2016-12-14 17:33:46 -0800324 } else if (!strcasecmp(arg, "LOG_ALWAYS_FATAL_IF")) {
Josh Gao3fa96372018-06-01 16:35:47 -0700325 LOG_ALWAYS_FATAL_IF(true, "hello %s", "world");
Elliott Hughes400628a2016-12-14 17:33:46 -0800326 } else if (!strcasecmp(arg, "SIGFPE")) {
Josh Gao3fa96372018-06-01 16:35:47 -0700327 raise(SIGFPE);
328 return EXIT_SUCCESS;
Elliott Hughes2baf4432018-05-30 12:55:04 -0700329 } else if (!strcasecmp(arg, "SIGILL")) {
330#if defined(__aarch64__)
331 __asm__ volatile(".word 0\n");
332#elif defined(__arm__)
333 __asm__ volatile(".word 0xe7f0def0\n");
334#elif defined(__i386__) || defined(__x86_64__)
335 __asm__ volatile("ud2\n");
Xia Lifangb13a10b2022-10-12 22:27:54 +0800336#elif defined(__riscv)
337 __asm__ volatile("unimp\n");
Elliott Hughes2baf4432018-05-30 12:55:04 -0700338#else
339#error
340#endif
Elliott Hughes400628a2016-12-14 17:33:46 -0800341 } else if (!strcasecmp(arg, "SIGTRAP")) {
Josh Gao3fa96372018-06-01 16:35:47 -0700342 raise(SIGTRAP);
343 return EXIT_SUCCESS;
Elliott Hughes400628a2016-12-14 17:33:46 -0800344 } else if (!strcasecmp(arg, "fprintf-NULL")) {
Josh Gao3fa96372018-06-01 16:35:47 -0700345 fprintf_null();
Elliott Hughes400628a2016-12-14 17:33:46 -0800346 } else if (!strcasecmp(arg, "readdir-NULL")) {
Josh Gao3fa96372018-06-01 16:35:47 -0700347 readdir_null();
Elliott Hughes400628a2016-12-14 17:33:46 -0800348 } else if (!strcasecmp(arg, "strlen-NULL")) {
Josh Gao3fa96372018-06-01 16:35:47 -0700349 return strlen_null();
Elliott Hughesda9e3952017-02-17 10:26:48 -0800350 } else if (!strcasecmp(arg, "pthread_join-NULL")) {
Josh Gao3fa96372018-06-01 16:35:47 -0700351 return pthread_join(0, nullptr);
Elliott Hughes400628a2016-12-14 17:33:46 -0800352 } else if (!strcasecmp(arg, "heap-usage")) {
Josh Gao3fa96372018-06-01 16:35:47 -0700353 abuse_heap();
Josh Gao399b4ee2017-06-30 12:46:31 -0700354 } else if (!strcasecmp(arg, "leak")) {
Josh Gao3fa96372018-06-01 16:35:47 -0700355 leak();
Elliott Hughes400628a2016-12-14 17:33:46 -0800356 } else if (!strcasecmp(arg, "SIGSEGV-unmapped")) {
Josh Gao3fa96372018-06-01 16:35:47 -0700357 char* map = reinterpret_cast<char*>(
358 mmap(nullptr, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0));
359 munmap(map, sizeof(int));
360 map[0] = '8';
Elliott Hughes0ba53592017-02-01 16:59:15 -0800361 } else if (!strcasecmp(arg, "seccomp")) {
Josh Gao3fa96372018-06-01 16:35:47 -0700362 set_system_seccomp_filter();
363 syscall(99999);
Elliott Hughesdd04c652019-04-15 13:03:48 -0700364#if defined(__LP64__)
365 } else if (!strcasecmp(arg, "xom")) {
366 // Try to read part of our code, which will fail if XOM is active.
367 printf("*%lx = %lx\n", reinterpret_cast<long>(usage), *reinterpret_cast<long*>(usage));
368#endif
Elliott Hughes0ba53592017-02-01 16:59:15 -0800369#if defined(__arm__)
370 } else if (!strcasecmp(arg, "kuser_helper_version")) {
371 return __kuser_helper_version;
372 } else if (!strcasecmp(arg, "kuser_get_tls")) {
373 return !__kuser_get_tls();
374 } else if (!strcasecmp(arg, "kuser_cmpxchg")) {
375 return __kuser_cmpxchg(0, 0, 0);
376 } else if (!strcasecmp(arg, "kuser_memory_barrier")) {
377 __kuser_dmb();
378 } else if (!strcasecmp(arg, "kuser_cmpxchg64")) {
379 return __kuser_cmpxchg64(0, 0, 0);
380#endif
Elliott Hughes1b13b142023-08-08 16:00:30 -0700381#if defined(__aarch64__)
382 } else if (!strcasecmp(arg, "bti")) {
383 CheckCpuFeature("bti");
384 crash_bti();
385 } else if (!strcasecmp(arg, "pac")) {
386 CheckCpuFeature("paca");
387 crash_pac();
388#endif
Josh Gao91ad6532017-02-09 12:37:39 -0800389 } else if (!strcasecmp(arg, "no_new_privs")) {
390 if (prctl(PR_SET_NO_NEW_PRIVS, 1) != 0) {
391 fprintf(stderr, "prctl(PR_SET_NO_NEW_PRIVS, 1) failed: %s\n", strerror(errno));
392 return EXIT_SUCCESS;
393 }
394 abort();
Elliott Hughes400628a2016-12-14 17:33:46 -0800395 } else {
396 return usage();
Elliott Hughesaa421302012-12-10 14:15:42 -0800397 }
398
Elliott Hughes400628a2016-12-14 17:33:46 -0800399 fprintf(stderr, "%s: exiting normally!\n", getprogname());
Elliott Hughes3808c4e2013-04-23 17:14:56 -0700400 return EXIT_SUCCESS;
Elliott Hughesaa421302012-12-10 14:15:42 -0800401}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800402
Elliott Hughes400628a2016-12-14 17:33:46 -0800403int main(int argc, char** argv) {
Josh Gao9c02dc52016-06-15 17:29:00 -0700404#if defined(STATIC_CRASHER)
405 debuggerd_callbacks_t callbacks = {
Peter Collingbournef3d542f2020-03-05 16:46:15 -0800406 .get_process_info = []() {
Josh Gao9c02dc52016-06-15 17:29:00 -0700407 static struct {
408 size_t size;
409 char msg[32];
410 } msg;
411
412 msg.size = strlen("dummy abort message");
413 memcpy(msg.msg, "dummy abort message", strlen("dummy abort message"));
Peter Collingbournef3d542f2020-03-05 16:46:15 -0800414 return debugger_process_info{
415 .abort_msg = reinterpret_cast<void*>(&msg),
416 };
Josh Gao9c02dc52016-06-15 17:29:00 -0700417 },
418 .post_dump = nullptr
419 };
420 debuggerd_init(&callbacks);
421#endif
422
Elliott Hughes400628a2016-12-14 17:33:46 -0800423 if (argc == 1) crash1();
424 else if (argc == 2) return do_action(argv[1]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800425
Elliott Hughes400628a2016-12-14 17:33:46 -0800426 return usage();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800427}
Elliott Hughes400628a2016-12-14 17:33:46 -0800428
429};