test_malloc.c 3.5 KB

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