test_realloc.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. TEST_CASE("realloc shrink buffer with EXEC CAPS", "[heap]")
  21. {
  22. const size_t buffer_size = 64;
  23. void *x = heap_caps_malloc(buffer_size, MALLOC_CAP_EXEC);
  24. TEST_ASSERT(x);
  25. void *y = heap_caps_realloc(x, buffer_size - 16, MALLOC_CAP_EXEC);
  26. TEST_ASSERT(y);
  27. //y needs to fall in a compatible memory area of IRAM:
  28. TEST_ASSERT(esp_ptr_executable(y));
  29. free(y);
  30. }
  31. TEST_CASE("realloc move data to a new heap type", "[heap]")
  32. {
  33. const char *test = "I am some test content to put in the heap";
  34. char buf[64];
  35. memset(buf, 0xEE, 64);
  36. strlcpy(buf, test, 64);
  37. char *a = malloc(64);
  38. memcpy(a, buf, 64);
  39. // move data from 'a' to IRAM
  40. char *b = heap_caps_realloc(a, 64, MALLOC_CAP_EXEC);
  41. TEST_ASSERT_NOT_NULL(b);
  42. TEST_ASSERT_NOT_EQUAL(a, b);
  43. TEST_ASSERT(heap_caps_check_integrity(MALLOC_CAP_INVALID, true));
  44. TEST_ASSERT_EQUAL_HEX32_ARRAY(buf, b, 64/sizeof(uint32_t));
  45. // Move data back to DRAM
  46. char *c = heap_caps_realloc(b, 48, MALLOC_CAP_8BIT);
  47. TEST_ASSERT_NOT_NULL(c);
  48. TEST_ASSERT_NOT_EQUAL(b, c);
  49. TEST_ASSERT(heap_caps_check_integrity(MALLOC_CAP_INVALID, true));
  50. TEST_ASSERT_EQUAL_HEX8_ARRAY(buf, c, 48);
  51. free(c);
  52. }