Merged
为 aclDestroyXxx 系列 API 增加 double-free 检测能力 #610
为 aclDestroyXxx 系列 API 增加 double-free 检测能力 #610
Merged
ren-botaoAdd on Jul 6
ren-botaoCollaborators
Jul 6

描述

为 aclDestroyXxx 系列 API 增加 best-effort double-free 检测能力

关联的Issue

#280 为 aclDestroyXxx 系列 API 增加 best-effort double-free 检测能力

测试

新增 tests/nnopbase/ut/composite_op/test_check_double_free.cpp UT测试文件,增加 10 个 TEST_F 测试用例

文档更新

不涉及

类型标签

likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for ren-botao's contribution.)
atomgit-bot
atomgit-bot
Jul 6 Comments:

变更摘要

此 PR 主要为所有 aclDestroyXxx 系列 API 引入了 double free 检测机制。在 debug 模式下(通过日志级别判断),CheckDoubleFree 函数会在释放对象前检查其对应的内存块是否已归还到缓存链表,若检测到重复释放则输出警告日志。同时,为配合检测逻辑,在 block_pool.h 中增加了 placement new 初始化 BlockHeader、新增 CurrentThreadCacheAddr 静态方法,并在 op_executor.cpp 的算子初始化阶段调用 InitAclnnDebugSwitch 以启用该功能。

主要改动

  • 新增 CheckDoubleFree 函数:在 bridge_pool.cpp 中实现,通过检查 BlockHeader 的 magic_ 魔数和 cacheExt_ 字段判断内存块是否已归还到缓存链表;对 nullptr、大页内存、同线程活跃块和非活跃态等场景进行了短路处理,避免误报。

  • 所有 aclDestroyXxx API 增加预检调用:在 acl_op_api.cpp 的 aclDestroyTensor、aclDestroyScalar、aclDestroyIntArray、aclDestroyFloatArray、aclDestroyBoolArray、aclDestroyTensorList 和 aclDestroyScalarList 中,delete 前均增加 debug 模式下的 CheckDoubleFree 调用,检测到疑似 double free 时输出 OP_LOGW 警告。

  • block_pool.h 改进内存块初始化与辅助接口:在 MallocImpl、PoolAlloc、CacheAllocImpl 等多个分配路径中对 std::malloc 返回的原始内存使用 placement new 初始化 BlockHeader,确保 magic_ 等 NSDMI 字段正确初始化;新增 CurrentThreadCacheAddr 静态方法,返回当前线程 BlockCache 实例地址,供 CheckDoubleFree 判定活跃块。

  • Debug 开关初始化:在 op_dfx.cpp 中新增 g_aclnnDebugEnabled 全局变量和 InitAclnnDebugSwitch 函数,通过检查日志级别决定是否开启 aclnn debug 模式;在 op_dfx_internal.h 中暴露 IsAclnnDebugEnabled 内联函数;在 op_executor.cpp 的 InitL2Phase1Context 中调用 InitAclnnDebugSwitch 完成初始化。

  • 新增单元测试覆盖:新增 test_check_double_free.cpp,覆盖 nullptr、野指针、同线程/跨线程 double free 检测、跨线程访问不误报、大页内存短路、冷路径(>64KB)块释放后漏报(符合设计)及活跃块不误报等场景。

likedislike
Incorrect?
atomgit-bot
atomgit-bot
Jul 6 Comments:

代码审查

Closing Summary

审查结论

本 PR 为 aclDestroyXxx 系列 API 增加了 double-free 检测能力,核心逻辑正确,实现质量较高。共发现 3 个实质性问题(不含我误报的一条 info),按优先级分布:

优先级 数量 说明
P0 0 —
P1 0 —
P2 1 g_aclnnDebugEnabled 数据竞争
P3 2 测试越界读取、冷路径 use-after-free

整体风险判断:低风险。P2 数据竞争影响面有限(最坏漏报一次 double-free,不会崩溃),P3 项分别为测试质量和设计内 known-limitation。核心检测逻辑(CheckDoubleFree 对 warm-path 已归还 cache 链表的 block 的判别)经逐路径验证正确,placement new 修复填补了 std::malloc 不初始化 NSDMI 的空白。

逐文件审查确认

文件 审查结果
src/nnopbase/common/api/acl_op_api.cpp ✅ 无问题(模式一致,const_cast 安全,list 嵌套析构正确)
src/nnopbase/common/inc/block_pool.h ✅ 无问题(placement new 修复必要且正确,CurrentThreadCacheAddr 实现正确)
src/nnopbase/common/inc/bridge_pool.h ✅ 无问题(声明与实现一致)
src/nnopbase/common/inc/op_dfx_internal.h ⚠️ 1 项 P2(g_aclnnDebugEnabled 数据竞争)
src/nnopbase/common/utils/bridge_pool.cpp ⚠️ 1 项 P3(冷路径 use-after-free)
src/nnopbase/composite_op/aclnn_engine/op_dfx.cpp ✅ 无问题(InitAclnnDebugSwitch 实现正确,<mutex> 已含)
src/nnopbase/composite_op/aclnn_engine/op_executor.cpp ✅ 无问题(调用点正确,InitL2Phase1Context 是合理的惰性初始化位置)
tests/nnopbase/ut/composite_op/test_check_double_free.cpp ⚠️ 1 项 P3(WildPointer_MagicMismatch 越界读取)
类型 数量
🔴 阻塞 0
🟡 建议 1

