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