From 41a6f2bc8da53e3277c6b183bdc167399245c858 Mon Sep 17 00:00:00 2001
From: Chris <52449218+shadow578@users.noreply.github.com>
Date: Wed, 1 Nov 2023 01:10:37 +0100
Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=91=E2=80=8D=F0=9F=92=BB=20Improve=20P?=
 =?UTF-8?q?OSTMORTEM=5FDEBUGGING=20(#26374)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Co-authored-by: Scott Lahteine <thinkyhead@users.noreply.github.com>
---
 Marlin/src/HAL/shared/backtrace/unwarmbytab.cpp       | 7 ++++++-
 Marlin/src/HAL/shared/cpu_exception/exception_arm.cpp | 2 --
 2 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/Marlin/src/HAL/shared/backtrace/unwarmbytab.cpp b/Marlin/src/HAL/shared/backtrace/unwarmbytab.cpp
index 148927a19f5..bd87b6731cd 100644
--- a/Marlin/src/HAL/shared/backtrace/unwarmbytab.cpp
+++ b/Marlin/src/HAL/shared/backtrace/unwarmbytab.cpp
@@ -55,7 +55,12 @@ static const char *UnwTabGetFunctionName(const UnwindCallbacks *cb, uint32_t add
     return nullptr;
 
   if ((flag_word & 0xFF000000) == 0xFF000000) {
-    return (const char *)(address - 4 - (flag_word & 0x00FFFFFF));
+    const uint32_t fn_name_addr = address - 4 - (flag_word & 0x00FFFFFF);
+
+    // Ensure the address is readable to avoid returning a bogus pointer
+    uint8_t dummy = 0;
+    if (cb->readB(fn_name_addr, &dummy))
+      return (const char *)fn_name_addr;
   }
   return nullptr;
 }
diff --git a/Marlin/src/HAL/shared/cpu_exception/exception_arm.cpp b/Marlin/src/HAL/shared/cpu_exception/exception_arm.cpp
index e54661c7707..3c514f58a9c 100644
--- a/Marlin/src/HAL/shared/cpu_exception/exception_arm.cpp
+++ b/Marlin/src/HAL/shared/cpu_exception/exception_arm.cpp
@@ -279,8 +279,6 @@ void CommonHandler_C(ContextStateFrame * frame, unsigned long lr, unsigned long
   if (!faulted_from_exception) { // Not sure about the non_usage_fault, we want to try anyway, don't we ? && !non_usage_fault_occurred)
     // Try to resume to our handler here
     CFSR |= CFSR; // The ARM programmer manual says you must write to 1 all fault bits to clear them so this instruction is correct
-    // The frame will not be valid when returning anymore, let's clean it
-    savedFrame.CFSR = 0;
 
     frame->pc = (uint32_t)resume_from_fault; // Patch where to return to
     frame->lr = 0xDEADBEEF;  // If our handler returns (it shouldn't), let's make it trigger an exception immediately