test_malloc.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Unlicense OR CC0-1.0
  5. */
  6. /*
  7. Generic test for malloc/free
  8. */
  9. #include <esp_types.h>
  10. #include <stdio.h>
  11. #include "freertos/FreeRTOS.h"
  12. #include "freertos/task.h"
  13. #include "freertos/semphr.h"
  14. #include "freertos/queue.h"
  15. #include "unity.h"
  16. #include "esp_heap_caps.h"
  17. #include "esp_system.h"
  18. #include "sdkconfig.h"
  19. static int **allocatedMem;
  20. static int noAllocated;
  21. static int tryAllocMem(void) {
  22. int i, j;
  23. const int allocateMaxK=1024*5; //try to allocate a max of 5MiB
  24. allocatedMem=malloc(sizeof(int *)*allocateMaxK);
  25. if (!allocatedMem) return 0;
  26. for (i=0; i<allocateMaxK; i++) {
  27. allocatedMem[i]=malloc(1024);
  28. if (allocatedMem[i]==NULL) break;
  29. for (j=0; j<1024/4; j++) allocatedMem[i][j]=(0xdeadbeef);
  30. }
  31. noAllocated=i;
  32. return i;
  33. }
  34. static void tryAllocMemFree(void) {
  35. int i, j;
  36. for (i=0; i<noAllocated; i++) {
  37. for (j=0; j<1024/4; j++) {
  38. TEST_ASSERT(allocatedMem[i][j]==(0xdeadbeef));
  39. }
  40. free(allocatedMem[i]);
  41. }
  42. free(allocatedMem);
  43. }
  44. TEST_CASE("Malloc/overwrite, then free all available DRAM", "[heap]")
  45. {
  46. int m1=0, m2=0;
  47. m1=tryAllocMem();
  48. tryAllocMemFree();
  49. m2=tryAllocMem();
  50. tryAllocMemFree();
  51. printf("Could allocate %dK on first try, %dK on 2nd try.\n", m1, m2);
  52. TEST_ASSERT(m1==m2);
  53. }
  54. #if CONFIG_SPIRAM_USE_MALLOC
  55. #if (CONFIG_SPIRAM_MALLOC_RESERVE_INTERNAL > 1024)
  56. TEST_CASE("Check if reserved DMA pool still can allocate even when malloc()'ed memory is exhausted", "[heap]")
  57. {
  58. char** dmaMem=malloc(sizeof(char*)*512);
  59. assert(dmaMem);
  60. int m=tryAllocMem();
  61. int i=0;
  62. for (i=0; i<512; i++) {
  63. dmaMem[i]=heap_caps_malloc(1024, MALLOC_CAP_DMA);
  64. if (dmaMem[i]==NULL) break;
  65. }
  66. for (int j=0; j<i; j++) free(dmaMem[j]);
  67. free(dmaMem);
  68. tryAllocMemFree();
  69. printf("Could allocate %dK of DMA memory after allocating all of %dK of normal memory.\n", i, m);
  70. TEST_ASSERT(i);
  71. }
  72. #endif
  73. #endif
  74. /* As you see, we are desperately trying to outsmart the compiler, so that it
  75. * doesn't warn about oversized allocations in the next two unit tests.
  76. * To be removed when we switch to GCC 8.2 and add
  77. * -Wno-alloc-size-larger-than=PTRDIFF_MAX to CFLAGS for this file.
  78. */
  79. void* (*g_test_malloc_ptr)(size_t) = &malloc;
  80. void* (*g_test_calloc_ptr)(size_t, size_t) = &calloc;
  81. void* test_malloc_wrapper(size_t size)
  82. {
  83. return (*g_test_malloc_ptr)(size);
  84. }
  85. void* test_calloc_wrapper(size_t count, size_t size)
  86. {
  87. return (*g_test_calloc_ptr)(count, size);
  88. }
  89. TEST_CASE("alloc overflows should all fail", "[heap]")
  90. {
  91. /* allocates 8 bytes if size_t overflows */
  92. TEST_ASSERT_NULL(test_calloc_wrapper(SIZE_MAX / 2 + 4, 2));
  93. /* will overflow if any poisoning is enabled
  94. (should fail for sensible OOM reasons, otherwise) */
  95. TEST_ASSERT_NULL(test_malloc_wrapper(SIZE_MAX - 1));
  96. TEST_ASSERT_NULL(test_calloc_wrapper(SIZE_MAX - 1, 1));
  97. /* will overflow when the size is rounded up to word align it */
  98. TEST_ASSERT_NULL(heap_caps_malloc(SIZE_MAX-1, MALLOC_CAP_32BIT));
  99. TEST_ASSERT_NULL(heap_caps_malloc(SIZE_MAX-1, MALLOC_CAP_EXEC));
  100. }
  101. TEST_CASE("unreasonable allocs should all fail", "[heap]")
  102. {
  103. TEST_ASSERT_NULL(test_calloc_wrapper(16, 1024*1024));
  104. TEST_ASSERT_NULL(test_malloc_wrapper(16*1024*1024));
  105. TEST_ASSERT_NULL(test_malloc_wrapper(SIZE_MAX / 2));
  106. TEST_ASSERT_NULL(test_malloc_wrapper(SIZE_MAX - 256));
  107. TEST_ASSERT_NULL(test_malloc_wrapper(esp_get_free_heap_size() - 1));
  108. }
  109. TEST_CASE("malloc(0) should return a NULL pointer", "[heap]")
  110. {
  111. void *p;
  112. p = malloc(0);
  113. TEST_ASSERT(p == NULL);
  114. }
  115. static bool failure_occured = false;
  116. static void test_alloc_failure_callback(size_t size, uint32_t caps, const char * function_name)
  117. {
  118. failure_occured = true;
  119. }
  120. TEST_CASE("malloc/calloc(0) should not call failure callback", "[heap]")
  121. {
  122. void* ptr = NULL;
  123. esp_err_t ret = heap_caps_register_failed_alloc_callback(test_alloc_failure_callback);
  124. TEST_ASSERT(ret == ESP_OK);
  125. ptr = malloc(0);
  126. TEST_ASSERT_NULL(ptr);
  127. /* Check that our callback was NOT called */
  128. TEST_ASSERT_FALSE(failure_occured);
  129. /* Do the same thing for calloc */
  130. ptr = calloc(0, 0);
  131. TEST_ASSERT_NULL(ptr);
  132. TEST_ASSERT_FALSE(failure_occured);
  133. }
  134. TEST_CASE("test get allocated size", "[heap]")
  135. {
  136. // random values to test, some are 4 bytes aligned, some are not
  137. const size_t alloc_sizes[] = { 1035, 1064, 1541 };
  138. const size_t iterations = sizeof(alloc_sizes) / sizeof(size_t);
  139. void *ptr_array[iterations];
  140. for (size_t i = 0; i < iterations; i++) {
  141. ptr_array[i] = heap_caps_malloc(alloc_sizes[i], MALLOC_CAP_DEFAULT);
  142. TEST_ASSERT_NOT_NULL(ptr_array[i]);
  143. // test that the heap_caps_get_allocated_size() returns the right number of bytes (aligned to 4 bytes
  144. // since the heap component aligns to 4 bytes)
  145. const size_t aligned_size = (alloc_sizes[i] + 3) & ~3;
  146. const size_t real_size = heap_caps_get_allocated_size(ptr_array[i]);
  147. printf("initial size: %d, requested size : %d, allocated size: %d\n", alloc_sizes[i], aligned_size, real_size);
  148. TEST_ASSERT_EQUAL(aligned_size, real_size);
  149. heap_caps_free(ptr_array[i]);
  150. }
  151. }
  152. #ifdef CONFIG_HEAP_USE_HOOKS
  153. // provide the definition of alloc and free hooks
  154. static const size_t alloc_size = 1234; // make this size atypical to be able to rely on it in the hook
  155. static const size_t expected_calls = 2; // one call for malloc/calloc and one call for realloc
  156. static uint32_t *alloc_ptr = NULL;
  157. static bool test_success = false;
  158. static size_t counter = 0;
  159. static void reset_static_variables(void) {
  160. test_success = false;
  161. alloc_ptr = NULL;
  162. counter = 0;
  163. }
  164. void esp_heap_trace_alloc_hook(void* ptr, size_t size, uint32_t caps)
  165. {
  166. if (size == alloc_size) {
  167. counter++;
  168. if (counter == expected_calls) {
  169. alloc_ptr = ptr;
  170. }
  171. }
  172. }
  173. void esp_heap_trace_free_hook(void* ptr)
  174. {
  175. if (alloc_ptr == ptr && counter == expected_calls) {
  176. test_success = true;
  177. }
  178. }
  179. TEST_CASE("test allocation and free function hooks", "[heap]")
  180. {
  181. // alloc, realloc and free memory, at the end of the test, test_success will be set
  182. // to true if both function hooks are called.
  183. uint32_t *ptr = heap_caps_malloc(alloc_size, MALLOC_CAP_DEFAULT);
  184. TEST_ASSERT_NOT_NULL(ptr);
  185. ptr = heap_caps_realloc(ptr, alloc_size, MALLOC_CAP_32BIT);
  186. heap_caps_free(ptr);
  187. TEST_ASSERT_TRUE(test_success);
  188. // re-init the static variables
  189. reset_static_variables();
  190. // calloc, realloc and free memory, at the end of the test, test_success will be set
  191. // to true if both function hooks are called.
  192. ptr = heap_caps_calloc(1, alloc_size, MALLOC_CAP_DEFAULT);
  193. TEST_ASSERT_NOT_NULL(ptr);
  194. ptr = heap_caps_realloc(ptr, alloc_size, MALLOC_CAP_32BIT);
  195. heap_caps_free(ptr);
  196. TEST_ASSERT_TRUE(test_success);
  197. // re-init the static variables
  198. reset_static_variables();
  199. // aligned alloc, realloc and aligned free memory, at the end of the test, test_success
  200. // will be set to true if both function hooks are called.
  201. ptr = heap_caps_aligned_alloc(0x200, alloc_size, MALLOC_CAP_DEFAULT);
  202. TEST_ASSERT_NOT_NULL(ptr);
  203. ptr = heap_caps_realloc(ptr, alloc_size, MALLOC_CAP_32BIT);
  204. heap_caps_free(ptr);
  205. TEST_ASSERT_TRUE(test_success);
  206. }
  207. #endif