Skip to content

[kernel][mem] fix false MEM_POOL assert when freeing block before heap_end - #12014

Merged
Rbb666 merged 1 commit into
RT-Thread:masterfrom
cms19859230182-lang:fix/smem-free-mempool-assert
Nov 12, 2026
Merged

Rbb666 merged 1 commit into
RT-Thread:masterfrom
cms19859230182-lang:fix/smem-free-mempool-assert

Conversation

@cms19859230182-lang

@cms19859230182-lang cms19859230182-lang commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

中文

问题

释放堆上最后一块时,rt_smem_free() 用 MEM_POOL(next) 校验会误断言:最后一块的 next 是 heap_end 哨兵,部分实现里哨兵只带 used bit(#10632)。

改动

  • src/mem.c:next == heap_end 则放行;其它 next 仍查同 pool
  • 不改用 mem->prev(语义不同)
  • src/utest/mem_tc.c:加回归用例

验证

主机 core.mem 通过;未跑板级。

English

Problem

Freeing the last heap block false-asserts in rt_smem_free(): next is the heap_end sentinel, and some pools only store the used bit there (#10632).

Changes

  • src/mem.c: allow next == heap_end; keep same-pool check otherwise
  • Do not use mem->prev
  • src/utest/mem_tc.c: regression test

Test plan

Host core.mem OK; no board run.

Fixes #10632

…p_end

rt_smem_free() validated mem->next by requiring the next item's pool
pointer to be the current pool. For the last block, mem->next points at
the heap_end sentinel. Older/vendor small-mem pools may keep only the
used bit in the sentinel's pool_ptr, so MEM_POOL(heap_end) is NULL and
the assertion fires although the heap is consistent.

Accept mem->next == heap_end offset explicitly and keep the same-pool
check for every other next item. Changing the check to mem->prev is not
equivalent: prev describes the previous block (0 for the first block), so
it would not validate the next pointer.

Reproduced on the host with the real mem.c by making the sentinel carry
only the used bit: the old assertion aborts, the new one passes. Added a
core.mem utest that allocates the last block, simulates that sentinel
state, frees it, and verifies the heap is fully merged. Normal middle,
first and last frees/merges remain covered by the existing test.

No functional change for well-formed heaps.
@github-actions

Copy link
Copy Markdown

👋 感谢您对 RT-Thread 的贡献!Thank you for your contribution to RT-Thread!

为确保代码符合 RT-Thread 的编码规范,请在你的仓库中执行以下步骤运行代码格式化工作流(如果格式化CI运行失败)。
To ensure your code complies with RT-Thread's coding style, please run the code formatting workflow by following the steps below (If the formatting of CI fails to run).


🛠 操作步骤 | Steps

  1. 前往 Actions 页面 | Go to the Actions page
    点击进入工作流 → | Click to open workflow →

  2. 点击 Run workflow | Click Run workflow

  • Use workflow from 保持默认分支(通常为 master)
    Keep the default branch (usually master) in Use workflow from
  • 在 branch 输入框填写 PR 分支 fix/smem-free-mempool-assert
    Enter PR branch fix/smem-free-mempool-assert in the branch field
  • 设置需排除的文件/目录(目录请以"/"结尾)
    Set files/directories to exclude (directories should end with "/")
  1. 等待工作流完成 | Wait for the workflow to complete
    格式化后的代码将作为独立提交推送至你的分支。
    The formatting changes will be pushed to your branch as a separate commit.

完成后,提交将自动更新至 fix/smem-free-mempool-assert 分支,关联的 Pull Request 也会同步更新。
Once completed, commits will be pushed to the fix/smem-free-mempool-assert branch automatically, and the related Pull Request will be updated.

如有问题欢迎联系我们,再次感谢您的贡献!💐
If you have any questions, feel free to reach out. Thanks again for your contribution!

@github-actions github-actions Bot added the Kernel PR has src relate code label Nov 10, 2026
@github-actions

Copy link
Copy Markdown

📌 Code Review Assignment

🏷️ Tag: kernel

Reviewers: @GorrayLi @ReviewSun @hamburger-os @lianux-mm @wdfk-prog @xu18838022837

Changed Files (Click to expand)
  • src/mem.c
  • src/utest/mem_tc.c

📊 Current Review Status (Last Updated: 2026-11-10 09:05 CST)


📝 Review Instructions

  1. 维护者可以通过单击此处来刷新审查状态: 🔄 刷新状态
    Maintainers can refresh the review status by clicking here: 🔄 Refresh Status

  2. 确认审核通过后评论 LGTM/lgtm
    Comment LGTM/lgtm after confirming approval

  3. PR合并前需至少一位维护者确认
    PR must be confirmed by at least one maintainer before merging

ℹ️ 刷新CI状态操作需要具备仓库写入权限。
ℹ️ Refresh CI status operation requires repository Write permission.

@github-actions

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

at32f415-start

  • ROM: .text +56 B (+0.1%, 65,500 B / 262,144 B, total: 25% used)

esp32-c3

  • dram0_0_seg: .dram0.data +36 B, .stack +4 B (+0.0%, 216,832 B / 325,120 B, total: 67% used)

gd32105r-start

  • CODE: .text +56 B (+0.1%, 69,412 B / 262,144 B, total: 26% used)

hc32f334

  • FLASH: .text +56 B (+0.1%, 99,668 B / 131,072 B, total: 76% used)

hpmicro-hpm5100evk

  • ILM: .fast +32 B (+0.1%, 42,672 B / 262,144 B, total: 16% used)
  • XPI0: .fast +32 B (+0.0%, 102,956 B / 1,048,576 B, total: 10% used)

infineon-psoc6

  • flash: .text +56 B (+0.0%, 116,268 B / 262,144 B, total: 44% used)

loongson-ls1cdev

  • Code: .text +84 B (+0.0%, 363,492 B)

nordic-nrf51822

  • FLASH: .text +60 B (+0.1%, 52,116 B / 262,144 B, total: 20% used)

nuvoton-m487

  • CODE: .text +52 B (+0.0%, 356,632 B / 524,288 B, total: 68% used)

qemu-virt64-aarch64

  • Code: .text +112 B (+0.0%, 1,009,868 B)

raspberry-pico-rp2040

  • FLASH: .rodata +40 B, .text +24 B (+0.1%, 114,256 B / 2,097,152 B, total: 5% used)

renesas-ra2l1

  • FLASH: .text +60 B (+0.1%, 98,364 B / 262,144 B, total: 38% used)

simulator

  • Code: .rela.dyn +648 B, .rodata +128 B, .text +309 B (+0.0%, 2,674,143 B)
  • Data: .data +768 B (+0.0%, 1,609,796 B)

stm32f407-rt-spark

  • CODE: .text +56 B (+0.1%, 86,144 B / 1,048,576 B, total: 8% used)

stm32l475-atk-pandora-llvm

  • ROM: .text +56 B (+0.1%, 82,768 B / 524,288 B, total: 16% used)

wch-ch32v208w-r0

  • FLASH: .text +64 B (+0.0%, 159,952 B / 491,520 B, total: 33% used)

x86

  • Code: .rodata +32 B, .text +32 B (+0.0%, 199,005 B)

xuantie-e901plus

  • ISRAM: .rodata +64 B (+0.1%, 46,488 B / 131,072 B, total: 35% used)

No memory changes detected for:


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

@cms19859230182-lang
cms19859230182-lang marked this pull request as draft November 10, 2026 15:40
@cms19859230182-lang
cms19859230182-lang marked this pull request as ready for review November 11, 2026 03:17

@BernardXiong BernardXiong left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

结论:未发现阻塞问题,可以合并。

rt_smem_free() 用 next == heap_end_offset 放行 heap_end 哨兵,其它 next 仍走 MEM_POOL() 的同 pool 校验,语义正确;新增的 utest 覆盖了最后一块 + 哨兵 pool_ptr 只带 used bit 的场景。

CI:108 pass / 4 skip,全部通过。

@Rbb666
Rbb666 merged commit 10e6d0f into RT-Thread:master Nov 12, 2026
115 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Kernel PR has src relate code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Wrong MEM_POOL check in rt_smem_free

3 participants