From patchwork Sat Jan 3 20:04:47 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 1234 Return-Path: X-Original-To: u-boot-concept@u-boot.org Delivered-To: u-boot-concept@u-boot.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1767470795; bh=5VZ+N5ADsK3TeeD+7m4mpPx5BqOXI8nS7zDqSsnyiSE=; h=From:To:Date:In-Reply-To:References:CC:Subject:List-Id: List-Archive:List-Help:List-Owner:List-Post:List-Subscribe: List-Unsubscribe:From; b=VVvg7FInca4t25G2XUz6+L3EOK0OVy6ck/5s/QCAgSQLko+1vfDIes70QnzxKVvKZ xk86R61M99zMPa/JRChMUd010IpMDVSwSrp80lCvFW/xC15UfeuaLpICq/YgTD816T deYDz5M/JVd43jxx9c04xp/WNDrSUNNzlJFFVZFGfHeR1CkcU9mi71lNz7aqdqq6DV O6KBFgh7ib/rb7wVPjBVgzGoyY706rcVlf9HKDLCAvoBrAqv2YFe96/8iWXyibFkHj 47SZn0q7kgZLfZyIyOSquSJaxPHAFne3VheOedBDpi0MEPtjOtlQTM52qNNw7Cq22/ VIFRL5T61KzXg== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 5F806690A8 for ; Sat, 3 Jan 2026 13:06:35 -0700 (MST) X-Virus-Scanned: Debian amavis at Received: from mail.u-boot.org ([127.0.0.1]) by localhost (mail.u-boot.org [127.0.0.1]) (amavis, port 10024) with ESMTP id ZLIzRKLAt7RL for ; Sat, 3 Jan 2026 13:06:35 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1767470795; bh=5VZ+N5ADsK3TeeD+7m4mpPx5BqOXI8nS7zDqSsnyiSE=; h=From:To:Date:In-Reply-To:References:CC:Subject:List-Id: List-Archive:List-Help:List-Owner:List-Post:List-Subscribe: List-Unsubscribe:From; b=VVvg7FInca4t25G2XUz6+L3EOK0OVy6ck/5s/QCAgSQLko+1vfDIes70QnzxKVvKZ xk86R61M99zMPa/JRChMUd010IpMDVSwSrp80lCvFW/xC15UfeuaLpICq/YgTD816T deYDz5M/JVd43jxx9c04xp/WNDrSUNNzlJFFVZFGfHeR1CkcU9mi71lNz7aqdqq6DV O6KBFgh7ib/rb7wVPjBVgzGoyY706rcVlf9HKDLCAvoBrAqv2YFe96/8iWXyibFkHj 47SZn0q7kgZLfZyIyOSquSJaxPHAFne3VheOedBDpi0MEPtjOtlQTM52qNNw7Cq22/ VIFRL5T61KzXg== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 1988A690AD for ; Sat, 3 Jan 2026 13:06:35 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1767470793; bh=5u79aadjw5yhkUyYrJy6byEWJ8O3+EQAah2FCgNEjSA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ppFxynIzisx1tIMocWP9TL5QPJJhYr1v/2UgnwMTNE2BcShyapgNcDx5PB6rXf28C Q+Ne00wBR6v5RrznOtcTby6JflRWyYFpzpRo4BSgxId/6PvM+ayi5MU174gmB8qdBA BULpgCOcZLLxc7X81rT/XpBLMWT1fcbfTfmOyuwHrRR07uEFMUzJiJRENJHu782FLv wpAwYSPbsch3ass2l+k8i1rZpp2ps8MdQlAwT3Pd3Xu9xYO4LuqSFIp0+B5wnUQLJY 4l6zt9nMh6nZvnMeCJKW5G13Ry2K1ZYxcjuZ8897tEafQEWEPuQegB2Re+iHCbHIZW mBlGWg6RWAOWA== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 0E24369095; Sat, 3 Jan 2026 13:06:33 -0700 (MST) X-Virus-Scanned: Debian amavis at Received: from mail.u-boot.org ([127.0.0.1]) by localhost (mail.u-boot.org [127.0.0.1]) (amavis, port 10026) with ESMTP id uwNXAgmisLRe; Sat, 3 Jan 2026 13:06:32 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1767470792; bh=PXgpgSyomoBKZco+Au9UUfHXlL8ThuxTpBNVcX27TeU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qvsRx0NGgdG9+b3lYcOYXg5uuE/6gn4hRPnxlT/SQDPnXjg2e+kA2MZkhjqbP/I8A rBzB7Qlp1ORMOsnjjPsGV13uDh4h8J2FGiK5ZgFtKCoMhwpFU0Uzesn8hbdG633qr9 SOEqnclb8Q1tTcNl4RzpkKubPFdFwCeSi3gizHE6clHqxRCffhBgPli35Q9Ej+Gmd1 +jm230o+wffIeAFJ2yHKaakWX8Yf2E9bTbdhTj0Ud/Iqbqk9ZTUOcW1yarx5Tx2Z1G ygVeexif0q6iZdEB7oiBdr1fgVMaIAVYlsxNsPMNsnhuGBbQEm77GphusSods3LCiD wEOBqbhqgZRuw== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id 730FB68FD2; Sat, 3 Jan 2026 13:06:32 -0700 (MST) From: Simon Glass To: U-Boot Concept Date: Sat, 3 Jan 2026 13:04:47 -0700 Message-ID: <20260103200510.3605009-17-sjg@u-boot.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260103200510.3605009-1-sjg@u-boot.org> References: <20260103200510.3605009-1-sjg@u-boot.org> MIME-Version: 1.0 Message-ID-Hash: ZJJX234QIB22WKIKPM4ASECZSLKDV2AV X-Message-ID-Hash: ZJJX234QIB22WKIKPM4ASECZSLKDV2AV X-MailFrom: sjg@u-boot.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: Heinrich Schuchardt , Simon Glass , Claude X-Mailman-Version: 3.3.10 Precedence: list Subject: [Concept] [PATCH v2 16/29] malloc: Fix malloc_usable_size() to handle mcheck headers List-Id: Discussion and patches related to U-Boot Concept Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: From: Simon Glass When mcheck is enabled, malloc_usable_size() returns incorrect results because mem2chunk() is called on the offset user pointer rather than the actual chunk. The pointer returned to the user is offset by the mcheck header, but malloc_usable_size() is unaware of this. Add a wrapper that returns the user-requested size stored in the mcheck header. This fixes test failures when CONFIG_MCHECK_CALLER_LEN is set to larger values. Also add a wrapper for the case where MALLOC_DEBUG is enabled without MCHECK_HEAP_PROTECTION, since MALLOC_DEBUG makes dlmalloc_usable_size_impl() static but no public dlmalloc_usable_size exists outside the mcheck block. Co-developed-by: Claude Signed-off-by: Simon Glass --- (no changes since v1) common/dlmalloc.c | 39 ++++++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/common/dlmalloc.c b/common/dlmalloc.c index 78efdf5fd9a..71f81aeaec7 100644 --- a/common/dlmalloc.c +++ b/common/dlmalloc.c @@ -648,6 +648,7 @@ static inline void MALLOC_COPY(void *dest, const void *src, size_t sz) { memcpy( #define dlrealloc_impl dlrealloc #define dlmemalign_impl dlmemalign #define dlcalloc_impl dlcalloc +#define dlmalloc_usable_size_impl dlmalloc_usable_size #endif static bool malloc_testing; /* enable test mode */ @@ -5943,13 +5944,15 @@ int dlmallopt(int param_number, int value) { return change_mparam(param_number, value); } -size_t dlmalloc_usable_size(const void* mem) { - if (mem != 0) { - mchunkptr p = mem2chunk((void*)mem); - if (is_inuse(p)) - return chunksize(p) - overhead_for(p); - } - return 0; +STATIC_IF_MCHECK size_t dlmalloc_usable_size_impl(const void *mem) +{ + if (mem != 0) { + mchunkptr p = mem2chunk((void *)mem); + + if (is_inuse(p)) + return chunksize(p) - overhead_for(p); + } + return 0; } #if CONFIG_IS_ENABLED(MCHECK_HEAP_PROTECTION) @@ -6098,6 +6101,20 @@ void *dlcalloc(size_t n, size_t elem_size) return mcheck_alloc_noclean_posthook(p, n * elem_size, mcheck_caller()); } +size_t dlmalloc_usable_size(const void *mem) +{ + if (!mem) + return 0; + + if (mcheck_disabled) + return dlmalloc_usable_size_impl(mem); + + /* Return the user-requested size from mcheck header */ + const struct mcheck_hdr *hdr = &((const struct mcheck_hdr *)mem)[-1]; + + return hdr->size; +} + /* mcheck API */ int mcheck_pedantic(mcheck_abortfunc_t f) { @@ -6145,6 +6162,14 @@ void *dlcalloc(size_t n, size_t elem_size) } #endif /* MCHECK_HEAP_PROTECTION */ +#if CONFIG_IS_ENABLED(MALLOC_DEBUG) && !CONFIG_IS_ENABLED(MCHECK_HEAP_PROTECTION) +/* Wrapper needed when MALLOC_DEBUG makes dlmalloc_usable_size_impl static */ +size_t dlmalloc_usable_size(const void *mem) +{ + return dlmalloc_usable_size_impl(mem); +} +#endif + #if !CONFIG_IS_ENABLED(MCHECK_HEAP_PROTECTION) /* Stub when mcheck is not enabled */ void mcheck_set_disabled(bool disabled)