libxl: make _dispose idempotent and NULL-tolerant
cover libxl: make _dispose idempotent and NULL-tolerant
The first two patches are bug fixes.
Other patches are used to make _dispose idempotent and NULL-tolerant.
Wei.
Wei Liu (8):
libxl: fix off-by-one error in JSON parser
gentest: make testidl valgrind clean
libxl: make some _dipose functions idempotent and tolerate NULL
gentypes: do not poison struct
gentypes: make dispose function tolerate NULL
gentypes: set pointer to NULL
testidl: call _init and _dispose several times
libxl: update libxl.h to say _dispose is idempotent
tools/libxl/gentest.py | 22 ++++++++++++++++------ tools/libxl/gentypes.py | 21 +++++++++++++-------- tools/libxl/libxl.c | 11 +++++++++-- tools/libxl/libxl.h | 3 +-- tools/libxl/libxl_cpuid.c | 5 ++++- tools/libxl/libxl_json.c | 2 +- tools/libxl/libxl_utils.c | 5 +++++ 7 files changed, 49 insertions(+), 20 deletions(-)
Signature · 1 line
1.9.1
PATCH 1/8 libxl: fix off-by-one error in JSON parser
We need a sentinel slot in the generated libxl_key_value_list.
---
This should be backported to 4.5.
--- tools/libxl/libxl_json.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/libxl/libxl_json.c +1 -1
@@ -247,7 +247,7 @@ int libxl__key_value_list_parse_json(libxl__gc *gc, const libxl__json_object *o, maps =libxl__json_object_get_map (o); size = maps->count *2 ; - kvl = *p =libxl__calloc (NOGC , size,sizeof (char *)); + kvl = *p =libxl__calloc (NOGC , size+1 ,sizeof (char *));for (i =0 ; i < maps->count; i++) {int idx = i *2 ;
Signature · 1 line
1.9.1
PATCH 2/8 gentest: make testidl valgrind clean
In reply to Wei LiuFree the JSON string after use to avoid memory leak. With this change testidl is valgrind clean.
--- tools/libxl/gentest.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)
tools/libxl/gentest.py +7 -3
@@ -229,7 +229,7 @@ int main(int argc, char **argv) (ty.typename, ty.typename, ty.typename)) f.write(""" int rc; - char *s, *new_s; + char *s, *new_s, *json_string; xentoollog_logger_stdiostream *logger; libxl_ctx *ctx;
@@ -323,9 +323,13 @@ int main(int argc, char **argv) f.write(" printf(\"%s -- to JSON:\\n\");\n" % (ty.typename)) for v in ty.values: + f.write(" json_string = %s_to_json(ctx, %s);\n" % \ + (ty.typename, v.name)) f.write(" printf(\"\\t%s = %%d = %%s\", " \ - "%s, %s_to_json(ctx, %s));\n" %\ - (v.valuename, v.name, ty.typename, v.name)) + "%s, json_string);\n" %\ + (v.valuename, v.name)) + f.write(" free(json_string);\n"); + f.write(" json_string = NULL;\n"); f.write("\n") f.write(" printf(\"%s -- from string:\\n\");\n" % (ty.typename))
Signature · 1 line
1.9.1
PATCH 3/8 libxl: make some _dipose functions idempotent and tolerate NULL
In reply to Wei LiuThese functions are not generated, so we need to do it by hand.
Functions list:
libxl_bitmap_dispose
libxl_string_list_dispose
libxl_key_value_list_dipose
libxl_cpuid_dispose
--- tools/libxl/libxl.c | 11 +++++++++-- tools/libxl/libxl_cpuid.c | 5 ++++- tools/libxl/libxl_utils.c | 5 +++++ 3 files changed, 18 insertions(+), 3 deletions(-)
tools/libxl/libxl.c +9 -2
@@ -214,9 +214,12 @@ void libxl_string_list_dispose(libxl_string_list *psl)if (!sl)return ; -for (i =0 ; sl[i] !=NULL ; i++) +for (i =0 ; sl[i] !=NULL ; i++) {free (sl[i]); + sl[i] =NULL ; + }free (sl); + *psl =NULL ; } void libxl_string_list_copy(libxl_ctx *ctx,
@@ -276,10 +279,14 @@ void libxl_key_value_list_dispose(libxl_key_value_list *pkvl)for (i =0 ; kvl[i] !=NULL ; i +=2 ) {free (kvl[i]); -if (kvl[i +1 ]) + kvl[i] =NULL ; +if (kvl[i +1 ]) {free (kvl[i +1 ]); + kvl[i+1 ] =NULL ; + } }free (kvl); + *pkvl =NULL ; } void libxl_key_value_list_copy(libxl_ctx *ctx,
tools/libxl/libxl_cpuid.c +4 -1
@@ -28,10 +28,13 @@ void libxl_cpuid_dispose(libxl_cpuid_policy_list *p_cpuid_list)return ;for (i =0 ; cpuid_list[i].input[0 ] !=XEN_CPUID_INPUT_UNUSED ; i++) {for (j =0 ; j <4 ; j++) -if (cpuid_list[i].policy[j] !=NULL ) +if (cpuid_list[i].policy[j] !=NULL ) {free (cpuid_list[i].policy[j]); + cpuid_list[i].policy[j] =NULL ; + } }free (cpuid_list); + *p_cpuid_list =NULL ;return ; }
tools/libxl/libxl_utils.c +5 -0
@@ -604,7 +604,12 @@ void libxl_bitmap_init(libxl_bitmap *map)void libxl_bitmap_dispose (libxl_bitmap *map) { +if (!map) +return ; +free (map->map); + map->map =NULL ; + map->size =0 ; } void libxl_bitmap_copy(libxl_ctx *ctx, libxl_bitmap *dptr,
Signature · 1 line
1.9.1
PATCH 4/8 gentypes: do not poison struct
In reply to Wei LiuWe don't want to poison the struct any more because we will later set pointers inside a struct to NULL to make it idempotent.
--- tools/libxl/gentypes.py | 3 --- 1 file changed, 3 deletions(-)
tools/libxl/gentypes.py +0 -3
@@ -636,15 +636,12 @@ if __name__ == '__main__': #include "libxl_internal.h" -#define LIBXL_DTOR_POISON 0xa5 - """ % " ".join(sys.argv)) for ty in [t for t in types if t.dispose_fn is not None and t.autogenerate_dispose_fn]: f.write("void %s(%s)\n" % (ty.dispose_fn, ty.make_arg("p"))) f.write("{\n") f.write(libxl_C_type_dispose(ty, "p")) - f.write(" memset(p, LIBXL_DTOR_POISON, sizeof(*p));\n") f.write("}\n") f.write("\n")
Signature · 1 line
1.9.1
PATCH 5/8 gentypes: make dispose function tolerate NULL
In reply to Wei Liu--- tools/libxl/gentypes.py | 1 + 1 file changed, 1 insertion(+)
tools/libxl/gentypes.py +1 -0
@@ -641,6 +641,7 @@ if __name__ == '__main__': for ty in [t for t in types if t.dispose_fn is not None and t.autogenerate_dispose_fn]: f.write("void %s(%s)\n" % (ty.dispose_fn, ty.make_arg("p"))) f.write("{\n") + f.write(" if (!p) return;\n") f.write(libxl_C_type_dispose(ty, "p")) f.write("}\n") f.write("\n")
Signature · 1 line
1.9.1
PATCH 6/8 gentypes: set pointer to NULL
In reply to Wei LiuIf the dispose_fn of a type is "free" it means it should be later set to NULL.
--- tools/libxl/gentypes.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-)
tools/libxl/gentypes.py +12 -5
@@ -78,14 +78,20 @@ def libxl_C_type_dispose(ty, v, indent = " ", parent = None): raise Exception("Array type must have a parent") if ty.elem_type.dispose_fn is not None: s += "{\n" - s += " int i;\n" - s += " for (i=0; i<%s; i++)\n" % (parent + ty.lenvar.name) + s += " if (%s) {\n" % (ty.pass_arg(v, parent is None),) + s += " int i;\n" + s += " for (i=0; i<%s; i++)\n" % (parent + ty.lenvar.name) s += libxl_C_type_dispose(ty.elem_type, v+"[i]", - indent + " ", parent) + indent + " ", parent) if ty.dispose_fn is not None: if ty.elem_type.dispose_fn is not None: - s += " " + s += " " s += "%s(%s);\n" % (ty.dispose_fn, ty.pass_arg(v, parent is None)) + if ty.dispose_fn == "free": + if ty.elem_type.dispose_fn is not None: s += " " + s += " %s = NULL;\n" % (ty.pass_arg(v, parent is None),) + if ty.elem_type.dispose_fn is not None: + s += " }\n" if ty.elem_type.dispose_fn is not None: s += "}\n" elif isinstance(ty, idl.Struct) and (parent is None or ty.dispose_fn is None):
@@ -95,7 +101,8 @@ def libxl_C_type_dispose(ty, v, indent = " ", parent = None): else: if ty.dispose_fn is not None: s += "%s(%s);\n" % (ty.dispose_fn, ty.pass_arg(v, parent is None)) - + if ty.dispose_fn == "free": + s += "%s = NULL;\n" % (ty.pass_arg(v, parent is None),) if s != "": s = indent + s return s.replace("\n", "\n%s" % indent).rstrip(indent)
Signature · 1 line
1.9.1
PATCH 7/8 testidl: call _init and _dispose several times
In reply to Wei LiuCall _init and _dispose between 1 to 10 times on a type to test if _init and _dispose are idempotent.
--- tools/libxl/gentest.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)
tools/libxl/gentest.py +9 -3
@@ -249,8 +249,11 @@ int main(int argc, char **argv) f.write(" %s_rand_init(%s);\n" % (ty.typename, \ ty.pass_arg(arg, isref=False, passby=idl.PASS_BY_REFERENCE))) if not isinstance(ty, idl.Enumeration): - f.write(" %s_init(%s_new);\n" % (ty.typename, \ - ty.pass_arg(arg, isref=False, passby=idl.PASS_BY_REFERENCE))) + iters = random.randrange(1,10) + while iters > 0: + f.write(" %s_init(%s_new);\n" % (ty.typename, \ + ty.pass_arg(arg, isref=False, passby=idl.PASS_BY_REFERENCE))) + iters -= 1 f.write(" s = %s_to_json(ctx, %s);\n" % \ (ty.typename, ty.pass_arg(arg, isref=False))) f.write(" printf(\"%%s: %%s\\n\", \"%s\", s);\n" % ty.typename)
@@ -269,8 +272,11 @@ int main(int argc, char **argv) f.write(" free(s);\n") f.write(" free(new_s);\n") if ty.dispose_fn is not None: + iters = random.randrange(1,10) f.write(" %s(&%s_val);\n" % (ty.dispose_fn, ty.typename)) - f.write(" %s(&%s_val_new);\n" % (ty.dispose_fn, ty.typename)) + while iters > 0: + f.write(" %s(&%s_val_new);\n" % (ty.dispose_fn, ty.typename)) + iters -= 1 f.write("\n") f.write(" printf(\"Testing TYPE_copy()\\n\");\n")
Signature · 1 line
1.9.1
PATCH 8/8 libxl: update libxl.h to say _dispose is idempotent
In reply to Wei Liu--- tools/libxl/libxl.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
tools/libxl/libxl.h +1 -2
@@ -308,8 +308,7 @@* once afterwards, to clean up, regardless of whether operations on * this object succeeded or failed. See the xl code for examples. * -* "init" is idempotent. We intend that "dispose" will become -* idempotent, but this is not currently the case. +* "init" and "dispose" are idempotent. * * void libxl_<type>_init(<type> *p): *
Signature · 1 line
1.9.1
Please ignore this series. I will send out another version that's simpler.
Wei.