test_aligned_alloc_caps.c 6.7 KB

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