From 16e2f46aede4294b491710f03bf466892aa2c36e Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Wed, 9 Dec 2020 06:45:19 -0800 Subject: [PATCH] fatal: prevent crash on fatal from currently-debugged process --- stratosphere/fatal/source/fatal_debug.cpp | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/stratosphere/fatal/source/fatal_debug.cpp b/stratosphere/fatal/source/fatal_debug.cpp index 6a45f589a..b19fca621 100644 --- a/stratosphere/fatal/source/fatal_debug.cpp +++ b/stratosphere/fatal/source/fatal_debug.cpp @@ -170,17 +170,18 @@ namespace ams::fatal::srv { void TryCollectDebugInformation(ThrowContext *ctx, os::ProcessId process_id) { /* Try to debug the process. This may fail, if we called into ourself. */ - os::ManagedHandle debug_handle; - if (R_FAILED(svcDebugActiveProcess(debug_handle.GetPointer(), static_cast(process_id)))) { + Handle debug_handle; + if (R_FAILED(svcDebugActiveProcess(std::addressof(debug_handle), static_cast(process_id)))) { 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. */ std::unordered_map thread_id_to_tls; { bool got_create_process = false; svc::DebugEventInfo d; - while (R_SUCCEEDED(svcGetDebugEvent(reinterpret_cast(&d), debug_handle.Get()))) { + while (R_SUCCEEDED(svcGetDebugEvent(reinterpret_cast(&d), debug_handle))) { switch (d.type) { case svc::DebugEvent_CreateProcess: 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. */ s32 thread_count; 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; } @@ -227,7 +228,7 @@ namespace ams::fatal::srv { 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_tls = thread_id_to_tls[thread_id]; found_fatal_caller = true; @@ -238,7 +239,7 @@ namespace ams::fatal::srv { 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; } @@ -259,7 +260,7 @@ namespace ams::fatal::srv { /* Read a new 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; } @@ -271,7 +272,7 @@ namespace ams::fatal::srv { /* Try to read up to 0x100 of stack. */ ctx->stack_dump_base = 0; 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_size = sz; break; @@ -279,7 +280,7 @@ namespace ams::fatal::srv { } /* 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; } else { ctx->tls_address = 0; @@ -287,7 +288,7 @@ namespace ams::fatal::srv { } /* 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)); } }