1 /* $NetBSD: t_once.c,v 1.3 2025/03/30 23:03:06 riastradh Exp $ */
2 
3 /*
4  * Copyright (c) 2008 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __COPYRIGHT("@(#) Copyright (c) 2008\
31  The NetBSD Foundation, inc. All rights reserved.");
32 __RCSID("$NetBSD: t_once.c,v 1.3 2025/03/30 23:03:06 riastradh Exp $");
33 
34 #include <sys/time.h>
35 #include <sys/wait.h>
36 
37 #include <pthread.h>
38 #include <signal.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <unistd.h>
42 
43 #include <atf-c.h>
44 
45 #include "h_common.h"
46 #include "h_macros.h"
47 
48 static pthread_once_t once = PTHREAD_ONCE_INIT;
49 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
50 static int x;
51 
52 #define NTHREADS 25
53 
54 static void
ofunc(void)55 ofunc(void)
56 {
57           printf("Variable x has value %d\n", x);
58           x++;
59 }
60 
61 static void
ofunc_silent(void)62 ofunc_silent(void)
63 {
64           x++;
65 }
66 
67 ATF_TC(once1);
ATF_TC_HEAD(once1,tc)68 ATF_TC_HEAD(once1, tc)
69 {
70           atf_tc_set_md_var(tc, "descr", "Checks pthread_once()");
71 }
ATF_TC_BODY(once1,tc)72 ATF_TC_BODY(once1, tc)
73 {
74 
75           printf("1: Test 1 of pthread_once()\n");
76 
77           PTHREAD_REQUIRE(pthread_once(&once, ofunc));
78           PTHREAD_REQUIRE(pthread_once(&once, ofunc));
79 
80           printf("1: X has value %d\n",x );
81           ATF_REQUIRE_EQ(x, 1);
82 }
83 
84 static void
once2_ofunc(void)85 once2_ofunc(void)
86 {
87           x++;
88           printf("ofunc: Variable x has value %d\n", x);
89           x++;
90 }
91 
92 static void *
once2_threadfunc(void * arg)93 once2_threadfunc(void *arg)
94 {
95           int num;
96 
97           PTHREAD_REQUIRE(pthread_once(&once, once2_ofunc));
98 
99           num = *(int *)arg;
100           printf("Thread %d sees x with value %d\n", num, x);
101           ATF_REQUIRE_EQ(x, 2);
102 
103           return NULL;
104 }
105 
106 ATF_TC(once2);
ATF_TC_HEAD(once2,tc)107 ATF_TC_HEAD(once2, tc)
108 {
109           atf_tc_set_md_var(tc, "descr", "Checks pthread_once()");
110 }
ATF_TC_BODY(once2,tc)111 ATF_TC_BODY(once2, tc)
112 {
113           pthread_t  threads[NTHREADS];
114           int id[NTHREADS];
115           int i;
116 
117           printf("1: Test 2 of pthread_once()\n");
118 
119           for (i=0; i < NTHREADS; i++) {
120                     id[i] = i;
121                     PTHREAD_REQUIRE(pthread_create(&threads[i], NULL, once2_threadfunc, &id[i]));
122           }
123 
124           for (i=0; i < NTHREADS; i++)
125                     PTHREAD_REQUIRE(pthread_join(threads[i], NULL));
126 
127           printf("1: X has value %d\n",x );
128           ATF_REQUIRE_EQ(x, 2);
129 }
130 
131 static void
once3_cleanup(void * m)132 once3_cleanup(void *m)
133 {
134           pthread_mutex_t *mu = m;
135 
136           PTHREAD_REQUIRE(pthread_mutex_unlock(mu));
137 }
138 
139 static void
once3_ofunc(void)140 once3_ofunc(void)
141 {
142           pthread_testcancel();
143 }
144 
145 static void *
once3_threadfunc(void * arg)146 once3_threadfunc(void *arg)
147 {
148           PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
149           pthread_cleanup_push(once3_cleanup, &mutex);
150           PTHREAD_REQUIRE(pthread_once(&once, once3_ofunc));
151           pthread_cleanup_pop(1);
152 
153           return NULL;
154 }
155 
156 static void
handler(int sig,siginfo_t * info,void * ctx)157 handler(int sig, siginfo_t *info, void *ctx)
158 {
159           atf_tc_fail("Signal handler was called; "
160                     "main thread deadlocked in pthread_once()");
161 }
162 
163 ATF_TC(once3);
ATF_TC_HEAD(once3,tc)164 ATF_TC_HEAD(once3, tc)
165 {
166           atf_tc_set_md_var(tc, "descr", "Checks pthread_once()");
167 }
ATF_TC_BODY(once3,tc)168 ATF_TC_BODY(once3, tc)
169 {
170           pthread_t thread;
171           struct sigaction act;
172           struct itimerval it;
173           printf("Test 3 of pthread_once() (test versus cancellation)\n");
174 
175           act.sa_sigaction = handler;
176           sigemptyset(&act.sa_mask);
177           act.sa_flags = SA_SIGINFO;
178           sigaction(SIGALRM, &act, NULL);
179 
180           timerclear(&it.it_value);
181           it.it_value.tv_usec = 500000;
182           timerclear(&it.it_interval);
183           setitimer(ITIMER_REAL, &it, NULL);
184 
185           PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
186           PTHREAD_REQUIRE(pthread_create(&thread, NULL, once3_threadfunc, NULL));
187           PTHREAD_REQUIRE(pthread_cancel(thread));
188           PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
189           PTHREAD_REQUIRE(pthread_join(thread, NULL));
190 
191           PTHREAD_REQUIRE(pthread_once(&once, ofunc));
192 
193           /* Cancel timer */
194           timerclear(&it.it_value);
195           setitimer(ITIMER_REAL, &it, NULL);
196 
197           printf("Test succeeded\n");
198 }
199 
200 static long trial;
201 
202 static void *
fork_and_once(void * cookie)203 fork_and_once(void *cookie)
204 {
205           pthread_barrier_t *bar = cookie;
206           pid_t pid, child;
207           int status;
208 
209           (void)pthread_barrier_wait(bar);
210           RL(pid = fork());
211           if (pid == 0) {
212                     (void)alarm(1);
213                     (void)pthread_once(&once, &ofunc_silent);
214                     _exit(x - 1);
215           }
216           RL(child = waitpid(pid, &status, 0));
217           ATF_REQUIRE_EQ_MSG(child, pid, "child=%lld pid=%lld",
218               (long long)child, (long long)pid);
219           ATF_REQUIRE_MSG(!WIFSIGNALED(status),
220               "child exited on signal %d (%s) in trial %ld",
221               WTERMSIG(status), strsignal(WTERMSIG(status)), trial);
222           ATF_REQUIRE_MSG(WIFEXITED(status) && WEXITSTATUS(status) == 0,
223               "child exited 0x%x in trial %ld", status, trial);
224           return NULL;
225 }
226 
227 ATF_TC(oncefork);
ATF_TC_HEAD(oncefork,tc)228 ATF_TC_HEAD(oncefork, tc)
229 {
230           atf_tc_set_md_var(tc, "descr", "Test racing pthread_once with fork");
231 }
ATF_TC_BODY(oncefork,tc)232 ATF_TC_BODY(oncefork, tc)
233 {
234           static pthread_once_t once0 = PTHREAD_ONCE_INIT;
235           pthread_barrier_t bar;
236           long ntrials = atf_tc_get_config_var_as_long_wd(tc,
237               "pthread_once_forktrials", 0);
238 
239           if (ntrials <= 0) {
240                     atf_tc_skip("pthread_once takes thousands of fork trials"
241                         " on a multicore system to detect a race; set"
242                         " pthread_once_forktrials to the number of trials to"
243                         " enable this test");
244           }
245 
246           RZ(pthread_barrier_init(&bar, NULL, 2));
247 
248           for (trial = 0; trial < ntrials; trial++) {
249                     pthread_t t;
250 
251                     once = once0;
252                     x = 0;
253 
254                     RZ(pthread_create(&t, NULL, &fork_and_once, &bar));
255                     (void)alarm(1);
256                     (void)pthread_barrier_wait(&bar);
257                     (void)pthread_once(&once, &ofunc_silent);
258                     (void)alarm(0);
259                     RZ(pthread_join(t, NULL));
260           }
261 }
262 
ATF_TP_ADD_TCS(tp)263 ATF_TP_ADD_TCS(tp)
264 {
265           ATF_TP_ADD_TC(tp, once1);
266           ATF_TP_ADD_TC(tp, once2);
267           ATF_TP_ADD_TC(tp, once3);
268           ATF_TP_ADD_TC(tp, oncefork);
269 
270           return atf_no_error();
271 }
272