Peter Collingbourne | f862252 | 2020-04-07 14:07:32 -0700 | [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 | #include "libdebuggerd/scudo.h" |
Peter Collingbourne | 1a1f7d7 | 2021-03-08 16:53:54 -0800 | [diff] [blame] | 18 | #include "libdebuggerd/tombstone.h" |
Peter Collingbourne | f862252 | 2020-04-07 14:07:32 -0700 | [diff] [blame] | 19 | |
Christopher Ferris | 3b7b7ba | 2022-03-15 16:56:09 -0700 | [diff] [blame] | 20 | #include "unwindstack/AndroidUnwinder.h" |
Peter Collingbourne | f862252 | 2020-04-07 14:07:32 -0700 | [diff] [blame] | 21 | #include "unwindstack/Memory.h" |
Peter Collingbourne | f862252 | 2020-04-07 14:07:32 -0700 | [diff] [blame] | 22 | |
Peter Collingbourne | 1a1f7d7 | 2021-03-08 16:53:54 -0800 | [diff] [blame] | 23 | #include <android-base/macros.h> |
Peter Collingbourne | f862252 | 2020-04-07 14:07:32 -0700 | [diff] [blame] | 24 | #include <bionic/macros.h> |
| 25 | |
Peter Collingbourne | 1a1f7d7 | 2021-03-08 16:53:54 -0800 | [diff] [blame] | 26 | #include "tombstone.pb.h" |
| 27 | |
Peter Collingbourne | 7827991 | 2022-06-30 18:39:54 -0700 | [diff] [blame^] | 28 | std::unique_ptr<char[]> AllocAndReadFully(unwindstack::Memory* process_memory, uint64_t addr, |
| 29 | size_t size) { |
| 30 | auto buf = std::make_unique<char[]>(size); |
| 31 | if (!process_memory->ReadFully(addr, buf.get(), size)) { |
| 32 | return std::unique_ptr<char[]>(); |
| 33 | } |
| 34 | return buf; |
| 35 | } |
| 36 | |
| 37 | ScudoCrashData::ScudoCrashData(unwindstack::Memory* process_memory, |
| 38 | const ProcessInfo& process_info) { |
Peter Collingbourne | f862252 | 2020-04-07 14:07:32 -0700 | [diff] [blame] | 39 | if (!process_info.has_fault_address) { |
Peter Collingbourne | 7827991 | 2022-06-30 18:39:54 -0700 | [diff] [blame^] | 40 | return; |
Peter Collingbourne | f862252 | 2020-04-07 14:07:32 -0700 | [diff] [blame] | 41 | } |
| 42 | |
Peter Collingbourne | 7827991 | 2022-06-30 18:39:54 -0700 | [diff] [blame^] | 43 | auto stack_depot = AllocAndReadFully(process_memory, process_info.scudo_stack_depot, |
| 44 | __scudo_get_stack_depot_size()); |
| 45 | auto region_info = AllocAndReadFully(process_memory, process_info.scudo_region_info, |
| 46 | __scudo_get_region_info_size()); |
| 47 | auto ring_buffer = AllocAndReadFully(process_memory, process_info.scudo_ring_buffer, |
| 48 | __scudo_get_ring_buffer_size()); |
| 49 | if (!stack_depot || !region_info || !ring_buffer) { |
| 50 | return; |
liyong | 381b89c | 2022-05-24 16:37:10 +0800 | [diff] [blame] | 51 | } |
Peter Collingbourne | f862252 | 2020-04-07 14:07:32 -0700 | [diff] [blame] | 52 | |
Mitch Phillips | e4adff0 | 2021-01-21 20:41:50 -0800 | [diff] [blame] | 53 | untagged_fault_addr_ = process_info.untagged_fault_address; |
Peter Collingbourne | 7827991 | 2022-06-30 18:39:54 -0700 | [diff] [blame^] | 54 | uintptr_t fault_page = untagged_fault_addr_ & ~(PAGE_SIZE - 1); |
Peter Collingbourne | f862252 | 2020-04-07 14:07:32 -0700 | [diff] [blame] | 55 | |
Peter Collingbourne | 7827991 | 2022-06-30 18:39:54 -0700 | [diff] [blame^] | 56 | uintptr_t memory_begin = fault_page - PAGE_SIZE * 16; |
| 57 | if (memory_begin > fault_page) { |
| 58 | return; |
Peter Collingbourne | f862252 | 2020-04-07 14:07:32 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Peter Collingbourne | 7827991 | 2022-06-30 18:39:54 -0700 | [diff] [blame^] | 61 | uintptr_t memory_end = fault_page + PAGE_SIZE * 16; |
| 62 | if (memory_end < fault_page) { |
| 63 | return; |
Peter Collingbourne | f862252 | 2020-04-07 14:07:32 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Peter Collingbourne | 7827991 | 2022-06-30 18:39:54 -0700 | [diff] [blame^] | 66 | auto memory = std::make_unique<char[]>(memory_end - memory_begin); |
| 67 | for (auto i = memory_begin; i != memory_end; i += PAGE_SIZE) { |
| 68 | process_memory->ReadFully(i, memory.get() + i - memory_begin, PAGE_SIZE); |
| 69 | } |
Peter Collingbourne | f862252 | 2020-04-07 14:07:32 -0700 | [diff] [blame] | 70 | |
Peter Collingbourne | 7827991 | 2022-06-30 18:39:54 -0700 | [diff] [blame^] | 71 | auto memory_tags = std::make_unique<char[]>((memory_end - memory_begin) / kTagGranuleSize); |
| 72 | for (auto i = memory_begin; i != memory_end; i += kTagGranuleSize) { |
| 73 | memory_tags[(i - memory_begin) / kTagGranuleSize] = process_memory->ReadTag(i); |
| 74 | } |
| 75 | |
| 76 | __scudo_get_error_info(&error_info_, process_info.maybe_tagged_fault_address, stack_depot.get(), |
| 77 | region_info.get(), ring_buffer.get(), memory.get(), memory_tags.get(), |
| 78 | memory_begin, memory_end - memory_begin); |
Peter Collingbourne | f862252 | 2020-04-07 14:07:32 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | bool ScudoCrashData::CrashIsMine() const { |
| 82 | return error_info_.reports[0].error_type != UNKNOWN; |
| 83 | } |
| 84 | |
Peter Collingbourne | 1a1f7d7 | 2021-03-08 16:53:54 -0800 | [diff] [blame] | 85 | void ScudoCrashData::FillInCause(Cause* cause, const scudo_error_report* report, |
Christopher Ferris | 3b7b7ba | 2022-03-15 16:56:09 -0700 | [diff] [blame] | 86 | unwindstack::AndroidUnwinder* unwinder) const { |
Peter Collingbourne | 1a1f7d7 | 2021-03-08 16:53:54 -0800 | [diff] [blame] | 87 | MemoryError* memory_error = cause->mutable_memory_error(); |
| 88 | HeapObject* heap_object = memory_error->mutable_heap(); |
| 89 | |
| 90 | memory_error->set_tool(MemoryError_Tool_SCUDO); |
| 91 | switch (report->error_type) { |
| 92 | case USE_AFTER_FREE: |
| 93 | memory_error->set_type(MemoryError_Type_USE_AFTER_FREE); |
| 94 | break; |
| 95 | case BUFFER_OVERFLOW: |
| 96 | memory_error->set_type(MemoryError_Type_BUFFER_OVERFLOW); |
| 97 | break; |
| 98 | case BUFFER_UNDERFLOW: |
| 99 | memory_error->set_type(MemoryError_Type_BUFFER_UNDERFLOW); |
| 100 | break; |
| 101 | default: |
| 102 | memory_error->set_type(MemoryError_Type_UNKNOWN); |
| 103 | break; |
| 104 | } |
| 105 | |
| 106 | heap_object->set_address(report->allocation_address); |
| 107 | heap_object->set_size(report->allocation_size); |
Peter Collingbourne | 1a1f7d7 | 2021-03-08 16:53:54 -0800 | [diff] [blame] | 108 | |
| 109 | heap_object->set_allocation_tid(report->allocation_tid); |
| 110 | for (size_t i = 0; i < arraysize(report->allocation_trace) && report->allocation_trace[i]; ++i) { |
| 111 | unwindstack::FrameData frame_data = unwinder->BuildFrameFromPcOnly(report->allocation_trace[i]); |
| 112 | BacktraceFrame* f = heap_object->add_allocation_backtrace(); |
Christopher Ferris | 22ad09b | 2021-11-10 17:25:11 -0800 | [diff] [blame] | 113 | fill_in_backtrace_frame(f, frame_data); |
Peter Collingbourne | 1a1f7d7 | 2021-03-08 16:53:54 -0800 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | heap_object->set_deallocation_tid(report->deallocation_tid); |
| 117 | for (size_t i = 0; i < arraysize(report->deallocation_trace) && report->deallocation_trace[i]; |
| 118 | ++i) { |
| 119 | unwindstack::FrameData frame_data = |
| 120 | unwinder->BuildFrameFromPcOnly(report->deallocation_trace[i]); |
| 121 | BacktraceFrame* f = heap_object->add_deallocation_backtrace(); |
Christopher Ferris | 22ad09b | 2021-11-10 17:25:11 -0800 | [diff] [blame] | 122 | fill_in_backtrace_frame(f, frame_data); |
Peter Collingbourne | 1a1f7d7 | 2021-03-08 16:53:54 -0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | set_human_readable_cause(cause, untagged_fault_addr_); |
| 126 | } |
| 127 | |
Christopher Ferris | 3b7b7ba | 2022-03-15 16:56:09 -0700 | [diff] [blame] | 128 | void ScudoCrashData::AddCauseProtos(Tombstone* tombstone, |
| 129 | unwindstack::AndroidUnwinder* unwinder) const { |
Peter Collingbourne | 1a1f7d7 | 2021-03-08 16:53:54 -0800 | [diff] [blame] | 130 | size_t report_num = 0; |
| 131 | while (report_num < sizeof(error_info_.reports) / sizeof(error_info_.reports[0]) && |
| 132 | error_info_.reports[report_num].error_type != UNKNOWN) { |
| 133 | FillInCause(tombstone->add_causes(), &error_info_.reports[report_num++], unwinder); |
| 134 | } |
| 135 | } |