From 2d10d86953b5795afb62ca6d2363cf4c0d1a5bcc Mon Sep 17 00:00:00 2001 From: maoyong Date: Mon, 6 May 2024 19:45:10 +0800 Subject: [PATCH 1/5] fix cache code warning Signed-off-by: maoyong --- .../nncompiled_cache.cpp | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/frameworks/native/neural_network_runtime/nncompiled_cache.cpp b/frameworks/native/neural_network_runtime/nncompiled_cache.cpp index 97da8c1..d8036f5 100644 --- a/frameworks/native/neural_network_runtime/nncompiled_cache.cpp +++ b/frameworks/native/neural_network_runtime/nncompiled_cache.cpp @@ -18,6 +18,8 @@ #include #include #include +#include +#include #include "common/utils.h" #include "backend_manager.h" @@ -197,6 +199,19 @@ OH_NN_ReturnCode NNCompiledCache::GenerateCacheModel(const std::vector(version); *cacheInfoPtr++ = static_cast(m_backendID); // Should call SetBackend first. + OH_NN_ReturnCode ret = OH_NN_SUCCESS; + char path[PATH_MAX]; + if (realpath(cacheDir.c_str(), path) == nullptr) { + LOGE("[NNCompiledCache] GenerateCacheModel failed, fail to get the real path of cacheDir."); + return OH_NN_INVALID_PARAMETER; + } + + ret = VerifyCachePath(path); + if (ret != OH_NN_SUCCESS) { + LOGE("[NNCompiledCache] GenerateCacheModel failed, fail to verify the file path of cacheDir."); + return ret; + } + for (size_t i = 0; i < cacheNumber; ++i) { std::string cacheModelFile = cacheDir + "/" + m_modelName + std::to_string(i) + ".nncache"; std::ofstream cacheModelStream(cacheModelFile, std::ios::binary | std::ios::out | std::ios::trunc); @@ -224,6 +239,18 @@ OH_NN_ReturnCode NNCompiledCache::WriteCacheInfo(uint32_t cacheSize, std::unique_ptr& cacheInfo, const std::string& cacheDir) const { + char path[PATH_MAX]; + if (realpath(cacheDir.c_str(), path) == nullptr) { + LOGE("[NNCompiledCache] WriteCacheInfo failed, fail to get the real path of cacheDir."); + return OH_NN_INVALID_PARAMETER; + } + + OH_NN_ReturnCode ret = VerifyCachePath(path); + if (ret != OH_NN_SUCCESS) { + LOGE("[NNCompiledCache] WriteCacheInfo failed, fail to verify the file path of cacheDir."); + return ret; + } + std::string cacheInfoPath = cacheDir + "/" + m_modelName + "cache_info.nncache"; std::ofstream cacheInfoStream(cacheInfoPath, std::ios::binary | std::ios::out | std::ios::trunc); if (cacheInfoStream.fail()) { @@ -380,5 +407,24 @@ OH_NN_ReturnCode NNCompiledCache::GetCacheFileLength(std::ifstream& ifs, int& fi fileSize = handleValue; return OH_NN_SUCCESS; } + +OH_NN_ReturnCode NNCompiledCache::VerifyCachePath(const std::string& cachePath) const +{ + if (cachePath.find('/') != size_t(0)) { + LOGE("[NNCompiledCache] VerifyCachePath failed, input file dir=%{public}s is invalid, " + "should start with '/'.", + cachePath.c_str()); + return OH_NN_INVALID_FILE; + } + + if (cachePath.find("//") != std::string::npos) { + LOGE("[NNCompiledCache] VerifyCachePath failed, input file dir=%{public}s is invalid, " + "containing double '/'.", + cachePath.c_str()); + return OH_NN_INVALID_FILE; + } + + return OH_NN_SUCCESS; +} } // namespace NeuralNetworkRuntime } // namespace OHOS -- Gitee From a218a3b7c3a983cd579ad1fa76e94219654cbbf8 Mon Sep 17 00:00:00 2001 From: maoyong Date: Mon, 6 May 2024 20:09:29 +0800 Subject: [PATCH 2/5] fix cache verify bug Signed-off-by: maoyong --- frameworks/native/neural_network_runtime/nncompiled_cache.h | 1 + 1 file changed, 1 insertion(+) diff --git a/frameworks/native/neural_network_runtime/nncompiled_cache.h b/frameworks/native/neural_network_runtime/nncompiled_cache.h index 49dc108..1140fab 100644 --- a/frameworks/native/neural_network_runtime/nncompiled_cache.h +++ b/frameworks/native/neural_network_runtime/nncompiled_cache.h @@ -65,6 +65,7 @@ private: OH_NN_ReturnCode ReadCacheModelFile(const std::string& file, Buffer& cache) const; unsigned short GetCrc16(char* buffer, size_t length) const; OH_NN_ReturnCode GetCacheFileLength(std::ifstream& ifs, int& fileSize) const; + OH_NN_ReturnCode VerifyCachePath(const std::string& cachePath) const; private: size_t m_backendID {0}; -- Gitee From a9072e29630039425859db83112aad20f0c5be15 Mon Sep 17 00:00:00 2001 From: maoyong Date: Tue, 7 May 2024 14:20:45 +0800 Subject: [PATCH 3/5] fix cache warning codex Signed-off-by: maoyong --- .../native/neural_network_runtime/nncompiled_cache.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/frameworks/native/neural_network_runtime/nncompiled_cache.cpp b/frameworks/native/neural_network_runtime/nncompiled_cache.cpp index d8036f5..2eb0b82 100644 --- a/frameworks/native/neural_network_runtime/nncompiled_cache.cpp +++ b/frameworks/native/neural_network_runtime/nncompiled_cache.cpp @@ -31,6 +31,8 @@ constexpr int MAX_MODEL_SIZE = 200 * 1024 * 1024; // 200MB constexpr int NULL_PTR_LENGTH = 0; constexpr int NUMBER_CACHE_INFO_MEMBERS = 3; constexpr int HEX_UNIT = 16; +constexpr std::string ROOT_DIR_STR = "/"; +constexpr std::string DOUBLE_SLASH_STR = "//"; OH_NN_ReturnCode NNCompiledCache::Save(const std::vector& caches, const std::string& cacheDir, @@ -410,14 +412,14 @@ OH_NN_ReturnCode NNCompiledCache::GetCacheFileLength(std::ifstream& ifs, int& fi OH_NN_ReturnCode NNCompiledCache::VerifyCachePath(const std::string& cachePath) const { - if (cachePath.find('/') != size_t(0)) { + if (cachePath.find(ROOT_DIR_STR) != size_t(0)) { LOGE("[NNCompiledCache] VerifyCachePath failed, input file dir=%{public}s is invalid, " "should start with '/'.", cachePath.c_str()); return OH_NN_INVALID_FILE; } - if (cachePath.find("//") != std::string::npos) { + if (cachePath.find(DOUBLE_SLASH_STR) != std::string::npos) { LOGE("[NNCompiledCache] VerifyCachePath failed, input file dir=%{public}s is invalid, " "containing double '/'.", cachePath.c_str()); -- Gitee From 1f6a9254d0d1959e70c4c3d757cba1b47c801602 Mon Sep 17 00:00:00 2001 From: maoyong Date: Tue, 7 May 2024 14:32:04 +0800 Subject: [PATCH 4/5] add code description Signed-off-by: maoyong --- .../native/neural_network_runtime/nncompiled_cache.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/frameworks/native/neural_network_runtime/nncompiled_cache.cpp b/frameworks/native/neural_network_runtime/nncompiled_cache.cpp index 2eb0b82..66ad34d 100644 --- a/frameworks/native/neural_network_runtime/nncompiled_cache.cpp +++ b/frameworks/native/neural_network_runtime/nncompiled_cache.cpp @@ -18,7 +18,6 @@ #include #include #include -#include #include #include "common/utils.h" @@ -201,6 +200,7 @@ OH_NN_ReturnCode NNCompiledCache::GenerateCacheModel(const std::vector(version); *cacheInfoPtr++ = static_cast(m_backendID); // Should call SetBackend first. + // standardize the input dir OH_NN_ReturnCode ret = OH_NN_SUCCESS; char path[PATH_MAX]; if (realpath(cacheDir.c_str(), path) == nullptr) { @@ -208,6 +208,7 @@ OH_NN_ReturnCode NNCompiledCache::GenerateCacheModel(const std::vector& cacheInfo, const std::string& cacheDir) const { + // standardize the input dir char path[PATH_MAX]; if (realpath(cacheDir.c_str(), path) == nullptr) { LOGE("[NNCompiledCache] WriteCacheInfo failed, fail to get the real path of cacheDir."); return OH_NN_INVALID_PARAMETER; } + // verify the Standardized path available OH_NN_ReturnCode ret = VerifyCachePath(path); if (ret != OH_NN_SUCCESS) { LOGE("[NNCompiledCache] WriteCacheInfo failed, fail to verify the file path of cacheDir."); @@ -412,6 +415,7 @@ OH_NN_ReturnCode NNCompiledCache::GetCacheFileLength(std::ifstream& ifs, int& fi OH_NN_ReturnCode NNCompiledCache::VerifyCachePath(const std::string& cachePath) const { + // exception: input path is not start with '/'. if (cachePath.find(ROOT_DIR_STR) != size_t(0)) { LOGE("[NNCompiledCache] VerifyCachePath failed, input file dir=%{public}s is invalid, " "should start with '/'.", @@ -419,6 +423,7 @@ OH_NN_ReturnCode NNCompiledCache::VerifyCachePath(const std::string& cachePath) return OH_NN_INVALID_FILE; } + // exception: input path contains continuous double '/'. if (cachePath.find(DOUBLE_SLASH_STR) != std::string::npos) { LOGE("[NNCompiledCache] VerifyCachePath failed, input file dir=%{public}s is invalid, " "containing double '/'.", -- Gitee From 672bc57f840bdc8268036a4c4d877db04f0277cc Mon Sep 17 00:00:00 2001 From: maoyong Date: Tue, 7 May 2024 16:11:27 +0800 Subject: [PATCH 5/5] fix const bug Signed-off-by: maoyong --- frameworks/native/neural_network_runtime/nncompiled_cache.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frameworks/native/neural_network_runtime/nncompiled_cache.cpp b/frameworks/native/neural_network_runtime/nncompiled_cache.cpp index 66ad34d..7d7ee1c 100644 --- a/frameworks/native/neural_network_runtime/nncompiled_cache.cpp +++ b/frameworks/native/neural_network_runtime/nncompiled_cache.cpp @@ -30,8 +30,8 @@ constexpr int MAX_MODEL_SIZE = 200 * 1024 * 1024; // 200MB constexpr int NULL_PTR_LENGTH = 0; constexpr int NUMBER_CACHE_INFO_MEMBERS = 3; constexpr int HEX_UNIT = 16; -constexpr std::string ROOT_DIR_STR = "/"; -constexpr std::string DOUBLE_SLASH_STR = "//"; +constexpr char ROOT_DIR_STR = '/'; +constexpr char DOUBLE_SLASH_STR[] = "//"; OH_NN_ReturnCode NNCompiledCache::Save(const std::vector& caches, const std::string& cacheDir, -- Gitee