Support running QEMU on Valgrind
PATCH v2 1/1 Support running QEMU on Valgrind
Valgrind is a tool which can automatically detect many kinds of bugs.
Running QEMU on Valgrind with x86_64 hosts was not possible because Valgrind aborts when memalign is called with an alignment larger than 1 MiB. QEMU normally uses 2 MiB on Linux x86_64.
Now the alignment is reduced to the page size when QEMU is running on Valgrind.
v2:
Instead of using the macro RUNNING_ON_VALGRIND from valgrind.h, the patch now uses a hack from libvirt which tests for the pre-loaded vgpreload_*.so shared libraries. This avoids the need for valgrind.h.
--- oslib-posix.c | 22 +++++++++++++++++++--- 1 files changed, 19 insertions(+), 3 deletions(-)
oslib-posix.c +19 -3
@@ -36,8 +36,11 @@ extern int daemon(int, int);#endif #if defined(__linux__) && defined(__x86_64__) -/* Use 2MB alignment so transparent hugepages can be used by KVM */ +/* Use 2 MiB alignment so transparent hugepages can be used by KVM. +Valgrind does not support alignments larger than 1 MiB, +therefore we need special code which handles running on Valgrind. */ # define QEMU_VMALLOC_ALIGN (512 * 4096) +# define CONFIG_VALGRIND #else # define QEMU_VMALLOC_ALIGN getpagesize()#endif
@@ -47,7 +50,11 @@ extern int daemon(int, int);#include "trace.h" #include "qemu_socket.h" - +#if defined(CONFIG_VALGRIND ) +static int running_on_valgrind =-1 ; +#else +# define running_on_valgrind 0 +#endif int qemu_daemon (int nochdir,int noclose) {
@@ -89,7 +96,16 @@ void *qemu_vmalloc(size_t size)void *ptr;size_t align =QEMU_VMALLOC_ALIGN ; -if (size < align) { +#if defined(CONFIG_VALGRIND ) +if (running_on_valgrind <0 ) { +/* First call, test whether we are running on Valgrind. +This is a substitute for RUNNING_ON_VALGRIND from valgrind.h. */ +const char *ld =getenv ("LD_PRELOAD" ); + running_on_valgrind = (ld !=NULL &&strstr (ld,"vgpreload" )); + } +#endif + +if (size < align || running_on_valgrind) { align =getpagesize (); } ptr =qemu_memalign (align, size);
Signature · 1 line
1.7.2.5
Anthony Liguori
AppliedQuoted 9 lines · Stefan Weil
Valgrind is a tool which can automatically detect many kinds of bugs.
Running QEMU on Valgrind with
x86_64hosts was not possible because Valgrind aborts when memalign is called with an alignment larger than 1 MiB. QEMU normally uses 2 MiB on Linuxx86_64.Now the alignment is reduced to the page size when QEMU is running on Valgrind.
Quoted 6 lines
v2:
Instead of using the macroRUNNING_ON_VALGRINDfromvalgrind.h, the patch now uses a hack from libvirt which tests for the pre-loaded vgpreload_*.so shared libraries. This avoids the need forvalgrind.h.Signed-off-by Stefan Weil<[email protected]>
Applied. Thanks.
Regards,
Anthony Liguori
Quoted 52 lines
--- oslib-posix.c | 22 +++++++++++++++++++--- 1 files changed, 19 insertions(+), 3 deletions(-)oslib-posix.c +19
-3@@ -36,8 +36,11 @@ extern int daemon(int, int);#endif #if defined(__linux__)&& defined(__x86_64__) -/* Use 2MB alignment so transparent hugepages can be used by KVM */ +/* Use 2 MiB alignment so transparent hugepages can be used by KVM. +Valgrind does not support alignments larger than 1 MiB, +therefore we need special code which handles running on Valgrind. */ # define QEMU_VMALLOC_ALIGN (512 * 4096) +# define CONFIG_VALGRIND #else # define QEMU_VMALLOC_ALIGN getpagesize()#endif @@ -47,7 +50,11 @@ extern int daemon(int, int);#include "trace.h" #include "qemu_socket.h" - +#if defined(CONFIG_VALGRIND ) +static int running_on_valgrind =-1 ; +#else +# define running_on_valgrind 0 +#endif int qemu_daemon (int nochdir,int noclose) {@@ -89,7 +96,16 @@ void *qemu_vmalloc(size_t size)void *ptr;size_t align =QEMU_VMALLOC_ALIGN ; -if (size< align) { +#if defined(CONFIG_VALGRIND ) +if (running_on_valgrind<0 ) { +/* First call, test whether we are running on Valgrind. +This is a substitute for RUNNING_ON_VALGRIND from valgrind.h. */ +const char *ld =getenv ("LD_PRELOAD" ); + running_on_valgrind = (ld !=NULL &&strstr (ld,"vgpreload" )); + } +#endif + +if (size< align || running_on_valgrind) { align =getpagesize (); } ptr =qemu_memalign (align, size);