Skip to content

Commit

Permalink
Fix for dangerous relocation: j: cannot encode (#8925)
Browse files Browse the repository at this point in the history
Fixes to recent changes to Postmortem to cover large jump offsets, use relaxed jump (J.L) in __wrap_system_restart_local.
Also add check that epc1 is a valid code address before reading.
  • Loading branch information
mhightower83 committed Jun 16, 2023
1 parent 8b33e2e commit 521ae60
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions cores/esp8266/core_esp8266_postmortem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ static void cut_here() {
ets_putc('\n');
}

static inline bool is_pc_valid(uint32_t pc) {
return pc >= XCHAL_INSTRAM0_VADDR && pc < (XCHAL_INSTROM0_VADDR + XCHAL_INSTROM0_SIZE);
}

/*
Add some assembly to grab the stack pointer and pass it as an argument before
it grows for the target function. Should stabilize the stack offsets, used to
Expand All @@ -125,7 +129,7 @@ asm(
"\n"
"__wrap_system_restart_local:\n\t"
"mov a2, a1\n\t"
"j postmortem_report\n\t"
"j.l postmortem_report, a3\n\t"
".size __wrap_system_restart_local, .-__wrap_system_restart_local\n\t"
);

Expand Down Expand Up @@ -183,7 +187,7 @@ static void postmortem_report(uint32_t sp_dump) {
else if (rst_info.reason == REASON_SOFT_WDT_RST) {
ets_printf_P(PSTR("\nSoft WDT reset"));
const char infinite_loop[] = { 0x06, 0xff, 0xff }; // loop: j loop
if (0 == memcmp_P(infinite_loop, (PGM_VOID_P)rst_info.epc1, 3u)) {
if (is_pc_valid(rst_info.epc1) && 0 == memcmp_P(infinite_loop, (PGM_VOID_P)rst_info.epc1, 3u)) {
// The SDK is riddled with these. They are usually preceded by an ets_printf.
ets_printf_P(PSTR(" - deliberate infinite loop detected"));
}
Expand Down

0 comments on commit 521ae60

Please sign in to comment.