blob: 27fae25a179cd48fedf7a92793615d5858150fa1 [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>
25
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -080026#include "tombstone.pb.h"
27
Peter Collingbournef8622522020-04-07 14:07:32 -070028std::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
Peter Collingbournef8622522020-04-07 14:07:32 -070037ScudoCrashData::ScudoCrashData(unwindstack::Memory* process_memory,
38 const ProcessInfo& process_info) {
39 if (!process_info.has_fault_address) {
40 return;
41 }
42
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());
Peter Collingbournebb4b49c2021-01-06 21:02:19 -080047 auto ring_buffer = AllocAndReadFully(process_memory, process_info.scudo_ring_buffer,
48 __scudo_get_ring_buffer_size());
Peter Collingbournef8622522020-04-07 14:07:32 -070049
Mitch Phillipse4adff02021-01-21 20:41:50 -080050 untagged_fault_addr_ = process_info.untagged_fault_address;
Peter Collingbournef8622522020-04-07 14:07:32 -070051 uintptr_t fault_page = untagged_fault_addr_ & ~(PAGE_SIZE - 1);
52
53 uintptr_t memory_begin = fault_page - PAGE_SIZE * 16;
54 if (memory_begin > fault_page) {
55 return;
56 }
57
58 uintptr_t memory_end = fault_page + PAGE_SIZE * 16;
59 if (memory_end < fault_page) {
60 return;
61 }
62
63 auto memory = std::make_unique<char[]>(memory_end - memory_begin);
64 for (auto i = memory_begin; i != memory_end; i += PAGE_SIZE) {
65 process_memory->ReadFully(i, memory.get() + i - memory_begin, PAGE_SIZE);
66 }
67
68 auto memory_tags = std::make_unique<char[]>((memory_end - memory_begin) / kTagGranuleSize);
69 for (auto i = memory_begin; i != memory_end; i += kTagGranuleSize) {
70 memory_tags[(i - memory_begin) / kTagGranuleSize] = process_memory->ReadTag(i);
71 }
72
Mitch Phillipse4adff02021-01-21 20:41:50 -080073 __scudo_get_error_info(&error_info_, process_info.maybe_tagged_fault_address, stack_depot.get(),
Peter Collingbournebb4b49c2021-01-06 21:02:19 -080074 region_info.get(), ring_buffer.get(), memory.get(), memory_tags.get(),
75 memory_begin, memory_end - memory_begin);
Peter Collingbournef8622522020-04-07 14:07:32 -070076}
77
78bool ScudoCrashData::CrashIsMine() const {
79 return error_info_.reports[0].error_type != UNKNOWN;
80}
81
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -080082void ScudoCrashData::FillInCause(Cause* cause, const scudo_error_report* report,
Christopher Ferris3b7b7ba2022-03-15 16:56:09 -070083 unwindstack::AndroidUnwinder* unwinder) const {
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -080084 MemoryError* memory_error = cause->mutable_memory_error();
85 HeapObject* heap_object = memory_error->mutable_heap();
86
87 memory_error->set_tool(MemoryError_Tool_SCUDO);
88 switch (report->error_type) {
89 case USE_AFTER_FREE:
90 memory_error->set_type(MemoryError_Type_USE_AFTER_FREE);
91 break;
92 case BUFFER_OVERFLOW:
93 memory_error->set_type(MemoryError_Type_BUFFER_OVERFLOW);
94 break;
95 case BUFFER_UNDERFLOW:
96 memory_error->set_type(MemoryError_Type_BUFFER_UNDERFLOW);
97 break;
98 default:
99 memory_error->set_type(MemoryError_Type_UNKNOWN);
100 break;
101 }
102
103 heap_object->set_address(report->allocation_address);
104 heap_object->set_size(report->allocation_size);
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -0800105
106 heap_object->set_allocation_tid(report->allocation_tid);
107 for (size_t i = 0; i < arraysize(report->allocation_trace) && report->allocation_trace[i]; ++i) {
108 unwindstack::FrameData frame_data = unwinder->BuildFrameFromPcOnly(report->allocation_trace[i]);
109 BacktraceFrame* f = heap_object->add_allocation_backtrace();
Christopher Ferris22ad09b2021-11-10 17:25:11 -0800110 fill_in_backtrace_frame(f, frame_data);
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -0800111 }
112
113 heap_object->set_deallocation_tid(report->deallocation_tid);
114 for (size_t i = 0; i < arraysize(report->deallocation_trace) && report->deallocation_trace[i];
115 ++i) {
116 unwindstack::FrameData frame_data =
117 unwinder->BuildFrameFromPcOnly(report->deallocation_trace[i]);
118 BacktraceFrame* f = heap_object->add_deallocation_backtrace();
Christopher Ferris22ad09b2021-11-10 17:25:11 -0800119 fill_in_backtrace_frame(f, frame_data);
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -0800120 }
121
122 set_human_readable_cause(cause, untagged_fault_addr_);
123}
124
Christopher Ferris3b7b7ba2022-03-15 16:56:09 -0700125void ScudoCrashData::AddCauseProtos(Tombstone* tombstone,
126 unwindstack::AndroidUnwinder* unwinder) const {
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -0800127 size_t report_num = 0;
128 while (report_num < sizeof(error_info_.reports) / sizeof(error_info_.reports[0]) &&
129 error_info_.reports[report_num].error_type != UNKNOWN) {
130 FillInCause(tombstone->add_causes(), &error_info_.reports[report_num++], unwinder);
131 }
132}