libxl: fixes for JSON infrastructure
cover libxl: fixes for JSON infrastructure
This small series contains three fixes to libxl JSON infrasturcture.
Wei.
Wei Liu (3):
libxl/gentypes.py: don't generate JSON for private type(s) libxl_types.idl: use empty Struct for invalid domain type
libxl_json: fix JSON parser debug code
tools/libxl/gentypes.py | 2 +- tools/libxl/libxl_json.c | 17 +++++++++++++---- tools/libxl/libxl_types.idl | 2 +- 3 files changed, 15 insertions(+), 6 deletions(-)
Signature · 1 line
1.7.10.4
PATCH 1/3 libxl/gentypes.py: don't generate JSON for private type(s)
Private types are only useful inside libxl. They don't have a valid JSON generation function by default.
Currently there's only one private type, that's libxl_ev_link. Not skipping this field causes testidl to fail as the code generated for this type is NULL.
--- tools/libxl/gentypes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/libxl/gentypes.py +1 -1
@@ -226,7 +226,7 @@ def libxl_C_type_gen_json(ty, v, indent = " ", parent = None): s += "s = yajl_gen_map_open(hand);\n" s += "if (s != yajl_gen_status_ok)\n" s += " goto out;\n" - for f in [f for f in ty.fields if not f.const]: + for f in [f for f in ty.fields if not f.const and not f.type.private]: (nparent,fexpr) = ty.member(v, f, parent is None) s += "s = yajl_gen_string(hand, (const unsigned char *)\"%s\", sizeof(\"%s\")-1);\n" % (f.name, f.name) s += "if (s != yajl_gen_status_ok)\n"
Signature · 1 line
1.7.10.4
PATCH 2/3 libxl_types.idl: use empty Struct for invalid domain type
In reply to Wei LiuUsing None makes gentypes.py generates a C function which generates nothing. Eventually the generated JSON string is malformed.
--- tools/libxl/libxl_types.idl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/libxl/libxl_types.idl +1 -1
@@ -384,7 +384,7 @@ libxl_domain_build_info = Struct("domain_build_info",[ # Use host's E820 for PCI passthrough. ("e820_host", libxl_defbool), ])), - ("invalid", None), + ("invalid", Struct(None, [])), ], keyvar_init_val = "LIBXL_DOMAIN_TYPE_INVALID")), ], dir=DIR_IN )
Signature · 1 line
1.7.10.4
Re: [PATCH 2/3] libxl_types.idl: use empty Struct for invalid domain type
Quoted 2 lines · Wei Liu
Using None makes
gentypes.pygenerates a C function which generates nothing. Eventually the generated JSON string is malformed.
I'd prefer to fix the generator to not do this rather than muddying the input description. How hard would it be?
Ian.
Quoted 19 lines
Signed-off-by Wei Liu <[email protected]>--- tools/libxl/libxl_types.idl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tools/libxl/libxl_types.idl +1
-1@@ -384,7 +384,7 @@ libxl_domain_build_info = Struct("domain_build_info",[ # Use host's E820 for PCI passthrough. ("e820_host", libxl_defbool), ])), - ("invalid", None), + ("invalid", Struct(None, [])), ], keyvar_init_val = "LIBXL_DOMAIN_TYPE_INVALID")), ], dir=DIR_IN )
Re: [PATCH 2/3] libxl_types.idl: use empty Struct for invalid domain type
Quoted 7 lines · Ian Campbell
Quoted 2 lines · Wei Liu
Using None makes
gentypes.pygenerates a C function which generates nothing. Eventually the generated JSON string is malformed.I'd prefer to fix the generator to not do this rather than muddying the input description. How hard would it be?
It certainly yields a patch much larger than this one-liner. The way to fix generator is when parsing keyed-union we need to 1) get hold of the key, 2) get its "invalid" value, 3) skip code generation if key points to "invalid".
We don't have definition in IDL for "invalid" value yet, so we need to add more code to get 2) working.
I only discovered this when I run testidl. There's no existing users of the generation function in libxl, so I think this fix is quite safe.
Wei.
Quoted 22 lines
Ian.
Quoted 19 lines
Signed-off-by Wei Liu <[email protected]>--- tools/libxl/libxl_types.idl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tools/libxl/libxl_types.idl +1
-1@@ -384,7 +384,7 @@ libxl_domain_build_info = Struct("domain_build_info",[ # Use host's E820 for PCI passthrough. ("e820_host", libxl_defbool), ])), - ("invalid", None), + ("invalid", Struct(None, [])), ], keyvar_init_val = "LIBXL_DOMAIN_TYPE_INVALID")), ], dir=DIR_IN )
Re: [PATCH 2/3] libxl_types.idl: use empty Struct for invalid domain type
Quoted 5 lines · Wei Liu
Quoted 4 lines · Ian Campbell
Quoted 2 lines · Wei Liu
Using None makes
gentypes.pygenerates a C function which generates nothing. Eventually the generated JSON string is malformed.
Quoted 8 lines
Quoted 3 lines
I'd prefer to fix the generator to not do this rather than muddying the input description. How hard would it be?
It certainly yields a patch much larger than this one-liner. The way to fix generator is when parsing keyed-union we need to 1) get hold of the key, 2) get its "invalid" value, 3) skip code generation if key points to "invalid".
Can you point me to the generate code which wrong? What I'm seeing in e.g. libxl_domain_build_info_gen_json is:
s = yajl_gen_map_open(hand);
...
switch (p->type) {
case LIBXL_DOMAIN_TYPE_HVM:
s = yajl_gen_map_open(hand);
...
s = yajl_gen_map_close(hand);
if (s != yajl_gen_status_ok)
goto out;
break;
case LIBXL_DOMAIN_TYPE_PV:
s = yajl_gen_map_open(hand);
...
s = yajl_gen_map_close(hand);
if (s != yajl_gen_status_ok)
goto out;
break;
case LIBXL_DOMAIN_TYPE_INVALID:
break;
}
s = yajl_gen_map_close(hand);Does that produce invalid json somehow?
And if it did could we not detect the use of None with an explicit check in the generator somewhere rather than worrying about whether a given enum value is valid or invalid? Relying on INVALID would be wrong anyway. It is in theory legitimate for a valid value to have no extra data in the enum -- a bunch of the entries in libxl_event could be so for example.
Quoted 5 lines
We don't have definition in IDL for "invalid" value yet, so we need to add more code to get 2) working.
I only discovered this when I run testidl. There's no existing users of the generation function in libxl, so I think this fix is quite safe.
At least libxl_domain_config_gen_json is used (by xl).
Ian.
Re: [PATCH 2/3] libxl_types.idl: use empty Struct for invalid domain type
Quoted 15 lines · Ian Campbell
Quoted 5 lines · Wei Liu
Quoted 4 lines · Ian Campbell
Quoted 2 lines · Wei Liu
Using None makes
gentypes.pygenerates a C function which generates nothing. Eventually the generated JSON string is malformed.Quoted 8 lines
Quoted 3 lines
I'd prefer to fix the generator to not do this rather than muddying the input description. How hard would it be?
It certainly yields a patch much larger than this one-liner. The way to fix generator is when parsing keyed-union we need to 1) get hold of the key, 2) get its "invalid" value, 3) skip code generation if key points to "invalid".
Quoted 4 lines
Can you point me to the generate code which wrong? What I'm seeing in e.g.
libxl_domain_build_info_gen_jsonis:s = yajl_gen_map_open(hand); ...
[ generate a map key called "u" ]
Quoted 9 lines
switch (p->type) {
case LIBXL_DOMAIN_TYPE_HVM:
s = yajl_gen_map_open(hand);
...
s = yajl_gen_map_close(hand);
if (s != yajl_gen_status_ok)
goto out;
break;
case LIBXL_DOMAIN_TYPE_PV:Quoted 8 lines
s = yajl_gen_map_open(hand);
...
s = yajl_gen_map_close(hand);
if (s != yajl_gen_status_ok)
goto out;
break;
case LIBXL_DOMAIN_TYPE_INVALID:
break;Here it doesn't generate value for map "u".
"u"(end of output) -- malformed JSON
With this change now it generate an empty map as the value of "u".
"u": { } -- correct JSON
Quoted 6 lines
} s = yajl_gen_map_close(hand);Does that produce invalid json somehow?
And if it did could we not detect the use of None with an explicit check
gentypes does that already. The result is not generating anything.
Quoted 2 lines
in the generator somewhere rather than worrying about whether a given enum value is valid or invalid? Relying on INVALID would be wrong
In the above example we need to skip generation of "u" if we encounter None or "INVALID". That's back referencing parent in recursive routines which requires more complex changes.
Quoted 11 lines
anyway. It is in theory legitimate for a valid value to have no extra data in the enum -- a bunch of the entries in
libxl_eventcould be so for example.Quoted 5 lines
We don't have definition in IDL for "invalid" value yet, so we need to add more code to get 2) working.
I only discovered this when I run testidl. There's no existing users of the generation function in libxl, so I think this fix is quite safe.
At least
libxl_domain_config_gen_jsonis used (by xl).
But when you create a domain the domain type will not be "INVALID". That's why it doesn't fail I presume. I only discoverd this when running testidl.
What's the possible downside you see with this change?
Wei.
Quoted 2 lines
Ian.
Re: [PATCH 2/3] libxl_types.idl: use empty Struct for invalid domain type
Quoted 48 lines · Wei Liu
Quoted 15 lines · Ian Campbell
Quoted 5 lines · Wei Liu
Quoted 4 lines · Ian Campbell
Quoted 2 lines · Wei Liu
Using None makes
gentypes.pygenerates a C function which generates nothing. Eventually the generated JSON string is malformed.Quoted 8 lines
Quoted 3 lines
I'd prefer to fix the generator to not do this rather than muddying the input description. How hard would it be?
It certainly yields a patch much larger than this one-liner. The way to fix generator is when parsing keyed-union we need to 1) get hold of the key, 2) get its "invalid" value, 3) skip code generation if key points to "invalid".
Quoted 4 lines
Can you point me to the generate code which wrong? What I'm seeing in e.g.
libxl_domain_build_info_gen_jsonis:s = yajl_gen_map_open(hand); ...[ generate a map key called "u" ]
Quoted 9 lines
switch (p->type) { case LIBXL_DOMAIN_TYPE_HVM: s = yajl_gen_map_open(hand); ... s = yajl_gen_map_close(hand); if (s != yajl_gen_status_ok) goto out; break; case LIBXL_DOMAIN_TYPE_PV:Quoted 8 lines
s = yajl_gen_map_open(hand); ... s = yajl_gen_map_close(hand); if (s != yajl_gen_status_ok) goto out; break; case LIBXL_DOMAIN_TYPE_INVALID: break;Here it doesn't generate value for map "u".
"u"(end of output) -- malformed JSONWith this change now it generate an empty map as the value of "u".
"u": { } -- correct JSON
Quoted 8 lines
Quoted 6 lines
} s = yajl_gen_map_close(hand);Does that produce invalid json somehow?
And if it did could we not detect the use of None with an explicit check
gentypes does that already. The result is not generating anything.
So make it generate something? That's just a map_open/map_close isn't it?
Quoted 6 lines
Quoted 2 lines
in the generator somewhere rather than worrying about whether a given enum value is valid or invalid? Relying on INVALID would be wrong
In the above example we need to skip generation of "u" if we encounter None or "INVALID". That's back referencing parent in recursive routines which requires more complex changes.
Which would be needed to push generating map "u" down into the switch? OK to avoiding that then.
Quoted 11 lines
Quoted 11 lines
anyway. It is in theory legitimate for a valid value to have no extra data in the enum -- a bunch of the entries in
libxl_eventcould be so for example.Quoted 5 lines
We don't have definition in IDL for "invalid" value yet, so we need to add more code to get 2) working.
I only discovered this when I run testidl. There's no existing users of the generation function in libxl, so I think this fix is quite safe.
Quoted 7 lines
Quoted 1 line
At least
libxl_domain_config_gen_jsonis used (by xl).But when you create a domain the domain type will not be "INVALID". That's why it doesn't fail I presume. I only discoverd this when running testidl.
What's the possible downside you see with this change?
It's a pointless empty struct specified in the idl which I'd rather avoid.
Ian.
Re: [PATCH 2/3] libxl_types.idl: use empty Struct for invalid domain type
On Thu, Apr 10, 2014 at 03:50:02PM +0100, Ian Campbell wrote:
[...]
Quoted 7 lines
Quoted 3 lines
Quoted 1 line
And if it did could we not detect the use of None with an explicit check
gentypes does that already. The result is not generating anything.
So make it generate something? That's just a map_open/map_close isn't it?
Yes, that's the whole point of using empty Struct.
I guess I can go down this route to make it generate empty map, achieving the same result like using empty Struct.
Quoted 10 lines
Quoted 6 lines
Quoted 2 lines
in the generator somewhere rather than worrying about whether a given enum value is valid or invalid? Relying on INVALID would be wrong
In the above example we need to skip generation of "u" if we encounter None or "INVALID". That's back referencing parent in recursive routines which requires more complex changes.
Which would be needed to push generating map "u" down into the switch? OK to avoiding that then.
I think the above approach is cleaner.
Wei.
Re: [PATCH 2/3] libxl_types.idl: use empty Struct for invalid domain type
Quoted 6 lines · Wei Liu
On Thu, Apr 10, 2014 at 03:50:02PM +0100, Ian Campbell wrote:
[...]Quoted 4 lines
Quoted 3 lines
Quoted 1 line
And if it did could we not detect the use of None with an explicit check
gentypes does that already. The result is not generating anything.
Quoted 8 lines
Quoted 3 lines
So make it generate something? That's just a map_open/map_close isn't it?
Yes, that's the whole point of using empty Struct.
I guess I can go down this route to make it generate empty map, achieving the same result like using empty Struct.
Please.
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
Quoted 2 lines
in the generator somewhere rather than worrying about whether a given enum value is valid or invalid? Relying on INVALID would be wrong
In the above example we need to skip generation of "u" if we encounter None or "INVALID". That's back referencing parent in recursive routines which requires more complex changes.
Quoted 5 lines
Quoted 3 lines
Which would be needed to push generating map "u" down into the switch? OK to avoiding that then.
I think the above approach is cleaner.
Yes.
Re: [PATCH 2/3] libxl_types.idl: use empty Struct for invalid domain type
In reply to Ian CampbellQuoted 8 lines · Ian Campbell
Quoted 4 lines
Quoted 2 lines
And if it did could we not detect the use of None with an explicit check
gentypes does that already. The result is not generating anything.
So make it generate something? That's just a map_open/map_close isn't it?
So if a keyed-union field is of type None:
tools/libxl/gentypes.py +7 -0
@@ -220,6 +220,13 @@ def libxl_C_type_gen_json(ty, v, indent = " ", parent = None): s += "case %s:\n" % f.enumname if f.type is not None: s += libxl_C_type_gen_json(f.type, fexpr, indent + " ", nparent) + else: + s += " yajl_gen_map_open(hand);\n" + s += " if (s != yajl_gen_status_ok)\n" + s += " goto out;\n" + s += " yajl_gen_map_close(hand);\n" + s += " if (s != yajl_gen_status_ok)\n" + s += " goto out;\n" s += " break;\n" s += "}\n" elif isinstance(ty, idl.Struct) and (parent is None or ty.json_fn is None):
Re: [PATCH 2/3] libxl_types.idl: use empty Struct for invalid domain type
Quoted 11 lines · Wei Liu
Quoted 8 lines · Ian Campbell
Quoted 4 lines
Quoted 2 lines
And if it did could we not detect the use of None with an explicit check
gentypes does that already. The result is not generating anything.
So make it generate something? That's just a map_open/map_close isn't it?
So if a keyed-union field is of type None:
Looks plausible, but I've not diffed the generated code.
What about yajl_gen_null() perhaps?
Quoted 20 lines
tools/libxl/gentypes.py +7
-0@@ -220,6 +220,13 @@ def libxl_C_type_gen_json(ty, v, indent = " ", parent = None): s += "case %s:\n" % f.enumname if f.type is not None: s += libxl_C_type_gen_json(f.type, fexpr, indent + " ", nparent) + else: + s += " yajl_gen_map_open(hand);\n" + s += " if (s != yajl_gen_status_ok)\n" + s += " goto out;\n" + s += " yajl_gen_map_close(hand);\n" + s += " if (s != yajl_gen_status_ok)\n" + s += " goto out;\n" s += " break;\n" s += "}\n" elif isinstance(ty, idl.Struct) and (parent is None or ty.json_fn is None):
Re: [PATCH 2/3] libxl_types.idl: use empty Struct for invalid domain type
Quoted 7 lines · Ian Campbell
Quoted 6 lines · Wei Liu
Quoted 5 lines · Ian Campbell
Quoted 4 lines
Quoted 2 lines
And if it did could we not detect the use of None with an explicit check
gentypes does that already. The result is not generating anything.
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
So make it generate something? That's just a map_open/map_close isn't it?
So if a keyed-union field is of type None:
Looks plausible, but I've not diffed the generated code.
case LIBXL_DOMAIN_TYPE_INVALID:
+ yajl_gen_map_open(hand);
+ if (s != yajl_gen_status_ok)
+ goto out;
+ yajl_gen_map_close(hand);
+ if (s != yajl_gen_status_ok)
+ goto out;
break;Quoted 1 line
I would rather stick with a map to be consistent with other keys. This can simplify later code generation for parser.
Wei.
Re: [PATCH 2/3] libxl_types.idl: use empty Struct for invalid domain type
In reply to Ian CampbellQuoted 3 lines · Ian Campbell
It is in theory legitimate for a valid value to have no extra data in the enum -- a bunch of the entries in
libxl_eventcould be so for example.
Would you mind tacking a patch onto this series to fixup the event ones to not use the empty struct trick? (I can't remember why we did this, perhaps it was this same breakage...)
Ian.
Re: [PATCH 2/3] libxl_types.idl: use empty Struct for invalid domain type
Quoted 7 lines · Ian Campbell
Quoted 3 lines · Ian Campbell
It is in theory legitimate for a valid value to have no extra data in the enum -- a bunch of the entries in
libxl_eventcould be so for example.Would you mind tacking a patch onto this series to fixup the event ones to not use the empty struct trick? (I can't remember why we did this,
Sure.
Quoted 1 line
perhaps it was this same breakage...)
It must be. :-)
Wei.
Quoted 2 lines
Ian.
Re: [PATCH 2/3] libxl_types.idl: use empty Struct for invalid domain type
Quoted 10 lines · Wei Liu
Quoted 7 lines · Ian Campbell
Quoted 3 lines · Ian Campbell
It is in theory legitimate for a valid value to have no extra data in the enum -- a bunch of the entries in
libxl_eventcould be so for example.Would you mind tacking a patch onto this series to fixup the event ones to not use the empty struct trick? (I can't remember why we did this,
Sure.
Thanks!
PATCH 3/3 libxl_json: fix JSON parser debug code
In reply to Wei LiuTwo changes included:
1. implement DEBUG_GEN_ALLOC for YAJL2
2. use size_t for variable "len"
without these two fixes it fails to compile when DEBUG_ANSWER is defined.
--- tools/libxl/libxl_json.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-)
tools/libxl/libxl_json.c +13 -4
@@ -34,11 +34,20 @@ struct libxl__yajl_ctx { };#ifdef DEBUG_ANSWER -# define DEBUG_GEN_ALLOC (ctx) \ - if ((ctx)->g == NULL) { \ - yajl_gen_config conf = { 1, " " }; \ +#if YAJL_VERSION <20000 +# define DEBUG_GEN_ALLOC (ctx) \ + if ((ctx)->g == NULL) { \ + yajl_gen_config conf = { 1, " " }; \ (ctx)->g = yajl_gen_alloc(&conf, NULL); \ } +#else /* YAJL2 */ +# define DEBUG_GEN_ALLOC (ctx) \ + if ((ctx)->g == NULL) { \ + (ctx)->g = yajl_gen_alloc(NULL); \ + yajl_gen_config((ctx)->g, yajl_gen_beautify, 1); \ + yajl_gen_config((ctx)->g, yajl_gen_indent_string, " "); \ + } +#endif # define DEBUG_GEN_FREE (ctx) \ if ((ctx)->g) yajl_gen_free((ctx)->g)# define DEBUG_GEN (ctx, type) yajl_gen_##type(ctx->g)
@@ -48,7 +57,7 @@ struct libxl__yajl_ctx {# define DEBUG_GEN_REPORT (yajl_ctx) \ do { \ const unsigned char *buf = NULL; \ - unsigned int len = 0; \ + size_t len = 0; \ yajl_gen_get_buf((yajl_ctx)->g, &buf, &len); \ LIBXL__LOG(libxl__gc_owner((yajl_ctx)->gc), \ LIBXL__LOG_DEBUG, "response:\n%s", buf); \
Signature · 1 line
1.7.10.4