test_malloc_caps.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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_ESP32S2_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 - largest_free_size));
  106. TEST_ASSERT(heap_caps_get_free_size(MALLOC_CAP_32BIT) == (free_size32 - largest_free_size));
  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. heap_caps_print_heap_info(MALLOC_CAP_32BIT);
  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. /* Allow some leeway here, because LWIP sometimes allocates up to 144 bytes in the background
  129. as part of timer management.
  130. */
  131. TEST_ASSERT_INT32_WITHIN(200, after.total_free_bytes, original.total_free_bytes);
  132. TEST_ASSERT_INT32_WITHIN(200, after.largest_free_block, original.largest_free_block);
  133. TEST_ASSERT(after.minimum_free_bytes < original.total_free_bytes);
  134. }
  135. /* Small function runs from IRAM to check that malloc/free/realloc
  136. all work OK when cache is disabled...
  137. */
  138. static IRAM_ATTR __attribute__((noinline)) bool iram_malloc_test(void)
  139. {
  140. spi_flash_guard_get()->start(); // Disables flash cache
  141. bool result = true;
  142. void *x = heap_caps_malloc(64, MALLOC_CAP_32BIT);
  143. result = result && (x != NULL);
  144. void *y = heap_caps_realloc(x, 32, MALLOC_CAP_32BIT);
  145. result = result && (y != NULL);
  146. heap_caps_free(y);
  147. spi_flash_guard_get()->end(); // Re-enables flash cache
  148. return result;
  149. }
  150. TEST_CASE("heap_caps_xxx functions work with flash cache disabled", "[heap]")
  151. {
  152. TEST_ASSERT( iram_malloc_test() );
  153. }
  154. #ifdef CONFIG_HEAP_ABORT_WHEN_ALLOCATION_FAILS
  155. TEST_CASE("When enabled, allocation operation failure generates an abort", "[heap][reset=abort,SW_CPU_RESET]")
  156. {
  157. const size_t stupid_allocation_size = (128 * 1024 * 1024);
  158. void *ptr = heap_caps_malloc(stupid_allocation_size, MALLOC_CAP_DEFAULT);
  159. (void)ptr;
  160. TEST_FAIL_MESSAGE("should not be reached");
  161. }
  162. #endif
  163. static bool called_user_failed_hook = false;
  164. void heap_caps_alloc_failed_hook(size_t requested_size, uint32_t caps, const char *function_name)
  165. {
  166. printf("%s was called but failed to allocate %d bytes with 0x%X capabilities. \n",function_name, requested_size, caps);
  167. called_user_failed_hook = true;
  168. }
  169. TEST_CASE("user provided alloc failed hook must be called when allocation fails", "[heap]")
  170. {
  171. TEST_ASSERT(heap_caps_register_failed_alloc_callback(heap_caps_alloc_failed_hook) == ESP_OK);
  172. const size_t stupid_allocation_size = (128 * 1024 * 1024);
  173. void *ptr = heap_caps_malloc(stupid_allocation_size, MALLOC_CAP_DEFAULT);
  174. TEST_ASSERT(called_user_failed_hook != false);
  175. called_user_failed_hook = false;
  176. ptr = heap_caps_realloc(ptr, stupid_allocation_size, MALLOC_CAP_DEFAULT);
  177. TEST_ASSERT(called_user_failed_hook != false);
  178. called_user_failed_hook = false;
  179. ptr = heap_caps_aligned_alloc(0x200, stupid_allocation_size, MALLOC_CAP_DEFAULT);
  180. TEST_ASSERT(called_user_failed_hook != false);
  181. (void)ptr;
  182. }
  183. TEST_CASE("allocation with invalid capability should also trigger the alloc failed hook", "[heap]")
  184. {
  185. const size_t allocation_size = 64;
  186. const uint32_t invalid_cap = MALLOC_CAP_INVALID;
  187. TEST_ASSERT(heap_caps_register_failed_alloc_callback(heap_caps_alloc_failed_hook) == ESP_OK);
  188. called_user_failed_hook = false;
  189. void *ptr = heap_caps_malloc(allocation_size, invalid_cap);
  190. TEST_ASSERT(called_user_failed_hook != false);
  191. called_user_failed_hook = false;
  192. ptr = heap_caps_realloc(ptr, allocation_size, invalid_cap);
  193. TEST_ASSERT(called_user_failed_hook != false);
  194. called_user_failed_hook = false;
  195. ptr = heap_caps_aligned_alloc(0x200, allocation_size, invalid_cap);
  196. TEST_ASSERT(called_user_failed_hook != false);
  197. (void)ptr;
  198. }