1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
|
#! /usr/bin/python
import os.path
import sys
import re
macros = {}
anyWarnings = False
types = {}
types['char'] = {"size": 1, "alignment": 1}
types['uint8_t'] = {"size": 1, "alignment": 1}
types['ovs_be16'] = {"size": 2, "alignment": 2}
types['ovs_be32'] = {"size": 4, "alignment": 4}
types['ovs_be64'] = {"size": 8, "alignment": 8}
types['ovs_32aligned_be64'] = {"size": 8, "alignment": 4}
types['struct eth_addr'] = {"size": 6, "alignment": 2}
types['struct eth_addr64'] = {"size": 8, "alignment": 2}
token = None
line = ""
idRe = "[a-zA-Z_][a-zA-Z_0-9]*"
tokenRe = "#?" + idRe + "|[0-9]+|."
includeRe = re.compile(r'\s*#include\s+<(openflow/[^#]+)>')
includePath = ''
inComment = False
inDirective = False
inputStack = []
def getToken():
global token
global line
global inComment
global inDirective
global inputFile
global fileName
while True:
line = line.lstrip()
if line != "":
if line.startswith("/*"):
inComment = True
line = line[2:]
elif inComment:
commentEnd = line.find("*/")
if commentEnd < 0:
line = ""
else:
inComment = False
line = line[commentEnd + 2:]
else:
match = re.match(tokenRe, line)
token = match.group(0)
line = line[len(token):]
if token.startswith('#'):
inDirective = True
elif token in macros and not inDirective:
line = macros[token] + line
continue
return True
elif inDirective:
token = "$"
inDirective = False
return True
else:
global lineNumber
while True:
line = inputFile.readline()
lineNumber += 1
while line.endswith("\\\n"):
line = line[:-2] + inputFile.readline()
lineNumber += 1
match = includeRe.match(line)
if match:
inputStack.append((fileName, inputFile, lineNumber))
inputFile = open(includePath + match.group(1))
lineNumber = 0
continue
if line == "":
if inputStack:
fileName, inputFile, lineNumber = inputStack.pop()
continue
if token == None:
fatal("unexpected end of input")
token = None
return False
break
def fatal(msg):
sys.stderr.write("%s:%d: error at \"%s\": %s\n" % (fileName, lineNumber, token, msg))
sys.exit(1)
def warn(msg):
global anyWarnings
anyWarnings = True
sys.stderr.write("%s:%d: warning: %s\n" % (fileName, lineNumber, msg))
def skipDirective():
getToken()
while token != '$':
getToken()
def isId(s):
return re.match(idRe + "$", s) != None
def forceId():
if not isId(token):
fatal("identifier expected")
def forceInteger():
if not re.match('[0-9]+$', token):
fatal("integer expected")
def match(t):
if token == t:
getToken()
return True
else:
return False
def forceMatch(t):
if not match(t):
fatal("%s expected" % t)
def parseTaggedName():
assert token in ('struct', 'union')
name = token
getToken()
forceId()
name = "%s %s" % (name, token)
getToken()
return name
def parseTypeName():
if token in ('struct', 'union'):
name = parseTaggedName()
elif isId(token):
name = token
getToken()
else:
fatal("type name expected")
if name in types:
return name
else:
fatal("unknown type \"%s\"" % name)
def parseStruct():
isStruct = token == 'struct'
structName = parseTaggedName()
if token == ";":
return
ofs = size = 0
alignment = 4 # ARM has minimum 32-bit alignment
forceMatch('{')
while not match('}'):
typeName = parseTypeName()
typeSize = types[typeName]['size']
typeAlignment = types[typeName]['alignment']
forceId()
memberName = token
getToken()
if match('['):
if token == ']':
count = 0
else:
forceInteger()
count = int(token)
getToken()
forceMatch(']')
else:
count = 1
nBytes = typeSize * count
if isStruct:
if ofs % typeAlignment:
shortage = typeAlignment - (ofs % typeAlignment)
warn("%s member %s is %d bytes short of %d-byte alignment"
% (structName, memberName, shortage, typeAlignment))
size += shortage
ofs += shortage
size += nBytes
ofs += nBytes
else:
if nBytes > size:
size = nBytes
if typeAlignment > alignment:
alignment = typeAlignment
forceMatch(';')
if size % alignment:
shortage = alignment - (size % alignment)
if (structName == "struct ofp10_packet_in" and
shortage == 2 and
memberName == 'data' and
count == 0):
# This is intentional
pass
else:
warn("%s needs %d bytes of tail padding" % (structName, shortage))
size += shortage
types[structName] = {"size": size, "alignment": alignment}
return structName
def checkStructs():
if len(sys.argv) < 2:
sys.stderr.write("at least one non-option argument required; "
"use --help for help")
sys.exit(1)
if '--help' in sys.argv:
argv0 = os.path.basename(sys.argv[0])
print('''\
%(argv0)s, for checking struct and struct member alignment
usage: %(argv0)s -Ipath HEADER [HEADER]...
This program reads the header files specified on the command line and
verifies that all struct members are aligned on natural boundaries
without any need for the compiler to add additional padding. It also
verifies that each struct's size is a multiple of 32 bits (because
some ABIs for ARM require all structs to be a multiple of 32 bits), or
64 bits if the struct has any 64-bit members, again without the
compiler adding additional padding. Finally, it checks struct size
assertions using OFP_ASSERT.
This program is specialized for reading Open vSwitch's OpenFlow header
files. It will not work on arbitrary header files without extensions.\
''' % {"argv0": argv0})
sys.exit(0)
global fileName
for fileName in sys.argv[1:]:
if fileName.startswith('-I'):
global includePath
includePath = fileName[2:]
if not includePath.endswith('/'):
includePath += '/'
continue
global inputFile
global lineNumber
inputFile = open(fileName)
lineNumber = 0
lastStruct = None
while getToken():
if token in ("#ifdef", "#ifndef", "#include",
"#endif", "#elif", "#else"):
skipDirective()
elif token == "#define":
getToken()
name = token
if line.startswith('('):
skipDirective()
else:
definition = ""
getToken()
while token != '$':
definition += token
getToken()
macros[name] = definition
elif token == "enum":
while token != ';':
getToken()
elif token in ('struct', 'union'):
lastStruct = parseStruct()
elif match('OFP_ASSERT') or match('BOOST_STATIC_ASSERT'):
forceMatch('(')
forceMatch('sizeof')
forceMatch('(')
typeName = parseTypeName()
if typeName != lastStruct:
warn("checking size of %s but %s was most recently defined"
% (typeName, lastStruct))
forceMatch(')')
forceMatch('=')
forceMatch('=')
forceInteger()
size = int(token)
getToken()
forceMatch(')')
if types[typeName]['size'] != size:
warn("%s is %d bytes long but declared as %d" % (
typeName, types[typeName]['size'], size))
else:
fatal("parse error")
inputFile.close()
if anyWarnings:
sys.exit(1)
if __name__ == '__main__':
checkStructs()
|