Pull Request已成功合入, 合并人@ascend-robot
(感谢 rich 的贡献)变更摘要
此 PR 对 reselect_static_kernel_with_path 接口的路径参数进行了强化校验,主要将原有的 stat() 直接检查改为先通过 realpath() 解析为绝对路径后再进行目录合法性检查,同时补充了类型校验、空字节校验等边界测试用例,并对 OpInterface.cpp 中的函数指针校验逻辑做了小幅重构。
主要改动
- 路径解析加强(
Module.cpp): 引入realpath()将用户传入的路径解析为绝对路径abs_path,随后基于绝对路径执行stat()和S_ISDIR检查,并将解析后的路径传递给后续的ReselectStaticKernelWithPath调用,替代了原先直接使用原始路径的做法;错误信息从"path does not exist"更新为"failed to resolve path"。 - 函数指针校验逻辑调整(
OpInterface.cpp): 将aclnnReselectStaticKernelWithPathFunc的空指针TORCH_CHECK从if块外部移至内部,使其仅在首次加载函数指针失败时触发校验,避免每次调用都执行冗余检查。 - 接口注释简化(
OpInterface.h): 删除了ReselectStaticKernelWithPath文档注释中 "it need to be called once at process" 的说明。 - 新增边界测试用例(
test_npu_static_kernel.py): 新增 4 个测试用例,覆盖非字符串类型参数、路径含空字节、路径不存在、以及路径为普通文件而非目录的异常场景,均验证抛出RuntimeError。


代码审查
经过对所有变更文件的逐一审查,我的发现如下。
审查总结
已审查文件(4/4):
| 文件 | 审查结果 |
|---|---|
test/_inductor/test_npu_static_kernel.py |
无问题 — 4 个新增测试用例覆盖了非字符串、空字节、不存在的路径、文件路径四种边界输入,与 C++ 侧的校验逻辑对应正确 |
torch_npu/csrc/core/npu/interface/OpInterface.cpp |
无问题 — TORCH_CHECK 移入 if 块是合理的懒加载优化,与静态函数指针只加载一次的语义一致 |
torch_npu/csrc/core/npu/interface/OpInterface.h |
无问题 — 仅移除一行冗余/不准确的注释 |
torch_npu/csrc/npu/Module.cpp |
1 个 P3(低置信度),见下文 |
发现统计:
- P0: 0
- P1: 0
- P2: 0
- P3: 1
整体风险评估:低风险。 此次变更的核心改动是引入 realpath() 进行路径规范化,这是一个安全加固措施(解析符号链接、消除 .. 穿越),逻辑正确且与项目中其他文件的 realpath() 使用模式一致。测试覆盖充分。唯一发现是一个极低概率的 TOCTOU 竞态场景下错误消息不够精确的轻微问题,不影响功能正确性。
⚠️ 已识别出整体风险,但无法提取行内评论,请参考整体评估。


Thanks for your pull-request.
The full list of commands accepted by me can be found at here。
You can get sig-info at here
PR Approval Progress
✅ Congratulations! All modules have met the lgtm and approve requirements.
Module Approval Details
| module | lgtm status | approve status |
|---|---|---|
| repo-Ascend/pytorch | ✅ liujunzhu, wjlflyer (2/2) | ✅ wjlflyer, liujunzhu (2/1) |
| test | ✅ liujunzhu, wjlflyer (2/2) | ✅ liujunzhu, wjlflyer (2/1) |
💡 Tip:
- Committer can comment
/approveor/lgtm- Commenting
/approveimplies both code review (lgtm) and intent to merge (approve)
CLA Signature Pass
rich9527, thanks for your pull request. All authors of the commits have signed the CLA. 👍


当前仓库存在以下 保护分支 :
| Protected Branch | Version | Release |
|---|---|---|
| master | ||
| v2.9.0 | ||
| v2.12.0 | ||
| v2.11.0 | ||
| v2.10.0 | ||
| v2.7.1 | ||
| v2.7.1-26.1.0 | ||
| v2.10.0-26.1.0 | ||
| v2.12.0-26.1.0 | ||
| v2.11.0-26.1.0 | ||
| v2.9.0-26.1.0 | ||
| ci-test |
评论 /sync <branch1> <branch2> ... 可将当前 PR 修改同步到其它分支(创建同步 PR):
a) 如果当前 PR 是 Open 状态,同步操作将延迟到 PR 被合并时执行
b) 如果当前 PR 已经 Merged,将立即执行同步操作
注意:
- /sync 命令可以指定同步到多个分支,仅最后一个 /sync 命令生效
- 如果创建的同步 PR 不正确,可通过向同步 PR 的源分支提交轻量级 PR 完善,或使用 /close 命令关闭


ascend docs pipeline is running...


✅ 跳过 docs ci 检查,没有需要检查的文档文件


