Skip to contentSkip to conversation

tools: fixes inspired by Coverity scan

PATCH Superseded Wei Liu 5 participants 37 messages Jul 23, 2015 – Jul 24, 2015

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Wei Liu

author
#0 lore raw

cover tools: fixes inspired by Coverity scan

Wei Liu (13):
libxl: use thread-safe localtime_t and handle NULL
libxl: properly clean up array in libxl_list_cpupool failure path
libxl: remove a bunch of pointless assignments
xl: free pid string in do_daemonize
xl: check json string is not null before printing in create_domain
xl: call libxl_dominfo_{init,dispose} in psr_cmt_show
xl: call libxl_dominfo_{init,dispose} in main_cpupoolnumasplit
xl: call libxl_dominfo_init in main_list
xl: call libxl_bitmap_{init,dispose} in main_cpupoolcreate
xl: valid fd can be 0 in main_loadpolicy
xl: free event struct after use in main_shutdown_or_reboot tools/ocaml: call libxl_dominfo_{init,dispose} in stub tools/ocaml: handle strdup failure in stub_xl_device_disk_of_vdev

 tools/libxl/libxl.c                  | 31 ++++++++++++++++++++---------
 tools/libxl/libxl_dom.c              |  2 +-
 tools/libxl/libxl_remus_disk_drbd.c  |  2 +-
 tools/libxl/libxl_x86.c              | 11 +++++++++--
 tools/libxl/xl_cmdimpl.c             | 38 ++++++++++++++++++++++++++++--------
 tools/ocaml/libs/xl/xenlight_stubs.c | 12 ++++++++++--
 6 files changed, 73 insertions(+), 23 deletions(-)
Signature · 1 line
1.9.1
author
#1 lore raw

PATCH 1/13 libxl: use thread-safe localtime_t and handle NULL

---
 tools/libxl/libxl_x86.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
tools/libxl/libxl_x86.c +9 -2
index 8cd15ca..bac0b8f 100644
@@ -306,10 +306,16 @@ int libxl__arch_domain_create(libxl__gc *gc, libxl_domain_config *d_config,
     rtc_timeoffset = d_config->b_info.rtc_timeoffset;
     if (libxl_defbool_val(d_config->b_info.localtime)) {
         time_t t;
-        struct tm *tm;
+        struct tm *tm, result;
 
         t = time(NULL);
-        tm = localtime(&t);
+        tm = localtime_r(&t, &result);
+
+        if (!tm) {
+            LOGE(ERROR, "Failed to call localtime_r");
+            ret = ERROR_FAIL;
+            goto out;
+        }
 
         rtc_timeoffset += tm->tm_gmtoff;
     }
@@ -335,6 +341,7 @@ int libxl__arch_domain_create(libxl__gc *gc, libxl_domain_config *d_config,
         }
     }
 
+out:
     return ret;
 }
 
Signature · 1 line
1.9.1
author
#3 lore raw

Re: [PATCH for-4.6 01/13] libxl: use thread-safe localtime_t and handle NULL

Quoted 4 lines · Ian Campbell
Quoted 1 line · Wei Liu

I notice that I fat-fingered localtime_r as localtime_t in subject line. Ironically I pointed out the same error your reply to a previous version of this patch... :-/

Wei.

author
#4 lore raw

PATCH 2/13 libxl: properly clean up array in libxl_list_cpupool failure path

In reply to Wei Liu

Document how cpupool_info works. Distinguish success (ERROR_FAIL + ENOENT) vs failure in libxl_list_cpupool and properly clean up the array in failure path.

Also switch to libxl__realloc and call libxl_cpupool_{init,dispose} where appropriate.

There is change of behaviour. Previously if memory allocation fails the said function returns NULL. Now memory allocation failure is fatal. This is in line with how we deal with memory allocation failure in other places in libxl though.

---
Cc: Dario Faggioli <[email protected]>

---
 tools/libxl/libxl.c | 31 ++++++++++++++++++++++---------
 1 file changed, 22 insertions(+), 9 deletions(-)
tools/libxl/libxl.c +22 -9
index ff0d616..db88ecc 100644
@@ -682,6 +682,11 @@ int libxl_domain_info(libxl_ctx *ctx, libxl_dominfo *info_r,
     return 0;
 }
 
