1 | /* $Id: tstSupSem-Zombie.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Support Library Testcase - Ring-3 Semaphore interface - Zombie bugs.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009-2022 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #include <VBox/sup.h>
|
---|
42 |
|
---|
43 | #include <VBox/param.h>
|
---|
44 | #include <iprt/env.h>
|
---|
45 | #include <iprt/errcore.h>
|
---|
46 | #include <iprt/initterm.h>
|
---|
47 | #include <iprt/process.h>
|
---|
48 | #include <iprt/stream.h>
|
---|
49 | #include <iprt/string.h>
|
---|
50 | #include <iprt/test.h>
|
---|
51 | #include <iprt/time.h>
|
---|
52 | #include <iprt/thread.h>
|
---|
53 |
|
---|
54 |
|
---|
55 | /*********************************************************************************************************************************
|
---|
56 | * Structures and Typedefs *
|
---|
57 | *********************************************************************************************************************************/
|
---|
58 | static PSUPDRVSESSION g_pSession;
|
---|
59 | static RTTEST g_hTest;
|
---|
60 |
|
---|
61 |
|
---|
62 |
|
---|
63 | static DECLCALLBACK(int) tstSupSemSRETimed(RTTHREAD hSelf, void *pvUser)
|
---|
64 | {
|
---|
65 | SUPSEMEVENT hEvent = (SUPSEMEVENT)pvUser;
|
---|
66 | RTThreadUserSignal(hSelf);
|
---|
67 | int rc = SUPSemEventWaitNoResume(g_pSession, hEvent, 120*1000);
|
---|
68 | AssertReleaseMsg(rc == VERR_INTERRUPTED, ("%Rrc\n", rc));
|
---|
69 | return rc;
|
---|
70 | }
|
---|
71 |
|
---|
72 |
|
---|
73 | static DECLCALLBACK(int) tstSupSemMRETimed(RTTHREAD hSelf, void *pvUser)
|
---|
74 | {
|
---|
75 | SUPSEMEVENTMULTI hEventMulti = (SUPSEMEVENTMULTI)pvUser;
|
---|
76 | RTThreadUserSignal(hSelf);
|
---|
77 | int rc = SUPSemEventMultiWaitNoResume(g_pSession, hEventMulti, 120*1000);
|
---|
78 | AssertReleaseMsg(rc == VERR_INTERRUPTED, ("%Rrc\n", rc));
|
---|
79 | return rc;
|
---|
80 | }
|
---|
81 |
|
---|
82 |
|
---|
83 | static DECLCALLBACK(int) tstSupSemSREInf(RTTHREAD hSelf, void *pvUser)
|
---|
84 | {
|
---|
85 | SUPSEMEVENT hEvent = (SUPSEMEVENT)pvUser;
|
---|
86 | RTThreadUserSignal(hSelf);
|
---|
87 | int rc = SUPSemEventWaitNoResume(g_pSession, hEvent, RT_INDEFINITE_WAIT);
|
---|
88 | AssertReleaseMsg(rc == VERR_INTERRUPTED, ("%Rrc\n", rc));
|
---|
89 | return rc;
|
---|
90 | }
|
---|
91 |
|
---|
92 |
|
---|
93 | static DECLCALLBACK(int) tstSupSemMREInf(RTTHREAD hSelf, void *pvUser)
|
---|
94 | {
|
---|
95 | SUPSEMEVENTMULTI hEventMulti = (SUPSEMEVENTMULTI)pvUser;
|
---|
96 | RTThreadUserSignal(hSelf);
|
---|
97 | int rc = SUPSemEventMultiWaitNoResume(g_pSession, hEventMulti, RT_INDEFINITE_WAIT);
|
---|
98 | AssertReleaseMsg(rc == VERR_INTERRUPTED, ("%Rrc\n", rc));
|
---|
99 | return rc;
|
---|
100 | }
|
---|
101 |
|
---|
102 | static int mainChild(void)
|
---|
103 | {
|
---|
104 | /*
|
---|
105 | * Init.
|
---|
106 | */
|
---|
107 | int rc = RTR3InitExeNoArguments(RTR3INIT_FLAGS_TRY_SUPLIB);
|
---|
108 | if (RT_FAILURE(rc))
|
---|
109 | {
|
---|
110 | RTPrintf("tstSupSem-Zombie-Child: fatal error: RTR3InitExeNoArguments failed with rc=%Rrc\n", rc);
|
---|
111 | return 1;
|
---|
112 | }
|
---|
113 |
|
---|
114 | RTTEST hTest;
|
---|
115 | rc = RTTestCreate("tstSupSem-Zombie-Child", &hTest);
|
---|
116 | if (RT_FAILURE(rc))
|
---|
117 | {
|
---|
118 | RTPrintf("tstSupSem-Zombie-Child: fatal error: RTTestCreate failed with rc=%Rrc\n", rc);
|
---|
119 | return 1;
|
---|
120 | }
|
---|
121 | g_hTest = hTest;
|
---|
122 |
|
---|
123 | PSUPDRVSESSION pSession;
|
---|
124 | rc = SUPR3Init(&pSession);
|
---|
125 | if (RT_FAILURE(rc))
|
---|
126 | {
|
---|
127 | RTTestFailed(hTest, "SUPR3Init failed with rc=%Rrc\n", rc);
|
---|
128 | return RTTestSummaryAndDestroy(hTest);
|
---|
129 | }
|
---|
130 | g_pSession = pSession;
|
---|
131 |
|
---|
132 | /*
|
---|
133 | * A semaphore of each kind and throw a bunch of threads on them.
|
---|
134 | */
|
---|
135 | SUPSEMEVENT hEvent = NIL_SUPSEMEVENT;
|
---|
136 | RTTESTI_CHECK_RC(rc = SUPSemEventCreate(pSession, &hEvent), VINF_SUCCESS);
|
---|
137 | if (RT_SUCCESS(rc))
|
---|
138 | {
|
---|
139 | SUPSEMEVENTMULTI hEventMulti = NIL_SUPSEMEVENT;
|
---|
140 | RTTESTI_CHECK_RC(SUPSemEventMultiCreate(pSession, &hEventMulti), VINF_SUCCESS);
|
---|
141 | if (RT_SUCCESS(rc))
|
---|
142 | {
|
---|
143 | for (uint32_t cThreads = 0; cThreads < 5; cThreads++)
|
---|
144 | {
|
---|
145 | RTTHREAD hThread;
|
---|
146 | RTTESTI_CHECK_RC(RTThreadCreate(&hThread, tstSupSemSRETimed, (void *)hEvent, 0, RTTHREADTYPE_TIMER, 0 /*fFlags*/, "IntSRE"), VINF_SUCCESS);
|
---|
147 | RTTESTI_CHECK_RC(RTThreadCreate(&hThread, tstSupSemMRETimed, (void *)hEventMulti, 0, RTTHREADTYPE_TIMER, 0 /*fFlags*/, "IntMRE"), VINF_SUCCESS);
|
---|
148 | RTTESTI_CHECK_RC(RTThreadCreate(&hThread, tstSupSemSREInf, (void *)hEvent, 0, RTTHREADTYPE_TIMER, 0 /*fFlags*/, "IntSRE"), VINF_SUCCESS);
|
---|
149 | RTTESTI_CHECK_RC(RTThreadCreate(&hThread, tstSupSemMREInf, (void *)hEventMulti, 0, RTTHREADTYPE_TIMER, 0 /*fFlags*/, "IntMRE"), VINF_SUCCESS);
|
---|
150 | RTThreadSleep(2);
|
---|
151 | }
|
---|
152 | RTThreadSleep(50);
|
---|
153 |
|
---|
154 | /*
|
---|
155 | * This is where the test really starts...
|
---|
156 | */
|
---|
157 | return 0;
|
---|
158 | }
|
---|
159 | }
|
---|
160 |
|
---|
161 | return RTTestSummaryAndDestroy(hTest);
|
---|
162 | }
|
---|
163 |
|
---|
164 |
|
---|
165 | /**
|
---|
166 | * The parent main routine.
|
---|
167 | * @param argv0 The executable name (or whatever).
|
---|
168 | */
|
---|
169 | static int mainParent(const char *argv0)
|
---|
170 | {
|
---|
171 | /*
|
---|
172 | * Init.
|
---|
173 | */
|
---|
174 | RTTEST hTest;
|
---|
175 | int rc = RTTestInitAndCreate("tstSupSem-Zombie", &hTest);
|
---|
176 | if (rc)
|
---|
177 | return rc;
|
---|
178 | RTTestBanner(hTest);
|
---|
179 |
|
---|
180 | /*
|
---|
181 | * Spin of the child process which may or may not turn into a zombie
|
---|
182 | */
|
---|
183 | for (uint32_t iPass = 0; iPass < 32; iPass++)
|
---|
184 | {
|
---|
185 | RTTestSubF(hTest, "Pass %u", iPass);
|
---|
186 |
|
---|
187 | RTPROCESS hProcess;
|
---|
188 | const char *apszArgs[3] = { argv0, "--child", NULL };
|
---|
189 | RTTESTI_CHECK_RC_OK(rc = RTProcCreate(argv0, apszArgs, RTENV_DEFAULT, 0 /*fFlags*/, &hProcess));
|
---|
190 | if (RT_SUCCESS(rc))
|
---|
191 | {
|
---|
192 | /*
|
---|
193 | * Wait for 60 seconds then give up.
|
---|
194 | */
|
---|
195 | RTPROCSTATUS Status;
|
---|
196 | uint64_t StartTS = RTTimeMilliTS();
|
---|
197 | for (;;)
|
---|
198 | {
|
---|
199 | rc = RTProcWait(hProcess, RTPROCWAIT_FLAGS_NOBLOCK, &Status);
|
---|
200 | if (RT_SUCCESS(rc))
|
---|
201 | break;
|
---|
202 | uint64_t cElapsed = RTTimeMilliTS() - StartTS;
|
---|
203 | if (cElapsed > 60*1000)
|
---|
204 | break;
|
---|
205 | RTThreadSleep(cElapsed < 60 ? 30 : cElapsed < 200 ? 10 : 100);
|
---|
206 | }
|
---|
207 | RTTESTI_CHECK_RC_OK(rc);
|
---|
208 | if ( RT_SUCCESS(rc)
|
---|
209 | && ( Status.enmReason != RTPROCEXITREASON_NORMAL
|
---|
210 | || Status.iStatus != 0))
|
---|
211 | {
|
---|
212 | RTTestIFailed("child %d (%#x) reason %d\n", Status.iStatus, Status.iStatus, Status.enmReason);
|
---|
213 | rc = VERR_PERMISSION_DENIED;
|
---|
214 | }
|
---|
215 | }
|
---|
216 | /* one zombie process is enough. */
|
---|
217 | if (RT_FAILURE(rc))
|
---|
218 | break;
|
---|
219 | }
|
---|
220 |
|
---|
221 | return RTTestSummaryAndDestroy(hTest);
|
---|
222 | }
|
---|
223 |
|
---|
224 |
|
---|
225 | int main(int argc, char **argv)
|
---|
226 | {
|
---|
227 | if ( argc == 2
|
---|
228 | && !strcmp(argv[1], "--child"))
|
---|
229 | return mainChild();
|
---|
230 | return mainParent(argv[0]);
|
---|
231 | }
|
---|
232 |
|
---|