VirtualBox

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

Last change on this file since 73416 was 73097, checked in by vboxsync, 7 years ago

*: Made RT_UOFFSETOF, RT_OFFSETOF, RT_UOFFSETOF_ADD and RT_OFFSETOF_ADD work like builtin_offsetof() and require compile time resolvable requests, adding RT_UOFFSETOF_DYN for the dynamic questions that can only be answered at runtime.

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