unicorn/qemu/qapi/qmp-input-visitor.c

408 lines
10 KiB
C
Raw Normal View History

2015-08-21 09:04:50 +02:00
/*
* Input Visitor
*
* 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.
*
*/
#include "qemu/osdep.h"
#include "qapi/error.h"
2015-08-21 09:04:50 +02:00
#include "qapi/qmp-input-visitor.h"
#include "qapi/visitor-impl.h"
#include "qemu/queue.h"
#include "qemu-common.h"
#include "qapi/qmp/types.h"
#include "qapi/qmp/qerror.h"
#define QIV_STACK_SIZE 1024
typedef struct StackObject
{
QObject *obj; /* Object being visited */
GHashTable *h; /* If obj is dict: unvisited keys */
const QListEntry *entry; /* If obj is list: unvisited tail */
qmp-input: Refactor when list is advanced In the QMP input visitor, visiting a list traverses two objects: the QAPI GenericList of the caller (which gets advanced in visit_next_list() regardless of this patch), and the QList input that we are converting to QAPI. For consistency with QDict visits, we want to consume elements from the input QList during the visit_type_FOO() for the list element; that is, we want ALL the code for consuming an input to live in qmp_input_get_object(), rather than having it split according to whether we are visiting a dict or a list. Making qmp_input_get_object() the common point of consumption will make it easier for a later patch to refactor visit_start_list() to cover the GenericList * head of a QAPI list, and in turn will get rid of the 'first' flag (which lived in qmp_input_next_list() pre-patch, and is hoisted to StackObject by this patch). This patch is therefore altering the post-condition use of 'entry', while keeping what gets visited unchanged, from: start_list next_list type_ELT ... next_list type_ELT next_list end_list visits 1st elt last elt entry NULL 1st elt 1st elt last elt last elt NULL gone where type_ELT() returns (entry ? entry : 1st elt) and next_list() steps entry to this usage: start_list next_list type_ELT ... next_list type_ELT next_list end_list visits 1st elt last elt entry 1st elt 1nd elt 2nd elt last elt NULL NULL gone where type_ELT() steps entry and returns the old entry, and next_list() leaves entry alone. Backports commit fcf3cb21783b2dae3358fdbe7001cb2f74e0cedf from qemu
2018-02-23 21:19:37 +01:00
bool first; /* If obj is list: next_list() not yet called? */
2015-08-21 09:04:50 +02:00
} StackObject;
struct QmpInputVisitor
{
Visitor visitor;
/* Root of visit at visitor creation. */
QObject *root;
/* Stack of objects being visited (all entries will be either
* QDict or QList). */
2015-08-21 09:04:50 +02:00
StackObject stack[QIV_STACK_SIZE];
int nb_stack;
/* True to reject parse in visit_end_struct() if unvisited keys remain. */
2015-08-21 09:04:50 +02:00
bool strict;
};
static QmpInputVisitor *to_qiv(Visitor *v)
{
return container_of(v, QmpInputVisitor, visitor);
}
static QObject *qmp_input_get_object(QmpInputVisitor *qiv,
const char *name,
bool consume)
{
StackObject *tos;
QObject *qobj;
QObject *ret;
if (!qiv->nb_stack) {
/* Starting at root, name is ignored. */
return qiv->root;
}
/* We are in a container; find the next element. */
tos = &qiv->stack[qiv->nb_stack - 1];
qobj = tos->obj;
assert(qobj);
if (qobject_type(qobj) == QTYPE_QDICT) {
assert(name);
ret = qdict_get(qobject_to_qdict(qobj), name);
if (tos->h && consume && ret) {
bool removed = g_hash_table_remove(tos->h, name);
assert(removed);
2015-08-21 09:04:50 +02:00
}
} else {
assert(qobject_type(qobj) == QTYPE_QLIST);
assert(!name);
qmp-input: Refactor when list is advanced In the QMP input visitor, visiting a list traverses two objects: the QAPI GenericList of the caller (which gets advanced in visit_next_list() regardless of this patch), and the QList input that we are converting to QAPI. For consistency with QDict visits, we want to consume elements from the input QList during the visit_type_FOO() for the list element; that is, we want ALL the code for consuming an input to live in qmp_input_get_object(), rather than having it split according to whether we are visiting a dict or a list. Making qmp_input_get_object() the common point of consumption will make it easier for a later patch to refactor visit_start_list() to cover the GenericList * head of a QAPI list, and in turn will get rid of the 'first' flag (which lived in qmp_input_next_list() pre-patch, and is hoisted to StackObject by this patch). This patch is therefore altering the post-condition use of 'entry', while keeping what gets visited unchanged, from: start_list next_list type_ELT ... next_list type_ELT next_list end_list visits 1st elt last elt entry NULL 1st elt 1st elt last elt last elt NULL gone where type_ELT() returns (entry ? entry : 1st elt) and next_list() steps entry to this usage: start_list next_list type_ELT ... next_list type_ELT next_list end_list visits 1st elt last elt entry 1st elt 1nd elt 2nd elt last elt NULL NULL gone where type_ELT() steps entry and returns the old entry, and next_list() leaves entry alone. Backports commit fcf3cb21783b2dae3358fdbe7001cb2f74e0cedf from qemu
2018-02-23 21:19:37 +01:00
assert(!tos->first);
ret = qlist_entry_obj(tos->entry);
qmp-input: Refactor when list is advanced In the QMP input visitor, visiting a list traverses two objects: the QAPI GenericList of the caller (which gets advanced in visit_next_list() regardless of this patch), and the QList input that we are converting to QAPI. For consistency with QDict visits, we want to consume elements from the input QList during the visit_type_FOO() for the list element; that is, we want ALL the code for consuming an input to live in qmp_input_get_object(), rather than having it split according to whether we are visiting a dict or a list. Making qmp_input_get_object() the common point of consumption will make it easier for a later patch to refactor visit_start_list() to cover the GenericList * head of a QAPI list, and in turn will get rid of the 'first' flag (which lived in qmp_input_next_list() pre-patch, and is hoisted to StackObject by this patch). This patch is therefore altering the post-condition use of 'entry', while keeping what gets visited unchanged, from: start_list next_list type_ELT ... next_list type_ELT next_list end_list visits 1st elt last elt entry NULL 1st elt 1st elt last elt last elt NULL gone where type_ELT() returns (entry ? entry : 1st elt) and next_list() steps entry to this usage: start_list next_list type_ELT ... next_list type_ELT next_list end_list visits 1st elt last elt entry 1st elt 1nd elt 2nd elt last elt NULL NULL gone where type_ELT() steps entry and returns the old entry, and next_list() leaves entry alone. Backports commit fcf3cb21783b2dae3358fdbe7001cb2f74e0cedf from qemu
2018-02-23 21:19:37 +01:00
if (consume) {
tos->entry = qlist_next(tos->entry);
}
2015-08-21 09:04:50 +02:00
}
return ret;
2015-08-21 09:04:50 +02:00
}
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)
{
GHashTable *h;
StackObject *tos = &qiv->stack[qiv->nb_stack];
2015-08-21 09:04:50 +02:00
assert(obj);
2015-08-21 09:04:50 +02:00
if (qiv->nb_stack >= QIV_STACK_SIZE) {
error_setg(errp, "An internal buffer overran");
return;
}
tos->obj = obj;
qmp-input: Refactor when list is advanced In the QMP input visitor, visiting a list traverses two objects: the QAPI GenericList of the caller (which gets advanced in visit_next_list() regardless of this patch), and the QList input that we are converting to QAPI. For consistency with QDict visits, we want to consume elements from the input QList during the visit_type_FOO() for the list element; that is, we want ALL the code for consuming an input to live in qmp_input_get_object(), rather than having it split according to whether we are visiting a dict or a list. Making qmp_input_get_object() the common point of consumption will make it easier for a later patch to refactor visit_start_list() to cover the GenericList * head of a QAPI list, and in turn will get rid of the 'first' flag (which lived in qmp_input_next_list() pre-patch, and is hoisted to StackObject by this patch). This patch is therefore altering the post-condition use of 'entry', while keeping what gets visited unchanged, from: start_list next_list type_ELT ... next_list type_ELT next_list end_list visits 1st elt last elt entry NULL 1st elt 1st elt last elt last elt NULL gone where type_ELT() returns (entry ? entry : 1st elt) and next_list() steps entry to this usage: start_list next_list type_ELT ... next_list type_ELT next_list end_list visits 1st elt last elt entry 1st elt 1nd elt 2nd elt last elt NULL NULL gone where type_ELT() steps entry and returns the old entry, and next_list() leaves entry alone. Backports commit fcf3cb21783b2dae3358fdbe7001cb2f74e0cedf from qemu
2018-02-23 21:19:37 +01:00
assert(!tos->h);
assert(!tos->entry);
2015-08-21 09:04:50 +02:00
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);
tos->h = h;
qmp-input: Refactor when list is advanced In the QMP input visitor, visiting a list traverses two objects: the QAPI GenericList of the caller (which gets advanced in visit_next_list() regardless of this patch), and the QList input that we are converting to QAPI. For consistency with QDict visits, we want to consume elements from the input QList during the visit_type_FOO() for the list element; that is, we want ALL the code for consuming an input to live in qmp_input_get_object(), rather than having it split according to whether we are visiting a dict or a list. Making qmp_input_get_object() the common point of consumption will make it easier for a later patch to refactor visit_start_list() to cover the GenericList * head of a QAPI list, and in turn will get rid of the 'first' flag (which lived in qmp_input_next_list() pre-patch, and is hoisted to StackObject by this patch). This patch is therefore altering the post-condition use of 'entry', while keeping what gets visited unchanged, from: start_list next_list type_ELT ... next_list type_ELT next_list end_list visits 1st elt last elt entry NULL 1st elt 1st elt last elt last elt NULL gone where type_ELT() returns (entry ? entry : 1st elt) and next_list() steps entry to this usage: start_list next_list type_ELT ... next_list type_ELT next_list end_list visits 1st elt last elt entry 1st elt 1nd elt 2nd elt last elt NULL NULL gone where type_ELT() steps entry and returns the old entry, and next_list() leaves entry alone. Backports commit fcf3cb21783b2dae3358fdbe7001cb2f74e0cedf from qemu
2018-02-23 21:19:37 +01:00
} else if (qobject_type(obj) == QTYPE_QLIST) {
tos->entry = qlist_first(qobject_to_qlist(obj));
tos->first = true;
2015-08-21 09:04:50 +02:00
}
qiv->nb_stack++;
}
/** Only for qmp_input_pop. */
static gboolean always_true(gpointer key, gpointer val, gpointer user_pkey)
{
*(const char **)user_pkey = (const char *)key;
return TRUE;
}
static void qmp_input_pop(QmpInputVisitor *qiv, Error **errp)
{
qmp-input: Refactor when list is advanced In the QMP input visitor, visiting a list traverses two objects: the QAPI GenericList of the caller (which gets advanced in visit_next_list() regardless of this patch), and the QList input that we are converting to QAPI. For consistency with QDict visits, we want to consume elements from the input QList during the visit_type_FOO() for the list element; that is, we want ALL the code for consuming an input to live in qmp_input_get_object(), rather than having it split according to whether we are visiting a dict or a list. Making qmp_input_get_object() the common point of consumption will make it easier for a later patch to refactor visit_start_list() to cover the GenericList * head of a QAPI list, and in turn will get rid of the 'first' flag (which lived in qmp_input_next_list() pre-patch, and is hoisted to StackObject by this patch). This patch is therefore altering the post-condition use of 'entry', while keeping what gets visited unchanged, from: start_list next_list type_ELT ... next_list type_ELT next_list end_list visits 1st elt last elt entry NULL 1st elt 1st elt last elt last elt NULL gone where type_ELT() returns (entry ? entry : 1st elt) and next_list() steps entry to this usage: start_list next_list type_ELT ... next_list type_ELT next_list end_list visits 1st elt last elt entry 1st elt 1nd elt 2nd elt last elt NULL NULL gone where type_ELT() steps entry and returns the old entry, and next_list() leaves entry alone. Backports commit fcf3cb21783b2dae3358fdbe7001cb2f74e0cedf from qemu
2018-02-23 21:19:37 +01:00
StackObject *tos = &qiv->stack[qiv->nb_stack - 1];
2015-08-21 09:04:50 +02:00
assert(qiv->nb_stack > 0);
if (qiv->strict) {
qmp-input: Refactor when list is advanced In the QMP input visitor, visiting a list traverses two objects: the QAPI GenericList of the caller (which gets advanced in visit_next_list() regardless of this patch), and the QList input that we are converting to QAPI. For consistency with QDict visits, we want to consume elements from the input QList during the visit_type_FOO() for the list element; that is, we want ALL the code for consuming an input to live in qmp_input_get_object(), rather than having it split according to whether we are visiting a dict or a list. Making qmp_input_get_object() the common point of consumption will make it easier for a later patch to refactor visit_start_list() to cover the GenericList * head of a QAPI list, and in turn will get rid of the 'first' flag (which lived in qmp_input_next_list() pre-patch, and is hoisted to StackObject by this patch). This patch is therefore altering the post-condition use of 'entry', while keeping what gets visited unchanged, from: start_list next_list type_ELT ... next_list type_ELT next_list end_list visits 1st elt last elt entry NULL 1st elt 1st elt last elt last elt NULL gone where type_ELT() returns (entry ? entry : 1st elt) and next_list() steps entry to this usage: start_list next_list type_ELT ... next_list type_ELT next_list end_list visits 1st elt last elt entry 1st elt 1nd elt 2nd elt last elt NULL NULL gone where type_ELT() steps entry and returns the old entry, and next_list() leaves entry alone. Backports commit fcf3cb21783b2dae3358fdbe7001cb2f74e0cedf from qemu
2018-02-23 21:19:37 +01:00
GHashTable *const top_ht = tos->h;
2015-08-21 09:04:50 +02:00
if (top_ht) {
if (g_hash_table_size(top_ht)) {
const char *key;
g_hash_table_find(top_ht, always_true, (gpointer)&key);
error_setg(errp, QERR_QMP_EXTRA_MEMBER, key);
2015-08-21 09:04:50 +02:00
}
g_hash_table_unref(top_ht);
}
qmp-input: Refactor when list is advanced In the QMP input visitor, visiting a list traverses two objects: the QAPI GenericList of the caller (which gets advanced in visit_next_list() regardless of this patch), and the QList input that we are converting to QAPI. For consistency with QDict visits, we want to consume elements from the input QList during the visit_type_FOO() for the list element; that is, we want ALL the code for consuming an input to live in qmp_input_get_object(), rather than having it split according to whether we are visiting a dict or a list. Making qmp_input_get_object() the common point of consumption will make it easier for a later patch to refactor visit_start_list() to cover the GenericList * head of a QAPI list, and in turn will get rid of the 'first' flag (which lived in qmp_input_next_list() pre-patch, and is hoisted to StackObject by this patch). This patch is therefore altering the post-condition use of 'entry', while keeping what gets visited unchanged, from: start_list next_list type_ELT ... next_list type_ELT next_list end_list visits 1st elt last elt entry NULL 1st elt 1st elt last elt last elt NULL gone where type_ELT() returns (entry ? entry : 1st elt) and next_list() steps entry to this usage: start_list next_list type_ELT ... next_list type_ELT next_list end_list visits 1st elt last elt entry 1st elt 1nd elt 2nd elt last elt NULL NULL gone where type_ELT() steps entry and returns the old entry, and next_list() leaves entry alone. Backports commit fcf3cb21783b2dae3358fdbe7001cb2f74e0cedf from qemu
2018-02-23 21:19:37 +01:00
tos->h = NULL;
2015-08-21 09:04:50 +02:00
}
qiv->nb_stack--;
}
static void qmp_input_start_struct(Visitor *v, const char *name, void **obj,
size_t size, Error **errp)
2015-08-21 09:04:50 +02:00
{
QmpInputVisitor *qiv = to_qiv(v);
QObject *qobj = qmp_input_get_object(qiv, name, false);
2015-08-21 09:04:50 +02:00
Error *err = NULL;
if (obj) {
*obj = NULL;
}
2015-08-21 09:04:50 +02:00
if (!qobj || qobject_type(qobj) != QTYPE_QDICT) {
error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
"QDict");
2015-08-21 09:04:50 +02:00
return;
}
qmp_input_push(qiv, qobj, &err);
if (err) {
error_propagate(errp, err);
return;
}
if (obj) {
*obj = g_malloc0(size);
}
}
static void qmp_input_end_struct(Visitor *v, Error **errp)
{
QmpInputVisitor *qiv = to_qiv(v);
qmp_input_pop(qiv, &error_abort);
2015-08-21 09:04:50 +02:00
}
static void qmp_input_start_implicit_struct(Visitor *v, void **obj,
size_t size, Error **errp)
{
if (obj) {
*obj = g_malloc0(size);
}
}
static void qmp_input_end_implicit_struct(Visitor *v)
2015-08-21 09:04:50 +02:00
{
}
static void qmp_input_start_list(Visitor *v, const char *name, Error **errp)
{
QmpInputVisitor *qiv = to_qiv(v);
QObject *qobj = qmp_input_get_object(qiv, name, true);
if (!qobj || qobject_type(qobj) != QTYPE_QLIST) {
error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
"list");
2015-08-21 09:04:50 +02:00
return;
}
qmp_input_push(qiv, qobj, errp);
}
qapi: Adjust layout of FooList types By sticking the next pointer first, we don't need a union with 64-bit padding for smaller types. On 32-bit platforms, this can reduce the size of uint8List from 16 bytes (or 12, depending on whether 64-bit ints can tolerate 4-byte alignment) down to 8. It has no effect on 64-bit platforms (where alignment still dictates a 16-byte struct); but fewer anonymous unions is still a win in my book. It requires visit_next_list() to gain a size parameter, to know what size element to allocate; comparable to the size parameter of visit_start_struct(). I debated about going one step further, to allow for fewer casts, by doing: typedef GenericList GenericList; struct GenericList { GenericList *next; }; struct FooList { GenericList base; Foo *value; }; so that you convert to 'GenericList *' by '&foolist->base', and back by 'container_of(generic, GenericList, base)' (as opposed to the existing '(GenericList *)foolist' and '(FooList *)generic'). But doing that would require hoisting the declaration of GenericList prior to inclusion of qapi-types.h, rather than its current spot in visitor.h; it also makes iteration a bit more verbose through 'foolist->base.next' instead of 'foolist->next'. Note that for lists of objects, the 'value' payload is still hidden behind a boxed pointer. Someday, it would be nice to do: struct FooList { FooList *next; Foo value; }; for one less level of malloc for each list element. This patch is a step in that direction (now that 'next' is no longer at a fixed non-zero offset within the struct, we can store more than just a pointer's-worth of data as the value payload), but the actual conversion would be a task for another series, as it will touch a lot of code. Backports commit e65d89bf1a4484e0db0f3dc820a8b209f2fb1e8b from qemu
2018-02-23 20:48:57 +01:00
static GenericList *qmp_input_next_list(Visitor *v, GenericList **list,
size_t size)
2015-08-21 09:04:50 +02:00
{
QmpInputVisitor *qiv = to_qiv(v);
GenericList *entry;
StackObject *so = &qiv->stack[qiv->nb_stack - 1];
qmp-input: Refactor when list is advanced In the QMP input visitor, visiting a list traverses two objects: the QAPI GenericList of the caller (which gets advanced in visit_next_list() regardless of this patch), and the QList input that we are converting to QAPI. For consistency with QDict visits, we want to consume elements from the input QList during the visit_type_FOO() for the list element; that is, we want ALL the code for consuming an input to live in qmp_input_get_object(), rather than having it split according to whether we are visiting a dict or a list. Making qmp_input_get_object() the common point of consumption will make it easier for a later patch to refactor visit_start_list() to cover the GenericList * head of a QAPI list, and in turn will get rid of the 'first' flag (which lived in qmp_input_next_list() pre-patch, and is hoisted to StackObject by this patch). This patch is therefore altering the post-condition use of 'entry', while keeping what gets visited unchanged, from: start_list next_list type_ELT ... next_list type_ELT next_list end_list visits 1st elt last elt entry NULL 1st elt 1st elt last elt last elt NULL gone where type_ELT() returns (entry ? entry : 1st elt) and next_list() steps entry to this usage: start_list next_list type_ELT ... next_list type_ELT next_list end_list visits 1st elt last elt entry 1st elt 1nd elt 2nd elt last elt NULL NULL gone where type_ELT() steps entry and returns the old entry, and next_list() leaves entry alone. Backports commit fcf3cb21783b2dae3358fdbe7001cb2f74e0cedf from qemu
2018-02-23 21:19:37 +01:00
if (!so->entry) {
2015-08-21 09:04:50 +02:00
return NULL;
}
qapi: Adjust layout of FooList types By sticking the next pointer first, we don't need a union with 64-bit padding for smaller types. On 32-bit platforms, this can reduce the size of uint8List from 16 bytes (or 12, depending on whether 64-bit ints can tolerate 4-byte alignment) down to 8. It has no effect on 64-bit platforms (where alignment still dictates a 16-byte struct); but fewer anonymous unions is still a win in my book. It requires visit_next_list() to gain a size parameter, to know what size element to allocate; comparable to the size parameter of visit_start_struct(). I debated about going one step further, to allow for fewer casts, by doing: typedef GenericList GenericList; struct GenericList { GenericList *next; }; struct FooList { GenericList base; Foo *value; }; so that you convert to 'GenericList *' by '&foolist->base', and back by 'container_of(generic, GenericList, base)' (as opposed to the existing '(GenericList *)foolist' and '(FooList *)generic'). But doing that would require hoisting the declaration of GenericList prior to inclusion of qapi-types.h, rather than its current spot in visitor.h; it also makes iteration a bit more verbose through 'foolist->base.next' instead of 'foolist->next'. Note that for lists of objects, the 'value' payload is still hidden behind a boxed pointer. Someday, it would be nice to do: struct FooList { FooList *next; Foo value; }; for one less level of malloc for each list element. This patch is a step in that direction (now that 'next' is no longer at a fixed non-zero offset within the struct, we can store more than just a pointer's-worth of data as the value payload), but the actual conversion would be a task for another series, as it will touch a lot of code. Backports commit e65d89bf1a4484e0db0f3dc820a8b209f2fb1e8b from qemu
2018-02-23 20:48:57 +01:00
entry = g_malloc0(size);
qmp-input: Refactor when list is advanced In the QMP input visitor, visiting a list traverses two objects: the QAPI GenericList of the caller (which gets advanced in visit_next_list() regardless of this patch), and the QList input that we are converting to QAPI. For consistency with QDict visits, we want to consume elements from the input QList during the visit_type_FOO() for the list element; that is, we want ALL the code for consuming an input to live in qmp_input_get_object(), rather than having it split according to whether we are visiting a dict or a list. Making qmp_input_get_object() the common point of consumption will make it easier for a later patch to refactor visit_start_list() to cover the GenericList * head of a QAPI list, and in turn will get rid of the 'first' flag (which lived in qmp_input_next_list() pre-patch, and is hoisted to StackObject by this patch). This patch is therefore altering the post-condition use of 'entry', while keeping what gets visited unchanged, from: start_list next_list type_ELT ... next_list type_ELT next_list end_list visits 1st elt last elt entry NULL 1st elt 1st elt last elt last elt NULL gone where type_ELT() returns (entry ? entry : 1st elt) and next_list() steps entry to this usage: start_list next_list type_ELT ... next_list type_ELT next_list end_list visits 1st elt last elt entry 1st elt 1nd elt 2nd elt last elt NULL NULL gone where type_ELT() steps entry and returns the old entry, and next_list() leaves entry alone. Backports commit fcf3cb21783b2dae3358fdbe7001cb2f74e0cedf from qemu
2018-02-23 21:19:37 +01:00
if (so->first) {
2015-08-21 09:04:50 +02:00
*list = entry;
qmp-input: Refactor when list is advanced In the QMP input visitor, visiting a list traverses two objects: the QAPI GenericList of the caller (which gets advanced in visit_next_list() regardless of this patch), and the QList input that we are converting to QAPI. For consistency with QDict visits, we want to consume elements from the input QList during the visit_type_FOO() for the list element; that is, we want ALL the code for consuming an input to live in qmp_input_get_object(), rather than having it split according to whether we are visiting a dict or a list. Making qmp_input_get_object() the common point of consumption will make it easier for a later patch to refactor visit_start_list() to cover the GenericList * head of a QAPI list, and in turn will get rid of the 'first' flag (which lived in qmp_input_next_list() pre-patch, and is hoisted to StackObject by this patch). This patch is therefore altering the post-condition use of 'entry', while keeping what gets visited unchanged, from: start_list next_list type_ELT ... next_list type_ELT next_list end_list visits 1st elt last elt entry NULL 1st elt 1st elt last elt last elt NULL gone where type_ELT() returns (entry ? entry : 1st elt) and next_list() steps entry to this usage: start_list next_list type_ELT ... next_list type_ELT next_list end_list visits 1st elt last elt entry 1st elt 1nd elt 2nd elt last elt NULL NULL gone where type_ELT() steps entry and returns the old entry, and next_list() leaves entry alone. Backports commit fcf3cb21783b2dae3358fdbe7001cb2f74e0cedf from qemu
2018-02-23 21:19:37 +01:00
so->first = false;
2015-08-21 09:04:50 +02:00
} else {
(*list)->next = entry;
}
return entry;
}
static void qmp_input_end_list(Visitor *v)
2015-08-21 09:04:50 +02:00
{
QmpInputVisitor *qiv = to_qiv(v);
qmp_input_pop(qiv, &error_abort);
2015-08-21 09:04:50 +02:00
}
static void qmp_input_get_next_type(Visitor *v, const char *name, QType *type,
bool promote_int, Error **errp)
2015-08-21 09:04:50 +02:00
{
QmpInputVisitor *qiv = to_qiv(v);
QObject *qobj = qmp_input_get_object(qiv, name, false);
if (!qobj) {
error_setg(errp, QERR_MISSING_PARAMETER, name ? name : "null");
2015-08-21 09:04:50 +02:00
return;
}
if (promote_int && *type == QTYPE_QINT) {
*type = QTYPE_QFLOAT;
}
qapi: Simplify visiting of alternate types Previously, working with alternates required two lookup arrays and some indirection: for type Foo, we created Foo_qtypes[] which maps each qtype to a value of the generated FooKind enum, then look up that value in FooKind_lookup[] like we do for other union types. This has a couple of subtle bugs. First, the generator was creating a call with a parameter '(int *) &(*obj)->type' where type is an enum type; this is unsafe if the compiler chooses to store the enum type in a different size than int, where assigning through the wrong size pointer can corrupt data or cause a SIGBUS. Related bug, not not fixed in this patch: qapi-visit.py's gen_visit_enum() generates a cast of its enum * argument to int *. Marked FIXME. Second, since the values of the FooKind enum start at zero, all entries of the Foo_qtypes[] array that were not explicitly initialized will map to the same branch of the union as the first member of the alternate, rather than triggering a desired failure in visit_get_next_type(). Fortunately, the bug seldom bites; the very next thing the input visitor does is try to parse the incoming JSON with the wrong parser, which normally fails; the output visitor is not used with a C struct in that state, and the dealloc visitor has nothing to clean up (so there is no leak). However, the second bug IS observable in one case: parsing an integer causes unusual behavior in an alternate that contains at least a 'number' member but no 'int' member, because the 'number' parser accepts QTYPE_QINT in addition to the expected QTYPE_QFLOAT (that is, since 'int' is not a member, the type QTYPE_QINT accidentally maps to FooKind 0; if this enum value is the 'number' branch the integer parses successfully, but if the 'number' branch is not first, some other branch tries to parse the integer and rejects it). A later patch will worry about fixing alternates to always parse all inputs that a non-alternate 'number' would accept, for now this is still marked FIXME in the updated test-qmp-input-visitor.c, to merely point out that new undesired behavior of 'ans' matches the existing undesired behavior of 'asn'. This patch fixes the default-initialization bug by deleting the indirection, and modifying get_next_type() to directly assign a QTypeCode parameter. This in turn fixes the type-casting bug, as we are no longer casting a pointer to enum to a questionable size. There is no longer a need to generate an implicit FooKind enum associated with the alternate type (since the QMP wire format never uses the stringized counterparts of the C union member names). Since the updated visit_get_next_type() does not know which qtypes are expected, the generated visitor is modified to generate an error statement if an unexpected type is encountered. Callers now have to know the QTYPE_* mapping when looking at the discriminator; but so far, only the testsuite was even using the C struct of an alternate types. I considered the possibility of keeping the internal enum FooKind, but initialized differently than most generated arrays, as in: typedef enum FooKind { FOO_KIND_A = QTYPE_QDICT, FOO_KIND_B = QTYPE_QINT, } FooKind; to create nicer aliases for knowing when to use foo->a or foo->b when inspecting foo->type; but it turned out to add too much complexity, especially without a client. There is a user-visible side effect to this change, but I consider it to be an improvement. Previously, the invalid QMP command: {"execute":"blockdev-add", "arguments":{"options": {"driver":"raw", "id":"a", "file":true}}} failed with: {"error": {"class": "GenericError", "desc": "Invalid parameter type for 'file', expected: QDict"}} (visit_get_next_type() succeeded, and the error comes from the visit_type_BlockdevOptions() expecting {}; there is no mention of the fact that a string would also work). Now it fails with: {"error": {"class": "GenericError", "desc": "Invalid parameter type for 'file', expected: BlockdevRef"}} (the error when the next type doesn't match any expected types for the overall alternate). Backports commit 0426d53c6530606bf7641b83f2b755fe61c280ee from qemu
2018-02-20 03:51:35 +01:00
*type = qobject_type(qobj);
2015-08-21 09:04:50 +02:00
}
static void qmp_input_type_int64(Visitor *v, const char *name, int64_t *obj,
Error **errp)
2015-08-21 09:04:50 +02:00
{
QmpInputVisitor *qiv = to_qiv(v);
QInt *qint = qobject_to_qint(qmp_input_get_object(qiv, name, true));
2015-08-21 09:04:50 +02:00
if (!qint) {
error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
"integer");
2015-08-21 09:04:50 +02:00
return;
}
*obj = qint_get_int(qint);
2015-08-21 09:04:50 +02:00
}
static void qmp_input_type_uint64(Visitor *v, const char *name, uint64_t *obj,
Error **errp)
{
/* FIXME: qobject_to_qint mishandles values over INT64_MAX */
QmpInputVisitor *qiv = to_qiv(v);
QInt *qint = qobject_to_qint(qmp_input_get_object(qiv, name, true));
if (!qint) {
error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
"integer");
return;
}
*obj = qint_get_int(qint);
}
static void qmp_input_type_bool(Visitor *v, const char *name, bool *obj,
2015-08-21 09:04:50 +02:00
Error **errp)
{
QmpInputVisitor *qiv = to_qiv(v);
QBool *qbool = qobject_to_qbool(qmp_input_get_object(qiv, name, true));
2015-08-21 09:04:50 +02:00
if (!qbool) {
error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
"boolean");
2015-08-21 09:04:50 +02:00
return;
}
*obj = qbool_get_bool(qbool);
2015-08-21 09:04:50 +02:00
}
static void qmp_input_type_str(Visitor *v, const char *name, char **obj,
2015-08-21 09:04:50 +02:00
Error **errp)
{
QmpInputVisitor *qiv = to_qiv(v);
QString *qstr = qobject_to_qstring(qmp_input_get_object(qiv, name, true));
2015-08-21 09:04:50 +02:00
if (!qstr) {
*obj = NULL;
error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
"string");
2015-08-21 09:04:50 +02:00
return;
}
*obj = g_strdup(qstring_get_str(qstr));
2015-08-21 09:04:50 +02:00
}
static void qmp_input_type_number(Visitor *v, const char *name, double *obj,
2015-08-21 09:04:50 +02:00
Error **errp)
{
QmpInputVisitor *qiv = to_qiv(v);
QObject *qobj = qmp_input_get_object(qiv, name, true);
QInt *qint;
QFloat *qfloat;
2015-08-21 09:04:50 +02:00
qint = qobject_to_qint(qobj);
if (qint) {
*obj = qint_get_int(qobject_to_qint(qobj));
2015-08-21 09:04:50 +02:00
return;
}
qfloat = qobject_to_qfloat(qobj);
if (qfloat) {
2015-08-21 09:04:50 +02:00
*obj = qfloat_get_double(qobject_to_qfloat(qobj));
return;
2015-08-21 09:04:50 +02:00
}
error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
"number");
2015-08-21 09:04:50 +02:00
}
static void qmp_input_type_any(Visitor *v, const char *name, QObject **obj,
Error **errp)
{
QmpInputVisitor *qiv = to_qiv(v);
QObject *qobj = qmp_input_get_object(qiv, name, true);
qobject_incref(qobj);
*obj = qobj;
}
static void qmp_input_optional(Visitor *v, const char *name, bool *present)
2015-08-21 09:04:50 +02:00
{
QmpInputVisitor *qiv = to_qiv(v);
QObject *qobj = qmp_input_get_object(qiv, name, true);
if (!qobj) {
*present = false;
return;
}
*present = true;
}
Visitor *qmp_input_get_visitor(QmpInputVisitor *v)
{
return &v->visitor;
}
void qmp_input_visitor_cleanup(QmpInputVisitor *v)
{
qobject_decref(v->root);
g_free(v);
2015-08-21 09:04:50 +02:00
}
QmpInputVisitor *qmp_input_visitor_new(QObject *obj, bool strict)
2015-08-21 09:04:50 +02:00
{
QmpInputVisitor *v;
v = g_malloc0(sizeof(*v));
v->visitor.type = VISITOR_INPUT;
2015-08-21 09:04:50 +02:00
v->visitor.start_struct = qmp_input_start_struct;
v->visitor.end_struct = qmp_input_end_struct;
v->visitor.start_implicit_struct = qmp_input_start_implicit_struct;
v->visitor.end_implicit_struct = qmp_input_end_implicit_struct;
v->visitor.start_list = qmp_input_start_list;
v->visitor.next_list = qmp_input_next_list;
v->visitor.end_list = qmp_input_end_list;
v->visitor.type_int64 = qmp_input_type_int64;
v->visitor.type_uint64 = qmp_input_type_uint64;
2015-08-21 09:04:50 +02:00
v->visitor.type_bool = qmp_input_type_bool;
v->visitor.type_str = qmp_input_type_str;
v->visitor.type_number = qmp_input_type_number;
v->visitor.type_any = qmp_input_type_any;
2015-08-21 09:04:50 +02:00
v->visitor.optional = qmp_input_optional;
v->strict = strict;
2015-08-21 09:04:50 +02:00
v->visitor.get_next_type = qmp_input_get_next_type;
v->root = obj;
2015-08-21 09:04:50 +02:00
qobject_incref(obj);
return v;
}