1/* Restartable Sequences NPTL test.
2 Copyright (C) 2021-2024 Free Software Foundation, Inc.
3
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
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
17
18/* These tests validate that rseq is registered from various execution
19 contexts (main thread, destructor, other threads, other threads created
20 from destructor, forked process (without exec), pthread_atfork handlers,
21 pthread setspecific destructors, signal handlers, atexit handlers).
22
23 See the Linux kernel selftests for extensive rseq stress-tests. */
24
25#include <stdio.h>
26#include <support/check.h>
27#include <support/xthread.h>
28#include <sys/rseq.h>
29#include <unistd.h>
30
31#ifdef RSEQ_SIG
32# include <array_length.h>
33# include <errno.h>
34# include <error.h>
35# include <pthread.h>
36# include <signal.h>
37# include <stdlib.h>
38# include <string.h>
39# include <support/namespace.h>
40# include <support/xsignal.h>
41# include <syscall.h>
42# include <sys/types.h>
43# include <sys/wait.h>
44# include "tst-rseq.h"
45
46static pthread_key_t rseq_test_key;
47
48static void
49atfork_prepare (void)
50{
51 if (!rseq_thread_registered ())
52 {
53 printf (format: "error: rseq not registered in pthread atfork prepare\n");
54 support_record_failure ();
55 }
56}
57
58static void
59atfork_parent (void)
60{
61 if (!rseq_thread_registered ())
62 {
63 printf (format: "error: rseq not registered in pthread atfork parent\n");
64 support_record_failure ();
65 }
66}
67
68static void
69atfork_child (void)
70{
71 if (!rseq_thread_registered ())
72 {
73 printf (format: "error: rseq not registered in pthread atfork child\n");
74 support_record_failure ();
75 }
76}
77
78static void
79rseq_key_destructor (void *arg)
80{
81 /* Cannot use deferred failure reporting after main returns. */
82 if (!rseq_thread_registered ())
83 FAIL_EXIT1 ("rseq not registered in pthread key destructor");
84}
85
86static void
87atexit_handler (void)
88{
89 /* Cannot use deferred failure reporting after main returns. */
90 if (!rseq_thread_registered ())
91 FAIL_EXIT1 ("rseq not registered in atexit handler");
92}
93
94/* Used to avoid -Werror=stringop-overread warning with
95 pthread_setspecific and GCC 11. */
96static char one = 1;
97
98static void
99do_rseq_main_test (void)
100{
101 TEST_COMPARE (atexit (atexit_handler), 0);
102 rseq_test_key = xpthread_key_create (destr_function: rseq_key_destructor);
103 TEST_COMPARE (pthread_atfork (atfork_prepare, atfork_parent, atfork_child), 0);
104 xraise (SIGUSR1);
105 TEST_COMPARE (pthread_setspecific (rseq_test_key, &one), 0);
106 TEST_VERIFY_EXIT (rseq_thread_registered ());
107}
108
109static void
110cancel_routine (void *arg)
111{
112 if (!rseq_thread_registered ())
113 {
114 printf (format: "error: rseq not registered in cancel routine\n");
115 support_record_failure ();
116 }
117}
118
119static pthread_barrier_t cancel_thread_barrier;
120static pthread_cond_t cancel_thread_cond = PTHREAD_COND_INITIALIZER;
121static pthread_mutex_t cancel_thread_mutex = PTHREAD_MUTEX_INITIALIZER;
122
123static void
124test_cancel_thread (void)
125{
126 pthread_cleanup_push (cancel_routine, NULL);
127 (void) xpthread_barrier_wait (barrier: &cancel_thread_barrier);
128 /* Wait forever until cancellation. */
129 xpthread_cond_wait (cond: &cancel_thread_cond, mutex: &cancel_thread_mutex);
130 pthread_cleanup_pop (0);
131}
132
133static void *
134thread_function (void * arg)
135{
136 int i = (int) (intptr_t) arg;
137
138 xraise (SIGUSR1);
139 if (i == 0)
140 test_cancel_thread ();
141 TEST_COMPARE (pthread_setspecific (rseq_test_key, &one), 0);
142 return rseq_thread_registered () ? NULL : (void *) 1l;
143}
144
145static void
146sighandler (int sig)
147{
148 if (!rseq_thread_registered ())
149 {
150 printf (format: "error: rseq not registered in signal handler\n");
151 support_record_failure ();
152 }
153}
154
155static void
156setup_signals (void)
157{
158 struct sigaction sa;
159
160 sigemptyset (&sa.sa_mask);
161 sigaddset (&sa.sa_mask, SIGUSR1);
162 sa.sa_flags = 0;
163 sa.sa_handler = sighandler;
164 xsigaction (SIGUSR1, newact: &sa, NULL);
165}
166
167static int
168do_rseq_threads_test (int nr_threads)
169{
170 pthread_t th[nr_threads];
171 int i;
172 int result = 0;
173
174 xpthread_barrier_init (barrier: &cancel_thread_barrier, NULL, count: 2);
175
176 for (i = 0; i < nr_threads; ++i)
177 th[i] = xpthread_create (NULL, thread_func: thread_function,
178 closure: (void *) (intptr_t) i);
179
180 (void) xpthread_barrier_wait (barrier: &cancel_thread_barrier);
181
182 xpthread_cancel (thr: th[0]);
183
184 for (i = 0; i < nr_threads; ++i)
185 {
186 void *v;
187
188 v = xpthread_join (thr: th[i]);
189 if (i != 0 && v != NULL)
190 {
191 printf (format: "error: join %d successful, but child failed\n", i);
192 result = 1;
193 }
194 else if (i == 0 && v == NULL)
195 {
196 printf (format: "error: join %d successful, child did not fail as expected\n", i);
197 result = 1;
198 }
199 }
200
201 xpthread_barrier_destroy (barrier: &cancel_thread_barrier);
202
203 return result;
204}
205
206static void
207subprocess_callback (void *closure)
208{
209 do_rseq_main_test ();
210}
211
212static void
213do_rseq_fork_test (void)
214{
215 support_isolate_in_subprocess (callback: subprocess_callback, NULL);
216}
217
218static int
219do_rseq_test (void)
220{
221 int t[] = { 1, 2, 6, 5, 4, 3, 50 };
222 int i, result = 0;
223
224 if (!rseq_available ())
225 FAIL_UNSUPPORTED ("kernel does not support rseq, skipping test");
226 setup_signals ();
227 xraise (SIGUSR1);
228 do_rseq_main_test ();
229 for (i = 0; i < array_length (t); i++)
230 if (do_rseq_threads_test (nr_threads: t[i]))
231 result = 1;
232 do_rseq_fork_test ();
233 return result;
234}
235
236static void __attribute__ ((destructor))
237do_rseq_destructor_test (void)
238{
239 /* Cannot use deferred failure reporting after main returns. */
240 if (do_rseq_test ())
241 FAIL_EXIT1 ("rseq not registered within destructor");
242 xpthread_key_delete (key: rseq_test_key);
243}
244
245#else /* RSEQ_SIG */
246static int
247do_rseq_test (void)
248{
249 FAIL_UNSUPPORTED ("glibc does not define RSEQ_SIG, skipping test");
250 return 0;
251}
252#endif /* RSEQ_SIG */
253
254static int
255do_test (void)
256{
257 return do_rseq_test ();
258}
259
260#include <support/test-driver.c>
261

source code of glibc/sysdeps/unix/sysv/linux/tst-rseq-nptl.c