test_malloc_caps.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. Tests for the capabilities-based memory allocator.
  3. */
  4. #include <esp_types.h>
  5. #include <stdio.h>
  6. #include "unity.h"
  7. #include "esp_attr.h"
  8. #include "esp_heap_caps.h"
  9. #include "esp_spi_flash.h"
  10. #include "esp_memory_utils.h"
  11. #include <stdlib.h>
  12. #include <sys/param.h>
  13. #if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32C2)
  14. //IDF-5167
  15. #ifndef CONFIG_ESP_SYSTEM_MEMPROT_FEATURE
  16. TEST_CASE("Capabilities allocator test", "[heap]")
  17. {
  18. char *m1, *m2[10];
  19. int x;
  20. size_t free8start, free32start, free8, free32;
  21. /* It's important we printf() something before we take the empty heap sizes,
  22. as the first printf() in a task allocates heap resources... */
  23. printf("Testing capabilities allocator...\n");
  24. free8start = heap_caps_get_free_size(MALLOC_CAP_8BIT);
  25. free32start = heap_caps_get_free_size(MALLOC_CAP_32BIT);
  26. printf("Free 8bit-capable memory (start): %dK, 32-bit capable memory %dK\n", free8start, free32start);
  27. TEST_ASSERT(free32start >= free8start);
  28. printf("Allocating 10K of 8-bit capable RAM\n");
  29. m1= heap_caps_malloc(10*1024, MALLOC_CAP_8BIT);
  30. printf("--> %p\n", m1);
  31. free8 = heap_caps_get_free_size(MALLOC_CAP_8BIT);
  32. free32 = heap_caps_get_free_size(MALLOC_CAP_32BIT);
  33. printf("Free 8bit-capable memory (both reduced): %dK, 32-bit capable memory %dK\n", free8, free32);
  34. //Both should have gone down by 10K; 8bit capable ram is also 32-bit capable
  35. TEST_ASSERT(free8<=(free8start-10*1024));
  36. TEST_ASSERT(free32<=(free32start-10*1024));
  37. //Assume we got DRAM back
  38. TEST_ASSERT((((int)m1)&0xFF000000)==0x3F000000);
  39. free(m1);
  40. //The goal here is to allocate from IRAM. Since there is no external IRAM (yet)
  41. //the following gives size of IRAM-only (not D/IRAM) memory.
  42. size_t free_iram = heap_caps_get_free_size(MALLOC_CAP_INTERNAL) -
  43. heap_caps_get_free_size(MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL);
  44. size_t alloc32 = MIN(free_iram / 2, 10*1024) & (~3);
  45. if(free_iram) {
  46. printf("Freeing; allocating %u bytes of 32K-capable RAM\n", alloc32);
  47. m1 = heap_caps_malloc(alloc32, MALLOC_CAP_32BIT);
  48. printf("--> %p\n", m1);
  49. //Check that we got IRAM back
  50. TEST_ASSERT((((int)m1)&0xFF000000)==0x40000000);
  51. free8 = heap_caps_get_free_size(MALLOC_CAP_8BIT);
  52. free32 = heap_caps_get_free_size(MALLOC_CAP_32BIT);
  53. printf("Free 8bit-capable memory (after 32-bit): %dK, 32-bit capable memory %dK\n", free8, free32);
  54. //Only 32-bit should have gone down by alloc32: 32-bit isn't necessarily 8bit capable
  55. TEST_ASSERT(free32<=(free32start-alloc32));
  56. TEST_ASSERT(free8==free8start);
  57. free(m1);
  58. } else {
  59. printf("This platform has no 32-bit only capable RAM, jumping to next test \n");
  60. }
  61. printf("Allocating impossible caps\n");
  62. m1= heap_caps_malloc(10*1024, MALLOC_CAP_8BIT|MALLOC_CAP_EXEC);
  63. printf("--> %p\n", m1);
  64. TEST_ASSERT(m1==NULL);
  65. if(free_iram) {
  66. printf("Testing changeover iram -> dram");
  67. // priorities will exhaust IRAM first, then start allocating from DRAM
  68. for (x=0; x<10; x++) {
  69. m2[x]= heap_caps_malloc(alloc32, MALLOC_CAP_32BIT);
  70. printf("--> %p\n", m2[x]);
  71. }
  72. TEST_ASSERT((((int)m2[0])&0xFF000000)==0x40000000);
  73. TEST_ASSERT((((int)m2[9])&0xFF000000)==0x3F000000);
  74. } else {
  75. printf("This platform has no IRAM-only so changeover will never occur, jumping to next test\n");
  76. }
  77. printf("Test if allocating executable code still gives IRAM, even with dedicated IRAM region depleted\n");
  78. if(free_iram) {
  79. // (the allocation should come from D/IRAM)
  80. free_iram = heap_caps_get_free_size(MALLOC_CAP_EXEC);
  81. m1= heap_caps_malloc(MIN(free_iram / 2, 10*1024), MALLOC_CAP_EXEC);
  82. printf("--> %p\n", m1);
  83. TEST_ASSERT((((int)m1)&0xFF000000)==0x40000000);
  84. for (x=0; x<10; x++) free(m2[x]);
  85. } else {
  86. // (the allocation should come from D/IRAM)
  87. free_iram = heap_caps_get_free_size(MALLOC_CAP_EXEC);
  88. m1= heap_caps_malloc(MIN(free_iram / 2, 10*1024), MALLOC_CAP_EXEC);
  89. printf("--> %p\n", m1);
  90. TEST_ASSERT((((int)m1)&0xFF000000)==0x40000000);
  91. }
  92. free(m1);
  93. printf("Done.\n");
  94. }
  95. #endif
  96. #endif //!TEMPORARY_DISABLED_FOR_TARGETS(ESP32C2)
  97. #ifdef CONFIG_ESP32_IRAM_AS_8BIT_ACCESSIBLE_MEMORY
  98. TEST_CASE("IRAM_8BIT capability test", "[heap]")
  99. {
  100. uint8_t *ptr;
  101. size_t free_size, free_size32, largest_free_size;
  102. /* need to print something as first printf allocates some heap */
  103. printf("IRAM_8BIT capability test\n");
  104. free_size = heap_caps_get_free_size(MALLOC_CAP_IRAM_8BIT);
  105. free_size32 = heap_caps_get_free_size(MALLOC_CAP_32BIT);
  106. largest_free_size = heap_caps_get_largest_free_block(MALLOC_CAP_IRAM_8BIT);
  107. ptr = heap_caps_malloc(largest_free_size, MALLOC_CAP_IRAM_8BIT);
  108. TEST_ASSERT((((int)ptr)&0xFF000000)==0x40000000);
  109. /* As the heap allocator may present an overhead for allocated blocks,
  110. * we need to check that the free heap size is now smaller or equal to the former free size. */
  111. TEST_ASSERT(heap_caps_get_free_size(MALLOC_CAP_IRAM_8BIT) <= (free_size - heap_caps_get_allocated_size(ptr)));
  112. TEST_ASSERT(heap_caps_get_free_size(MALLOC_CAP_32BIT) <= (free_size32 - heap_caps_get_allocated_size(ptr)));
  113. free(ptr);
  114. }
  115. #endif
  116. TEST_CASE("heap_caps metadata test", "[heap]")
  117. {
  118. /* need to print something as first printf allocates some heap */
  119. printf("heap_caps metadata test\n");
  120. heap_caps_print_heap_info(MALLOC_CAP_8BIT);
  121. multi_heap_info_t original;
  122. heap_caps_get_info(&original, MALLOC_CAP_8BIT);
  123. void *b = heap_caps_malloc(original.largest_free_block, MALLOC_CAP_8BIT);
  124. TEST_ASSERT_NOT_NULL(b);
  125. printf("After allocating %d bytes:\n", original.largest_free_block);
  126. heap_caps_print_heap_info(MALLOC_CAP_8BIT);
  127. multi_heap_info_t after;
  128. heap_caps_get_info(&after, MALLOC_CAP_8BIT);
  129. TEST_ASSERT(after.largest_free_block <= original.largest_free_block);
  130. TEST_ASSERT(after.total_free_bytes <= original.total_free_bytes);
  131. free(b);
  132. heap_caps_get_info(&after, MALLOC_CAP_8BIT);
  133. printf("\n\n After test, heap status:\n");
  134. heap_caps_print_heap_info(MALLOC_CAP_8BIT);
  135. /* Allow some leeway here, because LWIP sometimes allocates up to 144 bytes in the background
  136. as part of timer management.
  137. */
  138. TEST_ASSERT_INT32_WITHIN(200, after.total_free_bytes, original.total_free_bytes);
  139. TEST_ASSERT_INT32_WITHIN(200, after.largest_free_block, original.largest_free_block);
  140. TEST_ASSERT(after.minimum_free_bytes < original.total_free_bytes);
  141. }
  142. #if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32C2)
  143. //IDF-5167
  144. /* Small function runs from IRAM to check that malloc/free/realloc
  145. all work OK when cache is disabled...
  146. */
  147. static IRAM_ATTR __attribute__((noinline)) bool iram_malloc_test(void)
  148. {
  149. spi_flash_guard_get()->start(); // Disables flash cache
  150. bool result = true;
  151. void *x = heap_caps_malloc(64, MALLOC_CAP_EXEC);
  152. result = result && (x != NULL);
  153. void *y = heap_caps_realloc(x, 32, MALLOC_CAP_EXEC);
  154. result = result && (y != NULL);
  155. heap_caps_free(y);
  156. spi_flash_guard_get()->end(); // Re-enables flash cache
  157. return result;
  158. }
  159. TEST_CASE("heap_caps_xxx functions work with flash cache disabled", "[heap]")
  160. {
  161. TEST_ASSERT( iram_malloc_test() );
  162. }
  163. #endif //!TEMPORARY_DISABLED_FOR_TARGETS(ESP32C2)
  164. #ifdef CONFIG_HEAP_ABORT_WHEN_ALLOCATION_FAILS
  165. TEST_CASE("When enabled, allocation operation failure generates an abort", "[heap][reset=abort,SW_CPU_RESET]")
  166. {
  167. const size_t stupid_allocation_size = (128 * 1024 * 1024);
  168. void *ptr = heap_caps_malloc(stupid_allocation_size, MALLOC_CAP_DEFAULT);
  169. (void)ptr;
  170. TEST_FAIL_MESSAGE("should not be reached");
  171. }
  172. #endif
  173. static bool called_user_failed_hook = false;
  174. void heap_caps_alloc_failed_hook(size_t requested_size, uint32_t caps, const char *function_name)
  175. {
  176. printf("%s was called but failed to allocate %d bytes with 0x%X capabilities. \n",function_name, requested_size, caps);
  177. called_user_failed_hook = true;
  178. }
  179. TEST_CASE("user provided alloc failed hook must be called when allocation fails", "[heap]")
  180. {
  181. TEST_ASSERT(heap_caps_register_failed_alloc_callback(heap_caps_alloc_failed_hook) == ESP_OK);
  182. const size_t stupid_allocation_size = (128 * 1024 * 1024);
  183. void *ptr = heap_caps_malloc(stupid_allocation_size, MALLOC_CAP_DEFAULT);
  184. TEST_ASSERT(called_user_failed_hook != false);
  185. called_user_failed_hook = false;
  186. ptr = heap_caps_realloc(ptr, stupid_allocation_size, MALLOC_CAP_DEFAULT);
  187. TEST_ASSERT(called_user_failed_hook != false);
  188. called_user_failed_hook = false;
  189. ptr = heap_caps_aligned_alloc(0x200, stupid_allocation_size, MALLOC_CAP_DEFAULT);
  190. TEST_ASSERT(called_user_failed_hook != false);
  191. (void)ptr;
  192. }
  193. TEST_CASE("allocation with invalid capability should also trigger the alloc failed hook", "[heap]")
  194. {
  195. const size_t allocation_size = 64;
  196. const uint32_t invalid_cap = MALLOC_CAP_INVALID;
  197. TEST_ASSERT(heap_caps_register_failed_alloc_callback(heap_caps_alloc_failed_hook) == ESP_OK);
  198. called_user_failed_hook = false;
  199. void *ptr = heap_caps_malloc(allocation_size, invalid_cap);
  200. TEST_ASSERT(called_user_failed_hook != false);
  201. called_user_failed_hook = false;
  202. ptr = heap_caps_realloc(ptr, allocation_size, invalid_cap);
  203. TEST_ASSERT(called_user_failed_hook != false);
  204. called_user_failed_hook = false;
  205. ptr = heap_caps_aligned_alloc(0x200, allocation_size, invalid_cap);
  206. TEST_ASSERT(called_user_failed_hook != false);
  207. (void)ptr;
  208. }
  209. #ifdef CONFIG_ESP_SYSTEM_ALLOW_RTC_FAST_MEM_AS_HEAP
  210. /**
  211. * In MR 16031, the priority of RTC memory has been adjusted to the lowest.
  212. * RTC memory will not be consumed a lot during the startup process.
  213. */
  214. TEST_CASE("RTC memory shoule be lowest priority and its free size should be big enough", "[heap]")
  215. {
  216. const size_t allocation_size = 1024 * 4;
  217. void *ptr = NULL;
  218. size_t free_size = 0;
  219. ptr = heap_caps_malloc(allocation_size, MALLOC_CAP_DEFAULT);
  220. TEST_ASSERT_NOT_NULL(ptr);
  221. TEST_ASSERT(!esp_ptr_in_rtc_dram_fast(ptr));
  222. free_size = heap_caps_get_free_size(MALLOC_CAP_RTCRAM);
  223. TEST_ASSERT_GREATER_OR_EQUAL(1024 * 4, free_size);
  224. free(ptr);
  225. }
  226. #endif