VirtualBox

source: vbox/trunk/src/VBox/Additions/common/VBoxGuest/lib/testcase/tstVbglR0PhysHeap-1.cpp

Last change on this file was 100360, checked in by vboxsync, 11 months ago

Additions/VBoxGuest/VBoxGuestR0LibPhysHeap: Make use of the RTR0MemObjContAlloc() API and support allocating memory above 4GiB, bugref:10457

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 16.4 KB
Line 
1/* $Id: tstVbglR0PhysHeap-1.cpp 100360 2023-07-04 07:09:24Z vboxsync $ */
2/** @file
3 * IPRT Testcase - Offset Based Heap.
4 */
5
6/*
7 * Copyright (C) 2006-2023 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 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#include <iprt/assert.h>
42#include <iprt/errcore.h>
43#include <iprt/initterm.h>
44#include <iprt/log.h>
45#include <iprt/mem.h>
46#include <iprt/rand.h>
47#include <iprt/stream.h>
48#include <iprt/string.h>
49#include <iprt/param.h>
50#include <iprt/test.h>
51#include <iprt/time.h>
52
53#define IN_TESTCASE
54#define IN_RING0 /* pretend we're in ring-0 so we get access to the functions */
55#include <iprt/memobj.h>
56#include "../VBoxGuestR0LibInternal.h"
57
58
59/*********************************************************************************************************************************
60* Structures and Typedefs *
61*********************************************************************************************************************************/
62typedef struct
63{
64 uint32_t cb;
65 void *pv;
66} TSTHISTORYENTRY;
67
68
69typedef struct TSTMEMOBJ
70{
71 size_t cb;
72} TSTMEMOBJ;
73typedef TSTMEMOBJ *PTSTMEMOBJ;
74
75
76/*********************************************************************************************************************************
77* Global Variables *
78*********************************************************************************************************************************/
79VBGLDATA g_vbgldata;
80
81int g_cChunks = 0;
82size_t g_cbChunks = 0;
83
84/** Drop-in replacement for RTMemContAlloc */
85static int tstMemObjContAllocTag(PRTR0MEMOBJ pMemObj, size_t cb, RTHCPHYS PhysHighest, bool fExecutable, const char *pszTag)
86{
87 RT_NOREF(pszTag, PhysHighest, fExecutable);
88
89 RTTESTI_CHECK(cb > 0);
90
91#define TST_MAX_CHUNKS 24
92 if (g_cChunks < TST_MAX_CHUNKS)
93 {
94 PTSTMEMOBJ pMem = (PTSTMEMOBJ)RTMemAlloc(sizeof(TSTMEMOBJ) + cb);
95 if (pMem)
96 {
97 pMem->cb = cb;
98
99 g_cChunks++;
100 g_cbChunks += cb;
101 *pMemObj = (RTR0MEMOBJ)pMem;
102 return VINF_SUCCESS;
103 }
104 }
105
106 return VERR_NO_MEMORY;
107}
108
109
110/** Drop-in replacement for RTR0MemObjAddress */
111static void *tstMemObjAddress(RTR0MEMOBJ hMemObj)
112{
113 return (void *)((PTSTMEMOBJ)hMemObj + 1);
114}
115
116
117/** Drop-in replacement for RTR0MemObjGetPagePhysAddr */
118static RTHCPHYS tstMemObjGetPagePhysAddr(RTR0MEMOBJ hMemObj, uint32_t iPage)
119{
120 RTTESTI_CHECK(iPage == 0);
121
122 PTSTMEMOBJ pMemObj = (PTSTMEMOBJ)hMemObj;
123 uintptr_t PtrMem = (uintptr_t)(pMemObj + 1);
124 RTHCPHYS Phys = (uint32_t)(uintptr_t)PtrMem ^ (UINT32_C(0xf0f0f0f0) & ~(uint32_t)PAGE_OFFSET_MASK);
125
126 /* Avoid problematic values that won't happen in real life: */
127 if (!Phys)
128 Phys = 4U << PAGE_SHIFT;
129 if (UINT32_MAX - Phys < pMemObj->cb)
130 Phys -= RT_ALIGN_32(pMemObj->cb, PAGE_SIZE);
131
132 return Phys;
133}
134
135
136/** Drop-in replacement for RTR0MemObjFree */
137static void tstMemObjFree(RTR0MEMOBJ hMemObj, bool fFreeMappings)
138{
139 RT_NOREF(fFreeMappings);
140
141 PTSTMEMOBJ pMemObj = (PTSTMEMOBJ)hMemObj;
142 RTTESTI_CHECK(RT_VALID_PTR(pMemObj));
143 RTTESTI_CHECK(pMemObj->cb > 0);
144 RTTESTI_CHECK(g_cChunks > 0);
145 g_cChunks--;
146 g_cbChunks -= pMemObj->cb;
147 RTMemFree(pMemObj);
148}
149
150
151#define RTR0MemObjAllocContTag tstMemObjContAllocTag
152#define RTR0MemObjAddress tstMemObjAddress
153#define RTR0MemObjGetPagePhysAddr tstMemObjGetPagePhysAddr
154#define RTR0MemObjFree tstMemObjFree
155#include "../VBoxGuestR0LibPhysHeap.cpp"
156
157
158static void PrintStats(TSTHISTORYENTRY const *paHistory, size_t cHistory, const char *pszDesc)
159{
160 size_t cbAllocated = 0;
161 unsigned cLargeBlocks = 0;
162 unsigned cAllocated = 0;
163 for (size_t i = 0; i < cHistory; i++)
164 if (paHistory[i].pv)
165 {
166 cAllocated += 1;
167 cbAllocated += paHistory[i].cb;
168 cLargeBlocks += paHistory[i].cb > _1K;
169 }
170
171 size_t const cbOverhead = g_cChunks * sizeof(VBGLPHYSHEAPCHUNK) + cAllocated * sizeof(VBGLPHYSHEAPBLOCK);
172 size_t const cbFragmentation = g_cbChunks - cbOverhead - cbAllocated;
173 RTTestIPrintf(RTTESTLVL_ALWAYS,
174 "%s: %'9zu bytes in %2d chunks; %'9zu bytes in %4u blocks (%2u large)\n"
175 " => int-frag %'9zu (%2zu.%1zu%%) overhead %'9zu (%1zu.%02zu%%)\n",
176 pszDesc,
177 g_cbChunks, g_cChunks,
178 cbAllocated, cAllocated, cLargeBlocks,
179 cbFragmentation, cbFragmentation * 100 / g_cbChunks, (cbFragmentation * 1000 / g_cbChunks) % 10,
180 cbOverhead, cbOverhead * 100 / g_cbChunks, (cbOverhead * 10000 / g_cbChunks) % 100);
181}
182
183
184int main(int argc, char **argv)
185{
186 RT_NOREF_PV(argc); RT_NOREF_PV(argv);
187
188 /*
189 * Init runtime.
190 */
191 RTTEST hTest;
192 int rc = RTTestInitAndCreate("tstVbglR0PhysHeap-1", &hTest);
193 if (rc)
194 return rc;
195 RTTestBanner(hTest);
196
197 /*
198 * Arguments are taken to be random seeding.
199 */
200 uint64_t uRandSeed = RTTimeNanoTS();
201 for (int i = 1; i < argc; i++)
202 {
203 rc = RTStrToUInt64Full(argv[i], 0, &uRandSeed);
204 if (rc != VINF_SUCCESS)
205 {
206 RTTestIFailed("Invalid parameter: %Rrc: %s\n", rc, argv[i]);
207 return RTTestSummaryAndDestroy(hTest);
208 }
209 }
210
211 /*
212 * Create a heap.
213 */
214 RTTestSub(hTest, "Basics");
215 RTTESTI_CHECK_RC(rc = VbglR0PhysHeapInit(NIL_RTHCPHYS), VINF_SUCCESS);
216 if (RT_FAILURE(rc))
217 return RTTestSummaryAndDestroy(hTest);
218 RTTESTI_CHECK_RC_OK(VbglR0PhysHeapCheck(NULL));
219
220#define CHECK_PHYS_ADDR(a_pv) do { \
221 uint32_t const uPhys = VbglR0PhysHeapGetPhysAddr(a_pv); \
222 if (uPhys == 0 || uPhys == UINT32_MAX || (uPhys & PAGE_OFFSET_MASK) != ((uintptr_t)(a_pv) & PAGE_OFFSET_MASK)) \
223 RTTestIFailed("line %u: %s=%p: uPhys=%#x\n", __LINE__, #a_pv, (a_pv), uPhys); \
224 } while (0)
225
226 /*
227 * Try allocate.
228 */
229 static struct TstPhysHeapOps
230 {
231 uint32_t cb;
232 unsigned iFreeOrder;
233 void *pvAlloc;
234 } s_aOps[] =
235 {
236 { 16, 0, NULL }, // 0
237 { 16, 1, NULL },
238 { 16, 2, NULL },
239 { 16, 5, NULL },
240 { 16, 4, NULL },
241 { 32, 3, NULL }, // 5
242 { 31, 6, NULL },
243 { 1024, 8, NULL },
244 { 1024, 10, NULL },
245 { 1024, 12, NULL },
246 { PAGE_SIZE, 13, NULL }, // 10
247 { 1024, 9, NULL },
248 { PAGE_SIZE, 11, NULL },
249 { PAGE_SIZE, 14, NULL },
250 { 16, 15, NULL },
251 { 9, 7, NULL }, // 15
252 { 16, 7, NULL },
253 { 36, 7, NULL },
254 { 16, 7, NULL },
255 { 12344, 7, NULL },
256 { 50, 7, NULL }, // 20
257 { 16, 7, NULL },
258 };
259 uint32_t i;
260 //RTHeapOffsetDump(Heap, (PFNRTHEAPOFFSETPRINTF)(uintptr_t)RTPrintf); /** @todo Add some detail info output with a signature identical to RTPrintf. */
261 //size_t cbBefore = VbglR0PhysHeapGetFreeSize();
262 static char const s_szFill[] = "01234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
263
264 /* allocate */
265 for (i = 0; i < RT_ELEMENTS(s_aOps); i++)
266 {
267 s_aOps[i].pvAlloc = VbglR0PhysHeapAlloc(s_aOps[i].cb);
268 RTTESTI_CHECK_MSG(s_aOps[i].pvAlloc, ("VbglR0PhysHeapAlloc(%#x) -> NULL i=%d\n", s_aOps[i].cb, i));
269 if (!s_aOps[i].pvAlloc)
270 return RTTestSummaryAndDestroy(hTest);
271
272 memset(s_aOps[i].pvAlloc, s_szFill[i], s_aOps[i].cb);
273 RTTESTI_CHECK_MSG(RT_ALIGN_P(s_aOps[i].pvAlloc, sizeof(void *)) == s_aOps[i].pvAlloc,
274 ("VbglR0PhysHeapAlloc(%#x) -> %p\n", s_aOps[i].cb, i));
275
276 CHECK_PHYS_ADDR(s_aOps[i].pvAlloc);
277
278 /* Check heap integrity: */
279 RTTESTI_CHECK_RC_OK(VbglR0PhysHeapCheck(NULL));
280 }
281
282 /* free and allocate the same node again. */
283 for (i = 0; i < RT_ELEMENTS(s_aOps); i++)
284 {
285 if (!s_aOps[i].pvAlloc)
286 continue;
287 //RTPrintf("debug: i=%d pv=%#x cb=%#zx align=%#zx cbReal=%#zx\n", i, s_aOps[i].pvAlloc,
288 // s_aOps[i].cb, s_aOps[i].uAlignment, RTHeapOffsetSize(Heap, s_aOps[i].pvAlloc));
289 size_t cbBeforeSub = VbglR0PhysHeapGetFreeSize();
290 VbglR0PhysHeapFree(s_aOps[i].pvAlloc);
291 size_t cbAfterSubFree = VbglR0PhysHeapGetFreeSize();
292 RTTESTI_CHECK_RC_OK(VbglR0PhysHeapCheck(NULL));
293
294 void *pv;
295 pv = VbglR0PhysHeapAlloc(s_aOps[i].cb);
296 RTTESTI_CHECK_MSG(pv, ("VbglR0PhysHeapAlloc(%#x) -> NULL i=%d\n", s_aOps[i].cb, i));
297 if (!pv)
298 return RTTestSummaryAndDestroy(hTest);
299 CHECK_PHYS_ADDR(pv);
300 RTTESTI_CHECK_RC_OK(VbglR0PhysHeapCheck(NULL));
301
302 //RTPrintf("debug: i=%d pv=%p cbReal=%#zx cbBeforeSub=%#zx cbAfterSubFree=%#zx cbAfterSubAlloc=%#zx \n", i, pv, RTHeapOffsetSize(Heap, pv),
303 // cbBeforeSub, cbAfterSubFree, VbglR0PhysHeapGetFreeSize());
304
305 if (pv != s_aOps[i].pvAlloc)
306 RTTestIPrintf(RTTESTLVL_ALWAYS, "Warning: Free+Alloc returned different address. new=%p old=%p i=%d\n", pv, s_aOps[i].pvAlloc, i);
307 s_aOps[i].pvAlloc = pv;
308 size_t cbAfterSubAlloc = VbglR0PhysHeapGetFreeSize();
309 if (cbBeforeSub != cbAfterSubAlloc)
310 {
311 RTTestIPrintf(RTTESTLVL_ALWAYS, "Warning: cbBeforeSub=%#zx cbAfterSubFree=%#zx cbAfterSubAlloc=%#zx. i=%d\n",
312 cbBeforeSub, cbAfterSubFree, cbAfterSubAlloc, i);
313 //return 1; - won't work correctly until we start creating free block instead of donating memory on alignment.
314 }
315 }
316
317 VbglR0PhysHeapTerminate();
318 RTTESTI_CHECK_MSG(g_cChunks == 0, ("g_cChunks=%d\n", g_cChunks));
319
320
321 /*
322 * Use random allocation pattern
323 */
324 RTTestSub(hTest, "Random Test");
325 RTTESTI_CHECK_RC(rc = VbglR0PhysHeapInit(NIL_RTHCPHYS), VINF_SUCCESS);
326 if (RT_FAILURE(rc))
327 return RTTestSummaryAndDestroy(hTest);
328
329 RTRAND hRand;
330 RTTESTI_CHECK_RC(rc = RTRandAdvCreateParkMiller(&hRand), VINF_SUCCESS);
331 if (RT_FAILURE(rc))
332 return RTTestSummaryAndDestroy(hTest);
333 RTRandAdvSeed(hRand, uRandSeed);
334 RTTestValue(hTest, "RandSeed", uRandSeed, RTTESTUNIT_NONE);
335
336 static TSTHISTORYENTRY s_aHistory[3072];
337 RT_ZERO(s_aHistory);
338
339 for (unsigned iTest = 0; iTest < 131072; iTest++)
340 {
341 i = RTRandAdvU32Ex(hRand, 0, RT_ELEMENTS(s_aHistory) - 1);
342 if (!s_aHistory[i].pv)
343 {
344 s_aHistory[i].cb = RTRandAdvU32Ex(hRand, 8, 1024);
345 s_aHistory[i].pv = VbglR0PhysHeapAlloc(s_aHistory[i].cb);
346 if (!s_aHistory[i].pv)
347 {
348 s_aHistory[i].cb = 9;
349 s_aHistory[i].pv = VbglR0PhysHeapAlloc(s_aHistory[i].cb);
350 }
351 if (s_aHistory[i].pv)
352 {
353 memset(s_aHistory[i].pv, 0xbb, s_aHistory[i].cb);
354 CHECK_PHYS_ADDR(s_aHistory[i].pv);
355 }
356 }
357 else
358 {
359 VbglR0PhysHeapFree(s_aHistory[i].pv);
360 s_aHistory[i].pv = NULL;
361 }
362
363#if 1
364 /* Check heap integrity: */
365 RTTESTI_CHECK_RC_OK(VbglR0PhysHeapCheck(NULL));
366 int cChunks = 0;
367 for (VBGLPHYSHEAPCHUNK *pCurChunk = g_vbgldata.pChunkHead; pCurChunk; pCurChunk = pCurChunk->pNext)
368 cChunks++;
369 RTTESTI_CHECK_MSG(cChunks == g_cChunks, ("g_cChunks=%u, but only %u chunks in the list!\n", g_cChunks, cChunks));
370#endif
371
372 if ((iTest % 7777) == 7776)
373 {
374 /* exhaust the heap */
375 PrintStats(s_aHistory, RT_ELEMENTS(s_aHistory), "Exhaust-pre ");
376
377 for (i = 0; i < RT_ELEMENTS(s_aHistory) && (VbglR0PhysHeapGetFreeSize() >= 256 || g_cChunks < TST_MAX_CHUNKS); i++)
378 if (!s_aHistory[i].pv)
379 {
380 s_aHistory[i].cb = RTRandAdvU32Ex(hRand, VBGL_PH_CHUNKSIZE / 8, VBGL_PH_CHUNKSIZE / 2 + VBGL_PH_CHUNKSIZE / 4);
381 s_aHistory[i].pv = VbglR0PhysHeapAlloc(s_aHistory[i].cb);
382 if (s_aHistory[i].pv)
383 {
384 memset(s_aHistory[i].pv, 0x55, s_aHistory[i].cb);
385 CHECK_PHYS_ADDR(s_aHistory[i].pv);
386 }
387 }
388
389 size_t cbFree = VbglR0PhysHeapGetFreeSize();
390 if (cbFree)
391 for (i = 0; i < RT_ELEMENTS(s_aHistory); i++)
392 if (!s_aHistory[i].pv)
393 {
394 s_aHistory[i].cb = RTRandAdvU32Ex(hRand, 1, (uint32_t)cbFree);
395 s_aHistory[i].pv = VbglR0PhysHeapAlloc(s_aHistory[i].cb);
396 while (s_aHistory[i].pv == NULL && s_aHistory[i].cb > 2)
397 {
398 s_aHistory[i].cb >>= 1;
399 s_aHistory[i].pv = VbglR0PhysHeapAlloc(s_aHistory[i].cb);
400 }
401 if (s_aHistory[i].pv)
402 {
403 memset(s_aHistory[i].pv, 0x55, s_aHistory[i].cb);
404 CHECK_PHYS_ADDR(s_aHistory[i].pv);
405 }
406
407 cbFree = VbglR0PhysHeapGetFreeSize();
408 if (!cbFree)
409 break;
410 }
411
412 RTTESTI_CHECK_MSG(VbglR0PhysHeapGetFreeSize() == 0, ("%zu\n", VbglR0PhysHeapGetFreeSize()));
413 PrintStats(s_aHistory, RT_ELEMENTS(s_aHistory), "Exhaust-post");
414 }
415 else if ((iTest % 7777) == 1111)
416 {
417 /* free all */
418 RTTestIPrintf(RTTESTLVL_ALWAYS, "Free-all-pre: cFreeBlocks=%u cAllocedBlocks=%u in %u chunk(s)\n",
419 g_vbgldata.cFreeBlocks, g_vbgldata.cBlocks - g_vbgldata.cFreeBlocks, g_cChunks);
420 for (i = 0; i < RT_ELEMENTS(s_aHistory); i++)
421 {
422 VbglR0PhysHeapFree(s_aHistory[i].pv);
423 s_aHistory[i].pv = NULL;
424 }
425 RTTestIPrintf(RTTESTLVL_ALWAYS, "Free-all-post: cFreeBlocks=%u in %u chunk(s)\n", g_vbgldata.cFreeBlocks, g_cChunks);
426 RTTESTI_CHECK_MSG(g_cChunks == 1, ("g_cChunks=%d\n", g_cChunks));
427 RTTESTI_CHECK_MSG(g_vbgldata.cFreeBlocks == g_vbgldata.cBlocks,
428 ("g_vbgldata.cFreeBlocks=%d cBlocks=%d\n", g_vbgldata.cFreeBlocks, g_vbgldata.cBlocks));
429
430 //size_t cbAfterRand = VbglR0PhysHeapGetFreeSize();
431 //RTTESTI_CHECK_MSG(cbAfterRand == cbAfter, ("cbAfterRand=%zu cbAfter=%zu\n", cbAfterRand, cbAfter));
432 }
433 }
434
435 /* free the rest. */
436 for (i = 0; i < RT_ELEMENTS(s_aHistory); i++)
437 {
438 VbglR0PhysHeapFree(s_aHistory[i].pv);
439 s_aHistory[i].pv = NULL;
440 }
441
442 RTTESTI_CHECK_MSG(g_cChunks == 1, ("g_cChunks=%d\n", g_cChunks));
443
444 VbglR0PhysHeapTerminate();
445 RTTESTI_CHECK_MSG(g_cChunks == 0, ("g_cChunks=%d\n", g_cChunks));
446
447 RTTESTI_CHECK_RC(rc = RTRandAdvDestroy(hRand), VINF_SUCCESS);
448 return RTTestSummaryAndDestroy(hTest);
449}
450
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use