From f91a1cf1151c53a506310fafae83c93c520cf6d5 Mon Sep 17 00:00:00 2001 From: zhuyinlin <1085905529@qq.com> Date: Mon, 19 May 2025 16:06:45 +0800 Subject: [PATCH 1/4] check deviceId in hasCache Signed-off-by: zhuyinlin <1085905529@qq.com> --- .../neural_network_runtime.cpp | 45 ++++++++++++++++--- 1 file changed, 39 insertions(+), 6 deletions(-) diff --git a/frameworks/native/neural_network_runtime/neural_network_runtime.cpp b/frameworks/native/neural_network_runtime/neural_network_runtime.cpp index 2cb6e1d..9f248f7 100644 --- a/frameworks/native/neural_network_runtime/neural_network_runtime.cpp +++ b/frameworks/native/neural_network_runtime/neural_network_runtime.cpp @@ -46,7 +46,8 @@ const std::string EXTENSION_KEY_FM_SHARED = "NPU_FM_SHARED"; const std::string EXTENSION_KEY_IS_EXCEED_RAMLIMIT = "isExceedRamLimit"; const std::string NULL_HARDWARE_NAME = "default"; -const std::string HARDWARE_NAME = "const.ai.nnrt_deivce"; +const std::string NNRT_DEVICE_NAME = "const.ai.nnrt_deivce"; +const std::string HARDWARE_NAME = "ohos.boot.hardware"; const std::string HARDWARE_VERSION = "v5_0"; constexpr size_t HARDWARE_NAME_MAX_LENGTH = 128; constexpr size_t FILE_NUMBER_MAX = 100; // 限制cache文件数量最大为100 @@ -565,7 +566,7 @@ NNRT_API OH_NN_ReturnCode OH_NNModel_BuildFromLiteGraph(OH_NNModel *model, const } namespace { -OH_NN_ReturnCode CheckCacheFileExtension(const std::string& content, int64_t& fileNumber, int64_t& cacheVersion) +OH_NN_ReturnCode CheckCacheFileExtension(const std::string& content, int64_t& fileNumber, int64_t& cacheVersion, int64_t& deviceId) { if (!nlohmann::json::accept(content)) { LOGE("OH_NNModel_HasCache CheckCacheFile JSON parse error"); @@ -578,6 +579,12 @@ OH_NN_ReturnCode CheckCacheFileExtension(const std::string& content, int64_t& fi return OH_NN_INVALID_FILE; } + if (j["data"].find("deviceId") == j["data"].end()) { + LOGE("OH_NNModel_HasCache read deviceId from cache info file failed."); + return OH_NN_INVALID_FILE; + } + deviceId = j["data"]["deviceId"].get(); + if (j["data"].find("fileNumber") == j["data"].end()) { LOGE("OH_NNModel_HasCache read fileNumber from cache info file failed."); return OH_NN_INVALID_FILE; @@ -610,7 +617,7 @@ OH_NN_ReturnCode CheckCacheFileExtension(const std::string& content, int64_t& fi return OH_NN_SUCCESS; } -OH_NN_ReturnCode CheckCacheFile(const std::string& cacheInfoPath, int64_t& fileNumber, int64_t& cacheVersion) +OH_NN_ReturnCode CheckCacheFile(const std::string& cacheInfoPath, int64_t& fileNumber, int64_t& cacheVersion, int64_t& deviceId) { char path[PATH_MAX]; if (realpath(cacheInfoPath.c_str(), path) == nullptr) { @@ -633,7 +640,25 @@ OH_NN_ReturnCode CheckCacheFile(const std::string& cacheInfoPath, int64_t& fileN std::string content((std::istreambuf_iterator(ifs)), std::istreambuf_iterator()); ifs.close(); - return CheckCacheFileExtension(content, fileNumber, cacheVersion); + return CheckCacheFileExtension(content, fileNumber, cacheVersion, deviceId); +} + +OH_NN_ReturnCode CheckDeviceId(nt64_t& deviceId) { + std::string deviceName; + char cName[HARDWARE_NAME_MAX_LENGTH]; + int ret = GetParameter(HARDWARE_NAME.c_str(), NULL_HARDWARE_NAME.c_str(), cName, HARDWARE_NAME_MAX_LENGTH); + if (ret <= 0) { + LOGE("OH_NNModel_HasCache failed to get parameter."); + return OH_NN_FAILED; + } + + deviceName = HARDWARE_NAME + "." +cName; + if (deviceId != std::hash{}(deviceName)) { + LOGE("OH_NNModel_HasCache the deviceID in the cache files is different from current deviceID."); + return OH_NN_FAILED; + } + + return OH_NN_SUCCESS; } } @@ -657,15 +682,23 @@ NNRT_API bool OH_NNModel_HasCache(const char *cacheDir, const char *modelName, u return false; } + int64_t deviceId{0}; int64_t fileNumber{0}; int64_t cacheVersion{0}; - OH_NN_ReturnCode returnCode = CheckCacheFile(cacheInfoPath, fileNumber, cacheVersion); + OH_NN_ReturnCode returnCode = CheckCacheFile(cacheInfoPath, fileNumber, cacheVersion, deviceId); if (returnCode != OH_NN_SUCCESS) { LOGE("OH_NNModel_HasCache get fileNumber or cacheVersion fail."); std::filesystem::remove_all(cacheInfoPath); return false; } + returnCode = CheckDeviceId(deviceId); + if (returnCode != OH_NN_SUCCESS) { + LOGE("OH_NNModel_HasCache check deviceId fail."); + std::filesystem::remove_all(cacheInfoPath); + return false; + } + if (fileNumber <= 0 || fileNumber > FILE_NUMBER_MAX) { LOGE("OH_NNModel_HasCache fileNumber is invalid or more than 100"); std::filesystem::remove_all(cacheInfoPath); @@ -804,7 +837,7 @@ NNRT_API OH_NN_ReturnCode OH_NN_GetDeviceID(char *nnrtDevice, size_t len) } char cName[HARDWARE_NAME_MAX_LENGTH] = {0}; - int ret = GetParameter(HARDWARE_NAME.c_str(), NULL_HARDWARE_NAME.c_str(), cName, HARDWARE_NAME_MAX_LENGTH); + int ret = GetParameter(NNRT_DEVICE_NAME.c_str(), NULL_HARDWARE_NAME.c_str(), cName, HARDWARE_NAME_MAX_LENGTH); // 如果成功获取返回值为硬件名称的字节数 if (ret <= 0) { LOGE("GetNNRtDeviceName failed, failed to get parameter."); -- Gitee From d24b15e829c115f426f8ae53f46116966e02dae6 Mon Sep 17 00:00:00 2001 From: zhuyinlin <1085905529@qq.com> Date: Mon, 19 May 2025 08:26:42 +0000 Subject: [PATCH 2/4] update frameworks/native/neural_network_runtime/neural_network_runtime.cpp. Signed-off-by: zhuyinlin <1085905529@qq.com> --- .../native/neural_network_runtime/neural_network_runtime.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/frameworks/native/neural_network_runtime/neural_network_runtime.cpp b/frameworks/native/neural_network_runtime/neural_network_runtime.cpp index 9f248f7..8a28e98 100644 --- a/frameworks/native/neural_network_runtime/neural_network_runtime.cpp +++ b/frameworks/native/neural_network_runtime/neural_network_runtime.cpp @@ -643,7 +643,8 @@ OH_NN_ReturnCode CheckCacheFile(const std::string& cacheInfoPath, int64_t& fileN return CheckCacheFileExtension(content, fileNumber, cacheVersion, deviceId); } -OH_NN_ReturnCode CheckDeviceId(nt64_t& deviceId) { +OH_NN_ReturnCode CheckDeviceId(nt64_t& deviceId) +{ std::string deviceName; char cName[HARDWARE_NAME_MAX_LENGTH]; int ret = GetParameter(HARDWARE_NAME.c_str(), NULL_HARDWARE_NAME.c_str(), cName, HARDWARE_NAME_MAX_LENGTH); @@ -654,7 +655,7 @@ OH_NN_ReturnCode CheckDeviceId(nt64_t& deviceId) { deviceName = HARDWARE_NAME + "." +cName; if (deviceId != std::hash{}(deviceName)) { - LOGE("OH_NNModel_HasCache the deviceID in the cache files is different from current deviceID."); + LOGE("OH_NNModel_HasCache the deviceId in the cache files is different from current deviceID."); return OH_NN_FAILED; } -- Gitee From 38bf3a5a327a34f2f894672e139fec80e75ceb54 Mon Sep 17 00:00:00 2001 From: zhuyinlin <1085905529@qq.com> Date: Mon, 19 May 2025 08:29:37 +0000 Subject: [PATCH 3/4] update frameworks/native/neural_network_runtime/neural_network_runtime.cpp. Signed-off-by: zhuyinlin <1085905529@qq.com> --- .../native/neural_network_runtime/neural_network_runtime.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frameworks/native/neural_network_runtime/neural_network_runtime.cpp b/frameworks/native/neural_network_runtime/neural_network_runtime.cpp index 8a28e98..48d1ce6 100644 --- a/frameworks/native/neural_network_runtime/neural_network_runtime.cpp +++ b/frameworks/native/neural_network_runtime/neural_network_runtime.cpp @@ -643,7 +643,7 @@ OH_NN_ReturnCode CheckCacheFile(const std::string& cacheInfoPath, int64_t& fileN return CheckCacheFileExtension(content, fileNumber, cacheVersion, deviceId); } -OH_NN_ReturnCode CheckDeviceId(nt64_t& deviceId) +OH_NN_ReturnCode CheckDeviceId(int64_t& deviceId) { std::string deviceName; char cName[HARDWARE_NAME_MAX_LENGTH]; -- Gitee From 5c323c8933f4ce41e4d34d048f9a3e530ad0381d Mon Sep 17 00:00:00 2001 From: zhuyinlin <1085905529@qq.com> Date: Mon, 19 May 2025 08:35:14 +0000 Subject: [PATCH 4/4] update frameworks/native/neural_network_runtime/neural_network_runtime.cpp. Signed-off-by: zhuyinlin <1085905529@qq.com> --- .../native/neural_network_runtime/neural_network_runtime.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frameworks/native/neural_network_runtime/neural_network_runtime.cpp b/frameworks/native/neural_network_runtime/neural_network_runtime.cpp index 48d1ce6..39f79d2 100644 --- a/frameworks/native/neural_network_runtime/neural_network_runtime.cpp +++ b/frameworks/native/neural_network_runtime/neural_network_runtime.cpp @@ -653,9 +653,9 @@ OH_NN_ReturnCode CheckDeviceId(int64_t& deviceId) return OH_NN_FAILED; } - deviceName = HARDWARE_NAME + "." +cName; + deviceName = HARDWARE_NAME + "." + cName; if (deviceId != std::hash{}(deviceName)) { - LOGE("OH_NNModel_HasCache the deviceId in the cache files is different from current deviceID."); + LOGE("OH_NNModel_HasCache the deviceId in the cache files is different from current deviceId."); return OH_NN_FAILED; } -- Gitee