qapi visitor cleanups (post-introspection cleanups subset E)
Subsystems (3)
Re: [Qemu-devel] [PATCH v8 34/35] qapi: Change visit_type_FOO() to no longer return partial objects
In reply to Eric BlakeHi
Quoted 12 lines · Eric Blake
Returning a partial object on error is an invitation for a careless caller to leak memory. As no one outside the testsuite was actually relying on these semantics, it is cleaner to just document and guarantee that ALL pointer-based
visit_type_FOO()functions always leave a safe value in *obj during an input visitor (either the new object on success, or NULL if an error is encountered).Since input visitors have blind assignment semantics, we have to track the result of whether an assignment is made all the way down to each visitor callback implementation, to avoid making decisions based on potentially uninitialized storage.
Quoted 8 lines
Note that we still leave *obj unchanged after a scalar-based
visit_type_FOO(); I did not feel like auditing all uses ofvisit_type_Enum()to see if the callers would tolerate a specific sentinel value (not to mention having to decide whether it would be better to use 0 orENUM__MAXas that sentinel).Signed-off-by Eric Blake <[email protected]>
nice cleanup, few issues:
Quoted 325 lines
---
v8: rebase to earlier changes
v7: rebase to earlier changes, enhance commit message, also fixvisit_type_str()andvisit_type_any()
v6: rebase on top of earlier doc and formatting improvements, mention that *obj can be uninitialized on entry to an input visitor, rework semantics to keep valgrind happy on uninitialized input, break some long lines--- include/qapi/visitor-impl.h | 6 ++--- include/qapi/visitor.h | 53 ++++++++++++++++++++++++++++-------------- qapi/opts-visitor.c | 11 ++++++--- qapi/qapi-dealloc-visitor.c | 9 ++++--- qapi/qapi-visit-core.c | 39 ++++++++++++++++++++++++++----- qapi/qmp-input-visitor.c | 18 +++++++++----- qapi/qmp-output-visitor.c | 6 +++-- qapi/string-input-visitor.c | 6 +++-- qapi/string-output-visitor.c | 3 ++- scripts/qapi-visit.py | 40 +++++++++++++++++++++++-------- tests/test-qmp-commands.c | 13 +++++------ tests/test-qmp-input-strict.c | 19 +++++++-------- tests/test-qmp-input-visitor.c | 10 ++------ 13 files changed, 154 insertions(+), 79 deletions(-)include/qapi/visitor-impl.h +3
-3@@ -26,7 +26,7 @@ struct Visitor {/* Must be provided to visit structs (the string visitors do not * currently visit structs). */ -void (*start_struct)(Visitor *v,const char *name,void **obj, +bool (*start_struct)(Visitor *v,const char *name,void **obj,size_t size,Error **errp);/* May be NULL; most useful for input visitors. */ void (*check_struct)(Visitor *v,Error **errp);@@ -34,13 +34,13 @@ struct Visitorvoid (*end_struct)(Visitor *v);/* May be NULL; most useful for input visitors. */ -void (*start_implicit_struct)(Visitor *v,void **obj,size_t size, +bool (*start_implicit_struct)(Visitor *v,void **obj,size_t size,Error **errp);/* May be NULL */ void (*end_implicit_struct)(Visitor *v);/* Must be set */ -void (*start_list)(Visitor *v,const char *name,GenericList **list, +bool (*start_list)(Visitor *v,const char *name,GenericList **list,Error **errp);/* Must be set */ GenericList *(*next_list)(Visitor *v, GenericList *element, Error **errp);include/qapi/visitor.h +36
-17@@ -31,6 +31,27 @@* visitor-impl.h. */ +/* All qapi types have a corresponding function with a signature +* roughly compatible with this: +* +* void visit_type_FOO(Visitor *v, void *obj, const char *name, Error **errp); +* +* where *@obj is itself a pointer or a scalar. The visit functions for +* built-in types are declared here, while the functions for qapi-defined +* struct, union, enum, and list types are generated (see qapi-visit.h). +* Input visitors may receive an uninitialized *@obj, and guarantee a +* safe value is assigned (a new object on success, or NULL on failure). +* Output visitors expect *@obj to be properly initialized on entry. +* +* Additionally, all qapi structs have a generated function compatible +* with this: +* +* void qapi_free_FOO(void *obj); +* +* which behaves like free(), even if @obj is NULL or was only partially +* allocated before encountering an error. +*/ +/* This struct is layout-compatible with all other *List structs * created by the qapi generator. */ typedef struct GenericList@@ -62,11 +83,12 @@ typedef struct GenericList* Set *@errp on failure; for example, if the input stream does not * have a member @name or if the member is not an object. * -* FIXME: For input visitors, *@obj can be assigned here even if later -* visits will fail; this can lead to memory leaks if clients aren't -* careful. +* Returns true if *@obj was allocated; if that happens, and an error +* occurs any time before the matching visit_end_struct(), then the +* caller (usually a visit_type_FOO() function) knows to undo the +* allocation before returning control further. */ -void visit_start_struct (Visitor *v,const char *name,void **obj, +bool visit_start_struct (Visitor *v,const char *name,void **obj,size_t size,Error **errp);/** * Prepare for completing a struct visit. @@ -85,19 +107,15 @@ void visit_end_struct(Visitor *v);/** * Prepare to visit an implicit struct. -* Similar to visit_start_struct(), except that an implicit struct -* represents a subset of keys that are present at the same nesting level -* of a common object but as a separate qapi C struct, rather than a new -* object at a deeper nesting level. +* Similar to visit_start_struct(), including return semantics, except +* that an implicit struct represents a subset of keys that are present +* at the same nesting level of a common object but as a separate qapi +* C struct, rather than a new object at a deeper nesting level. * * @obj must not be NULL, since this function is only called when * visiting with qapi structs. -* -* FIXME: For input visitors, *@obj can be assigned here even if later -* visits will fail; this can lead to memory leaks if clients aren't -* careful. */ -void visit_start_implicit_struct (Visitor *v,void **obj,size_t size, +bool visit_start_implicit_struct (Visitor *v,void **obj,size_t size,Error **errp);/** * Complete an implicit struct visit started earlier. @@ -126,11 +144,12 @@ void visit_end_implicit_struct(Visitor *v);* in this case, visit_next_list() will not be needed, but * visit_end_list() is still mandatory. * -* FIXME: For input visitors, *@list can be assigned here even if -* later visits will fail; this can lead to memory leaks if clients -* aren't careful. +* Returns true if *@list was allocated; if that happens, and an error +* occurs any time before the matching visit_end_list(), then the +* caller (usually a visit_type_FOO() function) knows to undo the +* allocation before returning control further. */ -void visit_start_list (Visitor *v,const char *name,GenericList **list, +bool visit_start_list (Visitor *v,const char *name,GenericList **list,Error **errp);/** * Iterate over a GenericList during a list visit. qapi/opts-visitor.c +8
-3@@ -122,18 +122,20 @@ opts_visitor_insert(GHashTable *unprocessed_opts, const QemuOpt *opt) } -static void +static bool opts_start_struct (Visitor *v,const char *name,void **obj,size_t size,Error **errp) {OptsVisitor *ov =to_ov (v);const QemuOpt *opt; +bool result = false;if (obj) { *obj =g_malloc0 (size >0 ? size :1 ); + result = true; }if (ov->depth++ >0 ) { -return ; +return result; } ov->unprocessed_opts = g_hash_table_new_full(&g_str_hash, &g_str_equal,@@ -152,6 +154,7 @@ opts_start_struct(Visitor *v, const char *name, void **obj, ov->fake_id_opt->str =g_strdup (ov->opts_root->id);opts_visitor_insert (ov->unprocessed_opts, ov->fake_id_opt); } +return result; }@@ -210,7 +213,7 @@ lookup_distinct(const OptsVisitor *ov, const char *name, Error **errp) } -static void +static bool opts_start_list (Visitor *v,const char *name,GenericList **list,Error **errp) {OptsVisitor *ov =to_ov (v);@@ -223,8 +226,10 @@ opts_start_list(Visitor *v, const char *name, GenericList **list, Error **errp)if (ov->repeated_opts) { ov->list_mode =LM_IN_PROGRESS ; *list =g_new0 (GenericList,1 ); +return true; }else { *list =NULL ; +return false; } }qapi/qapi-dealloc-visitor.c +6
-3@@ -53,11 +53,12 @@ static void *qapi_dealloc_pop(QapiDeallocVisitor *qov)return value; } -static void qapi_dealloc_start_struct (Visitor *v,const char *name,void **obj, +static bool qapi_dealloc_start_struct (Visitor *v,const char *name,void **obj,size_t unused,Error **errp) {QapiDeallocVisitor *qov =to_qov (v);qapi_dealloc_push (qov, obj); +return false; }static void qapi_dealloc_end_struct (Visitor *v)@@ -69,13 +70,14 @@ static void qapi_dealloc_end_struct(Visitor *v) } } -static void qapi_dealloc_start_implicit_struct (Visitor *v, +static bool qapi_dealloc_start_implicit_struct (Visitor *v,void **obj,size_t size,Error **errp) {QapiDeallocVisitor *qov =to_qov (v);qapi_dealloc_push (qov, obj); +return false; }static void qapi_dealloc_end_implicit_struct (Visitor *v)@@ -87,11 +89,12 @@ static void qapi_dealloc_end_implicit_struct(Visitor *v) } } -static void qapi_dealloc_start_list (Visitor *v,const char *name, +static bool qapi_dealloc_start_list (Visitor *v,const char *name,GenericList **list,Error **errp) {QapiDeallocVisitor *qov =to_qov (v);qapi_dealloc_push (qov,NULL ); +return false; } static GenericList *qapi_dealloc_next_list(Visitor *v, GenericList *list,qapi/qapi-visit-core.c +23
-6@@ -26,14 +26,20 @@ static bool visit_is_output(Visitor *v)return v->type_enum == output_type_enum; } -void visit_start_struct (Visitor *v,const char *name,void **obj, +bool visit_start_struct (Visitor *v,const char *name,void **obj,size_t size,Error **errp) { +bool result; +assert (obj ? size : !size);if (obj &&visit_is_output (v)) {assert (*obj); } - v->start_struct (v, name, obj, size, errp); + result = v->start_struct (v, name, obj, size, errp); +if (result) { +assert (obj && *obj); + } +return result; }void visit_check_struct (Visitor *v,Error **errp)@@ -48,16 +54,22 @@ void visit_end_struct(Visitor *v) v->end_struct (v); } -void visit_start_implicit_struct (Visitor *v,void **obj,size_t size, +bool visit_start_implicit_struct (Visitor *v,void **obj,size_t size,Error **errp) { +bool result = false; +assert (obj && size);if (visit_is_output (v)) {assert (*obj); }if (v->start_implicit_struct) { - v->start_implicit_struct (v, obj, size, errp); + result = v->start_implicit_struct (v, obj, size, errp); } +if (result) { +assert (*obj); + } +return result; }void visit_end_implicit_struct (Visitor *v)@@ -67,10 +79,14 @@ void visit_end_implicit_struct(Visitor *v) } } -void visit_start_list (Visitor *v,const char *name,GenericList **list, +bool visit_start_list (Visitor *v,const char *name,GenericList **list,Error **errp) { - v->start_list (v, name, list, errp); +bool result = v->start_list (v, name, list, errp); +if (result) { +assert (list && *list); + } +return result; }GenericList *visit_next_list (Visitor *v,GenericList *list,Error **errp)@@ -229,6 +245,7 @@ void visit_type_bool(Visitor *v, const char *name, bool *obj, Error **errp)void visit_type_str (Visitor *v,const char *name,char **obj,Error **errp) { +Error *err =NULL ;assert (obj);/* TODO: Fix callers to not pass NULL when they mean "", so that we * can enable:
qapi/qapi-visit-core.c void visit_type_str(Visitor *v, const char *name, char **obj, Error **errp) · line 259
Quoted 6 lines
qapi/qapi-visit-core.c +3
-0@@ -237,6 +254,10 @@ void visit_type_str(Visitor *v, const char *name, char **obj, Error **errp)} */ v->type_str (v, name, obj, errp); +if (!visit_is_output (v) && err) { + *obj =NULL ; + }
This will always evelatute to false, you need to change the line above I suppose
Quoted 8 lines
qapi/qapi-visit-core.c +1
-0+error_propagate (errp, err); } void visit_type_number(Visitor *v, const char *name, double *obj,@@ -248,11 +269,17 @@ void visit_type_number(Visitor *v, const char *name, double *obj,void visit_type_any (Visitor *v,const char *name,QObject **obj,Error **errp) {
qapi/qapi-visit-core.c void visit_type_number(Visitor *v, const char *name, double *obj, · line 281
Quoted 10 lines
qapi/qapi-visit-core.c +5
-0+Error *err =NULL ; +assert (obj);if (visit_is_output (v)) {assert (*obj); } v->type_any (v, name, obj, errp); +if (!visit_is_output (v) && err) { + *obj =NULL ; + }
same here
Quoted 418 lines
qapi/qapi-visit-core.c +1
-0+error_propagate (errp, err); }void visit_type_null (Visitor *v,const char *name,Error **errp)qapi/qmp-input-visitor.c +12
-6@@ -130,7 +130,7 @@ static void qmp_input_pop(Visitor *v) qiv->nb_stack--; } -static void qmp_input_start_struct (Visitor *v,const char *name,void **obj, +static bool qmp_input_start_struct (Visitor *v,const char *name,void **obj,size_t size,Error **errp) {QmpInputVisitor *qiv =to_qiv (v);@@ -140,30 +140,34 @@ static void qmp_input_start_struct(Visitor *v, const char *name, void **obj,if (!qobj ||qobject_type (qobj) !=QTYPE_QDICT ) {error_setg (errp,QERR_INVALID_PARAMETER_TYPE , name ? name :"null" ,"QDict" ); -return ; +return false; }qmp_input_push (qiv, qobj,NULL , &err);if (err) {error_propagate (errp, err); -return ; +return false; }if (obj) { *obj =g_malloc0 (size); +return true; } +return false; } -static void qmp_input_start_implicit_struct (Visitor *v,void **obj, +static bool qmp_input_start_implicit_struct (Visitor *v,void **obj,size_t size,Error **errp) {if (obj) { *obj =g_malloc0 (size); +return true; } +return false; } -static void qmp_input_start_list (Visitor *v,const char *name, +static bool qmp_input_start_list (Visitor *v,const char *name,GenericList **list,Error **errp) {QmpInputVisitor *qiv =to_qiv (v);@@ -173,7 +177,7 @@ static void qmp_input_start_list(Visitor *v, const char *name,if (!qobj ||qobject_type (qobj) !=QTYPE_QLIST ) {error_setg (errp,QERR_INVALID_PARAMETER_TYPE , name ? name :"null" ,"list" ); -return ; +return false; } entry =qlist_first (qobject_to_qlist (qobj));@@ -181,10 +185,12 @@ static void qmp_input_start_list(Visitor *v, const char *name,if (list) {if (entry) { *list =g_new0 (GenericList,1 ); +return true; }else { *list =NULL ; } } +return false; } static GenericList *qmp_input_next_list(Visitor *v, GenericList *list,qapi/qmp-output-visitor.c +4
-2@@ -103,7 +103,7 @@ static void qmp_output_add_obj(QmpOutputVisitor *qov, const char *name, } } -static void qmp_output_start_struct (Visitor *v,const char *name,void **obj, +static bool qmp_output_start_struct (Visitor *v,const char *name,void **obj,size_t unused,Error **errp) {QmpOutputVisitor *qov =to_qov (v);@@ -111,6 +111,7 @@ static void qmp_output_start_struct(Visitor *v, const char *name, void **obj,qmp_output_add (qov, name, dict);qmp_output_push (qov, dict); +return false; }static void qmp_output_end_struct (Visitor *v)@@ -119,7 +120,7 @@ static void qmp_output_end_struct(Visitor *v)qmp_output_pop (qov); } -static void qmp_output_start_list (Visitor *v,const char *name, +static bool qmp_output_start_list (Visitor *v,const char *name,GenericList **listp,Error **errp) {QmpOutputVisitor *qov =to_qov (v);@@ -127,6 +128,7 @@ static void qmp_output_start_list(Visitor *v, const char *name,qmp_output_add (qov, name, list);qmp_output_push (qov, list); +return false; } static GenericList *qmp_output_next_list(Visitor *v, GenericList *list,qapi/string-input-visitor.c +4
-2@@ -120,7 +120,7 @@ error: siv->ranges =NULL ; } -static void +static bool start_list (Visitor *v,const char *name,GenericList **list,Error **errp) {StringInputVisitor *siv =to_siv (v);@@ -132,7 +132,7 @@ start_list(Visitor *v, const char *name, GenericList **list, Error **errp)parse_str (siv, &err);if (err) {error_propagate (errp, err); -return ; +return false; } siv->cur_range =g_list_first (siv->ranges);@@ -142,8 +142,10 @@ start_list(Visitor *v, const char *name, GenericList **list, Error **errp) siv->cur = r->begin; } *list =g_new0 (GenericList,1 ); +return true; }else { *list =NULL ; +return false; } }qapi/string-output-visitor.c +2
-1@@ -263,7 +263,7 @@ static void print_type_number(Visitor *v, const char *name, double *obj,string_output_set (sov,g_strdup_printf ("%f" , *obj)); } -static void +static bool start_list (Visitor *v,const char *name,GenericList **list,Error **errp) {StringOutputVisitor *sov =to_sov (v);@@ -276,6 +276,7 @@ start_list(Visitor *v, const char *name, GenericList **list, Error **errp)if (*list && (*list)->next) { sov->list_mode =LM_STARTED ; } +return false; } static GenericList *scripts/qapi-visit.py +30
-10@@ -58,8 +58,10 @@ def gen_visit_implicit_struct(typ): static void visit_type_implicit_%(c_type)s(Visitor *v, %(c_type)s **obj, Error **errp) { Error *err = NULL; + bool allocated; - visit_start_implicit_struct(v, (void **)obj, sizeof(%(c_type)s), &err); + allocated = visit_start_implicit_struct(v, (void **)obj, + sizeof(%(c_type)s), &err); if (err) { goto out; }@@ -69,6 +71,10 @@ static void visit_type_implicit_%(c_type)s(Visitor *v, %(c_type)s **obj, Error * visit_type_%(c_type)s_fields(v, obj, &err); out_obj: visit_end_implicit_struct(v); + if (allocated && err) { + g_free(*obj); + *obj = NULL; + } out: error_propagate(errp, err); }@@ -116,18 +122,15 @@ out: def gen_visit_list(name, element_type): - # FIXME: if *obj is NULL on entry, and the first visit_next_list() - # assigns to *obj, while a later one fails, we should clean up *obj - # rather than leaving it non-NULL. As currently written, the caller must - # call qapi_free_FOOList() to avoid a memory leak of the partial FOOList. return mcgen(''' void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error **errp) { Error *err = NULL; %(c_name)s *elt; + bool allocated; - visit_start_list(v, name, (GenericList **)obj, &err); + allocated = visit_start_list(v, name, (GenericList **)obj, &err); if (err) { goto out; }@@ -144,6 +147,10 @@ void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error } visit_end_list(v); out: + if (allocated && err) { + qapi_free_%(c_name)s(*obj); + *obj = NULL; + } error_propagate(errp, err); } ''',@@ -174,8 +181,10 @@ def gen_visit_alternate(name, variants): void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error **errp) { Error *err = NULL; + bool allocated; - visit_start_implicit_struct(v, (void**) obj, sizeof(%(c_name)s), &err); + allocated = visit_start_implicit_struct(v, (void **)obj, + sizeof(%(c_name)s), &err); if (err) { goto out; }@@ -204,11 +213,15 @@ void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error } out_obj: visit_end_implicit_struct(v); + if (allocated && err) { + qapi_free_%(c_name)s(*obj); + *obj = NULL; + } out: error_propagate(errp, err); } ''', - name=name) + name=name, c_name=c_name(name)) return ret@@ -236,8 +249,10 @@ def gen_visit_object(name, base, members, variants): void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error **errp) { Error *err = NULL; + bool allocated; - visit_start_struct(v, name, (void **)obj, sizeof(%(c_name)s), &err); + allocated = visit_start_struct(v, name, (void **)obj, sizeof(%(c_name)s), + &err); if (err) { goto out; }@@ -301,10 +316,15 @@ void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error visit_check_struct(v, &err); out_obj: visit_end_struct(v); + if (allocated && err) { + qapi_free_%(c_name)s(*obj); + *obj = NULL; + } out: error_propagate(errp, err); } -''') +''', + c_name=c_name(name)) return rettests/test-qmp-commands.c +6
-7@@ -227,14 +227,13 @@ static void test_dealloc_partial(void)QDECREF (ud2_dict); } -/* verify partial success */ -assert (ud2 !=NULL ); -assert (ud2->string0 !=NULL ); -assert (strcmp (ud2->string0, text) ==0 ); -assert (ud2->dict1 ==NULL ); - -/* confirm & release construction error */ +/* verify that visit_type_XXX() cleans up properly on error */ error_free_or_abort (&err); +assert (!ud2); + +/* Manually create a partial object, leaving ud2->dict1 at NULL */ + ud2 =g_new0 (UserDefTwo,1 ); + ud2->string0 =g_strdup (text);/* tear down partial object */ qapi_free_UserDefTwo (ud2);tests/test-qmp-input-strict.c +8
-11@@ -181,10 +181,7 @@ static void test_validate_fail_struct(TestInputVisitorData *data,visit_type_TestStruct (v,NULL , &p, &err);error_free_or_abort (&err); -if (p) { -g_free (p->string); - } -g_free (p); +g_assert (!p); } static void test_validate_fail_struct_nested(TestInputVisitorData *data,@@ -198,7 +195,7 @@ static void test_validate_fail_struct_nested(TestInputVisitorData *data,visit_type_UserDefTwo (v,NULL , &udp, &err);error_free_or_abort (&err); -qapi_free_UserDefTwo (udp); +g_assert (!udp); } static void test_validate_fail_list(TestInputVisitorData *data,@@ -212,7 +209,7 @@ static void test_validate_fail_list(TestInputVisitorData *data,visit_type_UserDefOneList (v,NULL , &head, &err);error_free_or_abort (&err); -qapi_free_UserDefOneList (head); +g_assert (!head); } static void test_validate_fail_union_native_list(TestInputVisitorData *data,@@ -227,7 +224,7 @@ static void test_validate_fail_union_native_list(TestInputVisitorData *data,visit_type_UserDefNativeListUnion (v,NULL , &tmp, &err);error_free_or_abort (&err); -qapi_free_UserDefNativeListUnion (tmp); +g_assert (!tmp); } static void test_validate_fail_union_flat(TestInputVisitorData *data,@@ -241,7 +238,7 @@ static void test_validate_fail_union_flat(TestInputVisitorData *data,visit_type_UserDefFlatUnion (v,NULL , &tmp, &err);error_free_or_abort (&err); -qapi_free_UserDefFlatUnion (tmp); +g_assert (!tmp); } static void test_validate_fail_union_flat_no_discrim(TestInputVisitorData *data,@@ -256,13 +253,13 @@ static void test_validate_fail_union_flat_no_discrim(TestInputVisitorData *data,visit_type_UserDefFlatUnion2 (v,NULL , &tmp, &err);error_free_or_abort (&err); -qapi_free_UserDefFlatUnion2 (tmp); +g_assert (!tmp); }static void test_validate_fail_alternate (TestInputVisitorData *data,const void *unused) { -UserDefAlternate *tmp =NULL ; +UserDefAlternate *tmp;Visitor *v;Error *err =NULL ;@@ -270,7 +267,7 @@ static void test_validate_fail_alternate(TestInputVisitorData *data,visit_type_UserDefAlternate (v,NULL , &tmp, &err);error_free_or_abort (&err); -qapi_free_UserDefAlternate (tmp); +g_assert (!tmp); } static void do_test_validate_qmp_introspect(TestInputVisitorData *data,tests/test-qmp-input-visitor.c +2
-9@@ -708,18 +708,12 @@ static void test_visitor_in_errors(TestInputVisitorData *data,visit_type_TestStruct (v,NULL , &p, &err);error_free_or_abort (&err); -/* FIXME - a failed parse should not leave a partially-allocated p -* for us to clean up; this could cause callers to leak memory. */ -g_assert (p->string ==NULL ); - -g_free (p->string); -g_free (p); +g_assert (!p); v =visitor_input_test_init (data,"[ '1', '2', false, '3' ]" );visit_type_strList (v,NULL , &q, &err);error_free_or_abort (&err); -assert (q); -qapi_free_strList (q); +assert (!q); } static void test_visitor_in_wrong_type(TestInputVisitorData *data, --2.4.3
Signature · 1 line
Marc-André Lureau
Re: [Qemu-devel] [PATCH v8 34/35] qapi: Change visit_type_FOO() to no longer return partial objects
Quoted 27 lines · Marc-André Lureau
Hi
Quoted 12 lines · Eric Blake
Returning a partial object on error is an invitation for a careless caller to leak memory. As no one outside the testsuite was actually relying on these semantics, it is cleaner to just document and guarantee that ALL pointer-based
visit_type_FOO()functions always leave a safe value in *obj during an input visitor (either the new object on success, or NULL if an error is encountered).Since input visitors have blind assignment semantics, we have to track the result of whether an assignment is made all the way down to each visitor callback implementation, to avoid making decisions based on potentially uninitialized storage.
Quoted 8 lines
Note that we still leave *obj unchanged after a scalar-based
visit_type_FOO(); I did not feel like auditing all uses ofvisit_type_Enum()to see if the callers would tolerate a specific sentinel value (not to mention having to decide whether it would be better to use 0 orENUM__MAXas that sentinel).Signed-off-by Eric Blake <[email protected]>nice cleanup, few issues:
Quoted 11 lines
qapi/qapi-visit-core.c void visit_type_str(Visitor *v, const char *name, char **obj, Error **errp) · line 259
Quoted 7 lines
qapi/qapi-visit-core.c +3
-0@@ -237,6 +254,10 @@ void visit_type_str(Visitor *v, const char *name, char **obj, Error **errp)} */ v->type_str (v, name, obj, errp); +if (!visit_is_output (v) && err) { + *obj =NULL ; + }This will always evelatute to false, you need to change the line above I suppose
Quoted 1 line
(quoted diff) +1
-0+ error_propagate(errp, err);
Oh right, that needs to be v->type_str(..., &err).
I'll have to double-check that no assertions trigger with the fixed code, and provide the fixup patch. I don't know if Markus will want me to spin a v9, but I'll wait for his comments before deciding if a full respin is needed.
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
PATCH v8 34/35 fixup! qapi: Change visit_type_FOO() to no longer return partial objects
In reply to Eric Blake[no change in commit message]
---
v9: fix bug in use of errp
v8: rebase to earlier changes
v7: rebase to earlier changes, enhance commit message, also fix visit_type_str() and visit_type_any()
v6: rebase on top of earlier doc and formatting improvements, mention that *obj can be uninitialized on entry to an input visitor, rework semantics to keep valgrind happy on uninitialized input, break some long lines
--- qapi/qapi-visit-core.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
qapi/qapi-visit-core.c +3 -3
@@ -2,7 +2,7 @@* Core Definitions for QAPI Visitor Classes * * Copyright IBM, Corp. 2011 -* Copyright (C) 2015 Red Hat, Inc. +* Copyright (C) 2015-2016 Red Hat, Inc. * * Authors: * Anthony Liguori <[email protected]>
@@ -253,7 +253,7 @@ void visit_type_str(Visitor *v, const char *name, char **obj, Error **errp)assert(*obj); } */ - v->type_str (v, name, obj, errp); + v->type_str (v, name, obj, &err);if (!visit_is_output (v) && err) { *obj =NULL ; }
@@ -275,7 +275,7 @@ void visit_type_any(Visitor *v, const char *name, QObject **obj, Error **errp)if (visit_is_output (v)) {assert (*obj); } - v->type_any (v, name, obj, errp); + v->type_any (v, name, obj, &err);if (!visit_is_output (v) && err) { *obj =NULL ; }
Signature · 1 line
2.4.3
PATCH v8 35/35 RFC: qapi: Adjust layout of FooList types
In reply to Eric BlakeBy 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.
However, this requires visit_start_list() and visit_next_list() to gain a size parameter, to know what size element to allocate.
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'.
---
v8: rebase to earlier changes
v7: new patch; probably too invasive to be worth it, especially if we can't prove that our current size for uint8List is a bottleneck
--- hw/ppc/spapr_drc.c | 2 +- include/qapi/visitor-impl.h | 5 +++-- include/qapi/visitor.h | 39 +++++++++++++++++++-------------------- qapi/opts-visitor.c | 9 +++++---- qapi/qapi-dealloc-visitor.c | 5 +++-- qapi/qapi-visit-core.c | 14 +++++++++----- qapi/qmp-input-visitor.c | 8 ++++---- qapi/qmp-output-visitor.c | 5 +++-- qapi/string-input-visitor.c | 9 +++++---- qapi/string-output-visitor.c | 5 +++-- scripts/qapi-types.py | 5 +---- scripts/qapi-visit.py | 4 ++-- 12 files changed, 58 insertions(+), 52 deletions(-)
hw/ppc/spapr_drc.c +1 -1
@@ -299,7 +299,7 @@ static void prop_get_fdt(Object *obj, Visitor *v, const char *name,int i; prop =fdt_get_property_by_offset (fdt, fdt_offset, &prop_len); name =fdt_string (fdt,fdt32_to_cpu (prop->nameoff)); -visit_start_list (v, name,NULL , &err); +visit_start_list (v, name,NULL ,0 , &err);if (err) {error_propagate (errp, err);return ;
include/qapi/visitor-impl.h +3 -2
@@ -41,9 +41,10 @@ struct Visitor/* Must be set */ bool (*start_list)(Visitor *v,const char *name,GenericList **list, -Error **errp); +size_t size,Error **errp);/* Must be set */ - GenericList *(*next_list)(Visitor *v, GenericList *element, Error **errp); +GenericList *(*next_list)(Visitor *v,GenericList *element,size_t size, +Error **errp);/* Must be set */ void (*end_list)(Visitor *v);
include/qapi/visitor.h +19 -20
@@ -56,11 +56,8 @@* created by the qapi generator. */ typedef struct GenericList { -union { -void *value; -uint64_t padding; - };struct GenericList *next; +char padding[]; }GenericList ;/**
@@ -130,19 +127,19 @@ void visit_end_implicit_struct(Visitor *v);/** * Prepare to visit a list tied to an object key @name. * @name will be NULL if this is visited as part of another list. -* Input visitors malloc a qapi List struct into *@list, or set it to -* NULL if there are no elements in the list; and output visitors -* expect *@list to point to the start of the list, if any. On -* return, if *@list is non-NULL, the caller should enter a loop +* Input visitors malloc a qapi List struct into *@list of size @size, +* or set it to NULL if there are no elements in the list; and output +* visitors expect *@list to point to the start of the list, if any. +* On return, if *@list is non-NULL, the caller should enter a loop * visiting the current element, then using visit_next_list() to * advance to the next element, until that returns NULL; then * visit_end_list() must be used to complete the visit. * -* If supported by a visitor, @list can be NULL to indicate that there -* is no qapi List struct, and that the upcoming visit calls are -* parsing input to or creating output from some other representation; -* in this case, visit_next_list() will not be needed, but -* visit_end_list() is still mandatory. +* If supported by a visitor, @list can be NULL (and @size 0) to +* indicate that there is no qapi List struct, and that the upcoming +* visit calls are parsing input to or creating output from some other +* representation; in this case, visit_next_list() will not be needed, +* but visit_end_list() is still mandatory. * * Returns true if *@list was allocated; if that happens, and an error * occurs any time before the matching visit_end_list(), then the
@@ -150,17 +147,19 @@ void visit_end_implicit_struct(Visitor *v);* allocation before returning control further. */ bool visit_start_list (Visitor *v,const char *name,GenericList **list, -Error **errp); +size_t size,Error **errp);/** * Iterate over a GenericList during a list visit. * Before calling this function, the caller should use the appropriate -* visit_type_FOO() for the current list element at @element->value, and -* check for errors. @element must not be NULL; on the first iteration, -* it should be the value in *list after visit_start_list(); on other -* calls it should be the previous return value. This function -* returns NULL once there are no further list elements. +* visit_type_FOO() for the current list element at @element->value, +* and check for errors. @element must not be NULL; on the first +* iteration, it should be the value in *list after +* visit_start_list(); on other calls it should be the previous return +* value. @size should be the same as for visit_start_list(). This +* function returns NULL once there are no further list elements. */ -GenericList *visit_next_list (Visitor *v,GenericList *element,Error **errp); +GenericList *visit_next_list (Visitor *v,GenericList *element,size_t size, +Error **errp);/** * Complete the list started earlier. * Must be called after any successful use of visit_start_list(),
qapi/opts-visitor.c +5 -4
@@ -214,7 +214,8 @@ lookup_distinct(const OptsVisitor *ov, const char *name, Error **errp)static bool -opts_start_list (Visitor *v,const char *name,GenericList **list,Error **errp) +opts_start_list (Visitor *v,const char *name,GenericList **list,size_t size, +Error **errp) {OptsVisitor *ov =to_ov (v);
@@ -225,7 +226,7 @@ opts_start_list(Visitor *v, const char *name, GenericList **list, Error **errp) ov->repeated_opts =lookup_distinct (ov, name, errp);if (ov->repeated_opts) { ov->list_mode =LM_IN_PROGRESS ; - *list =g_new0 (GenericList,1 ); + *list =g_malloc0 (size);return true; }else { *list =NULL ;
@@ -235,7 +236,7 @@ opts_start_list(Visitor *v, const char *name, GenericList **list, Error **errp)static GenericList * -opts_next_list (Visitor *v,GenericList *list,Error **errp) +opts_next_list (Visitor *v,GenericList *list,size_t size,Error **errp) {OptsVisitor *ov =to_ov (v);
@@ -269,7 +270,7 @@ opts_next_list(Visitor *v, GenericList *list, Error **errp)abort (); } - list->next =g_new0 (GenericList,1 ); + list->next =g_malloc0 (size);return list->next; }
qapi/qapi-dealloc-visitor.c +3 -2
@@ -90,7 +90,8 @@ static void qapi_dealloc_end_implicit_struct(Visitor *v) }static bool qapi_dealloc_start_list (Visitor *v,const char *name, -GenericList **list,Error **errp) +GenericList **list,size_t size, +Error **errp) {QapiDeallocVisitor *qov =to_qov (v);qapi_dealloc_push (qov,NULL );
@@ -98,7 +99,7 @@ static bool qapi_dealloc_start_list(Visitor *v, const char *name, }static GenericList *qapi_dealloc_next_list (Visitor *v,GenericList *list, -Error **errp) +size_t size,Error **errp) {GenericList *next = list->next;g_free (list);
qapi/qapi-visit-core.c +9 -5
@@ -80,19 +80,23 @@ void visit_end_implicit_struct(Visitor *v) }bool visit_start_list (Visitor *v,const char *name,GenericList **list, -Error **errp) +size_t size,Error **errp) { -bool result = v->start_list (v, name, list, errp); +bool result; + +assert (list ? size : !size); + result = v->start_list (v, name, list, size, errp);if (result) {assert (list && *list); }return result; } -GenericList *visit_next_list (Visitor *v,GenericList *list,Error **errp) +GenericList *visit_next_list (Visitor *v,GenericList *list,size_t size, +Error **errp) { -assert (list); -return v->next_list (v, list, errp); +assert (list && size); +return v->next_list (v, list, size, errp); }void visit_end_list (Visitor *v)
qapi/qmp-input-visitor.c +4 -4
@@ -168,7 +168,7 @@ static bool qmp_input_start_implicit_struct(Visitor *v, void **obj, }static bool qmp_input_start_list (Visitor *v,const char *name, -GenericList **list,Error **errp) +GenericList **list,size_t size,Error **errp) {QmpInputVisitor *qiv =to_qiv (v);QObject *qobj =qmp_input_get_object (qiv, name, true);
@@ -184,7 +184,7 @@ static bool qmp_input_start_list(Visitor *v, const char *name,qmp_input_push (qiv, qobj, entry, errp);if (list) {if (entry) { - *list =g_new0 (GenericList,1 ); + *list =g_malloc0 (size);return true; }else { *list =NULL ;
@@ -194,7 +194,7 @@ static bool qmp_input_start_list(Visitor *v, const char *name, }static GenericList *qmp_input_next_list (Visitor *v,GenericList *list, -Error **errp) +size_t size,Error **errp) {QmpInputVisitor *qiv =to_qiv (v);StackObject *so = &qiv->stack[qiv->nb_stack -1 ];
@@ -202,7 +202,7 @@ static GenericList *qmp_input_next_list(Visitor *v, GenericList *list,if (!so->entry) {return NULL ; } - list->next =g_new0 (GenericList,1 ); + list->next =g_malloc0 (size);return list->next; }
qapi/qmp-output-visitor.c +3 -2
@@ -121,7 +121,8 @@ static void qmp_output_end_struct(Visitor *v) }static bool qmp_output_start_list (Visitor *v,const char *name, -GenericList **listp,Error **errp) +GenericList **listp,size_t size, +Error **errp) {QmpOutputVisitor *qov =to_qov (v);QList *list =qlist_new ();
@@ -132,7 +133,7 @@ static bool qmp_output_start_list(Visitor *v, const char *name, }static GenericList *qmp_output_next_list (Visitor *v,GenericList *list, -Error **errp) +size_t size,Error **errp) {return list->next; }
qapi/string-input-visitor.c +5 -4
@@ -121,7 +121,8 @@ error: }static bool -start_list (Visitor *v,const char *name,GenericList **list,Error **errp) +start_list (Visitor *v,const char *name,GenericList **list,size_t size, +Error **errp) {StringInputVisitor *siv =to_siv (v);Error *err =NULL ;
@@ -141,7 +142,7 @@ start_list(Visitor *v, const char *name, GenericList **list, Error **errp)if (r) { siv->cur = r->begin; } - *list =g_new0 (GenericList,1 ); + *list =g_malloc0 (size);return true; }else { *list =NULL ;
@@ -150,7 +151,7 @@ start_list(Visitor *v, const char *name, GenericList **list, Error **errp) }static GenericList * -next_list (Visitor *v,GenericList *list,Error **errp) +next_list (Visitor *v,GenericList *list,size_t size,Error **errp) {StringInputVisitor *siv =to_siv (v);Range *r;
@@ -176,7 +177,7 @@ next_list(Visitor *v, GenericList *list, Error **errp) siv->cur = r->begin; } - list->next =g_new0 (GenericList,1 ); + list->next =g_malloc0 (size);return list->next; }
qapi/string-output-visitor.c +3 -2
@@ -264,7 +264,8 @@ static void print_type_number(Visitor *v, const char *name, double *obj, }static bool -start_list (Visitor *v,const char *name,GenericList **list,Error **errp) +start_list (Visitor *v,const char *name,GenericList **list,size_t size, +Error **errp) {StringOutputVisitor *sov =to_sov (v);
@@ -280,7 +281,7 @@ start_list(Visitor *v, const char *name, GenericList **list, Error **errp) }static GenericList * -next_list (Visitor *v,GenericList *list,Error **errp) +next_list (Visitor *v,GenericList *list,size_t size,Error **errp) {StringOutputVisitor *sov =to_sov (v);GenericList *ret = list->next;
scripts/qapi-types.py +1 -4
@@ -26,11 +26,8 @@ def gen_array(name, element_type): return mcgen(''' struct %(c_name)s { - union { - %(c_type)s value; - uint64_t padding; - }; %(c_name)s *next; + %(c_type)s value; }; ''', c_name=c_name(name), c_type=element_type.c_type())
scripts/qapi-visit.py +2 -2
@@ -130,7 +130,7 @@ void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error %(c_name)s *elt; bool allocated; - allocated = visit_start_list(v, name, (GenericList **)obj, &err); + allocated = visit_start_list(v, name, (GenericList **)obj, sizeof(%(c_name)s), &err); if (err) { goto out; }
@@ -140,7 +140,7 @@ void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error if (err) { break; } - elt = (%(c_name)s *)visit_next_list(v, (GenericList *)elt, &err); + elt = (%(c_name)s *)visit_next_list(v, (GenericList *)elt, sizeof(%(c_name)s), &err); if (err) { break; }
Signature · 1 line
2.4.3
Re: [Qemu-devel] [PATCH v8 35/35] RFC: qapi: Adjust layout of FooList types
Hi
Quoted 7 lines · Eric Blake
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.
small win, but a win
Quoted 2 lines
However, this requires
visit_start_list()andvisit_next_list()to gain a size parameter, to know what size element to allocate.
If this is the only "drawback", I'd be fine with it.
Quoted 18 lines
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 ofqapi-types.h, rather than its current spot invisitor.h; it also makes iteration a bit more verbose through 'foolist->base.next' instead of 'foolist->next'.
Quoted 2 lines
Signed-off-by Eric Blake <[email protected]>
otherwise, patch looks good to me, waiting for non-rfc version if all agree with it, for the review-by tag.
Signature · 1 line
Marc-André Lureau
PATCH 1/1 qapi: Update docs to match recent generated changes, part 2
In reply to Eric Blake[Either worth squashing into a single patch with the posted v8 14.5/35 and sinking it to the end of the series, or else splitting it into pieces and squashing per patch that makes a change to generated output]
---
v9: new patch
--- docs/qapi-code-gen.txt | 51 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 31 insertions(+), 20 deletions(-)
docs/qapi-code-gen.txt +31 -20
@@ -820,11 +820,8 @@ Example: void qapi_free_UserDefOne(UserDefOne *obj); struct UserDefOneList { - union { - UserDefOne *value; - uint64_t padding; - }; UserDefOneList *next; + UserDefOne *value; }; void qapi_free_UserDefOneList(UserDefOneList *obj);
@@ -878,8 +875,10 @@ Example: void visit_type_UserDefOne(Visitor *v, const char *name, UserDefOne **obj, Error **errp) { Error *err = NULL; + bool allocated; - visit_start_struct(v, name, (void **)obj, "UserDefOne", sizeof(UserDefOne), &err); + allocated = visit_start_struct(v, name, (void **)obj, sizeof(UserDefOne), + &err); if (err) { goto out; }
@@ -887,10 +886,16 @@ Example: goto out_obj; } visit_type_UserDefOne_fields(v, obj, &err); + if (err) { + goto out_obj; + } + visit_check_struct(v, &err); out_obj: - error_propagate(errp, err); - err = NULL; - visit_end_struct(v, &err); + visit_end_struct(v); + if (allocated && err) { + qapi_free_UserDefOne(*obj); + *obj = NULL; + } out: error_propagate(errp, err); }
@@ -898,24 +903,30 @@ Example: void visit_type_UserDefOneList(Visitor *v, const char *name, UserDefOneList **obj, Error **errp) { Error *err = NULL; - GenericList *i, **prev; + UserDefOneList *eld; + bool allocated; - visit_start_list(v, name, &err); + allocated = visit_start_list(v, name, (GenericList **)obj, sizeof(UserDefOneList), &err); if (err) { goto out; } - - for (prev = (GenericList **)obj; - !err && (i = visit_next_list(v, prev, &err)) != NULL; - prev = &i) { - UserDefOneList *native_i = (UserDefOneList *)i; - visit_type_UserDefOne(v, NULL, &native_i->value, &err); + elt = *obj; + while (elt) { + visit_type_UserDefOne(v, NULL, &elt->value, &err); + if (err) { + break; + } + elt = (UserDefOneList *)visit_next_list(v, (GenericList *)elt, sizeof(UserDefOneList), &err); + if (err) { + break; + } } - - error_propagate(errp, err); - err = NULL; - visit_end_list(v, &err); + visit_end_list(v); out: + if (allocated && err) { + qapi_free_UserDefOneList(*obj); + *obj = NULL; + } error_propagate(errp, err); } $ cat qapi-generated/example-qapi-visit.h
Signature · 1 line
2.4.3
Quoted 28 lines · Eric Blake
Based on qemu.git master. Pending prerequisites:
+ Not a strong dependency, but for qapi-tests to consistently pass, I needed a race fixed:
https://lists.gnu.org/archive/html/qemu-devel/2015-12/msg01827.htmlAlso available as a tag at this location:
git fetchgit://repo.or.cz/qemu/ericb.gitqapi-cleanupv8eand will soon be part of my branch with the rest of the v5 series, at:
http://repo.or.cz/qemu/ericb.git/shortlog/refs/heads/qapiv8 notes:
Four new patches (13-16/35), plus rebasing on top of them, so that the code base now consistently passes a 'v, name' pair anywhere a visitor needs a name, rather than putting other arguments in between the pair. I got to have fun with Coccinelle :) Also fix a bug in my changes tovisit_next_list()(v7 29/31), so that 'make check' and qemu-iotests now pass at all points in the series.The parameter ordering changes have the potential to be a rebase magnet, so I'm hoping this series can go in relatively soon after Markus returns from break.
I made good on my threat in v7 of writing a qapi-to-JSON output visitor, but that will remain a separate series based on this one (the only posting of that series so far now needs rebasing:
https://lists.gnu.org/archive/html/qemu-devel/2015-12/msg01760.html)
Quoted 13 lines
001/35:[----] [--] 'qobject: Document more shortcomings in our number handling' 002/35:[----] [--] 'qapi: Avoid use of misnamed
DO_UPCAST()' 003/35:[----] [--] 'qapi: Drop dead dealloc visitor variable' 004/35:[----] [--] 'hmp: Improve use of qapi visitor' 005/35:[----] [--] 'vl: Improve use of qapi visitor'
006/35:[----] [--] 'balloon: Improve use of qapi visitor' 007/35:[----] [--] 'qapi: Improve generated event use of qapi visitor' 008/35:[----] [--] 'qapi: Track all failures between visit_start/stop' 009/35:[----] [--] 'qapi: Prefertype_int64overtype_intin visitors' 010/35:[----] [--] 'qapi: Make all visitors supply uint64 callbacks' 011/35:[----] [--] 'qapi: Consolidate visitor small integer callbacks' 012/35:[----] [--] 'qapi: Don't cast Enum* to int*'
013/35:[down] 'qom: Use typedef for Visitor'
Applies cleanly until here.
Quoted 1 line
014/35:[down] 'qapi: Swap visit_* arguments for consistent 'name' placement'
Doesn't apply.
You can either spin v9 addressing Marc-André's review, or you can rebase v8 without changes somewhere I can pull, so I can review it properly.
[...]