blob: 17a1f79e3fb7996efaa5735076a7e0d13e594bbc [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"
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -080018#include "libdebuggerd/tombstone.h"
Peter Collingbournef8622522020-04-07 14:07:32 -070019
Christopher Ferris3b7b7ba2022-03-15 16:56:09 -070020#include "unwindstack/AndroidUnwinder.h"
Peter Collingbournef8622522020-04-07 14:07:32 -070021#include "unwindstack/Memory.h"
Peter Collingbournef8622522020-04-07 14:07:32 -070022
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -080023#include <android-base/macros.h>
Peter Collingbournef8622522020-04-07 14:07:32 -070024#include <bionic/macros.h>
Kelvin Zhang786dac32023-06-15 16:23:56 -070025#include <unistd.h>
Peter Collingbournef8622522020-04-07 14:07:32 -070026
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -080027#include "tombstone.pb.h"
28
Peter Collingbourne78279912022-06-30 18:39:54 -070029std::unique_ptr<char[]> AllocAndReadFully(unwindstack::Memory* process_memory, uint64_t addr,
30 size_t size) {
31 auto buf = std::make_unique<char[]>(size);
32 if (!process_memory->ReadFully(addr, buf.get(), size)) {
33 return std::unique_ptr<char[]>();
34 }
35 return buf;
36}
37
38ScudoCrashData::ScudoCrashData(unwindstack::Memory* process_memory,
39 const ProcessInfo& process_info) {
Peter Collingbournef8622522020-04-07 14:07:32 -070040 if (!process_info.has_fault_address) {
Peter Collingbourne78279912022-06-30 18:39:54 -070041 return;
Peter Collingbournef8622522020-04-07 14:07:32 -070042 }
43
Peter Collingbourne78279912022-06-30 18:39:54 -070044 auto region_info = AllocAndReadFully(process_memory, process_info.scudo_region_info,
45 __scudo_get_region_info_size());
Florian Mayer30a25282023-01-12 15:25:03 -080046 std::unique_ptr<char[]> ring_buffer;
47 if (process_info.scudo_ring_buffer_size != 0) {
48 ring_buffer = AllocAndReadFully(process_memory, process_info.scudo_ring_buffer,
49 process_info.scudo_ring_buffer_size);
50 }
Florian Mayer48412072023-12-04 17:28:41 -080051 std::unique_ptr<char[]> stack_depot;
52 if (process_info.scudo_stack_depot_size != 0) {
53 stack_depot = AllocAndReadFully(process_memory, process_info.scudo_stack_depot,
54 process_info.scudo_stack_depot_size);
55 }
56 if (!region_info) {
Peter Collingbourne78279912022-06-30 18:39:54 -070057 return;
liyong381b89c2022-05-24 16:37:10 +080058 }
Peter Collingbournef8622522020-04-07 14:07:32 -070059
Mitch Phillipse4adff02021-01-21 20:41:50 -080060 untagged_fault_addr_ = process_info.untagged_fault_address;
Kelvin Zhang786dac32023-06-15 16:23:56 -070061 uintptr_t fault_page = untagged_fault_addr_ & ~(getpagesize() - 1);
Peter Collingbournef8622522020-04-07 14:07:32 -070062
Kelvin Zhang786dac32023-06-15 16:23:56 -070063 uintptr_t memory_begin = fault_page - getpagesize() * 16;
Peter Collingbourne78279912022-06-30 18:39:54 -070064 if (memory_begin > fault_page) {
65 return;
Peter Collingbournef8622522020-04-07 14:07:32 -070066 }
67
Kelvin Zhang786dac32023-06-15 16:23:56 -070068 uintptr_t memory_end = fault_page + getpagesize() * 16;
Peter Collingbourne78279912022-06-30 18:39:54 -070069 if (memory_end < fault_page) {
70 return;
Peter Collingbournef8622522020-04-07 14:07:32 -070071 }
72
Peter Collingbourne78279912022-06-30 18:39:54 -070073 auto memory = std::make_unique<char[]>(memory_end - memory_begin);
Kelvin Zhang786dac32023-06-15 16:23:56 -070074 for (auto i = memory_begin; i != memory_end; i += getpagesize()) {
75 process_memory->ReadFully(i, memory.get() + i - memory_begin, getpagesize());
Peter Collingbourne78279912022-06-30 18:39:54 -070076 }
Peter Collingbournef8622522020-04-07 14:07:32 -070077
Peter Collingbourne78279912022-06-30 18:39:54 -070078 auto memory_tags = std::make_unique<char[]>((memory_end - memory_begin) / kTagGranuleSize);
79 for (auto i = memory_begin; i != memory_end; i += kTagGranuleSize) {
80 memory_tags[(i - memory_begin) / kTagGranuleSize] = process_memory->ReadTag(i);
81 }
82
83 __scudo_get_error_info(&error_info_, process_info.maybe_tagged_fault_address, stack_depot.get(),
84 region_info.get(), ring_buffer.get(), memory.get(), memory_tags.get(),
85 memory_begin, memory_end - memory_begin);
Peter Collingbournef8622522020-04-07 14:07:32 -070086}
87
88bool ScudoCrashData::CrashIsMine() const {
89 return error_info_.reports[0].error_type != UNKNOWN;
90}
91
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -080092void ScudoCrashData::FillInCause(Cause* cause, const scudo_error_report* report,
Christopher Ferris3b7b7ba2022-03-15 16:56:09 -070093 unwindstack::AndroidUnwinder* unwinder) const {
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -080094 MemoryError* memory_error = cause->mutable_memory_error();
95 HeapObject* heap_object = memory_error->mutable_heap();
96
97 memory_error->set_tool(MemoryError_Tool_SCUDO);
98 switch (report->error_type) {
99 case USE_AFTER_FREE:
100 memory_error->set_type(MemoryError_Type_USE_AFTER_FREE);
101 break;
102 case BUFFER_OVERFLOW:
103 memory_error->set_type(MemoryError_Type_BUFFER_OVERFLOW);
104 break;
105 case BUFFER_UNDERFLOW:
106 memory_error->set_type(MemoryError_Type_BUFFER_UNDERFLOW);
107 break;
108 default:
109 memory_error->set_type(MemoryError_Type_UNKNOWN);
110 break;
111 }
112
113 heap_object->set_address(report->allocation_address);
114 heap_object->set_size(report->allocation_size);
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -0800115
116 heap_object->set_allocation_tid(report->allocation_tid);
117 for (size_t i = 0; i < arraysize(report->allocation_trace) && report->allocation_trace[i]; ++i) {
118 unwindstack::FrameData frame_data = unwinder->BuildFrameFromPcOnly(report->allocation_trace[i]);
119 BacktraceFrame* f = heap_object->add_allocation_backtrace();
Christopher Ferris22ad09b2021-11-10 17:25:11 -0800120 fill_in_backtrace_frame(f, frame_data);
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -0800121 }
122
123 heap_object->set_deallocation_tid(report->deallocation_tid);
124 for (size_t i = 0; i < arraysize(report->deallocation_trace) && report->deallocation_trace[i];
125 ++i) {
126 unwindstack::FrameData frame_data =
127 unwinder->BuildFrameFromPcOnly(report->deallocation_trace[i]);
128 BacktraceFrame* f = heap_object->add_deallocation_backtrace();
Christopher Ferris22ad09b2021-11-10 17:25:11 -0800129 fill_in_backtrace_frame(f, frame_data);
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -0800130 }
131
132 set_human_readable_cause(cause, untagged_fault_addr_);
133}
134
Christopher Ferris3b7b7ba2022-03-15 16:56:09 -0700135void ScudoCrashData::AddCauseProtos(Tombstone* tombstone,
136 unwindstack::AndroidUnwinder* unwinder) const {
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -0800137 size_t report_num = 0;
138 while (report_num < sizeof(error_info_.reports) / sizeof(error_info_.reports[0]) &&
139 error_info_.reports[report_num].error_type != UNKNOWN) {
140 FillInCause(tombstone->add_causes(), &error_info_.reports[report_num++], unwinder);
141 }
142}