Closed
[Bug-Report|缺陷反馈]: SplitIntoEqualByFactor 截断超出 int32 范围的分块数量 #510
tianjunAdd on  Oct 13Closed at  Oct 13
tianjun
tianjun
Oct 13 Create

Describe the current behavior / 问题描述

最新 master a33c711799bfec3fbb043bc3bc6bd9ab02d5649a 中,SplitIntoEqualByFactor 的 splitLen 是 int64_t,计算出的块数也是 int64_t,却未经范围检查写入 SplitResult.splitCount(int32_t),随后返回 true。

完整生产源码实测 splitLen=2147483648、factor=1,返回 success=1、splitCount=-2147483648、splitFactor=1、splitTailFactor=1。成功状态携带负分块数量,无法描述输入长度的有效划分。这里仅证实 Host API 的结果错误,不据此声称已观察到 NPU 或模型故障。

Environment / 环境信息

DevEnvC_mmdv8,Ubuntu 20.04 aarch64 / Linux 5.10,GCC 9.4.0,真实系统 GoogleTest 1.10.0 源码。完整生产 math_util.cpp、公共头、原生完整 test_util.cpp 实测,没有 mock、stub、AST 抽取或被测实现替代。仅 Host,未进行完整 CANN 产品构建/全部产品 UT、SDK 链接、驱动或 NPU 测试,不声明版本兼容。

仓库 docs/zh/api/op_common/reserved_interface_op_common.md 将此函数列为预留接口,并明确不建议开发者使用。本问题修复现有实现的输出范围一致性,不将其描述为已证明影响推荐应用入口。

Steps to reproduce / 重现步骤

保存为 /tmp/split_factor.cpp,在上述 master 工作树执行:

#include "op_common/op_host/util/math_util.h"
#include <cstdint>
#include <iostream>
#include <limits>
int main()
{
    Ops::Base::SplitResult result;
    const int64_t length = int64_t{std::numeric_limits<int32_t>::max()} + 1;
    const bool success = Ops::Base::SplitIntoEqualByFactor(length, 1, result);
    std::cout << "success=" << success << " length=" << length
              << " count=" << result.splitCount << " factor=" << result.splitFactor
              << " tail=" << result.splitTailFactor << "\n";
    return !success ? 0 : 1;
}
g++ -std=c++17 -O0 -g -Wall -Wextra -Werror -Iinclude src/op_common/op_host/util/math_util.cpp /tmp/split_factor.cpp -o /tmp/split_factor
/tmp/split_factor

原版输出 success=1 length=2147483648 count=-2147483648 factor=1 tail=1,退出 1;修复后拒绝不能由结果类型表示的请求,success=0,退出 0。

Describe the expected behavior / 预期结果

只有可用 SplitResult 表示的分块才能返回成功。先在 int64_t 中计算完整份数,超过 int32_t 最大值时返回 false;检查通过后再显式转换,保留合法请求的因子、尾块、单块语义及现有 ABI。拒绝时保持调用者已有输出,与原有非正输入拒绝路径一致。

Self-test commands / 自测命令

g++ -std=c++17 -O0 -g -fPIE -Wall -Wextra -Werror -pthread -Iinclude -I/usr/src/googletest/googletest/include -I/usr/src/googletest/googletest tests/op_common/ut/test_util.cpp src/op_common/op_host/util/math_util.cpp /usr/src/googletest/googletest/src/gtest-all.cc /usr/src/googletest/googletest/src/gtest_main.cc -o /tmp/split_factor_ut
/tmp/split_factor_ut
# Repeat with -fsanitize=undefined -fno-sanitize-recover=undefined for UBSan.
pre-commit run --files src/op_common/op_host/util/math_util.cpp tests/CMakeLists.txt tests/op_common/ut/test_util.cpp

Results / 自测结果

  • 同一份最终完整 test_util.cpp:原版 4 failed / 14 passed,修复及当前提交 18 passed / 0 disabled / 0 errors。
  • 4 个原版失败均是计数超出 int32 的成功返回:超过上界 1、ceil 舍入越界、大 factor 的边界及 int64 最大长度。
  • 对照包含 int32 最大可表示份数、小长度/大 factor、尾块、整除、非正输入,及原有 4 项数学 UT。有效分块重新计算覆盖长度,未分配对应巨大数组。
  • 同一 18 项 UBSan(-fsanitize=undefined -fno-sanitize-recover=undefined)通过,无运行时诊断。这里的修复前缺陷是 int64 转 int32 结果截断,未将其错误表述为 UBSan 已检测到的未定义行为。
  • 原始 clang-format 16.0.0、pre-commit 和实际 commit hook 通过;原始 OAT 实扫 3 文件,Invalid File Type 0 / License Header Invalid 0,未修改版权、许可证或门禁。git diff --check 通过。
  • 在 ENABLE_UT 中将完整原有 math_util.cpp 加入 op_common_utest,确保原生 UT 链接非 inline API;未添加替身或更改正式产品链接。远端完整 CI 以当前 SHA 的实际结果为准。

Duplicate check / 重复核查

已核查全部作者、全部状态的 492 条 Issue、1006 条 PR。已排除 IsFloatEqual #467/PR #992、CeilDiv #329/PR #773、本轮 MurmurHash #507/PR #1005、BF16/FP8 转换问题,以及 broadcast #449/PR #930 的另一文件除零根因;无既有 SplitIntoEqualByFactor 的 int32 计数范围修复。

likedislike
tianjun
tianjun
Oct 13 Comments:

/assign @Ethan_Zou

likedislike
CANN-robotCANN-robotCollaborators
Oct 13 Assign Ethan_Zou as responsible person
tianjuntianjun
Oct 13 Associated pull request
CANN-robotCANN-robotCollaborators
Oct 13 Close issue
CANN-robotCANN-robotCollaborators
Oct 13 Added label: resolved