qemu_malloc dynamic checking
RFC PATCH 1/1 qemu_malloc dynamic checking
Hi,
This patch adds simple checks for qemu_malloc/free/realloc. With the check enabled, i386-softmmu crashes. Sparc32, Sparc64, PPC32 and PPC64 system emulators are fine.
qemu-malloc.c +43 -0
@@ -24,6 +24,11 @@#include "qemu-common.h" #include <stdlib.h> +#define DEBUG_MALLOC + +/* Canary must not break malloc alignment */ +static const char canary[] ="QEMU_MALLOC1234" ; +static void *oom_check (void *ptr) {if (ptr ==NULL )
@@ -38,20 +43,58 @@void qemu_free (void *ptr) { +#ifdef DEBUG_MALLOC +fprintf (stderr,"qemu_free: 0x%p\n" , ptr); +if (ptr) { + ptr = (void *)((unsigned long )ptr -sizeof (canary)); +if (memcmp (ptr, canary,sizeof (canary)) !=0 ) { +exit (0 ); + } +free (ptr); + } +#else free (ptr); +#endif }void *qemu_malloc (size_t size) { +#ifdef DEBUG_MALLOC +void *ptr; + + ptr =oom_check (malloc (size +sizeof (canary))); +memcpy (ptr, canary,sizeof (canary)); + ptr = (void *)((unsigned long )ptr +sizeof (canary)); +fprintf (stderr,"qemu_malloc: 0x%p + 0x%lx (+0x%lx)\n" , ptr, size, +sizeof (canary)); +return ptr; +#else return oom_check (malloc (size)); +#endif }void *qemu_realloc (void *ptr,size_t size) { +#ifdef DEBUG_MALLOC +if (size) { +void *newptr; + + newptr =qemu_malloc (size); +if (ptr) { +memcpy (newptr, ptr, 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)
Quoted 5 lines · Blue Swirl
Hi,
This patch adds simple checks for qemu_malloc/free/realloc. With the check enabled, i386-softmmu crashes. Sparc32, Sparc64, PPC32 and PPC64 system emulators are fine.
kvm_has_msr_star plain free is used on qemu_malloczed region. Perhaps something similar is done in one of the devices that are only enabled on i386.
Signature · 1 line
mailto:[email protected]
Quoted 4 lines · malc
Quoted 2 lines · Blue Swirl
Hi,
Quoted 8 lines
Quoted 3 lines
This patch adds simple checks for qemu_malloc/free/realloc. With the check enabled, i386-softmmu crashes. Sparc32, Sparc64, PPC32 and PPC64 system emulators are fine.
kvm_has_msr_starplain free is used onqemu_malloczedregion. Perhaps
something similar is done in one of the devices that are only enabled on i386.
Actually the crash was caused by a bug in the checker.
Quoted 10 lines · Blue Swirl
Quoted 9 lines · malc
Quoted 5 lines · Blue Swirl
Hi,
This patch adds simple checks for qemu_malloc/free/realloc. With the check enabled, i386-softmmu crashes. Sparc32, Sparc64, PPC32 and PPC64 system emulators are fine.
Quoted 5 lines
Quoted 3 lines
kvm_has_msr_starplain free is used onqemu_malloczedregion. Perhaps
something similar is done in one of the devices that are only enabled on i386.Actually the crash was caused by a bug in the checker.
memcpy in realloc?
Signature · 1 line
mailto:[email protected]
Am Mittwoch, 15. April 2009 20:23 schrieb Blue Swirl:
Quoted 3 lines
This patch adds simple checks for qemu_malloc/free/realloc. With the check enabled, i386-softmmu crashes. Sparc32, Sparc64, PPC32 and PPC64 system emulators are fine.
Generally I like the idea of running this kind of tests occasionally. I'm wondering though if we really need additional code for it, or if using external tools like valgrind could do the same without additional code and with the extra bonus of actually telling you who the culprit was.
Quoted 17 lines
Index:
qemu/qemu-malloc.c
===================================================================qemu-malloc.c 2009-04-15 18:09:49.000000000 +0000 renamed from qemu-malloc.c 2009-04-15 17:16:44.000000000 +0000 +5
-0@@ -24,6 +24,11 @@ #include "qemu-common.h" #include <stdlib.h> +#define DEBUG_MALLOC + +/* Canary must not break malloc alignment */ +static const char canary[] = "QEMU_MALLOC1234"; + static void *oom_check(void *ptr) { if (ptr == NULL)
qemu-malloc.c 2009-04-15 18:09:49.000000000 +0000 line 51
Quoted 9 lines
qemu-malloc.c 2009-04-15 18:09:49.000000000 +0000 renamed from qemu-malloc.c 2009-04-15 17:16:44.000000000 +0000 +6
-0@@ -38,20 +43,58 @@ void qemu_free(void *ptr) { +#ifdef DEBUG_MALLOC + fprintf(stderr, "qemu_free: 0x%p\n", ptr); + if (ptr) { + ptr = (void *)((unsigned long)ptr - sizeof(canary)); + if (memcmp(ptr, canary, sizeof(canary)) != 0) { + exit(0);
I'm used to the convention that an exit code of 0 means success. abort() might be the right thing here.
Kevin
Quoted 11 lines · Kevin Wolf
Am Mittwoch, 15. April 2009 20:23 schrieb Blue Swirl:
Quoted 3 lines
This patch adds simple checks for qemu_malloc/free/realloc. With the check enabled, i386-softmmu crashes. Sparc32, Sparc64, PPC32 and PPC64 system emulators are fine.
Generally I like the idea of running this kind of tests occasionally. I'm
wondering though if we really need additional code for it, or if using external tools like valgrind could do the same without additional code and with the extra bonus of actually telling you who the culprit was.
The check examines whether the memory allocated with qemu_malloc and friends is freed with plain free (or malloc'ed memory freed with qemu_free). We are not checking if the memory is corrupted that Valgrind checks for. Maybe semantic patch stuff might be able to catch this type of errors.
Quoted 18 lines
Quoted 18 lines
Index:
qemu/qemu-malloc.c
===================================================================qemu-malloc.c 2009-04-15 18:09:49.000000000 +0000 renamed from qemu-malloc.c 2009-04-15 17:16:44.000000000 +0000 +5
-0@@ -24,6 +24,11 @@ #include "qemu-common.h" #include <stdlib.h> +#define DEBUG_MALLOC + +/* Canary must not break malloc alignment */ +static const char canary[] = "QEMU_MALLOC1234"; + static void *oom_check(void *ptr) { if (ptr == NULL)@@ -38,20 +43,58 @@
Quoted 11 lines
qemu-malloc.c 2009-04-15 18:09:49.000000000 +0000 line 51
Quoted 8 lines
qemu-malloc.c 2009-04-15 18:09:49.000000000 +0000 renamed from qemu-malloc.c 2009-04-15 17:16:44.000000000 +0000 +6
-0void qemu_free(void *ptr) { +#ifdef DEBUG_MALLOC + fprintf(stderr, "qemu_free: 0x%p\n", ptr); + if (ptr) { + ptr = (void *)((unsigned long)ptr - sizeof(canary)); + if (memcmp(ptr, canary, sizeof(canary)) != 0) { + exit(0);I'm used to the convention that an exit code of 0 means success.
abort()might be the right thing here.
Right.
Quoted 6 lines · Blue Swirl
Hi,
This patch adds simple checks for qemu_malloc/free/realloc. With the check enabled, i386-softmmu crashes. Sparc32, Sparc64, PPC32 and PPC64 system emulators are fine.
The check for qemu_malloc result in qemu_realloc is not needed.
Signature · 1 line
mailto:[email protected]