From 9382ff0939a6a69d5118993fc982f279f667c1c8 Mon Sep 17 00:00:00 2001 From: misson20000 Date: Wed, 15 Jul 2020 20:12:49 -0700 Subject: [PATCH] Add stack overflow detection to fatal error code --- fusee/fusee-primary/src/panic.c | 2 ++ .../include/stratosphere/ams/ams_types.hpp | 1 + .../source/ams/ams_environment.cpp | 33 ++++++++++++++----- 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/fusee/fusee-primary/src/panic.c b/fusee/fusee-primary/src/panic.c index ff7f7064b..bd0520efb 100644 --- a/fusee/fusee-primary/src/panic.c +++ b/fusee/fusee-primary/src/panic.c @@ -42,6 +42,8 @@ static const char *get_error_desc_str(uint32_t error_desc) { return "SError"; case 0x301: return "Bad SVC"; + case 0xFFD: + return "Stack overflow"; case 0xFFE: return "std::abort() called"; default: diff --git a/libraries/libstratosphere/include/stratosphere/ams/ams_types.hpp b/libraries/libstratosphere/include/stratosphere/ams/ams_types.hpp index b16de724b..54b69ac4c 100644 --- a/libraries/libstratosphere/include/stratosphere/ams/ams_types.hpp +++ b/libraries/libstratosphere/include/stratosphere/ams/ams_types.hpp @@ -73,6 +73,7 @@ namespace ams { static constexpr uintptr_t StdAbortMagicAddress = 0x8; static constexpr u64 StdAbortMagicValue = 0xA55AF00DDEADCAFEul; static constexpr u32 StdAbortErrorDesc = 0xFFE; + static constexpr u32 StackOverflowErrorDesc = 0xFFD; static constexpr u32 DataAbortErrorDesc = 0x101; static constexpr u32 Magic = util::FourCC<'A', 'F', 'E', '2'>::Code; diff --git a/libraries/libstratosphere/source/ams/ams_environment.cpp b/libraries/libstratosphere/source/ams/ams_environment.cpp index 7679570a1..67e60b853 100644 --- a/libraries/libstratosphere/source/ams/ams_environment.cpp +++ b/libraries/libstratosphere/source/ams/ams_environment.cpp @@ -77,11 +77,26 @@ namespace ams { ams_ctx.afsr1 = ctx->afsr1; ams_ctx.far = ctx->far.x; ams_ctx.report_identifier = armGetSystemTick(); + + /* Detect stack overflow. */ + if (ams_ctx.error_desc == FatalErrorContext::DataAbortErrorDesc) { + svc::lp64::MemoryInfo mem_info; + svc::PageInfo page_info; + + if (/* Check if stack pointer is in guard page. */ + R_SUCCEEDED(svc::QueryMemory(std::addressof(mem_info), std::addressof(page_info), ams_ctx.sp)) && + mem_info.state == svc::MemoryState_Free && + /* Check if stack pointer fell off stack. */ + R_SUCCEEDED(svc::QueryMemory(std::addressof(mem_info), std::addressof(page_info), ams_ctx.sp + 0x1000)) && + mem_info.state == svc::MemoryState_Stack) { + ams_ctx.error_desc = FatalErrorContext::StackOverflowErrorDesc; + } + } /* Grab module base. */ { - MemoryInfo mem_info; - u32 page_info; - if (R_SUCCEEDED(svcQueryMemory(&mem_info, &page_info, GetPc()))) { + svc::lp64::MemoryInfo mem_info; + svc::PageInfo page_info; + if (R_SUCCEEDED(svc::QueryMemory(std::addressof(mem_info), std::addressof(page_info), GetPc()))) { ams_ctx.module_base = mem_info.addr; } else { ams_ctx.module_base = 0; @@ -97,9 +112,9 @@ namespace ams { /* Read a new frame. */ StackFrame cur_frame; - MemoryInfo mem_info; - u32 page_info; - if (R_SUCCEEDED(svcQueryMemory(&mem_info, &page_info, cur_fp)) && (mem_info.perm & Perm_R) == Perm_R) { + svc::lp64::MemoryInfo mem_info; + svc::PageInfo page_info; + if (R_SUCCEEDED(svc::QueryMemory(std::addressof(mem_info), std::addressof(page_info), cur_fp)) && (mem_info.perm & Perm_R) == Perm_R) { std::memcpy(&cur_frame, reinterpret_cast(cur_fp), sizeof(cur_frame)); } else { break; @@ -116,9 +131,9 @@ namespace ams { /* Grab up to 0x100 of stack. */ { - MemoryInfo mem_info; - u32 page_info; - if (R_SUCCEEDED(svcQueryMemory(&mem_info, &page_info, ams_ctx.sp)) && (mem_info.perm & Perm_R) == Perm_R) { + svc::lp64::MemoryInfo mem_info; + svc::PageInfo page_info; + if (R_SUCCEEDED(svc::QueryMemory(std::addressof(mem_info), std::addressof(page_info), ams_ctx.sp)) && (mem_info.perm & Perm_R) == Perm_R) { size_t copy_size = std::min(FatalErrorContext::MaxStackDumpSize, static_cast(mem_info.addr + mem_info.size - ams_ctx.sp)); ams_ctx.stack_dump_size = copy_size; std::memcpy(ams_ctx.stack_dump, reinterpret_cast(ams_ctx.sp), copy_size);