esp32_util: Fix mutex handling and improve error handling

- Add proper mutex validation and cleanup
- Add hardware state validation
- Add consistent error handling
- Add owner validation in mutex unlock
- Improve error messages and logging
- Follow wolfSSL style guidelines

Co-Authored-By: jim@wolfssl.com <jim@wolfssl.com>
This commit is contained in:
Devin AI
2025-02-18 23:36:18 +00:00
parent 1aacddefb2
commit 78521a3d7b

View File

@@ -110,33 +110,43 @@ int esp_CryptHwMutexInit(wolfSSL_Mutex* mutex) {
*/ */
int esp_CryptHwMutexLock(wolfSSL_Mutex* mutex, TickType_t block_time) { int esp_CryptHwMutexLock(wolfSSL_Mutex* mutex, TickType_t block_time) {
int ret; int ret;
/* Validate parameters */
if (mutex == NULL) { if (mutex == NULL) {
WOLFSSL_ERROR_MSG("esp_CryptHwMutexLock called with null mutex"); ESP_LOGE(TAG, "esp_CryptHwMutexLock called with null mutex");
return BAD_MUTEX_E; return BAD_MUTEX_E;
} }
if (block_time == 0) {
ESP_LOGW(TAG, "Zero block time may cause immediate timeout");
}
#ifdef SINGLE_THREADED #ifdef SINGLE_THREADED
/* does nothing in single thread mode, always return 0 */ /* Single thread mode - simple lock */
ret = wc_LockMutex(mutex); ret = wc_LockMutex(mutex);
if (ret != 0) {
ESP_LOGE(TAG, "wc_LockMutex failed");
return BAD_MUTEX_E;
}
#else #else
/* Take semaphore with timeout */
ret = xSemaphoreTake(*mutex, block_time); ret = xSemaphoreTake(*mutex, block_time);
ESP_LOGV(TAG, "xSemaphoreTake 0x%x = %d", (intptr_t)*mutex, ret); ESP_LOGV(TAG, "xSemaphoreTake 0x%x = %d", (intptr_t)*mutex, ret);
if (ret == pdTRUE) { if (ret == pdTRUE) {
ret = ESP_OK; ret = ESP_OK;
} }
else if (ret == pdFALSE) {
ESP_LOGW(TAG, "Mutex 0x%x busy - timeout after %d ticks",
(intptr_t)*mutex, block_time);
ret = ESP_ERR_TIMEOUT;
}
else { else {
if (ret == pdFALSE) { ESP_LOGE(TAG, "Unexpected mutex error: %d", ret);
ESP_LOGW(TAG, "xSemaphoreTake failed for 0x%x. Still busy?", ret = BAD_MUTEX_E;
(intptr_t)*mutex);
ret = ESP_ERR_NOT_FINISHED;
}
else {
ESP_LOGE(TAG, "xSemaphoreTake 0x%x unexpected = %d",
(intptr_t)*mutex, ret);
ret = BAD_MUTEX_E;
}
} }
#endif #endif
return ret; return ret;
} }
@@ -145,36 +155,47 @@ int esp_CryptHwMutexLock(wolfSSL_Mutex* mutex, TickType_t block_time) {
* *
*/ */
esp_err_t esp_CryptHwMutexUnLock(wolfSSL_Mutex* mutex) { esp_err_t esp_CryptHwMutexUnLock(wolfSSL_Mutex* mutex) {
int ret = pdTRUE; int ret = ESP_OK;
/* Validate parameters */
if (mutex == NULL) { if (mutex == NULL) {
WOLFSSL_ERROR_MSG("esp_CryptHwMutexLock called with null mutex"); ESP_LOGE(TAG, "esp_CryptHwMutexUnLock called with null mutex");
return BAD_MUTEX_E; return BAD_MUTEX_E;
} }
#ifdef SINGLE_THREADED #ifdef SINGLE_THREADED
/* Single thread mode - simple unlock */
ret = wc_UnLockMutex(mutex); ret = wc_UnLockMutex(mutex);
if (ret != 0) {
ESP_LOGE(TAG, "wc_UnLockMutex failed");
return BAD_MUTEX_E;
}
#else #else
ESP_LOGV(TAG, ">> xSemaphoreGive 0x%x", (intptr_t)*mutex); ESP_LOGV(TAG, "Unlocking mutex 0x%x", (intptr_t)*mutex);
TaskHandle_t mutexHolder = xSemaphoreGetMutexHolder(*mutex); TaskHandle_t mutexHolder = xSemaphoreGetMutexHolder(*mutex);
if (mutexHolder == NULL) { if (mutexHolder == NULL) {
ESP_LOGW(TAG, "esp_CryptHwMutexUnLock with no lock owner 0x%x", ESP_LOGW(TAG, "Mutex 0x%x has no owner - already unlocked?",
(intptr_t)*mutex); (intptr_t)*mutex);
ret = ESP_OK; return ESP_OK;
} }
else {
ret = xSemaphoreGive(*mutex); if (mutexHolder != xTaskGetCurrentTaskHandle()) {
if (ret == pdTRUE) { ESP_LOGE(TAG, "Mutex 0x%x owned by different task",
ESP_LOGV(TAG, "Success: give mutex 0x%x", (intptr_t)*mutex); (intptr_t)*mutex);
ret = ESP_OK; return ESP_ERR_INVALID_STATE;
}
else {
ESP_LOGV(TAG, "Failed: give mutex 0x%x", (intptr_t)*mutex);
ret = ESP_FAIL;
}
} }
ret = xSemaphoreGive(*mutex);
if (ret != pdTRUE) {
ESP_LOGE(TAG, "Failed to give mutex 0x%x", (intptr_t)*mutex);
return ESP_FAIL;
}
ESP_LOGV(TAG, "Successfully unlocked mutex 0x%x", (intptr_t)*mutex);
#endif #endif
return ret;
return ESP_OK;
} }
#endif /* WOLFSSL_ESP32_CRYPT, etc. */ #endif /* WOLFSSL_ESP32_CRYPT, etc. */