Skip to content

mm/mm_heap: memdump and mm_foreach() fixes - #22275

Merged
xiaoxiang781216 merged 5 commits into
apache:masterfrom
huojianchaoyy:nx-mm-heap-memdump-fixes-v1
Nov 25, 2026
Merged

xiaoxiang781216 merged 5 commits into
apache:masterfrom
huojianchaoyy:nx-mm-heap-memdump-fixes-v1

Conversation

@huojianchaoyy

Copy link
Copy Markdown

Note: Please adhere to Contributing Guidelines.

Summary

Heap fixes from Xiaomi Vela, split out of #22206 as asked in its review.

  • mm: support memdump default parameters: mm_memdump() keeps its own copy of the request; with CONFIG_MM_BACKTRACE_SEQNO, seqmin = seqmax = 0 means the whole sequence range. mmdump: fix pid ignored in foreach then keeps the pid of the request.
  • mm/memdump: fix typos: with CONFIG_MM_BACKTRACE > 0 and without CONFIG_MM_BACKTRACE_SEQNO, the format of memdump_allocnode() lost the '%' of "%*p" and passed an int to "%s" (crash on sim, see Testing).
  • group.h:add task_group_s statement: forward declaration of struct task_group_s in kmalloc.h, for the group heap prototypes.
  • mm/mm_heap: fix null pointer dereference in mm_foreach: mm_memdump() walks the heap with mm_foreach(), whose check of the first node read its NULL predecessor when the node is corrupted.

Impact

  • With CONFIG_MM_BACKTRACE_SEQNO, a memdump request with seqmin = seqmax = 0 dumps all sequence numbers instead of sequence number 0 only.
  • No API or Kconfig change.

Testing

Host: Ubuntu 22.04 x86_64, GCC 13.4.0, CMake 3.23 + Ninja 1.10. Base: master 25b649e.

./tools/checkpatch.sh -c -u -m -g HEAD~5..HEAD passes.

sim:nsh with CONFIG_MM_BACKTRACE=8 CONFIG_MM_BACKTRACE_SEQNO=n, echo used > /proc/memdump:

master:  PID        Size Overhead            Address Backtrace
              0          88       88*p        <- segmentation fault
this PR: PID        Size Overhead            Address Backtrace
              0          88       88     0x7af5da2007e8

With CONFIG_MM_BACKTRACE_SEQNO=y (the default) the /proc/memdump output (used, pid, leak) is the same as on master.

Builds (CMake): sim:nsh with backtrace 8 / 0, with and without MM_BACKTRACE_SEQNO, and with the TLSF manager with and without MM_BACKTRACE_SEQNO; /proc/memdump runs on the backtrace 8, backtrace 0 and TLSF builds.

Not tested: real hardware.

Gary-Hobson and others added 5 commits November 24, 2026 20:05
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 <yinshengkai@xiaomi.com>
Should not set to 0 again, as already memset.

Signed-off-by: buxiasen <buxiasen@xiaomi.com>
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 <yinshengkai@xiaomi.com>
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 <anjiahao@xiaomi.com>
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 <fangxinyong@xiaomi.com>
@github-actions github-actions Bot added Area: Memory Management Memory Management issues Size: S The size of the change in this PR is small labels Nov 24, 2026
@github-actions

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

esp32-devkitc

  • ROM: .flash.text -4 B (-0.0%, 133,480 B / 4,194,272 B, total: 3% used)
  • irom0_0_seg: .flash.text -4 B (-0.0%, 99,132 B / 3,342,304 B, total: 3% used)

hifive1-revb

  • flash: .text -8 B (-0.0%, 86,632 B / 4,194,304 B, total: 2% used)

mps2-an500-citest-protected

  • flash: .text +8 B (+0.0%, 116,472 B / 4,194,304 B, total: 3% used)

qemu-armv8a

  • Code: .text.memdump_handler -4 B, .text.mm_foreach +4 B, .text.mm_memdump -8 B (-0.0%, 377,624 B)

qemu-intel64

  • Code: .text +1 B (+0.0%, 8,707,090 B)
  • Data: .rodata +32 B (+0.0%, 109,089 B)

s698pm-dkit

  • Code: .text +48 B (+0.0%, 408,944 B)

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

@xiaoxiang781216
xiaoxiang781216 merged commit 49f9130 into apache:master Nov 25, 2026
58 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: Memory Management Memory Management issues Size: S The size of the change in this PR is small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants