test_aligned_alloc_caps.c 5.7 KB

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