blob: 71d5fcfa73ba14736133ac85a6424e042a050525 [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
Christopher Ferris1c46a002024-04-23 17:24:26 -070017#if defined(USE_SCUDO)
18
Peter Collingbournef8622522020-04-07 14:07:32 -070019#include "libdebuggerd/scudo.h"
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -080020#include "libdebuggerd/tombstone.h"
Peter Collingbourne39a17302024-03-07 17:50:10 -080021#include "libdebuggerd/utility_host.h"
Peter Collingbournef8622522020-04-07 14:07:32 -070022
Christopher Ferris3b7b7ba2022-03-15 16:56:09 -070023#include "unwindstack/AndroidUnwinder.h"
Peter Collingbournef8622522020-04-07 14:07:32 -070024#include "unwindstack/Memory.h"
Peter Collingbournef8622522020-04-07 14:07:32 -070025
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -080026#include <android-base/macros.h>
Peter Collingbournef8622522020-04-07 14:07:32 -070027#include <bionic/macros.h>
Kelvin Zhang786dac32023-06-15 16:23:56 -070028#include <unistd.h>
Peter Collingbournef8622522020-04-07 14:07:32 -070029
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -080030#include "tombstone.pb.h"
31
Peter Collingbourne78279912022-06-30 18:39:54 -070032std::unique_ptr<char[]> AllocAndReadFully(unwindstack::Memory* process_memory, uint64_t addr,
33 size_t size) {
34 auto buf = std::make_unique<char[]>(size);
35 if (!process_memory->ReadFully(addr, buf.get(), size)) {
36 return std::unique_ptr<char[]>();
37 }
38 return buf;
39}
40
41ScudoCrashData::ScudoCrashData(unwindstack::Memory* process_memory,
42 const ProcessInfo& process_info) {
Peter Collingbournef8622522020-04-07 14:07:32 -070043 if (!process_info.has_fault_address) {
Peter Collingbourne78279912022-06-30 18:39:54 -070044 return;
Peter Collingbournef8622522020-04-07 14:07:32 -070045 }
46
Peter Collingbourne78279912022-06-30 18:39:54 -070047 auto region_info = AllocAndReadFully(process_memory, process_info.scudo_region_info,
48 __scudo_get_region_info_size());
Florian Mayer30a25282023-01-12 15:25:03 -080049 std::unique_ptr<char[]> ring_buffer;
50 if (process_info.scudo_ring_buffer_size != 0) {
51 ring_buffer = AllocAndReadFully(process_memory, process_info.scudo_ring_buffer,
52 process_info.scudo_ring_buffer_size);
53 }
Florian Mayer48412072023-12-04 17:28:41 -080054 std::unique_ptr<char[]> stack_depot;
55 if (process_info.scudo_stack_depot_size != 0) {
56 stack_depot = AllocAndReadFully(process_memory, process_info.scudo_stack_depot,
57 process_info.scudo_stack_depot_size);
58 }
59 if (!region_info) {
Peter Collingbourne78279912022-06-30 18:39:54 -070060 return;
liyong381b89c2022-05-24 16:37:10 +080061 }
Peter Collingbournef8622522020-04-07 14:07:32 -070062
Mitch Phillipse4adff02021-01-21 20:41:50 -080063 untagged_fault_addr_ = process_info.untagged_fault_address;
Kelvin Zhang786dac32023-06-15 16:23:56 -070064 uintptr_t fault_page = untagged_fault_addr_ & ~(getpagesize() - 1);
Peter Collingbournef8622522020-04-07 14:07:32 -070065
Kelvin Zhang786dac32023-06-15 16:23:56 -070066 uintptr_t memory_begin = fault_page - getpagesize() * 16;
Peter Collingbourne78279912022-06-30 18:39:54 -070067 if (memory_begin > fault_page) {
68 return;
Peter Collingbournef8622522020-04-07 14:07:32 -070069 }
70
Kelvin Zhang786dac32023-06-15 16:23:56 -070071 uintptr_t memory_end = fault_page + getpagesize() * 16;
Peter Collingbourne78279912022-06-30 18:39:54 -070072 if (memory_end < fault_page) {
73 return;
Peter Collingbournef8622522020-04-07 14:07:32 -070074 }
75
Peter Collingbourne78279912022-06-30 18:39:54 -070076 auto memory = std::make_unique<char[]>(memory_end - memory_begin);
Kelvin Zhang786dac32023-06-15 16:23:56 -070077 for (auto i = memory_begin; i != memory_end; i += getpagesize()) {
78 process_memory->ReadFully(i, memory.get() + i - memory_begin, getpagesize());
Peter Collingbourne78279912022-06-30 18:39:54 -070079 }
Peter Collingbournef8622522020-04-07 14:07:32 -070080
Peter Collingbourne78279912022-06-30 18:39:54 -070081 auto memory_tags = std::make_unique<char[]>((memory_end - memory_begin) / kTagGranuleSize);
82 for (auto i = memory_begin; i != memory_end; i += kTagGranuleSize) {
83 memory_tags[(i - memory_begin) / kTagGranuleSize] = process_memory->ReadTag(i);
84 }
85
86 __scudo_get_error_info(&error_info_, process_info.maybe_tagged_fault_address, stack_depot.get(),
Florian Mayercdf55582023-12-13 22:21:16 +000087 process_info.scudo_stack_depot_size, region_info.get(), ring_buffer.get(),
Florian Mayer6757ecd2023-12-05 16:13:36 -080088 process_info.scudo_ring_buffer_size, memory.get(), memory_tags.get(),
Peter Collingbourne78279912022-06-30 18:39:54 -070089 memory_begin, memory_end - memory_begin);
Peter Collingbournef8622522020-04-07 14:07:32 -070090}
91
92bool ScudoCrashData::CrashIsMine() const {
93 return error_info_.reports[0].error_type != UNKNOWN;
94}
95
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -080096void ScudoCrashData::FillInCause(Cause* cause, const scudo_error_report* report,
Christopher Ferris3b7b7ba2022-03-15 16:56:09 -070097 unwindstack::AndroidUnwinder* unwinder) const {
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -080098 MemoryError* memory_error = cause->mutable_memory_error();
99 HeapObject* heap_object = memory_error->mutable_heap();
100
101 memory_error->set_tool(MemoryError_Tool_SCUDO);
102 switch (report->error_type) {
103 case USE_AFTER_FREE:
104 memory_error->set_type(MemoryError_Type_USE_AFTER_FREE);
105 break;
106 case BUFFER_OVERFLOW:
107 memory_error->set_type(MemoryError_Type_BUFFER_OVERFLOW);
108 break;
109 case BUFFER_UNDERFLOW:
110 memory_error->set_type(MemoryError_Type_BUFFER_UNDERFLOW);
111 break;
112 default:
113 memory_error->set_type(MemoryError_Type_UNKNOWN);
114 break;
115 }
116
117 heap_object->set_address(report->allocation_address);
118 heap_object->set_size(report->allocation_size);
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -0800119
120 heap_object->set_allocation_tid(report->allocation_tid);
121 for (size_t i = 0; i < arraysize(report->allocation_trace) && report->allocation_trace[i]; ++i) {
122 unwindstack::FrameData frame_data = unwinder->BuildFrameFromPcOnly(report->allocation_trace[i]);
123 BacktraceFrame* f = heap_object->add_allocation_backtrace();
Christopher Ferris22ad09b2021-11-10 17:25:11 -0800124 fill_in_backtrace_frame(f, frame_data);
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -0800125 }
126
127 heap_object->set_deallocation_tid(report->deallocation_tid);
128 for (size_t i = 0; i < arraysize(report->deallocation_trace) && report->deallocation_trace[i];
129 ++i) {
130 unwindstack::FrameData frame_data =
131 unwinder->BuildFrameFromPcOnly(report->deallocation_trace[i]);
132 BacktraceFrame* f = heap_object->add_deallocation_backtrace();
Christopher Ferris22ad09b2021-11-10 17:25:11 -0800133 fill_in_backtrace_frame(f, frame_data);
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -0800134 }
135
136 set_human_readable_cause(cause, untagged_fault_addr_);
137}
138
Christopher Ferris3b7b7ba2022-03-15 16:56:09 -0700139void ScudoCrashData::AddCauseProtos(Tombstone* tombstone,
140 unwindstack::AndroidUnwinder* unwinder) const {
Peter Collingbourne1a1f7d72021-03-08 16:53:54 -0800141 size_t report_num = 0;
142 while (report_num < sizeof(error_info_.reports) / sizeof(error_info_.reports[0]) &&
143 error_info_.reports[report_num].error_type != UNKNOWN) {
144 FillInCause(tombstone->add_causes(), &error_info_.reports[report_num++], unwinder);
145 }
146}
Christopher Ferris1c46a002024-04-23 17:24:26 -0700147
148#endif // USE_SCUDO