Skip to contentSkip to conversation

qemu_malloc dynamic checking v2

RFC PATCH In review Blue Swirl 1 participant 2 messages Apr 16, 2009

lore
Share & tools
lore

Blue Swirl

author
#0 lore raw

RFC PATCH 1/1 qemu_malloc dynamic checking v2

Hi,

This version adds stricter checking for also normal malloc/free/memalign functions.

Actually the crashes with the previous version were caused by bugs in the checker itself instead of function mismatch. With this version, also i386 works.

I'm not so sure this approach is effective, I found more mismatches with simple grep. Committing it would not hurt though.

author
#1 lore raw
Quoted 11 lines · Blue Swirl

Hi,

This version adds stricter checking for also normal malloc/free/memalign functions.

Actually the crashes with the previous version were caused by bugs in the checker itself instead of function mismatch. With this version, also i386 works.

I'm not so sure this approach is effective, I found more mismatches with simple grep. Committing it would not hurt though.

This time with the patch.

qemu-malloc.c +174 -0
Index: qemu/qemu-malloc.c
===================================================================
@@ -23,6 +23,21 @@
  */
 #include "qemu-common.h"
 #include <stdlib.h>
+#include <malloc.h>
+
+/*
+ * Check that qemu_malloc'ed memory is freed with qemu_free instead of
+ * plain free etc.
+ */
+#define DEBUG_MALLOC
+
+/* Canary must not break malloc alignment */
+struct canary {
+    unsigned long size;
+    unsigned long value;
+};
+#define QCANARY 0x12345678
+#define CCANARY 0x56781234
 
 static void *oom_check(void *ptr)
 {
@@ -38,20 +53,73 @@
 
 void qemu_free(void *ptr)
 {
+#ifdef DEBUG_MALLOC
+    fprintf(stderr, "qemu_free: 0x%p\n", ptr);
+    if (ptr) {
+        struct canary *cptr;
+
+        cptr = ptr;
+        cptr--;
+        if (cptr->value != QCANARY) {
+            abort();
+        }
+        cptr->value = 0;
+        free(cptr);
+    }
+#else
     free(ptr);
+#endif
 }
 
 void *qemu_malloc(size_t size)
 {
+#ifdef DEBUG_MALLOC
+    void *ptr;
+    struct canary *cptr;
+
+    cptr = oom_check(malloc(size + sizeof(struct canary)));
+    cptr->value = QCANARY;
+    cptr->size = size;
+    cptr++;
+    ptr = cptr;
+    fprintf(stderr, "qemu_malloc: 0x%p + 0x%lx (+0x%lx)\n", ptr, size,
+            sizeof(struct canary));
+    return ptr;
+#else
     return oom_check(malloc(size));
+#endif
 }
 
 void *qemu_realloc(void *ptr, size_t size)
 {
+#ifdef DEBUG_MALLOC
+    fprintf(stderr, "qemu_realloc: 0x%p + 0x%lx\n", ptr, size);
+    if (size) {
+        void *newptr;
+
+        newptr = qemu_malloc(size);
+        if (ptr) {
+            struct canary *cptr;
+
+            cptr = ptr;
+            cptr--;
+            if (cptr->value != QCANARY) {
+                abort();
+            }
+            memcpy(newptr, ptr, MIN(cptr->size, size));
+            qemu_free(ptr);
+        }
+        return newptr;
+    } else {
+        qemu_free(ptr);
+        return NULL;
+    }
+#else
     if (size)
         return oom_check(realloc(ptr, size));
     else
         return realloc(ptr, size);
+#endif
 }
 
 void *qemu_mallocz(size_t size)
@@ -84,3 +152,109 @@
 
     return memcpy(new, str, size);
 }
+
+#ifdef DEBUG_MALLOC
+static void *(*old_malloc_hook)(size_t size, const void *caller);
+static void (*old_free_hook)(void *ptr, const void *caller);
+static void *(*old_realloc_hook)(void *ptr, size_t size, const void *caller);
+static void *(*old_memalign_hook)(size_t alignment, size_t size, const void *caller);
+
+static void *debug_malloc(size_t size, const void *caller)
+{
+    void *ptr;
+    struct canary *cptr;
+
+    __malloc_hook = old_malloc_hook;
+    cptr = oom_check(malloc(size + sizeof(struct canary)));
+    __malloc_hook = debug_malloc;
+    cptr->value = CCANARY;
+    cptr->size = size;
+    cptr++;
+    ptr = cptr;
+    fprintf(stderr, "malloc: 0x%p + 0x%lx\n", ptr, size);
+    return ptr;
+}
+
+static void debug_free(void *ptr, const void *caller)
+{
+    fprintf(stderr, "free: 0x%p\n", ptr);
+    if (ptr) {
+        struct canary *cptr;
+
+        cptr = ptr;
+        cptr--;
+        if (cptr->value != CCANARY) {
+            abort();
+        }
+        cptr->value = 0;
+        __free_hook = old_free_hook;
+        free(cptr);
+        __free_hook = debug_free;
+    }
+}
+
+static void *debug_realloc(void *ptr, size_t size, const void *caller)
+{
+    fprintf(stderr, "realloc: 0x%p + 0x%lx\n", ptr, size);
+    if (size) {
+        void *newptr;
+
+        newptr = debug_malloc(size, caller);
+        if (ptr) {
+            struct canary *cptr;
+
+            cptr = ptr;
+            cptr--;
+            if (cptr->value != CCANARY) {
+                abort();
+            }
+            memcpy(newptr, ptr, MIN(cptr->size, size));
+            debug_free(ptr, caller);
+        }
+        return newptr;
+    } else {
+        debug_free(ptr, caller);
+        return NULL;
+    }
+}
+
+static void *debug_memalign(size_t alignment, size_t size,
+                            const void *caller)
+{
+    void *ptr;
+    struct canary *cptr;
+    size_t slack;
+
+    if (alignment > sizeof(struct canary)) {
+        slack = alignment - sizeof(struct canary);
+    } else {
+        slack = 0;
+        alignment = sizeof(struct canary);
+    }
+    __memalign_hook = old_memalign_hook;
+    cptr = oom_check(memalign(alignment, sizeof(struct canary) + slack + size));
+    __memalign_hook = debug_memalign;
+    cptr->value = CCANARY;
+    cptr->size = size;
+    cptr++;
+    ptr = (void *)((unsigned long)cptr + slack);
+    fprintf(stderr, "memalign: 0x%p + 0x%lx (+0x%lx) %% 0x%lx\n", ptr, size,
+            sizeof(struct canary) + slack, alignment);
+    return ptr;
+}
+
+static void qemu_malloc_init(void)
+{
+    old_malloc_hook = __malloc_hook;
+    __malloc_hook = debug_malloc;
+    old_realloc_hook = __realloc_hook;
+    __realloc_hook = debug_realloc;
+    old_free_hook = __free_hook;
+    __free_hook = debug_free;
+    old_memalign_hook = __memalign_hook;
+    __memalign_hook = debug_memalign;
+}
+
+void (*__malloc_initialize_hook) (void) = qemu_malloc_init;
+
+#endif