From c89c3169226fad9a26cbeb9a0eead54295c98b28 Mon Sep 17 00:00:00 2001 From: Mahavir Jain Date: Tue, 24 Sep 2024 14:13:22 +0530 Subject: [PATCH] fix(wpa_supplicant): add crypto init calls to address memory leak issue in tests - C61 does not feature MPI hardware and hence the other tests were getting executed first - Memory leak threshold should be independent of target crypto peripherals and hence added to crypto init to test `setUp` call --- .../test_apps/main/test_wpa_supplicant_main.c | 39 ++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/components/wpa_supplicant/test_apps/main/test_wpa_supplicant_main.c b/components/wpa_supplicant/test_apps/main/test_wpa_supplicant_main.c index 833d9ba3f0..ea7a63123f 100644 --- a/components/wpa_supplicant/test_apps/main/test_wpa_supplicant_main.c +++ b/components/wpa_supplicant/test_apps/main/test_wpa_supplicant_main.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -7,6 +7,16 @@ #include "unity.h" #include "unity_test_runner.h" #include "esp_heap_caps.h" +#include "mbedtls/aes.h" +#include "sdkconfig.h" +#include "soc/soc_caps.h" +#if SOC_SHA_SUPPORT_PARALLEL_ENG +#include "sha/sha_parallel_engine.h" +#elif SOC_SHA_SUPPORT_DMA +#include "sha/sha_dma.h" +#else +#include "sha/sha_block.h" +#endif #define TEST_MEMORY_LEAK_THRESHOLD_DEFAULT 0 static int leak_threshold = TEST_MEMORY_LEAK_THRESHOLD_DEFAULT; @@ -25,8 +35,35 @@ static void check_leak(size_t before_free, size_t after_free, const char *type) TEST_ASSERT_MESSAGE(delta > leak_threshold, "memory leak"); } +#if SOC_SHA_SUPPORT_SHA512 +#define SHA_TYPE SHA2_512 +#else +#define SHA_TYPE SHA2_256 +#endif //SOC_SHA_SUPPORT_SHA512 + void setUp(void) { +#if CONFIG_MBEDTLS_HARDWARE_SHA + // Execute esp_sha operation to allocate internal SHA semaphore (in case of ESP32) + // and initial DMA setup memory which is considered as leaked otherwise + const uint8_t input_buffer[64] = {0}; + uint8_t output_buffer[64]; + esp_sha(SHA_TYPE, input_buffer, sizeof(input_buffer), output_buffer); +#endif // SOC_SHA_SUPPORTED + +#if CONFIG_MBEDTLS_HARDWARE_AES + // Execute mbedtls_aes_init operation to allocate AES interrupt + // allocation memory which is considered as leak otherwise + const uint8_t plaintext[16] = {0}; + uint8_t ciphertext[16]; + const uint8_t key[16] = { 0 }; + mbedtls_aes_context ctx; + mbedtls_aes_init(&ctx); + mbedtls_aes_setkey_enc(&ctx, key, 128); + mbedtls_aes_crypt_ecb(&ctx, MBEDTLS_AES_ENCRYPT, plaintext, ciphertext); + mbedtls_aes_free(&ctx); +#endif // SOC_AES_SUPPORTED + before_free_8bit = heap_caps_get_free_size(MALLOC_CAP_8BIT); before_free_32bit = heap_caps_get_free_size(MALLOC_CAP_32BIT); }