VirtualBox

source: vbox/trunk/src/VBox/Devices/testcase/tstDeviceStructSize.cpp@ 57529

Last change on this file since 57529 was 57410, checked in by vboxsync, 9 years ago

Device/Graphics: fix padding if VBOX_WITH_VMSVGA is unset, and make the sanity checks a little more paranoid to get better diagnostics in the future if some VMSVGA related alignment is touched

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 12.6 KB
Line 
1/* $Id: tstDeviceStructSize.cpp 57410 2015-08-18 09:58:29Z vboxsync $ */
2/** @file
3 * tstDeviceStructSize - testcase for check structure sizes/alignment
4 * and to verify that HC and RC uses the same
5 * representation of the structures.
6 */
7
8/*
9 * Copyright (C) 2006-2015 Oracle Corporation
10 *
11 * This file is part of VirtualBox Open Source Edition (OSE), as
12 * available from http://www.virtualbox.org. This file is free software;
13 * you can redistribute it and/or modify it under the terms of the GNU
14 * General Public License (GPL) as published by the Free Software
15 * Foundation, in version 2 as it comes in the "COPYING" file of the
16 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
17 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
18 */
19
20
21/*********************************************************************************************************************************
22* Header Files *
23*********************************************************************************************************************************/
24#include <VBox/types.h>
25#include <iprt/x86.h>
26
27
28#define VBOX_WITH_HGCM /* grumble */
29#define VBOX_DEVICE_STRUCT_TESTCASE
30#undef LOG_GROUP
31#include "../Bus/DevPCI.cpp"
32#undef LOG_GROUP
33#include "../Bus/DevPciIch9.cpp"
34#undef LOG_GROUP
35#include "../Graphics/DevVGA.cpp"
36#undef LOG_GROUP
37#include "../Input/DevPS2.cpp"
38#undef LOG_GROUP
39#include "../Input/PS2K.cpp"
40#undef LOG_GROUP
41#include "../Input/PS2M.cpp"
42#ifdef VBOX_WITH_E1000
43# undef LOG_GROUP
44# include "../Network/DevE1000.cpp"
45#endif
46#undef LOG_GROUP
47#include "../Network/DevPCNet.cpp"
48#ifdef VBOX_WITH_VIRTIO
49# undef LOG_GROUP
50# include "../Network/DevVirtioNet.cpp"
51#endif
52#undef LOG_GROUP
53#include "../PC/DevACPI.cpp"
54#undef LOG_GROUP
55#include "../PC/DevPIC.cpp"
56#undef LOG_GROUP
57#include "../PC/DevPit-i8254.cpp"
58#undef LOG_GROUP
59#include "../PC/DevRTC.cpp"
60#undef LOG_GROUP
61#include "../PC/DevAPIC.cpp"
62#undef LOG_GROUP
63#include "../PC/DevIoApic.cpp"
64#undef LOG_GROUP
65#include "../PC/DevHPET.cpp"
66#undef LOG_GROUP
67#include "../PC/DevLPC.cpp"
68#undef LOG_GROUP
69#include "../EFI/DevSmc.cpp"
70#undef LOG_GROUP
71#include "../Storage/DevATA.cpp"
72#ifdef VBOX_WITH_USB
73# undef LOG_GROUP
74# include "../USB/DevOHCI.cpp"
75# ifdef VBOX_WITH_EHCI_IMPL
76# undef LOG_GROUP
77# include "../USB/DevEHCI.cpp"
78# endif
79# ifdef VBOX_WITH_XHCI_IMPL
80# undef LOG_GROUP
81# include "../USB/DevXHCI.cpp"
82# endif
83#endif
84#undef LOG_GROUP
85#include "../VMMDev/VMMDev.cpp"
86#undef LOG_GROUP
87#include "../Parallel/DevParallel.cpp"
88#undef LOG_GROUP
89#include "../Serial/DevSerial.cpp"
90#ifdef VBOX_WITH_AHCI
91# undef LOG_GROUP
92# include "../Storage/DevAHCI.cpp"
93#endif
94#ifdef VBOX_WITH_BUSLOGIC
95# undef LOG_GROUP
96# include "../Storage/DevBusLogic.cpp"
97#endif
98#ifdef VBOX_WITH_LSILOGIC
99# undef LOG_GROUP
100# include "../Storage/DevLsiLogicSCSI.cpp"
101#endif
102
103#ifdef VBOX_WITH_PCI_PASSTHROUGH_IMPL
104# undef LOG_GROUP
105# include "../Bus/DevPciRaw.cpp"
106#endif
107
108#include <VBox/vmm/pdmaudioifs.h>
109
110#undef LOG_GROUP
111#include "../Audio/DevIchAc97.cpp"
112#undef LOG_GROUP
113#include "../Audio/DevIchHda.cpp"
114
115#include <stdio.h>
116
117
118/*********************************************************************************************************************************
119* Defined Constants And Macros *
120*********************************************************************************************************************************/
121/**
122 * Checks the offset of a data member.
123 * @param type Type.
124 * @param off Correct offset.
125 * @param m Member name.
126 */
127#define CHECK_OFF(type, off, m) \
128 do { \
129 if (off != RT_OFFSETOF(type, m)) \
130 { \
131 printf("tstDeviceStructSize: error! %#010x %s Off by %d!! (off=%#x)\n", RT_OFFSETOF(type, m), #type "." #m, off - RT_OFFSETOF(type, m), off); \
132 rc++; \
133 } \
134 else \
135 printf("%#08x (%d) %s\n", RT_OFFSETOF(type, m), RT_OFFSETOF(type, m), #type "." #m); \
136 } while (0)
137
138/**
139 * Checks the size of type.
140 * @param type Type.
141 * @param size Correct size.
142 */
143#define CHECK_SIZE(type, size) \
144 do { \
145 if (size != sizeof(type)) \
146 { \
147 printf("tstDeviceStructSize: error! sizeof(%s): %#x (%d) Off by %d!!\n", #type, (int)sizeof(type), (int)sizeof(type), (int)(sizeof(type) - size)); \
148 rc++; \
149 } \
150 else \
151 printf("tstDeviceStructSize: info: sizeof(%s): %#x (%d)\n", #type, (int)sizeof(type), (int)sizeof(type)); \
152 } while (0)
153
154/**
155 * Checks the alignment of a struct member.
156 */
157#define CHECK_MEMBER_ALIGNMENT(strct, member, align) \
158 do \
159 { \
160 if (RT_OFFSETOF(strct, member) & ((align) - 1) ) \
161 { \
162 printf("tstDeviceStructSize: error! %s::%s offset=%#x (%u) expected alignment %#x, meaning %#x (%u) off\n", \
163 #strct, #member, \
164 (unsigned)RT_OFFSETOF(strct, member), \
165 (unsigned)RT_OFFSETOF(strct, member), \
166 (unsigned)(align), \
167 (unsigned)(((align) - RT_OFFSETOF(strct, member)) & ((align) - 1)), \
168 (unsigned)(((align) - RT_OFFSETOF(strct, member)) & ((align) - 1)) ); \
169 rc++; \
170 } \
171 } while (0)
172
173/**
174 * Checks that the size of a type is aligned correctly.
175 */
176#define CHECK_SIZE_ALIGNMENT(type, align) \
177 do { \
178 if (RT_ALIGN_Z(sizeof(type), (align)) != sizeof(type)) \
179 { \
180 printf("tstDeviceStructSize: error! %s size=%#x (%u), align=%#x %#x (%u) bytes off\n", \
181 #type, \
182 (unsigned)sizeof(type), \
183 (unsigned)sizeof(type), \
184 (align), \
185 (unsigned)RT_ALIGN_Z(sizeof(type), align) - (unsigned)sizeof(type), \
186 (unsigned)RT_ALIGN_Z(sizeof(type), align) - (unsigned)sizeof(type)); \
187 rc++; \
188 } \
189 } while (0)
190
191/**
192 * Checks that a internal struct padding is big enough.
193 */
194#define CHECK_PADDING(strct, member, align) \
195 do \
196 { \
197 strct *p = NULL; NOREF(p); \
198 if (sizeof(p->member.s) > sizeof(p->member.padding)) \
199 { \
200 printf("tstDeviceStructSize: error! padding of %s::%s is too small, padding=%d struct=%d correct=%d\n", #strct, #member, \
201 (int)sizeof(p->member.padding), (int)sizeof(p->member.s), (int)RT_ALIGN_Z(sizeof(p->member.s), (align))); \
202 rc++; \
203 } \
204 else if (RT_ALIGN_Z(sizeof(p->member.padding), (align)) != sizeof(p->member.padding)) \
205 { \
206 printf("tstDeviceStructSize: error! padding of %s::%s is misaligned, padding=%d correct=%d\n", #strct, #member, \
207 (int)sizeof(p->member.padding), (int)RT_ALIGN_Z(sizeof(p->member.s), (align))); \
208 rc++; \
209 } \
210 } while (0)
211
212/**
213 * Checks that a internal struct padding is big enough.
214 */
215#define CHECK_PADDING2(strct) \
216 do \
217 { \
218 strct *p = NULL; NOREF(p); \
219 if (sizeof(p->s) > sizeof(p->padding)) \
220 { \
221 printf("tstDeviceStructSize: error! padding of %s is too small, padding=%d struct=%d correct=%d\n", #strct, \
222 (int)sizeof(p->padding), (int)sizeof(p->s), (int)RT_ALIGN_Z(sizeof(p->s), 32)); \
223 rc++; \
224 } \
225 } while (0)
226
227/**
228 * Checks that a internal struct padding is big enough.
229 */
230#define CHECK_PADDING3(strct, member, pad_member) \
231 do \
232 { \
233 strct *p = NULL; NOREF(p); \
234 if (sizeof(p->member) > sizeof(p->pad_member)) \
235 { \
236 printf("tstDeviceStructSize: error! padding of %s::%s is too small, padding=%d struct=%d\n", #strct, #member, \
237 (int)sizeof(p->pad_member), (int)sizeof(p->member)); \
238 rc++; \
239 } \
240 } while (0)
241
242/**
243 * Prints the offset of a struct member.
244 */
245#define PRINT_OFFSET(strct, member) \
246 do \
247 { \
248 printf("tstDeviceStructSize: info: %s::%s offset %d sizeof %d\n", #strct, #member, (int)RT_OFFSETOF(strct, member), (int)RT_SIZEOFMEMB(strct, member)); \
249 } while (0)
250
251
252int main()
253{
254 int rc = 0;
255 printf("tstDeviceStructSize: TESTING\n");
256
257 /* Assert sanity */
258 CHECK_SIZE(uint128_t, 128/8);
259 CHECK_SIZE(int128_t, 128/8);
260 CHECK_SIZE(uint64_t, 64/8);
261 CHECK_SIZE(int64_t, 64/8);
262 CHECK_SIZE(uint32_t, 32/8);
263 CHECK_SIZE(int32_t, 32/8);
264 CHECK_SIZE(uint16_t, 16/8);
265 CHECK_SIZE(int16_t, 16/8);
266 CHECK_SIZE(uint8_t, 8/8);
267 CHECK_SIZE(int8_t, 8/8);
268
269 /* Basic alignment checks. */
270 CHECK_MEMBER_ALIGNMENT(PDMDEVINS, achInstanceData, 64);
271 CHECK_MEMBER_ALIGNMENT(PCIDEVICE, Int.s, 16);
272 CHECK_MEMBER_ALIGNMENT(PCIDEVICE, Int.s.aIORegions, 16);
273
274 /*
275 * Misc alignment checks (keep this somewhat alphabetical).
276 */
277 CHECK_MEMBER_ALIGNMENT(AHCI, lock, 8);
278 CHECK_MEMBER_ALIGNMENT(AHCIPort, StatDMA, 8);
279#ifdef VBOX_WITH_STATISTICS
280 CHECK_MEMBER_ALIGNMENT(APICDeviceInfo, StatMMIOReadGC, 8);
281#endif
282 CHECK_MEMBER_ALIGNMENT(ATADevState, cTotalSectors, 8);
283 CHECK_MEMBER_ALIGNMENT(ATADevState, StatATADMA, 8);
284 CHECK_MEMBER_ALIGNMENT(ATADevState, StatReads, 8);
285 CHECK_MEMBER_ALIGNMENT(ATACONTROLLER, lock, 8);
286 CHECK_MEMBER_ALIGNMENT(ATACONTROLLER, StatAsyncOps, 8);
287 CHECK_MEMBER_ALIGNMENT(BUSLOGIC, CritSectIntr, 8);
288#ifdef VBOX_WITH_STATISTICS
289 CHECK_MEMBER_ALIGNMENT(DEVPIC, StatSetIrqGC, 8);
290#endif
291#ifdef VBOX_WITH_E1000
292 CHECK_MEMBER_ALIGNMENT(E1KSTATE, cs, 8);
293 CHECK_MEMBER_ALIGNMENT(E1KSTATE, csRx, 8);
294 CHECK_MEMBER_ALIGNMENT(E1KSTATE, StatReceiveBytes, 8);
295#endif
296#ifdef VBOX_WITH_VIRTIO
297 CHECK_MEMBER_ALIGNMENT(VNETSTATE, StatReceiveBytes, 8);
298#endif
299 //CHECK_MEMBER_ALIGNMENT(E1KSTATE, csTx, 8);
300#ifdef VBOX_WITH_USB
301# ifdef VBOX_WITH_EHCI_IMPL
302 CHECK_MEMBER_ALIGNMENT(EHCI, RootHub, 8);
303# ifdef VBOX_WITH_STATISTICS
304 CHECK_MEMBER_ALIGNMENT(EHCI, StatCanceledIsocUrbs, 8);
305# endif
306# endif
307# ifdef VBOX_WITH_XHCI_IMPL
308 CHECK_MEMBER_ALIGNMENT(XHCI, RootHub2, 8);
309 CHECK_MEMBER_ALIGNMENT(XHCI, RootHub3, 8);
310 CHECK_MEMBER_ALIGNMENT(XHCI, cmdr_dqp, 8);
311# ifdef VBOX_WITH_STATISTICS
312 CHECK_MEMBER_ALIGNMENT(XHCI, StatCanceledIsocUrbs, 8);
313 CHECK_MEMBER_ALIGNMENT(XHCI, StatIntrsCleared, 8);
314# endif
315# endif
316#endif
317 CHECK_MEMBER_ALIGNMENT(E1KSTATE, StatReceiveBytes, 8);
318#ifdef VBOX_WITH_STATISTICS
319 CHECK_MEMBER_ALIGNMENT(IOAPIC, StatMMIOReadGC, 8);
320 CHECK_MEMBER_ALIGNMENT(IOAPIC, StatMMIOReadGC, 8);
321#endif
322 CHECK_MEMBER_ALIGNMENT(LSILOGISCSI, GCPhysMMIOBase, 8);
323 CHECK_MEMBER_ALIGNMENT(LSILOGISCSI, aMessage, 8);
324 CHECK_MEMBER_ALIGNMENT(LSILOGISCSI, ReplyPostQueueCritSect, 8);
325 CHECK_MEMBER_ALIGNMENT(LSILOGISCSI, ReplyFreeQueueCritSect, 8);
326 CHECK_MEMBER_ALIGNMENT(LSILOGISCSI, uReplyFreeQueueNextEntryFreeWrite, 8);
327 CHECK_MEMBER_ALIGNMENT(LSILOGISCSI, VBoxSCSI, 8);
328#ifdef VBOX_WITH_USB
329 CHECK_MEMBER_ALIGNMENT(OHCI, RootHub, 8);
330# ifdef VBOX_WITH_STATISTICS
331 CHECK_MEMBER_ALIGNMENT(OHCI, StatCanceledIsocUrbs, 8);
332# endif
333#endif
334 CHECK_MEMBER_ALIGNMENT(PCIBUS, devices, 16);
335 CHECK_MEMBER_ALIGNMENT(PCIBUS, devices, 16);
336 CHECK_MEMBER_ALIGNMENT(PCIGLOBALS, pci_irq_levels, 16);
337 CHECK_MEMBER_ALIGNMENT(PCNETSTATE, u64LastPoll, 8);
338 CHECK_MEMBER_ALIGNMENT(PCNETSTATE, CritSect, 8);
339 CHECK_MEMBER_ALIGNMENT(PCNETSTATE, StatReceiveBytes, 8);
340#ifdef VBOX_WITH_STATISTICS
341 CHECK_MEMBER_ALIGNMENT(PCNETSTATE, StatMMIOReadRZ, 8);
342#endif
343 CHECK_MEMBER_ALIGNMENT(PITSTATE, StatPITIrq, 8);
344 CHECK_MEMBER_ALIGNMENT(SerialState, CritSect, 8);
345#ifdef VBOX_WITH_VMSVGA
346 CHECK_SIZE(VMSVGAState, RT_ALIGN_Z(sizeof(VMSVGAState), 8));
347 CHECK_MEMBER_ALIGNMENT(VGASTATE, svga, 8);
348 CHECK_MEMBER_ALIGNMENT(VGASTATE, svga.u64HostWindowId, 8);
349#endif
350 CHECK_MEMBER_ALIGNMENT(VGASTATE, cMonitors, 8);
351 CHECK_MEMBER_ALIGNMENT(VGASTATE, GCPhysVRAM, 8);
352 CHECK_MEMBER_ALIGNMENT(VGASTATE, Dev, 8);
353 CHECK_MEMBER_ALIGNMENT(VGASTATE, CritSect, 8);
354 CHECK_MEMBER_ALIGNMENT(VGASTATE, StatRZMemoryRead, 8);
355 CHECK_MEMBER_ALIGNMENT(VMMDevState, CritSect, 8);
356#ifdef VBOX_WITH_VIRTIO
357 CHECK_MEMBER_ALIGNMENT(VPCISTATE, cs, 8);
358 CHECK_MEMBER_ALIGNMENT(VPCISTATE, led, 4);
359 CHECK_MEMBER_ALIGNMENT(VPCISTATE, Queues, 8);
360#endif
361#ifdef VBOX_WITH_PCI_PASSTHROUGH_IMPL
362 CHECK_MEMBER_ALIGNMENT(PCIRAWSENDREQ, u.aGetRegionInfo.u64RegionSize, 8);
363#endif
364
365#ifdef VBOX_WITH_RAW_MODE
366 /*
367 * Compare HC and RC.
368 */
369 printf("tstDeviceStructSize: Comparing HC and RC...\n");
370# include "tstDeviceStructSizeRC.h"
371#endif
372
373 /*
374 * Report result.
375 */
376 if (rc)
377 printf("tstDeviceStructSize: FAILURE - %d errors\n", rc);
378 else
379 printf("tstDeviceStructSize: SUCCESS\n");
380 return rc;
381}
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