test_malloc.c 3.5 KB

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