Skip to contentSkip to conversation

: xl: start implementing "the policy" wrt memory allocations

RFC PATCH New Gianni Tedesco 1 participant 1 message Aug 6, 2010

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 : xl: start implementing "the policy" wrt memory allocations

It is libxl policy to return objects to callers which the caller must free. It therefore a matter of correctness for libxl to only free it's internal scratch allocations and never to free things which may be returned so as to avoid double-free error conditions.

This patch (probably won't apply and is dirty as hell for now) - begins to implement this. The approach taken is to use gcc's visibility attribute to hide libxl internal functions from callers. This includes the libxl garbage collection routines. These routines now take a new structure libxl_gc which contains the garbage collection variables and a back-pointer to the associated libxl context. Other changes include returning void from libxl_*free* and abort()'ing when it goes wrong.

Working on this patch exposed numerous violations of the policy:
- libxl_domid_to_name()
- libxl_get_version_info()
- libxl_list_vcpu()
- libxl_list_nics()
- libxl_device_net2_list()
- perhaps others

domid_to_name has numerous in and out of library callers so for that I introduced a libxl-internal _libxl_domid_to_name() which participates in libxl garbage collection - a little too subtle for my taste but perhaps something to be lived with.

The others would benefit from destructor functions such as Ian Campbell's patch introduced.

A side effect of the gcc visibility stuff is getting rid of about a thousand GOT/PLT entries which shaves 1-2KB off the binary size and ought to speed up dynamic linking.

What needs to be done from here is a thorough audit of the policy and a lot of time alone in a dark room doing bad things to xl with valgrind :)

I am thinking eventually of 4 logical changesets:
1. Fix flagrant policy violations
2. Get rid of leakiness with the new gc stuff (bulk of patch)
3. Sprinkle the _hidden macros around
4. Not decided how best to handle xs vs. NULL paths yet...

    probably xs_$(OP)f() w/ printf semantics built-in?

Comments, suggestions?

 tools/libxl/flexarray.c        |    3 
 tools/libxl/flexarray.h        |   12 
 tools/libxl/libxl.c            | 1006 ++++++++++++++++++++++++-----------------
 tools/libxl/libxl.h            |    5 
 tools/libxl/libxl_bootloader.c |   52 +-
 tools/libxl/libxl_device.c     |  102 +---
 tools/libxl/libxl_dom.c        |  198 +++++---
 tools/libxl/libxl_exec.c       |   20 
 tools/libxl/libxl_internal.c   |   79 +--
 tools/libxl/libxl_internal.h   |  134 +++--
 tools/libxl/libxl_pci.c        |  366 +++++++-------
 tools/libxl/libxl_utils.c      |  146 +++--
 tools/libxl/libxl_utils.h      |    2 
 tools/libxl/libxl_xshelp.c     |   29 -
 tools/libxl/xl_cmdimpl.c       |   12 
 16 files changed, 1258 insertions(+), 910 deletions(-)
tools/libxl/flexarray.c +1 -2
diff -r e49e70e31a99 tools/libxl/flexarray.c
@@ -13,8 +13,7 @@
  * GNU Lesser General Public License for more details.
  */
 
-#include <stdlib.h>
-#include "flexarray.h"
+#include "libxl_internal.h"
 
 flexarray_t *flexarray_make(int size, int autogrow)
 {
tools/libxl/flexarray.h +6 -6
diff -r e49e70e31a99 tools/libxl/flexarray.h
@@ -22,12 +22,12 @@ typedef struct flexarray {
     void **data; /* array of pointer */
 } flexarray_t;
 
-flexarray_t *flexarray_make(int size, int autogrow);
-void flexarray_free(flexarray_t *array);
-int flexarray_grow(flexarray_t *array, int extents);
-int flexarray_set(flexarray_t *array, unsigned int index, void *ptr);
-int flexarray_get(flexarray_t *array, int index, void **ptr);
+_hidden flexarray_t *flexarray_make(int size, int autogrow);
+_hidden void flexarray_free(flexarray_t *array);
+_hidden int flexarray_grow(flexarray_t *array, int extents);
+_hidden int flexarray_set(flexarray_t *array, unsigned int index, void *ptr);
+_hidden int flexarray_get(flexarray_t *array, int index, void **ptr);
 
-void **flexarray_contents(flexarray_t *array);
+_hidden void **flexarray_contents(flexarray_t *array);
 
 #endif
tools/libxl/libxl.c +596 -410

2,411 lines not on this page · raw message

tools/libxl/libxl.h +0 -5
diff -r e49e70e31a99 tools/libxl/libxl.h
@@ -57,10 +57,6 @@ typedef struct {
     xc_interface *xch;
     struct xs_handle *xsh;
 
-    /* mini-GC */
-    int alloc_maxsize;
-    void **alloc_ptrs;
-
     /* for callers who reap children willy-nilly; caller must only
      * set this after libxl_init and before any other call - or
      * may leave them untouched */
@@ -127,7 +123,6 @@ int libxl_run_bootloader(libxl_ctx *ctx,
                          libxl_device_disk *disk,
                          uint32_t domid);
 
-char *libxl_uuid2string(libxl_ctx *ctx, const libxl_uuid uuid);
   /* 0 means ERROR_ENOMEM, which we have logged */
 
 /* events handling */
tools/libxl/libxl_bootloader.c +33 -19

134 lines not on this page · raw message

tools/libxl/libxl_device.c +47 -55

251 lines not on this page · raw message

tools/libxl/libxl_dom.c +124 -74

160 lines not on this page · raw message

tools/libxl/libxl_exec.c +12 -8
diff -r e49e70e31a99 tools/libxl/libxl_exec.c

Lines of this file are not shown. View the raw message.

tools/libxl/libxl_internal.c +43 -36

79 lines not on this page · raw message

tools/libxl/libxl_internal.h +82 -52

134 lines not on this page · raw message

tools/libxl/libxl_pci.c +195 -171

366 lines not on this page · raw message

tools/libxl/libxl_utils.c +91 -55

146 lines not on this page · raw message

tools/libxl/libxl_utils.h +1 -1
diff -r e49e70e31a99 tools/libxl/libxl_utils.h

Lines of this file are not shown. View the raw message.

tools/libxl/libxl_xshelp.c +17 -12
diff -r e49e70e31a99 tools/libxl/libxl_xshelp.c

Lines of this file are not shown. View the raw message.

tools/libxl/xl_cmdimpl.c +9 -3
diff -r e49e70e31a99 tools/libxl/xl_cmdimpl.c

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

Part of the diff is not shown here. View the raw message.