: QMP queue
All of them are QAPI fixes.
The changes (since 8a22565b7c2d1920b02b94e7a8021c65895a3a22) are available in the following repository:
git://repo.or.cz/qemu/qmp-unstable.git queue/qmp
Laszlo Ersek (1):
qapi: fix double free in qmp_output_visitor_cleanup()
Paolo Bonzini (12):
qapi: add struct-errors test case to test-qmp-output-visitor
qapi: add a test case for type errors
qapi: fail hard on stack imbalance
qapi: fix memory leak on error
qapi: shortcut visits on errors
qapi: allow freeing partially-allocated objects
qapi: untangle next_list
qapi: place outermost object on qiv stack
qapi: add strict mode to input visitor
qmp: add and use q type specifier
qmp: parse commands in strict mode
qmp: document strict parsing
QMP/qmp-spec.txt | 20 +++- docs/qapi-code-gen.txt | 4 +- monitor.c | 3 + qapi-schema-test.json | 2 +- qapi/qmp-input-visitor.c | 120 +++++++++++++++-------- qapi/qmp-input-visitor.h | 2 + qapi/qmp-output-visitor.c | 8 +- qmp-commands.hx | 4 +- scripts/qapi-commands.py | 2 +- scripts/qapi-visit.py | 20 +++- test-qmp-input-strict.c | 234 +++++++++++++++++++++++++++++++++++++++++++++ test-qmp-input-visitor.c | 19 ++++ test-qmp-output-visitor.c | 20 ++++ tests/Makefile | 5 +- 14 files changed, 407 insertions(+), 56 deletions(-)
PATCH 1/13 qapi: fix double free in qmp_output_visitor_cleanup()
From: Laszlo Ersek <[email protected]>
Stack entries in QmpOutputVisitor are navigation links (weak references), except the bottom (ie. least recently added) entry, which owns the root QObject [1]. Make qmp_output_visitor_cleanup() drop the stack entries, then release the QObject tree by the root.
Attempting to serialize an invalid enum inside a dictionary is an example for triggering the double free.
[1] http://lists.nongnu.org/archive/html/qemu-devel/2012-03/msg03276.html
--- qapi/qmp-output-visitor.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
qapi/qmp-output-visitor.c +5 -3
@@ -199,14 +199,16 @@ void qmp_output_visitor_cleanup(QmpOutputVisitor *v) {QStackEntry *e, *tmp; +/* The bottom QStackEntry, if any, owns the root QObject. See the +* qmp_output_push_obj() invocations in qmp_output_add_obj(). */ +QObject *root =QTAILQ_EMPTY (&v->stack) ?NULL :qmp_output_first (v); + QTAILQ_FOREACH_SAFE(e, &v->stack, node, tmp) {QTAILQ_REMOVE (&v->stack, e, node); -if (e->value) { -qobject_decref (e->value); - }g_free (e); } +qobject_decref (root);g_free (v); }
Signature · 1 line
1.7.9.2.384.g4a92a
PATCH 2/13 qapi: add struct-errors test case to test-qmp-output-visitor
In reply to Luiz CapitulinoFrom: Paolo Bonzini <[email protected]>
This test case verifies that invalid native enums are caught, and causes qapi to tear down the QObject tree under construction, exercising the previous patch.
--- qapi-schema-test.json | 2 +- test-qmp-output-visitor.c | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-)
qapi-schema-test.json +1 -1
@@ -8,7 +8,7 @@ # for testing nested structs { 'type': 'UserDefOne', - 'data': { 'integer': 'int', 'string': 'str' } } + 'data': { 'integer': 'int', 'string': 'str', '*enum1': 'EnumOne' } } { 'type': 'UserDefTwo', 'data': { 'string': 'str',
test-qmp-output-visitor.c +20 -0
@@ -274,6 +274,24 @@ static void test_visitor_out_struct_nested(TestOutputVisitorData *data,qapi_free_UserDefNested (ud2); } +static void test_visitor_out_struct_errors (TestOutputVisitorData *data, +const void *unused) +{ +EnumOne bad_values[] = {ENUM_ONE_MAX ,-1 }; +UserDefOne u = {0 }, *pu = &u; +Error *errp; +int i; + +for (i =0 ; i <ARRAY_SIZE (bad_values) ; i++) { + errp =NULL ; + u.has_enum1 = true; + u.enum1 = bad_values[i]; +visit_type_UserDefOne (data->ov, &pu,"unused" , &errp); +g_assert (error_is_set (&errp) == true); +error_free (errp); + } +} + typedef struct TestStructList { TestStruct *value;
@@ -444,6 +462,8 @@ int main(int argc, char **argv) &out_visitor_data, test_visitor_out_struct);output_visitor_test_add ("/visitor/output/struct-nested" , &out_visitor_data, test_visitor_out_struct_nested); +output_visitor_test_add ("/visitor/output/struct-errors" , + &out_visitor_data, test_visitor_out_struct_errors);output_visitor_test_add ("/visitor/output/list" , &out_visitor_data, test_visitor_out_list); output_visitor_test_add("/visitor/output/list-qapi-free",
Signature · 1 line
1.7.9.2.384.g4a92a
PATCH 3/13 qapi: add a test case for type errors
In reply to Luiz CapitulinoFrom: Paolo Bonzini <[email protected]>
There is no test case for parse errors, add one.
--- test-qmp-input-visitor.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)
test-qmp-input-visitor.c +19 -0
@@ -258,6 +258,23 @@ static void input_visitor_test_add(const char *testpath, visitor_input_teardown); } +static void test_visitor_in_errors (TestInputVisitorData *data, +const void *unused) +{ +TestStruct *p =NULL ; +Error *errp =NULL ; +Visitor *v; + + v =visitor_input_test_init (data,"{ 'integer': false, 'boolean': 'foo', 'string': -42 }" ); + +visit_type_TestStruct (v, &p,NULL , &errp); +g_assert (error_is_set (&errp)); +g_assert (p->string ==NULL ); + +g_free (p->string); +g_free (p); +} +int main (int argc,char **argv) {TestInputVisitorData in_visitor_data;
@@ -282,6 +299,8 @@ int main(int argc, char **argv) &in_visitor_data, test_visitor_in_list);input_visitor_test_add ("/visitor/input/union" , &in_visitor_data, test_visitor_in_union); +input_visitor_test_add ("/visitor/input/errors" , + &in_visitor_data, test_visitor_in_errors);g_test_run ();
Signature · 1 line
1.7.9.2.384.g4a92a
PATCH 4/13 qapi: fail hard on stack imbalance
In reply to Luiz CapitulinoFrom: Paolo Bonzini <[email protected]>
QmpOutputVisitor will segfault if an imbalanced end function is called. So we can abort in QmpInputVisitor too.
--- qapi/qmp-input-visitor.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)
qapi/qmp-input-visitor.c +1 -4
@@ -77,11 +77,8 @@ static void qmp_input_push(QmpInputVisitor *qiv, const QObject *obj, Error **errstatic void qmp_input_pop (QmpInputVisitor *qiv,Error **errp) { +assert (qiv->nb_stack >0 ); qiv->nb_stack--; -if (qiv->nb_stack <0 ) { -error_set (errp,QERR_BUFFER_OVERRUN ); -return ; - } } static void qmp_input_start_struct(Visitor *v, void **obj, const char *kind,
Signature · 1 line
1.7.9.2.384.g4a92a
PATCH 5/13 qapi: fix memory leak on error
In reply to Luiz CapitulinoFrom: Paolo Bonzini <[email protected]>
QmpInputVisitor would leak the malloced struct if the stack was overflowed. This can be easily fixed using error_propagate.
--- qapi/qmp-input-visitor.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
qapi/qmp-input-visitor.c +4 -2
@@ -86,6 +86,7 @@ static void qmp_input_start_struct(Visitor *v, void **obj, const char *kind, {QmpInputVisitor *qiv =to_qiv (v);const QObject *qobj =qmp_input_get_object (qiv, name); +Error *err =NULL ;if (!qobj ||qobject_type (qobj) !=QTYPE_QDICT ) { error_set(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
@@ -93,8 +94,9 @@ static void qmp_input_start_struct(Visitor *v, void **obj, const char *kind,return ; } -qmp_input_push (qiv, qobj, errp); -if (error_is_set (errp)) { +qmp_input_push (qiv, qobj, &err); +if (err) { +error_propagate (errp, err);return ; }
Signature · 1 line
1.7.9.2.384.g4a92a
PATCH 6/13 qapi: shortcut visits on errors
In reply to Luiz CapitulinoFrom: Paolo Bonzini <[email protected]>
We can exit very soon if we enter a visitor with a preexisting error. This simplifies some cases because we will not have to deal with obj being non-NULL while *obj is NULL.
--- scripts/qapi-visit.py | 9 +++++++++ 1 file changed, 9 insertions(+)
scripts/qapi-visit.py +9 -0
@@ -61,6 +61,9 @@ def generate_visit_struct(name, members): void visit_type_%(name)s(Visitor *m, %(name)s ** obj, const char *name, Error **errp) { + if (error_is_set(errp)) { + return; + } visit_start_struct(m, (void **)obj, "%(name)s", name, sizeof(%(name)s), errp); ''', name=name)
@@ -81,6 +84,9 @@ void visit_type_%(name)sList(Visitor *m, %(name)sList ** obj, const char *name, { GenericList *i, **head = (GenericList **)obj; + if (error_is_set(errp)) { + return; + } visit_start_list(m, name, errp); for (*head = i = visit_next_list(m, head, errp); i; i = visit_next_list(m, &i, errp)) {
@@ -112,6 +118,9 @@ void visit_type_%(name)s(Visitor *m, %(name)s ** obj, const char *name, Error ** { Error *err = NULL; + if (error_is_set(errp)) { + return; + } visit_start_struct(m, (void **)obj, "%(name)s", name, sizeof(%(name)s), &err); visit_type_%(name)sKind(m, &(*obj)->kind, "type", &err); if (err) {
Signature · 1 line
1.7.9.2.384.g4a92a
PATCH 7/13 qapi: allow freeing partially-allocated objects
In reply to Luiz CapitulinoFrom: Paolo Bonzini <[email protected]>
Objects going through the dealloc visitor can be only partially allocated. Detect the situation and avoid a segfault. This also helps with the input visitor, when there are errors.
--- scripts/qapi-visit.py | 7 +++++++ 1 file changed, 7 insertions(+)
scripts/qapi-visit.py +7 -0
@@ -65,6 +65,9 @@ void visit_type_%(name)s(Visitor *m, %(name)s ** obj, const char *name, Error ** return; } visit_start_struct(m, (void **)obj, "%(name)s", name, sizeof(%(name)s), errp); + if (obj && !*obj) { + goto end; + } ''', name=name) push_indent()
@@ -72,6 +75,7 @@ void visit_type_%(name)s(Visitor *m, %(name)s ** obj, const char *name, Error ** pop_indent() ret += mcgen(''' +end: visit_end_struct(m, errp); } ''')
@@ -122,6 +126,9 @@ void visit_type_%(name)s(Visitor *m, %(name)s ** obj, const char *name, Error ** return; } visit_start_struct(m, (void **)obj, "%(name)s", name, sizeof(%(name)s), &err); + if (obj && !*obj) { + goto end; + } visit_type_%(name)sKind(m, &(*obj)->kind, "type", &err); if (err) { error_propagate(errp, err);
Signature · 1 line
1.7.9.2.384.g4a92a
PATCH 8/13 qapi: untangle next_list
In reply to Luiz CapitulinoFrom: Paolo Bonzini <[email protected]>
Right now, the semantics of next_list are complicated. The caller must:
* call start_list
* call next_list for each element *including the first*
* on the first call to next_list, the second argument should point to NULL and the result is the head of the list. On subsequent calls, the second argument should point to the last node (last result of next_list) and next_list itself tacks the element at the tail of the list.
This works for both input and output visitor, but having the visitor write memory when it is only reading the list is ugly. Plus, relying on *list to detect the first call is tricky and undocumented.
We can initialize so->entry in next_list instead of start_list, leaving it NULL in start_list. This way next_list sees clearly whether it is on the first call---as a bonus, it discriminates the cases based on internal state of the visitor rather than external state. We can also pull the assignment of the list head from generated code up to next_list.
--- docs/qapi-code-gen.txt | 4 ++-- qapi/qmp-input-visitor.c | 22 +++++++++++++--------- scripts/qapi-visit.py | 4 ++-- 3 files changed, 17 insertions(+), 13 deletions(-)
docs/qapi-code-gen.txt +2 -2
@@ -194,11 +194,11 @@ Example: void visit_type_UserDefOneList(Visitor *m, UserDefOneList ** obj, const char *name, Error **errp) { - GenericList *i; + GenericList *i, **prev = (GenericList **)obj; visit_start_list(m, name, errp); - for (i = visit_next_list(m, (GenericList **)obj, errp); i; i = visit_next_list(m, &i, errp)) { + for (; (i = visit_next_list(m, prev, errp)) != NULL; prev = &i) { UserDefOneList *native_i = (UserDefOneList *)i; visit_type_UserDefOne(m, &native_i->value, NULL, errp); }
qapi/qmp-input-visitor.c +13 -9
@@ -64,9 +64,7 @@ static const QObject *qmp_input_get_object(QmpInputVisitor *qiv,static void qmp_input_push (QmpInputVisitor *qiv,const QObject *obj,Error **errp) { qiv->stack[qiv->nb_stack].obj = obj; -if (qobject_type (obj) ==QTYPE_QLIST ) { - qiv->stack[qiv->nb_stack].entry =qlist_first (qobject_to_qlist (obj)); - } + qiv->stack[qiv->nb_stack].entry =NULL ; qiv->nb_stack++;if (qiv->nb_stack >=QIV_STACK_SIZE ) {
@@ -132,18 +130,24 @@ static GenericList *qmp_input_next_list(Visitor *v, GenericList **list,QmpInputVisitor *qiv =to_qiv (v);GenericList *entry;StackObject *so = &qiv->stack[qiv->nb_stack -1 ]; +bool first; + +if (so->entry ==NULL ) { + so->entry =qlist_first (qobject_to_qlist (so->obj)); + first = true; + }else { + so->entry =qlist_next (so->entry); + first = false; + }if (so->entry ==NULL ) {return NULL ; } entry =g_malloc0 (sizeof (*entry)); -if (*list) { - so->entry =qlist_next (so->entry); -if (so->entry ==NULL ) { -g_free (entry); -return NULL ; - } +if (first) { + *list = entry; + }else { (*list)->next = entry; }
scripts/qapi-visit.py +2 -2
@@ -86,14 +86,14 @@ def generate_visit_list(name, members): void visit_type_%(name)sList(Visitor *m, %(name)sList ** obj, const char *name, Error **errp) { - GenericList *i, **head = (GenericList **)obj; + GenericList *i, **prev = (GenericList **)obj; if (error_is_set(errp)) { return; } visit_start_list(m, name, errp); - for (*head = i = visit_next_list(m, head, errp); i; i = visit_next_list(m, &i, errp)) { + for (; (i = visit_next_list(m, prev, errp)) != NULL; prev = &i) { %(name)sList *native_i = (%(name)sList *)i; visit_type_%(name)s(m, &native_i->value, NULL, errp); }
Signature · 1 line
1.7.9.2.384.g4a92a
PATCH 9/13 qapi: place outermost object on qiv stack
In reply to Luiz CapitulinoFrom: Paolo Bonzini <[email protected]>
This is a slight change in the implementation of QMPInputVisitor that helps when adding strict mode.
Const QObjects cannot be inc/decref-ed, and that's why QMPInputVisitor relies heavily on weak references to inner objects. I'm not removing the weak references now, but since refcount+const is a lost battle in C (C++ has "mutable") I think removing const is fine in this case.
--- qapi/qmp-input-visitor.c | 41 +++++++++++++++++------------------------ 1 file changed, 17 insertions(+), 24 deletions(-)
qapi/qmp-input-visitor.c +17 -24
@@ -22,14 +22,13 @@typedef struct StackObject { -const QObject *obj; -const QListEntry *entry; +QObject *obj; +const QListEntry *entry; }StackObject ;struct QmpInputVisitor {Visitor visitor; -QObject *obj;StackObject stack[QIV_STACK_SIZE ];int nb_stack; };
@@ -39,21 +38,15 @@ static QmpInputVisitor *to_qiv(Visitor *v)return container_of (v, QmpInputVisitor, visitor); } -static const QObject *qmp_input_get_object (QmpInputVisitor *qiv, -const char *name) +static QObject *qmp_input_get_object (QmpInputVisitor *qiv, +const char *name) { -const QObject *qobj; - -if (qiv->nb_stack ==0 ) { - qobj = qiv->obj; - }else { - qobj = qiv->stack[qiv->nb_stack -1 ].obj; - } +QObject *qobj = qiv->stack[qiv->nb_stack -1 ].obj;if (qobj) {if (name &&qobject_type (qobj) ==QTYPE_QDICT ) {return qdict_get (qobject_to_qdict (qobj), name); - }else if (qiv->nb_stack >0 &&qobject_type (qobj) ==QTYPE_QLIST ) { + }else if (qiv->stack[qiv->nb_stack -1 ].entry) {return qlist_entry_obj (qiv->stack[qiv->nb_stack -1 ].entry); } }
@@ -61,7 +54,7 @@ static const QObject *qmp_input_get_object(QmpInputVisitor *qiv,return qobj; } -static void qmp_input_push (QmpInputVisitor *qiv,const QObject *obj,Error **errp) +static void qmp_input_push (QmpInputVisitor *qiv,QObject *obj,Error **errp) { qiv->stack[qiv->nb_stack].obj = obj; qiv->stack[qiv->nb_stack].entry =NULL ;
@@ -83,7 +76,7 @@ static void qmp_input_start_struct(Visitor *v, void **obj, const char *kind, const char *name, size_t size, Error **errp) { QmpInputVisitor *qiv = to_qiv(v); -const QObject *qobj =qmp_input_get_object (qiv, name); +QObject *qobj =qmp_input_get_object (qiv, name);Error *err =NULL ;if (!qobj ||qobject_type (qobj) !=QTYPE_QDICT ) {
@@ -113,7 +106,7 @@ static void qmp_input_end_struct(Visitor *v, Error **errp)static void qmp_input_start_list (Visitor *v,const char *name,Error **errp) {QmpInputVisitor *qiv =to_qiv (v); -const QObject *qobj =qmp_input_get_object (qiv, name); +QObject *qobj =qmp_input_get_object (qiv, name);if (!qobj ||qobject_type (qobj) !=QTYPE_QLIST ) { error_set(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
@@ -165,7 +158,7 @@ static void qmp_input_type_int(Visitor *v, int64_t *obj, const char *name, Error **errp) {QmpInputVisitor *qiv =to_qiv (v); -const QObject *qobj =qmp_input_get_object (qiv, name); +QObject *qobj =qmp_input_get_object (qiv, name);if (!qobj ||qobject_type (qobj) !=QTYPE_QINT ) { error_set(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
@@ -180,7 +173,7 @@ static void qmp_input_type_bool(Visitor *v, bool *obj, const char *name, Error **errp) {QmpInputVisitor *qiv =to_qiv (v); -const QObject *qobj =qmp_input_get_object (qiv, name); +QObject *qobj =qmp_input_get_object (qiv, name);if (!qobj ||qobject_type (qobj) !=QTYPE_QBOOL ) { error_set(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
@@ -195,7 +188,7 @@ static void qmp_input_type_str(Visitor *v, char **obj, const char *name, Error **errp) {QmpInputVisitor *qiv =to_qiv (v); -const QObject *qobj =qmp_input_get_object (qiv, name); +QObject *qobj =qmp_input_get_object (qiv, name);if (!qobj ||qobject_type (qobj) !=QTYPE_QSTRING ) { error_set(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
@@ -210,7 +203,7 @@ static void qmp_input_type_number(Visitor *v, double *obj, const char *name, Error **errp) {QmpInputVisitor *qiv =to_qiv (v); -const QObject *qobj =qmp_input_get_object (qiv, name); +QObject *qobj =qmp_input_get_object (qiv, name);if (!qobj ||qobject_type (qobj) !=QTYPE_QFLOAT ) { error_set(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
@@ -225,7 +218,7 @@ static void qmp_input_start_optional(Visitor *v, bool *present, const char *name, Error **errp) { QmpInputVisitor *qiv = to_qiv(v); -const QObject *qobj =qmp_input_get_object (qiv, name); +QObject *qobj =qmp_input_get_object (qiv, name);if (!qobj) { *present = false;
@@ -242,7 +235,7 @@ Visitor *qmp_input_get_visitor(QmpInputVisitor *v)void qmp_input_visitor_cleanup (QmpInputVisitor *v) { -qobject_decref (v->obj); +qobject_decref (v->stack[0 ].obj);g_free (v); }
@@ -264,8 +257,8 @@ QmpInputVisitor *qmp_input_visitor_new(QObject *obj) v->visitor.type_number = qmp_input_type_number; v->visitor.start_optional = qmp_input_start_optional; - v->obj = obj; -qobject_incref (v->obj); +qmp_input_push (v, obj,NULL ); +qobject_incref (obj);return v; }
Signature · 1 line
1.7.9.2.384.g4a92a
PATCH 10/13 qapi: add strict mode to input visitor
In reply to Luiz CapitulinoFrom: Paolo Bonzini <[email protected]>
While QMP in general is designed so that it is possible to ignore unknown arguments, in the case of the QMP server it is better to reject them to detect bad clients. In fact, we're already doing this at the top level in the argument checker. To extend this to complex structures, add a mode to the input visitor where it checks for unvisited keys and raises an error if it finds one.
--- qapi/qmp-input-visitor.c | 48 +++++++++- qapi/qmp-input-visitor.h | 2 + test-qmp-input-strict.c | 234 ++++++++++++++++++++++++++++++++++++++++++++++ tests/Makefile | 5 +- 4 files changed, 285 insertions(+), 4 deletions(-) create mode 100644 test-qmp-input-strict.c
qapi/qmp-input-visitor.c +45 -3
@@ -24,6 +24,7 @@ typedef struct StackObject {QObject *obj;const QListEntry *entry; +GHashTable *h; } StackObject;struct QmpInputVisitor
@@ -31,6 +32,7 @@ struct QmpInputVisitorVisitor visitor;StackObject stack[QIV_STACK_SIZE ];int nb_stack; +bool strict; };static QmpInputVisitor *to_qiv (Visitor *v)
@@ -45,6 +47,9 @@ static QObject *qmp_input_get_object(QmpInputVisitor *qiv,if (qobj) {if (name &&qobject_type (qobj) ==QTYPE_QDICT ) { +if (qiv->stack[qiv->nb_stack -1 ].h) { +g_hash_table_remove (qiv->stack[qiv->nb_stack -1 ].h, name); + }return qdict_get (qobject_to_qdict (qobj), name); }else if (qiv->stack[qiv->nb_stack -1 ].entry) {return qlist_entry_obj (qiv->stack[qiv->nb_stack -1 ].entry);
@@ -54,20 +59,47 @@ static QObject *qmp_input_get_object(QmpInputVisitor *qiv,return qobj; } +static void qdict_add_key (const char *key,QObject *obj,void *opaque) +{ +GHashTable *h = opaque; +g_hash_table_insert (h, (gpointer ) key,NULL ); +} +static void qmp_input_push (QmpInputVisitor *qiv,QObject *obj,Error **errp) { - qiv->stack[qiv->nb_stack].obj = obj; - qiv->stack[qiv->nb_stack].entry =NULL ; - qiv->nb_stack++; +GHashTable *h;if (qiv->nb_stack >=QIV_STACK_SIZE ) {error_set (errp,QERR_BUFFER_OVERRUN );return ; } + + qiv->stack[qiv->nb_stack].obj = obj; + qiv->stack[qiv->nb_stack].entry =NULL ; + qiv->stack[qiv->nb_stack].h =NULL ; + +if (qiv->strict &&qobject_type (obj) ==QTYPE_QDICT ) { + h =g_hash_table_new (g_str_hash, g_str_equal); +qdict_iter (qobject_to_qdict (obj), qdict_add_key, h); + qiv->stack[qiv->nb_stack].h = h; + } + + qiv->nb_stack++; }static void qmp_input_pop (QmpInputVisitor *qiv,Error **errp) { +GHashTableIter iter; +gpointer key; + +if (qiv->strict && qiv->stack[qiv->nb_stack -1 ].h) { +g_hash_table_iter_init (&iter, qiv->stack[qiv->nb_stack -1 ].h); +if (g_hash_table_iter_next (&iter, &key,NULL )) { +error_set (errp,QERR_QMP_EXTRA_MEMBER , (char *) key); + } +g_hash_table_unref (qiv->stack[qiv->nb_stack -1 ].h); + } +assert (qiv->nb_stack >0 ); qiv->nb_stack--; }
@@ -262,3 +294,13 @@ QmpInputVisitor *qmp_input_visitor_new(QObject *obj)return v; } + +QmpInputVisitor *qmp_input_visitor_new_strict (QObject *obj) +{ +QmpInputVisitor *v; + + v =qmp_input_visitor_new (obj); + v->strict = true; + +return v; +}
qapi/qmp-input-visitor.h +2 -0
@@ -20,6 +20,8 @@typedef struct QmpInputVisitor QmpInputVisitor ;QmpInputVisitor *qmp_input_visitor_new (QObject *obj); +QmpInputVisitor *qmp_input_visitor_new_strict (QObject *obj); +void qmp_input_visitor_cleanup (QmpInputVisitor *v);Visitor *qmp_input_get_visitor (QmpInputVisitor *v);
test-qmp-input-strict.c new file +234 -0
234 lines not on this page · raw message
tests/Makefile +4 -1
@@ -16,7 +16,7 @@ check-qfloat: check-qfloat.o qfloat.o $(tools-obj-y) check-qjson: check-qjson.o $(qobject-obj-y) $(tools-obj-y) test-coroutine: test-coroutine.o qemu-timer-common.o async.o $(coroutine-obj-y) $(tools-obj-y) -test-qmp-input-visitor.o test-qmp-output-visitor.o \ +test-qmp-input-visitor.o test-qmp-output-visitor.o test-qmp-input-strict.o \ test-string-input-visitor.o test-string-output-visitor.o \ test-qmp-commands.o: QEMU_CFLAGS += -I $(qapi-dir)
@@ -37,6 +37,9 @@ test-string-output-visitor: test-string-output-visitor.o $(qobject-obj-y) $(qapi test-string-input-visitor.o: $(addprefix $(qapi-dir)/, test-qapi-types.c test-qapi-types.h test-qapi-visit.c test-qapi-visit.h) $(qapi-obj-y) test-string-input-visitor: test-string-input-visitor.o $(qobject-obj-y) $(qapi-obj-y) $(tools-obj-y) $(qapi-dir)/test-qapi-visit.o $(qapi-dir)/test-qapi-types.o +test-qmp-input-strict.o: $(addprefix $(qapi-dir)/, test-qapi-types.c test-qapi-types.h test-qapi-visit.c test-qapi-visit.h) $(qapi-obj-y) +test-qmp-input-strict: test-qmp-input-strict.o $(qobject-obj-y) $(qapi-obj-y) $(tools-obj-y) $(qapi-dir)/test-qapi-visit.o $(qapi-dir)/test-qapi-types.o + test-qmp-output-visitor.o: $(addprefix $(qapi-dir)/, test-qapi-types.c test-qapi-types.h test-qapi-visit.c test-qapi-visit.h) $(qapi-obj-y) test-qmp-output-visitor: test-qmp-output-visitor.o $(qobject-obj-y) $(qapi-obj-y) $(tools-obj-y) $(qapi-dir)/test-qapi-visit.o $(qapi-dir)/test-qapi-types.o
Signature · 1 line
1.7.9.2.384.g4a92a
PATCH 11/13 qmp: add and use q type specifier
In reply to Luiz CapitulinoFrom: Paolo Bonzini <[email protected]>
"O" is being used by the transaction and qom-set commands to mean "any QObject", but it really means "do not validate the argument list". Add a new specifier with the correct meaning.
--- monitor.c | 3 +++ qmp-commands.hx | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-)
monitor.c +3 -0
@@ -4157,6 +4157,9 @@ static int check_client_args_type(const QDict *client_args,case 'O' :assert (flags &QMP_ACCEPT_UNKNOWNS );break ; +case 'q' : +/* Any QObject can be passed. */ +break ;case '/' :case '.' :/*
qmp-commands.hx +2 -2
@@ -708,7 +708,7 @@ EQMP }, { .name = "transaction", - .args_type = "actions:O", + .args_type = "actions:q", .mhandler.cmd_new = qmp_marshal_input_transaction, },
@@ -2125,7 +2125,7 @@ EQMP { .name = "qom-set", - .args_type = "path:s,property:s,opts:O", + .args_type = "path:s,property:s,value:q", .mhandler.cmd_new = qmp_qom_set, },
Signature · 1 line
1.7.9.2.384.g4a92a
PATCH 12/13 qmp: parse commands in strict mode
In reply to Luiz CapitulinoFrom: Paolo Bonzini <[email protected]>
--- scripts/qapi-commands.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/qapi-commands.py +1 -1
@@ -140,7 +140,7 @@ v = qapi_dealloc_get_visitor(md); ''') else: ret += mcgen(''' -mi = qmp_input_visitor_new(%(obj)s); +mi = qmp_input_visitor_new_strict(%(obj)s); v = qmp_input_get_visitor(mi); ''', obj=obj)
Signature · 1 line
1.7.9.2.384.g4a92a
PATCH 13/13 qmp: document strict parsing
In reply to Luiz CapitulinoFrom: Paolo Bonzini <[email protected]>
--- QMP/qmp-spec.txt | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-)
QMP/qmp-spec.txt +17 -3
@@ -209,13 +209,27 @@ incompatible way are disabled by default and will be advertised by the capabilities array (section '2.2 Server Greeting'). Thus, Clients can check that array and enable the capabilities they support. -Additionally, Clients must not assume any particular: - -- Size of json-objects or length of json-arrays +The QMP Server performs a type check on the arguments to a command. It +generates an error if a value does not have the expected type for its +key, or if it does not understand a key that the Client included. The +strictness of the Server catches wrong assumptions of Clients about +the Server's schema. Clients can assume that, when such validation +errors occur, they will be reported before the command generated any +side effect. + +However, Clients must not assume any particular: + +- Length of json-arrays +- Size of json-objects; in particular, future versions of QEMU may add + new keys and Clients should be able to ignore them. - Order of json-object members or json-array elements - Amount of errors generated by a command, that is, new errors can be added to any existing command in newer versions of the Server +Of course, the Server does guarantee to send valid JSON. But apart from +this, a Client should be "conservative in what they send, and liberal in +what they accept". + 6. Downstream extension of QMP ------------------------------
Signature · 1 line
1.7.9.2.384.g4a92a
Quoted 6 lines · Luiz Capitulino
All of them are QAPI fixes.
The changes (since
8a22565b7c2d1920b02b94e7a8021c65895a3a22) are available in the following repository:
git://repo.or.cz/qemu/qmp-unstable.gitqueue/qmp
Pulled. Thanks.
Regards,
Anthony Liguori
Quoted 36 lines
Laszlo Ersek (1):
qapi: fix double free inqmp_output_visitor_cleanup()Paolo Bonzini (12):
qapi: add struct-errors test case to test-qmp-output-visitor
qapi: add a test case for type errors
qapi: fail hard on stack imbalance
qapi: fix memory leak on error
qapi: shortcut visits on errors
qapi: allow freeing partially-allocated objects
qapi: untanglenext_list
qapi: place outermost object on qiv stack
qapi: add strict mode to input visitor
qmp: add and use q type specifier
qmp: parse commands in strict mode
qmp: document strict parsingQMP/qmp-spec.txt | 20 +++- docs/qapi-code-gen.txt | 4 +- monitor.c | 3 + qapi-schema-test.json | 2 +- qapi/qmp-input-visitor.c | 120 +++++++++++++++-------- qapi/qmp-input-visitor.h | 2 + qapi/qmp-output-visitor.c | 8 +- qmp-commands.hx | 4 +- scripts/qapi-commands.py | 2 +- scripts/qapi-visit.py | 20 +++- test-qmp-input-strict.c | 234 +++++++++++++++++++++++++++++++++++++++++++++ test-qmp-input-visitor.c | 19 ++++ test-qmp-output-visitor.c | 20 ++++ tests/Makefile | 5 +- 14 files changed, 407 insertions(+), 56 deletions(-)