VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstRTR0SemMutexDriver.cpp@ 67808

Last change on this file since 67808 was 62726, checked in by vboxsync, 8 years ago

IPRT/testcases: warnings

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette