diff --git a/components/ulp/ulp_fsm/include/ulp_fsm_common.h b/components/ulp/ulp_fsm/include/ulp_fsm_common.h index 306eb59ea04..881067d4c30 100644 --- a/components/ulp/ulp_fsm/include/ulp_fsm_common.h +++ b/components/ulp/ulp_fsm/include/ulp_fsm_common.h @@ -89,7 +89,8 @@ esp_err_t ulp_process_macros_and_load(uint32_t load_addr, const ulp_insn_t* prog * @return * - ESP_OK on success * - ESP_ERR_INVALID_ARG if load_addr is out of range - * - ESP_ERR_INVALID_SIZE if program_size doesn't match (TEXT_OFFSET + TEXT_SIZE + DATA_SIZE) + * - ESP_ERR_INVALID_SIZE if program_size doesn't match (TEXT_OFFSET + TEXT_SIZE + DATA_SIZE), + * or if TEXT_SIZE + DATA_SIZE + BSS_SIZE exceeds the reserved ULP memory region * - ESP_ERR_NOT_SUPPORTED if the magic number is incorrect */ esp_err_t ulp_load_binary(uint32_t load_addr, const uint8_t* program_binary, size_t program_size); diff --git a/components/ulp/ulp_fsm/ulp.c b/components/ulp/ulp_fsm/ulp.c index 6d7406fd823..092db6abd40 100644 --- a/components/ulp/ulp_fsm/ulp.c +++ b/components/ulp/ulp_fsm/ulp.c @@ -154,6 +154,13 @@ esp_err_t ulp_load_binary(uint32_t load_addr, const uint8_t* program_binary, siz } size_t text_data_size = header.text_size + header.data_size; + if (text_data_size > CONFIG_ULP_COPROC_RESERVE_MEM - load_addr_bytes) { + return ESP_ERR_INVALID_SIZE; + } + if ((size_t) header.bss_size > CONFIG_ULP_COPROC_RESERVE_MEM - load_addr_bytes - text_data_size) { + return ESP_ERR_INVALID_SIZE; + } + uint8_t* base = (uint8_t*) RTC_SLOW_MEM; memcpy(base + load_addr_bytes, program_binary + header.text_offset, text_data_size);