http_client.c - heap memory overflow

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

http_client.c - heap memory overflow

Jaroslav
Hello,
I am using LWIP 2.1.2 + FreeRTOS 10 on board TM4C1294XL. I am trying to
push measured data to a web server using http_client.c. I mannaged to
make it working based on example in
https://lists.nongnu.org/archive/html/lwip-users/2019-07/msg00001.

However, with each calling of httpc_get_file_dns usage of heap memory
increases, and after approx 500th calling the application stops working
due to heap memory overflow:

MEM HEAP:
         avail: 32768
         used: 31948
         max: 32732
         err: 561

and I am getting the following erros:

httpc_result: 7
memp_malloc: out of memory in pool TCP_PCB
mem_malloc: could not allocate 412 bytes

Here is my simplified code:

--------------------------------------------------------
#define STACKSIZE_HTTPCTASK       4096

err_t
RecvCallback(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
{
     char str[100];
     if (p == NULL)
        UARTprintf("NULL TCP packet received\n");
     else
     {
         memcpy(&str, p->payload, p->len);
        if(strstr(str, "success") == NULL)
            UARTprintf("GET FAILED: %s\n", str);
     }
      pbuf_free(p);
      tcp_close(tpcb);
      return ERR_OK;
}

err_t
RecvHttpHeaderCallback(httpc_state_t *connection, void *arg, struct pbuf
*hdr, u16_t hdr_len, u32_t content_len)
{
     return ERR_OK;
}

void
HttpClientResultCallback(void *arg, httpc_result_t httpc_result, u32_t
rx_content_len, u32_t srv_res, err_t err)
{
     UARTprintf("httpc_result: %u\n", httpc_result);
     UARTprintf("received number of bytes: %lu\n", rx_content_len);
}

static void
HttpcTask(void *pvParameters)
{
     portTickType ui32LastTime;
     httpc_connection_t conn_settings;
     httpc_state_t *connection;

     conn_settings.use_proxy = 0;
     conn_settings.headers_done_fn = RecvHttpHeaderCallback;
     conn_settings.result_fn = HttpClientResultCallback;
     ui32LastTime = xTaskGetTickCount();

     while(1)
     {
        httpc_get_file_dns("my.domain.com", 80, "/write.php?d=myData",
&conn_settings, RecvCallback, NULL, &connection);
        vTaskDelayUntil(&ui32LastTime, g_ui32HttpcDelay / portTICK_RATE_MS);
     }
}

uint32_t
HttpcTaskInit(void)
{
     xTaskCreate(HttpcTask, (const portCHAR *)"Httpc",
STACKSIZE_HTTPCTASK, NULL,
                    tskIDLE_PRIORITY + PRIORITY_HTTPC_TASK, NULL)
     return(0);
}
------------------------------------------------------------

Any idea what I am doing wrong?

Thanks

Jaroslav



_______________________________________________
lwip-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/lwip-users
Reply | Threaded
Open this post in threaded view
|

Re: http_client.c - heap memory overflow

goldsimon@gmx.de
Am 05.01.2020 um 23:41 schrieb Jaroslav:

> Hello,
> I am using LWIP 2.1.2 + FreeRTOS 10 on board TM4C1294XL. I am trying to
> push measured data to a web server using http_client.c. I mannaged to
> make it working based on example in
> https://lists.nongnu.org/archive/html/lwip-users/2019-07/msg00001.
>
> However, with each calling of httpc_get_file_dns usage of heap memory
> increases, and after approx 500th calling the application stops working
> due to heap memory overflow:
>
> MEM HEAP:
>           avail: 32768
>           used: 31948
>           max: 32732
>           err: 561
>
> and I am getting the following erros:
>
> httpc_result: 7
> memp_malloc: out of memory in pool TCP_PCB
> mem_malloc: could not allocate 412 bytes

Heap (mem_*) and pool (memp_*) are a bit confused here, can you clarify
this?

Because TCP pcbs are meant to live on after being closed (time-wait
state), so maybe you are seeing this?

Regards,
Simon

>
> Here is my simplified code:
>
> --------------------------------------------------------
> #define STACKSIZE_HTTPCTASK       4096
>
> err_t
> RecvCallback(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
> {
>       char str[100];
>       if (p == NULL)
>          UARTprintf("NULL TCP packet received\n");
>       else
>       {
>           memcpy(&str, p->payload, p->len);
> if(strstr(str, "success") == NULL)
>    UARTprintf("GET FAILED: %s\n", str);
>       }
>        pbuf_free(p);
>        tcp_close(tpcb);
>        return ERR_OK;
> }
>
> err_t
> RecvHttpHeaderCallback(httpc_state_t *connection, void *arg, struct pbuf
> *hdr, u16_t hdr_len, u32_t content_len)
> {
>       return ERR_OK;
> }
>
> void
> HttpClientResultCallback(void *arg, httpc_result_t httpc_result, u32_t
> rx_content_len, u32_t srv_res, err_t err)
> {
>       UARTprintf("httpc_result: %u\n", httpc_result);
>       UARTprintf("received number of bytes: %lu\n", rx_content_len);
> }
>
> static void
> HttpcTask(void *pvParameters)
> {
>       portTickType ui32LastTime;
>       httpc_connection_t conn_settings;
>       httpc_state_t *connection;
>
>       conn_settings.use_proxy = 0;
>       conn_settings.headers_done_fn = RecvHttpHeaderCallback;
>       conn_settings.result_fn = HttpClientResultCallback;
>       ui32LastTime = xTaskGetTickCount();
>
>       while(1)
>       {
> httpc_get_file_dns("my.domain.com", 80, "/write.php?d=myData",
> &conn_settings, RecvCallback, NULL, &connection);
> vTaskDelayUntil(&ui32LastTime, g_ui32HttpcDelay / portTICK_RATE_MS);
>       }
> }
>
> uint32_t
> HttpcTaskInit(void)
> {
>       xTaskCreate(HttpcTask, (const portCHAR *)"Httpc",
> STACKSIZE_HTTPCTASK, NULL,
>                      tskIDLE_PRIORITY + PRIORITY_HTTPC_TASK, NULL)
>       return(0);
> }
> ------------------------------------------------------------
>
> Any idea what I am doing wrong?
>
> Thanks
>
> Jaroslav
>
>
>
> _______________________________________________
> lwip-users mailing list
> [hidden email]
> https://lists.nongnu.org/mailman/listinfo/lwip-users
>


_______________________________________________
lwip-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/lwip-users
Reply | Threaded
Open this post in threaded view
|

Re: http_client.c - heap memory overflow

Jaroslav
Hi Simon
Thanks for reply. I have found one problem in my code - calling
"pbuf_free" and "tcp_close" inside the "RecvCallback" (recv_fn) prevents
execution of "httpc_free_state" in http_client.c. As the memory is not
being properly freed the used memory increases and later overflows.
Removing theses funtions from the RecvCallback solved this memory
problem.

However, now I am seeing another problem. After several iterations of
sending data in the httpc task something fails probably in
"httpc_tcp_recv" and the "result_fn" is not executed. I see that
"https_tcp_poll" is being several times callad, and then I get
httpc_result = 5 (HTTPC_RESULT_ERR_TIMEOUT).

Note that when I set recv_fn to NULL (with disabled null check in the
http_client.c) I don't see this probem and the httpc task is stably
sending data to my web server. (Actually I don't need the recv_fn
callback in my aplication necesarly since I am just pushing data to web
server but it would be useful for checking the server response)

Regrds,
Jarosalv


Dne 2020-01-10 23:14, [hidden email] napsal:

> Am 05.01.2020 um 23:41 schrieb Jaroslav:
>> Hello,
>> I am using LWIP 2.1.2 + FreeRTOS 10 on board TM4C1294XL. I am trying
>> to
>> push measured data to a web server using http_client.c. I mannaged to
>> make it working based on example in
>> https://lists.nongnu.org/archive/html/lwip-users/2019-07/msg00001.
>>
>> However, with each calling of httpc_get_file_dns usage of heap memory
>> increases, and after approx 500th calling the application stops
>> working
>> due to heap memory overflow:
>>
>> MEM HEAP:
>>           avail: 32768
>>           used: 31948
>>           max: 32732
>>           err: 561
>>
>> and I am getting the following erros:
>>
>> httpc_result: 7
>> memp_malloc: out of memory in pool TCP_PCB
>> mem_malloc: could not allocate 412 bytes
>
> Heap (mem_*) and pool (memp_*) are a bit confused here, can you clarify
> this?
>
> Because TCP pcbs are meant to live on after being closed (time-wait
> state), so maybe you are seeing this?
>
> Regards,
> Simon
>
>>
>> Here is my simplified code:
>>
>> --------------------------------------------------------
>> #define STACKSIZE_HTTPCTASK       4096
>>
>> err_t
>> RecvCallback(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t
>> err)
>> {
>>       char str[100];
>>       if (p == NULL)
>>          UARTprintf("NULL TCP packet received\n");
>>       else
>>       {
>>           memcpy(&str, p->payload, p->len);
>> if(strstr(str, "success") == NULL)
>>    UARTprintf("GET FAILED: %s\n", str);
>>       }
>>        pbuf_free(p);
>>        tcp_close(tpcb);
>>        return ERR_OK;
>> }
>>
>> err_t
>> RecvHttpHeaderCallback(httpc_state_t *connection, void *arg, struct
>> pbuf
>> *hdr, u16_t hdr_len, u32_t content_len)
>> {
>>       return ERR_OK;
>> }
>>
>> void
>> HttpClientResultCallback(void *arg, httpc_result_t httpc_result, u32_t
>> rx_content_len, u32_t srv_res, err_t err)
>> {
>>       UARTprintf("httpc_result: %u\n", httpc_result);
>>       UARTprintf("received number of bytes: %lu\n", rx_content_len);
>> }
>>
>> static void
>> HttpcTask(void *pvParameters)
>> {
>>       portTickType ui32LastTime;
>>       httpc_connection_t conn_settings;
>>       httpc_state_t *connection;
>>
>>       conn_settings.use_proxy = 0;
>>       conn_settings.headers_done_fn = RecvHttpHeaderCallback;
>>       conn_settings.result_fn = HttpClientResultCallback;
>>       ui32LastTime = xTaskGetTickCount();
>>
>>       while(1)
>>       {
>> httpc_get_file_dns("my.domain.com", 80, "/write.php?d=myData",
>> &conn_settings, RecvCallback, NULL, &connection);
>> vTaskDelayUntil(&ui32LastTime, g_ui32HttpcDelay / portTICK_RATE_MS);
>>       }
>> }
>>
>> uint32_t
>> HttpcTaskInit(void)
>> {
>>       xTaskCreate(HttpcTask, (const portCHAR *)"Httpc",
>> STACKSIZE_HTTPCTASK, NULL,
>>                      tskIDLE_PRIORITY + PRIORITY_HTTPC_TASK, NULL)
>>       return(0);
>> }
>> ------------------------------------------------------------
>>
>> Any idea what I am doing wrong?
>>
>> Thanks
>>
>> Jaroslav
>>
>>
>>
>> _______________________________________________
>> lwip-users mailing list
>> [hidden email]
>> https://lists.nongnu.org/mailman/listinfo/lwip-users
>>
>
>
> _______________________________________________
> lwip-users mailing list
> [hidden email]
> https://lists.nongnu.org/mailman/listinfo/lwip-users

_______________________________________________
lwip-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/lwip-users