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