2018-02-15 13:21:00 -08:00
|
|
|
// Copyright 2018 The Crashpad Authors. All rights reserved.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2018-02-22 12:12:26 -08:00
|
|
|
#include "snapshot/fuchsia/process_reader_fuchsia.h"
|
2018-02-15 13:21:00 -08:00
|
|
|
|
|
|
|
#include <link.h>
|
|
|
|
#include <zircon/syscalls.h>
|
|
|
|
|
2018-02-22 11:06:35 -08:00
|
|
|
#include "base/fuchsia/fuchsia_logging.h"
|
|
|
|
#include "base/fuchsia/scoped_zx_handle.h"
|
|
|
|
#include "base/logging.h"
|
2018-04-13 09:42:19 -07:00
|
|
|
#include "util/fuchsia/koid_utilities.h"
|
2018-02-22 11:06:35 -08:00
|
|
|
|
2018-02-15 13:21:00 -08:00
|
|
|
namespace crashpad {
|
|
|
|
|
2018-04-30 12:57:03 -07:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Based on the thread's SP and the process's memory map, attempts to figure out
|
|
|
|
// the stack regions for the thread. Fuchsia's C ABI specifies
|
|
|
|
// https://fuchsia.googlesource.com/zircon/+/master/docs/safestack.md so the
|
|
|
|
// callstack and locals-that-have-their-address-taken are in two different
|
|
|
|
// stacks.
|
|
|
|
void GetStackRegions(
|
|
|
|
const zx_thread_state_general_regs_t& regs,
|
|
|
|
const MemoryMapFuchsia& memory_map,
|
|
|
|
std::vector<CheckedRange<zx_vaddr_t, size_t>>* stack_regions) {
|
|
|
|
stack_regions->clear();
|
|
|
|
|
|
|
|
uint64_t sp;
|
|
|
|
#if defined(ARCH_CPU_X86_64)
|
|
|
|
sp = regs.rsp;
|
|
|
|
#elif defined(ARCH_CPU_ARM64)
|
|
|
|
sp = regs.sp;
|
|
|
|
#else
|
|
|
|
#error Port
|
|
|
|
#endif
|
|
|
|
|
|
|
|
zx_info_maps_t range_with_sp;
|
|
|
|
if (!memory_map.FindMappingForAddress(sp, &range_with_sp)) {
|
|
|
|
LOG(ERROR) << "stack pointer not found in mapping";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (range_with_sp.type != ZX_INFO_MAPS_TYPE_MAPPING) {
|
|
|
|
LOG(ERROR) << "stack range has unexpected type, continuing anyway";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (range_with_sp.u.mapping.mmu_flags & ZX_VM_FLAG_PERM_EXECUTE) {
|
|
|
|
LOG(ERROR)
|
|
|
|
<< "stack range is unexpectedly marked executable, continuing anyway";
|
|
|
|
}
|
|
|
|
|
|
|
|
stack_regions->push_back(
|
|
|
|
CheckedRange<zx_vaddr_t, size_t>(range_with_sp.base, range_with_sp.size));
|
|
|
|
|
|
|
|
// TODO(scottmg): https://crashpad.chromium.org/bug/196, once the retrievable
|
|
|
|
// registers include FS and similar for ARM, retrieve the region for the
|
|
|
|
// unsafe part of the stack too.
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2018-02-22 12:12:26 -08:00
|
|
|
ProcessReaderFuchsia::Module::Module() = default;
|
2018-02-15 13:21:00 -08:00
|
|
|
|
2018-02-22 12:12:26 -08:00
|
|
|
ProcessReaderFuchsia::Module::~Module() = default;
|
2018-02-15 13:21:00 -08:00
|
|
|
|
2018-02-22 12:12:26 -08:00
|
|
|
ProcessReaderFuchsia::Thread::Thread() = default;
|
2018-02-22 11:06:35 -08:00
|
|
|
|
2018-02-22 12:12:26 -08:00
|
|
|
ProcessReaderFuchsia::Thread::~Thread() = default;
|
2018-02-22 11:06:35 -08:00
|
|
|
|
2018-02-22 12:12:26 -08:00
|
|
|
ProcessReaderFuchsia::ProcessReaderFuchsia() = default;
|
2018-02-15 13:21:00 -08:00
|
|
|
|
2018-02-22 12:12:26 -08:00
|
|
|
ProcessReaderFuchsia::~ProcessReaderFuchsia() = default;
|
2018-02-15 13:21:00 -08:00
|
|
|
|
2018-02-22 12:12:26 -08:00
|
|
|
bool ProcessReaderFuchsia::Initialize(zx_handle_t process) {
|
2018-02-15 13:21:00 -08:00
|
|
|
INITIALIZATION_STATE_SET_INITIALIZING(initialized_);
|
|
|
|
|
|
|
|
process_ = process;
|
|
|
|
|
|
|
|
process_memory_.reset(new ProcessMemoryFuchsia());
|
|
|
|
process_memory_->Initialize(process_);
|
|
|
|
|
2018-04-30 12:57:03 -07:00
|
|
|
memory_map_.Initialize(process_);
|
|
|
|
|
2018-02-15 13:21:00 -08:00
|
|
|
INITIALIZATION_STATE_SET_VALID(initialized_);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-02-22 12:12:26 -08:00
|
|
|
const std::vector<ProcessReaderFuchsia::Module>&
|
|
|
|
ProcessReaderFuchsia::Modules() {
|
2018-02-15 13:21:00 -08:00
|
|
|
INITIALIZATION_STATE_DCHECK_VALID(initialized_);
|
|
|
|
|
|
|
|
if (!initialized_modules_) {
|
|
|
|
InitializeModules();
|
|
|
|
}
|
|
|
|
|
|
|
|
return modules_;
|
|
|
|
}
|
|
|
|
|
2018-02-22 12:12:26 -08:00
|
|
|
const std::vector<ProcessReaderFuchsia::Thread>&
|
|
|
|
ProcessReaderFuchsia::Threads() {
|
2018-02-22 11:06:35 -08:00
|
|
|
INITIALIZATION_STATE_DCHECK_VALID(initialized_);
|
|
|
|
|
|
|
|
if (!initialized_threads_) {
|
|
|
|
InitializeThreads();
|
|
|
|
}
|
|
|
|
|
|
|
|
return threads_;
|
|
|
|
}
|
|
|
|
|
2018-02-22 12:12:26 -08:00
|
|
|
void ProcessReaderFuchsia::InitializeModules() {
|
2018-02-15 13:21:00 -08:00
|
|
|
DCHECK(!initialized_modules_);
|
|
|
|
DCHECK(modules_.empty());
|
|
|
|
|
|
|
|
initialized_modules_ = true;
|
|
|
|
|
|
|
|
// TODO(scottmg): <inspector/inspector.h> does some of this, but doesn't
|
|
|
|
// expose any of the data that's necessary to fill out a Module after it
|
|
|
|
// retrieves (some of) the data into internal structures. It may be worth
|
|
|
|
// trying to refactor/upstream some of this into Fuchsia.
|
|
|
|
|
|
|
|
std::string app_name("app:");
|
|
|
|
{
|
|
|
|
char name[ZX_MAX_NAME_LEN];
|
|
|
|
zx_status_t status =
|
|
|
|
zx_object_get_property(process_, ZX_PROP_NAME, name, sizeof(name));
|
|
|
|
if (status != ZX_OK) {
|
|
|
|
LOG(ERROR) << "zx_object_get_property ZX_PROP_NAME";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
app_name += name;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Starting from the ld.so's _dl_debug_addr, read the link_map structure and
|
|
|
|
// walk the list to fill out modules_.
|
|
|
|
|
|
|
|
uintptr_t debug_address;
|
|
|
|
zx_status_t status = zx_object_get_property(process_,
|
|
|
|
ZX_PROP_PROCESS_DEBUG_ADDR,
|
|
|
|
&debug_address,
|
|
|
|
sizeof(debug_address));
|
|
|
|
if (status != ZX_OK || debug_address == 0) {
|
|
|
|
LOG(ERROR) << "zx_object_get_property ZX_PROP_PROCESS_DEBUG_ADDR";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr auto k_r_debug_map_offset = offsetof(r_debug, r_map);
|
|
|
|
uintptr_t map;
|
|
|
|
if (!process_memory_->Read(
|
|
|
|
debug_address + k_r_debug_map_offset, sizeof(map), &map)) {
|
|
|
|
LOG(ERROR) << "read link_map";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
constexpr int kMaxDso = 1000; // Stop after an unreasonably large number.
|
|
|
|
while (map != 0) {
|
|
|
|
if (++i >= kMaxDso) {
|
|
|
|
LOG(ERROR) << "possibly circular dso list, terminating";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr auto k_link_map_addr_offset = offsetof(link_map, l_addr);
|
|
|
|
zx_vaddr_t base;
|
|
|
|
if (!process_memory_->Read(
|
|
|
|
map + k_link_map_addr_offset, sizeof(base), &base)) {
|
|
|
|
LOG(ERROR) << "Read base";
|
|
|
|
// Could theoretically continue here, but realistically if any part of
|
|
|
|
// link_map fails to read, things are looking bad, so just abort.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr auto k_link_map_next_offset = offsetof(link_map, l_next);
|
|
|
|
zx_vaddr_t next;
|
|
|
|
if (!process_memory_->Read(
|
|
|
|
map + k_link_map_next_offset, sizeof(next), &next)) {
|
|
|
|
LOG(ERROR) << "Read next";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr auto k_link_map_name_offset = offsetof(link_map, l_name);
|
|
|
|
zx_vaddr_t name_address;
|
|
|
|
if (!process_memory_->Read(map + k_link_map_name_offset,
|
|
|
|
sizeof(name_address),
|
|
|
|
&name_address)) {
|
|
|
|
LOG(ERROR) << "Read name address";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string dsoname;
|
|
|
|
if (!process_memory_->ReadCString(name_address, &dsoname)) {
|
|
|
|
// In this case, it could be reasonable to continue on to the next module
|
|
|
|
// as this data isn't strictly in the link_map.
|
|
|
|
LOG(ERROR) << "ReadCString name";
|
|
|
|
}
|
|
|
|
|
|
|
|
Module module;
|
2018-02-20 16:10:33 -08:00
|
|
|
if (dsoname.empty()) {
|
|
|
|
module.name = app_name;
|
|
|
|
module.type = ModuleSnapshot::kModuleTypeExecutable;
|
|
|
|
} else {
|
|
|
|
module.name = dsoname;
|
|
|
|
// TODO(scottmg): Handle kModuleTypeDynamicLoader.
|
|
|
|
module.type = ModuleSnapshot::kModuleTypeSharedLibrary;
|
|
|
|
}
|
2018-02-15 13:21:00 -08:00
|
|
|
|
|
|
|
std::unique_ptr<ElfImageReader> reader(new ElfImageReader());
|
|
|
|
|
|
|
|
std::unique_ptr<ProcessMemoryRange> process_memory_range(
|
|
|
|
new ProcessMemoryRange());
|
|
|
|
// TODO(scottmg): Could this be limited range?
|
|
|
|
process_memory_range->Initialize(process_memory_.get(), true);
|
|
|
|
process_memory_ranges_.push_back(std::move(process_memory_range));
|
|
|
|
|
|
|
|
reader->Initialize(*process_memory_ranges_.back(), base);
|
|
|
|
module.reader = reader.get();
|
|
|
|
module_readers_.push_back(std::move(reader));
|
|
|
|
modules_.push_back(module);
|
|
|
|
|
|
|
|
map = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-22 12:12:26 -08:00
|
|
|
void ProcessReaderFuchsia::InitializeThreads() {
|
2018-02-22 11:06:35 -08:00
|
|
|
DCHECK(!initialized_threads_);
|
|
|
|
DCHECK(threads_.empty());
|
|
|
|
|
|
|
|
initialized_threads_ = true;
|
|
|
|
|
2018-04-13 09:42:19 -07:00
|
|
|
std::vector<zx_koid_t> thread_koids =
|
|
|
|
GetChildKoids(process_, ZX_INFO_PROCESS_THREADS);
|
|
|
|
std::vector<base::ScopedZxHandle> thread_handles =
|
|
|
|
GetHandlesForChildKoids(process_, thread_koids);
|
|
|
|
DCHECK_EQ(thread_koids.size(), thread_handles.size());
|
2018-02-22 11:06:35 -08:00
|
|
|
|
2018-04-13 09:42:19 -07:00
|
|
|
for (size_t i = 0; i < thread_handles.size(); ++i) {
|
2018-02-22 11:06:35 -08:00
|
|
|
Thread thread;
|
2018-04-13 09:42:19 -07:00
|
|
|
thread.id = thread_koids[i];
|
|
|
|
|
|
|
|
if (thread_handles[i].is_valid()) {
|
|
|
|
char name[ZX_MAX_NAME_LEN] = {0};
|
|
|
|
zx_status_t status = zx_object_get_property(
|
|
|
|
thread_handles[i].get(), ZX_PROP_NAME, &name, sizeof(name));
|
|
|
|
if (status != ZX_OK) {
|
|
|
|
ZX_LOG(WARNING, status) << "zx_object_get_property ZX_PROP_NAME";
|
|
|
|
} else {
|
|
|
|
thread.name.assign(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
zx_info_thread_t thread_info;
|
|
|
|
status = zx_object_get_info(thread_handles[i].get(),
|
|
|
|
ZX_INFO_THREAD,
|
|
|
|
&thread_info,
|
|
|
|
sizeof(thread_info),
|
|
|
|
nullptr,
|
|
|
|
nullptr);
|
|
|
|
if (status != ZX_OK) {
|
|
|
|
ZX_LOG(WARNING, status) << "zx_object_get_info ZX_INFO_THREAD";
|
|
|
|
} else {
|
|
|
|
thread.state = thread_info.state;
|
|
|
|
}
|
2018-02-22 11:06:35 -08:00
|
|
|
|
2018-04-13 11:47:26 -07:00
|
|
|
zx_thread_state_general_regs_t regs;
|
|
|
|
status = zx_thread_read_state(thread_handles[i].get(),
|
|
|
|
ZX_THREAD_STATE_GENERAL_REGS,
|
|
|
|
®s,
|
|
|
|
sizeof(regs));
|
|
|
|
if (status != ZX_OK) {
|
|
|
|
ZX_LOG(WARNING, status) << "zx_thread_read_state";
|
|
|
|
} else {
|
|
|
|
thread.general_registers = regs;
|
2018-04-30 12:57:03 -07:00
|
|
|
|
|
|
|
GetStackRegions(regs, memory_map_, &thread.stack_regions);
|
2018-04-13 11:47:26 -07:00
|
|
|
}
|
2018-04-12 14:16:10 -07:00
|
|
|
}
|
|
|
|
|
2018-02-22 11:06:35 -08:00
|
|
|
threads_.push_back(thread);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-15 13:21:00 -08:00
|
|
|
} // namespace crashpad
|