Add wolfSSL_GetAllocators PSRAM support for Espressif ESP32

This commit is contained in:
gojimmypi
2025-07-22 11:34:47 -07:00
parent 357b624ca5
commit 97c2e9f973
4 changed files with 159 additions and 18 deletions

View File

@@ -1311,7 +1311,8 @@ int esp_sha_try_hw_lock(WC_ESP32SHA* ctx)
} }
} }
#ifdef ESP_MONITOR_HW_TASK_LOCK #if defined(ESP_MONITOR_HW_TASK_LOCK) || \
(defined(WOLFSSL_DEBUG_MUTEX) && WOLFSSL_DEBUG_MUTEX)
/* Nothing happening here other than messages based on mutex states */ /* Nothing happening here other than messages based on mutex states */
if (mutex_ctx_task == 0 || mutex_ctx_owner == 0) { if (mutex_ctx_task == 0 || mutex_ctx_owner == 0) {
/* no known stray mutex task owner */ /* no known stray mutex task owner */
@@ -1347,13 +1348,15 @@ int esp_sha_try_hw_lock(WC_ESP32SHA* ctx)
} /* mutex owner ESP32_SHA_FREED check */ } /* mutex owner ESP32_SHA_FREED check */
} /* mutex_ctx_task is current task */ } /* mutex_ctx_task is current task */
else { else {
#ifdef WOLFSSL_ESP32_HW_LOCK_DEBUG
ESP_LOGW(TAG, "Warning: sha mutex unlock from unexpected task."); ESP_LOGW(TAG, "Warning: sha mutex unlock from unexpected task.");
ESP_LOGW(TAG, "Locking task: 0x%x", (word32)mutex_ctx_task); ESP_LOGW(TAG, "Locking task: 0x%x", (word32)mutex_ctx_task);
ESP_LOGW(TAG, "This xTaskGetCurrentTaskHandle: 0x%x", ESP_LOGW(TAG, "This xTaskGetCurrentTaskHandle: 0x%x",
(word32)xTaskGetCurrentTaskHandle()); (word32)xTaskGetCurrentTaskHandle());
#endif
} }
} }
#endif /* ESP_MONITOR_HW_TASK_LOCK */ #endif /* ESP_MONITOR_HW_TASK_LOCK || WOLFSSL_DEBUG_MUTEX */
/* check if this SHA has been operated as SW or HW, or not yet init */ /* check if this SHA has been operated as SW or HW, or not yet init */
if (ctx->mode == ESP32_SHA_INIT) { if (ctx->mode == ESP32_SHA_INIT) {

View File

@@ -189,7 +189,8 @@ int sdk_log_meminfo(enum sdk_memory_segment m, void* start, void* end)
} }
/* Show all known linker memory segment names, starting & ending addresses. */ /* Show all known linker memory segment names, starting & ending addresses. */
int sdk_init_meminfo(void) { int sdk_init_meminfo(void)
{
void* sample_heap_var; void* sample_heap_var;
int sample_stack_var = 0; int sample_stack_var = 0;
@@ -241,7 +242,8 @@ int sdk_init_meminfo(void) {
} }
/* Returns ESP_OK if found in known memory map, ESP_FAIL otherwise */ /* Returns ESP_OK if found in known memory map, ESP_FAIL otherwise */
esp_err_t sdk_var_whereis(const char* v_name, void* v) { esp_err_t sdk_var_whereis(const char* v_name, void* v)
{
esp_err_t ret = ESP_FAIL; esp_err_t ret = ESP_FAIL;
for (enum sdk_memory_segment m = 0 ;m < SDK_MEMORY_SEGMENT_COUNT; m++) { for (enum sdk_memory_segment m = 0 ;m < SDK_MEMORY_SEGMENT_COUNT; m++) {
@@ -289,15 +291,110 @@ esp_err_t esp_sdk_mem_lib_init(void)
return ret; return ret;
} }
#if defined(DEBUG_WOLFSSL_MALLOC) || defined(DEBUG_WOLFSSL)
void* wc_debug_pvPortMalloc(size_t size, void* wc_debug_pvPortMalloc(size_t size,
const char* file, int line, const char* fname) { const char* file, int line, const char* fname)
#else
void* wc_pvPortMalloc(size_t size)
#endif
{
void* ret = NULL; void* ret = NULL;
wolfSSL_Malloc_cb mc;
wolfSSL_Free_cb fc;
wolfSSL_Realloc_cb rc;
wolfSSL_GetAllocators(&mc, &fc, &rc);
if (mc == NULL) {
ret = pvPortMalloc(size); ret = pvPortMalloc(size);
}
else {
#if defined(USE_WOLFSSL_MEMORY) && !defined(NO_WOLFSSL_MEMORY)
ret = mc(size);
#else
ret = pvPortMalloc(size);
#endif
}
#if defined(DEBUG_WOLFSSL_MALLOC) || defined(DEBUG_WOLFSSL)
if (ret == NULL) { if (ret == NULL) {
ESP_LOGE("malloc", "%s:%d (%s)", file, line, fname); ESP_LOGE("malloc", "%s:%d (%s)", file, line, fname);
ESP_LOGE("malloc", "Failed Allocating memory of size: %d bytes", size); ESP_LOGE("malloc", "Failed Allocating memory of size: %d bytes", size);
} }
#endif
return ret; return ret;
} } /* wc_debug_pvPortMalloc */
#if defined(DEBUG_WOLFSSL_MALLOC) || defined(DEBUG_WOLFSSL)
void wc_debug_pvPortFree(void *ptr,
const char* file, int line, const char* fname)
#else
void wc_pvPortFree(void *ptr)
#endif
{
wolfSSL_Malloc_cb mc;
wolfSSL_Free_cb fc;
wolfSSL_Realloc_cb rc;
if (ptr == NULL) {
#ifdef DEBUG_WOLFSSL_MALLOC
/* It's ok to free a null pointer, and that happens quite frequently */
#endif
}
else {
wolfSSL_GetAllocators(&mc, &fc, &rc);
if (fc == NULL) {
vPortFree(ptr);
}
else {
#if defined(USE_WOLFSSL_MEMORY) && !defined(NO_WOLFSSL_MEMORY)
fc(ptr);
#else
vPortFree(ptr);
#endif
}
}
} /* wc_debug_pvPortFree */
#ifndef WOLFSSL_NO_REALLOC
/* see XREALLOC(p, n, h, t) */
#if defined(DEBUG_WOLFSSL_MALLOC) || defined(DEBUG_WOLFSSL)
void* wc_debug_pvPortRealloc(void* ptr, size_t size,
const char* file, int line, const char* fname)
#else
void* wc_pvPortRealloc(void* ptr, size_t size)
#endif
{
void* ret = NULL;
wolfSSL_Malloc_cb mc;
wolfSSL_Free_cb fc;
wolfSSL_Realloc_cb rc;
wolfSSL_GetAllocators(&mc, &fc, &rc);
if (mc == NULL) {
ret = realloc(ptr, size);
}
else {
#if defined(USE_WOLFSSL_MEMORY) && !defined(NO_WOLFSSL_MEMORY)
if (rc != NULL) {
ret = rc(ptr, size); /* (void *ptr, size_t size) */
}
else {
ret = realloc(ptr, size);
}
#else
ret = realloc(ptr, size);
#endif
}
#if defined(DEBUG_WOLFSSL_MALLOC) || defined(DEBUG_WOLFSSL)
if (ret == NULL) {
ESP_LOGE("realloc", "%s:%d (%s)", file, line, fname);
ESP_LOGE("realloc", "Failed Re-allocating memory of size: %d bytes",
size);
}
#endif
return ret;
} /* wc_debug_pvPortRealloc */
#endif /* WOLFSSL_NO_REALLOC */
#endif #endif

View File

@@ -212,8 +212,23 @@ WOLFSSL_LOCAL esp_err_t esp_sdk_wifi_show_ip(void);
* Debug helpers * Debug helpers
******************************************************************************/ ******************************************************************************/
WOLFSSL_LOCAL esp_err_t sdk_init_meminfo(void); WOLFSSL_LOCAL esp_err_t sdk_init_meminfo(void);
#if defined(DEBUG_WOLFSSL_MALLOC) || defined(DEBUG_WOLFSSL)
WOLFSSL_LOCAL void* wc_debug_pvPortMalloc(size_t size, WOLFSSL_LOCAL void* wc_debug_pvPortMalloc(size_t size,
const char* file, int line, const char* fname); const char* file, int line, const char* fname);
WOLFSSL_LOCAL void wc_debug_pvPortFree(void *ptr,
const char* file, int line, const char* fname);
#ifndef WOLFSSL_NO_REALLOC
WOLFSSL_LOCAL void* wc_debug_pvPortRealloc(void* ptr, size_t size,
const char* file, int line, const char* fname);
#endif
#else
WOLFSSL_LOCAL void* wc_pvPortMalloc(size_t size);
WOLFSSL_LOCAL void wc_pvPortFree(void *ptr);
#ifndef WOLFSSL_NO_REALLOC
WOLFSSL_LOCAL void* wc_pvPortRealloc(void* ptr, size_t size);
#endif
#endif /*DEBUG_WOLFSSL_MALLOC || DEBUG_WOLFSSL */
#ifdef __cplusplus #ifdef __cplusplus
} /* extern "C" */ } /* extern "C" */

View File

@@ -1497,27 +1497,53 @@ extern void uITRON4_free(void *p) ;
#if !defined(XMALLOC_USER) && !defined(NO_WOLFSSL_MEMORY) && \ #if !defined(XMALLOC_USER) && !defined(NO_WOLFSSL_MEMORY) && \
!defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFSSL_TRACK_MEMORY) !defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFSSL_TRACK_MEMORY)
/* XMALLOC */ #if defined(WOLFSSL_ESPIDF)
#if defined(WOLFSSL_ESPIDF) && \
(defined(DEBUG_WOLFSSL) || defined(DEBUG_WOLFSSL_MALLOC))
#include <wolfssl/wolfcrypt/port/Espressif/esp-sdk-lib.h> #include <wolfssl/wolfcrypt/port/Espressif/esp-sdk-lib.h>
#endif
/* XMALLOC */
#if defined(WOLFSSL_ESPIDF)
#if (defined(DEBUG_WOLFSSL) || defined(DEBUG_WOLFSSL_MALLOC))
#define XMALLOC(s, h, type) \ #define XMALLOC(s, h, type) \
((void)(h), (void)(type), wc_debug_pvPortMalloc( \ ((void)(h), (void)(type), wc_debug_pvPortMalloc( \
(s), (__FILE__), (__LINE__), (__FUNCTION__) )) (s), (__FILE__), (__LINE__), (__FUNCTION__) ))
#else
#define XMALLOC(s, h, type) \
((void)(h), (void)(type), wc_pvPortMalloc((s))) /* native heap */
#endif
#else #else
#define XMALLOC(s, h, type) \ #define XMALLOC(s, h, type) \
((void)(h), (void)(type), pvPortMalloc((s))) /* native heap */ ((void)(h), (void)(type), pvPortMalloc((s))) /* native heap */
#endif #endif
/* XFREE */ /* XFREE */
#define XFREE(p, h, type) ((void)(h), (void)(type), vPortFree((p))) /* native heap */ #if defined(WOLFSSL_ESPIDF)
#if (defined(DEBUG_WOLFSSL) || defined(DEBUG_WOLFSSL_MALLOC))
#define XFREE(p, h, type) \
((void)(h), (void)(type), wc_debug_pvPortFree( \
(p), (__FILE__), (__LINE__), (__FUNCTION__) ))
#else
#define XFREE(p, h, type) \
((void)(h), (void)(type), wc_pvPortFree((p)))
#endif
#else
#define XFREE(p, h, type) \
((void)(h), (void)(type), vPortFree((p))) /* native heap */
#endif
/* XREALLOC */ /* XREALLOC */
#if defined(WOLFSSL_ESPIDF) #if defined(WOLFSSL_ESPIDF)
#if (defined(DEBUG_WOLFSSL) || defined(DEBUG_WOLFSSL_MALLOC))
#define XREALLOC(p, n, h, t) \
((void)(h), (void)(t), wc_debug_pvPortRealloc( \
(p), (n),(__FILE__), (__LINE__), (__FUNCTION__) ))
#else
/* In the Espressif EDP-IDF, realloc(p, n) is equivalent to /* In the Espressif EDP-IDF, realloc(p, n) is equivalent to
* heap_caps_realloc(p, s, MALLOC_CAP_8BIT) * heap_caps_realloc(p, s, MALLOC_CAP_8BIT)
* There's no pvPortRealloc available: */ * There's no pvPortRealloc available, use native heap: */
#define XREALLOC(p, n, h, t) ((void)(h), (void)(t), realloc((p), (n))) /* native heap */ #define XREALLOC(p, n, h, t) \
((void)(h), (void)(t), wc_pvPortRealloc((p), (n)))
#endif
#elif defined(USE_INTEGER_HEAP_MATH) || defined(OPENSSL_EXTRA) || \ #elif defined(USE_INTEGER_HEAP_MATH) || defined(OPENSSL_EXTRA) || \
defined(OPENSSL_ALL) defined(OPENSSL_ALL)
/* FreeRTOS pvPortRealloc() implementation can be found here: /* FreeRTOS pvPortRealloc() implementation can be found here: