Merged
fix: validate negative indices in gather_v2 aicpu #14527
fix: validate negative indices in gather_v2 aicpu #14527
Merged
ayuemoonAdd on 5 days ago
ayuemoon
ayuemoon
5 days ago

描述

修复 GatherV2 AICPU kernel 对负索引只检查上界的问题。

在归一化索引前,先对原始索引执行 [-gather_dim_size, gather_dim_size) 双界检查,再对合法负索引加上 gather_dim_size。这样可以:

  1. 拦截 -gather_dim_size 以下的非法负索引,避免越界读。
  2. 保持 -gather_dim_size 和 -1 的合法负索引语义。
  3. 在地址计算前拦截 INT64_MIN 等极端非法负索引,避免非法源地址及后续偏移计算风险。

修改同时覆盖 HOSTCPU 常量折叠和设备 AICPU 两条编译路径使用的同一份 kernel 源码。

关联的Issue

关联 Issue #8948:https://gitcode.com/cann/ops-nn/issues/8948

测试

  • GatherV2 共 9 个 AICPU kernel UT 全部通过。
  • 覆盖负索引越界、合法边界及 double/complex 类型。
  • ASan 运行无内存越界告警。
  • 验证范围为主机 UT,未单独验证设备侧执行和常量折叠。

文档更新

无

类型标签

AI/Agent生成声明

likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for ayuemoon's contribution.)
ayuemoonayuemoon
5 days ago Created pull request, commit d3feb8d2
atomgit-bot
atomgit-bot
5 days ago Comments:

变更摘要

本 PR 修复 GatherV2 AICPU kernel 对负索引仅做上界检查的问题。在 gather_v2_aicpu.cpp 的 CopyGatherV2Data 中,先对原始索引值执行 [-gather_dim_size, gather_dim_size) 的双界校验,校验通过后再将合法的负索引加上 gather_dim_size 完成归一化,从而拦截过小的非法负索引并避免 INT64_MIN 在归一化加法中发生有符号整数下溢。配套的 AICPU kernel UT 增加了返回值与期望状态的支持,并补充了合法负索引边界值与非法负索引的测试用例。

主要改动

  • 索引校验逻辑前移并改为双界判断: CopyGatherV2Data 引入 raw_index 保存原始索引,使用 KERNEL_CHECK_FALSE(raw_index >= -info.gather_dim_size && raw_index < info.gather_dim_size, ...) 在归一化前完成双侧范围校验,非法索引直接返回 KERNEL_STATUS_PARAM_INVALID。
  • 负索引归一化在校验之后进行: 将 indices_value 的计算改为在校验通过后执行 raw_index < 0 ? raw_index + info.gather_dim_size : raw_index,保留 -gather_dim_size 到 -1 的合法负索引语义。
  • 错误提示范围更新: 越界报错信息由原先的 [0, %ld) 调整为 [-%ld, %ld),与新的合法区间一致。
  • 测试辅助函数支持期望状态: RunGatherV2Kernel 返回值由 void 改为 uint32_t,新增 expect_status 参数(默认 KERNEL_STATUS_OK),并仅在返回 KERNEL_STATUS_OK 时执行结果比较。
  • 新增负索引边界与越界用例: 覆盖 gather_dim_size=10 下 -10、-1、9 的合法边界,以及 double、complex64、complex128 下 -11、-100、numeric_limits<int64_t>::min() 的非法负索引场景。

🧩 本次 Review 已使用自定义 Skill:

likedislike
Incorrect?
atomgit-bot
atomgit-bot
5 days ago Comments:

代码审查

审查结论

逐文件复核

  • index/gather_v2/op_kernel_aicpu/gather_v2_aicpu.cpp:no issues。逐条核对:raw_index 先做 [-gather_dim_size, gather_dim_size) 双界检查再归一化,-info.gather_dim_size 只对 gather_dim_size(形状维度,非负)取负,raw_index 为 INT64_MIN 时仅参与比较、不参与加法,因此不再有符号下溢与归一化后负偏移越界读;gather_dim_size == 0 时 params->NumElements()==0 已在 InitGatherV2Info/DoGatherV2Compute 提前返回,循环不可达,不会误报;-gather_dim_size 与 -1 的合法负索引语义保持;日志格式串与实参个数一致;与仓库内同源实现 index/scatter_elements/op_kernel_aicpu/scatter_elements_aicpu.cpp:158 的 NormalizeIndicesValue 双界检查写法一致,未产生跨文件契约分歧。
  • index/gather_v2/tests/ut/op_kernel_aicpu/test_gather_v2.cpp:1 处问题(见下)。新增用例本身有效:-11/-100/INT64_MIN 在旧实现下会归一化为负偏移并返回 OK,可真正捕获回归;{-10, -1, 9} 的期望输出 {x[0], x[9], x[9]} 与 kernel 的 params_idx 计算一致;<limits> 已补充,numeric_limits<int64_t>::min() 无窄化问题。

发现清单(按优先级)

  • P2 × 1,P0/P1/P3 × 0

总体风险判断

核心缺陷修复方向与边界语义正确,未发现残留越界读或整数溢出路径,回归用例具备区分能力;主要残余风险集中在测试侧对框架宏(utils/aicpu_test_utils.h 的 RUN_KERNEL)内部变量 ret 的未验证依赖——该头文件不在本仓库内,且提交者未实际编译,若宏将 ret 限定在块作用域则会构建失败。整体风险:低(除上述构建侧不确定性外),建议合入前实际跑一次 --opkernel_aicpu_test --ops=gather_v2 编译确认。

