tst-malloc-thread-exit.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /* Test malloc with concurrent thread termination.
  2. Copyright (C) 2015-2019 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. <http://www.gnu.org/licenses/>. */
  15. /* This thread spawns a number of outer threads, equal to the arena
  16. limit. The outer threads run a loop which start and join two
  17. different kinds of threads: the first kind allocates (attaching an
  18. arena to the thread; malloc_first_thread) and waits, the second
  19. kind waits and allocates (wait_first_threads). Both kinds of
  20. threads exit immediately after waiting. The hope is that this will
  21. exhibit races in thread termination and arena management,
  22. particularly related to the arena free list. */
  23. #include <errno.h>
  24. #include <malloc.h>
  25. #include <pthread.h>
  26. #include <stdbool.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <unistd.h>
  30. #include <support/support.h>
  31. #include <support/xthread.h>
  32. #include <support/test-driver.h>
  33. static bool termination_requested;
  34. static int inner_thread_count = 4;
  35. static size_t malloc_size = 32;
  36. static void
  37. __attribute__ ((noinline, noclone))
  38. unoptimized_free (void *ptr)
  39. {
  40. free (ptr);
  41. }
  42. static void *
  43. malloc_first_thread (void * closure)
  44. {
  45. pthread_barrier_t *barrier = closure;
  46. void *ptr = xmalloc (malloc_size);
  47. xpthread_barrier_wait (barrier);
  48. unoptimized_free (ptr);
  49. return NULL;
  50. }
  51. static void *
  52. wait_first_thread (void * closure)
  53. {
  54. pthread_barrier_t *barrier = closure;
  55. xpthread_barrier_wait (barrier);
  56. void *ptr = xmalloc (malloc_size);
  57. unoptimized_free (ptr);
  58. return NULL;
  59. }
  60. static void *
  61. outer_thread (void *closure)
  62. {
  63. pthread_t *threads = xcalloc (sizeof (*threads), inner_thread_count);
  64. while (!__atomic_load_n (&termination_requested, __ATOMIC_RELAXED))
  65. {
  66. pthread_barrier_t barrier;
  67. xpthread_barrier_init (&barrier, NULL, inner_thread_count + 1);
  68. for (int i = 0; i < inner_thread_count; ++i)
  69. {
  70. void *(*func) (void *);
  71. if ((i % 2) == 0)
  72. func = malloc_first_thread;
  73. else
  74. func = wait_first_thread;
  75. threads[i] = xpthread_create (NULL, func, &barrier);
  76. }
  77. xpthread_barrier_wait (&barrier);
  78. for (int i = 0; i < inner_thread_count; ++i)
  79. xpthread_join (threads[i]);
  80. xpthread_barrier_destroy (&barrier);
  81. }
  82. free (threads);
  83. return NULL;
  84. }
  85. static int
  86. do_test (void)
  87. {
  88. /* The number of threads should be smaller than the number of
  89. arenas, so that there will be some free arenas to add to the
  90. arena free list. */
  91. enum { outer_thread_count = 2 };
  92. if (mallopt (M_ARENA_MAX, 8) == 0)
  93. {
  94. printf ("error: mallopt (M_ARENA_MAX) failed\n");
  95. return 1;
  96. }
  97. /* Leave some room for shutting down all threads gracefully. */
  98. int timeout = 3;
  99. if (timeout > DEFAULT_TIMEOUT)
  100. timeout = DEFAULT_TIMEOUT - 1;
  101. pthread_t *threads = xcalloc (sizeof (*threads), outer_thread_count);
  102. for (long i = 0; i < outer_thread_count; ++i)
  103. threads[i] = xpthread_create (NULL, outer_thread, NULL);
  104. struct timespec ts = {timeout, 0};
  105. if (nanosleep (&ts, NULL))
  106. {
  107. printf ("error: error: nanosleep: %m\n");
  108. abort ();
  109. }
  110. __atomic_store_n (&termination_requested, true, __ATOMIC_RELAXED);
  111. for (long i = 0; i < outer_thread_count; ++i)
  112. xpthread_join (threads[i]);
  113. free (threads);
  114. return 0;
  115. }
  116. #include <support/test-driver.c>