Closed
[Bug-Report|缺陷反馈]: KernelGraphUtils::Link 索引越界检查不严谨导致访问越界(std::out_of_range) #409
ren-botaoAdd on  Sep 11Closed at  Sep 14
ren-botao
ren-botaoCollaborators
Sep 11 Create

一、问题描述(Describe the current behavior)

op::mem::KernelGraphUtils::Link() 用于在 Kernel 图拓扑构建时建立节点间的 tensor 依赖关系,其内部对输入输出索引做了越界检查,但边界判断有误:

  • 原代码使用 from > fromNodeOutputs.size() 判断 from 索引是否越界;
  • 当调用方传入的索引恰好等于容器元素个数(from == fromNodeOutputs.size(),即指向最后一个元素之后的越界位置)时,> 判断为 false,无法拦截;
  • 随后执行 fromNodeOutputs.at(from),op::internal::SmallVector::at() 内部 CheckOutOfRange() 检测到 index >= size_ 成立,抛出 std::out_of_range("Index out of range") 异常;
  • 该异常在 Link() 函数内没有捕获,会沿调用栈向上传播:若上层未捕获,将触发 std::terminate() 导致进程崩溃;若上层 catch 吞掉异常,则建链逻辑失败且错误不可见。

to 索引对 toNodeInputs 的检查存在同样问题。

涉及代码(src/nnopbase/composite_op/mem_mgr/kernel_graph_utils.cpp):

aclnnStatus KernelGraphUtils::Link(const KernelNode* fromNode, const KernelNode* toNode, size_t from, size_t to)
{
    // ...
    auto& fromNodeOutputs = fromNode->GetOutputs();
    if (from > fromNodeOutputs.size()) {   // 缺陷:应为 >=
        OP_LOGE(ACLNN_ERR_INNER, "From index %zu is larger than fromNode output size %zu.", from,
                fromNodeOutputs.size());
        return ACLNN_ERR_INNER;
    }
    auto& toNodeInputs = toNode->GetInputs();
    if (to > toNodeInputs.size()) {        // 缺陷:应为 >=
        OP_LOGE(ACLNN_ERR_INNER, "To index %zu is larger than toNode input size %zu.", to, toNodeInputs.size());
        return ACLNN_ERR_INNER;
    }
    auto* fromTensor = fromNodeOutputs.at(from);   // from == size() 时此处抛 std::out_of_range
    auto* toTensor = toNode->GetInputs().at(to);
    // ...
}

容器语义:KernelNode::GetOutputs() / GetInputs() 返回 op::FVector<KernelTensor*, BASIC_NUM>,其底层 op::internal::SmallVector::at() 在 index >= size_ 时主动抛出 std::out_of_range(见 small_vector.h)。因此索引越界时表现为异常而非返回错误码,与函数 aclnnStatus 返回码的错误处理约定不一致。

二、环境信息(Environment)

该缺陷为纯代码逻辑问题,不特定于昇腾硬件型号或软件版本。凡是走 KernelGraphUtils::Link() 建链路径的环境均可能触发(建议在 Ascend NPU + CANN 版本配套环境复现验证)。

三、重现步骤(Steps to reproduce)

  1. 构造两个 KernelNode 节点 A、B,其中 A 含 N 个输出、B 含 M 个输入;
  2. 调用 KernelGraphUtils::Link(A, B, N, 0),即 from = N(恰好等于 A 的输出数量);
  3. 旧代码中 from > fromNodeOutputs.size()(即 N > N)判断为 false,未拦截;
  4. 执行 fromNodeOutputs.at(N),SmallVector::at() 检测 index >= size_ 成立,抛出 std::out_of_range;
  5. Link() 内无 try/catch,异常向上传播:未被上层捕获时进程以 terminate 崩溃。

to 侧同理:KernelGraphUtils::Link(A, B, 0, M) 触发 toNodeInputs.at(M) 越界。

四、预期结果(Expected behavior)

  • 当 from/to 索引等于对应容器元素个数(>= size())时,应被视为越界:Link() 打印明确错误日志(如 "From index N is out of range, fromNode output size N")并返回 ACLNN_ERR_INNER,不应进入 .at() 访问;
  • Link() 对非法参数应走错误码返回路径,而不是向调用方抛出 std::out_of_range 异常,避免进程级崩溃。

五、日志 / 截图(Related log / screenshot)

触发时的典型输出(进程崩溃场景):

terminate called after throwing an instance of 'std::out_of_range'
  what():  Index out of range

或上层 catch 到 std::out_of_range(what(): Index out of range)导致建链失败、图拓扑构建中断。

六、备注(Special notes for this issue)

  • 修复方案:将 kernel_graph_utils.cpp 中两处边界判断 > 改为 >=(from >= fromNodeOutputs.size()、to >= toNodeInputs.size()),并将错误日志文案更新为 "is out of range";
  • 回归测试建议:补充边界用例,覆盖 from == outputs.size() 与 to == inputs.size() 两种场景,断言返回 ACLNN_ERR_INNER 而非抛异常;
  • 关联修复 PR:https://gitcode.com/cann/opbase/pull/838
likedislike
ren-botaoren-botaoCollaborators
Sep 11 Added label: bug-report
ren-botao
ren-botaoCollaborators
Sep 11 Comments:

/assign @ren-botao

likedislike
CANN-robotCANN-robotCollaborators
Sep 11 Assign ren-botao as responsible person
CANN-robotCANN-robotCollaborators
Sep 14 Close issue
CANN-robotCANN-robotCollaborators
Sep 14 Added label: resolved
ren-botaoren-botaoCollaborators
Sep 15 Associated pull request