How is my heap getting corrupted? [Code assistance/review]

rfleming
Posts: 47
Joined: Tue Oct 09, 2018 12:30 am

How is my heap getting corrupted? [Code assistance/review]

Postby rfleming » Tue May 04, 2021 12:53 am

This one is a little hard to replicate, though I have reduced the code to be able to replicate it. I have ~50% chance of getting a HEAP_CORRUPTION when allocating uint8_t test[4]; inside app_main function, when doing so it sometimes hits an exception or crashes during the dump process too. When I allocate it outside (globally), it fails rarely. While this does work immediately, I would prefer not to just global everything to get it working.

Funnily enough after a few reboots, the app does start working and operating as expected, though this obviously is not a solution.

This was tested on a blank ESP32-PICO dev kit and the ESP32-Etherkit. Erase flash contents, then program the device.

Error 1.

Code: Select all

Success, Key pair created.
Test read 255
CORRUPT HEAP: multi_heap.c:172 detected at 0x3ffb5734
abort() was called at PC 0x4008790f on core 0

ELF file SHA256: c190fbb902ee15f9

Backtrace: 0x40083fed:0x3ffb72b0 0x400843f9:0x3ffb72d0 0x4008790f:0x3ffb72f0 0x40087be1:0x3ffb7310 0x40081eea:0x3ffb7330 0x40088175:0x3ffb7350 0x400859a4:0x3ffb7370 0x400865a2:0x3ffb7390 0x400865b7:0x3ffb73b0 0x40084ae5:0x3ffb73d0

