diff mbox series

tools/ocaml/mmap: Drop the len parameter from Xenmmap.write

Message ID 20230324202525.3256586-1-andrew.cooper3@citrix.com (mailing list archive)
State New, archived
Headers show
Series tools/ocaml/mmap: Drop the len parameter from Xenmmap.write | expand

Commit Message

Andrew Cooper March 24, 2023, 8:25 p.m. UTC
Strings in Ocaml carry their own length.  Absolutely nothing good can come
from having caml_string_length(data) be different to len.

Use the appropriate accessor, String_val(), but retain the workaround for the
Ocaml -safe-string constness bug in the same way as we've done elsewhere in
Xen.

Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
---
CC: Christian Lindig <christian.lindig@citrix.com>
CC: David Scott <dave@recoil.org>
CC: Edwin Török <edwin.torok@cloud.com>
CC: Rob Hoes <Rob.Hoes@citrix.com>
---
 tools/ocaml/libs/mmap/xenmmap.ml      |  4 ++--
 tools/ocaml/libs/mmap/xenmmap.mli     |  2 +-
 tools/ocaml/libs/mmap/xenmmap_stubs.c | 11 +++++------
 3 files changed, 8 insertions(+), 9 deletions(-)

Comments

Christian Lindig March 29, 2023, 8:39 a.m. UTC | #1
> On 24 Mar 2023, at 20:25, Andrew Cooper <Andrew.Cooper3@citrix.com> wrote:
> 
> Strings in Ocaml carry their own length.  Absolutely nothing good can come
> from having caml_string_length(data) be different to len.
> 
> Use the appropriate accessor, String_val(), but retain the workaround for the
> Ocaml -safe-string constness bug in the same way as we've done elsewhere in
> Xen.
> 
> Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
> ---
> CC: Christian Lindig <christian.lindig@citrix.com>
> CC: David Scott <dave@recoil.org>
> CC: Edwin Török <edwin.torok@cloud.com>
> CC: Rob Hoes <Rob.Hoes@citrix.com>
> ---
> tools/ocaml/libs/mmap/xenmmap.ml      |  4 ++--
> tools/ocaml/libs/mmap/xenmmap.mli     |  2 +-
> tools/ocaml/libs/mmap/xenmmap_stubs.c | 11 +++++------
> 3 files changed, 8 insertions(+), 9 deletions(-)

Acked-by: Christian Lindig <christian.lindig@cloud.com>
diff mbox series

Patch

diff --git a/tools/ocaml/libs/mmap/xenmmap.ml b/tools/ocaml/libs/mmap/xenmmap.ml
index fd6735649f4c..746ca6e21c52 100644
--- a/tools/ocaml/libs/mmap/xenmmap.ml
+++ b/tools/ocaml/libs/mmap/xenmmap.ml
@@ -25,7 +25,7 @@  external mmap: Unix.file_descr -> mmap_prot_flag -> mmap_map_flag
 external unmap: mmap_interface -> unit = "stub_mmap_final"
 (* read: interface -> start -> length -> data *)
 external read: mmap_interface -> int -> int -> string = "stub_mmap_read"
-(* write: interface -> data -> start -> length -> unit *)
-external write: mmap_interface -> string -> int -> int -> unit = "stub_mmap_write"
+(* write: interface -> data -> start -> unit *)
+external write: mmap_interface -> string -> int -> unit = "stub_mmap_write"
 (* getpagesize: unit -> size of page *)
 external getpagesize: unit -> int = "stub_mmap_getpagesize"
diff --git a/tools/ocaml/libs/mmap/xenmmap.mli b/tools/ocaml/libs/mmap/xenmmap.mli
index d097b68a8fdf..5d6aa19ca6cb 100644
--- a/tools/ocaml/libs/mmap/xenmmap.mli
+++ b/tools/ocaml/libs/mmap/xenmmap.mli
@@ -22,7 +22,7 @@  external mmap : Unix.file_descr -> mmap_prot_flag -> mmap_map_flag -> int -> int
   -> mmap_interface = "stub_mmap_init"
 external unmap : mmap_interface -> unit = "stub_mmap_final"
 external read : mmap_interface -> int -> int -> string = "stub_mmap_read"
-external write : mmap_interface -> string -> int -> int -> unit
+external write : mmap_interface -> string -> int -> unit
   = "stub_mmap_write"
 
 external getpagesize : unit -> int = "stub_mmap_getpagesize"
diff --git a/tools/ocaml/libs/mmap/xenmmap_stubs.c b/tools/ocaml/libs/mmap/xenmmap_stubs.c
index c85b1fcce7d5..c15a565aaa52 100644
--- a/tools/ocaml/libs/mmap/xenmmap_stubs.c
+++ b/tools/ocaml/libs/mmap/xenmmap_stubs.c
@@ -99,27 +99,26 @@  CAMLprim value stub_mmap_read(value intf, value start, value len)
 		caml_invalid_argument("len invalid");
 
 	data = caml_alloc_string(c_len);
-	memcpy((char *) data, Intf_val(intf)->addr + c_start, c_len);
+	memcpy((char *)String_val(data), Intf_val(intf)->addr + c_start, c_len);
 
 	CAMLreturn(data);
 }
 
-CAMLprim value stub_mmap_write(value intf, value data,
-                               value start, value len)
+CAMLprim value stub_mmap_write(value intf, value data, value start)
 {
-	CAMLparam4(intf, data, start, len);
+	CAMLparam3(intf, data, start);
 	int c_start;
 	int c_len;
 
 	c_start = Int_val(start);
-	c_len = Int_val(len);
+	c_len = caml_string_length(data);
 
 	if (c_start > Intf_val(intf)->len)
 		caml_invalid_argument("start invalid");
 	if (c_start + c_len > Intf_val(intf)->len)
 		caml_invalid_argument("len invalid");
 
-	memcpy(Intf_val(intf)->addr + c_start, (char *) data, c_len);
+	memcpy(Intf_val(intf)->addr + c_start, String_val(data), c_len);
 
 	CAMLreturn(Val_unit);
 }