test_runtime_heap_reg.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. Tests for registering new heap memory at runtime
  3. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include "unity.h"
  7. #include "esp_heap_caps_init.h"
  8. #include "esp_system.h"
  9. #include "heap_memory_layout.h"
  10. /* NOTE: This is not a well-formed unit test, it leaks memory */
  11. TEST_CASE("Allocate new heap at runtime", "[heap][ignore]")
  12. {
  13. const size_t BUF_SZ = 1000;
  14. const size_t HEAP_OVERHEAD_MAX = 200;
  15. void *buffer = malloc(BUF_SZ);
  16. TEST_ASSERT_NOT_NULL(buffer);
  17. uint32_t before_free = esp_get_free_heap_size();
  18. TEST_ESP_OK( heap_caps_add_region((intptr_t)buffer, (intptr_t)buffer + BUF_SZ) );
  19. uint32_t after_free = esp_get_free_heap_size();
  20. printf("Before %u after %u\n", before_free, after_free);
  21. /* allow for some 'heap overhead' from accounting structures */
  22. TEST_ASSERT(after_free >= before_free + BUF_SZ - HEAP_OVERHEAD_MAX);
  23. }
  24. /* NOTE: This is not a well-formed unit test, it leaks memory and
  25. may fail if run twice in a row without a reset.
  26. */
  27. TEST_CASE("Allocate new heap with new capability", "[heap][ignore]")
  28. {
  29. const size_t BUF_SZ = 100;
  30. #ifdef CONFIG_ESP_SYSTEM_MEMPROT_FEATURE
  31. const size_t ALLOC_SZ = 32;
  32. #else
  33. const size_t ALLOC_SZ = 64; // More than half of BUF_SZ
  34. #endif
  35. const uint32_t MALLOC_CAP_INVENTED = (1 << 30); /* this must be unused in esp_heap_caps.h */
  36. /* no memory exists to provide this capability */
  37. TEST_ASSERT_NULL( heap_caps_malloc(ALLOC_SZ, MALLOC_CAP_INVENTED) );
  38. void *buffer = malloc(BUF_SZ);
  39. TEST_ASSERT_NOT_NULL(buffer);
  40. uint32_t caps[SOC_MEMORY_TYPE_NO_PRIOS] = { MALLOC_CAP_INVENTED };
  41. TEST_ESP_OK( heap_caps_add_region_with_caps(caps, (intptr_t)buffer, (intptr_t)buffer + BUF_SZ) );
  42. /* ta-da, it's now possible! */
  43. TEST_ASSERT_NOT_NULL( heap_caps_malloc(ALLOC_SZ, MALLOC_CAP_INVENTED) );
  44. }
  45. /* NOTE: This is not a well-formed unit test.
  46. * If run twice without a reset, it will failed.
  47. */
  48. TEST_CASE("Add .bss memory to heap region runtime", "[heap][ignore]")
  49. {
  50. #define BUF_SZ 1000
  51. #define HEAP_OVERHEAD_MAX 200
  52. static uint8_t s_buffer[BUF_SZ];
  53. printf("s_buffer start %08x end %08x\n", (intptr_t)s_buffer, (intptr_t)s_buffer + BUF_SZ);
  54. uint32_t before_free = esp_get_free_heap_size();
  55. TEST_ESP_OK( heap_caps_add_region((intptr_t)s_buffer, (intptr_t)s_buffer + BUF_SZ) );
  56. uint32_t after_free = esp_get_free_heap_size();
  57. printf("Before %u after %u\n", before_free, after_free);
  58. /* allow for some 'heap overhead' from accounting structures */
  59. TEST_ASSERT(after_free >= before_free + BUF_SZ - HEAP_OVERHEAD_MAX);
  60. /* Twice add must be failed */
  61. TEST_ASSERT( (heap_caps_add_region((intptr_t)s_buffer, (intptr_t)s_buffer + BUF_SZ) != ESP_OK) );
  62. }
  63. extern esp_err_t heap_caps_check_add_region_allowed(intptr_t heap_start, intptr_t heap_end, intptr_t start, intptr_t end);
  64. TEST_CASE("Add heap region address range checks", "[heap]")
  65. {
  66. const intptr_t heap_start = 0x1000;
  67. const intptr_t heap_end = 0x3000;
  68. TEST_ASSERT_TRUE(heap_caps_check_add_region_allowed(heap_start, heap_end, 0x0, 0x1000));
  69. TEST_ASSERT_TRUE(heap_caps_check_add_region_allowed(heap_start, heap_end, 0x1000, 0x2000));
  70. TEST_ASSERT_TRUE(heap_caps_check_add_region_allowed(heap_start, heap_end, 0x1000, 0x3000));
  71. TEST_ASSERT_TRUE(heap_caps_check_add_region_allowed(heap_start, heap_end, 0x3000, 0x4000));
  72. TEST_ASSERT_FALSE(heap_caps_check_add_region_allowed(heap_start, heap_end, 0x0, 0x2000));
  73. TEST_ASSERT_FALSE(heap_caps_check_add_region_allowed(heap_start, heap_end, 0x0, 0x4000));
  74. TEST_ASSERT_FALSE(heap_caps_check_add_region_allowed(heap_start, heap_end, 0x1000, 0x4000));
  75. TEST_ASSERT_FALSE(heap_caps_check_add_region_allowed(heap_start, heap_end, 0x2000, 0x4000));
  76. }