tst-malloc-tcache-leak.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* Bug 22111: Test that threads do not leak their per thread cache.
  2. Copyright (C) 2015-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. /* The point of this test is to start and exit a large number of
  16. threads, while at the same time looking to see if the used
  17. memory grows with each round of threads run. If the memory
  18. grows above some linear bound we declare the test failed and
  19. that the malloc implementation is leaking memory with each
  20. thread. This is a good indicator that the thread local cache
  21. is leaking chunks. */
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <malloc.h>
  25. #include <pthread.h>
  26. #include <assert.h>
  27. #include <libc-diag.h>
  28. #include <support/check.h>
  29. #include <support/support.h>
  30. #include <support/xthread.h>
  31. void *
  32. worker (void *data)
  33. {
  34. void *ret;
  35. /* Allocate an arbitrary amount of memory that is known to fit into
  36. the thread local cache (tcache). If we have at least 64 bins
  37. (default e.g. TCACHE_MAX_BINS) we should be able to allocate 32
  38. bytes and force malloc to fill the tcache. We are assuming tcache
  39. init happens at the first small alloc, but it might in the future
  40. be deferred to some other point. Therefore to future proof this
  41. test we include a full alloc/free/alloc cycle for the thread. We
  42. need a compiler barrier to avoid the removal of the useless
  43. alloc/free. We send some memory back to main to have the memory
  44. freed after the thread dies, as just another check that the chunks
  45. that were previously in the tcache are still OK to free after
  46. thread death. */
  47. ret = xmalloc (32);
  48. __asm__ volatile ("" ::: "memory");
  49. free (ret);
  50. return (void *) xmalloc (32);
  51. }
  52. static int
  53. do_test (void)
  54. {
  55. pthread_t *thread;
  56. struct mallinfo info_before, info_after;
  57. void *retval;
  58. /* This is an arbitrary choice. We choose a total of THREADS
  59. threads created and joined. This gives us enough iterations to
  60. show a leak. */
  61. int threads = 100000;
  62. /* Avoid there being 0 malloc'd data at this point by allocating the
  63. pthread_t required to run the test. */
  64. thread = (pthread_t *) xcalloc (1, sizeof (pthread_t));
  65. /* The test below covers the deprecated mallinfo function. */
  66. DIAG_PUSH_NEEDS_COMMENT;
  67. DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wdeprecated-declarations");
  68. info_before = mallinfo ();
  69. assert (info_before.uordblks != 0);
  70. printf ("INFO: %d (bytes) are in use before starting threads.\n",
  71. info_before.uordblks);
  72. for (int loop = 0; loop < threads; loop++)
  73. {
  74. *thread = xpthread_create (NULL, worker, NULL);
  75. retval = xpthread_join (*thread);
  76. free (retval);
  77. }
  78. info_after = mallinfo ();
  79. printf ("INFO: %d (bytes) are in use after all threads joined.\n",
  80. info_after.uordblks);
  81. /* We need to compare the memory in use before and the memory in use
  82. after starting and joining THREADS threads. We almost always grow
  83. memory slightly, but not much. Consider that if even 1-byte leaked
  84. per thread we'd have THREADS bytes of additional memory, and in
  85. general the in-use at the start of main is quite low. We will
  86. always leak a full malloc chunk, and never just 1-byte, therefore
  87. anything above "+ threads" from the start (constant offset) is a
  88. leak. Obviously this assumes no thread-related malloc'd internal
  89. libc data structures persist beyond the thread death, and any that
  90. did would limit the number of times you could call pthread_create,
  91. which is a QoI we'd want to detect and fix. */
  92. if (info_after.uordblks > (info_before.uordblks + threads))
  93. FAIL_EXIT1 ("Memory usage after threads is too high.\n");
  94. DIAG_POP_NEEDS_COMMENT;
  95. /* Did not detect excessive memory usage. */
  96. free (thread);
  97. exit (0);
  98. }
  99. #define TIMEOUT 50
  100. #include <support/test-driver.c>