VirtualBox

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

Last change on this file since 108668 was 108668, checked in by vboxsync, 5 weeks ago

Additions/common/VBoxGuest/lib/testcase/tstVbglR0PhysHeap-1.cpp: Make it work for hosts where we don't know the host page size when building, bugref:10391

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

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