test_malloc_caps.c 9.1 KB

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