{1B}[0;32mI (10930) esp_core_dump_uart: Press Enter to print core dump to UART...{1B}[0m
{1B}[0;32mI (10937) esp_core_dump_uart: Print core dump to uart...{1B}[0m
{1B}[0;32mI (10943) esp_core_dump_elf: Found tasks: 7{1B}[0m
================= CORE DUMP START =================
/CoAAAABAAAHAAAAdAEAAAAAAAA=
f0VMRgEBAQAAAAAAAAAAAAQAXgABAAAAAAAAADQAAAAAAAAAAAAAADQAIAAQACgA
...
etc...
...
AAAAALYAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAA==
dUttHA==
Error 2.

Code: Select all

Success, Key pair created.
Guru Meditation Error: Core  0 panic'ed (LoadProhibited). Exception was unhandled.
Core 0 register dump:
PC      : 0x40087487  PS      : 0x00060433  A0      : 0x80086acc  A1      : 0x3ffb6ca0  
A2      : 0x3f9d3c70  A3      : 0x3ffb55dc  A4      : 0x00000000  A5      : 0x00000001  
A6      : 0x00060420  A7      : 0x00000000  A8      : 0xffffffff  A9      : 0x00000000  
A10     : 0x3ffb5194  A11     : 0x00000000  A12     : 0x00000000  A13     : 0x00000001  
A14     : 0x00060423  A15     : 0x3ffb92fc  SAR     : 0x00000018  EXCCAUSE: 0x0000001c  
EXCVADDR: 0x00000007  LBEG    : 0x4000c46c  LEND    : 0x4000c477  LCOUNT  : 0x00000000  

ELF file SHA256: f0f4e1950e01d82f

Backtrace: 0x40087484:0x3ffb6ca0 0x40086ac9:0x3ffb6cc0 0x40085289:0x3ffb6ce0 0x400d25b4:0x3ffb6d20 0x400d266e:0x3ffb6d40 0x40082a8d:0x3ffb6d60 0x40083411:0x3ffb6d80 0x40083dc5:0x3ffb6da0 0x40083353:0x3ffb6dc0 0x400d473a:0x3ffb6df0 0x400d449f:0x3ffb6e10 0x400d12a2:0x3ffb6e30 0x40084ae5:0x3ffb6e50

{1B}[0;32mI (3023) esp_core_dump_uart: Press Enter to print core dump to UART...{1B}[0m
{1B}[0;32mI (3030) esp_core_dump_uart: Print core dump to uart...{1B}[0m
Guru Meditation Error: Core  0 panic'ed (LoadProhibited). Exception was unhandled.
Core 0 register dump:
PC      : 0x40085810  PS      : 0x00060533  A0      : 0x80086e18  A1      : 0x3ffb6a60  
A2      : 0x3ffb49d8  A3      : 0x3ffb6a90  A4      : 0x00000040  A5      : 0x3ffb32a4  
A6      : 0x00000000  A7      : 0x3ffb32ac  A8      : 0x3ffafda8  A9      : 0x3ffb6a40  
A10     : 0x3ffb49d8  A11     : 0x3ffb6a90  A12     : 0x00000000  A13     : 0x40082168  
A14     : 0x3ffb5194  A15     : 0x00000000  SAR     : 0x00000004  EXCCAUSE: 0x0000001c  
EXCVADDR: 0x0000000c  LBEG    : 0x4000c2e0  LEND    : 0x4000c2f6  LCOUNT  : 0x00000000  

ELF file SHA256: f0f4e1950e01d82f

Backtrace: 0x4008580d:0x3ffb6a60 0x40086e15:0x3ffb6a90 0x400d59d1:0x3ffb6ac0 0x400d67e7:0x3ffb6af0 0x400d56fa:0x3ffb6b30 0x400d5dd0:0x3ffb6b50 0x4008434d:0x3ffb6ba0 0x400846dd:0x3ffb6bc0 0x400821b2:0x3ffb6be0 0x40087484:0x3ffb6ca0 0x40086ac9:0x3ffb6cc0 0x40085289:0x3ffb6ce0 0x400d25b4:0x3ffb6d20 0x400d266e:0x3ffb6d40 0x40082a8d:0x3ffb6d60 0x40083411:0x3ffb6d80 0x40083dc5:0x3ffb6da0 0x40083353:0x3ffb6dc0 0x400d473a:0x3ffb6df0 0x400d449f:0x3ffb6e10 0x400d12a2:0x3ffb6e30 0x40084ae5:0x3ffb6e50

Re-entered core dump! Exception happened during core dump!
Rebooting...
Code

Code: Select all

#include <stdio.h>
#include <string.h>

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/timers.h"

#include <esp_system.h>
#include <nvs_flash.h>
#include <sys/param.h>

#include "esp_err.h"
#include "nvs_flash.h"

//app driver includes
#include <mbedtls/pk.h>
#include <mbedtls/error.h>
#include <mbedtls/entropy.h>
#include <mbedtls/ctr_drbg.h>
#include "mbedtls/bignum.h"
#include "mbedtls/x509.h"
#include "mbedtls/rsa.h"
#include "mbedtls/config.h"
#include "mbedtls/platform.h"
#include "mbedtls/base64.h"
#include "esp_partition.h"

static unsigned char privKeyPem[2000];
static unsigned char pubKeyPem[1000];

uint32_t privKeyOffset = 0 * CONFIG_WL_SECTOR_SIZE;
uint32_t pubKeyOffset = 1 * CONFIG_WL_SECTOR_SIZE;
uint32_t tamperDataOffset = 2 * CONFIG_WL_SECTOR_SIZE;
esp_partition_t *partStorage = NULL;

mbedtls_pk_context pk;
mbedtls_entropy_context entropy;
mbedtls_ctr_drbg_context ctrDrbg;

const char personalizationString[11] = "rsa_genkey";	//requires static size allocation in classes
#define KEY_SIZE 2048
#define EXPONENT 65537

bool Crypto_Verify(bool initisalised);
bool Crypto_LoadKeys();
bool Crypto_GenerateKeyPair(void);

//uint8_t test[4];

extern "C" void app_main() {
	//Assign partition regions
	partStorage = (esp_partition_t *)esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, "storage");

	if (partStorage != NULL) {
		if (!Crypto_LoadKeys()) {
			if (!Crypto_GenerateKeyPair()) {
				puts("!ERROR! Unable to create key pair!");
			}
		}

		//allocate inside function
		uint8_t test[4];

		//test bad alignment...maybe?
		for (int i = 0; i < sizeof(test); i++) {
			esp_partition_read(partStorage, tamperDataOffset, &test[i], 1);
		}

		printf("Test read %d\n", test[0]);
	}
	else {
		puts("Couldn't find partition");
	}
}

bool Crypto_LoadKeys() {
	bool ret = true;
	esp_err_t resp = esp_partition_read(partStorage, privKeyOffset, privKeyPem, sizeof(privKeyPem) - 1);
	if (ESP_OK != resp) {
		puts("Err: Couldn't load priv key from partition");
		return false;
	}

	resp = esp_partition_read(partStorage, pubKeyOffset, pubKeyPem, sizeof(pubKeyPem) - 1);
	if (ESP_OK != resp) {
		puts("Err: Couldn't load pub key from partition");
		ret = false;
	}
	else if (NULL == strstr((const char *)privKeyPem, "-----BEGIN RSA PRIVATE KEY-----") || NULL == strstr((const char *)privKeyPem, "-----END RSA PRIVATE KEY-----")) {
		puts("Err: Priv key couldn't be loaded");
		ret = false;
	}
	else if (NULL == strstr((const char *)pubKeyPem, "-----BEGIN PUBLIC KEY-----") || NULL == strstr((const char *)pubKeyPem, "-----END PUBLIC KEY-----")) {
		puts("Err: Pub key couldn't be loaded");
		ret = false;
	}

	/* pk struct not currently initialised */
	if (!Crypto_Verify(false)) {
		puts("Err: Private key couldn't verified");
		ret = false;
	}

	//null terminate safety
	privKeyPem[sizeof(privKeyPem) - 1] = 0;
	pubKeyPem[sizeof(pubKeyPem) - 1] = 0;

	return ret;
}

bool Crypto_Verify(bool initisalised) {
	int ret = 0;
	/* Key has no password - parse in NULL size 0 */
	if (!initisalised) {
		mbedtls_pk_init(&pk);
		if ((ret = mbedtls_pk_parse_key(&pk, privKeyPem, strlen((const char *)privKeyPem) + 1, NULL, 0)) != 0) {
			printf("Unable to parse %d\n", ret);
			goto exit;
		}
	}

	/* Asumme keys are already loaded into internal variables */
	if ((ret = mbedtls_rsa_check_privkey(mbedtls_pk_rsa(pk))) != 0) {
		puts("Err: RSA context does not contain an rsa private key");
		goto exit;
	}

	if ((ret = mbedtls_rsa_check_pubkey(mbedtls_pk_rsa(pk))) != 0) {
		puts("Err: RSA context does not contain an rsa public key");
		goto exit;
	}

	puts("RSA keys confirmed");

exit:
	if (!initisalised) {
		mbedtls_pk_free(&pk);
	}
	return !ret;
}

bool Crypto_GenerateKeyPair(void) {
	bool ret = false;	 //== ESP_OK

	/* Initialise variables */
	mbedtls_entropy_init(&entropy);
	mbedtls_ctr_drbg_init(&ctrDrbg);
	mbedtls_pk_init(&pk);

	puts("Seeding the random number generator...");
	if ((ret = mbedtls_ctr_drbg_seed(&ctrDrbg, mbedtls_entropy_func, &entropy, (const unsigned char *)personalizationString, strlen(personalizationString))) != 0) {
		printf("Failed to seed rng\nmbedtls_ctr_drbg_seed returned %d\n", ret);
		goto cleanup;
	}

	if ((ret = mbedtls_pk_setup(&pk, mbedtls_pk_info_from_type(MBEDTLS_PK_RSA))) != 0) {
		printf("pk_setup failed: %i\n", ret);
	}

	printf(" ok\n  . Generating the RSA key [ %d-bit ]...", KEY_SIZE);

	/* force task yield before generate a key that could take several seconds to do */
	taskYIELD();
	puts("Generating a key pair takes up to 20s and will likely trigger task watchdog during this process");

	if ((ret = mbedtls_rsa_gen_key(mbedtls_pk_rsa(pk), mbedtls_ctr_drbg_random, &ctrDrbg, KEY_SIZE, EXPONENT)) != 0) {
		printf(" failed\n  ! mbedtls_rsa_gen_key returned %d\n\n", ret);
		goto cleanup;
	}

	/* pk struct already intitialised */
	if (!Crypto_Verify(true)) {
		goto cleanup;
	}

	puts("Writing public key to string(PEM format)....");

	//unsigned char pubKeyPem[1000];
	memset(pubKeyPem, 0, sizeof(pubKeyPem));
	if (mbedtls_pk_write_pubkey_pem(&pk, pubKeyPem, sizeof(pubKeyPem)) != 0) {
		puts("Err: Write public key to string failed");
		goto cleanup;
	}

	puts("Writing private key to string(PEM format)....");

	memset(privKeyPem, 0, sizeof(privKeyPem));
	ret = mbedtls_pk_write_key_pem(&pk, privKeyPem, sizeof(privKeyPem));
	if (ret != 0) {
		printf("write private key to string failed with code %04x\n", ret);
	}

	puts(" ok\n  .");
	puts("Success, Key pair created.");

cleanup:
	mbedtls_pk_free(&pk);
	mbedtls_ctr_drbg_free(&ctrDrbg);
	mbedtls_entropy_free(&entropy);

	return !ret;
}
partition table

Code: Select all

# Name, 	Type, 	SubType, 	Offset, 	Size, 		Flags
nvs,      	data,	nvs,     	0x9000,  	0x5000,
otadata,	data,	ota,		0xe000,		0x2000,
ota_0,		app,	ota_0,		0x10000,	0x190000,
ota_1,		app,	ota_1, 		,			0x190000,
storage,	data,	0x40,		,			0xD0000,
crash_dump analysis...short version

Code: Select all

==================== CURRENT THREAD STACK =====================
#0  0x40083ff0 in invoke_abort () at C:/msys32/home/4Volts/esp/esp-idf/components/esp32/panic.c:159
#1  0x400843fc in abort () at C:/msys32/home/4Volts/esp/esp-idf/components/esp32/panic.c:174
Backtrace stopped: previous frame identical to this frame (corrupt stack?)

======================== THREADS INFO =========================
  Id   Target Id         Frame
* 1    process 1073443936 0x40083ff0 in invoke_abort () at C:/msys32/home/4Volts/esp/esp-idf/components/esp32/panic.c:159
  2    process 1073445340 0x400f56e2 in esp_pm_impl_waiti () at C:/msys32/home/4Volts/esp/esp-idf/components/esp32/pm_esp32.c:484
  3    process 1073448012 0x4008130c in esp_crosscore_int_send_yield (core_id=0) at C:/msys32/home/4Volts/esp/esp-idf/components/esp32/crosscore_int.c:111
  4    process 1073442532 0x4008130c in esp_crosscore_int_send_yield (core_id=0) at C:/msys32/home/4Volts/esp/esp-idf/components/esp32/crosscore_int.c:111
  5    process 1073412788 0x4008130c in esp_crosscore_int_send_yield (core_id=0) at C:/msys32/home/4Volts/esp/esp-idf/components/esp32/crosscore_int.c:111
  6    process 1073436096 0x4008130c in esp_crosscore_int_send_yield (core_id=1) at C:/msys32/home/4Volts/esp/esp-idf/components/esp32/crosscore_int.c:111
  7    process 1073413544 0x4008130c in esp_crosscore_int_send_yield (core_id=0) at C:/msys32/home/4Volts/esp/esp-idf/components/esp32/crosscore_int.c:111

==================== THREAD 2 (TCB: 0x3ffb79dc, name: 'IDLE1') =====================
#0  0x400f56e2 in esp_pm_impl_waiti () at C:/msys32/home/4Volts/esp/esp-idf/components/esp32/pm_esp32.c:484
#1  0x400d2472 in esp_vApplicationIdleHook () at C:/msys32/home/4Volts/esp/esp-idf/components/esp_common/src/freertos_hooks.c:63
#2  0x400865bd in prvIdleTask (pvParameters=0x0) at C:/msys32/home/4Volts/esp/esp-idf/components/freertos/tasks.c:3383
#3  0x40084ae8 in vPortTaskWrapper (pxCode=0x400865b4 <prvIdleTask>, pvParameters=0x0) at C:/msys32/home/4Volts/esp/esp-idf/components/freertos/port.c:143

==================== THREAD 3 (TCB: 0x3ffb844c, name: 'Tmr Svc') =====================
#0  0x4008130c in esp_crosscore_int_send_yield (core_id=0) at C:/msys32/home/4Volts/esp/esp-idf/components/esp32/crosscore_int.c:111
#1  0x400870de in prvProcessTimerOrBlockTask (xNextExpireTime=<optimized out>, xListWasEmpty=<optimized out>) at C:/msys32/home/4Volts/esp/esp-idf/components/freertos/include/freertos/portable.h:214
#2  0x400871cb in prvTimerTask (pvParameters=0x0) at C:/msys32/home/4Volts/esp/esp-idf/components/freertos/timers.c:544
#3  0x40084ae8 in vPortTaskWrapper (pxCode=0x400871bc <prvTimerTask>, pvParameters=0x0) at C:/msys32/home/4Volts/esp/esp-idf/components/freertos/port.c:143

==================== THREAD 4 (TCB: 0x3ffb6ee4, name: '') =====================
#0  0x4008130c in esp_crosscore_int_send_yield (core_id=0) at C:/msys32/home/4Volts/esp/esp-idf/components/esp32/crosscore_int.c:111
#1  0x4008630b in vTaskDelete (xTaskToDelete=<optimized out>) at C:/msys32/home/4Volts/esp/esp-idf/components/freertos/include/freertos/portable.h:214
#2  0x400d12a6 in main_task (args=0x0) at C:/msys32/home/4Volts/esp/esp-idf/components/esp32/cpu_start.c:589
#3  0x40084ae8 in vPortTaskWrapper (pxCode=0x400d123c <main_task>, pvParameters=0x0) at C:/msys32/home/4Volts/esp/esp-idf/components/freertos/port.c:143

==================== THREAD 5 (TCB: 0x3ffafab4, name: 'esp_timer') =====================
#0  0x4008130c in esp_crosscore_int_send_yield (core_id=0) at C:/msys32/home/4Volts/esp/esp-idf/components/esp32/crosscore_int.c:111
#1  0x40085654 in xQueueGenericReceive (xQueue=0x3ffaea5c, pvBuffer=0x0, xTicksToWait=<optimized out>, xJustPeeking=0) at C:/msys32/home/4Volts/esp/esp-idf/components/freertos/include/freertos/portable.h:214
#2  0x400d23a7 in timer_task (arg=0x0) at C:/msys32/home/4Volts/esp/esp-idf/components/esp_common/src/esp_timer.c:325
#3  0x40084ae8 in vPortTaskWrapper (pxCode=0x400d2394 <timer_task>, pvParameters=0x0) at C:/msys32/home/4Volts/esp/esp-idf/components/freertos/port.c:143

==================== THREAD 6 (TCB: 0x3ffb55c0, name: 'ipc1') =====================
#0  0x4008130c in esp_crosscore_int_send_yield (core_id=1) at C:/msys32/home/4Volts/esp/esp-idf/components/esp32/crosscore_int.c:111
#1  0x40085654 in xQueueGenericReceive (xQueue=0x3ffb5168, pvBuffer=0x0, xTicksToWait=<optimized out>, xJustPeeking=0) at C:/msys32/home/4Volts/esp/esp-idf/components/freertos/include/freertos/portable.h:214
#2  0x40081b9f in ipc_task (arg=0x1) at C:/msys32/home/4Volts/esp/esp-idf/components/esp_common/src/ipc.c:51
#3  0x40084ae8 in vPortTaskWrapper (pxCode=0x40081b6c <ipc_task>, pvParameters=0x1) at C:/msys32/home/4Volts/esp/esp-idf/components/freertos/port.c:143

==================== THREAD 7 (TCB: 0x3ffafda8, name: 'ipc0') =====================
#0  0x4008130c in esp_crosscore_int_send_yield (core_id=0) at C:/msys32/home/4Volts/esp/esp-idf/components/esp32/crosscore_int.c:111
#1  0x40085654 in xQueueGenericReceive (xQueue=0x3ffafd54, pvBuffer=0x0, xTicksToWait=<optimized out>, xJustPeeking=0) at C:/msys32/home/4Volts/esp/esp-idf/components/freertos/include/freertos/portable.h:214
#2  0x40081b9f in ipc_task (arg=0x0) at C:/msys32/home/4Volts/esp/esp-idf/components/esp_common/src/ipc.c:51
#3  0x40084ae8 in vPortTaskWrapper (pxCode=0x40081b6c <ipc_task>, pvParameters=0x0) at C:/msys32/home/4Volts/esp/esp-idf/components/freertos/port.c:143

ESP_Angus
Posts: 2315
Joined: Sun May 08, 2016 4:11 am

Re: How is my heap getting corrupted? [Code assistance/review]

Postby ESP_Angus » Wed May 05, 2021 8:44 am

Hi rfleming,

Thanks for the comprehensive description of the issue.

What ESP-IDF version are you using?

I had a quick attempt to reproduce this on ESP-IDF master branch and failed. I think either it depends on the IDF version you're using, or it depends on the sdkconfig contents (for example, the offsets in the partition seem to be controlled by the CONFIG_WL_SECTOR_SIZE value.)

I can't really explain why moving a variable from the stack to .bss makes a heap corruption issue appear/disappear, my best guess would be that this changes timing or memory layout in a way that is otherwise unrelated to the code itself.

Suggest using some of the heap corruption detection tools to try and pin this down:
https://docs.espressif.com/projects/esp ... -detection

In particular you could try calling heaps_caps_check_integrity_all() at key points in the execution flow to pin down when the corruption happens, and/or try to figure out what variable or buffer is allocated at an address close to the address where the corruption is reported - probably a write is overflowing the nearby buffer.

rfleming
Posts: 47
Joined: Tue Oct 09, 2018 12:30 am

Re: How is my heap getting corrupted? [Code assistance/review]

Postby rfleming » Thu May 06, 2021 4:47 am

I always forget to include something.

v4.1.1 release - Repeated also with v4.2.1

SDK contents

Code: Select all

#
# Automatically generated file. DO NOT EDIT.
# Espressif IoT Development Framework (ESP-IDF) Project Configuration
#
CONFIG_IDF_CMAKE=y
CONFIG_IDF_TARGET="esp32"
CONFIG_IDF_TARGET_ESP32=y
CONFIG_IDF_FIRMWARE_CHIP_ID=0x0000

#
# SDK tool configuration
#
CONFIG_SDK_TOOLPREFIX="xtensa-esp32-elf-"
# CONFIG_SDK_TOOLCHAIN_SUPPORTS_TIME_WIDE_64_BITS is not set
# end of SDK tool configuration

#
# Build type
#
CONFIG_APP_BUILD_TYPE_APP_2NDBOOT=y
# CONFIG_APP_BUILD_TYPE_ELF_RAM is not set
CONFIG_APP_BUILD_GENERATE_BINARIES=y
CONFIG_APP_BUILD_BOOTLOADER=y
CONFIG_APP_BUILD_USE_FLASH_SECTIONS=y
# end of Build type

#
# Application manager
#
CONFIG_APP_COMPILE_TIME_DATE=y
# CONFIG_APP_EXCLUDE_PROJECT_VER_VAR is not set
# CONFIG_APP_EXCLUDE_PROJECT_NAME_VAR is not set
CONFIG_APP_RETRIEVE_LEN_ELF_SHA=16
# end of Application manager

#
# Bootloader config
#
CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_SIZE=y
# CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_DEBUG is not set
# CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_PERF is not set
# CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_NONE is not set
# CONFIG_BOOTLOADER_LOG_LEVEL_NONE is not set
# CONFIG_BOOTLOADER_LOG_LEVEL_ERROR is not set
CONFIG_BOOTLOADER_LOG_LEVEL_WARN=y
# CONFIG_BOOTLOADER_LOG_LEVEL_INFO is not set
# CONFIG_BOOTLOADER_LOG_LEVEL_DEBUG is not set
# CONFIG_BOOTLOADER_LOG_LEVEL_VERBOSE is not set
CONFIG_BOOTLOADER_LOG_LEVEL=2
# CONFIG_BOOTLOADER_VDDSDIO_BOOST_1_8V is not set
CONFIG_BOOTLOADER_VDDSDIO_BOOST_1_9V=y
# CONFIG_BOOTLOADER_FACTORY_RESET is not set
# CONFIG_BOOTLOADER_APP_TEST is not set
CONFIG_BOOTLOADER_WDT_ENABLE=y
# CONFIG_BOOTLOADER_WDT_DISABLE_IN_USER_CODE is not set
CONFIG_BOOTLOADER_WDT_TIME_MS=9000
# CONFIG_BOOTLOADER_APP_ROLLBACK_ENABLE is not set
# CONFIG_BOOTLOADER_SKIP_VALIDATE_IN_DEEP_SLEEP is not set
CONFIG_BOOTLOADER_RESERVE_RTC_SIZE=0
# CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC is not set
# end of Bootloader config

#
# Security features
#
# CONFIG_SECURE_SIGNED_APPS_NO_SECURE_BOOT is not set
# CONFIG_SECURE_BOOT is not set
# CONFIG_SECURE_FLASH_ENC_ENABLED is not set
# end of Security features

#
# Serial flasher config
#
CONFIG_ESPTOOLPY_BAUD_OTHER_VAL=115200
# CONFIG_ESPTOOLPY_FLASHMODE_QIO is not set
# CONFIG_ESPTOOLPY_FLASHMODE_QOUT is not set
CONFIG_ESPTOOLPY_FLASHMODE_DIO=y
# CONFIG_ESPTOOLPY_FLASHMODE_DOUT is not set
CONFIG_ESPTOOLPY_FLASHMODE="dio"
# CONFIG_ESPTOOLPY_FLASHFREQ_80M is not set
CONFIG_ESPTOOLPY_FLASHFREQ_40M=y
# CONFIG_ESPTOOLPY_FLASHFREQ_26M is not set
# CONFIG_ESPTOOLPY_FLASHFREQ_20M is not set
CONFIG_ESPTOOLPY_FLASHFREQ="40m"
# CONFIG_ESPTOOLPY_FLASHSIZE_1MB is not set
# CONFIG_ESPTOOLPY_FLASHSIZE_2MB is not set
CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y
# CONFIG_ESPTOOLPY_FLASHSIZE_8MB is not set
# CONFIG_ESPTOOLPY_FLASHSIZE_16MB is not set
CONFIG_ESPTOOLPY_FLASHSIZE="4MB"
CONFIG_ESPTOOLPY_FLASHSIZE_DETECT=y
CONFIG_ESPTOOLPY_BEFORE_RESET=y
# CONFIG_ESPTOOLPY_BEFORE_NORESET is not set
CONFIG_ESPTOOLPY_BEFORE="default_reset"
CONFIG_ESPTOOLPY_AFTER_RESET=y
# CONFIG_ESPTOOLPY_AFTER_NORESET is not set
CONFIG_ESPTOOLPY_AFTER="hard_reset"
# CONFIG_ESPTOOLPY_MONITOR_BAUD_9600B is not set
# CONFIG_ESPTOOLPY_MONITOR_BAUD_57600B is not set
CONFIG_ESPTOOLPY_MONITOR_BAUD_115200B=y
# CONFIG_ESPTOOLPY_MONITOR_BAUD_230400B is not set
# CONFIG_ESPTOOLPY_MONITOR_BAUD_921600B is not set
# CONFIG_ESPTOOLPY_MONITOR_BAUD_2MB is not set
# CONFIG_ESPTOOLPY_MONITOR_BAUD_OTHER is not set
CONFIG_ESPTOOLPY_MONITOR_BAUD_OTHER_VAL=115200
CONFIG_ESPTOOLPY_MONITOR_BAUD=115200
# end of Serial flasher config

#
# Partition Table
#
# CONFIG_PARTITION_TABLE_SINGLE_APP is not set
# CONFIG_PARTITION_TABLE_TWO_OTA is not set
CONFIG_PARTITION_TABLE_CUSTOM=y
CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partition_project.csv"
CONFIG_PARTITION_TABLE_FILENAME="partition_project.csv"
CONFIG_PARTITION_TABLE_OFFSET=0x8000
CONFIG_PARTITION_TABLE_MD5=y
# end of Partition Table

#
# Compiler options
#
CONFIG_COMPILER_OPTIMIZATION_DEFAULT=y
# CONFIG_COMPILER_OPTIMIZATION_SIZE is not set
# CONFIG_COMPILER_OPTIMIZATION_PERF is not set
# CONFIG_COMPILER_OPTIMIZATION_NONE is not set
CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_ENABLE=y
# CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_SILENT is not set
# CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_DISABLE is not set
# CONFIG_COMPILER_CXX_EXCEPTIONS is not set
# CONFIG_COMPILER_CXX_RTTI is not set
CONFIG_COMPILER_STACK_CHECK_MODE_NONE=y
# CONFIG_COMPILER_STACK_CHECK_MODE_NORM is not set
# CONFIG_COMPILER_STACK_CHECK_MODE_STRONG is not set
# CONFIG_COMPILER_STACK_CHECK_MODE_ALL is not set
# CONFIG_COMPILER_WARN_WRITE_STRINGS is not set
# CONFIG_COMPILER_DISABLE_GCC8_WARNINGS is not set
# end of Compiler options

#
# Component config
#

#
# Application Level Tracing
#
# CONFIG_APPTRACE_DEST_TRAX is not set
CONFIG_APPTRACE_DEST_NONE=y
CONFIG_APPTRACE_LOCK_ENABLE=y
# end of Application Level Tracing

#
# Bluetooth
#
# CONFIG_BT_ENABLED is not set
CONFIG_BTDM_CTRL_BR_EDR_SCO_DATA_PATH_EFF=0
CONFIG_BTDM_CTRL_PCM_ROLE_EFF=0
CONFIG_BTDM_CTRL_PCM_POLAR_EFF=0
CONFIG_BTDM_CTRL_BLE_MAX_CONN_EFF=0
CONFIG_BTDM_CTRL_BR_EDR_MAX_ACL_CONN_EFF=0
CONFIG_BTDM_CTRL_BR_EDR_MAX_SYNC_CONN_EFF=0
CONFIG_BTDM_CTRL_PINNED_TO_CORE=0
CONFIG_BTDM_BLE_SLEEP_CLOCK_ACCURACY_INDEX_EFF=1
CONFIG_BT_RESERVE_DRAM=0
# end of Bluetooth

# CONFIG_BLE_MESH is not set

#
# CoAP Configuration
#
CONFIG_COAP_MBEDTLS_PSK=y
# CONFIG_COAP_MBEDTLS_PKI is not set
# CONFIG_COAP_MBEDTLS_DEBUG is not set
CONFIG_COAP_LOG_DEFAULT_LEVEL=0
# end of CoAP Configuration

#
# Driver configurations
#

#
# ADC configuration
#
# CONFIG_ADC_FORCE_XPD_FSM is not set
CONFIG_ADC_DISABLE_DAC=y
# end of ADC configuration

#
# SPI configuration
#
# CONFIG_SPI_MASTER_IN_IRAM is not set
CONFIG_SPI_MASTER_ISR_IN_IRAM=y
# CONFIG_SPI_SLAVE_IN_IRAM is not set
CONFIG_SPI_SLAVE_ISR_IN_IRAM=y
# end of SPI configuration

#
# UART configuration
#
# CONFIG_UART_ISR_IN_IRAM is not set
# end of UART configuration

#
# RTCIO configuration
#
# CONFIG_RTCIO_SUPPORT_RTC_GPIO_DESC is not set
# end of RTCIO configuration
# end of Driver configurations

#
# eFuse Bit Manager
#
# CONFIG_EFUSE_CUSTOM_TABLE is not set
# CONFIG_EFUSE_VIRTUAL is not set
# CONFIG_EFUSE_CODE_SCHEME_COMPAT_NONE is not set
CONFIG_EFUSE_CODE_SCHEME_COMPAT_3_4=y
# CONFIG_EFUSE_CODE_SCHEME_COMPAT_REPEAT is not set
CONFIG_EFUSE_MAX_BLK_LEN=192
# end of eFuse Bit Manager

#
# ESP-TLS
#
CONFIG_ESP_TLS_USING_MBEDTLS=y
# CONFIG_ESP_TLS_SERVER is not set
# CONFIG_ESP_TLS_PSK_VERIFICATION is not set
# end of ESP-TLS

#
# ESP32-specific
#
CONFIG_ESP32_REV_MIN_0=y
# CONFIG_ESP32_REV_MIN_1 is not set
# CONFIG_ESP32_REV_MIN_2 is not set
# CONFIG_ESP32_REV_MIN_3 is not set
CONFIG_ESP32_REV_MIN=0
CONFIG_ESP32_DPORT_WORKAROUND=y
# CONFIG_ESP32_DEFAULT_CPU_FREQ_80 is not set
# CONFIG_ESP32_DEFAULT_CPU_FREQ_160 is not set
CONFIG_ESP32_DEFAULT_CPU_FREQ_240=y
CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ=240
# CONFIG_ESP32_SPIRAM_SUPPORT is not set
# CONFIG_ESP32_TRAX is not set
CONFIG_ESP32_TRACEMEM_RESERVE_DRAM=0x0
# CONFIG_ESP32_UNIVERSAL_MAC_ADDRESSES_TWO is not set
CONFIG_ESP32_UNIVERSAL_MAC_ADDRESSES_FOUR=y
CONFIG_ESP32_UNIVERSAL_MAC_ADDRESSES=4
# CONFIG_ESP32_ULP_COPROC_ENABLED is not set
CONFIG_ESP32_ULP_COPROC_RESERVE_MEM=0
# CONFIG_ESP32_PANIC_PRINT_HALT is not set
CONFIG_ESP32_PANIC_PRINT_REBOOT=y
# CONFIG_ESP32_PANIC_SILENT_REBOOT is not set
# CONFIG_ESP32_PANIC_GDBSTUB is not set
CONFIG_ESP32_DEBUG_OCDAWARE=y
CONFIG_ESP32_BROWNOUT_DET=y
CONFIG_ESP32_BROWNOUT_DET_LVL_SEL_0=y
# CONFIG_ESP32_BROWNOUT_DET_LVL_SEL_1 is not set
# CONFIG_ESP32_BROWNOUT_DET_LVL_SEL_2 is not set
# CONFIG_ESP32_BROWNOUT_DET_LVL_SEL_3 is not set
# CONFIG_ESP32_BROWNOUT_DET_LVL_SEL_4 is not set
# CONFIG_ESP32_BROWNOUT_DET_LVL_SEL_5 is not set
# CONFIG_ESP32_BROWNOUT_DET_LVL_SEL_6 is not set
# CONFIG_ESP32_BROWNOUT_DET_LVL_SEL_7 is not set
CONFIG_ESP32_BROWNOUT_DET_LVL=0
CONFIG_ESP32_REDUCE_PHY_TX_POWER=y
CONFIG_ESP32_TIME_SYSCALL_USE_RTC_FRC1=y
# CONFIG_ESP32_TIME_SYSCALL_USE_RTC is not set
# CONFIG_ESP32_TIME_SYSCALL_USE_FRC1 is not set
# CONFIG_ESP32_TIME_SYSCALL_USE_NONE is not set
# CONFIG_ESP32_RTC_CLK_SRC_INT_RC is not set
CONFIG_ESP32_RTC_CLK_SRC_EXT_CRYS=y
# CONFIG_ESP32_RTC_CLK_SRC_EXT_OSC is not set
# CONFIG_ESP32_RTC_CLK_SRC_INT_8MD256 is not set
# CONFIG_ESP32_RTC_EXT_CRYST_ADDIT_CURRENT is not set
CONFIG_ESP32_RTC_CLK_CAL_CYCLES=1024
CONFIG_ESP32_RTC_XTAL_CAL_RETRY=1
CONFIG_ESP32_RTC_XTAL_BOOTSTRAP_CYCLES=5
CONFIG_ESP32_DEEP_SLEEP_WAKEUP_DELAY=2000
CONFIG_ESP32_XTAL_FREQ_40=y
# CONFIG_ESP32_XTAL_FREQ_26 is not set
# CONFIG_ESP32_XTAL_FREQ_AUTO is not set
CONFIG_ESP32_XTAL_FREQ=40
# CONFIG_ESP32_DISABLE_BASIC_ROM_CONSOLE is not set
# CONFIG_ESP32_NO_BLOBS is not set
# CONFIG_ESP32_COMPATIBLE_PRE_V2_1_BOOTLOADERS is not set
# CONFIG_ESP32_USE_FIXED_STATIC_RAM_SIZE is not set
CONFIG_ESP32_DPORT_DIS_INTERRUPT_LVL=5
# end of ESP32-specific

#
# Power Management
#
# CONFIG_PM_ENABLE is not set
# end of Power Management

#
# ADC-Calibration
#
CONFIG_ADC_CAL_EFUSE_TP_ENABLE=y
CONFIG_ADC_CAL_EFUSE_VREF_ENABLE=y
CONFIG_ADC_CAL_LUT_ENABLE=y
# end of ADC-Calibration

#
# Common ESP-related
#
# CONFIG_ESP_TIMER_PROFILING is not set
CONFIG_ESP_ERR_TO_NAME_LOOKUP=y
CONFIG_ESP_SYSTEM_EVENT_QUEUE_SIZE=32
CONFIG_ESP_SYSTEM_EVENT_TASK_STACK_SIZE=2048
CONFIG_ESP_MAIN_TASK_STACK_SIZE=4096
CONFIG_ESP_IPC_TASK_STACK_SIZE=1024
CONFIG_ESP_IPC_USES_CALLERS_PRIORITY=y
CONFIG_ESP_TIMER_TASK_STACK_SIZE=3584
CONFIG_ESP_MINIMAL_SHARED_STACK_SIZE=2048
CONFIG_ESP_CONSOLE_UART_DEFAULT=y
# CONFIG_ESP_CONSOLE_UART_CUSTOM is not set
# CONFIG_ESP_CONSOLE_UART_NONE is not set
CONFIG_ESP_CONSOLE_UART_NUM=0
CONFIG_ESP_CONSOLE_UART_BAUDRATE=115200
CONFIG_ESP_INT_WDT=y
CONFIG_ESP_INT_WDT_TIMEOUT_MS=300
CONFIG_ESP_INT_WDT_CHECK_CPU1=y
CONFIG_ESP_TASK_WDT=y
# CONFIG_ESP_TASK_WDT_PANIC is not set
CONFIG_ESP_TASK_WDT_TIMEOUT_S=5
CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU0=y
CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU1=y
# CONFIG_ESP_PANIC_HANDLER_IRAM is not set
# end of Common ESP-related

#
# Ethernet
#
CONFIG_ETH_ENABLED=y
CONFIG_ETH_USE_ESP32_EMAC=y
CONFIG_ETH_PHY_INTERFACE_RMII=y
# CONFIG_ETH_PHY_INTERFACE_MII is not set
CONFIG_ETH_RMII_CLK_INPUT=y
# CONFIG_ETH_RMII_CLK_OUTPUT is not set
CONFIG_ETH_RMII_CLK_IN_GPIO=0
CONFIG_ETH_DMA_BUFFER_SIZE=512
CONFIG_ETH_DMA_RX_BUFFER_NUM=10
CONFIG_ETH_DMA_TX_BUFFER_NUM=10
CONFIG_ETH_USE_SPI_ETHERNET=y
CONFIG_ETH_SPI_ETHERNET_DM9051=y
# CONFIG_ETH_USE_OPENETH is not set
# end of Ethernet

#
# Event Loop Library
#
# CONFIG_ESP_EVENT_LOOP_PROFILING is not set
CONFIG_ESP_EVENT_POST_FROM_ISR=y
CONFIG_ESP_EVENT_POST_FROM_IRAM_ISR=y
# end of Event Loop Library

#
# GDB Stub
#
# end of GDB Stub

#
# ESP HTTP client
#
CONFIG_ESP_HTTP_CLIENT_ENABLE_HTTPS=y
# CONFIG_ESP_HTTP_CLIENT_ENABLE_BASIC_AUTH is not set
# end of ESP HTTP client

#
# HTTP Server
#
CONFIG_HTTPD_MAX_REQ_HDR_LEN=1024
CONFIG_HTTPD_MAX_URI_LEN=512
CONFIG_HTTPD_ERR_RESP_NO_DELAY=y
CONFIG_HTTPD_PURGE_BUF_LEN=32
# CONFIG_HTTPD_LOG_PURGE_DATA is not set
# end of HTTP Server

#
# ESP HTTPS OTA
#
# CONFIG_OTA_ALLOW_HTTP is not set
# end of ESP HTTPS OTA

#
# ESP HTTPS server
#
# CONFIG_ESP_HTTPS_SERVER_ENABLE is not set
# end of ESP HTTPS server

#
# ESP NETIF Adapter
#
CONFIG_ESP_NETIF_IP_LOST_TIMER_INTERVAL=120
CONFIG_ESP_NETIF_TCPIP_LWIP=y
# CONFIG_ESP_NETIF_LOOPBACK is not set
CONFIG_ESP_NETIF_TCPIP_ADAPTER_COMPATIBLE_LAYER=y
# end of ESP NETIF Adapter

#
# Wi-Fi
#
CONFIG_ESP32_WIFI_STATIC_RX_BUFFER_NUM=10
CONFIG_ESP32_WIFI_DYNAMIC_RX_BUFFER_NUM=32
# CONFIG_ESP32_WIFI_STATIC_TX_BUFFER is not set
CONFIG_ESP32_WIFI_DYNAMIC_TX_BUFFER=y
CONFIG_ESP32_WIFI_TX_BUFFER_TYPE=1
CONFIG_ESP32_WIFI_DYNAMIC_TX_BUFFER_NUM=32
# CONFIG_ESP32_WIFI_CSI_ENABLED is not set
CONFIG_ESP32_WIFI_AMPDU_TX_ENABLED=y
CONFIG_ESP32_WIFI_TX_BA_WIN=6
CONFIG_ESP32_WIFI_AMPDU_RX_ENABLED=y
CONFIG_ESP32_WIFI_RX_BA_WIN=6
CONFIG_ESP32_WIFI_NVS_ENABLED=y
CONFIG_ESP32_WIFI_TASK_PINNED_TO_CORE_0=y
# CONFIG_ESP32_WIFI_TASK_PINNED_TO_CORE_1 is not set
CONFIG_ESP32_WIFI_SOFTAP_BEACON_MAX_LEN=752
CONFIG_ESP32_WIFI_MGMT_SBUF_NUM=32
# CONFIG_ESP32_WIFI_DEBUG_LOG_ENABLE is not set
CONFIG_ESP32_WIFI_IRAM_OPT=y
CONFIG_ESP32_WIFI_RX_IRAM_OPT=y
CONFIG_ESP32_WIFI_ENABLE_WPA3_SAE=y
# end of Wi-Fi

#
# PHY
#
CONFIG_ESP32_PHY_CALIBRATION_AND_DATA_STORAGE=y
# CONFIG_ESP32_PHY_INIT_DATA_IN_PARTITION is not set
CONFIG_ESP32_PHY_MAX_WIFI_TX_POWER=20
CONFIG_ESP32_PHY_MAX_TX_POWER=20
# end of PHY

#
# Core dump
#
# CONFIG_ESP32_ENABLE_COREDUMP_TO_FLASH is not set
CONFIG_ESP32_ENABLE_COREDUMP_TO_UART=y
# CONFIG_ESP32_ENABLE_COREDUMP_TO_NONE is not set
# CONFIG_ESP32_COREDUMP_DATA_FORMAT_BIN is not set
CONFIG_ESP32_COREDUMP_DATA_FORMAT_ELF=y
CONFIG_ESP32_COREDUMP_CHECKSUM_CRC32=y
# CONFIG_ESP32_COREDUMP_CHECKSUM_SHA256 is not set
CONFIG_ESP32_ENABLE_COREDUMP=y
CONFIG_ESP32_CORE_DUMP_MAX_TASKS_NUM=64
CONFIG_ESP32_CORE_DUMP_UART_DELAY=0
CONFIG_ESP32_CORE_DUMP_STACK_SIZE=0
# end of Core dump

#
# FAT Filesystem support
#
# CONFIG_FATFS_CODEPAGE_DYNAMIC is not set
CONFIG_FATFS_CODEPAGE_437=y
# CONFIG_FATFS_CODEPAGE_720 is not set
# CONFIG_FATFS_CODEPAGE_737 is not set
# CONFIG_FATFS_CODEPAGE_771 is not set
# CONFIG_FATFS_CODEPAGE_775 is not set
# CONFIG_FATFS_CODEPAGE_850 is not set
# CONFIG_FATFS_CODEPAGE_852 is not set
# CONFIG_FATFS_CODEPAGE_855 is not set
# CONFIG_FATFS_CODEPAGE_857 is not set
# CONFIG_FATFS_CODEPAGE_860 is not set
# CONFIG_FATFS_CODEPAGE_861 is not set
# CONFIG_FATFS_CODEPAGE_862 is not set
# CONFIG_FATFS_CODEPAGE_863 is not set
# CONFIG_FATFS_CODEPAGE_864 is not set
# CONFIG_FATFS_CODEPAGE_865 is not set
# CONFIG_FATFS_CODEPAGE_866 is not set
# CONFIG_FATFS_CODEPAGE_869 is not set
# CONFIG_FATFS_CODEPAGE_932 is not set
# CONFIG_FATFS_CODEPAGE_936 is not set
# CONFIG_FATFS_CODEPAGE_949 is not set
# CONFIG_FATFS_CODEPAGE_950 is not set
CONFIG_FATFS_CODEPAGE=437
CONFIG_FATFS_LFN_NONE=y
# CONFIG_FATFS_LFN_HEAP is not set
# CONFIG_FATFS_LFN_STACK is not set
CONFIG_FATFS_FS_LOCK=0
CONFIG_FATFS_TIMEOUT_MS=10000
CONFIG_FATFS_PER_FILE_CACHE=y
# end of FAT Filesystem support

#
# Modbus configuration
#
CONFIG_FMB_COMM_MODE_RTU_EN=y
CONFIG_FMB_COMM_MODE_ASCII_EN=y
CONFIG_FMB_MASTER_TIMEOUT_MS_RESPOND=150
CONFIG_FMB_MASTER_DELAY_MS_CONVERT=200
CONFIG_FMB_QUEUE_LENGTH=20
CONFIG_FMB_SERIAL_TASK_STACK_SIZE=2048
CONFIG_FMB_SERIAL_BUF_SIZE=256
CONFIG_FMB_SERIAL_ASCII_BITS_PER_SYMB=8
CONFIG_FMB_SERIAL_ASCII_TIMEOUT_RESPOND_MS=1000
CONFIG_FMB_SERIAL_TASK_PRIO=10
# CONFIG_FMB_CONTROLLER_SLAVE_ID_SUPPORT is not set
CONFIG_FMB_CONTROLLER_NOTIFY_TIMEOUT=20
CONFIG_FMB_CONTROLLER_NOTIFY_QUEUE_SIZE=20
CONFIG_FMB_CONTROLLER_STACK_SIZE=4096
CONFIG_FMB_EVENT_QUEUE_TIMEOUT=20
CONFIG_FMB_TIMER_PORT_ENABLED=y
CONFIG_FMB_TIMER_GROUP=0
CONFIG_FMB_TIMER_INDEX=0
# CONFIG_FMB_TIMER_ISR_IN_IRAM is not set
# end of Modbus configuration

#
# FreeRTOS
#
# CONFIG_FREERTOS_UNICORE is not set
CONFIG_FREERTOS_NO_AFFINITY=0x7FFFFFFF
CONFIG_FREERTOS_CORETIMER_0=y
# CONFIG_FREERTOS_CORETIMER_1 is not set
CONFIG_FREERTOS_HZ=1000
CONFIG_FREERTOS_ASSERT_ON_UNTESTED_FUNCTION=y
# CONFIG_FREERTOS_CHECK_STACKOVERFLOW_NONE is not set
CONFIG_FREERTOS_CHECK_STACKOVERFLOW_PTRVAL=y
# CONFIG_FREERTOS_CHECK_STACKOVERFLOW_CANARY is not set
# CONFIG_FREERTOS_WATCHPOINT_END_OF_STACK is not set
CONFIG_FREERTOS_INTERRUPT_BACKTRACE=y
CONFIG_FREERTOS_THREAD_LOCAL_STORAGE_POINTERS=3
CONFIG_FREERTOS_ASSERT_FAIL_ABORT=y
# CONFIG_FREERTOS_ASSERT_FAIL_PRINT_CONTINUE is not set
# CONFIG_FREERTOS_ASSERT_DISABLE is not set
CONFIG_FREERTOS_IDLE_TASK_STACKSIZE=1024
CONFIG_FREERTOS_ISR_STACKSIZE=1536
# CONFIG_FREERTOS_LEGACY_HOOKS is not set
CONFIG_FREERTOS_MAX_TASK_NAME_LEN=16
# CONFIG_FREERTOS_SUPPORT_STATIC_ALLOCATION is not set
CONFIG_FREERTOS_TIMER_TASK_PRIORITY=1
CONFIG_FREERTOS_TIMER_TASK_STACK_DEPTH=2048
CONFIG_FREERTOS_TIMER_QUEUE_LENGTH=10
CONFIG_FREERTOS_QUEUE_REGISTRY_SIZE=0
# CONFIG_FREERTOS_USE_TRACE_FACILITY is not set
# CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS is not set
# CONFIG_FREERTOS_DEBUG_INTERNALS is not set
CONFIG_FREERTOS_TASK_FUNCTION_WRAPPER=y
CONFIG_FREERTOS_CHECK_MUTEX_GIVEN_BY_OWNER=y
# CONFIG_FREERTOS_CHECK_PORT_CRITICAL_COMPLIANCE is not set
CONFIG_FREERTOS_DEBUG_OCDAWARE=y
# end of FreeRTOS

#
# Heap memory debugging
#
CONFIG_HEAP_POISONING_DISABLED=y
# CONFIG_HEAP_POISONING_LIGHT is not set
# CONFIG_HEAP_POISONING_COMPREHENSIVE is not set
CONFIG_HEAP_TRACING_OFF=y
# CONFIG_HEAP_TRACING_STANDALONE is not set
# CONFIG_HEAP_TRACING_TOHOST is not set
# end of Heap memory debugging

#
# jsmn
#
# CONFIG_JSMN_PARENT_LINKS is not set
# CONFIG_JSMN_STRICT is not set
# end of jsmn

#
# libsodium
#
CONFIG_LIBSODIUM_USE_MBEDTLS_SHA=y
# end of libsodium

#
# Log output
#
# CONFIG_LOG_DEFAULT_LEVEL_NONE is not set
# CONFIG_LOG_DEFAULT_LEVEL_ERROR is not set
# CONFIG_LOG_DEFAULT_LEVEL_WARN is not set
CONFIG_LOG_DEFAULT_LEVEL_INFO=y
# CONFIG_LOG_DEFAULT_LEVEL_DEBUG is not set
# CONFIG_LOG_DEFAULT_LEVEL_VERBOSE is not set
CONFIG_LOG_DEFAULT_LEVEL=3
CONFIG_LOG_COLORS=y
CONFIG_LOG_TIMESTAMP_SOURCE_RTOS=y
# CONFIG_LOG_TIMESTAMP_SOURCE_SYSTEM is not set
# end of Log output

#
# LWIP
#
CONFIG_LWIP_LOCAL_HOSTNAME="espressif"
CONFIG_LWIP_DNS_SUPPORT_MDNS_QUERIES=y
# CONFIG_LWIP_L2_TO_L3_COPY is not set
# CONFIG_LWIP_IRAM_OPTIMIZATION is not set
CONFIG_LWIP_TIMERS_ONDEMAND=y
CONFIG_LWIP_MAX_SOCKETS=6
CONFIG_LWIP_USE_ONLY_LWIP_SELECT=y
# CONFIG_LWIP_SO_LINGER is not set
CONFIG_LWIP_SO_REUSE=y
CONFIG_LWIP_SO_REUSE_RXTOALL=y
# CONFIG_LWIP_SO_RCVBUF is not set
# CONFIG_LWIP_NETBUF_RECVINFO is not set
CONFIG_LWIP_IP4_FRAG=y
CONFIG_LWIP_IP6_FRAG=y
# CONFIG_LWIP_IP4_REASSEMBLY is not set
# CONFIG_LWIP_IP6_REASSEMBLY is not set
# CONFIG_LWIP_STATS is not set
CONFIG_LWIP_ETHARP_TRUST_IP_MAC=y
CONFIG_LWIP_ESP_GRATUITOUS_ARP=y
CONFIG_LWIP_GARP_TMR_INTERVAL=60
CONFIG_LWIP_TCPIP_RECVMBOX_SIZE=32
CONFIG_LWIP_DHCP_DOES_ARP_CHECK=y
# CONFIG_LWIP_DHCP_RESTORE_LAST_IP is not set

#
# DHCP server
#
CONFIG_LWIP_DHCPS_LEASE_UNIT=60
CONFIG_LWIP_DHCPS_MAX_STATION_NUM=8
# end of DHCP server

# CONFIG_LWIP_AUTOIP is not set
# CONFIG_LWIP_IPV6_AUTOCONFIG is not set
CONFIG_LWIP_NETIF_LOOPBACK=y
CONFIG_LWIP_LOOPBACK_MAX_PBUFS=8

#
# TCP
#
CONFIG_LWIP_TCP_ISN_HOOK=y
CONFIG_LWIP_MAX_ACTIVE_TCP=16
CONFIG_LWIP_MAX_LISTENING_TCP=16
CONFIG_LWIP_TCP_HIGH_SPEED_RETRANSMISSION=y
CONFIG_LWIP_TCP_MAXRTX=12
CONFIG_LWIP_TCP_SYNMAXRTX=6
CONFIG_LWIP_TCP_MSS=1436
CONFIG_LWIP_TCP_TMR_INTERVAL=250
CONFIG_LWIP_TCP_MSL=60000
CONFIG_LWIP_TCP_SND_BUF_DEFAULT=5744
CONFIG_LWIP_TCP_WND_DEFAULT=5744
CONFIG_LWIP_TCP_RECVMBOX_SIZE=6
CONFIG_LWIP_TCP_QUEUE_OOSEQ=y
# CONFIG_LWIP_TCP_SACK_OUT is not set
# CONFIG_LWIP_TCP_KEEP_CONNECTION_WHEN_IP_CHANGES is not set
CONFIG_LWIP_TCP_OVERSIZE_MSS=y
# CONFIG_LWIP_TCP_OVERSIZE_QUARTER_MSS is not set
# CONFIG_LWIP_TCP_OVERSIZE_DISABLE is not set
CONFIG_LWIP_TCP_RTO_TIME=1500
# end of TCP

#
# UDP
#
CONFIG_LWIP_MAX_UDP_PCBS=16
CONFIG_LWIP_UDP_RECVMBOX_SIZE=6
# end of UDP

CONFIG_LWIP_TCPIP_TASK_STACK_SIZE=2048
CONFIG_LWIP_TCPIP_TASK_AFFINITY_NO_AFFINITY=y
# CONFIG_LWIP_TCPIP_TASK_AFFINITY_CPU0 is not set
# CONFIG_LWIP_TCPIP_TASK_AFFINITY_CPU1 is not set
CONFIG_LWIP_TCPIP_TASK_AFFINITY=0x7FFFFFFF
# CONFIG_LWIP_PPP_SUPPORT is not set
CONFIG_LWIP_IPV6_MEMP_NUM_ND6_QUEUE=3
CONFIG_LWIP_IPV6_ND6_NUM_NEIGHBORS=5

#
# ICMP
#
# CONFIG_LWIP_MULTICAST_PING is not set
# CONFIG_LWIP_BROADCAST_PING is not set
# end of ICMP

#
# LWIP RAW API
#
CONFIG_LWIP_MAX_RAW_PCBS=16
# end of LWIP RAW API

#
# SNTP
#
CONFIG_LWIP_DHCP_MAX_NTP_SERVERS=1
CONFIG_LWIP_SNTP_UPDATE_DELAY=3600000
# end of SNTP
# end of LWIP

#
# mbedTLS
#
CONFIG_MBEDTLS_INTERNAL_MEM_ALLOC=y
# CONFIG_MBEDTLS_DEFAULT_MEM_ALLOC is not set
# CONFIG_MBEDTLS_CUSTOM_MEM_ALLOC is not set
CONFIG_MBEDTLS_SSL_MAX_CONTENT_LEN=16384
# CONFIG_MBEDTLS_ASYMMETRIC_CONTENT_LEN is not set
# CONFIG_MBEDTLS_DEBUG is not set
# CONFIG_MBEDTLS_ECP_RESTARTABLE is not set
# CONFIG_MBEDTLS_CMAC_C is not set
CONFIG_MBEDTLS_HARDWARE_AES=y
CONFIG_MBEDTLS_HARDWARE_MPI=y
# CONFIG_MBEDTLS_HARDWARE_SHA is not set
CONFIG_MBEDTLS_HAVE_TIME=y
# CONFIG_MBEDTLS_HAVE_TIME_DATE is not set
CONFIG_MBEDTLS_TLS_SERVER_AND_CLIENT=y
# CONFIG_MBEDTLS_TLS_SERVER_ONLY is not set
# CONFIG_MBEDTLS_TLS_CLIENT_ONLY is not set
# CONFIG_MBEDTLS_TLS_DISABLED is not set
CONFIG_MBEDTLS_TLS_SERVER=y
CONFIG_MBEDTLS_TLS_CLIENT=y
CONFIG_MBEDTLS_TLS_ENABLED=y

#
# TLS Key Exchange Methods
#
# CONFIG_MBEDTLS_PSK_MODES is not set
CONFIG_MBEDTLS_KEY_EXCHANGE_RSA=y
CONFIG_MBEDTLS_KEY_EXCHANGE_DHE_RSA=y
CONFIG_MBEDTLS_KEY_EXCHANGE_ELLIPTIC_CURVE=y
CONFIG_MBEDTLS_KEY_EXCHANGE_ECDHE_RSA=y
CONFIG_MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA=y
CONFIG_MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA=y
CONFIG_MBEDTLS_KEY_EXCHANGE_ECDH_RSA=y
# end of TLS Key Exchange Methods

CONFIG_MBEDTLS_SSL_RENEGOTIATION=y
# CONFIG_MBEDTLS_SSL_PROTO_SSL3 is not set
CONFIG_MBEDTLS_SSL_PROTO_TLS1=y
CONFIG_MBEDTLS_SSL_PROTO_TLS1_1=y
CONFIG_MBEDTLS_SSL_PROTO_TLS1_2=y
# CONFIG_MBEDTLS_SSL_PROTO_DTLS is not set
CONFIG_MBEDTLS_SSL_ALPN=y
CONFIG_MBEDTLS_CLIENT_SSL_SESSION_TICKETS=y
CONFIG_MBEDTLS_SERVER_SSL_SESSION_TICKETS=y

#
# Symmetric Ciphers
#
CONFIG_MBEDTLS_AES_C=y
# CONFIG_MBEDTLS_CAMELLIA_C is not set
# CONFIG_MBEDTLS_DES_C is not set
CONFIG_MBEDTLS_RC4_DISABLED=y
# CONFIG_MBEDTLS_RC4_ENABLED_NO_DEFAULT is not set
# CONFIG_MBEDTLS_RC4_ENABLED is not set
# CONFIG_MBEDTLS_BLOWFISH_C is not set
# CONFIG_MBEDTLS_XTEA_C is not set
CONFIG_MBEDTLS_CCM_C=y
CONFIG_MBEDTLS_GCM_C=y
# end of Symmetric Ciphers

# CONFIG_MBEDTLS_RIPEMD160_C is not set

#
# Certificates
#
CONFIG_MBEDTLS_PEM_PARSE_C=y
CONFIG_MBEDTLS_PEM_WRITE_C=y
CONFIG_MBEDTLS_X509_CRL_PARSE_C=y
CONFIG_MBEDTLS_X509_CSR_PARSE_C=y
# end of Certificates

CONFIG_MBEDTLS_ECP_C=y
CONFIG_MBEDTLS_ECDH_C=y
CONFIG_MBEDTLS_ECDSA_C=y
CONFIG_MBEDTLS_ECP_DP_SECP192R1_ENABLED=y
CONFIG_MBEDTLS_ECP_DP_SECP224R1_ENABLED=y
CONFIG_MBEDTLS_ECP_DP_SECP256R1_ENABLED=y
CONFIG_MBEDTLS_ECP_DP_SECP384R1_ENABLED=y
CONFIG_MBEDTLS_ECP_DP_SECP521R1_ENABLED=y
CONFIG_MBEDTLS_ECP_DP_SECP192K1_ENABLED=y
CONFIG_MBEDTLS_ECP_DP_SECP224K1_ENABLED=y
CONFIG_MBEDTLS_ECP_DP_SECP256K1_ENABLED=y
CONFIG_MBEDTLS_ECP_DP_BP256R1_ENABLED=y
CONFIG_MBEDTLS_ECP_DP_BP384R1_ENABLED=y
CONFIG_MBEDTLS_ECP_DP_BP512R1_ENABLED=y
CONFIG_MBEDTLS_ECP_DP_CURVE25519_ENABLED=y
CONFIG_MBEDTLS_ECP_NIST_OPTIM=y
# CONFIG_MBEDTLS_SECURITY_RISKS is not set
# end of mbedTLS

#
# mDNS
#
CONFIG_MDNS_MAX_SERVICES=10
CONFIG_MDNS_TASK_PRIORITY=1
# CONFIG_MDNS_TASK_AFFINITY_NO_AFFINITY is not set
CONFIG_MDNS_TASK_AFFINITY_CPU0=y
# CONFIG_MDNS_TASK_AFFINITY_CPU1 is not set
CONFIG_MDNS_TASK_AFFINITY=0x0
CONFIG_MDNS_SERVICE_ADD_TIMEOUT_MS=2000
CONFIG_MDNS_TIMER_PERIOD_MS=100
# end of mDNS

#
# ESP-MQTT Configurations
#
CONFIG_MQTT_PROTOCOL_311=y
CONFIG_MQTT_TRANSPORT_SSL=y
CONFIG_MQTT_TRANSPORT_WEBSOCKET=y
CONFIG_MQTT_TRANSPORT_WEBSOCKET_SECURE=y
# CONFIG_MQTT_USE_CUSTOM_CONFIG is not set
# CONFIG_MQTT_TASK_CORE_SELECTION_ENABLED is not set
# CONFIG_MQTT_CUSTOM_OUTBOX is not set
# end of ESP-MQTT Configurations

#
# Newlib
#
# CONFIG_NEWLIB_STDOUT_LINE_ENDING_CRLF is not set
CONFIG_NEWLIB_STDOUT_LINE_ENDING_LF=y
# CONFIG_NEWLIB_STDOUT_LINE_ENDING_CR is not set
# CONFIG_NEWLIB_STDIN_LINE_ENDING_CRLF is not set
CONFIG_NEWLIB_STDIN_LINE_ENDING_LF=y
# CONFIG_NEWLIB_STDIN_LINE_ENDING_CR is not set
# CONFIG_NEWLIB_NANO_FORMAT is not set
# end of Newlib

#
# NVS
#
# end of NVS

#
# OpenSSL
#
# CONFIG_OPENSSL_DEBUG is not set
CONFIG_OPENSSL_ASSERT_DO_NOTHING=y
# CONFIG_OPENSSL_ASSERT_EXIT is not set
# end of OpenSSL

#
# PThreads
#
CONFIG_PTHREAD_TASK_PRIO_DEFAULT=5
CONFIG_PTHREAD_TASK_STACK_SIZE_DEFAULT=3072
CONFIG_PTHREAD_STACK_MIN=768
CONFIG_PTHREAD_DEFAULT_CORE_NO_AFFINITY=y
# CONFIG_PTHREAD_DEFAULT_CORE_0 is not set
# CONFIG_PTHREAD_DEFAULT_CORE_1 is not set
CONFIG_PTHREAD_TASK_CORE_DEFAULT=-1
CONFIG_PTHREAD_TASK_NAME_DEFAULT="pthread"
# end of PThreads

#
# SPI Flash driver
#
CONFIG_SPI_FLASH_VERIFY_WRITE=y
# CONFIG_SPI_FLASH_LOG_FAILED_WRITE is not set
# CONFIG_SPI_FLASH_WARN_SETTING_ZERO_TO_ONE is not set
# CONFIG_SPI_FLASH_ENABLE_COUNTERS is not set
CONFIG_SPI_FLASH_ROM_DRIVER_PATCH=y
CONFIG_SPI_FLASH_DANGEROUS_WRITE_ABORTS=y
# CONFIG_SPI_FLASH_DANGEROUS_WRITE_FAILS is not set
# CONFIG_SPI_FLASH_DANGEROUS_WRITE_ALLOWED is not set
# CONFIG_SPI_FLASH_USE_LEGACY_IMPL is not set
# CONFIG_SPI_FLASH_BYPASS_BLOCK_ERASE is not set
CONFIG_SPI_FLASH_YIELD_DURING_ERASE=y
CONFIG_SPI_FLASH_ERASE_YIELD_DURATION_MS=20
CONFIG_SPI_FLASH_ERASE_YIELD_TICKS=1

#
# Auto-detect flash chips
#
CONFIG_SPI_FLASH_SUPPORT_ISSI_CHIP=y
CONFIG_SPI_FLASH_SUPPORT_GD_CHIP=y
# end of Auto-detect flash chips
# end of SPI Flash driver

#
# SPIFFS Configuration
#
CONFIG_SPIFFS_MAX_PARTITIONS=3

#
# SPIFFS Cache Configuration
#
CONFIG_SPIFFS_CACHE=y
CONFIG_SPIFFS_CACHE_WR=y
# CONFIG_SPIFFS_CACHE_STATS is not set
# end of SPIFFS Cache Configuration

CONFIG_SPIFFS_PAGE_CHECK=y
CONFIG_SPIFFS_GC_MAX_RUNS=10
# CONFIG_SPIFFS_GC_STATS is not set
CONFIG_SPIFFS_PAGE_SIZE=256
CONFIG_SPIFFS_OBJ_NAME_LEN=32
# CONFIG_SPIFFS_FOLLOW_SYMLINKS is not set
CONFIG_SPIFFS_USE_MAGIC=y
CONFIG_SPIFFS_USE_MAGIC_LENGTH=y
CONFIG_SPIFFS_META_LENGTH=4
CONFIG_SPIFFS_USE_MTIME=y

#
# Debug Configuration
#
# CONFIG_SPIFFS_DBG is not set
# CONFIG_SPIFFS_API_DBG is not set
# CONFIG_SPIFFS_GC_DBG is not set
# CONFIG_SPIFFS_CACHE_DBG is not set
# CONFIG_SPIFFS_CHECK_DBG is not set
# CONFIG_SPIFFS_TEST_VISUALISATION is not set
# end of Debug Configuration
# end of SPIFFS Configuration

#
# Unity unit testing library
#
CONFIG_UNITY_ENABLE_FLOAT=y
CONFIG_UNITY_ENABLE_DOUBLE=y
# CONFIG_UNITY_ENABLE_COLOR is not set
CONFIG_UNITY_ENABLE_IDF_TEST_RUNNER=y
# CONFIG_UNITY_ENABLE_FIXTURE is not set
# CONFIG_UNITY_ENABLE_BACKTRACE_ON_FAIL is not set
# end of Unity unit testing library

#
# Virtual file system
#
CONFIG_VFS_SUPPRESS_SELECT_DEBUG_OUTPUT=y
CONFIG_VFS_SUPPORT_TERMIOS=y

#
# Host File System I/O (Semihosting)
#
CONFIG_SEMIHOSTFS_MAX_MOUNT_POINTS=1
CONFIG_SEMIHOSTFS_HOST_PATH_MAX_LEN=128
# end of Host File System I/O (Semihosting)
# end of Virtual file system

#
# Wear Levelling
#
# CONFIG_WL_SECTOR_SIZE_512 is not set
CONFIG_WL_SECTOR_SIZE_4096=y
CONFIG_WL_SECTOR_SIZE=4096
# end of Wear Levelling

#
# Wi-Fi Provisioning Manager
#
CONFIG_WIFI_PROV_SCAN_MAX_ENTRIES=16
CONFIG_WIFI_PROV_AUTOSTOP_TIMEOUT=30
# end of Wi-Fi Provisioning Manager

#
# Supplicant
#
CONFIG_WPA_MBEDTLS_CRYPTO=y
# CONFIG_WPA_TLS_V12 is not set
# CONFIG_WPA_WPS_WARS is not set
# CONFIG_WPA_DEBUG_PRINT is not set
# end of Supplicant
# end of Component config

#
# Compatibility options
#
# CONFIG_LEGACY_INCLUDE_COMMON_HEADERS is not set
# end of Compatibility options

# Deprecated options for backward compatibility
CONFIG_TOOLPREFIX="xtensa-esp32-elf-"
# CONFIG_LOG_BOOTLOADER_LEVEL_NONE is not set
# CONFIG_LOG_BOOTLOADER_LEVEL_ERROR is not set
CONFIG_LOG_BOOTLOADER_LEVEL_WARN=y
# CONFIG_LOG_BOOTLOADER_LEVEL_INFO is not set
# CONFIG_LOG_BOOTLOADER_LEVEL_DEBUG is not set
# CONFIG_LOG_BOOTLOADER_LEVEL_VERBOSE is not set
CONFIG_LOG_BOOTLOADER_LEVEL=2
# CONFIG_APP_ROLLBACK_ENABLE is not set
# CONFIG_FLASH_ENCRYPTION_ENABLED is not set
# CONFIG_FLASHMODE_QIO is not set
# CONFIG_FLASHMODE_QOUT is not set
CONFIG_FLASHMODE_DIO=y
# CONFIG_FLASHMODE_DOUT is not set
# CONFIG_MONITOR_BAUD_9600B is not set
# CONFIG_MONITOR_BAUD_57600B is not set
CONFIG_MONITOR_BAUD_115200B=y
# CONFIG_MONITOR_BAUD_230400B is not set
# CONFIG_MONITOR_BAUD_921600B is not set
# CONFIG_MONITOR_BAUD_2MB is not set
# CONFIG_MONITOR_BAUD_OTHER is not set
CONFIG_MONITOR_BAUD_OTHER_VAL=115200
CONFIG_MONITOR_BAUD=115200
CONFIG_COMPILER_OPTIMIZATION_LEVEL_DEBUG=y
# CONFIG_COMPILER_OPTIMIZATION_LEVEL_RELEASE is not set
CONFIG_OPTIMIZATION_ASSERTIONS_ENABLED=y
# CONFIG_OPTIMIZATION_ASSERTIONS_SILENT is not set
# CONFIG_OPTIMIZATION_ASSERTIONS_DISABLED is not set
# CONFIG_CXX_EXCEPTIONS is not set
CONFIG_STACK_CHECK_NONE=y
# CONFIG_STACK_CHECK_NORM is not set
# CONFIG_STACK_CHECK_STRONG is not set
# CONFIG_STACK_CHECK_ALL is not set
# CONFIG_WARN_WRITE_STRINGS is not set
# CONFIG_DISABLE_GCC8_WARNINGS is not set
# CONFIG_ESP32_APPTRACE_DEST_TRAX is not set
CONFIG_ESP32_APPTRACE_DEST_NONE=y
CONFIG_ESP32_APPTRACE_LOCK_ENABLE=y
CONFIG_BTDM_CONTROLLER_BLE_MAX_CONN_EFF=0
CONFIG_BTDM_CONTROLLER_BR_EDR_MAX_ACL_CONN_EFF=0
CONFIG_BTDM_CONTROLLER_BR_EDR_MAX_SYNC_CONN_EFF=0
CONFIG_BTDM_CONTROLLER_PINNED_TO_CORE=0
CONFIG_ADC2_DISABLE_DAC=y
# CONFIG_SPIRAM_SUPPORT is not set
CONFIG_TRACEMEM_RESERVE_DRAM=0x0
# CONFIG_TWO_UNIVERSAL_MAC_ADDRESS is not set
CONFIG_FOUR_UNIVERSAL_MAC_ADDRESS=y
CONFIG_NUMBER_OF_UNIVERSAL_MAC_ADDRESS=4
# CONFIG_ULP_COPROC_ENABLED is not set
CONFIG_ULP_COPROC_RESERVE_MEM=0
CONFIG_BROWNOUT_DET=y
CONFIG_BROWNOUT_DET_LVL_SEL_0=y
# CONFIG_BROWNOUT_DET_LVL_SEL_1 is not set
# CONFIG_BROWNOUT_DET_LVL_SEL_2 is not set
# CONFIG_BROWNOUT_DET_LVL_SEL_3 is not set
# CONFIG_BROWNOUT_DET_LVL_SEL_4 is not set
# CONFIG_BROWNOUT_DET_LVL_SEL_5 is not set
# CONFIG_BROWNOUT_DET_LVL_SEL_6 is not set
# CONFIG_BROWNOUT_DET_LVL_SEL_7 is not set
CONFIG_BROWNOUT_DET_LVL=0
CONFIG_REDUCE_PHY_TX_POWER=y
# CONFIG_ESP32_RTC_CLOCK_SOURCE_INTERNAL_RC is not set
CONFIG_ESP32_RTC_CLOCK_SOURCE_EXTERNAL_CRYSTAL=y
# CONFIG_ESP32_RTC_CLOCK_SOURCE_EXTERNAL_OSC is not set
# CONFIG_ESP32_RTC_CLOCK_SOURCE_INTERNAL_8MD256 is not set
# CONFIG_ESP32_RTC_EXTERNAL_CRYSTAL_ADDITIONAL_CURRENT is not set
# CONFIG_DISABLE_BASIC_ROM_CONSOLE is not set
# CONFIG_NO_BLOBS is not set
# CONFIG_COMPATIBLE_PRE_V2_1_BOOTLOADERS is not set
CONFIG_SYSTEM_EVENT_QUEUE_SIZE=32
CONFIG_SYSTEM_EVENT_TASK_STACK_SIZE=2048
CONFIG_MAIN_TASK_STACK_SIZE=4096
CONFIG_IPC_TASK_STACK_SIZE=1024
CONFIG_TIMER_TASK_STACK_SIZE=3584
CONFIG_CONSOLE_UART_DEFAULT=y
# CONFIG_CONSOLE_UART_CUSTOM is not set
# CONFIG_CONSOLE_UART_NONE is not set
CONFIG_CONSOLE_UART_NUM=0
CONFIG_CONSOLE_UART_BAUDRATE=115200
CONFIG_INT_WDT=y
CONFIG_INT_WDT_TIMEOUT_MS=300
CONFIG_INT_WDT_CHECK_CPU1=y
CONFIG_TASK_WDT=y
# CONFIG_TASK_WDT_PANIC is not set
CONFIG_TASK_WDT_TIMEOUT_S=5
CONFIG_TASK_WDT_CHECK_IDLE_TASK_CPU0=y
CONFIG_TASK_WDT_CHECK_IDLE_TASK_CPU1=y
# CONFIG_EVENT_LOOP_PROFILING is not set
CONFIG_POST_EVENTS_FROM_ISR=y
CONFIG_POST_EVENTS_FROM_IRAM_ISR=y
CONFIG_MB_MASTER_TIMEOUT_MS_RESPOND=150
CONFIG_MB_MASTER_DELAY_MS_CONVERT=200
CONFIG_MB_QUEUE_LENGTH=20
CONFIG_MB_SERIAL_TASK_STACK_SIZE=2048
CONFIG_MB_SERIAL_BUF_SIZE=256
CONFIG_MB_SERIAL_TASK_PRIO=10
# CONFIG_MB_CONTROLLER_SLAVE_ID_SUPPORT is not set
CONFIG_MB_CONTROLLER_NOTIFY_TIMEOUT=20
CONFIG_MB_CONTROLLER_NOTIFY_QUEUE_SIZE=20
CONFIG_MB_CONTROLLER_STACK_SIZE=4096
CONFIG_MB_EVENT_QUEUE_TIMEOUT=20
CONFIG_MB_TIMER_PORT_ENABLED=y
CONFIG_MB_TIMER_GROUP=0
CONFIG_MB_TIMER_INDEX=0
# CONFIG_SUPPORT_STATIC_ALLOCATION is not set
CONFIG_TIMER_TASK_PRIORITY=1
CONFIG_TIMER_TASK_STACK_DEPTH=2048
CONFIG_TIMER_QUEUE_LENGTH=10
# CONFIG_L2_TO_L3_COPY is not set
CONFIG_USE_ONLY_LWIP_SELECT=y
CONFIG_ESP_GRATUITOUS_ARP=y
CONFIG_GARP_TMR_INTERVAL=60
CONFIG_TCPIP_RECVMBOX_SIZE=32
CONFIG_TCP_MAXRTX=12
CONFIG_TCP_SYNMAXRTX=6
CONFIG_TCP_MSS=1436
CONFIG_TCP_MSL=60000
CONFIG_TCP_SND_BUF_DEFAULT=5744
CONFIG_TCP_WND_DEFAULT=5744
CONFIG_TCP_RECVMBOX_SIZE=6
CONFIG_TCP_QUEUE_OOSEQ=y
# CONFIG_ESP_TCP_KEEP_CONNECTION_WHEN_IP_CHANGES is not set
CONFIG_TCP_OVERSIZE_MSS=y
# CONFIG_TCP_OVERSIZE_QUARTER_MSS is not set
# CONFIG_TCP_OVERSIZE_DISABLE is not set
CONFIG_UDP_RECVMBOX_SIZE=6
CONFIG_TCPIP_TASK_STACK_SIZE=2048
CONFIG_TCPIP_TASK_AFFINITY_NO_AFFINITY=y
# CONFIG_TCPIP_TASK_AFFINITY_CPU0 is not set
# CONFIG_TCPIP_TASK_AFFINITY_CPU1 is not set
CONFIG_TCPIP_TASK_AFFINITY=0x7FFFFFFF
# CONFIG_PPP_SUPPORT is not set
CONFIG_ESP32_PTHREAD_TASK_PRIO_DEFAULT=5
CONFIG_ESP32_PTHREAD_TASK_STACK_SIZE_DEFAULT=3072
CONFIG_ESP32_PTHREAD_STACK_MIN=768
CONFIG_ESP32_DEFAULT_PTHREAD_CORE_NO_AFFINITY=y
# CONFIG_ESP32_DEFAULT_PTHREAD_CORE_0 is not set
# CONFIG_ESP32_DEFAULT_PTHREAD_CORE_1 is not set
CONFIG_ESP32_PTHREAD_TASK_CORE_DEFAULT=-1
CONFIG_ESP32_PTHREAD_TASK_NAME_DEFAULT="pthread"
CONFIG_SPI_FLASH_WRITING_DANGEROUS_REGIONS_ABORTS=y
# CONFIG_SPI_FLASH_WRITING_DANGEROUS_REGIONS_FAILS is not set
# CONFIG_SPI_FLASH_WRITING_DANGEROUS_REGIONS_ALLOWED is not set
CONFIG_SUPPRESS_SELECT_DEBUG_OUTPUT=y
CONFIG_SUPPORT_TERMIOS=y
# End of deprecated options
I have attached the project in a zip, I hope this helps!

My thought was that the app_main thread/task that this runs in may have a stack size preallocated. Though this obviously shows my FreeRTOS ineptness as I don't think it runs on a task at all and having no task allocated by this point means it has no effect to stack corruption...despite being identified as "Heap Corruption".
Attachments
BugTest.zip
(11.25 KiB) Downloaded 6 times

rfleming
Posts: 47
Joined: Tue Oct 09, 2018 12:30 am

Re: How is my heap getting corrupted? [Code assistance/review]

Postby rfleming » Fri May 07, 2021 5:44 am

Update. Been playing around with heap checking. I set Heap Debugging to "Comprehensive". Things get a little more interesting from here...

From the previously attached code, I can see that sometimes the private/public key pair can take only a few seconds, but if it takes longer like >5s, the task wdt usually jumps in and triggers an async CORRUPTION message.

Code: Select all

Seeding the random number generator...
 ok
  . Generating the RSA key [ 2048-bit ]...Generating a key pair takes up to 20s and will likely trigger task watchdog during this process
{1B}[0;31mE (10450) task_wdt: Task watchdog got triggered. The following tasks did not reset the watchdog in time:{1B}[0m
{1B}[0;31mE (10450) task_wdt:  - IDLE0 (CPU 0){1B}[0m
{1B}[0;31mE (10450) task_wdt: Tasks currently running:{1B}[0m
{1B}[0;31mE (10450) task_wdt: CPU 0: main{1B}[0m
{1B}[0;31mE (10450) task_wdt: CPU 1: IDLE1{1B}[0m
{1B}[0;31mE (10450) task_wdt: Print CPU 0 (current core) backtrace{1B}[0m


Backtrace:0x400D3C94:0x3FFB1910 0x4008254A:0x3FFB1930 0x0004001E:0x00000000  |<-CORRUPTED

{1B}[0;31mE (10450) task_wdt: Print CPU 1 backtrace{1B}[0m


Backtrace:0x40081943:0x3FFB2140 0x4008254A:0x3FFB2160 0x0004001E:0x00000000  |<-CORRUPTED

RSA keys confirmed
Writing public key to string(PEM format)....
Writing private key to string(PEM format)....
..1..
 ok
  .
CORRUPT HEAP: Bad tail at 0x3ffb6084. Expected 0xbaad5678 got 0x3ffb5297
CORRUPT HEAP: Last block 0x3ffb6088 not 0x3ffdfff8
CORRUPT HEAP: Expected 157452 free bytes counted 0
Success, Key pair created.
Guru Meditation Error: Core  1 panic'ed (LoadProhibited). Exception was unhandled.
Regardless of the time it takes to generate the key, doing a heap checkout outside mbedtls_pk_write_key_pem triggers nothing, but jumping inside and doing a heap check actually inside the function does... :?

outside

Code: Select all

heap_caps_check_integrity_all(1);
puts("..1..");
ret = mbedtls_pk_write_key_pem(&pk, privKeyPem, sizeof(privKeyPem));
inside

Code: Select all

#include "esp_heap_caps.h"

int mbedtls_pk_write_key_pem( mbedtls_pk_context *key, unsigned char *buf, size_t size )
{
    heap_caps_check_integrity_all(1);
    int ret;
    unsigned char output_buf[PRV_DER_MAX_BYTES];
    const char *begin, *end;
    ...
}
The output of this generally is as follows:

Code: Select all

{1B}[0;32mI (155) cpu_start: ESP-IDF:          v4.2.1-dirty{1B}[0m
{1B}[0;32mI (160) cpu_start: Starting app cpu, entry point is 0x4008169c{1B}[0m
{1B}[0;32mI (152) cpu_start: App cpu up.{1B}[0m
{1B}[0;32mI (171) heap_init: Initializing. RAM available for dynamic allocation:{1B}[0m
{1B}[0;32mI (177) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM{1B}[0m
{1B}[0;32mI (184) heap_init: At 3FFB56C8 len 0002A938 (170 KiB): DRAM{1B}[0m
{1B}[0;32mI (190) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM{1B}[0m
{1B}[0;32mI (196) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM{1B}[0m
{1B}[0;32mI (205) heap_init: At 4008A704 len 000158FC (86 KiB): IRAM{1B}[0m
{1B}[0;32mI (209) cpu_start: Pro cpu start user code{1B}[0m
{1B}[0;33mW (375) clk: 32 kHz XTAL not found, switching to internal 150 kHz oscillator{1B}[0m
{1B}[0;32mI (385) spi_flash: detected chip: generic{1B}[0m
{1B}[0;32mI (385) spi_flash: flash io: dio{1B}[0m
{1B}[0;32mI (385) esp_core_dump_uart: Init core dump to UART{1B}[0m
{1B}[0;32mI (388) cpu_start: Starting scheduler on PRO CPU.{1B}[0m
{1B}[0;32mI (0) cpu_start: Starting scheduler on APP CPU.{1B}[0m
Err: Priv key couldn't be loaded
Unable to parse 0xffffc300
Err: Private key couldn't verified
Seeding the random number generator...
 ok
  . Generating the RSA key [ 2048-bit ]...Generating a key pair takes up to 20s and will likely trigger task watchdog during this process
RSA keys confirmed
Writing public key to string(PEM format)....
Writing private key to string(PEM format)....
..1..
 ok
  .
CORRUPT HEAP: Bad tail at 0x3ffb6084. Expected 0xbaad5678 got 0x3ffb5293
CORRUPT HEAP: Last block 0x3ffb6088 not 0x3ffdfff8
CORRUPT HEAP: Expected 157452 free bytes counted 0
Success, Key pair created.
Guru Meditation Error: Core  1 panic'ed (LoadProhibited). Exception was unhandled.

Core  1 register dump:
PC      : 0x40084bdf  PS      : 0x00050033  A0      : 0x40084b61  A1      : 0x3ffb4c08  
A2      : 0x000007d0  A3      : 0x3ffb6098  A4      : 0x40084bab  A5      : 0x3ffb8180  
A6      : 0x3ffb257c  A7      : 0x00050023  A8      : 0xb33fffff  A9      : 0x00050023  
A10     : 0x00000000  A11     : 0x00060823  A12     : 0x80085a08  A13     : 0x3ffb8170  
A14     : 0x00000003  A15     : 0x00060023  SAR     : 0x00000000  EXCCAUSE: 0x0000001c  
EXCVADDR: 0x000007d0  LBEG    : 0x4000c46c  LEND    : 0x4000c477  LCOUNT  : 0xffffffff  

Backtrace:0x40084bdc:0x3ffb4c08 |<-CORRUPTED


ELF file SHA256: b6a5f44216c2081f

{1B}[0;32mI (3084) esp_core_dump_uart: Press Enter to print core dump to UART...{1B}[0m
{1B}[0;32mI (3091) esp_core_dump_uart: Print core dump to uart...{1B}[0m
{1B}[0;32mI (3097) esp_core_dump_elf: Found tasks: 8{1B}[0m
================= CORE DUMP START =================
XC8AAAABAAAIAAAAbAEAAAAAAAA=
f0VMRgEBAQAAAAAAAAAAAAQAXgABAAAAAAAAADQAAAAAAAAAAAAAADQAIAASACgA
...
I hope some else is a bit better at this than me :(

ESP_Angus
Posts: 2315
Joined: Sun May 08, 2016 4:11 am

Re: How is my heap getting corrupted? [Code assistance/review]

Postby ESP_Angus » Fri May 07, 2021 7:33 am

Hi rfleming,

I think your observation is right and it's possible you are overflowing the stack of the main task (meaning: the code in app_main is using more stack than is assigned for the task that runs it). The stack for each task is allocated from the heap, so a major overflow can trigger heap corruption.

This line in particular, after the final crash, suggests something has gone wrong on the stack (only one stack frame):

Code: Select all

Backtrace:0x40084bdc:0x3ffb4c08 |<-CORRUPTED
You can configure this stack size, take a look here:
https://docs.espressif.com/projects/esp ... stack-size

You can also try turning on this option:
https://docs.espressif.com/projects/esp ... d-of-stack

Although not every stack overflow will cause this option to trigger (as noted in the help)

Angus

rfleming
Posts: 47
Joined: Tue Oct 09, 2018 12:30 am

Re: How is my heap getting corrupted? [Code assistance/review]

Postby rfleming » Fri May 07, 2021 11:33 am

Thanks gus,

I was looking at those options, I thought it would even be better to allocate a task for the crypto, wait for it to complete, then destroy the task. This way the app_main allocation won't always be allocated on the stack as the generation only needs to happen on startup.

Futhermore, the real thing in my ignorance without going to stupid sizes, I honestly dont know how to profile how much heap/stack I should be allocating for such a task. Could you provide any suggestions?

I would guess the best way is to check how much stack is allocated (or used) looping through my app many times then simply add like 20% safety. What would be the best way to check stack allocation of a task? I did also notice I could do task_create_static which could help solve a few issues in the future for me if the compiler did the checks for me :o

Thanks again mate.

ESP_Angus
Posts: 2315
Joined: Sun May 08, 2016 4:11 am

Re: How is my heap getting corrupted? [Code assistance/review]

Postby ESP_Angus » Fri May 07, 2021 11:25 pm

Hi rfleming,

Unfortunately at least in C the compiler can't determine maximum stack size (for example, think of recursive functions). I'm unsure if you can check it for arbitrary functions in any language without solving the halting problem!

You can call uxTaskGetStackHighWaterMark(NULL) from a task to return the minimum amount of free stack it has had, up until now. If you set the main task to a very high stack size (say, double or triple the default) and then call this function after all calculations are done, you will have an estimate of the "optimum" stack size that is large enough for all calculations but not wasting too much RAM (of course leave some headroom still, to be safe against any small change in the code!)

Alternatively, your idea of using a dedicated task for this may be a good option as well.

rfleming
Posts: 47
Joined: Tue Oct 09, 2018 12:30 am

Re: How is my heap getting corrupted? [Code assistance/review]

Postby rfleming » Mon May 10, 2021 1:03 am

Following your suggestions I made a few modifications to the c code. I have 2 interesting issues now. 1 is that it doesn't matter what stack size I allocated, I can still corrupt my heap. It usually takes several restarts, but if the key generation takes longer than normal, it can corrupt heap. Second is calling uxTaskGetStackHighWaterMark results in a system crash.

Overview of code changes:
1. Moved crypto stuff into its own task so I can keep the app_main task small.
2. Wait for task to start (hence 10 tick delay)
3. Check and wait until task has complete and deleted from the active list before continuing.

This will allow me to increase that for loop count to test task creation.

Corruption without uxTaskGetStackHighWaterMark

Code: Select all

{1B}[0;32mI (0) cpu_start: Starting scheduler on APP CPU.{1B}[0m
Err: Priv key couldn't be loaded
Unable to parse 0xffffc300
Err: Private key couldn't verified
Seeding the random number generator...
 ok
  . Generating the RSA key [ 2048-bit ]...Generating a key pair takes up to 20s and will likely trigger task watchdog during this process
{1B}[0;31mE (10406) task_wdt: Task watchdog got triggered. The following tasks did not reset the watchdog in time:{1B}[0m
{1B}[0;31mE (10406) task_wdt:  - IDLE1 (CPU 1){1B}[0m
{1B}[0;31mE (10406) task_wdt: Tasks currently running:{1B}[0m
{1B}[0;31mE (10406) task_wdt: CPU 0: IDLE0{1B}[0m
{1B}[0;31mE (10406) task_wdt: CPU 1: CryptoTask{1B}[0m
{1B}[0;31mE (10406) task_wdt: Print CPU 0 (current core) backtrace{1B}[0m


Backtrace:0x400D3CA0:0x3FFB1910 0x4008254E:0x3FFB1930 0x0004001E:0x00000000  |<-CORRUPTED

{1B}[0;31mE (10406) task_wdt: Print CPU 1 backtrace{1B}[0m


Backtrace:0x40081947:0x3FFB2140 0x4008254E:0x3FFB2160 0x0004001E:0x00000000  |<-CORRUPTED

RSA keys confirmed
Writing public key to string(PEM format)....
Writing private key to string(PEM format)....
..1..
 ok
  .
Success, Key pair created.
Test read 255
Complete
Crash trying to use uxTaskGetStackHighWaterMark

Code: Select all

==================== CURRENT THREAD STACK =====================
#0  prvTaskCheckFreeStackSpace (pucStackByte=0xfefefefe <error: Cannot access memory at address 0xfefefefe>) at C:/msys32/home/4Volts/esp/esp-idf/components/freertos/tasks.c:3805
#1  0x40085ef4 in uxTaskGetStackHighWaterMark (xTask=<optimized out>) at C:/msys32/home/4Volts/esp/esp-idf/components/freertos/tasks.c:3839
#2  0x400d4614 in app_main () at ../main/app.cpp:100
#3  0x400d32cd in main_task (args=<optimized out>) at C:/msys32/home/4Volts/esp/esp-idf/components/esp32/cpu_start.c:609
#4  0x40084824 in vPortTaskWrapper (pxCode=0x400d3250 <main_task>, pvParameters=0x0) at C:/msys32/home/4Volts/esp/esp-idf/components/freertos/xtensa/port.c:143
I have enabled heap memory debugging as comprehensive, disabled heap tracing and have heap task tracking enabled.

Current code attached for ease of testing...pls :D

esp-idf v4.2.1 release

app.cpp

Code: Select all

#include <stdio.h>
#include <string.h>

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/timers.h"

#include <esp_system.h>
#include <nvs_flash.h>
#include <sys/param.h>

#include "esp_err.h"
#include "nvs_flash.h"

//app driver includes
#include <mbedtls/pk.h>
#include <mbedtls/error.h>
#include <mbedtls/entropy.h>
#include <mbedtls/ctr_drbg.h>
#include "mbedtls/bignum.h"
#include "mbedtls/x509.h"
#include "mbedtls/rsa.h"
#include "mbedtls/config.h"
#include "mbedtls/platform.h"
#include "mbedtls/base64.h"
#include "esp_partition.h"

static unsigned char privKeyPem[2000];
static unsigned char pubKeyPem[1000];

uint32_t privKeyOffset = 0 * CONFIG_WL_SECTOR_SIZE;
uint32_t pubKeyOffset = 1 * CONFIG_WL_SECTOR_SIZE;
uint32_t tamperDataOffset = 2 * CONFIG_WL_SECTOR_SIZE;
esp_partition_t *partStorage = NULL;

mbedtls_pk_context pk;
mbedtls_entropy_context entropy;
mbedtls_ctr_drbg_context ctrDrbg;

const char personalizationString[11] = "rsa_genkey";	//requires static size allocation in classes
#define KEY_SIZE 2048
#define EXPONENT 65537

bool Crypto_Verify(bool initisalised);
bool Crypto_LoadKeys();
bool Crypto_GenerateKeyPair(void);

static void CryptoTask(void *args) {
	//Assign partition regions
	partStorage = (esp_partition_t *)esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, "storage");

	if (partStorage != NULL) {
		if (!Crypto_LoadKeys()) {
			if (!Crypto_GenerateKeyPair()) {
				puts("!ERROR! Unable to create key pair!");
			}
		}

		//allocate inside function
		uint8_t test[4];

		//test bad alignment...maybe?
		for (int i = 0; i < sizeof(test); i++) {
			esp_partition_read(partStorage, tamperDataOffset, &test[i], 1);
		}

		printf("Test read %d\n", test[0]);
	}
	else {
		puts("Couldn't find partition");
	}

	vTaskDelete(NULL);
}

//uint8_t test[4];
extern "C" void app_main() {
	for (int i = 0; i < 1; i++) {
		TaskHandle_t task = NULL;
		xTaskCreate(&CryptoTask, "CryptoTask", 0x8000, NULL, 5, &task);
		vTaskDelay(10);	 //wait for task to be added
		while (true) {
			//wait to see when task has completed then break
			eTaskState state = eTaskGetState(task);
			if (state == eReady) {
				break;
			}
			vTaskDelay(10);
		}
		puts("Complete");
		unsigned int useage = uxTaskGetStackHighWaterMark(task);
		printf("Used: %d bytes\n\n", useage);
	}

	//app finished...
	while (true) {
		vTaskDelay(1000);
	}
}

bool Crypto_LoadKeys() {
	bool ret = true;
	esp_err_t resp = esp_partition_read(partStorage, privKeyOffset, privKeyPem, sizeof(privKeyPem) - 1);
	if (ESP_OK != resp) {
		puts("Err: Couldn't load priv key from partition");
		return false;
	}

	resp = esp_partition_read(partStorage, pubKeyOffset, pubKeyPem, sizeof(pubKeyPem) - 1);
	if (ESP_OK != resp) {
		puts("Err: Couldn't load pub key from partition");
		ret = false;
	}
	else if (NULL == strstr((const char *)privKeyPem, "-----BEGIN RSA PRIVATE KEY-----") || NULL == strstr((const char *)privKeyPem, "-----END RSA PRIVATE KEY-----")) {
		puts("Err: Priv key couldn't be loaded");
		ret = false;
	}
	else if (NULL == strstr((const char *)pubKeyPem, "-----BEGIN PUBLIC KEY-----") || NULL == strstr((const char *)pubKeyPem, "-----END PUBLIC KEY-----")) {
		puts("Err: Pub key couldn't be loaded");
		ret = false;
	}

	/* pk struct not currently initialised */
	if (!Crypto_Verify(false)) {
		puts("Err: Private key couldn't verified");
		ret = false;
	}

	//null terminate safety
	privKeyPem[sizeof(privKeyPem) - 1] = 0;
	pubKeyPem[sizeof(pubKeyPem) - 1] = 0;

	return ret;
}

bool Crypto_Verify(bool initisalised) {
	int ret = 0;
	/* Key has no password - parse in NULL size 0 */
	if (!initisalised) {
		mbedtls_pk_init(&pk);
		if ((ret = mbedtls_pk_parse_key(&pk, privKeyPem, strlen((const char *)privKeyPem) + 1, NULL, 0)) != 0) {
			printf("Unable to parse 0x%x\n", ret);
			goto exit;
		}
	}

	/* Asumme keys are already loaded into internal variables */
	if ((ret = mbedtls_rsa_check_privkey(mbedtls_pk_rsa(pk))) != 0) {
		puts("Err: RSA context does not contain an rsa private key");
		goto exit;
	}

	if ((ret = mbedtls_rsa_check_pubkey(mbedtls_pk_rsa(pk))) != 0) {
		puts("Err: RSA context does not contain an rsa public key");
		goto exit;
	}

	puts("RSA keys confirmed");

exit:
	if (!initisalised) {
		mbedtls_pk_free(&pk);
	}
	return !ret;
}

bool Crypto_GenerateKeyPair(void) {
	bool ret = false;	 //== ESP_OK

	/* Initialise variables */
	mbedtls_entropy_init(&entropy);
	mbedtls_ctr_drbg_init(&ctrDrbg);
	mbedtls_pk_init(&pk);

	puts("Seeding the random number generator...");
	if ((ret = mbedtls_ctr_drbg_seed(&ctrDrbg, mbedtls_entropy_func, &entropy, (const unsigned char *)personalizationString, strlen(personalizationString))) != 0) {
		printf("Failed to seed rng\nmbedtls_ctr_drbg_seed returned %d\n", ret);
		goto cleanup;
	}

	if ((ret = mbedtls_pk_setup(&pk, mbedtls_pk_info_from_type(MBEDTLS_PK_RSA))) != 0) {
		printf("pk_setup failed: %i\n", ret);
	}

	printf(" ok\n  . Generating the RSA key [ %d-bit ]...", KEY_SIZE);

	/* force task yield before generate a key that could take several seconds to do */
	taskYIELD();
	puts("Generating a key pair takes up to 20s and will likely trigger task watchdog during this process");

	if ((ret = mbedtls_rsa_gen_key(mbedtls_pk_rsa(pk), mbedtls_ctr_drbg_random, &ctrDrbg, KEY_SIZE, EXPONENT)) != 0) {
		printf(" failed\n  ! mbedtls_rsa_gen_key returned %d\n\n", ret);
		goto cleanup;
	}

	/* pk struct already intitialised */
	if (!Crypto_Verify(true)) {
		goto cleanup;
	}

	puts("Writing public key to string(PEM format)....");

	//unsigned char pubKeyPem[1000];
	memset(pubKeyPem, 0, sizeof(pubKeyPem));
	if (mbedtls_pk_write_pubkey_pem(&pk, pubKeyPem, sizeof(pubKeyPem)) != 0) {
		puts("Err: Write public key to string failed");
		goto cleanup;
	}

	puts("Writing private key to string(PEM format)....");
	memset(privKeyPem, 0, sizeof(privKeyPem));

	heap_caps_check_integrity_all(1);
	puts("..1..");

	ret = mbedtls_pk_write_key_pem(&pk, privKeyPem, sizeof(privKeyPem));
	if (ret != 0) {
		printf("write private key to string failed with code %04x\n", ret);
	}
	puts(" ok\n  .");

	heap_caps_check_integrity_all(true);
	puts("Success, Key pair created.");

cleanup:
	mbedtls_pk_free(&pk);
	mbedtls_ctr_drbg_free(&ctrDrbg);
	mbedtls_entropy_free(&entropy);

	return !ret;
}
Attachments
BugTest.zip
(11.99 KiB) Downloaded 1 time

ESP_Angus
Posts: 2315
Joined: Sun May 08, 2016 4:11 am

Re: How is my heap getting corrupted? [Code assistance/review]

Postby ESP_Angus » Mon May 10, 2021 11:55 pm

Hi rfleming,

The reason uxTaskGetStackHighWaterMark is crashing is that a task handle is only valid while the task is valid, after the task has been deleted all of its resources are freed and the task handle itself becomes a pointer to freed memory.

Suggest calling uxTaskGetStackHighWaterMark(NULL) and printing the result immediately before vTaskDelete(NULL), inside the task itself.

(The other way is to replace vTaskDelete(NULL) with vTaskSuspend(NULL) inside the task when it's done, which puts it into a suspended state indefinitely, and then from the other task you can call uxTaskGetStackHighWaterMark(task) and finally vTaskDelete(task) when you're totally done and want to free the resources. But for this kind of quick debugging it's often simpler to do it from inside the target task itself.

Regarding the other unexpected behaviour, will try to take a closer look soon.

Who is online

Users browsing this forum: No registered users and 42 guests