scsi: esp: check buffer length before reading scsi command
[qemu.git] / scripts / qapi-types.py
1 #
2 # QAPI types generator
3 #
4 # Copyright IBM, Corp. 2011
5 # Copyright (c) 2013-2016 Red Hat Inc.
6 #
7 # Authors:
8 # Anthony Liguori <aliguori@us.ibm.com>
9 # Markus Armbruster <armbru@redhat.com>
10 #
11 # This work is licensed under the terms of the GNU GPL, version 2.
12 # See the COPYING file in the top-level directory.
13
14 from qapi import *
15
16
17 # variants must be emitted before their container; track what has already
18 # been output
19 objects_seen = set()
20
21
22 def gen_fwd_object_or_array(name):
23 return mcgen('''
24
25 typedef struct %(c_name)s %(c_name)s;
26 ''',
27 c_name=c_name(name))
28
29
30 def gen_array(name, element_type):
31 return mcgen('''
32
33 struct %(c_name)s {
34 %(c_name)s *next;
35 %(c_type)s value;
36 };
37 ''',
38 c_name=c_name(name), c_type=element_type.c_type())
39
40
41 def gen_struct_members(members):
42 ret = ''
43 for memb in members:
44 if memb.optional:
45 ret += mcgen('''
46 bool has_%(c_name)s;
47 ''',
48 c_name=c_name(memb.name))
49 ret += mcgen('''
50 %(c_type)s %(c_name)s;
51 ''',
52 c_type=memb.type.c_type(), c_name=c_name(memb.name))
53 return ret
54
55
56 def gen_object(name, base, members, variants):
57 if name in objects_seen:
58 return ''
59 objects_seen.add(name)
60
61 ret = ''
62 if variants:
63 for v in variants.variants:
64 if isinstance(v.type, QAPISchemaObjectType):
65 ret += gen_object(v.type.name, v.type.base,
66 v.type.local_members, v.type.variants)
67
68 ret += mcgen('''
69
70 struct %(c_name)s {
71 ''',
72 c_name=c_name(name))
73
74 if base:
75 if not base.is_implicit():
76 ret += mcgen('''
77 /* Members inherited from %(c_name)s: */
78 ''',
79 c_name=base.c_name())
80 ret += gen_struct_members(base.members)
81 if not base.is_implicit():
82 ret += mcgen('''
83 /* Own members: */
84 ''')
85 ret += gen_struct_members(members)
86
87 if variants:
88 ret += gen_variants(variants)
89
90 # Make sure that all structs have at least one member; this avoids
91 # potential issues with attempting to malloc space for zero-length
92 # structs in C, and also incompatibility with C++ (where an empty
93 # struct is size 1).
94 if not (base and base.members) and not members and not variants:
95 ret += mcgen('''
96 char qapi_dummy_for_empty_struct;
97 ''')
98
99 ret += mcgen('''
100 };
101 ''')
102
103 return ret
104
105
106 def gen_upcast(name, base):
107 # C makes const-correctness ugly. We have to cast away const to let
108 # this function work for both const and non-const obj.
109 return mcgen('''
110
111 static inline %(base)s *qapi_%(c_name)s_base(const %(c_name)s *obj)
112 {
113 return (%(base)s *)obj;
114 }
115 ''',
116 c_name=c_name(name), base=base.c_name())
117
118
119 def gen_variants(variants):
120 ret = mcgen('''
121 union { /* union tag is @%(c_name)s */
122 ''',
123 c_name=c_name(variants.tag_member.name))
124
125 for var in variants.variants:
126 ret += mcgen('''
127 %(c_type)s %(c_name)s;
128 ''',
129 c_type=var.type.c_unboxed_type(),
130 c_name=c_name(var.name))
131
132 ret += mcgen('''
133 } u;
134 ''')
135
136 return ret
137
138
139 def gen_type_cleanup_decl(name):
140 ret = mcgen('''
141
142 void qapi_free_%(c_name)s(%(c_name)s *obj);
143 ''',
144 c_name=c_name(name))
145 return ret
146
147
148 def gen_type_cleanup(name):
149 ret = mcgen('''
150
151 void qapi_free_%(c_name)s(%(c_name)s *obj)
152 {
153 QapiDeallocVisitor *qdv;
154 Visitor *v;
155
156 if (!obj) {
157 return;
158 }
159
160 qdv = qapi_dealloc_visitor_new();
161 v = qapi_dealloc_get_visitor(qdv);
162 visit_type_%(c_name)s(v, NULL, &obj, NULL);
163 qapi_dealloc_visitor_cleanup(qdv);
164 }
165 ''',
166 c_name=c_name(name))
167 return ret
168
169
170 class QAPISchemaGenTypeVisitor(QAPISchemaVisitor):
171 def __init__(self):
172 self.decl = None
173 self.defn = None
174 self._fwdecl = None
175 self._btin = None
176
177 def visit_begin(self, schema):
178 # gen_object() is recursive, ensure it doesn't visit the empty type
179 objects_seen.add(schema.the_empty_object_type.name)
180 self.decl = ''
181 self.defn = ''
182 self._fwdecl = ''
183 self._btin = guardstart('QAPI_TYPES_BUILTIN')
184
185 def visit_end(self):
186 self.decl = self._fwdecl + self.decl
187 self._fwdecl = None
188 # To avoid header dependency hell, we always generate
189 # declarations for built-in types in our header files and
190 # simply guard them. See also do_builtins (command line
191 # option -b).
192 self._btin += guardend('QAPI_TYPES_BUILTIN')
193 self.decl = self._btin + self.decl
194 self._btin = None
195
196 def _gen_type_cleanup(self, name):
197 self.decl += gen_type_cleanup_decl(name)
198 self.defn += gen_type_cleanup(name)
199
200 def visit_enum_type(self, name, info, values, prefix):
201 # Special case for our lone builtin enum type
202 # TODO use something cleaner than existence of info
203 if not info:
204 self._btin += gen_enum(name, values, prefix)
205 if do_builtins:
206 self.defn += gen_enum_lookup(name, values, prefix)
207 else:
208 self._fwdecl += gen_enum(name, values, prefix)
209 self.defn += gen_enum_lookup(name, values, prefix)
210
211 def visit_array_type(self, name, info, element_type):
212 if isinstance(element_type, QAPISchemaBuiltinType):
213 self._btin += gen_fwd_object_or_array(name)
214 self._btin += gen_array(name, element_type)
215 self._btin += gen_type_cleanup_decl(name)
216 if do_builtins:
217 self.defn += gen_type_cleanup(name)
218 else:
219 self._fwdecl += gen_fwd_object_or_array(name)
220 self.decl += gen_array(name, element_type)
221 self._gen_type_cleanup(name)
222
223 def visit_object_type(self, name, info, base, members, variants):
224 # Nothing to do for the special empty builtin
225 if name == 'q_empty':
226 return
227 self._fwdecl += gen_fwd_object_or_array(name)
228 self.decl += gen_object(name, base, members, variants)
229 if base and not base.is_implicit():
230 self.decl += gen_upcast(name, base)
231 # TODO Worth changing the visitor signature, so we could
232 # directly use rather than repeat type.is_implicit()?
233 if not name.startswith('q_'):
234 # implicit types won't be directly allocated/freed
235 self._gen_type_cleanup(name)
236
237 def visit_alternate_type(self, name, info, variants):
238 self._fwdecl += gen_fwd_object_or_array(name)
239 self.decl += gen_object(name, None, [variants.tag_member], variants)
240 self._gen_type_cleanup(name)
241
242 # If you link code generated from multiple schemata, you want only one
243 # instance of the code for built-in types. Generate it only when
244 # do_builtins, enabled by command line option -b. See also
245 # QAPISchemaGenTypeVisitor.visit_end().
246 do_builtins = False
247
248 (input_file, output_dir, do_c, do_h, prefix, opts) = \
249 parse_command_line("b", ["builtins"])
250
251 for o, a in opts:
252 if o in ("-b", "--builtins"):
253 do_builtins = True
254
255 c_comment = '''
256 /*
257 * deallocation functions for schema-defined QAPI types
258 *
259 * Copyright IBM, Corp. 2011
260 *
261 * Authors:
262 * Anthony Liguori <aliguori@us.ibm.com>
263 * Michael Roth <mdroth@linux.vnet.ibm.com>
264 *
265 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
266 * See the COPYING.LIB file in the top-level directory.
267 *
268 */
269 '''
270 h_comment = '''
271 /*
272 * schema-defined QAPI types
273 *
274 * Copyright IBM, Corp. 2011
275 *
276 * Authors:
277 * Anthony Liguori <aliguori@us.ibm.com>
278 *
279 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
280 * See the COPYING.LIB file in the top-level directory.
281 *
282 */
283 '''
284
285 (fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix,
286 'qapi-types.c', 'qapi-types.h',
287 c_comment, h_comment)
288
289 fdef.write(mcgen('''
290 #include "qemu/osdep.h"
291 #include "qapi/dealloc-visitor.h"
292 #include "%(prefix)sqapi-types.h"
293 #include "%(prefix)sqapi-visit.h"
294 ''',
295 prefix=prefix))
296
297 schema = QAPISchema(input_file)
298 gen = QAPISchemaGenTypeVisitor()
299 schema.visit(gen)
300 fdef.write(gen.defn)
301 fdecl.write(gen.decl)
302
303 close_output(fdef, fdecl)