From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-Status: No, score=-3.5 required=3.0 tests=AWL,BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_PASS, SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id 186061F5AE for ; Tue, 7 Jul 2020 13:52:27 +0000 (UTC) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 04AC5386188A; Tue, 7 Jul 2020 13:52:26 +0000 (GMT) Received: from mx2.suse.de (mx2.suse.de [195.135.220.15]) by sourceware.org (Postfix) with ESMTPS id 794173851C34 for ; Tue, 7 Jul 2020 13:52:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 794173851C34 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=suse.cz Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=mliska@suse.cz X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id E7AC6AC23; Tue, 7 Jul 2020 13:52:21 +0000 (UTC) Subject: Re: [PATCH] Use size_t for mallinfo fields. To: Florian Weimer References: <87tuyja59i.fsf@igel.home> <87v9izxwo7.fsf@oldenburg2.str.redhat.com> From: =?UTF-8?Q?Martin_Li=c5=a1ka?= Message-ID: Date: Tue, 7 Jul 2020 15:52:20 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.9.0 MIME-Version: 1.0 In-Reply-To: <87v9izxwo7.fsf@oldenburg2.str.redhat.com> Content-Type: multipart/mixed; boundary="------------7CA831CB2475F4BF80729D67" Content-Language: en-US X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: libc-alpha@sourceware.org, Andreas Schwab Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" This is a multi-part message in MIME format. --------------7CA831CB2475F4BF80729D67 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit On 7/7/20 3:49 PM, Florian Weimer wrote: > * Martin Liška: > >> On 7/7/20 2:17 PM, Andreas Schwab wrote: >>> On Jul 07 2020, Martin Liška wrote: >>> >>>> The current int type can easily overflow for allocation of more >>>> than 4GB. >>>> >>>> The following patch changes that to size_t. I guess I need to adjust >>>> the API version of the function, right? >>> >>> Not only that, it breaks the ABI of mallinfo. >> >> Sure, so what options do I have? I'm new to glibc so a hint would be >> appreciated. > > We need to add a new function. Symbol versioning does not work because > mallinfo is interposed by alternative mallocs (tcmalloc, Address > Sanitizer, etc.). Without the new function name, the interposer does > not know which ABI the application expects, so it's going to be quite > messy. > > Thanks, > Florian > All right, am I closer with the suggested patch? Martin --------------7CA831CB2475F4BF80729D67 Content-Type: text/x-patch; charset=UTF-8; name="0001-Use-size_t-for-mallinfo-fields.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="0001-Use-size_t-for-mallinfo-fields.patch" >From 514b41592d8f435d066817580e0cec29df15b818 Mon Sep 17 00:00:00 2001 From: Martin Liska Date: Tue, 7 Jul 2020 13:58:24 +0200 Subject: [PATCH] Use size_t for mallinfo fields. The current int type can easily overflow for allocation of more than 4GB. --- malloc/malloc.c | 27 ++++++++++++++++++++++++++- malloc/malloc.h | 16 +++++++++++++++- manual/memory.texi | 22 +++++++++++----------- 3 files changed, 52 insertions(+), 13 deletions(-) diff --git a/malloc/malloc.c b/malloc/malloc.c index ee87ddbbf9..9944473a72 100644 --- a/malloc/malloc.c +++ b/malloc/malloc.c @@ -640,6 +640,8 @@ libc_hidden_proto (__libc_mallopt) */ struct mallinfo __libc_mallinfo(void); +struct mallinfo_compat __libc_mallinfo_compat(void); + /* pvalloc(size_t n); @@ -4998,6 +5000,27 @@ __libc_mallinfo (void) return m; } +struct mallinfo_compat +__libc_mallinfo_compat (void) +{ + struct mallinfo_compat m; + struct mallinfo mnew = __libc_mallinfo (); + + m.arena = mnew.arena; + m.ordblks = mnew.ordblks; + m.smblks = mnew.smblks; + m.hblks = mnew.hblks; + m.hblkhd = mnew.hblkhd; + m.usmblks = mnew.usmblks; + m.fsmblks = mnew.fsmblks; + m.uordblks = mnew.uordblks; + m.fordblks = mnew.fordblks; + m.keepcost = mnew.keepcost; + + return m; +} + + /* ------------------------------ malloc_stats ------------------------------ */ @@ -5630,8 +5653,10 @@ weak_alias (__libc_memalign, memalign) strong_alias (__libc_realloc, __realloc) strong_alias (__libc_realloc, realloc) strong_alias (__libc_valloc, __valloc) weak_alias (__libc_valloc, valloc) strong_alias (__libc_pvalloc, __pvalloc) weak_alias (__libc_pvalloc, pvalloc) -strong_alias (__libc_mallinfo, __mallinfo) +#if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_32) weak_alias (__libc_mallinfo, mallinfo) +compat_symbol (libc, __libc_mallinfo_compat, mallinfo, GLIBC_2_32); +#endif strong_alias (__libc_mallopt, __mallopt) weak_alias (__libc_mallopt, mallopt) weak_alias (__malloc_stats, malloc_stats) diff --git a/malloc/malloc.h b/malloc/malloc.h index a6903fdd54..500012bea8 100644 --- a/malloc/malloc.h +++ b/malloc/malloc.h @@ -83,7 +83,7 @@ __THROW __attribute_malloc__; /* SVID2/XPG mallinfo structure */ -struct mallinfo +struct mallinfo_compat { int arena; /* non-mmapped space allocated from system */ int ordblks; /* number of free chunks */ @@ -97,6 +97,20 @@ struct mallinfo int keepcost; /* top-most, releasable (via malloc_trim) space */ }; +struct mallinfo +{ + size_t arena; /* non-mmapped space allocated from system */ + size_t ordblks; /* number of free chunks */ + size_t smblks; /* number of fastbin blocks */ + size_t hblks; /* number of mmapped regions */ + size_t hblkhd; /* space in mmapped regions */ + size_t usmblks; /* always 0, preserved for backwards compatibility */ + size_t fsmblks; /* space available in freed fastbin blocks */ + size_t uordblks; /* total allocated space */ + size_t fordblks; /* total free space */ + size_t keepcost; /* top-most, releasable (via malloc_trim) space */ +}; + /* Returns a copy of the updated current mallinfo. */ extern struct mallinfo mallinfo (void) __THROW; diff --git a/manual/memory.texi b/manual/memory.texi index aa5011e4f9..ac803dd2d5 100644 --- a/manual/memory.texi +++ b/manual/memory.texi @@ -1516,39 +1516,39 @@ This structure type is used to return information about the dynamic memory allocator. It contains the following members: @table @code -@item int arena +@item size_t arena This is the total size of memory allocated with @code{sbrk} by @code{malloc}, in bytes. -@item int ordblks +@item size_t ordblks This is the number of chunks not in use. (The memory allocator -internally gets chunks of memory from the operating system, and then +size_ternally gets chunks of memory from the operating system, and then carves them up to satisfy individual @code{malloc} requests; @pxref{The GNU Allocator}.) -@item int smblks +@item size_t smblks This field is unused. -@item int hblks +@item size_t hblks This is the total number of chunks allocated with @code{mmap}. -@item int hblkhd +@item size_t hblkhd This is the total size of memory allocated with @code{mmap}, in bytes. -@item int usmblks +@item size_t usmblks This field is unused and always 0. -@item int fsmblks +@item size_t fsmblks This field is unused. -@item int uordblks +@item size_t uordblks This is the total size of memory occupied by chunks handed out by @code{malloc}. -@item int fordblks +@item size_t fordblks This is the total size of memory occupied by free (not in use) chunks. -@item int keepcost +@item size_t keepcost This is the size of the top-most releasable chunk that normally borders the end of the heap (i.e., the high end of the virtual address space's data segment). -- 2.27.0 --------------7CA831CB2475F4BF80729D67--