From 3996f5df109662f59e7eca969ff9ce9392a8badf Mon Sep 17 00:00:00 2001 From: zhaozhixuan Date: Wed, 23 Jun 2021 19:32:49 +0800 Subject: [PATCH] Fix bug of recursive depth protection. --- ge/common/ge/tbe_plugin_manager.cc | 16 +++++----------- ge/common/ge/tbe_plugin_manager.h | 3 ++- ge/session/omg.cc | 16 +++++----------- inc/framework/omg/omg.h | 3 ++- 4 files changed, 14 insertions(+), 24 deletions(-) diff --git a/ge/common/ge/tbe_plugin_manager.cc b/ge/common/ge/tbe_plugin_manager.cc index c876e300..70c1ab94 100755 --- a/ge/common/ge/tbe_plugin_manager.cc +++ b/ge/common/ge/tbe_plugin_manager.cc @@ -104,14 +104,12 @@ void TBEPluginManager::ProcessSoFullName(vector &file_list, string &caff } } -void TBEPluginManager::FindParserSo(const string &path, vector &file_list, string &caffe_parser_path) { - static uint32_t temp_depth = 0; +void TBEPluginManager::FindParserSo(const string &path, vector &file_list, + string &caffe_parser_path, uint32_t recursive_depth) { static const uint32_t max_recursive_depth = 20; // For recursive depth protection - temp_depth++; - if (temp_depth >= max_recursive_depth) { - GELOGW("Recursive depth is become %u, Please check input!", temp_depth); - temp_depth--; + if (recursive_depth >= max_recursive_depth) { + GELOGW("Recursive depth is become %u, Please check input!", recursive_depth); return; } @@ -120,14 +118,12 @@ void TBEPluginManager::FindParserSo(const string &path, vector &file_lis // Plugin path does not exist if (real_path.empty()) { GELOGW("RealPath is empty."); - temp_depth--; return; } INT32 is_dir = mmIsDir(real_path.c_str()); // Lib plugin path not exist if (is_dir != EN_OK) { GELOGW("%s is not a dir. errmsg:%s", real_path.c_str(), strerror(errno)); - temp_depth--; return; } @@ -135,7 +131,6 @@ void TBEPluginManager::FindParserSo(const string &path, vector &file_lis auto ret = mmScandir(real_path.c_str(), &entries, nullptr, nullptr); if (ret < EN_OK) { GELOGW("scan dir failed. path = %s, ret = %d, errmsg = %s", real_path.c_str(), ret, strerror(errno)); - temp_depth--; return; } for (int i = 0; i < ret; ++i) { @@ -151,11 +146,10 @@ void TBEPluginManager::FindParserSo(const string &path, vector &file_lis ProcessSoFullName(file_list, caffe_parser_path, full_name, caffe_parser_so_suff, aicpu_so_suff, aicpu_host_so_suff); } else { - FindParserSo(full_name, file_list, caffe_parser_path); + FindParserSo(full_name, file_list, caffe_parser_path, recursive_depth + 1); } } mmScandirFree(entries, ret); - temp_depth--; } void TBEPluginManager::GetPluginSoFileList(const string &path, vector &file_list, string &caffe_parser_path) { diff --git a/ge/common/ge/tbe_plugin_manager.h b/ge/common/ge/tbe_plugin_manager.h index 4bd8c6e3..eada3e64 100755 --- a/ge/common/ge/tbe_plugin_manager.h +++ b/ge/common/ge/tbe_plugin_manager.h @@ -57,7 +57,8 @@ class TBEPluginManager { static void ProcessSoFullName(vector &file_list, string &caffe_parser_path, string &full_name, const string &caffe_parser_so_suff, const string &aicpu_so_suff, const string &aicpu_host_so_suff); - static void FindParserSo(const string &path, vector &file_list, string &caffe_parser_path); + static void FindParserSo(const string &path, vector &file_list, string &caffe_parser_path, + uint32_t recursive_depth = 0); static void GetPluginSoFileList(const string &path, vector &file_list, string &caffe_parser_path); static void GetCustomOpPath(std::string &customop_path); void LoadCustomOpLib(); diff --git a/ge/session/omg.cc b/ge/session/omg.cc index 8d826043..a2ee176f 100755 --- a/ge/session/omg.cc +++ b/ge/session/omg.cc @@ -220,26 +220,22 @@ static Status ParseOutputFp16NodesFormat(const string &is_output_fp16) { return SUCCESS; } -void FindParserSo(const string &path, vector &file_list, string &caffe_parser_path) { - static uint32_t temp_depth = 0; +void FindParserSo(const string &path, vector &file_list, + string &caffe_parser_path, uint32_t recursive_depth) { static const uint32_t max_recursive_depth = 20; // For recursive depth protection - temp_depth++; - if (temp_depth >= max_recursive_depth) { - GELOGW("Recursive depth is become %u, Please check input!", temp_depth); - temp_depth--; + if (recursive_depth >= max_recursive_depth) { + GELOGW("Recursive depth is become %u, Please check input!", recursive_depth); return; } // path, Change to absolute path string real_path = RealPath(path.c_str()); if (real_path.empty()) { // plugin path does not exist - temp_depth--; return; } struct stat stat_buf; if ((stat(real_path.c_str(), &stat_buf) != 0) || (!S_ISDIR(stat_buf.st_mode))) { GELOGI("The path %s is not a directory.", real_path.c_str()); - temp_depth--; return; } @@ -248,7 +244,6 @@ void FindParserSo(const string &path, vector &file_list, string &caffe_p if (nullptr == dir) { // plugin path does not exist GELOGW("Open directory %s failed.", path.c_str()); - temp_depth--; return; } @@ -269,10 +264,9 @@ void FindParserSo(const string &path, vector &file_list, string &caffe_p continue; } - FindParserSo(full_name, file_list, caffe_parser_path); + FindParserSo(full_name, file_list, caffe_parser_path, recursive_depth + 1); } closedir(dir); - temp_depth--; return; } diff --git a/inc/framework/omg/omg.h b/inc/framework/omg/omg.h index a0cdb449..1c39d203 100644 --- a/inc/framework/omg/omg.h +++ b/inc/framework/omg/omg.h @@ -91,7 +91,8 @@ GE_FUNC_VISIBILITY Status ConvertFwkModelToJson(domi::FrameworkType framework, c GE_FUNC_VISIBILITY void GetGroupName(ge::proto::ModelDef &model); -GE_FUNC_VISIBILITY void FindParserSo(const string &path, vector &fileList, string &caffe_parser_path); +GE_FUNC_VISIBILITY void FindParserSo(const string &path, vector &fileList, string &caffe_parser_path, + uint32_t recursive_depth = 0); GE_FUNC_VISIBILITY Status DumpInfershapeJson(const ge::Graph &graph, const char *json_file);