[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 2/7] tools/ocaml/libs: Allocate the correct amount of memory for Abstract_tag


  • To: Xen-devel <xen-devel@xxxxxxxxxxxxxxxxxxxx>
  • From: Andrew Cooper <andrew.cooper3@xxxxxxxxxx>
  • Date: Tue, 31 Jan 2023 21:29:08 +0000
  • Authentication-results: esa1.hc3370-68.iphmx.com; dkim=none (message not signed) header.i=none
  • Cc: Andrew Cooper <andrew.cooper3@xxxxxxxxxx>, Christian Lindig <christian.lindig@xxxxxxxxxx>, David Scott <dave@xxxxxxxxxx>, Edwin Török <edwin.torok@xxxxxxxxx>, Rob Hoes <Rob.Hoes@xxxxxxxxxx>
  • Delivery-date: Tue, 31 Jan 2023 21:29:41 +0000
  • Ironport-data: A9a23:dbqxyaKz/vJ8W1YuFE+R0JUlxSXFcZb7ZxGr2PjKsXjdYENShDMOz 2QcXGCPaPuNajDyKNkiPYmy8E1S6JTWm4MyQVBlqX01Q3x08seUXt7xwmUcnc+xBpaaEB84t ZV2hv3odp1coqr0/0/1WlTZhSAgk/rOHv+kUrWs1hlZHWdMUD0mhQ9oh9k3i4tphcnRKw6Ws Jb5rta31GWNglaYCUpJrfPcwP9TlK6q4mhA5wdnPasjUGL2zBH5MrpOfcldEFOgKmVkNrbSb /rOyri/4lTY838FYj9yuu+mGqGiaue60Tmm0hK6aYD76vRxjnVaPpIAHOgdcS9qZwChxLid/ jnvWauYEm/FNoWU8AgUvoIx/ytWZcWq85efSZSzXFD6I+QrvBIAzt03ZHzaM7H09c5eGUQWx 7s6NgkUZx2Po7nq6YCJW9BF05FLwMnDZOvzu1llxDDdS/0nXYrCU+PB4towMDUY354UW6yEP oxANGQpNU6bC/FMEg5/5JYWteGknHTgNRZfr0qYv/Ef6GnP1g1hlrPqNbI5f/TbGJkEzx/H9 woq+UziPB9CZYSR+AaI91ypgM70xDrZcYE7QejQGvlC3wTImz175ActfUu2p7y1h1CzX/pbK lcI4Ww+oK4q7kupQ9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CtQbdKRnGY46ML+K40H2MGDx2VipUCHaPbBCP2iIp4/84b0z6u3vcJsUzIEqkJ CEVF9Dr2Y9d0/nFMXLhfRwg2bwaXT4WS6oxtBV5pB/tLG5TL33MTebQFRrl8e7uf0QDsDSRv 72MpNLBP3oK3foBO9yrnrDcogXLWNbXNweu949VV7LqsXXKpfyvuiea/rXLKqFK0dWZoo+OA pyYNHeHrQw0qnwYA6GvPH4YQKSRnDC
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>

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@xxxxxxxxxx>
---
CC: Christian Lindig <christian.lindig@xxxxxxxxxx>
CC: David Scott <dave@xxxxxxxxxx>
CC: Edwin Török <edwin.torok@xxxxxxxxx>
CC: Rob Hoes <Rob.Hoes@xxxxxxxxxx>
---
 tools/ocaml/libs/mmap/Makefile        | 2 ++
 tools/ocaml/libs/mmap/xenmmap_stubs.c | 6 +++++-
 tools/ocaml/libs/xc/xenctrl_stubs.c   | 5 ++++-
 3 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/tools/ocaml/libs/mmap/Makefile b/tools/ocaml/libs/mmap/Makefile
index a62153713571..855b8b2c9877 100644
--- a/tools/ocaml/libs/mmap/Makefile
+++ b/tools/ocaml/libs/mmap/Makefile
@@ -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
diff --git a/tools/ocaml/libs/mmap/xenmmap_stubs.c 
b/tools/ocaml/libs/mmap/xenmmap_stubs.c
index e03951d781bb..d623ad390e40 100644
--- a/tools/ocaml/libs/mmap/xenmmap_stubs.c
+++ b/tools/ocaml/libs/mmap/xenmmap_stubs.c
@@ -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,
diff --git a/tools/ocaml/libs/xc/xenctrl_stubs.c 
b/tools/ocaml/libs/xc/xenctrl_stubs.c
index 728818445975..fd1f306f0202 100644
--- a/tools/ocaml/libs/xc/xenctrl_stubs.c
+++ b/tools/ocaml/libs/xc/xenctrl_stubs.c
@@ -1031,7 +1031,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);
-- 
2.11.0




 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.