test_aligned_alloc_caps.c 5.8 KB

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