已关闭
【缺陷报告】数据类型截断 - 文件cast.h - 函数CastExtendWithMaskMode - 行号134 #259
zhangjunkai9创建于  27 天前关闭于  12 天前
zhangjunkai9
27 天前 创建

缺陷信息

缺陷类型dataTypeTruncation (数据类型截断)
函数CastExtendWithMaskMode
文件autofuse/ascendc/api/cast.h
行号134
置信度86%

缺陷描述

函数 CastExtendWithMaskMode 的入参 output_last_dim_stride(uint32_t)在第134行经 output_last_dim_stride * sizeof(OutT) / ONE_BLK_SIZE 计算后,结果直接赋值给 uint8_t dst_repeat_stride(8位硬件指令参数)。当 stride 超过 255 blocks(如 float 输出时 stride > 255*8=2040 元素)时高位截断,导致 Cast 指令使用错误的 repeat stride。该函数被 CastExtend(cast.h:273)调用,CastExtend 被 codegen/cast_api_call.cpp 调用,output_last_dim_stride 来自 tiling 的 output_second_to_last_stride。无范围检查。

事实核查

核查1-模式成立:第134行 uint8_t dst_repeat_stride = output_last_dim_stride * sizeof(OutT) / ONE_BLK_SIZE 将 uint32_t 计算结果赋值给 uint8_t,无范围检查,截断模式真实存在。
核查2-防护无效:无任何范围校验。
核查3-函数可达:调用链为 codegen/cast_api_call.cpp:115-118 → CastExtend(cast.h:273) → CastExtendWithMaskMode 7参数重载(第174行) → 6参数重载(第134行缺陷处)。
核查4-触发无法证实满足:output_last_dim_stride 来自 codegen 传入的 tpipe.tiler.Size(param.output_second_to_last_stride) 符号化 tiling 值,当 output_last_dim_stride > 255*ONE_BLK_SIZE/sizeof(OutT)(float 输出时 >2040 元素)时截断。
该条件在常见张量形状下可能成立(如 last_dim=4096 的 float 张量),但调用方实参为符号化 tiling 值,无法在当前代码中确认具体触发路径。
属假设性缺陷,调用方实参存在模糊(可能触发未证实),对应 confidence 锚点 0.86。

数据流证据

Source(问题源头)

autofuse/ascendc/api/cast.h:128 行 const uint32_t output_last_dim_stride 作为函数入参传入(来自 CastExtend:273 的 output_last_dim_stride 参数,codegen 传入 tiling stride 值,无范围约束)

Sink(问题爆发点)

autofuse/ascendc/api/cast.h:134 行 uint8_t dst_repeat_stride = output_last_dim_stride * sizeof(OutT) / ONE_BLK_SIZE 将 uint32_t 计算结果赋值给 uint8_t(若结果>255 则高位截断)

传播路径:

# 文件 行号 说明
1 autofuse/ascendc/api/cast.h 128 const uint32_t output_last_dim_stride 作为函数入参接收(无范围校验)
2 autofuse/ascendc/api/cast.h 134 uint8_t dst_repeat_stride = output_last_dim_stride * sizeof(OutT) / ONE_BLK_SIZE 将 uint32_t 结果截断为 uint8_t(sink,stride>255 blocks 时高位丢失)

修复建议

uint32_t dst_rpt_stride = output_last_dim_stride * sizeof(OutT) / ONE_BLK_SIZE;
ASCENDC_ASSERT(dst_rpt_stride <= UINT8_MAX, {
  KERNEL_LOG(KERNEL_ERROR, "dst_repeat_stride exceeds 255: %u", dst_rpt_stride);
});
uint8_t dst_repeat_stride = static_cast<uint8_t>(dst_rpt_stride);
likedislike
Wwangmingming成员
27 天前 将 gcw_V3YyYBt1 设为负责人
wangmingming成员
27 天前 评论:

你好,感谢建议, 后续分析处理

likedislike
m0_56712298m0_56712298
19 天前 关联了pull request:fix: 修复 Gather 回绕及 BrcInline/Cast/Concat 截断(#256 #258 #259 #260)
Wwangmingming成员
12 天前 issue状态由 进行中 改变为 已完成
Wwangmingming成员
12 天前 关闭了 issue