已关闭
【缺陷报告】空指针解引用 - 文件aclnn_modulate_backward.cpp - 函数CheckMaxDimension - 行号103 #5015
zhangjunkai9创建于  8月24日关闭于  29 天前
zhangjunkai9
8月24日 创建

缺陷信息

缺陷类型nullptrDeref (空指针解引用)
函数CheckMaxDimension
文件vfusion/modulate_grad/op_host/op_api/aclnn_modulate_backward.cpp
行号103
置信度90%

缺陷描述

函数 CheckMaxDimension 的参数 scale 和 shift 为可选参数,可为 nullptr(源码注释 line 151 明确说明 scale和shift参数可以为空)。在第 103 行的 if 条件 (!scale->IsEmpty() || !shift->IsEmpty()) 中,当 grad_output 或 input 为空 tensor 时(IsEmpty() 返回 true),&& 的左操作数为 true,导致右操作数被求值,直接对可能为 nullptr 的 scale/shift 调用 IsEmpty(),触发空指针解引用。同文件的 CheckDtypeValid 和 CheckDimension 函数均使用 CheckNotNullForScaleAndShift 保护 scale/shift 的解引用,唯独此处遗漏。调用路径:aclnnModulateBackwardGetWorkspaceSize(line 164) -> CheckParams(line 175) -> CheckMaxDimension(line 157),entry_callchain 为空但函数在文件内可达。

事实核查

核查1:缺陷模式成立。

第103行 (!scale->IsEmpty() || !shift->IsEmpty()) 直接对 scale/shift 调用 IsEmpty(),当 scale 或 shift 为 nullptr 时空指针解引用。

核查2:无有效防护,CheckParams:152 仅检查 grad_output/input/grad_input 非空,未检查 scale/shift;

CheckDtypeValid 和 CheckDimension 均使用 CheckNotNullForScaleAndShift 保护,唯独此处遗漏。

核查3:函数可达,aclnnModulateBackwardGetWorkspaceSize:175->CheckParams:157->CheckMaxDimension。

核查4:触发可满足——scale/shift 为可选参数(line 151 注释确认可为空),当调用方传入 scale=nullptr 且 grad_output 或 input 为空 tensor 时,&& 左侧为 true,求值右侧 !scale->IsEmpty() 即触发空指针解引用崩溃,该路径可直接构造

数据流证据

Source(问题源头)

vfusion/modulate_grad/op_host/op_api/aclnn_modulate_backward.cpp:164 行 aclnnModulateBackwardGetWorkspaceSize(const aclTensor* grad_output, const aclTensor* input, const aclTensor* scale, const aclTensor* shift, ...) 接收 scale 和 shift 作为可选参数(可能为 nullptr 的外部输入)

Sink(问题爆发点)

vfusion/modulate_grad/op_host/op_api/aclnn_modulate_backward.cpp:103 行 (!scale->IsEmpty() || !shift->IsEmpty()) 直接解引用可能为 nullptr 的 scale 和 shift(空指针解引用 sink)

传播路径:

# 文件 行号 说明
1 vfusion/modulate_grad/op_host/op_api/aclnn_modulate_backward.cpp 164 aclnnModulateBackwardGetWorkspaceSize 接收 scale/shift 作为可选参数(可能为 nullptr 的外部输入)
2 vfusion/modulate_grad/op_host/op_api/aclnn_modulate_backward.cpp 175 auto ret = CheckParams(grad_output, input, scale, shift, grad_input, grad_scale, grad_shift) 将 scale/shift 传递给 CheckParams
3 vfusion/modulate_grad/op_host/op_api/aclnn_modulate_backward.cpp 151 注释(scale和shift参数可以为空,所以只检查grad_out input grad_input这三个参数是否为空指针) 确认 scale/shift 可为 nullptr
4 vfusion/modulate_grad/op_host/op_api/aclnn_modulate_backward.cpp 152 CHECK_RET(CheckNotNull(grad_output, input, grad_input), ACLNN_ERR_PARAM_NULLPTR) 仅检查 grad_output/input/grad_input 非空,未检查 scale/shift
5 vfusion/modulate_grad/op_host/op_api/aclnn_modulate_backward.cpp 157 CHECK_RET(CheckMaxDimension(grad_output, input, scale, shift), ACLNN_ERR_PARAM_INVALID) 将未校验非空的 scale/shift 传入 CheckMaxDimension
6 vfusion/modulate_grad/op_host/op_api/aclnn_modulate_backward.cpp 103 (!scale->IsEmpty() || !shift->IsEmpty()) 当 grad_output 或 input 为空时 && 左侧为 true,求值右侧直接解引用可能为 nullptr 的 scale/shift(sink)

修复建议

static bool CheckMaxDimension(const aclTensor* grad_output, const aclTensor* input, const aclTensor* scale,
                              const aclTensor* shift)
{
    op::Shape gradoutputShape = grad_output->GetViewShape();
    size_t gradoutputDimNum = gradoutputShape.GetDimNum();
    op::Shape inputShape = input->GetViewShape();
    size_t inputDimNum = inputShape.GetDimNum();
    if (gradoutputDimNum != MAX_SUPPORT_DIM || inputDimNum != MAX_SUPPORT_DIM) {
        OP_LOGE(ACLNN_ERR_PARAM_INVALID, "The dimension of self must be 3");
        return false;
    }
    if ((grad_output->IsEmpty() || input->IsEmpty()) &&
        ((scale != nullptr && !scale->IsEmpty()) || (shift != nullptr && !shift->IsEmpty()))) {
        OP_LOGE(ACLNN_ERR_PARAM_INVALID, "when grad_output or input is empty, scale and shift must be empty");
        return false;
    }

    return true;
}
likedislike
zhangqijia1
8月24日 评论:

/assign

likedislike
CANN-robotCANN-robot成员
8月24日 将 gcw_SUaZx3UQ 设为负责人
yuning_chenyuning_chen成员
8月24日 将 tianqiguang 设为负责人,移除负责人 gcw_SUaZx3UQ
zhangqijia1
8月24日 评论:

/assign

likedislike
CANN-robotCANN-robot成员
8月24日 将 gcw_SUaZx3UQ 设为负责人,移除负责人 tianqiguang
Zzhangqijia1
8月24日 关联了pull request:fix(modulate_grad): 修复 aclnnModulateBackward 中 scale/shift 空指针解引用
CANN-robotCANN-robot成员
29 天前 关闭了 issue
CANN-robotCANN-robot成员
29 天前 添加了label:resolved