1 | /* $Id: tstVMREQ.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VMM Testcase.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2022 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 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 |
|
---|
29 | /*********************************************************************************************************************************
|
---|
30 | * Header Files *
|
---|
31 | *********************************************************************************************************************************/
|
---|
32 | #include <VBox/vmm/vm.h>
|
---|
33 | #include <VBox/vmm/vmm.h>
|
---|
34 | #include <VBox/vmm/cpum.h>
|
---|
35 | #include <VBox/err.h>
|
---|
36 | #include <VBox/log.h>
|
---|
37 | #include <iprt/assert.h>
|
---|
38 | #include <iprt/initterm.h>
|
---|
39 | #include <iprt/semaphore.h>
|
---|
40 | #include <iprt/stream.h>
|
---|
41 | #include <iprt/string.h>
|
---|
42 | #include <iprt/thread.h>
|
---|
43 | #include <iprt/time.h>
|
---|
44 |
|
---|
45 |
|
---|
46 | /*********************************************************************************************************************************
|
---|
47 | * Defined Constants And Macros *
|
---|
48 | *********************************************************************************************************************************/
|
---|
49 | #define TESTCASE "tstVMREQ"
|
---|
50 |
|
---|
51 |
|
---|
52 | /*********************************************************************************************************************************
|
---|
53 | * Global Variables *
|
---|
54 | *********************************************************************************************************************************/
|
---|
55 | /** the error count. */
|
---|
56 | static int g_cErrors = 0;
|
---|
57 |
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * Testings va_list passing in VMSetRuntimeError.
|
---|
61 | */
|
---|
62 | static DECLCALLBACK(void) MyAtRuntimeError(PUVM pUVM, void *pvUser, uint32_t fFlags, const char *pszErrorId, const char *pszFormat, va_list va)
|
---|
63 | {
|
---|
64 | NOREF(pUVM);
|
---|
65 | if (strcmp((const char *)pvUser, "user argument"))
|
---|
66 | {
|
---|
67 | RTPrintf(TESTCASE ": pvUser=%p:{%s}!\n", pvUser, (const char *)pvUser);
|
---|
68 | g_cErrors++;
|
---|
69 | }
|
---|
70 | if (fFlags)
|
---|
71 | {
|
---|
72 | RTPrintf(TESTCASE ": fFlags=%#x!\n", fFlags);
|
---|
73 | g_cErrors++;
|
---|
74 | }
|
---|
75 | if (strcmp(pszErrorId, "enum"))
|
---|
76 | {
|
---|
77 | RTPrintf(TESTCASE ": pszErrorId=%p:{%s}!\n", pszErrorId, pszErrorId);
|
---|
78 | g_cErrors++;
|
---|
79 | }
|
---|
80 | if (strcmp(pszFormat, "some %s string"))
|
---|
81 | {
|
---|
82 | RTPrintf(TESTCASE ": pszFormat=%p:{%s}!\n", pszFormat, pszFormat);
|
---|
83 | g_cErrors++;
|
---|
84 | }
|
---|
85 |
|
---|
86 | char szBuf[1024];
|
---|
87 | RTStrPrintfV(szBuf, sizeof(szBuf), pszFormat, va);
|
---|
88 | if (strcmp(szBuf, "some error string"))
|
---|
89 | {
|
---|
90 | RTPrintf(TESTCASE ": RTStrPrintfV -> '%s'!\n", szBuf);
|
---|
91 | g_cErrors++;
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * The function PassVA and PassVA2 calls.
|
---|
98 | */
|
---|
99 | static DECLCALLBACK(int) PassVACallback(PUVM pUVM, unsigned u4K, unsigned u1G, const char *pszFormat, va_list *pva)
|
---|
100 | {
|
---|
101 | NOREF(pUVM);
|
---|
102 | if (u4K != _4K)
|
---|
103 | {
|
---|
104 | RTPrintf(TESTCASE ": u4K=%#x!\n", u4K);
|
---|
105 | g_cErrors++;
|
---|
106 | }
|
---|
107 | if (u1G != _1G)
|
---|
108 | {
|
---|
109 | RTPrintf(TESTCASE ": u1G=%#x!\n", u1G);
|
---|
110 | g_cErrors++;
|
---|
111 | }
|
---|
112 |
|
---|
113 | if (strcmp(pszFormat, "hello %s"))
|
---|
114 | {
|
---|
115 | RTPrintf(TESTCASE ": pszFormat=%p:{%s}!\n", pszFormat, pszFormat);
|
---|
116 | g_cErrors++;
|
---|
117 | }
|
---|
118 |
|
---|
119 | char szBuf[1024];
|
---|
120 | RTStrPrintfV(szBuf, sizeof(szBuf), pszFormat, *pva);
|
---|
121 | if (strcmp(szBuf, "hello world"))
|
---|
122 | {
|
---|
123 | RTPrintf(TESTCASE ": RTStrPrintfV -> '%s'!\n", szBuf);
|
---|
124 | g_cErrors++;
|
---|
125 | }
|
---|
126 |
|
---|
127 | return VINF_SUCCESS;
|
---|
128 | }
|
---|
129 |
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * Functions that tests passing a va_list * argument in a request,
|
---|
133 | * similar to VMSetRuntimeError.
|
---|
134 | */
|
---|
135 | static void PassVA2(PUVM pUVM, const char *pszFormat, va_list va)
|
---|
136 | {
|
---|
137 | #if 0 /** @todo test if this is a GCC problem only or also happens with AMD64+VCC80... */
|
---|
138 | void *pvVA = &va;
|
---|
139 | #else
|
---|
140 | va_list va2;
|
---|
141 | va_copy(va2, va);
|
---|
142 | void *pvVA = &va2;
|
---|
143 | #endif
|
---|
144 |
|
---|
145 | int rc = VMR3ReqCallWaitU(pUVM, VMCPUID_ANY, (PFNRT)PassVACallback, 5, pUVM, _4K, _1G, pszFormat, pvVA);
|
---|
146 | NOREF(rc);
|
---|
147 |
|
---|
148 | #if 1
|
---|
149 | va_end(va2);
|
---|
150 | #endif
|
---|
151 | }
|
---|
152 |
|
---|
153 |
|
---|
154 | /**
|
---|
155 | * Functions that tests passing a va_list * argument in a request,
|
---|
156 | * similar to VMSetRuntimeError.
|
---|
157 | */
|
---|
158 | static void PassVA(PUVM pUVM, const char *pszFormat, ...)
|
---|
159 | {
|
---|
160 | /* 1st test */
|
---|
161 | va_list va1;
|
---|
162 | va_start(va1, pszFormat);
|
---|
163 | int rc = VMR3ReqCallWaitU(pUVM, VMCPUID_ANY, (PFNRT)PassVACallback, 5, pUVM, _4K, _1G, pszFormat, &va1);
|
---|
164 | va_end(va1);
|
---|
165 | NOREF(rc);
|
---|
166 |
|
---|
167 | /* 2nd test */
|
---|
168 | va_list va2;
|
---|
169 | va_start(va2, pszFormat);
|
---|
170 | PassVA2(pUVM, pszFormat, va2);
|
---|
171 | va_end(va2);
|
---|
172 | }
|
---|
173 |
|
---|
174 |
|
---|
175 | /**
|
---|
176 | * Thread function which allocates and frees requests like wildfire.
|
---|
177 | */
|
---|
178 | static DECLCALLBACK(int) Thread(RTTHREAD hThreadSelf, void *pvUser)
|
---|
179 | {
|
---|
180 | int rc = VINF_SUCCESS;
|
---|
181 | PUVM pUVM = (PUVM)pvUser;
|
---|
182 | NOREF(hThreadSelf);
|
---|
183 |
|
---|
184 | for (unsigned i = 0; i < 100000; i++)
|
---|
185 | {
|
---|
186 | PVMREQ apReq[17];
|
---|
187 | const unsigned cReqs = i % RT_ELEMENTS(apReq);
|
---|
188 | unsigned iReq;
|
---|
189 | for (iReq = 0; iReq < cReqs; iReq++)
|
---|
190 | {
|
---|
191 | rc = VMR3ReqAlloc(pUVM, &apReq[iReq], VMREQTYPE_INTERNAL, VMCPUID_ANY);
|
---|
192 | if (RT_FAILURE(rc))
|
---|
193 | {
|
---|
194 | RTPrintf(TESTCASE ": i=%d iReq=%d cReqs=%d rc=%Rrc (alloc)\n", i, iReq, cReqs, rc);
|
---|
195 | return rc;
|
---|
196 | }
|
---|
197 | apReq[iReq]->iStatus = iReq + i;
|
---|
198 | }
|
---|
199 |
|
---|
200 | for (iReq = 0; iReq < cReqs; iReq++)
|
---|
201 | {
|
---|
202 | if (apReq[iReq]->iStatus != (int)(iReq + i))
|
---|
203 | {
|
---|
204 | RTPrintf(TESTCASE ": i=%d iReq=%d cReqs=%d: iStatus=%d != %d\n", i, iReq, cReqs, apReq[iReq]->iStatus, iReq + i);
|
---|
205 | return VERR_GENERAL_FAILURE;
|
---|
206 | }
|
---|
207 | rc = VMR3ReqFree(apReq[iReq]);
|
---|
208 | if (RT_FAILURE(rc))
|
---|
209 | {
|
---|
210 | RTPrintf(TESTCASE ": i=%d iReq=%d cReqs=%d rc=%Rrc (free)\n", i, iReq, cReqs, rc);
|
---|
211 | return rc;
|
---|
212 | }
|
---|
213 | }
|
---|
214 | //if (!(i % 10000))
|
---|
215 | // RTPrintf(TESTCASE ": i=%d\n", i);
|
---|
216 | }
|
---|
217 |
|
---|
218 | return VINF_SUCCESS;
|
---|
219 | }
|
---|
220 |
|
---|
221 | static DECLCALLBACK(int)
|
---|
222 | tstVMREQConfigConstructor(PUVM pUVM, PVM pVM, PCVMMR3VTABLE pVMM, void *pvUser)
|
---|
223 | {
|
---|
224 | RT_NOREF(pUVM, pVMM, pvUser);
|
---|
225 | return CFGMR3ConstructDefaultTree(pVM);
|
---|
226 | }
|
---|
227 |
|
---|
228 | /**
|
---|
229 | * Entry point.
|
---|
230 | */
|
---|
231 | extern "C" DECLEXPORT(int) TrustedMain(int argc, char **argv, char **envp)
|
---|
232 | {
|
---|
233 | RT_NOREF1(envp);
|
---|
234 | RTR3InitExe(argc, &argv, RTR3INIT_FLAGS_TRY_SUPLIB);
|
---|
235 | RTPrintf(TESTCASE ": TESTING...\n");
|
---|
236 | RTStrmFlush(g_pStdOut);
|
---|
237 |
|
---|
238 | /*
|
---|
239 | * Create empty VM.
|
---|
240 | */
|
---|
241 | PUVM pUVM;
|
---|
242 | int rc = VMR3Create(1, NULL, NULL, NULL, tstVMREQConfigConstructor, NULL, NULL, &pUVM);
|
---|
243 | if (RT_SUCCESS(rc))
|
---|
244 | {
|
---|
245 | /*
|
---|
246 | * Do testing.
|
---|
247 | */
|
---|
248 | uint64_t u64StartTS = RTTimeNanoTS();
|
---|
249 | RTTHREAD Thread0;
|
---|
250 | rc = RTThreadCreate(&Thread0, Thread, pUVM, 0, RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "REQ1");
|
---|
251 | if (RT_SUCCESS(rc))
|
---|
252 | {
|
---|
253 | RTTHREAD Thread1;
|
---|
254 | rc = RTThreadCreate(&Thread1, Thread, pUVM, 0, RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "REQ1");
|
---|
255 | if (RT_SUCCESS(rc))
|
---|
256 | {
|
---|
257 | int rcThread1;
|
---|
258 | rc = RTThreadWait(Thread1, RT_INDEFINITE_WAIT, &rcThread1);
|
---|
259 | if (RT_FAILURE(rc))
|
---|
260 | {
|
---|
261 | RTPrintf(TESTCASE ": RTThreadWait(Thread1,,) failed, rc=%Rrc\n", rc);
|
---|
262 | g_cErrors++;
|
---|
263 | }
|
---|
264 | if (RT_FAILURE(rcThread1))
|
---|
265 | g_cErrors++;
|
---|
266 | }
|
---|
267 | else
|
---|
268 | {
|
---|
269 | RTPrintf(TESTCASE ": RTThreadCreate(&Thread1,,,,) failed, rc=%Rrc\n", rc);
|
---|
270 | g_cErrors++;
|
---|
271 | }
|
---|
272 |
|
---|
273 | int rcThread0;
|
---|
274 | rc = RTThreadWait(Thread0, RT_INDEFINITE_WAIT, &rcThread0);
|
---|
275 | if (RT_FAILURE(rc))
|
---|
276 | {
|
---|
277 | RTPrintf(TESTCASE ": RTThreadWait(Thread1,,) failed, rc=%Rrc\n", rc);
|
---|
278 | g_cErrors++;
|
---|
279 | }
|
---|
280 | if (RT_FAILURE(rcThread0))
|
---|
281 | g_cErrors++;
|
---|
282 | }
|
---|
283 | else
|
---|
284 | {
|
---|
285 | RTPrintf(TESTCASE ": RTThreadCreate(&Thread0,,,,) failed, rc=%Rrc\n", rc);
|
---|
286 | g_cErrors++;
|
---|
287 | }
|
---|
288 | uint64_t u64ElapsedTS = RTTimeNanoTS() - u64StartTS;
|
---|
289 | RTPrintf(TESTCASE ": %llu ns elapsed\n", u64ElapsedTS);
|
---|
290 | RTStrmFlush(g_pStdOut);
|
---|
291 |
|
---|
292 | /*
|
---|
293 | * Print stats.
|
---|
294 | */
|
---|
295 | STAMR3Print(pUVM, "/VM/Req/*");
|
---|
296 |
|
---|
297 | /*
|
---|
298 | * Testing va_list fun.
|
---|
299 | */
|
---|
300 | RTPrintf(TESTCASE ": va_list argument test...\n"); RTStrmFlush(g_pStdOut);
|
---|
301 | PassVA(pUVM, "hello %s", "world");
|
---|
302 | VMR3AtRuntimeErrorRegister(pUVM, MyAtRuntimeError, (void *)"user argument");
|
---|
303 | VMSetRuntimeError(VMR3GetVM(pUVM), 0 /*fFlags*/, "enum", "some %s string", "error");
|
---|
304 |
|
---|
305 | /*
|
---|
306 | * Cleanup.
|
---|
307 | */
|
---|
308 | rc = VMR3PowerOff(pUVM);
|
---|
309 | if (!RT_SUCCESS(rc))
|
---|
310 | {
|
---|
311 | RTPrintf(TESTCASE ": error: failed to power off vm! rc=%Rrc\n", rc);
|
---|
312 | g_cErrors++;
|
---|
313 | }
|
---|
314 | rc = VMR3Destroy(pUVM);
|
---|
315 | if (!RT_SUCCESS(rc))
|
---|
316 | {
|
---|
317 | RTPrintf(TESTCASE ": error: failed to destroy vm! rc=%Rrc\n", rc);
|
---|
318 | g_cErrors++;
|
---|
319 | }
|
---|
320 | VMR3ReleaseUVM(pUVM);
|
---|
321 | }
|
---|
322 | else if (rc == VERR_SVM_NO_SVM || rc == VERR_VMX_NO_VMX)
|
---|
323 | {
|
---|
324 | RTPrintf(TESTCASE ": Skipped: %Rrc\n", rc);
|
---|
325 | return RTEXITCODE_SKIPPED;
|
---|
326 | }
|
---|
327 | else
|
---|
328 | {
|
---|
329 | RTPrintf(TESTCASE ": fatal error: failed to create vm! rc=%Rrc\n", rc);
|
---|
330 | g_cErrors++;
|
---|
331 | }
|
---|
332 |
|
---|
333 | /*
|
---|
334 | * Summary and return.
|
---|
335 | */
|
---|
336 | if (!g_cErrors)
|
---|
337 | RTPrintf(TESTCASE ": SUCCESS\n");
|
---|
338 | else
|
---|
339 | RTPrintf(TESTCASE ": FAILURE - %d errors\n", g_cErrors);
|
---|
340 |
|
---|
341 | return !!g_cErrors;
|
---|
342 | }
|
---|
343 |
|
---|
344 |
|
---|
345 | #if !defined(VBOX_WITH_HARDENING) || !defined(RT_OS_WINDOWS)
|
---|
346 | /**
|
---|
347 | * Main entry point.
|
---|
348 | */
|
---|
349 | int main(int argc, char **argv, char **envp)
|
---|
350 | {
|
---|
351 | return TrustedMain(argc, argv, envp);
|
---|
352 | }
|
---|
353 | #endif
|
---|
354 |
|
---|