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"
20 #include "libdebuggerd/gwp_asan.h"
21 #if defined(USE_SCUDO)
22 #include "libdebuggerd/scudo.h"
23 #endif
24
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <inttypes.h>
28 #include <signal.h>
29 #include <stddef.h>
30 #include <stdint.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <sys/mman.h>
34 #include <sys/sysinfo.h>
35 #include <time.h>
36
37 #include <map>
38 #include <memory>
39 #include <optional>
40 #include <set>
41 #include <string>
42 #include <utility>
43 #include <vector>
44
45 #include <async_safe/log.h>
46
47 #include <android-base/file.h>
48 #include <android-base/logging.h>
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>
55 #include <android/set_abort_message.h>
56 #include <bionic/crash_detail_internal.h>
57 #include <bionic/macros.h>
58 #include <bionic/mte.h>
59 #include <bionic/reserved_signals.h>
60 #include <bionic/tls_defines.h>
61 #include <log/log.h>
62 #include <log/log_read.h>
63 #include <log/logprint.h>
64 #include <private/android_filesystem_config.h>
65
66 #include <procinfo/process.h>
67 #include <unwindstack/AndroidUnwinder.h>
68 #include <unwindstack/Error.h>
69 #include <unwindstack/MapInfo.h>
70 #include <unwindstack/Maps.h>
71 #include <unwindstack/Regs.h>
72
73 #include "libdebuggerd/open_files_list.h"
74 #include "libdebuggerd/utility.h"
75 #include "libdebuggerd/utility_host.h"
76 #include "util.h"
77
78 #include "tombstone.pb.h"
79
80 using android::base::StringPrintf;
81
82 // The maximum number of messages to save in the protobuf per file.
83 static constexpr size_t kMaxLogMessages = 500;
84
85 // Use the demangler from libc++.
86 extern "C" char* __cxa_demangle(const char*, char*, size_t*, int* status);
87
get_arch()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;
97 #elif defined(__riscv) && (__riscv_xlen == 64)
98 return Architecture::RISCV64;
99 #else
100 #error Unknown architecture!
101 #endif
102 }
103
get_stack_overflow_cause(uint64_t fault_addr,uint64_t sp,unwindstack::Maps * maps)104 static std::optional<std::string> get_stack_overflow_cause(uint64_t fault_addr, uint64_t sp,
105 unwindstack::Maps* maps) {
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);
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.
128 std::shared_ptr<unwindstack::MapInfo> map_info = maps->Find(sp);
129 if (map_info == nullptr) {
130 return "stack pointer is in a non-existent map; likely due to stack overflow.";
131 } else if ((map_info->flags() & (PROT_READ | PROT_WRITE)) != (PROT_READ | PROT_WRITE)) {
132 return "stack pointer is not in a rw map; likely due to stack overflow.";
133 } else if ((sp - map_info->start()) <= kMaxDifferenceBytes) {
134 return "stack pointer is close to top of stack; likely stack overflow.";
135 }
136 }
137 return {};
138 }
139
set_human_readable_cause(Cause * cause,uint64_t fault_addr)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
212 #if defined(__aarch64__)
dump_stack_history(unwindstack::AndroidUnwinder * unwinder,uintptr_t target_tls,StackHistoryBuffer & shb_obj,bool nounwind)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
get_containing_thread(unwindstack::MapInfo * map_info,pid_t main_tid)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
maybe_stack_mte_cause(Tombstone * tombstone,unwindstack::AndroidUnwinder * unwinder,const ThreadInfo & target_thread,const std::map<pid_t,ThreadInfo> & threads,uint64_t fault_addr)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
dump_probable_cause(Tombstone * tombstone,unwindstack::AndroidUnwinder * unwinder,const ProcessInfo & process_info,const ThreadInfo & target_thread,const std::map<pid_t,ThreadInfo> & threads)320 static void dump_probable_cause(Tombstone* tombstone, unwindstack::AndroidUnwinder* unwinder,
321 const ProcessInfo& process_info, const ThreadInfo& target_thread,
322 [[maybe_unused]] const std::map<pid_t, ThreadInfo>& threads) {
323 #if defined(USE_SCUDO)
324 ScudoCrashData scudo_crash_data(unwinder->GetProcessMemory().get(), process_info);
325 if (scudo_crash_data.CrashIsMine()) {
326 scudo_crash_data.AddCauseProtos(tombstone, unwinder);
327 return;
328 }
329 #endif
330
331 GwpAsanCrashData gwp_asan_crash_data(unwinder->GetProcessMemory().get(), process_info,
332 target_thread);
333 if (gwp_asan_crash_data.CrashIsMine()) {
334 gwp_asan_crash_data.AddCauseProtos(tombstone, unwinder);
335 return;
336 }
337
338 const siginfo *si = target_thread.siginfo;
339 auto fault_addr = reinterpret_cast<uint64_t>(si->si_addr);
340 unwindstack::Maps* maps = unwinder->GetMaps();
341
342 std::optional<std::string> cause;
343 if (si->si_signo == SIGSEGV && si->si_code == SEGV_MAPERR) {
344 if (fault_addr < 4096) {
345 cause = "null pointer dereference";
346 } else if (fault_addr == 0xffff0ffc) {
347 cause = "call to kuser_helper_version";
348 } else if (fault_addr == 0xffff0fe0) {
349 cause = "call to kuser_get_tls";
350 } else if (fault_addr == 0xffff0fc0) {
351 cause = "call to kuser_cmpxchg";
352 } else if (fault_addr == 0xffff0fa0) {
353 cause = "call to kuser_memory_barrier";
354 } else if (fault_addr == 0xffff0f60) {
355 cause = "call to kuser_cmpxchg64";
356 } else {
357 cause = get_stack_overflow_cause(fault_addr, target_thread.registers->sp(), maps);
358 }
359 } else if (si->si_signo == SIGSEGV && si->si_code == SEGV_ACCERR) {
360 auto map_info = maps->Find(fault_addr);
361 if (map_info != nullptr && map_info->flags() == PROT_EXEC) {
362 cause = "execute-only (no-read) memory access error; likely due to data in .text.";
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.";
366 } else {
367 cause = get_stack_overflow_cause(fault_addr, target_thread.registers->sp(), maps);
368 }
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) {
378 cause = StringPrintf("seccomp prevented call to disallowed %s system call %d", ABI_STRING,
379 si->si_syscall);
380 }
381
382 if (cause) {
383 Cause *cause_proto = tombstone->add_causes();
384 cause_proto->set_human_readable(*cause);
385 }
386 }
387
dump_crash_details(Tombstone * tombstone,std::shared_ptr<unwindstack::Memory> & process_memory,const ProcessInfo & process_info)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
dump_abort_message(Tombstone * tombstone,std::shared_ptr<unwindstack::Memory> & process_memory,const ProcessInfo & process_info)428 static void dump_abort_message(Tombstone* tombstone,
429 std::shared_ptr<unwindstack::Memory>& process_memory,
430 const ProcessInfo& process_info) {
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))) {
438 async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, "failed to read abort message header: %s",
439 strerror(errno));
440 return;
441 }
442
443 // The length field includes the length of the length field itself.
444 if (length < sizeof(size_t)) {
445 async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG,
446 "abort message header malformed: claimed length = %zu", length);
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)) {
457 async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, "failed to read abort message header: %s",
458 strerror(errno));
459 return;
460 }
461
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
469 // Make sure only UTF8 characters are present since abort_message is a string.
470 tombstone->set_abort_message(oct_encode(msg));
471 }
472
dump_open_fds(Tombstone * tombstone,const OpenFilesList * open_files)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
fill_in_backtrace_frame(BacktraceFrame * f,const unwindstack::FrameData & frame)498 void fill_in_backtrace_frame(BacktraceFrame* f, const unwindstack::FrameData& frame) {
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
516 if (frame.map_info == nullptr) {
517 // No valid map associated with this frame.
518 f->set_file_name("<unknown>");
519 return;
520 }
521
522 if (!frame.map_info->name().empty()) {
523 f->set_file_name(frame.map_info->GetFullName());
524 } else {
525 f->set_file_name(StringPrintf("<anonymous:%" PRIx64 ">", frame.map_info->start()));
526 }
527 f->set_file_map_offset(frame.map_info->elf_start_offset());
528
529 f->set_build_id(frame.map_info->GetPrintableBuildID());
530 }
531
dump_registers(unwindstack::AndroidUnwinder * unwinder,const std::unique_ptr<unwindstack::Regs> & regs,Thread & thread,bool memory_dump)532 static void dump_registers(unwindstack::AndroidUnwinder* unwinder,
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
dump_thread_backtrace(std::vector<unwindstack::FrameData> & frames,Thread & thread)586 static void dump_thread_backtrace(std::vector<unwindstack::FrameData>& frames, Thread& thread) {
587 std::set<std::string> unreadable_elf_files;
588 for (const auto& frame : frames) {
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();
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.";
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 }
609 }
610 }
611
dump_thread(Tombstone * tombstone,unwindstack::AndroidUnwinder * unwinder,const ThreadInfo & thread_info,bool memory_dump=false,unwindstack::AndroidUnwinder * guest_unwinder=nullptr)612 static void dump_thread(Tombstone* tombstone, unwindstack::AndroidUnwinder* unwinder,
613 const ThreadInfo& thread_info, bool memory_dump = false,
614 unwindstack::AndroidUnwinder* guest_unwinder = nullptr) {
615 Thread thread;
616
617 thread.set_id(thread_info.tid);
618 thread.set_name(thread_info.thread_name);
619 thread.set_tagged_addr_ctrl(thread_info.tagged_addr_ctrl);
620 thread.set_pac_enabled_keys(thread_info.pac_enabled_keys);
621
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);
628 } else {
629 unwind_ret = unwinder->Unwind(thread_info.tid, data);
630 }
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);
638
639 auto& threads = *tombstone->mutable_threads();
640 threads[thread_info.tid] = thread;
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 }
662 }
663
dump_mappings(Tombstone * tombstone,unwindstack::Maps * maps,std::shared_ptr<unwindstack::Memory> & process_memory)664 static void dump_mappings(Tombstone* tombstone, unwindstack::Maps* maps,
665 std::shared_ptr<unwindstack::Memory>& process_memory) {
666 for (const auto& map_info : *maps) {
667 auto* map = tombstone->add_memory_mappings();
668 map->set_begin_address(map_info->start());
669 map->set_end_address(map_info->end());
670 map->set_offset(map_info->offset());
671
672 if (map_info->flags() & PROT_READ) {
673 map->set_read(true);
674 }
675 if (map_info->flags() & PROT_WRITE) {
676 map->set_write(true);
677 }
678 if (map_info->flags() & PROT_EXEC) {
679 map->set_execute(true);
680 }
681
682 map->set_mapping_name(map_info->name());
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
693 // This creates a fake log message that indicates an error occurred when
694 // reading the log.
add_error_log_msg(Tombstone * tombstone,const std::string && error_msg)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
dump_log_file(Tombstone * tombstone,const char * logger,pid_t pid)712 static void dump_log_file(Tombstone* tombstone, const char* logger, pid_t pid) {
713 logger_list* logger_list = android_logger_list_open(android_name_to_log_id(logger),
714 ANDROID_LOG_NONBLOCK, kMaxLogMessages, pid);
715 if (logger_list == nullptr) {
716 add_error_log_msg(tombstone, android::base::StringPrintf("Cannot open log file %s", logger));
717 return;
718 }
719
720 LogBuffer buffer;
721 while (true) {
722 log_msg log_entry;
723 ssize_t actual = android_logger_list_read(logger_list, &log_entry);
724 if (actual < 0) {
725 if (actual == -EINTR) {
726 // interrupted by signal, retry
727 continue;
728 }
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)));
733 }
734 break;
735 } else if (actual == 0) {
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);
778 // Make sure only UTF8 characters are present since message is a string.
779 log_msg->set_message(oct_encode(msg));
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
dump_logcat(Tombstone * tombstone,pid_t pid)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
dump_tags_around_fault_addr(Signal * signal,const Tombstone & tombstone,std::shared_ptr<unwindstack::Memory> & process_memory,uintptr_t fault_addr)795 static void dump_tags_around_fault_addr(Signal* signal, const Tombstone& tombstone,
796 std::shared_ptr<unwindstack::Memory>& process_memory,
797 uintptr_t fault_addr) {
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
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.
816 while (process_memory->ReadTag(start_address) < 0) {
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) {
828 long tag = process_memory->ReadTag(start_address + i * kTagGranuleSize);
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
engrave_tombstone_proto(Tombstone * tombstone,unwindstack::AndroidUnwinder * unwinder,const std::map<pid_t,ThreadInfo> & threads,pid_t target_tid,const ProcessInfo & process_info,const OpenFilesList * open_files,const Architecture * guest_arch,unwindstack::AndroidUnwinder * guest_unwinder)838 void engrave_tombstone_proto(Tombstone* tombstone, unwindstack::AndroidUnwinder* unwinder,
839 const std::map<pid_t, ThreadInfo>& threads, pid_t target_tid,
840 const ProcessInfo& process_info, const OpenFilesList* open_files,
841 const Architecture* guest_arch,
842 unwindstack::AndroidUnwinder* guest_unwinder) {
843 Tombstone result;
844
845 result.set_arch(get_arch());
846 if (guest_arch != nullptr) {
847 result.set_guest_arch(*guest_arch);
848 } else {
849 result.set_guest_arch(Architecture::NONE);
850 }
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
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);
860 // The main thread must have a valid siginfo.
861 CHECK(target_thread.siginfo != nullptr);
862
863 struct sysinfo si;
864 sysinfo(&si);
865 android::procinfo::ProcessInfo proc_info;
866 std::string error;
867 if (android::procinfo::GetProcessInfo(target_thread.pid, &proc_info, &error)) {
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
875 result.set_page_size(getpagesize());
876 result.set_has_been_16kb_mode(android::base::GetBoolProperty("ro.misctrl.16kb_before", false));
877
878 auto cmd_line = result.mutable_command_line();
879 for (const auto& arg : target_thread.command_line) {
880 *cmd_line->Add() = arg;
881 }
882
883 if (!target_thread.siginfo) {
884 async_safe_fatal("siginfo missing");
885 }
886
887 Signal sig;
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));
892
893 if (signal_has_sender(target_thread.siginfo, target_thread.pid)) {
894 sig.set_has_sender(true);
895 sig.set_sender_uid(target_thread.siginfo->si_uid);
896 sig.set_sender_pid(target_thread.siginfo->si_pid);
897 }
898
899 if (process_info.has_fault_address) {
900 sig.set_has_fault_address(true);
901 uintptr_t fault_addr = process_info.maybe_tagged_fault_address;
902 sig.set_fault_address(fault_addr);
903 dump_tags_around_fault_addr(&sig, result, unwinder->GetProcessMemory(), fault_addr);
904 }
905
906 *result.mutable_signal_info() = sig;
907
908 dump_abort_message(&result, unwinder->GetProcessMemory(), process_info);
909 dump_crash_details(&result, unwinder->GetProcessMemory(), process_info);
910 // Dump the target thread, but save the memory around the registers.
911 dump_thread(&result, unwinder, target_thread, /* memory_dump */ true, guest_unwinder);
912
913 for (const auto& [tid, thread_info] : threads) {
914 if (tid != target_tid) {
915 dump_thread(&result, unwinder, thread_info, /* memory_dump */ false, guest_unwinder);
916 }
917 }
918
919 dump_probable_cause(&result, unwinder, process_info, target_thread, threads);
920
921 dump_mappings(&result, unwinder->GetMaps(), unwinder->GetProcessMemory());
922
923 // Only dump logs on debuggable devices.
924 if (android::base::GetBoolProperty("ro.debuggable", false)) {
925 // Get the thread that corresponds to the main pid of the process.
926 const ThreadInfo& thread = threads.at(target_thread.pid);
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") {
931 dump_logcat(&result, target_thread.pid);
932 }
933 }
934
935 dump_open_fds(&result, open_files);
936
937 *tombstone = std::move(result);
938 }
939