| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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 "DEBUG" |
| 18 | |
| 19 | #include "libdebuggerd/tombstone.h" |
| Peter Collingbourne | 1a1f7d7 | 2021-03-08 16:53:54 -0800 | [diff] [blame] | 20 | #include "libdebuggerd/gwp_asan.h" |
| Christopher Ferris | 7aad256 | 2021-09-24 00:06:38 -0700 | [diff] [blame] | 21 | #if defined(USE_SCUDO) |
| Peter Collingbourne | 1a1f7d7 | 2021-03-08 16:53:54 -0800 | [diff] [blame] | 22 | #include "libdebuggerd/scudo.h" |
| Christopher Ferris | 7aad256 | 2021-09-24 00:06:38 -0700 | [diff] [blame] | 23 | #endif |
| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 24 | |
| 25 | #include <errno.h> |
| 26 | #include <fcntl.h> |
| 27 | #include <inttypes.h> |
| 28 | #include <signal.h> |
| 29 | #include <stddef.h> |
| Florian Mayer | e3e7bc7 | 2024-04-26 09:38:50 -0700 | [diff] [blame] | 30 | #include <stdint.h> |
| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 31 | #include <stdlib.h> |
| 32 | #include <string.h> |
| 33 | #include <sys/mman.h> |
| Elliott Hughes | 32d3cdd | 2021-09-21 12:20:53 -0700 | [diff] [blame] | 34 | #include <sys/sysinfo.h> |
| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 35 | #include <time.h> |
| 36 | |
| Christopher Ferris | 6a5db68 | 2024-11-15 01:06:55 +0000 | [diff] [blame] | 37 | #include <map> |
| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 38 | #include <memory> |
| Josh Gao | dbb83de | 2021-03-01 23:13:13 -0800 | [diff] [blame] | 39 | #include <optional> |
| Christopher Ferris | c95047d | 2022-03-14 15:02:11 -0700 | [diff] [blame] | 40 | #include <set> |
| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 41 | #include <string> |
| Christopher Ferris | 6a5db68 | 2024-11-15 01:06:55 +0000 | [diff] [blame] | 42 | #include <utility> |
| 43 | #include <vector> |
| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 44 | |
| Josh Gao | 618cea3 | 2021-01-26 17:45:43 -0800 | [diff] [blame] | 45 | #include <async_safe/log.h> |
| 46 | |
| Josh Gao | dbb83de | 2021-03-01 23:13:13 -0800 | [diff] [blame] | 47 | #include <android-base/file.h> |
| Christopher Ferris | 2b98c82 | 2021-09-28 15:50:04 -0700 | [diff] [blame] | 48 | #include <android-base/logging.h> |
| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 49 | #include <android-base/properties.h> |
| 50 | #include <android-base/stringprintf.h> |
| 51 | #include <android-base/strings.h> |
| 52 | #include <android-base/unique_fd.h> |
| 53 | |
| 54 | #include <android/log.h> |
| Florian Mayer | 5fa6663 | 2024-02-07 16:42:23 -0800 | [diff] [blame] | 55 | #include <android/set_abort_message.h> |
| Florian Mayer | 920d95b | 2024-02-14 12:57:09 -0800 | [diff] [blame] | 56 | #include <bionic/crash_detail_internal.h> |
| Florian Mayer | e3e7bc7 | 2024-04-26 09:38:50 -0700 | [diff] [blame] | 57 | #include <bionic/macros.h> |
| 58 | #include <bionic/mte.h> |
| 59 | #include <bionic/reserved_signals.h> |
| 60 | #include <bionic/tls_defines.h> |
| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 61 | #include <log/log.h> |
| 62 | #include <log/log_read.h> |
| 63 | #include <log/logprint.h> |
| 64 | #include <private/android_filesystem_config.h> |
| 65 | |
| Josh Gao | dbb83de | 2021-03-01 23:13:13 -0800 | [diff] [blame] | 66 | #include <procinfo/process.h> |
| Christopher Ferris | 3b7b7ba | 2022-03-15 16:56:09 -0700 | [diff] [blame] | 67 | #include <unwindstack/AndroidUnwinder.h> |
| 68 | #include <unwindstack/Error.h> |
| 69 | #include <unwindstack/MapInfo.h> |
| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 70 | #include <unwindstack/Maps.h> |
| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 71 | #include <unwindstack/Regs.h> |
| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 72 | |
| 73 | #include "libdebuggerd/open_files_list.h" |
| 74 | #include "libdebuggerd/utility.h" |
| Peter Collingbourne | 39a1730 | 2024-03-07 17:50:10 -0800 | [diff] [blame] | 75 | #include "libdebuggerd/utility_host.h" |
| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 76 | #include "util.h" |
| 77 | |
| 78 | #include "tombstone.pb.h" |
| 79 | |
| 80 | using android::base::StringPrintf; |
| 81 | |
| Christopher Ferris | 98d6242 | 2023-05-24 20:01:10 +0000 | [diff] [blame] | 82 | // The maximum number of messages to save in the protobuf per file. |
| 83 | static constexpr size_t kMaxLogMessages = 500; |
| 84 | |
| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 85 | // Use the demangler from libc++. |
| 86 | extern "C" char* __cxa_demangle(const char*, char*, size_t*, int* status); |
| 87 | |
| 88 | static Architecture get_arch() { |
| 89 | #if defined(__arm__) |
| 90 | return Architecture::ARM32; |
| 91 | #elif defined(__aarch64__) |
| 92 | return Architecture::ARM64; |
| 93 | #elif defined(__i386__) |
| 94 | return Architecture::X86; |
| 95 | #elif defined(__x86_64__) |
| 96 | return Architecture::X86_64; |
| Liu Cunyuan | 8c0101b | 2022-10-12 22:35:51 +0800 | [diff] [blame] | 97 | #elif defined(__riscv) && (__riscv_xlen == 64) |
| 98 | return Architecture::RISCV64; |
| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 99 | #else |
| 100 | #error Unknown architecture! |
| 101 | #endif |
| 102 | } |
| 103 | |
| 104 | static std::optional<std::string> get_stack_overflow_cause(uint64_t fault_addr, uint64_t sp, |
| 105 | unwindstack::Maps* maps) { |
| Mitch Phillips | bf2d6dd | 2024-02-06 15:18:04 +0100 | [diff] [blame] | 106 | // Under stack MTE the stack pointer and/or the fault address can be tagged. |
| 107 | // In order to calculate deltas between them, strip off the tags off both |
| 108 | // addresses. |
| 109 | fault_addr = untag_address(fault_addr); |
| 110 | sp = untag_address(sp); |
| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 111 | static constexpr uint64_t kMaxDifferenceBytes = 256; |
| 112 | uint64_t difference; |
| 113 | if (sp >= fault_addr) { |
| 114 | difference = sp - fault_addr; |
| 115 | } else { |
| 116 | difference = fault_addr - sp; |
| 117 | } |
| 118 | if (difference <= kMaxDifferenceBytes) { |
| 119 | // The faulting address is close to the current sp, check if the sp |
| 120 | // indicates a stack overflow. |
| 121 | // On arm, the sp does not get updated when the instruction faults. |
| 122 | // In this case, the sp will still be in a valid map, which is the |
| 123 | // last case below. |
| 124 | // On aarch64, the sp does get updated when the instruction faults. |
| 125 | // In this case, the sp will be in either an invalid map if triggered |
| 126 | // on the main thread, or in a guard map if in another thread, which |
| 127 | // will be the first case or second case from below. |
| Christopher Ferris | c677606 | 2021-01-15 15:59:28 -0800 | [diff] [blame] | 128 | std::shared_ptr<unwindstack::MapInfo> map_info = maps->Find(sp); |
| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 129 | if (map_info == nullptr) { |
| 130 | return "stack pointer is in a non-existent map; likely due to stack overflow."; |
| David Srbecky | d8ab48b | 2021-05-13 00:03:26 +0100 | [diff] [blame] | 131 | } else if ((map_info->flags() & (PROT_READ | PROT_WRITE)) != (PROT_READ | PROT_WRITE)) { |
| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 132 | return "stack pointer is not in a rw map; likely due to stack overflow."; |
| David Srbecky | d8ab48b | 2021-05-13 00:03:26 +0100 | [diff] [blame] | 133 | } else if ((sp - map_info->start()) <= kMaxDifferenceBytes) { |
| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 134 | return "stack pointer is close to top of stack; likely stack overflow."; |
| 135 | } |
| 136 | } |
| 137 | return {}; |
| 138 | } |
| 139 | |
| Peter Collingbourne | 1a1f7d7 | 2021-03-08 16:53:54 -0800 | [diff] [blame] | 140 | void set_human_readable_cause(Cause* cause, uint64_t fault_addr) { |
| 141 | if (!cause->has_memory_error() || !cause->memory_error().has_heap()) { |
| 142 | return; |
| 143 | } |
| 144 | |
| 145 | const MemoryError& memory_error = cause->memory_error(); |
| 146 | const HeapObject& heap_object = memory_error.heap(); |
| 147 | |
| 148 | const char *tool_str; |
| 149 | switch (memory_error.tool()) { |
| 150 | case MemoryError_Tool_GWP_ASAN: |
| 151 | tool_str = "GWP-ASan"; |
| 152 | break; |
| 153 | case MemoryError_Tool_SCUDO: |
| 154 | tool_str = "MTE"; |
| 155 | break; |
| 156 | default: |
| 157 | tool_str = "Unknown"; |
| 158 | break; |
| 159 | } |
| 160 | |
| 161 | const char *error_type_str; |
| 162 | switch (memory_error.type()) { |
| 163 | case MemoryError_Type_USE_AFTER_FREE: |
| 164 | error_type_str = "Use After Free"; |
| 165 | break; |
| 166 | case MemoryError_Type_DOUBLE_FREE: |
| 167 | error_type_str = "Double Free"; |
| 168 | break; |
| 169 | case MemoryError_Type_INVALID_FREE: |
| 170 | error_type_str = "Invalid (Wild) Free"; |
| 171 | break; |
| 172 | case MemoryError_Type_BUFFER_OVERFLOW: |
| 173 | error_type_str = "Buffer Overflow"; |
| 174 | break; |
| 175 | case MemoryError_Type_BUFFER_UNDERFLOW: |
| 176 | error_type_str = "Buffer Underflow"; |
| 177 | break; |
| 178 | default: |
| 179 | cause->set_human_readable( |
| 180 | StringPrintf("[%s]: Unknown error occurred at 0x%" PRIx64 ".", tool_str, fault_addr)); |
| 181 | return; |
| 182 | } |
| 183 | |
| 184 | uint64_t diff; |
| 185 | const char* location_str; |
| 186 | |
| 187 | if (fault_addr < heap_object.address()) { |
| 188 | // Buffer Underflow, 6 bytes left of a 41-byte allocation at 0xdeadbeef. |
| 189 | location_str = "left of"; |
| 190 | diff = heap_object.address() - fault_addr; |
| 191 | } else if (fault_addr - heap_object.address() < heap_object.size()) { |
| 192 | // Use After Free, 40 bytes into a 41-byte allocation at 0xdeadbeef. |
| 193 | location_str = "into"; |
| 194 | diff = fault_addr - heap_object.address(); |
| 195 | } else { |
| 196 | // Buffer Overflow, 6 bytes right of a 41-byte allocation at 0xdeadbeef. |
| 197 | location_str = "right of"; |
| 198 | diff = fault_addr - heap_object.address() - heap_object.size(); |
| 199 | } |
| 200 | |
| 201 | // Suffix of 'bytes', i.e. 4 bytes' vs. '1 byte'. |
| 202 | const char* byte_suffix = "s"; |
| 203 | if (diff == 1) { |
| 204 | byte_suffix = ""; |
| 205 | } |
| 206 | |
| 207 | cause->set_human_readable(StringPrintf( |
| 208 | "[%s]: %s, %" PRIu64 " byte%s %s a %" PRIu64 "-byte allocation at 0x%" PRIx64, tool_str, |
| 209 | error_type_str, diff, byte_suffix, location_str, heap_object.size(), heap_object.address())); |
| 210 | } |
| 211 | |
| Florian Mayer | e3e7bc7 | 2024-04-26 09:38:50 -0700 | [diff] [blame] | 212 | #if defined(__aarch64__) |
| 213 | void dump_stack_history(unwindstack::AndroidUnwinder* unwinder, uintptr_t target_tls, |
| 214 | StackHistoryBuffer& shb_obj, bool nounwind) { |
| 215 | auto process_memory = unwinder->GetProcessMemory(); |
| 216 | target_tls += sizeof(void*) * TLS_SLOT_STACK_MTE; |
| 217 | uintptr_t stack_mte; |
| 218 | if (!process_memory->ReadFully(target_tls, &stack_mte, sizeof(stack_mte))) { |
| 219 | async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, |
| 220 | "dump_stack_history: failed to read TLS_SLOT_STACK_MTE: %m"); |
| 221 | return; |
| 222 | } |
| 223 | if (stack_mte == 0) { |
| 224 | async_safe_format_log(ANDROID_LOG_DEBUG, LOG_TAG, |
| 225 | "dump_stack_history: stack history buffer is null"); |
| 226 | return; |
| 227 | } |
| 228 | uintptr_t untagged_stack_mte = untag_address(stack_mte); |
| 229 | uintptr_t buf_size = stack_mte_ringbuffer_size_from_pointer(stack_mte); |
| 230 | if (buf_size == 0) { |
| 231 | async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, "dump_stack_history: empty size"); |
| 232 | return; |
| 233 | } |
| 234 | uintptr_t buf_start = untagged_stack_mte & ~(buf_size - 1ULL); |
| 235 | std::vector<char> buf(buf_size); |
| 236 | if (!process_memory->ReadFully(buf_start, buf.data(), buf.size())) { |
| 237 | async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, |
| 238 | "dump_stack_history: failed to read stack history: %m"); |
| 239 | return; |
| 240 | } |
| 241 | uintptr_t original_off = untagged_stack_mte - buf_start; |
| 242 | if (original_off % 16 || original_off > buf_size) { |
| 243 | async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, |
| 244 | "dump_stack_history: invalid offset: %" PRIuPTR, original_off); |
| 245 | return; |
| 246 | } |
| 247 | |
| 248 | // The original_off is the next slot that would have been written, so the last |
| 249 | // slot that was written is the previous one. |
| 250 | for (uintptr_t idx = 16; idx <= buf_size; idx += 16) { |
| 251 | int64_t off = original_off - idx; |
| 252 | if (off < 0) off += buf_size; |
| 253 | uintptr_t pc, taggedfp; |
| 254 | memcpy(&pc, &(buf[off]), sizeof(pc)); |
| 255 | memcpy(&taggedfp, &(buf[off + sizeof(pc)]), sizeof(taggedfp)); |
| 256 | |
| 257 | if (pc == 0) break; |
| 258 | uintptr_t fp = untag_address(taggedfp); |
| 259 | uintptr_t tag = taggedfp >> 56; |
| 260 | |
| 261 | unwindstack::FrameData frame_data; |
| 262 | |
| 263 | if (nounwind) { |
| 264 | frame_data.pc = pc; |
| 265 | } else { |
| 266 | // +4 is to counteract the "pc adjustment" in BuildFrameFromPcOnly. |
| 267 | // BuildFrameFromPcOnly assumes we are unwinding, so it needs to correct for that |
| 268 | // the PC is the return address. That is not the case here. |
| 269 | // It doesn't really matter, because either should be in the correct function, but |
| 270 | // this is more correct (and consistent with the nounwind case). |
| 271 | frame_data = unwinder->BuildFrameFromPcOnly(pc); |
| 272 | frame_data.pc += 4; |
| 273 | frame_data.rel_pc += 4; |
| 274 | } |
| 275 | |
| 276 | StackHistoryBufferEntry* entry = shb_obj.add_entries(); |
| 277 | fill_in_backtrace_frame(entry->mutable_addr(), frame_data); |
| 278 | entry->set_fp(fp); |
| 279 | entry->set_tag(tag); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | static pid_t get_containing_thread(unwindstack::MapInfo* map_info, pid_t main_tid) { |
| 284 | if (map_info == nullptr) return 0; |
| 285 | |
| 286 | std::string name = map_info->name(); |
| 287 | if (name == "[stack]") { |
| 288 | return main_tid; |
| 289 | } |
| 290 | int tid; |
| 291 | if (sscanf(name.c_str(), "[anon:stack_and_tls:%d", &tid) != 1) { |
| 292 | return 0; |
| 293 | } |
| 294 | return tid; |
| 295 | } |
| 296 | |
| 297 | static std::optional<std::string> maybe_stack_mte_cause( |
| 298 | Tombstone* tombstone, unwindstack::AndroidUnwinder* unwinder, const ThreadInfo& target_thread, |
| 299 | [[maybe_unused]] const std::map<pid_t, ThreadInfo>& threads, uint64_t fault_addr) { |
| 300 | unwindstack::Maps* maps = unwinder->GetMaps(); |
| 301 | auto map_info = maps->Find(untag_address(fault_addr)); |
| 302 | pid_t tid = get_containing_thread(map_info.get(), target_thread.tid); |
| 303 | if (!tid) { |
| 304 | return std::nullopt; |
| 305 | } |
| 306 | auto it = threads.find(tid); |
| 307 | if (it != threads.end()) { |
| 308 | StackHistoryBuffer* shb = tombstone->mutable_stack_history_buffer(); |
| 309 | shb->set_tid(tid); |
| 310 | dump_stack_history(unwinder, it->second.tls, *shb); |
| 311 | } else { |
| 312 | async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, |
| 313 | "dump_probable_cause: unknown target thread %d", tid); |
| 314 | } |
| 315 | return StringPrintf("stack tag-mismatch on thread %u", tid); |
| 316 | } |
| 317 | |
| 318 | #endif |
| 319 | |
| Christopher Ferris | 3b7b7ba | 2022-03-15 16:56:09 -0700 | [diff] [blame] | 320 | static void dump_probable_cause(Tombstone* tombstone, unwindstack::AndroidUnwinder* unwinder, |
| Florian Mayer | e3e7bc7 | 2024-04-26 09:38:50 -0700 | [diff] [blame] | 321 | const ProcessInfo& process_info, const ThreadInfo& target_thread, |
| 322 | [[maybe_unused]] const std::map<pid_t, ThreadInfo>& threads) { |
| Christopher Ferris | 7aad256 | 2021-09-24 00:06:38 -0700 | [diff] [blame] | 323 | #if defined(USE_SCUDO) |
| Peter Collingbourne | 7827991 | 2022-06-30 18:39:54 -0700 | [diff] [blame] | 324 | ScudoCrashData scudo_crash_data(unwinder->GetProcessMemory().get(), process_info); |
| 325 | if (scudo_crash_data.CrashIsMine()) { |
| Peter Collingbourne | 1a1f7d7 | 2021-03-08 16:53:54 -0800 | [diff] [blame] | 326 | scudo_crash_data.AddCauseProtos(tombstone, unwinder); |
| 327 | return; |
| 328 | } |
| Christopher Ferris | 7aad256 | 2021-09-24 00:06:38 -0700 | [diff] [blame] | 329 | #endif |
| Peter Collingbourne | 1a1f7d7 | 2021-03-08 16:53:54 -0800 | [diff] [blame] | 330 | |
| 331 | GwpAsanCrashData gwp_asan_crash_data(unwinder->GetProcessMemory().get(), process_info, |
| Florian Mayer | 2d45331 | 2024-04-22 23:43:59 +0000 | [diff] [blame] | 332 | target_thread); |
| Peter Collingbourne | 1a1f7d7 | 2021-03-08 16:53:54 -0800 | [diff] [blame] | 333 | if (gwp_asan_crash_data.CrashIsMine()) { |
| 334 | gwp_asan_crash_data.AddCauseProtos(tombstone, unwinder); |
| 335 | return; |
| 336 | } |
| 337 | |
| Florian Mayer | 2d45331 | 2024-04-22 23:43:59 +0000 | [diff] [blame] | 338 | const siginfo *si = target_thread.siginfo; |
| Peter Collingbourne | 1a1f7d7 | 2021-03-08 16:53:54 -0800 | [diff] [blame] | 339 | auto fault_addr = reinterpret_cast<uint64_t>(si->si_addr); |
| 340 | unwindstack::Maps* maps = unwinder->GetMaps(); |
| 341 | |
| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 342 | std::optional<std::string> cause; |
| 343 | if (si->si_signo == SIGSEGV && si->si_code == SEGV_MAPERR) { |
| Peter Collingbourne | 1a1f7d7 | 2021-03-08 16:53:54 -0800 | [diff] [blame] | 344 | if (fault_addr < 4096) { |
| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 345 | cause = "null pointer dereference"; |
| Peter Collingbourne | 1a1f7d7 | 2021-03-08 16:53:54 -0800 | [diff] [blame] | 346 | } else if (fault_addr == 0xffff0ffc) { |
| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 347 | cause = "call to kuser_helper_version"; |
| Peter Collingbourne | 1a1f7d7 | 2021-03-08 16:53:54 -0800 | [diff] [blame] | 348 | } else if (fault_addr == 0xffff0fe0) { |
| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 349 | cause = "call to kuser_get_tls"; |
| Peter Collingbourne | 1a1f7d7 | 2021-03-08 16:53:54 -0800 | [diff] [blame] | 350 | } else if (fault_addr == 0xffff0fc0) { |
| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 351 | cause = "call to kuser_cmpxchg"; |
| Peter Collingbourne | 1a1f7d7 | 2021-03-08 16:53:54 -0800 | [diff] [blame] | 352 | } else if (fault_addr == 0xffff0fa0) { |
| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 353 | cause = "call to kuser_memory_barrier"; |
| Peter Collingbourne | 1a1f7d7 | 2021-03-08 16:53:54 -0800 | [diff] [blame] | 354 | } else if (fault_addr == 0xffff0f60) { |
| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 355 | cause = "call to kuser_cmpxchg64"; |
| 356 | } else { |
| Florian Mayer | 2d45331 | 2024-04-22 23:43:59 +0000 | [diff] [blame] | 357 | cause = get_stack_overflow_cause(fault_addr, target_thread.registers->sp(), maps); |
| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 358 | } |
| 359 | } else if (si->si_signo == SIGSEGV && si->si_code == SEGV_ACCERR) { |
| Christopher Ferris | c677606 | 2021-01-15 15:59:28 -0800 | [diff] [blame] | 360 | auto map_info = maps->Find(fault_addr); |
| David Srbecky | d8ab48b | 2021-05-13 00:03:26 +0100 | [diff] [blame] | 361 | if (map_info != nullptr && map_info->flags() == PROT_EXEC) { |
| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 362 | cause = "execute-only (no-read) memory access error; likely due to data in .text."; |
| Elliott Hughes | b17d7be | 2024-09-11 16:23:18 +0000 | [diff] [blame] | 363 | } else if (fault_addr == target_thread.registers->pc() && |
| 364 | map_info != nullptr && (map_info->flags() & PROT_EXEC) == 0) { |
| 365 | cause = "trying to execute non-executable memory."; |
| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 366 | } else { |
| Florian Mayer | 2d45331 | 2024-04-22 23:43:59 +0000 | [diff] [blame] | 367 | cause = get_stack_overflow_cause(fault_addr, target_thread.registers->sp(), maps); |
| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 368 | } |
| Florian Mayer | e3e7bc7 | 2024-04-26 09:38:50 -0700 | [diff] [blame] | 369 | } |
| 370 | #if defined(__aarch64__) && defined(SEGV_MTESERR) |
| 371 | else if (si->si_signo == SIGSEGV && si->si_code == SEGV_MTESERR) { |
| 372 | // If this was a heap MTE crash, it would have been handled by scudo. Checking whether it |
| 373 | // is a stack one. |
| 374 | cause = maybe_stack_mte_cause(tombstone, unwinder, target_thread, threads, fault_addr); |
| 375 | } |
| 376 | #endif |
| 377 | else if (si->si_signo == SIGSYS && si->si_code == SYS_SECCOMP) { |
| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 378 | cause = StringPrintf("seccomp prevented call to disallowed %s system call %d", ABI_STRING, |
| 379 | si->si_syscall); |
| 380 | } |
| 381 | |
| 382 | if (cause) { |
| Peter Collingbourne | 1a1f7d7 | 2021-03-08 16:53:54 -0800 | [diff] [blame] | 383 | Cause *cause_proto = tombstone->add_causes(); |
| 384 | cause_proto->set_human_readable(*cause); |
| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 385 | } |
| 386 | } |
| 387 | |
| Florian Mayer | 5fa6663 | 2024-02-07 16:42:23 -0800 | [diff] [blame] | 388 | static void dump_crash_details(Tombstone* tombstone, |
| 389 | std::shared_ptr<unwindstack::Memory>& process_memory, |
| 390 | const ProcessInfo& process_info) { |
| 391 | uintptr_t address = process_info.crash_detail_page; |
| 392 | while (address) { |
| 393 | struct crash_detail_page_t page; |
| 394 | if (!process_memory->ReadFully(address, &page, sizeof(page))) { |
| 395 | async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, "failed to read crash detail page: %m"); |
| 396 | break; |
| 397 | } |
| 398 | if (page.used > kNumCrashDetails) { |
| 399 | async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, "crash detail: page corrupted"); |
| 400 | break; |
| 401 | } |
| 402 | for (size_t i = 0; i < page.used; ++i) { |
| 403 | const crash_detail_t& crash_detail = page.crash_details[i]; |
| 404 | if (!crash_detail.data) { |
| 405 | continue; |
| 406 | } |
| 407 | std::string name(crash_detail.name_size, '\0'); |
| 408 | if (!process_memory->ReadFully(reinterpret_cast<uintptr_t>(crash_detail.name), name.data(), |
| 409 | crash_detail.name_size)) { |
| 410 | async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, "crash detail: failed to read name: %m"); |
| 411 | continue; |
| 412 | } |
| 413 | std::string data(crash_detail.data_size, '\0'); |
| 414 | if (!process_memory->ReadFully(reinterpret_cast<uintptr_t>(crash_detail.data), data.data(), |
| 415 | crash_detail.data_size)) { |
| 416 | async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, |
| 417 | "crash detail: failed to read data for %s: %m", name.c_str()); |
| 418 | continue; |
| 419 | } |
| 420 | auto* proto_detail = tombstone->add_crash_details(); |
| 421 | proto_detail->set_name(name); |
| 422 | proto_detail->set_data(data); |
| 423 | } |
| 424 | address = reinterpret_cast<uintptr_t>(page.prev); |
| 425 | } |
| 426 | } |
| 427 | |
| Christopher Ferris | 3b7b7ba | 2022-03-15 16:56:09 -0700 | [diff] [blame] | 428 | static void dump_abort_message(Tombstone* tombstone, |
| 429 | std::shared_ptr<unwindstack::Memory>& process_memory, |
| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 430 | const ProcessInfo& process_info) { |
| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 431 | uintptr_t address = process_info.abort_msg_address; |
| 432 | if (address == 0) { |
| 433 | return; |
| 434 | } |
| 435 | |
| 436 | size_t length; |
| 437 | if (!process_memory->ReadFully(address, &length, sizeof(length))) { |
| Josh Gao | 618cea3 | 2021-01-26 17:45:43 -0800 | [diff] [blame] | 438 | async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, "failed to read abort message header: %s", |
| 439 | strerror(errno)); |
| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 440 | return; |
| 441 | } |
| 442 | |
| 443 | // The length field includes the length of the length field itself. |
| 444 | if (length < sizeof(size_t)) { |
| Josh Gao | 618cea3 | 2021-01-26 17:45:43 -0800 | [diff] [blame] | 445 | async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, |
| 446 | "abort message header malformed: claimed length = %zu", length); |
| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 447 | return; |
| 448 | } |
| 449 | |
| 450 | length -= sizeof(size_t); |
| 451 | |
| 452 | // The abort message should be null terminated already, but reserve a spot for NUL just in case. |
| 453 | std::string msg; |
| 454 | msg.resize(length); |
| 455 | |
| 456 | if (!process_memory->ReadFully(address + sizeof(length), &msg[0], length)) { |
| Josh Gao | 618cea3 | 2021-01-26 17:45:43 -0800 | [diff] [blame] | 457 | async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, "failed to read abort message header: %s", |
| 458 | strerror(errno)); |
| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 459 | return; |
| 460 | } |
| 461 | |
| Christopher Ferris | e889145 | 2021-08-17 17:34:53 -0700 | [diff] [blame] | 462 | // Remove any trailing newlines. |
| 463 | size_t index = msg.size(); |
| 464 | while (index > 0 && (msg[index - 1] == '\0' || msg[index - 1] == '\n')) { |
| 465 | --index; |
| 466 | } |
| 467 | msg.resize(index); |
| 468 | |
| Christopher Ferris | 6a5db68 | 2024-11-15 01:06:55 +0000 | [diff] [blame] | 469 | // Make sure only UTF8 characters are present since abort_message is a string. |
| Christopher Ferris | 69210a6 | 2024-12-06 09:35:21 -0800 | [diff] [blame^] | 470 | tombstone->set_abort_message(oct_encode_non_ascii_printable(msg)); |
| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | static void dump_open_fds(Tombstone* tombstone, const OpenFilesList* open_files) { |
| 474 | if (open_files) { |
| 475 | for (auto& [fd, entry] : *open_files) { |
| 476 | FD f; |
| 477 | |
| 478 | f.set_fd(fd); |
| 479 | |
| 480 | const std::optional<std::string>& path = entry.path; |
| 481 | if (path) { |
| 482 | f.set_path(*path); |
| 483 | } |
| 484 | |
| 485 | const std::optional<uint64_t>& fdsan_owner = entry.fdsan_owner; |
| 486 | if (fdsan_owner) { |
| 487 | const char* type = android_fdsan_get_tag_type(*fdsan_owner); |
| 488 | uint64_t value = android_fdsan_get_tag_value(*fdsan_owner); |
| 489 | f.set_owner(type); |
| 490 | f.set_tag(value); |
| 491 | } |
| 492 | |
| 493 | *tombstone->add_open_fds() = f; |
| 494 | } |
| 495 | } |
| 496 | } |
| 497 | |
| Christopher Ferris | 22ad09b | 2021-11-10 17:25:11 -0800 | [diff] [blame] | 498 | void fill_in_backtrace_frame(BacktraceFrame* f, const unwindstack::FrameData& frame) { |
| Peter Collingbourne | 1a1f7d7 | 2021-03-08 16:53:54 -0800 | [diff] [blame] | 499 | f->set_rel_pc(frame.rel_pc); |
| 500 | f->set_pc(frame.pc); |
| 501 | f->set_sp(frame.sp); |
| 502 | |
| 503 | if (!frame.function_name.empty()) { |
| 504 | // TODO: Should this happen here, or on the display side? |
| 505 | char* demangled_name = __cxa_demangle(frame.function_name.c_str(), nullptr, nullptr, nullptr); |
| 506 | if (demangled_name) { |
| 507 | f->set_function_name(demangled_name); |
| 508 | free(demangled_name); |
| 509 | } else { |
| 510 | f->set_function_name(frame.function_name); |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | f->set_function_offset(frame.function_offset); |
| 515 | |
| Christopher Ferris | 22ad09b | 2021-11-10 17:25:11 -0800 | [diff] [blame] | 516 | if (frame.map_info == nullptr) { |
| Peter Collingbourne | 1a1f7d7 | 2021-03-08 16:53:54 -0800 | [diff] [blame] | 517 | // No valid map associated with this frame. |
| 518 | f->set_file_name("<unknown>"); |
| Christopher Ferris | 22ad09b | 2021-11-10 17:25:11 -0800 | [diff] [blame] | 519 | return; |
| 520 | } |
| 521 | |
| 522 | if (!frame.map_info->name().empty()) { |
| 523 | f->set_file_name(frame.map_info->GetFullName()); |
| Peter Collingbourne | 1a1f7d7 | 2021-03-08 16:53:54 -0800 | [diff] [blame] | 524 | } else { |
| Christopher Ferris | 22ad09b | 2021-11-10 17:25:11 -0800 | [diff] [blame] | 525 | f->set_file_name(StringPrintf("<anonymous:%" PRIx64 ">", frame.map_info->start())); |
| Peter Collingbourne | 1a1f7d7 | 2021-03-08 16:53:54 -0800 | [diff] [blame] | 526 | } |
| Christopher Ferris | 22ad09b | 2021-11-10 17:25:11 -0800 | [diff] [blame] | 527 | f->set_file_map_offset(frame.map_info->elf_start_offset()); |
| Peter Collingbourne | 1a1f7d7 | 2021-03-08 16:53:54 -0800 | [diff] [blame] | 528 | |
| Christopher Ferris | 22ad09b | 2021-11-10 17:25:11 -0800 | [diff] [blame] | 529 | f->set_build_id(frame.map_info->GetPrintableBuildID()); |
| Peter Collingbourne | 1a1f7d7 | 2021-03-08 16:53:54 -0800 | [diff] [blame] | 530 | } |
| 531 | |
| Christopher Ferris | 3b7b7ba | 2022-03-15 16:56:09 -0700 | [diff] [blame] | 532 | static void dump_registers(unwindstack::AndroidUnwinder* unwinder, |
| Christopher Ferris | b999b82 | 2022-02-09 17:57:21 -0800 | [diff] [blame] | 533 | const std::unique_ptr<unwindstack::Regs>& regs, Thread& thread, |
| 534 | bool memory_dump) { |
| 535 | if (regs == nullptr) { |
| 536 | return; |
| 537 | } |
| 538 | |
| 539 | unwindstack::Maps* maps = unwinder->GetMaps(); |
| 540 | unwindstack::Memory* memory = unwinder->GetProcessMemory().get(); |
| 541 | |
| 542 | regs->IterateRegisters([&thread, memory_dump, maps, memory](const char* name, uint64_t value) { |
| 543 | Register r; |
| 544 | r.set_name(name); |
| 545 | r.set_u64(value); |
| 546 | *thread.add_registers() = r; |
| 547 | |
| 548 | if (memory_dump) { |
| 549 | MemoryDump dump; |
| 550 | |
| 551 | dump.set_register_name(name); |
| 552 | std::shared_ptr<unwindstack::MapInfo> map_info = maps->Find(untag_address(value)); |
| 553 | if (map_info) { |
| 554 | dump.set_mapping_name(map_info->name()); |
| 555 | } |
| 556 | |
| 557 | constexpr size_t kNumBytesAroundRegister = 256; |
| 558 | constexpr size_t kNumTagsAroundRegister = kNumBytesAroundRegister / kTagGranuleSize; |
| 559 | char buf[kNumBytesAroundRegister]; |
| 560 | uint8_t tags[kNumTagsAroundRegister]; |
| 561 | ssize_t bytes = dump_memory(buf, sizeof(buf), tags, sizeof(tags), &value, memory); |
| 562 | if (bytes == -1) { |
| 563 | return; |
| 564 | } |
| 565 | dump.set_begin_address(value); |
| 566 | dump.set_memory(buf, bytes); |
| 567 | |
| 568 | bool has_tags = false; |
| 569 | #if defined(__aarch64__) |
| 570 | for (size_t i = 0; i < kNumTagsAroundRegister; ++i) { |
| 571 | if (tags[i] != 0) { |
| 572 | has_tags = true; |
| 573 | } |
| 574 | } |
| 575 | #endif // defined(__aarch64__) |
| 576 | |
| 577 | if (has_tags) { |
| 578 | dump.mutable_arm_mte_metadata()->set_memory_tags(tags, kNumTagsAroundRegister); |
| 579 | } |
| 580 | |
| 581 | *thread.add_memory_dump() = std::move(dump); |
| 582 | } |
| 583 | }); |
| 584 | } |
| 585 | |
| Christopher Ferris | 3b7b7ba | 2022-03-15 16:56:09 -0700 | [diff] [blame] | 586 | static void dump_thread_backtrace(std::vector<unwindstack::FrameData>& frames, Thread& thread) { |
| Christopher Ferris | c95047d | 2022-03-14 15:02:11 -0700 | [diff] [blame] | 587 | std::set<std::string> unreadable_elf_files; |
| Christopher Ferris | 3b7b7ba | 2022-03-15 16:56:09 -0700 | [diff] [blame] | 588 | for (const auto& frame : frames) { |
| Christopher Ferris | c95047d | 2022-03-14 15:02:11 -0700 | [diff] [blame] | 589 | BacktraceFrame* f = thread.add_current_backtrace(); |
| 590 | fill_in_backtrace_frame(f, frame); |
| 591 | if (frame.map_info != nullptr && frame.map_info->ElfFileNotReadable()) { |
| 592 | unreadable_elf_files.emplace(frame.map_info->name()); |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | if (!unreadable_elf_files.empty()) { |
| 597 | auto unreadable_elf_files_proto = thread.mutable_unreadable_elf_files(); |
| Christopher Ferris | b999b82 | 2022-02-09 17:57:21 -0800 | [diff] [blame] | 598 | auto backtrace_note = thread.mutable_backtrace_note(); |
| 599 | *backtrace_note->Add() = |
| 600 | "Function names and BuildId information is missing for some frames due"; |
| 601 | *backtrace_note->Add() = "to unreadable libraries. For unwinds of apps, only shared libraries"; |
| 602 | *backtrace_note->Add() = "found under the lib/ directory are readable."; |
| 603 | *backtrace_note->Add() = "On this device, run setenforce 0 to make the libraries readable."; |
| Christopher Ferris | c95047d | 2022-03-14 15:02:11 -0700 | [diff] [blame] | 604 | *backtrace_note->Add() = "Unreadable libraries:"; |
| 605 | for (auto& name : unreadable_elf_files) { |
| 606 | *backtrace_note->Add() = " " + name; |
| 607 | *unreadable_elf_files_proto->Add() = name; |
| 608 | } |
| Christopher Ferris | b999b82 | 2022-02-09 17:57:21 -0800 | [diff] [blame] | 609 | } |
| 610 | } |
| 611 | |
| Christopher Ferris | 3b7b7ba | 2022-03-15 16:56:09 -0700 | [diff] [blame] | 612 | static void dump_thread(Tombstone* tombstone, unwindstack::AndroidUnwinder* unwinder, |
| Sijie Chen | c802793 | 2024-05-10 18:40:48 +0000 | [diff] [blame] | 613 | const ThreadInfo& thread_info, bool memory_dump = false, |
| 614 | unwindstack::AndroidUnwinder* guest_unwinder = nullptr) { |
| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 615 | Thread thread; |
| 616 | |
| 617 | thread.set_id(thread_info.tid); |
| 618 | thread.set_name(thread_info.thread_name); |
| Peter Collingbourne | 1a1f7d7 | 2021-03-08 16:53:54 -0800 | [diff] [blame] | 619 | thread.set_tagged_addr_ctrl(thread_info.tagged_addr_ctrl); |
| Elliott Hughes | d13ea52 | 2022-01-13 09:20:26 -0800 | [diff] [blame] | 620 | thread.set_pac_enabled_keys(thread_info.pac_enabled_keys); |
| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 621 | |
| Christopher Ferris | 3b7b7ba | 2022-03-15 16:56:09 -0700 | [diff] [blame] | 622 | unwindstack::AndroidUnwinderData data; |
| 623 | // Indicate we want a copy of the initial registers. |
| 624 | data.saved_initial_regs = std::make_optional<std::unique_ptr<unwindstack::Regs>>(); |
| 625 | bool unwind_ret; |
| 626 | if (thread_info.registers != nullptr) { |
| 627 | unwind_ret = unwinder->Unwind(thread_info.registers.get(), data); |
| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 628 | } else { |
| Christopher Ferris | 3b7b7ba | 2022-03-15 16:56:09 -0700 | [diff] [blame] | 629 | unwind_ret = unwinder->Unwind(thread_info.tid, data); |
| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 630 | } |
| Christopher Ferris | 3b7b7ba | 2022-03-15 16:56:09 -0700 | [diff] [blame] | 631 | if (!unwind_ret) { |
| 632 | async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, "Unwind failed for tid %d: Error %s", |
| 633 | thread_info.tid, data.GetErrorString().c_str()); |
| 634 | } else { |
| 635 | dump_thread_backtrace(data.frames, thread); |
| 636 | } |
| 637 | dump_registers(unwinder, *data.saved_initial_regs, thread, memory_dump); |
| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 638 | |
| 639 | auto& threads = *tombstone->mutable_threads(); |
| 640 | threads[thread_info.tid] = thread; |
| Sijie Chen | c802793 | 2024-05-10 18:40:48 +0000 | [diff] [blame] | 641 | |
| 642 | if (guest_unwinder) { |
| 643 | if (!thread_info.guest_registers) { |
| 644 | async_safe_format_log(ANDROID_LOG_INFO, LOG_TAG, |
| 645 | "No guest state registers information for tid %d", thread_info.tid); |
| 646 | return; |
| 647 | } |
| 648 | Thread guest_thread; |
| 649 | unwindstack::AndroidUnwinderData guest_data; |
| 650 | guest_data.saved_initial_regs = std::make_optional<std::unique_ptr<unwindstack::Regs>>(); |
| 651 | if (guest_unwinder->Unwind(thread_info.guest_registers.get(), guest_data)) { |
| 652 | dump_thread_backtrace(guest_data.frames, guest_thread); |
| 653 | } else { |
| 654 | async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, |
| 655 | "Unwind guest state registers failed for tid %d: Error %s", |
| 656 | thread_info.tid, guest_data.GetErrorString().c_str()); |
| 657 | } |
| 658 | dump_registers(guest_unwinder, *guest_data.saved_initial_regs, guest_thread, memory_dump); |
| 659 | auto& guest_threads = *tombstone->mutable_guest_threads(); |
| 660 | guest_threads[thread_info.tid] = guest_thread; |
| 661 | } |
| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 662 | } |
| 663 | |
| Christopher Ferris | 3b7b7ba | 2022-03-15 16:56:09 -0700 | [diff] [blame] | 664 | static void dump_mappings(Tombstone* tombstone, unwindstack::Maps* maps, |
| 665 | std::shared_ptr<unwindstack::Memory>& process_memory) { |
| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 666 | for (const auto& map_info : *maps) { |
| 667 | auto* map = tombstone->add_memory_mappings(); |
| David Srbecky | d8ab48b | 2021-05-13 00:03:26 +0100 | [diff] [blame] | 668 | map->set_begin_address(map_info->start()); |
| 669 | map->set_end_address(map_info->end()); |
| 670 | map->set_offset(map_info->offset()); |
| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 671 | |
| David Srbecky | d8ab48b | 2021-05-13 00:03:26 +0100 | [diff] [blame] | 672 | if (map_info->flags() & PROT_READ) { |
| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 673 | map->set_read(true); |
| 674 | } |
| David Srbecky | d8ab48b | 2021-05-13 00:03:26 +0100 | [diff] [blame] | 675 | if (map_info->flags() & PROT_WRITE) { |
| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 676 | map->set_write(true); |
| 677 | } |
| David Srbecky | d8ab48b | 2021-05-13 00:03:26 +0100 | [diff] [blame] | 678 | if (map_info->flags() & PROT_EXEC) { |
| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 679 | map->set_execute(true); |
| 680 | } |
| 681 | |
| David Srbecky | d8ab48b | 2021-05-13 00:03:26 +0100 | [diff] [blame] | 682 | map->set_mapping_name(map_info->name()); |
| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 683 | |
| 684 | std::string build_id = map_info->GetPrintableBuildID(); |
| 685 | if (!build_id.empty()) { |
| 686 | map->set_build_id(build_id); |
| 687 | } |
| 688 | |
| 689 | map->set_load_bias(map_info->GetLoadBias(process_memory)); |
| 690 | } |
| 691 | } |
| 692 | |
| Christopher Ferris | 3a0833c | 2023-07-28 13:07:53 -0700 | [diff] [blame] | 693 | // This creates a fake log message that indicates an error occurred when |
| 694 | // reading the log. |
| 695 | static void add_error_log_msg(Tombstone* tombstone, const std::string&& error_msg) { |
| 696 | LogBuffer buffer; |
| 697 | buffer.set_name("ERROR"); |
| 698 | |
| 699 | LogMessage* log_msg = buffer.add_logs(); |
| 700 | log_msg->set_timestamp("00-00 00:00:00.000"); |
| 701 | log_msg->set_pid(0); |
| 702 | log_msg->set_tid(0); |
| 703 | log_msg->set_priority(ANDROID_LOG_ERROR); |
| 704 | log_msg->set_tag(""); |
| 705 | log_msg->set_message(error_msg); |
| 706 | |
| 707 | *tombstone->add_log_buffers() = std::move(buffer); |
| 708 | |
| 709 | async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, "%s", error_msg.c_str()); |
| 710 | } |
| 711 | |
| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 712 | static void dump_log_file(Tombstone* tombstone, const char* logger, pid_t pid) { |
| Christopher Ferris | 98d6242 | 2023-05-24 20:01:10 +0000 | [diff] [blame] | 713 | logger_list* logger_list = android_logger_list_open(android_name_to_log_id(logger), |
| 714 | ANDROID_LOG_NONBLOCK, kMaxLogMessages, pid); |
| Christopher Ferris | 3a0833c | 2023-07-28 13:07:53 -0700 | [diff] [blame] | 715 | if (logger_list == nullptr) { |
| 716 | add_error_log_msg(tombstone, android::base::StringPrintf("Cannot open log file %s", logger)); |
| 717 | return; |
| 718 | } |
| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 719 | |
| 720 | LogBuffer buffer; |
| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 721 | while (true) { |
| 722 | log_msg log_entry; |
| 723 | ssize_t actual = android_logger_list_read(logger_list, &log_entry); |
| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 724 | if (actual < 0) { |
| 725 | if (actual == -EINTR) { |
| 726 | // interrupted by signal, retry |
| 727 | continue; |
| 728 | } |
| Christopher Ferris | 3a0833c | 2023-07-28 13:07:53 -0700 | [diff] [blame] | 729 | // Don't consider EAGAIN an error since this is a non-blocking call. |
| 730 | if (actual != -EAGAIN) { |
| 731 | add_error_log_msg(tombstone, android::base::StringPrintf("reading log %s failed (%s)", |
| 732 | logger, strerror(-actual))); |
| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 733 | } |
| Christopher Ferris | 3a0833c | 2023-07-28 13:07:53 -0700 | [diff] [blame] | 734 | break; |
| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 735 | } else if (actual == 0) { |
| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 736 | break; |
| 737 | } |
| 738 | |
| 739 | char timestamp_secs[32]; |
| 740 | time_t sec = static_cast<time_t>(log_entry.entry.sec); |
| 741 | tm tm; |
| 742 | localtime_r(&sec, &tm); |
| 743 | strftime(timestamp_secs, sizeof(timestamp_secs), "%m-%d %H:%M:%S", &tm); |
| 744 | std::string timestamp = |
| 745 | StringPrintf("%s.%03d", timestamp_secs, log_entry.entry.nsec / 1'000'000); |
| 746 | |
| 747 | // Msg format is: <priority:1><tag:N>\0<message:N>\0 |
| 748 | char* msg = log_entry.msg(); |
| 749 | if (msg == nullptr) { |
| 750 | continue; |
| 751 | } |
| 752 | |
| 753 | unsigned char prio = msg[0]; |
| 754 | char* tag = msg + 1; |
| 755 | msg = tag + strlen(tag) + 1; |
| 756 | |
| 757 | // consume any trailing newlines |
| 758 | char* nl = msg + strlen(msg) - 1; |
| 759 | while (nl >= msg && *nl == '\n') { |
| 760 | *nl-- = '\0'; |
| 761 | } |
| 762 | |
| 763 | // Look for line breaks ('\n') and display each text line |
| 764 | // on a separate line, prefixed with the header, like logcat does. |
| 765 | do { |
| 766 | nl = strchr(msg, '\n'); |
| 767 | if (nl != nullptr) { |
| 768 | *nl = '\0'; |
| 769 | ++nl; |
| 770 | } |
| 771 | |
| 772 | LogMessage* log_msg = buffer.add_logs(); |
| 773 | log_msg->set_timestamp(timestamp); |
| 774 | log_msg->set_pid(log_entry.entry.pid); |
| 775 | log_msg->set_tid(log_entry.entry.tid); |
| 776 | log_msg->set_priority(prio); |
| 777 | log_msg->set_tag(tag); |
| Christopher Ferris | 6a5db68 | 2024-11-15 01:06:55 +0000 | [diff] [blame] | 778 | // Make sure only UTF8 characters are present since message is a string. |
| Christopher Ferris | 69210a6 | 2024-12-06 09:35:21 -0800 | [diff] [blame^] | 779 | log_msg->set_message(oct_encode_non_ascii_printable(msg)); |
| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 780 | } while ((msg = nl)); |
| 781 | } |
| 782 | android_logger_list_free(logger_list); |
| 783 | |
| 784 | if (!buffer.logs().empty()) { |
| 785 | buffer.set_name(logger); |
| 786 | *tombstone->add_log_buffers() = std::move(buffer); |
| 787 | } |
| 788 | } |
| 789 | |
| 790 | static void dump_logcat(Tombstone* tombstone, pid_t pid) { |
| 791 | dump_log_file(tombstone, "system", pid); |
| 792 | dump_log_file(tombstone, "main", pid); |
| 793 | } |
| 794 | |
| Mitch Phillips | 5ddcea2 | 2021-04-19 09:59:17 -0700 | [diff] [blame] | 795 | static void dump_tags_around_fault_addr(Signal* signal, const Tombstone& tombstone, |
| Christopher Ferris | 3b7b7ba | 2022-03-15 16:56:09 -0700 | [diff] [blame] | 796 | std::shared_ptr<unwindstack::Memory>& process_memory, |
| 797 | uintptr_t fault_addr) { |
| Mitch Phillips | 5ddcea2 | 2021-04-19 09:59:17 -0700 | [diff] [blame] | 798 | if (tombstone.arch() != Architecture::ARM64) return; |
| 799 | |
| 800 | fault_addr = untag_address(fault_addr); |
| 801 | constexpr size_t kNumGranules = kNumTagRows * kNumTagColumns; |
| 802 | constexpr size_t kBytesToRead = kNumGranules * kTagGranuleSize; |
| 803 | |
| 804 | // If the low part of the tag dump would underflow to the high address space, it's probably not |
| 805 | // a valid address for us to dump tags from. |
| 806 | if (fault_addr < kBytesToRead / 2) return; |
| 807 | |
| Mitch Phillips | 5ddcea2 | 2021-04-19 09:59:17 -0700 | [diff] [blame] | 808 | constexpr uintptr_t kRowStartMask = ~(kNumTagColumns * kTagGranuleSize - 1); |
| 809 | size_t start_address = (fault_addr & kRowStartMask) - kBytesToRead / 2; |
| 810 | MemoryDump tag_dump; |
| 811 | size_t granules_to_read = kNumGranules; |
| 812 | |
| 813 | // Attempt to read the first tag. If reading fails, this likely indicates the |
| 814 | // lowest touched page is inaccessible or not marked with PROT_MTE. |
| 815 | // Fast-forward over pages until one has tags, or we exhaust the search range. |
| Christopher Ferris | 3b7b7ba | 2022-03-15 16:56:09 -0700 | [diff] [blame] | 816 | while (process_memory->ReadTag(start_address) < 0) { |
| Mitch Phillips | 5ddcea2 | 2021-04-19 09:59:17 -0700 | [diff] [blame] | 817 | size_t page_size = sysconf(_SC_PAGE_SIZE); |
| 818 | size_t bytes_to_next_page = page_size - (start_address % page_size); |
| 819 | if (bytes_to_next_page >= granules_to_read * kTagGranuleSize) return; |
| 820 | start_address += bytes_to_next_page; |
| 821 | granules_to_read -= bytes_to_next_page / kTagGranuleSize; |
| 822 | } |
| 823 | tag_dump.set_begin_address(start_address); |
| 824 | |
| 825 | std::string* mte_tags = tag_dump.mutable_arm_mte_metadata()->mutable_memory_tags(); |
| 826 | |
| 827 | for (size_t i = 0; i < granules_to_read; ++i) { |
| Christopher Ferris | 3b7b7ba | 2022-03-15 16:56:09 -0700 | [diff] [blame] | 828 | long tag = process_memory->ReadTag(start_address + i * kTagGranuleSize); |
| Mitch Phillips | 5ddcea2 | 2021-04-19 09:59:17 -0700 | [diff] [blame] | 829 | if (tag < 0) break; |
| 830 | mte_tags->push_back(static_cast<uint8_t>(tag)); |
| 831 | } |
| 832 | |
| 833 | if (!mte_tags->empty()) { |
| 834 | *signal->mutable_fault_adjacent_metadata() = tag_dump; |
| 835 | } |
| 836 | } |
| 837 | |
| Christopher Ferris | 3b7b7ba | 2022-03-15 16:56:09 -0700 | [diff] [blame] | 838 | void engrave_tombstone_proto(Tombstone* tombstone, unwindstack::AndroidUnwinder* unwinder, |
| Florian Mayer | 2d45331 | 2024-04-22 23:43:59 +0000 | [diff] [blame] | 839 | const std::map<pid_t, ThreadInfo>& threads, pid_t target_tid, |
| Sijie Chen | c802793 | 2024-05-10 18:40:48 +0000 | [diff] [blame] | 840 | const ProcessInfo& process_info, const OpenFilesList* open_files, |
| 841 | const Architecture* guest_arch, |
| 842 | unwindstack::AndroidUnwinder* guest_unwinder) { |
| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 843 | Tombstone result; |
| 844 | |
| 845 | result.set_arch(get_arch()); |
| Sijie Chen | c802793 | 2024-05-10 18:40:48 +0000 | [diff] [blame] | 846 | if (guest_arch != nullptr) { |
| 847 | result.set_guest_arch(*guest_arch); |
| 848 | } else { |
| 849 | result.set_guest_arch(Architecture::NONE); |
| 850 | } |
| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 851 | result.set_build_fingerprint(android::base::GetProperty("ro.build.fingerprint", "unknown")); |
| 852 | result.set_revision(android::base::GetProperty("ro.revision", "unknown")); |
| 853 | result.set_timestamp(get_timestamp()); |
| 854 | |
| Florian Mayer | 2d45331 | 2024-04-22 23:43:59 +0000 | [diff] [blame] | 855 | const ThreadInfo& target_thread = threads.at(target_tid); |
| 856 | result.set_pid(target_thread.pid); |
| 857 | result.set_tid(target_thread.tid); |
| 858 | result.set_uid(target_thread.uid); |
| 859 | result.set_selinux_label(target_thread.selinux_label); |
| Christopher Ferris | 2b98c82 | 2021-09-28 15:50:04 -0700 | [diff] [blame] | 860 | // The main thread must have a valid siginfo. |
| Florian Mayer | 2d45331 | 2024-04-22 23:43:59 +0000 | [diff] [blame] | 861 | CHECK(target_thread.siginfo != nullptr); |
| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 862 | |
| Elliott Hughes | 32d3cdd | 2021-09-21 12:20:53 -0700 | [diff] [blame] | 863 | struct sysinfo si; |
| 864 | sysinfo(&si); |
| 865 | android::procinfo::ProcessInfo proc_info; |
| 866 | std::string error; |
| Florian Mayer | 2d45331 | 2024-04-22 23:43:59 +0000 | [diff] [blame] | 867 | if (android::procinfo::GetProcessInfo(target_thread.pid, &proc_info, &error)) { |
| Elliott Hughes | 32d3cdd | 2021-09-21 12:20:53 -0700 | [diff] [blame] | 868 | uint64_t starttime = proc_info.starttime / sysconf(_SC_CLK_TCK); |
| 869 | result.set_process_uptime(si.uptime - starttime); |
| 870 | } else { |
| 871 | async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, "failed to read process info: %s", |
| 872 | error.c_str()); |
| 873 | } |
| 874 | |
| Devin Moore | 4647b6b | 2024-05-03 00:02:24 +0000 | [diff] [blame] | 875 | result.set_page_size(getpagesize()); |
| 876 | result.set_has_been_16kb_mode(android::base::GetBoolProperty("ro.misctrl.16kb_before", false)); |
| 877 | |
| Josh Gao | 31348a7 | 2021-03-29 21:53:42 -0700 | [diff] [blame] | 878 | auto cmd_line = result.mutable_command_line(); |
| Florian Mayer | 2d45331 | 2024-04-22 23:43:59 +0000 | [diff] [blame] | 879 | for (const auto& arg : target_thread.command_line) { |
| Josh Gao | 31348a7 | 2021-03-29 21:53:42 -0700 | [diff] [blame] | 880 | *cmd_line->Add() = arg; |
| 881 | } |
| 882 | |
| Florian Mayer | 2d45331 | 2024-04-22 23:43:59 +0000 | [diff] [blame] | 883 | if (!target_thread.siginfo) { |
| Josh Gao | 618cea3 | 2021-01-26 17:45:43 -0800 | [diff] [blame] | 884 | async_safe_fatal("siginfo missing"); |
| 885 | } |
| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 886 | |
| 887 | Signal sig; |
| Florian Mayer | 2d45331 | 2024-04-22 23:43:59 +0000 | [diff] [blame] | 888 | sig.set_number(target_thread.signo); |
| 889 | sig.set_name(get_signame(target_thread.siginfo)); |
| 890 | sig.set_code(target_thread.siginfo->si_code); |
| 891 | sig.set_code_name(get_sigcode(target_thread.siginfo)); |
| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 892 | |
| Florian Mayer | 2d45331 | 2024-04-22 23:43:59 +0000 | [diff] [blame] | 893 | if (signal_has_sender(target_thread.siginfo, target_thread.pid)) { |
| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 894 | sig.set_has_sender(true); |
| Florian Mayer | 2d45331 | 2024-04-22 23:43:59 +0000 | [diff] [blame] | 895 | sig.set_sender_uid(target_thread.siginfo->si_uid); |
| 896 | sig.set_sender_pid(target_thread.siginfo->si_pid); |
| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 897 | } |
| 898 | |
| 899 | if (process_info.has_fault_address) { |
| 900 | sig.set_has_fault_address(true); |
| Mitch Phillips | 5ddcea2 | 2021-04-19 09:59:17 -0700 | [diff] [blame] | 901 | uintptr_t fault_addr = process_info.maybe_tagged_fault_address; |
| 902 | sig.set_fault_address(fault_addr); |
| Christopher Ferris | 3b7b7ba | 2022-03-15 16:56:09 -0700 | [diff] [blame] | 903 | dump_tags_around_fault_addr(&sig, result, unwinder->GetProcessMemory(), fault_addr); |
| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | *result.mutable_signal_info() = sig; |
| 907 | |
| Christopher Ferris | 3b7b7ba | 2022-03-15 16:56:09 -0700 | [diff] [blame] | 908 | dump_abort_message(&result, unwinder->GetProcessMemory(), process_info); |
| Florian Mayer | 5fa6663 | 2024-02-07 16:42:23 -0800 | [diff] [blame] | 909 | dump_crash_details(&result, unwinder->GetProcessMemory(), process_info); |
| Florian Mayer | 2d45331 | 2024-04-22 23:43:59 +0000 | [diff] [blame] | 910 | // Dump the target thread, but save the memory around the registers. |
| Sijie Chen | c802793 | 2024-05-10 18:40:48 +0000 | [diff] [blame] | 911 | dump_thread(&result, unwinder, target_thread, /* memory_dump */ true, guest_unwinder); |
| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 912 | |
| 913 | for (const auto& [tid, thread_info] : threads) { |
| Florian Mayer | 2d45331 | 2024-04-22 23:43:59 +0000 | [diff] [blame] | 914 | if (tid != target_tid) { |
| Sijie Chen | c802793 | 2024-05-10 18:40:48 +0000 | [diff] [blame] | 915 | dump_thread(&result, unwinder, thread_info, /* memory_dump */ false, guest_unwinder); |
| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 916 | } |
| 917 | } |
| 918 | |
| Florian Mayer | e3e7bc7 | 2024-04-26 09:38:50 -0700 | [diff] [blame] | 919 | dump_probable_cause(&result, unwinder, process_info, target_thread, threads); |
| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 920 | |
| Christopher Ferris | 3b7b7ba | 2022-03-15 16:56:09 -0700 | [diff] [blame] | 921 | dump_mappings(&result, unwinder->GetMaps(), unwinder->GetProcessMemory()); |
| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 922 | |
| 923 | // Only dump logs on debuggable devices. |
| 924 | if (android::base::GetBoolProperty("ro.debuggable", false)) { |
| Christopher Ferris | bda1064 | 2023-04-24 18:14:53 -0700 | [diff] [blame] | 925 | // Get the thread that corresponds to the main pid of the process. |
| Florian Mayer | 2d45331 | 2024-04-22 23:43:59 +0000 | [diff] [blame] | 926 | const ThreadInfo& thread = threads.at(target_thread.pid); |
| Christopher Ferris | bda1064 | 2023-04-24 18:14:53 -0700 | [diff] [blame] | 927 | |
| 928 | // Do not attempt to dump logs of the logd process because the gathering |
| 929 | // of logs can hang until a timeout occurs. |
| 930 | if (thread.thread_name != "logd") { |
| Florian Mayer | 2d45331 | 2024-04-22 23:43:59 +0000 | [diff] [blame] | 931 | dump_logcat(&result, target_thread.pid); |
| Christopher Ferris | bda1064 | 2023-04-24 18:14:53 -0700 | [diff] [blame] | 932 | } |
| Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 933 | } |
| 934 | |
| 935 | dump_open_fds(&result, open_files); |
| 936 | |
| 937 | *tombstone = std::move(result); |
| 938 | } |