From patchwork Mon Mar 16 18:30:31 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 2019 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=1773685887; bh=rx0VSRRi9KQ6tqEFDzPSo7DaahePzTatFyurrMkKnm4=; 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=d4P99gQWLAd9eRtGgDtR7bQjETV2wyvg4H/R7u843d7MmX1cRyF+cfs/dcvFZpB10 CUfbJFOm+I+j9qlTho4eKqUxPNUDi3O2i32dL8TGWbiMsT/m2yLit7tTgLzssB35F3 J3n3gDGxFuMr1+xgIiCSAZsIwTIRuM+z+3ZLgm+0o6lZAQyJjTJQnjX8mysT+j5FAp o4BDoabjvRUgILeYx0S97b5dn4hdokIvhpdtA8CIwqnTkJVIynem2FdTC+BgHYLQnZ awNMzh3hRspxLpRzn0nrpwRd88TJSNEBW1OUvr92++JJjSLM2ZVWLeNzbi+8ocWfL0 +BE+bh87TjYzQ== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 78BAA6A09B for ; Mon, 16 Mar 2026 12:31:27 -0600 (MDT) 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 iMGQVTHZ_ilY for ; Mon, 16 Mar 2026 12:31:27 -0600 (MDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1773685887; bh=rx0VSRRi9KQ6tqEFDzPSo7DaahePzTatFyurrMkKnm4=; 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=d4P99gQWLAd9eRtGgDtR7bQjETV2wyvg4H/R7u843d7MmX1cRyF+cfs/dcvFZpB10 CUfbJFOm+I+j9qlTho4eKqUxPNUDi3O2i32dL8TGWbiMsT/m2yLit7tTgLzssB35F3 J3n3gDGxFuMr1+xgIiCSAZsIwTIRuM+z+3ZLgm+0o6lZAQyJjTJQnjX8mysT+j5FAp o4BDoabjvRUgILeYx0S97b5dn4hdokIvhpdtA8CIwqnTkJVIynem2FdTC+BgHYLQnZ awNMzh3hRspxLpRzn0nrpwRd88TJSNEBW1OUvr92++JJjSLM2ZVWLeNzbi+8ocWfL0 +BE+bh87TjYzQ== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 18C936A0AC for ; Mon, 16 Mar 2026 12:31:27 -0600 (MDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1773685880; bh=wtb4B22sKhZuaV/j0OJnsR5Yto/M78OVn6rH4Kxdr20=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=l5I9bLfq7JE8ojwZJtz77f9nxJi7M2Pgu8An9F1pWT6DejhrdIZYuk+cBKQrg3wz7 rtY5Nb6qseCWGcWpYfK4c7nPfEQT0C6Nk4+aUd3z38kzGXcs1Sv8nAN/bz63RX+pb2 R69QQFnWFQkFmLyrWkwjQuD/GCoNmvRA9hMl/nDb/Wl4x7AONigg23jh8HeuyAfNJC CdQ06LU8YynYg+DnoKtFbQ9ex7m0logLCe/O7Hd3F2ArfbxS4zp8dTLjV+7OzRkzWl muKxn2lvsNMX6umS7wr0SMLm1XcnjseiqO1E/IWykm/GmAvKOsVrEKm31yhIsUpBja mntKkpDC3lIlg== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 61C896A07B; Mon, 16 Mar 2026 12:31:20 -0600 (MDT) 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 X0rKh8H51pvQ; Mon, 16 Mar 2026 12:31:20 -0600 (MDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1773685878; bh=ltAg2Ffak6sUHbMdSZkPXTAALgIqIHs+Akk+cdTTyP8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=R0YALA4a2RaF07HaqqxQiZHpyvMdR6DqRvNdj5saLBAy0C/jfeovGZoQK56htX034 RaUvp1xxvdOg+inqDPaYx7nla1H+7U85pllKZvndnjKpLNZeTen6K7oizEvF/xNlCf /Coebm/AMzxYSRLYCdAM3Y3OUdcz30b4iv3aRk6a+WWDB1YNx5dOZmNEKMIzzbW45o E9fEBoqwcTUZO1uS6i9gELF3OGOS2hE4AcLOhC9ChTHwcsbr6dBnzAdTxwRMJ2yKgC UXg9IAmPRm+WIpG/pkOsO5Fvdw6VdeoCmm2bG4+R+lkVWwtDFzqkC65Er/iBasZNSc JEtCASWyX/L/A== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id 4B3CF6A0A6; Mon, 16 Mar 2026 12:31:18 -0600 (MDT) From: Simon Glass To: U-Boot Concept Date: Mon, 16 Mar 2026 12:30:31 -0600 Message-ID: <20260316183050.3855921-9-sjg@u-boot.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260316183050.3855921-1-sjg@u-boot.org> References: <20260316183050.3855921-1-sjg@u-boot.org> MIME-Version: 1.0 Message-ID-Hash: V3BBJIQMIBA5FKDQTCU2APBGASHJVOFC X-Message-ID-Hash: V3BBJIQMIBA5FKDQTCU2APBGASHJVOFC 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: Simon Glass X-Mailman-Version: 3.3.10 Precedence: list Subject: [Concept] [PATCH 08/17] test: Reset malloc backtrace collection before each test 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 stack-protector test and __stack_chk_fail() call malloc_backtrace_skip(true) to prevent crashes when collecting backtraces from a corrupted stack. However, they never reset it, so all subsequent allocations in a long-running session (e.g. pytest) have empty caller backtraces. Add malloc_backtrace_unbusy() to clear the reentrant guard which can also get stuck if backtrace collection crashes. Reset both flags at the start of each test in test_pre_run() Signed-off-by: Simon Glass --- common/dlmalloc.c | 24 ++++++++++++++++++++++++ include/malloc.h | 30 ++++++++++++++++++++++++++++++ test/test-main.c | 8 ++++++++ 3 files changed, 62 insertions(+) diff --git a/common/dlmalloc.c b/common/dlmalloc.c index e61f565bbab..f05d7ae83c5 100644 --- a/common/dlmalloc.c +++ b/common/dlmalloc.c @@ -5986,6 +5986,30 @@ void malloc_backtrace_skip(bool skip) #endif } +void malloc_backtrace_unbusy(void) +{ +#if CONFIG_IS_ENABLED(MCHECK_BACKTRACE) + in_backtrace = false; +#endif +} + +bool malloc_backtrace_is_active(bool *skipp, bool *busyp) +{ +#if CONFIG_IS_ENABLED(MCHECK_BACKTRACE) + if (skipp) + *skipp = mcheck_skip_backtrace; + if (busyp) + *busyp = in_backtrace; + return !mcheck_skip_backtrace && !in_backtrace; +#else + if (skipp) + *skipp = false; + if (busyp) + *busyp = false; + return false; +#endif +} + static const char *mcheck_caller(void) { #if CONFIG_IS_ENABLED(MCHECK_BACKTRACE) diff --git a/include/malloc.h b/include/malloc.h index ded5520ca23..05d4c06fb89 100644 --- a/include/malloc.h +++ b/include/malloc.h @@ -827,10 +827,40 @@ int malloc_log_entry(uint idx, struct mlog_entry **entryp); * * @skip: true to skip backtrace collection, false to enable it */ + +/** + * malloc_backtrace_unbusy() - Clear the backtrace reentrant guard + * + * The malloc backtrace collector sets a guard flag while collecting a + * backtrace to prevent re-entrancy. If a crash or longjmp occurs during + * collection, the guard stays set and all subsequent backtraces are + * silently skipped. Call this to reset it. + */ + +/** + * malloc_backtrace_is_active() - Check whether backtrace collection works + * + * @skipp: If non-NULL, returns true if collection is disabled via + * malloc_backtrace_skip() + * @busyp: If non-NULL, returns true if the reentrant guard is stuck + * Return: true if backtrace collection is active (neither skipped nor busy) + */ #if CONFIG_IS_ENABLED(MCHECK_HEAP_PROTECTION) void malloc_backtrace_skip(bool skip); +void malloc_backtrace_unbusy(void); +bool malloc_backtrace_is_active(bool *skipp, bool *busyp); #else static inline void malloc_backtrace_skip(bool skip) {} +static inline void malloc_backtrace_unbusy(void) {} +static inline bool malloc_backtrace_is_active(bool *skipp, bool *busyp) +{ + if (skipp) + *skipp = false; + if (busyp) + *busyp = false; + + return false; +} #endif /** diff --git a/test/test-main.c b/test/test-main.c index 09458fa91da..06bb0ff6e1b 100644 --- a/test/test-main.c +++ b/test/test-main.c @@ -464,6 +464,14 @@ static int dm_test_restore(struct device_node *of_root) */ static int test_pre_run(struct unit_test_state *uts, struct unit_test *test) { + /* + * Reset backtrace collection in case a previous test disabled it + * (e.g. stack-protector test via __stack_chk_fail) or a crash + * left the reentrant guard set. + */ + malloc_backtrace_skip(false); + malloc_backtrace_unbusy(); + ut_assertok(event_init()); /*