Skip to content

Commit 521ae60

Browse files
authored
Fix for dangerous relocation: j: cannot encode (esp8266#8925)
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.
1 parent 8b33e2e commit 521ae60

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

cores/esp8266/core_esp8266_postmortem.cpp

+6-2
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ static void cut_here() {
110110
ets_putc('\n');
111111
}
112112

113+
static inline bool is_pc_valid(uint32_t pc) {
114+
return pc >= XCHAL_INSTRAM0_VADDR && pc < (XCHAL_INSTROM0_VADDR + XCHAL_INSTROM0_SIZE);
115+
}
116+
113117
/*
114118
Add some assembly to grab the stack pointer and pass it as an argument before
115119
it grows for the target function. Should stabilize the stack offsets, used to
@@ -125,7 +129,7 @@ asm(
125129
"\n"
126130
"__wrap_system_restart_local:\n\t"
127131
"mov a2, a1\n\t"
128-
"j postmortem_report\n\t"
132+
"j.l postmortem_report, a3\n\t"
129133
".size __wrap_system_restart_local, .-__wrap_system_restart_local\n\t"
130134
);
131135

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

0 commit comments

Comments
 (0)