Skip to content

Commit df6b40a

Browse files
committed
esp32: Workaround native code execution crash on ESP32-S2.
Seemingly ESP-IDF incorrectly marks RTC FAST memory region as MALLOC_CAP_EXEC on ESP32-S2 when it isn't. This memory is the lowest priority, so it only is returned if D/IRAM is exhausted. Apply this workaround to treat the allocation as failed if it gives us non-executable RAM back, rather than crashing. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <[email protected]>
1 parent 594670e commit df6b40a

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

ports/esp32/main.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include "esp_task.h"
4040
#include "esp_event.h"
4141
#include "esp_log.h"
42+
#include "esp_memory_utils.h"
4243
#include "esp_psram.h"
4344

4445
#include "py/cstack.h"
@@ -237,6 +238,13 @@ void *esp_native_code_commit(void *buf, size_t len, void *reloc) {
237238
len = (len + 3) & ~3;
238239
size_t len_node = sizeof(native_code_node_t) + len;
239240
native_code_node_t *node = heap_caps_malloc(len_node, MALLOC_CAP_EXEC);
241+
#if CONFIG_IDF_TARGET_ESP32S2
242+
// Workaround for ESP-IDF bug https://github.com/espressif/esp-idf/issues/14835
243+
if (node != NULL && !esp_ptr_executable(node)) {
244+
free(node);
245+
node = NULL;
246+
}
247+
#endif // CONFIG_IDF_TARGET_ESP32S2
240248
if (node == NULL) {
241249
m_malloc_fail(len_node);
242250
}

0 commit comments

Comments
 (0)