已关闭
【缺陷报告】数组索引越界 - 文件compare.h - 函数CompareScalarExtend - 行号394 #253
zhangjunkai9创建于  20 天前关闭于  1 天前
zhangjunkai9
20 天前 创建

缺陷信息

缺陷类型arrayIndexOutOfBounds (数组索引越界)
函数CompareScalarExtend
文件autofuse/ascendc/api/compare.h
行号394
置信度85%

缺陷描述

在 CompareScalarExtend 函数第394行,CompareScalar 的源操作数使用了 src_tmp[cnt],但数据在第392行通过 DataCopy(src_tmp[0], src[cnt], left_cnt) 被复制到了 src_tmp[0]。cnt 是源 tensor 的累计偏移量(在循环中每轮累加 max_block_cnt),而非临时缓冲区 src_tmp 的偏移量。当 cnt 超过 src_tmp 的有效大小时(src_tmp 是 tmp_buf 的子视图,大小受限于 UB 缓冲区),src_tmp[cnt] 构成数组越界访问。正确写法应为 src_tmp[0]。

事实核查

核查1(模式成立):第392行 DataCopy(src_tmp[0], src[cnt], left_cnt) 将数据复制到 src_tmp[0],但第394行 CompareScalar(compare_out[0], src_tmp[cnt], ...) 使用 src_tmp[cnt] 作为源操作数。
cnt 是源 tensor src 的累计偏移(循环中每轮累加 max_block_cnt,可达数千),而 src_tmp 是 tmp_buf 的子视图(大小受限于 UB 缓冲区),src_tmp[cnt] 远超 src_tmp 有效边界,数组越界访问模式成立。
核查2(防护无效):第390行 if(left_cnt*sizeof(T)<256) 仅判断是否走小数据路径,无对 cnt 与 src_tmp 大小关系的校验,无有效防护。
核查3(函数可达):Grep 确认 compare_api_call.cpp:73 和 compare_v2_api_call.cpp:138/144 通过 codegen 生成对本函数的调用,函数可达。
核查4(触发可满足):触发条件为 left_cnt*sizeof(T)<256(第390行),当总数据量非块对齐时常见满足,触发可满足。
四项均成立,确认缺陷。

数据流证据

Source(问题源头)

autofuse/ascendc/api/compare.h:367 行 const uint32_t cal_cnt 作为函数入参传入(外部决定计算量大小,决定 cnt 累积上限)

Sink(问题爆发点)

autofuse/ascendc/api/compare.h:394 行 CompareScalar(compare_out[0], src_tmp[cnt], ...) 使用 src_tmp[cnt] 作为源操作数(cnt 为源 tensor 偏移而非临时缓冲区偏移,越界访问 sink)

传播路径:

# 文件 行号 说明
1 autofuse/ascendc/api/compare.h 367 const uint32_t cal_cnt 作为函数入参接收(外部输入,决定循环次数和 cnt 上限)
2 autofuse/ascendc/api/compare.h 372 const int32_t loop_num = cal_cnt / max_block_cnt 根据 cal_cnt 计算循环次数
3 autofuse/ascendc/api/compare.h 378 LocalTensor src_tmp = tmp_buf[tmp_offset].ReinterpretCast() 创建临时缓冲区视图 src_tmp(大小受限于 tmp_buf 剩余空间)
4 autofuse/ascendc/api/compare.h 379 int32_t cnt = 0 初始化源 tensor 偏移计数器
5 autofuse/ascendc/api/compare.h 385 cnt += max_block_cnt 在循环中累加 cnt(每轮增加 max_block_cnt,可达数千至上万)
6 autofuse/ascendc/api/compare.h 392 DataCopy(src_tmp[0], src[cnt], left_cnt) 将数据复制到 src_tmp[0](正确目标偏移为0)
7 autofuse/ascendc/api/compare.h 394 CompareScalar(compare_out[0], src_tmp[cnt], ...) 使用 src_tmp[cnt] 读取数据(应使用 src_tmp[0],cnt 远超 src_tmp 边界导致越界)

调用链

可达调用链1 起点:(entry functions) → 终点:CompareScalarExtend 深度:0

# 文件 函数 函数起始行 调用点行
1 autofuse/ascendc/api/compare.h CompareScalarExtend 45

修复建议

CompareScalar(compare_out[0], src_tmp[0], constant_y, mode, 256 / sizeof(T));
likedislike
wangmingming成员
20 天前 评论:

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

likedislike
Wwangmingming成员
20 天前 将 gcw_V3YyYBt1 设为负责人
Jjcmrn0930
12 天前 关联了pull request:解决issue253 254 255
Jjcmrn0930
12 天前 关联了pull request:解决issue 253 254 255
Wwangmingming成员
1 天前 issue状态由 进行中 改变为 已完成
Wwangmingming成员
1 天前 关闭了 issue