Skip to contentSkip to conversation

tools: libxl: make it illegal to pass libxl__realloc(gc) a non-gc ptr

PATCH Superseded Ian Campbell 2 participants 3 messages Feb 10, 2016

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tools: libxl: make it illegal to pass libxl__realloc(gc) a non-gc ptr

That is, if gc is not NOGC and ptr is not NULL then ptr must be associated gc.

Currently in this case the new_ptr would not be registered with any gc, which Coverity rightly points out would be a memory leak.

It would also be possible to fix this by adding a libxl__ptr_add() at the same point, however semantically it seems like a programming error to gc-realloc a pointer which is not associated with the gc in question, so treat it as such.

Compile tested only, this change could expose latent bugs.

---
 tools/libxl/libxl_internal.c | 7 +++++++
 tools/libxl/libxl_internal.h | 4 +++-
 2 files changed, 10 insertions(+), 1 deletion(-)
tools/libxl/libxl_internal.c +7 -0
index 328046b..179d618 100644
@@ -122,6 +122,13 @@ void *libxl__realloc(libxl__gc *gc, void *ptr, size_t new_size)
                 break;
             }
         }
+        /*
+         * If we get here then ptr was not previously registered with
+         * this gc.
+         */
+        LOG(CRITICAL,
+            "libxl__realloc on a pointer which tracked by the given gc");
+        abort();
     }
 
     return new_ptr;
tools/libxl/libxl_internal.h +3 -1
index fc1b558..650a958 100644
@@ -617,7 +617,9 @@ _hidden void *libxl__zalloc(libxl__gc *gc_opt, size_t size) NN1;
 _hidden void *libxl__calloc(libxl__gc *gc_opt, size_t nmemb, size_t size) NN1;
 /* change the size of the memory block pointed to by @ptr to @new_size bytes.
  * unlike other allocation functions here any additional space between the
- * oldsize and @new_size is not initialised (similar to a gc'd realloc(3)). */
+ * oldsize and @new_size is not initialised (similar to a gc'd realloc(3)).
+ * if @ptr is non-NULL and @gc_opt is not nogc_gc then @ptr must have been
+ * registered with @gc_opt previously. */
 _hidden void *libxl__realloc(libxl__gc *gc_opt, void *ptr, size_t new_size) NN1;
 /* print @fmt into an allocated string large enoughto contain the result.
  * (similar to gc'd asprintf(3)). */
Signature · 1 line
2.1.4
#1 lore raw
Quoted 24 lines · Ian Campbell

That is, if gc is not NOGC and ptr is not NULL then ptr must be associated gc.

Currently in this case the new_ptr would not be registered with any gc, which Coverity rightly points out would be a memory leak.

It would also be possible to fix this by adding a libxl__ptr_add() at the same point, however semantically it seems like a programming error to gc-realloc a pointer which is not associated with the gc in question, so treat it as such.

Compile tested only, this change could expose latent bugs.

---
 tools/libxl/libxl_internal.c | 7 +++++++
 tools/libxl/libxl_internal.h | 4 +++-
 2 files changed, 10 insertions(+), 1 deletion(-)
tools/libxl/libxl_internal.c +0 -0
index 328046b..179d618 100644

tools/libxl/libxl_internal.c void *libxl__realloc(libxl__gc *gc, void *ptr, size_t new_size) · line 130

Quoted 9 lines
tools/libxl/libxl_internal.c +6 -0
@@ -122,6 +122,13 @@ void *libxl__realloc(libxl__gc *gc, void *ptr, size_t new_size)
                 break;
             }
         }
+        /*
+         * If we get here then ptr was not previously registered with
+         * this gc.
+         */
+        LOG(CRITICAL,
+            "libxl__realloc on a pointer which tracked by the given gc");

"is not" ?

~Andrew

#2 lore raw
Quoted 34 lines · Andrew Cooper
Quoted 7 lines · Ian Campbell

That is, if gc is not NOGC and ptr is not NULL then ptr must be associated gc.

Currently in this case the new_ptr would not be registered with any gc, which Coverity rightly points out would be a memory leak.

It would also be possible to fix this by adding a libxl__ptr_add()

at

Quoted 1 line

the same point, however semantically it seems like a programming

error

Quoted 5 lines

to gc-realloc a pointer which is not associated with the gc in question, so treat it as such.

Compile tested only, this change could expose latent bugs.

Review

Quoted 7 lines
---
 tools/libxl/libxl_internal.c | 7 +++++++
 tools/libxl/libxl_internal.h | 4 +++-
 2 files changed, 10 insertions(+), 1 deletion(-)
(quoted diff) +0 -0

b/tools/libxl/libxl_internal.c

tools/libxl/libxl_internal.c void *libxl__realloc(libxl__gc *gc, void *ptr,

Quoted 4 lines

index 328046b..179d618 100644

tools/libxl/libxl_internal.c +0 -0
@@ -122,6 +122,13 @@ void *libxl__realloc(libxl__gc *gc, void *ptr,

size_t new_size)

tools/libxl/libxl_internal.c void *libxl__realloc(libxl__gc *gc, void *ptr, size_t new_size)

Quoted 5 lines
tools/libxl/libxl_internal.c +2 -0
                 break;
             }
         }
+        /*
+         * If we get here then ptr was not previously registered
Quoted 8 lines

with

tools/libxl/libxl_internal.c void *libxl__realloc(libxl__gc *gc, void *ptr, size_t new_size)

Quoted 4 lines
tools/libxl/libxl_internal.c +4 -0
+         * this gc.
+         */
+        LOG(CRITICAL,
+            "libxl__realloc on a pointer which tracked by the

given gc");

"is not" ?

Yes.

More importantly I'd somehow convinced myself that the break in the for loop went one scope further than it did. IOW this code _really_ needs an i == gc->alloc_maxsize check before it makes a fuss!

At which point I may just turn it into an assert.

Ian.