Closed
ForeachSin arch35 tiling 无输入校验致混合 dtype 列表输出越界写,另含溢出哨兵/aclnn 出参判空/const 及资料口径问题 #7809
xuejinghuiAdd on 21 days agoClosed at 21 days ago
21 days ago Modify description of issue
xuejinghui
21 days ago Comments:
21 days ago Comments:
/assign


21 days ago Assign xuejinghui as responsible person
21 days ago Close issue
21 days ago Added label: resolved
问题描述
ForeachSin(arch35 / ascend950 交付)经 950 全流程验证(静态检查 + 上板实测)确认以下问题,均已定位修复:
1. arch35 tiling 无输入校验,混合 dtype 列表输出越界写(内存安全,P1)
op_host/arch35/foreach_sin_tiling.cpp仅在tensorNum > 0时读取动态输入实例 (0,0) 的 dtype 生成 TilingKey(:67-72、:103-114),循环只取 shape,全链无任何"列表内 dtype 一致性"校验。混合 dtype 列表(各成员均在 {FLOAT16, FLOAT, BF16} 白名单内,README 声明"所有 Tensor 数据类型保持一致")经图模式通路(GEIR/Kernel 直连路由,torchair 转换器透传不检查)整体送入 kernel 后,以实例 0 的 dtype 单一模板处理全部 tensor(op_kernel/arch35/foreach_sin.cpp:31-37→foreach_sin_simt.h以编译期固定T读写):成员 0 为 float32、成员 1 为 bfloat16 时,成员 1 及其输出按 4 字节步长访问 2 字节缓冲,物理读写量达分配大小 2 倍。上板实测:x = [float32 (147,3,104), bfloat16 (16135,2,2)],Const/Dynamic/Release Binary 三执行模式均被接受执行,输出成员 1 越界写:"The 1th output memory out of bound"(memory_oob FAIL)。aclnn 通路有逐项 dtype 校验(161002 拒绝),kernel/GEIR 直连路由无防护。
2. GetShapeSize 溢出哨兵 INT64_MIN 未拦截,进入 kernel 形成巨大无符号循环上界(P1)
gert::Shape::GetShapeSize()维度乘积溢出 int64 时返回哨兵kInvalidDimValue = INT64_MIN(负值)。tiling 将其原样写入tensorElements[i](无非负校验);kernel 仅跳过count == 0,负值经static_cast<uint64_t>变为 9.2e18 循环上界 → 无界读写。构造仅需元数据(如 dims=[2^62, 2]),无需物化存储。API 层 CheckShape 只查 rank≤8 与 x/out 相等,无维值上限校验。3. 张量数量校验晚于 int32 窄化(P1)
int32_t tensorNum = static_cast<int32_t>(GetInstanceNum())之后才检查> 256;GetInstanceNum 返回 size_t,≥2^32 时窄化回绕可能变成小正数绕过检查,越过 tiling data 定长数组tensorElements[256]。4. aclnn 第一段接口出参未判空(P1)
aclnnForeachSinGetWorkspaceSize在未校验workspaceSize/executor非空的情况下直接解引用写入(空输入列表早退路径同样命中);仓内标准宏OP_CHECK_COMM_INPUT(142 个 aclnn 实现在用)未被使用。5. aclnn 头文件 out 参数缺 const(P1)
aclnn_foreach_sin.h第一段接口声明aclTensorList* out(非 const),与基线公开原型(canndev 中英文资料均const aclTensorList* out)及自身接口文档原型不一致;按基线原型持有 const 指针的存量调用方无法直接传参(非法隐式转换,编译失败)。6. 资料口径与实现不符(P2)
op_graph/foreach_sin_proto.h:21/:26注册注释把正弦算子误写为 cos("Apply cos operation...the cos value of the x");OP_CHECK_SHAPE_NOT_EQUAL强等),按文档构造元素数更多的 out 被拒绝;7. 样例失败路径资源泄漏(P2)
examples/test_aclnn_foreach_sin.cpp仅在成功路径释放资源,任一 CHECK_RET 提前 return 时已申请的 device 内存/TensorList/stream/上下文均不释放。8. arch35 tiling UT include 笔误致其从未参与编译(P2,本次顺带修复)
该 UT 写的是
kernel_run_context_faker.h,仓内实际为kernel_run_context_facker.h——凡触发 arch35 UT 编译的门禁必失败;此前该文件从未被选中编译(历次"ophost UT 5/5"实为 arch22(2)+infershape(2)+opapi(1))。环境信息
ops-nn master(2026-10-30 复核全部仍在);Ascend 950PR;CANN 9.3.0(npu-smi 25.7.rc1.10)
重现步骤
# 问题1:混合 dtype 越界写(需 kernel/GEIR 图模式路由;torch eager 经 aclnn 校验返回 161002 不复现) # 上板用例(TTK kernel 路由,约束违反用例,必现): x = [torch.randn(147, 3, 104, dtype=torch.float32), torch.randn(16135, 2, 2, dtype=torch.bfloat16)] # 图模式提交 ForeachSin(x) → 三执行模式均接受执行,输出成员 1 memory out of bound// 问题4:出参未判空(必现,段错误) uint64_t ws = 0; aclnnForeachSinGetWorkspaceSize(xList, outList, nullptr, nullptr); // 空指针解引用 // 空列表早退路径同样命中:xList->Size()==0 时 *workspaceSize = 0 直接解引用// 问题5:out const 失配(编译期必现) const aclTensorList* out = MakeOutList(...); aclnnForeachSinGetWorkspaceSize(x, out, &ws, &exec); // const → 非 const 非法隐式转换,编译失败问题 2:构造 dims=[4611686018427387904, 2] 的输入元数据(仅元数据,无需真实内存)→ tiling 接受 → kernel 循环上界 static_cast<uint64_t>(INT64_MIN)。问题 3/6/7/8 为源码级确认(行号见上)。
根因分析
ForeachCommonTiling::Init有逐 tensor dtype 校验;aclnn 层CheckDtypeValid有逐项校验;唯独 arch35 tiling 只读实例 0);OP_CHECK_COMM_INPUT/const 契约);修复方案
对齐上游
fc40bf74c(foreach_neg/sign/tanh)与30da3abee(foreach_abs/asin/exp)的ValidateForeachXxx家族范式:uint64_t instanceNum直接比对 ≤256);新增ValidateForeachSin逐实例校验——x/y 数量配对、desc/shape 非空、维度 ≤8、GetShapeSize() < 0拒绝(拦截溢出哨兵)、dtype 白名单、列表内 dtype 一致性、y dtype 与 x 一致、y 与对应位置 x 的 shape 严格相等;空列表 no-op 成功(与 aclnn 口径一致)。校验日志用OP_LOGE_FOR_INVALID_*系列预定义错误码宏。合法输入行为零变化(只加校验不改流程)。OP_CHECK_COMM_INPUT(workspaceSize, executor);头文件/实现 out 参数补 const(内部函数本就是 const)。验证
修复提交
fork 仓 xuejinghui/ops-nn 分支
fix/foreach-sin-hardening(基于 master4d83e6d30),commit2a6e26b2c(10 文件,+424/-94)。提 MR 后以Fixes #<本Issue>关联。备注
fc40bf74c(foreach_neg/sign/tanh 950 静态检查修复)、30da3abee(foreach_abs/asin/exp),foreach_sin 为家族中最后一个未补齐该校验的算子,本修复与其结构逐项对齐便于统一维护;Simt::VF_CALL/Simt::Dim3/Simt::Get*)本次未动,拟另案迁移asc_vf_call/dim3/blockIdx.x系;