fatal: prevent crash on fatal from currently-debugged process

This commit is contained in:
Michael Scire 2020-12-09 06:45:19 -08:00
parent bcc7eed037
commit 16e2f46aed

View file

@ -170,17 +170,18 @@ namespace ams::fatal::srv {
void TryCollectDebugInformation(ThrowContext *ctx, os::ProcessId process_id) { void TryCollectDebugInformation(ThrowContext *ctx, os::ProcessId process_id) {
/* Try to debug the process. This may fail, if we called into ourself. */ /* Try to debug the process. This may fail, if we called into ourself. */
os::ManagedHandle debug_handle; Handle debug_handle;
if (R_FAILED(svcDebugActiveProcess(debug_handle.GetPointer(), static_cast<u64>(process_id)))) { if (R_FAILED(svcDebugActiveProcess(std::addressof(debug_handle), static_cast<u64>(process_id)))) {
return; return;
} }
ON_SCOPE_EXIT { R_ABORT_UNLESS(svc::CloseHandle(debug_handle)); };
/* First things first, check if process is 64 bits, and get list of thread infos. */ /* First things first, check if process is 64 bits, and get list of thread infos. */
std::unordered_map<u64, u64> thread_id_to_tls; std::unordered_map<u64, u64> thread_id_to_tls;
{ {
bool got_create_process = false; bool got_create_process = false;
svc::DebugEventInfo d; svc::DebugEventInfo d;
while (R_SUCCEEDED(svcGetDebugEvent(reinterpret_cast<u8 *>(&d), debug_handle.Get()))) { while (R_SUCCEEDED(svcGetDebugEvent(reinterpret_cast<u8 *>(&d), debug_handle))) {
switch (d.type) { switch (d.type) {
case svc::DebugEvent_CreateProcess: case svc::DebugEvent_CreateProcess:
ctx->cpu_ctx.architecture = (d.info.create_process.flags & 1) ? CpuContext::Architecture_Aarch64 : CpuContext::Architecture_Aarch32; ctx->cpu_ctx.architecture = (d.info.create_process.flags & 1) ? CpuContext::Architecture_Aarch64 : CpuContext::Architecture_Aarch32;
@ -216,7 +217,7 @@ namespace ams::fatal::srv {
/* We start by trying to get a list of threads. */ /* We start by trying to get a list of threads. */
s32 thread_count; s32 thread_count;
u64 thread_ids[0x60]; u64 thread_ids[0x60];
if (R_FAILED(svc::GetThreadList(&thread_count, thread_ids, 0x60, debug_handle.Get()))) { if (R_FAILED(svc::GetThreadList(&thread_count, thread_ids, 0x60, debug_handle))) {
return; return;
} }
@ -227,7 +228,7 @@ namespace ams::fatal::srv {
continue; continue;
} }
if (IsThreadFatalCaller(ctx->result, debug_handle.Get(), cur_thread_id, thread_id_to_tls[cur_thread_id], &thread_ctx)) { if (IsThreadFatalCaller(ctx->result, debug_handle, cur_thread_id, thread_id_to_tls[cur_thread_id], &thread_ctx)) {
thread_id = cur_thread_id; thread_id = cur_thread_id;
thread_tls = thread_id_to_tls[thread_id]; thread_tls = thread_id_to_tls[thread_id];
found_fatal_caller = true; found_fatal_caller = true;
@ -238,7 +239,7 @@ namespace ams::fatal::srv {
return; return;
} }
} }
if (R_FAILED(svcGetDebugThreadContext(&thread_ctx, debug_handle.Get(), thread_id, svc::ThreadContextFlag_All))) { if (R_FAILED(svcGetDebugThreadContext(&thread_ctx, debug_handle, thread_id, svc::ThreadContextFlag_All))) {
return; return;
} }
@ -259,7 +260,7 @@ namespace ams::fatal::srv {
/* Read a new frame. */ /* Read a new frame. */
StackFrame cur_frame = {}; StackFrame cur_frame = {};
if (R_FAILED(svcReadDebugProcessMemory(&cur_frame, debug_handle.Get(), cur_fp, sizeof(StackFrame)))) { if (R_FAILED(svcReadDebugProcessMemory(&cur_frame, debug_handle, cur_fp, sizeof(StackFrame)))) {
break; break;
} }
@ -271,7 +272,7 @@ namespace ams::fatal::srv {
/* Try to read up to 0x100 of stack. */ /* Try to read up to 0x100 of stack. */
ctx->stack_dump_base = 0; ctx->stack_dump_base = 0;
for (size_t sz = 0x100; sz > 0; sz -= 0x10) { for (size_t sz = 0x100; sz > 0; sz -= 0x10) {
if (R_SUCCEEDED(svcReadDebugProcessMemory(ctx->stack_dump, debug_handle.Get(), thread_ctx.sp, sz))) { if (R_SUCCEEDED(svcReadDebugProcessMemory(ctx->stack_dump, debug_handle, thread_ctx.sp, sz))) {
ctx->stack_dump_base = thread_ctx.sp; ctx->stack_dump_base = thread_ctx.sp;
ctx->stack_dump_size = sz; ctx->stack_dump_size = sz;
break; break;
@ -279,7 +280,7 @@ namespace ams::fatal::srv {
} }
/* Try to read the first 0x100 of TLS. */ /* Try to read the first 0x100 of TLS. */
if (R_SUCCEEDED(svcReadDebugProcessMemory(ctx->tls_dump, debug_handle.Get(), thread_tls, sizeof(ctx->tls_dump)))) { if (R_SUCCEEDED(svcReadDebugProcessMemory(ctx->tls_dump, debug_handle, thread_tls, sizeof(ctx->tls_dump)))) {
ctx->tls_address = thread_tls; ctx->tls_address = thread_tls;
} else { } else {
ctx->tls_address = 0; ctx->tls_address = 0;
@ -287,7 +288,7 @@ namespace ams::fatal::srv {
} }
/* Parse the base address. */ /* Parse the base address. */
ctx->cpu_ctx.aarch64_ctx.SetBaseAddress(GetBaseAddress(ctx, &thread_ctx, debug_handle.Get())); ctx->cpu_ctx.aarch64_ctx.SetBaseAddress(GetBaseAddress(ctx, &thread_ctx, debug_handle));
} }
} }