test_heap_trace.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. Generic test for heap tracing support
  3. Only compiled in if CONFIG_HEAP_TRACING is set
  4. */
  5. #include <esp_types.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include "sdkconfig.h"
  10. #include "unity.h"
  11. #include "freertos/FreeRTOS.h"
  12. #include "freertos/task.h"
  13. #ifdef CONFIG_HEAP_TRACING
  14. // only compile in heap tracing tests if tracing is enabled
  15. #include "esp_heap_trace.h"
  16. TEST_CASE("heap trace leak check", "[heap]")
  17. {
  18. heap_trace_record_t recs[8];
  19. heap_trace_init_standalone(recs, 8);
  20. printf("Leak check test\n"); // Print something before trace starts, or stdout allocations skew total counts
  21. fflush(stdout);
  22. heap_trace_start(HEAP_TRACE_LEAKS);
  23. void *a = malloc(64);
  24. memset(a, '3', 64);
  25. void *b = malloc(96);
  26. memset(b, '4', 11);
  27. printf("a.address %p vs %p b.address %p vs %p\n", a, recs[0].address, b, recs[1].address);
  28. heap_trace_dump();
  29. TEST_ASSERT_EQUAL(2, heap_trace_get_count());
  30. heap_trace_record_t trace_a, trace_b;
  31. heap_trace_get(0, &trace_a);
  32. heap_trace_get(1, &trace_b);
  33. printf("trace_a.address %p trace_bb.address %p\n", trace_a.address, trace_b.address);
  34. TEST_ASSERT_EQUAL_PTR(a, trace_a.address);
  35. TEST_ASSERT_EQUAL_PTR(b, trace_b.address);
  36. TEST_ASSERT_EQUAL_PTR(recs[0].address, trace_a.address);
  37. TEST_ASSERT_EQUAL_PTR(recs[1].address, trace_b.address);
  38. free(a);
  39. TEST_ASSERT_EQUAL(1, heap_trace_get_count());
  40. heap_trace_get(0, &trace_b);
  41. TEST_ASSERT_EQUAL_PTR(b, trace_b.address);
  42. /* buffer deletes trace_a when freed,
  43. so trace_b at head of buffer */
  44. TEST_ASSERT_EQUAL_PTR(recs[0].address, trace_b.address);
  45. heap_trace_stop();
  46. }
  47. TEST_CASE("heap trace wrapped buffer check", "[heap]")
  48. {
  49. const size_t N = 8;
  50. heap_trace_record_t recs[N];
  51. heap_trace_init_standalone(recs, N);
  52. heap_trace_start(HEAP_TRACE_LEAKS);
  53. void *ptrs[N+1];
  54. for (int i = 0; i < N+1; i++) {
  55. ptrs[i] = malloc(i*3);
  56. }
  57. // becuase other mallocs happen as part of this control flow,
  58. // we can't guarantee N entries of ptrs[] are in the heap check buffer.
  59. // but we should guarantee at least the last one is
  60. bool saw_last_ptr = false;
  61. for (int i = 0; i < N; i++) {
  62. heap_trace_record_t rec;
  63. heap_trace_get(i, &rec);
  64. if (rec.address == ptrs[N-1]) {
  65. saw_last_ptr = true;
  66. }
  67. }
  68. TEST_ASSERT(saw_last_ptr);
  69. void *other = malloc(6);
  70. heap_trace_dump();
  71. for (int i = 0; i < N+1; i++) {
  72. free(ptrs[i]);
  73. }
  74. heap_trace_dump();
  75. bool saw_other = false;
  76. for (int i = 0; i < heap_trace_get_count(); i++) {
  77. heap_trace_record_t rec;
  78. heap_trace_get(i, &rec);
  79. // none of ptr[]s should be in the heap trace any more
  80. for (int j = 0; j < N+1; j++) {
  81. TEST_ASSERT_NOT_EQUAL(ptrs[j], rec.address);
  82. }
  83. if (rec.address == other) {
  84. saw_other = true;
  85. }
  86. }
  87. // 'other' pointer should be somewhere in the leak dump
  88. TEST_ASSERT(saw_other);
  89. heap_trace_stop();
  90. }
  91. static void print_floats_task(void *ignore)
  92. {
  93. heap_trace_start(HEAP_TRACE_ALL);
  94. char buf[16] = { };
  95. volatile float f = 12.3456;
  96. sprintf(buf, "%.4f", f);
  97. TEST_ASSERT_EQUAL_STRING("12.3456", buf);
  98. heap_trace_stop();
  99. vTaskDelete(NULL);
  100. }
  101. TEST_CASE("can trace allocations made by newlib", "[heap]")
  102. {
  103. const size_t N = 8;
  104. heap_trace_record_t recs[N];
  105. heap_trace_init_standalone(recs, N);
  106. /* Verifying that newlib code performs an allocation is very fiddly:
  107. - Printing a float allocates data associated with the task, but only the
  108. first time a task prints a float of this length. So we do it in a one-shot task
  109. to avoid possibility it already happened.
  110. - If newlib is updated this test may start failing if the printf() implementation
  111. changes. (This version passes for both nano & regular formatting in newlib 2.2.0)
  112. - We also do the tracing in the task so we only capture things directly related to it.
  113. */
  114. xTaskCreate(print_floats_task, "print_float", 4096, NULL, 5, NULL);
  115. vTaskDelay(10);
  116. /* has to be at least a few as newlib allocates via multiple different function calls */
  117. TEST_ASSERT(heap_trace_get_count() > 3);
  118. }
  119. #endif