From 0376e6ec790f4a28a60f148b528675baee3318ee Mon Sep 17 00:00:00 2001 From: Guillaume Souchere Date: Fri, 4 Nov 2022 15:48:09 +0100 Subject: [PATCH] heap: add check for usage of flash content from iram this commits: - adds build-time test to check that no call to flash regions are done from IRAM functions - resolves problems related to IRAM function using content in flash memory - update heap_caps_alloc_failed to use a default function name in DRAM when necessary instead of creating a function name variable in DRAM for each call of heap_caps_alloc_failed. This allows to save some extra bytes in RAM. --- components/heap/CMakeLists.txt | 2 +- components/heap/heap_caps.c | 12 +++++++++--- components/heap/test_apps/CMakeLists.txt | 13 +++++++++++++ components/heap/test_apps/sdkconfig.defaults | 1 + 4 files changed, 24 insertions(+), 4 deletions(-) diff --git a/components/heap/CMakeLists.txt b/components/heap/CMakeLists.txt index 61cb81219a..f0a691f493 100644 --- a/components/heap/CMakeLists.txt +++ b/components/heap/CMakeLists.txt @@ -37,7 +37,7 @@ idf_component_register(SRCS "${srcs}" INCLUDE_DIRS ${includes} PRIV_INCLUDE_DIRS ${priv_includes} LDFRAGMENTS linker.lf - PRIV_REQUIRES soc) + PRIV_REQUIRES soc spi_flash) if(CONFIG_HEAP_TRACING) set(WRAP_FUNCTIONS diff --git a/components/heap/heap_caps.c b/components/heap/heap_caps.c index 9c35b56a81..50fe13763d 100644 --- a/components/heap/heap_caps.c +++ b/components/heap/heap_caps.c @@ -14,7 +14,7 @@ #include "esp_log.h" #include "heap_private.h" #include "esp_system.h" - +#include "esp_private/cache_utils.h" /* Forward declaration for base function, put in IRAM. * These functions don't check for errors after trying to allocate memory. */ @@ -56,10 +56,16 @@ IRAM_ATTR static void *dram_alloc_to_iram_addr(void *addr, size_t len) } -static void heap_caps_alloc_failed(size_t requested_size, uint32_t caps, const char *function_name) +IRAM_ATTR static void heap_caps_alloc_failed(size_t requested_size, uint32_t caps, const char *function_name) { + static const DRAM_ATTR char *default_func_name = ""; if (alloc_failed_callback) { - alloc_failed_callback(requested_size, caps, function_name); + if (!spi_flash_cache_enabled() && !esp_ptr_internal(function_name)) { + alloc_failed_callback(requested_size, caps, default_func_name); + } + else { + alloc_failed_callback(requested_size, caps, function_name); + } } #ifdef CONFIG_HEAP_ABORT_WHEN_ALLOCATION_FAILS diff --git a/components/heap/test_apps/CMakeLists.txt b/components/heap/test_apps/CMakeLists.txt index df2d30eeb0..5af129f397 100644 --- a/components/heap/test_apps/CMakeLists.txt +++ b/components/heap/test_apps/CMakeLists.txt @@ -4,3 +4,16 @@ cmake_minimum_required(VERSION 3.16) include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(test_heap) + +if(CONFIG_COMPILER_DUMP_RTL_FILES) + add_custom_target(check_test_app_sections ALL + COMMAND ${PYTHON} $ENV{IDF_PATH}/tools/ci/check_callgraph.py + --rtl-dir ${CMAKE_BINARY_DIR}/esp-idf/heap/ + --elf-file ${CMAKE_BINARY_DIR}/test_heap.elf + find-refs + --from-sections=.iram0.text + --to-sections=.flash.text,.flash.rodata + --exit-code + DEPENDS ${elf} + ) +endif() diff --git a/components/heap/test_apps/sdkconfig.defaults b/components/heap/test_apps/sdkconfig.defaults index 0e8a2c2557..2fbcda0f36 100644 --- a/components/heap/test_apps/sdkconfig.defaults +++ b/components/heap/test_apps/sdkconfig.defaults @@ -1,2 +1,3 @@ +CONFIG_COMPILER_DUMP_RTL_FILES=y CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU0=n CONFIG_ESP_SYSTEM_MEMPROT_FEATURE=n # memory protection needs to be disabled for certain tests