2020-04-07 23:07:32 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2020 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libdebuggerd/scudo.h"
|
2021-03-09 01:53:54 +01:00
|
|
|
#include "libdebuggerd/tombstone.h"
|
2020-04-07 23:07:32 +02:00
|
|
|
|
2022-03-16 00:56:09 +01:00
|
|
|
#include "unwindstack/AndroidUnwinder.h"
|
2020-04-07 23:07:32 +02:00
|
|
|
#include "unwindstack/Memory.h"
|
|
|
|
|
2021-03-09 01:53:54 +01:00
|
|
|
#include <android-base/macros.h>
|
2020-04-07 23:07:32 +02:00
|
|
|
#include <bionic/macros.h>
|
2023-06-16 01:23:56 +02:00
|
|
|
#include <unistd.h>
|
2020-04-07 23:07:32 +02:00
|
|
|
|
2021-03-09 01:53:54 +01:00
|
|
|
#include "tombstone.pb.h"
|
|
|
|
|
2022-07-01 03:39:54 +02:00
|
|
|
std::unique_ptr<char[]> AllocAndReadFully(unwindstack::Memory* process_memory, uint64_t addr,
|
|
|
|
size_t size) {
|
|
|
|
auto buf = std::make_unique<char[]>(size);
|
|
|
|
if (!process_memory->ReadFully(addr, buf.get(), size)) {
|
|
|
|
return std::unique_ptr<char[]>();
|
2020-04-07 23:07:32 +02:00
|
|
|
}
|
2022-07-01 03:39:54 +02:00
|
|
|
return buf;
|
|
|
|
}
|
2020-04-07 23:07:32 +02:00
|
|
|
|
2022-07-01 03:39:54 +02:00
|
|
|
ScudoCrashData::ScudoCrashData(unwindstack::Memory* process_memory,
|
|
|
|
const ProcessInfo& process_info) {
|
|
|
|
if (!process_info.has_fault_address) {
|
|
|
|
return;
|
2020-04-07 23:07:32 +02:00
|
|
|
}
|
|
|
|
|
2022-07-01 03:39:54 +02:00
|
|
|
auto region_info = AllocAndReadFully(process_memory, process_info.scudo_region_info,
|
|
|
|
__scudo_get_region_info_size());
|
2023-01-13 00:25:03 +01:00
|
|
|
std::unique_ptr<char[]> ring_buffer;
|
|
|
|
if (process_info.scudo_ring_buffer_size != 0) {
|
|
|
|
ring_buffer = AllocAndReadFully(process_memory, process_info.scudo_ring_buffer,
|
|
|
|
process_info.scudo_ring_buffer_size);
|
|
|
|
}
|
2023-12-05 02:28:41 +01:00
|
|
|
std::unique_ptr<char[]> stack_depot;
|
|
|
|
if (process_info.scudo_stack_depot_size != 0) {
|
|
|
|
stack_depot = AllocAndReadFully(process_memory, process_info.scudo_stack_depot,
|
|
|
|
process_info.scudo_stack_depot_size);
|
|
|
|
}
|
|
|
|
if (!region_info) {
|
2022-07-01 03:39:54 +02:00
|
|
|
return;
|
|
|
|
}
|
2020-04-07 23:07:32 +02:00
|
|
|
|
2021-01-22 05:41:50 +01:00
|
|
|
untagged_fault_addr_ = process_info.untagged_fault_address;
|
2023-06-16 01:23:56 +02:00
|
|
|
uintptr_t fault_page = untagged_fault_addr_ & ~(getpagesize() - 1);
|
2020-04-07 23:07:32 +02:00
|
|
|
|
2023-06-16 01:23:56 +02:00
|
|
|
uintptr_t memory_begin = fault_page - getpagesize() * 16;
|
2022-07-01 03:39:54 +02:00
|
|
|
if (memory_begin > fault_page) {
|
|
|
|
return;
|
2020-04-07 23:07:32 +02:00
|
|
|
}
|
|
|
|
|
2023-06-16 01:23:56 +02:00
|
|
|
uintptr_t memory_end = fault_page + getpagesize() * 16;
|
2022-07-01 03:39:54 +02:00
|
|
|
if (memory_end < fault_page) {
|
|
|
|
return;
|
2020-04-07 23:07:32 +02:00
|
|
|
}
|
|
|
|
|
2022-07-01 03:39:54 +02:00
|
|
|
auto memory = std::make_unique<char[]>(memory_end - memory_begin);
|
2023-06-16 01:23:56 +02:00
|
|
|
for (auto i = memory_begin; i != memory_end; i += getpagesize()) {
|
|
|
|
process_memory->ReadFully(i, memory.get() + i - memory_begin, getpagesize());
|
2020-04-07 23:07:32 +02:00
|
|
|
}
|
|
|
|
|
2022-07-01 03:39:54 +02:00
|
|
|
auto memory_tags = std::make_unique<char[]>((memory_end - memory_begin) / kTagGranuleSize);
|
|
|
|
for (auto i = memory_begin; i != memory_end; i += kTagGranuleSize) {
|
|
|
|
memory_tags[(i - memory_begin) / kTagGranuleSize] = process_memory->ReadTag(i);
|
2020-04-07 23:07:32 +02:00
|
|
|
}
|
|
|
|
|
2022-07-01 03:39:54 +02:00
|
|
|
__scudo_get_error_info(&error_info_, process_info.maybe_tagged_fault_address, stack_depot.get(),
|
2023-12-06 01:13:36 +01:00
|
|
|
__scudo_get_stack_depot_size(), region_info.get(), ring_buffer.get(),
|
|
|
|
process_info.scudo_ring_buffer_size, memory.get(), memory_tags.get(),
|
2022-07-01 03:39:54 +02:00
|
|
|
memory_begin, memory_end - memory_begin);
|
2020-04-07 23:07:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ScudoCrashData::CrashIsMine() const {
|
|
|
|
return error_info_.reports[0].error_type != UNKNOWN;
|
|
|
|
}
|
|
|
|
|
2021-03-09 01:53:54 +01:00
|
|
|
void ScudoCrashData::FillInCause(Cause* cause, const scudo_error_report* report,
|
2022-03-16 00:56:09 +01:00
|
|
|
unwindstack::AndroidUnwinder* unwinder) const {
|
2021-03-09 01:53:54 +01:00
|
|
|
MemoryError* memory_error = cause->mutable_memory_error();
|
|
|
|
HeapObject* heap_object = memory_error->mutable_heap();
|
|
|
|
|
|
|
|
memory_error->set_tool(MemoryError_Tool_SCUDO);
|
|
|
|
switch (report->error_type) {
|
|
|
|
case USE_AFTER_FREE:
|
|
|
|
memory_error->set_type(MemoryError_Type_USE_AFTER_FREE);
|
|
|
|
break;
|
|
|
|
case BUFFER_OVERFLOW:
|
|
|
|
memory_error->set_type(MemoryError_Type_BUFFER_OVERFLOW);
|
|
|
|
break;
|
|
|
|
case BUFFER_UNDERFLOW:
|
|
|
|
memory_error->set_type(MemoryError_Type_BUFFER_UNDERFLOW);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
memory_error->set_type(MemoryError_Type_UNKNOWN);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
heap_object->set_address(report->allocation_address);
|
|
|
|
heap_object->set_size(report->allocation_size);
|
|
|
|
|
|
|
|
heap_object->set_allocation_tid(report->allocation_tid);
|
|
|
|
for (size_t i = 0; i < arraysize(report->allocation_trace) && report->allocation_trace[i]; ++i) {
|
|
|
|
unwindstack::FrameData frame_data = unwinder->BuildFrameFromPcOnly(report->allocation_trace[i]);
|
|
|
|
BacktraceFrame* f = heap_object->add_allocation_backtrace();
|
2021-11-11 02:25:11 +01:00
|
|
|
fill_in_backtrace_frame(f, frame_data);
|
2021-03-09 01:53:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
heap_object->set_deallocation_tid(report->deallocation_tid);
|
|
|
|
for (size_t i = 0; i < arraysize(report->deallocation_trace) && report->deallocation_trace[i];
|
|
|
|
++i) {
|
|
|
|
unwindstack::FrameData frame_data =
|
|
|
|
unwinder->BuildFrameFromPcOnly(report->deallocation_trace[i]);
|
|
|
|
BacktraceFrame* f = heap_object->add_deallocation_backtrace();
|
2021-11-11 02:25:11 +01:00
|
|
|
fill_in_backtrace_frame(f, frame_data);
|
2021-03-09 01:53:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
set_human_readable_cause(cause, untagged_fault_addr_);
|
|
|
|
}
|
|
|
|
|
2022-03-16 00:56:09 +01:00
|
|
|
void ScudoCrashData::AddCauseProtos(Tombstone* tombstone,
|
|
|
|
unwindstack::AndroidUnwinder* unwinder) const {
|
2021-03-09 01:53:54 +01:00
|
|
|
size_t report_num = 0;
|
|
|
|
while (report_num < sizeof(error_info_.reports) / sizeof(error_info_.reports[0]) &&
|
|
|
|
error_info_.reports[report_num].error_type != UNKNOWN) {
|
|
|
|
FillInCause(tombstone->add_causes(), &error_info_.reports[report_num++], unwinder);
|
|
|
|
}
|
|
|
|
}
|