From 0aeddf9992774c833247390252ce6c5b34590a7f Mon Sep 17 00:00:00 2001 From: yinshengkai Date: Thu, 6 Feb 2025 11:10:26 +0800 Subject: [PATCH 1/5] mm: support memdump default parameters mm_memdump() of both heap managers now keeps its own copy of the request instead of a pointer to the caller's. With CONFIG_MM_BACKTRACE_SEQNO, a request whose seqmin and seqmax are both 0 (a caller that leaves them unset) is taken as the whole sequence range, 0 to ULONG_MAX, instead of sequence number 0 only. Assisted-by: Kiro:claude-opus-5-5 Signed-off-by: yinshengkai --- mm/mm_heap/mm_memdump.c | 22 +++++++++++++++++----- mm/tlsf/mm_tlsf.c | 22 +++++++++++++++++----- 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/mm/mm_heap/mm_memdump.c b/mm/mm_heap/mm_memdump.c index d514e710710d4..c29ad8d55798b 100644 --- a/mm/mm_heap/mm_memdump.c +++ b/mm/mm_heap/mm_memdump.c @@ -46,7 +46,7 @@ struct mm_memdump_priv_s { - FAR const struct mm_memdump_s *dump; + struct mm_memdump_s dump; struct mallinfo_task info; #if CONFIG_MM_HEAP_BIGGEST_COUNT > 0 FAR struct mm_allocnode_s *node[CONFIG_MM_HEAP_BIGGEST_COUNT]; @@ -154,7 +154,7 @@ static inline_function void memdump_info_pool(FAR struct mm_memdump_priv_s *priv, FAR struct mm_heap_s *heap) { - priv->info = mempool_multiple_info_task(heap->mm_mpool, priv->dump); + priv->info = mempool_multiple_info_task(heap->mm_mpool, &priv->dump); } static inline_function @@ -163,7 +163,7 @@ void memdump_dump_pool(FAR struct mm_memdump_priv_s *priv, { if (priv->info.aordblks > 0) { - mempool_multiple_memdump(heap->mm_mpool, priv->dump); + mempool_multiple_memdump(heap->mm_mpool, &priv->dump); } } #else @@ -174,7 +174,7 @@ void memdump_dump_pool(FAR struct mm_memdump_priv_s *priv, static void memdump_handler(FAR struct mm_allocnode_s *node, FAR void *arg) { FAR struct mm_memdump_priv_s *priv = arg; - FAR const struct mm_memdump_s *dump = priv->dump; + FAR const struct mm_memdump_s *dump = &priv->dump; size_t nodesize = MM_SIZEOF_NODE(node); if (MM_NODE_IS_ALLOC(node)) @@ -251,7 +251,19 @@ void mm_memdump(FAR struct mm_heap_s *heap, pid_t pid = dump->pid; memset(&priv, 0, sizeof(struct mm_memdump_priv_s)); - priv.dump = dump; + +#ifdef CONFIG_MM_BACKTRACE_SEQNO + if (dump->seqmin == 0 && dump->seqmax == 0) + { + priv.dump.seqmin = 0; + priv.dump.seqmax = ULONG_MAX; + } + else + { + priv.dump.seqmin = dump->seqmin; + priv.dump.seqmax = dump->seqmax; + } +#endif if (pid == PID_MM_MEMPOOL) { diff --git a/mm/tlsf/mm_tlsf.c b/mm/tlsf/mm_tlsf.c index f8e9c3bb7305e..854ad31aad9bc 100644 --- a/mm/tlsf/mm_tlsf.c +++ b/mm/tlsf/mm_tlsf.c @@ -172,7 +172,7 @@ struct mm_tlsf_node_s struct mm_memdump_priv_s { - FAR const struct mm_memdump_s *dump; + struct mm_memdump_s dump; struct mallinfo_task info; #if CONFIG_MM_HEAP_BIGGEST_COUNT > 0 struct mm_tlsf_node_s node[CONFIG_MM_HEAP_BIGGEST_COUNT]; @@ -185,7 +185,7 @@ static inline_function void memdump_info_pool(FAR struct mm_memdump_priv_s *priv, FAR struct mm_heap_s *heap) { - priv->info = mempool_multiple_info_task(heap->mm_mpool, priv->dump); + priv->info = mempool_multiple_info_task(heap->mm_mpool, &priv->dump); } static inline_function @@ -194,7 +194,7 @@ void memdump_dump_pool(FAR struct mm_memdump_priv_s *priv, { if (priv->info.aordblks > 0) { - mempool_multiple_memdump(heap->mm_mpool, priv->dump); + mempool_multiple_memdump(heap->mm_mpool, &priv->dump); } } #else @@ -696,7 +696,7 @@ static void memdump_handler(FAR void *ptr, size_t size, int used, FAR void *user) { FAR struct mm_memdump_priv_s *priv = user; - FAR const struct mm_memdump_s *dump = priv->dump; + FAR struct mm_memdump_s *dump = &priv->dump; if (used) { @@ -1335,7 +1335,19 @@ void mm_memdump(FAR struct mm_heap_s *heap, pid_t pid = dump->pid; memset(&priv, 0, sizeof(struct mm_memdump_priv_s)); - priv.dump = dump; + +#ifdef CONFIG_MM_BACKTRACE_SEQNO + if (dump->seqmin == 0 && dump->seqmax == 0) + { + priv.dump.seqmin = 0; + priv.dump.seqmax = ULONG_MAX; + } + else + { + priv.dump.seqmin = dump->seqmin; + priv.dump.seqmax = dump->seqmax; + } +#endif free_delaylist(heap, true); From f66ae9c8c431e3348e63d77b394efd27dc857db0 Mon Sep 17 00:00:00 2001 From: buxiasen Date: Mon, 17 Feb 2025 23:36:57 +0800 Subject: [PATCH 2/5] mmdump: fix pid ignored in foreach Should not set to 0 again, as already memset. Signed-off-by: buxiasen --- mm/mm_heap/mm_memdump.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mm/mm_heap/mm_memdump.c b/mm/mm_heap/mm_memdump.c index c29ad8d55798b..0db564230eaa7 100644 --- a/mm/mm_heap/mm_memdump.c +++ b/mm/mm_heap/mm_memdump.c @@ -251,11 +251,11 @@ void mm_memdump(FAR struct mm_heap_s *heap, pid_t pid = dump->pid; memset(&priv, 0, sizeof(struct mm_memdump_priv_s)); + priv.dump.pid = pid; #ifdef CONFIG_MM_BACKTRACE_SEQNO if (dump->seqmin == 0 && dump->seqmax == 0) { - priv.dump.seqmin = 0; priv.dump.seqmax = ULONG_MAX; } else From fafccc8cf7b13fa1690de7591f14a641caf3702a Mon Sep 17 00:00:00 2001 From: yinshengkai Date: Tue, 14 Jan 2025 15:14:03 +0800 Subject: [PATCH 3/5] mm/memdump: fix typos The '%' of the "%*p" conversion in memdump_allocnode() sat in the string literal that is only compiled with CONFIG_MM_BACKTRACE_SEQNO. Without it the format read "...%9zu*p %s", so the address was printed as the text "*p" and "%s" took the width argument. Keep the whole conversion in the unconditional literal. Assisted-by: Kiro:claude-opus-5-5 Signed-off-by: yinshengkai --- mm/mm_heap/mm_memdump.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mm/mm_heap/mm_memdump.c b/mm/mm_heap/mm_memdump.c index 0db564230eaa7..6aff1a20302bb 100644 --- a/mm/mm_heap/mm_memdump.c +++ b/mm/mm_heap/mm_memdump.c @@ -86,9 +86,9 @@ static void memdump_allocnode(FAR struct mm_allocnode_s *node) syslog(LOG_INFO, "%6d%12zu%9zu" # ifdef CONFIG_MM_BACKTRACE_SEQNO - "%12lu%" + "%12lu" # endif - "*p %s\n", + "%*p %s\n", node->pid, nodesize, overhead, # ifdef CONFIG_MM_BACKTRACE_SEQNO node->seqno, From 2a5e98deb0df9398e23c6d626e2ee764e72ed5c4 Mon Sep 17 00:00:00 2001 From: anjiahao Date: Wed, 10 Sep 2025 11:08:29 +0800 Subject: [PATCH 4/5] group.h:add task_group_s statement The group heap prototypes of include/nuttx/kmalloc.h take a struct task_group_s pointer. Forward-declare the structure, so that they do not depend on its definition in nuttx/sched.h having been seen. Assisted-by: Kiro:claude-opus-5-5 Signed-off-by: anjiahao --- include/nuttx/kmalloc.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/nuttx/kmalloc.h b/include/nuttx/kmalloc.h index 408f06374e4d8..8192bffec7b7f 100644 --- a/include/nuttx/kmalloc.h +++ b/include/nuttx/kmalloc.h @@ -129,6 +129,8 @@ extern "C" * ****************************************************************************/ +struct task_group_s; + /* Functions defined in group/group_malloc.c ********************************/ FAR void *group_malloc(FAR struct task_group_s *group, size_t nbytes); From 9551bd39248366de9c5645f6c7f737b96e13def0 Mon Sep 17 00:00:00 2001 From: fangxinyong Date: Fri, 24 Apr 2026 11:38:19 +0800 Subject: [PATCH 5/5] mm/mm_heap: fix null pointer dereference in mm_foreach On the first loop iteration prev is NULL. If the heap start node has a corrupted preceding field with MM_PREVFREE_BIT set, the short-circuit evaluation of the DEBUGASSERT falls through to MM_SIZEOF_NODE(prev) which dereferences NULL. Add a prev == NULL guard so the preceding-size consistency check is skipped for the first node, which has no predecessor. CID 1817303 (#1 of 1): Dereference null pointer (NULL_RETURNS) Signed-off-by: fangxinyong --- mm/mm_heap/mm_foreach.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mm/mm_heap/mm_foreach.c b/mm/mm_heap/mm_foreach.c index b310170827a57..22c0f99271696 100644 --- a/mm/mm_heap/mm_foreach.c +++ b/mm/mm_heap/mm_foreach.c @@ -93,7 +93,8 @@ void mm_foreach(FAR struct mm_heap_s *heap, mm_node_handler_t handler, handler(node, arg); - DEBUGASSERT(MM_PREVNODE_IS_ALLOC(node) || + DEBUGASSERT(prev == NULL || + MM_PREVNODE_IS_ALLOC(node) || MM_SIZEOF_NODE(prev) == node->preceding); prev = node; }