scsi: esp: check buffer length before reading scsi command
[qemu.git] / scripts / qapi-visit.py
1 #
2 # QAPI visitor generator
3 #
4 # Copyright IBM, Corp. 2011
5 # Copyright (C) 2014-2016 Red Hat, Inc.
6 #
7 # Authors:
8 # Anthony Liguori <aliguori@us.ibm.com>
9 # Michael Roth <mdroth@linux.vnet.ibm.com>
10 # Markus Armbruster <armbru@redhat.com>
11 #
12 # This work is licensed under the terms of the GNU GPL, version 2.
13 # See the COPYING file in the top-level directory.
14
15 from qapi import *
16 import re
17
18
19 def gen_visit_decl(name, scalar=False):
20 c_type = c_name(name) + ' *'
21 if not scalar:
22 c_type += '*'
23 return mcgen('''
24 void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_type)sobj, Error **errp);
25 ''',
26 c_name=c_name(name), c_type=c_type)
27
28
29 def gen_visit_members_decl(name):
30 return mcgen('''
31
32 void visit_type_%(c_name)s_members(Visitor *v, %(c_name)s *obj, Error **errp);
33 ''',
34 c_name=c_name(name))
35
36
37 def gen_visit_object_members(name, base, members, variants):
38 ret = mcgen('''
39
40 void visit_type_%(c_name)s_members(Visitor *v, %(c_name)s *obj, Error **errp)
41 {
42 Error *err = NULL;
43
44 ''',
45 c_name=c_name(name))
46
47 if base:
48 ret += mcgen('''
49 visit_type_%(c_type)s_members(v, (%(c_type)s *)obj, &err);
50 ''',
51 c_type=base.c_name())
52 ret += gen_err_check()
53
54 for memb in members:
55 if memb.optional:
56 ret += mcgen('''
57 if (visit_optional(v, "%(name)s", &obj->has_%(c_name)s)) {
58 ''',
59 name=memb.name, c_name=c_name(memb.name))
60 push_indent()
61 ret += mcgen('''
62 visit_type_%(c_type)s(v, "%(name)s", &obj->%(c_name)s, &err);
63 ''',
64 c_type=memb.type.c_name(), name=memb.name,
65 c_name=c_name(memb.name))
66 ret += gen_err_check()
67 if memb.optional:
68 pop_indent()
69 ret += mcgen('''
70 }
71 ''')
72
73 if variants:
74 ret += mcgen('''
75 switch (obj->%(c_name)s) {
76 ''',
77 c_name=c_name(variants.tag_member.name))
78
79 for var in variants.variants:
80 ret += mcgen('''
81 case %(case)s:
82 visit_type_%(c_type)s_members(v, &obj->u.%(c_name)s, &err);
83 break;
84 ''',
85 case=c_enum_const(variants.tag_member.type.name,
86 var.name,
87 variants.tag_member.type.prefix),
88 c_type=var.type.c_name(), c_name=c_name(var.name))
89
90 ret += mcgen('''
91 default:
92 abort();
93 }
94 ''')
95
96 # 'goto out' produced for base, for each member, and if variants were
97 # present
98 if base or members or variants:
99 ret += mcgen('''
100
101 out:
102 ''')
103 ret += mcgen('''
104 error_propagate(errp, err);
105 }
106 ''')
107 return ret
108
109
110 def gen_visit_list(name, element_type):
111 return mcgen('''
112
113 void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error **errp)
114 {
115 Error *err = NULL;
116 %(c_name)s *tail;
117 size_t size = sizeof(**obj);
118
119 visit_start_list(v, name, (GenericList **)obj, size, &err);
120 if (err) {
121 goto out;
122 }
123
124 for (tail = *obj; tail;
125 tail = (%(c_name)s *)visit_next_list(v, (GenericList *)tail, size)) {
126 visit_type_%(c_elt_type)s(v, NULL, &tail->value, &err);
127 if (err) {
128 break;
129 }
130 }
131
132 visit_end_list(v);
133 if (err && visit_is_input(v)) {
134 qapi_free_%(c_name)s(*obj);
135 *obj = NULL;
136 }
137 out:
138 error_propagate(errp, err);
139 }
140 ''',
141 c_name=c_name(name), c_elt_type=element_type.c_name())
142
143
144 def gen_visit_enum(name):
145 return mcgen('''
146
147 void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s *obj, Error **errp)
148 {
149 int value = *obj;
150 visit_type_enum(v, name, &value, %(c_name)s_lookup, errp);
151 *obj = value;
152 }
153 ''',
154 c_name=c_name(name))
155
156
157 def gen_visit_alternate(name, variants):
158 promote_int = 'true'
159 ret = ''
160 for var in variants.variants:
161 if var.type.alternate_qtype() == 'QTYPE_QINT':
162 promote_int = 'false'
163
164 ret += mcgen('''
165
166 void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error **errp)
167 {
168 Error *err = NULL;
169
170 visit_start_alternate(v, name, (GenericAlternate **)obj, sizeof(**obj),
171 %(promote_int)s, &err);
172 if (err) {
173 goto out;
174 }
175 switch ((*obj)->type) {
176 ''',
177 c_name=c_name(name), promote_int=promote_int)
178
179 for var in variants.variants:
180 ret += mcgen('''
181 case %(case)s:
182 ''',
183 case=var.type.alternate_qtype())
184 if isinstance(var.type, QAPISchemaObjectType):
185 ret += mcgen('''
186 visit_start_struct(v, name, NULL, 0, &err);
187 if (err) {
188 break;
189 }
190 visit_type_%(c_type)s_members(v, &(*obj)->u.%(c_name)s, &err);
191 if (!err) {
192 visit_check_struct(v, &err);
193 }
194 visit_end_struct(v);
195 ''',
196 c_type=var.type.c_name(),
197 c_name=c_name(var.name))
198 else:
199 ret += mcgen('''
200 visit_type_%(c_type)s(v, name, &(*obj)->u.%(c_name)s, &err);
201 ''',
202 c_type=var.type.c_name(),
203 c_name=c_name(var.name))
204 ret += mcgen('''
205 break;
206 ''')
207
208 ret += mcgen('''
209 default:
210 error_setg(&err, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
211 "%(name)s");
212 }
213 visit_end_alternate(v);
214 if (err && visit_is_input(v)) {
215 qapi_free_%(c_name)s(*obj);
216 *obj = NULL;
217 }
218 out:
219 error_propagate(errp, err);
220 }
221 ''',
222 name=name, c_name=c_name(name))
223
224 return ret
225
226
227 def gen_visit_object(name, base, members, variants):
228 return mcgen('''
229
230 void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error **errp)
231 {
232 Error *err = NULL;
233
234 visit_start_struct(v, name, (void **)obj, sizeof(%(c_name)s), &err);
235 if (err) {
236 goto out;
237 }
238 if (!*obj) {
239 goto out_obj;
240 }
241 visit_type_%(c_name)s_members(v, *obj, &err);
242 if (err) {
243 goto out_obj;
244 }
245 visit_check_struct(v, &err);
246 out_obj:
247 visit_end_struct(v);
248 if (err && visit_is_input(v)) {
249 qapi_free_%(c_name)s(*obj);
250 *obj = NULL;
251 }
252 out:
253 error_propagate(errp, err);
254 }
255 ''',
256 c_name=c_name(name))
257
258
259 class QAPISchemaGenVisitVisitor(QAPISchemaVisitor):
260 def __init__(self):
261 self.decl = None
262 self.defn = None
263 self._btin = None
264
265 def visit_begin(self, schema):
266 self.decl = ''
267 self.defn = ''
268 self._btin = guardstart('QAPI_VISIT_BUILTIN')
269
270 def visit_end(self):
271 # To avoid header dependency hell, we always generate
272 # declarations for built-in types in our header files and
273 # simply guard them. See also do_builtins (command line
274 # option -b).
275 self._btin += guardend('QAPI_VISIT_BUILTIN')
276 self.decl = self._btin + self.decl
277 self._btin = None
278
279 def visit_enum_type(self, name, info, values, prefix):
280 # Special case for our lone builtin enum type
281 # TODO use something cleaner than existence of info
282 if not info:
283 self._btin += gen_visit_decl(name, scalar=True)
284 if do_builtins:
285 self.defn += gen_visit_enum(name)
286 else:
287 self.decl += gen_visit_decl(name, scalar=True)
288 self.defn += gen_visit_enum(name)
289
290 def visit_array_type(self, name, info, element_type):
291 decl = gen_visit_decl(name)
292 defn = gen_visit_list(name, element_type)
293 if isinstance(element_type, QAPISchemaBuiltinType):
294 self._btin += decl
295 if do_builtins:
296 self.defn += defn
297 else:
298 self.decl += decl
299 self.defn += defn
300
301 def visit_object_type(self, name, info, base, members, variants):
302 # Nothing to do for the special empty builtin
303 if name == 'q_empty':
304 return
305 self.decl += gen_visit_members_decl(name)
306 self.defn += gen_visit_object_members(name, base, members, variants)
307 # TODO Worth changing the visitor signature, so we could
308 # directly use rather than repeat type.is_implicit()?
309 if not name.startswith('q_'):
310 # only explicit types need an allocating visit
311 self.decl += gen_visit_decl(name)
312 self.defn += gen_visit_object(name, base, members, variants)
313
314 def visit_alternate_type(self, name, info, variants):
315 self.decl += gen_visit_decl(name)
316 self.defn += gen_visit_alternate(name, variants)
317
318 # If you link code generated from multiple schemata, you want only one
319 # instance of the code for built-in types. Generate it only when
320 # do_builtins, enabled by command line option -b. See also
321 # QAPISchemaGenVisitVisitor.visit_end().
322 do_builtins = False
323
324 (input_file, output_dir, do_c, do_h, prefix, opts) = \
325 parse_command_line("b", ["builtins"])
326
327 for o, a in opts:
328 if o in ("-b", "--builtins"):
329 do_builtins = True
330
331 c_comment = '''
332 /*
333 * schema-defined QAPI visitor functions
334 *
335 * Copyright IBM, Corp. 2011
336 *
337 * Authors:
338 * Anthony Liguori <aliguori@us.ibm.com>
339 *
340 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
341 * See the COPYING.LIB file in the top-level directory.
342 *
343 */
344 '''
345 h_comment = '''
346 /*
347 * schema-defined QAPI visitor functions
348 *
349 * Copyright IBM, Corp. 2011
350 *
351 * Authors:
352 * Anthony Liguori <aliguori@us.ibm.com>
353 *
354 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
355 * See the COPYING.LIB file in the top-level directory.
356 *
357 */
358 '''
359
360 (fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix,
361 'qapi-visit.c', 'qapi-visit.h',
362 c_comment, h_comment)
363
364 fdef.write(mcgen('''
365 #include "qemu/osdep.h"
366 #include "qemu-common.h"
367 #include "qapi/error.h"
368 #include "%(prefix)sqapi-visit.h"
369 ''',
370 prefix=prefix))
371
372 fdecl.write(mcgen('''
373 #include "qapi/visitor.h"
374 #include "qapi/qmp/qerror.h"
375 #include "%(prefix)sqapi-types.h"
376
377 ''',
378 prefix=prefix))
379
380 schema = QAPISchema(input_file)
381 gen = QAPISchemaGenVisitVisitor()
382 schema.visit(gen)
383 fdef.write(gen.defn)
384 fdecl.write(gen.decl)
385
386 close_output(fdef, fdecl)