From patchwork Wed Dec 10 00:07:18 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 884 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=1765325400; bh=nMKXndHi+RVAXmzxFpRkIjDdruX60bq/k75BZT/W1mQ=; 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=t/8wQnfw4DDAztB7TnlQ9GrGHzW8fgcFU1Fp5o6huxvsDgaODd6DFGpyM40prgKb5 Yuj9UzSmI00e/81lLWnx7fIO1hHeimSptpLNiysNmJoezZRUdluPibdPmSO6WP0sHC IDCblZzMn+9lnNbiwPNB7fufCFcHAb5xL1s8cYpvOBIpepO82bBIp4tHqMM+16587U 54i/LgowQjhC9oHILwz5c67sb4pv7O9ZtQkieLUlPi9G8NJm3MCfzpYlzZrtEkBv7S 6yLKygr12VdnzLrapPGxG9v8GePVJAkTSUJMahnBT4shrmui14YnNAaaWIH5d/Cpnv /fKBZ2NdA6LJg== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 1F80E68A3E for ; Tue, 9 Dec 2025 17:10:00 -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 xe-U02i0Y1wV for ; Tue, 9 Dec 2025 17:10:00 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325400; bh=nMKXndHi+RVAXmzxFpRkIjDdruX60bq/k75BZT/W1mQ=; 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=t/8wQnfw4DDAztB7TnlQ9GrGHzW8fgcFU1Fp5o6huxvsDgaODd6DFGpyM40prgKb5 Yuj9UzSmI00e/81lLWnx7fIO1hHeimSptpLNiysNmJoezZRUdluPibdPmSO6WP0sHC IDCblZzMn+9lnNbiwPNB7fufCFcHAb5xL1s8cYpvOBIpepO82bBIp4tHqMM+16587U 54i/LgowQjhC9oHILwz5c67sb4pv7O9ZtQkieLUlPi9G8NJm3MCfzpYlzZrtEkBv7S 6yLKygr12VdnzLrapPGxG9v8GePVJAkTSUJMahnBT4shrmui14YnNAaaWIH5d/Cpnv /fKBZ2NdA6LJg== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 0EB65689EA for ; Tue, 9 Dec 2025 17:10:00 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325398; bh=FrDb7ImFH6NHaUGGLrjYggUzgbOtRY3fhYIkRxBe4N4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=TUt3B2hfTwqe2ZuFVPB2zjZG6dKDsNSfVBeXHCqrq7AvIavhUz65h/2MCLK/CmN2G NaQvgK3RBZct44ErSlKPsghVfFvPr16DyY+kcKLBIpHqHMBCrMQdweA17uptt7Fco2 swb/Bv5vKWiRbHfbWQkDzh1QEhPzxH10pEA8mFOKz5nCAaveDJ3OdISYuiPClpvxVE SCDa3R+a6lb9vE+jE3cmDu2t9CjB4MchO1VbAhq7PhCvJWomqdwNduSSD+TORC3vzt X9G8LkmdOfP+qMrumJk5DIFvEzy4LPlQjlIpA9jE6+p/N93YnLtXVhH8J+UbiHhFXj br9YOFN6+Nd6Q== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 67D22687A7; Tue, 9 Dec 2025 17:09:58 -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 6OSo4SpP30ls; Tue, 9 Dec 2025 17:09:58 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325394; bh=oMr05HL1phoXzZ6fb2Ui2Vz+dJU+8jh04tAJ9ld3RBU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YQG3gt0vrZqKg+cBZT5Mx7hjrGm5j4mCraQbCthGFWGGiRKwCwqrj0dLSHu9A1SgE lPpRk6hOWV4duUAuBp5GcbYZI578iEowBYmrqE8MPGAAuPCFwmUB8lD+x8dPdI34dh sDUijYiJD7sAbQxVrcxFfRsvXWPjFifUgJJLyL2liA2MRx4qQqWK8uszztiy/uE205 0ICZ2Mb0ZpSEAGQYx52I+zFLlTMynjKShkc3nImsf8dnTSpFcNwUhzf6O33DM2FAQF y9K7JcYs2fbOooGXV3+DEptQt+IHXVJNkVVHLQc/yjNQ+92H3Zn+EQCh+eK1sR7J83 w/mHyEuGMRBfg== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id DD9906884F; Tue, 9 Dec 2025 17:09:53 -0700 (MST) From: Simon Glass To: U-Boot Concept Date: Tue, 9 Dec 2025 17:07:18 -0700 Message-ID: <20251210000737.180797-28-sjg@u-boot.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20251210000737.180797-1-sjg@u-boot.org> References: <20251210000737.180797-1-sjg@u-boot.org> MIME-Version: 1.0 Message-ID-Hash: TCHUWQRSAQMTN2B22KQ4KNLNENNMWUMV X-Message-ID-Hash: TCHUWQRSAQMTN2B22KQ4KNLNENNMWUMV 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 27/35] malloc: Support storing caller information 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 Add a field to store the caller for each allocation, using the backtrace information. This can be useful when debugging heap corruption. Co-developed-by: Claude Signed-off-by: Simon Glass --- common/dlmalloc.c | 8 ++++---- common/mcheck_core.inc.h | 28 +++++++++++++++++++++------- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/common/dlmalloc.c b/common/dlmalloc.c index e288d94d433..706c12d9b9a 100644 --- a/common/dlmalloc.c +++ b/common/dlmalloc.c @@ -5954,7 +5954,7 @@ void *dlmalloc(size_t bytes) if (!p) return p; - return mcheck_alloc_posthook(p, bytes); + return mcheck_alloc_posthook(p, bytes, NULL); } void dlfree(void *mem) { dlfree_impl(mcheck_free_prehook(mem)); } @@ -5979,7 +5979,7 @@ void *dlrealloc(void *oldmem, size_t bytes) p = dlrealloc_impl(p, newsz); if (!p) return p; - return mcheck_alloc_noclean_posthook(p, bytes); + return mcheck_alloc_noclean_posthook(p, bytes, NULL); } void *dlmemalign(size_t alignment, size_t bytes) @@ -5990,7 +5990,7 @@ void *dlmemalign(size_t alignment, size_t bytes) if (!p) return p; - return mcheck_memalign_posthook(alignment, p, bytes); + return mcheck_memalign_posthook(alignment, p, bytes, NULL); } /* dlpvalloc, dlvalloc redirect to dlmemalign, so they need no wrapping */ @@ -6004,7 +6004,7 @@ void *dlcalloc(size_t n, size_t elem_size) if (!p) return p; - return mcheck_alloc_noclean_posthook(p, n * elem_size); + return mcheck_alloc_noclean_posthook(p, n * elem_size, NULL); } /* mcheck API */ diff --git a/common/mcheck_core.inc.h b/common/mcheck_core.inc.h index c2089add0d6..63dbeaa5f56 100644 --- a/common/mcheck_core.inc.h +++ b/common/mcheck_core.inc.h @@ -73,6 +73,7 @@ // Full test suite can exceed 10000 concurrent allocations #define REGISTRY_SZ 12000 #define CANARY_DEPTH 2 +#define MCHECK_CALLER_LEN 48 // avoid problems with BSS at early stage: static char mcheck_pedantic_flag __section(".data") = 0; @@ -88,6 +89,7 @@ struct mcheck_hdr { size_t size; /* Exact size requested by user. */ size_t aln_skip; /* Ignored bytes, before the mcheck_hdr, to fulfill alignment */ mcheck_canary canary; /* Magic number to check header integrity. */ + char caller[MCHECK_CALLER_LEN]; /* caller info for debugging */ }; static void mcheck_default_abort(enum mcheck_status status, const void *p) @@ -196,7 +198,8 @@ static size_t mcheck_alloc_prehook(size_t sz) } static void *mcheck_allocated_helper(void *altoghether_ptr, size_t customer_sz, - size_t alignment, int clean_content) + size_t alignment, int clean_content, + const char *caller) { const size_t slop = alignment ? mcheck_evaluate_memalign_prefix_size(alignment) - sizeof(struct mcheck_hdr) : 0; @@ -207,6 +210,10 @@ static void *mcheck_allocated_helper(void *altoghether_ptr, size_t customer_sz, hdr->aln_skip = slop; for (i = 0; i < CANARY_DEPTH; ++i) hdr->canary.elems[i] = MAGICWORD; + if (caller) + strlcpy(hdr->caller, caller, MCHECK_CALLER_LEN); + else + hdr->caller[0] = '\0'; char *payload = (char *)&hdr[1]; @@ -239,14 +246,19 @@ static void *mcheck_allocated_helper(void *altoghether_ptr, size_t customer_sz, return payload; } -static void *mcheck_alloc_posthook(void *altoghether_ptr, size_t customer_sz) +static void *mcheck_alloc_posthook(void *altoghether_ptr, size_t customer_sz, + const char *caller) { - return mcheck_allocated_helper(altoghether_ptr, customer_sz, ANY_ALIGNMENT, CLEAN_CONTENT); + return mcheck_allocated_helper(altoghether_ptr, customer_sz, + ANY_ALIGNMENT, CLEAN_CONTENT, caller); } -static void *mcheck_alloc_noclean_posthook(void *altoghether_ptr, size_t customer_sz) +static void *mcheck_alloc_noclean_posthook(void *altoghether_ptr, + size_t customer_sz, + const char *caller) { - return mcheck_allocated_helper(altoghether_ptr, customer_sz, ANY_ALIGNMENT, KEEP_CONTENT); + return mcheck_allocated_helper(altoghether_ptr, customer_sz, + ANY_ALIGNMENT, KEEP_CONTENT, caller); } static size_t mcheck_memalign_prehook(size_t alig, size_t sz) @@ -254,9 +266,11 @@ static size_t mcheck_memalign_prehook(size_t alig, size_t sz) return mcheck_evaluate_memalign_prefix_size(alig) + sz + sizeof(mcheck_canary); } -static void *mcheck_memalign_posthook(size_t alignment, void *altoghether_ptr, size_t customer_sz) +static void *mcheck_memalign_posthook(size_t alignment, void *altoghether_ptr, + size_t customer_sz, const char *caller) { - return mcheck_allocated_helper(altoghether_ptr, customer_sz, alignment, CLEAN_CONTENT); + return mcheck_allocated_helper(altoghether_ptr, customer_sz, alignment, + CLEAN_CONTENT, caller); } static enum mcheck_status mcheck_mprobe(void *ptr)