Page 1 of 1

mbedtls sample app fails when using heap allocation

Posted: Fri Jun 16, 2017 4:07 pm
by SteveOfTheStow
Hi,
I'm trying to get mbedtls working on my ESP32 but have encountered a strange problem around allocation.

When I attempt to allocate the mbedtls_ssl_context struct on the heap rather than the stack, the program subsequently fails to allocate memory for the certificates. I've attached a sample program with this issue, based on [url]https://github.com/ARMmbed/mbedtls/blob ... rver.c/url], with the primary change as follows:

Code: Select all

mbedtls_ssl_context ssl;
becomes

Code: Select all

mbedtls_ssl_context *ssl = malloc(sizeof(mbedtls_ssl_context));
Any help would be much appreciated :)

Here's my output from the console:

Code: Select all

rst:0x10 (RTCWDT_RTC_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0x00
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0008,len:8
load:0x3fff0010,len:3444
load:0x40078000,len:10356
load:0x40080000,len:252
entry 0x40080034
I (1135) wifi: wifi firmware version: 41eede3
I (1135) wifi: config NVS flash: enabled
I (1135) wifi: config nano formating: disabled
I (1140) wifi: Init dynamic tx buffer num: 32
I (1141) wifi: Init dynamic rx buffer num: 32
I (1145) wifi: wifi driver task: 3ffcde94, prio:23, stack:4096
I (1150) wifi: Init static rx buffer num: 10
I (1154) wifi: Init dynamic rx buffer num: 32
I (1158) wifi: Init rx ampdu len mblock:7
I (1162) wifi: Init lldesc rx ampdu entry mblock:4
I (1166) wifi: wifi power manager task: 0x3ffd326c prio: 21 stack: 2560
I (1173) wifi: wifi timer task: 3ffd42fc, prio:22, stack:3584
I (1199) wifi: mode : sta (24:0a:c4:04:52:64)
I (2527) wifi: n:11 0, o:1 0, ap:255 255, sta:11 0, prof:1
I (3184) wifi: state: init -> auth (b0)
I (3186) wifi: state: auth -> assoc (0)
I (3191) wifi: state: assoc -> run (10)
I (3213) wifi: connected with LXIII-D, channel 11

  . Loading the server cert. and key…[0m
[0;32mI (4791) PEM: Free RAM: 174432[0m
Guru Meditation Error of type LoadProhibited occurred on core  0. Exception was unhandled.
Register dump:
PC      : 0x40083283  PS      : 0x00060633  A0      : 0x800833e0  A1      : 0x3ffcb7c0  
A2      : 0x3ffd52b8  A3      : 0x00000000  A4      : 0xff000000  A5      : 0x80ffffff  
A6      : 0x00000005  A7      : 0x00000000  A8      : 0x00000000  A9      : 0x00000000  
A10     : 0x3ffc0b68  A11     : 0x00000000  A12     : 0x3ffcc6c8  A13     : 0x00000000  
A14     : 0x00000000  A15     : 0x3ffcb8b0  SAR     : 0x00000004  EXCCAUSE: 0x0000001c  
EXCVADDR: 0x00000000  LBEG    : 0x400014fd  LEND    : 0x4000150d  LCOUNT  : 0xfffffffe  

Backtrace: 0x40083283:0x3ffcb7c0 0x400833e0:0x3ffcb7e0 0x400862b3:0x3ffcb800 0x400862f4:0x3ffcb820 0x40081a9c:0x3ffcb840 0x4000bef8:0x3ffcb860 0x40123710:0x3ffcb880 0x40115e8e:0x3ffcb8d0 0x400def59:0x3ffcb920 0x400df2ea:0x3ffcc310 0x400e0000:0x3ffcc330 0x400e003c:0x3ffcc350

CPU halted.
and where GDB tells me the program halted:

Code: Select all

0x40170954 is in mbedtls_pem_read_buffer (/Users/stephenbarker/bin/espressif/esp32/esp-idf/components/mbedtls/library/pem.c:327).
322	    ret = mbedtls_base64_decode( NULL, 0, &len, s1, s2 - s1 );
323	
324	    if( ret == MBEDTLS_ERR_BASE64_INVALID_CHARACTER )
325	        return( MBEDTLS_ERR_PEM_INVALID_DATA + ret );
326	
327	    if( ( buf = mbedtls_calloc( 1, len ) ) == NULL )
328	        return( MBEDTLS_ERR_PEM_ALLOC_FAILED );
329	
330	    if( ( ret = mbedtls_base64_decode( buf, len, &len, s1, s2 - s1 ) ) != 0 )
331	    {