From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1LV26O-0002d1-1w for qemu-devel@nongnu.org; Thu, 05 Feb 2009 06:09:00 -0500 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1LV26L-0002bf-6G for qemu-devel@nongnu.org; Thu, 05 Feb 2009 06:08:58 -0500 Received: from [199.232.76.173] (port=51260 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1LV26K-0002bT-VC for qemu-devel@nongnu.org; Thu, 05 Feb 2009 06:08:57 -0500 Received: from mx2.redhat.com ([66.187.237.31]:39288) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1LV26K-0000ee-1o for qemu-devel@nongnu.org; Thu, 05 Feb 2009 06:08:56 -0500 From: Avi Kivity Date: Thu, 5 Feb 2009 13:08:41 +0200 Message-Id: <1233832126-9046-2-git-send-email-avi@redhat.com> In-Reply-To: <1233832126-9046-1-git-send-email-avi@redhat.com> References: <1233832126-9046-1-git-send-email-avi@redhat.com> Subject: [Qemu-devel] [PATCH 1/6] Terminate emulation on memory allocation failure Reply-To: qemu-devel@nongnu.org List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Anthony Liguori , qemu-devel@nongnu.org Memory allocation failures are a very rare condition on virtual-memory hosts. They are also very difficult to handle correctly (especially in a hardware emulation context). Because of this, it is better to gracefully terminate emulation rather than executing untested or even unwritten recovery code paths. This patch changes the qemu memory allocation routines to terminate emulation if an allocation failure is encountered. Signed-off-by: Avi Kivity --- qemu-malloc.c | 16 ++++++++++------ 1 files changed, 10 insertions(+), 6 deletions(-) diff --git a/qemu-malloc.c b/qemu-malloc.c index dc74efe..1d00f26 100644 --- a/qemu-malloc.c +++ b/qemu-malloc.c @@ -22,6 +22,14 @@ * THE SOFTWARE. */ #include "qemu-common.h" +#include + +static void *oom_check(void *ptr) +{ + if (ptr == NULL) + exit(13); + return ptr; +} void *get_mmap_addr(unsigned long size) { @@ -35,20 +43,18 @@ void qemu_free(void *ptr) void *qemu_malloc(size_t size) { - return malloc(size); + return oom_check(malloc(size)); } void *qemu_realloc(void *ptr, size_t size) { - return realloc(ptr, size); + return oom_check(realloc(ptr, size)); } void *qemu_mallocz(size_t size) { void *ptr; ptr = qemu_malloc(size); - if (!ptr) - return NULL; memset(ptr, 0, size); return ptr; } @@ -58,8 +64,6 @@ char *qemu_strdup(const char *str) char *ptr; size_t len = strlen(str); ptr = qemu_malloc(len + 1); - if (!ptr) - return NULL; memcpy(ptr, str, len + 1); return ptr; } -- 1.6.1.1