From patchwork Wed Dec 10 00:06:52 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 859 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=1765325298; bh=AfzuWxWfF0X7Lmp/RDiXvA3qNRMb7r3MO8V3Km34CeM=; 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=f9yMTm/hS1NC/EIzNc9rTIZ/thFb8N6ZJgwK8XZjrWwuv+wF+DHdD3TEjy7CLrJAC 42s8D+GSqm7t27guWwFCROuYd7Cv7blDd8tsrSF41fdfCrJAnKSIH5So0NlSq1bsvo qps4P/AEyn2v4n7DQJxK287H4k0BQJLKw2qsBlYsDqhTydg5o5KNPjRFBKZE30AFuJ ojhCO8NblPJRU48MkPEa7lbvaBzUwIXvagwsn1mJrPlNpi9aVuBt0BOVyHSBfbsGDz Cdsy8P7IXLhjzDHjVUlBiz+dYzUY6tCKiECM0F7fndPzSGMYSmbv6CIUzgUN0ssUul nRDrPDt+evnIA== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 88329689EA for ; Tue, 9 Dec 2025 17:08:18 -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 xvMAmbdZvNTS for ; Tue, 9 Dec 2025 17:08:18 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325298; bh=AfzuWxWfF0X7Lmp/RDiXvA3qNRMb7r3MO8V3Km34CeM=; 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=f9yMTm/hS1NC/EIzNc9rTIZ/thFb8N6ZJgwK8XZjrWwuv+wF+DHdD3TEjy7CLrJAC 42s8D+GSqm7t27guWwFCROuYd7Cv7blDd8tsrSF41fdfCrJAnKSIH5So0NlSq1bsvo qps4P/AEyn2v4n7DQJxK287H4k0BQJLKw2qsBlYsDqhTydg5o5KNPjRFBKZE30AFuJ ojhCO8NblPJRU48MkPEa7lbvaBzUwIXvagwsn1mJrPlNpi9aVuBt0BOVyHSBfbsGDz Cdsy8P7IXLhjzDHjVUlBiz+dYzUY6tCKiECM0F7fndPzSGMYSmbv6CIUzgUN0ssUul nRDrPDt+evnIA== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 76B6768974 for ; Tue, 9 Dec 2025 17:08:18 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325295; bh=EpGVkQ0NrQQZ9kG6XzRshIUODuNysa9RzATkU8Ui7d4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ke7+ZzX5S934lOeR5ZUIfEp5ggtH+iZsQdJ8Y3vvx/8v8zjqsu6gEU/SzAigwZmTh 8/CnhHPMDPqeqH4AZK8acxhQ2ju+6xWW/KQekYqgtUFMp4ijYdszNBOIp6w5alebZn DEm8y9tGUSBc39hVOP0vYyOc5p5GxQu3p+bvVX++HCcgfPR/cZ5uXHhTHWgtS/SAJL c9fX1fpA8gqgySHEU2v5acoSHo4zDFLnIya/zBY7b57TSQT6zLodOs8mF+Se4xzFYG wCOffN6cwujlhYWNdsPvcAIoZlCl1zA9VOgw+qrPguJWz+3W6860HcymoAW/mtIydF K/GWhsJNW2n3A== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 7A7E868947; Tue, 9 Dec 2025 17:08:15 -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 lLToLCezm18C; Tue, 9 Dec 2025 17:08:15 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325291; bh=CyG3xl/YeKjcJ2c4yivwNjAy0W9fQS1MIEjoIuyun78=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ZBM88SZVNlf6bCLpZYqM9Nzmc+xND4kfjcrgGbER/rQHxJdnFReR/mh0Q7vyckujQ EzvT8PVJ9mtvcuwZoz85MyfW/sTQApomQAjf++t6Sy1kNiJE3XOZeerbONM1z1OluA a4/Yg+RXkIT+qLb2WgKBnVadFtoj352xQ7M61G5Nmph/qwIjcxZZDTnpvF08OyjKIX Lhd9XYYr9E6kUY7JWFnBMERCcdgSFuytymUCI1aST2vepCbQA2rNcYfBzP52xlUoiT Nhddb2swQ0zRF1sdcCqWrf4DpZKbCaVw1PX2KkbfQ79dKnhZ4fU5sX1HpOcTAUogfw A2MQ1MTm465ug== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id 07E2A687A7; Tue, 9 Dec 2025 17:08:10 -0700 (MST) From: Simon Glass To: U-Boot Concept Date: Tue, 9 Dec 2025 17:06:52 -0700 Message-ID: <20251210000737.180797-2-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: QNLX734N76TYBHTDBSHUBWOQGVI556A3 X-Message-ID-Hash: QNLX734N76TYBHTDBSHUBWOQGVI556A3 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 01/35] watchdog: Unregister cyclic on device removal 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 a watchdog device is destroyed, the cyclic_info embedded in the device's private data is freed but remains in the global cyclic list. The subsequent cyclic_unregister_all() call then accesses freed memory, causing a crash. Add a pre_remove hook to the watchdog uclass to unregister the cyclic function before the device is destroyed. Co-developed-by: Claude Signed-off-by: Simon Glass --- drivers/watchdog/wdt-uclass.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/drivers/watchdog/wdt-uclass.c b/drivers/watchdog/wdt-uclass.c index 10be334e9ed..c2d19530b3d 100644 --- a/drivers/watchdog/wdt-uclass.c +++ b/drivers/watchdog/wdt-uclass.c @@ -256,10 +256,21 @@ static int wdt_pre_probe(struct udevice *dev) return 0; } +static int wdt_pre_remove(struct udevice *dev) +{ + struct wdt_priv *priv = dev_get_uclass_priv(dev); + + if (IS_ENABLED(CONFIG_WATCHDOG) && priv->running) + cyclic_unregister(&priv->cyclic); + + return 0; +} + UCLASS_DRIVER(wdt) = { .id = UCLASS_WDT, .name = "watchdog", .flags = DM_UC_FLAG_SEQ_ALIAS, .pre_probe = wdt_pre_probe, + .pre_remove = wdt_pre_remove, .per_device_auto = sizeof(struct wdt_priv), }; From patchwork Wed Dec 10 00:06:54 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 860 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=1765325305; bh=Lpms4AlWA3+4JtI3mbex9vypMx41NVbXIFSxyVdpsnc=; 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=RrVk94GqhdzKMnTSsNYIsb75a6/du+4rHIU+Ahe7AelPFr8hoCiTl5c/Qmx4aXVqk G8sDjPf7PqdX6CZroZP3ksrheAcHRfrkV0Ps9+Eoil5IM/zRu2bJ/MB9lwZce71eWX sS+qObQVl/R6Q6eug2EyiFcD1yaoHqWTUOSUQTinrgOwNyx86lbwJ3GZqwaUdUUMDT BoETqJidYKHg0nhMEf+DjWpwe6aDK00XFzbqQMd5+d+DXkQCsv8U66oLnNA7R3jPYA 4GWcAIq3Td71PUu0CliUMuhWaDVqTrjCFJ8b2p2lbV4zjLTe8/yFuavezz3Kycghxe uEEZhdIMmzN/Q== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id AD62B68A44 for ; Tue, 9 Dec 2025 17:08:25 -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 dvR9pf00_jq0 for ; Tue, 9 Dec 2025 17:08:25 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325304; bh=Lpms4AlWA3+4JtI3mbex9vypMx41NVbXIFSxyVdpsnc=; 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=hcfBlNRhfaqcRf83k++FduenQ+Zqi7jG6hP/tT7HXvqNy9WuCVjBGBoCT4U3SegHS Rqh+NFd/bzBugGPkNTJhbKpiqkVf4bP21xjZmOHoa5QrM2oBOrR/nYlICY7HlHFbg2 anXxelWJEZZxXGZj3R7SxADQAU8ETbY/Gs6k2spb6YHaeDTBqxB0qFb0UCMYbdpS22 NL/iD6vXhWioCJ57iXFBvRaLWZZOjX0f4cAExhshIqUZ7YyYGVubkd8WivV+tQoa2q EQ6L/eXMMKFkV6sPtqIZBqJ34N+hS7QJ+RmxsU85MxdkqivezY/0TUDvNvSAznMV+l X87PentJoVivA== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id A57C468947 for ; Tue, 9 Dec 2025 17:08:24 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325302; bh=23TNKTEAyPIY2qLcM2ZwxAb0g2NNhAafJ+bX5RdTpgA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=r8ypHyBbpRysa16PQc/ncuWO0v3dgXYHo6/VsFURLgAequzwlTeO9fDGnlNoNTrAS Kd7HbFk/imzTEwzkUhaimqV2xQnpdS/IADoT/NeRG4MsAOorZ09EKcLZEoad8E/HTz JZqJ6IarQHndBdaMTC+eNA42V06hCXPnCj8GcEuoP4qbqRgqTdD0qvwQ9UEBelZQUW jRdqnzPBd5lzA/lWAN+5i7kBxBzGQuzCcLCjIhW1KN+ffiUBKVgv246lJA6MXI+ef2 1vBTCQMAl0N77O1w5nuo/OyfpaY+cZN3bpi/xRIg7TszP/W96uUK+s6H8EisnLRmwz WjTIDrnvw4HBw== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 317B768947; Tue, 9 Dec 2025 17:08:22 -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 Rww_xpkR-HC9; Tue, 9 Dec 2025 17:08:22 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325301; bh=Vapis0fXP81eqLMDnHzFF8XiACbgE2y2h6kMvE7dA4k=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=pQ9ebG9SpL31xES70WTpgRYF88R6Zn55tYbx0XW1ZF452cDGEuQ+FO8OER9NNe/KR d9Wg18yz4F+B6+QLtoJthLtmXufs7Y7iu8vM+X1FneAK9m+MzrofMq8gRXzLq/1IJN fwfaR0sPpgrfnCVUdwIkmmmwGRu638hIPL2FCF42+JUae7JQv7QAJUNtVy8SJb84S5 tUcn/EaZH2zd9w/O9S3du91fpusZaYwh9GnAUE/ItcFud1uqHlsTBJ950CN8jFFKQx EtaGX97u7nSwkLXl/nO+eyf59hxpWPgzPMGFjGRPeApQBVFAROaUvuFYq5U974VDUB r557SeoRkazYA== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id 7BDDB687A7; Tue, 9 Dec 2025 17:08:21 -0700 (MST) From: Simon Glass To: U-Boot Concept Date: Tue, 9 Dec 2025 17:06:54 -0700 Message-ID: <20251210000737.180797-4-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: 436RETOMBOONOECU2OU3343734Z2AYEY X-Message-ID-Hash: 436RETOMBOONOECU2OU3343734Z2AYEY 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 Opus 4 X-Mailman-Version: 3.3.10 Precedence: list Subject: [Concept] [PATCH 03/35] test: dm: Fix memory leaks in test drivers 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 The test_manual_drv driver allocates private data in its probe function with calloc(), but never frees it in remove(). Add the missing free() call to test_manual_remove(). Similarly, create_children() allocates platform data with calloc() and sets it with dev_set_plat(), but doesn't set the DM_FLAG_ALLOC_PDATA flag. This flag tells the device removal code to free the platform data. Set this flag so driver model will free the allocated memory on unbind. These fixes eliminate memory leaks that caused the malloc_dump output to grow excessively after running DM tests. Co-developed-by: Claude Opus 4 Signed-off-by: Simon Glass --- test/dm/core.c | 1 + test/dm/test-driver.c | 3 +++ 2 files changed, 4 insertions(+) diff --git a/test/dm/core.c b/test/dm/core.c index 959b834576f..75c44e8328c 100644 --- a/test/dm/core.c +++ b/test/dm/core.c @@ -606,6 +606,7 @@ static int create_children(struct unit_test_state *uts, struct udevice *parent, pdata = calloc(1, sizeof(*pdata)); pdata->ping_add = key + i; dev_set_plat(dev, pdata); + dev_or_flags(dev, DM_FLAG_ALLOC_PDATA); if (child) child[i] = dev; } diff --git a/test/dm/test-driver.c b/test/dm/test-driver.c index 759de3a5f77..fc4e3d82a1f 100644 --- a/test/dm/test-driver.c +++ b/test/dm/test-driver.c @@ -135,12 +135,15 @@ static int test_manual_probe(struct udevice *dev) static int test_manual_remove(struct udevice *dev) { dm_testdrv_op_count[DM_TEST_OP_REMOVE]++; + free(dev_get_priv(dev)); + return 0; } static int test_manual_unbind(struct udevice *dev) { dm_testdrv_op_count[DM_TEST_OP_UNBIND]++; + return 0; } From patchwork Wed Dec 10 00:06:55 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 861 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=1765325306; bh=Q0zZjrJx6FuUzaj3Zsv89IAejJwldvgUIzXAsWY20tw=; 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=qsjHWY+iv9/thPPxQrd1NEdgN6q0916ss+Nchot75lZGKoEYZiHQhIgkI+5qeR9v7 fmjsC1/WVVvryPcmoz95Pi16e6E397N2LO7xtoc0oYNIr1mEmONUebTkOnhZWN2eVG AC/7MIJbOYRSG18l2FL2fKbsm8jKBwIlTYtiFdLk7BsuO9ETqbjFVfdGYnvYdZDJy9 TzpbMEnVvPzFHBSdwZ/j7jL7KfH2dAjgi7GWo4ldiqBs9hPRt4OcuS7CJweD0Oy7Fd haD2dT/sPK9WBNjB5K3ASd9f3aiuGkb1NlzIanqvYHP5FDqFGtfzXQNmAa6vRVxgFO eVuAkwLcxpAaQ== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 12B0F68A3B for ; Tue, 9 Dec 2025 17:08:26 -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 Hhk_qzY-HXji for ; Tue, 9 Dec 2025 17:08:26 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325305; bh=Q0zZjrJx6FuUzaj3Zsv89IAejJwldvgUIzXAsWY20tw=; 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=ZmayoB39HcSMkz79bTzS9H0qCy/BH1Vf2zkakUpY1joRA6ZBbqtL8SL+XHcPnb0rv LYN6cj0zI/5IA05BZ0GSDHGymY6AyRqq/QvN1Y5QKobSSbJIGG98vphFmhj2h9FDJq bF8WR7L9B7vbgZlScJeMeAM+yK+sOI7XuDLkp701c8xCaOIBISC5tb1RMakBjaYRqj GFgtUp9HGPp1QBX2HCoF0jSgs+B7bvE3JKyxl0DgmNUAl9OIKcoYccbOE6mXWBjZZS Pq2567nMUSGtKOC8UCq9c65S1b1LkyMJpBkn0QcrvbIteOG6fLRdjhArdUXEGYqdas e9WFpH1w0Nk2g== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 1827568A3F for ; Tue, 9 Dec 2025 17:08:25 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325303; bh=pz8k6Q5x/3Xpdmft2bin0XHZjwMOOsKz+g5P0NNZYjs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=QJLS97lS9IAY/agEykR/g4grINwIwg2mK1HpYzXQTus/6qet+6SnFMSh5fHS+KXzU mKK6xSIi8Ks08WRu4G2FX+BGPH3viA9hc5Se3cIiaOqiAMiyIEKFD2+PTFAhHg+R06 YzYHy2/Uu1lj9j3mk6tVht1+jwxe/5I9z15KfObAy4/bdU8HSi8bHRfxx+6tM5e8KS MFO6ASrp4dXJT95+OYL7+35m9yoIdJpiO4qEbf6VVdPbMIm2Lsbp0gq9zORREx3ox7 zupCbSWFT7lOu0KFxzrl63KjXtH/rp6PfN7c4tHlV1A0AQGlmVhNcE8NS9iZcHtj2B FW7U3Vcl9LLJg== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 487E668A2E; Tue, 9 Dec 2025 17:08:23 -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 0mj5k6bnHV57; Tue, 9 Dec 2025 17:08:23 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325302; bh=YSgqEtYT3JnfkR1m6IKlCHqm00P2hS8TzocMoqjrvvs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ftq1XI1OKgfqi5OojkpCxBDlqBsLrYdLpk0lyheen43masiUc4e9RDDTW7WcFLucv IC4haTBacoLIBobxpFtR3341GtmkDp4laGOIfbCL+A1J6q+InLt+lfi+mDWVm1LqIw SBv388R9yvyDD3RY04UBbr82NGT6D0TOIs2zgFJOXF1QNMN0GxAbB356pvDgMwY2Wv 4q1gnaZ+9PIC4BVF6NELMIq/QrGwO+WN8o2j/32+a9Q/GK8vUu+qPt0TYjabWA6A+m ntUQj3mIEnsNo5a1W/LdrPy+wngWpLBvRHeqoR6PXUYGsv03RQEmBvQ502JU/ikLjX SpNBrSXLq46+g== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id 806A9687A7; Tue, 9 Dec 2025 17:08:22 -0700 (MST) From: Simon Glass To: U-Boot Concept Date: Tue, 9 Dec 2025 17:06:55 -0700 Message-ID: <20251210000737.180797-5-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: 3YM4VNGODA7UOAMIAF7IQIYNH7VBGNRH X-Message-ID-Hash: 3YM4VNGODA7UOAMIAF7IQIYNH7VBGNRH 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 04/35] test: dm: Fix delta usage in dm_test_devres_free() 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 This test passes delta values to ut_check_delta() where it expects absolute values. ut_check_delta(last) computes (current - last), so 'last' must be an absolute reference point from a previous ut_check_delta(0) call, not a delta. The bug causes incorrect memory accounting that happens to work with smaller allocation overhead but fails when mcheck header size increases. Use ut_check_delta(0) to capture absolute reference points before each operation. Co-developed-by: Claude Signed-off-by: Simon Glass --- test/dm/devres.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/test/dm/devres.c b/test/dm/devres.c index efc5c72ae2a..353353767b4 100644 --- a/test/dm/devres.c +++ b/test/dm/devres.c @@ -44,23 +44,21 @@ DM_TEST(dm_test_devres_alloc, UTF_SCAN_PDATA); /* Test devm_kfree() can be used to free memory too */ static int dm_test_devres_free(struct unit_test_state *uts) { - ulong mem_start, mem_dev, mem_kmalloc; + ulong mem_start, mem_alloc; struct udevice *dev; void *ptr; mem_start = ut_check_delta(0); ut_assertok(uclass_first_device_err(UCLASS_TEST, &dev)); - mem_dev = ut_check_delta(mem_start); - ut_assert(mem_dev > 0); + ut_assert(ut_check_delta(mem_start) > 0); ptr = devm_kmalloc(dev, TEST_DEVRES_SIZE, 0); ut_assert(ptr != NULL); - mem_kmalloc = ut_check_delta(mem_dev); - ut_assert(mem_kmalloc > 0); /* Free the ptr and check that memory usage goes down */ + mem_alloc = ut_check_delta(0); devm_kfree(dev, ptr); - ut_assert(ut_check_delta(mem_kmalloc) < 0); + ut_assert(ut_check_delta(mem_alloc) < 0); device_remove(dev, DM_REMOVE_NORMAL); ut_asserteq(0, ut_check_delta(mem_start)); From patchwork Wed Dec 10 00:06:56 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 862 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=1765325310; bh=aCS/RvU/N7WOUtfjp8KSk15CWfKsJA1J4NaKla+FoqQ=; 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=LQoq5pU6jbIajggQtzvd72C6PCvLjE+hmuWSbNtasFNnP0JRV8Zol2kginEABr6fy M11mnCMYOtbXQcZAOnXMrVg/N7yhWlbJ3irQRswIKWAPVYci2iGtSwscCXX0dlpMwW fleJauOnsMNsWNw5mTknktAUBOpUvLpC0CkSNDXOmYFe7svxfHXIpl9E/8fJ4DUWr4 +j/fsZ0gaHbWyWHrJTa4yaCVngx7kWJkh6NyO7HBqoCfugbQ+aNu0YFBSmCrRhLs5E Xm2VQkIT1G4yEZhDOfV5QH+SjsXLcwb7PAKCJK0pLOnzQZKh5BCtS3caJQgCtAKvkF usAVC/KUIF07Q== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 91B446898C for ; Tue, 9 Dec 2025 17:08:30 -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 Nj6fjN2oBoDs for ; Tue, 9 Dec 2025 17:08:30 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325310; bh=aCS/RvU/N7WOUtfjp8KSk15CWfKsJA1J4NaKla+FoqQ=; 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=LQoq5pU6jbIajggQtzvd72C6PCvLjE+hmuWSbNtasFNnP0JRV8Zol2kginEABr6fy M11mnCMYOtbXQcZAOnXMrVg/N7yhWlbJ3irQRswIKWAPVYci2iGtSwscCXX0dlpMwW fleJauOnsMNsWNw5mTknktAUBOpUvLpC0CkSNDXOmYFe7svxfHXIpl9E/8fJ4DUWr4 +j/fsZ0gaHbWyWHrJTa4yaCVngx7kWJkh6NyO7HBqoCfugbQ+aNu0YFBSmCrRhLs5E Xm2VQkIT1G4yEZhDOfV5QH+SjsXLcwb7PAKCJK0pLOnzQZKh5BCtS3caJQgCtAKvkF usAVC/KUIF07Q== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 7F5E9689EC for ; Tue, 9 Dec 2025 17:08:30 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325308; bh=Kj4JJPjsC5axm0vFnQ+tewl/Foh6sxUWWYi7G4hFUM4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=v0jcG/4fPe/e1xsOw32tMnbqmxjYBLzzUBeglc419T6cyfnw74N0tCh2tN4vJrKAc gmjZhTI1DyeXFF5L63R9nOj+fsvrjPYLZiuOMF2Xaw52laO/3SObR8m838jEkm5Cya 4ybJuaKqWdF1sRwTMKP/Tmfx5+S+jvoWMCGCzV5ipj62fgMP8Bc1g5h0G5xkHaQFat zNtsHhW8Rnp65JXaEDSn3pCZyTIVwD9FpbwYya7bYWOJHcoOyOUhpthZU+C5L/Ca2g qMaQbvHo2qqJ7EUsm2DFaD2AublNCf6TyPsBcsha0LZBTqd7BZGCNscBURZ6uOXjFK TS0UmcRabsILg== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 51F8868972; Tue, 9 Dec 2025 17:08:28 -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 gZoWLSnEdWIo; Tue, 9 Dec 2025 17:08:28 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325303; bh=3sPBQgnRXnhgWIbPwyBkWtBpBVbh+ShdrN5by77jaMk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=cD0yRmVYUN9/A9JhPCQZeID83f+lneZd2v+HGdSHTjI5uDzLy+OPlJWmi+haKc0cN 2QhqrWl6fNV8UzR2j7SEmXKw10dgIYD/XNHs2FHYJCpIfKtMK3QQuhiz3Relce1Nof fc3Kx+G1kM5clGhLFW6ePgydOoHAjmPXOKmk1Eq7zN43XQlGex+YhM7I9+VmMKXD/E ktG7Hg9AtTRI7wctN3Y/IU9XdEkX1OlLMaudLtO2fshPjqw1ab/1l1weyPvSga2HKk pVWljHrV4rUgEqjab+JJmgO/7xbxuWc9Ou4Y/2ktuWBzqClbEwga7yaIVsQyHMYwOn blteuVtOPxP9g== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id 8FA9A68A26; Tue, 9 Dec 2025 17:08:23 -0700 (MST) From: Simon Glass To: U-Boot Concept Date: Tue, 9 Dec 2025 17:06:56 -0700 Message-ID: <20251210000737.180797-6-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: KWE5DJX6AB2QPRI4AOMKJDERVPOGNTZL X-Message-ID-Hash: KWE5DJX6AB2QPRI4AOMKJDERVPOGNTZL 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 X-Mailman-Version: 3.3.10 Precedence: list Subject: [Concept] [PATCH 05/35] mmc: sandbox: Fix memory leak on probe failure 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 If mmc_init() fails, the buffer allocated by calloc() or mapped by os_map_file() is not freed. Free or unmap the buffer before returning the error. Signed-off-by: Simon Glass --- drivers/mmc/sandbox_mmc.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/drivers/mmc/sandbox_mmc.c b/drivers/mmc/sandbox_mmc.c index a24520f2e78..f49cb4b146a 100644 --- a/drivers/mmc/sandbox_mmc.c +++ b/drivers/mmc/sandbox_mmc.c @@ -190,7 +190,16 @@ static int sandbox_mmc_probe(struct udevice *dev) } } - return mmc_init(&plat->mmc); + ret = mmc_init(&plat->mmc); + if (ret) { + if (plat->fname) + os_unmap(priv->buf, priv->size); + else + free(priv->buf); + return ret; + } + + return 0; } static int sandbox_mmc_remove(struct udevice *dev) From patchwork Wed Dec 10 00:06:57 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 863 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=1765325312; bh=ALbmhRkONuUgeJcWbToMEM3zmu5YXtW8ncRv0SVrqkw=; 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=goNnn8z5WJ5KthxgxLpdI/PRLapEAHrxQwPgJnYvibBOY5WWxfsMmIQcNOj+vKANy eRhGTJBlDkMxE92qQuEtmirb5Mdmj+chT8K4gQJ8g1qyVtOcdpO6RVKW7lEa5TEcwC +24L9sh8bNDBeGHgm+8dV7ynDT6I3HD3q2SHAJ46nj1wwy7HNoVuOux9uLK2e/wHTj SNQOa4EH6/XK2OG3nRn4xAG04dFWQU/XriDvg+nPyYDGbefzWnHGR2bdM6d8HOIPr+ zFgxlrUOwzsUL0IxKSACPp501BRuc00dndOmcBEWEYASdIT1rNIDHEwcE3prO2yzh5 E7OWOdvlPGxpg== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id F00D068A44 for ; Tue, 9 Dec 2025 17:08:32 -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 v0plSkrS9q8B for ; Tue, 9 Dec 2025 17:08:32 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325311; bh=ALbmhRkONuUgeJcWbToMEM3zmu5YXtW8ncRv0SVrqkw=; 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=h1JNAhWPQNEjyUZEbTxTObw/GS9V2gNi7WoE837x6jaZjYWP5OvE6MsAJJ0jCm4Nh 0YbHebu1crH02BIEDy0B0n+IGXm0rwb/jBACKStVLw/KZytxoR37+3a5cdyb6MWQ2V YQrwXc0PBKWR2DR6rhKjl4W0Cu048/HMG0cccrqosMURRLXTxVY4/ld7vIzeglkdvT sj9Y1c/lxvKkbDRoJn1u9haMWjgVVkd6JiwwNT21bh5zl5AejSJFcp1RfGuYj3GgU+ nqa4BtAzPeu+eDwykFL/avTCTgbdCH8ClUDklGKszXxOslMkDkeksHUHfuMTRO8asU 3PF9fP1NEjWmQ== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 23E33689EC for ; Tue, 9 Dec 2025 17:08:31 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325309; bh=GSwsHt5fVcvncr07cDR/oeoOfUqXZsClLF8jL/e0t/M=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=gCOqEUfGrdYtqgC3wyVLDLb6YuVkSjuA8AHCCF3n607g9KkNwFg7QSwkUxgH1R+Po GP4LDB/DLHoDQkTE04bLJFi8gwchs0xzhG0Mz1/NhCwbKRHtS0ARSHzKi3QEhveSl4 ZYhEhc7+/m/LjIHXCsoyPffZkok8STu+viOj83/CR8kD368LSJv2GlrtFXaFSoDulT sDH+PQEVZTwxb8NdfdSGuV05v7QHhyMbbSvVnWCqdMOacSGa0+yBClxaRdGhNlKJrv BPTorUGLgEeOEtmkazh5KPB7U22d0POXATsrQVFzP2WTWM6F/2U6P7CKP2ICvTC3/M 0+Pbwqj87Worw== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 209B96898C; Tue, 9 Dec 2025 17:08:29 -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 ZZG4LSO8tuK9; Tue, 9 Dec 2025 17:08:29 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325308; bh=cEuFvV9M+bEZONlckegjsOhhm5exylGQwZgySt8Bxfo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=LYIErBBOC4Jne4kBHbacmGenjI395Ntlgl/d2kVv5mq95mg8sndHNg9j8YtLF/Epj rzxzjxSUuSSToZiCyUDdy8xQTSAEpVLHpH/tv5TLsfI+54hgYv0nCrVQcdru0PUYMk s9SIR7s9hXIvde4iYaOlYvmzJDW4LhE9TZisw94AmZKlq4V0qx21qpGD0MF2INwJCk Dd0outjFbsUBfDEifrYWByiKkX5jhNUIu5xVizgOuCA1JPgMrdlGHmMtEUCy8M5w4L pj1sAPyv8qrY+XpR6rdOZfHVO0VRx/1MWWDutUnNRG8vxZboYoG3/toC8nyORn5OBJ m2zhBT3vkUJbQ== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id 3840F68947; Tue, 9 Dec 2025 17:08:28 -0700 (MST) From: Simon Glass To: U-Boot Concept Date: Tue, 9 Dec 2025 17:06:57 -0700 Message-ID: <20251210000737.180797-7-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: H6NKHDZUB22XMDGPZ5REN5SNMBQUC2WB X-Message-ID-Hash: H6NKHDZUB22XMDGPZ5REN5SNMBQUC2WB 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 Opus 4 X-Mailman-Version: 3.3.10 Precedence: list Subject: [Concept] [PATCH 06/35] video: truetype: Use pre-allocated buffer for glyph rendering 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 The TrueType console driver calls malloc/free for every character rendered, which causes significant memory fragmentation and allocation traffic. Add a pre-allocated buffer (CONFIG_CONSOLE_TRUETYPE_GLYPH_BUF_SIZE, default 4KB) to the driver's private data. When rendering a glyph, use this buffer if the glyph bitmap fits, avoiding malloc/free for normal characters. Fall back to malloc only for oversized glyphs. The buffer is allocated lazily after relocation to avoid consuming early malloc space before the full heap is available. Add CONFIG_VIDEO_GLYPH_STATS (default y on sandbox) to track cumulative glyph rendering statistics in global_data. This persists across video device rebinds and can be viewed via 'font info'. Co-developed-by: Claude Opus 4 Signed-off-by: Simon Glass --- cmd/font.c | 30 ++++++++++++++-- doc/usage/cmd/font.rst | 20 ++++++++++- drivers/video/Kconfig | 24 +++++++++++++ drivers/video/console_truetype.c | 59 +++++++++++++++++++++++++++---- include/asm-generic/global_data.h | 22 ++++++++++++ test/cmd/font.c | 20 +++++++++++ 6 files changed, 166 insertions(+), 9 deletions(-) diff --git a/cmd/font.c b/cmd/font.c index 384751e787a..99c8ae76f2e 100644 --- a/cmd/font.c +++ b/cmd/font.c @@ -11,6 +11,22 @@ #include #include +#if CONFIG_IS_ENABLED(VIDEO_GLYPH_STATS) +static int do_font_info(struct cmd_tbl *cmdtp, int flag, int argc, + char *const argv[]) +{ + int buf_count, malloc_count; + + /* read the results first so that they don't change while printing */ + buf_count = gd->glyph_buf_count; + malloc_count = gd->glyph_malloc_count; + printf("glyph buffer renders: %u\n", buf_count); + printf("glyph malloc renders: %u\n", malloc_count); + + return 0; +} +#endif + static int do_font_list(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) { @@ -75,12 +91,22 @@ static int do_font_size(struct cmd_tbl *cmdtp, int flag, int argc, return 0; } +#if CONFIG_IS_ENABLED(VIDEO_GLYPH_STATS) +#define FONT_INFO_HELP "\nfont info - show glyph rendering statistics" +#define FONT_INFO_SUB , U_BOOT_SUBCMD_MKENT(info, 1, 1, do_font_info) +#else +#define FONT_INFO_HELP +#define FONT_INFO_SUB +#endif + U_BOOT_LONGHELP(font, "list - list available fonts\n" "font select [] - select font to use\n" - "font size - select font size to"); + "font size - select font size to" + FONT_INFO_HELP); U_BOOT_CMD_WITH_SUBCMDS(font, "Fonts", font_help_text, U_BOOT_SUBCMD_MKENT(list, 1, 1, do_font_list), U_BOOT_SUBCMD_MKENT(select, 3, 1, do_font_select), - U_BOOT_SUBCMD_MKENT(size, 2, 1, do_font_size)); + U_BOOT_SUBCMD_MKENT(size, 2, 1, do_font_size) + FONT_INFO_SUB); diff --git a/doc/usage/cmd/font.rst b/doc/usage/cmd/font.rst index 6e313e70c7a..5e1f6e446e4 100644 --- a/doc/usage/cmd/font.rst +++ b/doc/usage/cmd/font.rst @@ -14,6 +14,7 @@ Synopsis font list font select [ []] font size [] + font info Description ----------- @@ -38,6 +39,15 @@ font size This changes the font size only. With no argument it shows the current size. +font info +~~~~~~~~~ + +This shows glyph rendering statistics. The TrueType driver uses a pre-allocated +buffer to avoid malloc/free for each character rendered. This command shows how +many glyphs were rendered using the buffer vs. falling back to malloc. + +This subcommand requires CONFIG_VIDEO_GLYPH_STATS=y. + Examples -------- @@ -52,7 +62,7 @@ Examples => font select cantoraone_regular 20 => -This shows an example of selecting a bitmap font Truetype is active:: +This shows an example of selecting a bitmap font when Truetype is active:: => font list 8x16 @@ -61,12 +71,20 @@ This shows an example of selecting a bitmap font Truetype is active:: cantoraone_regular => font sel 8x16 +This shows glyph rendering statistics:: + + => font info + glyph buffer renders: 32705 + glyph malloc renders: 0 + Configuration ------------- The command is only available if CONFIG_CONSOLE_TRUETYPE=y. +CONFIG_VIDEO_GLYPH_STATS enables tracking of glyph-rendering statistics. + Return value ------------ diff --git a/drivers/video/Kconfig b/drivers/video/Kconfig index 446ce51fe27..c09341a08dc 100644 --- a/drivers/video/Kconfig +++ b/drivers/video/Kconfig @@ -247,6 +247,30 @@ config CONSOLE_TRUETYPE_MAX_METRICS font metrics which are expensive to regenerate each time the font size changes. +config CONSOLE_TRUETYPE_GLYPH_BUF_SIZE + int "TrueType per-character glyph buffer size" + depends on CONSOLE_TRUETYPE + default 4096 + help + This sets the size of a pre-allocated buffer for rendering glyph + bitmaps. If a glyph fits within this buffer, no malloc/free is + needed during character rendering, which reduces memory fragmentation + and improves performance. Glyphs larger than this size fall back to + malloc. + + The default of 4096 bytes supports characters up to 64x64 pixels, + which covers most font sizes. Increase this if using very large fonts. + +config VIDEO_GLYPH_STATS + bool "Track glyph rendering statistics" + depends on CONSOLE_TRUETYPE + default y if SANDBOX + help + Track cumulative glyph rendering statistics in global_data, so they + persist across video device rebinds. This allows seeing the total + count of glyphs rendered using the pre-allocated buffer vs. malloc + fallback. Use 'font info' to view the statistics. + config SYS_WHITE_ON_BLACK bool "Display console as white on a black background" default y if ARCH_AT91 || ARCH_EXYNOS || ARCH_ROCKCHIP || ARCH_TEGRA || X86 || ARCH_SUNXI diff --git a/drivers/video/console_truetype.c b/drivers/video/console_truetype.c index 7f5a2262b17..f335c9f1148 100644 --- a/drivers/video/console_truetype.c +++ b/drivers/video/console_truetype.c @@ -180,6 +180,9 @@ struct console_tt_metrics { * @pos_start: Value of pos_ptr when the cursor is at the start of the text * being entered by the user * @pos_count: Maximum value reached by pos_ptr (initially zero) + * @glyph_buf: Pre-allocated buffer for rendering glyphs. If a glyph fits, + * this avoids malloc/free per character. Allocated lazily after + * relocation to avoid using early malloc space. */ struct console_tt_priv { struct console_tt_metrics *cur_met; @@ -190,6 +193,7 @@ struct console_tt_priv { struct video_fontdata *cur_fontdata; int pos_start; int pos_count; + u8 *glyph_buf; }; /** @@ -365,6 +369,7 @@ static int console_truetype_putc_xy(struct udevice *dev, uint x, uint y, int advance; void *start, *end, *line; int row, kern; + bool use_buf; /* Use fixed font if selected */ if (priv->cur_fontdata) @@ -440,13 +445,53 @@ static int console_truetype_putc_xy(struct udevice *dev, uint x, uint y, * information into the render, which will return a 8-bit-per-pixel * image of the character. For empty characters, like ' ', data will * return NULL; + * + * Use the pre-allocated glyph buffer if large enough, falling back to + * malloc for oversized glyphs. This avoids alloc/free traffic for + * normal characters. */ - data = stbtt_GetCodepointBitmapSubpixel(font, met->scale, met->scale, - x_shift, 0, cp, &width, &height, - &xoff, &yoff); - if (!data) + { + int ix0, iy0, ix1, iy1; + + stbtt_GetCodepointBitmapBoxSubpixel(font, cp, met->scale, + met->scale, x_shift, 0, + &ix0, &iy0, &ix1, &iy1); + width = ix1 - ix0; + height = iy1 - iy0; + xoff = ix0; + yoff = iy0; + } + if (!width || !height) return width_frac; + /* + * Use the pre-allocated buffer if available and large enough. Allocate + * it lazily, but only after relocation to avoid using early malloc. + */ + use_buf = false; + if (xpl_phase() >= PHASE_BOARD_R && + width * height <= CONFIG_CONSOLE_TRUETYPE_GLYPH_BUF_SIZE) { + if (!priv->glyph_buf) { + priv->glyph_buf = + malloc(CONFIG_CONSOLE_TRUETYPE_GLYPH_BUF_SIZE); + } + if (priv->glyph_buf) { + data = priv->glyph_buf; + use_buf = true; + gd_inc_glyph_buf_count(); + } + } + if (!use_buf) { + data = malloc(width * height); + if (!data) + return width_frac; + gd_inc_glyph_malloc_count(); + } + + stbtt_MakeCodepointBitmapSubpixel(font, data, width, height, width, + met->scale, met->scale, x_shift, 0, + cp); + /* Figure out where to write the character in the frame buffer */ bits = data; start = vid_priv->fb + y * vid_priv->line_length + @@ -534,7 +579,8 @@ static int console_truetype_putc_xy(struct udevice *dev, uint x, uint y, break; } default: - free(data); + if (!use_buf) + free(data); return -ENOSYS; } @@ -547,7 +593,8 @@ static int console_truetype_putc_xy(struct udevice *dev, uint x, uint y, width, height); - free(data); + if (!use_buf) + free(data); return width_frac; } diff --git a/include/asm-generic/global_data.h b/include/asm-generic/global_data.h index cff9066de53..d91103dfe00 100644 --- a/include/asm-generic/global_data.h +++ b/include/asm-generic/global_data.h @@ -365,6 +365,16 @@ struct global_data { */ ulong video_bottom; #endif +#if CONFIG_IS_ENABLED(VIDEO_GLYPH_STATS) + /** + * @glyph_buf_count: glyphs rendered using pre-allocated buffer + */ + uint glyph_buf_count; + /** + * @glyph_malloc_count: glyphs rendered using malloc fallback + */ + uint glyph_malloc_count; +#endif #ifdef CONFIG_BOOTSTAGE /** * @bootstage: boot stage information @@ -637,6 +647,18 @@ static_assert(sizeof(struct global_data) == GD_SIZE); #define gd_pager_page_len() 0 #endif +#if CONFIG_IS_ENABLED(VIDEO_GLYPH_STATS) +#define gd_glyph_buf_count() gd->glyph_buf_count +#define gd_inc_glyph_buf_count() gd->glyph_buf_count++ +#define gd_glyph_malloc_count() gd->glyph_malloc_count +#define gd_inc_glyph_malloc_count() gd->glyph_malloc_count++ +#else +#define gd_glyph_buf_count() 0 +#define gd_inc_glyph_buf_count() +#define gd_glyph_malloc_count() 0 +#define gd_inc_glyph_malloc_count() +#endif + /** * enum gd_flags - global data flags * diff --git a/test/cmd/font.c b/test/cmd/font.c index adfeebe920d..1bdc14e0250 100644 --- a/test/cmd/font.c +++ b/test/cmd/font.c @@ -98,3 +98,23 @@ static int font_test_base(struct unit_test_state *uts) } FONT_TEST(font_test_base, UTF_SCAN_PDATA | UTF_SCAN_FDT | UTF_CONSOLE | UTF_DM); + +/* Test 'font info' command */ +static int font_test_info(struct unit_test_state *uts) +{ + int buf_count, malloc_count; + + if (!CONFIG_IS_ENABLED(VIDEO_GLYPH_STATS)) + return -EAGAIN; + + /* read the results first so that they don't change while printing */ + buf_count= gd_glyph_buf_count(); + malloc_count = gd_glyph_malloc_count(); + ut_assertok(run_command("font info", 0)); + ut_assert_nextline("glyph buffer renders: %u", buf_count); + ut_assert_nextline("glyph malloc renders: %u", malloc_count); + ut_assert_console_end(); + + return 0; +} +FONT_TEST(font_test_info, UTF_CONSOLE); From patchwork Wed Dec 10 00:06:58 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 864 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=1765325319; bh=wQZ7w48wH0jwq1d+Um5e28PPWa6scFt/cmUOXRDBfuw=; 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=Q8Jii9KPLw1d1Dc9GwzILhahdITvXq3QATBwYiH39UtXthUbrn+QnLtOfP+PNOTDJ 6kd8ZLCzumwxgUN0G+Qdepd9/JOZcaZinCND8oGLReIkwsABo1DpYv8A7is7ZezjhL /X2yRo5kdWTokrwwFlGqayprlu7R9VVWwMyVso2ngMyC4qlKHNwGvBm6tIvBVyn/An G/QFzWOf0Gj4798zI4OUDTgpgUK78AL2Tmok0fFyraKusWWSCoPRAZiImp3X/8+zae S622fZf89DEWBbOgUHDQGFLhdY8VhfPEX51cl7ymghkR+yRrqaKDeOqCmN6MSoC14H 0dKO58JR5cRxw== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 9862C68A26 for ; Tue, 9 Dec 2025 17:08:39 -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 bVgywI3v7925 for ; Tue, 9 Dec 2025 17:08:39 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325319; bh=wQZ7w48wH0jwq1d+Um5e28PPWa6scFt/cmUOXRDBfuw=; 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=Q8Jii9KPLw1d1Dc9GwzILhahdITvXq3QATBwYiH39UtXthUbrn+QnLtOfP+PNOTDJ 6kd8ZLCzumwxgUN0G+Qdepd9/JOZcaZinCND8oGLReIkwsABo1DpYv8A7is7ZezjhL /X2yRo5kdWTokrwwFlGqayprlu7R9VVWwMyVso2ngMyC4qlKHNwGvBm6tIvBVyn/An G/QFzWOf0Gj4798zI4OUDTgpgUK78AL2Tmok0fFyraKusWWSCoPRAZiImp3X/8+zae S622fZf89DEWBbOgUHDQGFLhdY8VhfPEX51cl7ymghkR+yRrqaKDeOqCmN6MSoC14H 0dKO58JR5cRxw== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 3C68A68974 for ; Tue, 9 Dec 2025 17:08:39 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325315; bh=kkD2ElO3qfEZDNGWoXBqLTDMvg/UOdoEJSzQ+Ip6PjE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=I7DreutgLOxSzdkH1CXUI3L+cMAH28dxXtej+cldtJsTtRhMGCOi0AQcZDNHMlP/H CiGPc8Mg4i8T4yc3XTZ2Lx63ctMDFJ/VVAslgbEtT8xa3qmwZLgCLNLG3lbXKeUIYR +W3uNcRQ173xpC8SH6NdRQkJ0OtS4os5G/7BCz6moiWCRoAxnt9q144WfLCQ7Wn3IR dHGdfm+ub2G0U6PJH9mq4UEOkg2EXHtoLO/7wibtHO4ycXtrM4FON59S0nY1kZEIfa hiRt0HNT7gre2rlC5Cr0YhQjZ1ZNTzkSmU9jdlEAhNtq+zG24Sz0GhlJdnCI2k5cFn xewUAm2XJ35OQ== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 4EC08689EC; Tue, 9 Dec 2025 17:08: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 10026) with ESMTP id BiQ00YfaxJbL; Tue, 9 Dec 2025 17:08:35 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325309; bh=a/hij0xrZsgNHzc0SzI42Te6zxWWuoQmgdTNMZeZH60=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WHA7EB/T0ON9ZYjCtIklG32M9yeupjkTxqnrBaoMAihxbEBL1uel0qyFAfxYnwOvx kgVfhMJ4zqLgTlgMd68/W+/g0S9kwuIeUO0V1lpIa85xhW6xeSkQOb5T6tlyYgEjtl nQZhlrjE7dwhu/tGgp0j+iJVVjvJ8iHe94wLir/FL2sAS3HMOJE4ebYYvqRbdYjRWp 1Oetf5V3KEx10zV5E9ROLfObWxDVCW0/6LU5DPdGF15/8zAc2OdrnvDBv7ha4ULnam +KC42dCfP6qvk04kgWkL9+b1VIMD3DzNGWRpRj9Yt2iF5YFMVe6hKAuCbjFgJCKASe AzWczn5GtNZcA== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id 64A4F68947; Tue, 9 Dec 2025 17:08:29 -0700 (MST) From: Simon Glass To: U-Boot Concept Date: Tue, 9 Dec 2025 17:06:58 -0700 Message-ID: <20251210000737.180797-8-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: EYK5CIPKW7D2V7BXZRO4IEMRLI6YC5L3 X-Message-ID-Hash: EYK5CIPKW7D2V7BXZRO4IEMRLI6YC5L3 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 07/35] lib: Add format_size() to format sizes as strings 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 Refactor print_size() to use a new format_size() helper that formats a size into a buffer. This allows callers to get the formatted string without printing it directly. The format_size() function is only exported in U-Boot proper (controlled by CONFIG_LIB_FORMAT_SIZE) to avoid code-size impact in SPL/TPL where it remains static. Co-developed-by: Claude Signed-off-by: Simon Glass --- include/display_options.h | 14 ++++++++++++++ lib/Kconfig | 7 +++++++ lib/display_options.c | 34 ++++++++++++++++++++++++---------- test/lib/test_print.c | 14 ++++++++++++++ 4 files changed, 59 insertions(+), 10 deletions(-) diff --git a/include/display_options.h b/include/display_options.h index f3bdff172eb..5941d6bd37a 100644 --- a/include/display_options.h +++ b/include/display_options.h @@ -11,6 +11,20 @@ #include +#if CONFIG_IS_ENABLED(LIB_FORMAT_SIZE) +/** + * format_size() - Format a size with a suffix + * + * Format sizes as "xxx KiB", "xxx.y KiB", "xxx MiB", "xxx.y MiB", + * xxx GiB, xxx.y GiB, etc as needed + * + * @buf: Buffer to write to (must be at least 12 bytes) + * @size: Size to format + * Return: @buf + */ +char *format_size(char *buf, uint64_t size); +#endif + /** * print_size() - Print a size with a suffix * diff --git a/lib/Kconfig b/lib/Kconfig index d7f791f77f3..79a75f98446 100644 --- a/lib/Kconfig +++ b/lib/Kconfig @@ -36,6 +36,13 @@ config BACKTRACE stack. This is currently only available on sandbox. The backtrace command can be used to print the backtrace. +config LIB_FORMAT_SIZE + bool + default y + help + Enables the format_size() helper which formats a size as a + human-readable string. + config BCH bool "Enable Software based BCH ECC" help diff --git a/lib/display_options.c b/lib/display_options.c index 7f9dfcc43ee..3aee08c7b00 100644 --- a/lib/display_options.c +++ b/lib/display_options.c @@ -98,7 +98,11 @@ void print_freq(uint64_t freq, const char *s) printf(" %cHz%s", c, s); } -void print_size(uint64_t size, const char *s) +#if CONFIG_IS_ENABLED(LIB_FORMAT_SIZE) +char *format_size(char *buf, uint64_t size) +#else +static char *format_size(char *buf, uint64_t size) +#endif { unsigned long m = 0, n; uint64_t f; @@ -107,6 +111,7 @@ void print_size(uint64_t size, const char *s) char c = 0; unsigned int i; + /* Find the best unit to display */ for (i = 0; i < ARRAY_SIZE(names); i++, d -= 10) { if (size >> d) { c = names[i]; @@ -116,12 +121,12 @@ void print_size(uint64_t size, const char *s) if (!c) { /* - * SPL tiny-printf is not capable for printing uint64_t. - * We have just checked that the size is small enought to fit + * SPL tiny-printf is not capable of printing uint64_t. + * We have just checked that the size is small enough to fit * unsigned int safely. */ - printf("%u Bytes%s", (unsigned int)size, s); - return; + sprintf(buf, "%u Bytes", (unsigned int)size); + return buf; } n = size >> d; @@ -143,11 +148,20 @@ void print_size(uint64_t size, const char *s) } } - printf ("%lu", n); - if (m) { - printf (".%ld", m); - } - printf (" %ciB%s", c, s); + if (m) + sprintf(buf, "%lu.%ld %ciB", n, m, c); + else + sprintf(buf, "%lu %ciB", n, c); + + return buf; +} + +void print_size(uint64_t size, const char *s) +{ + char buf[12]; + + format_size(buf, size); + printf("%s%s", buf, s); } #define MAX_LINE_LENGTH_BYTES 64 diff --git a/test/lib/test_print.c b/test/lib/test_print.c index cd7f3f85769..f9b04a3e3cb 100644 --- a/test/lib/test_print.c +++ b/test/lib/test_print.c @@ -68,3 +68,17 @@ static int lib_test_print_size(struct unit_test_state *uts) return 0; } LIB_TEST(lib_test_print_size, UTF_CONSOLE); + +static int lib_test_format_size(struct unit_test_state *uts) +{ + char buf[12]; + + ut_asserteq_str("321 Bytes", format_size(buf, 321)); + ut_asserteq_str("4.2 KiB", format_size(buf, 4321)); + ut_asserteq_str("53 KiB", format_size(buf, 54321)); + ut_asserteq_str("1 GiB", format_size(buf, 1073741824)); + ut_asserteq_str("49.4 TiB", format_size(buf, 54321987654321)); + + return 0; +} +LIB_TEST(lib_test_format_size, 0); From patchwork Wed Dec 10 00:06:59 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 865 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=1765325324; bh=MQ3McUJ6v+muWQJVt2mTlBYYSeQcDNQAzNOiET8+524=; 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=FC39N6HsHPuu8LyRDJtZE3XhmMDxlEd+WuH8rN26ZlKmjPWm+3gSwRkY0V4gVTeQH c0X+HcBuEPM1ohp2qDDUnvKB+Eutj9kyw8AC4Ge5QvbBCJhGtpjftZe38jKfQxNyb9 P1KafBzxRJxFPJqkXUY65yvlX6FgkY4G4qy5SJi0cn/cFGm7s5Bl8+PXC8MWitOKvH I5pG7uVswlt/MniDI8DaitaA57z9jXG+HqiwkB/LFaI8UAdfykc0KTbk0QeacF4utE q3CSsbovxlKPECA0aO4AddIyLonsdo5jeRJmp+bdpW2LKw0c18kxMe/ZEoue6/P9C3 0kYVIVcrV0gow== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id F1E1368974 for ; Tue, 9 Dec 2025 17:08:44 -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 uSdZcczH2wse for ; Tue, 9 Dec 2025 17:08:44 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325324; bh=MQ3McUJ6v+muWQJVt2mTlBYYSeQcDNQAzNOiET8+524=; 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=FC39N6HsHPuu8LyRDJtZE3XhmMDxlEd+WuH8rN26ZlKmjPWm+3gSwRkY0V4gVTeQH c0X+HcBuEPM1ohp2qDDUnvKB+Eutj9kyw8AC4Ge5QvbBCJhGtpjftZe38jKfQxNyb9 P1KafBzxRJxFPJqkXUY65yvlX6FgkY4G4qy5SJi0cn/cFGm7s5Bl8+PXC8MWitOKvH I5pG7uVswlt/MniDI8DaitaA57z9jXG+HqiwkB/LFaI8UAdfykc0KTbk0QeacF4utE q3CSsbovxlKPECA0aO4AddIyLonsdo5jeRJmp+bdpW2LKw0c18kxMe/ZEoue6/P9C3 0kYVIVcrV0gow== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 9D27768947 for ; Tue, 9 Dec 2025 17:08:44 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325320; bh=NMwKenp0lHoltW2sWnZL9eoXEykQlpDBVotNnhQ3MlE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=uI1bPyllIAb6JfSt5JBIf8oYUaJsnQTcCImmlUQh2x/FG89sb9Rv7SmaJGmjWYNxF TqN5qEmK27txyCgOqUolmgzHV3GFzDuCetEa7uxV875v2L/66VxB9HcrLQ9L5Dzzuk ibdlJiPt6YPNAtNn57MLJn4oDjWBvVpl7Imn4Sw0BnYWuYNuvvn2Lg3RHv17qm24fu itmUFSoplfXvIK9BwPYajIsXJ9g8x39050TOioZ3myF3R+Bos+B+NC9APNdMBg4jls VGSjMRWZv+PrlUHCWkNDigg4Ac5koCinPR2pM/liD2AXBiZvxEksWktZaq9i5UInPE oy4fcjpmE0WlA== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 409E168A22; Tue, 9 Dec 2025 17:08:40 -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 vKMcAKhkciuH; Tue, 9 Dec 2025 17:08:40 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325314; bh=Oc4XW6U3l/Abzn7VyPCNREWFYoMbVhaPkj9St5V4LfE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=v1US6jCG1UM6pseXUcFeFi5fvcUzpE/aswj604uBwZiLwfjb1llOoibcMUMdxkZPG NVUK1G4VlVWR8zhfcUubaBtjjMGjcosaCZddDck/3Z4KUO62d7AT266rVj+CDMufcZ rkjV7yE0nTkZ39nc85a09VbrlejcmlOO4L1EJ1b7R5v9zkFugUZGWjPjuPyFwbVnZA 8zRIrCDfIxi6Udg1z4jT4MVGHh+ds9/t9iqQYwloCQxPaaaG5ntV1YyNJbRM171oqx gb4UvCu/Kdhw/pWAUCbqL3G/b60/SZ5vGd1Pk7KLHwX9v1MlOz2M7mWN++9W+tnV0Y CyeBiD+KV8ciw== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id 270C968972; Tue, 9 Dec 2025 17:08:34 -0700 (MST) From: Simon Glass To: U-Boot Concept Date: Tue, 9 Dec 2025 17:06:59 -0700 Message-ID: <20251210000737.180797-9-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: WG2ONOTGUHRTLJQPLOECTUQ74OLFRVLO X-Message-ID-Hash: WG2ONOTGUHRTLJQPLOECTUQ74OLFRVLO 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 08/35] test: Add ut_asserteq_regex() for regex pattern matching 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 new assertion macro ut_asserteq_regex() that checks if a string matches a regular expression pattern using the SLRE library. This is useful for tests where exact string-matching is difficult, such as when output contains line numbers or other variable content. Use a helper function ut_check_regex() to avoid including slre.h in the header. Co-developed-by: Claude Signed-off-by: Simon Glass --- doc/develop/tests_writing.rst | 5 +++++ include/test/ut.h | 29 +++++++++++++++++++++++++++++ test/ut.c | 30 ++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+) diff --git a/doc/develop/tests_writing.rst b/doc/develop/tests_writing.rst index 31454aa4819..43756989d43 100644 --- a/doc/develop/tests_writing.rst +++ b/doc/develop/tests_writing.rst @@ -422,6 +422,11 @@ ut_asserteq_addr(expr1, expr2) Assert that two addresses (converted from pointers via map_to_sysmem()) are equal +ut_asserteq_regex(pattern, str) + Assert that a string matches a regular expression pattern. Uses the SLRE + library for regex matching. Useful when exact matching is fragile, e.g. + when output contains line numbers or variable content. + Pointer assertions ~~~~~~~~~~~~~~~~~~ diff --git a/include/test/ut.h b/include/test/ut.h index 70eaaea5e0e..a2b42cdf414 100644 --- a/include/test/ut.h +++ b/include/test/ut.h @@ -13,6 +13,9 @@ #include #include +/* Size of error buffer for ut_check_regex() */ +#define UT_REGEX_ERR_SIZE 256 + struct unit_test_state; /** @@ -41,6 +44,16 @@ void ut_failf(struct unit_test_state *uts, const char *fname, int line, const char *func, const char *cond, const char *fmt, ...) __attribute__ ((format (__printf__, 6, 7))); +/** + * ut_check_regex() - Check if a string matches a regex pattern + * + * @pattern: Regular expression pattern + * @str: String to match against + * @err: Buffer to hold error message on failure (UT_REGEX_ERR_SIZE bytes) + * Return: 0 if match, -EINVAL if pattern is invalid, -ENOENT if no match + */ +int ut_check_regex(const char *pattern, const char *str, char *err); + /** * ut_check_console_line() - Check the next console line against expectations * @@ -254,6 +267,22 @@ int ut_check_console_dump(struct unit_test_state *uts, int total_bytes); __ret; \ }) +/* Assert that a string matches a regex pattern */ +#define ut_asserteq_regex(pattern, str) ({ \ + const char *_pattern = (pattern), *_str = (str); \ + char _err[UT_REGEX_ERR_SIZE]; \ + int __ret = 0; \ + \ + __ret = ut_check_regex(_pattern, _str, _err); \ + if (__ret) { \ + ut_failf(uts, __FILE__, __LINE__, __func__, \ + #pattern " matches " #str, "%s", _err); \ + if (!uts->soft_fail) \ + return CMD_RET_FAILURE; \ + } \ + __ret; \ +}) + /* Assert that two memory areas are equal */ #define ut_asserteq_mem(expr1, expr2, len) ({ \ const u8 *_val1 = (u8 *)(expr1), *_val2 = (u8 *)(expr2); \ diff --git a/test/ut.c b/test/ut.c index a16fdfb3a93..b4f2a8bf40f 100644 --- a/test/ut.c +++ b/test/ut.c @@ -6,7 +6,10 @@ */ #include +#include #include +#include +#include #ifdef CONFIG_SANDBOX #include #endif @@ -38,6 +41,33 @@ void ut_failf(struct unit_test_state *uts, const char *fname, int line, uts->cur.fail_count++; } +int ut_check_regex(const char *pattern, const char *str, char *err) +{ + struct slre slre; + + if (!pattern || !str) { + snprintf(err, UT_REGEX_ERR_SIZE, + "NULL value: pattern=%s, str=%s", + pattern ? pattern : "(null)", + str ? str : "(null)"); + return -EINVAL; + } + + if (!slre_compile(&slre, pattern)) { + snprintf(err, UT_REGEX_ERR_SIZE, + "Invalid regex '%s': %s", pattern, slre.err_str); + return -EINVAL; + } + + if (!slre_match(&slre, str, strlen(str), NULL)) { + snprintf(err, UT_REGEX_ERR_SIZE, + "No match: pattern '%s', str '%s'", pattern, str); + return -ENOENT; + } + + return 0; +} + ulong ut_check_free(void) { struct mallinfo info = mallinfo(); From patchwork Wed Dec 10 00:07:00 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 866 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=1765325329; bh=1bON2oYSSLHSnn9N5rbQHhvY8lPrncdfpBHIWinsgPg=; 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=s111+8wB4WobJErQpFpvlJXOOqbC0RrH1SFR2A8mncQSF+2SEFLcGgB1EblSQrX03 oQR+1bZKfO1Y4TlAcegrAbfjEcRrJXwW1bF50sA8TJPU8BwKh86dMezRVj0DfEnzGK 3nX8SgYavZNMBFG6Qr/f6/MmGtCbdoEGDX6/CuAlJD/08nspbZO8N7Iz1JUU3CKC+n PLztA0q7M8K/8WSy43L6a0dkTlUhESbWuvS+QQDodkkly7AD8Oox7NbfnqPPxB2AQX RiBfecMF6SWiJmiewVSY7lB2BcKEbeyKPmYd2yvWEHCy8Ssks82LkdnyrOxawXub0C 6NRnUdxuRSh1w== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 524B5687A7 for ; Tue, 9 Dec 2025 17:08:49 -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 RBewbC6a8dGX for ; Tue, 9 Dec 2025 17:08:49 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325328; bh=1bON2oYSSLHSnn9N5rbQHhvY8lPrncdfpBHIWinsgPg=; 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=uSqVukyVNPpIm2Y+8VyVrTj8k8crId7VO9h06J8rhrpggNc5GBnu2LNm3o1kq8Kwu ZNccfVXoSCE2vfbg04112QLpVOzI+i7p3fQEZZYCen372Fg/GdC0Er5xmTgp/oWxnu kQUINMW66JbkT1dJ8+K8FCICYpL5yU03fbgjdlTY6qlKSb5YEpFnjyb5pzi3gu7foL eabw3wvyNdQayb7pkqFXWynWIjFJDpLeKq2x+0iugbUl8xLWBq1thb1yBF17M0/5/s WGskRAUdtZs7m4QNAjpKmlWbUDmwayL7qKl/f+4jlQvqeWMvwLjQ7+0imx9TNh+GT7 RPlNCmQXcENEQ== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id EC7B0689EA for ; Tue, 9 Dec 2025 17:08:48 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325322; bh=EUx9prZPwsX1mN/vSWheiJuD+xdyAs01A/LquhD0mmw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=K1rJVdcnU7aH2EFcBgWz2pWGB71WdSbdmBNGa/Op87GQZK242gbs8YKYOro/FnkJM FwHADC+OizCro3RRzj7ZQ2DD6T7IH9ojacNeQWDqo8zDpkXJncJyQ8esGqUoMmFbTR pMbJIOR/4QFqiw8Vq3bnRx1+uoe19w3YTEP+6J+ZzXhfWeW5mIAc9XBZQCKNVtrdWI 2yzkx9xBOmsjb6FTA28kYjjvX2ZR7LccrLkxh18oaX2BqWtDYK8Ozc0KQ0CUoHBpjT cxSXojzAwG3BLRnj8c/hsaCQKYWfx6USuF1BifTLtxa0u+o9KatfwtgKignts0F8La En+X+B2mwPO7A== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id C59B768947; Tue, 9 Dec 2025 17:08:42 -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 vUb3Ht_v8Fwn; Tue, 9 Dec 2025 17:08:42 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325321; bh=1uW3gfYRdfoxufcJkN1x7b98/ijcFIFHYm6I2RzegT4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=oqeJFwJUrKpMotPhhnUlghG1BWf6Gcby0SNXgge0ZKGlHjo9hdohvCuxNQIYIww3o K8OOcBUsNqabVBRqwq/X12iJqi6JvPuxzJsSUqBeyzc0c1p2qdB3i+Pf94oa3O8bC/ NoBUv8eSvsAa3OS8upJ3oN1LhTK9cvH0zOC6Qr9gbywNgq5wwxwX+lKxLOIVsQfDwb gXONXP272d7gY9OmAGXdIQJKMfjD2qzJwSVVEi7SkDHWtrZtaSWbvM9XMmJRg+Hpsd kwc4tJQ8Pjvi88JK5Ep6zaUzZLaPHdWGlEmi20MCsuaWtIqh5g9or64zyqVMiOSgbW ObXCrogcNImtQ== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id A24E7687A7; Tue, 9 Dec 2025 17:08:40 -0700 (MST) From: Simon Glass To: U-Boot Concept Date: Tue, 9 Dec 2025 17:07:00 -0700 Message-ID: <20251210000737.180797-10-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: WKBSGOLPIWNAERMRSFNYDNM3JBF72OPV X-Message-ID-Hash: WKBSGOLPIWNAERMRSFNYDNM3JBF72OPV 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 09/35] test: Show the required size when console-record overflows 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 the console-record buffer overflows, show both the current buffer size and the size needed. This helps the user know what value to set for CONFIG_CONSOLE_RECORD_OUT_SIZE. Add a console_out_ovf field to global_data to track the number of bytes that could not be written due to overflow. Co-developed-by: Claude Signed-off-by: Simon Glass --- common/console.c | 16 +++++++++++----- include/asm-generic/global_data.h | 14 ++++++++++++++ test/ut.c | 15 +++++++++++++-- 3 files changed, 38 insertions(+), 7 deletions(-) diff --git a/common/console.c b/common/console.c index c033d72486a..da1125cfa98 100644 --- a/common/console.c +++ b/common/console.c @@ -36,21 +36,26 @@ static void console_record_putc(const char c) { if (!(gd->flags & GD_FLG_RECORD)) return; - if (gd->console_out.start && - !membuf_putbyte((struct membuf *)&gd->console_out, c)) + if (gd->console_out.start && + !membuf_putbyte((struct membuf *)&gd->console_out, c)) { gd->flags |= GD_FLG_RECORD_OVF; + gd->console_out_ovf++; + } } static void console_record_puts(const char *s) { if (!(gd->flags & GD_FLG_RECORD)) return; - if (gd->console_out.start) { + if (gd->console_out.start) { int len = strlen(s); + int written; - if (membuf_put((struct membuf *)&gd->console_out, s, len) != - len) + written = membuf_put((struct membuf *)&gd->console_out, s, len); + if (written != len) { gd->flags |= GD_FLG_RECORD_OVF; + gd->console_out_ovf += len - written; + } } } @@ -893,6 +898,7 @@ void console_record_reset(void) membuf_purge((struct membuf *)&gd->console_out); membuf_purge((struct membuf *)&gd->console_in); gd->flags &= ~GD_FLG_RECORD_OVF; + gd->console_out_ovf = 0; } int console_record_reset_enable(void) diff --git a/include/asm-generic/global_data.h b/include/asm-generic/global_data.h index d91103dfe00..4a197a2c230 100644 --- a/include/asm-generic/global_data.h +++ b/include/asm-generic/global_data.h @@ -347,6 +347,12 @@ struct global_data { * This buffer is used to collect output during console recording. */ struct membuf console_out; + /** + * @console_out_ovf: overflow byte count for console recording + * + * Number of bytes that could not be written due to buffer overflow. + */ + int console_out_ovf; /** * @console_in: input buffer for console recording * @@ -589,6 +595,14 @@ static_assert(sizeof(struct global_data) == GD_SIZE); #define gd_malloc_ptr() 0L #endif +#ifdef CONFIG_CONSOLE_RECORD +#define gd_console_out() (&gd->console_out) +#define gd_console_out_ovf() gd->console_out_ovf +#else +#define gd_console_out() NULL +#define gd_console_out_ovf() 0 +#endif + #if CONFIG_IS_ENABLED(UPL) #define gd_upl() gd->upl #define gd_set_upl(_val) gd->upl = (_val) diff --git a/test/ut.c b/test/ut.c index b4f2a8bf40f..94b09364687 100644 --- a/test/ut.c +++ b/test/ut.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #ifdef CONFIG_SANDBOX @@ -86,8 +87,18 @@ static int readline_check(struct unit_test_state *uts) ret = console_record_readline(uts->actual_str, sizeof(uts->actual_str)); if (ret == -ENOSPC) { - ut_fail(uts, __FILE__, __LINE__, __func__, - "Console record buffer too small - increase CONFIG_CONSOLE_RECORD_OUT_SIZE"); + if (IS_ENABLED(CONFIG_CONSOLE_RECORD)) { + int cur_size = membuf_size(gd_console_out()); + + ut_failf(uts, __FILE__, __LINE__, __func__, + "Console record buffer too small", + "CONFIG_CONSOLE_RECORD_OUT_SIZE=%#x, need %#x", + cur_size, cur_size + gd_console_out_ovf()); + } else { + ut_fail(uts, __FILE__, __LINE__, __func__, + "Console record buffer too small - increase " + "CONFIG_CONSOLE_RECORD_OUT_SIZE"); + } return ret; } else if (ret == -ENOENT) { strcpy(uts->actual_str, ""); From patchwork Wed Dec 10 00:07:01 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 867 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=1765325335; bh=O7wg8GZtwdBriiH2Rrii1lOrNWdt8qOj1dfpahGABdY=; 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=h8plMqJ4Oa20FHakBEFBTRs2LrxQCR4D/N6jmJe2EW5m0J49T2Wts317JbZjNF6nC yvx73k7itr3cojbG+zynMK9gBFUJCRnvlJ/J3IqHhORFMtrDNtCEp3h7J58gkxkUEn +fThzpNEe3Rvt0dQBrAk6Ppmvno0X5kHedR7NQTWdTnQxJ7GtKHuCVrrG/7weAI+3A 4Tnt9T2XUoub0YbGz0y8qy21MBYdl7Hx7QmJ19fh9tiNWwASNHXWusX/gR81c4TW/Y JXc6fcyEGw0NQyMluDnRwUKdFeQ1TWs4SUv9sTTzSi6rzyv3rm3Dl640SGQtKwSzD6 zUzWuo8v4rQ1w== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id D106368A48 for ; Tue, 9 Dec 2025 17:08:55 -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 X88KJKO-m-op for ; Tue, 9 Dec 2025 17:08:55 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325333; bh=O7wg8GZtwdBriiH2Rrii1lOrNWdt8qOj1dfpahGABdY=; 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=aLqbDCn9s9aIIXrNq8U+XP7PrylcQfCtcWDGz5YE7juJ3MP2i8OQZ2mGvk4DVyMqv LXkrBjK/tl0ytPmyAt/E3oRALD+JNG/OxgSdPXtbWW5oKoUO4+BLYjpNpjIj10k381 w7LrPK95Wj1bYdoHgOriMtNZrDzFZxWPHpnWwTwJr1wCszdHgDE/EgPaQLkwLi5+ac 7hXgSZBbxICSbJ/7OjA9/3Nja7WtK6bFjvq7gZeIJVxMi4bXWcRNYXshUS9L+Zh+5a MVtAOIJSw2CjoZMDaZ9vpaYVFqNFzrDbuDRDd7sBZgY5hZORp91MNq2BoMdXr4uK2t W6YX4oRzdsdSQ== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id DA86B687A7 for ; Tue, 9 Dec 2025 17:08:53 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325325; bh=wUFWwzhFMV4qIP+RDR91iCUR61mnirMM+rQA57rf/1s=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=iCjQ0aX7bnbV7P2YDHaLPO8TQPpd9nd+TvzCegc3rwDW3f2J1FXr6baJ8qkmtL8vR xCSL9PKi7NVWVs/XI6lvraj5BgJqBQDyM3pl3AHtYDYQ9ByJ6Q0Za1vfe8WkLovxiT okB7vASMWKZmCjXZ6ScPAXq7kLDWkEryyY0IwzwIo2GR/H9+3ue/Z83zPNBUmoXdDd Rx3kSfn88boBuikOQid1oZ+6r2nxqDt5jBoD+jy4RCOLu5nI6Vl6CBD8hU6qrVTFvr gzFP1QpBSe16+JCEBg3G7qYYbwbpW0Y6BUvL7hR4SMVv2lvx+y2KnSFrHxCUD43iTG 3Rc454f64TdAg== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 02106689EA; Tue, 9 Dec 2025 17:08:45 -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 czezRY44oKoi; Tue, 9 Dec 2025 17:08:44 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325323; bh=84ErNq1MZ6r2HGzyQKaMsLD6GkpCEa3XBDf6TPHS1MI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=sTa3faxPysEAER1AXWOQrQv1ypFHjR5fUMaFgo5x8Vp1ZOdq00/HDjNhkt8xiyRuR 6F/pJab3sSYhB2ZQ5My5bBeSjLJ9CQlT0PBHYSkbaKSmLsAYD0+WkCG4iMk/LUq+fO wAF86+BypU5GGQ9tI70xDiylY7TybSFoIZ75MOwjKzE5SVsu/hvxhKcSkva1A7g9oU xbJNhJPDw+mHHO6t9Z08zwnDeG73emzoqncFqODzpL1+URAB7AuKGJ5DXUYs8AbWro ddQH95ghIcokKoS9MMznm8yx3hdy4qfx7CH8aYg1Kjor/6CBCX/C5s1+B+yrUhHJiB PAAED+vmAI35Q== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id 1BB41687A7; Tue, 9 Dec 2025 17:08:43 -0700 (MST) From: Simon Glass To: U-Boot Concept Date: Tue, 9 Dec 2025 17:07:01 -0700 Message-ID: <20251210000737.180797-11-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: 3RIXQVMRDRAX3ZRR246HCHHYZ635BZ57 X-Message-ID-Hash: 3RIXQVMRDRAX3ZRR246HCHHYZ635BZ57 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 10/35] malloc: Use mcheck.h header instead of local declaration 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 Include and remove the local declaration of mcheck_on_ramrelocation() to use the proper header file. Co-developed-by: Claude Signed-off-by: Simon Glass --- common/board_f.c | 2 +- include/mcheck.h | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/common/board_f.c b/common/board_f.c index 879862865e1..a3e4c69d449 100644 --- a/common/board_f.c +++ b/common/board_f.c @@ -33,6 +33,7 @@ #include #include #include +#include #include #include #include @@ -723,7 +724,6 @@ static int reloc_bloblist(void) return 0; } -void mcheck_on_ramrelocation(size_t offset); static int setup_reloc(void) { if (!(gd->flags & GD_FLG_SKIP_RELOC)) { diff --git a/include/mcheck.h b/include/mcheck.h index bd506ae6291..b170acf6281 100644 --- a/include/mcheck.h +++ b/include/mcheck.h @@ -48,4 +48,7 @@ void mcheck_check_all(void); */ enum mcheck_status mprobe(void *__ptr); +/* Called during RAM relocation to reset the heap registry */ +void mcheck_on_ramrelocation(size_t offset); + #endif From patchwork Wed Dec 10 00:07:02 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 868 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=1765325336; bh=/FV4Bd8j25n98/wkXJa0fYn84GD8Je+cOW5YJQkGGCw=; 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=pIYhat25mOPBBAtXfIME+2nkQHoOnz/fo+ZOxiww5sIlEKn7De6WeB+YNpH7IzPJR gbbmvX/DVARmzDdBE8dmI08mmOaeHdzeb8q8hYHXOAyoAag4Sok77u672xJJ5qQcJ/ dKVWrNVKLevdULMAA00CUBZoX/ZSIA6D2wstbpFBtzJ/FJhnWsLFTMZY/R24+c6M75 kcE8+CsggNxO+QieeEwIW59WPWTjaf9RRALiAOXpg7e+oRF0idwQY7arnmxN+2CsQA DLCdfxcXphjcGAi8qoJ+3Y8XWX3cLT5/IDzXpuxsNvDomoOYy/8KxMddQR8RQhB++a ueuMtipGLMNxA== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 2140E689EA for ; Tue, 9 Dec 2025 17:08:56 -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 R9pY6l5sKfBM for ; Tue, 9 Dec 2025 17:08:56 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325335; bh=/FV4Bd8j25n98/wkXJa0fYn84GD8Je+cOW5YJQkGGCw=; 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=YtNtPR6CDm1OTdF4IaPK5wjkwLqV/dZv0qpIxfwOCBtORdFyactCdVslbDORfb+Ue k2iLgUXGIsCbvRErE0lLOLpI8m/SA/lArUvJ9TUmaUBwVi7ih8ccahkDuTv5zY47V3 pAK2cNSuHnlqeGF4k1JPRYlHGc8+LRSt7H+GF24nwEN6QmK/nfIsCLp8aY2wUljfMo 1yQ5dJTRsAooX90AmdPNFDcDcBoMHGZ4okO0EhNHsJ0U05RbTXSzXGhqgX4bPjOblC wvHyWg26TMr0JE8eICwkZcChetfMJb6hEr0HQHxRxgg/8tKQ1eAdKfPe1kmH2OMQHW TtpTZDwLlhuBw== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 7225A68A42 for ; Tue, 9 Dec 2025 17:08:55 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325331; bh=L6+L+iTRVQgT+g7gXuOfkO49+VE/GjuCFKiXPuyGjvs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=h/WGtQHtmMsNY64CMtX9a//2xHn1zpqjTALu0hmJRM8EEzEWQ7XP1VSdQGxzs6hSl HTlN830RSHz7GIi4W08zZP18F+edt1AQR9rSPk9QcffkvkBavGN560iDvcy00EvVrZ Fc1J2tSUrlP1qcbsBbuw7GFRJg4m/Pg8tloj+M9oRU8Zoo84Kr9adkOKeIZpf7fIjE TtPtc/oRoEWIRniJ0yvC5+mQIaIFukUlrRN/JHBqKzyZrdJbHYvbd9rOAv7DRMK1O1 3zsn0plJBppvT2HPk3jGI/T4fa9utcRkLLTTUG9yv5k6ScxnLWi6vimIPSfmOeYi7m zFyl9PJSJL6nA== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 0615868974; Tue, 9 Dec 2025 17:08:51 -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 n5ojw4bLyeBe; Tue, 9 Dec 2025 17:08:50 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325325; bh=1DdkXiGfeffYvHLHR6SOmSHeoam8jVSpOg6dRJgCbPo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=gX2HjQF6uWsaKShBSmKNngwQZAUUL9oSs7M3hrQXwm3VvdyvfXAJSSBsFfzVpzAuK lkKBB6KIeM/qcqj0w4wMF5LWNjEbu/OkX93RhEOD1SJNbewnthQq7kN1NcUevsh8el oEK/wfHll8y0w7V/H6fRdn2S3MYwKpPFWGIP42t/zGWSLR0gPlB0/QR4/Cm2dJEf13 1/qR7vp18IUw/a+Av3WByvIfvDUJF+uOeYw5HmGbMp1iFyS++fyRwxanY29C+RpFd6 cLQ7vfZVkjVFk0CwxXBMRbZf/+4KSOpjyiOo6bRiT2vcFZqcZfq0Xuufe6HgyaE7L8 lsJFTOyGLeTIQ== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id 59F3768A22; Tue, 9 Dec 2025 17:08:45 -0700 (MST) From: Simon Glass To: U-Boot Concept Date: Tue, 9 Dec 2025 17:07:02 -0700 Message-ID: <20251210000737.180797-12-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: OCZHJKCNQUSQ6W73O4I7244ERHVCEWV2 X-Message-ID-Hash: OCZHJKCNQUSQ6W73O4I7244ERHVCEWV2 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 X-Mailman-Version: 3.3.10 Precedence: list Subject: [Concept] [PATCH 11/35] malloc: Make mcheck respect REALLOC_ZERO_BYTES_FREES 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 The mcheck wrapper for realloc() unconditionally frees memory and returns NULL when size is 0. This differs from dlmalloc's default behaviour which returns a minimum-sized allocation unless REALLOC_ZERO_BYTES_FREES is defined. Make the mcheck wrapper respect the same REALLOC_ZERO_BYTES_FREES setting for consistent behavior with or without mcheck enabled. Signed-off-by: Simon Glass --- common/dlmalloc.c | 2 ++ test/common/malloc.c | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/common/dlmalloc.c b/common/dlmalloc.c index c1c9d8a8938..b46be1899f0 100644 --- a/common/dlmalloc.c +++ b/common/dlmalloc.c @@ -5923,11 +5923,13 @@ void dlfree(void *mem) { dlfree_impl(mcheck_free_prehook(mem)); } void *dlrealloc(void *oldmem, size_t bytes) { mcheck_pedantic_prehook(); +#ifdef REALLOC_ZERO_BYTES_FREES if (bytes == 0) { if (oldmem) dlfree(oldmem); return NULL; } +#endif if (oldmem == NULL) return dlmalloc(bytes); diff --git a/test/common/malloc.c b/test/common/malloc.c index b114267dd83..9fdc1789645 100644 --- a/test/common/malloc.c +++ b/test/common/malloc.c @@ -178,7 +178,7 @@ COMMON_TEST(common_test_realloc_null, 0); /* * Test realloc() with zero size * - * Standard dlmalloc behavior (without REALLOC_ZERO_BYTES_FREES or mcheck): + * Standard dlmalloc behavior (without REALLOC_ZERO_BYTES_FREES): * realloc(ptr, 0) returns a minimum-sized allocation. */ static int common_test_realloc_zero(struct unit_test_state *uts) From patchwork Wed Dec 10 00:07:03 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 869 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=1765325340; bh=7g6Wws3ziqELN5NwO+gFWmD9PyEARvaDNLxP+fG/bZY=; 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=UZlRiYycgirPt1oaHewb2sBmxu+m2iu+vurALh8at1uN2emrsYpfk86WilcbdxvZH ZG2R+81Hpj70XB16ihu7Dn18deA+/dF8vc95TdEH6bvUCbloT0gZoODvOFKOo6NMRd cQqMQrhf0irMZb4QLTJFi+3aWpgftCqaFLXMdK+C1zMFTku3HMrx9FlTCVcB0E+mhA hXe8WubPpEDeKXDMkNmFJBtwlyD95fpoXlKKNabDLZbijuHK1qw10N6jmZD1OJCr1a W6OcSYzUNtccl0U/KTHxXd0M2Ya0Vwgh3zOFz51EVzz/nGw7+SAQYyfglIGPZwTdP8 YpPJOXYmSzjRw== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 235BF689EC for ; Tue, 9 Dec 2025 17:09: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 i1mAxulFPYDX for ; Tue, 9 Dec 2025 17:09:00 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325340; bh=7g6Wws3ziqELN5NwO+gFWmD9PyEARvaDNLxP+fG/bZY=; 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=UZlRiYycgirPt1oaHewb2sBmxu+m2iu+vurALh8at1uN2emrsYpfk86WilcbdxvZH ZG2R+81Hpj70XB16ihu7Dn18deA+/dF8vc95TdEH6bvUCbloT0gZoODvOFKOo6NMRd cQqMQrhf0irMZb4QLTJFi+3aWpgftCqaFLXMdK+C1zMFTku3HMrx9FlTCVcB0E+mhA hXe8WubPpEDeKXDMkNmFJBtwlyD95fpoXlKKNabDLZbijuHK1qw10N6jmZD1OJCr1a W6OcSYzUNtccl0U/KTHxXd0M2Ya0Vwgh3zOFz51EVzz/nGw7+SAQYyfglIGPZwTdP8 YpPJOXYmSzjRw== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 11EE168972 for ; Tue, 9 Dec 2025 17:09:00 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325338; bh=0Z4WjKl2yGaDBMylUab79zeTXpas7D1wROEoNDvSfVE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=SEXNTAkBKXxmWx+hD3T7DtOrbHw+jT4spAImQwSnKJ33PAkVDMyfl6DmRhaySQSa1 5qf5ouuzKvi73q0L8u+1kUQG+kueLO1PRX6ttk86DGt8d7iaX9U6KVt9i19ZN76Y1G HlKOi8tTbgLMH6TK0yr4ldWC0K+73hHmonDqmCsDo47t9RBkdM8FDuvHevutqptmGm XAymvJMCcSv/rw9Up4nlv+tsqJCS20CI2v8eB5L1JlwptROXkFOCPVzj6sXPuoGrdm AWaeIki0nZ9gs6PQ5CL82mbVZa8F/x6JHQMNx14HABaE3S1CLd4d745Ps7E6UtGWcf jSf8PbF/M5Gqw== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 7429C68957; Tue, 9 Dec 2025 17:08: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 tWWMo4R2Dx4W; Tue, 9 Dec 2025 17:08:58 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325331; bh=0mfHZI35VEowy+WkJ5yV1YmqvYvP9DirA1lGdewBEfk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=wTB1mzvm5C3AfFjEgu+pR/D2Vi0ciVg3JCVpH2xXyIIhTHeaXBjcW4ZBZCCa2j2At 4CwfN8SjaxLs/ppi0nAIZXTif0fe/Z7Kw0E6TACToD/p8bltDAOUkkqpMrTUt7ZV0U cJeFZDoyrHy0C5SbjXUwJ3eZl5n2p4qorhjp9TQFAvcnhZQmiuWB8yom0rcCKDANAU hR7c3Ca/+OtrctepPCjSMicueRxjF2BxpCLTL/A5GiWkqOH8ltGTNe/g5++KxrbJ1I fN5LCbyy4PTM3ej8Ca/prSK4Dg0VKUL2xRQQOXUJWw1u0Wou2qDEWPc5t0+/F0bpTY ILFGbfC++fsDg== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id 2C3FB689EC; Tue, 9 Dec 2025 17:08:51 -0700 (MST) From: Simon Glass To: U-Boot Concept Date: Tue, 9 Dec 2025 17:07:03 -0700 Message-ID: <20251210000737.180797-13-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: N7RL5WWAWZGCEGKHYP5EAEWMMGMJ4CG4 X-Message-ID-Hash: N7RL5WWAWZGCEGKHYP5EAEWMMGMJ4CG4 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 12/35] backtrace: Use a static buffer in backtrace_ctx for symbols 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 Replace the dynamic allocation in os_backtrace_symbols() with a static buffer embedded in struct backtrace_ctx. This avoids malloc recursion when backtrace is called from within dlmalloc (e.g., for the upcoming mcheck caller-tracking). The API gets a complete rework as part of this: - Combine addrs[] and syms[] arrays into struct backtrace_frame with addr and sym fields - Store the strings in a unified buffer, with pointers from an array - Change os_backtrace_symbols() to take ctx pointer and fill sym_buf - Remove os_backtrace_symbols_free() as nothing needs freeing - Rename BACKTRACE_MAX to BACKTRACE_MAX_FRAMES Co-developed-by: Claude Signed-off-by: Simon Glass --- arch/sandbox/cpu/backtrace.c | 53 +++++++++++++++++------------------- arch/sandbox/lib/backtrace.c | 47 ++++++-------------------------- include/backtrace.h | 41 ++++++++++++++++++++++------ include/os.h | 22 ++++----------- lib/backtrace.c | 13 +++++---- test/lib/backtrace.c | 13 +++++---- 6 files changed, 86 insertions(+), 103 deletions(-) diff --git a/arch/sandbox/cpu/backtrace.c b/arch/sandbox/cpu/backtrace.c index 1f5a14ed541..2d0a0113cc0 100644 --- a/arch/sandbox/cpu/backtrace.c +++ b/arch/sandbox/cpu/backtrace.c @@ -16,6 +16,8 @@ #include #include +/* For BACKTRACE_MAX_FRAMES - include U-Boot's header after system headers */ +#include "../../../include/backtrace.h" /* libbacktrace state - created once and cached */ static struct backtrace_state *bt_state; @@ -77,46 +79,41 @@ static int bt_full_callback(void *data, uintptr_t pc, const char *fname, return 0; /* continue to get innermost frame for inlined functions */ } -char **os_backtrace_symbols(void *const *buffer, uint count) +void os_backtrace_symbols(struct backtrace_ctx *ctx) { + char *end = ctx->sym_buf + BACKTRACE_SYM_BUFSZ; struct backtrace_state *state; - char *str_storage; - char **strings; - uint i; + char *p = ctx->sym_buf; + int remaining, i; state = get_bt_state(); - /* Allocate array of string pointers plus space for strings */ - strings = malloc(count * sizeof(char *) + count * 256); - if (!strings) - return NULL; + for (i = 0; i < ctx->count; i++) { + struct backtrace_frame *frame = &ctx->frame[i]; + struct bt_sym_ctx sym_ctx; - /* String storage starts after the pointer array */ - str_storage = (char *)(strings + count); - - for (i = 0; i < count; i++) { - struct bt_sym_ctx ctx; + remaining = end - p; + if (remaining <= 1) { + /* No more space, leave remaining syms as NULL */ + frame->sym = NULL; + continue; + } - strings[i] = str_storage + i * 256; - ctx.buf = strings[i]; - ctx.size = 256; - ctx.found = 0; + frame->sym = p; + sym_ctx.buf = p; + sym_ctx.size = remaining; + sym_ctx.found = 0; if (state) { - backtrace_pcinfo(state, (uintptr_t)buffer[i], + backtrace_pcinfo(state, (uintptr_t)frame->addr, bt_full_callback, bt_error_callback, - &ctx); + &sym_ctx); } /* Fall back to address if no symbol found */ - if (!ctx.found) - snprintf(strings[i], 256, "%p", buffer[i]); - } + if (!sym_ctx.found) + snprintf(p, remaining, "%p", frame->addr); - return strings; -} - -void os_backtrace_symbols_free(char **strings) -{ - free(strings); + p += strlen(p) + 1; + } } diff --git a/arch/sandbox/lib/backtrace.c b/arch/sandbox/lib/backtrace.c index 073eb945622..62a1c9ee028 100644 --- a/arch/sandbox/lib/backtrace.c +++ b/arch/sandbox/lib/backtrace.c @@ -13,54 +13,23 @@ int backtrace_init(struct backtrace_ctx *ctx, uint skip) { + void *addrs[BACKTRACE_MAX_FRAMES]; uint i; - for (i = 0; i < BACKTRACE_MAX; i++) - ctx->syms[i] = NULL; /* +1 to skip this function */ - ctx->count = os_backtrace(ctx->addrs, BACKTRACE_MAX, skip + 1); + ctx->count = os_backtrace(addrs, BACKTRACE_MAX_FRAMES, skip + 1); + + for (i = 0; i < ctx->count; i++) { + ctx->frame[i].addr = addrs[i]; + ctx->frame[i].sym = NULL; + } return ctx->count; } int backtrace_get_syms(struct backtrace_ctx *ctx, char *buf, int size) { - char **raw_syms; - size_t total_len; - char *p; - uint i; - - raw_syms = os_backtrace_symbols(ctx->addrs, ctx->count); - if (!raw_syms) - return -ENOMEM; - - /* Calculate total buffer size needed */ - total_len = 0; - for (i = 0; i < ctx->count; i++) { - if (raw_syms[i]) - total_len += strlen(raw_syms[i]) + 1; - else - total_len += 1; /* empty string */ - } - - if ((size_t)size < total_len) { - os_backtrace_symbols_free(raw_syms); - return -ENOSPC; - } - - /* Copy strings into buffer */ - p = buf; - for (i = 0; i < ctx->count; i++) { - ctx->syms[i] = p; - if (raw_syms[i]) { - strcpy(p, raw_syms[i]); - p += strlen(raw_syms[i]) + 1; - } else { - *p++ = '\0'; - } - } - - os_backtrace_symbols_free(raw_syms); + os_backtrace_symbols(ctx); return 0; } diff --git a/include/backtrace.h b/include/backtrace.h index eece61e4d9a..7bb2ba68bec 100644 --- a/include/backtrace.h +++ b/include/backtrace.h @@ -9,21 +9,46 @@ #ifndef __BACKTRACE_H #define __BACKTRACE_H -#define BACKTRACE_MAX 100 -#define BACKTRACE_SYM_SIZE 128 -#define BACKTRACE_BUFSZ (BACKTRACE_MAX * BACKTRACE_SYM_SIZE) +/* Maximum number of stack frames that can be collected */ +#define BACKTRACE_MAX_FRAMES 100 + +/* Size of buffer for all symbol strings combined */ +#define BACKTRACE_SYM_BUFSZ (4 * 1024) + +/** + * struct backtrace_frame - a single stack frame in a backtrace + * + * @addr: return address for this frame + * @sym: pointer to symbol string in backtrace_ctx->sym_buf, or NULL if not + * yet resolved or if sym_buf ran out of space + */ +struct backtrace_frame { + void *addr; + char *sym; +}; /** * struct backtrace_ctx - context for backtrace operations * - * @addrs: array of return addresses - * @syms: array of symbol strings (NULL until backtrace_get_syms() called) - * @count: number of entries in addrs/syms arrays + * This structure holds all state for collecting and symbolising a backtrace. + * It should be declared static to avoid consuming stack space (~5KB). + * + * Lifecycle: + * 1. Call backtrace_init() - fills @frame[].addr with return addresses and + * sets @count. The @frame[].sym pointers are initialised to NULL. + * 2. Call backtrace_get_syms() - resolves addresses to symbol strings, + * writing them into @sym_buf and setting @frame[].sym pointers. + * 3. Access @frame[0..count-1] to read addresses and symbol strings. + * 4. Call backtrace_uninit() to release resources (currently a no-op). + * + * @frame: array of stack frames + * @count: number of valid entries in @frame + * @sym_buf: buffer holding NUL-terminated symbol strings packed consecutively */ struct backtrace_ctx { - void *addrs[BACKTRACE_MAX]; - char *syms[BACKTRACE_MAX]; + struct backtrace_frame frame[BACKTRACE_MAX_FRAMES]; unsigned int count; + char sym_buf[BACKTRACE_SYM_BUFSZ]; }; /** diff --git a/include/os.h b/include/os.h index ab4710fc265..3ea88230af3 100644 --- a/include/os.h +++ b/include/os.h @@ -588,27 +588,17 @@ void os_signal_action(int sig, unsigned long pc); */ uint os_backtrace(void **buffer, uint size, uint skip); -/** - * os_backtrace_symbols() - convert addresses to symbol strings - * - * Convert backtrace addresses to human-readable symbol strings. The returned - * array and strings are allocated with malloc() and must be freed with - * os_backtrace_symbols_free(). - * - * @buffer: array of addresses from os_backtrace() - * @count: number of addresses in buffer - * Return: array of symbol strings, or NULL on error - */ -char **os_backtrace_symbols(void *const *buffer, uint count); +struct backtrace_ctx; /** - * os_backtrace_symbols_free() - free symbol strings + * os_backtrace_symbols() - convert addresses to symbol strings * - * Free the array returned by os_backtrace_symbols(). + * Convert backtrace addresses to human-readable symbol strings. The symbol + * strings are written into ctx->sym_buf and ctx->syms pointers are set up. * - * @strings: array to free (may be NULL) + * @ctx: backtrace context with addrs and count already filled in */ -void os_backtrace_symbols_free(char **strings); +void os_backtrace_symbols(struct backtrace_ctx *ctx); /** * os_get_time_offset() - get time offset diff --git a/lib/backtrace.c b/lib/backtrace.c index 715d7d1d05e..b01a08af8ba 100644 --- a/lib/backtrace.c +++ b/lib/backtrace.c @@ -26,8 +26,7 @@ static void print_sym(const char *sym) int backtrace_show(void) { - char buf[BACKTRACE_BUFSZ]; - struct backtrace_ctx ctx; + static struct backtrace_ctx ctx; uint i; int ret; @@ -35,7 +34,7 @@ int backtrace_show(void) if (ret < 0) return ret; - ret = backtrace_get_syms(&ctx, buf, sizeof(buf)); + ret = backtrace_get_syms(&ctx, NULL, 0); if (ret) { backtrace_uninit(&ctx); return ret; @@ -43,10 +42,12 @@ int backtrace_show(void) printf("backtrace: %d addresses\n", ctx.count); for (i = 0; i < ctx.count; i++) { - if (ctx.syms[i]) - print_sym(ctx.syms[i]); + const struct backtrace_frame *frame = &ctx.frame[i]; + + if (frame->sym) + print_sym(frame->sym); else - printf(" %p\n", ctx.addrs[i]); + printf(" %p\n", frame->addr); } backtrace_uninit(&ctx); diff --git a/test/lib/backtrace.c b/test/lib/backtrace.c index d9c36bbd495..11f0d43ca7e 100644 --- a/test/lib/backtrace.c +++ b/test/lib/backtrace.c @@ -15,24 +15,25 @@ /* Test backtrace_init() and backtrace_get_syms() */ static int lib_test_backtrace(struct unit_test_state *uts) { - char buf[BACKTRACE_BUFSZ]; - struct backtrace_ctx ctx; + static struct backtrace_ctx ctx; bool found_self = false; bool found_ut_run_list = false; uint i; ut_assert(backtrace_init(&ctx, 0) > 2); - ut_assertok(backtrace_get_syms(&ctx, buf, sizeof(buf))); + ut_assertok(backtrace_get_syms(&ctx, NULL, 0)); /* * Check for known functions in the call stack. With libbacktrace * we can find static functions too, so check for this test function. */ for (i = 0; i < ctx.count; i++) { - if (ctx.syms[i]) { - if (strstr(ctx.syms[i], "lib_test_backtrace")) + const struct backtrace_frame *frame = &ctx.frame[i]; + + if (frame->sym) { + if (strstr(frame->sym, "lib_test_backtrace")) found_self = true; - if (strstr(ctx.syms[i], "ut_run_list")) + if (strstr(frame->sym, "ut_run_list")) found_ut_run_list = true; } } From patchwork Wed Dec 10 00:07:04 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 870 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=1765325344; bh=SGH0r+eMZHop0z7Z4vQGkxWijl6kIWWibrv929XTejg=; 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=b2N7EbUAL7AD7v5xdXs6Fae6OMXfTWgCdulgjh7EktScyL3b65j17qHqVwJSmaFJC Fbof2ttE8SHplxkOPpWDmy34ke8g9mOFp3aIUgUbFcSqMYgFGpDGI3nMbVYoHuCwT2 r4nfyQB1f7LxDqlQk7CMKMaWxJ1nbtn2AjDu9ArnqLGwZ+JVUobrmt+u8BnTSfO8e3 KQMz5WDgyanK+FmwPIjPx/1BwvPkFLyikfObVLv8Qd/T0mTU3am9NJ7MCW3XPgRehW fMoXTSkrvaaK4GWMqmt6SlBp87nJTCrgpx0HgxVnQ+76HD2gIodypRP0JDqyejx6Pb +wGoZ4B7MBWxg== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 0D3B868A3E for ; Tue, 9 Dec 2025 17:09:04 -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 66XkTGuXIIkb for ; Tue, 9 Dec 2025 17:09:03 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325343; bh=SGH0r+eMZHop0z7Z4vQGkxWijl6kIWWibrv929XTejg=; 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=AxVK3z0yCyA+m2qkBAoLidUPDKo6F25nJkDaVXcTQ6tGXPfiU9nNgqhxbEOmwZYn2 1R0OhGvVmzzdcF30Nd1kcIfYiz4kVnUiD/seWwcYlbGMH3Qwv1a5bNDrur7L4FcXuF KtPzd2kYgLGY4Y+zFEm59dsbOA0FKMEgQp2MfbhsP0y4EVr3oAQVIYPeYX6mPt5JC4 VPGRWrgOQF7z7+hUojhVT8NNcNbwY8v4m0qtr2dTt4uJ6iTQnDvc002IofNuuEHxf8 ssBtE7raujbbz97LShgyAr7YyzPkXmJe/qL/W1yaYHAZiQ8nue0zPAyKJcQFJ20fh5 WJMp3HHpG0dGg== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id DFDBC6884F for ; Tue, 9 Dec 2025 17:09:03 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325342; bh=Cjbehmj145pWtuMLDCN5v5SqFjblPXbbidvkCYGYd0M=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jyF/1jIUxBGK2auHV1mRvM6/si1Wv4E/FSsnsE8IDbKLT7yEKvhef1J3la/9UN0qf RspzVeqaR1H/YJFd4z5FifeAp4L3TXSkHuciJOSPKbt8z4zJ1PkzOoDWd0hhXq956F 5p7NXvfbr9eHFAHGSUPE4YvwclH1N86/1ggYM5+v3wrEHE+uvdy2GlhjJttKtl+oRF 9Zy3fN0rOJGEO3XGpExMW7fTNJBlqhfKZ4Hbjz+bgTnCGoVSLa1z+6MH1bBpl6eVHd 5EPEixR+GdGT2yLGSM+qobpbZ9GSkbyHIDTisrN/C+3pRusWq8kqN0HejgMj547KpZ I4wDqF7dfLCFQ== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 6FAA768972; Tue, 9 Dec 2025 17:09:02 -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 YsyoJ60j_FZ7; Tue, 9 Dec 2025 17:09:02 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325337; bh=uktQD1LAm6VGCnyrE4fRiwr7uHb82VL9rmh13DIK82k=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=e+naYp4s8CxTE+z9GzAVESzcy7dk+sDwxAJQfhL08iDUwAk8P8ZozMdU96pR2j83Z 05zq8gGkDOi/2HBRTHvDiR2mcrazBomHvyqJ9El5vtGJjanh5mVNmTWHopn88g/C6s IDy3UiecNM/2kMFi+lJLWSbvlJzLHX6kysZXLa2y8DeKAq4zrWzu7OsXirUYF39tpt jXijNr11lpBvji4k+tvzlVHPIC/T7ZYvYXbr5gNqpQKfX1ybj0SxMGJFwEJXTjq825 zWxRuAQ/+FF0OSLloei3LsHIWeNyXG0msjEKRAcvWLahcPbABq7/ro+nHzBb86Rl/v nFILSCxW4/NJw== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id C4DFF687A7; Tue, 9 Dec 2025 17:08:56 -0700 (MST) From: Simon Glass To: U-Boot Concept Date: Tue, 9 Dec 2025 17:07:04 -0700 Message-ID: <20251210000737.180797-14-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: DOV6QWC37JI26JSIZWQF2C2LZ56QHFQV X-Message-ID-Hash: DOV6QWC37JI26JSIZWQF2C2LZ56QHFQV 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 13/35] backtrace: Add backtrace_str() for condensed backtrace output 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 new function backtrace_str() that returns a condensed backtrace string containing function names and line numbers separated by " <-". For example: "func_a:123 <-func_b:456 <-func_c:789" This is useful for logging and debugging where a compact representation of the call stack is needed. The depth is controlled by the new CONFIG_BACKTRACE_DEPTH option (default 3). Co-developed-by: Claude Signed-off-by: Simon Glass --- include/backtrace.h | 36 +++++++++++++ lib/Kconfig | 9 ++++ lib/backtrace.c | 122 +++++++++++++++++++++++++++++++++++++++++++ test/lib/backtrace.c | 36 +++++++++++++ 4 files changed, 203 insertions(+) diff --git a/include/backtrace.h b/include/backtrace.h index 7bb2ba68bec..7d9d8c1bb88 100644 --- a/include/backtrace.h +++ b/include/backtrace.h @@ -94,4 +94,40 @@ void backtrace_uninit(struct backtrace_ctx *ctx); */ int backtrace_show(void); +/** + * backtrace_strf() - get a condensed backtrace string into a buffer + * + * Return a string containing the last CONFIG_BACKTRACE_SUMMARY_FRAMES function names + * and line number, separated by ``<-``. + * + * For example: ``func_a:12 <-func_b:34 <-func_c:56`` + * + * @skip: number of stack frames to skip (0 to include backtrace_strf itself) + * @buf: buffer to write the string to + * @size: size of buffer + * Return: pointer to buf, or NULL on error + */ +char *backtrace_strf(unsigned int skip, char *buf, int size); + +/** + * backtrace_str() - get a condensed backtrace string + * + * Return a string containing the last CONFIG_BACKTRACE_SUMMARY_FRAMES function names + * and line number, separated by ``<-``. The string is statically allocated and + * will be overwritten on the next call. + * + * For example: ``func_a:12 <-func_b:34 <-func_c:56`` + * + * @skip: number of stack frames to skip (0 to include backtrace_str itself) + * Return: pointer to static string, or NULL on error + */ +#if CONFIG_IS_ENABLED(BACKTRACE) +const char *backtrace_str(unsigned int skip); +#else +static inline const char *backtrace_str(unsigned int skip) +{ + return NULL; +} +#endif + #endif /* __BACKTRACE_H */ diff --git a/lib/Kconfig b/lib/Kconfig index 79a75f98446..9c7eb27c392 100644 --- a/lib/Kconfig +++ b/lib/Kconfig @@ -36,6 +36,15 @@ config BACKTRACE stack. This is currently only available on sandbox. The backtrace command can be used to print the backtrace. +config BACKTRACE_SUMMARY_FRAMES + int "Number of frames in condensed backtrace" + depends on BACKTRACE + default 3 + help + Number of stack frames to include in the condensed backtrace + string returned by backtrace_str(). This affects BSS usage + since space must be allocated for the string. + config LIB_FORMAT_SIZE bool default y diff --git a/lib/backtrace.c b/lib/backtrace.c index b01a08af8ba..b657e8336ab 100644 --- a/lib/backtrace.c +++ b/lib/backtrace.c @@ -7,6 +7,7 @@ */ #include +#include #include #include @@ -54,3 +55,124 @@ int backtrace_show(void) return 0; } + +/** + * extract_func_info() - extract function name and line number from a symbol + * + * Parse a backtrace symbol string and extract function name with line number. + * The format is typically "func_name() at /path/to/file.c:line" or similar. + * + * @sym: symbol string from backtrace + * @buf: buffer to write "func_name:line" to + * @size: size of buffer + * Return: pointer to buf, or NULL if extraction failed + */ +static char *extract_func_info(const char *sym, char *buf, int size) +{ + const char *start, *end, *colon; + int len; + + if (!sym) + return NULL; + + /* + * Skip leading whitespace and any address prefix (e.g. "0x12345678 ") + * Look for the function name which ends at '+' or '(' or ' ' + */ + start = sym; + while (*start == ' ') + start++; + + /* Skip hex address if present */ + if (start[0] == '0' && start[1] == 'x') { + while (*start && *start != ' ') + start++; + while (*start == ' ') + start++; + } + + /* Find end of function name */ + end = start; + while (*end && *end != '+' && *end != '(' && *end != ' ') + end++; + + len = end - start; + if (len <= 0 || len >= size) + return NULL; + + memcpy(buf, start, len); + + /* Look for line number after last colon (file:line format) */ + colon = strrchr(sym, ':'); + if (colon && colon[1] >= '0' && colon[1] <= '9') { + buf[len++] = ':'; + colon++; + /* Copy digits */ + while (*colon >= '0' && *colon <= '9' && len < size - 1) + buf[len++] = *colon++; + } + buf[len] = '\0'; + + return buf; +} + +char *backtrace_strf(unsigned int skip, char *buf, int size) +{ + static struct backtrace_ctx ctx; + int remaining = size; + bool first = true; + char func[64]; + char *p = buf; + uint i, count; + int ret, len; + + /* skip + 1 to skip backtrace_strf() */ + ret = backtrace_init(&ctx, skip + 1); + if (ret < 0) + return NULL; + + ret = backtrace_get_syms(&ctx, NULL, 0); + if (ret) { + backtrace_uninit(&ctx); + return NULL; + } + + count = ctx.count; + if (count > CONFIG_BACKTRACE_SUMMARY_FRAMES) + count = CONFIG_BACKTRACE_SUMMARY_FRAMES; + + for (i = 0; i < count; i++) { + if (!extract_func_info(ctx.frame[i].sym, func, sizeof(func))) + continue; + + if (!first) { + if (remaining < 4) + break; + *p++ = ' '; + *p++ = '<'; + *p++ = '-'; + remaining -= 3; + } + first = false; + + len = strlen(func); + if (len >= remaining) + break; + memcpy(p, func, len); + p += len; + remaining -= len; + } + *p = '\0'; + + backtrace_uninit(&ctx); + + return buf; +} + +const char *backtrace_str(unsigned int skip) +{ + static char result[CONFIG_BACKTRACE_SUMMARY_FRAMES * 64]; + + /* skip + 1 to account for this wrapper function */ + return backtrace_strf(skip + 1, result, sizeof(result)); +} diff --git a/test/lib/backtrace.c b/test/lib/backtrace.c index 11f0d43ca7e..3f20b7854bf 100644 --- a/test/lib/backtrace.c +++ b/test/lib/backtrace.c @@ -46,3 +46,39 @@ static int lib_test_backtrace(struct unit_test_state *uts) return 0; } LIB_TEST(lib_test_backtrace, 0); + +/* Test backtrace_strf() and backtrace_str() */ +static int lib_test_backtrace_str(struct unit_test_state *uts) +{ + char pattern[128]; + char buf[256]; + const char *cstr; + char *str; + int line; + + /* Test backtrace_strf() with skip=1 to skip backtrace_strf() itself */ + line = __LINE__ + 1; + str = backtrace_strf(1, buf, sizeof(buf)); + ut_assertnonnull(str); + ut_asserteq_ptr(buf, str); + + printf("backtrace_strf: %s\n", str); + snprintf(pattern, sizeof(pattern), + "lib_test_backtrace_str:%d <-ut_run_test:\\d+ <-ut_run_test_live_flat:\\d+", + line); + ut_asserteq_regex(pattern, str); + + /* Test backtrace_str() */ + line = __LINE__ + 1; + cstr = backtrace_str(0); + ut_assertnonnull(cstr); + + printf("backtrace_str: %s\n", cstr); + snprintf(pattern, sizeof(pattern), + "lib_test_backtrace_str:%d <-ut_run_test:\\d+ <-ut_run_test_live_flat:\\d+", + line); + ut_asserteq_regex(pattern, cstr); + + return 0; +} +LIB_TEST(lib_test_backtrace_str, 0); From patchwork Wed Dec 10 00:07:05 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 871 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=1765325347; bh=vFNkxXa04aA8g15IyPvqCCS9kwkD/TuEBvLMTST+9SA=; 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=jZyNh0NP39fUyJ6UCbIIzL6Jj0OwlAuqktT24C6hei/KQ4g087d+BMMB08Ocf7yUg +TA6ZFctXHsubTumKD7X+A/8QPObWupH8BLxlxU5Mo9oYYww4sBF8FRd0nb6A7npUm AcHFYNIQ7jT9te2rNOBAD4gd961DDcRw7GNObrgNUZkuo6qnbllXjcoAdgOJ1n3FYC kExFowp5NDrcqoWHWJUxsVxaTaAv664lrp7HuwfLpPP6eyNRvfx7VYBvgv2MxEV+MZ r+ioMDRjEEoH+xChzt9XzE439oH5FC/ZRTka+iMRfRanAez+jItHeIUVrsrYnkQrOV PdTlT5OvvzizA== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 6C63068A22 for ; Tue, 9 Dec 2025 17:09:07 -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 OQm1vf5BdYxJ for ; Tue, 9 Dec 2025 17:09:07 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325347; bh=vFNkxXa04aA8g15IyPvqCCS9kwkD/TuEBvLMTST+9SA=; 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=jZyNh0NP39fUyJ6UCbIIzL6Jj0OwlAuqktT24C6hei/KQ4g087d+BMMB08Ocf7yUg +TA6ZFctXHsubTumKD7X+A/8QPObWupH8BLxlxU5Mo9oYYww4sBF8FRd0nb6A7npUm AcHFYNIQ7jT9te2rNOBAD4gd961DDcRw7GNObrgNUZkuo6qnbllXjcoAdgOJ1n3FYC kExFowp5NDrcqoWHWJUxsVxaTaAv664lrp7HuwfLpPP6eyNRvfx7VYBvgv2MxEV+MZ r+ioMDRjEEoH+xChzt9XzE439oH5FC/ZRTka+iMRfRanAez+jItHeIUVrsrYnkQrOV PdTlT5OvvzizA== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 5BE0F68974 for ; Tue, 9 Dec 2025 17:09:07 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325346; bh=yBA6tS74TUgno+qDEs1+TKmgy1qVo/UioN1kAoWRZzA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=c+PRd+iCFuejVGkpfe7Oc10PZTdGx71+74UH9C14+bU9szu29xvQqtjGLnCTJSbMR Ck294jV1wGI7gBQM7crVs6GDMs7gLQVSAb0b0NNT/AiC+zwfjWEX0Fh/zn+HR9J9sg Ljv9Q8XJe9NeMWUfi39nlClrJxVQT0MDL6EDLoe4y68hmZ94PBRkbzcm+lUYqqyilX ZrxsD/lWJOOO5cgxTFMduSbn/qZqS/1UKTAdkzVd5uptupSVtXwxtawB18xRs5Kn9P /xTGYbZiu6ADb5EN7Cw27YvGbd1MIKhuekm9x6mBqUutQBagLYkM2Xua7nltM6TeRb AKNi2DP/BIs7w== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 5BB1D687A7; Tue, 9 Dec 2025 17:09:06 -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 jG82GSONjt3V; Tue, 9 Dec 2025 17:09:06 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325342; bh=IygW9TX1kp/+OOopXMQ6rMK2Z1QciyZB1ON2mWs5RUI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=e6unVKNRBR+XnZluwRS5jukJFSzoYvTvri8SpFfzfPtVL2z6nr1uKhzslJALQHpbK diCtpzpavJIps2sR7OeFe0Wa/gBNdmBLgacayApDLLkqxZIXkn80BmteM8Obkxt8XG sWPLVA09esTsV1CGcocpoPYkqT4IngsKBMC+eBGlJB/mopsH+1+Tbp7qI9Zn38ZmR7 C8BTC18eOCoE8lU96uYWTodIDrbCFIbzSJjj68rlsitnBa8Ej7lFUfgmTstmzN/3VS fX0rDTjnt4g5FxXHNSE/d15Az1wEYMKpgC/PWqR32n64VnLCrAVI4msVrtUhIhm1lG aGQWJJcsQ4qvQ== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id A908B689EA; Tue, 9 Dec 2025 17:09:01 -0700 (MST) From: Simon Glass To: U-Boot Concept Date: Tue, 9 Dec 2025 17:07:05 -0700 Message-ID: <20251210000737.180797-15-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: GE2UZYKUN2E3SWXVZJQUS43VO2SIHJKY X-Message-ID-Hash: GE2UZYKUN2E3SWXVZJQUS43VO2SIHJKY 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 14/35] malloc: Fix unused internal_memalign warning with mcheck 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 CONFIG_MCHECK_HEAP_PROTECTION is enabled, dlmemalign_impl() calls dlmalloc_impl() directly since the mcheck wrapper handles alignment. This leaves internal_memalign() unused, causing a compiler warning. Guard internal_memalign() with a preprocessor check so it's only compiled when needed (mcheck disabled or MSPACES enabled). Co-developed-by: Claude Signed-off-by: Simon Glass --- common/dlmalloc.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/common/dlmalloc.c b/common/dlmalloc.c index b46be1899f0..efeff18653f 100644 --- a/common/dlmalloc.c +++ b/common/dlmalloc.c @@ -5309,6 +5309,7 @@ static mchunkptr try_realloc_chunk(mstate m, mchunkptr p, size_t nb, } #endif /* !defined(__UBOOT__) || !NO_REALLOC_IN_PLACE */ +#if !CONFIG_IS_ENABLED(MCHECK_HEAP_PROTECTION) || MSPACES static void* internal_memalign(mstate m, size_t alignment, size_t bytes) { void* mem = 0; if (alignment < MIN_CHUNK_SIZE) /* must be at least a minimum chunk size */ @@ -5422,6 +5423,7 @@ static void* internal_memalign(mstate m, size_t alignment, size_t bytes) { } return mem; } +#endif /* !CONFIG_MCHECK_HEAP_PROTECTION || MSPACES */ /* Common support for independent_X routines, handling From patchwork Wed Dec 10 00:07:06 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 872 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=1765325352; bh=niD8XzvXaZR4SbPB9UN/KsQMWpBu13+ZCu6jblrXQhU=; 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=ZDPTnYeHgoAkqANmA38eFYpSzkPaSm9mDA18wf8+d2H94v52TyctpvISMTaEpuJLL vOPuJ53KLLi8EivjDQLLuHQFq3gI0sHGN3hBiALiQPzgx3FOoW/SO0LrCxLO+ZqLdG W6N8tVZ69h+mC/QMd/GL05ouzZ+sMxuzCEkm4MzhrmoB2+EYJGacnOrO+zUXO+sDQ3 76cQOCvx0sTh1kCl5UqtxdYJ/FrrLx0sMT9oLKcZonqagahvuH5KcoYlIyjeyrbtq3 TJXLJ7/dl6NrhvAA9qecDi8jXwui1O7F8L5WQZ7ESRcVCSjwpxPob991JhHqsmQHl2 0BJhRk3LFHjDw== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id E8A1768A22 for ; Tue, 9 Dec 2025 17:09:12 -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 nXRS7owEKSWu for ; Tue, 9 Dec 2025 17:09:12 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325352; bh=niD8XzvXaZR4SbPB9UN/KsQMWpBu13+ZCu6jblrXQhU=; 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=ZDPTnYeHgoAkqANmA38eFYpSzkPaSm9mDA18wf8+d2H94v52TyctpvISMTaEpuJLL vOPuJ53KLLi8EivjDQLLuHQFq3gI0sHGN3hBiALiQPzgx3FOoW/SO0LrCxLO+ZqLdG W6N8tVZ69h+mC/QMd/GL05ouzZ+sMxuzCEkm4MzhrmoB2+EYJGacnOrO+zUXO+sDQ3 76cQOCvx0sTh1kCl5UqtxdYJ/FrrLx0sMT9oLKcZonqagahvuH5KcoYlIyjeyrbtq3 TJXLJ7/dl6NrhvAA9qecDi8jXwui1O7F8L5WQZ7ESRcVCSjwpxPob991JhHqsmQHl2 0BJhRk3LFHjDw== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id D644F689E6 for ; Tue, 9 Dec 2025 17:09:12 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325350; bh=jFBZlHvGLhaYiV66bpJYtGcE/xdxYBQhCDocw52/nyw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=b+N58qXDotf2aTn7T75Qcc35U2P8REyHvjyc1l6/ivBRzAQ0yvdieL/ZiIpCMzmDK EDF3zeCx9XITKlKzF6MpioaogLeAQxVhmags5bt2GfKHBp0fDZQJUK4wGORXWV3463 FSHvlKwdfOe0ht5ii+OW5EYVLeOVhQW8+DTW16BiRQxyrV0OGiBZdFhsWoJ/cZlvRZ XLaOLfTkaNNgkJmbgHWGIjuegCSLyTdJwJvu/MtZZaPgQzHSTMde4vKEmq0IgVdRD8 L1Ib3kFUIuCvtcs3IiXoEHg7xduOKnjpG1PxvO5tX+fTObwL3qkvSqbIkEdgtYwZ4i XtAxY/Yg0fDMA== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id ED40A68957; Tue, 9 Dec 2025 17:09:10 -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 rGg0JOnsGc0P; Tue, 9 Dec 2025 17:09:10 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325346; bh=HuxHmHRq8v22xBkwgRGb+HL0t7pjXuNFDKCfP8KqeQ0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=E1sJxIt5qo9tlw4t4VFKh5m+gFG8Te2CAdlaILX4Jo4+ZgY4hnsFDFcMiojA4nP4P il8ZparQJKWYjDp2pVj66zvD/LYKdRHe6AjiRHKY7Kt/klcz9aT4OHWbjjRNIVKR91 TvHj3wvnZwuzOsuo/i3cP3EnY2Q+YjZ1d/oh/OrDRHzo3VMPfd5v8s8qcyfjiOX3JS q2P3eFh01HrIpfzpeNtgRmk8tuF9IuL/TDAzzKSiadS3t+4R4idisya6GcdylPFmBj QTlSD0o5wKqLWFf/Wqd5VNuz3+wuIyV3zlHcJHfRKTB9MD0Ew2ylB2RGVRO7byCCra wDgpsBb896ePw== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id 7262A6884F; Tue, 9 Dec 2025 17:09:06 -0700 (MST) From: Simon Glass To: U-Boot Concept Date: Tue, 9 Dec 2025 17:07:06 -0700 Message-ID: <20251210000737.180797-16-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: HKFJMCH23CB7MVXHC476Z4MH6CI6TCKC X-Message-ID-Hash: HKFJMCH23CB7MVXHC476Z4MH6CI6TCKC 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 15/35] malloc: Add malloc_get_info() to retrieve memory statistics 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 struct malloc_info and malloc_get_info() function to programmatically access the memory-allocation stats that malloc_stats() prints. The struct contains the size of the malloc() poll and the number of bytes in use. Add a static inline stub to return an error when DEBUG is not defined. Co-developed-by: Claude Signed-off-by: Simon Glass --- common/dlmalloc.c | 10 ++++++++++ include/malloc.h | 29 +++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/common/dlmalloc.c b/common/dlmalloc.c index efeff18653f..004dafe4b3b 100644 --- a/common/dlmalloc.c +++ b/common/dlmalloc.c @@ -3689,6 +3689,16 @@ static struct mallinfo internal_mallinfo(mstate m) { } return nm; } + +int malloc_get_info(struct malloc_info *info) +{ + struct mallinfo mi = internal_mallinfo(gm); + + info->total_bytes = mem_malloc_end - mem_malloc_start; + info->in_use_bytes = mi.uordblks; + + return 0; +} #endif /* !NO_MALLINFO */ #if !NO_MALLOC_STATS diff --git a/include/malloc.h b/include/malloc.h index 73b2da0c383..fd8a8ddcfaf 100644 --- a/include/malloc.h +++ b/include/malloc.h @@ -665,8 +665,22 @@ void mspace_inspect_all(mspace msp, /* --------------------- U-Boot additions --------------------- */ #ifdef __UBOOT__ +#include #include +/** + * struct malloc_info - Memory allocation statistics + * + * This is filled in by malloc_get_info(). + * + * @total_bytes: Total bytes available in the heap + * @in_use_bytes: Current bytes allocated (in use by application) + */ +struct malloc_info { + ulong total_bytes; + ulong in_use_bytes; +}; + /* Memory pool boundaries */ extern ulong mem_malloc_start; extern ulong mem_malloc_end; @@ -735,6 +749,21 @@ void *memalign_simple(size_t alignment, size_t bytes); */ int initf_malloc(void); +/** + * malloc_get_info() - Get memory allocation statistics + * + * @info: Place to put the statistics + * Return: 0 on success, -ENOSYS if not available (DEBUG not defined) + */ +#ifdef DEBUG +int malloc_get_info(struct malloc_info *info); +#else +static inline int malloc_get_info(struct malloc_info *info) +{ + return -ENOSYS; +} +#endif + #endif /* __UBOOT__ */ #ifdef __cplusplus From patchwork Wed Dec 10 00:07:07 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 873 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=1765325357; bh=zBBD230c1QUQMRAoyCT0VNvl2hPgeui2YdRQb4W2Mfc=; 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=qamnnfwNhWEZU5+6NluTRmgcSAC0cxJYzxf3R2FIa7/ffUE84KwgZ32D4vCB2yMUr CfbtJByI9sKttOWXSQoP4R1SA69sStnJASe2ErN96a42b993kaDifB0PGp1yqr75ly Dxnbn58IrTmgy/6AM89YNl6UkC2y7jRuSqHeP9kHvJgxUBaaIQv4RcGQIMlhYv2BIe NNJ+51jGo64N7Z9/uluwEBJ3Gq/VvWcxf5cQpHfu9IqERjhC3CAysggGyqPtHb1qyM DsiHL6SAQAd76xN7GrJzKc7BMo3mW2O8lMcyv550MuieSUjmIYwM3aTOsR4PbF0V09 DhDTAKg1rXsUQ== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 6661D68A22 for ; Tue, 9 Dec 2025 17:09:17 -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 4UmiCnYZSPVG for ; Tue, 9 Dec 2025 17:09:17 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325357; bh=zBBD230c1QUQMRAoyCT0VNvl2hPgeui2YdRQb4W2Mfc=; 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=qamnnfwNhWEZU5+6NluTRmgcSAC0cxJYzxf3R2FIa7/ffUE84KwgZ32D4vCB2yMUr CfbtJByI9sKttOWXSQoP4R1SA69sStnJASe2ErN96a42b993kaDifB0PGp1yqr75ly Dxnbn58IrTmgy/6AM89YNl6UkC2y7jRuSqHeP9kHvJgxUBaaIQv4RcGQIMlhYv2BIe NNJ+51jGo64N7Z9/uluwEBJ3Gq/VvWcxf5cQpHfu9IqERjhC3CAysggGyqPtHb1qyM DsiHL6SAQAd76xN7GrJzKc7BMo3mW2O8lMcyv550MuieSUjmIYwM3aTOsR4PbF0V09 DhDTAKg1rXsUQ== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 55E77689E6 for ; Tue, 9 Dec 2025 17:09:17 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325355; bh=3//tW05RY83XIgtXdAQJuKYxlyuKLoOZDsonMuxcOCg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=DYNi6ZC8qUjmLLLSMRvBYbrwHdnCZg6DzGlNI/3NnIC/m/eXi9iVVxeoTYPAxRzrV wW3VebtKd/Kpmbswwzab3tw8mu8PQ1PgI2mox3WxFndi2EeZxE7/gt+yjaivOc10St nbqBs/3OTy3o+/iie+i7gaoBTffMNeYKuxkOAeOYBKzMwpAtQff/epqdT83LVv1sta kR9jHpXg2uvnMSDU3EfbHGcDx184fZWNqNoUk0FWhNa0XhT6GVw7uqPD7dXbiZjw1J fYR6d/nA56XM0hwfnf2o6BmRle8UhKim9Vz87yb808Lm8Glhair1Kq1ytSNM+ehjEZ rhI3g7gEyNc4g== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id B888E687A7; Tue, 9 Dec 2025 17:09:15 -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 MCIRmymgl3d1; Tue, 9 Dec 2025 17:09:15 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325351; bh=uL/4p+fTOhYJgUJ6mIimdpQIyyq7fUn3ZzSjyzBThng=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=hYo0etS8UuP6ewJyCxoXDGm/9HqrB6BHVwwNRWpKko4qmS1tk8rA9MY9xIlMRdH9a KfEdzGrucAZTOfm1PpqOphcyd7vASYQpEF9Ou9e3pMdf0zleBwhXubqyS52ngaeyKV OkERGf33VCfRrjTPtdIDFKdDQP9R8PvZtU2r+b8ypFmClye3mfGCG2gp1V8DvGQ5nA lw0UnnISfEXeWPCvIK7fe93nf5djmbdOI1p3vtQnwNSgzVEjL9+gjqjz/RGWKFyOd5 2gI8Z/UWzqpLELN8y0lsA56/8DJEpbIiIYUFIXDiP0HQuqu5YU0FFK/8yUVqppfGKS SE20yREHGYDng== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id 43EBC6884F; Tue, 9 Dec 2025 17:09:11 -0700 (MST) From: Simon Glass To: U-Boot Concept Date: Tue, 9 Dec 2025 17:07:07 -0700 Message-ID: <20251210000737.180797-17-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: O5EUJN7ROJGIHMWRNVXEEEPLXW5QIMZB X-Message-ID-Hash: O5EUJN7ROJGIHMWRNVXEEEPLXW5QIMZB 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 16/35] malloc: Add a Kconfig option for debugging malloc() 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 new CONFIG_MALLOC_DEBUG option to control malloc() debugging features. This replaces the direct UNIT_TEST check and allows enabling malloc debugging independently of whether UNIT_TEST is enabled. The option defaults to y when UNIT_TEST is enabled, preserving existing behaviour. Disable for RISC-V since it seems to have a size limitation. Co-developed-by: Claude Signed-off-by: Simon Glass --- Kconfig | 8 ++++++++ common/dlmalloc.c | 2 +- include/malloc.h | 4 ++-- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/Kconfig b/Kconfig index 378ecfb1867..86276c89f38 100644 --- a/Kconfig +++ b/Kconfig @@ -337,6 +337,14 @@ config SYS_MALLOC_LEN This defines memory to be allocated for Dynamic allocation TODO: Use for other architectures +config MALLOC_DEBUG + bool "Enable malloc debugging" + default y if UNIT_TEST && !CPU_RISCV + help + Enable debugging features in the malloc implementation. This + enables additional assertions and the malloc_get_info() function + to retrieve memory-allocation statistics. + config SPL_SYS_MALLOC_F bool "Enable malloc() pool in SPL" depends on SPL_FRAMEWORK && SYS_MALLOC_F && SPL diff --git a/common/dlmalloc.c b/common/dlmalloc.c index 004dafe4b3b..b5dc2b13dc6 100644 --- a/common/dlmalloc.c +++ b/common/dlmalloc.c @@ -568,7 +568,7 @@ MAX_RELEASE_CHECK_RATE default: 4095 unless not HAVE_MMAP #ifdef __UBOOT__ -#if CONFIG_IS_ENABLED(UNIT_TEST) +#if CONFIG_IS_ENABLED(MALLOC_DEBUG) #define DEBUG 1 #endif diff --git a/include/malloc.h b/include/malloc.h index fd8a8ddcfaf..7e276d444ab 100644 --- a/include/malloc.h +++ b/include/malloc.h @@ -753,9 +753,9 @@ int initf_malloc(void); * malloc_get_info() - Get memory allocation statistics * * @info: Place to put the statistics - * Return: 0 on success, -ENOSYS if not available (DEBUG not defined) + * Return: 0 on success, -ENOSYS if not available (MALLOC_DEBUG not enabled) */ -#ifdef DEBUG +#if CONFIG_IS_ENABLED(MALLOC_DEBUG) int malloc_get_info(struct malloc_info *info); #else static inline int malloc_get_info(struct malloc_info *info) From patchwork Wed Dec 10 00:07:08 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 874 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=1765325362; bh=w0oMNjr3PowbXP4pgadG3dPxL7ORfDW3XaW0COs6wQM=; 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=Yh/S+fcUpn8iKeAMx1woJab8+xwXLy2SY+ehlbSfaZTSLWEB9wgVIckzxyS+PEDnE Z/P9f2JVEYg4/TYtpDV70DieSJh2a3ykXkTJAQX1swPONrx92tJYJpm/2Q+AJLcid3 Y6ShQo7seRHuPHbi9V06ZH1jIy987jxZ3g52n0L+sgnmnp5JBO5LDTYwvgHbmgOKI4 +D2pZDcBX7F3BzDH3yW8kIPLJAkeyJk+PUbp7TKot8nOjxjkz5qZ8eCMT0ueUja4rI no59VWB3zy4UQPrQg0TPgONwrfDGso8+q+olLApRZp2xjDo+gXiTNixBbyD+AH86gL BjKW1AYCFOz7Q== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 2E413687A7 for ; Tue, 9 Dec 2025 17:09:22 -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 30Vhs8OBfjG9 for ; Tue, 9 Dec 2025 17:09:22 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325362; bh=w0oMNjr3PowbXP4pgadG3dPxL7ORfDW3XaW0COs6wQM=; 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=Yh/S+fcUpn8iKeAMx1woJab8+xwXLy2SY+ehlbSfaZTSLWEB9wgVIckzxyS+PEDnE Z/P9f2JVEYg4/TYtpDV70DieSJh2a3ykXkTJAQX1swPONrx92tJYJpm/2Q+AJLcid3 Y6ShQo7seRHuPHbi9V06ZH1jIy987jxZ3g52n0L+sgnmnp5JBO5LDTYwvgHbmgOKI4 +D2pZDcBX7F3BzDH3yW8kIPLJAkeyJk+PUbp7TKot8nOjxjkz5qZ8eCMT0ueUja4rI no59VWB3zy4UQPrQg0TPgONwrfDGso8+q+olLApRZp2xjDo+gXiTNixBbyD+AH86gL BjKW1AYCFOz7Q== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 1D4D8689EA for ; Tue, 9 Dec 2025 17:09:22 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325360; bh=joBp36szA75SKBpJsZLY0Qg1qRXcVUcEwsFfoOIwbrM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=PPyYze8yXDhPeYLKivSKw/LCUdg6te5SoA9xmQwCJzBDYmjE16CAl8jC9Kjpqn3it CV1rYslJiP0JNQ+zIHWupYcYbRtsZ969I0RkrJfo8o3CfgYWlgOWr5/SUxZTAzBTcG 7QysnayOWtN6k6dMY6dv2DWc69ZG/kFVEylAQgOjgMM5oP2oOXtmcXA2gotey2TGO3 3vvdS+9J08QTHrSlGGu7mK08TwSgnIWUfixSc2iLEnVzukFJjSREVvjOL1lqdXY7AY BNQFEJwnnaPWoGaH3d36zo07EavBQ1J+5l0WirtITu1fQFuOanczbHX0ZxKCxhCT0N Ex89aoqZMAX0g== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 79D8B687A7; Tue, 9 Dec 2025 17:09:20 -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 jRbN9Ou0pgNL; Tue, 9 Dec 2025 17:09:20 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325356; bh=WZUQ9UwW8moXYCbV9p8KCowO8p4UsXnyuPqUiltaDPg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=dHY3JNM+UhoZzKtxhgmtbfY1x4/ManfpgN4w490WTRdJmxDBbaacpI1TE3KQ0qhq3 ohuify1tTkNKo8SlW0V77ZGGNwH3VQI7x7i/3F0OZ5LM7CbuNE3QMTy7+tLJzACbof Qqd538pe5wXprAWuPEA0Wo/+zPgtykIbw7seKqIAScKo9oE9pSkkiVDkBDfeRlHOqM mJttMtawYhgu4pQFFL4PqUQnVC+W2S/kBuNw+T0g10Tp6FVAdqIfaFaUM5OyFL0Drk RCp9mbATPMCPQvl9apK7mpAKoMbcs87PoIUFwZxi4YoofmFVnLLXJ2ofZidruRmDnh 7p5TQAjQzbuFg== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id 129F66884F; Tue, 9 Dec 2025 17:09:15 -0700 (MST) From: Simon Glass To: U-Boot Concept Date: Tue, 9 Dec 2025 17:07:08 -0700 Message-ID: <20251210000737.180797-18-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: D7VBZAZIKAV6QXYEVZ7OYCT6XAPMCTZV X-Message-ID-Hash: D7VBZAZIKAV6QXYEVZ7OYCT6XAPMCTZV 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 X-Mailman-Version: 3.3.10 Precedence: list Subject: [Concept] [PATCH 17/35] malloc: Enable stats if UNIT_TEST or MALLOC_DEBUG 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 debugging or running unit tests it is helpful to have information available from the malloc subsystem. Enable these features in those cases. Signed-off-by: Simon Glass --- common/dlmalloc.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/common/dlmalloc.c b/common/dlmalloc.c index b5dc2b13dc6..b0845e6ea7c 100644 --- a/common/dlmalloc.c +++ b/common/dlmalloc.c @@ -599,12 +599,12 @@ static inline void MALLOC_COPY(void *dest, const void *src, size_t sz) { memcpy( #define MORECORE_CONTIGUOUS 1 #define MORECORE_CANNOT_TRIM 1 #define MORECORE_CLEARS 1 -#define NO_MALLOC_STATS 1 #define USE_LOCKS 0 #define USE_SPIN_LOCKS 0 -#if !CONFIG_IS_ENABLED(UNIT_TEST) +#if !CONFIG_IS_ENABLED(UNIT_TEST) && !IS_ENABLED(CONFIG_MALLOC_DEBUG) #define NO_MALLINFO 1 +#define NO_MALLOC_STATS 1 #endif #if !CONFIG_IS_ENABLED(SANDBOX) #define INSECURE 1 @@ -3690,6 +3690,9 @@ static struct mallinfo internal_mallinfo(mstate m) { return nm; } +#endif /* !NO_MALLINFO */ + +#if CONFIG_IS_ENABLED(MALLOC_DEBUG) int malloc_get_info(struct malloc_info *info) { struct mallinfo mi = internal_mallinfo(gm); @@ -3699,7 +3702,7 @@ int malloc_get_info(struct malloc_info *info) return 0; } -#endif /* !NO_MALLINFO */ +#endif #if !NO_MALLOC_STATS static void internal_malloc_stats(mstate m) { From patchwork Wed Dec 10 00:07:09 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 875 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=1765325366; bh=7NlqR/btHofmvlthWxoJAfmNqyOja8pidYLs9fKNY6c=; 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=Ou4lx9s0lJxC/cSqNruEO+QOsudhAHE5CtrLrJqcB4+N3++cHQBsWDRiD1wR2AHtW agx3Rw6zh2fB8riyQGtDf1jl2SLkYUoy7JEC4YvB34iPfdXFr+CXnBuP2rkl2SfReQ /69IJj0JXLs2O3EhQVYwbnIBZgL2MipQFQ/SML0FhnEVmYJ6+PhFJ7/XlLoXPDAtam gra5mt7RWcMYrd/xWyGFFG9ac5dx7cdCbtI05p+EMzip2eguKomIeZcz9nHUH2BCYz KwPCjZvOaFU0J4TmFxhhLR2x5XJs9rS541GJua09VQH7FLqXUT2J+K58mcIitWyiSE NkWVD3sTqyLkA== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id BC5C068A3E for ; Tue, 9 Dec 2025 17:09:26 -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 oGFeIny8j_fm for ; Tue, 9 Dec 2025 17:09:26 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325366; bh=7NlqR/btHofmvlthWxoJAfmNqyOja8pidYLs9fKNY6c=; 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=Ou4lx9s0lJxC/cSqNruEO+QOsudhAHE5CtrLrJqcB4+N3++cHQBsWDRiD1wR2AHtW agx3Rw6zh2fB8riyQGtDf1jl2SLkYUoy7JEC4YvB34iPfdXFr+CXnBuP2rkl2SfReQ /69IJj0JXLs2O3EhQVYwbnIBZgL2MipQFQ/SML0FhnEVmYJ6+PhFJ7/XlLoXPDAtam gra5mt7RWcMYrd/xWyGFFG9ac5dx7cdCbtI05p+EMzip2eguKomIeZcz9nHUH2BCYz KwPCjZvOaFU0J4TmFxhhLR2x5XJs9rS541GJua09VQH7FLqXUT2J+K58mcIitWyiSE NkWVD3sTqyLkA== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id AB22868957 for ; Tue, 9 Dec 2025 17:09:26 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325365; bh=XP8YSXp8GFWBIlVrj6dqMgU6IcBhLa8J0nY0RDwZ6oc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=VFxwaw99Co21Ch7UKxNOnfWMWhJ//vfbvmPW8DtEfzPVbu8GPOZ6aq59+0IxPhraJ 1dTrgclkQ9ZD2CVdKRGCtd+TWlneEvwHREKrj4ikwLh9hquI9oQ57vvKpX/1bOdcwy hTz8wKEYt4UJpryQ+KbFPfDTbtuEt0iSc3f3wHDcnbWze71Zd8pkfyWI2+d4ae51vH 4a6fZpU4AXXpbfEsZAyLFl1JRxmRxUoZLWP/lPvcCTTa0wWeiu/4sc1zWx6GmcNNhq /mhE3rZ/q04NQBEVNs5J8Tzub3jQnQarrR/O5rwe7auvKixmzKu68a8LQbodCuNh7R /i63EwJqd53Lg== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 5E9D568957; Tue, 9 Dec 2025 17:09:25 -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 Qdw8rVfGFWL4; Tue, 9 Dec 2025 17:09:25 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325361; bh=6rC/aYWgERQ183A+cfkFVxTUJhBiFklQwU8fPWkfbWg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=bxl2OLf1NTLph3WwEbWLX4cCrwNR89libf8yc8+PpchMtFi9Kgd5Y0mEEgRAJcxci abwclt9OPMUQ385lm3XZxtZB9GIQPY9amlSw9WnbWAQpaIzOx830wAPX4YeK5XXW1X CwvHugZE0x6jjfh/oV3pGdzDZa8o2DsjtpO9ojteKESX8B6vxRzzxWt9543sOfVVBx ZhtcBOKVnT5xudhn4j83ipC0YGuW3l2rv0GUSnXNlIQELNdyd1hi+Cdh1C8iIFlmAM ra86Fe8U5P4PrMxrsSkJ9Mq/8KCpm+yJHkETdwMhsm5WqaBiX34XBqFlkJQKMSNhcy Vm8voTKD7x5ig== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id CE5146884F; Tue, 9 Dec 2025 17:09:20 -0700 (MST) From: Simon Glass To: U-Boot Concept Date: Tue, 9 Dec 2025 17:07:09 -0700 Message-ID: <20251210000737.180797-19-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: ZJTPG2B5SLTBJZYQEAXV77R3BR2REPTM X-Message-ID-Hash: ZJTPG2B5SLTBJZYQEAXV77R3BR2REPTM 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 18/35] malloc: Add 'malloc' command with 'info' subcommand 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 command to display malloc heap statistics, showing total heap size and memory currently in use. Co-developed-by: Claude Signed-off-by: Simon Glass --- cmd/Kconfig | 9 ++++++++ cmd/Makefile | 1 + cmd/malloc.c | 34 ++++++++++++++++++++++++++++++ doc/usage/cmd/malloc.rst | 45 ++++++++++++++++++++++++++++++++++++++++ doc/usage/index.rst | 1 + test/cmd/Makefile | 1 + test/cmd/malloc.c | 30 +++++++++++++++++++++++++++ 7 files changed, 121 insertions(+) create mode 100644 cmd/malloc.c create mode 100644 doc/usage/cmd/malloc.rst create mode 100644 test/cmd/malloc.c diff --git a/cmd/Kconfig b/cmd/Kconfig index ff5f6f85144..43e12e413f8 100644 --- a/cmd/Kconfig +++ b/cmd/Kconfig @@ -3018,6 +3018,15 @@ config CMD_LOG maximum log level for emitting of records). It also provides access to a command used for testing the log system. +config CMD_MALLOC + bool "malloc - Show malloc statistics" + depends on MALLOC_DEBUG + default y + help + This provides access to malloc information. It shows statistics + about memory allocation, such as total memory allocated and + currently in use. + config CMD_MOUSE bool "mouse - Show mouse input" default y if MOUSE diff --git a/cmd/Makefile b/cmd/Makefile index ebf66ea0d3c..c010da8dc3b 100644 --- a/cmd/Makefile +++ b/cmd/Makefile @@ -113,6 +113,7 @@ obj-$(CONFIG_CMD_LED) += led.o obj-$(CONFIG_CMD_LICENSE) += license.o obj-y += load.o obj-$(CONFIG_CMD_LOG) += log.o +obj-$(CONFIG_CMD_MALLOC) += malloc.o obj-$(CONFIG_CMD_LSBLK) += lsblk.o obj-$(CONFIG_CMD_MD5SUM) += md5sum.o obj-$(CONFIG_CMD_MEMORY) += mem.o diff --git a/cmd/malloc.c b/cmd/malloc.c new file mode 100644 index 00000000000..cb2fa34155b --- /dev/null +++ b/cmd/malloc.c @@ -0,0 +1,34 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * malloc command - show malloc information + * + * Copyright 2025 Canonical Ltd + * Written by Simon Glass + */ + +#include +#include +#include + +static int do_malloc_info(struct cmd_tbl *cmdtp, int flag, int argc, + char *const argv[]) +{ + struct malloc_info info; + char buf[12]; + int ret; + + ret = malloc_get_info(&info); + if (ret) + return CMD_RET_FAILURE; + + printf("total bytes = %s\n", format_size(buf, info.total_bytes)); + printf("in use bytes = %s\n", format_size(buf, info.in_use_bytes)); + + return 0; +} + +U_BOOT_LONGHELP(malloc, + "info - display malloc statistics\n"); + +U_BOOT_CMD_WITH_SUBCMDS(malloc, "malloc information", malloc_help_text, + U_BOOT_SUBCMD_MKENT(info, 1, 1, do_malloc_info)); diff --git a/doc/usage/cmd/malloc.rst b/doc/usage/cmd/malloc.rst new file mode 100644 index 00000000000..d21aa6c1421 --- /dev/null +++ b/doc/usage/cmd/malloc.rst @@ -0,0 +1,45 @@ +.. SPDX-License-Identifier: GPL-2.0+: + +.. index:: + single: malloc (command) + +malloc command +============== + +Synopsis +-------- + +:: + + malloc info + +Description +----------- + +The malloc command shows information about the malloc heap. + +info + Shows memory-allocation statistics, including the total heap size and the + amount currently in use. + +The total heap size is set by ``CONFIG_SYS_MALLOC_LEN``. + +Example +------- + +:: + + => malloc info + total bytes = 96 MiB + in use bytes = 700.9 KiB + +Configuration +------------- + +The malloc command is enabled by CONFIG_CMD_MALLOC which depends on +CONFIG_MALLOC_DEBUG. + +Return value +------------ + +The return value $? is 0 (true) on success, 1 (false) on failure. diff --git a/doc/usage/index.rst b/doc/usage/index.rst index bee7884a066..7ed79f89af6 100644 --- a/doc/usage/index.rst +++ b/doc/usage/index.rst @@ -97,6 +97,7 @@ Shell commands cmd/loadx cmd/loady cmd/luks + cmd/malloc cmd/meminfo cmd/mbr cmd/md diff --git a/test/cmd/Makefile b/test/cmd/Makefile index c43aefb4eb3..14cbdc3a6e6 100644 --- a/test/cmd/Makefile +++ b/test/cmd/Makefile @@ -26,6 +26,7 @@ endif obj-$(CONFIG_CMD_HASH) += hash.o obj-$(CONFIG_CMD_HISTORY) += history.o obj-$(CONFIG_CMD_LOADM) += loadm.o +obj-$(CONFIG_CMD_MALLOC) += malloc.o ifdef CONFIG_SANDBOX obj-$(CONFIG_CMD_MEMINFO) += meminfo.o endif diff --git a/test/cmd/malloc.c b/test/cmd/malloc.c new file mode 100644 index 00000000000..6cd52b68900 --- /dev/null +++ b/test/cmd/malloc.c @@ -0,0 +1,30 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Test for 'malloc' command + * + * Copyright 2025 Canonical Ltd + * Written by Simon Glass + */ + +#include +#include +#include +#include + +/* Test 'malloc info' command */ +static int cmd_test_malloc_info(struct unit_test_state *uts) +{ + struct malloc_info info; + + ut_assertok(malloc_get_info(&info)); + ut_assert(info.total_bytes >= CONFIG_SYS_MALLOC_LEN); + ut_assert(info.in_use_bytes < info.total_bytes); + + ut_assertok(run_command("malloc info", 0)); + ut_assert_nextlinen("total bytes = "); + ut_assert_nextlinen("in use bytes = "); + ut_assert_console_end(); + + return 0; +} +CMD_TEST(cmd_test_malloc_info, UTF_CONSOLE); From patchwork Wed Dec 10 00:07:10 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 876 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=1765325372; bh=wu+3p+4ZrnJt+weQC/JOjZ61yPrBPd0XmAg/GON2664=; 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=gYOBMchL/99YITAVQywnwsb28SlV7I8GHQyDdVragVj4+5FLdotA2qKItdN7+fKZY QdjCwwrDWn7sI8PXZ/rolEdr6IZ2TNODLVMnaEakSALX3yopuQCMY/oXSMLF7pabM+ VmAE5md5iFwtk7Qc9R2ChMyQRSz69cotsIi2J3fsFERl97mVbHhhLbL1eL8qm8NIpv pmHxLym+MLHQEzSuVQKs6bg8RpRAF57uLHWKUS8gUTH3Q4EsR07noCKW5Z//zZYrfs LaK7pooA6RlzfgB9668274jjWvpBFdK90Bw1nOFCALUbiyXwsa8R63pNIHoO1wAJkE ASldm7CwaJh5A== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 387CB68A3E for ; Tue, 9 Dec 2025 17:09:32 -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 o36qKhqgufF7 for ; Tue, 9 Dec 2025 17:09:32 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325372; bh=wu+3p+4ZrnJt+weQC/JOjZ61yPrBPd0XmAg/GON2664=; 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=gYOBMchL/99YITAVQywnwsb28SlV7I8GHQyDdVragVj4+5FLdotA2qKItdN7+fKZY QdjCwwrDWn7sI8PXZ/rolEdr6IZ2TNODLVMnaEakSALX3yopuQCMY/oXSMLF7pabM+ VmAE5md5iFwtk7Qc9R2ChMyQRSz69cotsIi2J3fsFERl97mVbHhhLbL1eL8qm8NIpv pmHxLym+MLHQEzSuVQKs6bg8RpRAF57uLHWKUS8gUTH3Q4EsR07noCKW5Z//zZYrfs LaK7pooA6RlzfgB9668274jjWvpBFdK90Bw1nOFCALUbiyXwsa8R63pNIHoO1wAJkE ASldm7CwaJh5A== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 2731F68957 for ; Tue, 9 Dec 2025 17:09:32 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325370; bh=gp/5p3S5jSfNCsffYFgfeVMFaqmxBvP+JEcWTiRh+B4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=huY8W36ImJSBYKyfJH9UuMRs52+8kWz6M9CivpYwaCspZQiSzwXEpIbeN6m2DCqqd giEgeKZ7ToM6IOuvju2OFESclp6K8XdnDX96TGKl5ulDd3g/ioCMAw1vbvodbb/eSB mPzf3RSdgGQl1l70amHqsv6dPXNNk5kygRfBDn44iwXTSWzJntuDgZELAcq6gQ5Kxd +kRxhlPiwN0PDdNloSTSvZwrfgzCz5D2Y65qvznr7Rr+TcCdHToNWBHm0mufg7Esos qpdYne30fUVazsBaBJeUt+N1CrNCNhblRPOMivCLi7T55DBQBR8zlZs8tW/pqilycG D9FmDt1z2Ef6Q== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 536B8687A7; Tue, 9 Dec 2025 17:09:30 -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 NMrtjawLKMc7; Tue, 9 Dec 2025 17:09:30 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325366; bh=V6rdZ7gIvAlUEQFdDzYbReu0/ClK2oLVGtlHg1WGpxs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=TkCFTLyJGaqXvns+PoooqYyxCa/2pli79CbhROqfsq3zv8B4jzZ2+7g30cbhPiuSG 9GBtAM4X/Caf/vyaXETy+K1ZcirRQy3KvBVqB5omrOL6hkfEJHmWFO7MQsW938Gc4x sonEEhZPghIDVfe/W7UT+AbwOVvFyUqcN4+o3sc+ljnfns90jwBnfn1ziF8YHHJOlR bT6nT8+QWigAaSDuhqq4+Ti1oQYdf/zFYSE+Ig/cYbEWCIeCH3PKqJcHO+NEoVGc9m E94+ls8RqJk0wom3SUoK1eEPLGPDxZ6W+jvL5ixAZ10QZp89fpmX0T4Sis90Jo501s Q4mNF82uAfXHw== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id B725B6884F; Tue, 9 Dec 2025 17:09:25 -0700 (MST) From: Simon Glass To: U-Boot Concept Date: Tue, 9 Dec 2025 17:07:10 -0700 Message-ID: <20251210000737.180797-20-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: LEI7TPDK4YIT6H4CORYORWTFWZ5ZDXZD X-Message-ID-Hash: LEI7TPDK4YIT6H4CORYORWTFWZ5ZDXZD 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 19/35] malloc: Add call counters for malloc, free, realloc 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 counters to track the number of calls to malloc(), free(), and realloc(). These are displayed by the 'malloc info' command and accessible via malloc_get_info(). Co-developed-by: Claude Signed-off-by: Simon Glass --- cmd/malloc.c | 7 +++++-- common/dlmalloc.c | 18 ++++++++++++++++++ doc/usage/cmd/malloc.rst | 12 ++++++++---- include/malloc.h | 6 ++++++ test/cmd/malloc.c | 4 ++++ 5 files changed, 41 insertions(+), 6 deletions(-) diff --git a/cmd/malloc.c b/cmd/malloc.c index cb2fa34155b..3750a16158b 100644 --- a/cmd/malloc.c +++ b/cmd/malloc.c @@ -21,8 +21,11 @@ static int do_malloc_info(struct cmd_tbl *cmdtp, int flag, int argc, if (ret) return CMD_RET_FAILURE; - printf("total bytes = %s\n", format_size(buf, info.total_bytes)); - printf("in use bytes = %s\n", format_size(buf, info.in_use_bytes)); + printf("total bytes = %s\n", format_size(buf, info.total_bytes)); + printf("in use bytes = %s\n", format_size(buf, info.in_use_bytes)); + printf("malloc count = %lu\n", info.malloc_count); + printf("free count = %lu\n", info.free_count); + printf("realloc count = %lu\n", info.realloc_count); return 0; } diff --git a/common/dlmalloc.c b/common/dlmalloc.c index b0845e6ea7c..b8de42cc47e 100644 --- a/common/dlmalloc.c +++ b/common/dlmalloc.c @@ -646,6 +646,12 @@ ulong mem_malloc_start; ulong mem_malloc_end; ulong mem_malloc_brk; +#ifndef NO_MALLOC_STATS +static ulong malloc_count; +static ulong free_count; +static ulong realloc_count; +#endif /* !NO_MALLOC_STATS */ + #endif /* __UBOOT__ */ #ifndef SMALLCHUNKS_AS_FUNCS @@ -3699,6 +3705,9 @@ int malloc_get_info(struct malloc_info *info) info->total_bytes = mem_malloc_end - mem_malloc_start; info->in_use_bytes = mi.uordblks; + info->malloc_count = malloc_count; + info->free_count = free_count; + info->realloc_count = realloc_count; return 0; } @@ -4926,6 +4935,9 @@ void* dlmalloc_impl(size_t bytes) { if (!(gd->flags & GD_FLG_FULL_MALLOC_INIT)) return malloc_simple(bytes); #endif +#if !NO_MALLOC_STATS + malloc_count++; +#endif /* Return NULL if not initialized yet */ if (!mem_malloc_start && !mem_malloc_end) @@ -5092,6 +5104,9 @@ void* dlmalloc_impl(size_t bytes) { STATIC_IF_MCHECK void dlfree_impl(void* mem) { #ifdef __UBOOT__ +#if !NO_MALLOC_STATS + free_count++; +#endif #if CONFIG_IS_ENABLED(SYS_MALLOC_F) /* free() is a no-op - all the memory will be freed on relocation */ if (!(gd->flags & GD_FLG_FULL_MALLOC_INIT)) { @@ -5662,6 +5677,9 @@ static void internal_inspect_all(mstate m, STATIC_IF_MCHECK void* dlrealloc_impl(void* oldmem, size_t bytes) { #ifdef __UBOOT__ +#if !NO_MALLOC_STATS + realloc_count++; +#endif #if CONFIG_IS_ENABLED(SYS_MALLOC_F) if (!(gd->flags & GD_FLG_FULL_MALLOC_INIT)) { /* This is harder to support and should not be needed */ diff --git a/doc/usage/cmd/malloc.rst b/doc/usage/cmd/malloc.rst index d21aa6c1421..7b08b358258 100644 --- a/doc/usage/cmd/malloc.rst +++ b/doc/usage/cmd/malloc.rst @@ -19,8 +19,9 @@ Description The malloc command shows information about the malloc heap. info - Shows memory-allocation statistics, including the total heap size and the - amount currently in use. + Shows memory-allocation statistics, including the total heap size, the + amount currently in use, and call counts for malloc(), free(), and + realloc(). The total heap size is set by ``CONFIG_SYS_MALLOC_LEN``. @@ -30,8 +31,11 @@ Example :: => malloc info - total bytes = 96 MiB - in use bytes = 700.9 KiB + total bytes = 96 MiB + in use bytes = 700.9 KiB + malloc count = 1234 + free count = 567 + realloc count = 89 Configuration ------------- diff --git a/include/malloc.h b/include/malloc.h index 7e276d444ab..0d8a2a43f2a 100644 --- a/include/malloc.h +++ b/include/malloc.h @@ -675,10 +675,16 @@ void mspace_inspect_all(mspace msp, * * @total_bytes: Total bytes available in the heap * @in_use_bytes: Current bytes allocated (in use by application) + * @malloc_count: Number of calls to malloc() + * @free_count: Number of calls to free() + * @realloc_count: Number of calls to realloc() */ struct malloc_info { ulong total_bytes; ulong in_use_bytes; + ulong malloc_count; + ulong free_count; + ulong realloc_count; }; /* Memory pool boundaries */ diff --git a/test/cmd/malloc.c b/test/cmd/malloc.c index 6cd52b68900..f9d41a36cad 100644 --- a/test/cmd/malloc.c +++ b/test/cmd/malloc.c @@ -19,10 +19,14 @@ static int cmd_test_malloc_info(struct unit_test_state *uts) ut_assertok(malloc_get_info(&info)); ut_assert(info.total_bytes >= CONFIG_SYS_MALLOC_LEN); ut_assert(info.in_use_bytes < info.total_bytes); + ut_assert(info.malloc_count > 0); ut_assertok(run_command("malloc info", 0)); ut_assert_nextlinen("total bytes = "); ut_assert_nextlinen("in use bytes = "); + ut_assert_nextlinen("malloc count = "); + ut_assert_nextlinen("free count = "); + ut_assert_nextlinen("realloc count = "); ut_assert_console_end(); return 0; From patchwork Wed Dec 10 00:07:11 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 877 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=1765325378; bh=Q4E+pBY9beN+QGhJ1OxC3+EYhFQ1AIct+F0R/91Rckk=; 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=poxs4OgSyNEaQQOmn2vGSxPoU7RpkVaDj6lKTTKz5GHZN/iUJjtE60+qDxwaftIzi tKOZUfNX7fCxI8yebiED2wPuHIjAKGSZolLnq4Sjf3Js8/8ra7mgQqlCdMb0HXyAyX DsPbF8oFMzLQzR43Btr/sFMXUcpOigBo8C+XBU8QOAS1whm6mGn0JSTf4O5YC+nJbv sE332hrAWEnN9JNQ4T292oxCLrhIX5hwVCZD4IF3wYGJKE2iK+D7QTStyEdIEo+mr3 2AMWPeZZXEykJUXyFkcTl1kSs0yqoZMXrKkDtj+LuYOtG+pghwqHarP7v3M+E8TZvK /GGyMiPvOpVmw== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 2405B68972 for ; Tue, 9 Dec 2025 17:09:38 -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 HTMcRmKOhlgr for ; Tue, 9 Dec 2025 17:09:38 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325378; bh=Q4E+pBY9beN+QGhJ1OxC3+EYhFQ1AIct+F0R/91Rckk=; 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=poxs4OgSyNEaQQOmn2vGSxPoU7RpkVaDj6lKTTKz5GHZN/iUJjtE60+qDxwaftIzi tKOZUfNX7fCxI8yebiED2wPuHIjAKGSZolLnq4Sjf3Js8/8ra7mgQqlCdMb0HXyAyX DsPbF8oFMzLQzR43Btr/sFMXUcpOigBo8C+XBU8QOAS1whm6mGn0JSTf4O5YC+nJbv sE332hrAWEnN9JNQ4T292oxCLrhIX5hwVCZD4IF3wYGJKE2iK+D7QTStyEdIEo+mr3 2AMWPeZZXEykJUXyFkcTl1kSs0yqoZMXrKkDtj+LuYOtG+pghwqHarP7v3M+E8TZvK /GGyMiPvOpVmw== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 0C57B68A43 for ; Tue, 9 Dec 2025 17:09:38 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325375; bh=ENqdcg+lb98QYzjEvvE3GH8Dz+9GVAyHIjp64A4jO+Q=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=a2EUXvGMd8xzqwaV6zf1CCAKL0r8X5jvnDut1Ej0HRmIY+R3LwRPuo9S9pwMkcqz7 Rpmdm+jIGampGs5CCbS/8IV7q77FYI9/Bl4HSA3vr8EPIGYZPqbO2OFmwgiQqI2lDh 12HaN8MGLKXtO3g/Hv5zTpOTuaWjXOdW6PVfZWc0Ub9dOaeLmnWKkzxJbTREWqqfyh hXBdxyQTVntCoYl1B8US3LyJGI4O++bxQUFiUZOhY6Yh8xLl1LZWLIgJtOQlxFBQFT fzcRvt56nbbny7aCPS9to5dHbsR0cnmyCxrGCK6AQrEk4OvbObFQp6h2AZTNGY72NO SZjasUVGUCRZw== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 72225687A7; Tue, 9 Dec 2025 17:09: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 10026) with ESMTP id NbTUVHAKt2q3; Tue, 9 Dec 2025 17:09:35 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325371; bh=0o6Ifhy9P3JXgA6/r2AuClT6wjj4RvDJIuqOA37UEww=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=c1BuGOjm3VTSnZ90466hOwCoSbK0/mtqcwsKnoEBXcw2SFfWX1LXrWwPcg15oV090 FjwxBEKVZRGMAeTxwOaBF9xA3rIIgY61GepypwRU3nafdyjTh1L82sN0nZBeX44ew3 kWGDk3vaywbspsXa36LuMT2CY1zZRSaY9KSfbg84QDBDSFDLTE4JB087HuMKaa5wZA VNkcC+HamhdF0RZMPLgsj5FNUZkdJI1xmKJ9eOEGioEIPJDVVaDIbMP7Hd9F3760ZC VBOV9TCqoEYg/p2NGSFebzzAv9COZXuUBrD145vQwXzGV11KuoouND9feENyHm7bTS +kpHK6grcqnhw== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id B4EE36884F; Tue, 9 Dec 2025 17:09:30 -0700 (MST) From: Simon Glass To: U-Boot Concept Date: Tue, 9 Dec 2025 17:07:11 -0700 Message-ID: <20251210000737.180797-21-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: W5DCWM6A6U4NU5OJY7JTVDBECDDZ4M6X X-Message-ID-Hash: W5DCWM6A6U4NU5OJY7JTVDBECDDZ4M6X 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 20/35] malloc: Add a Kconfig option for heap protection 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 CONFIG_MCHECK_HEAP_PROTECTION option to enable mcheck heap protection. Convert all uses of MCHECK_HEAP_PROTECTION to use the CONFIG_ -prefixed version to work with Kconfig. Disable this option when tracing is enabled, since the mcheck hooks (mcheck_pedantic_prehook(), etc.) interfere with function tracing. Co-developed-by: Claude Signed-off-by: Simon Glass --- Kconfig | 10 ++++++++++ common/board_f.c | 2 +- common/dlmalloc.c | 6 +++--- common/mcheck_core.inc.h | 6 +++--- 4 files changed, 17 insertions(+), 7 deletions(-) diff --git a/Kconfig b/Kconfig index 86276c89f38..fb320fdb418 100644 --- a/Kconfig +++ b/Kconfig @@ -345,6 +345,16 @@ config MALLOC_DEBUG enables additional assertions and the malloc_get_info() function to retrieve memory-allocation statistics. +config MCHECK_HEAP_PROTECTION + bool "Enable mcheck heap protection" + depends on !TRACE + help + Enable heap protection using the mcheck library. This adds canary + values before and after each allocation to detect buffer overflows + and underflows, double-frees, and memory corruption. This + significantly increases memory overhead and should only be used for + debugging. + config SPL_SYS_MALLOC_F bool "Enable malloc() pool in SPL" depends on SPL_FRAMEWORK && SYS_MALLOC_F && SPL diff --git a/common/board_f.c b/common/board_f.c index a3e4c69d449..9dce08002c5 100644 --- a/common/board_f.c +++ b/common/board_f.c @@ -749,7 +749,7 @@ static int setup_reloc(void) if (gd->flags & GD_FLG_SKIP_RELOC) { debug("Skipping relocation due to flag\n"); } else { -#ifdef MCHECK_HEAP_PROTECTION +#ifdef CONFIG_MCHECK_HEAP_PROTECTION mcheck_on_ramrelocation(gd->reloc_off); #endif debug("Relocation Offset is: %08lx\n", gd->reloc_off); diff --git a/common/dlmalloc.c b/common/dlmalloc.c index b8de42cc47e..7258a7dda84 100644 --- a/common/dlmalloc.c +++ b/common/dlmalloc.c @@ -572,7 +572,7 @@ MAX_RELEASE_CHECK_RATE default: 4095 unless not HAVE_MMAP #define DEBUG 1 #endif -#ifdef MCHECK_HEAP_PROTECTION +#if CONFIG_IS_ENABLED(MCHECK_HEAP_PROTECTION) #define STATIC_IF_MCHECK static #undef MALLOC_COPY #undef MALLOC_ZERO @@ -5451,7 +5451,7 @@ static void* internal_memalign(mstate m, size_t alignment, size_t bytes) { } return mem; } -#endif /* !CONFIG_MCHECK_HEAP_PROTECTION || MSPACES */ +#endif /* !MCHECK_HEAP_PROTECTION || MSPACES */ /* Common support for independent_X routines, handling @@ -5937,7 +5937,7 @@ size_t dlmalloc_usable_size(const void* mem) { return 0; } -#ifdef MCHECK_HEAP_PROTECTION +#if CONFIG_IS_ENABLED(MCHECK_HEAP_PROTECTION) #include "mcheck_core.inc.h" void *dlmalloc(size_t bytes) diff --git a/common/mcheck_core.inc.h b/common/mcheck_core.inc.h index 69021409922..7caa9ac8dff 100644 --- a/common/mcheck_core.inc.h +++ b/common/mcheck_core.inc.h @@ -45,8 +45,8 @@ * an array, for index(+1/-1) errors. * * U-Boot is a BL, not an OS with a lib. Activity of the library is set not in runtime, - * rather in compile-time, by MCHECK_HEAP_PROTECTION macro. That guarantees that - * we haven't missed first malloc. + * rather in compile-time, by CONFIG_MCHECK_HEAP_PROTECTION macro. That + * guarantees that we haven't missed first malloc. */ /* @@ -59,7 +59,7 @@ #define _MCHECKCORE_INC_H 1 #include "mcheck.h" -#if defined(MCHECK_HEAP_PROTECTION) +#if CONFIG_IS_ENABLED(MCHECK_HEAP_PROTECTION) #define mcheck_flood memset // these are from /dev/random: From patchwork Wed Dec 10 00:07:12 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 878 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=1765325380; bh=UPjrcgt4BKht/M2Rcde7WwacYxmIqsbZXIAoeX9O9Uo=; 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=q/CljNlq1Q/WaBDx89Iykh2Nc+v+SriX/QHS9BSmpqn9pB0xnyAmk4Zpwl+XKIX+P AYt1MgBFYdw46kSmwfF82p0QGCHqSQDrf/8Dl4iI5a2bVTF7i1hZjuWgygVyr2AEbI jZX5DNcl2iZhrhXQos3C7jkWigEOeGL1blXXAg38Tk4yDyLxE8bU8JOgOG/wojr1ZM fHld2cDUXFqRa5kJnUVHUK4RqvMr3jTrnyieXp+IRBjKCLMPVzKgYqj5QV+gDHsX+g Vlbo2tyrGrH/yLqohiQcGQ7X04zePRXuJGmpZH9glJQNfkYl+0CS0NwDjh7kmHE7nJ mIS0TtWmZdClg== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 8DE1168A4B for ; Tue, 9 Dec 2025 17:09:40 -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 CBZ_aGZgffso for ; Tue, 9 Dec 2025 17:09:40 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325378; bh=UPjrcgt4BKht/M2Rcde7WwacYxmIqsbZXIAoeX9O9Uo=; 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=pv74htzhA2gUnHxpEOpWUf632ZnHIWurC+Ia++IGO7I0L+sOQmOoPWb18TAHQ4lHL O1DiSsnKh9SY4M6k0PYAjaYQOs5hcKDtgW8VeRrpU4BKQ5yFSPtZtBNAlLC4FIzO8C Fon/MCSu7q53A7dnREHalkCMZd5jMAhsM5psAbbKIqMcLWXA5+ZEgXVbNwS8IGWWer bUs3AIAuEhEsX5pn0kh0f1NEGYxDKFqHzOLKJmlFaMwq5gdxpmqCu0ib1QH1F1J2td W/N32S9EjPMQw1kgSJkgJ0s1omHo9L3+GxIOIFdgmJfi7P+b699AYZG29lrezN80qH th+mXmnb7H9iw== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 92D9A68A22 for ; Tue, 9 Dec 2025 17:09:38 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325376; bh=x38WOAXNJa05iCwbPBjWCSs42aKrRzLrrHG1G0Dl3gI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=FRumRme3asvjlw/pnF5V2c2OdxBjyB9D/gOen6E2Fti1oaNrZipT1tl8cffJypReJ hr+9haHQr1iKYjnXHkL8ezH5eUjH1gILITgrml2yhmgNSvmHMgDOa5fm9Jaokc1wFz 6lBuZCfnW6Rxesy4HrcoojUlA8wethJApW6Ntd0MVRhLSK+j4YRjjeaxZoCsFLftua +0KKSrS+dnbYgWm/WKrFfsJyACZf9oGArprRkp41Ts4dByV5Q/nb69s1kRRMj7E8Gs 9WmLdxMWZ68hE06f+dn/TeGQ9EWdM3FHoySkxBbuMr7Tl0ji43479W3d18et777b02 QEupZdn4lXbuA== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id BFB1468972; Tue, 9 Dec 2025 17:09:36 -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 igNOo-v9gLM0; Tue, 9 Dec 2025 17:09:36 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325376; bh=MuXqGfNchGhd7eT675CzZc5ifU3jNhz+HLBmN4CCfdQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BLmIZAjB2bZNT8aFbgDWP6M0O87RlNw3RMn1S2nzcZXn1mjLNDnr030PVNBeS/mr3 FDrGEMezbIFO9M5aEJe1i9yeP8CyfKa5lgtnjhQqSqcXla0fx/R8jRNGJ2ounC5vk/ SwEayx+dd+ut8ir37cBZ6KxSN215lXEN+KQ2VVteqD88F7QZRujNFYAhbUXI97K2c8 5MaPqc/PJjmYg1iB+inXqk8IFCVrMK3P+rYPOpDKshjvpSYTBi1WxhH9ei/2gq/RR5 VXZgQ1sYNAFhv7tac/4dtZl43ekljAXlmfSTH9zInD1VJnprYsPojyz6ndNQ0YyR1P PtTWTq6x+4ZeQ== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id CA69F6884F; Tue, 9 Dec 2025 17:09:35 -0700 (MST) From: Simon Glass To: U-Boot Concept Date: Tue, 9 Dec 2025 17:07:12 -0700 Message-ID: <20251210000737.180797-22-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: LIDAFBISFOSDZMQULKSWYHNGR7XE72OL X-Message-ID-Hash: LIDAFBISFOSDZMQULKSWYHNGR7XE72OL 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 21/35] malloc: Move mcheck block after includes 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 Move the CONFIG_MCHECK_HEAP_PROTECTION block after the standard includes so that size_t and string functions are available for the inline MALLOC_ZERO and MALLOC_COPY functions. Add the and includes needed for mcheck. Co-developed-by: Claude Signed-off-by: Simon Glass --- common/dlmalloc.c | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/common/dlmalloc.c b/common/dlmalloc.c index 7258a7dda84..f59b1151606 100644 --- a/common/dlmalloc.c +++ b/common/dlmalloc.c @@ -572,21 +572,6 @@ MAX_RELEASE_CHECK_RATE default: 4095 unless not HAVE_MMAP #define DEBUG 1 #endif -#if CONFIG_IS_ENABLED(MCHECK_HEAP_PROTECTION) -#define STATIC_IF_MCHECK static -#undef MALLOC_COPY -#undef MALLOC_ZERO -static inline void MALLOC_ZERO(void *p, size_t sz) { memset(p, 0, sz); } -static inline void MALLOC_COPY(void *dest, const void *src, size_t sz) { memcpy(dest, src, sz); } -#else -#define STATIC_IF_MCHECK -#define dlmalloc_impl dlmalloc -#define dlfree_impl dlfree -#define dlrealloc_impl dlrealloc -#define dlmemalign_impl dlmemalign -#define dlcalloc_impl dlcalloc -#endif - #define LACKS_FCNTL_H #define LACKS_UNISTD_H #define LACKS_SYS_PARAM_H @@ -633,12 +618,29 @@ static inline void MALLOC_COPY(void *dest, const void *src, size_t sz) { memcpy( #include #include #include +#include #include #include +#include #include DECLARE_GLOBAL_DATA_PTR; +#if CONFIG_IS_ENABLED(MCHECK_HEAP_PROTECTION) +#define STATIC_IF_MCHECK static +#undef MALLOC_COPY +#undef MALLOC_ZERO +static inline void MALLOC_ZERO(void *p, size_t sz) { memset(p, 0, sz); } +static inline void MALLOC_COPY(void *dest, const void *src, size_t sz) { memcpy(dest, src, sz); } +#else +#define STATIC_IF_MCHECK +#define dlmalloc_impl dlmalloc +#define dlfree_impl dlfree +#define dlrealloc_impl dlrealloc +#define dlmemalign_impl dlmemalign +#define dlcalloc_impl dlcalloc +#endif + static bool malloc_testing; /* enable test mode */ static int malloc_max_allocs; /* return NULL after this many calls to malloc() */ From patchwork Wed Dec 10 00:07:13 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 879 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=1765325385; bh=0MHfWf39LlKBKmS0RG+mBUeBhCKq92QwxAvgZYKszX0=; 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=bLHqGPf0wzTzIs/EMcWTC4KtF3ImfTKZxzqDGVRChDSbnith5gxYSbkMTELWqPs1x vkvDA7H5OomI5R7j1gIl/JrgdblWoy6x1iEKB6N0qyl+zCocfuC+EfK6o3bidd59j+ Fu6+xq4HJFBnqkZqWD1lLrHQVWgl+QrQ620TlVZc4FCfvLF25Y8Gvb2SY+JsRENVEo Tu3bblMSFd82zaRiIRrBuefjr+Z3QqxsNZAPLYYsgNBQfA+LN91yV/i6x8WEYK2Mf/ 8son7pgyKPCjpF7HFmfFYQSCcXC1NvdIzNIxl14DnlJSyYJrQH2SuTnX6o9fTp3WJf yAUbq6nMPDm+w== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 1098C68A22 for ; Tue, 9 Dec 2025 17:09:45 -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 soYS3BLDhYga for ; Tue, 9 Dec 2025 17:09:45 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325384; bh=0MHfWf39LlKBKmS0RG+mBUeBhCKq92QwxAvgZYKszX0=; 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=tc4bN3nMSJbmwzwFXjKaklpprv2l6qaJF8eVapT7u1k4WvYCEKs6d9WRrocVCV+GC LPvymtDDmf23HYoC1vvPcy0y8JPdrGGkVFqTInDkR3zLMDa7RHKE71m4K8/IS5eQKk xCZ5cj4l/A7wNtfYYVxyhkE92hSrAYc7OOkdcMw9PCxzwxpEDDeTg0+XfRHIO04B0y lmnuZLxKCb2xquxdEzeAoGHXZjvbKuQ5+30a/I/RBMVu6loNQIN1HIgLZI6THuPNQC 5tS5SO85qvtdn9dCmHsK6EIXJKRWXvs/DMSFM8w/3Nd3Tbs83v8rxN7857Q2kqqNqU dbXiolnrTigDg== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id F30806884F for ; Tue, 9 Dec 2025 17:09:44 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325382; bh=BBWm3Kna+hb+ZJHkyJp9xEk0M7dqYZQ98Jb/45e8ZCc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=PUqZ/5wYUq22fjBqDWQyHtgikT40piNlm7I37qg0s7Cv8lSw1mEtWGPaYaa3GEjDz BmHNpg01JQlvgtUjryvjRDxkPYZtN5KI4nq3GG5b9g5HGPoN6WQN+9hNv9+gVUuCkM ujKOM5Tn9HwHBSzyDWCgIAposSqr1Tb8c0bK8ePLZ//6rjJqb88yLIvnuR7v+lMvo4 fv69C+yPNbc18xcrU/gfGaDCeOUWQ3qHaF4ObOZM57DSDkz0vWLlUN4kWq1KXjuSG+ aSr8+oPE6VKobDI3JC4hbfCYaeB72H9QRnGXp3twuk9aiXObi5HWHNjy4vzjTtB6Pg TzZAR+H9Do68w== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id D7F9868973; Tue, 9 Dec 2025 17:09:42 -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 UzHmUAGkh9Vi; Tue, 9 Dec 2025 17:09:42 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325377; bh=51Sl7KNf8f8AqTBIb0+FzR9WJq1kddOl0CuV2weYBBs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=OnOJOB+FM9J3alzAYISfQyiKJuOylbCuzoLICW15yIije3MLL3k1JdfSAW1eLj/hS /PdmVXdPbm3UCHFHaa/wG8psWBVke1zqD1sBHZXHeS8qAx7GTUs3dC82MwJRtyULG9 1tfkKXXPsQrd9gECGdFVhGhXM5RFmSK+ThZzLjECxWn9HClJtE5Dpr+BhYQK1JjQ+x 1ffG2JMveM8l6HpQ7/gCrMS5+4Pg41q+oEX+lJrZuA7hKNzI96sE+AfZ1kvP62U9aU YuYXd9QZl5AmTxBXDQzjkPYK1LghVjO53mrO1qMo5b3rZXhU484UUO+Ufomx3LMlh1 9WodvACzY4+Bw== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id 204AA687A7; Tue, 9 Dec 2025 17:09:37 -0700 (MST) From: Simon Glass To: U-Boot Concept Date: Tue, 9 Dec 2025 17:07:13 -0700 Message-ID: <20251210000737.180797-23-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: 6VMAHSCQNPUOHFH7AKIIMENOLMZ4623P X-Message-ID-Hash: 6VMAHSCQNPUOHFH7AKIIMENOLMZ4623P 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 22/35] malloc: Fix internal calls and memalign for mcheck 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 With mcheck enabled, internal_malloc and internal_free go through mcheck wrappers which expect user pointers (after mcheck header), not raw allocator pointers. Also, dlmemalign_impl() needs a properly aligned base pointer for mcheck to place its header correctly. Use _impl functions directly for internal_malloc/internal_free. Call internal_memalign() for alignments greater than MALLOC_ALIGNMENT. Co-developed-by: Claude Signed-off-by: Simon Glass --- common/dlmalloc.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/common/dlmalloc.c b/common/dlmalloc.c index f59b1151606..bcdb4d29424 100644 --- a/common/dlmalloc.c +++ b/common/dlmalloc.c @@ -4092,8 +4092,17 @@ static void unlink_large_chunk(mstate M, tchunkptr X) { #define internal_free(m, mem)\ if (m == gm) dlfree(mem); else mspace_free(m,mem); #else /* MSPACES */ +/* + * When mcheck is enabled, internal calls must use the _impl functions + * to avoid going through the mcheck wrappers which expect user pointers. + */ +#ifdef CONFIG_MCHECK_HEAP_PROTECTION +#define internal_malloc(m, b) dlmalloc_impl(b) +#define internal_free(m, mem) dlfree_impl(mem) +#else #define internal_malloc(m, b) dlmalloc(b) #define internal_free(m, mem) dlfree(mem) +#endif #endif /* MSPACES */ #endif /* ONLY_MSPACES */ @@ -5339,7 +5348,6 @@ static mchunkptr try_realloc_chunk(mstate m, mchunkptr p, size_t nb, } #endif /* !defined(__UBOOT__) || !NO_REALLOC_IN_PLACE */ -#if !CONFIG_IS_ENABLED(MCHECK_HEAP_PROTECTION) || MSPACES static void* internal_memalign(mstate m, size_t alignment, size_t bytes) { void* mem = 0; if (alignment < MIN_CHUNK_SIZE) /* must be at least a minimum chunk size */ @@ -5453,7 +5461,6 @@ static void* internal_memalign(mstate m, size_t alignment, size_t bytes) { } return mem; } -#endif /* !MCHECK_HEAP_PROTECTION || MSPACES */ /* Common support for independent_X routines, handling @@ -5808,9 +5815,13 @@ void* dlmemalign_impl(size_t alignment, size_t bytes) { return memalign_simple(alignment, bytes); #endif #endif - if (alignment <= MALLOC_ALIGNMENT) { + /* + * With mcheck, the wrapper handles alignment via mcheck_memalign_prehook() + * which adds space for the header aligned to the requested alignment. + * The base pointer must still be properly aligned for this to work. + */ + if (alignment <= MALLOC_ALIGNMENT) return dlmalloc_impl(bytes); - } return internal_memalign(gm, alignment, bytes); } From patchwork Wed Dec 10 00:07:14 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 880 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=1765325387; bh=lfarUQHq5lpwJgUNK1aKCMKUp2pbrzKdnPuXmffLxKo=; 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=KPHnocV9d7PymS291I3SwrSPN4glZ9ifGGbC7Nw07TYvsv5SfL2Z3z5CHXfsza5L6 KAWCnPDgxohf1Z/nlKqiX0VQzIIJWtwPezHqaM1Mk0HqgmE2Qh6f1K98ZyIbzVm2M5 cPKYZZMBYFZYY8SUfuOo1MWeNGyy3Yw5uuoLWcCMeknVJHh/FDg1aQPPdpoCt+8soH 4ZCKuja/MkrR56jl9Z5hyhxoxNHkbAWCjnP8sbHtoXBLfPfcuNkUi/BTCGHt9oBP9I TJf4R5QwJmHIy7N1CmXFcDlRDN7J+eQ345m6ZyS173m94JFR1sY8OdP6IH4Cn9oF97 cp5l6OAkt/irw== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 7D27268973 for ; Tue, 9 Dec 2025 17:09:47 -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 YqHgG5lzBKZc for ; Tue, 9 Dec 2025 17:09:47 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325385; bh=lfarUQHq5lpwJgUNK1aKCMKUp2pbrzKdnPuXmffLxKo=; 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=mx9epStCqafg7iXfiPhq/V86XToMC3gZD8g8neS6K7RZqfAU/MAUYxkV+N5Ldntgy VYCYWfTJ5dyGvx536t4/TLtnOE1a0ocrLjzJBjoJR2LbQ3HkqkjiYKe0H/H46qhrKI ctBdQG6hjeMuZk3Y4CEcnMswwMWMzDDxzBoDrmPYOhx6nrZmy+g5etPrm52LC/PpdV CEi9qpyYXhBN9TxyVfkIOU1uf9CAkUJBbAZm+D+9edgWnrG7Y4TF+EN3W922Yn4g+y XMAUhhZu86o/kEejX56SVE2S5aPCPzI4H/RIe/2MkccW8P4e/2O8MJwcygxQFz1KZP /FkwFyeGmt4rw== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 7656268A44 for ; Tue, 9 Dec 2025 17:09:45 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325382; bh=pE+Cw/xl/FW4lz4jZPkATYX1xFqgh/c3ZVy96Le7oas=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=k867dXp1XlsgVBUwG9pfN2pCkILYgdHW0IpUKxyDLjLx46Wkrzdh6szkW6xspUdCS yDdPbRLaImbFM/WmwPJh4ilHB8/kOSW7aIQOga7GOJzslTqtfZRbpd3fxMhrBW4/FN wNTw1WCFVTM2uPSEff9tvX5Q2A8M4XuVJp5hB2lNZ3lNmJ7hX7WjxShK/pVZp3WGyV DjhwY13AbVopNw99FCLBCcHQH0OJyyl0T4Z8qevC88MGsakkMiom/pvSyFn0CTmYnJ KbfgmPH/0B8Tui+iQtpehuO5DdQ++BjZJB8QrixOyrVzV3k0ff1dCUfGKCLr3STjxO 1+nMQ763b7O4w== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id D7FA5689EA; Tue, 9 Dec 2025 17:09:42 -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 sJo9aeuKt10D; Tue, 9 Dec 2025 17:09:42 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325382; bh=Yb/EqtJ1GB/V9d4PymG+VkQxAJgcRhphLGh1Ster3Ys=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=d5CRZ7PdqNZ90JZNrMa84QxkqhiikyINmstJDO6jecFEeiq/08xQZbZb3tINPcZ+M AH/VgpZmAbEAhAuRowFW3cKshFbpVpO0/878pohE6KBYyshajxTPXuqRhBahXMUhYz iYYVmkL1t8vc/fT4sjKLg7joF1xU09FxPFGBRwZrnSWbqv6wWXqu/UfDla/GFgx+Fc MqHi112t5JWYrystu1a5tRB49CLUzABURP0zGaSa9jxxuZTDbVZQB5e1X3Nk90nTHN vXdu5NjhGtjNi4RiVVMpAIQs9XOiwmQtsS7cz41j2ltTtj0kW4iURa19ZDNmHeFNCl 1uUXpZGpkHLHQ== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id 037826884F; Tue, 9 Dec 2025 17:09:41 -0700 (MST) From: Simon Glass To: U-Boot Concept Date: Tue, 9 Dec 2025 17:07:14 -0700 Message-ID: <20251210000737.180797-24-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: 63V5QL37GEPGZEDYEHCBREVRRQOIG4ZK X-Message-ID-Hash: 63V5QL37GEPGZEDYEHCBREVRRQOIG4ZK 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 23/35] malloc: Update the valloc functions to use mcheck wrappers 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 Fix dlposix_memalign(), dlvalloc(), and dlpvalloc() to call dlmemalign() instead of dlmemalign_impl() or internal_memalign() directly. This ensures these functions go through the mcheck wrappers when CONFIG_MCHECK_HEAP_PROTECTION is enabled. Without this fix, internal_memalign is undefined when mcheck is enabled, causing a build error. Co-developed-by: Claude Signed-off-by: Simon Glass --- common/dlmalloc.c | 33 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/common/dlmalloc.c b/common/dlmalloc.c index bcdb4d29424..e288d94d433 100644 --- a/common/dlmalloc.c +++ b/common/dlmalloc.c @@ -4096,7 +4096,7 @@ static void unlink_large_chunk(mstate M, tchunkptr X) { * When mcheck is enabled, internal calls must use the _impl functions * to avoid going through the mcheck wrappers which expect user pointers. */ -#ifdef CONFIG_MCHECK_HEAP_PROTECTION +#if CONFIG_IS_ENABLED(MCHECK_HEAP_PROTECTION) #define internal_malloc(m, b) dlmalloc_impl(b) #define internal_free(m, mem) dlfree_impl(mem) #else @@ -5827,39 +5827,32 @@ void* dlmemalign_impl(size_t alignment, size_t bytes) { int dlposix_memalign(void** pp, size_t alignment, size_t bytes) { void* mem = 0; - if (alignment == MALLOC_ALIGNMENT) - mem = dlmalloc_impl(bytes); - else { - size_t d = alignment / sizeof(void*); - size_t r = alignment % sizeof(void*); - if (r != 0 || d == 0 || (d & (d-SIZE_T_ONE)) != 0) - return EINVAL; - else if (bytes <= MAX_REQUEST - alignment) { - if (alignment < MIN_CHUNK_SIZE) - alignment = MIN_CHUNK_SIZE; - mem = internal_memalign(gm, alignment, bytes); - } - } + size_t d = alignment / sizeof(void*); + size_t r = alignment % sizeof(void*); + + if (r != 0 || d == 0 || (d & (d-SIZE_T_ONE)) != 0) + return EINVAL; + if (bytes > MAX_REQUEST - alignment) + return ENOMEM; + mem = dlmemalign(alignment, bytes); if (mem == 0) return ENOMEM; - else { - *pp = mem; - return 0; - } + *pp = mem; + return 0; } void* dlvalloc(size_t bytes) { size_t pagesz; ensure_initialization(); pagesz = mparams.page_size; - return dlmemalign_impl(pagesz, bytes); + return dlmemalign(pagesz, bytes); } void* dlpvalloc(size_t bytes) { size_t pagesz; ensure_initialization(); pagesz = mparams.page_size; - return dlmemalign_impl(pagesz, (bytes + pagesz - SIZE_T_ONE) & ~(pagesz - SIZE_T_ONE)); + return dlmemalign(pagesz, (bytes + pagesz - SIZE_T_ONE) & ~(pagesz - SIZE_T_ONE)); } void** dlindependent_calloc(size_t n_elements, size_t elem_size, From patchwork Wed Dec 10 00:07:15 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 882 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=1765325392; bh=QLODlkEpFM8iy9h9ejW/1G9QT0hw+9/nY+xndZpNVfM=; 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=DkweZZAS+lxSkxqOtnT+N2AbFpZjBBsmcV8bYCwRcoTqdUlEbbrtD5TN8hstsWe19 25yTwmaNwiBNTr7XceKH+LN2mrg1afOr8vxTh96cDdkoqA6nmvZVfq1LC0SsWh3VaZ z/G1wnANfkMjlcz0dnZ2995Np6HZij0DiU96DOwXLZc1xzvdlhoRwfAg0QvSFFS91f 4XHfFfuT6MMwJFzLeBxbrT+oPRNd8M5XE7awHzQiBQ7653MZT84AZiKC2Ure4MZLIo /Pcgtj4NjGCTm/rcA2rRjRV8R4nqBossXt1awdK0bxbUt983GTKudRt38ugGj8h9kh Rm/jDeLpd9Wyg== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 421FE68A4C for ; Tue, 9 Dec 2025 17:09:52 -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 TJ3sxaJmOo53 for ; Tue, 9 Dec 2025 17:09:52 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325392; bh=QLODlkEpFM8iy9h9ejW/1G9QT0hw+9/nY+xndZpNVfM=; 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=DkweZZAS+lxSkxqOtnT+N2AbFpZjBBsmcV8bYCwRcoTqdUlEbbrtD5TN8hstsWe19 25yTwmaNwiBNTr7XceKH+LN2mrg1afOr8vxTh96cDdkoqA6nmvZVfq1LC0SsWh3VaZ z/G1wnANfkMjlcz0dnZ2995Np6HZij0DiU96DOwXLZc1xzvdlhoRwfAg0QvSFFS91f 4XHfFfuT6MMwJFzLeBxbrT+oPRNd8M5XE7awHzQiBQ7653MZT84AZiKC2Ure4MZLIo /Pcgtj4NjGCTm/rcA2rRjRV8R4nqBossXt1awdK0bxbUt983GTKudRt38ugGj8h9kh Rm/jDeLpd9Wyg== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 2A3CB689EA for ; Tue, 9 Dec 2025 17:09:52 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325389; bh=T7yE8KG/PsJBQLYnfe4hjf4wzOrsJm3D6KrtxgUd9i8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=l0RbYqLX6sqOae+ACk2G3MVnKfAsqdvMNaqCnsR4hACJ6fDXq9dhlmRfP4aqa4OHK oJhk8fJGd/IbsiGk0QWUKpKXN/H2hnV4lk4f2viMcV0rN7Djl1IBawIAhz99kPHx3F ogWoZuTLluj1gbxcmuYTe70uAZkPwuCW52RE1RuZkUQXtf+6G+YGxGEmxlAzWQMzoc jgVz/MB1IOEUf9Q4DriXc55T7wGzuzvs0bDPrK6kg6D/6pRy54xr3Jqnuc78VNvPVH AnQ71pYzA5BSmxxAa9q9G690T8oXiyEPHoF4VOQl4eJ6dL66ME2QDQansjPnraGbzq HuSjUkyRmPozw== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id C1BE368A22; Tue, 9 Dec 2025 17:09:49 -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 QG5GtMX2pfxe; Tue, 9 Dec 2025 17:09:49 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325383; bh=H9z012mE/z+TxlnvrIUEQ3GHNeLdDZKdCFbD+7/FlG0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=hf4zBXsvWnOSgTf5aq41dY+6GNjCgurIkH2VTEe92gNkClh63i5tECFLYRb30rPqf NXoJbO9daxR9rA8hCbE5tZyjhRzBgMXA12uRwFSugC3uwEKzHF59p+mihUYEDVIusO 8zsKVcl/WipwTI3/EsTCEKYEI8Bg70NiZdM2/FzbbZZisPyEYh03gCNPNxfCEHB0Fn eBjdO69YcrbswOgdI6OJncltl0vg7iehxasCfo6F4qAZnYXcLMhmacGBs8IZGrB7hb iMkOSUBMhnEOmVvnF8ZbDiiLJw0tGWoUmTevBpPzaWap2U6HWy6PFN6N4F6SlRVUdu hPyU1t9D7A3Gg== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id 25352687A7; Tue, 9 Dec 2025 17:09:43 -0700 (MST) From: Simon Glass To: U-Boot Concept Date: Tue, 9 Dec 2025 17:07:15 -0700 Message-ID: <20251210000737.180797-25-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: 6K4GOA75BZGDZLWWDW5IOHFQUKU6TJLR X-Message-ID-Hash: 6K4GOA75BZGDZLWWDW5IOHFQUKU6TJLR 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 24/35] malloc: Increase the mcheck registry size 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 The mcheck registry tracks concurrent allocations for pedantic checking. The current size of 6608 entries is insufficient when running the full pytest suite, causing registry overflow warnings. Increase REGISTRY_SZ to 12000 to handle the full test suite. Co-developed-by: Claude Signed-off-by: Simon Glass --- common/mcheck_core.inc.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/common/mcheck_core.inc.h b/common/mcheck_core.inc.h index 7caa9ac8dff..2839027564e 100644 --- a/common/mcheck_core.inc.h +++ b/common/mcheck_core.inc.h @@ -70,8 +70,8 @@ #define FREEFLOOD ((char)0xf5) #define PADDINGFLOOD ((char)0x58) -// my normal run demands 4427-6449 chunks: -#define REGISTRY_SZ 6608 +// Full test suite can exceed 10000 concurrent allocations +#define REGISTRY_SZ 12000 #define CANARY_DEPTH 2 // avoid problems with BSS at early stage: From patchwork Wed Dec 10 00:07:16 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 881 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=1765325390; bh=7ubv+0h2HhJz2jRAZOu9jv/ypovV0ziKQDtwl/+0n4s=; 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=RnQiaaMelYpflBvfWzafnlyZhceVJQtCZ+C6K+cJh2SJqcncXwjnPM/pK8DBk9uts 2MMqUmSx6ChTKfGPssNjXbAb0bx8rwEq97X1QtM67pwx76ZwOP/igJCPjSzJTm4SRU T19idgk0CsSoKHFYh7PtWrvDPf5ls5OuEY4FB42PyYmBs3FRey9FjGH5NVHVQG1d/m ZfD/McIZQtvE74dMgINf+X6bWDnLvR4/T5pl+7/V/yHejJsVB41ruwxqHW6jCKjiSx CBGwasZ12+dvB3FxxyorYTbnSFGQIaOQwn6z+mwe1Tw+jUnpaE4CGchSl9HiwGvuai 06N4/sil0TzZw== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id F31AD68A45 for ; Tue, 9 Dec 2025 17:09:50 -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 rVW6Wv-qQzL2 for ; Tue, 9 Dec 2025 17:09:50 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325390; bh=7ubv+0h2HhJz2jRAZOu9jv/ypovV0ziKQDtwl/+0n4s=; 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=RnQiaaMelYpflBvfWzafnlyZhceVJQtCZ+C6K+cJh2SJqcncXwjnPM/pK8DBk9uts 2MMqUmSx6ChTKfGPssNjXbAb0bx8rwEq97X1QtM67pwx76ZwOP/igJCPjSzJTm4SRU T19idgk0CsSoKHFYh7PtWrvDPf5ls5OuEY4FB42PyYmBs3FRey9FjGH5NVHVQG1d/m ZfD/McIZQtvE74dMgINf+X6bWDnLvR4/T5pl+7/V/yHejJsVB41ruwxqHW6jCKjiSx CBGwasZ12+dvB3FxxyorYTbnSFGQIaOQwn6z+mwe1Tw+jUnpaE4CGchSl9HiwGvuai 06N4/sil0TzZw== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id DF5246884F for ; Tue, 9 Dec 2025 17:09:50 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325389; bh=/2tPwBkePD8cSHCtUSt4wu6SK9xnJ05cZU7FiVsuy/E=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=OjiUmhCBT+1Lve0/5r1H21xsNtVSJ08QWMbqLqWf5C5o7drsD1HLYCxz7e5H5B7Wv aeU5DWR71GxBCA+4vyYputGhQaoGKkW38efNf3MjDESQa+9ExvtaKUFe7jjUR1EqOE RmyM4QyzGrF0/k5zZ1EO8LC/cHi3otmdSUS8edGaNdUjAhIVeLQm8vGcis8SvvwNhI Rw4C1BoVe7s8i5IAwEu4gd3Tbq8Y9RETKN18xBHpDjvhtKb7FyWC6calb7TumQ5TzQ FRLx6Th3ACU1zfMbO96LlSr6SSBML2elx6ATyUaEpJKCGJNYnM5XjOXSfWTWVjOcMO j8rv2IXK09gxA== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id C1BD4689EA; Tue, 9 Dec 2025 17:09:49 -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 VVCterPAERoS; Tue, 9 Dec 2025 17:09:49 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325388; bh=D193XXLIqnQ2u56E63ob29cCaH9bXSp0m66uVdHJB8c=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YS6Ua6hEQSdunhHNM6iS/jzDK84o0FkGrTiAL47DB+9i3yj0eZV+GMrqxSZSc6BF6 eOnmVHB6cTA1d+NG76LxwJfzQWp107Hvrf5eplyXhc5npSeLvsvrTEpf7HxgaMtW5O NbFwTVXq2UFpuQeJ0zBpv7dJkDrmMhwdnsjx8ZnncX3nPQJmuP3NUkReOK+S8oigjl SrhWdgRF3PjNWAxDnYBH+e1VYyreB8PjV56ceD22tpBxDIkPPQ5GknN4iDwUK9/m0m uiby6xouByLlLd7+hOzBBzEP2fdcJux9DU9fG/FaJMDgufGROuGncQ2hrDon9qWlOU 1YELQCNu3b17w== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id 068126884F; Tue, 9 Dec 2025 17:09:47 -0700 (MST) From: Simon Glass To: U-Boot Concept Date: Tue, 9 Dec 2025 17:07:16 -0700 Message-ID: <20251210000737.180797-26-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: Q3OCO3ERXCHMW5YNOBGC5L243Y7K2JDW X-Message-ID-Hash: Q3OCO3ERXCHMW5YNOBGC5L243Y7K2JDW 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 25/35] sandbox: Enable mcheck heap protection 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 Enable CONFIG_MCHECK_HEAP_PROTECTION for the sandbox board to detect heap corruption during development and testing. Increase the heap size from 96 MiB to 128 MiB to account for the additional overhead of mcheck metadata (~48 bytes per allocation). Increase the console-out buffer to 128K to allow space for the new malloc dump, when checked in tests. Co-developed-by: Claude Signed-off-by: Simon Glass --- configs/sandbox_defconfig | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/configs/sandbox_defconfig b/configs/sandbox_defconfig index 44185ccb7eb..b1b2e725b4c 100644 --- a/configs/sandbox_defconfig +++ b/configs/sandbox_defconfig @@ -1,5 +1,5 @@ CONFIG_TEXT_BASE=0 -CONFIG_SYS_MALLOC_LEN=0x6000000 +CONFIG_SYS_MALLOC_LEN=0x8000000 CONFIG_BLOBLIST_SIZE_RELOC=0x20000 CONFIG_NR_DRAM_BANKS=1 CONFIG_ENV_SIZE=0x2000 @@ -12,6 +12,7 @@ CONFIG_DEBUG_UART=y CONFIG_SYS_MEMTEST_START=0x00100000 CONFIG_SYS_MEMTEST_END=0x00101000 CONFIG_ULIB=y +CONFIG_MCHECK_HEAP_PROTECTION=y CONFIG_EXAMPLES=y CONFIG_EFI_SECURE_BOOT=y CONFIG_EFI_RT_VOLATILE_STORE=y @@ -49,7 +50,7 @@ CONFIG_IMAGE_PRE_LOAD=y CONFIG_IMAGE_PRE_LOAD_SIG=y CONFIG_CEDIT=y CONFIG_CONSOLE_RECORD=y -CONFIG_CONSOLE_RECORD_OUT_SIZE=0x6000 +CONFIG_CONSOLE_RECORD_OUT_SIZE=0x20000 CONFIG_PRE_CONSOLE_BUFFER=y CONFIG_LOG=y CONFIG_LOG_MAX_LEVEL=9 From patchwork Wed Dec 10 00:07:17 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 883 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=1765325395; bh=nLA7mlmDXgxxwvLJG9k92mRkUVSqnvLg057FYNOF57c=; 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=Pcr71outXKP/hPlORQgE24Sv+9Kbfk4viTPwrsvma26J8YRyHSPIzn6IMVe111WeA SHUFy7dSGWV65EjpSWRVlWrcW7T6lety6x1Bkqn9ilFnc/E82SDIdBUHpO3AMZxsDy voIeqVyCbjKooMbx3fE0//nTpcqVbyf6M00hIJLTAdEyucI1ursylnbKcngjnCMV8n 0fhRquxBkJ9RSfayefPkVbPZr1ZTOoaHaWZeVjj5t1SNj4wt+fGE1arIdoXFN+ePVH ZxLSbIDkpebN7MX9uBYYhT9SMIb24O2AVbiJJkA10wjLImEfEzCW9Ot7rYYz3tugXd SkBxwddVnq9PQ== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id A62F168A44 for ; Tue, 9 Dec 2025 17:09:55 -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 1ckbavhtW5T7 for ; Tue, 9 Dec 2025 17:09:55 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325395; bh=nLA7mlmDXgxxwvLJG9k92mRkUVSqnvLg057FYNOF57c=; 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=Pcr71outXKP/hPlORQgE24Sv+9Kbfk4viTPwrsvma26J8YRyHSPIzn6IMVe111WeA SHUFy7dSGWV65EjpSWRVlWrcW7T6lety6x1Bkqn9ilFnc/E82SDIdBUHpO3AMZxsDy voIeqVyCbjKooMbx3fE0//nTpcqVbyf6M00hIJLTAdEyucI1ursylnbKcngjnCMV8n 0fhRquxBkJ9RSfayefPkVbPZr1ZTOoaHaWZeVjj5t1SNj4wt+fGE1arIdoXFN+ePVH ZxLSbIDkpebN7MX9uBYYhT9SMIb24O2AVbiJJkA10wjLImEfEzCW9Ot7rYYz3tugXd SkBxwddVnq9PQ== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 964236898C for ; Tue, 9 Dec 2025 17:09:55 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325394; bh=WKlL0mpSc/2uhJF5GG68UTIE20uV7hbAxWN3GHOfSxA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=iCM7HR5zZd8ytoTVWZI/mrcKGZvqmSHBuvuSBxbn4PO5ZLKGfnvIYDFuqE/0EIuyv PtlQOa3SPR0UWxOkXd2GrNPqbPXaqnuMC9UC9O5Sy12NBeEsD6YTI1BnDhXxi7xLgO K7ENQjoRittqPX03LJVRSNrqu75CsUJlHuyoeDRAcft3fKPNTUdmGcJJOsHR9gx9mx +glrAalyV31C9rSW9cWYZaUdD2hhAg2YuRkm4G7b59UNENyAtkPwSPVPYsHE2g6eB2 bolEr+Vq5ZF9PSYijHAZqNm7Yg1aqmpfDpLZ8PP/PbFnjDsZJ5QIMkW42584Oh2xgO BMxyWJsOfLmmQ== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 95C2A68972; Tue, 9 Dec 2025 17:09:54 -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 h3uypt7ddxdP; Tue, 9 Dec 2025 17:09:54 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325389; bh=UL5yiD/5HK+tCZbaviLVXZiN0Mb5Zf9YSkxfA3lEF3w=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=B94HJUT2Or4H4GarhkFxcj++yJJiUAaaCkAIwFcUiBCQpCyajqiDkKFOhpswBNGRM lxR7QO49gKN6Hfo+1hpSsclB5CG+QrryRs/5Xrxys4iljB0LDbLBbSZ5gm44UwY4yb n9ZYrPlx2OHRHBSQ4HexD75SL+xBL1ZI57BVCiQTIE3p3CGFBym2AcZYTPq4SGK/oS J24TAYfrLpySRFATc3Flz1VxrmJdy4izE5SjzH/1YTiuWPTgOM/dGMQvLkWrMNZb83 wI2ng4CtA0Rxheq0T4HzFU0xx2gvzrgGShqBq/lAaSQ2sci3RAMookogIbyHOBK90/ S/r0dbmqnnASg== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id 1A58968973; Tue, 9 Dec 2025 17:09:49 -0700 (MST) From: Simon Glass To: U-Boot Concept Date: Tue, 9 Dec 2025 17:07:17 -0700 Message-ID: <20251210000737.180797-27-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: BX3VVWT7RMYAYC3WRWPWI5V3NX6INVHR X-Message-ID-Hash: BX3VVWT7RMYAYC3WRWPWI5V3NX6INVHR 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 26/35] malloc: Remove warning messages during relocation 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 Pre-relocation heap allocations are expected to be discarded during relocation - this is normal behavior, not a warning condition. Remove the verbose messages that were printed for each chunk. Co-developed-by: Claude Signed-off-by: Simon Glass --- common/mcheck_core.inc.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/common/mcheck_core.inc.h b/common/mcheck_core.inc.h index 2839027564e..c2089add0d6 100644 --- a/common/mcheck_core.inc.h +++ b/common/mcheck_core.inc.h @@ -289,14 +289,14 @@ static void mcheck_initialize(mcheck_abortfunc_t new_func, char pedantic_flag) void mcheck_on_ramrelocation(size_t offset) { - char *p; int i; - // Simple, but inaccurate strategy: drop the pre-reloc heap + + /* + * Pre-relocation heap allocations are no longer valid after + * relocation. Clear the registry since we can't track them. + */ for (i = 0; i < REGISTRY_SZ; ++i) - if ((p = mcheck_registry[i]) != NULL ) { - printf("mcheck, WRN: forgetting %p chunk\n", p); - mcheck_registry[i] = 0; - } + mcheck_registry[i] = 0; mcheck_chunk_count = 0; } 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) From patchwork Wed Dec 10 00:07:19 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 885 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=1765325406; bh=kHrhzwoxV9aBksHClZxvsil5mPp3VHGiS0AYnX0p0uE=; 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=EhTT0y65GOY8xcWa6HBEVTTAsFwKgkOcWoxs61Qg+wkRtnNn7ZoZLR7VAEKKvLDcH SJiUCUANBjlrionOITMTHcB46SeCRkWeSsj7C2VZtcrBgF2AoAFHqTD2ofiTknAMqA /q/us5DqjPo4wE6F0wpkXt6eaSlHqS8HPwgRNUGTjBuNyp9KnGRu7bvF+xFsRUkr3e 4ganV1IIh7dpfDBmy6kUWXAaE/5qg1j3Ub4zrax02cPdWEQ9HsGNwiSuMwtBO7AmOv CVKv52XQ2Oqj+DRV3ZEkV1F6zUY2FKZ55pW9LCMPdVNHoTPvpWlAEqMm0mbw5SVIlE 4sbMxb9g53Ybw== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id E603F68A47 for ; Tue, 9 Dec 2025 17:10:06 -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 uEb6ixN4-khs for ; Tue, 9 Dec 2025 17:10:06 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325406; bh=kHrhzwoxV9aBksHClZxvsil5mPp3VHGiS0AYnX0p0uE=; 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=EhTT0y65GOY8xcWa6HBEVTTAsFwKgkOcWoxs61Qg+wkRtnNn7ZoZLR7VAEKKvLDcH SJiUCUANBjlrionOITMTHcB46SeCRkWeSsj7C2VZtcrBgF2AoAFHqTD2ofiTknAMqA /q/us5DqjPo4wE6F0wpkXt6eaSlHqS8HPwgRNUGTjBuNyp9KnGRu7bvF+xFsRUkr3e 4ganV1IIh7dpfDBmy6kUWXAaE/5qg1j3Ub4zrax02cPdWEQ9HsGNwiSuMwtBO7AmOv CVKv52XQ2Oqj+DRV3ZEkV1F6zUY2FKZ55pW9LCMPdVNHoTPvpWlAEqMm0mbw5SVIlE 4sbMxb9g53Ybw== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id D38CE68A3E for ; Tue, 9 Dec 2025 17:10:06 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325404; bh=Tgx77gVCplz3z9OpR9ZTrQTfjV4Z7qUcIF0SAn5nQ8U=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=OwOKMmqoZ0Rvo1pPJ38bjdTvo7JNGBAlPXBAkkfQfwyFbahNh4NBAHr9OCp1UA+SF FU1H9UmSvkKXeJ8Eq20gErWXaS6bvgKCQBxbsFmGX0eaf5v3Ho0kKrMW1oA2ht5k0N ktHofxzJSPAlYJgPMo4N40QmBCCqbQEX6Gfnr4SufYgl7r/yktdnpTUzn964aUUOnu 8Bz+Qhxr0flCSZSmcm89oefj27sIyo55lN5IqHUydNjXVc4VqxlrDjuSXT61KARpYS Hv1qX08oEcUYPmGfID1fDf3VFvHZSNk8/McTtCtWtFC+UEc/bWi5uEBjMG1Wd7fmf/ HpRcuTE93HYqg== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 906B768A44; Tue, 9 Dec 2025 17:10:04 -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 jqpFr5OOJwnY; Tue, 9 Dec 2025 17:10:04 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325399; bh=EBpqMy4b8tyAgIvrp/wnATpEtC6q3pYlQEi7/nHp5ho=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Mus/WCPv9JpRlyxLh3xdbSSXknWI8Sbs5SXMZll1ty1vrN1L01ZublqE65A3m0r3R 47R1W8+dQQhMJu3Ugmflpmmhb7OlXA0X21DTE+Lb3Fuw4CimjMKnK8tB1/HIItwOGe vRZyqVYhORS9rduNaajZ7qpSwDiKhre2Zi1ZGpr4vRTIJ2rNL+uKqACr02txIZdeoi LPNew8yRtblHj6o7CtQHVfey6LbQnRkoD6gY24T7+3VHVfdSZV3B3aYj2Mjng2J0tz HFDcJbUCoK0WSshCcRsIfZA6JLN8PA5jRUn4eKAXjUala1VyS92m5szxHBekfVGReA w61jKHb5do8gA== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id B712B6884F; Tue, 9 Dec 2025 17:09:58 -0700 (MST) From: Simon Glass To: U-Boot Concept Date: Tue, 9 Dec 2025 17:07:19 -0700 Message-ID: <20251210000737.180797-29-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: PUBAZWZFGCRH4IL6OIZKDQQZENJKGYG6 X-Message-ID-Hash: PUBAZWZFGCRH4IL6OIZKDQQZENJKGYG6 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 28/35] malloc: Add a caller-info parameter to dlmalloc_impl() 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 CONFIG_MALLOC_DEBUG is enabled, add an optional caller string parameter to dlmalloc_impl(). This allows tracking where allocations originate from when debugging memory issues. The CALLER_PARAM, CALLER_ARG, and CALLER_NULL macros hide the parameter when MALLOC_DEBUG is not enabled, keeping the non-debug code path clean. When MALLOC_DEBUG is enabled (with or without MCHECK), the _impl functions must be separate from the public API functions since they have different signatures. Add simple pass-through wrappers for this case. Co-developed-by: Claude Signed-off-by: Simon Glass --- common/dlmalloc.c | 54 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 46 insertions(+), 8 deletions(-) diff --git a/common/dlmalloc.c b/common/dlmalloc.c index 706c12d9b9a..420bed1c480 100644 --- a/common/dlmalloc.c +++ b/common/dlmalloc.c @@ -570,6 +570,13 @@ MAX_RELEASE_CHECK_RATE default: 4095 unless not HAVE_MMAP #if CONFIG_IS_ENABLED(MALLOC_DEBUG) #define DEBUG 1 +#define CALLER_PARAM , const char *caller +#define CALLER_ARG , caller +#define CALLER_NULL , NULL +#else +#define CALLER_PARAM +#define CALLER_ARG +#define CALLER_NULL #endif #define LACKS_FCNTL_H @@ -626,12 +633,14 @@ MAX_RELEASE_CHECK_RATE default: 4095 unless not HAVE_MMAP DECLARE_GLOBAL_DATA_PTR; -#if CONFIG_IS_ENABLED(MCHECK_HEAP_PROTECTION) +#if CONFIG_IS_ENABLED(MCHECK_HEAP_PROTECTION) || CONFIG_IS_ENABLED(MALLOC_DEBUG) #define STATIC_IF_MCHECK static +#ifdef CONFIG_MCHECK_HEAP_PROTECTION #undef MALLOC_COPY #undef MALLOC_ZERO static inline void MALLOC_ZERO(void *p, size_t sz) { memset(p, 0, sz); } static inline void MALLOC_COPY(void *dest, const void *src, size_t sz) { memcpy(dest, src, sz); } +#endif #else #define STATIC_IF_MCHECK #define dlmalloc_impl dlmalloc @@ -4097,7 +4106,7 @@ static void unlink_large_chunk(mstate M, tchunkptr X) { * to avoid going through the mcheck wrappers which expect user pointers. */ #if CONFIG_IS_ENABLED(MCHECK_HEAP_PROTECTION) -#define internal_malloc(m, b) dlmalloc_impl(b) +#define internal_malloc(m, b) dlmalloc_impl(b CALLER_NULL) #define internal_free(m, mem) dlfree_impl(mem) #else #define internal_malloc(m, b) dlmalloc(b) @@ -4940,7 +4949,7 @@ static void* tmalloc_small(mstate m, size_t nb) { #if !ONLY_MSPACES STATIC_IF_MCHECK -void* dlmalloc_impl(size_t bytes) { +void *dlmalloc_impl(size_t bytes CALLER_PARAM) { #ifdef __UBOOT__ #if CONFIG_IS_ENABLED(SYS_MALLOC_F) if (!(gd->flags & GD_FLG_FULL_MALLOC_INIT)) @@ -5247,7 +5256,7 @@ void* dlcalloc_impl(size_t n_elements, size_t elem_size) { (req / n_elements != elem_size)) req = MAX_SIZE_T; /* force downstream failure on overflow */ } - mem = dlmalloc_impl(req); + mem = dlmalloc_impl(req CALLER_NULL); #ifdef __UBOOT__ #if CONFIG_IS_ENABLED(SYS_MALLOC_F) /* For pre-reloc simple malloc, just zero the memory directly */ @@ -5702,7 +5711,7 @@ void* dlrealloc_impl(void* oldmem, size_t bytes) { #endif void* mem = 0; if (oldmem == 0) { - mem = dlmalloc_impl(bytes); + mem = dlmalloc_impl(bytes CALLER_NULL); } else if (bytes >= MAX_REQUEST) { MALLOC_FAILURE_ACTION; @@ -5755,7 +5764,7 @@ void* dlrealloc_impl(void* oldmem, size_t bytes) { } } #else /* defined(__UBOOT__) && NO_REALLOC_IN_PLACE */ - mem = dlmalloc_impl(bytes); + mem = dlmalloc_impl(bytes CALLER_NULL); if (mem != 0) { size_t oc = chunksize(oldp) - overhead_for(oldp); memcpy(mem, oldmem, (oc < bytes)? oc : bytes); @@ -5821,7 +5830,7 @@ void* dlmemalign_impl(size_t alignment, size_t bytes) { * The base pointer must still be properly aligned for this to work. */ if (alignment <= MALLOC_ALIGNMENT) - return dlmalloc_impl(bytes); + return dlmalloc_impl(bytes CALLER_NULL); return internal_memalign(gm, alignment, bytes); } @@ -5950,7 +5959,7 @@ void *dlmalloc(size_t bytes) { mcheck_pedantic_prehook(); size_t fullsz = mcheck_alloc_prehook(bytes); - void *p = dlmalloc_impl(fullsz); + void *p = dlmalloc_impl(fullsz CALLER_NULL); if (!p) return p; @@ -6023,6 +6032,35 @@ int mcheck(mcheck_abortfunc_t f) void mcheck_check_all(void) { mcheck_pedantic_check(); } enum mcheck_status mprobe(void *__ptr) { return mcheck_mprobe(__ptr); } +#elif CONFIG_IS_ENABLED(MALLOC_DEBUG) +/* + * Simple wrappers when MALLOC_DEBUG is enabled but not MCHECK. + * These just forward to the _impl functions. + */ +void *dlmalloc(size_t bytes) +{ + return dlmalloc_impl(bytes CALLER_NULL); +} + +void dlfree(void *mem) +{ + dlfree_impl(mem); +} + +void *dlrealloc(void *oldmem, size_t bytes) +{ + return dlrealloc_impl(oldmem, bytes); +} + +void *dlmemalign(size_t alignment, size_t bytes) +{ + return dlmemalign_impl(alignment, bytes); +} + +void *dlcalloc(size_t n, size_t elem_size) +{ + return dlcalloc_impl(n, elem_size); +} #endif /* MCHECK_HEAP_PROTECTION */ #endif /* !ONLY_MSPACES */ From patchwork Wed Dec 10 00:07:20 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 886 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=1765325411; bh=MdVDqXRYt/cgw8gj3viR1p20zpbN1diSjQsE1MYIaxg=; 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=u5Qoh32YikryQIT5cDBeV6X0IrfIwHvNG9ed4dIF8ac69HunwXj2I+L0+AIbLJjsv VAr2C/XqCBAFg+QUeejKxlh1Ha76R7W16RB3cwhf7+Zc1l+OJkPxTyktYAGT8vLPK8 6jhjitpsTnJeZ6gD1bfQNHBFn+RsFDhWlXUQuiPhWQmu+BmlwSeHIm3+Gxxst2oNEf DfcYi7hItrX/CbkNVQ9G4gjSopkMnYYlUpxAw2Z5+/YYF8fKSL9QDRyFiS3Af6gbrq anJ51sZLkm3IvbdabyHYou/5Aj+rqRA11BCJzlgrc0aNZ/KKODDSRyNSxsmpG+HdUQ ARo5Pfvgp/3KA== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 9B3C16896E for ; Tue, 9 Dec 2025 17:10:11 -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 NKwYfnymqZTK for ; Tue, 9 Dec 2025 17:10:11 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325411; bh=MdVDqXRYt/cgw8gj3viR1p20zpbN1diSjQsE1MYIaxg=; 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=u5Qoh32YikryQIT5cDBeV6X0IrfIwHvNG9ed4dIF8ac69HunwXj2I+L0+AIbLJjsv VAr2C/XqCBAFg+QUeejKxlh1Ha76R7W16RB3cwhf7+Zc1l+OJkPxTyktYAGT8vLPK8 6jhjitpsTnJeZ6gD1bfQNHBFn+RsFDhWlXUQuiPhWQmu+BmlwSeHIm3+Gxxst2oNEf DfcYi7hItrX/CbkNVQ9G4gjSopkMnYYlUpxAw2Z5+/YYF8fKSL9QDRyFiS3Af6gbrq anJ51sZLkm3IvbdabyHYou/5Aj+rqRA11BCJzlgrc0aNZ/KKODDSRyNSxsmpG+HdUQ ARo5Pfvgp/3KA== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 86CFE689EA for ; Tue, 9 Dec 2025 17:10:11 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325409; bh=GVCvsWRiuMcY4ZJPVV5YjAWb73VJPiGWAWoQcbfGSgw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fm1OA04UylMRUkWu8aVU/NvhixLMoyQg+aKzWvD4hC1qDmP0/ZJoxLRc7EJO9FZwe 37IRAOyDqRb3Uf/ii9Hys/fxzErzxZateJA75sde94778MQ0vV7ziL08eMSWCsqiE9 8xQgU+gDD9WUqQjMLMoW0cSIWfB93i0gjP36VDsa5RdnKDJRd51940pQ/4gmCPVb1Y tf4Ikzs2Vcm+fB/H51ffAwKsesjySodw9OR9Pc8/WayPHkxaAmmL5+LMphO42muFyl mNqPsBcjqO/DrxLLyxR8e3OGuadfPQXAW+T3C5lMxEx/K0Rk2UwGoDPSjnm7gqdUtw MDXRmNGyeOGrw== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 5876468A47; Tue, 9 Dec 2025 17:10:09 -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 PjFTnA5m2rSZ; Tue, 9 Dec 2025 17:10:09 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325403; bh=kNpf+c0MT1Ul/5niTGMYMx02jIXRaHVJyUZGfq5Wx9U=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=X9gxIAD8qa6dU9+HwBR+WcFPy1vF7+umAlOeSzUjHceYU+GV7XVjQFLw1O5HkA0LH /eJo3e1cZDQMKyyQpknmpvsWhHg4XD1InDRNSYoB2yabWcKN5a+Th8/a3MN9v6t6Gh uysvbt/TVpaFchao7HKCgsk9qdF/gDwoKAeK/mwaWhrHF5vZtJP6c/C6RCe7F4q8f1 RsPSRCeB10Isi7KkMCl+aW8j5DjYcYhfXQ9R4xzBZ7VuMcfRdNm2OqMDfrTROY7mM0 WR1btrk6olMcBttgPxFCTAJWuVPDIUfkg2lMKM6WRFrZiSpv1HpYb8vYzqSVcWy261 vX7qu96dnn5wQ== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id 768B4689EA; Tue, 9 Dec 2025 17:10:03 -0700 (MST) From: Simon Glass To: U-Boot Concept Date: Tue, 9 Dec 2025 17:07:20 -0700 Message-ID: <20251210000737.180797-30-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: 2N6YV7C3OAOM3ZV2RZF5KZ4PYD6FHHB3 X-Message-ID-Hash: 2N6YV7C3OAOM3ZV2RZF5KZ4PYD6FHHB3 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 29/35] malloc: Add malloc dump command to walk the heap 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 new 'malloc dump' command that walks the dlmalloc heap from start to end, printing each chunk's address, size (in hex), and status (used/free/top). This is useful for debugging memory allocation issues. When CONFIG_MCHECK_HEAP_PROTECTION is enabled, the caller string is also shown if available. Example output: Heap dump: 18a1d000 - 1ea1f000 Address Size Status ---------------------------------- 18a1d000 10 (chunk header) 18a1d010 90 used 18adfc30 60 18adff90 5f3f030 top 1ea1f000 end ---------------------------------- Used: c2ef0 bytes in 931 chunks Free: 5f3f0c0 bytes in 2 chunks + top Expand the console-record size to handle this command. Co-developed-by: Claude Signed-off-by: Simon Glass --- cmd/malloc.c | 14 ++++++-- common/Kconfig | 1 + common/dlmalloc.c | 71 +++++++++++++++++++++++++++++++++++++++- doc/usage/cmd/malloc.rst | 22 +++++++++++++ include/malloc.h | 8 +++++ test/cmd/malloc.c | 20 +++++++++++ 6 files changed, 133 insertions(+), 3 deletions(-) diff --git a/cmd/malloc.c b/cmd/malloc.c index 3750a16158b..9c7dfbfc0c3 100644 --- a/cmd/malloc.c +++ b/cmd/malloc.c @@ -30,8 +30,18 @@ static int do_malloc_info(struct cmd_tbl *cmdtp, int flag, int argc, return 0; } +static int do_malloc_dump(struct cmd_tbl *cmdtp, int flag, int argc, + char *const argv[]) +{ + malloc_dump(); + + return 0; +} + U_BOOT_LONGHELP(malloc, - "info - display malloc statistics\n"); + "info - display malloc statistics\n" + "malloc dump - dump heap chunks (address, size, status)\n"); U_BOOT_CMD_WITH_SUBCMDS(malloc, "malloc information", malloc_help_text, - U_BOOT_SUBCMD_MKENT(info, 1, 1, do_malloc_info)); + U_BOOT_SUBCMD_MKENT(info, 1, 1, do_malloc_info), + U_BOOT_SUBCMD_MKENT(dump, 1, 1, do_malloc_dump)); diff --git a/common/Kconfig b/common/Kconfig index 3bd11f44c51..0aa32145710 100644 --- a/common/Kconfig +++ b/common/Kconfig @@ -26,6 +26,7 @@ config CONSOLE_RECORD_INIT_F config CONSOLE_RECORD_OUT_SIZE hex "Output buffer size" depends on CONSOLE_RECORD + default 0x2000 if CMD_MALLOC && UNIT_TEST default 0x1000 if UNIT_TEST default 0x400 help diff --git a/common/dlmalloc.c b/common/dlmalloc.c index 420bed1c480..b40963604e4 100644 --- a/common/dlmalloc.c +++ b/common/dlmalloc.c @@ -635,7 +635,7 @@ DECLARE_GLOBAL_DATA_PTR; #if CONFIG_IS_ENABLED(MCHECK_HEAP_PROTECTION) || CONFIG_IS_ENABLED(MALLOC_DEBUG) #define STATIC_IF_MCHECK static -#ifdef CONFIG_MCHECK_HEAP_PROTECTION +#if CONFIG_IS_ENABLED(MCHECK_HEAP_PROTECTION) #undef MALLOC_COPY #undef MALLOC_ZERO static inline void MALLOC_ZERO(void *p, size_t sz) { memset(p, 0, sz); } @@ -7021,6 +7021,75 @@ void malloc_disable_testing(void) malloc_testing = false; } +void malloc_dump(void) +{ + mchunkptr q; + msegmentptr s; + size_t used = 0, free_space = 0; + int used_count = 0, free_count = 0; + + if (!is_initialized(gm)) { + printf("dlmalloc not initialized\n"); + return; + } + + printf("Heap dump: %lx - %lx\n", mem_malloc_start, mem_malloc_end); + printf("%12s %10s %s\n", "Address", "Size", "Status"); + printf("----------------------------------\n"); + + s = &gm->seg; + while (s != 0) { + q = align_as_chunk(s->base); + + /* Show chunk header before first allocation */ + printf("%12lx %10zx (chunk header)\n", (ulong)s->base, + (size_t)((char *)chunk2mem(q) - (char *)s->base)); + + while (segment_holds(s, q) && + q != gm->top && q->head != FENCEPOST_HEAD) { + size_t sz = chunksize(q); + void *mem = chunk2mem(q); + + if (is_inuse(q)) { +#if CONFIG_IS_ENABLED(MCHECK_HEAP_PROTECTION) + struct mcheck_hdr *hdr = (struct mcheck_hdr *)mem; + + if (hdr->caller[0]) + printf("%12lx %10zx %s\n", + (ulong)mem, sz, hdr->caller); + else + printf("%12lx %10zx\n", + (ulong)mem, sz); +#else + printf("%12lx %10zx\n", + (ulong)mem, sz); +#endif + used += sz; + used_count++; + } else { + printf("%12lx %10zx \n", + (ulong)mem, sz); + free_space += sz; + free_count++; + } + q = next_chunk(q); + } + s = s->next; + } + + /* Print top chunk (wilderness) */ + if (gm->top && gm->topsize > 0) { + printf("%12lx %10zx top\n", + (ulong)chunk2mem(gm->top), gm->topsize); + free_space += gm->topsize; + } + + printf("%12lx %10s end\n", mem_malloc_end, ""); + printf("----------------------------------\n"); + printf("Used: %zx bytes in %d chunks\n", used, used_count); + printf("Free: %zx bytes in %d chunks + top\n", free_space, free_count); +} + int initf_malloc(void) { #if CONFIG_IS_ENABLED(SYS_MALLOC_F) diff --git a/doc/usage/cmd/malloc.rst b/doc/usage/cmd/malloc.rst index 7b08b358258..fac9bb29aac 100644 --- a/doc/usage/cmd/malloc.rst +++ b/doc/usage/cmd/malloc.rst @@ -12,6 +12,7 @@ Synopsis :: malloc info + malloc dump Description ----------- @@ -23,6 +24,13 @@ info amount currently in use, and call counts for malloc(), free(), and realloc(). +dump + Walks the heap and prints each chunk's address, size (in hex), and status. + In-use chunks show no status label, while free chunks show ````. + Special entries show ``(chunk header)``, ``top``, or ``end``. This is useful + for debugging memory allocation issues. When CONFIG_MCHECK_HEAP_PROTECTION + is enabled, the caller string is also shown if available. + The total heap size is set by ``CONFIG_SYS_MALLOC_LEN``. Example @@ -37,6 +45,20 @@ Example free count = 567 realloc count = 89 + => malloc dump + Heap dump: 19a0e000 - 1fa10000 + Address Size Status + ---------------------------------- + 19a0e000 10 (chunk header) + 19a0e010 a0 + 19a0e0b0 6070 + 19adfc30 60 + 19adff90 5f3f030 top + 1fa10000 end + ---------------------------------- + Used: c2ef0 bytes in 931 chunks + Free: 5f3f0c0 bytes in 2 chunks + top + Configuration ------------- diff --git a/include/malloc.h b/include/malloc.h index 0d8a2a43f2a..a4d588936ec 100644 --- a/include/malloc.h +++ b/include/malloc.h @@ -704,6 +704,14 @@ void malloc_enable_testing(int max_allocs); */ void malloc_disable_testing(void); +/** + * malloc_dump() - Print a dump of all heap chunks + * + * Walks the dlmalloc heap from start to end, printing each chunk's + * address, size, and status (used/free/top). + */ +void malloc_dump(void); + /** * mem_malloc_init() - Initialize the malloc() heap * diff --git a/test/cmd/malloc.c b/test/cmd/malloc.c index f9d41a36cad..3c1a44bcacf 100644 --- a/test/cmd/malloc.c +++ b/test/cmd/malloc.c @@ -32,3 +32,23 @@ static int cmd_test_malloc_info(struct unit_test_state *uts) return 0; } CMD_TEST(cmd_test_malloc_info, UTF_CONSOLE); + +/* Test 'malloc dump' command */ +static int cmd_test_malloc_dump(struct unit_test_state *uts) +{ + /* this takes a long time to dump, with truetype enabled, so skip it */ + return -EAGAIN; + + ut_assertok(run_command("malloc dump", 0)); + ut_assert_nextlinen("Heap dump: "); + ut_assert_nextline("%12s %10s %s", "Address", "Size", "Status"); + ut_assert_nextline("----------------------------------"); + ut_assert_nextline("%12lx %10x (chunk header)", mem_malloc_start, 0x10); + ut_assert_skip_to_line("----------------------------------"); + ut_assert_nextlinen("Used: "); + ut_assert_nextlinen("Free: "); + ut_assert_console_end(); + + return 0; +} +CMD_TEST(cmd_test_malloc_dump, UTF_CONSOLE); From patchwork Wed Dec 10 00:07:21 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 887 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=1765325414; bh=RAWv3kmKGLPxON78VgH6JTZ8eSkyoFjDIG+QkaZxHWA=; 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=TPvy2GP/szH2JcgWXHSPH523uSvb3hHPhxIYPsbh2EuGueqYdGATvqUC7xRkH9tN0 jY8ZW2uE6wkiY8s3EyomzE1jcPoUlN1gBjsWmSqlTIcBs3pZZ3Q2Qpb08O3YdTaOXX Mw6NHocZj2Yditg+GcjB0RSD1arm/6IT4UYtnDxjmKoUkEtnUEY0pAv9BHjFxawTke Tu3D6LJrIDKH08G+TID8EUw82aa50s6X1BDXPXKcFZ2gTNTvi0ulmZWK6ZQS8+3oOs grU7V2Oplp79U8X8HmKj68bR+tBQA1dGOZccAHRhyfX8Q+c9PtTC4gQDirdqCg4kN9 iDfNg2Q06sz2Q== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 0649C68A3E for ; Tue, 9 Dec 2025 17:10:14 -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 X-NP68xDcWjd for ; Tue, 9 Dec 2025 17:10:13 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325412; bh=RAWv3kmKGLPxON78VgH6JTZ8eSkyoFjDIG+QkaZxHWA=; 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=hqSVC/uzDC+2n5JZKG7kRH5ra9ARoiaw9ONypVFKsnRTWolcc4PqDTNjbiUCEBX1r v1MPY1E/w0J2SUlzqROfQqEKcDeKIlFUNEAoEstNjQht3E9MlOUMlmYYExFPc2dRaD 6+ZDFrKeyYdiXT14FS5fE7AW5lfMO5txy7blPjqP78643Sjd/1F73CoCQ9kU1VJGGV DCHpaopHfsNl6KaLrScyCQABip4Fo22GJ+Z5XnLbR+1dNlgwhpFYQYSAGVNf74FnEL oCHrmMVYneyCPskNxirXBL16cacJl0mxo+xNEy62qY/MnSckSY421eAfAcMggegFKt GD+DB1rZD0S/Q== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 02A0E687A7 for ; Tue, 9 Dec 2025 17:10:12 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325409; bh=/Oo39TzjwYLcS554eBwFoxnfkAnriVbv0iwOyLtWDnw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=SEiZnxSE3aKPkoepWSKzEWet7UCh6QRP780UZt2apfs1Ff57blAlCKWyIWw/YSYOx 1fSQ8VwTVw/xjzhClUzCm/1x7IVeCg5saa/Nuw09AIWuLYaa/G808QkzFxbAo64oDb v12TkrKU/MnHVagLQ8fGCcEqRIU0oNfS4j2hetWVjLuAxoK9mLuQkySKrS5mYyKXtl OAchrplp0hoBP2QCLe9GgR81xKfUuhjCoE0msln0kF4HOFwqrknSd2coycSF4WiJLO 2m2TBOEIzdSvr9Tbv6dHQdbu8mb6TY4J2DmuRsJgzDtA3ZaWsv803Fgmo+/z9MTQyx npSwLx5fCXowA== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 586DC68972; Tue, 9 Dec 2025 17:10:09 -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 q8BqZeNrjQZt; Tue, 9 Dec 2025 17:10:09 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325408; bh=LvH1mr8CQDiiki+y/05ngRaWJdNRgumdGzh59gBkphU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BSgCTZsMb0y0w0BAF+0oaAxhyRUaEOnV4NRXQr5YgGWBc9rGTxvFMt7ewm5YfHCjJ QWODt7a3O1sp8xm0oW0CodnDG2X/H064ef40CQ1j9bgO93ahccO42T2gpc3x+b5dAW tFcHwTFoJ1ilIZZCRWvSPL/+DmNXBXkVTKvCKp/SWLufHKzBSC+hc1sPm+GSi9sU45 htqU2kcpbBKXfsasbWt2mIyDhOPR1Y8nnMbppSYmA8vTNT525XX7zPmRpJBTxcGzhH elwG8i6L3yz/yAoLgNrJcPjrx68QVSYyMxKO2W0iNcxCFubP8Q3k+tgPvZ4llyWRau eTm4l7JIq9W3g== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id 2C7FA6896E; Tue, 9 Dec 2025 17:10:08 -0700 (MST) From: Simon Glass To: U-Boot Concept Date: Tue, 9 Dec 2025 17:07:21 -0700 Message-ID: <20251210000737.180797-31-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: HO422WWLCIBJ7XO3ROV4SUQ3JHZSOMAG X-Message-ID-Hash: HO422WWLCIBJ7XO3ROV4SUQ3JHZSOMAG 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 30/35] malloc: Fix malloc_dump to find mcheck headers in memalign chunks 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 For memalign allocations, the mcheck header is placed at an offset from the chunk start to maintain alignment. The current assumption is that the header is always at the start of the chunk, but this is not true for memalign allocations. Add find_mcheck_hdr_in_chunk() which looks up the header in the mcheck registry and validates: - The header falls within the chunk's memory range - The aln_skip field is consistent with the header position - The canary is MAGICWORD (active), not MAGICFREE (freed) This ensures malloc_dump correctly displays caller info for all allocations including those made via memalign. Co-developed-by: Claude Signed-off-by: Simon Glass --- common/dlmalloc.c | 52 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 2 deletions(-) diff --git a/common/dlmalloc.c b/common/dlmalloc.c index b40963604e4..14515e423cc 100644 --- a/common/dlmalloc.c +++ b/common/dlmalloc.c @@ -7021,6 +7021,53 @@ void malloc_disable_testing(void) malloc_testing = false; } +/** + * find_mcheck_hdr_in_chunk() - find mcheck header within a chunk + * + * For memalign allocations, the mcheck header may be at an offset from + * the chunk start to maintain alignment. Look up the header in the + * mcheck registry, which stores pointers to all active headers. + * + * @mem: chunk memory pointer (from chunk2mem) + * @sz: chunk size + * Return: pointer to mcheck header if found, NULL otherwise + */ +#if CONFIG_IS_ENABLED(MCHECK_HEAP_PROTECTION) +static struct mcheck_hdr *find_mcheck_hdr_in_chunk(void *mem, size_t sz) +{ + struct mcheck_hdr *hdr; + char *start = (char *)mem; + char *end = start + sz; + int i, j; + + for (i = 0; i < REGISTRY_SZ; i++) { + hdr = mcheck_registry[i]; + if (!hdr) + continue; + + /* Check if this header falls within our chunk */ + if ((char *)hdr < start || (char *)hdr >= end) + continue; + + /* Validate the aln_skip is consistent with position */ + if ((char *)hdr != start + hdr->aln_skip) + continue; + + /* Verify canary is valid (not freed) */ + for (j = 0; j < CANARY_DEPTH; j++) { + if (hdr->canary.elems[j] != MAGICWORD) + goto next; + } + + return hdr; +next: + continue; + } + + return NULL; +} +#endif + void malloc_dump(void) { mchunkptr q; @@ -7052,9 +7099,10 @@ void malloc_dump(void) if (is_inuse(q)) { #if CONFIG_IS_ENABLED(MCHECK_HEAP_PROTECTION) - struct mcheck_hdr *hdr = (struct mcheck_hdr *)mem; + struct mcheck_hdr *hdr; - if (hdr->caller[0]) + hdr = find_mcheck_hdr_in_chunk(mem, sz); + if (hdr && hdr->caller[0]) printf("%12lx %10zx %s\n", (ulong)mem, sz, hdr->caller); else From patchwork Wed Dec 10 00:07:22 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 888 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=1765325417; bh=8oY13tm1qysJnCbb2QcuNElxpFZyrHGnAHxyfDRPa78=; 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=FAWMBdWstCiOmb13dRfEabQdZylETCu6iRbv80wpYOHVs3loGwyien+2ndgnsp96o H3mze2fwhE6dfilwdoTEVo9AeoxjA9oDwyc83LXdjq0/Rnt71Z3FpWgRiCOSkNEDVr sLKiyHU2A7rrD8b/s42gKobW/35XX7sjhB8JC2oWVcc2IbSyBY+rcxvGS+wryPkYko dgy33cnrhq5f2wskINQZ3Yfbjn9lgBZi5Suu7FvwxByGpQYo3C3fURh7sXEVGDKgbe WaIfqKWASqrKdhkN1+LHPGvuAHhobcj2FwmVSgOAMxYWtTjb6YRCcdQgp6//s1Qw/D KD5aFlGATQtNQ== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id C0E176895B for ; Tue, 9 Dec 2025 17:10:17 -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 VB_roeI6fdxC for ; Tue, 9 Dec 2025 17:10:17 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325417; bh=8oY13tm1qysJnCbb2QcuNElxpFZyrHGnAHxyfDRPa78=; 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=FAWMBdWstCiOmb13dRfEabQdZylETCu6iRbv80wpYOHVs3loGwyien+2ndgnsp96o H3mze2fwhE6dfilwdoTEVo9AeoxjA9oDwyc83LXdjq0/Rnt71Z3FpWgRiCOSkNEDVr sLKiyHU2A7rrD8b/s42gKobW/35XX7sjhB8JC2oWVcc2IbSyBY+rcxvGS+wryPkYko dgy33cnrhq5f2wskINQZ3Yfbjn9lgBZi5Suu7FvwxByGpQYo3C3fURh7sXEVGDKgbe WaIfqKWASqrKdhkN1+LHPGvuAHhobcj2FwmVSgOAMxYWtTjb6YRCcdQgp6//s1Qw/D KD5aFlGATQtNQ== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id AD61E687A7 for ; Tue, 9 Dec 2025 17:10:17 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325416; bh=XpE5d/E4PWs5f+0hd7Mk0dZGIsrtEJA6UqMDMmOTKrc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=R1VMAJUnZOgg97nG2yHu5vI8fT8QwVgqFnRnVFjL41xiIF+XMC+qqksZlDjxJ1Byd 7XRaGIHcoWoHhLK0oY5+OiK46jUKKukfoTXLUFVMEURzUb3dbcXh+Jny6rOMJlcYPw 80RGVBYwzKSwJxG0fEssdDU98jx8UvDs8Vw/KxQX+ooxT4jvZBjYfTuqxCqsIIR2kU P5tMV1ZDeZ7qnON/rZ2mkQfb8paNINH7RD5MPU7OtL6dnruS11Ny+3CcxvkV7i+Myh Axb2DwuIUf5sM+MiNZbdLRYSPj1kBYVxco8puT0sdiUoaJ06Nl5S5hJz5zcFd4C8ue D+5KskkP79Z8w== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 5A191687A7; Tue, 9 Dec 2025 17:10:16 -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 BA7R6Z3U9te2; Tue, 9 Dec 2025 17:10:16 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325409; bh=ZYrkMRkcZFGcTNxyOGrhoLRdm4D0GZbnwUE0bFPPjN4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=eXAjWxLv10Ocf3igP2sG9hLVELNw73RN01swjRnX6d6NhBtQfccHt4JP7QaFgtdj7 vyRi7DkiQwTl5pDRzDwu37CVgpBxHMxcYWdHWNPBDaL+ICSBc/JHxoLDrFrTozrbsm c8ARftFxl365W6P7EqY5dg6RWwbp51BYiIvH8tElIH7kc94pekXlUsVaQVD9pQUnmB sSVG8tS8kJbozSSi7GwNst9LtAczlGjjz9Zq/jcCpXbufpV3Z6Ry8vWeypR2f08UP/ +cA4e+p6oEE6Mg31qOmePib8hp3HlBGcdC4rzyi+J8XXY6r50IULfzSht5IUfCurwT R+Lq3B7Kk4+rA== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id 141C86895B; Tue, 9 Dec 2025 17:10:09 -0700 (MST) From: Simon Glass To: U-Boot Concept Date: Tue, 9 Dec 2025 17:07:22 -0700 Message-ID: <20251210000737.180797-32-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: RHMX6DNDUK7OUTVWOMSZ5JIFZTCSGQPN X-Message-ID-Hash: RHMX6DNDUK7OUTVWOMSZ5JIFZTCSGQPN 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 31/35] malloc: Record caller backtrace for each allocation 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 CONFIG_MCHECK_HEAP_PROTECTION is enabled, use backtrace_str() to capture the caller information for each malloc/calloc/realloc/memalign call. This information is stored in the mcheck header and can be viewed with 'malloc dump'. Add a flag to disable the backtrace when the stack is corrupted, since the backtrace code tries to walks the invalid stack frames and will crash. Note: A few allocations made during libbacktrace initialisation may not have caller info since they occur during the first backtrace call. Example output showing caller info: 18a1d010 90 used log_init:453 <-board_init_r:774 18a1d0a0 6060 used membuf_new:420 <-console_record 18a3b840 90 used of_alias_scan:911 <-board_init_ Fix up the backtrace test to avoid recursion. Co-developed-by: Claude Signed-off-by: Simon Glass --- common/dlmalloc.c | 35 +++++++++++++++++++++++++++++++---- doc/usage/cmd/malloc.rst | 12 ++++++++++++ include/malloc.h | 15 +++++++++++++++ test/lib/backtrace.c | 7 ++++--- 4 files changed, 62 insertions(+), 7 deletions(-) diff --git a/common/dlmalloc.c b/common/dlmalloc.c index 14515e423cc..c294b355ff2 100644 --- a/common/dlmalloc.c +++ b/common/dlmalloc.c @@ -5953,8 +5953,35 @@ size_t dlmalloc_usable_size(const void* mem) { } #if CONFIG_IS_ENABLED(MCHECK_HEAP_PROTECTION) +#include #include "mcheck_core.inc.h" +/* Guard against recursive backtrace calls during malloc */ +static bool in_backtrace __section(".data"); + +/* + * Flag to disable backtrace collection when the stack is known to be corrupt. + * Set via malloc_backtrace_skip() before calling panic(). + */ +static bool mcheck_skip_backtrace __section(".data"); + +void malloc_backtrace_skip(bool skip) +{ + mcheck_skip_backtrace = skip; +} + +static const char *mcheck_caller(void) +{ + const char *caller = NULL; + + if (!in_backtrace && !mcheck_skip_backtrace) { + in_backtrace = true; + caller = backtrace_str(2); + in_backtrace = false; + } + return caller; +} + void *dlmalloc(size_t bytes) { mcheck_pedantic_prehook(); @@ -5963,7 +5990,7 @@ void *dlmalloc(size_t bytes) if (!p) return p; - return mcheck_alloc_posthook(p, bytes, NULL); + return mcheck_alloc_posthook(p, bytes, mcheck_caller()); } void dlfree(void *mem) { dlfree_impl(mcheck_free_prehook(mem)); } @@ -5988,7 +6015,7 @@ void *dlrealloc(void *oldmem, size_t bytes) p = dlrealloc_impl(p, newsz); if (!p) return p; - return mcheck_alloc_noclean_posthook(p, bytes, NULL); + return mcheck_alloc_noclean_posthook(p, bytes, mcheck_caller()); } void *dlmemalign(size_t alignment, size_t bytes) @@ -5999,7 +6026,7 @@ void *dlmemalign(size_t alignment, size_t bytes) if (!p) return p; - return mcheck_memalign_posthook(alignment, p, bytes, NULL); + return mcheck_memalign_posthook(alignment, p, bytes, mcheck_caller()); } /* dlpvalloc, dlvalloc redirect to dlmemalign, so they need no wrapping */ @@ -6013,7 +6040,7 @@ void *dlcalloc(size_t n, size_t elem_size) if (!p) return p; - return mcheck_alloc_noclean_posthook(p, n * elem_size, NULL); + return mcheck_alloc_noclean_posthook(p, n * elem_size, mcheck_caller()); } /* mcheck API */ diff --git a/doc/usage/cmd/malloc.rst b/doc/usage/cmd/malloc.rst index fac9bb29aac..3693034b41e 100644 --- a/doc/usage/cmd/malloc.rst +++ b/doc/usage/cmd/malloc.rst @@ -59,6 +59,18 @@ Example Used: c2ef0 bytes in 931 chunks Free: 5f3f0c0 bytes in 2 chunks + top +With CONFIG_MCHECK_HEAP_PROTECTION enabled, the caller backtrace is shown:: + + => malloc dump + Heap dump: 18a1d000 - 1ea1f000 + Address Size Status + ---------------------------------- + 18a1d000 10 (chunk header) + 18a1d010 90 used log_init:453 <-board_init_r:774 + 18a1d0a0 6060 used membuf_new:420 <-console_record + 18a3b840 90 used of_alias_scan:911 <-board_init_ + ... + Configuration ------------- diff --git a/include/malloc.h b/include/malloc.h index a4d588936ec..3327bdcb44f 100644 --- a/include/malloc.h +++ b/include/malloc.h @@ -712,6 +712,21 @@ void malloc_disable_testing(void); */ void malloc_dump(void); +/** + * malloc_backtrace_skip() - Control backtrace collection in malloc + * + * When the stack is corrupted (e.g., by a stack overflow), collecting + * a backtrace during malloc can crash. Use this function to disable + * backtrace collection before corrupting the stack. + * + * @skip: true to skip backtrace collection, false to enable it + */ +#if CONFIG_IS_ENABLED(MCHECK_HEAP_PROTECTION) +void malloc_backtrace_skip(bool skip); +#else +static inline void malloc_backtrace_skip(bool skip) {} +#endif + /** * mem_malloc_init() - Initialize the malloc() heap * diff --git a/test/lib/backtrace.c b/test/lib/backtrace.c index 3f20b7854bf..155e5b13af8 100644 --- a/test/lib/backtrace.c +++ b/test/lib/backtrace.c @@ -68,16 +68,17 @@ static int lib_test_backtrace_str(struct unit_test_state *uts) line); ut_asserteq_regex(pattern, str); - /* Test backtrace_str() */ + /* Test backtrace_str() - copy result before printf since it may recurse */ line = __LINE__ + 1; cstr = backtrace_str(0); ut_assertnonnull(cstr); + strlcpy(buf, cstr, sizeof(buf)); - printf("backtrace_str: %s\n", cstr); + printf("backtrace_str: %s\n", buf); snprintf(pattern, sizeof(pattern), "lib_test_backtrace_str:%d <-ut_run_test:\\d+ <-ut_run_test_live_flat:\\d+", line); - ut_asserteq_regex(pattern, cstr); + ut_asserteq_regex(pattern, buf); return 0; } From patchwork Wed Dec 10 00:07:23 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 889 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=1765325421; bh=yHAPvqKhi5ljzrAcoSO8m8VoOyJAnpVmAzGb5PVagtg=; 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=p0yVNTujIxlXGPXdAO6p8z0yLfZ4AbhRhyr7naGkOMINwwBLvQboubMv9xWwFSEo4 SXJUsmjsQJfjdfEZ8TYdILV5AzzIdV5kkZyLKJQQ6DhLeqNGU8IOMqPE2VVTVm9fFk 1Yfq/ij4kqkjdUDitw03sTv6sjGlkBCc+3hmtF7+ticly1UGdnvp5+OvMhEMoSjlH/ QYlP+jX/e4ISxQNUNgF66fQ0JWfpjqkHio/usd+J2aXuzk5k6AmUEVxrU0OkssbxGM f9aMOkjMdJGjfWqYTYiaZSgm3TYwGbYulYI761FhnbyZfWthcnW0EL6rZPQrkb9FTQ Q0W5f9ABu9dww== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 47933689EA for ; Tue, 9 Dec 2025 17:10:21 -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 SB6JZJqFxIuM for ; Tue, 9 Dec 2025 17:10:21 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325421; bh=yHAPvqKhi5ljzrAcoSO8m8VoOyJAnpVmAzGb5PVagtg=; 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=p0yVNTujIxlXGPXdAO6p8z0yLfZ4AbhRhyr7naGkOMINwwBLvQboubMv9xWwFSEo4 SXJUsmjsQJfjdfEZ8TYdILV5AzzIdV5kkZyLKJQQ6DhLeqNGU8IOMqPE2VVTVm9fFk 1Yfq/ij4kqkjdUDitw03sTv6sjGlkBCc+3hmtF7+ticly1UGdnvp5+OvMhEMoSjlH/ QYlP+jX/e4ISxQNUNgF66fQ0JWfpjqkHio/usd+J2aXuzk5k6AmUEVxrU0OkssbxGM f9aMOkjMdJGjfWqYTYiaZSgm3TYwGbYulYI761FhnbyZfWthcnW0EL6rZPQrkb9FTQ Q0W5f9ABu9dww== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 37E1D68972 for ; Tue, 9 Dec 2025 17:10:21 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325420; bh=zIzA1T10NcYp+jX2JvjkYAmpxlzMcgabKcMgSBL7dbU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=uc0TlKijx98GCueMikXQObl7ytYRf2ySxBmLqdpwIL1+ZOhMZeBoDBtg0prEFhcYM tKNOOSv1Xgbujn+PwXX0EghiPigoPyaUGI0aM+lcLeCsQfNiEPH+wBuQtejX0QFkfp OX6W1JvHTiYDiTDPny/+LIGbM9QmMvgSSABcKORIlJ5kZhvCpEkWNlVkFFnGAaI7Uz mIQFyIOjq1Y6cQHOpfxeprVf2Lj4b4QuuiTKVC/9rCDcfadNDDyRafpxDhL4VDkIT6 nvOeWGRxYWPhtzgPM2YylcMlDkIY+oflopQ0/tW9XAYwxnTdPTTOg6G0o9ZjKRob/D w3yPkU2zUHXYw== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 39823687A7; Tue, 9 Dec 2025 17:10:20 -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 N1XHJ1FLc-GS; Tue, 9 Dec 2025 17:10:20 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325414; bh=2sBDxo2+d4L1MKfEZxXUGlD43Ub0rm8dzqIVMl92Guw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Wg/M5hsJDZSYK7K6d3HCxDLPTilfOn1kPnLu4OcdwlGfzEecfbLnuMpbZ7Ch/w5aK c7xsLgRZg0voRD7ZT/X+oBGK4WFv0zjWo1i9lkIBRPOvaAbSKqaaEykQNPxcGTYsQK CS+LyabdUAtxByCqx2+Ulb2xFjbmRVNVUlHGXvofu+d4GVBtpDr71fi/rxjOeQJUdv aPKGX45N2wSXlTXZqSYXNplzAY44o5CAigZBcrt0kJ0Aql02fYb0YTYsiG/KzrGHJs +KQ6J+5qOVkGDy6qmHs8AGWh4jFCv9WCzeCzi/aIIqcze8lkHjglFpIavtfndf3oXS Z5iGODImAY7zw== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id C4655689EA; Tue, 9 Dec 2025 17:10:13 -0700 (MST) From: Simon Glass To: U-Boot Concept Date: Tue, 9 Dec 2025 17:07:23 -0700 Message-ID: <20251210000737.180797-33-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: TXIMDSP2SFZYIAALR6VSBMNET3GMPZGS X-Message-ID-Hash: TXIMDSP2SFZYIAALR6VSBMNET3GMPZGS 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 32/35] malloc: Skip backtrace when stack is corrupted 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 the stack is corrupted (e.g., by the stack protector test), collecting a backtrace during malloc causes a crash because the backtrace code walks the invalid stack frames. Update __stack_chk_fail() to set the flag before calling panic() Also update stackprot_test() to set the flag before intentionally corrupting the stack. This is needed because of the printf() in the test: on sandbox printf() results in truetype allocations due to the console output. These fixes allow the stack protector test to pass with mcheck enabled. Co-developed-by: Claude Signed-off-by: Simon Glass --- cmd/stackprot_test.c | 7 +++++++ common/stackprot.c | 6 ++++++ 2 files changed, 13 insertions(+) diff --git a/cmd/stackprot_test.c b/cmd/stackprot_test.c index e7ff4a06158..d7fbc3ecca0 100644 --- a/cmd/stackprot_test.c +++ b/cmd/stackprot_test.c @@ -4,6 +4,7 @@ */ #include +#include static int do_test_stackprot_fail(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) @@ -14,6 +15,12 @@ static int do_test_stackprot_fail(struct cmd_tbl *cmdtp, int flag, int argc, */ char a[128]; + /* + * Disable backtrace collection before corrupting the stack. + * Otherwise, any malloc (e.g., from printf/font rendering) will + * attempt to collect a backtrace from the corrupted stack and crash. + */ + malloc_backtrace_skip(true); memset(a, 0xa5, 512); printf("We have smashed our stack as this should not exceed 128: sizeof(a) = %zd\n", diff --git a/common/stackprot.c b/common/stackprot.c index 4e3297b7d00..408cd6d1e05 100644 --- a/common/stackprot.c +++ b/common/stackprot.c @@ -4,6 +4,7 @@ */ #include +#include DECLARE_GLOBAL_DATA_PTR; @@ -13,6 +14,11 @@ void __stack_chk_fail(void) { void *ra; + /* + * When the stack is corrupted, backtrace collection will crash. + * Skip it before calling panic(). + */ + malloc_backtrace_skip(true); ra = __builtin_extract_return_addr(__builtin_return_address(0)); panic("Stack smashing detected in function:\n%p relocated from %p", ra, ra - gd->reloc_off); From patchwork Wed Dec 10 00:07:24 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 890 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=1765325425; bh=xcAmDNvqLnbUWLYvqMQM6ovflsqKHFtO5Dy6858BuKE=; 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=okRLKbIT1gKLqMRn/7HfZtluIYF1bowwXuy4SGwF7tOc0D1cqhNSwaLFBsbSZcoue WjL1MWSTKUjgHK0ufIhI7csdLgiDYM2fqmq4zeAAcpMFJgoggywpNEArUUNLQd1FTV vkGdnSZsLf52QcFHGP8aGhDUZzW/01RzrfP6ifkkjOLTqKVLNZw9Qg6Kb7C0JC2RQL Nm7mp8LrxURzzrLmH/iWXzpMEpc//TYHU5i/h54Ysq1ZWU/7n9B6yz0g2HyfwatRgv YAqMJdhOZkBqd4Twsm6AjVt/fUqIKDNd3XetJCnmh4DgAZuFX9KPVxLt4NWsmBCEQs Gz1WTsvzsRhig== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id AF3F068A47 for ; Tue, 9 Dec 2025 17:10:25 -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 yjMxsuIGhq7U for ; Tue, 9 Dec 2025 17:10:25 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325425; bh=xcAmDNvqLnbUWLYvqMQM6ovflsqKHFtO5Dy6858BuKE=; 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=okRLKbIT1gKLqMRn/7HfZtluIYF1bowwXuy4SGwF7tOc0D1cqhNSwaLFBsbSZcoue WjL1MWSTKUjgHK0ufIhI7csdLgiDYM2fqmq4zeAAcpMFJgoggywpNEArUUNLQd1FTV vkGdnSZsLf52QcFHGP8aGhDUZzW/01RzrfP6ifkkjOLTqKVLNZw9Qg6Kb7C0JC2RQL Nm7mp8LrxURzzrLmH/iWXzpMEpc//TYHU5i/h54Ysq1ZWU/7n9B6yz0g2HyfwatRgv YAqMJdhOZkBqd4Twsm6AjVt/fUqIKDNd3XetJCnmh4DgAZuFX9KPVxLt4NWsmBCEQs Gz1WTsvzsRhig== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 9DA5E68A3E for ; Tue, 9 Dec 2025 17:10:25 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325423; bh=q5AhuEsh0y/lVTO2bxZz/2rH2P9I4DZV/lUVR319buA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rbxppQBO1E82nkTWaIIiR8EeNaXga8rpick8WOljC2bKfIyPn9iUZTiOFZGoQaHaL zwGN+UiJcTjHs0iplvCziL++qv2aTEefJhT8GtOpTczvYuX0cvB1c2ohPo6YYEkXOK wH4WkskN4/rBzITdLZDn1ZFJD6rqQQTXM4EEjliDJ6o3QkvGZUAnmtK4sDTc2Aaxd9 9O7qZ2lT742O+aC7YzZbL6joUeT2WUAEMoNKPurthIO1Yi5xNb6+TpBvsR0xrdBve1 aWvrePjBcTv2hO7M68pLM5X5bZsSZUeWqhgRFY03Vj4+Uy1HsFvrstgbJlsRyfYZEY FIcX+nWRM2AeA== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 9B68A68A3E; Tue, 9 Dec 2025 17:10:23 -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 VH3qapEAI6Cb; Tue, 9 Dec 2025 17:10:23 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325418; bh=yG+7rVMYiXen5yzaNqZHlGuj5c5aAPcfHgvUcVAq73k=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WLSEEpFUWpW/WcNLztvKupxWud/B6vjzQ2vPIv6fhsy4cGJhWwD/J9L4v47yzdu04 DhbM4XUxwNSLJsdcRKLJrZQe6wrQOM0mvtFmH+mp4hzki2cIfIwJTaLxT5bLY70pqX QcdT+APeySGtKdTKOQHzGyxIWf7VBVT/t5TgaU2gzm36buZkYbg0pM4pR14bBtEQiw wS6rLzAyOERWaOtj3+cchBZ7SQx6mf5Z90SSGCCaVRnMxSV48eqv4MilX8hT9OGbSe BWQk41Dqb6cbvJNfOUjBwDrzwIZmtiPy/a7r6gf5n4UVBsV4gnLfW+TnPWCt7rcthe RodUbHMdh0eww== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id 88CD66895B; Tue, 9 Dec 2025 17:10:18 -0700 (MST) From: Simon Glass To: U-Boot Concept Date: Tue, 9 Dec 2025 17:07:24 -0700 Message-ID: <20251210000737.180797-34-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: Y63CL7N5ET3SDSYZWXJPXQDGU762T4RV X-Message-ID-Hash: Y63CL7N5ET3SDSYZWXJPXQDGU762T4RV 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 33/35] malloc: Show caller info for freed chunks in malloc_dump 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 CONFIG_MCHECK_HEAP_PROTECTION is enabled, show the original caller info for freed chunks. This helps identify what code allocated memory that is now free. The mcheck header's canary and caller fields survive after free since dlmalloc only overwrites the first 16 bytes (size, aln_skip) with its free list pointers (fd, bk). We detect freed headers by looking for the MAGICFREE canary pattern. For small chunks or coalesced chunks where the header is not preserved, the caller info is simply omitted. Co-developed-by: Claude Signed-off-by: Simon Glass --- common/dlmalloc.c | 47 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/common/dlmalloc.c b/common/dlmalloc.c index c294b355ff2..62e1e0a77da 100644 --- a/common/dlmalloc.c +++ b/common/dlmalloc.c @@ -7093,6 +7093,39 @@ next: return NULL; } + +/** + * find_freed_mcheck_hdr() - find a freed mcheck header in a free chunk + * + * For freed chunks, the mcheck header remains with MAGICFREE canaries. + * Since freed entries are removed from the registry, scan the chunk + * for the MAGICFREE pattern. Only check offset 0 since free chunks + * may have been coalesced and we want the first (original) allocation. + * + * Note: dlmalloc overwrites the first 16 bytes (size, aln_skip) with + * free list pointers (fd, bk), but the canary and caller remain intact. + * + * @mem: chunk memory pointer (from chunk2mem) + * @sz: chunk size + * Return: pointer to mcheck header if found, NULL otherwise + */ +static struct mcheck_hdr *find_freed_mcheck_hdr(void *mem, size_t sz) +{ + struct mcheck_hdr *hdr = (struct mcheck_hdr *)mem; + int i; + + /* Only check at offset 0 - coalesced chunks lose alignment info */ + if (sz < sizeof(struct mcheck_hdr)) + return NULL; + + /* Check for MAGICFREE canary pattern */ + for (i = 0; i < CANARY_DEPTH; i++) { + if (hdr->canary.elems[i] != MAGICFREE) + return NULL; + } + + return hdr; +} #endif void malloc_dump(void) @@ -7142,8 +7175,20 @@ void malloc_dump(void) used += sz; used_count++; } else { - printf("%12lx %10zx \n", +#if CONFIG_IS_ENABLED(MCHECK_HEAP_PROTECTION) + struct mcheck_hdr *hdr; + + hdr = find_freed_mcheck_hdr(mem, sz); + if (hdr && hdr->caller[0]) + printf("%12lx %10zx free %s\n", + (ulong)mem, sz, hdr->caller); + else + printf("%12lx %10zx free\n", + (ulong)mem, sz); +#else + printf("%12lx %10zx free\n", (ulong)mem, sz); +#endif free_space += sz; free_count++; } From patchwork Wed Dec 10 00:07:25 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 891 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=1765325427; bh=YsAdI3Z95Eco433L3ZIuUFc8s03i00cE9qqJMpjRB5s=; 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=s7tiHyz3al4gmUm6QezQgNbsIEmETamlcWTQmEtHBlhaH2g1b9jtUhdwWzzfFeqLE IZsSnZRbDT6xbxDQHqCV5iGNLDMIuI0EPMoFr704j4oSeKQ17XukGfJxSKLp97OfAt QdNuWpxSZ2JpHqtdhdUOFo7UgHL056SNIwDmFEzsqM6lbGftNsamrgpBvQwyHO27KA 4GuMZ1SW19BHLLmjcYpcKVmpm7nyyUhUMEM1dh7aOVnNqkiCTrbGOdDA2hdNUr/+Th AQ2s+lveEzMfcT1b6vtaFxC+rfptQ5BKRX9SmtpdYHMKZjP6Weq0FyaDEkwgOgQmdl bcMd6097fHYPQ== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 2F86268A5D for ; Tue, 9 Dec 2025 17:10:27 -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 NbYaDyyHHoi1 for ; Tue, 9 Dec 2025 17:10:27 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325426; bh=YsAdI3Z95Eco433L3ZIuUFc8s03i00cE9qqJMpjRB5s=; 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=elAc6anjwBeF/MwFbq2fmoSLNwduc/L2eng2LYl3yw2p9Qd0xgsLLXJJh1R3svdbg N9NcffPngQRBAAKopd3qDRIsf+BFfCZBazvktc6e2wwSCuZUJa3I+GCM1xw8fPUWaj e8ziM4mA2uqa4pc2PtOSON6SQmekuYx9BSCc7Yt3aT/KJdwujo+6sq9vUGnv+Zt+LS Jpr+oR3dbprn3ti6bBDhQyJyFekbjwSmcp2solo6FWKWdhrCpz9Fkjx2clhacezrsp nfDtg/1HEbvhM4LWfr1ADErjm1hF2gMVFDtmecvFq9qSpbTNTtFBlWARyUH5pL004x mBks5lI5D/dnw== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 7E8B568A44 for ; Tue, 9 Dec 2025 17:10:26 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325424; bh=ppjB/IOAy0X5+Jx6XzZF965nmzkDu0sz+09pyiZtMQg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=exTUN1zISGpp9xXsdf/Xe0xZ29MJdE0suBrAdcp8qRAZ4aYJmBil94eDQTMKqvUfH M8fLKl7w1xv9g0HYZFxybUN4DJ/XtsGXJR94xrFuhmQ7ZQfs6nYewhhwnrJQtYC5KK r02+AF5TkDEfsOAclBskGUukpDkEUxGKlOWFJGgw8P9kWqKU9o+mYcve7WHtYwO3yA xtLu6sN78pAyQBm/Dx3GLoHxKQAByjp8vJkDKUFwperogbp2hUtFDwsjvtuYUl5hsE Ng62Ig7o3klg8xcAzSXovt3mPXb/BpdCMjtDHUHYf+wDmvC3X9yFP/f4hGHDAtdkJO ji+DmewW2n9mQ== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 23E356895B; Tue, 9 Dec 2025 17:10:24 -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 FSme5_XXyAoj; Tue, 9 Dec 2025 17:10:24 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325423; bh=9jbEjNQo46/7HDNtYvj04vhYWD8hdmj8EjmPPHY8YvM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ZEExKwzFmiD9qFzdOoxWIGSjd/xoS89oI9L4Dde1hKYeAsMBcP6jYnEA117++6GGt J5hIPv6eZAaYrvPX0HpRqDLKMbjIVh25vDgH4zoqxr5k5IcrioA1W9aB33BrG68wSw Pm/o3p/s0PVlz1oiGVfM5gP9fu1KOU1sdbaOmfTk5v4AsVuPAqxvbiZUim5aHYjVgj VV/NIsyCYlkhAL9aJ+/GpPOtFgOdroIpl7NKmWoSzypid3ervd45oDjF5rlguwePXL ojQk1QQJrkiUT1Dw1fmtk15OV5np2fVwQbzhQ3zQ7KF4BFq5m223mYXTLIsb+tGI7W V2iBmA5EaUmuw== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id 4EDAC689EA; Tue, 9 Dec 2025 17:10:23 -0700 (MST) From: Simon Glass To: U-Boot Concept Date: Tue, 9 Dec 2025 17:07:25 -0700 Message-ID: <20251210000737.180797-35-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: 6FCO7XDAGKYQP7Q26YOFOS4HGULBAHEW X-Message-ID-Hash: 6FCO7XDAGKYQP7Q26YOFOS4HGULBAHEW 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 34/35] malloc: Print mcheck registry-overflow message only once 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 Use a static bool flag to ensure the overflow warning is printed only once, avoiding repeated messages when the registry is full. Make sure to set the flag before calling printf(), which can itself do allocations with sandbox / Truetype. Co-developed-by: Claude Signed-off-by: Simon Glass --- common/mcheck_core.inc.h | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/common/mcheck_core.inc.h b/common/mcheck_core.inc.h index 63dbeaa5f56..598a5d018ab 100644 --- a/common/mcheck_core.inc.h +++ b/common/mcheck_core.inc.h @@ -201,6 +201,7 @@ static void *mcheck_allocated_helper(void *altoghether_ptr, size_t customer_sz, size_t alignment, int clean_content, const char *caller) { + static bool overflow_msg_shown; const size_t slop = alignment ? mcheck_evaluate_memalign_prefix_size(alignment) - sizeof(struct mcheck_hdr) : 0; struct mcheck_hdr *hdr = (struct mcheck_hdr *)((char *)altoghether_ptr + slop); @@ -239,10 +240,10 @@ static void *mcheck_allocated_helper(void *altoghether_ptr, size_t customer_sz, return payload; // normal end } - static char *overflow_msg = "\n\n\nERROR: mcheck registry overflow, pedantic check would be incomplete!!\n\n\n\n"; - - printf("%s", overflow_msg); - overflow_msg = "(mcheck registry full)"; + if (!overflow_msg_shown) { + overflow_msg_shown = true; + printf("\n\nERROR: mcheck registry overflow, pedantic check would be incomplete!\n\n"); + } return payload; } From patchwork Wed Dec 10 00:07:26 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 892 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=1765325431; bh=tgsGfH2HfuT25hzCjtVly5UeW3pCYMiNrF2TJ3dsU9Y=; 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=B4IWDNLdvFtaFSa8fYhBjsRLAlPG5IyWF1eYCDbG7wIJ4ObNadWewITNIIyiji2n+ uCbCxSJvv22rEAGbUkQDhwYf6WfVGKvFnvjMifAyxBW420M23cIKr/fXJo5JNHW7vW NULWJqaAqbnaXd6gWI29cl4pOecUbri1VnXeIAeGu5VnEwbW7hmqMRRF2lmmVKRds9 fR7J4T12Xd6AJAo1M68C82f8+s24BU0Jj2X6XqS8R9F2WTYBoyB+R+BejUzyWYkCwB 2DcyGOKc/KhwOS611STP/+TMAfX5FyD+eOInQ1qjkZHjZOkzKBG3zByKcshBzgYM2l AMWVjYgpKy+uA== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 10911689EA for ; Tue, 9 Dec 2025 17:10:31 -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 J0jwAIcys_TF for ; Tue, 9 Dec 2025 17:10:31 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325430; bh=tgsGfH2HfuT25hzCjtVly5UeW3pCYMiNrF2TJ3dsU9Y=; 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=Av34kNCejZCPaGkihTUYF/qZFZinqoxf65Um6rsa85DW/yFq6+itq3mYoURJHTVAu 2DZp16o4fhvBGUb2EfwnqSscwuGHthokCkxsQPPDpNWrH2f8j4tyo4WbzxKZKP4GFR geVaRk/4DWPLEBREcjCr0wStQyuAZFWH/VOB+GAQGa7GcDlEgA4cS/CXffJzlPgOSe /hH+E0S8GErhtWZG4gLBI5c8885Q4cQDxpedA2NzOjGgT0cZjpUbfkW43scRl9sNE9 h5Wm+JdI6jBbfCaILf8wDuC8Z5fHkwTp1QSRaUaNFbVH5ojc9+qvdUyp37oPQplxvK LzpKH8OEJh3Pw== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id F36D46895B for ; Tue, 9 Dec 2025 17:10:30 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325429; bh=rtDui87/krw82VUM0LnWC0cyX6SAJShObB+pbfG4OXo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=LklQo3DKUKXtAkRE8849AtSXK9/oksFNSK1GQarL37bqQN6XS88cazmMMW2/LvvIi e0uF22xVHjugxuGTYZIZD9jz06vUMxOnw25klmKCAurqEqTImXC5xwFxftxgtuaO+i utDqLQnZRDkYTUleXfx7gD02uEPreY+/debiceikGfIP1CJH39C4MU21rVNVN5kWHU QbkuU/EQuqLcEe6E8TMp0w+DxOUt3zhJKtMTBZ4rOvArR64DPoGYHqiCOi4gbnlwMA hzoQbOgbVLWEqYWQvhls1VBOde7bZtjw3Hp9SibBm+8YWA2bGKxAbeUdDduLqVJLCx YC5S4Y+UPEnCw== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 67BEE687A7; Tue, 9 Dec 2025 17:10:29 -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 s23HcKDvbilU; Tue, 9 Dec 2025 17:10:29 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325424; bh=m/BOtZf9aDRWbWusBfV392ySNWEN1wXuHGQLi81WYoI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=m+rOgEsAWDkdj9hIdDvXzFFB74Ik4ezemTCJODujrOLRAelVr5gkUIDUhUwuQqbFi CUyaLFTILOg7S50K6RWSHGgC2ZOjRGoa7tLhQC+NYvVVufQ4DLl8YYDlIlMKq0M/f0 Cn0WpAu8tHcF/TdBarikFLOh9BQsP1AONje2ANcLVwkOkPnTM1rBRLQAuEn2t/4lhL NHYSValBVocqIaQZQICq+wVwB+Ten49EmgTIz2qZVMY30VijNwf94KlmR2yQaHBxIW EUZruaVbLYonZOAcYnYVE93BHxRUOZzhH4hEx9G9PKcxxWeonOxp6ZpsIzSaMpXxkT geazHr6Ym24kg== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id 6E6AD689EA; Tue, 9 Dec 2025 17:10:24 -0700 (MST) From: Simon Glass To: U-Boot Concept Date: Tue, 9 Dec 2025 17:07:26 -0700 Message-ID: <20251210000737.180797-36-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: AAF6P2NUOGZLLACV52Q4HKPLBQN7DXB7 X-Message-ID-Hash: AAF6P2NUOGZLLACV52Q4HKPLBQN7DXB7 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 35/35] doc: malloc: Document debugging features 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 Update the malloc() with more info about the debugging features: - CONFIG_MALLOC_DEBUG and CONFIG_MCHECK_HEAP_PROTECTION Kconfig options - The malloc command with info and dump subcommands - Caller backtrace display when mcheck is enabled Co-developed-by: Claude Signed-off-by: Simon Glass --- doc/develop/malloc.rst | 103 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 95 insertions(+), 8 deletions(-) diff --git a/doc/develop/malloc.rst b/doc/develop/malloc.rst index 3c6b6ea65a4..8dba2d98afe 100644 --- a/doc/develop/malloc.rst +++ b/doc/develop/malloc.rst @@ -117,6 +117,19 @@ Main U-Boot (post-relocation) compatibility and testing. New boards should use the modern allocator. Default: n +``CONFIG_MALLOC_DEBUG`` + Bool to enable malloc debugging features. This enables the + ``malloc_get_info()`` function to retrieve memory statistics and supports + the ``malloc`` command. Default: y if UNIT_TEST is enabled. + +``CONFIG_MCHECK_HEAP_PROTECTION`` + Bool to enable heap protection using the mcheck library. This adds canary + values before and after each allocation to detect buffer overflows, + underflows, double-frees, and memory corruption. When enabled, caller + backtraces are recorded for each allocation and displayed by + ``malloc dump``. This significantly increases memory overhead and should + only be used for debugging. Default: n + xPL Boot Phases ~~~~~~~~~~~~~~~ @@ -298,17 +311,90 @@ for memory-leak detection. Debugging --------- -For debugging heap issues, consider: +U-Boot provides several features to help debug memory-allocation issues: + +CONFIG_MALLOC_DEBUG +~~~~~~~~~~~~~~~~~~~ + +Enable ``CONFIG_MALLOC_DEBUG`` to activate malloc debugging features. This is +enabled by default when ``CONFIG_UNIT_TEST`` is set. It provides: + +- The ``malloc_get_info()`` function to retrieve memory statistics +- Allocation call counters (malloc, free, realloc counts) +- Support for the ``malloc`` command (see :doc:`/usage/cmd/malloc`) + +The :doc:`/usage/cmd/malloc` command provides two subcommands: + +``malloc info`` + Shows memory-allocation statistics including total heap size, memory in use, + and call counts:: + + => malloc info + total bytes = 96 MiB + in use bytes = 700.9 KiB + malloc count = 1234 + free count = 567 + realloc count = 89 + +``malloc dump`` + Walks the entire heap and prints each chunk's address, size, and status + (used, free, or top). This is useful for understanding heap layout and + finding memory leaks:: + + => malloc dump + Heap dump: 19a0e000 - 1fa10000 + Address Size Status + ---------------------------------- + 19a0e000 10 (chunk header) + 19a0e010 a0 + 19a0e0b0 6070 + 19adfc30 60 + 19adff90 5f3f030 top + 1fa10000 end + ---------------------------------- + Used: c2ef0 bytes in 931 chunks + Free: 5f3f0c0 bytes in 2 chunks + top + +CONFIG_MCHECK_HEAP_PROTECTION +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Enable ``CONFIG_MCHECK_HEAP_PROTECTION`` for heap protection using the mcheck +library. This adds canary values before and after each allocation to detect: + +- Buffer overflows and underflows +- Double-frees +- Memory corruption + +This significantly increases memory overhead and should only be used for +debugging. U-Boot includes mcheck support via mcheck(), mcheck_pedantic(), and +mcheck_check_all(). + +When mcheck is enabled, the ``malloc dump`` command also shows caller +information for each allocation, including a backtrace showing where the +allocation was made:: + + => malloc dump + Heap dump: 18a1d000 - 1ea1f000 + Address Size Status + ---------------------------------- + 18a1d000 10 (chunk header) + 18a1d010 90 used log_init:453 <-board_init_r:774 + 18a1d0a0 6060 used membuf_new:420 <-console_record + 18a3b840 90 used of_alias_scan:911 <-board_init_ + +This caller information makes it easy to track down memory leaks by showing +exactly where each allocation originated. + +Valgrind +~~~~~~~~ -1. **mcheck**: U-Boot includes mcheck support for detecting buffer overruns. - Enable CONFIG_MCHECK to use mcheck(), mcheck_pedantic(), and - mcheck_check_all(). +When running sandbox with Valgrind, the allocator includes annotations to help +detect memory errors. See :ref:`sandbox_valgrind`. -2. **Valgrind**: When running sandbox with Valgrind, the allocator includes - annotations to help detect memory errors. See :ref:`sandbox_valgrind`. +malloc testing +~~~~~~~~~~~~~~ -3. **malloc testing**: Unit tests can use malloc_enable_testing() to simulate - allocation failures. +Unit tests can use malloc_enable_testing() to simulate allocation failures. API Reference ------------- @@ -331,3 +417,4 @@ See Also - :doc:`memory` - Memory management overview - :doc:`global_data` - Global data and the GD_FLG_FULL_MALLOC_INIT flag +- :doc:`/usage/cmd/malloc` - malloc command reference