test_malloc_caps.c 9.8 KB

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