Open
【缺陷报告】整数溢出(含上溢出/下溢出) - 文件kernel_operator_vec_reduce_impl.h - 函数isMasked - 行号1365 #1861
zhangjunkai9Add on  27 days ago
zhangjunkai9
27 days ago Create

缺陷信息

缺陷类型integerOverflow (整数溢出(含上溢出/下溢出))
函数isMasked
文件asc-devkit-master/impl/basic_api/dav_l300/kernel_operator_vec_reduce_impl.h
行号1365
置信度90%
分析模型maas-glm-5.1-zhipu

缺陷描述

在 isMasked 函数第 1365 行,表达式 uint32_t bit_to_check = (1 << (inRepeatIndex % 64)) 存在有符号整数移位溢出(未定义行为)缺陷。字面量 1 的类型为 int(32 位有符号),当 inRepeatIndex % 64 的结果 >= 32 时,1 << 32 或更大的移位量会导致有符号整数溢出,属于 C/C++ 标准中的未定义行为(UB)。调用方 IterateSrc(第 1388 行)计算 inRepeatIndex = j * blockLen + k,其中 blockCount=8,blockLen = 32/sizeof(T)。对于 half 类型(sizeof=2),blockLen=16,最大 inRepeatIndex = 7*16 + 15 = 127,127 % 64 = 63 >= 32,触发 UB。对于 float 类型(sizeof=4),blockLen=8,最大 inRepeatIndex = 7*8 + 7 = 63,63 % 64 = 63 >= 32,同样触发 UB。该函数通过 GetReduceMaxMinCountImpl → IterateSrc 调用链可达,且 bitwiseMask 为 true 时执行此路径。

事实核查

核查1(模式):第1365行 uint32_t bit_to_check = (1 << (inRepeatIndex % 64)),字面量1为int(32位有符号),当inRepeatIndex%64>=32时,1<<N超出有符号int表示范围,属C/C++标准未定义行为(UB),模式成立。

核查2(防护):isMasked函数内无任何对inRepeatIndex的范围校验。

核查3(可达):GetReduceMaxMinCountImpl(line 1472)->IterateSrc(line 1393)->isMasked(line 1363),函数可达。

核查4(触发):IterateSrc中inRepeatIndex=j*blockLen+k(line 1388),blockLen=32/sizeof(T)(line 1379),blockCount=8(line 1380)。

对float(sizeof=4)blockLen=8,srcBlkStride=1时j=4,k=0得inRepeatIndex=32,32%64=32>=32触发UB;

对half(sizeof=2)blockLen=16,j=2,k=0得inRepeatIndex=32同样触发。

bitwiseMask=true路径(line 1452)为合法功能路径,触发可满足,缺陷确认。

数据流证据

Source(问题源头)

asc-devkit-master/impl/basic_api/dav_l300/kernel_operator_vec_reduce_impl.h:1363 行 uint32_t inRepeatIndex 作为函数 isMasked 的入参传入(无范围约束)

Sink(问题爆发点)

asc-devkit-master/impl/basic_api/dav_l300/kernel_operator_vec_reduce_impl.h:1365 行 uint32_t bit_to_check = (1 << (inRepeatIndex % 64)) 有符号整数移位溢出/UB(当移位量 >= 32)

传播路径:

# 文件 行号 说明
1 asc-devkit-master/impl/basic_api/dav_l300/kernel_operator_vec_reduce_impl.h 1363 uint32_t inRepeatIndex 作为函数入参接收(无范围校验)
2 asc-devkit-master/impl/basic_api/dav_l300/kernel_operator_vec_reduce_impl.h 1365 1 << (inRepeatIndex % 64) 字面量 1 为 int 类型,inRepeatIndex % 64 范围 [0,63],当 >= 32 时 1 << N 为有符号整数溢出 UB
3 asc-devkit-master/impl/basic_api/dav_l300/kernel_operator_vec_reduce_impl.h 1388 uint32_t inRepeatIndex = j * blockLen + k; 调用方 IterateSrc 计算 inRepeatIndex,对 half 类型最大可达 127,对 float 类型最大可达 63
4 asc-devkit-master/impl/basic_api/dav_l300/kernel_operator_vec_reduce_impl.h 1393 if (!isMasked(inRepeatIndex, mask0, mask1)) 调用点,bitwiseMask 为 true 时执行此路径
5 asc-devkit-master/impl/basic_api/dav_l300/kernel_operator_vec_reduce_impl.h 1472 IterateSrc(src, repeat, ...) GetReduceMaxMinCountImpl 调用 IterateSrc,传入的 repeat 和 mask 来自运行时数据

修复建议

将字面量 1 改为无符号类型以避免有符号移位溢出:
uint32_t bit_to_check = (1u << (inRepeatIndex % 64));
likedislike
Lli_fangyuan
27 days ago Associated pull request
Ffengjian12345
27 days ago Associated pull request
li_fangyuan
25 days ago Comments:

您好,相关问题已修复,已修改bit_to_check的声明类型和位操作逻辑

likedislike
li_fangyuan
25 days ago Comments:

/close

likedislike
CANN-robot
CANN-robotCollaborators
25 days ago Comments:

Notice

@li_fangyuan , you can't close an issue unless you are the author of it or a collaborator.

likedislike
li_fangyuan
25 days ago Comments:

/assign

likedislike
CANN-robotCANN-robotCollaborators
25 days ago Assign li_fangyuan as responsible person
li_fangyuan
25 days ago Comments:

/close

likedislike
CANN-robot
CANN-robotCollaborators
25 days ago Comments:

Notice

@li_fangyuan , you can't close an issue unless you are the author of it or a collaborator.

likedislike
ApeiriaNode_BookerApeiriaNode_BookerCollaborators
19 days ago changed custom category from 任务 to 缺陷