VirtualBox

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

Last change on this file since 30863 was 30772, checked in by vboxsync, 14 years ago

VMMDevTesting/test1: Report the results to the host via a RTTest* like framework.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 11.0 KB
Line 
1/* $Id: tstDeviceStructSize.cpp 30772 2010-07-10 04:58:28Z 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-2010 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* Header Files *
22*******************************************************************************/
23#include <VBox/types.h>
24#include <VBox/x86.h>
25
26
27#define VBOX_WITH_HGCM /* grumble */
28#define VBOX_DEVICE_STRUCT_TESTCASE
29#undef LOG_GROUP
30#include "../Bus/DevPCI.cpp"
31#undef LOG_GROUP
32#include "../Graphics/DevVGA.cpp"
33#undef LOG_GROUP
34#include "../Input/DevPS2.cpp"
35#ifdef VBOX_WITH_E1000
36# undef LOG_GROUP
37# include "../Network/DevE1000.cpp"
38#endif
39#undef LOG_GROUP
40#include "../Network/DevPCNet.cpp"
41#ifdef VBOX_WITH_VIRTIO
42# undef LOG_GROUP
43# include "../Network/DevVirtioNet.cpp"
44#endif
45#undef LOG_GROUP
46#include "../PC/DevACPI.cpp"
47#undef LOG_GROUP
48#include "../PC/DevPIC.cpp"
49#undef LOG_GROUP
50#include "../PC/DevPit-i8254.cpp"
51#undef LOG_GROUP
52#include "../PC/DevRTC.cpp"
53#undef LOG_GROUP
54#include "../PC/DevAPIC.cpp"
55#ifdef VBOX_WITH_HPET
56# undef LOG_GROUP
57# include "../PC/DevHPET.cpp"
58#endif
59#ifdef VBOX_WITH_LPC
60# undef LOG_GROUP
61# include "../PC/DevLPC.cpp"
62#endif
63#ifdef VBOX_WITH_SMC
64# undef LOG_GROUP
65# include "../PC/DevSMC.cpp"
66#endif
67#undef LOG_GROUP
68#include "../Storage/DevATA.cpp"
69#ifdef VBOX_WITH_USB
70# undef LOG_GROUP
71# include "../USB/DevOHCI.cpp"
72# include "../USB/DevEHCI.cpp"
73#endif
74#undef LOG_GROUP
75#include "../VMMDev/VMMDev.cpp"
76#undef LOG_GROUP
77#include "../Parallel/DevParallel.cpp"
78#undef LOG_GROUP
79#include "../Serial/DevSerial.cpp"
80#ifdef VBOX_WITH_AHCI
81# undef LOG_GROUP
82# include "../Storage/DevAHCI.cpp"
83#endif
84#ifdef VBOX_WITH_BUSLOGIC
85# undef LOG_GROUP
86# include "../Storage/DevBusLogic.cpp"
87#endif
88#ifdef VBOX_WITH_LSILOGIC
89# undef LOG_GROUP
90# include "../Storage/DevLsiLogicSCSI.cpp"
91#endif
92
93#include <stdio.h>
94
95
96/*******************************************************************************
97* Defined Constants And Macros *
98*******************************************************************************/
99/**
100 * Checks the offset of a data member.
101 * @param type Type.
102 * @param off Correct offset.
103 * @param m Member name.
104 */
105#define CHECK_OFF(type, off, m) \
106 do { \
107 if (off != RT_OFFSETOF(type, m)) \
108 { \
109 printf("tstDeviceStructSize: error! %#010x %s Off by %d!! (off=%#x)\n", RT_OFFSETOF(type, m), #type "." #m, off - RT_OFFSETOF(type, m), off); \
110 rc++; \
111 } \
112 /*else */ \
113 /*printf("%#08x %s\n", RT_OFFSETOF(type, m), #m);*/ \
114 } while (0)
115
116/**
117 * Checks the size of type.
118 * @param type Type.
119 * @param size Correct size.
120 */
121#define CHECK_SIZE(type, size) \
122 do { \
123 if (size != sizeof(type)) \
124 { \
125 printf("tstDeviceStructSize: error! sizeof(%s): %#x (%d) Off by %d!!\n", #type, (int)sizeof(type), (int)sizeof(type), (int)(sizeof(type) - size)); \
126 rc++; \
127 } \
128 else \
129 printf("tstDeviceStructSize: info: sizeof(%s): %#x (%d)\n", #type, (int)sizeof(type), (int)sizeof(type)); \
130 } while (0)
131
132/**
133 * Checks the alignment of a struct member.
134 */
135#define CHECK_MEMBER_ALIGNMENT(strct, member, align) \
136 do \
137 { \
138 if (RT_OFFSETOF(strct, member) & ((align) - 1) ) \
139 { \
140 printf("tstDeviceStructSize: error! %s::%s offset=%#x (%u) expected alignment %x, meaning %#x (%u) off\n", \
141 #strct, #member, \
142 (unsigned)RT_OFFSETOF(strct, member), \
143 (unsigned)RT_OFFSETOF(strct, member), \
144 (unsigned)(align), \
145 (unsigned)(((align) - RT_OFFSETOF(strct, member)) & ((align) - 1)), \
146 (unsigned)(((align) - RT_OFFSETOF(strct, member)) & ((align) - 1)) ); \
147 rc++; \
148 } \
149 } while (0)
150
151/**
152 * Checks that the size of a type is aligned correctly.
153 */
154#define CHECK_SIZE_ALIGNMENT(type, align) \
155 do { \
156 if (RT_ALIGN_Z(sizeof(type), (align)) != sizeof(type)) \
157 { \
158 printf("tstDeviceStructSize: error! %s size=%#x (%u), align=%#x %#x (%u) bytes off\n", \
159 #type, \
160 (unsigned)sizeof(type), \
161 (unsigned)sizeof(type), \
162 (align), \
163 (unsigned)RT_ALIGN_Z(sizeof(type), align) - (unsigned)sizeof(type), \
164 (unsigned)RT_ALIGN_Z(sizeof(type), align) - (unsigned)sizeof(type)); \
165 rc++; \
166 } \
167 } while (0)
168
169/**
170 * Checks that a internal struct padding is big enough.
171 */
172#define CHECK_PADDING(strct, member, align) \
173 do \
174 { \
175 strct *p = NULL; NOREF(p); \
176 if (sizeof(p->member.s) > sizeof(p->member.padding)) \
177 { \
178 printf("tstDeviceStructSize: error! padding of %s::%s is too small, padding=%d struct=%d correct=%d\n", #strct, #member, \
179 (int)sizeof(p->member.padding), (int)sizeof(p->member.s), (int)RT_ALIGN_Z(sizeof(p->member.s), (align))); \
180 rc++; \
181 } \
182 else if (RT_ALIGN_Z(sizeof(p->member.padding), (align)) != sizeof(p->member.padding)) \
183 { \
184 printf("tstDeviceStructSize: error! padding of %s::%s is misaligned, padding=%d correct=%d\n", #strct, #member, \
185 (int)sizeof(p->member.padding), (int)RT_ALIGN_Z(sizeof(p->member.s), (align))); \
186 rc++; \
187 } \
188 } while (0)
189
190/**
191 * Checks that a internal struct padding is big enough.
192 */
193#define CHECK_PADDING2(strct) \
194 do \
195 { \
196 strct *p = NULL; NOREF(p); \
197 if (sizeof(p->s) > sizeof(p->padding)) \
198 { \
199 printf("tstDeviceStructSize: error! padding of %s is too small, padding=%d struct=%d correct=%d\n", #strct, \
200 (int)sizeof(p->padding), (int)sizeof(p->s), (int)RT_ALIGN_Z(sizeof(p->s), 32)); \
201 rc++; \
202 } \
203 } while (0)
204
205/**
206 * Checks that a internal struct padding is big enough.
207 */
208#define CHECK_PADDING3(strct, member, pad_member) \
209 do \
210 { \
211 strct *p = NULL; NOREF(p); \
212 if (sizeof(p->member) > sizeof(p->pad_member)) \
213 { \
214 printf("tstDeviceStructSize: error! padding of %s::%s is too small, padding=%d struct=%d\n", #strct, #member, \
215 (int)sizeof(p->pad_member), (int)sizeof(p->member)); \
216 rc++; \
217 } \
218 } while (0)
219
220/**
221 * Prints the offset of a struct member.
222 */
223#define PRINT_OFFSET(strct, member) \
224 do \
225 { \
226 printf("tstDeviceStructSize: info: %s::%s offset %d sizeof %d\n", #strct, #member, (int)RT_OFFSETOF(strct, member), (int)RT_SIZEOFMEMB(strct, member)); \
227 } while (0)
228
229
230int main()
231{
232 int rc = 0;
233 printf("tstDeviceStructSize: TESTING\n");
234
235 /* Assert sanity */
236 CHECK_SIZE(uint128_t, 128/8);
237 CHECK_SIZE(int128_t, 128/8);
238 CHECK_SIZE(uint64_t, 64/8);
239 CHECK_SIZE(int64_t, 64/8);
240 CHECK_SIZE(uint32_t, 32/8);
241 CHECK_SIZE(int32_t, 32/8);
242 CHECK_SIZE(uint16_t, 16/8);
243 CHECK_SIZE(int16_t, 16/8);
244 CHECK_SIZE(uint8_t, 8/8);
245 CHECK_SIZE(int8_t, 8/8);
246
247 /* Basic alignment checks. */
248 CHECK_MEMBER_ALIGNMENT(PDMDEVINS, achInstanceData, 64);
249 CHECK_MEMBER_ALIGNMENT(PCIDEVICE, Int.s, 16);
250 CHECK_MEMBER_ALIGNMENT(PCIDEVICE, Int.s.aIORegions, 16);
251
252 /*
253 * Misc alignment checks (keep this somewhat alphabetical).
254 */
255 CHECK_MEMBER_ALIGNMENT(AHCI, lock, 8);
256 CHECK_MEMBER_ALIGNMENT(AHCIPort, StatDMA, 8);
257 CHECK_MEMBER_ALIGNMENT(AHCIATACONTROLLER, lock, 8);
258 CHECK_MEMBER_ALIGNMENT(AHCIATACONTROLLER, StatAsyncOps, 8);
259#ifdef VBOX_WITH_STATISTICS
260 CHECK_MEMBER_ALIGNMENT(APICDeviceInfo, StatMMIOReadGC, 8);
261#endif
262 CHECK_MEMBER_ALIGNMENT(ATADevState, cTotalSectors, 8);
263 CHECK_MEMBER_ALIGNMENT(ATADevState, StatATADMA, 8);
264 CHECK_MEMBER_ALIGNMENT(ATADevState, StatReads, 8);
265 CHECK_MEMBER_ALIGNMENT(ATACONTROLLER, lock, 8);
266 CHECK_MEMBER_ALIGNMENT(ATACONTROLLER, StatAsyncOps, 8);
267 CHECK_MEMBER_ALIGNMENT(BUSLOGIC, CritSectIntr, 8);
268 CHECK_MEMBER_ALIGNMENT(DEVPARALLELSTATE, CritSect, 8);
269#ifdef VBOX_WITH_STATISTICS
270 CHECK_MEMBER_ALIGNMENT(DEVPIC, StatSetIrqGC, 8);
271#endif
272#ifdef VBOX_WITH_E1000
273 CHECK_MEMBER_ALIGNMENT(E1KSTATE, cs, 8);
274 CHECK_MEMBER_ALIGNMENT(E1KSTATE, csRx, 8);
275 CHECK_MEMBER_ALIGNMENT(E1KSTATE, StatReceiveBytes, 8);
276#endif
277#ifdef VBOX_WITH_VIRTIO
278 CHECK_MEMBER_ALIGNMENT(VNETSTATE, StatReceiveBytes, 8);
279#endif
280 //CHECK_MEMBER_ALIGNMENT(E1KSTATE, csTx, 8);
281#ifdef VBOX_WITH_USB
282 CHECK_MEMBER_ALIGNMENT(EHCI, RootHub, 8);
283# ifdef VBOX_WITH_STATISTICS
284 CHECK_MEMBER_ALIGNMENT(EHCI, StatCanceledIsocUrbs, 8);
285# endif
286#endif
287 CHECK_MEMBER_ALIGNMENT(E1KSTATE, StatReceiveBytes, 8);
288#ifdef VBOX_WITH_STATISTICS
289 CHECK_MEMBER_ALIGNMENT(IOAPICState, StatMMIOReadGC, 8);
290 CHECK_MEMBER_ALIGNMENT(IOAPICState, StatMMIOReadGC, 8);
291#endif
292 CHECK_MEMBER_ALIGNMENT(KBDState, CritSect, 8);
293 CHECK_MEMBER_ALIGNMENT(LSILOGISCSI, ReplyPostQueueCritSect, 8);
294 CHECK_MEMBER_ALIGNMENT(LSILOGISCSI, ReplyFreeQueueCritSect, 8);
295#ifdef VBOX_WITH_USB
296 CHECK_MEMBER_ALIGNMENT(OHCI, RootHub, 8);
297# ifdef VBOX_WITH_STATISTICS
298 CHECK_MEMBER_ALIGNMENT(OHCI, StatCanceledIsocUrbs, 8);
299# endif
300#endif
301 CHECK_MEMBER_ALIGNMENT(PCIBUS, devices, 16);
302 CHECK_MEMBER_ALIGNMENT(PCIBUS, devices, 16);
303 CHECK_MEMBER_ALIGNMENT(PCIGLOBALS, pci_irq_levels, 16);
304 CHECK_MEMBER_ALIGNMENT(PCNetState, u64LastPoll, 8);
305 CHECK_MEMBER_ALIGNMENT(PCNetState, CritSect, 8);
306 CHECK_MEMBER_ALIGNMENT(PCNetState, StatReceiveBytes, 8);
307#ifdef VBOX_WITH_STATISTICS
308 CHECK_MEMBER_ALIGNMENT(PCNetState, StatMMIOReadRZ, 8);
309#endif
310 CHECK_MEMBER_ALIGNMENT(PITState, StatPITIrq, 8);
311 CHECK_MEMBER_ALIGNMENT(SerialState, CritSect, 8);
312 CHECK_MEMBER_ALIGNMENT(VGASTATE, Dev, 8);
313 CHECK_MEMBER_ALIGNMENT(VGASTATE, lock, 8);
314 CHECK_MEMBER_ALIGNMENT(VGASTATE, StatRZMemoryRead, 8);
315 CHECK_MEMBER_ALIGNMENT(VMMDevState, CritSect, 8);
316#ifdef VBOX_WITH_VIRTIO
317 CHECK_MEMBER_ALIGNMENT(VPCISTATE, cs, 8);
318 CHECK_MEMBER_ALIGNMENT(VPCISTATE, led, 4);
319 CHECK_MEMBER_ALIGNMENT(VPCISTATE, Queues, 8);
320#endif
321
322#ifdef VBOX_WITH_RAW_MODE
323 /*
324 * Compare HC and RC.
325 */
326 printf("tstDeviceStructSize: Comparing HC and RC...\n");
327# include "tstDeviceStructSizeRC.h"
328#endif
329
330 /*
331 * Report result.
332 */
333 if (rc)
334 printf("tstDeviceStructSize: FAILURE - %d errors\n", rc);
335 else
336 printf("tstDeviceStructSize: SUCCESS\n");
337 return rc;
338}
339
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