From patchwork Wed Dec 9 19:04:31 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anthony Liguori X-Patchwork-Id: 40757 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id A81F1B6F1B for ; Thu, 10 Dec 2009 06:05:43 +1100 (EST) Received: from localhost ([127.0.0.1]:60183 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NIRr2-0002el-JT for incoming@patchwork.ozlabs.org; Wed, 09 Dec 2009 14:05:40 -0500 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1NIRqW-0002bn-Fm for qemu-devel@nongnu.org; Wed, 09 Dec 2009 14:05:08 -0500 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1NIRqR-0002Vj-Gz for qemu-devel@nongnu.org; Wed, 09 Dec 2009 14:05:07 -0500 Received: from [199.232.76.173] (port=57532 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NIRqR-0002Vc-Co for qemu-devel@nongnu.org; Wed, 09 Dec 2009 14:05:03 -0500 Received: from e37.co.us.ibm.com ([32.97.110.158]:34857) by monty-python.gnu.org with esmtps (TLS-1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1NIRqR-0000Ch-2d for qemu-devel@nongnu.org; Wed, 09 Dec 2009 14:05:03 -0500 Received: from d03relay04.boulder.ibm.com (d03relay04.boulder.ibm.com [9.17.195.106]) by e37.co.us.ibm.com (8.14.3/8.13.1) with ESMTP id nB9J3X4m029656 for ; Wed, 9 Dec 2009 12:03:33 -0700 Received: from d03av03.boulder.ibm.com (d03av03.boulder.ibm.com [9.17.195.169]) by d03relay04.boulder.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id nB9J4afZ164284 for ; Wed, 9 Dec 2009 12:04:38 -0700 Received: from d03av03.boulder.ibm.com (loopback [127.0.0.1]) by d03av03.boulder.ibm.com (8.14.3/8.13.1/NCO v10.0 AVout) with ESMTP id nB9J4WHQ028506 for ; Wed, 9 Dec 2009 12:04:32 -0700 Received: from localhost.localdomain (sig-9-65-55-188.mts.ibm.com [9.65.55.188]) by d03av03.boulder.ibm.com (8.14.3/8.13.1/NCO v10.0 AVin) with ESMTP id nB9J4WrM028454; Wed, 9 Dec 2009 12:04:32 -0700 From: Anthony Liguori To: qemu-devel@nongnu.org Date: Wed, 9 Dec 2009 13:04:31 -0600 Message-Id: <1260385471-8561-1-git-send-email-aliguori@us.ibm.com> X-Mailer: git-send-email 1.6.0.6 X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6, seldom 2.4 (older, 4) Cc: Anthony Liguori , Avi Kivity , Mark Armbruster Subject: [Qemu-devel] [PATCH] Do not abort on qemu_malloc(0) in production builds X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org qemu_malloc() does not allow size=0 to be passed in and aborts on this behavior. Unfortunately, there is good reason to believe that within qemu, there are a number of, so far, undetected places that assume size=0 can be safely passed. Since we do not want to abort unnecessarily in production builds, return qemu_malloc(1) whenever the version file indicates that this is a production build. Also introduce --enable-zero-malloc/--disable-zero-malloc to make this behavior overridable. Signed-off-by: Anthony Liguori --- configure | 24 +++++++++++++++++++++++- qemu-malloc.c | 17 +++++++++++++---- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/configure b/configure index a29839e..ede820e 100755 --- a/configure +++ b/configure @@ -256,6 +256,7 @@ blobs="yes" pkgversion="" check_utests="no" user_pie="no" +zero_malloc="" # OS specific if check_define __linux__ ; then @@ -600,6 +601,10 @@ for opt do ;; --enable-docs) docs="yes" ;; + --enable-zero-malloc) zero_malloc="yes" + ;; + --disable-zero-malloc) zero_malloc="no" + ;; *) echo "ERROR: unknown option $opt"; show_help="yes" ;; esac @@ -752,6 +757,8 @@ echo " --enable-linux-aio enable Linux AIO support" echo " --enable-io-thread enable IO thread" echo " --disable-blobs disable installing provided firmware blobs" echo " --kerneldir=PATH look for kernel includes in PATH" +echo " --enable-zero-malloc allow allocations of zero size" +echo " --disable-zero-malloc abort on allocations of zero size" echo "" echo "NOTE: The object files are built at the place where configure is launched" exit 1 @@ -1792,8 +1799,9 @@ fi # Consult white-list to determine whether to enable werror # by default. Only enable by default for git builds +z_version=`cut -f3 -d. $source_path/VERSION` + if test -z "$werror" ; then - z_version=`cut -f3 -d. $source_path/VERSION` if test "$z_version" = "50" -a \ "$linux" = "yes" ; then werror="yes" @@ -1802,6 +1810,16 @@ if test -z "$werror" ; then fi fi +# Disable zero malloc errors for official releases unless explicitly told to +# enable/disable +if test -z "$zero_malloc" ; then + if test "$z_version" = "50" ; then + zero_malloc="no" + else + zero_malloc="yes" + fi +fi + if test "$werror" = "yes" ; then QEMU_CFLAGS="-Werror $QEMU_CFLAGS" fi @@ -2109,6 +2127,10 @@ fi echo "CONFIG_UNAME_RELEASE=\"$uname_release\"" >> $config_host_mak +if test "$zero_malloc" = "yes" ; then + echo "CONFIG_ZERO_MALLOC=y" >> $config_host_mak +fi + # USB host support case "$usb" in linux) diff --git a/qemu-malloc.c b/qemu-malloc.c index 295d185..e82af26 100644 --- a/qemu-malloc.c +++ b/qemu-malloc.c @@ -42,21 +42,30 @@ void qemu_free(void *ptr) free(ptr); } +static int allow_zero_malloc(void) +{ +#if defined(CONFIG_ZERO_MALLOC) + return 1; +#else + return 0; +#endif +} + void *qemu_malloc(size_t size) { - if (!size) { + if (!size && !allow_zero_malloc()) { abort(); } - return oom_check(malloc(size)); + return oom_check(malloc(size ? size : 1)); } void *qemu_realloc(void *ptr, size_t size) { if (size) { return oom_check(realloc(ptr, size)); - } else { + } else if (allow_zero_malloc()) { if (ptr) { - return realloc(ptr, size); + return realloc(ptr, size ? size : 1); } } abort();