| 阶段 | 任务名 | 状态 | 详情 |
|---|---|---|---|
| 编译构建 | Build_X86 | ✅ | >>> |
| Build_ARM | ✅ | >>> | |
| Build_LibTorch_x86 | ✅ | >>> | |
| Build_LibTorch_ARM | ✅ | >>> | |
| Build_X86_torchair | 🛑 | >>> | |
| Build_ARM_torchair | 🛑 | >>> | |
| patch_test | 🛑 | >>> | |
| 恶意代码检查 | Antipoison | ✅ | >>> |
| 编码安全与规范检查 | CodeCheck | ✅ | >>> |
| check_error | ✅ | >>> | |
| CodeCheck_lintrunner | ✅ | >>> | |
| 开源片段检查 | SCA | ✅ | >>> |
| 开发者测试 | UT_X86_Part_01 | 🛑 | >>> |
| UT_X86_Part_02 | 🛑 | >>> | |
| UT_ARM_A3_Part_01 | 🛑 | >>> | |
| UT_ARM_A3_Part_02 | 🛑 | >>> | |
| UT_ARM_A2_Part_01 | ✅ | >>> | |
| UT_ARM_A2_Part_02 | ✅ | >>> | |
| UT_ARM_A2_Part_03 | ✅ | >>> | |
| UT_inductor_Part_01 | 🛑 | >>> | |
| UT_inductor_Part_02 | 🛑 | >>> | |
| UT_inductor_Part_03 | 🛑 | >>> | |
| UT_inductor_Part_04 | 🛑 | >>> | |
| UT_DIST_ARM_Part_01 | 🛑 | >>> | |
| UT_DIST_ARM_Part_02 | 🛑 | >>> | |
| UT_DIST_ARM_Part_03 | 🛑 | >>> | |
| UT_DIST_ARM_Part_04 | 🛑 | >>> | |
| UT_ARM_A2_Select_Part_01 | ✅ | >>> | |
| UT_ARM_A2_Select_Part_02 | ✅ | >>> | |
| 流水线 | PR-pipeline_pytorch | ✅ | >>> |
- compile、compile_inductor、compile_torchair : 运行流水线
- retry : 重试流水线所有失败子任务
- retry <任务名> : 仅重试指定失败子任务
- stop : 停止流水线


ascend docs pipeline is running...


✅ 跳过 docs ci 检查,没有需要检查的文档文件


compile


ascend docs pipeline is running...


✅ 跳过 docs ci 检查,没有需要检查的文档文件


| 阶段 | 任务名 | 状态 | 详情 |
|---|---|---|---|
| 编译构建 | Build_X86 | ✅ | >>> |
| Build_ARM | ✅ | >>> | |
| Build_LibTorch_x86 | ✅ | >>> | |
| Build_LibTorch_ARM | ✅ | >>> | |
| Build_X86_torchair | 🛑 | >>> | |
| Build_ARM_torchair | 🛑 | >>> | |
| patch_test | 🛑 | >>> | |
| 恶意代码检查 | Antipoison | ✅ | >>> |
| 编码安全与规范检查 | CodeCheck | ✅ | >>> |
| check_error | ✅ | >>> | |
| CodeCheck_lintrunner | ✅ | >>> | |
| 开源片段检查 | SCA | ✅ | >>> |
| 开发者测试 | UT_X86_Part_01 | 🛑 | >>> |
| UT_X86_Part_02 | 🛑 | >>> | |
| UT_ARM_A3_Part_01 | 🛑 | >>> | |
| UT_ARM_A3_Part_02 | 🛑 | >>> | |
| UT_ARM_A2_Part_01 | ✅ | >>> | |
| UT_ARM_A2_Part_02 | ✅ | >>> | |
| UT_ARM_A2_Part_03 | ✅ | >>> | |
| UT_inductor_Part_01 | 🛑 | >>> | |
| UT_inductor_Part_02 | 🛑 | >>> | |
| UT_inductor_Part_03 | 🛑 | >>> | |
| UT_inductor_Part_04 | 🛑 | >>> | |
| UT_DIST_ARM_Part_01 | 🛑 | >>> | |
| UT_DIST_ARM_Part_02 | 🛑 | >>> | |
| UT_DIST_ARM_Part_03 | 🛑 | >>> | |
| UT_DIST_ARM_Part_04 | 🛑 | >>> | |
| UT_ARM_A2_Select_Part_01 | ✅ | >>> | |
| UT_ARM_A2_Select_Part_02 | ✅ | >>> | |
| 流水线 | PR-pipeline_pytorch | ✅ | >>> |
- compile、compile_inductor、compile_torchair : 运行流水线
- retry : 重试流水线所有失败子任务
- retry <任务名> : 仅重试指定失败子任务
- stop : 停止流水线


/approve


【合入来源】
https://gitcode.com/Ascend/pytorch/issues/2634
https://gitcode.com/Ascend/pytorch/pull/40386
【修改方案】
1.路径规范化与解析增强: 在 Module.cpp 中,使用 realpath() 将用户输入的路径解析为绝对路径 abs_path,再基于 abs_path 进行 stat 存在性检查和 S_ISDIR 目录类型判断,并将解析后的路径传入后续的 lambda 和 API 调用,防止原始路径中的符号链接或相对路径绕过校验。
2.新增边界条件单元测试: 在 test_npu_static_kernel.py 中新增 4 个测试用例,分别覆盖路径非字符串、路径含空字节、路径不存在、路径为文件而非目录的场景,验证各类非法输入均抛出 RuntimeError。
3.函数指针判空逻辑调整: 在 OpInterface.cpp 的 ReselectStaticKernelWithPath 中,将 TORCH_CHECK 判空检查移入 if 分支内部,仅在函数指针未初始化时才执行校验,避免重复检查。
4.接口注释修正: 在 OpInterface.h 中移除了 ReselectStaticKernelWithPath 文档注释中“it need to be called once at process”的过时描述。
5.测试文件存量lint检查修正:去除未使用的import,修改一个重复定义bug。
【资料变更】
不涉及
【接口变更】
不涉及
【功能验证】
新增UT补充
(zrq_310_29) root@xxx:xxx# python /xxx/torch_npu/test/_inductor/test_npu_static_kernel.py /xxx/lib/python3.10/site-packages/torch_npu/_inductor/__init__.py:97: UserWarning: triton-ascend is not installed, install it first. warnings.warn("triton-ascend is not installed, install it first.") .......... ---------------------------------------------------------------------- Ran 10 tests in 0.593s OK【CheckList】