VirtualBox

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

Last change on this file since 62724 was 62721, 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 62721 2016-07-29 22:31:28Z 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 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 */
105static 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 */
176extern "C" DECLEXPORT(int) TrustedMain(int argc, char **argv, char **envp)
177{
178 RT_NOREF3(argc, argv, envp);
179#ifndef VBOX
180 RTPrintf("tstSup: SKIPPED\n");
181 return 0;
182#else
183 /*
184 * Init.
185 */
186 RTTEST hTest;
187 int rc = RTTestInitAndCreate("tstRTR0SemMutex", &hTest);
188 if (rc)
189 return rc;
190 g_hTest = hTest;
191 RTTestBanner(hTest);
192
193 PSUPDRVSESSION pSession;
194 rc = SUPR3Init(&pSession);
195 if (RT_FAILURE(rc))
196 {
197 RTTestFailed(hTest, "SUPR3Init failed with rc=%Rrc\n", rc);
198 return RTTestSummaryAndDestroy(hTest);
199 }
200
201 char szPath[RTPATH_MAX];
202 rc = RTPathExecDir(szPath, sizeof(szPath));
203 if (RT_SUCCESS(rc))
204 rc = RTPathAppend(szPath, sizeof(szPath), "tstRTR0SemMutex.r0");
205 if (RT_FAILURE(rc))
206 {
207 RTTestFailed(hTest, "Failed constructing .r0 filename (rc=%Rrc)", rc);
208 return RTTestSummaryAndDestroy(hTest);
209 }
210
211 void *pvImageBase;
212 rc = SUPR3LoadServiceModule(szPath, "tstRTR0SemMutex",
213 "TSTRTR0SemMutexSrvReqHandler",
214 &pvImageBase);
215 if (RT_FAILURE(rc))
216 {
217 RTTestFailed(hTest, "SUPR3LoadServiceModule(%s,,,) failed with rc=%Rrc\n", szPath, rc);
218 return RTTestSummaryAndDestroy(hTest);
219 }
220
221 /* test request */
222 TSTRTR0SEMMUTEXREQ Req;
223
224 /*
225 * Sanity checks.
226 */
227 RTTestSub(hTest, "Sanity");
228 Req.Hdr.u32Magic = SUPR0SERVICEREQHDR_MAGIC;
229 Req.Hdr.cbReq = sizeof(Req);
230 Req.szMsg[0] = '\0';
231 RTTESTI_CHECK_RC(rc = SUPR3CallR0Service("tstRTR0SemMutex", sizeof("tstRTR0SemMutex") - 1,
232 TSTRTR0SEMMUTEX_SANITY_OK, 0, &Req.Hdr), VINF_SUCCESS);
233 if (RT_FAILURE(rc))
234 return RTTestSummaryAndDestroy(hTest);
235 RTTESTI_CHECK_MSG(Req.szMsg[0] == '\0', ("%s", Req.szMsg));
236 if (Req.szMsg[0] != '\0')
237 return RTTestSummaryAndDestroy(hTest);
238
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_FAILURE, 0, &Req.Hdr), VINF_SUCCESS);
244 if (RT_FAILURE(rc))
245 return RTTestSummaryAndDestroy(hTest);
246 RTTESTI_CHECK_MSG(!strncmp(Req.szMsg, RT_STR_TUPLE("!42failure42")), ("%s", Req.szMsg));
247 if (strncmp(Req.szMsg, RT_STR_TUPLE("!42failure42")))
248 return RTTestSummaryAndDestroy(hTest);
249
250 /*
251 * Basic tests, bail out on failure.
252 */
253 RTTestSub(hTest, "Basics");
254 Req.Hdr.u32Magic = SUPR0SERVICEREQHDR_MAGIC;
255 Req.Hdr.cbReq = sizeof(Req);
256 Req.szMsg[0] = '\0';
257 RTTESTI_CHECK_RC(rc = SUPR3CallR0Service("tstRTR0SemMutex", sizeof("tstRTR0SemMutex") - 1,
258 TSTRTR0SEMMUTEX_BASIC, 0, &Req.Hdr), VINF_SUCCESS);
259 if (RT_FAILURE(rc))
260 return RTTestSummaryAndDestroy(hTest);
261 if (Req.szMsg[0] == '!')
262 {
263 RTTestIFailed("%s", &Req.szMsg[1]);
264 return RTTestSummaryAndDestroy(hTest);
265 }
266 if (Req.szMsg[0])
267 RTTestIPrintf(RTTESTLVL_ALWAYS, "%s", Req.szMsg);
268
269 /*
270 * Tests with multiple threads for bugs in the contention part of the code.
271 * Test #2: Try to hold the semaphore for 1 ms.
272 * Test #3: Grab and release immediately.
273 * Test #4: Timeout checks. Try grab it for 0-32 ms and hold it for 1 s.
274 */
275 tstDoThreadedTest(TSTRTR0SEMMUTEX_TEST2_SETUP, TSTRTR0SEMMUTEX_TEST2_DO, TSTRTR0SEMMUTEX_TEST2_CLEANUP, 1, 1, &Req, "test #2");
276 tstDoThreadedTest(TSTRTR0SEMMUTEX_TEST2_SETUP, TSTRTR0SEMMUTEX_TEST2_DO, TSTRTR0SEMMUTEX_TEST2_CLEANUP, 2, 3, &Req, "test #2");
277 tstDoThreadedTest(TSTRTR0SEMMUTEX_TEST2_SETUP, TSTRTR0SEMMUTEX_TEST2_DO, TSTRTR0SEMMUTEX_TEST2_CLEANUP, 3, 3, &Req, "test #2");
278 tstDoThreadedTest(TSTRTR0SEMMUTEX_TEST2_SETUP, TSTRTR0SEMMUTEX_TEST2_DO, TSTRTR0SEMMUTEX_TEST2_CLEANUP, 9, 3, &Req, "test #2");
279
280 tstDoThreadedTest(TSTRTR0SEMMUTEX_TEST3_SETUP, TSTRTR0SEMMUTEX_TEST3_DO, TSTRTR0SEMMUTEX_TEST3_CLEANUP, 1, 1, &Req, "test #3");
281 tstDoThreadedTest(TSTRTR0SEMMUTEX_TEST3_SETUP, TSTRTR0SEMMUTEX_TEST3_DO, TSTRTR0SEMMUTEX_TEST3_CLEANUP, 2, 3, &Req, "test #3");
282 tstDoThreadedTest(TSTRTR0SEMMUTEX_TEST3_SETUP, TSTRTR0SEMMUTEX_TEST3_DO, TSTRTR0SEMMUTEX_TEST3_CLEANUP, 3, 3, &Req, "test #3");
283 tstDoThreadedTest(TSTRTR0SEMMUTEX_TEST3_SETUP, TSTRTR0SEMMUTEX_TEST3_DO, TSTRTR0SEMMUTEX_TEST3_CLEANUP, 9, 3, &Req, "test #3");
284
285 tstDoThreadedTest(TSTRTR0SEMMUTEX_TEST4_SETUP, TSTRTR0SEMMUTEX_TEST4_DO, TSTRTR0SEMMUTEX_TEST4_CLEANUP, 1, 1, &Req, "test #4");
286 tstDoThreadedTest(TSTRTR0SEMMUTEX_TEST4_SETUP, TSTRTR0SEMMUTEX_TEST4_DO, TSTRTR0SEMMUTEX_TEST4_CLEANUP, 2, 3, &Req, "test #4");
287 tstDoThreadedTest(TSTRTR0SEMMUTEX_TEST4_SETUP, TSTRTR0SEMMUTEX_TEST4_DO, TSTRTR0SEMMUTEX_TEST4_CLEANUP, 3, 3, &Req, "test #4");
288 tstDoThreadedTest(TSTRTR0SEMMUTEX_TEST4_SETUP, TSTRTR0SEMMUTEX_TEST4_DO, TSTRTR0SEMMUTEX_TEST4_CLEANUP, 9, 3, &Req, "test #4");
289
290 /*
291 * Done.
292 */
293 return RTTestSummaryAndDestroy(hTest);
294#endif
295}
296
297
298#if !defined(VBOX_WITH_HARDENING) || !defined(RT_OS_WINDOWS)
299/**
300 * Main entry point.
301 */
302int main(int argc, char **argv, char **envp)
303{
304 return TrustedMain(argc, argv, envp);
305}
306#endif
307
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