test_malloc.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. Generic test for malloc/free
  3. */
  4. #include <esp_types.h>
  5. #include <stdio.h>
  6. #include "freertos/FreeRTOS.h"
  7. #include "freertos/task.h"
  8. #include "freertos/semphr.h"
  9. #include "freertos/queue.h"
  10. #include "unity.h"
  11. #include "esp_heap_caps.h"
  12. #include "sdkconfig.h"
  13. static int **allocatedMem;
  14. static int noAllocated;
  15. static int tryAllocMem(void) {
  16. int i, j;
  17. const int allocateMaxK=1024*5; //try to allocate a max of 5MiB
  18. allocatedMem=malloc(sizeof(int *)*allocateMaxK);
  19. if (!allocatedMem) return 0;
  20. for (i=0; i<allocateMaxK; i++) {
  21. allocatedMem[i]=malloc(1024);
  22. if (allocatedMem[i]==NULL) break;
  23. for (j=0; j<1024/4; j++) allocatedMem[i][j]=(0xdeadbeef);
  24. }
  25. noAllocated=i;
  26. return i;
  27. }
  28. static void tryAllocMemFree(void) {
  29. int i, j;
  30. for (i=0; i<noAllocated; i++) {
  31. for (j=0; j<1024/4; j++) {
  32. TEST_ASSERT(allocatedMem[i][j]==(0xdeadbeef));
  33. }
  34. free(allocatedMem[i]);
  35. }
  36. free(allocatedMem);
  37. }
  38. TEST_CASE("Malloc/overwrite, then free all available DRAM", "[heap]")
  39. {
  40. int m1=0, m2=0;
  41. m1=tryAllocMem();
  42. tryAllocMemFree();
  43. m2=tryAllocMem();
  44. tryAllocMemFree();
  45. printf("Could allocate %dK on first try, %dK on 2nd try.\n", m1, m2);
  46. TEST_ASSERT(m1==m2);
  47. }
  48. #if CONFIG_SPIRAM_USE_MALLOC
  49. #if (CONFIG_SPIRAM_MALLOC_RESERVE_INTERNAL > 1024)
  50. TEST_CASE("Check if reserved DMA pool still can allocate even when malloc()'ed memory is exhausted", "[heap]")
  51. {
  52. char** dmaMem=malloc(sizeof(char*)*512);
  53. assert(dmaMem);
  54. int m=tryAllocMem();
  55. int i=0;
  56. for (i=0; i<512; i++) {
  57. dmaMem[i]=heap_caps_malloc(1024, MALLOC_CAP_DMA);
  58. if (dmaMem[i]==NULL) break;
  59. }
  60. for (int j=0; j<i; j++) free(dmaMem[j]);
  61. free(dmaMem);
  62. tryAllocMemFree();
  63. printf("Could allocate %dK of DMA memory after allocating all of %dK of normal memory.\n", i, m);
  64. TEST_ASSERT(i);
  65. }
  66. #endif
  67. #endif
  68. /* As you see, we are desperately trying to outsmart the compiler, so that it
  69. * doesn't warn about oversized allocations in the next two unit tests.
  70. * To be removed when we switch to GCC 8.2 and add
  71. * -Wno-alloc-size-larger-than=PTRDIFF_MAX to CFLAGS for this file.
  72. */
  73. void* (*g_test_malloc_ptr)(size_t) = &malloc;
  74. void* (*g_test_calloc_ptr)(size_t, size_t) = &calloc;
  75. void* test_malloc_wrapper(size_t size)
  76. {
  77. return (*g_test_malloc_ptr)(size);
  78. }
  79. void* test_calloc_wrapper(size_t count, size_t size)
  80. {
  81. return (*g_test_calloc_ptr)(count, size);
  82. }
  83. TEST_CASE("alloc overflows should all fail", "[heap]")
  84. {
  85. /* allocates 8 bytes if size_t overflows */
  86. TEST_ASSERT_NULL(test_calloc_wrapper(SIZE_MAX / 2 + 4, 2));
  87. /* will overflow if any poisoning is enabled
  88. (should fail for sensible OOM reasons, otherwise) */
  89. TEST_ASSERT_NULL(test_malloc_wrapper(SIZE_MAX - 1));
  90. TEST_ASSERT_NULL(test_calloc_wrapper(SIZE_MAX - 1, 1));
  91. /* will overflow when the size is rounded up to word align it */
  92. TEST_ASSERT_NULL(heap_caps_malloc(SIZE_MAX-1, MALLOC_CAP_32BIT));
  93. TEST_ASSERT_NULL(heap_caps_malloc(SIZE_MAX-1, MALLOC_CAP_EXEC));
  94. }
  95. TEST_CASE("unreasonable allocs should all fail", "[heap]")
  96. {
  97. TEST_ASSERT_NULL(test_calloc_wrapper(16, 1024*1024));
  98. TEST_ASSERT_NULL(test_malloc_wrapper(16*1024*1024));
  99. TEST_ASSERT_NULL(test_malloc_wrapper(SIZE_MAX / 2));
  100. TEST_ASSERT_NULL(test_malloc_wrapper(SIZE_MAX - 256));
  101. TEST_ASSERT_NULL(test_malloc_wrapper(xPortGetFreeHeapSize() - 1));
  102. }
  103. TEST_CASE("malloc(0) should return a NULL pointer", "[heap]")
  104. {
  105. void *p;
  106. p = malloc(0);
  107. TEST_ASSERT(p == NULL);
  108. }