Skip to content

Commit 5ff9cd4

Browse files
committed
panic: skip over the first invalid PC in case of InstrFetchProhibited
InstrFetchProhibited usually occurs because of a jump to an invalid pointer. In this case, PC in the exception frame is the address of the jump destination. 'esp_ptr_executable' check in print_backtrace function recognizes the first frame as invalid, and the backtrace is interrupted. This prevents the user from finding the location where the invalid pointer is dereferenced. Bypass the 'esp_ptr_executable' check if the exception cause is InstrFetchProhibited. Update the test case to no longer ignore this issue.
1 parent 48b659b commit 5ff9cd4

File tree

3 files changed

+10
-7
lines changed

3 files changed

+10
-7
lines changed

components/esp_system/port/panic_handler.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,9 @@ static void print_backtrace(const void *f, int core)
156156

157157
//Check if first frame is valid
158158
bool corrupted = !(esp_stack_ptr_is_sane(stk_frame.sp) &&
159-
esp_ptr_executable((void *)esp_cpu_process_stack_pc(stk_frame.pc)));
159+
(esp_ptr_executable((void *)esp_cpu_process_stack_pc(stk_frame.pc)) ||
160+
/* Ignore the first corrupted PC in case of InstrFetchProhibited */
161+
frame->exccause == EXCCAUSE_INSTR_PROHIBITED));
160162

161163
uint32_t i = ((depth <= 0) ? INT32_MAX : depth) - 1; //Account for stack frame that's already printed
162164
while (i-- > 0 && stk_frame.next_pc != 0 && !corrupted) {
@@ -456,7 +458,7 @@ static void frame_to_panic_info(XtExcFrame *frame, panic_info_t *info, bool pseu
456458

457459
info->description = "Exception was unhandled.";
458460

459-
if (info->reason == reason[0]) {
461+
if (frame->exccause == EXCCAUSE_ILLEGAL) {
460462
info->details = print_illegal_instruction_details;
461463
}
462464
}

tools/test_apps/system/panic/panic_tests.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,7 @@ def instr_fetch_prohibited_inner(env, test_name):
107107
with get_dut(env, test_name, "test_instr_fetch_prohibited") as dut:
108108
dut.expect_gme("InstrFetchProhibited")
109109
dut.expect_reg_dump(0)
110-
dut.expect("Backtrace:")
111-
# At the moment the backtrace is corrupted, need to jump over the first PC in case of InstrFetchProhibited.
112-
# Fix this and change expect to expect_none.
113-
dut.expect("CORRUPTED")
110+
dut.expect_backtrace()
114111
dut.expect_elf_sha256()
115-
dut.expect_none("Guru Meditation")
112+
dut.expect_none("CORRUPTED", "Guru Meditation")
116113
test_common(dut, test_name)

tools/test_apps/system/panic/test_panic_util/test_panic_util.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ def expect_elf_sha256(self):
7676
elf_sha256_len = int(sdkconfig.get("CONFIG_APP_RETRIEVE_LEN_ELF_SHA", "16"))
7777
self.expect("ELF file SHA256: " + elf_sha256[0:elf_sha256_len])
7878

79+
def expect_backtrace(self):
80+
self.expect("Backtrace:")
81+
self.expect_none("CORRUPTED")
82+
7983
def __enter__(self):
8084
self._raw_data = None
8185
return self

0 commit comments

Comments
 (0)