tst-mallocfork2.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /* Test case for async-signal-safe fork (with respect to malloc).
  2. Copyright (C) 2016-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 License as
  6. published by the Free Software Foundation; either version 2.1 of the
  7. 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; see the file COPYING.LIB. If
  14. not, see <https://www.gnu.org/licenses/>. */
  15. /* This test will fail if the process is multi-threaded because we
  16. only have an async-signal-safe fork in the single-threaded case
  17. (where we skip acquiring the malloc heap locks).
  18. This test only checks async-signal-safety with regards to malloc;
  19. other, more rarely-used glibc subsystems could have locks which
  20. still make fork unsafe, even in single-threaded processes. */
  21. #include <errno.h>
  22. #include <sched.h>
  23. #include <signal.h>
  24. #include <stdbool.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <sys/wait.h>
  29. #include <time.h>
  30. #include <unistd.h>
  31. #include <array_length.h>
  32. #include <support/check.h>
  33. #include <support/support.h>
  34. #include <support/xthread.h>
  35. #include <support/xunistd.h>
  36. /* How many malloc objects to keep arond. */
  37. enum { malloc_objects = 1009 };
  38. /* The maximum size of an object. */
  39. enum { malloc_maximum_size = 70000 };
  40. /* How many iterations the test performs before exiting. */
  41. enum { iterations = 10000 };
  42. /* Barrier for synchronization with the processes sending SIGUSR1
  43. signals, to make it more likely that the signals arrive during a
  44. fork/free/malloc call. */
  45. static struct { pthread_barrier_t barrier; } *shared;
  46. /* Set to 1 if SIGUSR1 is received. Used to detect a signal during
  47. fork/free/malloc. */
  48. static volatile sig_atomic_t sigusr1_received;
  49. /* Periodically set to 1, to indicate that the process is making
  50. progress. Checked by liveness_signal_handler. */
  51. static volatile sig_atomic_t progress_indicator = 1;
  52. /* Set to 1 if an error occurs in the signal handler. */
  53. static volatile sig_atomic_t error_indicator = 0;
  54. static void
  55. sigusr1_handler (int signo)
  56. {
  57. sigusr1_received = 1;
  58. /* Perform a fork with a trivial subprocess. */
  59. pid_t pid = fork ();
  60. if (pid == -1)
  61. {
  62. write_message ("error: fork\n");
  63. error_indicator = 1;
  64. return;
  65. }
  66. if (pid == 0)
  67. _exit (0);
  68. int status;
  69. int ret = TEMP_FAILURE_RETRY (waitpid (pid, &status, 0));
  70. if (ret < 0)
  71. {
  72. write_message ("error: waitpid\n");
  73. error_indicator = 1;
  74. return;
  75. }
  76. if (status != 0)
  77. {
  78. write_message ("error: unexpected exit status from subprocess\n");
  79. error_indicator = 1;
  80. return;
  81. }
  82. }
  83. static void
  84. liveness_signal_handler (int signo)
  85. {
  86. if (progress_indicator)
  87. progress_indicator = 0;
  88. else
  89. write_message ("warning: process seems to be stuck\n");
  90. }
  91. /* Send SIGNO to the parent process. If SLEEP, wait a second between
  92. signals, otherwise use barriers to delay sending signals. */
  93. static void
  94. __attribute__ ((noreturn))
  95. signal_sender (int signo, bool sleep)
  96. {
  97. pid_t target = getppid ();
  98. while (true)
  99. {
  100. if (!sleep)
  101. xpthread_barrier_wait (&shared->barrier);
  102. if (kill (target, signo) != 0)
  103. {
  104. dprintf (STDOUT_FILENO, "error: kill: %m\n");
  105. abort ();
  106. }
  107. if (sleep)
  108. usleep (1 * 1000 * 1000);
  109. else
  110. xpthread_barrier_wait (&shared->barrier);
  111. }
  112. }
  113. /* Children processes. */
  114. static pid_t sigusr1_sender_pids[5] = { 0 };
  115. static pid_t sigusr2_sender_pid = 0;
  116. static void
  117. kill_children (void)
  118. {
  119. for (size_t i = 0; i < array_length (sigusr1_sender_pids); ++i)
  120. if (sigusr1_sender_pids[i] > 0)
  121. kill (sigusr1_sender_pids[i], SIGKILL);
  122. if (sigusr2_sender_pid > 0)
  123. kill (sigusr2_sender_pid, SIGKILL);
  124. }
  125. static int
  126. do_test (void)
  127. {
  128. atexit (kill_children);
  129. /* shared->barrier is initialized along with sigusr1_sender_pids
  130. below. */
  131. shared = support_shared_allocate (sizeof (*shared));
  132. struct sigaction action =
  133. {
  134. .sa_handler = sigusr1_handler,
  135. };
  136. sigemptyset (&action.sa_mask);
  137. if (sigaction (SIGUSR1, &action, NULL) != 0)
  138. {
  139. printf ("error: sigaction: %m");
  140. return 1;
  141. }
  142. action.sa_handler = liveness_signal_handler;
  143. if (sigaction (SIGUSR2, &action, NULL) != 0)
  144. {
  145. printf ("error: sigaction: %m");
  146. return 1;
  147. }
  148. sigusr2_sender_pid = xfork ();
  149. if (sigusr2_sender_pid == 0)
  150. signal_sender (SIGUSR2, true);
  151. /* Send SIGUSR1 signals from several processes. Hopefully, one
  152. signal will hit one of the critical functions. Use a barrier to
  153. avoid sending signals while not running fork/free/malloc. */
  154. {
  155. pthread_barrierattr_t attr;
  156. xpthread_barrierattr_init (&attr);
  157. xpthread_barrierattr_setpshared (&attr, PTHREAD_PROCESS_SHARED);
  158. xpthread_barrier_init (&shared->barrier, &attr,
  159. array_length (sigusr1_sender_pids) + 1);
  160. xpthread_barrierattr_destroy (&attr);
  161. }
  162. for (size_t i = 0; i < array_length (sigusr1_sender_pids); ++i)
  163. {
  164. sigusr1_sender_pids[i] = xfork ();
  165. if (sigusr1_sender_pids[i] == 0)
  166. signal_sender (SIGUSR1, false);
  167. }
  168. void *objects[malloc_objects] = {};
  169. unsigned int fork_signals = 0;
  170. unsigned int free_signals = 0;
  171. unsigned int malloc_signals = 0;
  172. unsigned seed = 1;
  173. for (int i = 0; i < iterations; ++i)
  174. {
  175. progress_indicator = 1;
  176. int slot = rand_r (&seed) % malloc_objects;
  177. size_t size = rand_r (&seed) % malloc_maximum_size;
  178. /* Occasionally do a fork first, to catch deadlocks there as
  179. well (see bug 24161). */
  180. bool do_fork = (rand_r (&seed) % 7) == 0;
  181. xpthread_barrier_wait (&shared->barrier);
  182. if (do_fork)
  183. {
  184. sigusr1_received = 0;
  185. pid_t pid = xfork ();
  186. if (sigusr1_received)
  187. ++fork_signals;
  188. if (pid == 0)
  189. _exit (0);
  190. int status;
  191. int ret = TEMP_FAILURE_RETRY (waitpid (pid, &status, 0));
  192. if (ret < 0)
  193. FAIL_EXIT1 ("waitpid: %m");
  194. TEST_COMPARE (status, 0);
  195. }
  196. sigusr1_received = 0;
  197. free (objects[slot]);
  198. if (sigusr1_received)
  199. ++free_signals;
  200. sigusr1_received = 0;
  201. objects[slot] = malloc (size);
  202. if (sigusr1_received)
  203. ++malloc_signals;
  204. xpthread_barrier_wait (&shared->barrier);
  205. if (objects[slot] == NULL || error_indicator != 0)
  206. {
  207. printf ("error: malloc: %m\n");
  208. for (size_t i = 0; i < array_length (sigusr1_sender_pids); ++i)
  209. kill (sigusr1_sender_pids[i], SIGKILL);
  210. kill (sigusr2_sender_pid, SIGKILL);
  211. return 1;
  212. }
  213. }
  214. /* Clean up allocations. */
  215. for (int slot = 0; slot < malloc_objects; ++slot)
  216. free (objects[slot]);
  217. printf ("info: signals received during fork: %u\n", fork_signals);
  218. printf ("info: signals received during free: %u\n", free_signals);
  219. printf ("info: signals received during malloc: %u\n", malloc_signals);
  220. /* Do not destroy the barrier because of the SIGKILL above, which
  221. may have left the barrier in an inconsistent state. */
  222. support_shared_free (shared);
  223. return 0;
  224. }
  225. #define TIMEOUT 100
  226. #include <support/test-driver.c>