VirtualBox

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

Last change on this file since 88956 was 88624, checked in by vboxsync, 4 years ago

AMD IOMMU: bugref:9654 Devices/testcase: struct size and alignment verification.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 14.7 KB
Line 
1/* $Id: tstDeviceStructSize.cpp 88624 2021-04-21 08:00:07Z 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-2020 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#define VBOX_WITH_HGCM /* grumble */
28#define VBOX_DEVICE_STRUCT_TESTCASE
29
30/* Check that important preprocessor macros does not get redefined: */
31#include <VBox/cdefs.h>
32#include <VBox/log.h>
33#ifdef DEBUG
34# define VBOX_DEVICE_STRUCT_TESTCASE_CHECK_DEBUG
35#else
36# undef VBOX_DEVICE_STRUCT_TESTCASE_CHECK_DEBUG
37#endif
38#ifdef LOG_ENABLED
39# define VBOX_DEVICE_STRUCT_TESTCASE_CHECK_LOG_ENABLED
40#else
41# undef VBOX_DEVICE_STRUCT_TESTCASE_CHECK_LOG_ENABLED
42#endif
43#ifdef VBOX_STRICT
44# define VBOX_DEVICE_STRUCT_TESTCASE_CHECK_VBOX_STRICT
45#else
46# undef VBOX_DEVICE_STRUCT_TESTCASE_CHECK_VBOX_STRICT
47#endif
48#ifdef RT_STRICT
49# define VBOX_DEVICE_STRUCT_TESTCASE_CHECK_RT_STRICT
50#else
51# undef VBOX_DEVICE_STRUCT_TESTCASE_CHECK_RT_STRICT
52#endif
53
54/* The structures we're checking: */
55#undef LOG_GROUP
56#include "../Bus/DevPciInternal.h"
57#undef LOG_GROUP
58#include "../Graphics/DevVGA.cpp"
59#undef LOG_GROUP
60#include "../Input/DevPS2.h"
61#ifdef VBOX_WITH_E1000
62# undef LOG_GROUP
63# include "../Network/DevE1000.cpp"
64#endif
65#undef LOG_GROUP
66#include "../Network/DevPCNet.cpp"
67#ifdef VBOX_WITH_VIRTIO
68# undef LOG_GROUP
69# include "../Network/DevVirtioNet.cpp"
70#endif
71#undef LOG_GROUP
72#include "../PC/DevACPI.cpp"
73#undef LOG_GROUP
74#include "../PC/DevPIC.cpp"
75#undef LOG_GROUP
76#include "../PC/DevPit-i8254.cpp"
77#undef LOG_GROUP
78#include "../PC/DevRTC.cpp"
79# undef LOG_GROUP
80# include "../../VMM/VMMR3/APIC.cpp"
81#undef LOG_GROUP
82#include "../PC/DevIoApic.cpp"
83#undef LOG_GROUP
84#include "../PC/DevHPET.cpp"
85#undef LOG_GROUP
86#include "../PC/DevDMA.cpp"
87#undef LOG_GROUP
88#include "../EFI/DevSmc.cpp"
89#undef LOG_GROUP
90#include "../Storage/DevATA.cpp"
91#ifdef VBOX_WITH_USB
92# undef LOG_GROUP
93# include "../USB/DevOHCI.cpp"
94# ifdef VBOX_WITH_EHCI_IMPL
95# undef LOG_GROUP
96# include "../USB/DevEHCI.cpp"
97# endif
98# ifdef VBOX_WITH_XHCI_IMPL
99# undef LOG_GROUP
100# include "../USB/DevXHCI.cpp"
101# endif
102#endif
103#undef LOG_GROUP
104#include "../VMMDev/VMMDev.cpp"
105#undef LOG_GROUP
106#include "../Parallel/DevParallel.cpp"
107#undef LOG_GROUP
108#include "../Serial/DevSerial.cpp"
109#undef LOG_GROUP
110#include "../Serial/DevOxPcie958.cpp"
111#ifdef VBOX_WITH_AHCI
112# undef LOG_GROUP
113# include "../Storage/DevAHCI.cpp"
114#endif
115#ifdef VBOX_WITH_BUSLOGIC
116# undef LOG_GROUP
117# include "../Storage/DevBusLogic.cpp"
118#endif
119#ifdef VBOX_WITH_LSILOGIC
120# undef LOG_GROUP
121# include "../Storage/DevLsiLogicSCSI.cpp"
122#endif
123#ifdef VBOX_WITH_NVME_IMPL
124# undef LOG_GROUP
125# include "../Storage/DevNVMe.cpp"
126#endif
127
128#ifdef VBOX_WITH_PCI_PASSTHROUGH_IMPL
129# undef LOG_GROUP
130# include "../Bus/DevPciRaw.cpp"
131#endif
132
133#ifdef VBOX_WITH_IOMMU_AMD
134# undef LOG_GROUP
135# include "../Bus/DevIommuAmd.cpp"
136#endif
137
138#include <VBox/vmm/pdmaudioifs.h>
139
140#undef LOG_GROUP
141#include "../Audio/DevIchAc97.cpp"
142#undef LOG_GROUP
143#include "../Audio/DevHda.h"
144
145
146/* Check that important preprocessor macros didn't get redefined: */
147#if defined(DEBUG) != defined(VBOX_DEVICE_STRUCT_TESTCASE_CHECK_DEBUG)
148# error "DEBUG was modified! This may throw off structure tests."
149#endif
150#if defined(LOG_ENABLED) != defined(VBOX_DEVICE_STRUCT_TESTCASE_CHECK_LOG_ENABLED)
151# error "LOG_ENABLED was modified! This may throw off structure tests."
152#endif
153#if defined(RT_STRICT) != defined(VBOX_DEVICE_STRUCT_TESTCASE_CHECK_RT_STRICT)
154# error "RT_STRICT was modified! This may throw off structure tests."
155#endif
156#if defined(VBOX_STRICT) != defined(VBOX_DEVICE_STRUCT_TESTCASE_CHECK_VBOX_STRICT)
157# error "VBOX_STRICT was modified! This may throw off structure tests."
158#endif
159
160
161#include <stdio.h>
162
163
164/*********************************************************************************************************************************
165* Defined Constants And Macros *
166*********************************************************************************************************************************/
167/**
168 * Checks the offset of a data member.
169 * @param type Type.
170 * @param off Correct offset.
171 * @param m Member name.
172 */
173#define CHECK_OFF(type, off, m) \
174 do { \
175 if (off != RT_OFFSETOF(type, m)) \
176 { \
177 printf("tstDeviceStructSize: Error! %#010x %s Member offset wrong by %d (should be %d -- but is %d)\n", \
178 RT_OFFSETOF(type, m), #type "." #m, off - RT_OFFSETOF(type, m), off, RT_OFFSETOF(type, m)); \
179 rc++; \
180 } \
181 else \
182 printf("%#08x (%d) %s\n", RT_OFFSETOF(type, m), RT_OFFSETOF(type, m), #type "." #m); \
183 } while (0)
184
185/**
186 * Checks the size of type.
187 * @param type Type.
188 * @param size Correct size.
189 */
190#define CHECK_SIZE(type, size) \
191 do { \
192 if (size != sizeof(type)) \
193 { \
194 printf("tstDeviceStructSize: Error! sizeof(%s): %#x (%d) Size wrong by %d (should be %d -- but is %d)\n", \
195 #type, (int)sizeof(type), (int)sizeof(type), (int)sizeof(type) - (int)size, (int)size, (int)sizeof(type)); \
196 rc++; \
197 } \
198 else \
199 printf("tstDeviceStructSize: info: sizeof(%s): %#x (%d)\n", #type, (int)sizeof(type), (int)sizeof(type)); \
200 } while (0)
201
202/**
203 * Checks the alignment of a struct member.
204 */
205#define CHECK_MEMBER_ALIGNMENT(strct, member, align) \
206 do \
207 { \
208 if (RT_OFFSETOF(strct, member) & ((align) - 1) ) \
209 { \
210 printf("tstDeviceStructSize: error! %s::%s offset=%#x (%u) expected alignment %#x, meaning %#x (%u) off\n", \
211 #strct, #member, \
212 (unsigned)RT_OFFSETOF(strct, member), \
213 (unsigned)RT_OFFSETOF(strct, member), \
214 (unsigned)(align), \
215 (unsigned)(((align) - RT_OFFSETOF(strct, member)) & ((align) - 1)), \
216 (unsigned)(((align) - RT_OFFSETOF(strct, member)) & ((align) - 1)) ); \
217 rc++; \
218 } \
219 } while (0)
220
221/**
222 * Checks that the size of a type is aligned correctly.
223 */
224#define CHECK_SIZE_ALIGNMENT(type, align) \
225 do { \
226 if (RT_ALIGN_Z(sizeof(type), (align)) != sizeof(type)) \
227 { \
228 printf("tstDeviceStructSize: error! %s size=%#x (%u), align=%#x %#x (%u) bytes off\n", \
229 #type, \
230 (unsigned)sizeof(type), \
231 (unsigned)sizeof(type), \
232 (align), \
233 (unsigned)RT_ALIGN_Z(sizeof(type), align) - (unsigned)sizeof(type), \
234 (unsigned)RT_ALIGN_Z(sizeof(type), align) - (unsigned)sizeof(type)); \
235 rc++; \
236 } \
237 } while (0)
238
239/**
240 * Checks that a internal struct padding is big enough.
241 */
242#define CHECK_PADDING(strct, member, align) \
243 do \
244 { \
245 strct *p = NULL; NOREF(p); \
246 if (sizeof(p->member.s) > sizeof(p->member.padding)) \
247 { \
248 printf("tstDeviceStructSize: error! padding of %s::%s is too small, padding=%d struct=%d correct=%d\n", #strct, #member, \
249 (int)sizeof(p->member.padding), (int)sizeof(p->member.s), (int)RT_ALIGN_Z(sizeof(p->member.s), (align))); \
250 rc++; \
251 } \
252 else if (RT_ALIGN_Z(sizeof(p->member.padding), (align)) != sizeof(p->member.padding)) \
253 { \
254 printf("tstDeviceStructSize: error! padding of %s::%s is misaligned, padding=%d correct=%d\n", #strct, #member, \
255 (int)sizeof(p->member.padding), (int)RT_ALIGN_Z(sizeof(p->member.s), (align))); \
256 rc++; \
257 } \
258 } while (0)
259
260/**
261 * Checks that a internal struct padding is big enough.
262 */
263#define CHECK_PADDING2(strct) \
264 do \
265 { \
266 strct *p = NULL; NOREF(p); \
267 if (sizeof(p->s) > sizeof(p->padding)) \
268 { \
269 printf("tstDeviceStructSize: error! padding of %s is too small, padding=%d struct=%d correct=%d\n", #strct, \
270 (int)sizeof(p->padding), (int)sizeof(p->s), (int)RT_ALIGN_Z(sizeof(p->s), 32)); \
271 rc++; \
272 } \
273 } while (0)
274
275/**
276 * Checks that a internal struct padding is big enough.
277 */
278#define CHECK_PADDING3(strct, member, pad_member) \
279 do \
280 { \
281 strct *p = NULL; NOREF(p); \
282 if (sizeof(p->member) > sizeof(p->pad_member)) \
283 { \
284 printf("tstDeviceStructSize: error! padding of %s::%s is too small, padding=%d struct=%d\n", #strct, #member, \
285 (int)sizeof(p->pad_member), (int)sizeof(p->member)); \
286 rc++; \
287 } \
288 } while (0)
289
290/**
291 * Prints the offset of a struct member.
292 */
293#define PRINT_OFFSET(strct, member) \
294 do \
295 { \
296 printf("tstDeviceStructSize: info: %s::%s offset %d sizeof %d\n", #strct, #member, (int)RT_OFFSETOF(strct, member), (int)RT_SIZEOFMEMB(strct, member)); \
297 } while (0)
298
299
300int main()
301{
302 int rc = 0;
303 printf("tstDeviceStructSize: TESTING\n");
304
305 /* Assert sanity */
306 CHECK_SIZE(uint128_t, 128/8);
307 CHECK_SIZE(int128_t, 128/8);
308 CHECK_SIZE(uint64_t, 64/8);
309 CHECK_SIZE(int64_t, 64/8);
310 CHECK_SIZE(uint32_t, 32/8);
311 CHECK_SIZE(int32_t, 32/8);
312 CHECK_SIZE(uint16_t, 16/8);
313 CHECK_SIZE(int16_t, 16/8);
314 CHECK_SIZE(uint8_t, 8/8);
315 CHECK_SIZE(int8_t, 8/8);
316
317 /* Basic alignment checks. */
318 CHECK_MEMBER_ALIGNMENT(PDMDEVINS, achInstanceData, 64);
319 CHECK_MEMBER_ALIGNMENT(PDMPCIDEV, Int.s, 16);
320 CHECK_MEMBER_ALIGNMENT(PDMPCIDEV, Int.s.aIORegions, 16);
321
322 /*
323 * Misc alignment checks (keep this somewhat alphabetical).
324 */
325 CHECK_MEMBER_ALIGNMENT(AC97STATE, CritSect, 8);
326
327 CHECK_MEMBER_ALIGNMENT(AHCI, lock, 8);
328 CHECK_MEMBER_ALIGNMENT(AHCI, aPorts[0], 8);
329 CHECK_MEMBER_ALIGNMENT(AHCIR3, aPorts[0], 8);
330
331 CHECK_MEMBER_ALIGNMENT(ATADEVSTATE, cTotalSectors, 8);
332 CHECK_MEMBER_ALIGNMENT(ATADEVSTATE, StatATADMA, 8);
333 CHECK_MEMBER_ALIGNMENT(ATADEVSTATE, StatReads, 8);
334 CHECK_MEMBER_ALIGNMENT(ATACONTROLLER, lock, 8);
335 CHECK_MEMBER_ALIGNMENT(ATACONTROLLER, StatAsyncOps, 8);
336 CHECK_MEMBER_ALIGNMENT(BUSLOGIC, CritSectIntr, 8);
337#ifdef VBOX_WITH_STATISTICS
338 CHECK_MEMBER_ALIGNMENT(DEVPIC, StatSetIrqRZ, 8);
339#endif
340#ifdef VBOX_WITH_E1000
341 CHECK_MEMBER_ALIGNMENT(E1KSTATE, cs, 8);
342 CHECK_MEMBER_ALIGNMENT(E1KSTATE, csRx, 8);
343 CHECK_MEMBER_ALIGNMENT(E1KSTATE, StatReceiveBytes, 8);
344#endif
345#ifdef VBOX_WITH_VIRTIO
346 CHECK_MEMBER_ALIGNMENT(VNETSTATE, StatReceiveBytes, 8);
347#endif
348 //CHECK_MEMBER_ALIGNMENT(E1KSTATE, csTx, 8);
349#ifdef VBOX_WITH_USB
350# ifdef VBOX_WITH_EHCI_IMPL
351 CHECK_MEMBER_ALIGNMENT(EHCI, RootHub, 8);
352# endif
353# ifdef VBOX_WITH_XHCI_IMPL
354 CHECK_MEMBER_ALIGNMENT(XHCI, aPorts, 8);
355 CHECK_MEMBER_ALIGNMENT(XHCI, aInterrupters, 8);
356 CHECK_MEMBER_ALIGNMENT(XHCI, aInterrupters[0].lock, 8);
357 CHECK_MEMBER_ALIGNMENT(XHCI, aInterrupters[1].lock, 8);
358 CHECK_MEMBER_ALIGNMENT(XHCI, cmdr_dqp, 8);
359 CHECK_MEMBER_ALIGNMENT(XHCI, hMmio, 8);
360# ifdef VBOX_WITH_STATISTICS
361 CHECK_MEMBER_ALIGNMENT(XHCI, StatErrorIsocUrbs, 8);
362 CHECK_MEMBER_ALIGNMENT(XHCI, StatIntrsCleared, 8);
363# endif
364# endif
365#endif
366 CHECK_MEMBER_ALIGNMENT(E1KSTATE, StatReceiveBytes, 8);
367 CHECK_MEMBER_ALIGNMENT(IOAPIC, au64RedirTable, 8);
368# ifdef VBOX_WITH_STATISTICS
369 CHECK_MEMBER_ALIGNMENT(IOAPIC, StatMmioReadRZ, 8);
370# endif
371 CHECK_MEMBER_ALIGNMENT(LSILOGICSCSI, aMessage, 8);
372 CHECK_MEMBER_ALIGNMENT(LSILOGICSCSI, ReplyPostQueueCritSect, 8);
373 CHECK_MEMBER_ALIGNMENT(LSILOGICSCSI, ReplyFreeQueueCritSect, 8);
374 CHECK_MEMBER_ALIGNMENT(LSILOGICSCSI, uReplyFreeQueueNextEntryFreeWrite, 8);
375 CHECK_MEMBER_ALIGNMENT(LSILOGICSCSICC, VBoxSCSI, 8);
376#ifdef VBOX_WITH_USB
377 CHECK_MEMBER_ALIGNMENT(OHCI, RootHub, 8);
378# ifdef VBOX_WITH_STATISTICS
379 CHECK_MEMBER_ALIGNMENT(OHCI, StatCanceledIsocUrbs, 8);
380# endif
381#endif
382 CHECK_MEMBER_ALIGNMENT(DEVPCIBUS, apDevices, 64);
383 CHECK_MEMBER_ALIGNMENT(DEVPCIROOT, auPciApicIrqLevels, 16);
384 CHECK_MEMBER_ALIGNMENT(DEVPCIROOT, Piix3.auPciLegacyIrqLevels, 16);
385 CHECK_MEMBER_ALIGNMENT(PCNETSTATE, u64LastPoll, 8);
386 CHECK_MEMBER_ALIGNMENT(PCNETSTATE, CritSect, 8);
387 CHECK_MEMBER_ALIGNMENT(PCNETSTATE, StatReceiveBytes, 8);
388#ifdef VBOX_WITH_STATISTICS
389 CHECK_MEMBER_ALIGNMENT(PCNETSTATE, StatMMIOReadRZ, 8);
390#endif
391 CHECK_MEMBER_ALIGNMENT(PITSTATE, StatPITIrq, 8);
392 CHECK_MEMBER_ALIGNMENT(DEVSERIAL, UartCore, 8);
393 CHECK_MEMBER_ALIGNMENT(UARTCORE, CritSect, 8);
394#ifdef VBOX_WITH_VMSVGA
395 CHECK_SIZE(VMSVGAState, RT_ALIGN_Z(sizeof(VMSVGAState), 8));
396 CHECK_MEMBER_ALIGNMENT(VGASTATE, svga, 8);
397 CHECK_MEMBER_ALIGNMENT(VGASTATE, svga.au32ScratchRegion, 8);
398 CHECK_MEMBER_ALIGNMENT(VGASTATE, svga.StatRegBitsPerPixelWr, 8);
399#endif
400 CHECK_MEMBER_ALIGNMENT(VGASTATE, cMonitors, 8);
401 CHECK_MEMBER_ALIGNMENT(VGASTATE, GCPhysVRAM, 8);
402 CHECK_MEMBER_ALIGNMENT(VGASTATE, CritSect, 8);
403 CHECK_MEMBER_ALIGNMENT(VGASTATE, StatRZMemoryRead, 8);
404 CHECK_MEMBER_ALIGNMENT(VGASTATE, CritSectIRQ, 8);
405 CHECK_MEMBER_ALIGNMENT(VMMDEV, CritSect, 8);
406#ifdef VBOX_WITH_VIRTIO
407 CHECK_MEMBER_ALIGNMENT(VPCISTATE, cs, 8);
408 CHECK_MEMBER_ALIGNMENT(VPCISTATE, led, 4);
409 CHECK_MEMBER_ALIGNMENT(VPCISTATE, Queues, 8);
410#endif
411#ifdef VBOX_WITH_PCI_PASSTHROUGH_IMPL
412 CHECK_MEMBER_ALIGNMENT(PCIRAWSENDREQ, u.aGetRegionInfo.u64RegionSize, 8);
413#endif
414#ifdef VBOX_WITH_IOMMU_AMD
415 CHECK_MEMBER_ALIGNMENT(IOMMU, IommuBar, 8);
416 CHECK_MEMBER_ALIGNMENT(IOMMU, aDevTabBaseAddrs, 8);
417 CHECK_MEMBER_ALIGNMENT(IOMMU, CmdBufHeadPtr, 8);
418 CHECK_MEMBER_ALIGNMENT(IOMMU, Status, 8);
419# ifdef VBOX_WITH_STATISTICS
420 CHECK_MEMBER_ALIGNMENT(IOMMU, StatMmioReadR3, 8);
421# endif
422#endif
423
424#ifdef VBOX_WITH_RAW_MODE
425 /*
426 * Compare HC and RC.
427 */
428 printf("tstDeviceStructSize: Comparing HC and RC...\n");
429# include "tstDeviceStructSizeRC.h"
430#endif
431
432 /*
433 * Report result.
434 */
435 if (rc)
436 printf("tstDeviceStructSize: FAILURE - %d errors\n", rc);
437 else
438 printf("tstDeviceStructSize: SUCCESS\n");
439 return rc;
440}
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