123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- #include <esp_types.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "sdkconfig.h"
- #include "unity.h"
- #include "freertos/FreeRTOS.h"
- #include "freertos/task.h"
- #ifdef CONFIG_HEAP_TRACING
- #include "esp_heap_trace.h"
- TEST_CASE("heap trace leak check", "[heap]")
- {
- heap_trace_record_t recs[8];
- heap_trace_init_standalone(recs, 8);
- printf("Leak check test\n");
- fflush(stdout);
- heap_trace_start(HEAP_TRACE_LEAKS);
- void *a = malloc(64);
- memset(a, '3', 64);
- void *b = malloc(96);
- memset(b, '4', 11);
- printf("a.address %p vs %p b.address %p vs %p\n", a, recs[0].address, b, recs[1].address);
- heap_trace_dump();
- TEST_ASSERT_EQUAL(2, heap_trace_get_count());
- heap_trace_record_t trace_a, trace_b;
- heap_trace_get(0, &trace_a);
- heap_trace_get(1, &trace_b);
- printf("trace_a.address %p trace_bb.address %p\n", trace_a.address, trace_b.address);
- TEST_ASSERT_EQUAL_PTR(a, trace_a.address);
- TEST_ASSERT_EQUAL_PTR(b, trace_b.address);
- TEST_ASSERT_EQUAL_PTR(recs[0].address, trace_a.address);
- TEST_ASSERT_EQUAL_PTR(recs[1].address, trace_b.address);
- free(a);
- TEST_ASSERT_EQUAL(1, heap_trace_get_count());
- heap_trace_get(0, &trace_b);
- TEST_ASSERT_EQUAL_PTR(b, trace_b.address);
-
- TEST_ASSERT_EQUAL_PTR(recs[0].address, trace_b.address);
- heap_trace_stop();
- }
- TEST_CASE("heap trace wrapped buffer check", "[heap]")
- {
- const size_t N = 8;
- heap_trace_record_t recs[N];
- heap_trace_init_standalone(recs, N);
- heap_trace_start(HEAP_TRACE_LEAKS);
- void *ptrs[N+1];
- for (int i = 0; i < N+1; i++) {
- ptrs[i] = malloc(i*3);
- }
-
-
-
- bool saw_last_ptr = false;
- for (int i = 0; i < N; i++) {
- heap_trace_record_t rec;
- heap_trace_get(i, &rec);
- if (rec.address == ptrs[N-1]) {
- saw_last_ptr = true;
- }
- }
- TEST_ASSERT(saw_last_ptr);
- void *other = malloc(6);
- heap_trace_dump();
- for (int i = 0; i < N+1; i++) {
- free(ptrs[i]);
- }
- heap_trace_dump();
- bool saw_other = false;
- for (int i = 0; i < heap_trace_get_count(); i++) {
- heap_trace_record_t rec;
- heap_trace_get(i, &rec);
-
- for (int j = 0; j < N+1; j++) {
- TEST_ASSERT_NOT_EQUAL(ptrs[j], rec.address);
- }
- if (rec.address == other) {
- saw_other = true;
- }
- }
-
- TEST_ASSERT(saw_other);
- heap_trace_stop();
- }
- static void print_floats_task(void *ignore)
- {
- heap_trace_start(HEAP_TRACE_ALL);
- char buf[16] = { };
- volatile float f = 12.3456;
- sprintf(buf, "%.4f", f);
- TEST_ASSERT_EQUAL_STRING("12.3456", buf);
- heap_trace_stop();
- vTaskDelete(NULL);
- }
- TEST_CASE("can trace allocations made by newlib", "[heap]")
- {
- const size_t N = 8;
- heap_trace_record_t recs[N];
- heap_trace_init_standalone(recs, N);
-
- xTaskCreate(print_floats_task, "print_float", 4096, NULL, 5, NULL);
- vTaskDelay(10);
-
- TEST_ASSERT(heap_trace_get_count() > 3);
- }
- #endif
|