test_realloc.c 1.7 KB

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