| Josh Gao | 10ec928 | 2017-04-03 15:13:29 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2017 The Android Open Source Project | 
|  | 3 | * All rights reserved. | 
|  | 4 | * | 
|  | 5 | * Redistribution and use in source and binary forms, with or without | 
|  | 6 | * modification, are permitted provided that the following conditions | 
|  | 7 | * are met: | 
|  | 8 | *  * Redistributions of source code must retain the above copyright | 
|  | 9 | *    notice, this list of conditions and the following disclaimer. | 
|  | 10 | *  * Redistributions in binary form must reproduce the above copyright | 
|  | 11 | *    notice, this list of conditions and the following disclaimer in | 
|  | 12 | *    the documentation and/or other materials provided with the | 
|  | 13 | *    distribution. | 
|  | 14 | * | 
|  | 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 
|  | 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 
|  | 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | 
|  | 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | 
|  | 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | 
|  | 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | 
|  | 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS | 
|  | 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | 
|  | 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | 
|  | 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | 
|  | 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 
|  | 26 | * SUCH DAMAGE. | 
|  | 27 | */ | 
|  | 28 |  | 
|  | 29 | #include <android/set_abort_message.h> | 
|  | 30 |  | 
| Florian Mayer | 7c83d09 | 2024-02-07 16:25:18 -0800 | [diff] [blame] | 31 | #include <async_safe/log.h> | 
|  | 32 | #include <bionic/set_abort_message_internal.h> | 
|  | 33 |  | 
|  | 34 | #include <bits/stdatomic.h> | 
| Josh Gao | 10ec928 | 2017-04-03 15:13:29 -0700 | [diff] [blame] | 35 | #include <pthread.h> | 
| Luis Hector Chavez | 6d93c60 | 2018-08-22 07:39:26 -0700 | [diff] [blame] | 36 | #include <stddef.h> | 
| Florian Mayer | 7c83d09 | 2024-02-07 16:25:18 -0800 | [diff] [blame] | 37 | #include <stdint.h> | 
| Dan Albert | 1c78cb0 | 2017-10-11 11:25:25 -0700 | [diff] [blame] | 38 | #include <string.h> | 
| Josh Gao | 10ec928 | 2017-04-03 15:13:29 -0700 | [diff] [blame] | 39 | #include <sys/mman.h> | 
| Peter Collingbourne | feb5ed1 | 2019-03-27 14:28:57 -0700 | [diff] [blame] | 40 | #include <sys/prctl.h> | 
| Josh Gao | 10ec928 | 2017-04-03 15:13:29 -0700 | [diff] [blame] | 41 |  | 
| dimitry | b6d2b87 | 2017-10-18 15:15:54 +0200 | [diff] [blame] | 42 | #include "private/bionic_defs.h" | 
| Ryan Prichard | 7752bcb | 2018-11-22 02:41:04 -0800 | [diff] [blame] | 43 | #include "private/bionic_globals.h" | 
| Josh Gao | 10ec928 | 2017-04-03 15:13:29 -0700 | [diff] [blame] | 44 | #include "private/ScopedPthreadMutexLocker.h" | 
|  | 45 |  | 
| Josh Gao | 10ec928 | 2017-04-03 15:13:29 -0700 | [diff] [blame] | 46 | struct abort_msg_t { | 
|  | 47 | size_t size; | 
|  | 48 | char msg[0]; | 
|  | 49 | }; | 
| Luis Hector Chavez | 6d93c60 | 2018-08-22 07:39:26 -0700 | [diff] [blame] | 50 | static_assert( | 
|  | 51 | offsetof(abort_msg_t, msg) == sizeof(size_t), | 
|  | 52 | "The in-memory layout of abort_msg_t is not consistent with what libdebuggerd expects."); | 
|  | 53 |  | 
|  | 54 | struct magic_abort_msg_t { | 
|  | 55 | uint64_t magic1; | 
|  | 56 | uint64_t magic2; | 
|  | 57 | abort_msg_t msg; | 
|  | 58 | }; | 
|  | 59 | static_assert(offsetof(magic_abort_msg_t, msg) == 2 * sizeof(uint64_t), | 
|  | 60 | "The in-memory layout of magic_abort_msg_t is not consistent with what automated " | 
|  | 61 | "tools expect."); | 
| Josh Gao | 10ec928 | 2017-04-03 15:13:29 -0700 | [diff] [blame] | 62 |  | 
| Florian Mayer | 7c83d09 | 2024-02-07 16:25:18 -0800 | [diff] [blame] | 63 | static _Atomic(crash_detail_t*) free_head = nullptr; | 
|  | 64 |  | 
| Luis Hector Chavez | 6d93c60 | 2018-08-22 07:39:26 -0700 | [diff] [blame] | 65 | [[clang::optnone]] | 
|  | 66 | static void fill_abort_message_magic(magic_abort_msg_t* new_magic_abort_message) { | 
|  | 67 | // 128-bit magic for the abort message. Chosen by fair dice roll. | 
|  | 68 | // This function is intentionally deoptimized to avoid the magic to be present | 
|  | 69 | // in the final binary. This causes clang to only use instructions where parts | 
|  | 70 | // of the magic are encoded into immediate arguments for the instructions in | 
|  | 71 | // all supported architectures. | 
|  | 72 | new_magic_abort_message->magic1 = 0xb18e40886ac388f0ULL; | 
|  | 73 | new_magic_abort_message->magic2 = 0xc6dfba755a1de0b5ULL; | 
|  | 74 | } | 
|  | 75 |  | 
| dimitry | b6d2b87 | 2017-10-18 15:15:54 +0200 | [diff] [blame] | 76 | __BIONIC_WEAK_FOR_NATIVE_BRIDGE | 
| Josh Gao | 10ec928 | 2017-04-03 15:13:29 -0700 | [diff] [blame] | 77 | void android_set_abort_message(const char* msg) { | 
| Ryan Prichard | 7752bcb | 2018-11-22 02:41:04 -0800 | [diff] [blame] | 78 | ScopedPthreadMutexLocker locker(&__libc_shared_globals()->abort_msg_lock); | 
| Josh Gao | 10ec928 | 2017-04-03 15:13:29 -0700 | [diff] [blame] | 79 |  | 
| Ryan Prichard | 7752bcb | 2018-11-22 02:41:04 -0800 | [diff] [blame] | 80 | if (__libc_shared_globals()->abort_msg != nullptr) { | 
| Josh Gao | 10ec928 | 2017-04-03 15:13:29 -0700 | [diff] [blame] | 81 | // We already have an abort message. | 
|  | 82 | // Assume that the first crash is the one most worth reporting. | 
|  | 83 | return; | 
|  | 84 | } | 
|  | 85 |  | 
| Christopher Ferris | bbf9cd8 | 2022-04-11 16:01:37 -0700 | [diff] [blame] | 86 | if (msg == nullptr) { | 
|  | 87 | msg = "(null)"; | 
|  | 88 | } | 
|  | 89 |  | 
| Luis Hector Chavez | 6d93c60 | 2018-08-22 07:39:26 -0700 | [diff] [blame] | 90 | size_t size = sizeof(magic_abort_msg_t) + strlen(msg) + 1; | 
| Josh Gao | 10ec928 | 2017-04-03 15:13:29 -0700 | [diff] [blame] | 91 | void* map = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0); | 
|  | 92 | if (map == MAP_FAILED) { | 
|  | 93 | return; | 
|  | 94 | } | 
|  | 95 |  | 
| Peter Collingbourne | feb5ed1 | 2019-03-27 14:28:57 -0700 | [diff] [blame] | 96 | // Name the abort message mapping to make it easier for tools to find the | 
|  | 97 | // mapping. | 
|  | 98 | prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, map, size, "abort message"); | 
|  | 99 |  | 
| Luis Hector Chavez | 6d93c60 | 2018-08-22 07:39:26 -0700 | [diff] [blame] | 100 | magic_abort_msg_t* new_magic_abort_message = reinterpret_cast<magic_abort_msg_t*>(map); | 
|  | 101 | fill_abort_message_magic(new_magic_abort_message); | 
|  | 102 | new_magic_abort_message->msg.size = size; | 
|  | 103 | strcpy(new_magic_abort_message->msg.msg, msg); | 
| Ryan Prichard | 7752bcb | 2018-11-22 02:41:04 -0800 | [diff] [blame] | 104 | __libc_shared_globals()->abort_msg = &new_magic_abort_message->msg; | 
| Josh Gao | 10ec928 | 2017-04-03 15:13:29 -0700 | [diff] [blame] | 105 | } | 
| Florian Mayer | 7c83d09 | 2024-02-07 16:25:18 -0800 | [diff] [blame] | 106 |  | 
|  | 107 | __BIONIC_WEAK_FOR_NATIVE_BRIDGE | 
|  | 108 | crash_detail_t* android_register_crash_detail(const void* name, size_t name_size, const void* data, | 
|  | 109 | size_t data_size) { | 
|  | 110 | auto populate_crash_detail = [&](crash_detail_t* result) { | 
|  | 111 | result->name = reinterpret_cast<const char*>(name); | 
|  | 112 | result->name_size = name_size; | 
|  | 113 | result->data = reinterpret_cast<const char*>(data); | 
|  | 114 | result->data_size = data_size; | 
|  | 115 | }; | 
|  | 116 | // This is a atomic fast-path for RAII use-cases where the app keeps creating and deleting | 
|  | 117 | // crash details for short periods of time to capture detailed scopes. | 
|  | 118 | if (crash_detail_t* head = atomic_load(&free_head)) { | 
|  | 119 | while (head != nullptr && !atomic_compare_exchange_strong(&free_head, &head, head->prev_free)) { | 
|  | 120 | // intentionally left blank. | 
|  | 121 | } | 
|  | 122 | if (head) { | 
|  | 123 | head->prev_free = nullptr; | 
|  | 124 | populate_crash_detail(head); | 
|  | 125 | return head; | 
|  | 126 | } | 
|  | 127 | } | 
|  | 128 | ScopedPthreadMutexLocker locker(&__libc_shared_globals()->crash_detail_page_lock); | 
|  | 129 | struct crash_detail_page_t* prev = nullptr; | 
|  | 130 | struct crash_detail_page_t* page = __libc_shared_globals()->crash_detail_page; | 
|  | 131 | if (page != nullptr && page->used == kNumCrashDetails) { | 
|  | 132 | prev = page; | 
|  | 133 | page = nullptr; | 
|  | 134 | } | 
|  | 135 | if (page == nullptr) { | 
|  | 136 | size_t size = sizeof(crash_detail_page_t); | 
|  | 137 | void* map = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0); | 
|  | 138 | if (map == MAP_FAILED) { | 
|  | 139 | async_safe_format_log(ANDROID_LOG_ERROR, "libc", "failed to allocate crash_detail_page: %m"); | 
|  | 140 | return nullptr; | 
|  | 141 | } | 
|  | 142 | prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, map, size, "crash details"); | 
|  | 143 | page = reinterpret_cast<struct crash_detail_page_t*>(map); | 
|  | 144 | page->prev = prev; | 
|  | 145 | __libc_shared_globals()->crash_detail_page = page; | 
|  | 146 | } | 
|  | 147 | crash_detail_t* result = &page->crash_details[page->used]; | 
|  | 148 | populate_crash_detail(result); | 
|  | 149 | page->used++; | 
|  | 150 | return result; | 
|  | 151 | } | 
|  | 152 |  | 
|  | 153 | __BIONIC_WEAK_FOR_NATIVE_BRIDGE | 
|  | 154 | void android_unregister_crash_detail(crash_detail_t* crash_detail) { | 
|  | 155 | if (crash_detail) { | 
|  | 156 | if (crash_detail->prev_free) { | 
|  | 157 | // removing already removed would mess up the free-list by creating a circle. | 
|  | 158 | return; | 
|  | 159 | } | 
|  | 160 | crash_detail->data = nullptr; | 
|  | 161 | crash_detail->name = nullptr; | 
|  | 162 | crash_detail_t* prev = atomic_load(&free_head); | 
|  | 163 | do { | 
|  | 164 | crash_detail->prev_free = prev; | 
|  | 165 | } while (!atomic_compare_exchange_strong(&free_head, &prev, crash_detail)); | 
|  | 166 | } | 
|  | 167 | } |