Pull Request has been successfully merged by CANN-robot.
(Thank you for ren-botao's contribution.)变更摘要
此 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、大页内存、同线程活跃块和非活跃态等场景进行了短路处理,避免误报。 -
所有
aclDestroyXxxAPI 增加预检调用:在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)块释放后漏报(符合设计)及活跃块不误报等场景。


代码审查
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 |
💬 仅评论


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


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
/approveor/lgtm- Commenting
/approveimplies 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. 👍


🟡 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。


【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 |


流水线任务触发成功
任务链接 [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 | >>>>> | >>>>> |




流水线任务触发成功
任务链接 [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执行结束




/lgtm
/approve


描述
为 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 测试用例
文档更新
不涉及
类型标签