From e3d9a0f911f5fd860fcdf13df2ff5d2f5163915a Mon Sep 17 00:00:00 2001 From: w30052974 Date: Fri, 27 Sep 2024 13:54:12 +0800 Subject: [PATCH 1/2] =?UTF-8?q?nnrt=20=E5=91=8A=E8=AD=A6=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=20ut=20&=20file=20realpath?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: w30052974 --- .../neural_network_runtime/nncompiled_cache.cpp | 17 +++++++++++------ .../nn_compiled_cache_test.cpp | 13 ------------- 2 files changed, 11 insertions(+), 19 deletions(-) diff --git a/frameworks/native/neural_network_runtime/nncompiled_cache.cpp b/frameworks/native/neural_network_runtime/nncompiled_cache.cpp index 6917b6e..a98bcc6 100644 --- a/frameworks/native/neural_network_runtime/nncompiled_cache.cpp +++ b/frameworks/native/neural_network_runtime/nncompiled_cache.cpp @@ -111,11 +111,6 @@ OH_NN_ReturnCode NNCompiledCache::Restore(const std::string& cacheDir, for (uint32_t i = 0; i < cacheInfo.fileNumber; ++i) { std::string cacheModelPath = cacheDir + "/" + m_modelName + std::to_string(i) + ".nncache"; - if (access(cacheModelPath.c_str(), 0) != 0) { - LOGE("[NNCompiledCache] Restore failed, %{public}s is not exist.", cacheModelPath.c_str()); - return OH_NN_INVALID_PARAMETER; - } - OHOS::NeuralNetworkRuntime::Buffer modelBuffer; ret = ReadCacheModelFile(cacheModelPath, modelBuffer); if (ret != OH_NN_SUCCESS) { @@ -343,7 +338,17 @@ OH_NN_ReturnCode NNCompiledCache::ReadCacheModelFile(const std::string& filePath OHOS::NeuralNetworkRuntime::Buffer& cache) const { // filePath is validate in NNCompiledCache::Restore, no need to check again. - FILE* pFile = fopen(filePath.c_str(), "rb"); + char path[PATH_MAX]; + if (realpath(filePath.c_str(), path) == nullptr) { + LOGE("[NNCompiledCache] ReadCacheModelFile failed, fail to get the real path of cacheInfoPath."); + return OH_NN_INVALID_PARAMETER; + } + if (access(path, 0) != 0) { + LOGE("[NNCompiledCache] ReadCacheModelFile failed, %{public}s is not exist.", path); + return OH_NN_INVALID_PARAMETER; + } + + FILE* pFile = fopen(path, "rb"); if (pFile == NULL) { LOGE("[NNCompiledCache] ReadCacheModelFile failed, file fopen failed."); return OH_NN_INVALID_FILE; diff --git a/test/unittest/components/nn_compiled_cache/nn_compiled_cache_test.cpp b/test/unittest/components/nn_compiled_cache/nn_compiled_cache_test.cpp index 8cdc1c7..690be40 100644 --- a/test/unittest/components/nn_compiled_cache/nn_compiled_cache_test.cpp +++ b/test/unittest/components/nn_compiled_cache/nn_compiled_cache_test.cpp @@ -466,19 +466,6 @@ HWTEST_F(NNCompiledCacheTest, nncompiledcachetest_setbackend_002, TestSize.Level EXPECT_EQ(OH_NN_SUCCESS, ret); } -/** - * @tc.name: nncompiledcachetest_setmodelname_001 - * @tc.desc: Verify the QuantParams function return nullptr in case of fd -1. - * @tc.type: FUNC - */ -HWTEST_F(NNCompiledCacheTest, nncompiledcachetest_setmodelname_001, TestSize.Level0) -{ - LOGE("SetModelName nncompiledcachetest_setmodelname_001"); - NNCompiledCache nncompiledCache; - std::string m_modelName; - nncompiledCache.SetModelName(m_modelName); -} - /** * @tc.name: nncompiledcachetest_writecacheinfo_001 * @tc.desc: Verify the QuantParams function return nullptr in case of fd -1. -- Gitee From c20c0e784a77b69657db53f414540325c3fe4222 Mon Sep 17 00:00:00 2001 From: w30052974 Date: Fri, 27 Sep 2024 13:58:34 +0800 Subject: [PATCH 2/2] =?UTF-8?q?nnrt=20=E5=91=8A=E8=AD=A6=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=20ut=20&=20file=20realpath?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: w30052974 --- .../native/neural_network_runtime/nncompiled_cache.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/frameworks/native/neural_network_runtime/nncompiled_cache.cpp b/frameworks/native/neural_network_runtime/nncompiled_cache.cpp index a98bcc6..a75a62d 100644 --- a/frameworks/native/neural_network_runtime/nncompiled_cache.cpp +++ b/frameworks/native/neural_network_runtime/nncompiled_cache.cpp @@ -86,13 +86,13 @@ OH_NN_ReturnCode NNCompiledCache::Restore(const std::string& cacheDir, LOGE("[NNCompiledCache] Restore failed, fail to get the real path of cacheInfoPath."); return OH_NN_INVALID_PARAMETER; } - if (access(cacheInfoPath.c_str(), F_OK) != 0) { + if (access(path, F_OK) != 0) { LOGE("[NNCompiledCache] Restore failed, cacheInfoPath is not exist."); return OH_NN_INVALID_PARAMETER; } NNCompiledCacheInfo cacheInfo; - OH_NN_ReturnCode ret = CheckCacheInfo(cacheInfo, cacheInfoPath); + OH_NN_ReturnCode ret = CheckCacheInfo(cacheInfo, path); if (ret != OH_NN_SUCCESS) { LOGE("[NNCompiledCache] Restore failed, error happened when calling CheckCacheInfo."); return ret; @@ -337,10 +337,9 @@ OH_NN_ReturnCode NNCompiledCache::CheckCacheInfo(NNCompiledCacheInfo& modelCache OH_NN_ReturnCode NNCompiledCache::ReadCacheModelFile(const std::string& filePath, OHOS::NeuralNetworkRuntime::Buffer& cache) const { - // filePath is validate in NNCompiledCache::Restore, no need to check again. char path[PATH_MAX]; if (realpath(filePath.c_str(), path) == nullptr) { - LOGE("[NNCompiledCache] ReadCacheModelFile failed, fail to get the real path of cacheInfoPath."); + LOGE("[NNCompiledCache] ReadCacheModelFile failed, fail to get the real path of filePath."); return OH_NN_INVALID_PARAMETER; } if (access(path, 0) != 0) { -- Gitee