已关闭
【缺陷报告】空指针解引用 - 文件PvModelFactory.cpp - 函数PvModelFactory::CreateDyn - 行号34 #3011
zhangjunkai9创建于  8月18日关闭于  8月18日
zhangjunkai9
8月18日 创建

缺陷信息

缺陷类型nullptrDeref (空指针解引用)
函数PvModelFactory::CreateDyn
文件framework/src/cost_model/simulation/pv/PvModelFactory.cpp
行号34

缺陷描述

PvModelFactory::CreateDyn 函数在第31行通过 dlsym(handle, "CreateDynPvModelImpl") 获取函数指针 createFunc,但未对其返回值做 NULL 校验,第34行直接以 createFunc() 形式调用。dlsym 在符号未找到时返回 NULL,此时调用空函数指针导致未定义行为(崩溃)。dlopen 的返回值 handle 已在第24行做了非空检查,但 dlsym 的返回值 createFunc 未做任何校验。该函数通过 Python 绑定入口经 CostModelLauncher::RunPvModel 可达(entry_callchain 深度7),实际触发概率高。

数据流证据

Source(问题源头)

framework/src/cost_model/simulation/pv/PvModelFactory.cpp:23 行 void* handle = dlopen(soPath.c_str(), RTLD_LAZY) dlopen 加载动态库 libtile_fwk_simulation_pv.so,handle 已做非空检查(第24行)

Sink(问题爆发点)

framework/src/cost_model/simulation/pv/PvModelFactory.cpp:34 行 return createFunc() createFunc 可能为 NULL(dlsym 符号未找到时),调用空函数指针导致未定义行为

传播路径:

# 文件 行号 说明
1 framework/src/cost_model/simulation/pv/PvModelFactory.cpp 23 void* handle = dlopen(soPath.c_str(), RTLD_LAZY) dlopen 加载动态库,handle 非空检查在第24行通过(throw 异常保护 dlopen 失败路径)
2 framework/src/cost_model/simulation/pv/PvModelFactory.cpp 31 auto createFunc = (CreateFunc)(dlsym(handle, funcName.c_str())) dlsym 查找符号 CreateDynPvModelImpl,若符号不存在则返回 NULL,createFunc 被赋值为可能的 NULL 函数指针,无任何 NULL 校验
3 framework/src/cost_model/simulation/pv/PvModelFactory.cpp 34 return createFunc() 直接调用未校验的 createFunc 函数指针,若为 NULL 则调用空函数指针(sink)
4 framework/src/cost_model/simulation/cost_model_launcher.h 272 pv_ = CostModel::PvModelFactory::CreateDyn() RunPvModel 函数调用 CreateDyn,证明该缺陷函数从 Python 绑定入口可达(entry_callchain 深度7)

调用链

可达调用链1 起点:(entry functions) → 终点:CostModel::PvModelFactory::CreateDyn 深度:7

# 文件 函数 函数起始行 调用点行
1 python/src/pybind11.cpp:pypto: PYBIND11_MODULE 24 39
2 python/src/bindings/cost_model.cpp:pypto: BindCostModelRuntime 99 101
3 python/src/bindings/cost_model.cpp:pypto: CostModelRunOnceDataFromHost 82 94
4 framework/src/cost_model/simulation/cost_model_launcher.h:npu::tile_fwk::dynamic::CostModelLauncher: CostModelRunOnce 169 174
5 framework/src/cost_model/simulation/cost_model_launcher.h:npu::tile_fwk::dynamic::CostModelLauncher: RunDynamic 189 196
6 framework/src/cost_model/simulation/cost_model_launcher.h:npu::tile_fwk::dynamic::CostModelLauncher: RunModel 201 221
7 framework/src/cost_model/simulation/cost_model_launcher.h:npu::tile_fwk::dynamic::CostModelLauncher: RunPvModel 262 272
8 framework/src/cost_model/simulation/pv/PvModelFactory.cpp:CostModel::PvModelFactory: CreateDyn 20 —

修复建议

    auto createFunc = (CreateFunc)(dlsym(handle, funcName.c_str()));
    if (createFunc == nullptr) {
        const char* err = dlerror();
        throw std::runtime_error(std::string("can not find symbol: ") + funcName + ": " + (err ? err : "unknown"));
    }

    // 创建对象并返回
    return createFunc();
likedislike
杨旭
杨旭成员
8月18日 评论:

感谢参与 PyPTO 生态~,相关问题涉及具体模块代码实现细节,我将联系具体模块负责人,进行进一步确认处理~

likedislike
杨旭杨旭成员
8月18日 将 zhiweiyuan 设为负责人
杨旭
杨旭成员
8月18日 评论:

@zhiweiyuan 请协助看下对应实现,是否确实有问题,是否需要修改。

likedislike
杨旭杨旭成员
8月18日 添加了label:Simulation
杨旭杨旭成员
8月18日 issue类型由 任务 改变为 缺陷
zhiweiyuan
zhiweiyuan成员
8月18日 评论:

已解决,感谢您的问题反馈

likedislike
zhiweiyuanzhiweiyuan成员
8月18日 issue状态由 待办的 改变为 已完成
zhiweiyuanzhiweiyuan成员
8月18日 关闭了 issue
CANN-robotCANN-robot成员
8月18日 添加了label:resolved