2011-07-19 21:50:42 +02:00
|
|
|
#
|
|
|
|
# QAPI command marshaller generator
|
|
|
|
#
|
|
|
|
# Copyright IBM, Corp. 2011
|
qapi: Replace uncommon use of the error API by the common one
We commonly use the error API like this:
err = NULL;
foo(..., &err);
if (err) {
goto out;
}
bar(..., &err);
Every error source is checked separately. The second function is only
called when the first one succeeds. Both functions are free to pass
their argument to error_set(). Because error_set() asserts no error
has been set, this effectively means they must not be called with an
error set.
The qapi-generated code uses the error API differently:
// *errp was initialized to NULL somewhere up the call chain
frob(..., errp);
gnat(..., errp);
Errors accumulate in *errp: first error wins, subsequent errors get
dropped. To make this work, the second function does nothing when
called with an error set. Requires non-null errp, or else the second
function can't see the first one fail.
This usage has also bled into visitor tests, and two device model
object property getters rtc_get_date() and balloon_stats_get_all().
With the "accumulate" technique, you need fewer error checks in
callers, and buy that with an error check in every callee. Can be
nice.
However, mixing the two techniques is confusing. You can't use the
"accumulate" technique with functions designed for the "check
separately" technique. You can use the "check separately" technique
with functions designed for the "accumulate" technique, but then
error_set() can't catch you setting an error more than once.
Standardize on the "check separately" technique for now, because it's
overwhelmingly prevalent.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2014-05-07 09:53:54 +02:00
|
|
|
# Copyright (C) 2014 Red Hat, Inc.
|
2011-07-19 21:50:42 +02:00
|
|
|
#
|
|
|
|
# Authors:
|
|
|
|
# Anthony Liguori <aliguori@us.ibm.com>
|
|
|
|
# Michael Roth <mdroth@linux.vnet.ibm.com>
|
qapi: Replace uncommon use of the error API by the common one
We commonly use the error API like this:
err = NULL;
foo(..., &err);
if (err) {
goto out;
}
bar(..., &err);
Every error source is checked separately. The second function is only
called when the first one succeeds. Both functions are free to pass
their argument to error_set(). Because error_set() asserts no error
has been set, this effectively means they must not be called with an
error set.
The qapi-generated code uses the error API differently:
// *errp was initialized to NULL somewhere up the call chain
frob(..., errp);
gnat(..., errp);
Errors accumulate in *errp: first error wins, subsequent errors get
dropped. To make this work, the second function does nothing when
called with an error set. Requires non-null errp, or else the second
function can't see the first one fail.
This usage has also bled into visitor tests, and two device model
object property getters rtc_get_date() and balloon_stats_get_all().
With the "accumulate" technique, you need fewer error checks in
callers, and buy that with an error check in every callee. Can be
nice.
However, mixing the two techniques is confusing. You can't use the
"accumulate" technique with functions designed for the "check
separately" technique. You can use the "check separately" technique
with functions designed for the "accumulate" technique, but then
error_set() can't catch you setting an error more than once.
Standardize on the "check separately" technique for now, because it's
overwhelmingly prevalent.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2014-05-07 09:53:54 +02:00
|
|
|
# Markus Armbruster <armbru@redhat.com>
|
2011-07-19 21:50:42 +02:00
|
|
|
#
|
2014-03-01 08:40:34 +01:00
|
|
|
# This work is licensed under the terms of the GNU GPL, version 2.
|
|
|
|
# See the COPYING file in the top-level directory.
|
2011-07-19 21:50:42 +02:00
|
|
|
|
|
|
|
from ordereddict import OrderedDict
|
|
|
|
from qapi import *
|
qapi: Replace uncommon use of the error API by the common one
We commonly use the error API like this:
err = NULL;
foo(..., &err);
if (err) {
goto out;
}
bar(..., &err);
Every error source is checked separately. The second function is only
called when the first one succeeds. Both functions are free to pass
their argument to error_set(). Because error_set() asserts no error
has been set, this effectively means they must not be called with an
error set.
The qapi-generated code uses the error API differently:
// *errp was initialized to NULL somewhere up the call chain
frob(..., errp);
gnat(..., errp);
Errors accumulate in *errp: first error wins, subsequent errors get
dropped. To make this work, the second function does nothing when
called with an error set. Requires non-null errp, or else the second
function can't see the first one fail.
This usage has also bled into visitor tests, and two device model
object property getters rtc_get_date() and balloon_stats_get_all().
With the "accumulate" technique, you need fewer error checks in
callers, and buy that with an error check in every callee. Can be
nice.
However, mixing the two techniques is confusing. You can't use the
"accumulate" technique with functions designed for the "check
separately" technique. You can use the "check separately" technique
with functions designed for the "accumulate" technique, but then
error_set() can't catch you setting an error more than once.
Standardize on the "check separately" technique for now, because it's
overwhelmingly prevalent.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2014-05-07 09:53:54 +02:00
|
|
|
import re
|
2011-07-19 21:50:42 +02:00
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import getopt
|
|
|
|
import errno
|
|
|
|
|
2011-09-14 21:30:00 +02:00
|
|
|
def type_visitor(name):
|
|
|
|
if type(name) == list:
|
|
|
|
return 'visit_type_%sList' % name[0]
|
|
|
|
else:
|
|
|
|
return 'visit_type_%s' % name
|
|
|
|
|
2011-07-19 21:50:42 +02:00
|
|
|
def generate_command_decl(name, args, ret_type):
|
|
|
|
arglist=""
|
|
|
|
for argname, argtype, optional, structured in parse_args(args):
|
2014-06-10 13:25:52 +02:00
|
|
|
argtype = c_type(argtype, is_param=True)
|
2011-07-19 21:50:42 +02:00
|
|
|
if optional:
|
|
|
|
arglist += "bool has_%s, " % c_var(argname)
|
|
|
|
arglist += "%s %s, " % (argtype, c_var(argname))
|
|
|
|
return mcgen('''
|
|
|
|
%(ret_type)s qmp_%(name)s(%(args)sError **errp);
|
|
|
|
''',
|
2012-03-20 14:54:35 +01:00
|
|
|
ret_type=c_type(ret_type), name=c_fun(name), args=arglist).strip()
|
2011-07-19 21:50:42 +02:00
|
|
|
|
qapi: Replace uncommon use of the error API by the common one
We commonly use the error API like this:
err = NULL;
foo(..., &err);
if (err) {
goto out;
}
bar(..., &err);
Every error source is checked separately. The second function is only
called when the first one succeeds. Both functions are free to pass
their argument to error_set(). Because error_set() asserts no error
has been set, this effectively means they must not be called with an
error set.
The qapi-generated code uses the error API differently:
// *errp was initialized to NULL somewhere up the call chain
frob(..., errp);
gnat(..., errp);
Errors accumulate in *errp: first error wins, subsequent errors get
dropped. To make this work, the second function does nothing when
called with an error set. Requires non-null errp, or else the second
function can't see the first one fail.
This usage has also bled into visitor tests, and two device model
object property getters rtc_get_date() and balloon_stats_get_all().
With the "accumulate" technique, you need fewer error checks in
callers, and buy that with an error check in every callee. Can be
nice.
However, mixing the two techniques is confusing. You can't use the
"accumulate" technique with functions designed for the "check
separately" technique. You can use the "check separately" technique
with functions designed for the "accumulate" technique, but then
error_set() can't catch you setting an error more than once.
Standardize on the "check separately" technique for now, because it's
overwhelmingly prevalent.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2014-05-07 09:53:54 +02:00
|
|
|
def gen_err_check(errvar):
|
|
|
|
if errvar:
|
|
|
|
return mcgen('''
|
|
|
|
if (local_err) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
''')
|
|
|
|
return ''
|
|
|
|
|
2011-07-19 21:50:42 +02:00
|
|
|
def gen_sync_call(name, args, ret_type, indent=0):
|
|
|
|
ret = ""
|
|
|
|
arglist=""
|
|
|
|
retval=""
|
|
|
|
if ret_type:
|
|
|
|
retval = "retval = "
|
|
|
|
for argname, argtype, optional, structured in parse_args(args):
|
|
|
|
if optional:
|
|
|
|
arglist += "has_%s, " % c_var(argname)
|
|
|
|
arglist += "%s, " % (c_var(argname))
|
|
|
|
push_indent(indent)
|
|
|
|
ret = mcgen('''
|
qapi: Replace uncommon use of the error API by the common one
We commonly use the error API like this:
err = NULL;
foo(..., &err);
if (err) {
goto out;
}
bar(..., &err);
Every error source is checked separately. The second function is only
called when the first one succeeds. Both functions are free to pass
their argument to error_set(). Because error_set() asserts no error
has been set, this effectively means they must not be called with an
error set.
The qapi-generated code uses the error API differently:
// *errp was initialized to NULL somewhere up the call chain
frob(..., errp);
gnat(..., errp);
Errors accumulate in *errp: first error wins, subsequent errors get
dropped. To make this work, the second function does nothing when
called with an error set. Requires non-null errp, or else the second
function can't see the first one fail.
This usage has also bled into visitor tests, and two device model
object property getters rtc_get_date() and balloon_stats_get_all().
With the "accumulate" technique, you need fewer error checks in
callers, and buy that with an error check in every callee. Can be
nice.
However, mixing the two techniques is confusing. You can't use the
"accumulate" technique with functions designed for the "check
separately" technique. You can use the "check separately" technique
with functions designed for the "accumulate" technique, but then
error_set() can't catch you setting an error more than once.
Standardize on the "check separately" technique for now, because it's
overwhelmingly prevalent.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2014-05-07 09:53:54 +02:00
|
|
|
%(retval)sqmp_%(name)s(%(args)s&local_err);
|
2011-07-19 21:50:42 +02:00
|
|
|
|
|
|
|
''',
|
2012-03-20 14:54:35 +01:00
|
|
|
name=c_fun(name), args=arglist, retval=retval).rstrip()
|
2011-07-19 21:50:42 +02:00
|
|
|
if ret_type:
|
qapi: Replace uncommon use of the error API by the common one
We commonly use the error API like this:
err = NULL;
foo(..., &err);
if (err) {
goto out;
}
bar(..., &err);
Every error source is checked separately. The second function is only
called when the first one succeeds. Both functions are free to pass
their argument to error_set(). Because error_set() asserts no error
has been set, this effectively means they must not be called with an
error set.
The qapi-generated code uses the error API differently:
// *errp was initialized to NULL somewhere up the call chain
frob(..., errp);
gnat(..., errp);
Errors accumulate in *errp: first error wins, subsequent errors get
dropped. To make this work, the second function does nothing when
called with an error set. Requires non-null errp, or else the second
function can't see the first one fail.
This usage has also bled into visitor tests, and two device model
object property getters rtc_get_date() and balloon_stats_get_all().
With the "accumulate" technique, you need fewer error checks in
callers, and buy that with an error check in every callee. Can be
nice.
However, mixing the two techniques is confusing. You can't use the
"accumulate" technique with functions designed for the "check
separately" technique. You can use the "check separately" technique
with functions designed for the "accumulate" technique, but then
error_set() can't catch you setting an error more than once.
Standardize on the "check separately" technique for now, because it's
overwhelmingly prevalent.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2014-05-07 09:53:54 +02:00
|
|
|
ret += "\n" + gen_err_check('local_err')
|
2011-07-19 21:50:42 +02:00
|
|
|
ret += "\n" + mcgen(''''
|
qapi: Replace uncommon use of the error API by the common one
We commonly use the error API like this:
err = NULL;
foo(..., &err);
if (err) {
goto out;
}
bar(..., &err);
Every error source is checked separately. The second function is only
called when the first one succeeds. Both functions are free to pass
their argument to error_set(). Because error_set() asserts no error
has been set, this effectively means they must not be called with an
error set.
The qapi-generated code uses the error API differently:
// *errp was initialized to NULL somewhere up the call chain
frob(..., errp);
gnat(..., errp);
Errors accumulate in *errp: first error wins, subsequent errors get
dropped. To make this work, the second function does nothing when
called with an error set. Requires non-null errp, or else the second
function can't see the first one fail.
This usage has also bled into visitor tests, and two device model
object property getters rtc_get_date() and balloon_stats_get_all().
With the "accumulate" technique, you need fewer error checks in
callers, and buy that with an error check in every callee. Can be
nice.
However, mixing the two techniques is confusing. You can't use the
"accumulate" technique with functions designed for the "check
separately" technique. You can use the "check separately" technique
with functions designed for the "accumulate" technique, but then
error_set() can't catch you setting an error more than once.
Standardize on the "check separately" technique for now, because it's
overwhelmingly prevalent.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2014-05-07 09:53:54 +02:00
|
|
|
%(marshal_output_call)s
|
2011-07-19 21:50:42 +02:00
|
|
|
''',
|
|
|
|
marshal_output_call=gen_marshal_output_call(name, ret_type)).rstrip()
|
|
|
|
pop_indent(indent)
|
|
|
|
return ret.rstrip()
|
|
|
|
|
|
|
|
|
|
|
|
def gen_marshal_output_call(name, ret_type):
|
|
|
|
if not ret_type:
|
|
|
|
return ""
|
qapi: Replace uncommon use of the error API by the common one
We commonly use the error API like this:
err = NULL;
foo(..., &err);
if (err) {
goto out;
}
bar(..., &err);
Every error source is checked separately. The second function is only
called when the first one succeeds. Both functions are free to pass
their argument to error_set(). Because error_set() asserts no error
has been set, this effectively means they must not be called with an
error set.
The qapi-generated code uses the error API differently:
// *errp was initialized to NULL somewhere up the call chain
frob(..., errp);
gnat(..., errp);
Errors accumulate in *errp: first error wins, subsequent errors get
dropped. To make this work, the second function does nothing when
called with an error set. Requires non-null errp, or else the second
function can't see the first one fail.
This usage has also bled into visitor tests, and two device model
object property getters rtc_get_date() and balloon_stats_get_all().
With the "accumulate" technique, you need fewer error checks in
callers, and buy that with an error check in every callee. Can be
nice.
However, mixing the two techniques is confusing. You can't use the
"accumulate" technique with functions designed for the "check
separately" technique. You can use the "check separately" technique
with functions designed for the "accumulate" technique, but then
error_set() can't catch you setting an error more than once.
Standardize on the "check separately" technique for now, because it's
overwhelmingly prevalent.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2014-05-07 09:53:54 +02:00
|
|
|
return "qmp_marshal_output_%s(retval, ret, &local_err);" % c_fun(name)
|
2011-07-19 21:50:42 +02:00
|
|
|
|
2014-05-07 09:53:44 +02:00
|
|
|
def gen_visitor_input_containers_decl(args, obj):
|
2011-07-19 21:50:42 +02:00
|
|
|
ret = ""
|
|
|
|
|
|
|
|
push_indent()
|
|
|
|
if len(args) > 0:
|
|
|
|
ret += mcgen('''
|
2014-05-07 09:53:44 +02:00
|
|
|
QmpInputVisitor *mi = qmp_input_visitor_new_strict(%(obj)s);
|
2011-07-19 21:50:42 +02:00
|
|
|
QapiDeallocVisitor *md;
|
|
|
|
Visitor *v;
|
2014-05-07 09:53:44 +02:00
|
|
|
''',
|
|
|
|
obj=obj)
|
2011-07-19 21:50:42 +02:00
|
|
|
pop_indent()
|
|
|
|
|
|
|
|
return ret.rstrip()
|
|
|
|
|
|
|
|
def gen_visitor_input_vars_decl(args):
|
|
|
|
ret = ""
|
|
|
|
push_indent()
|
|
|
|
for argname, argtype, optional, structured in parse_args(args):
|
|
|
|
if optional:
|
|
|
|
ret += mcgen('''
|
|
|
|
bool has_%(argname)s = false;
|
|
|
|
''',
|
|
|
|
argname=c_var(argname))
|
2014-06-10 13:25:53 +02:00
|
|
|
if is_c_ptr(argtype):
|
2011-07-19 21:50:42 +02:00
|
|
|
ret += mcgen('''
|
|
|
|
%(argtype)s %(argname)s = NULL;
|
|
|
|
''',
|
|
|
|
argname=c_var(argname), argtype=c_type(argtype))
|
|
|
|
else:
|
|
|
|
ret += mcgen('''
|
qapi: zero-initialize all QMP command parameters
In general QMP command parameter values are specified by consumers of the
QMP/HMP interface, but in the case of optional parameters these values may
be left uninitialized.
It is considered a bug for code to make use of optional parameters that have
not been flagged as being present by the marshalling code (via corresponding
has_<parameter> parameter), however our marshalling code will still pass
these uninitialized values on to the corresponding QMP function (to then
be ignored). Some compilers (clang in particular) consider this unsafe
however, and generate warnings as a result. As reported by Peter Maydell:
This is something clang's -fsanitize=undefined spotted. The
code generated by qapi-commands.py in qmp-marshal.c for
qmp_marshal_* functions where there are some optional
arguments looks like this:
bool has_force = false;
bool force;
mi = qmp_input_visitor_new_strict(QOBJECT(args));
v = qmp_input_get_visitor(mi);
visit_type_str(v, &device, "device", errp);
visit_start_optional(v, &has_force, "force", errp);
if (has_force) {
visit_type_bool(v, &force, "force", errp);
}
visit_end_optional(v, errp);
qmp_input_visitor_cleanup(mi);
if (error_is_set(errp)) {
goto out;
}
qmp_eject(device, has_force, force, errp);
In the case where has_force is false, we never initialize
force, but then we use it by passing it to qmp_eject.
I imagine we don't then actually use the value, but clang
complains in particular for 'bool' variables because the value
that ends up being loaded from memory for 'force' is not either
0 or 1 (being uninitialized stack contents).
Fix this by initializing all QMP command parameters to {0} in the
marshalling code prior to passing them on to the QMP functions.
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
Reported-by: Peter Maydell <peter.maydell@linaro.org>
Tested-by: Peter Maydell <peter.maydell@linaro.org>
Reviewed-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2014-05-20 19:20:39 +02:00
|
|
|
%(argtype)s %(argname)s = {0};
|
2011-07-19 21:50:42 +02:00
|
|
|
''',
|
|
|
|
argname=c_var(argname), argtype=c_type(argtype))
|
|
|
|
|
|
|
|
pop_indent()
|
|
|
|
return ret.rstrip()
|
|
|
|
|
2014-05-07 09:53:44 +02:00
|
|
|
def gen_visitor_input_block(args, dealloc=False):
|
2011-07-19 21:50:42 +02:00
|
|
|
ret = ""
|
qapi: Replace uncommon use of the error API by the common one
We commonly use the error API like this:
err = NULL;
foo(..., &err);
if (err) {
goto out;
}
bar(..., &err);
Every error source is checked separately. The second function is only
called when the first one succeeds. Both functions are free to pass
their argument to error_set(). Because error_set() asserts no error
has been set, this effectively means they must not be called with an
error set.
The qapi-generated code uses the error API differently:
// *errp was initialized to NULL somewhere up the call chain
frob(..., errp);
gnat(..., errp);
Errors accumulate in *errp: first error wins, subsequent errors get
dropped. To make this work, the second function does nothing when
called with an error set. Requires non-null errp, or else the second
function can't see the first one fail.
This usage has also bled into visitor tests, and two device model
object property getters rtc_get_date() and balloon_stats_get_all().
With the "accumulate" technique, you need fewer error checks in
callers, and buy that with an error check in every callee. Can be
nice.
However, mixing the two techniques is confusing. You can't use the
"accumulate" technique with functions designed for the "check
separately" technique. You can use the "check separately" technique
with functions designed for the "accumulate" technique, but then
error_set() can't catch you setting an error more than once.
Standardize on the "check separately" technique for now, because it's
overwhelmingly prevalent.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2014-05-07 09:53:54 +02:00
|
|
|
errparg = '&local_err'
|
|
|
|
errarg = 'local_err'
|
qapi: qapi-commands: fix possible leaks on visitor dealloc
In qmp-marshal.c the dealloc visitor calls use the same errp
pointer of the input visitor calls. This means that if any of
the input visitor calls fails, then the dealloc visitor will
return early, before freeing the object's memory.
Here's an example, consider this code:
int qmp_marshal_input_block_passwd(Monitor *mon, const QDict *qdict, QObject **ret)
{
[...]
char * device = NULL;
char * password = NULL;
mi = qmp_input_visitor_new_strict(QOBJECT(args));
v = qmp_input_get_visitor(mi);
visit_type_str(v, &device, "device", errp);
visit_type_str(v, &password, "password", errp);
qmp_input_visitor_cleanup(mi);
if (error_is_set(errp)) {
goto out;
}
qmp_block_passwd(device, password, errp);
out:
md = qapi_dealloc_visitor_new();
v = qapi_dealloc_get_visitor(md);
visit_type_str(v, &device, "device", errp);
visit_type_str(v, &password, "password", errp);
qapi_dealloc_visitor_cleanup(md);
[...]
return 0;
}
Consider errp != NULL when the out label is reached, we're going
to leak device and password.
This patch fixes this by always passing errp=NULL for dealloc
visitors, meaning that we always try to free them regardless of
any previous failure. The above example would then be:
out:
md = qapi_dealloc_visitor_new();
v = qapi_dealloc_get_visitor(md);
visit_type_str(v, &device, "device", NULL);
visit_type_str(v, &password, "password", NULL);
qapi_dealloc_visitor_cleanup(md);
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
Reviewed-by: Michael Roth <mdroth@linux.vnet.ibm.com>
2013-07-11 20:26:56 +02:00
|
|
|
|
2011-07-19 21:50:42 +02:00
|
|
|
if len(args) == 0:
|
|
|
|
return ret
|
|
|
|
|
|
|
|
push_indent()
|
|
|
|
|
|
|
|
if dealloc:
|
qapi: qapi-commands: fix possible leaks on visitor dealloc
In qmp-marshal.c the dealloc visitor calls use the same errp
pointer of the input visitor calls. This means that if any of
the input visitor calls fails, then the dealloc visitor will
return early, before freeing the object's memory.
Here's an example, consider this code:
int qmp_marshal_input_block_passwd(Monitor *mon, const QDict *qdict, QObject **ret)
{
[...]
char * device = NULL;
char * password = NULL;
mi = qmp_input_visitor_new_strict(QOBJECT(args));
v = qmp_input_get_visitor(mi);
visit_type_str(v, &device, "device", errp);
visit_type_str(v, &password, "password", errp);
qmp_input_visitor_cleanup(mi);
if (error_is_set(errp)) {
goto out;
}
qmp_block_passwd(device, password, errp);
out:
md = qapi_dealloc_visitor_new();
v = qapi_dealloc_get_visitor(md);
visit_type_str(v, &device, "device", errp);
visit_type_str(v, &password, "password", errp);
qapi_dealloc_visitor_cleanup(md);
[...]
return 0;
}
Consider errp != NULL when the out label is reached, we're going
to leak device and password.
This patch fixes this by always passing errp=NULL for dealloc
visitors, meaning that we always try to free them regardless of
any previous failure. The above example would then be:
out:
md = qapi_dealloc_visitor_new();
v = qapi_dealloc_get_visitor(md);
visit_type_str(v, &device, "device", NULL);
visit_type_str(v, &password, "password", NULL);
qapi_dealloc_visitor_cleanup(md);
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
Reviewed-by: Michael Roth <mdroth@linux.vnet.ibm.com>
2013-07-11 20:26:56 +02:00
|
|
|
errparg = 'NULL'
|
qapi: Replace uncommon use of the error API by the common one
We commonly use the error API like this:
err = NULL;
foo(..., &err);
if (err) {
goto out;
}
bar(..., &err);
Every error source is checked separately. The second function is only
called when the first one succeeds. Both functions are free to pass
their argument to error_set(). Because error_set() asserts no error
has been set, this effectively means they must not be called with an
error set.
The qapi-generated code uses the error API differently:
// *errp was initialized to NULL somewhere up the call chain
frob(..., errp);
gnat(..., errp);
Errors accumulate in *errp: first error wins, subsequent errors get
dropped. To make this work, the second function does nothing when
called with an error set. Requires non-null errp, or else the second
function can't see the first one fail.
This usage has also bled into visitor tests, and two device model
object property getters rtc_get_date() and balloon_stats_get_all().
With the "accumulate" technique, you need fewer error checks in
callers, and buy that with an error check in every callee. Can be
nice.
However, mixing the two techniques is confusing. You can't use the
"accumulate" technique with functions designed for the "check
separately" technique. You can use the "check separately" technique
with functions designed for the "accumulate" technique, but then
error_set() can't catch you setting an error more than once.
Standardize on the "check separately" technique for now, because it's
overwhelmingly prevalent.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2014-05-07 09:53:54 +02:00
|
|
|
errarg = None;
|
2011-07-19 21:50:42 +02:00
|
|
|
ret += mcgen('''
|
2014-05-07 09:53:44 +02:00
|
|
|
qmp_input_visitor_cleanup(mi);
|
2011-07-19 21:50:42 +02:00
|
|
|
md = qapi_dealloc_visitor_new();
|
|
|
|
v = qapi_dealloc_get_visitor(md);
|
|
|
|
''')
|
|
|
|
else:
|
|
|
|
ret += mcgen('''
|
|
|
|
v = qmp_input_get_visitor(mi);
|
2014-05-07 09:53:44 +02:00
|
|
|
''')
|
2011-07-19 21:50:42 +02:00
|
|
|
|
|
|
|
for argname, argtype, optional, structured in parse_args(args):
|
|
|
|
if optional:
|
|
|
|
ret += mcgen('''
|
2014-05-07 09:53:46 +02:00
|
|
|
visit_optional(v, &has_%(c_name)s, "%(name)s", %(errp)s);
|
2011-07-19 21:50:42 +02:00
|
|
|
''',
|
qapi: qapi-commands: fix possible leaks on visitor dealloc
In qmp-marshal.c the dealloc visitor calls use the same errp
pointer of the input visitor calls. This means that if any of
the input visitor calls fails, then the dealloc visitor will
return early, before freeing the object's memory.
Here's an example, consider this code:
int qmp_marshal_input_block_passwd(Monitor *mon, const QDict *qdict, QObject **ret)
{
[...]
char * device = NULL;
char * password = NULL;
mi = qmp_input_visitor_new_strict(QOBJECT(args));
v = qmp_input_get_visitor(mi);
visit_type_str(v, &device, "device", errp);
visit_type_str(v, &password, "password", errp);
qmp_input_visitor_cleanup(mi);
if (error_is_set(errp)) {
goto out;
}
qmp_block_passwd(device, password, errp);
out:
md = qapi_dealloc_visitor_new();
v = qapi_dealloc_get_visitor(md);
visit_type_str(v, &device, "device", errp);
visit_type_str(v, &password, "password", errp);
qapi_dealloc_visitor_cleanup(md);
[...]
return 0;
}
Consider errp != NULL when the out label is reached, we're going
to leak device and password.
This patch fixes this by always passing errp=NULL for dealloc
visitors, meaning that we always try to free them regardless of
any previous failure. The above example would then be:
out:
md = qapi_dealloc_visitor_new();
v = qapi_dealloc_get_visitor(md);
visit_type_str(v, &device, "device", NULL);
visit_type_str(v, &password, "password", NULL);
qapi_dealloc_visitor_cleanup(md);
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
Reviewed-by: Michael Roth <mdroth@linux.vnet.ibm.com>
2013-07-11 20:26:56 +02:00
|
|
|
c_name=c_var(argname), name=argname, errp=errparg)
|
qapi: Replace uncommon use of the error API by the common one
We commonly use the error API like this:
err = NULL;
foo(..., &err);
if (err) {
goto out;
}
bar(..., &err);
Every error source is checked separately. The second function is only
called when the first one succeeds. Both functions are free to pass
their argument to error_set(). Because error_set() asserts no error
has been set, this effectively means they must not be called with an
error set.
The qapi-generated code uses the error API differently:
// *errp was initialized to NULL somewhere up the call chain
frob(..., errp);
gnat(..., errp);
Errors accumulate in *errp: first error wins, subsequent errors get
dropped. To make this work, the second function does nothing when
called with an error set. Requires non-null errp, or else the second
function can't see the first one fail.
This usage has also bled into visitor tests, and two device model
object property getters rtc_get_date() and balloon_stats_get_all().
With the "accumulate" technique, you need fewer error checks in
callers, and buy that with an error check in every callee. Can be
nice.
However, mixing the two techniques is confusing. You can't use the
"accumulate" technique with functions designed for the "check
separately" technique. You can use the "check separately" technique
with functions designed for the "accumulate" technique, but then
error_set() can't catch you setting an error more than once.
Standardize on the "check separately" technique for now, because it's
overwhelmingly prevalent.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2014-05-07 09:53:54 +02:00
|
|
|
ret += gen_err_check(errarg)
|
|
|
|
ret += mcgen('''
|
|
|
|
if (has_%(c_name)s) {
|
|
|
|
''',
|
|
|
|
c_name=c_var(argname))
|
2011-07-19 21:50:42 +02:00
|
|
|
push_indent()
|
|
|
|
ret += mcgen('''
|
qapi: qapi-commands: fix possible leaks on visitor dealloc
In qmp-marshal.c the dealloc visitor calls use the same errp
pointer of the input visitor calls. This means that if any of
the input visitor calls fails, then the dealloc visitor will
return early, before freeing the object's memory.
Here's an example, consider this code:
int qmp_marshal_input_block_passwd(Monitor *mon, const QDict *qdict, QObject **ret)
{
[...]
char * device = NULL;
char * password = NULL;
mi = qmp_input_visitor_new_strict(QOBJECT(args));
v = qmp_input_get_visitor(mi);
visit_type_str(v, &device, "device", errp);
visit_type_str(v, &password, "password", errp);
qmp_input_visitor_cleanup(mi);
if (error_is_set(errp)) {
goto out;
}
qmp_block_passwd(device, password, errp);
out:
md = qapi_dealloc_visitor_new();
v = qapi_dealloc_get_visitor(md);
visit_type_str(v, &device, "device", errp);
visit_type_str(v, &password, "password", errp);
qapi_dealloc_visitor_cleanup(md);
[...]
return 0;
}
Consider errp != NULL when the out label is reached, we're going
to leak device and password.
This patch fixes this by always passing errp=NULL for dealloc
visitors, meaning that we always try to free them regardless of
any previous failure. The above example would then be:
out:
md = qapi_dealloc_visitor_new();
v = qapi_dealloc_get_visitor(md);
visit_type_str(v, &device, "device", NULL);
visit_type_str(v, &password, "password", NULL);
qapi_dealloc_visitor_cleanup(md);
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
Reviewed-by: Michael Roth <mdroth@linux.vnet.ibm.com>
2013-07-11 20:26:56 +02:00
|
|
|
%(visitor)s(v, &%(c_name)s, "%(name)s", %(errp)s);
|
2011-07-19 21:50:42 +02:00
|
|
|
''',
|
2011-09-14 21:30:00 +02:00
|
|
|
c_name=c_var(argname), name=argname, argtype=argtype,
|
qapi: qapi-commands: fix possible leaks on visitor dealloc
In qmp-marshal.c the dealloc visitor calls use the same errp
pointer of the input visitor calls. This means that if any of
the input visitor calls fails, then the dealloc visitor will
return early, before freeing the object's memory.
Here's an example, consider this code:
int qmp_marshal_input_block_passwd(Monitor *mon, const QDict *qdict, QObject **ret)
{
[...]
char * device = NULL;
char * password = NULL;
mi = qmp_input_visitor_new_strict(QOBJECT(args));
v = qmp_input_get_visitor(mi);
visit_type_str(v, &device, "device", errp);
visit_type_str(v, &password, "password", errp);
qmp_input_visitor_cleanup(mi);
if (error_is_set(errp)) {
goto out;
}
qmp_block_passwd(device, password, errp);
out:
md = qapi_dealloc_visitor_new();
v = qapi_dealloc_get_visitor(md);
visit_type_str(v, &device, "device", errp);
visit_type_str(v, &password, "password", errp);
qapi_dealloc_visitor_cleanup(md);
[...]
return 0;
}
Consider errp != NULL when the out label is reached, we're going
to leak device and password.
This patch fixes this by always passing errp=NULL for dealloc
visitors, meaning that we always try to free them regardless of
any previous failure. The above example would then be:
out:
md = qapi_dealloc_visitor_new();
v = qapi_dealloc_get_visitor(md);
visit_type_str(v, &device, "device", NULL);
visit_type_str(v, &password, "password", NULL);
qapi_dealloc_visitor_cleanup(md);
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
Reviewed-by: Michael Roth <mdroth@linux.vnet.ibm.com>
2013-07-11 20:26:56 +02:00
|
|
|
visitor=type_visitor(argtype), errp=errparg)
|
qapi: Replace uncommon use of the error API by the common one
We commonly use the error API like this:
err = NULL;
foo(..., &err);
if (err) {
goto out;
}
bar(..., &err);
Every error source is checked separately. The second function is only
called when the first one succeeds. Both functions are free to pass
their argument to error_set(). Because error_set() asserts no error
has been set, this effectively means they must not be called with an
error set.
The qapi-generated code uses the error API differently:
// *errp was initialized to NULL somewhere up the call chain
frob(..., errp);
gnat(..., errp);
Errors accumulate in *errp: first error wins, subsequent errors get
dropped. To make this work, the second function does nothing when
called with an error set. Requires non-null errp, or else the second
function can't see the first one fail.
This usage has also bled into visitor tests, and two device model
object property getters rtc_get_date() and balloon_stats_get_all().
With the "accumulate" technique, you need fewer error checks in
callers, and buy that with an error check in every callee. Can be
nice.
However, mixing the two techniques is confusing. You can't use the
"accumulate" technique with functions designed for the "check
separately" technique. You can use the "check separately" technique
with functions designed for the "accumulate" technique, but then
error_set() can't catch you setting an error more than once.
Standardize on the "check separately" technique for now, because it's
overwhelmingly prevalent.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2014-05-07 09:53:54 +02:00
|
|
|
ret += gen_err_check(errarg)
|
2011-07-19 21:50:42 +02:00
|
|
|
if optional:
|
|
|
|
pop_indent()
|
|
|
|
ret += mcgen('''
|
|
|
|
}
|
2014-05-07 09:53:46 +02:00
|
|
|
''')
|
2011-07-19 21:50:42 +02:00
|
|
|
|
|
|
|
if dealloc:
|
|
|
|
ret += mcgen('''
|
|
|
|
qapi_dealloc_visitor_cleanup(md);
|
|
|
|
''')
|
|
|
|
pop_indent()
|
|
|
|
return ret.rstrip()
|
|
|
|
|
2011-09-02 19:34:46 +02:00
|
|
|
def gen_marshal_output(name, args, ret_type, middle_mode):
|
2011-07-19 21:50:42 +02:00
|
|
|
if not ret_type:
|
|
|
|
return ""
|
2011-09-02 19:34:46 +02:00
|
|
|
|
2011-07-19 21:50:42 +02:00
|
|
|
ret = mcgen('''
|
|
|
|
static void qmp_marshal_output_%(c_name)s(%(c_ret_type)s ret_in, QObject **ret_out, Error **errp)
|
|
|
|
{
|
qapi: Replace uncommon use of the error API by the common one
We commonly use the error API like this:
err = NULL;
foo(..., &err);
if (err) {
goto out;
}
bar(..., &err);
Every error source is checked separately. The second function is only
called when the first one succeeds. Both functions are free to pass
their argument to error_set(). Because error_set() asserts no error
has been set, this effectively means they must not be called with an
error set.
The qapi-generated code uses the error API differently:
// *errp was initialized to NULL somewhere up the call chain
frob(..., errp);
gnat(..., errp);
Errors accumulate in *errp: first error wins, subsequent errors get
dropped. To make this work, the second function does nothing when
called with an error set. Requires non-null errp, or else the second
function can't see the first one fail.
This usage has also bled into visitor tests, and two device model
object property getters rtc_get_date() and balloon_stats_get_all().
With the "accumulate" technique, you need fewer error checks in
callers, and buy that with an error check in every callee. Can be
nice.
However, mixing the two techniques is confusing. You can't use the
"accumulate" technique with functions designed for the "check
separately" technique. You can use the "check separately" technique
with functions designed for the "accumulate" technique, but then
error_set() can't catch you setting an error more than once.
Standardize on the "check separately" technique for now, because it's
overwhelmingly prevalent.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2014-05-07 09:53:54 +02:00
|
|
|
Error *local_err = NULL;
|
2011-07-19 21:50:42 +02:00
|
|
|
QmpOutputVisitor *mo = qmp_output_visitor_new();
|
2014-05-07 09:53:44 +02:00
|
|
|
QapiDeallocVisitor *md;
|
2011-07-19 21:50:42 +02:00
|
|
|
Visitor *v;
|
|
|
|
|
|
|
|
v = qmp_output_get_visitor(mo);
|
qapi: Replace uncommon use of the error API by the common one
We commonly use the error API like this:
err = NULL;
foo(..., &err);
if (err) {
goto out;
}
bar(..., &err);
Every error source is checked separately. The second function is only
called when the first one succeeds. Both functions are free to pass
their argument to error_set(). Because error_set() asserts no error
has been set, this effectively means they must not be called with an
error set.
The qapi-generated code uses the error API differently:
// *errp was initialized to NULL somewhere up the call chain
frob(..., errp);
gnat(..., errp);
Errors accumulate in *errp: first error wins, subsequent errors get
dropped. To make this work, the second function does nothing when
called with an error set. Requires non-null errp, or else the second
function can't see the first one fail.
This usage has also bled into visitor tests, and two device model
object property getters rtc_get_date() and balloon_stats_get_all().
With the "accumulate" technique, you need fewer error checks in
callers, and buy that with an error check in every callee. Can be
nice.
However, mixing the two techniques is confusing. You can't use the
"accumulate" technique with functions designed for the "check
separately" technique. You can use the "check separately" technique
with functions designed for the "accumulate" technique, but then
error_set() can't catch you setting an error more than once.
Standardize on the "check separately" technique for now, because it's
overwhelmingly prevalent.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2014-05-07 09:53:54 +02:00
|
|
|
%(visitor)s(v, &ret_in, "unused", &local_err);
|
|
|
|
if (local_err) {
|
|
|
|
goto out;
|
2011-07-19 21:50:42 +02:00
|
|
|
}
|
qapi: Replace uncommon use of the error API by the common one
We commonly use the error API like this:
err = NULL;
foo(..., &err);
if (err) {
goto out;
}
bar(..., &err);
Every error source is checked separately. The second function is only
called when the first one succeeds. Both functions are free to pass
their argument to error_set(). Because error_set() asserts no error
has been set, this effectively means they must not be called with an
error set.
The qapi-generated code uses the error API differently:
// *errp was initialized to NULL somewhere up the call chain
frob(..., errp);
gnat(..., errp);
Errors accumulate in *errp: first error wins, subsequent errors get
dropped. To make this work, the second function does nothing when
called with an error set. Requires non-null errp, or else the second
function can't see the first one fail.
This usage has also bled into visitor tests, and two device model
object property getters rtc_get_date() and balloon_stats_get_all().
With the "accumulate" technique, you need fewer error checks in
callers, and buy that with an error check in every callee. Can be
nice.
However, mixing the two techniques is confusing. You can't use the
"accumulate" technique with functions designed for the "check
separately" technique. You can use the "check separately" technique
with functions designed for the "accumulate" technique, but then
error_set() can't catch you setting an error more than once.
Standardize on the "check separately" technique for now, because it's
overwhelmingly prevalent.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2014-05-07 09:53:54 +02:00
|
|
|
*ret_out = qmp_output_get_qobject(mo);
|
|
|
|
|
|
|
|
out:
|
|
|
|
error_propagate(errp, local_err);
|
2011-07-19 21:50:42 +02:00
|
|
|
qmp_output_visitor_cleanup(mo);
|
2014-05-07 09:53:44 +02:00
|
|
|
md = qapi_dealloc_visitor_new();
|
2011-07-19 21:50:42 +02:00
|
|
|
v = qapi_dealloc_get_visitor(md);
|
qapi: qapi-commands: fix possible leaks on visitor dealloc
In qmp-marshal.c the dealloc visitor calls use the same errp
pointer of the input visitor calls. This means that if any of
the input visitor calls fails, then the dealloc visitor will
return early, before freeing the object's memory.
Here's an example, consider this code:
int qmp_marshal_input_block_passwd(Monitor *mon, const QDict *qdict, QObject **ret)
{
[...]
char * device = NULL;
char * password = NULL;
mi = qmp_input_visitor_new_strict(QOBJECT(args));
v = qmp_input_get_visitor(mi);
visit_type_str(v, &device, "device", errp);
visit_type_str(v, &password, "password", errp);
qmp_input_visitor_cleanup(mi);
if (error_is_set(errp)) {
goto out;
}
qmp_block_passwd(device, password, errp);
out:
md = qapi_dealloc_visitor_new();
v = qapi_dealloc_get_visitor(md);
visit_type_str(v, &device, "device", errp);
visit_type_str(v, &password, "password", errp);
qapi_dealloc_visitor_cleanup(md);
[...]
return 0;
}
Consider errp != NULL when the out label is reached, we're going
to leak device and password.
This patch fixes this by always passing errp=NULL for dealloc
visitors, meaning that we always try to free them regardless of
any previous failure. The above example would then be:
out:
md = qapi_dealloc_visitor_new();
v = qapi_dealloc_get_visitor(md);
visit_type_str(v, &device, "device", NULL);
visit_type_str(v, &password, "password", NULL);
qapi_dealloc_visitor_cleanup(md);
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
Reviewed-by: Michael Roth <mdroth@linux.vnet.ibm.com>
2013-07-11 20:26:56 +02:00
|
|
|
%(visitor)s(v, &ret_in, "unused", NULL);
|
2011-07-19 21:50:42 +02:00
|
|
|
qapi_dealloc_visitor_cleanup(md);
|
|
|
|
}
|
|
|
|
''',
|
2012-03-20 14:54:35 +01:00
|
|
|
c_ret_type=c_type(ret_type), c_name=c_fun(name),
|
2011-09-14 21:30:00 +02:00
|
|
|
visitor=type_visitor(ret_type))
|
2011-07-19 21:50:42 +02:00
|
|
|
|
|
|
|
return ret
|
|
|
|
|
2011-09-02 19:34:46 +02:00
|
|
|
def gen_marshal_input_decl(name, args, ret_type, middle_mode):
|
|
|
|
if middle_mode:
|
2012-03-20 14:54:35 +01:00
|
|
|
return 'int qmp_marshal_input_%s(Monitor *mon, const QDict *qdict, QObject **ret)' % c_fun(name)
|
2011-09-02 19:34:46 +02:00
|
|
|
else:
|
2012-03-20 14:54:35 +01:00
|
|
|
return 'static void qmp_marshal_input_%s(QDict *args, QObject **ret, Error **errp)' % c_fun(name)
|
2011-09-02 19:34:46 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def gen_marshal_input(name, args, ret_type, middle_mode):
|
|
|
|
hdr = gen_marshal_input_decl(name, args, ret_type, middle_mode)
|
|
|
|
|
2011-07-19 21:50:42 +02:00
|
|
|
ret = mcgen('''
|
2011-09-02 19:34:46 +02:00
|
|
|
%(header)s
|
2011-07-19 21:50:42 +02:00
|
|
|
{
|
qapi: Replace uncommon use of the error API by the common one
We commonly use the error API like this:
err = NULL;
foo(..., &err);
if (err) {
goto out;
}
bar(..., &err);
Every error source is checked separately. The second function is only
called when the first one succeeds. Both functions are free to pass
their argument to error_set(). Because error_set() asserts no error
has been set, this effectively means they must not be called with an
error set.
The qapi-generated code uses the error API differently:
// *errp was initialized to NULL somewhere up the call chain
frob(..., errp);
gnat(..., errp);
Errors accumulate in *errp: first error wins, subsequent errors get
dropped. To make this work, the second function does nothing when
called with an error set. Requires non-null errp, or else the second
function can't see the first one fail.
This usage has also bled into visitor tests, and two device model
object property getters rtc_get_date() and balloon_stats_get_all().
With the "accumulate" technique, you need fewer error checks in
callers, and buy that with an error check in every callee. Can be
nice.
However, mixing the two techniques is confusing. You can't use the
"accumulate" technique with functions designed for the "check
separately" technique. You can use the "check separately" technique
with functions designed for the "accumulate" technique, but then
error_set() can't catch you setting an error more than once.
Standardize on the "check separately" technique for now, because it's
overwhelmingly prevalent.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2014-05-07 09:53:54 +02:00
|
|
|
Error *local_err = NULL;
|
2011-07-19 21:50:42 +02:00
|
|
|
''',
|
2011-09-02 19:34:46 +02:00
|
|
|
header=hdr)
|
|
|
|
|
|
|
|
if middle_mode:
|
|
|
|
ret += mcgen('''
|
|
|
|
QDict *args = (QDict *)qdict;
|
|
|
|
''')
|
2011-07-19 21:50:42 +02:00
|
|
|
|
|
|
|
if ret_type:
|
2014-06-10 13:25:53 +02:00
|
|
|
if is_c_ptr(ret_type):
|
2011-07-19 21:50:42 +02:00
|
|
|
retval = " %s retval = NULL;" % c_type(ret_type)
|
|
|
|
else:
|
|
|
|
retval = " %s retval;" % c_type(ret_type)
|
|
|
|
ret += mcgen('''
|
|
|
|
%(retval)s
|
|
|
|
''',
|
|
|
|
retval=retval)
|
|
|
|
|
|
|
|
if len(args) > 0:
|
|
|
|
ret += mcgen('''
|
|
|
|
%(visitor_input_containers_decl)s
|
|
|
|
%(visitor_input_vars_decl)s
|
|
|
|
|
|
|
|
%(visitor_input_block)s
|
|
|
|
|
|
|
|
''',
|
2014-05-07 09:53:44 +02:00
|
|
|
visitor_input_containers_decl=gen_visitor_input_containers_decl(args, "QOBJECT(args)"),
|
2011-07-19 21:50:42 +02:00
|
|
|
visitor_input_vars_decl=gen_visitor_input_vars_decl(args),
|
2014-05-07 09:53:44 +02:00
|
|
|
visitor_input_block=gen_visitor_input_block(args))
|
2011-09-02 19:34:46 +02:00
|
|
|
else:
|
|
|
|
ret += mcgen('''
|
qapi: Replace uncommon use of the error API by the common one
We commonly use the error API like this:
err = NULL;
foo(..., &err);
if (err) {
goto out;
}
bar(..., &err);
Every error source is checked separately. The second function is only
called when the first one succeeds. Both functions are free to pass
their argument to error_set(). Because error_set() asserts no error
has been set, this effectively means they must not be called with an
error set.
The qapi-generated code uses the error API differently:
// *errp was initialized to NULL somewhere up the call chain
frob(..., errp);
gnat(..., errp);
Errors accumulate in *errp: first error wins, subsequent errors get
dropped. To make this work, the second function does nothing when
called with an error set. Requires non-null errp, or else the second
function can't see the first one fail.
This usage has also bled into visitor tests, and two device model
object property getters rtc_get_date() and balloon_stats_get_all().
With the "accumulate" technique, you need fewer error checks in
callers, and buy that with an error check in every callee. Can be
nice.
However, mixing the two techniques is confusing. You can't use the
"accumulate" technique with functions designed for the "check
separately" technique. You can use the "check separately" technique
with functions designed for the "accumulate" technique, but then
error_set() can't catch you setting an error more than once.
Standardize on the "check separately" technique for now, because it's
overwhelmingly prevalent.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2014-05-07 09:53:54 +02:00
|
|
|
|
2011-09-02 19:34:46 +02:00
|
|
|
(void)args;
|
|
|
|
''')
|
2011-07-19 21:50:42 +02:00
|
|
|
|
|
|
|
ret += mcgen('''
|
|
|
|
%(sync_call)s
|
|
|
|
''',
|
|
|
|
sync_call=gen_sync_call(name, args, ret_type, indent=4))
|
qapi: Replace uncommon use of the error API by the common one
We commonly use the error API like this:
err = NULL;
foo(..., &err);
if (err) {
goto out;
}
bar(..., &err);
Every error source is checked separately. The second function is only
called when the first one succeeds. Both functions are free to pass
their argument to error_set(). Because error_set() asserts no error
has been set, this effectively means they must not be called with an
error set.
The qapi-generated code uses the error API differently:
// *errp was initialized to NULL somewhere up the call chain
frob(..., errp);
gnat(..., errp);
Errors accumulate in *errp: first error wins, subsequent errors get
dropped. To make this work, the second function does nothing when
called with an error set. Requires non-null errp, or else the second
function can't see the first one fail.
This usage has also bled into visitor tests, and two device model
object property getters rtc_get_date() and balloon_stats_get_all().
With the "accumulate" technique, you need fewer error checks in
callers, and buy that with an error check in every callee. Can be
nice.
However, mixing the two techniques is confusing. You can't use the
"accumulate" technique with functions designed for the "check
separately" technique. You can use the "check separately" technique
with functions designed for the "accumulate" technique, but then
error_set() can't catch you setting an error more than once.
Standardize on the "check separately" technique for now, because it's
overwhelmingly prevalent.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2014-05-07 09:53:54 +02:00
|
|
|
if re.search('^ *goto out\\;', ret, re.MULTILINE):
|
|
|
|
ret += mcgen('''
|
2011-07-19 21:50:42 +02:00
|
|
|
|
|
|
|
out:
|
qapi: Replace uncommon use of the error API by the common one
We commonly use the error API like this:
err = NULL;
foo(..., &err);
if (err) {
goto out;
}
bar(..., &err);
Every error source is checked separately. The second function is only
called when the first one succeeds. Both functions are free to pass
their argument to error_set(). Because error_set() asserts no error
has been set, this effectively means they must not be called with an
error set.
The qapi-generated code uses the error API differently:
// *errp was initialized to NULL somewhere up the call chain
frob(..., errp);
gnat(..., errp);
Errors accumulate in *errp: first error wins, subsequent errors get
dropped. To make this work, the second function does nothing when
called with an error set. Requires non-null errp, or else the second
function can't see the first one fail.
This usage has also bled into visitor tests, and two device model
object property getters rtc_get_date() and balloon_stats_get_all().
With the "accumulate" technique, you need fewer error checks in
callers, and buy that with an error check in every callee. Can be
nice.
However, mixing the two techniques is confusing. You can't use the
"accumulate" technique with functions designed for the "check
separately" technique. You can use the "check separately" technique
with functions designed for the "accumulate" technique, but then
error_set() can't catch you setting an error more than once.
Standardize on the "check separately" technique for now, because it's
overwhelmingly prevalent.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2014-05-07 09:53:54 +02:00
|
|
|
''')
|
|
|
|
if not middle_mode:
|
|
|
|
ret += mcgen('''
|
|
|
|
error_propagate(errp, local_err);
|
2011-07-19 21:50:42 +02:00
|
|
|
''')
|
|
|
|
ret += mcgen('''
|
|
|
|
%(visitor_input_block_cleanup)s
|
2011-09-02 19:34:46 +02:00
|
|
|
''',
|
2014-05-07 09:53:44 +02:00
|
|
|
visitor_input_block_cleanup=gen_visitor_input_block(args,
|
2011-09-02 19:34:46 +02:00
|
|
|
dealloc=True))
|
|
|
|
|
|
|
|
if middle_mode:
|
|
|
|
ret += mcgen('''
|
|
|
|
|
|
|
|
if (local_err) {
|
|
|
|
qerror_report_err(local_err);
|
|
|
|
error_free(local_err);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
''')
|
|
|
|
else:
|
|
|
|
ret += mcgen('''
|
2011-07-19 21:50:42 +02:00
|
|
|
return;
|
2011-09-02 19:34:46 +02:00
|
|
|
''')
|
|
|
|
|
|
|
|
ret += mcgen('''
|
2011-07-19 21:50:42 +02:00
|
|
|
}
|
2011-09-02 19:34:46 +02:00
|
|
|
''')
|
|
|
|
|
2011-07-19 21:50:42 +02:00
|
|
|
return ret
|
|
|
|
|
2012-05-08 19:24:44 +02:00
|
|
|
def option_value_matches(opt, val, cmd):
|
|
|
|
if opt in cmd and cmd[opt] == val:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2011-07-19 21:50:42 +02:00
|
|
|
def gen_registry(commands):
|
|
|
|
registry=""
|
|
|
|
push_indent()
|
|
|
|
for cmd in commands:
|
2012-05-08 19:24:44 +02:00
|
|
|
options = 'QCO_NO_OPTIONS'
|
|
|
|
if option_value_matches('success-response', 'no', cmd):
|
|
|
|
options = 'QCO_NO_SUCCESS_RESP'
|
|
|
|
|
2011-07-19 21:50:42 +02:00
|
|
|
registry += mcgen('''
|
2012-05-08 19:24:44 +02:00
|
|
|
qmp_register_command("%(name)s", qmp_marshal_input_%(c_name)s, %(opts)s);
|
2011-07-19 21:50:42 +02:00
|
|
|
''',
|
2012-05-08 19:24:44 +02:00
|
|
|
name=cmd['command'], c_name=c_fun(cmd['command']),
|
|
|
|
opts=options)
|
2011-07-19 21:50:42 +02:00
|
|
|
pop_indent()
|
|
|
|
ret = mcgen('''
|
|
|
|
static void qmp_init_marshal(void)
|
|
|
|
{
|
|
|
|
%(registry)s
|
|
|
|
}
|
|
|
|
|
|
|
|
qapi_init(qmp_init_marshal);
|
|
|
|
''',
|
|
|
|
registry=registry.rstrip())
|
|
|
|
return ret
|
|
|
|
|
|
|
|
def gen_command_decl_prologue(header, guard, prefix=""):
|
|
|
|
ret = mcgen('''
|
|
|
|
/* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* schema-defined QAPI function prototypes
|
|
|
|
*
|
|
|
|
* Copyright IBM, Corp. 2011
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Anthony Liguori <aliguori@us.ibm.com>
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
|
|
|
|
* See the COPYING.LIB file in the top-level directory.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef %(guard)s
|
|
|
|
#define %(guard)s
|
|
|
|
|
|
|
|
#include "%(prefix)sqapi-types.h"
|
2012-12-17 18:19:43 +01:00
|
|
|
#include "qapi/qmp/qdict.h"
|
|
|
|
#include "qapi/error.h"
|
2011-07-19 21:50:42 +02:00
|
|
|
|
|
|
|
''',
|
2011-09-02 19:34:46 +02:00
|
|
|
header=basename(header), guard=guardname(header), prefix=prefix)
|
2011-07-19 21:50:42 +02:00
|
|
|
return ret
|
|
|
|
|
|
|
|
def gen_command_def_prologue(prefix="", proxy=False):
|
|
|
|
ret = mcgen('''
|
|
|
|
/* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* schema-defined QMP->QAPI command dispatch
|
|
|
|
*
|
|
|
|
* Copyright IBM, Corp. 2011
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Anthony Liguori <aliguori@us.ibm.com>
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
|
|
|
|
* See the COPYING.LIB file in the top-level directory.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2012-12-06 11:22:34 +01:00
|
|
|
#include "qemu-common.h"
|
2012-12-17 18:20:00 +01:00
|
|
|
#include "qemu/module.h"
|
2012-12-17 18:19:43 +01:00
|
|
|
#include "qapi/qmp/qerror.h"
|
|
|
|
#include "qapi/qmp/types.h"
|
|
|
|
#include "qapi/qmp/dispatch.h"
|
|
|
|
#include "qapi/visitor.h"
|
2011-07-19 21:50:42 +02:00
|
|
|
#include "qapi/qmp-output-visitor.h"
|
|
|
|
#include "qapi/qmp-input-visitor.h"
|
2012-12-17 18:19:43 +01:00
|
|
|
#include "qapi/dealloc-visitor.h"
|
2011-07-19 21:50:42 +02:00
|
|
|
#include "%(prefix)sqapi-types.h"
|
|
|
|
#include "%(prefix)sqapi-visit.h"
|
|
|
|
|
|
|
|
''',
|
|
|
|
prefix=prefix)
|
|
|
|
if not proxy:
|
|
|
|
ret += '#include "%sqmp-commands.h"' % prefix
|
2011-09-02 19:34:46 +02:00
|
|
|
return ret + "\n\n"
|
2011-07-19 21:50:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
try:
|
2014-05-02 15:52:35 +02:00
|
|
|
opts, args = getopt.gnu_getopt(sys.argv[1:], "chp:i:o:m",
|
2011-12-27 15:02:16 +01:00
|
|
|
["source", "header", "prefix=",
|
2014-05-02 15:52:35 +02:00
|
|
|
"input-file=", "output-dir=",
|
|
|
|
"type=", "middle"])
|
2011-07-19 21:50:42 +02:00
|
|
|
except getopt.GetoptError, err:
|
|
|
|
print str(err)
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
output_dir = ""
|
|
|
|
prefix = ""
|
|
|
|
dispatch_type = "sync"
|
|
|
|
c_file = 'qmp-marshal.c'
|
|
|
|
h_file = 'qmp-commands.h'
|
2011-09-02 19:34:46 +02:00
|
|
|
middle_mode = False
|
2011-07-19 21:50:42 +02:00
|
|
|
|
2011-12-27 15:02:16 +01:00
|
|
|
do_c = False
|
|
|
|
do_h = False
|
|
|
|
|
2011-07-19 21:50:42 +02:00
|
|
|
for o, a in opts:
|
|
|
|
if o in ("-p", "--prefix"):
|
|
|
|
prefix = a
|
2014-05-02 15:52:35 +02:00
|
|
|
elif o in ("-i", "--input-file"):
|
|
|
|
input_file = a
|
2011-07-19 21:50:42 +02:00
|
|
|
elif o in ("-o", "--output-dir"):
|
|
|
|
output_dir = a + "/"
|
|
|
|
elif o in ("-t", "--type"):
|
|
|
|
dispatch_type = a
|
2011-09-02 19:34:46 +02:00
|
|
|
elif o in ("-m", "--middle"):
|
|
|
|
middle_mode = True
|
2011-12-27 15:02:16 +01:00
|
|
|
elif o in ("-c", "--source"):
|
|
|
|
do_c = True
|
2011-12-28 11:26:58 +01:00
|
|
|
elif o in ("-h", "--header"):
|
|
|
|
do_h = True
|
2011-12-27 15:02:16 +01:00
|
|
|
|
|
|
|
if not do_c and not do_h:
|
|
|
|
do_c = True
|
|
|
|
do_h = True
|
2011-07-19 21:50:42 +02:00
|
|
|
|
|
|
|
c_file = output_dir + prefix + c_file
|
|
|
|
h_file = output_dir + prefix + h_file
|
|
|
|
|
2011-12-27 15:02:16 +01:00
|
|
|
def maybe_open(really, name, opt):
|
|
|
|
if really:
|
|
|
|
return open(name, opt)
|
|
|
|
else:
|
2011-12-28 11:26:58 +01:00
|
|
|
import StringIO
|
|
|
|
return StringIO.StringIO()
|
2011-12-27 15:02:16 +01:00
|
|
|
|
2011-07-19 21:50:42 +02:00
|
|
|
try:
|
|
|
|
os.makedirs(output_dir)
|
|
|
|
except os.error, e:
|
|
|
|
if e.errno != errno.EEXIST:
|
|
|
|
raise
|
|
|
|
|
2014-05-02 15:52:35 +02:00
|
|
|
exprs = parse_schema(input_file)
|
2011-07-19 21:50:42 +02:00
|
|
|
commands = filter(lambda expr: expr.has_key('command'), exprs)
|
2011-12-12 21:29:33 +01:00
|
|
|
commands = filter(lambda expr: not expr.has_key('gen'), commands)
|
2011-07-19 21:50:42 +02:00
|
|
|
|
|
|
|
if dispatch_type == "sync":
|
2011-12-27 15:02:16 +01:00
|
|
|
fdecl = maybe_open(do_h, h_file, 'w')
|
|
|
|
fdef = maybe_open(do_c, c_file, 'w')
|
2011-07-19 21:50:42 +02:00
|
|
|
ret = gen_command_decl_prologue(header=basename(h_file), guard=guardname(h_file), prefix=prefix)
|
|
|
|
fdecl.write(ret)
|
|
|
|
ret = gen_command_def_prologue(prefix=prefix)
|
|
|
|
fdef.write(ret)
|
|
|
|
|
|
|
|
for cmd in commands:
|
|
|
|
arglist = []
|
|
|
|
ret_type = None
|
|
|
|
if cmd.has_key('data'):
|
|
|
|
arglist = cmd['data']
|
|
|
|
if cmd.has_key('returns'):
|
|
|
|
ret_type = cmd['returns']
|
|
|
|
ret = generate_command_decl(cmd['command'], arglist, ret_type) + "\n"
|
|
|
|
fdecl.write(ret)
|
|
|
|
if ret_type:
|
2011-09-02 19:34:46 +02:00
|
|
|
ret = gen_marshal_output(cmd['command'], arglist, ret_type, middle_mode) + "\n"
|
2011-07-19 21:50:42 +02:00
|
|
|
fdef.write(ret)
|
2011-09-02 19:34:46 +02:00
|
|
|
|
|
|
|
if middle_mode:
|
|
|
|
fdecl.write('%s;\n' % gen_marshal_input_decl(cmd['command'], arglist, ret_type, middle_mode))
|
|
|
|
|
|
|
|
ret = gen_marshal_input(cmd['command'], arglist, ret_type, middle_mode) + "\n"
|
2011-07-19 21:50:42 +02:00
|
|
|
fdef.write(ret)
|
|
|
|
|
2011-08-10 20:10:51 +02:00
|
|
|
fdecl.write("\n#endif\n");
|
2011-09-02 19:34:46 +02:00
|
|
|
|
|
|
|
if not middle_mode:
|
|
|
|
ret = gen_registry(commands)
|
|
|
|
fdef.write(ret)
|
2011-07-19 21:50:42 +02:00
|
|
|
|
|
|
|
fdef.flush()
|
|
|
|
fdef.close()
|
|
|
|
fdecl.flush()
|
|
|
|
fdecl.close()
|