Richard W.M. Jones
2016-Apr-12  07:41 UTC
[Libguestfs] [PATCH v2] customize/perl_edit-c.c: Don't use internal APIs.
In v1 of this patch, there was the (small) possibility that 'g' might have been garbage collected while we were in the C function. Avoid this by passing 'g' to the function as well as the C pointer, so that 'g' is pinned as a garbage collector root [by CAMLparam5] so it cannot be collected while we're in the function. Rich.
Richard W.M. Jones
2016-Apr-12  07:41 UTC
[Libguestfs] [PATCH v2] customize/perl_edit-c.c: Don't use internal APIs.
We can now use the Guestfs.c_pointer method to access the underlying
guestfs_h *.  So no need to use internal APIs for this.
---
 customize/perl_edit-c.c | 18 ++++++------------
 customize/perl_edit.ml  | 10 ++++++++--
 2 files changed, 14 insertions(+), 14 deletions(-)
diff --git a/customize/perl_edit-c.c b/customize/perl_edit-c.c
index 753d990..7191bef 100644
--- a/customize/perl_edit-c.c
+++ b/customize/perl_edit-c.c
@@ -25,30 +25,24 @@
 #include <caml/alloc.h>
 #include <caml/memory.h>
 #include <caml/mlvalues.h>
+#include <caml/fail.h>
 
 #include "file-edit.h"
 
-/**
- * We try to reuse the internals of the OCaml binding (for extracting
- * the guestfs handle, and raising errors); hopefully this should be safe,
- * as long as it's kept internal within the libguestfs sources.
- */
-#include "../ocaml/guestfs-c.h"
-
 #pragma GCC diagnostic ignored "-Wmissing-prototypes"
 
 value
-virt_customize_edit_file_perl (value verbosev, value gv, value filev,
-                               value exprv)
+virt_customize_edit_file_perl (value verbosev, value gv, value gpv,
+                               value filev, value exprv)
 {
-  CAMLparam4 (verbosev, gv, filev, exprv);
+  CAMLparam5 (verbosev, gv, gpv, filev, exprv);
   int r;
-  guestfs_h *g = Guestfs_val (gv);
+  guestfs_h *g = (guestfs_h *) Int64_val (gpv);
 
   r = edit_file_perl (g, String_val (filev), String_val (exprv), NULL,
                       Bool_val (verbosev));
   if (r == -1)
-    guestfs_int_ocaml_raise_error (g, "edit_file_perl");
+    caml_failwith (guestfs_last_error (g) ? : "edit_file_perl: unknown
error");
 
   CAMLreturn (Val_unit);
 }
diff --git a/customize/perl_edit.ml b/customize/perl_edit.ml
index f1f06cc..5cd250b 100644
--- a/customize/perl_edit.ml
+++ b/customize/perl_edit.ml
@@ -18,6 +18,12 @@
 
 open Common_utils
 
-external c_edit_file : verbose:bool -> Guestfs.t -> string -> string
-> unit
+external c_edit_file : verbose:bool -> Guestfs.t -> int64 -> string
-> string -> unit
   = "virt_customize_edit_file_perl"
-let edit_file g file expr = c_edit_file (verbose ()) g file expr
+let edit_file g file expr +  (* Note we pass original 'g' even though
it is not used by the
+   * callee.  This is so that 'g' is kept as a root on the stack, and
+   * so cannot be garbage collected while we are in the c_edit_file
+   * function.
+   *)
+  c_edit_file (verbose ()) g (Guestfs.c_pointer g) file expr
-- 
2.7.4
Pino Toscano
2016-Apr-12  08:30 UTC
Re: [Libguestfs] [PATCH v2] customize/perl_edit-c.c: Don't use internal APIs.
On Tuesday 12 April 2016 08:41:37 Richard W.M. Jones wrote:> We can now use the Guestfs.c_pointer method to access the underlying > guestfs_h *. So no need to use internal APIs for this. > ---LGTM. Did it cause any particular issue? (Not that it is a problem, just to know whether it was doing anything problematic.) Thanks, -- Pino Toscano
Pino Toscano
2016-Apr-14  13:33 UTC
Re: [Libguestfs] [PATCH v2] customize/perl_edit-c.c: Don't use internal APIs.
On Tuesday 12 April 2016 08:41:37 Richard W.M. Jones wrote:> We can now use the Guestfs.c_pointer method to access the underlying > guestfs_h *. So no need to use internal APIs for this. > --- > customize/perl_edit-c.c | 18 ++++++------------ > customize/perl_edit.ml | 10 ++++++++-- > 2 files changed, 14 insertions(+), 14 deletions(-) > > diff --git a/customize/perl_edit-c.c b/customize/perl_edit-c.c > index 753d990..7191bef 100644 > --- a/customize/perl_edit-c.c > +++ b/customize/perl_edit-c.c > @@ -25,30 +25,24 @@ > #include <caml/alloc.h> > #include <caml/memory.h> > #include <caml/mlvalues.h> > +#include <caml/fail.h> > > #include "file-edit.h" > > -/** > - * We try to reuse the internals of the OCaml binding (for extracting > - * the guestfs handle, and raising errors); hopefully this should be safe, > - * as long as it's kept internal within the libguestfs sources. > - */ > -#include "../ocaml/guestfs-c.h" > - > #pragma GCC diagnostic ignored "-Wmissing-prototypes" > > value > -virt_customize_edit_file_perl (value verbosev, value gv, value filev, > - value exprv) > +virt_customize_edit_file_perl (value verbosev, value gv, value gpv, > + value filev, value exprv) > { > - CAMLparam4 (verbosev, gv, filev, exprv); > + CAMLparam5 (verbosev, gv, gpv, filev, exprv); > int r; > - guestfs_h *g = Guestfs_val (gv); > + guestfs_h *g = (guestfs_h *) Int64_val (gpv);This causes warnings on 32bit platforms though: perl_edit-c.c: In function 'virt_customize_edit_file_perl': perl_edit-c.c:40:18: error: cast to pointer from integer of different size [-Werror=int-to-pointer-cast] guestfs_h *g = (guestfs_h *) Int64_val (gpv); ^ What about the following patch? Subject: [PATCH] customize: cast value to intptr_t for pointer usage Cast a Int64 OCaml value to intptr_t before casting it to a pointer, so this avoids warnings about casting an int to pointer. Fixes commit 80a13b7216340b2bee437c39db2375b61804e133. --- customize/perl_edit-c.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/customize/perl_edit-c.c b/customize/perl_edit-c.c index 7191bef..9558d07 100644 --- a/customize/perl_edit-c.c +++ b/customize/perl_edit-c.c @@ -37,7 +37,7 @@ virt_customize_edit_file_perl (value verbosev, value gv, value gpv, { CAMLparam5 (verbosev, gv, gpv, filev, exprv); int r; - guestfs_h *g = (guestfs_h *) Int64_val (gpv); + guestfs_h *g = (guestfs_h *) (intptr_t) Int64_val (gpv); r = edit_file_perl (g, String_val (filev), String_val (exprv), NULL, Bool_val (verbosev)); Thanks, -- Pino Toscano
Apparently Analagous Threads
- Re: [PATCH v2] customize/perl_edit-c.c: Don't use internal APIs.
- [PATCH v2] customize/perl_edit-c.c: Don't use internal APIs.
- [PATCH] customize/perl_edit-c.c: Don't use internal APIs.
- [PATCH 00/13] code refactorings for tools
- [PATCH 0/5] RFC: switch augeas APIs to OCaml