test_aligned_alloc_caps.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Unlicense OR CC0-1.0
  5. */
  6. /*
  7. Tests for the capabilities-based memory allocator.
  8. */
  9. #include <esp_types.h>
  10. #include <stdio.h>
  11. #include "unity.h"
  12. #include "esp_attr.h"
  13. #include "esp_heap_caps.h"
  14. #include "spi_flash_mmap.h"
  15. #include <stdlib.h>
  16. #include <sys/param.h>
  17. #include <string.h>
  18. #include <malloc.h>
  19. #include <inttypes.h>
  20. TEST_CASE("Capabilities aligned allocator test", "[heap]")
  21. {
  22. uint32_t alignments = 0;
  23. printf("[ALIGNED_ALLOC] Allocating from default CAP: \n");
  24. for(;alignments <= 1024; alignments++) {
  25. uint8_t *buf = (uint8_t *)memalign(alignments, (alignments + 137));
  26. if(((alignments & (alignments - 1)) != 0) || (!alignments)) {
  27. TEST_ASSERT( buf == NULL );
  28. //printf("[ALIGNED_ALLOC] alignment: %"PRIu32" is not a power of two, don't allow allocation \n", aligments);
  29. } else {
  30. TEST_ASSERT( buf != NULL );
  31. printf("[ALIGNED_ALLOC] alignment required: %"PRIu32" \n", alignments);
  32. printf("[ALIGNED_ALLOC] address of allocated memory: %p \n\n", (void *)buf);
  33. //Address of obtained block must be aligned with selected value
  34. TEST_ASSERT(((intptr_t)buf & (alignments - 1)) == 0);
  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_SPIRAM
  45. alignments = 0;
  46. printf("[ALIGNED_ALLOC] Allocating from external memory: \n");
  47. for(;alignments <= 1024 * 512; 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: %"PRIu32" 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: %"PRIu32" \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. TEST_ASSERT(((intptr_t)buf & (alignments - 1)) == 0);
  59. //Write some data, if it corrupts memory probably the heap
  60. //canary verification will fail:
  61. memset(buf, 0xA5, (10*1024));
  62. heap_caps_free(buf);
  63. }
  64. }
  65. #endif
  66. }
  67. TEST_CASE("Capabilities aligned calloc test", "[heap]")
  68. {
  69. uint32_t alignments = 0;
  70. printf("[ALIGNED_ALLOC] Allocating from default CAP: \n");
  71. for(;alignments <= 1024; alignments++) {
  72. uint8_t *buf = (uint8_t *)heap_caps_aligned_calloc(alignments, 1, (alignments + 137), MALLOC_CAP_DEFAULT);
  73. if(((alignments & (alignments - 1)) != 0) || (!alignments)) {
  74. TEST_ASSERT( buf == NULL );
  75. //printf("[ALIGNED_ALLOC] alignment: %"PRIu32" is not a power of two, don't allow allocation \n", aligments);
  76. } else {
  77. TEST_ASSERT( buf != NULL );
  78. printf("[ALIGNED_ALLOC] alignment required: %"PRIu32" \n", alignments);
  79. printf("[ALIGNED_ALLOC] address of allocated memory: %p \n\n", (void *)buf);
  80. //Address of obtained block must be aligned with selected value
  81. TEST_ASSERT(((intptr_t)buf & (alignments - 1)) == 0);
  82. //Write some data, if it corrupts memory probably the heap
  83. //canary verification will fail:
  84. memset(buf, 0xA5, (alignments + 137));
  85. heap_caps_free(buf);
  86. }
  87. }
  88. //Check if memory is initialized with zero:
  89. uint8_t byte_array[1024];
  90. memset(&byte_array, 0, sizeof(byte_array));
  91. uint8_t *buf = (uint8_t *)heap_caps_aligned_calloc(1024, 1, 1024, MALLOC_CAP_DEFAULT);
  92. TEST_ASSERT(memcmp(byte_array, buf, sizeof(byte_array)) == 0);
  93. heap_caps_free(buf);
  94. //Same size, but different chunk:
  95. buf = (uint8_t *)heap_caps_aligned_calloc(1024, 1024, 1, MALLOC_CAP_DEFAULT);
  96. TEST_ASSERT(memcmp(byte_array, buf, sizeof(byte_array)) == 0);
  97. heap_caps_free(buf);
  98. //Alloc from a non permitted area:
  99. uint32_t *not_permitted_buf = (uint32_t *)heap_caps_aligned_calloc(alignments, 1, (alignments + 137), MALLOC_CAP_32BIT);
  100. TEST_ASSERT( not_permitted_buf == NULL );
  101. #if CONFIG_SPIRAM
  102. alignments = 0;
  103. printf("[ALIGNED_ALLOC] Allocating from external memory: \n");
  104. for(;alignments <= 1024 * 512; alignments++) {
  105. //Now try to take aligned memory from IRAM:
  106. uint8_t *buf = (uint8_t *)(uint8_t *)heap_caps_aligned_calloc(alignments, 1, 10*1024, MALLOC_CAP_SPIRAM);
  107. if(((alignments & (alignments - 1)) != 0) || (!alignments)) {
  108. TEST_ASSERT( buf == NULL );
  109. //printf("[ALIGNED_ALLOC] alignment: %"PRIu32" is not a power of two, don't allow allocation \n", aligments);
  110. } else {
  111. TEST_ASSERT( buf != NULL );
  112. printf("[ALIGNED_ALLOC] alignment required: %"PRIu32" \n", alignments);
  113. printf("[ALIGNED_ALLOC] address of allocated memory: %p \n\n", (void *)buf);
  114. //Address of obtained block must be aligned with selected value
  115. TEST_ASSERT(((intptr_t)buf & (alignments - 1)) == 0);
  116. //Write some data, if it corrupts memory probably the heap
  117. //canary verification will fail:
  118. memset(buf, 0xA5, (10*1024));
  119. heap_caps_free(buf);
  120. }
  121. }
  122. #endif
  123. }
  124. TEST_CASE("aligned_alloc(0) should return a NULL pointer", "[heap]")
  125. {
  126. void *p;
  127. p = heap_caps_aligned_alloc(32, 0, MALLOC_CAP_DEFAULT);
  128. TEST_ASSERT(p == NULL);
  129. }