From patchwork Thu Feb 11 09:23:54 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Campbell X-Patchwork-Id: 8277411 Return-Path: X-Original-To: patchwork-xen-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id DB9FABEEE5 for ; Thu, 11 Feb 2016 09:26:38 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 0D434202FE for ; Thu, 11 Feb 2016 09:26:38 +0000 (UTC) Received: from lists.xen.org (lists.xenproject.org [50.57.142.19]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 2B406202EC for ; Thu, 11 Feb 2016 09:26:37 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xen.org) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1aTnTl-0007WC-Qr; Thu, 11 Feb 2016 09:24:01 +0000 Received: from mail6.bemta14.messagelabs.com ([193.109.254.103]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1aTnTj-0007W7-RQ for xen-devel@lists.xen.org; Thu, 11 Feb 2016 09:23:59 +0000 Received: from [193.109.254.147] by server-2.bemta-14.messagelabs.com id AD/8E-12889-F235CB65; Thu, 11 Feb 2016 09:23:59 +0000 X-Env-Sender: prvs=842d64d66=Ian.Campbell@citrix.com X-Msg-Ref: server-15.tower-27.messagelabs.com!1455182637!23029130!1 X-Originating-IP: [66.165.176.89] X-SpamReason: No, hits=0.0 required=7.0 tests=sa_preprocessor: VHJ1c3RlZCBJUDogNjYuMTY1LjE3Ni44OSA9PiAyMDMwMDc=\n, received_headers: No Received headers X-StarScan-Received: X-StarScan-Version: 7.35.1; banners=-,-,- X-VirusChecked: Checked Received: (qmail 43814 invoked from network); 11 Feb 2016 09:23:58 -0000 Received: from smtp.citrix.com (HELO SMTP.CITRIX.COM) (66.165.176.89) by server-15.tower-27.messagelabs.com with RC4-SHA encrypted SMTP; 11 Feb 2016 09:23:58 -0000 X-IronPort-AV: E=Sophos;i="5.22,430,1449532800"; d="scan'208";a="331041120" From: Ian Campbell To: , , Date: Thu, 11 Feb 2016 09:23:54 +0000 Message-ID: <1455182634-19386-1-git-send-email-ian.campbell@citrix.com> X-Mailer: git-send-email 2.1.4 MIME-Version: 1.0 X-DLP: MIA1 Cc: Ian Campbell Subject: [Xen-devel] [PATCH v2] tools: libxl: make it illegal to pass libxl__realloc(gc) a non-gc ptr X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP That is, if gc is not NOGC and ptr is not NULL then ptr must be associated gc. Currently in this case the new_ptr would not be registered with any gc, which Coverity rightly points out (in various different places) would be a memory leak. It would also be possible to fix this by adding a libxl__ptr_add() at the same point, however semantically it seems like a programming error to gc-realloc a pointer which is not associated with the gc in question, so treat it as such. Compile tested only, this change could expose latent bugs. Signed-off-by: Ian Campbell Acked-by: Wei Liu --- v2: Check we actually didn't find the ptr in the gc Correct log message and shorten since LOG will inject the function name. --- tools/libxl/libxl_internal.c | 4 ++++ tools/libxl/libxl_internal.h | 4 +++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/tools/libxl/libxl_internal.c b/tools/libxl/libxl_internal.c index 328046b..fc81130 100644 --- a/tools/libxl/libxl_internal.c +++ b/tools/libxl/libxl_internal.c @@ -122,6 +122,10 @@ void *libxl__realloc(libxl__gc *gc, void *ptr, size_t new_size) break; } } + if (i == gc->alloc_maxsize) { + LOG(CRITICAL, "pointer is not tracked by the given gc"); + abort(); + } } return new_ptr; diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h index fc1b558..650a958 100644 --- a/tools/libxl/libxl_internal.h +++ b/tools/libxl/libxl_internal.h @@ -617,7 +617,9 @@ _hidden void *libxl__zalloc(libxl__gc *gc_opt, size_t size) NN1; _hidden void *libxl__calloc(libxl__gc *gc_opt, size_t nmemb, size_t size) NN1; /* change the size of the memory block pointed to by @ptr to @new_size bytes. * unlike other allocation functions here any additional space between the - * oldsize and @new_size is not initialised (similar to a gc'd realloc(3)). */ + * oldsize and @new_size is not initialised (similar to a gc'd realloc(3)). + * if @ptr is non-NULL and @gc_opt is not nogc_gc then @ptr must have been + * registered with @gc_opt previously. */ _hidden void *libxl__realloc(libxl__gc *gc_opt, void *ptr, size_t new_size) NN1; /* print @fmt into an allocated string large enoughto contain the result. * (similar to gc'd asprintf(3)). */