tools/ocaml/libs: Allocate the correct amount of memory for Abstract_tag
authorAndrew Cooper <andrew.cooper3@citrix.com>
Tue, 31 Jan 2023 10:59:42 +0000 (10:59 +0000)
committerAndrew Cooper <andrew.cooper3@citrix.com>
Thu, 9 Feb 2023 15:55:25 +0000 (15:55 +0000)
caml_alloc() takes units of Wsize (word size), not bytes.  As a consequence,
we're allocating 4 or 8 times too much memory.

Ocaml has a helper, Wsize_bsize(), but it truncates cases which aren't an
exact multiple.  Use a BUILD_BUG_ON() to cover the potential for truncation,
as there's no rounding-up form of the helper.

Fixes: 8b7ce06a2d34 ("ocaml: Add XC bindings.")
Fixes: d3e649277a13 ("ocaml: add mmap bindings implementation.")
Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
Acked-by: Christian Lindig <christian.lindig@citrix.com>
(cherry picked from commit 36eb2de31b6ecb8787698fb1a701bd708c8971b2)

tools/ocaml/libs/mmap/Makefile
tools/ocaml/libs/mmap/xenmmap_stubs.c
tools/ocaml/libs/xc/xenctrl_stubs.c

index a6215371357140e2b3db2cf922a7350d58ce8995..855b8b2c98775657b55258041c7a5723fa4dea8c 100644 (file)
@@ -2,6 +2,8 @@ OCAML_TOPLEVEL=$(CURDIR)/../..
 XEN_ROOT=$(OCAML_TOPLEVEL)/../..
 include $(OCAML_TOPLEVEL)/common.make
 
+CFLAGS += $(CFLAGS_xeninclude)
+
 OBJS = xenmmap
 INTF = $(foreach obj, $(OBJS),$(obj).cmi)
 LIBS = xenmmap.cma xenmmap.cmxa
index e03951d781bbe28d8584053bb0c74ef59a8014ff..d623ad390e40ba4f5bc6741ca9964ac1d07b6a75 100644 (file)
@@ -21,6 +21,8 @@
 #include <errno.h>
 #include "mmap_stubs.h"
 
+#include <xen-tools/libs.h>
+
 #include <caml/mlvalues.h>
 #include <caml/memory.h>
 #include <caml/alloc.h>
@@ -59,7 +61,9 @@ CAMLprim value stub_mmap_init(value fd, value pflag, value mflag,
        default: caml_invalid_argument("maptype");
        }
 
-       result = caml_alloc(sizeof(struct mmap_interface), Abstract_tag);
+       BUILD_BUG_ON((sizeof(struct mmap_interface) % sizeof(value)) != 0);
+       result = caml_alloc(Wsize_bsize(sizeof(struct mmap_interface)),
+                           Abstract_tag);
 
        if (mmap_interface_init(Intf_val(result), Int_val(fd),
                                c_pflag, c_mflag,
index 6eb0ea69daced1a1bb27986eb531bbe2f77408e0..e25367531b8524cf10088c7eb00cb0d5fc68a81a 100644 (file)
@@ -956,7 +956,10 @@ CAMLprim value stub_map_foreign_range(value xch, value dom,
        uint32_t c_dom;
        unsigned long c_mfn;
 
-       result = caml_alloc(sizeof(struct mmap_interface), Abstract_tag);
+       BUILD_BUG_ON((sizeof(struct mmap_interface) % sizeof(value)) != 0);
+       result = caml_alloc(Wsize_bsize(sizeof(struct mmap_interface)),
+                           Abstract_tag);
+
        intf = (struct mmap_interface *) result;
 
        intf->len = Int_val(size);