Closed
【缺陷报告】算子mc2/moe_distribute_combine_v2 #4775
zhangjunkai9Add on  Aug 31Closed at  Sep 14
zhangjunkai9
zhangjunkai9
Aug 31 Create

缺陷信息

缺陷类型nullptrDeref (空指针解引用)
函数CheckOptionalScalesTensorDim
文件mc2/moe_distribute_combine_v2/op_host/op_tiling/moe_distribute_combine_v2_tiling.cpp
行号500
置信度90%
分析模型maas-glm-5.1-zhipu

缺陷描述

函数 CheckOptionalScalesTensorDim 中,expandScaleStorageShape 在第499行由 context->GetOptionalInputShape(EXPAND_SCALES_INDEX) 获取(可选输入,可能返回 nullptr),但在第500行直接解引用 expandScaleStorageShape->GetStorageShape().GetDimNum(),而空指针检查 OP_TILING_CHECK(expandScaleStorageShape == nullptr, ...) 在第501行才执行。解引用在空检查之前,顺序颠倒,若该可选输入不存在(GetOptionalInputShape 返回 nullptr),第500行将解引用空指针导致崩溃。对比同函数中第511-513行 constExpertAlpha1StorageShape 的正确模式(先 if (ptr != nullptr) 再解引用),以及第575-576行 oriXStorageShape 的正确模式,可确认第499-500行为缺陷代码。

同算子问题

1

缺陷类型nullptrDeref (空指针解引用)
函数Init
文件mc2/moe_distribute_combine_v2/op_kernel/arch35/moe_distribute_combine_v2_a5_mte.h
行号544
置信度90%
分析模型maas-glm-5.1-zhipu

在 MoeDistributeCombineV2A5Mte::Init 函数中,当 tilingData 的 isMc2Context 标志为 false 时,else 分支在第546行调用 Mc2Kernel::GetWinSize(epWinContext_),而 epWinContext_ 在类定义中初始化为 nullptr(第215行),此时尚未被赋值。epWinContext_ 的赋值发生在 InitCommContext 的 else 分支(第455行),而 InitCommContext 由 InitAttrs(第480行)调用,InitAttrs 在第549行才被调用——即在第546行之后。GetWinSize 函数(moe_distribute_base.h 第472-475行)直接解引用 winContext->winSize,导致空指针解引用。该函数可通过 moe_distribute_combine_v2_apt.cpp、moe_distribute_combine_add_rms_norm_apt.cpp 和 moe_distribute_combine_v3_apt.cpp 三个内核入口函数到达,其中前两个入口使用 GetHcclContext 而非 mc2Context,isMc2Context 为 false,必然触发此缺陷。

事实核查

经核查(1)模式成立:第499行 GetOptionalInputShape(EXPAND_SCALES_INDEX) 获取可选输入 shape 指针后,第500行直接解引用 expandScaleStorageShape->GetStorageShape().GetDimNum(),而空指针检查 OP_TILING_CHECK(expandScaleStorageShape==nullptr,...) 在第501行才执行,顺序颠倒——对比同函数第511-513行 constExpertAlpha1StorageShape 的正确模式(先 if(ptr!=nullptr) 再解引用)及第575-576行 oriXStorageShape 的正确模式,可确认第499-500行为缺陷代码;

(2)防护无效:第501行空检查在解引用之后,无法保护第500行;

(3)函数可达:CheckTensorDim(行1576)→CheckOptionalInputTensorDim(行707)→CheckOptionalScalesTensorDim(行584),入口函数经 GetAttrAndSetTilingData(行1896) 在第329行设 isLayered=strcmp(commAlgPtr,"hierarchy")==0,isLayered 可为 true,CheckInputParam(行1912)→TilingCheckMoeDistributeCombine(行1830)→CheckTensorDim 调用链完整可达;

(4)触发可满足:expandScales 为可选输入,GetOptionalInputShape 对未提供的可选输入返回 nullptr,开发者第501行的空检查本身即证明其预期 nullptr 可达,用户设 commAlg="hierarchy"(isLayered=true) 且不提供 expandScales 时第500行解引用 nullptr 导致崩溃。

