Skip to content

[components][asan] Add runtime AddressSanitizer for heap - #11772

Open
Zepp-Hanzj wants to merge 4 commits into
RT-Thread:masterfrom
Zepp-Hanzj:agent/rt-asan
Open

[components][asan] Add runtime AddressSanitizer for heap#11772
Zepp-Hanzj wants to merge 4 commits into
RT-Thread:masterfrom
Zepp-Hanzj:agent/rt-asan

Conversation

@Zepp-Hanzj

Copy link
Copy Markdown
Contributor

Description / 描述

Add a heap-only runtime AddressSanitizer (kernel-address) for RT-Thread. It instruments memory accesses via GCC's -fsanitize=kernel-address and detects heap-buffer-overflow and use-after-free at runtime, with a FinSH asan_info command for interactive diagnosis.

为 RT-Thread 新增仅针对堆的运行时 AddressSanitizer(kernel-address)。通过 GCC 的 -fsanitize=kernel-address 对内存访问插桩,在运行时检测 堆缓冲区越界(heap-buffer-overflow)释放后使用(use-after-free),并提供 FinSH asan_info 命令进行交互式诊断。

Why / 为什么需要

RT-Thread 在 MCU 目标上缺乏运行时内存安全检查(ASan 之前仅存在于 x86 模拟器)。堆越界与 use-after-free 是最常见的嵌入式内存 bug,本组件提供目标机上的运行时检测,且报告包含触发线程名与内存块归属信息。

What / 修改了哪些文件

  • components/asan/Kconfig:注册 RT_USING_ASAN(含 shadow 大小 / track 表大小 / backtrace 开关)
  • components/asan/SConscript:runtime 自身以 -fno-sanitize=kernel-address 编译避免递归
  • components/asan/asan.c:shadow memory + 插桩访问检查 + 报告 + malloc/free/realloc hook
  • components/Kconfigrsource "asan/Kconfig"
  • src/SConscript:将 mem/memheap/slab 分配器移入非插桩编译组
  • tools/building.py:GCC 下注入 -fsanitize=kernel-address

Heap algorithm support / 堆算法支持

堆类型 越界检测 use-after-free
small mem (RT_USING_SMALL_MEM_AS_HEAP)
slab (RT_USING_SLAB_AS_HEAP) ❌(元数据走插桩 rt_memset,避免误报)
memheap (RT_USING_MEMHEAP_AS_HEAP) ❌(同上)
userheap (RT_USING_USERHEAP) 互斥(Kconfig 已排除)

Verification / 验证

  • qemu-vexpress-a9:small mem / slab / memheap 三种堆编译 + 运行验证
  • 真机 STM32F407ZGT6:asan_test_overflow(overflow by 2 bytes)、asan_test_uaf(USE-AFTER-FREE, offset +0)、asan_info 全部命中
  • 编译器兼容性:GCC 8+,ARM(M0/M3/M4/M7/M33/A7/A9/A53)与 RISC-V(rv32/rv64)实测支持 kernel-address

Add a heap-only AddressSanitizer (kernel-address) runtime for RT-Thread.
It instruments memory accesses via GCC's -fsanitize=kernel-address and
detects heap-buffer-overflow and use-after-free at runtime, with a
FinSH 'asan_info' command for interactive diagnosis.

## What
- components/asan/: shadow memory + instrumented-access checks + report
- allocator hooks to poison/unpoison heap blocks (malloc/free/realloc)
- components/Kconfig: register RT_USING_ASAN with shadow/track/backtrace opts
- src/SConscript: build mem/memheap/slab allocators without instrumentation
- tools/building.py: inject -fsanitize=kernel-address on GCC

## Why
RT-Thread lacks runtime memory-safety checking on MCU targets (ASan only
existed on the x86 simulator). Heap overflow and use-after-free are the
most common embedded memory bugs; this gives on-target detection with
thread and block context in the report.

## Heap algorithm support
- small mem: full support (overflow + use-after-free)
- slab / memheap: overflow only (their allocators reuse freed blocks for
  metadata written via instrumented rt_memset, so free-block poisoning is
  disabled to avoid false positives)
- userheap: mutually exclusive (Kconfig)

Verified on qemu-vexpress-a9 (small mem / slab / memheap) and on a real
STM32F407ZGT6 board.
@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 分支 agent/rt-asan
    Enter PR branch agent/rt-asan 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.

完成后,提交将自动更新至 agent/rt-asan 分支,关联的 Pull Request 也会同步更新。
Once completed, commits will be pushed to the agent/rt-asan 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 Kernel PR has src relate code tools Component labels Aug 30, 2026
@github-actions

github-actions Bot commented Aug 30, 2026

Copy link
Copy Markdown

📌 Code Review Assignment

🏷️ Tag: components

Reviewers: @Maihuanyi

Changed Files (Click to expand)
  • components/utilities/Kconfig
  • components/utilities/asan/Kconfig
  • components/utilities/asan/SConscript
  • components/utilities/asan/asan.c

🏷️ Tag: kernel

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

Changed Files (Click to expand)
  • src/SConscript

📊 Current Review Status (Last Updated: 2026-08-31 10:34 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

github-actions Bot commented Aug 30, 2026

Copy link
Copy Markdown

@BernardXiong

Copy link
Copy Markdown
Member

这部分以什么方式加入?直接加也并不合适,不应该把它做为一个独立的组件放到 components 目录下,可以考虑的方式是 components/utilities 下,或者还有一种是软件包方式。

@Zepp-Hanzj

Copy link
Copy Markdown
Contributor Author

这部分以什么方式加入?直接加也并不合适,不应该把它做为一个独立的组件放到 components 目录下,可以考虑的方式是 components/utilities 下,或者还有一种是软件包方式。

感谢反馈,我理解后觉得放 components/utilities/ 下是合适的,会把它调整为
components/utilities/asan/

关于「软件包方式」,有一点技术约束想说明:ASan 依赖 GCC 的
-fsanitize=kernel-address 插桩,这个编译选项需要注入到整个内核与
BSP 所有 .c 文件的编译流程(tools/building.py),并且需要修改内核
src/SConscript 把 mem/memheap/slab 分配器移出插桩组(否则分配器
元数据访问会被误报)。软件包机制无法修改内核的全局编译选项和构建脚本,
因此纯软件包方式只能做「不插桩、仅靠 hook」的弱化版本,无法实现真正的
AddressSanitizer。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Component Kernel PR has src relate code tools

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants