Closed
[Bug-Report|缺陷反馈]: PyACL Allocator 回调的 size_t 参数截断 #1251
KaranocaVeAdd on  Oct 22Closed at  24 days ago
KaranocaVe
KaranocaVe
Oct 22 Create

[Bug-Report|缺陷反馈]: PyACL Allocator 回调的 size_t 参数截断

现象

AllocatorAllocFunc 和 AllocatorAllocAdviseFunc 都接收 size_t size,但传给 Python 回调时分别使用 Py_BuildValue("kI", ...) 和 Py_BuildValue("kIk", ...)。格式项 I 只读取 unsigned int。在 64 位 Host 上,请求 4 GiB 时回调收到 0;请求 4 KiB 时仍收到 4096。Python 回调因此无法按完整申请大小处理请求。
预期是两处回调都收到完整的 size_t 大小;实际值在 UINT32_MAX 之后被截为低 32 位。问题范围仅限传给 Python 的大小参数,不据此推断设备内存越界。

修复与验证

将两处格式项改为 K,并显式转换为 unsigned long long。Host CPython 3.11 C-API 对照中,4096 保持为 4096,4 GiB 从旧格式的 0 变为完整的 4294967296。

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

likedislike
KaranocaVeKaranocaVe
Oct 22 Associated pull request
KaranocaVeKaranocaVe
Oct 22 Modify description of issue
wuqifan
wuqifan
Oct 22 Comments:

/assign

likedislike
CANN-robotCANN-robotCollaborators
Oct 22 Assign weixin_46600764 as responsible person
wuqifan
wuqifan
Oct 22 Comments:

感谢反馈!根据提供的信息核实:PyACL 注册的 Allocator 分配回调在把申请大小传给 Python 时用了 "I",在 64 位 CPython 下,该格式项按 unsigned int 读取,超过 4 GiB 的申请会被截断;回调收到的就不是真实请求大小,无法按实际需求分配。该参数应按与 size_t 等宽的整数类型传递。我们正在复现该问题,修复会在通过测试后合入。

likedislike
KaranocaVe
KaranocaVe
Oct 22 Comments:

感谢跟进。对应修复已在 PR !5802 提交,两处 Allocator 回调都改为使用 K 格式项传递完整大小;当前 head 85b6953 的 CI 已通过。若复现发现额外调用路径或需要补充的测试,请直接在 PR 中指出。

likedislike
wuqifan
wuqifan
26 days ago Comments:

感谢反馈!已对关联修复 !5802 进行核查和对照验证。问题原因是两个 Allocator 分配回调将 size_t 大小通过 Py_BuildValue 的 I 格式项传给 Python,导致本次验证的 64 位环境中大于 32 位范围的大小被截断。MR 将两处格式项改为 K,并显式转换为 unsigned long long。
在 Host CPython 3.14.6 的回调对照中,两个回调的 4 KiB 和 UINT32_MAX 输入在修复前后均保持正确;4 GiB 在修复前收到 0、修复后收到 4294967296;4 GiB + 1 在修复前收到 1、修复后收到 4294967297。本次验证使用了从对应版本源码提取的回调函数,尚未覆盖完整 pyACL 构建和设备端分配流程。当前回调桥接还会丢弃 Python 返回对象并返回空指针,对应已经存在的issue:https://gitcode.com/cann/runtime/issues/1135 ,我们也会推进另一个issue的解决。我们会尽快推进合入,感谢您的修复和跟进!

likedislike
wuqifanwuqifan
26 days ago Associated pull request
ykl999ykl999Collaborators
24 days ago changed custom state from 进行中 to 已解决
ykl999ykl999Collaborators
24 days ago Close issue
ykl999
ykl999Collaborators
24 days ago Comments:

pr已合入,issue关闭处理

likedislike
CANN-robotCANN-robotCollaborators
24 days ago Added label: resolved