test_realloc.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. Generic test for realloc
  3. */
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include "unity.h"
  7. #include "sdkconfig.h"
  8. #include "esp_heap_caps.h"
  9. #include "soc/soc_memory_layout.h"
  10. #ifndef CONFIG_HEAP_POISONING_COMPREHENSIVE
  11. /* (can't realloc in place if comprehensive is enabled) */
  12. TEST_CASE("realloc shrink buffer in place", "[heap]")
  13. {
  14. void *x = malloc(64);
  15. TEST_ASSERT(x);
  16. void *y = realloc(x, 48);
  17. TEST_ASSERT_EQUAL_PTR(x, y);
  18. }
  19. #endif
  20. #if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32C2)
  21. //IDF-5167
  22. #ifndef CONFIG_ESP_SYSTEM_MEMPROT_FEATURE
  23. TEST_CASE("realloc shrink buffer with EXEC CAPS", "[heap]")
  24. {
  25. const size_t buffer_size = 64;
  26. void *x = heap_caps_malloc(buffer_size, MALLOC_CAP_EXEC);
  27. TEST_ASSERT(x);
  28. void *y = heap_caps_realloc(x, buffer_size - 16, MALLOC_CAP_EXEC);
  29. TEST_ASSERT(y);
  30. //y needs to fall in a compatible memory area of IRAM:
  31. TEST_ASSERT(esp_ptr_executable(y)|| esp_ptr_in_iram(y) || esp_ptr_in_diram_iram(y));
  32. free(y);
  33. }
  34. TEST_CASE("realloc move data to a new heap type", "[heap]")
  35. {
  36. const char *test = "I am some test content to put in the heap";
  37. char buf[64];
  38. memset(buf, 0xEE, 64);
  39. strlcpy(buf, test, 64);
  40. char *a = malloc(64);
  41. memcpy(a, buf, 64);
  42. // move data from 'a' to IRAM
  43. char *b = heap_caps_realloc(a, 64, MALLOC_CAP_EXEC);
  44. TEST_ASSERT_NOT_NULL(b);
  45. TEST_ASSERT(heap_caps_check_integrity(MALLOC_CAP_INVALID, true));
  46. TEST_ASSERT_EQUAL_HEX32_ARRAY(buf, b, 64 / sizeof(uint32_t));
  47. // Move data back to DRAM
  48. char *c = heap_caps_realloc(b, 48, MALLOC_CAP_8BIT);
  49. TEST_ASSERT_NOT_NULL(c);
  50. TEST_ASSERT(heap_caps_check_integrity(MALLOC_CAP_INVALID, true));
  51. TEST_ASSERT_EQUAL_HEX8_ARRAY(buf, c, 48);
  52. free(c);
  53. }
  54. #endif
  55. #endif //!TEMPORARY_DISABLED_FOR_TARGETS(ESP32C2)