Merged
test(sanitizer): Add memory check tool #5668
test(sanitizer): Add memory check tool #5668
Merged
zhengyaoff12Add on Aug 25
zhengyaoff12
zhengyaoff12Collaborators
Aug 25
Pull Request has been successfully merged by CANN-robot.
(Thank you for zhengyaoff12's contribution.)
zhengyaoff12zhengyaoff12Collaborators
Aug 25 Created pull request, commit 5dea5911
atomgit-bot
atomgit-bot
Aug 25 Comments:

变更摘要

本 PR 新增一套自研(self-hosted)日志回放式 sanitizer 的系统测试(ST)套件:在 IR 层通过新增的 SanitizerPass 向 kernel 的每次访问/同步操作前插入 block.sanitizer_log 日志记录,并给 kernel 追加隐藏的 sanitizer_log(GM 日志缓冲区指针)与 sanitizer_log_slots(每 block 记录槽数)参数;设备端 codegen 将每条记录写成固定 32 字节的槽位记录;host 侧新增 sanitizer_replay.py 回放工具解码日志缓冲区并执行五类检测(GM 越界、片上 tile 重叠、多核竞争、未初始化读、同步配对),检测到问题时抛出聚合的 SanitizerReplayError。同时为 jit 增加 sanitizer 开关并贯通 codegen 与启动器,新增 Python 绑定和 ST 用例,默认路径(sanitizer=False)行为保持不变。

主要改动

  • 新增 SanitizerPass IR 插桩 pass:新增 framework/include/ir/transforms/sanitizer_pass.h 与 framework/src/interface/ir/transforms/sanitizer_pass.cpp,定义 SanitizerLogKind 记录类型枚举(header/GM 读写/tile 读写/同步 set/wait/barrier),通过 LogInjector 在访问与同步操作前插入日志语句,维护 span 表、tile 表并追加隐藏日志参数。
  • 注册 block.sanitizer_log 设备端 codegen:在 framework/src/interface/pypto_pro/backend/backend_cce_ops.cpp 注册后端算子,生成按 get_block_idx() 与槽号寻址的 GM 缓冲区写入代码(每条记录 8 个 u32,共 32 字节)。
  • jit 贯通 sanitizer 开关与启动器自动回放:在 python/pypto_pro/runtime/jit.py 中新增 sanitizer 参数并逐层传入 _parse_and_codegen_targets/_codegen/_TileJitKernel;instrument 后把 spans、tile_table、slots、original_param_count 写入构建目录的 sanitizer_meta.json;启动器检测到尾部 sanitizer_log/sanitizer_log_slots 参数时自动分配 slots*block_dim*32 字节的 NPU 日志缓冲区、追加隐藏参数,并在同步后调用 _replay_sanitizer 执行回放。
  • 新增 host 回放工具:新增 python/pypto_pro/runtime/sanitizer_replay.py,提供 decode_buffer、run_replay、ReplayContext 及 SanitizerReplayError,实现五类检测(GM_OUT_OF_BOUNDS、TILE_OVERLAP、RACE_WRITE_WRITE/RACE_WRITE_READ、UNINITIALIZED_READ、UNPAIRED_SYNC_DST/SYNC_WAIT_BEFORE_SET)。
  • 新增 Python 绑定与 ST 用例:在 python/src/bindings/ir/pass.cpp 暴露 SanitizerPass 的 instrument/spans/tile_table/original_param_count/slots 接口;新增 python/tests/st/pypto_pro/frontend/sanitizer/test_sanitizer_selflog.py,覆盖五类检测的正/负样本(正样本不误报、负样本被检出、默认路径不受影响),仅限 Ascend950(@pytest.mark.soc("950"))执行。
likedislike
Incorrect?
atomgit-bot
atomgit-bot
Aug 25 Comments:

代码审查

审查结论汇总

各变更文件核查结果

1. framework/include/ir/transforms/sanitizer_pass.h(新增,+106 行)

  • 已逐行核查:SanitizerLogKind 枚举、SanitizerPass 接口、成员与注释。注释中声明了"slot 0 = header / 头部记录只发一次"的布局契约,与实现不符(见下),本文件本身未发现其他独立缺陷。
  • 结论:问题在实现侧,见 sanitizer_pass.cpp 的发现。

2. framework/src/interface/ir/transforms/sanitizer_pass.cpp(新增,+421 行)

  • 已核查全部插桩路径:LogInjector、TileAddrCollector、SpanId、TileSlot、LogStmt、InstrumentAccess(block.load/store/store_fp/move/move_fp/insert/通用计算路径)、InstrumentSync、Instrument。
  • 已核实 block.load=(out,tile,tensor,offsets)、block.store=(tensor,tile,offsets)、block.store_fp=(tensor,tile,fp_tile,offsets)、block.insert=(out,src,row,col)、block.make_tile 的 memref_addr 为 set_attr<int>(int32)——参数索引均与后端签名一致。
  • 发现:P2 × 2(头部记录 slot 顺序/缺失;tensor_param_ids_ operator[] 静默置 0),P3 × 2(original_param_count_ 末函数覆盖;缺单测)。

3. framework/src/interface/pypto_pro/backend/backend_cce_ops.cpp(+50 行)

  • 已核查 MakeSanitizerLogCodegenCCE:10 参数 CHECK 与 LogStmt 参数序一致、逐块寻址 block_idx*slots*8 + slot*8(u32 单位)与 slots*block_dim*32 布局自洽、__gm__ 指针运算无溢出(uint64 强转)。
  • 发现:P2 × 1(裸 get_block_idx() 未按 Vector 段语义取全局块号,与本文件 956-967 行既有 get_block_idx 后端 op 的契约不一致)。

发现统计

  • P0:0 | P1:0 | P2:3 | P3:2

总体风险判断

本 PR 是自托管 sanitizer 日志回放测试链路的基础设施:新增插桩 pass + 设备端日志 codegen。整体为新增代码、未接入现有管线(全库无 SanitizerPass 调用方),无破坏现有功能的回归风险;但插桩侧存在明确的契约违背——头部记录在常见情况下(首个函数含记录)根本不会发出,slot 0 被第一条真实记录占用,且 Vector 段块号寻址与既有契约不一致,这两处会直接导致 host 回放工具按文档布局解析日志时错位/归因错误,属于系统性数据质量问题,建议合入前修复并补足单测。


变更审查总结

各文件审查确认

1. python/pypto_pro/runtime/sanitizer_replay.py(新增,288 行) — 已审查。核心回放逻辑存在多处逻辑缺陷:

  • P2:竞争检测(第3项)从不读取 sync/barrier 记录却宣称"无 barrier 在中间",正确同步的多 block kernel 会被误报(与测试"正样本不误报"承诺冲突);
  • P2:未初始化读检查使用跨 block 全局 defined 集合,片上 tile 为每 block 私有,block0 的写会掩盖 block1 的未初始化读(漏报);
  • P3:GM 越界检查只验上界不验负偏移(漏检);
  • P3:模块文档宣称的 ON_CHIP_OVERFLOW 容量检测未实现(文档/行为不符)。

2. python/pypto_pro/runtime/jit.py(+89 -1) — 已审查。sanitizer 参数贯穿 jit → _TileJitKernel → _codegen → _parse_and_codegen_targets → _load_lib 的链路完整,正常路径下追加 (log_buffer, slots) 与 instrumented param_specs 对齐无误:

  • P2:元数据缺失/读取失败仅 warning,随后 _validate_args 必然 TypeError 崩溃(静默降级转硬崩溃);
  • P3:_replay_sanitizer 宽泛 except Exception 吞掉所有回放异常,回放缺陷(含标量参数导致的 tensor_args 序号错位)会静默产生"干净"假结果。

3. python/src/bindings/ir/pass.cpp(+16) — 已审查。SanitizerPass 绑定与 Python 侧 from pypto.pypto_impl.ir import SanitizerPass 一致,.def 的方法名/参数与调用点匹配;#include "ir/transforms/sanitizer_pass.h" 及 SanitizerPass 类定义位于审查范围外的 C++ 源码树,无法核验其存在性——若该类不在库中会导致构建失败(P0 风险,但无法从可见代码证实,故未上报为 finding,建议合入前确认该头文件/类已存在)。no issues(除上述无法核实的构建风险)。

4. python/tests/st/pypto_pro/frontend/sanitizer/test_sanitizer_selflog.py(新增,303 行) — 已审查。测试用例与五类检测的对应关系正确,正/负样本断言与 SanitizerReplayError 同步抛出路径匹配;但正样本全部为单 block,无法覆盖上述竞争误报与未初始化读漏报两类缺陷(这正是前面两个 P2 finding 未被测试暴露的原因)。no issues(无独立于已报 finding 的问题)。

汇总

  • 发现数:P2 × 3、P3 × 3,共 6 项(另含 1 项无法核实的 C++ 构建风险提示)。
  • 总体风险判断:本 PR 的 sanitizer 功能链路(instrument → 元数据落盘 → launch 追加参数 → host 回放)主路径自洽,测试覆盖了各检出类别的单 block 基本路径;但回放侧存在与文档承诺不符的近似逻辑(竞争检测不查同步、未初始化读跨 block 掩盖、负偏移漏检),会导致误报/漏报,且元数据异常路径会以误导性 TypeError 崩溃、宽泛 except 会静默吞掉回放错误。建议在合入前修复竞争检测与未初始化读的跨 block 语义、元数据异常处理,并为"多 block 正确同步"补正样本用例。
类型 数量
🔴 阻塞 0
🟡 建议 3

💬 仅评论

likedislike
Incorrect?
CANN-robotCANN-robotCollaborators
Aug 25 Added label: stat/needs-squash
CANN-robotCANN-robotCollaborators
Aug 25 Added label: cann-cla/yes
290 messages are collapsed here。 See more
CANN-robotCANN-robotCollaborators
Sep 12 Added label: lgtmapproved
CANN-robot
CANN-robotCollaborators
Sep 12 Comments:

The MR can not be merged, because of CodeReview discussion not resolved

If you want to solve this problem, you can click here to do it in the FAQs.

likedislike
gaoxiang618
gaoxiang618CollaboratorsSep 12Start code review1
framework/include/ir/transforms/passes.h
@@ -258,1 +258,4 @@
258258Pass FinalizeDynamicFunction();
259+ 
260+/**
261+ * \brief Create a sanitizer instrumentation pass
gaoxiang618
gaoxiang618Sep 12Comments:

补充资料

likedislike
poorwillpoorwillCollaborators
Sep 12 Resolved final issue
CANN-robotCANN-robotCollaborators
Sep 12 Merged pull request