💬 仅评论

likedislike
Incorrect?
CANN-robot
CANN-robotCollaboratorsJul 6Start code review1
src/nnopbase/common/utils/bridge_pool.cpp
Outdated
@@ -37,0 +75,2 @@
75+ // (无 +1/-1 算术),故此处读 magic_ 也不做指针算术,保持与写入端对称
76+ BlockStore::BlockHeader* nextHead = reinterpret_cast<BlockStore::BlockHeader*>(ext); // NOLINT
77+ if (nextHead->magic_ == BlockStore::MAGIC) {
CANN-robot
CANN-robotJul 6Comments:

This code comment section is from+75To+77

【openlibing.ci】识别到代码检查告警抑制注释,匹配工具:clang-tidy,请Committer检视其合理性。

likedislike
CANN-robotCANN-robotCollaborators
Jul 6 Added label: stat/needs-squash
CANN-robotCANN-robotCollaborators
Jul 6 Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
Jul 6 Comments:

Thanks for your pull-request.
The full list of commands accepted by me can be found at here。
You can get sig-info at here


PR Approval Progress

✅ Congratulations! All modules have met the lgtm and approve requirements.

Module Approval Details

module lgtm status approve status
repo-cann/opbase ✅ 宋恺, 张磊 (2/2) ✅ 张磊, 宋恺 (2/1)

💡 Tip:

  • Committer can comment /approve or /lgtm
  • Commenting /approve implies both code review (lgtm) and intent to merge (approve)

CLA Signature Pass

ren-botao, thanks for your pull request. All authors of the commits have signed the CLA. 👍

likedislike
CANN-robotCANN-robotCollaborators
Jul 6 Set zhou-qilong,wangrui_,rxtfeng,loov1,jia0liang,songkai111,llimwang,gubaocheng as approver
CANN-robotCANN-robotCollaborators
Jul 6 Set zhou-qilong,wangrui_,rxtfeng,loov1,jia0liang,songkai111,llimwang,gubaocheng as approver
atomgit-bot
atomgit-botJul 6Start code review1
src/nnopbase/common/inc/op_dfx_internal.h
Outdated
@@ -170,0 +167,4 @@
167+extern bool g_aclnnDebugEnabled;
168+ 
169+void InitAclnnDebugSwitch();
170+inline bool IsAclnnDebugEnabled() { return g_aclnnDebugEnabled; }
atomgit-bot
atomgit-botJul 6Comments:

🟡 Medium Priority

变更在 op_dfx_internal.h:170 添加了 inline bool IsAclnnDebugEnabled() { return g_aclnnDebugEnabled; },在 op_dfx.cpp:127-138 通过 std::call_once 写入 g_aclnnDebugEnabled = true。IsAclnnDebugEnabled() 的读端(acl_op_api.cpp 各 aclDestroyXxx 函数)未经过任何同步机制直接读取该 bool。

按照 C++ 内存模型,std::call_once 的同步关系仅对同一次 call_once 的其他调用者成立;IsAclnnDebugEnabled() 不调用 call_once,因此对 g_aclnnDebugEnabled 的读写构成 data race(UB)。实践影响有限:值仅从 false → true 单向变化,且 bool 在常见平台上为字节原子操作,最坏表现为短暂漏报一次 double-free,不会崩溃。但仍需修复以消除 UB。

likedislike
Incorrect?
Rren-botaoCollaborators
Jul 9 Close pull request
Rren-botaoCollaborators
Jul 20 Reopen pull request
Rren-botaoCollaborators
Jul 20 Force-pushed 47 commits:60e46562-45 commits from branch master,f0cbaa4e-提供aclnn框架维测功能开关获取接口,eb701037-check double free in aclDestroyXxx APIs
CANN-robot
CANN-robotCollaboratorsJul 20Start code review1
src/nnopbase/common/api/acl_op_api.cpp
CANN-robot
CANN-robotJul 20Comments:

【openlibing.ci】检测到当前PR中存在代码检查告警抑制 1 处,详情见下表,请Committer检视合理性。 / Detected 1 code check alert suppression(s) in this PR, see table below. Committers please review.

文件路径/File 行号/Line 代码片段/Snippet 工具/Tool
src/nnopbase/common/utils/
bridge_pool.cpp
84 BlockStore::BlockHeader* nextHead = reinterpret_cast<BlockStore::BlockHeader*>(ext); // NOLINT clang-tidy
likedislike
CANN-robotCANN-robotCollaborators
Jul 20 Set zl_hw as approver
CANN-robotCANN-robotCollaborators
Jul 20 Set zl_hw as approver
Rren-botaoCollaborators
Jul 20 Pushed 1 commits:5c1bae7c-维测开关全局变量改为atomic
Rren-botaoCollaborators
Jul 21 Pushed 1 commits:9437a9c8-[double-free check]代码位置调整
Rren-botaoCollaborators
Jul 21 Resolved final issue
ren-botaoCollaborators
Jul 21 Comments:

compile

likedislike
CANN-robotCANN-robotCollaborators
Jul 21 Added label: ci-pipeline-running
CANN-robot
CANN-robotCollaborators
Jul 21 Comments:

流水线任务触发成功
任务链接 [6d57f1fdc3424b9786593ee6e2c9a74f][流水线指导]

任务名称状态日志下载链接
SCA ✅ SUCCESS >>>>>
antipoison ✅ SUCCESS >>>>>
Check_Pr ✅ SUCCESS >>>>>
Compile_Ascend_X86_ubuntu24 ✅ SUCCESS >>>>> >>>>>
UT_Test ✅ SUCCESS
Compile_Ascend_ARM_ubuntu24 ✅ SUCCESS >>>>> >>>>>
pre_comment ✅ SUCCESS >>>>>
StaticCheck_codespell ✅ SUCCESS
StaticCheck_link_validity ✅ SUCCESS
StaticCheck_resource_existence ✅ SUCCESS
StaticCheck_tag_closed ✅ SUCCESS
StaticCheck_markdownlint ✅ SUCCESS
codecheck_style ✅ SUCCESS >>>>>
precommit ❌ FAILED >>>>> >>>>>
likedislike
CANN-robotCANN-robotCollaborators
Jul 21 Removed label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
Jul 21 Added label: ci-pipeline-failed
CANN-robot
CANN-robotCollaborators
Jul 21 Comments:
流水线 PR-pipeline_cann_opbase#1524 [ commitID:9437a9c8 ] 运行失败
likedislike
Rren-botaoCollaborators
Jul 21 Force-pushed 1 commits:955b9663-添加内存重复释放的检测功能
CANN-robotCANN-robotCollaborators
Jul 21 Removed label: stat/needs-squash
CANN-robot
CANN-robotCollaborators
Jul 21 Comments:

Notification

This pull request has been changed(code update) or closed, so removes the following label(s): stat/needs-squash.

likedislike
Rren-botaoCollaborators
Jul 21 Modify description of pull request
ren-botaoCollaborators
Jul 21 Comments:

compile

likedislike
CANN-robotCANN-robotCollaborators
Jul 21 Removed label: ci-pipeline-failed
CANN-robotCANN-robotCollaborators
Jul 21 Added label: ci-pipeline-running
CANN-robot
CANN-robotCollaborators
Jul 21 Comments:

流水线任务触发成功
任务链接 [da50ecff61224042a4ba85acc9f932db][流水线指导]

任务名称状态日志下载链接
SCA ✅ SUCCESS >>>>>
antipoison ✅ SUCCESS >>>>>
Check_Pr ✅ SUCCESS >>>>>
Compile_Ascend_X86_ubuntu24 ✅ SUCCESS >>>>> >>>>>
UT_Test ✅ SUCCESS
Compile_Ascend_ARM_ubuntu24 ✅ SUCCESS >>>>> >>>>>
pre_comment ✅ SUCCESS >>>>>
StaticCheck_codespell ✅ SUCCESS
StaticCheck_link_validity ✅ SUCCESS
StaticCheck_resource_existence ✅ SUCCESS
StaticCheck_tag_closed ✅ SUCCESS
StaticCheck_markdownlint ✅ SUCCESS
codecheck_style ✅ SUCCESS >>>>>
precommit ✅ SUCCESS >>>>>
API_Check ✅ SUCCESS >>>>>

[2026-07-21 10:27:06]    CI执行结束

likedislike
CANN-robotCANN-robotCollaborators
Jul 21 Added label: api-check-pass
CANN-robotCANN-robotCollaborators
Jul 21 Removed label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
Jul 21 Added label: ci-pipeline-passed
CANN-robot
CANN-robotCollaborators
Jul 21 Comments:
流水线 PR-pipeline_cann_opbase#1528 [ commitID:955b9663 ] 已完成
likedislike
Rren-botaoCollaborators
Jul 21 Change title to "为 aclDestroyXxx 系列 API 增加 double-free 检测能力", originally titled "check double free in aclDestroyXxx APIs".
songkai111Collaborators
Jul 21 Comments:

/lgtm
/approve

likedislike
CANN-robotCANN-robotCollaborators
Jul 21 Added label: approved
zl_hwCollaborators
Jul 21 Comments:

/lgtm
/approve

likedislike
CANN-robotCANN-robotCollaborators
Jul 21 Added label: lgtm
CANN-robotCANN-robotCollaborators
Jul 21 Closed associated issue
CANN-robotCANN-robotCollaborators
Jul 21 Merged pull request