From aed472775c1942f9251e94c560dc7245e89491ba Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Sat, 6 Apr 2019 15:00:40 -0700 Subject: [PATCH 1/2] fatal: fix invalid struct access, cap stack trace size. --- stratosphere/fatal/source/fatal_throw.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/stratosphere/fatal/source/fatal_throw.cpp b/stratosphere/fatal/source/fatal_throw.cpp index eac1ef23f..ac44b81d9 100644 --- a/stratosphere/fatal/source/fatal_throw.cpp +++ b/stratosphere/fatal/source/fatal_throw.cpp @@ -50,6 +50,13 @@ Result ThrowFatalImpl(u32 error, u64 pid, FatalType policy, FatalCpuContext *cpu for (u32 i = 0; i < NumAarch64Gprs; i++) { ctx.has_gprs[i] = true; } + /* Cap the stack trace size at a sane limit. */ + /* TODO: Better to set to zero, in order to manually collect debug info ourselves instead? */ + if (cpu_ctx->is_aarch32) { + ctx.cpu_ctx.aarch32_ctx.stack_trace_size = std::max(ctx.cpu_ctx.aarch32_ctx.stack_trace_size, static_cast(Aarch32CpuContext::MaxStackTraceDepth)); + } else { + ctx.cpu_ctx.aarch64_ctx.stack_trace_size = std::max(ctx.cpu_ctx.aarch64_ctx.stack_trace_size, static_cast(Aarch64CpuContext::MaxStackTraceDepth)); + } } else { std::memset(&ctx.cpu_ctx, 0, sizeof(ctx.cpu_ctx)); cpu_ctx = &ctx.cpu_ctx; @@ -70,7 +77,7 @@ Result ThrowFatalImpl(u32 error, u64 pid, FatalType policy, FatalCpuContext *cpu /* Atmosphere extension: automatic debug info collection. */ if (GetRuntimeFirmwareVersion() >= FirmwareVersion_200 && !ctx.is_creport) { - if ((cpu_ctx->is_aarch32 && cpu_ctx->aarch32_ctx.stack_trace_size == 0) || (!cpu_ctx->is_aarch32 && cpu_ctx->aarch32_ctx.stack_trace_size == 0)) { + if ((cpu_ctx->is_aarch32 && cpu_ctx->aarch32_ctx.stack_trace_size == 0) || (!cpu_ctx->is_aarch32 && cpu_ctx->aarch64_ctx.stack_trace_size == 0)) { TryCollectDebugInformation(&ctx, pid); } } From 6b41a7e2e156ecd299594ea73888d4f629de822c Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Sat, 6 Apr 2019 15:02:48 -0700 Subject: [PATCH 2/2] fatal: unconditionally reassign cpu_ctx ptr --- stratosphere/fatal/source/fatal_throw.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/stratosphere/fatal/source/fatal_throw.cpp b/stratosphere/fatal/source/fatal_throw.cpp index ac44b81d9..b36396dd0 100644 --- a/stratosphere/fatal/source/fatal_throw.cpp +++ b/stratosphere/fatal/source/fatal_throw.cpp @@ -59,8 +59,9 @@ Result ThrowFatalImpl(u32 error, u64 pid, FatalType policy, FatalCpuContext *cpu } } else { std::memset(&ctx.cpu_ctx, 0, sizeof(ctx.cpu_ctx)); - cpu_ctx = &ctx.cpu_ctx; } + /* Reassign this unconditionally, for convenience. */ + cpu_ctx = &ctx.cpu_ctx; /* Get config. */ const FatalConfig *config = GetFatalConfig();