VirtualBox

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

Last change on this file since 97923 was 97923, checked in by vboxsync, 2 years ago

Add/VBoxGuestR0LibPhysHeap.cpp: Made VbglR0PhysHeapFree and VbglR0PhysHeapGetPhysAddr more strict wrt input. Cleanups.

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