test_aligned_alloc_caps.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. #include <string.h>
  13. TEST_CASE("Capabilities aligned allocator test", "[heap]")
  14. {
  15. uint32_t alignments = 0;
  16. printf("[ALIGNED_ALLOC] Allocating from default CAP: \n");
  17. for(;alignments <= 1024; alignments++) {
  18. uint8_t *buf = (uint8_t *)heap_caps_aligned_alloc(alignments, (alignments + 137), MALLOC_CAP_DEFAULT);
  19. if(((alignments & (alignments - 1)) != 0) || (!alignments)) {
  20. TEST_ASSERT( buf == NULL );
  21. //printf("[ALIGNED_ALLOC] alignment: %u is not a power of two, don't allow allocation \n", aligments);
  22. } else {
  23. TEST_ASSERT( buf != NULL );
  24. printf("[ALIGNED_ALLOC] alignment required: %u \n", alignments);
  25. printf("[ALIGNED_ALLOC] address of allocated memory: %p \n\n", (void *)buf);
  26. //Address of obtained block must be aligned with selected value
  27. if((alignments & 0x03) == 0) {
  28. //Alignment is a multiple of four:
  29. TEST_ASSERT(((intptr_t)buf & 0x03) == 0);
  30. } else {
  31. //Exotic alignments:
  32. TEST_ASSERT(((intptr_t)buf & (alignments - 1)) == 0);
  33. }
  34. //Write some data, if it corrupts memory probably the heap
  35. //canary verification will fail:
  36. memset(buf, 0xA5, (alignments + 137));
  37. heap_caps_aligned_free(buf);
  38. }
  39. }
  40. //Alloc from a non permitted area:
  41. uint32_t *not_permitted_buf = (uint32_t *)heap_caps_aligned_alloc(alignments, (alignments + 137), MALLOC_CAP_EXEC | MALLOC_CAP_32BIT);
  42. TEST_ASSERT( not_permitted_buf == NULL );
  43. #if CONFIG_ESP32_SPIRAM_SUPPORT || CONFIG_ESP32S2_SPIRAM_SUPPORT
  44. alignments = 0;
  45. printf("[ALIGNED_ALLOC] Allocating from external memory: \n");
  46. for(;alignments <= 1024 * 1024; alignments++) {
  47. //Now try to take aligned memory from IRAM:
  48. uint8_t *buf = (uint8_t *)heap_caps_aligned_alloc(alignments, 10*1024, MALLOC_CAP_SPIRAM);
  49. if(((alignments & (alignments - 1)) != 0) || (!alignments)) {
  50. TEST_ASSERT( buf == NULL );
  51. //printf("[ALIGNED_ALLOC] alignment: %u is not a power of two, don't allow allocation \n", aligments);
  52. } else {
  53. TEST_ASSERT( buf != NULL );
  54. printf("[ALIGNED_ALLOC] alignment required: %u \n", alignments);
  55. printf("[ALIGNED_ALLOC] address of allocated memory: %p \n\n", (void *)buf);
  56. //Address of obtained block must be aligned with selected value
  57. if((alignments & 0x03) == 0) {
  58. //Alignment is a multiple of four:
  59. TEST_ASSERT(((intptr_t)buf & 0x03) == 0);
  60. } else {
  61. //Exotic alignments:
  62. TEST_ASSERT(((intptr_t)buf & (alignments - 1)) == 0);
  63. }
  64. //Write some data, if it corrupts memory probably the heap
  65. //canary verification will fail:
  66. memset(buf, 0xA5, (10*1024));
  67. heap_caps_aligned_free(buf);
  68. }
  69. }
  70. #endif
  71. }
  72. TEST_CASE("Capabilities aligned calloc test", "[heap]")
  73. {
  74. uint32_t alignments = 0;
  75. printf("[ALIGNED_ALLOC] Allocating from default CAP: \n");
  76. for(;alignments <= 1024; alignments++) {
  77. uint8_t *buf = (uint8_t *)heap_caps_aligned_calloc(alignments, 1, (alignments + 137), MALLOC_CAP_DEFAULT);
  78. if(((alignments & (alignments - 1)) != 0) || (!alignments)) {
  79. TEST_ASSERT( buf == NULL );
  80. //printf("[ALIGNED_ALLOC] alignment: %u is not a power of two, don't allow allocation \n", aligments);
  81. } else {
  82. TEST_ASSERT( buf != NULL );
  83. printf("[ALIGNED_ALLOC] alignment required: %u \n", alignments);
  84. printf("[ALIGNED_ALLOC] address of allocated memory: %p \n\n", (void *)buf);
  85. //Address of obtained block must be aligned with selected value
  86. if((alignments & 0x03) == 0) {
  87. //Alignment is a multiple of four:
  88. TEST_ASSERT(((intptr_t)buf & 0x03) == 0);
  89. } else {
  90. //Exotic alignments:
  91. TEST_ASSERT(((intptr_t)buf & (alignments - 1)) == 0);
  92. }
  93. //Write some data, if it corrupts memory probably the heap
  94. //canary verification will fail:
  95. memset(buf, 0xA5, (alignments + 137));
  96. heap_caps_aligned_free(buf);
  97. }
  98. }
  99. //Check if memory is initialized with zero:
  100. uint8_t byte_array[1024];
  101. memset(&byte_array, 0, sizeof(byte_array));
  102. uint8_t *buf = (uint8_t *)heap_caps_aligned_calloc(1024, 1, 1024, MALLOC_CAP_DEFAULT);
  103. TEST_ASSERT(memcmp(byte_array, buf, sizeof(byte_array)) == 0);
  104. heap_caps_aligned_free(buf);
  105. //Same size, but different chunk:
  106. buf = (uint8_t *)heap_caps_aligned_calloc(1024, 1024, 1, MALLOC_CAP_DEFAULT);
  107. TEST_ASSERT(memcmp(byte_array, buf, sizeof(byte_array)) == 0);
  108. heap_caps_aligned_free(buf);
  109. //Alloc from a non permitted area:
  110. uint32_t *not_permitted_buf = (uint32_t *)heap_caps_aligned_calloc(alignments, 1, (alignments + 137), MALLOC_CAP_32BIT);
  111. TEST_ASSERT( not_permitted_buf == NULL );
  112. #if CONFIG_ESP32_SPIRAM_SUPPORT || CONFIG_ESP32S2_SPIRAM_SUPPORT
  113. alignments = 0;
  114. printf("[ALIGNED_ALLOC] Allocating from external memory: \n");
  115. for(;alignments <= 1024 * 1024; alignments++) {
  116. //Now try to take aligned memory from IRAM:
  117. uint8_t *buf = (uint8_t *)(uint8_t *)heap_caps_aligned_calloc(alignments, 1, 10*1024, MALLOC_CAP_SPIRAM);;
  118. if(((alignments & (alignments - 1)) != 0) || (!alignments)) {
  119. TEST_ASSERT( buf == NULL );
  120. //printf("[ALIGNED_ALLOC] alignment: %u is not a power of two, don't allow allocation \n", aligments);
  121. } else {
  122. TEST_ASSERT( buf != NULL );
  123. printf("[ALIGNED_ALLOC] alignment required: %u \n", alignments);
  124. printf("[ALIGNED_ALLOC] address of allocated memory: %p \n\n", (void *)buf);
  125. //Address of obtained block must be aligned with selected value
  126. if((alignments & 0x03) == 0) {
  127. //Alignment is a multiple of four:
  128. TEST_ASSERT(((intptr_t)buf & 0x03) == 0);
  129. } else {
  130. //Exotic alignments:
  131. TEST_ASSERT(((intptr_t)buf & (alignments - 1)) == 0);
  132. }
  133. //Write some data, if it corrupts memory probably the heap
  134. //canary verification will fail:
  135. memset(buf, 0xA5, (10*1024));
  136. heap_caps_aligned_free(buf);
  137. }
  138. }
  139. #endif
  140. }
  141. TEST_CASE("aligned_alloc(0) should return a NULL pointer", "[heap]")
  142. {
  143. void *p;
  144. p = heap_caps_aligned_alloc(32, 0, MALLOC_CAP_DEFAULT);
  145. TEST_ASSERT(p == NULL);
  146. }