1 | /* $Id: tstRTR0SemMutexDriver.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT R0 Testcase - Thread Preemption, driver program.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009-2023 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 <iprt/initterm.h>
|
---|
42 |
|
---|
43 | #include <iprt/errcore.h>
|
---|
44 | #include <iprt/path.h>
|
---|
45 | #include <iprt/param.h>
|
---|
46 | #include <iprt/stream.h>
|
---|
47 | #include <iprt/string.h>
|
---|
48 | #include <iprt/test.h>
|
---|
49 | #include <iprt/thread.h>
|
---|
50 | #ifdef VBOX
|
---|
51 | # include <VBox/sup.h>
|
---|
52 | # include "tstRTR0SemMutex.h"
|
---|
53 | #endif
|
---|
54 |
|
---|
55 |
|
---|
56 | /*********************************************************************************************************************************
|
---|
57 | * Structures and Typedefs *
|
---|
58 | *********************************************************************************************************************************/
|
---|
59 | typedef struct TSTRTR0SEMMUTEXREQ
|
---|
60 | {
|
---|
61 | SUPR0SERVICEREQHDR Hdr;
|
---|
62 | char szMsg[256];
|
---|
63 | } TSTRTR0SEMMUTEXREQ;
|
---|
64 | typedef TSTRTR0SEMMUTEXREQ *PTSTRTR0SEMMUTEXREQ;
|
---|
65 |
|
---|
66 |
|
---|
67 | /*********************************************************************************************************************************
|
---|
68 | * Global Variables *
|
---|
69 | *********************************************************************************************************************************/
|
---|
70 | static RTTEST g_hTest;
|
---|
71 |
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * Thread function employed by tstDoThreadedTest.
|
---|
75 | *
|
---|
76 | * @returns IPRT status code.
|
---|
77 | * @param hThreadSelf The thread.
|
---|
78 | * @param pvUser The operation to perform.
|
---|
79 | */
|
---|
80 | static DECLCALLBACK(int) tstThreadFn(RTTHREAD hThreadSelf, void *pvUser)
|
---|
81 | {
|
---|
82 | RT_NOREF1(hThreadSelf);
|
---|
83 | uint32_t u32 = (uint32_t)(uintptr_t)pvUser;
|
---|
84 | TSTRTR0SEMMUTEX enmDo = (TSTRTR0SEMMUTEX)RT_LOWORD(u32);
|
---|
85 | uint32_t cSecs = RT_HIWORD(u32);
|
---|
86 | TSTRTR0SEMMUTEXREQ Req;
|
---|
87 | RT_ZERO(Req);
|
---|
88 | Req.Hdr.u32Magic = SUPR0SERVICEREQHDR_MAGIC;
|
---|
89 | Req.Hdr.cbReq = sizeof(Req);
|
---|
90 | Req.szMsg[0] = '\0';
|
---|
91 | RTTEST_CHECK_RC_RET(g_hTest, SUPR3CallR0Service("tstRTR0SemMutex", sizeof("tstRTR0SemMutex") - 1,
|
---|
92 | enmDo, cSecs, &Req.Hdr),
|
---|
93 | VINF_SUCCESS,
|
---|
94 | rcCheck);
|
---|
95 | if (Req.szMsg[0] == '!')
|
---|
96 | {
|
---|
97 | RTTestFailed(g_hTest, "%s", &Req.szMsg[1]);
|
---|
98 | return VERR_GENERAL_FAILURE;
|
---|
99 | }
|
---|
100 | if (Req.szMsg[0])
|
---|
101 | RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "%s", Req.szMsg);
|
---|
102 | return VINF_SUCCESS;
|
---|
103 | }
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * Performs a threaded test.
|
---|
107 | *
|
---|
108 | * @returns true on success, false on failure.
|
---|
109 | * @param enmSetup The setup operation number.
|
---|
110 | * @param enmDo The do-it operation number.
|
---|
111 | * @param enmCleanup The cleanup operation number.
|
---|
112 | * @param cThreads The number of threads.
|
---|
113 | * @param pReq The request structure.
|
---|
114 | * @param pszTest The test name.
|
---|
115 | */
|
---|
116 | static bool tstDoThreadedTest(TSTRTR0SEMMUTEX enmSetup, TSTRTR0SEMMUTEX enmDo, TSTRTR0SEMMUTEX enmCleanup,
|
---|
117 | unsigned cThreads, unsigned cSecs, PTSTRTR0SEMMUTEXREQ pReq, const char *pszTest)
|
---|
118 | {
|
---|
119 | RTTestSubF(g_hTest, "%s - %u threads", pszTest, cThreads);
|
---|
120 | RTTHREAD ahThreads[32];
|
---|
121 | RTTESTI_CHECK_RET(cThreads <= RT_ELEMENTS(ahThreads), false);
|
---|
122 |
|
---|
123 | /*
|
---|
124 | * Setup.
|
---|
125 | */
|
---|
126 | pReq->Hdr.u32Magic = SUPR0SERVICEREQHDR_MAGIC;
|
---|
127 | pReq->Hdr.cbReq = sizeof(*pReq);
|
---|
128 | pReq->szMsg[0] = '\0';
|
---|
129 | RTTESTI_CHECK_RC_RET(SUPR3CallR0Service("tstRTR0SemMutex", sizeof("tstRTR0SemMutex") - 1, enmSetup, 0, &pReq->Hdr),
|
---|
130 | VINF_SUCCESS, false);
|
---|
131 | if (pReq->szMsg[0] == '!')
|
---|
132 | return !!RTTestIFailedRc(false, "%s", &pReq->szMsg[1]);
|
---|
133 | if (pReq->szMsg[0])
|
---|
134 | RTTestIPrintf(RTTESTLVL_ALWAYS, "%s", pReq->szMsg);
|
---|
135 |
|
---|
136 | /*
|
---|
137 | * Test execution.
|
---|
138 | */
|
---|
139 | for (unsigned i = 0; i < RT_ELEMENTS(ahThreads); i++)
|
---|
140 | ahThreads[i] = NIL_RTTHREAD;
|
---|
141 |
|
---|
142 | int rc = VINF_SUCCESS;
|
---|
143 | for (unsigned i = 0; i < cThreads && RT_SUCCESS(rc); i++)
|
---|
144 | rc = RTThreadCreateF(&ahThreads[i], tstThreadFn, (void *)(uintptr_t)RT_MAKE_U32(enmDo, cSecs), 0, RTTHREADTYPE_DEFAULT,
|
---|
145 | RTTHREADFLAGS_WAITABLE, "test-%u", i);
|
---|
146 |
|
---|
147 | for (unsigned i = 0; i < cThreads; i++)
|
---|
148 | if (ahThreads[i] != NIL_RTTHREAD)
|
---|
149 | {
|
---|
150 | int rcThread = VINF_SUCCESS;
|
---|
151 | int rc2 = RTThreadWait(ahThreads[i], 3600*1000, &rcThread);
|
---|
152 | if (RT_SUCCESS(rc2))
|
---|
153 | {
|
---|
154 | ahThreads[i] = NIL_RTTHREAD;
|
---|
155 | if (RT_FAILURE(rcThread) && RT_SUCCESS(rc2))
|
---|
156 | rc = rcThread;
|
---|
157 | }
|
---|
158 | else if (RT_SUCCESS(rc))
|
---|
159 | rc = rc2;
|
---|
160 | }
|
---|
161 |
|
---|
162 | /*
|
---|
163 | * Cleanup.
|
---|
164 | */
|
---|
165 | pReq->Hdr.u32Magic = SUPR0SERVICEREQHDR_MAGIC;
|
---|
166 | pReq->Hdr.cbReq = sizeof(*pReq);
|
---|
167 | pReq->szMsg[0] = '\0';
|
---|
168 | RTTESTI_CHECK_RC_RET(rc = SUPR3CallR0Service("tstRTR0SemMutex", sizeof("tstRTR0SemMutex") - 1, enmCleanup, 0, &pReq->Hdr),
|
---|
169 | VINF_SUCCESS, false);
|
---|
170 | if (pReq->szMsg[0] == '!')
|
---|
171 | return !!RTTestIFailedRc(false, "%s", &pReq->szMsg[1]);
|
---|
172 | if (pReq->szMsg[0])
|
---|
173 | RTTestIPrintf(RTTESTLVL_ALWAYS, "%s", pReq->szMsg);
|
---|
174 |
|
---|
175 | if (RT_FAILURE(rc))
|
---|
176 | for (unsigned i = 0; i < cThreads; i++)
|
---|
177 | if (ahThreads[i] != NIL_RTTHREAD)
|
---|
178 | RTThreadWait(ahThreads[i], 1000, NULL);
|
---|
179 |
|
---|
180 | return RT_SUCCESS(rc);
|
---|
181 | }
|
---|
182 |
|
---|
183 |
|
---|
184 | /**
|
---|
185 | * Entry point.
|
---|
186 | */
|
---|
187 | extern "C" DECLEXPORT(int) TrustedMain(int argc, char **argv, char **envp)
|
---|
188 | {
|
---|
189 | RT_NOREF3(argc, argv, envp);
|
---|
190 | #ifndef VBOX
|
---|
191 | RTPrintf("tstSup: SKIPPED\n");
|
---|
192 | return 0;
|
---|
193 | #else
|
---|
194 | /*
|
---|
195 | * Init.
|
---|
196 | */
|
---|
197 | RTTEST hTest;
|
---|
198 | int rc = RTTestInitAndCreate("tstRTR0SemMutex", &hTest);
|
---|
199 | if (rc)
|
---|
200 | return rc;
|
---|
201 | g_hTest = hTest;
|
---|
202 | RTTestBanner(hTest);
|
---|
203 |
|
---|
204 | PSUPDRVSESSION pSession;
|
---|
205 | rc = SUPR3Init(&pSession);
|
---|
206 | if (RT_FAILURE(rc))
|
---|
207 | {
|
---|
208 | RTTestFailed(hTest, "SUPR3Init failed with rc=%Rrc\n", rc);
|
---|
209 | return RTTestSummaryAndDestroy(hTest);
|
---|
210 | }
|
---|
211 |
|
---|
212 | char szPath[RTPATH_MAX];
|
---|
213 | rc = RTPathExecDir(szPath, sizeof(szPath));
|
---|
214 | if (RT_SUCCESS(rc))
|
---|
215 | rc = RTPathAppend(szPath, sizeof(szPath), "tstRTR0SemMutex.r0");
|
---|
216 | if (RT_FAILURE(rc))
|
---|
217 | {
|
---|
218 | RTTestFailed(hTest, "Failed constructing .r0 filename (rc=%Rrc)", rc);
|
---|
219 | return RTTestSummaryAndDestroy(hTest);
|
---|
220 | }
|
---|
221 |
|
---|
222 | void *pvImageBase;
|
---|
223 | rc = SUPR3LoadServiceModule(szPath, "tstRTR0SemMutex",
|
---|
224 | "TSTRTR0SemMutexSrvReqHandler",
|
---|
225 | &pvImageBase);
|
---|
226 | if (RT_FAILURE(rc))
|
---|
227 | {
|
---|
228 | RTTestFailed(hTest, "SUPR3LoadServiceModule(%s,,,) failed with rc=%Rrc\n", szPath, rc);
|
---|
229 | return RTTestSummaryAndDestroy(hTest);
|
---|
230 | }
|
---|
231 |
|
---|
232 | /* test request */
|
---|
233 | TSTRTR0SEMMUTEXREQ Req;
|
---|
234 |
|
---|
235 | /*
|
---|
236 | * Sanity checks.
|
---|
237 | */
|
---|
238 | RTTestSub(hTest, "Sanity");
|
---|
239 | Req.Hdr.u32Magic = SUPR0SERVICEREQHDR_MAGIC;
|
---|
240 | Req.Hdr.cbReq = sizeof(Req);
|
---|
241 | Req.szMsg[0] = '\0';
|
---|
242 | RTTESTI_CHECK_RC(rc = SUPR3CallR0Service("tstRTR0SemMutex", sizeof("tstRTR0SemMutex") - 1,
|
---|
243 | TSTRTR0SEMMUTEX_SANITY_OK, 0, &Req.Hdr), VINF_SUCCESS);
|
---|
244 | if (RT_FAILURE(rc))
|
---|
245 | return RTTestSummaryAndDestroy(hTest);
|
---|
246 | RTTESTI_CHECK_MSG(Req.szMsg[0] == '\0', ("%s", Req.szMsg));
|
---|
247 | if (Req.szMsg[0] != '\0')
|
---|
248 | return RTTestSummaryAndDestroy(hTest);
|
---|
249 |
|
---|
250 | Req.Hdr.u32Magic = SUPR0SERVICEREQHDR_MAGIC;
|
---|
251 | Req.Hdr.cbReq = sizeof(Req);
|
---|
252 | Req.szMsg[0] = '\0';
|
---|
253 | RTTESTI_CHECK_RC(rc = SUPR3CallR0Service("tstRTR0SemMutex", sizeof("tstRTR0SemMutex") - 1,
|
---|
254 | TSTRTR0SEMMUTEX_SANITY_FAILURE, 0, &Req.Hdr), VINF_SUCCESS);
|
---|
255 | if (RT_FAILURE(rc))
|
---|
256 | return RTTestSummaryAndDestroy(hTest);
|
---|
257 | RTTESTI_CHECK_MSG(!strncmp(Req.szMsg, RT_STR_TUPLE("!42failure42")), ("%s", Req.szMsg));
|
---|
258 | if (strncmp(Req.szMsg, RT_STR_TUPLE("!42failure42")))
|
---|
259 | return RTTestSummaryAndDestroy(hTest);
|
---|
260 |
|
---|
261 | /*
|
---|
262 | * Basic tests, bail out on failure.
|
---|
263 | */
|
---|
264 | RTTestSub(hTest, "Basics");
|
---|
265 | Req.Hdr.u32Magic = SUPR0SERVICEREQHDR_MAGIC;
|
---|
266 | Req.Hdr.cbReq = sizeof(Req);
|
---|
267 | Req.szMsg[0] = '\0';
|
---|
268 | RTTESTI_CHECK_RC(rc = SUPR3CallR0Service("tstRTR0SemMutex", sizeof("tstRTR0SemMutex") - 1,
|
---|
269 | TSTRTR0SEMMUTEX_BASIC, 0, &Req.Hdr), VINF_SUCCESS);
|
---|
270 | if (RT_FAILURE(rc))
|
---|
271 | return RTTestSummaryAndDestroy(hTest);
|
---|
272 | if (Req.szMsg[0] == '!')
|
---|
273 | {
|
---|
274 | RTTestIFailed("%s", &Req.szMsg[1]);
|
---|
275 | return RTTestSummaryAndDestroy(hTest);
|
---|
276 | }
|
---|
277 | if (Req.szMsg[0])
|
---|
278 | RTTestIPrintf(RTTESTLVL_ALWAYS, "%s", Req.szMsg);
|
---|
279 |
|
---|
280 | /*
|
---|
281 | * Tests with multiple threads for bugs in the contention part of the code.
|
---|
282 | * Test #2: Try to hold the semaphore for 1 ms.
|
---|
283 | * Test #3: Grab and release immediately.
|
---|
284 | * Test #4: Timeout checks. Try grab it for 0-32 ms and hold it for 1 s.
|
---|
285 | */
|
---|
286 | tstDoThreadedTest(TSTRTR0SEMMUTEX_TEST2_SETUP, TSTRTR0SEMMUTEX_TEST2_DO, TSTRTR0SEMMUTEX_TEST2_CLEANUP, 1, 1, &Req, "test #2");
|
---|
287 | tstDoThreadedTest(TSTRTR0SEMMUTEX_TEST2_SETUP, TSTRTR0SEMMUTEX_TEST2_DO, TSTRTR0SEMMUTEX_TEST2_CLEANUP, 2, 3, &Req, "test #2");
|
---|
288 | tstDoThreadedTest(TSTRTR0SEMMUTEX_TEST2_SETUP, TSTRTR0SEMMUTEX_TEST2_DO, TSTRTR0SEMMUTEX_TEST2_CLEANUP, 3, 3, &Req, "test #2");
|
---|
289 | tstDoThreadedTest(TSTRTR0SEMMUTEX_TEST2_SETUP, TSTRTR0SEMMUTEX_TEST2_DO, TSTRTR0SEMMUTEX_TEST2_CLEANUP, 9, 3, &Req, "test #2");
|
---|
290 |
|
---|
291 | tstDoThreadedTest(TSTRTR0SEMMUTEX_TEST3_SETUP, TSTRTR0SEMMUTEX_TEST3_DO, TSTRTR0SEMMUTEX_TEST3_CLEANUP, 1, 1, &Req, "test #3");
|
---|
292 | tstDoThreadedTest(TSTRTR0SEMMUTEX_TEST3_SETUP, TSTRTR0SEMMUTEX_TEST3_DO, TSTRTR0SEMMUTEX_TEST3_CLEANUP, 2, 3, &Req, "test #3");
|
---|
293 | tstDoThreadedTest(TSTRTR0SEMMUTEX_TEST3_SETUP, TSTRTR0SEMMUTEX_TEST3_DO, TSTRTR0SEMMUTEX_TEST3_CLEANUP, 3, 3, &Req, "test #3");
|
---|
294 | tstDoThreadedTest(TSTRTR0SEMMUTEX_TEST3_SETUP, TSTRTR0SEMMUTEX_TEST3_DO, TSTRTR0SEMMUTEX_TEST3_CLEANUP, 9, 3, &Req, "test #3");
|
---|
295 |
|
---|
296 | tstDoThreadedTest(TSTRTR0SEMMUTEX_TEST4_SETUP, TSTRTR0SEMMUTEX_TEST4_DO, TSTRTR0SEMMUTEX_TEST4_CLEANUP, 1, 1, &Req, "test #4");
|
---|
297 | tstDoThreadedTest(TSTRTR0SEMMUTEX_TEST4_SETUP, TSTRTR0SEMMUTEX_TEST4_DO, TSTRTR0SEMMUTEX_TEST4_CLEANUP, 2, 3, &Req, "test #4");
|
---|
298 | tstDoThreadedTest(TSTRTR0SEMMUTEX_TEST4_SETUP, TSTRTR0SEMMUTEX_TEST4_DO, TSTRTR0SEMMUTEX_TEST4_CLEANUP, 3, 3, &Req, "test #4");
|
---|
299 | tstDoThreadedTest(TSTRTR0SEMMUTEX_TEST4_SETUP, TSTRTR0SEMMUTEX_TEST4_DO, TSTRTR0SEMMUTEX_TEST4_CLEANUP, 9, 3, &Req, "test #4");
|
---|
300 |
|
---|
301 | /*
|
---|
302 | * Done.
|
---|
303 | */
|
---|
304 | return RTTestSummaryAndDestroy(hTest);
|
---|
305 | #endif
|
---|
306 | }
|
---|
307 |
|
---|
308 |
|
---|
309 | #if !defined(VBOX_WITH_HARDENING) || !defined(RT_OS_WINDOWS)
|
---|
310 | /**
|
---|
311 | * Main entry point.
|
---|
312 | */
|
---|
313 | int main(int argc, char **argv, char **envp)
|
---|
314 | {
|
---|
315 | return TrustedMain(argc, argv, envp);
|
---|
316 | }
|
---|
317 | #endif
|
---|
318 |
|
---|