VirtualBox

source: vbox/trunk/src/VBox/VMM/testcase/tstMMHyperHeap.cpp@ 80387

Last change on this file since 80387 was 80334, checked in by vboxsync, 5 years ago

VMM: Eliminating the VBOX_BUGREF_9217 preprocessor macro. bugref:9217

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 8.8 KB
Line 
1/* $Id: tstMMHyperHeap.cpp 80334 2019-08-17 00:43:24Z vboxsync $ */
2/** @file
3 * MM Hypervisor Heap testcase.
4 */
5
6/*
7 * Copyright (C) 2006-2019 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
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#include <VBox/vmm/mm.h>
23#include <VBox/vmm/stam.h>
24#include <VBox/vmm/vm.h>
25#include <VBox/vmm/uvm.h>
26#include <VBox/vmm/gvm.h>
27#include <VBox/sup.h>
28#include <VBox/param.h>
29#include <iprt/errcore.h>
30
31#include <VBox/log.h>
32#include <iprt/initterm.h>
33#include <iprt/mem.h>
34#include <iprt/assert.h>
35#include <iprt/stream.h>
36#include <iprt/string.h>
37
38#define NUM_CPUS 16
39
40
41/**
42 * Entry point.
43 */
44extern "C" DECLEXPORT(int) TrustedMain(int argc, char **argv, char **envp)
45{
46 RT_NOREF1(envp);
47
48 /*
49 * Init runtime.
50 */
51 int rc = RTR3InitExe(argc, &argv, 0);
52 AssertRCReturn(rc, RTEXITCODE_INIT);
53
54 /*
55 * Create empty VM structure and call MMR3Init().
56 */
57 void *pvVM = NULL;
58 RTR0PTR pvR0 = NIL_RTR0PTR;
59 SUPPAGE aPages[(sizeof(GVM) + NUM_CPUS * sizeof(GVMCPU)) >> PAGE_SHIFT];
60 rc = SUPR3Init(NULL);
61 if (RT_FAILURE(rc))
62 {
63 RTPrintf("Fatal error: SUP failure! rc=%Rrc\n", rc);
64 return RTEXITCODE_FAILURE;
65 }
66 rc = SUPR3PageAllocEx(RT_ELEMENTS(aPages), 0, &pvVM, &pvR0, &aPages[0]);
67 if (RT_FAILURE(rc))
68 {
69 RTPrintf("Fatal error: Allocation failure! rc=%Rrc\n", rc);
70 return RTEXITCODE_FAILURE;
71 }
72 RT_BZERO(pvVM, RT_ELEMENTS(aPages) * PAGE_SIZE); /* SUPR3PageAllocEx doesn't necessarily zero the memory. */
73 PVM pVM = (PVM)pvVM;
74 pVM->paVMPagesR3 = aPages;
75 pVM->pVMR0ForCall = pvR0;
76
77 PUVM pUVM = (PUVM)RTMemPageAllocZ(RT_ALIGN_Z(sizeof(*pUVM), PAGE_SIZE));
78 if (!pUVM)
79 {
80 RTPrintf("Fatal error: RTMEmPageAllocZ failed\n");
81 return RTEXITCODE_FAILURE;
82 }
83 pUVM->u32Magic = UVM_MAGIC;
84 pUVM->pVM = pVM;
85 pVM->pUVM = pUVM;
86
87 pVM->cCpus = NUM_CPUS;
88 pVM->cbSelf = sizeof(VM);
89 pVM->cbVCpu = sizeof(VMCPU);
90 PVMCPU pVCpu = (PVMCPU)((uintptr_t)pVM + sizeof(GVM));
91 for (VMCPUID idCpu = 0; idCpu < NUM_CPUS; idCpu++)
92 {
93 pVM->apCpusR3[idCpu] = pVCpu;
94 pVCpu = (PVMCPU)((uintptr_t)pVCpu + sizeof(GVMCPU));
95 }
96
97 rc = STAMR3InitUVM(pUVM);
98 if (RT_FAILURE(rc))
99 {
100 RTPrintf("FAILURE: STAMR3Init failed. rc=%Rrc\n", rc);
101 return 1;
102 }
103
104 rc = MMR3InitUVM(pUVM);
105 if (RT_FAILURE(rc))
106 {
107 RTPrintf("FAILURE: STAMR3Init failed. rc=%Rrc\n", rc);
108 return 1;
109 }
110
111 rc = CFGMR3Init(pVM, NULL, NULL);
112 if (RT_FAILURE(rc))
113 {
114 RTPrintf("FAILURE: CFGMR3Init failed. rc=%Rrc\n", rc);
115 return 1;
116 }
117
118 rc = MMR3Init(pVM);
119 if (RT_FAILURE(rc))
120 {
121 RTPrintf("Fatal error: MMR3Init failed! rc=%Rrc\n", rc);
122 return 1;
123 }
124
125 /*
126 * Try allocate.
127 */
128 static struct
129 {
130 size_t cb;
131 unsigned uAlignment;
132 void *pvAlloc;
133 unsigned iFreeOrder;
134 } aOps[] =
135 {
136 { 16, 0, NULL, 0 },
137 { 16, 4, NULL, 1 },
138 { 16, 8, NULL, 2 },
139 { 16, 16, NULL, 5 },
140 { 16, 32, NULL, 4 },
141 { 32, 0, NULL, 3 },
142 { 31, 0, NULL, 6 },
143 { 1024, 0, NULL, 8 },
144 { 1024, 32, NULL, 10 },
145 { 1024, 32, NULL, 12 },
146 { PAGE_SIZE, PAGE_SIZE, NULL, 13 },
147 { 1024, 32, NULL, 9 },
148 { PAGE_SIZE, 32, NULL, 11 },
149 { PAGE_SIZE, PAGE_SIZE, NULL, 14 },
150 { 16, 0, NULL, 15 },
151 { 9, 0, NULL, 7 },
152 { 16, 0, NULL, 7 },
153 { 36, 0, NULL, 7 },
154 { 16, 0, NULL, 7 },
155 { 12344, 0, NULL, 7 },
156 { 50, 0, NULL, 7 },
157 { 16, 0, NULL, 7 },
158 };
159 unsigned i;
160#ifdef DEBUG
161 MMHyperHeapDump(pVM);
162#endif
163 size_t cbBefore = MMHyperHeapGetFreeSize(pVM);
164 static char szFill[] = "01234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
165
166 /* allocate */
167 for (i = 0; i < RT_ELEMENTS(aOps); i++)
168 {
169 rc = MMHyperAlloc(pVM, aOps[i].cb, aOps[i].uAlignment, MM_TAG_VM, &aOps[i].pvAlloc);
170 if (RT_FAILURE(rc))
171 {
172 RTPrintf("Failure: MMHyperAlloc(, %#x, %#x,) -> %d i=%d\n", aOps[i].cb, aOps[i].uAlignment, rc, i);
173 return 1;
174 }
175 memset(aOps[i].pvAlloc, szFill[i], aOps[i].cb);
176 if (RT_ALIGN_P(aOps[i].pvAlloc, (aOps[i].uAlignment ? aOps[i].uAlignment : 8)) != aOps[i].pvAlloc)
177 {
178 RTPrintf("Failure: MMHyperAlloc(, %#x, %#x,) -> %p, invalid alignment!\n", aOps[i].cb, aOps[i].uAlignment, aOps[i].pvAlloc);
179 return 1;
180 }
181 }
182
183 /* free and allocate the same node again. */
184 for (i = 0; i < RT_ELEMENTS(aOps); i++)
185 {
186 if ( !aOps[i].pvAlloc
187 || aOps[i].uAlignment == PAGE_SIZE)
188 continue;
189 //size_t cbBeforeSub = MMHyperHeapGetFreeSize(pVM);
190 rc = MMHyperFree(pVM, aOps[i].pvAlloc);
191 if (RT_FAILURE(rc))
192 {
193 RTPrintf("Failure: MMHyperFree(, %p,) -> %d i=%d\n", aOps[i].pvAlloc, rc, i);
194 return 1;
195 }
196 //RTPrintf("debug: i=%d cbBeforeSub=%d now=%d\n", i, cbBeforeSub, MMHyperHeapGetFreeSize(pVM));
197 void *pv;
198 rc = MMHyperAlloc(pVM, aOps[i].cb, aOps[i].uAlignment, MM_TAG_VM_REQ, &pv);
199 if (RT_FAILURE(rc))
200 {
201 RTPrintf("Failure: MMHyperAlloc(, %#x, %#x,) -> %d i=%d\n", aOps[i].cb, aOps[i].uAlignment, rc, i);
202 return 1;
203 }
204 if (pv != aOps[i].pvAlloc)
205 {
206 RTPrintf("Failure: Free+Alloc returned different address. new=%p old=%p i=%d (doesn't work with delayed free)\n", pv, aOps[i].pvAlloc, i);
207 //return 1;
208 }
209 aOps[i].pvAlloc = pv;
210 #if 0 /* won't work :/ */
211 size_t cbAfterSub = MMHyperHeapGetFreeSize(pVM);
212 if (cbBeforeSub != cbAfterSub)
213 {
214 RTPrintf("Failure: cbBeforeSub=%d cbAfterSub=%d. i=%d\n", cbBeforeSub, cbAfterSub, i);
215 return 1;
216 }
217 #endif
218 }
219
220 /* free it in a specific order. */
221 int cFreed = 0;
222 for (i = 0; i < RT_ELEMENTS(aOps); i++)
223 {
224 unsigned j;
225 for (j = 0; j < RT_ELEMENTS(aOps); j++)
226 {
227 if ( aOps[j].iFreeOrder != i
228 || !aOps[j].pvAlloc)
229 continue;
230 RTPrintf("j=%d i=%d free=%d cb=%d pv=%p\n", j, i, MMHyperHeapGetFreeSize(pVM), aOps[j].cb, aOps[j].pvAlloc);
231 if (aOps[j].uAlignment == PAGE_SIZE)
232 cbBefore -= aOps[j].cb;
233 else
234 {
235 rc = MMHyperFree(pVM, aOps[j].pvAlloc);
236 if (RT_FAILURE(rc))
237 {
238 RTPrintf("Failure: MMHyperFree(, %p,) -> %d j=%d i=%d\n", aOps[j].pvAlloc, rc, i, j);
239 return 1;
240 }
241 }
242 aOps[j].pvAlloc = NULL;
243 cFreed++;
244 }
245 }
246 Assert(cFreed == RT_ELEMENTS(aOps));
247 RTPrintf("i=done free=%d\n", MMHyperHeapGetFreeSize(pVM));
248
249 /* check that we're back at the right amount of free memory. */
250 size_t cbAfter = MMHyperHeapGetFreeSize(pVM);
251 if (cbBefore != cbAfter)
252 {
253 RTPrintf("Warning: Either we've split out an alignment chunk at the start, or we've got\n"
254 " an alloc/free accounting bug: cbBefore=%d cbAfter=%d\n", cbBefore, cbAfter);
255#ifdef DEBUG
256 MMHyperHeapDump(pVM);
257#endif
258 }
259
260 RTPrintf("tstMMHyperHeap: Success\n");
261#ifdef LOG_ENABLED
262 RTLogFlush(NULL);
263#endif
264 SUPR3PageFreeEx(pVM, RT_ELEMENTS(aPages));
265 RTMemPageFree(pUVM, RT_ALIGN_Z(sizeof(*pUVM), PAGE_SIZE));
266 return 0;
267}
268
269
270#if !defined(VBOX_WITH_HARDENING) || !defined(RT_OS_WINDOWS)
271/**
272 * Main entry point.
273 */
274int main(int argc, char **argv, char **envp)
275{
276 return TrustedMain(argc, argv, envp);
277}
278#endif
279
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