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


Sep 11 Assign ren-botao as responsible person
Sep 14 Close issue
Sep 14 Added label: resolved
Sep 15 Associated pull request
一、问题描述(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)
KernelNode节点 A、B,其中 A 含 N 个输出、B 含 M 个输入;KernelGraphUtils::Link(A, B, N, 0),即from = N(恰好等于 A 的输出数量);from > fromNodeOutputs.size()(即N > N)判断为 false,未拦截;fromNodeOutputs.at(N),SmallVector::at()检测index >= size_成立,抛出std::out_of_range;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)
触发时的典型输出(进程崩溃场景):
或上层 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而非抛异常;