blob: 1c3437fde84af91b09103c9c889ee1a37b3ee511 [file] [log] [blame]
Peter Collingbournef8622522020-04-07 14:07:32 -07001/*
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"
18#include "libdebuggerd/gwp_asan.h"
19
20#include "unwindstack/Memory.h"
21#include "unwindstack/Unwinder.h"
22
23#include <bionic/macros.h>
24
25std::unique_ptr<char[]> AllocAndReadFully(unwindstack::Memory* process_memory, uint64_t addr,
26 size_t size) {
27 auto buf = std::make_unique<char[]>(size);
28 if (!process_memory->ReadFully(addr, buf.get(), size)) {
29 return std::unique_ptr<char[]>();
30 }
31 return buf;
32}
33
34static const uintptr_t kTagGranuleSize = 16;
35
36ScudoCrashData::ScudoCrashData(unwindstack::Memory* process_memory,
37 const ProcessInfo& process_info) {
38 if (!process_info.has_fault_address) {
39 return;
40 }
41
42 auto stack_depot = AllocAndReadFully(process_memory, process_info.scudo_stack_depot,
43 __scudo_get_stack_depot_size());
44 auto region_info = AllocAndReadFully(process_memory, process_info.scudo_region_info,
45 __scudo_get_region_info_size());
Peter Collingbournebb4b49c2021-01-06 21:02:19 -080046 auto ring_buffer = AllocAndReadFully(process_memory, process_info.scudo_ring_buffer,
47 __scudo_get_ring_buffer_size());
Peter Collingbournef8622522020-04-07 14:07:32 -070048
Mitch Phillipse4adff02021-01-21 20:41:50 -080049 untagged_fault_addr_ = process_info.untagged_fault_address;
Peter Collingbournef8622522020-04-07 14:07:32 -070050 uintptr_t fault_page = untagged_fault_addr_ & ~(PAGE_SIZE - 1);
51
52 uintptr_t memory_begin = fault_page - PAGE_SIZE * 16;
53 if (memory_begin > fault_page) {
54 return;
55 }
56
57 uintptr_t memory_end = fault_page + PAGE_SIZE * 16;
58 if (memory_end < fault_page) {
59 return;
60 }
61
62 auto memory = std::make_unique<char[]>(memory_end - memory_begin);
63 for (auto i = memory_begin; i != memory_end; i += PAGE_SIZE) {
64 process_memory->ReadFully(i, memory.get() + i - memory_begin, PAGE_SIZE);
65 }
66
67 auto memory_tags = std::make_unique<char[]>((memory_end - memory_begin) / kTagGranuleSize);
68 for (auto i = memory_begin; i != memory_end; i += kTagGranuleSize) {
69 memory_tags[(i - memory_begin) / kTagGranuleSize] = process_memory->ReadTag(i);
70 }
71
Mitch Phillipse4adff02021-01-21 20:41:50 -080072 __scudo_get_error_info(&error_info_, process_info.maybe_tagged_fault_address, stack_depot.get(),
Peter Collingbournebb4b49c2021-01-06 21:02:19 -080073 region_info.get(), ring_buffer.get(), memory.get(), memory_tags.get(),
74 memory_begin, memory_end - memory_begin);
Peter Collingbournef8622522020-04-07 14:07:32 -070075}
76
77bool ScudoCrashData::CrashIsMine() const {
78 return error_info_.reports[0].error_type != UNKNOWN;
79}
80
81void ScudoCrashData::DumpCause(log_t* log, unwindstack::Unwinder* unwinder) const {
82 if (error_info_.reports[1].error_type != UNKNOWN) {
83 _LOG(log, logtype::HEADER,
84 "\nNote: multiple potential causes for this crash were detected, listing them in "
85 "decreasing order of probability.\n");
86 }
87
88 size_t report_num = 0;
89 while (report_num < sizeof(error_info_.reports) / sizeof(error_info_.reports[0]) &&
90 error_info_.reports[report_num].error_type != UNKNOWN) {
91 DumpReport(&error_info_.reports[report_num++], log, unwinder);
92 }
93}
94
95void ScudoCrashData::DumpReport(const scudo_error_report* report, log_t* log,
96 unwindstack::Unwinder* unwinder) const {
97 const char *error_type_str;
98 switch (report->error_type) {
99 case USE_AFTER_FREE:
100 error_type_str = "Use After Free";
101 break;
102 case BUFFER_OVERFLOW:
103 error_type_str = "Buffer Overflow";
104 break;
105 case BUFFER_UNDERFLOW:
106 error_type_str = "Buffer Underflow";
107 break;
108 default:
109 error_type_str = "Unknown";
110 break;
111 }
112
113 uintptr_t diff;
114 const char* location_str;
115
116 if (untagged_fault_addr_ < report->allocation_address) {
117 // Buffer Underflow, 6 bytes left of a 41-byte allocation at 0xdeadbeef.
118 location_str = "left of";
119 diff = report->allocation_address - untagged_fault_addr_;
120 } else if (untagged_fault_addr_ - report->allocation_address < report->allocation_size) {
121 // Use After Free, 40 bytes into a 41-byte allocation at 0xdeadbeef.
122 location_str = "into";
123 diff = untagged_fault_addr_ - report->allocation_address;
124 } else {
125 // Buffer Overflow, 6 bytes right of a 41-byte allocation at 0xdeadbeef.
126 location_str = "right of";
127 diff = untagged_fault_addr_ - report->allocation_address - report->allocation_size;
128 }
129
130 // Suffix of 'bytes', i.e. 4 bytes' vs. '1 byte'.
131 const char* byte_suffix = "s";
132 if (diff == 1) {
133 byte_suffix = "";
134 }
135 _LOG(log, logtype::HEADER,
136 "\nCause: [MTE]: %s, %" PRIuPTR " byte%s %s a %zu-byte allocation at 0x%" PRIxPTR "\n",
137 error_type_str, diff, byte_suffix, location_str, report->allocation_size,
138 report->allocation_address);
139
140 if (report->allocation_trace[0]) {
141 _LOG(log, logtype::BACKTRACE, "\nallocated by thread %u:\n", report->allocation_tid);
142 unwinder->SetDisplayBuildID(true);
143 for (size_t i = 0; i < 64 && report->allocation_trace[i]; ++i) {
144 unwindstack::FrameData frame_data =
145 unwinder->BuildFrameFromPcOnly(report->allocation_trace[i]);
146 frame_data.num = i;
147 _LOG(log, logtype::BACKTRACE, " %s\n", unwinder->FormatFrame(frame_data).c_str());
148 }
149 }
150
151 if (report->deallocation_trace[0]) {
152 _LOG(log, logtype::BACKTRACE, "\ndeallocated by thread %u:\n", report->deallocation_tid);
153 unwinder->SetDisplayBuildID(true);
154 for (size_t i = 0; i < 64 && report->deallocation_trace[i]; ++i) {
155 unwindstack::FrameData frame_data =
156 unwinder->BuildFrameFromPcOnly(report->deallocation_trace[i]);
157 frame_data.num = i;
158 _LOG(log, logtype::BACKTRACE, " %s\n", unwinder->FormatFrame(frame_data).c_str());
159 }
160 }
161}