Closed
pyACL 字典整数参数的无符号窄化 #1352
KaranocaVeAdd on  19 days agoClosed at  16 hours ago
KaranocaVe
KaranocaVe
19 days ago Create

pyACL 字典整数参数的无符号窄化

现象

固定源码 cann/runtime@506b161234801758c070c873da1c5d6e07c663b8 的 pyACL 字典转换 helper 在将 Python 整数写入 unsigned char、unsigned short、unsigned int 及对应数组时,先把 PyLong_AsUnsignedLong 的结果静默转换为目标窄类型。64 位 Host 上,超出目标类型范围的值因此回绕后继续传给 ACL。

证据

  • python/pyACL/utils_methods.cpp 的三个无符号标量转换和两个无符号数组转换均在范围检查前执行窄化;基线探针传入 USHRT_MAX + 1 时 helper 返回成功,输出值已被截断。
  • 已合入的 int/short 有符号范围修复只覆盖对应有符号重载;本案针对仍未校验的无符号重载和数组路径,范围独立。
  • 同一探针针对 unsigned short、unsigned int、unsigned char 以及 hi_u8[]、unsigned int[] 的越界值,最终补丁均返回 ValueError。
  • 合法范围内的值仍按原路径转换;补丁只增加目标类型上界检查,不改变字段缺省处理或 ACL 调用签名。

影响

Python 配置值与底层 ACL 实际收到的值可能不一致。调用者无法从返回状态判断发生了截断,设备配置、媒体参数和保留字段等使用这些 helper 的接口都可能受到影响。

修复

保留 PyLong_AsUnsignedLong 的宽类型结果,在转换为目标类型前分别检查 UCHAR_MAX、USHRT_MAX 和 UINT_MAX;越界统一设置 ValueError 并拒绝调用者输入。新增 CPython helper 回归测试覆盖标量与数组路径。

验证

  • g++ -std=c++17 -Wall -Wextra -Werror -fsyntax-only 对 helper 和回归测试通过;clang-format --dry-run --Werror、git diff --check 通过。
  • git apply --check --whitespace=error-all fix.patch 在固定源码 checkout 上通过。
  • 未执行完整 runtime/pyACL 构建或真实 NPU 测试;本问题发生在 CPython 参数转换层,验证聚焦 Host helper,设备语义不在补丁范围内。

关联 PR: https://gitcode.com/cann/runtime/merge_requests/6199

likedislike
KaranocaVeKaranocaVe
19 days ago Associated pull request
KaranocaVeKaranocaVe
19 days ago Modify description of issue
duanpengliang
duanpengliangCollaborators
19 days ago Comments:

初步分析,pyACL 字典转换缺少目标无符号类型的上界检查。当整数超出目标类型范围、但仍处于 unsigned long 范围内时,转换函数会返回成功并传递截断后的值,造成配置值与底层参数不一致;已有 int/short 修复未覆盖这些路径。

该问题已有关联 PR:https://gitcode.com/cann/runtime/merge_requests/6199。后续需核对窄化前的范围检查及标量、数组边界回归,确保越界输入报错、合法值保持原值。

likedislike
duanpengliangduanpengliangCollaborators
19 days ago Assign KaranocaVe、xiao_cun as responsible person
wuqifanwuqifan
17 days ago Associated pull request
wuqifan
wuqifan
17 days ago Comments:

感谢反馈!已对关联 MR !6199 进行源码核查和修复前后验证,比较版本为 506b16123480 → 190d3877cc30。在 Linux aarch64、CANN 9.3、Python 3.9.9 环境下,分别完整构建两版 pyACL,通过真实 Python 接口验证了三种无符号标量和两种数组转换路径。基线会截断越界值并继续调用底层;MR 版本在所测路径上抛出 ValueError,且不再调用底层。每版 48 个用例均符合预期。此外,由于我们当前的测试用例暂未开源,请从本 MR 中移除新增的 tests/ut/pyACL/test_utils_unsigned_range.cpp,保留修复代码即可。感谢修复,我们会尽快推进合入!

likedislike
xiangyang0909xiangyang0909Collaborators
9 days ago Assign weixin_46600764 as responsible person
Hongyun ChenHongyun Chen
2 days ago Associated pull request
ykl999
ykl999Collaborators
16 hours ago Comments:

你好,pr已上库,相关pyacl的问题已修改,感谢贡献!

likedislike
ykl999ykl999Collaborators
16 hours ago changed custom state from 待办的 to 已解决
ykl999ykl999Collaborators
16 hours ago Close issue
CANN-robotCANN-robotCollaborators
13 hours ago Added label: resolved