+/* Returns:
+ *   0 - success
+ *   ERROR_FAIL + errno == ENOENT - no entry found
+ *   ERROR_$FOO + errno != ENOENT - other failure
+ */
 static int cpupool_info(libxl__gc *gc,
                         libxl_cpupoolinfo *info,
                         uint32_t poolid,
@@ -737,7 +742,9 @@ int libxl_cpupool_info(libxl_ctx *ctx,
 libxl_cpupoolinfo * libxl_list_cpupool(libxl_ctx *ctx, int *nb_pool_out)
 {
     GC_INIT(ctx);
-    libxl_cpupoolinfo info, *ptr, *tmp;
+    libxl_cpupoolinfo info, *ptr;
+    bool failed = false;
+
     int i;
     uint32_t poolid;
 
@@ -745,18 +752,24 @@ libxl_cpupoolinfo * libxl_list_cpupool(libxl_ctx *ctx, int *nb_pool_out)
 
     poolid = 0;
     for (i = 0;; i++) {
-        if (cpupool_info(gc, &info, poolid, false))
+        libxl_cpupoolinfo_init(&info);
+        if (cpupool_info(gc, &info, poolid, false)) {
+            if (errno != ENOENT) failed = true;
+            libxl_cpupoolinfo_dispose(&info);
             break;
-        tmp = realloc(ptr, (i + 1) * sizeof(libxl_cpupoolinfo));
-        if (!tmp) {
-            LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "allocating cpupool info");
-            libxl_cpupoolinfo_list_free(ptr, i);
-            ptr = NULL;
-            goto out;
         }
-        ptr = tmp;
+
+        ptr = libxl__realloc(NOGC, ptr, (i+1) * sizeof(libxl_cpupoolinfo));
         ptr[i] = info;
         poolid = info.poolid + 1;
+        /* Don't dispose of info because it will be returned to caller */
+    }
+
+    if (failed) {
+        libxl_cpupoolinfo_list_free(ptr, i);
+        ptr = NULL;
+        *nb_pool_out = 0;
+        goto out;
     }
 
     *nb_pool_out = i;
Signature · 1 line
1.9.1
#5 lore raw

Re: [PATCH for-4.6 02/13] libxl: properly clean up array in libxl_list_cpupool failure path

Quoted 8 lines · Wei Liu

Document how cpupool_info works. Distinguish success (ERROR_FAIL + ENOENT) vs failure in libxl_list_cpupool and properly clean up the array
in failure path.

Also switch to libxl__realloc and call libxl_cpupool_{init,dispose} where appropriate.

Quoted 6 lines

There is change of behaviour. Previously if memory allocation fails the
said function returns NULL. Now memory allocation failure is fatal. This
is in line with how we deal with memory allocation failure in other places in libxl though.

I think this function would benefit from making the out: label be the error path and the success case just a return ptr (just before the label).

Then your error handling for cpupool_info would become

            libxl_cpupoolinfo_dispose(&info);
            if (errno != ENOENT) goto out;
            break;

and the "if (failed)" block would be at the out label (without the iff).

Such splitting of the success/failure case is allowed if the only shared code would be the GC_FREE, which is the case here.

Ian.

#6 lore raw

Re: [PATCH for-4.6 02/13] libxl: properly clean up array in libxl_list_cpupool failure path

Quoted 13 lines · Ian Campbell
Quoted 6 lines · Wei Liu

There is change of behaviour. Previously if memory allocation fails the
said function returns NULL. Now memory allocation failure is fatal. This
is in line with how we deal with memory allocation failure in other places in libxl though.

I think this function would benefit from making the out: label be the error path and the success case just a return ptr (just before the label).

Quoted 8 lines

Then your error handling for cpupool_info would become

            libxl_cpupoolinfo_dispose(&info);
            if (errno != ENOENT) goto out;
            break;

and the "if (failed)" block would be at the out label (without the iff).

I was about to give my Reviewed-by, but yes, I like Ian's suggestion too.

In any case, apart from how error handling is done, I think this patch is fine, as far as dealing with cpupools goes.

Dario

Signature · 4 lines
<<This happens because I choose it to happen!>> (Raistlin Majere)
-----------------------------------------------------------------
Dario Faggioli, Ph.D, http://about.me/dario.faggioli
Senior Software Engineer, Citrix Systems R&D Ltd., Cambridge (UK)

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

author
#7 lore raw

PATCH 3/13 xl/libxl: remove a bunch of pointless assignments

In reply to Wei Liu

Those values are overwritten before they can be of any use.

---
 tools/libxl/libxl_dom.c             | 2 +-
 tools/libxl/libxl_remus_disk_drbd.c | 2 +-
 tools/libxl/xl_cmdimpl.c            | 4 ++--
 3 files changed, 4 insertions(+), 4 deletions(-)
tools/libxl/libxl_dom.c +1 -1
index edd7f3f..93ab8fe 100644
@@ -1686,7 +1686,7 @@ void libxl__domain_save(libxl__egc *egc, libxl__domain_suspend_state *dss)
 {
     STATE_AO_GC(dss->ao);
     int port;
-    int rc = ERROR_FAIL;
+    int rc;
 
     /* Convenience aliases */
     const uint32_t domid = dss->domid;
tools/libxl/libxl_remus_disk_drbd.c +1 -1
index fc76b89..1c3a88a 100644
@@ -47,7 +47,7 @@ static void drbd_async_call(libxl__egc *egc,
                             void func(libxl__remus_device *),
                             libxl__ev_child_callback callback)
 {
-    int pid = -1, rc;
+    int pid, rc;
     libxl__ao_device *aodev = &dev->aodev;
     STATE_AO_GC(dev->rds->ao);
 
tools/libxl/xl_cmdimpl.c +2 -2
index 5c6d1b0..5c74a73 100644
@@ -4037,7 +4037,7 @@ static pid_t create_migration_child(const char *rune, int *send_fd,
                                         int *recv_fd)
 {
     int sendpipe[2], recvpipe[2];
-    pid_t child = -1;
+    pid_t child;
 
     if (!rune || !send_fd || !recv_fd)
         return -1;
@@ -7788,7 +7788,7 @@ int main_getenforce(int argc, char **argv)
 
 int main_setenforce(int argc, char **argv)
 {
-    int ret, mode = -1;
+    int ret, mode;
     const char *p = NULL;
 
     if (optind >= argc) {
Signature · 1 line
1.9.1
Acked-by on v1 3/13
#8 lore raw

Re: [PATCH for-4.6 03/13] xl/libxl: remove a bunch of pointless assignments

Quoted 1 line · Wei Liu

Those values are overwritten before they can be of any use.

I commented on a similar patch recently (having acked it):
The flip side is that if the code uses the "init everything and goto out on error" idiom then the init might need to be put back if the code changes, hopefully even a regular compiler would spot this though.

The rc = ERROR_FAIL one is consistent with coding style.

coding style does say that things which might need cleaning up should be initialised as they are declared, however I don't think any of the pid/ret's here need cleanup. So:

author
#9 lore raw

PATCH 4/13 xl: free pid string in do_daemonize

In reply to Wei Liu

Pid is a null terminated string allocated by asprintf. It should be freed after use.

Also fixed a coding style problem while I was there.

---
 tools/libxl/xl_cmdimpl.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
tools/libxl/xl_cmdimpl.c +3 -1
index 5c74a73..bfd8e59 100644
@@ -524,10 +524,12 @@ static int do_daemonize(char *name, const char *pidfile)
             exit(1);
         }
 
-        if ( close(fd) < 0 ) {
+        if (close(fd) < 0) {
             perror("Closing pidfile");
             exit(1);
         }
+
+        free(pid);
     }
 
 out:
Signature · 1 line
1.9.1
author
#11 lore raw

PATCH 5/13 xl: check json string is not null before printing in create_domain

In reply to Wei Liu
---
 tools/libxl/xl_cmdimpl.c | 5 +++++
 1 file changed, 5 insertions(+)
tools/libxl/xl_cmdimpl.c +5 -0
index bfd8e59..fc61650 100644
@@ -2763,6 +2763,11 @@ static uint32_t create_domain(struct domain_create *dom_info)
             printf_info_sexp(-1, &d_config, cfg_print_fh);
         } else {
             char *json = libxl_domain_config_to_json(ctx, &d_config);
+            if (!json) {
+                fprintf(stderr,
+                        "Failed to convert domain configuration to JSON\n");
+                exit(1);
+            }
             fputs(json, cfg_print_fh);
             free(json);
             flush_stream(cfg_print_fh);
Signature · 1 line
1.9.1
author
#13 lore raw

PATCH 6/13 xl: call libxl_dominfo_{init, dispose} in psr_cmt_show

In reply to Wei Liu
---
 tools/libxl/xl_cmdimpl.c | 3 +++
 1 file changed, 3 insertions(+)
tools/libxl/xl_cmdimpl.c +3 -0
index fc61650..2d95e35 100644
@@ -8217,11 +8217,14 @@ static int psr_cmt_show(libxl_psr_cmt_type type, uint32_t domid)
     /* Each domain */
     if (domid != INVALID_DOMID) {
         libxl_dominfo dominfo;
+
+        libxl_dominfo_init(&dominfo);
         if (libxl_domain_info(ctx, &dominfo, domid)) {
             fprintf(stderr, "Failed to get domain info for %d\n", domid);
             return -1;
         }
         psr_cmt_print_domain_info(&dominfo, type, nr_sockets);
+        libxl_dominfo_dispose(&dominfo);
     }
     else
     {
Signature · 1 line
1.9.1
author
#15 lore raw

PATCH 7/13 xl: call libxl_dominfo_{init, dispose} in main_cpupoolnumasplit

In reply to Wei Liu
---
 tools/libxl/xl_cmdimpl.c | 3 +++
 1 file changed, 3 insertions(+)
tools/libxl/xl_cmdimpl.c +3 -0
index 2d95e35..a4e56ef 100644
@@ -7660,6 +7660,8 @@ int main_cpupoolnumasplit(int argc, char **argv)
         /* No options */
     }
 
+    libxl_dominfo_init(&info);
+
     rc = 1;
 
     libxl_bitmap_init(&cpumap);
@@ -7768,6 +7770,7 @@ int main_cpupoolnumasplit(int argc, char **argv)
 out:
     libxl_cputopology_list_free(topology, n_cpus);
     libxl_bitmap_dispose(&cpumap);
+    libxl_dominfo_dispose(&info);
     free(name);
 
     return rc;
Signature · 1 line
1.9.1
#16 lore raw

Re: [PATCH for-4.6 07/13] xl: call libxl_dominfo_{init, dispose} in main_cpupoolnumasplit

Quoted 1 line · Wei Liu

info here seems to be used in a loop, without any init or free. I think the init should be moved there, and the looping case needs a dispose, with only the final one being left until the end.

(that loop looks a bit odd to me, but that's independent...)

author
#17 lore raw

Re: [PATCH for-4.6 07/13] xl: call libxl_dominfo_{init, dispose} in main_cpupoolnumasplit

Quoted 7 lines · Ian Campbell
Quoted 1 line · Wei Liu

info here seems to be used in a loop, without any init or free. I think the init should be moved there, and the looping case needs a dispose, with only the final one being left until the end.

Indeed, I will look into fixing that.

Quoted 1 line

(that loop looks a bit odd to me, but that's independent...)

author
#18 lore raw

PATCH 8/13 xl: call libxl_dominfo_init in main_list

In reply to Wei Liu

Also change the path that disposes of the buffer to use info_buf to avoid confusing coverity.

---
 tools/libxl/xl_cmdimpl.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
tools/libxl/xl_cmdimpl.c +3 -1
index a4e56ef..79cef7a 100644
@@ -4843,6 +4843,8 @@ int main_list(int argc, char **argv)
         info_free = info;
     } else if (optind == argc-1) {
         uint32_t domid = find_domain(argv[optind]);
+
+        libxl_dominfo_init(&info_buf);
         rc = libxl_domain_info(ctx, &info_buf, domid);
         if (rc == ERROR_DOMAIN_NOTFOUND) {
             fprintf(stderr, "Error: Domain \'%s\' does not exist.\n",
@@ -4869,7 +4871,7 @@ int main_list(int argc, char **argv)
     if (info_free)
         libxl_dominfo_list_free(info, nb_domain);
     else
-        libxl_dominfo_dispose(info);
+        libxl_dominfo_dispose(&info_buf);
 
     return 0;
 }
Signature · 1 line
1.9.1
Acked-by on v1 8/13
#19 lore raw

Re: [PATCH for-4.6 08/13] xl: call libxl_dominfo_init in main_list

Quoted 4 lines · Wei Liu

Also change the path that disposes of the buffer to use info_buf to avoid confusing coverity.

But would it be less confusing to both human and machine readers if info_buf was always init'd and dispose'd unconditionally at the head and tail of the function? i.e. ignoring the fact that in one code path we use a different info?

Quoted 27 lines
---
 tools/libxl/xl_cmdimpl.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
tools/libxl/xl_cmdimpl.c +2 -0
index a4e56ef..79cef7a 100644
@@ -4843,6 +4843,8 @@ int main_list(int argc, char **argv)
         info_free = info;
     } else if (optind == argc-1) {
         uint32_t domid = find_domain(argv[optind]);
+
+        libxl_dominfo_init(&info_buf);
         rc = libxl_domain_info(ctx, &info_buf, domid);
         if (rc == ERROR_DOMAIN_NOTFOUND) {
             fprintf(stderr, "Error: Domain \'%s\' does not 

exist.\n",

tools/libxl/xl_cmdimpl.c +1 -1
@@ -4869,7 +4871,7 @@ int main_list(int argc, char **argv)
     if (info_free)
         libxl_dominfo_list_free(info, nb_domain);
     else
-        libxl_dominfo_dispose(info);
+        libxl_dominfo_dispose(&info_buf);
 
     return 0;
 }
author
#20 lore raw

PATCH 9/13 xl: call libxl_bitmap_{init, dispose} in main_cpupoolcreate

In reply to Wei Liu
---
 tools/libxl/xl_cmdimpl.c | 5 +++++
 1 file changed, 5 insertions(+)
tools/libxl/xl_cmdimpl.c +5 -0
index 79cef7a..7e279cd 100644
@@ -7244,6 +7244,9 @@ int main_cpupoolcreate(int argc, char **argv)
         break;
     }
 
+    libxl_bitmap_init(&freemap);
+    libxl_bitmap_init(&cpumap);
+
     while (optind < argc) {
         if ((p = strchr(argv[optind], '='))) {
             string_realloc_append(&extra_config, "\n");
@@ -7405,6 +7408,8 @@ int main_cpupoolcreate(int argc, char **argv)
 out_cfg:
     xlu_cfg_destroy(config);
 out:
+    libxl_bitmap_dispose(&freemap);
+    libxl_bitmap_dispose(&cpumap);
     free(name);
     free(config_data);
     free(extra_config);
Signature · 1 line
1.9.1
author
#22 lore raw

PATCH 10/13 xl: valid fd can be 0 in main_loadpolicy

In reply to Wei Liu

Also fixed some style problems while I was there.

---
 tools/libxl/xl_cmdimpl.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
tools/libxl/xl_cmdimpl.c +3 -3
index 7e279cd..3717568 100644
@@ -7856,7 +7856,7 @@ int main_loadpolicy(int argc, char **argv)
 
     polFName = argv[optind];
     polFd = open(polFName, O_RDONLY);
-    if ( polFd < 0 ) {
+    if (polFd < 0) {
         fprintf(stderr, "Error occurred opening policy file '%s': %s\n",
                 polFName, strerror(errno));
         ret = -1;
@@ -7864,7 +7864,7 @@ int main_loadpolicy(int argc, char **argv)
     }
 
     ret = stat(polFName, &info);
-    if ( ret < 0 ) {
+    if (ret < 0) {
         fprintf(stderr, "Error occurred retrieving information about"
                 "policy file '%s': %s\n", polFName, strerror(errno));
         goto done;
@@ -7896,7 +7896,7 @@ int main_loadpolicy(int argc, char **argv)
 
 done:
     free(polMemCp);
-    if ( polFd > 0 )
+    if (polFd >= 0)
         close(polFd);
 
     return ret;
Signature · 1 line
1.9.1
#23 lore raw

Re: [PATCH for-4.6 10/13] xl: valid fd can be 0 in main_loadpolicy

Quoted 1 line · Wei Liu

Also fixed some style problems while I was there.

polFd is initialised to 0, I think it should be initialised to -1 as well as your change here, otherwise you can unexpectedly close stdin on some error paths.

(also: studly caps, yuk, not your fault I expect)

Quoted 40 lines
---
 tools/libxl/xl_cmdimpl.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
tools/libxl/xl_cmdimpl.c +1 -1
index 7e279cd..3717568 100644
@@ -7856,7 +7856,7 @@ int main_loadpolicy(int argc, char **argv)
 
     polFName = argv[optind];
     polFd = open(polFName, O_RDONLY);
-    if ( polFd < 0 ) {
+    if (polFd < 0) {
         fprintf(stderr, "Error occurred opening policy file '%s': 
 %s\n",
                 polFName, strerror(errno));
         ret = -1;
tools/libxl/xl_cmdimpl.c +1 -1
@@ -7864,7 +7864,7 @@ int main_loadpolicy(int argc, char **argv)
     }
 
     ret = stat(polFName, &info);
-    if ( ret < 0 ) {
+    if (ret < 0) {
         fprintf(stderr, "Error occurred retrieving information 

about"

                 "policy file '%s': %s\n", polFName, 
strerror(errno));
         goto done;
tools/libxl/xl_cmdimpl.c +1 -1
@@ -7896,7 +7896,7 @@ int main_loadpolicy(int argc, char **argv)
 
 done:
     free(polMemCp);
-    if ( polFd > 0 )
+    if (polFd >= 0)
         close(polFd);
 
     return ret;
author
#24 lore raw

PATCH 11/13 xl: free event struct after use in main_shutdown_or_reboot

In reply to Wei Liu
---
 tools/libxl/xl_cmdimpl.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
tools/libxl/xl_cmdimpl.c +3 -1
index 3717568..9edc0db 100644
@@ -4766,8 +4766,10 @@ static int main_shutdown_or_reboot(int do_reboot, int argc, char **argv)
                fallback_trigger);
         }
 
-        if (wait_for_it)
+        if (wait_for_it) {
             wait_for_domain_deaths(deathws, nb_domain - 1 /* not dom 0 */);
+            free(deathws);
+        }
 
         libxl_dominfo_list_free(dominfo, nb_domain);
     } else {
Signature · 1 line
1.9.1
Acked-by on v1 11/13
#25 lore raw

Re: [PATCH for-4.6 11/13] xl: free event struct after use in main_shutdown_or_reboot

Quoted 1 line · Wei Liu

It appears there is no libxl_evgen_domain_death_dispose to call on the individual members of the array. Ah, because it is opaque.

Although since it is init'd to NULL you could also call free unconditionally.

Quoted 22 lines
---
 tools/libxl/xl_cmdimpl.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
tools/libxl/xl_cmdimpl.c +3 -1
index 3717568..9edc0db 100644
@@ -4766,8 +4766,10 @@ static int main_shutdown_or_reboot(int 
 do_reboot, int argc, char **argv)
                fallback_trigger);
         }
 
-        if (wait_for_it)
+        if (wait_for_it) {
             wait_for_domain_deaths(deathws, nb_domain - 1 /* not dom 
 0 */);
+            free(deathws);
+        }
 
         libxl_dominfo_list_free(dominfo, nb_domain);
     } else {
author
#26 lore raw

PATCH 12/13 tools/ocaml: call libxl_dominfo_{init, dispose} in stub

In reply to Wei Liu

---
Cc: David Scott <[email protected]>

As far as I can tell, all Val_$foo function does deep-copy, so we can safely call dispose in said function.

---
 tools/ocaml/libs/xl/xenlight_stubs.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
tools/ocaml/libs/xl/xenlight_stubs.c +6 -2
index 4133527..7b8d6db 100644
@@ -979,14 +979,18 @@ value stub_xl_dominfo_get(value ctx, value domid)
 	int ret;
 	uint32_t c_domid = Int_val(domid);
 
+	libxl_dominfo_init(&c_dominfo);
+
 	caml_enter_blocking_section();
 	ret = libxl_domain_info(CTX, &c_dominfo, c_domid);
 	caml_leave_blocking_section();
 
-	if (ret != 0)
+	if (ret != 0) {
 		failwith_xl(ERROR_FAIL, "domain_info");
+		libxl_dominfo_dispose(&c_dominfo);
+	}
 	dominfo = Val_dominfo(&c_dominfo);
-
+	libxl_dominfo_dispose(&c_dominfo);
 	CAMLreturn(dominfo);
 }
 
Signature · 1 line
1.9.1
#27 lore raw

Re: [PATCH for-4.6 12/13] tools/ocaml: call libxl_dominfo_{init, dispose} in stub

Quoted 6 lines · Wei Liu

---
Cc: David Scott <[email protected]>

As far as I can tell, all Val_$foo function does deep-copy, so we can safely call dispose in said function.

Sadly this is insufficient. failwith_xl() longjump()s back into the ocaml runtime, which ends up leaking any allocations made for dominfo.

This is a systemic problem with the Ocaml bindings and I have a proposed solution but it involves rewriting quite a lot of this code and is definitely not 4.6 material.

~Andrew

Quoted 29 lines
---
 tools/ocaml/libs/xl/xenlight_stubs.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
tools/ocaml/libs/xl/xenlight_stubs.c +6 -2
index 4133527..7b8d6db 100644
@@ -979,14 +979,18 @@ value stub_xl_dominfo_get(value ctx, value domid)
 	int ret;
 	uint32_t c_domid = Int_val(domid);
 
+	libxl_dominfo_init(&c_dominfo);
+
 	caml_enter_blocking_section();
 	ret = libxl_domain_info(CTX, &c_dominfo, c_domid);
 	caml_leave_blocking_section();
 
-	if (ret != 0)
+	if (ret != 0) {
 		failwith_xl(ERROR_FAIL, "domain_info");
+		libxl_dominfo_dispose(&c_dominfo);
+	}
 	dominfo = Val_dominfo(&c_dominfo);
-
+	libxl_dominfo_dispose(&c_dominfo);
 	CAMLreturn(dominfo);
 }
 
author
#28 lore raw

Re: [PATCH for-4.6 12/13] tools/ocaml: call libxl_dominfo_{init, dispose} in stub

Quoted 8 lines · Andrew Cooper
Quoted 6 lines · Wei Liu

---
Cc: David Scott <[email protected]>

As far as I can tell, all Val_$foo function does deep-copy, so we can safely call dispose in said function.

Quoted 7 lines

Sadly this is insufficient. failwith_xl() longjump()s back into the ocaml runtime, which ends up leaking any allocations made for dominfo.

This is a systemic problem with the Ocaml bindings and I have a proposed solution but it involves rewriting quite a lot of this code and is definitely not 4.6 material.

Fine then. Systematic problem requires systematic fix. Let's ignore the last two patches in this series.

Wei.

#29 lore raw

Re: [PATCH for-4.6 12/13] tools/ocaml: call libxl_dominfo_{init, dispose} in stub

Quoted 11 lines · Wei Liu
Quoted 10 lines · Andrew Cooper
Quoted 6 lines · Wei Liu

---
Cc: David Scott <[email protected]>

As far as I can tell, all Val_$foo function does deep-copy, so we can safely call dispose in said function.

Sadly this is insufficient. failwith_xl() longjump()s back into the ocaml runtime, which ends up leaking any allocations made for dominfo.

Quoted 6 lines
Quoted 4 lines

This is a systemic problem with the Ocaml bindings and I have a proposed solution but it involves rewriting quite a lot of this code and is definitely not 4.6 material.

Fine then. Systematic problem requires systematic fix. Let's ignore the last two patches in this series.

Agreed.

What I not yet figured out how to fix is libxl__alloc_failed().

_exit() is not a reasonable out-of-memory action in a library, especially not in use in a runtime which actually has a garbage collector.

~Andrew

#30 lore raw

Re: [PATCH for-4.6 12/13] tools/ocaml: call libxl_dominfo_{init, dispose} in stub

In reply to Andrew Cooper
Quoted 9 lines · Andrew Cooper
Quoted 7 lines · Wei Liu

---
Cc: David Scott <[email protected]>

As far as I can tell, all Val_$foo function does deep-copy, so we can
safely call dispose in said function.

Quoted 8 lines

Sadly this is insufficient. failwith_xl() longjump()s back into the ocaml runtime, which ends up leaking any allocations made for dominfo.

This is a systemic problem with the Ocaml bindings and I have a proposed
solution but it involves rewriting quite a lot of this code and is definitely not 4.6 material.

Is it not sufficient to treat failwith_xl as a longjump statement (or any sort of "return-y" thing), which would simply necessitate doing the cleanup before calling it?

Perhaps Coverity could model it as such and would therefore warn about the dead code being added here?

#31 lore raw

Re: [PATCH for-4.6 12/13] tools/ocaml: call libxl_dominfo_{init, dispose} in stub

Quoted 17 lines · Ian Campbell
Quoted 8 lines · Andrew Cooper
Quoted 7 lines · Wei Liu

---
Cc: David Scott <[email protected]>

As far as I can tell, all Val_$foo function does deep-copy, so we can
safely call dispose in said function.

Quoted 8 lines

Sadly this is insufficient. failwith_xl() longjump()s back into the ocaml runtime, which ends up leaking any allocations made for dominfo.

This is a systemic problem with the Ocaml bindings and I have a proposed
solution but it involves rewriting quite a lot of this code and is definitely not 4.6 material.

Quoted 7 lines

Is it not sufficient to treat failwith_xl as a longjump statement (or any sort of "return-y" thing), which would simply necessitate doing the cleanup before calling it?

Perhaps Coverity could model it as such and would therefore warn about the dead code being added here?

Part of my Ocaml series is to properly mark failwith_xl() as a Noreturn function. Currently as far as the compiler and Coverity can tell, failwith_xl() may return normally.

While it is possible to rearrange this code to avoid leaking in the ret != 0 case, it is not possible to rearrange it to avoid leaking if Val_dominfo() uses failwith_xl()/caml_out_of_memory() itself.

The solution I have in mind is to wrap all libxl IDL objects in Ocaml Custom blocks, which allows the Ocaml runtime to garbage collect them.

~Andrew

author
#32 lore raw

PATCH 13/13 tools/ocaml: handle strdup failure in stub_xl_device_disk_of_vdev

In reply to Wei Liu

---
Cc: [email protected]

Please check if the use of caml_failwith is correct.

---
 tools/ocaml/libs/xl/xenlight_stubs.c | 4 ++++
 1 file changed, 4 insertions(+)
tools/ocaml/libs/xl/xenlight_stubs.c +4 -0
index 7b8d6db..dccd7ed 100644
@@ -780,6 +780,10 @@ value stub_xl_device_disk_of_vdev(value ctx, value domid, value vdev)
 
 	c_vdev = strdup(String_val(vdev));
 
+	if (!c_vdev) {
+		caml_failwith("Failed to duplicate vdev string.");
+	}
+
 	caml_enter_blocking_section();
 	libxl_vdev_to_device_disk(CTX, c_domid, c_vdev, &c_disk);
 	caml_leave_blocking_section();
Signature · 1 line
1.9.1
#33 lore raw

Re: [PATCH for-4.6 13/13] tools/ocaml: handle strdup failure in stub_xl_device_disk_of_vdev

Quoted 5 lines · Wei Liu

---
Cc: [email protected]

Please check if the use of caml_failwith is correct.

This issue I have a different patch for, which could be 4.6 material, if it weren't for the same issue as identified in patch 12.

In this case, Ocaml strings may contain embedded NULs which generally makes the use of the strxxx() functions incorrect. OTOH, the first thing libxl will do is assume that they are NUL terminated, so it probably isn't too much of a problem.

There is already a dup_String_Val() function which attempts to deal with this, although it has failure cases from integer overflows.

~Andrew

Quoted 19 lines
---
 tools/ocaml/libs/xl/xenlight_stubs.c | 4 ++++
 1 file changed, 4 insertions(+)
tools/ocaml/libs/xl/xenlight_stubs.c +4 -0
index 7b8d6db..dccd7ed 100644
@@ -780,6 +780,10 @@ value stub_xl_device_disk_of_vdev(value ctx, value domid, value vdev)
 
 	c_vdev = strdup(String_val(vdev));
 
+	if (!c_vdev) {
+		caml_failwith("Failed to duplicate vdev string.");
+	}
+
 	caml_enter_blocking_section();
 	libxl_vdev_to_device_disk(CTX, c_domid, c_vdev, &c_disk);
 	caml_leave_blocking_section();
#34 lore raw

Re: [PATCH for-4.6 13/13] tools/ocaml: handle strdup failure in stub_xl_device_disk_of_vdev

Quoted 10 lines · Andrew Cooper
Quoted 5 lines · Wei Liu

---
Cc: [email protected]

Please check if the use of caml_failwith is correct.

Quoted 7 lines

This issue I have a different patch for, which could be 4.6 material, if it weren't for the same issue as identified in patch 12.

In this case, Ocaml strings may contain embedded NULs which generally makes the use of the strxxx() functions incorrect. OTOH, the first thing libxl will do is assume that they are NUL terminated, so it probably isn't too much of a problem.

Yeah I would assume if my OCaml program sent libxl a legal OCaml string containing NULLs, that libxl would end up truncating it at the first NULL. I think it would be ok to document this at the OCaml level.

Cheers,
Dave

Quoted 26 lines

There is already a dup_String_Val() function which attempts to deal with this, although it has failure cases from integer overflows.

~Andrew

Quoted 19 lines

---
tools/ocaml/libs/xl/xenlight_stubs.c | 4 ++++

1 file changed, 4 insertions(+)
tools/ocaml/libs/xl/xenlight_stubs.c +4 -0
index 7b8d6db..dccd7ed 100644
@@ -780,6 +780,10 @@ value stub_xl_device_disk_of_vdev(value ctx, value domid, value vdev)
 
 	c_vdev = strdup(String_val(vdev));
 
+	if (!c_vdev) {
+		caml_failwith("Failed to duplicate vdev string.");
+	}
+
 	caml_enter_blocking_section();
 	libxl_vdev_to_device_disk(CTX, c_domid, c_vdev, &c_disk);
 	caml_leave_blocking_section();
Acked-by on v1 13/13
#35 lore raw

Re: [PATCH for-4.6 13/13] tools/ocaml: handle strdup failure in stub_xl_device_disk_of_vdev

In reply to Wei Liu
Quoted 8 lines · Wei Liu

---
Cc: [email protected]

Please check if the use of caml_failwith is correct.

This looks fine to me.

Quoted 22 lines

---
tools/ocaml/libs/xl/xenlight_stubs.c | 4 ++++

1 file changed, 4 insertions(+)
tools/ocaml/libs/xl/xenlight_stubs.c +4 -0
index 7b8d6db..dccd7ed 100644
@@ -780,6 +780,10 @@ value stub_xl_device_disk_of_vdev(value ctx, value domid, value vdev)
 
 	c_vdev = strdup(String_val(vdev));
 
+	if (!c_vdev) {
+		caml_failwith("Failed to duplicate vdev string.");
+	}
+
 	caml_enter_blocking_section();
 	libxl_vdev_to_device_disk(CTX, c_domid, c_vdev, &c_disk);
 	caml_leave_blocking_section();
Signature · 1 line
1.9.1
#36 lore raw
In reply to Wei Liu
Quoted 7 lines · Wei Liu

Wei Liu (13):
libxl: properly clean up array in libxl_list_cpupool failure path
xl: call libxl_dominfo_{init,dispose} in main_cpupoolnumasplit
xl: call libxl_dominfo_init in main_list
xl: valid fd can be 0 in main_loadpolicy
tools/ocaml: call libxl_dominfo_{init, dispose} in stub tools/ocaml: handle strdup failure in stub_xl_device_disk_of_vdev

I applied all but these.

I fixed localtime_r in the subject of #1.

Ian.