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