Closed
[Bug-Report|缺陷反馈]: aclnnMaxPool2dWithIndicesBackward 未校验 indices 取值越界,A2 Scatter 路径越界写显存(msSanitizer: illegal write) #7068
liuhanwen8Add on  Oct 21Closed at  3 days ago
liuhanwen8
liuhanwen8
Oct 21 Create

问题描述

aclnnMaxPool2dWithIndicesBackward 的资料 §约束说明 明确要求 0 <= indices < H_in*W_in,但 aclnn 入口与内核都没有对这个取值范围做任何校验,A2 上按 kernel 大小分成两种后果:

路径 触发条件 越界 indices 的后果
Normal k ≤ 15×15(UB 放得下) indices 只用于 CMPMODE::EQ 相等比较,越界值被静默丢弃:两段都返回 0,gradInput 全 0,结果错但不报错
Scatter k ≥ 16×16 indices 被直接当成 gradInput 的 GM 元素偏移写入,无上下界裁剪 → 越界写显存

Scatter 内核(pool_3d_common/op_kernel/arch22/max_pool3d_grad_scatter_unified.h:73,81):

uint64_t outOffset = ncOffset + (uint64_t)argmaxUb.GetValue(ubOffset);   // 索引直接当时偏移
DataCopyPad(this->yGm[outOffset], yUb, {1, sizeof(TGrad), 0, 0});        // 没有任何范围裁剪

负的 int32 转 uint64_t 后回绕,会写到缓冲区前面 4 字节;取 2^30 之类的大值会写到 4 GiB 之外,直接触发向量核异常 507035(同流后续任务一起失败)。

上游 arch35(950)内核有索引范围过滤(op_kernel/arch35/max_pool3d_grad_with_argmax_simd.h:172-191 的 FilterMask3D,上下界比较 + And),arch22 缺这一层。

复现环境

  • 硬件:Atlas A2 训练系列 910B2(驱动 26.1.1)
  • 软件:CANN 9.2.0,g++ 11.4.0
  • 算子目录:pooling/max_pool3d_grad_with_argmax

复现步骤

基于仓内官方示例 examples/test_aclnn_max_pool2d_with_indices_backward.cpp,改三处(其余不动):

// 1) 换成 Scatter 路径的形状:kernel/stride 都 16,self 64x64 → out 4x4
std::vector<int64_t> selfShape     = {1, 1, 64, 64};
std::vector<int64_t> gradOutShape  = {1, 1, 4, 4};
std::vector<int64_t> indicesShape  = {1, 1, 4, 4};
std::vector<int64_t> gradInShape   = {1, 1, 64, 64};
std::vector<int64_t> kernelSizeData = {16, 16};
std::vector<int64_t> strideData     = {16, 16};

// 2) indices 全填 -1(同理可填 H*W=4096 或 2^30)
std::vector<int32_t> indicesHostData = {-1, -1, -1, -1, -1, -1, -1, -1,
                                        -1, -1, -1, -1, -1, -1, -1, -1};

// 3) 用 msSanitizer 包住可执行文件
//    mssanitizer --tool=memcheck -- ./demo

实际结果

first-phase  ret = 0
second-phase ret = 0, sync ret = 0
====== ERROR: illegal write of size 4
======    at 0x12c0c001dffc on GM in MaxPool3DGradWithArgmax_fp32_high_performance_2
======    in block aiv(0) on device 0
  • indices = 2^30 时:aclrtSynchronizeStream 返回 507035 = ACL_ERROR_RT_VECTOR_CORE_EXCEPTION。
  • Normal 路径(self 1x1x4x4、k2 s2、indices 全 16=H*W 或全 -1):两段都返回 0,gradInput 全 0,无任何报错。

预期结果

两种后果都不该出现:

  1. 入口按资料把 indices 取值范围校验掉,非法值返回 161002(ACLNN_ERR_PARAM_INVALID);
  2. 或在内核侧按 arch35 的 FilterMask3D 做范围裁剪,越界索引不参与写。

备注

同目录的 aclnnMaxPool2dWithMaskBackward 没有这个问题(它是 int8 位图,不走偏移寻址)。
A2 上本接口实际下发的是 1 层深的 3D 池化 MaxPool3DGradWithArgmax(op_api/aclnn_max_pool2d_with_indices_backward.cpp:942),msan 日志里的 kernel 名即由此而来。

likedislike
yuning_chen
yuning_chenCollaborators
Oct 21 Comments:

初步分析

感谢反馈。

现象理解:aclnnMaxPool2dWithIndicesBackward 没有校验 indices 越界。A2 的 Scatter 路径会越界写显存。

当前判断:初步判断,待复现确认。

涉及范围:max_pool2d_with_indices_backward 的索引范围。

下一步:用越界 indices 做内存检查。确认越界写之后,在入口拒绝。

likedislike
yuning_chenyuning_chenCollaborators
Oct 21 Assign duxinlei as responsible person
yuning_chen
yuning_chenCollaborators
Oct 22 Comments:

您好,该问题已收到,当前问题 @duxinlei 处理中

likedislike
memorecoolmemorecoolCollaborators
17 days ago Removed responsible person duxinlei
memorecoolmemorecoolCollaborators
17 days ago Assign cjz_ as responsible person
chenjiayi_chenjiayi_Collaborators
5 days ago Assign chenjiayi_ as responsible person
chenjiayi_
chenjiayi_Collaborators
5 days ago Comments:

补充说明本问题的处理进展:

当前状态:
该 issue 当前指派给 cjz_,指派时间为 2026-11-04 17:29;关联 PR 记录为空,指派后评论区保持静止。

依据:

  • 当前责任人:cjz_。
  • 指派时间:2026-11-04 17:29。
  • 指派后评论记录:静止。
  • 关联 PR 记录:空。

后续:
请责任人补充修复计划或提交 PR;当前 master 行为核对后同步结论。

likedislike
陈俊卓陈俊卓Collaborators
4 days ago Associated pull request
陈俊卓陈俊卓Collaborators
3 days ago changed custom state from 进行中 to 已完成
陈俊卓陈俊卓Collaborators
3 days ago Close issue
陈俊卓
陈俊卓Collaborators
3 days ago Comments:

已修改

likedislike
CANN-robotCANN-robotCollaborators
3 days ago Added label: resolved
chenjiayi_chenjiayi_Collaborators
12 hours ago Removed responsible person chenjiayi_