VirtualBox

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

Last change on this file since 53401 was 51906, checked in by vboxsync, 10 years ago

Fixing testcases on windows.

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