类型 数量
🔴 阻塞 0
🟡 建议 1

💬 仅评论


🧩 本次 Review 已使用自定义 Skill:

likedislike
Incorrect?
CANN-robotCANN-robotCollaborators
5 days ago Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
5 days ago 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.
You can self-configure the PR merge rules for this repository. For more details, please refer to Here.
For more, you also can visit HICANN.


PR Approval Progress

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

Module Approval Details

module lgtm status approve status
index ✅ 汤平川, 陈展熹 (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

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

likedislike
CANN-robotCANN-robotCollaborators
5 days ago Set zengjuan,u010470851,chenfeng61,chaotang233,kevin_huang1234,crystalhu,yangyang016,fanqirui,wangrui_,su-yueming,yourealize,chenqi317,wang-xing001,liubo75,tangweiwei2,chenxingyu18,pingchuantang,qianzehong as approver
CANN-robotCANN-robotCollaborators
5 days ago Set u010470851,chenfeng61,kevin_huang1234,wangrui_,su-yueming,yourealize,wang-xing001,chenxingyu18,pingchuantang,qianzehong as approver
ayuemoonayuemoon
5 days ago Modify description of pull request
ayuemoonayuemoon
5 days ago Modify description of pull request
atomgit-bot
atomgit-bot5 days agoStart code review1
index/gather_v2/tests/ut/op_kernel_aicpu/test_gather_v2.cpp
@@ -78,0 +76,4 @@
76+ if (ret == KERNEL_STATUS_OK) {
77+ EXPECT_TRUE(CompareResult(output_data.get(), expect_output, output_size));
78+ }
79+ return ret;
atomgit-bot
atomgit-bot5 days agoComments:

🟡 Medium Priority

变更点:test_gather_v2.cpp 第 75-79 行改为 RUN_KERNEL(node_def, HOST, expect_status); 后直接使用 ret(if (ret == KERNEL_STATUS_OK) 与 return ret;),但本文件从未声明 ret,它只能来自 utils/aicpu_test_utils.h 中 RUN_KERNEL 宏的内部实现。

证据链与影响:

  1. RUN_KERNEL、CompareResult、CpuKernelUtils::RunKernel 均不在本仓库内(在 index 目录下搜索 #define RUN_KERNEL、bool CompareResult 均无结果,该头文件来自已安装的 CANN aicpu 测试框架)。
  2. 因此“宏是否把状态变量暴露到调用者作用域、变量名是否恰为 ret”无法从仓库代码确认;若该宏以 do { ... } while (0) 或裸 { } 块包裹并在内部声明 uint32_t ret,则第 76 行与第 79 行会因 ret 未声明而编译失败;若宏内部变量名为 ret 且未加块作用域,才可编译。
  3. 该 PR 描述中明确说明作者未能实际编译(验证环境缺 Python/GCC/CMake,构建停在依赖检查阶段),pre-commit 的 clang-format/codespell 等检查也不会发现此类问题,因此“测试文件编译不过”这一失败模式目前无任何验证覆盖。

失败模式:bash build.sh --opkernel_aicpu_test --ops=gather_v2 编译 test_gather_v2.cpp 时报 'ret' was not declared in this scope,导致整个 gather_v2 AICPU UT 目标构建失败(属构建级阻断)。触发条件就是该 UT 目标被编译,无额外前置条件。

likedislike
Incorrect?
ayuemoonayuemoon
5 days ago Modify description of pull request
ayuemoon
ayuemoon
5 days ago Comments:

compile

likedislike
ayuemoonayuemoon
5 days ago Pre merge succeeded (commit_id: a016598b7d79b339a50e3d8bbef65d43997181dc)
ayuemoon
ayuemoon
5 days ago Comments:

compile

likedislike
CANN-robot
CANN-robotCollaborators
5 days ago Comments:
🚀 CI 流水线已启动
📋 执行详情: 点击查看流水线
likedislike
CANN-robotCANN-robotCollaborators
5 days ago Added label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
5 days ago Removed label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
5 days ago Added label: ci-pipeline-passed
yuning_chen
yuning_chenCollaborators
5 days ago Comments:

您好,pr合入已准备就绪,请尽快联系committer进行检视,谢谢!
@wangrui_ @chenxingyu18 @pingchuantang @qianzehong @wang-xing001 @yourealize @chenfeng61 @u010470851

likedislike
chenqi317
chenqi317Collaborators
5 days ago Comments:

请补充关联issue 说明合入背景

likedislike
ayuemoonayuemoon
4 days ago Modify description of pull request
liu-weiliu-weiCollaborators
4 days ago Resolved final issue
liu-wei
liu-weiCollaborators
4 days ago Comments:

/lgtm
/approve

likedislike
TangPC
TangPCCollaborators
4 days ago Comments:

/lgtm
/approve

likedislike
陈展熹
陈展熹Collaborators
4 days ago Comments:

/approve

likedislike
CANN-robotCANN-robotCollaborators
4 days ago Added label: approved
CANN-robotCANN-robotCollaborators
4 days ago Added label: lgtm
CANN-robot
CANN-robotCollaborators
4 days ago Comments:

The following users do not have permission to comment /lgtm or /approve on any module in this PR:
/approve: 刘伟
/lgtm: 刘伟

likedislike
CANN-robotCANN-robotCollaborators
4 days ago Closed associated issue
CANN-robotCANN-robotCollaborators
4 days ago Merged pull request
CANN-robot
CANN-robotCollaborators
4 days ago Comments:

Pull Request 已合并或已关闭。

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

likedislike