1 | /** @file
|
---|
2 | *
|
---|
3 | * MM Hypervisor Heap testcase.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006 InnoTek Systemberatung GmbH
|
---|
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 as published by the Free Software Foundation,
|
---|
13 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
14 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
15 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * If you received this file as part of a commercial VirtualBox
|
---|
18 | * distribution, then only the terms of your commercial VirtualBox
|
---|
19 | * license agreement apply instead of the previous paragraph.
|
---|
20 | */
|
---|
21 |
|
---|
22 | /*******************************************************************************
|
---|
23 | * Header Files *
|
---|
24 | *******************************************************************************/
|
---|
25 | #include <VBox/mm.h>
|
---|
26 | #include <VBox/stam.h>
|
---|
27 | #include <VBox/vm.h>
|
---|
28 | #include <VBox/sup.h>
|
---|
29 | #include <VBox/param.h>
|
---|
30 | #include <VBox/err.h>
|
---|
31 |
|
---|
32 | #include <VBox/log.h>
|
---|
33 | #include <iprt/runtime.h>
|
---|
34 | #include <iprt/assert.h>
|
---|
35 | #include <iprt/stream.h>
|
---|
36 |
|
---|
37 | #include <string.h>
|
---|
38 |
|
---|
39 |
|
---|
40 | int main(int argc, char *argv[])
|
---|
41 | {
|
---|
42 |
|
---|
43 | /*
|
---|
44 | * Init runtime.
|
---|
45 | */
|
---|
46 | RTR3Init();
|
---|
47 |
|
---|
48 | /*
|
---|
49 | * Create empty VM structure and call MMR3Init().
|
---|
50 | */
|
---|
51 | PVM pVM;
|
---|
52 | int rc = SUPInit(NULL);
|
---|
53 | if (VBOX_SUCCESS(rc))
|
---|
54 | rc = SUPPageAlloc((sizeof(*pVM) + PAGE_SIZE - 1) >> PAGE_SHIFT, (void **)&pVM);
|
---|
55 | if (VBOX_FAILURE(rc))
|
---|
56 | {
|
---|
57 | RTPrintf("Fatal error: SUP Failure! rc=%Vrc\n", rc);
|
---|
58 | return 1;
|
---|
59 | }
|
---|
60 | rc = STAMR3Init(pVM);
|
---|
61 | if (VBOX_FAILURE(rc))
|
---|
62 | {
|
---|
63 | RTPrintf("Fatal error: STAMR3Init failed! rc=%Vrc\n", rc);
|
---|
64 | return 1;
|
---|
65 | }
|
---|
66 | rc = MMR3Init(pVM);
|
---|
67 | if (VBOX_FAILURE(rc))
|
---|
68 | {
|
---|
69 | RTPrintf("Fatal error: MMR3Init failed! rc=%Vrc\n", rc);
|
---|
70 | return 1;
|
---|
71 | }
|
---|
72 |
|
---|
73 | /*
|
---|
74 | * Try allocate.
|
---|
75 | */
|
---|
76 | static struct
|
---|
77 | {
|
---|
78 | size_t cb;
|
---|
79 | unsigned uAlignment;
|
---|
80 | void *pvAlloc;
|
---|
81 | unsigned iFreeOrder;
|
---|
82 | } aOps[] =
|
---|
83 | {
|
---|
84 | { 16, 0, NULL, 0 },
|
---|
85 | { 16, 4, NULL, 1 },
|
---|
86 | { 16, 8, NULL, 2 },
|
---|
87 | { 16, 16, NULL, 5 },
|
---|
88 | { 16, 32, NULL, 4 },
|
---|
89 | { 32, 0, NULL, 3 },
|
---|
90 | { 31, 0, NULL, 6 },
|
---|
91 | { 1024, 0, NULL, 8 },
|
---|
92 | { 1024, 32, NULL, 10 },
|
---|
93 | { 1024, 32, NULL, 12 },
|
---|
94 | { PAGE_SIZE, PAGE_SIZE, NULL, 13 },
|
---|
95 | { 1024, 32, NULL, 9 },
|
---|
96 | { PAGE_SIZE, 32, NULL, 11 },
|
---|
97 | { PAGE_SIZE, PAGE_SIZE, NULL, 14 },
|
---|
98 | { 16, 0, NULL, 15 },
|
---|
99 | { 9, 0, NULL, 7 },
|
---|
100 | { 16, 0, NULL, 7 },
|
---|
101 | { 36, 0, NULL, 7 },
|
---|
102 | { 16, 0, NULL, 7 },
|
---|
103 | { 12344, 0, NULL, 7 },
|
---|
104 | { 50, 0, NULL, 7 },
|
---|
105 | { 16, 0, NULL, 7 },
|
---|
106 | };
|
---|
107 | unsigned i;
|
---|
108 | #ifdef DEBUG
|
---|
109 | MMHyperHeapDump(pVM);
|
---|
110 | #endif
|
---|
111 | size_t cbBefore = MMHyperHeapGetFreeSize(pVM);
|
---|
112 | static char szFill[] = "01234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
---|
113 |
|
---|
114 | /* allocate */
|
---|
115 | for (i = 0; i < ELEMENTS(aOps); i++)
|
---|
116 | {
|
---|
117 | rc = MMHyperAlloc(pVM, aOps[i].cb, aOps[i].uAlignment, MM_TAG_VM, &aOps[i].pvAlloc);
|
---|
118 | if (VBOX_FAILURE(rc))
|
---|
119 | {
|
---|
120 | RTPrintf("Failure: MMHyperAlloc(, %#x, %#x,) -> %d i=%d\n", aOps[i].cb, aOps[i].uAlignment, rc, i);
|
---|
121 | return 1;
|
---|
122 | }
|
---|
123 | memset(aOps[i].pvAlloc, szFill[i], aOps[i].cb);
|
---|
124 | if (ALIGNP(aOps[i].pvAlloc, (aOps[i].uAlignment ? aOps[i].uAlignment : 8)) != aOps[i].pvAlloc)
|
---|
125 | {
|
---|
126 | RTPrintf("Failure: MMHyperAlloc(, %#x, %#x,) -> %p, invalid alignment!\n", aOps[i].cb, aOps[i].uAlignment, aOps[i].pvAlloc);
|
---|
127 | return 1;
|
---|
128 | }
|
---|
129 | }
|
---|
130 |
|
---|
131 | /* free and allocate the same node again. */
|
---|
132 | for (i = 0; i < ELEMENTS(aOps); i++)
|
---|
133 | {
|
---|
134 | if ( !aOps[i].pvAlloc
|
---|
135 | || aOps[i].uAlignment == PAGE_SIZE)
|
---|
136 | continue;
|
---|
137 | //size_t cbBeforeSub = MMHyperHeapGetFreeSize(pVM);
|
---|
138 | rc = MMHyperFree(pVM, aOps[i].pvAlloc);
|
---|
139 | if (VBOX_FAILURE(rc))
|
---|
140 | {
|
---|
141 | RTPrintf("Failure: MMHyperFree(, %p,) -> %d i=%d\n", aOps[i].pvAlloc, rc, i);
|
---|
142 | return 1;
|
---|
143 | }
|
---|
144 | //RTPrintf("debug: i=%d cbBeforeSub=%d now=%d\n", i, cbBeforeSub, MMHyperHeapGetFreeSize(pVM));
|
---|
145 | void *pv;
|
---|
146 | rc = MMHyperAlloc(pVM, aOps[i].cb, aOps[i].uAlignment, MM_TAG_VM_REQ, &pv);
|
---|
147 | if (VBOX_FAILURE(rc))
|
---|
148 | {
|
---|
149 | RTPrintf("Failure: MMHyperAlloc(, %#x, %#x,) -> %d i=%d\n", aOps[i].cb, aOps[i].uAlignment, rc, i);
|
---|
150 | return 1;
|
---|
151 | }
|
---|
152 | if (pv != aOps[i].pvAlloc)
|
---|
153 | {
|
---|
154 | 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);
|
---|
155 | //return 1;
|
---|
156 | }
|
---|
157 | aOps[i].pvAlloc = pv;
|
---|
158 | #if 0 /* won't work :/ */
|
---|
159 | size_t cbAfterSub = MMHyperHeapGetFreeSize(pVM);
|
---|
160 | if (cbBeforeSub != cbAfterSub)
|
---|
161 | {
|
---|
162 | RTPrintf("Failure: cbBeforeSub=%d cbAfterSub=%d. i=%d\n", cbBeforeSub, cbAfterSub, i);
|
---|
163 | return 1;
|
---|
164 | }
|
---|
165 | #endif
|
---|
166 | }
|
---|
167 |
|
---|
168 | /* free it in a specific order. */
|
---|
169 | int cFreed = 0;
|
---|
170 | for (i = 0; i < ELEMENTS(aOps); i++)
|
---|
171 | {
|
---|
172 | unsigned j;
|
---|
173 | for (j = 0; j < ELEMENTS(aOps); j++)
|
---|
174 | {
|
---|
175 | if ( aOps[j].iFreeOrder != i
|
---|
176 | || !aOps[j].pvAlloc)
|
---|
177 | continue;
|
---|
178 | RTPrintf("j=%d i=%d free=%d cb=%d pv=%p\n", j, i, MMHyperHeapGetFreeSize(pVM), aOps[j].cb, aOps[j].pvAlloc);
|
---|
179 | if (aOps[j].uAlignment == PAGE_SIZE)
|
---|
180 | cbBefore -= aOps[j].cb;
|
---|
181 | else
|
---|
182 | {
|
---|
183 | rc = MMHyperFree(pVM, aOps[j].pvAlloc);
|
---|
184 | if (VBOX_FAILURE(rc))
|
---|
185 | {
|
---|
186 | RTPrintf("Failure: MMHyperFree(, %p,) -> %d j=%d i=%d\n", aOps[j].pvAlloc, rc, i, j);
|
---|
187 | return 1;
|
---|
188 | }
|
---|
189 | }
|
---|
190 | aOps[j].pvAlloc = NULL;
|
---|
191 | cFreed++;
|
---|
192 | }
|
---|
193 | }
|
---|
194 | Assert(cFreed == ELEMENTS(aOps));
|
---|
195 | RTPrintf("i=done free=%d\n", MMHyperHeapGetFreeSize(pVM));
|
---|
196 |
|
---|
197 | /* check that we're back at the right amount of free memory. */
|
---|
198 | size_t cbAfter = MMHyperHeapGetFreeSize(pVM);
|
---|
199 | if (cbBefore != cbAfter)
|
---|
200 | {
|
---|
201 | RTPrintf("Warning: Either we've split out an alignment chunk at the start, or we've got\n"
|
---|
202 | " an alloc/free accounting bug: cbBefore=%d cbAfter=%d\n", cbBefore, cbAfter);
|
---|
203 | #ifdef DEBUG
|
---|
204 | MMHyperHeapDump(pVM);
|
---|
205 | #endif
|
---|
206 | }
|
---|
207 |
|
---|
208 | RTPrintf("tstMMHyperHeap: Success\n");
|
---|
209 | #ifdef LOG_ENABLED
|
---|
210 | RTLogFlush(NULL);
|
---|
211 | #endif
|
---|
212 | return 0;
|
---|
213 | }
|
---|