VirtualBox

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

Last change on this file since 77807 was 76553, checked in by vboxsync, 6 years ago

scm --update-copyright-year

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