缺陷真实,confidence 保持 0.9。

数据流证据

Source(问题源头)

mc2/moe_distribute_combine_v2/op_host/op_tiling/moe_distribute_combine_v2_tiling.cpp:499 行 const gert::StorageShape *expandScaleStorageShape = context->GetOptionalInputShape(EXPAND_SCALES_INDEX) 获取可选输入的 shape 指针(GetOptionalInputShape 对不存在的可选输入返回 nullptr)

Sink(问题爆发点)

mc2/moe_distribute_combine_v2/op_host/op_tiling/moe_distribute_combine_v2_tiling.cpp:500 行 expandScaleStorageShape->GetStorageShape().GetDimNum() 在空指针检查之前直接解引用(空指针解引用 sink)

传播路径:

# 文件 行号 说明
1 mc2/moe_distribute_combine_v2/op_host/op_tiling/moe_distribute_combine_v2_tiling.cpp 498 if (isLayered) 进入 isLayered 分支(该分支下 expandScales 为可选输入)
2 mc2/moe_distribute_combine_v2/op_host/op_tiling/moe_distribute_combine_v2_tiling.cpp 499 expandScaleStorageShape = context->GetOptionalInputShape(EXPAND_SCALES_INDEX) 获取可选输入 shape 指针(可能为 nullptr,无任何前置非空保证)
3 mc2/moe_distribute_combine_v2/op_host/op_tiling/moe_distribute_combine_v2_tiling.cpp 500 const int64_t expandScaleDim = expandScaleStorageShape->GetStorageShape().GetDimNum() 直接解引用未校验的指针(sink,此时空指针检查尚未执行)
4 mc2/moe_distribute_combine_v2/op_host/op_tiling/moe_distribute_combine_v2_tiling.cpp 501 OP_TILING_CHECK(expandScaleStorageShape == nullptr, ...) 空指针检查在解引用之后才执行,无法保护第500行的解引用
5 mc2/moe_distribute_combine_v2/op_host/op_tiling/moe_distribute_combine_v2_tiling.cpp 584 CheckOptionalInputTensorDim 调用 CheckOptionalScalesTensorDim(函数可达,由 CheckTensorDim 在第707行调用)

修复建议

if (isLayered) {
    const gert::StorageShape *expandScaleStorageShape = context->GetOptionalInputShape(EXPAND_SCALES_INDEX);
    OP_TILING_CHECK(expandScaleStorageShape == nullptr, OP_LOGE_WITH_INVALID_INPUT(nodeName, "expandScales"),
                    return false);
    const int64_t expandScaleDim = expandScaleStorageShape->GetStorageShape().GetDimNum();
    OP_TILING_CHECK(
        expandScaleDim != ONE_DIM,
        OP_LOGE_FOR_INVALID_SHAPEDIM_WITH_REASON(nodeName, "expandScales", std::to_string(expandScaleDim).c_str(),
                                                 "The shape dim of expandScales must be 1D."),
        return false);
    OP_LOGD(nodeName, "expandScales dim0 = %ld", expandScaleStorageShape->GetStorageShape().GetDim(0));
}
likedislike
weihao18
weihao18Collaborators
Sep 1 Comments:

你好,建议汇总到具体算子进行集中处理

likedislike
zhangjunkai9zhangjunkai9
Sep 2 Change title to "【缺陷报告】算子mc2/moe_distribute_combine_v2", originally titled "【缺陷报告】空指针解引用 - 文件moe_distribute_combine_v2_tiling.cpp - 函数CheckOptionalScalesTensorDim - 行号500".
zhangjunkai9zhangjunkai9
Sep 2 Modify description of issue
游震
游震Collaborators
Sep 3 Comments:

/assign @tgwsakiko_

likedislike
CANN-robotCANN-robotCollaborators
Sep 3 Assign tgwsakiko_ as responsible person
CANN-robotCANN-robotCollaborators
Sep 14 Close issue
CANN-robotCANN-robotCollaborators
Sep 14 Added label: resolved