VirtualBox

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

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

tstDeviceStructSize,tstHelp.h: Fixed cast in CHECK_SIZE that can cause troublesome compliation warnings/errors when things go wrong.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 13.3 KB
Line 
1/* $Id: tstDeviceStructSize.cpp 71731 2018-04-07 14:10:34Z 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-2017 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/DevPciInternal.h"
32#undef LOG_GROUP
33#include "../Graphics/DevVGA.cpp"
34#undef LOG_GROUP
35#include "../Input/DevPS2.cpp"
36#undef LOG_GROUP
37#include "../Input/PS2K.cpp"
38#undef LOG_GROUP
39#include "../Input/PS2M.cpp"
40#ifdef VBOX_WITH_E1000
41# undef LOG_GROUP
42# include "../Network/DevE1000.cpp"
43#endif
44#undef LOG_GROUP
45#include "../Network/DevPCNet.cpp"
46#ifdef VBOX_WITH_VIRTIO
47# undef LOG_GROUP
48# include "../Network/DevVirtioNet.cpp"
49#endif
50#undef LOG_GROUP
51#include "../PC/DevACPI.cpp"
52#undef LOG_GROUP
53#include "../PC/DevPIC.cpp"
54#undef LOG_GROUP
55#include "../PC/DevPit-i8254.cpp"
56#undef LOG_GROUP
57#include "../PC/DevRTC.cpp"
58# undef LOG_GROUP
59# include "../../VMM/VMMR3/APIC.cpp"
60#undef LOG_GROUP
61#include "../PC/DevIoApic.cpp"
62#undef LOG_GROUP
63#include "../PC/DevHPET.cpp"
64#undef LOG_GROUP
65#include "../PC/DevDMA.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#ifdef VBOX_WITH_NVME_IMPL
103# undef LOG_GROUP
104# include "../Storage/DevNVMe.cpp"
105#endif
106
107#ifdef VBOX_WITH_PCI_PASSTHROUGH_IMPL
108# undef LOG_GROUP
109# include "../Bus/DevPciRaw.cpp"
110#endif
111
112#include <VBox/vmm/pdmaudioifs.h>
113
114#undef LOG_GROUP
115#include "../Audio/DevIchAc97.cpp"
116#undef LOG_GROUP
117#include "../Audio/DevHDA.cpp"
118
119#include <stdio.h>
120
121
122/*********************************************************************************************************************************
123* Defined Constants And Macros *
124*********************************************************************************************************************************/
125/**
126 * Checks the offset of a data member.
127 * @param type Type.
128 * @param off Correct offset.
129 * @param m Member name.
130 */
131#define CHECK_OFF(type, off, m) \
132 do { \
133 if (off != RT_OFFSETOF(type, m)) \
134 { \
135 printf("tstDeviceStructSize: Error! %#010x %s Member offset wrong by %d (should be %d -- but is %d)\n", \
136 RT_OFFSETOF(type, m), #type "." #m, off - RT_OFFSETOF(type, m), off, RT_OFFSETOF(type, m)); \
137 rc++; \
138 } \
139 else \
140 printf("%#08x (%d) %s\n", RT_OFFSETOF(type, m), RT_OFFSETOF(type, m), #type "." #m); \
141 } while (0)
142
143/**
144 * Checks the size of type.
145 * @param type Type.
146 * @param size Correct size.
147 */
148#define CHECK_SIZE(type, size) \
149 do { \
150 if (size != sizeof(type)) \
151 { \
152 printf("tstDeviceStructSize: Error! sizeof(%s): %#x (%d) Size wrong by %d (should be %d -- but is %d)\n", \
153 #type, (int)sizeof(type), (int)sizeof(type), (int)sizeof(type) - (int)size, (int)size, (int)sizeof(type)); \
154 rc++; \
155 } \
156 else \
157 printf("tstDeviceStructSize: info: sizeof(%s): %#x (%d)\n", #type, (int)sizeof(type), (int)sizeof(type)); \
158 } while (0)
159
160/**
161 * Checks the alignment of a struct member.
162 */
163#define CHECK_MEMBER_ALIGNMENT(strct, member, align) \
164 do \
165 { \
166 if (RT_OFFSETOF(strct, member) & ((align) - 1) ) \
167 { \
168 printf("tstDeviceStructSize: error! %s::%s offset=%#x (%u) expected alignment %#x, meaning %#x (%u) off\n", \
169 #strct, #member, \
170 (unsigned)RT_OFFSETOF(strct, member), \
171 (unsigned)RT_OFFSETOF(strct, member), \
172 (unsigned)(align), \
173 (unsigned)(((align) - RT_OFFSETOF(strct, member)) & ((align) - 1)), \
174 (unsigned)(((align) - RT_OFFSETOF(strct, member)) & ((align) - 1)) ); \
175 rc++; \
176 } \
177 } while (0)
178
179/**
180 * Checks that the size of a type is aligned correctly.
181 */
182#define CHECK_SIZE_ALIGNMENT(type, align) \
183 do { \
184 if (RT_ALIGN_Z(sizeof(type), (align)) != sizeof(type)) \
185 { \
186 printf("tstDeviceStructSize: error! %s size=%#x (%u), align=%#x %#x (%u) bytes off\n", \
187 #type, \
188 (unsigned)sizeof(type), \
189 (unsigned)sizeof(type), \
190 (align), \
191 (unsigned)RT_ALIGN_Z(sizeof(type), align) - (unsigned)sizeof(type), \
192 (unsigned)RT_ALIGN_Z(sizeof(type), align) - (unsigned)sizeof(type)); \
193 rc++; \
194 } \
195 } while (0)
196
197/**
198 * Checks that a internal struct padding is big enough.
199 */
200#define CHECK_PADDING(strct, member, align) \
201 do \
202 { \
203 strct *p = NULL; NOREF(p); \
204 if (sizeof(p->member.s) > sizeof(p->member.padding)) \
205 { \
206 printf("tstDeviceStructSize: error! padding of %s::%s is too small, padding=%d struct=%d correct=%d\n", #strct, #member, \
207 (int)sizeof(p->member.padding), (int)sizeof(p->member.s), (int)RT_ALIGN_Z(sizeof(p->member.s), (align))); \
208 rc++; \
209 } \
210 else if (RT_ALIGN_Z(sizeof(p->member.padding), (align)) != sizeof(p->member.padding)) \
211 { \
212 printf("tstDeviceStructSize: error! padding of %s::%s is misaligned, padding=%d correct=%d\n", #strct, #member, \
213 (int)sizeof(p->member.padding), (int)RT_ALIGN_Z(sizeof(p->member.s), (align))); \
214 rc++; \
215 } \
216 } while (0)
217
218/**
219 * Checks that a internal struct padding is big enough.
220 */
221#define CHECK_PADDING2(strct) \
222 do \
223 { \
224 strct *p = NULL; NOREF(p); \
225 if (sizeof(p->s) > sizeof(p->padding)) \
226 { \
227 printf("tstDeviceStructSize: error! padding of %s is too small, padding=%d struct=%d correct=%d\n", #strct, \
228 (int)sizeof(p->padding), (int)sizeof(p->s), (int)RT_ALIGN_Z(sizeof(p->s), 32)); \
229 rc++; \
230 } \
231 } while (0)
232
233/**
234 * Checks that a internal struct padding is big enough.
235 */
236#define CHECK_PADDING3(strct, member, pad_member) \
237 do \
238 { \
239 strct *p = NULL; NOREF(p); \
240 if (sizeof(p->member) > sizeof(p->pad_member)) \
241 { \
242 printf("tstDeviceStructSize: error! padding of %s::%s is too small, padding=%d struct=%d\n", #strct, #member, \
243 (int)sizeof(p->pad_member), (int)sizeof(p->member)); \
244 rc++; \
245 } \
246 } while (0)
247
248/**
249 * Prints the offset of a struct member.
250 */
251#define PRINT_OFFSET(strct, member) \
252 do \
253 { \
254 printf("tstDeviceStructSize: info: %s::%s offset %d sizeof %d\n", #strct, #member, (int)RT_OFFSETOF(strct, member), (int)RT_SIZEOFMEMB(strct, member)); \
255 } while (0)
256
257
258int main()
259{
260 int rc = 0;
261 printf("tstDeviceStructSize: TESTING\n");
262
263 /* Assert sanity */
264 CHECK_SIZE(uint128_t, 128/8);
265 CHECK_SIZE(int128_t, 128/8);
266 CHECK_SIZE(uint64_t, 64/8);
267 CHECK_SIZE(int64_t, 64/8);
268 CHECK_SIZE(uint32_t, 32/8);
269 CHECK_SIZE(int32_t, 32/8);
270 CHECK_SIZE(uint16_t, 16/8);
271 CHECK_SIZE(int16_t, 16/8);
272 CHECK_SIZE(uint8_t, 8/8);
273 CHECK_SIZE(int8_t, 8/8);
274
275 /* Basic alignment checks. */
276 CHECK_MEMBER_ALIGNMENT(PDMDEVINS, achInstanceData, 64);
277 CHECK_MEMBER_ALIGNMENT(PDMPCIDEV, Int.s, 16);
278 CHECK_MEMBER_ALIGNMENT(PDMPCIDEV, Int.s.aIORegions, 16);
279
280 /*
281 * Misc alignment checks (keep this somewhat alphabetical).
282 */
283 CHECK_MEMBER_ALIGNMENT(AC97STATE, CritSect, 8);
284
285 CHECK_MEMBER_ALIGNMENT(AHCI, lock, 8);
286 CHECK_MEMBER_ALIGNMENT(AHCI, ahciPort[0], 8);
287
288 CHECK_MEMBER_ALIGNMENT(APICDEV, pDevInsR0, 8);
289 CHECK_MEMBER_ALIGNMENT(APICDEV, pDevInsRC, 8);
290
291 CHECK_MEMBER_ALIGNMENT(ATADevState, cTotalSectors, 8);
292 CHECK_MEMBER_ALIGNMENT(ATADevState, StatATADMA, 8);
293 CHECK_MEMBER_ALIGNMENT(ATADevState, StatReads, 8);
294 CHECK_MEMBER_ALIGNMENT(ATACONTROLLER, lock, 8);
295 CHECK_MEMBER_ALIGNMENT(ATACONTROLLER, StatAsyncOps, 8);
296 CHECK_MEMBER_ALIGNMENT(BUSLOGIC, CritSectIntr, 8);
297#ifdef VBOX_WITH_STATISTICS
298 CHECK_MEMBER_ALIGNMENT(DEVPIC, StatSetIrqGC, 8);
299#endif
300#ifdef VBOX_WITH_E1000
301 CHECK_MEMBER_ALIGNMENT(E1KSTATE, cs, 8);
302 CHECK_MEMBER_ALIGNMENT(E1KSTATE, csRx, 8);
303 CHECK_MEMBER_ALIGNMENT(E1KSTATE, StatReceiveBytes, 8);
304#endif
305#ifdef VBOX_WITH_VIRTIO
306 CHECK_MEMBER_ALIGNMENT(VNETSTATE, StatReceiveBytes, 8);
307#endif
308 //CHECK_MEMBER_ALIGNMENT(E1KSTATE, csTx, 8);
309#ifdef VBOX_WITH_USB
310# ifdef VBOX_WITH_EHCI_IMPL
311 CHECK_MEMBER_ALIGNMENT(EHCI, RootHub, 8);
312# ifdef VBOX_WITH_STATISTICS
313 CHECK_MEMBER_ALIGNMENT(EHCI, StatCanceledIsocUrbs, 8);
314# endif
315# endif
316# ifdef VBOX_WITH_XHCI_IMPL
317 CHECK_MEMBER_ALIGNMENT(XHCI, pWorkerThread, 8);
318 CHECK_MEMBER_ALIGNMENT(XHCI, IBase, 8);
319 CHECK_MEMBER_ALIGNMENT(XHCI, MMIOBase, 8);
320 CHECK_MEMBER_ALIGNMENT(XHCI, RootHub2, 8);
321 CHECK_MEMBER_ALIGNMENT(XHCI, RootHub3, 8);
322 CHECK_MEMBER_ALIGNMENT(XHCI, cmdr_dqp, 8);
323# ifdef VBOX_WITH_STATISTICS
324 CHECK_MEMBER_ALIGNMENT(XHCI, StatErrorIsocUrbs, 8);
325 CHECK_MEMBER_ALIGNMENT(XHCI, StatIntrsCleared, 8);
326# endif
327# endif
328#endif
329 CHECK_MEMBER_ALIGNMENT(E1KSTATE, StatReceiveBytes, 8);
330 CHECK_MEMBER_ALIGNMENT(IOAPIC, au64RedirTable, 8);
331# ifdef VBOX_WITH_STATISTICS
332 CHECK_MEMBER_ALIGNMENT(IOAPIC, StatMmioReadRZ, 8);
333# endif
334 CHECK_MEMBER_ALIGNMENT(LSILOGISCSI, GCPhysMMIOBase, 8);
335 CHECK_MEMBER_ALIGNMENT(LSILOGISCSI, aMessage, 8);
336 CHECK_MEMBER_ALIGNMENT(LSILOGISCSI, ReplyPostQueueCritSect, 8);
337 CHECK_MEMBER_ALIGNMENT(LSILOGISCSI, ReplyFreeQueueCritSect, 8);
338 CHECK_MEMBER_ALIGNMENT(LSILOGISCSI, uReplyFreeQueueNextEntryFreeWrite, 8);
339 CHECK_MEMBER_ALIGNMENT(LSILOGISCSI, VBoxSCSI, 8);
340#ifdef VBOX_WITH_USB
341 CHECK_MEMBER_ALIGNMENT(OHCI, RootHub, 8);
342# ifdef VBOX_WITH_STATISTICS
343 CHECK_MEMBER_ALIGNMENT(OHCI, StatCanceledIsocUrbs, 8);
344# endif
345#endif
346 CHECK_MEMBER_ALIGNMENT(DEVPCIBUS, apDevices, 64);
347 CHECK_MEMBER_ALIGNMENT(DEVPCIROOT, auPciApicIrqLevels, 16);
348 CHECK_MEMBER_ALIGNMENT(DEVPCIROOT, Piix3.auPciLegacyIrqLevels, 16);
349 CHECK_MEMBER_ALIGNMENT(PCNETSTATE, u64LastPoll, 8);
350 CHECK_MEMBER_ALIGNMENT(PCNETSTATE, CritSect, 8);
351 CHECK_MEMBER_ALIGNMENT(PCNETSTATE, StatReceiveBytes, 8);
352#ifdef VBOX_WITH_STATISTICS
353 CHECK_MEMBER_ALIGNMENT(PCNETSTATE, StatMMIOReadRZ, 8);
354#endif
355 CHECK_MEMBER_ALIGNMENT(PITSTATE, StatPITIrq, 8);
356 CHECK_MEMBER_ALIGNMENT(SerialState, CritSect, 8);
357#ifdef VBOX_WITH_VMSVGA
358 CHECK_SIZE(VMSVGAState, RT_ALIGN_Z(sizeof(VMSVGAState), 8));
359 CHECK_MEMBER_ALIGNMENT(VGASTATE, svga, 8);
360 CHECK_MEMBER_ALIGNMENT(VGASTATE, svga.u64HostWindowId, 8);
361 CHECK_MEMBER_ALIGNMENT(VGASTATE, svga.au32ScratchRegion, 8);
362 CHECK_MEMBER_ALIGNMENT(VGASTATE, svga.StatRegBitsPerPixelWr, 8);
363#endif
364 CHECK_MEMBER_ALIGNMENT(VGASTATE, cMonitors, 8);
365 CHECK_MEMBER_ALIGNMENT(VGASTATE, GCPhysVRAM, 8);
366 CHECK_MEMBER_ALIGNMENT(VGASTATE, Dev, 8);
367 CHECK_MEMBER_ALIGNMENT(VGASTATE, CritSect, 8);
368 CHECK_MEMBER_ALIGNMENT(VGASTATE, StatRZMemoryRead, 8);
369 CHECK_MEMBER_ALIGNMENT(VGASTATE, CritSectIRQ, 8);
370 CHECK_MEMBER_ALIGNMENT(VMMDevState, CritSect, 8);
371#ifdef VBOX_WITH_VIRTIO
372 CHECK_MEMBER_ALIGNMENT(VPCISTATE, cs, 8);
373 CHECK_MEMBER_ALIGNMENT(VPCISTATE, led, 4);
374 CHECK_MEMBER_ALIGNMENT(VPCISTATE, Queues, 8);
375#endif
376#ifdef VBOX_WITH_PCI_PASSTHROUGH_IMPL
377 CHECK_MEMBER_ALIGNMENT(PCIRAWSENDREQ, u.aGetRegionInfo.u64RegionSize, 8);
378#endif
379
380#ifdef VBOX_WITH_RAW_MODE
381 /*
382 * Compare HC and RC.
383 */
384 printf("tstDeviceStructSize: Comparing HC and RC...\n");
385# include "tstDeviceStructSizeRC.h"
386#endif
387
388 /*
389 * Report result.
390 */
391 if (rc)
392 printf("tstDeviceStructSize: FAILURE - %d errors\n", rc);
393 else
394 printf("tstDeviceStructSize: SUCCESS\n");
395 return rc;
396}
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