2022-09-06 19:14:07 -04:00
|
|
|
// Copyright 2016 The Crashpad Authors
|
2016-01-14 12:50:22 -08:00
|
|
|
//
|
|
|
|
// 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 "snapshot/capture_memory.h"
|
|
|
|
|
|
|
|
#include <stdint.h>
|
2022-05-16 15:38:37 -07:00
|
|
|
#include <windows.h>
|
|
|
|
|
|
|
|
// dbghelp must be after windows.h.
|
|
|
|
#include <dbghelp.h>
|
2016-01-14 12:50:22 -08:00
|
|
|
|
2022-02-28 20:57:19 -08:00
|
|
|
#include <iterator>
|
2016-01-14 12:50:22 -08:00
|
|
|
#include <limits>
|
|
|
|
|
Replace std::unique_ptr<T> with HeapArray
Bug: crashpad: 326459659,326458942,326459376,326459390,326459417,326458979,326459333,326459016,326458338,326458738,326459156,326459512,326458694
Change-Id: I04724530cbef50a8d3c18f306d16c0bbf3b0815b
Reviewed-on: https://chromium-review.googlesource.com/c/crashpad/crashpad/+/5512394
Reviewed-by: Mark Mentovai <mark@chromium.org>
Commit-Queue: Arthur Wang <wuwang@chromium.org>
2024-05-09 12:39:17 -07:00
|
|
|
#include "base/containers/heap_array.h"
|
2020-06-18 15:35:28 +02:00
|
|
|
#include "base/logging.h"
|
2024-05-10 09:13:11 -07:00
|
|
|
#include "build/build_config.h"
|
2016-01-14 12:50:22 -08:00
|
|
|
#include "snapshot/memory_snapshot.h"
|
|
|
|
|
|
|
|
namespace crashpad {
|
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
void MaybeCaptureMemoryAround(CaptureMemory::Delegate* delegate,
|
|
|
|
uint64_t address) {
|
2017-07-25 19:15:48 -04:00
|
|
|
constexpr uint64_t non_address_offset = 0x10000;
|
2016-01-14 12:50:22 -08:00
|
|
|
if (address < non_address_offset)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const uint64_t max_address = delegate->Is64Bit() ?
|
|
|
|
std::numeric_limits<uint64_t>::max() :
|
|
|
|
std::numeric_limits<uint32_t>::max();
|
|
|
|
if (address > max_address - non_address_offset)
|
|
|
|
return;
|
|
|
|
|
2017-07-25 19:15:48 -04:00
|
|
|
constexpr uint64_t kRegisterByteOffset = 128;
|
2016-01-14 12:50:22 -08:00
|
|
|
const uint64_t target = address - kRegisterByteOffset;
|
2017-07-25 19:15:48 -04:00
|
|
|
constexpr uint64_t size = 512;
|
2016-01-14 12:50:22 -08:00
|
|
|
static_assert(kRegisterByteOffset <= size / 2,
|
|
|
|
"negative offset too large");
|
|
|
|
auto ranges =
|
|
|
|
delegate->GetReadableRanges(CheckedRange<uint64_t>(target, size));
|
|
|
|
for (const auto& range : ranges) {
|
|
|
|
delegate->AddNewMemorySnapshot(range);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
void CaptureAtPointersInRange(uint8_t* buffer,
|
|
|
|
uint64_t buffer_size,
|
|
|
|
CaptureMemory::Delegate* delegate) {
|
|
|
|
for (uint64_t address_offset = 0; address_offset < buffer_size;
|
|
|
|
address_offset += sizeof(T)) {
|
|
|
|
uint64_t target_address = *reinterpret_cast<T*>(&buffer[address_offset]);
|
|
|
|
MaybeCaptureMemoryAround(delegate, target_address);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
// static
|
|
|
|
void CaptureMemory::PointedToByContext(const CPUContext& context,
|
|
|
|
Delegate* delegate) {
|
|
|
|
#if defined(ARCH_CPU_X86_FAMILY)
|
|
|
|
if (context.architecture == kCPUArchitectureX86_64) {
|
2022-08-04 12:53:33 -07:00
|
|
|
MaybeCaptureMemoryAround(delegate, context.x86_64->rip);
|
2016-01-14 12:50:22 -08:00
|
|
|
MaybeCaptureMemoryAround(delegate, context.x86_64->rax);
|
|
|
|
MaybeCaptureMemoryAround(delegate, context.x86_64->rbx);
|
|
|
|
MaybeCaptureMemoryAround(delegate, context.x86_64->rcx);
|
|
|
|
MaybeCaptureMemoryAround(delegate, context.x86_64->rdx);
|
|
|
|
MaybeCaptureMemoryAround(delegate, context.x86_64->rdi);
|
|
|
|
MaybeCaptureMemoryAround(delegate, context.x86_64->rsi);
|
|
|
|
MaybeCaptureMemoryAround(delegate, context.x86_64->rbp);
|
|
|
|
MaybeCaptureMemoryAround(delegate, context.x86_64->r8);
|
|
|
|
MaybeCaptureMemoryAround(delegate, context.x86_64->r9);
|
|
|
|
MaybeCaptureMemoryAround(delegate, context.x86_64->r10);
|
|
|
|
MaybeCaptureMemoryAround(delegate, context.x86_64->r11);
|
|
|
|
MaybeCaptureMemoryAround(delegate, context.x86_64->r12);
|
|
|
|
MaybeCaptureMemoryAround(delegate, context.x86_64->r13);
|
|
|
|
MaybeCaptureMemoryAround(delegate, context.x86_64->r14);
|
|
|
|
MaybeCaptureMemoryAround(delegate, context.x86_64->r15);
|
2022-05-16 16:16:07 -07:00
|
|
|
// Note: Shadow stack region is directly captured.
|
2016-01-14 12:50:22 -08:00
|
|
|
} else {
|
2022-08-04 12:53:33 -07:00
|
|
|
MaybeCaptureMemoryAround(delegate, context.x86->eip);
|
2016-01-14 12:50:22 -08:00
|
|
|
MaybeCaptureMemoryAround(delegate, context.x86->eax);
|
|
|
|
MaybeCaptureMemoryAround(delegate, context.x86->ebx);
|
|
|
|
MaybeCaptureMemoryAround(delegate, context.x86->ecx);
|
|
|
|
MaybeCaptureMemoryAround(delegate, context.x86->edx);
|
|
|
|
MaybeCaptureMemoryAround(delegate, context.x86->edi);
|
|
|
|
MaybeCaptureMemoryAround(delegate, context.x86->esi);
|
|
|
|
MaybeCaptureMemoryAround(delegate, context.x86->ebp);
|
|
|
|
}
|
2018-02-19 10:55:17 -08:00
|
|
|
#elif defined(ARCH_CPU_ARM_FAMILY)
|
|
|
|
if (context.architecture == kCPUArchitectureARM64) {
|
|
|
|
MaybeCaptureMemoryAround(delegate, context.arm64->pc);
|
2022-02-28 20:57:19 -08:00
|
|
|
for (size_t i = 0; i < std::size(context.arm64->regs); ++i) {
|
2018-02-19 10:55:17 -08:00
|
|
|
MaybeCaptureMemoryAround(delegate, context.arm64->regs[i]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
MaybeCaptureMemoryAround(delegate, context.arm->pc);
|
2022-02-28 20:57:19 -08:00
|
|
|
for (size_t i = 0; i < std::size(context.arm->regs); ++i) {
|
2018-02-19 10:55:17 -08:00
|
|
|
MaybeCaptureMemoryAround(delegate, context.arm->regs[i]);
|
|
|
|
}
|
|
|
|
}
|
2018-07-10 11:17:22 +02:00
|
|
|
#elif defined(ARCH_CPU_MIPS_FAMILY)
|
2022-02-28 20:57:19 -08:00
|
|
|
for (size_t i = 0; i < std::size(context.mipsel->regs); ++i) {
|
2018-07-10 11:17:22 +02:00
|
|
|
MaybeCaptureMemoryAround(delegate, context.mipsel->regs[i]);
|
|
|
|
}
|
2023-06-08 21:08:54 +00:00
|
|
|
#elif defined(ARCH_CPU_RISCV64)
|
|
|
|
MaybeCaptureMemoryAround(delegate, context.riscv64->pc);
|
|
|
|
for (size_t i = 0; i < std::size(context.riscv64->regs); ++i) {
|
|
|
|
MaybeCaptureMemoryAround(delegate, context.riscv64->regs[i]);
|
|
|
|
}
|
2016-01-14 12:50:22 -08:00
|
|
|
#else
|
2018-02-19 10:55:17 -08:00
|
|
|
#error Port.
|
2016-01-14 12:50:22 -08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
void CaptureMemory::PointedToByMemoryRange(const MemorySnapshot& memory,
|
|
|
|
Delegate* delegate) {
|
|
|
|
if (memory.Size() == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const size_t alignment =
|
|
|
|
delegate->Is64Bit() ? sizeof(uint64_t) : sizeof(uint32_t);
|
|
|
|
if (memory.Address() % alignment != 0 || memory.Size() % alignment != 0) {
|
|
|
|
LOG(ERROR) << "unaligned range";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
Replace std::unique_ptr<T> with HeapArray
Bug: crashpad: 326459659,326458942,326459376,326459390,326459417,326458979,326459333,326459016,326458338,326458738,326459156,326459512,326458694
Change-Id: I04724530cbef50a8d3c18f306d16c0bbf3b0815b
Reviewed-on: https://chromium-review.googlesource.com/c/crashpad/crashpad/+/5512394
Reviewed-by: Mark Mentovai <mark@chromium.org>
Commit-Queue: Arthur Wang <wuwang@chromium.org>
2024-05-09 12:39:17 -07:00
|
|
|
auto buffer = base::HeapArray<uint8_t>::Uninit(memory.Size());
|
|
|
|
if (!delegate->ReadMemory(memory.Address(), memory.Size(), buffer.data())) {
|
2016-01-14 12:50:22 -08:00
|
|
|
LOG(ERROR) << "ReadMemory";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (delegate->Is64Bit())
|
Replace std::unique_ptr<T> with HeapArray
Bug: crashpad: 326459659,326458942,326459376,326459390,326459417,326458979,326459333,326459016,326458338,326458738,326459156,326459512,326458694
Change-Id: I04724530cbef50a8d3c18f306d16c0bbf3b0815b
Reviewed-on: https://chromium-review.googlesource.com/c/crashpad/crashpad/+/5512394
Reviewed-by: Mark Mentovai <mark@chromium.org>
Commit-Queue: Arthur Wang <wuwang@chromium.org>
2024-05-09 12:39:17 -07:00
|
|
|
CaptureAtPointersInRange<uint64_t>(buffer.data(), buffer.size(), delegate);
|
2016-01-14 12:50:22 -08:00
|
|
|
else
|
Replace std::unique_ptr<T> with HeapArray
Bug: crashpad: 326459659,326458942,326459376,326459390,326459417,326458979,326459333,326459016,326458338,326458738,326459156,326459512,326458694
Change-Id: I04724530cbef50a8d3c18f306d16c0bbf3b0815b
Reviewed-on: https://chromium-review.googlesource.com/c/crashpad/crashpad/+/5512394
Reviewed-by: Mark Mentovai <mark@chromium.org>
Commit-Queue: Arthur Wang <wuwang@chromium.org>
2024-05-09 12:39:17 -07:00
|
|
|
CaptureAtPointersInRange<uint32_t>(buffer.data(), buffer.size(), delegate);
|
2016-01-14 12:50:22 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace crashpad
|