From c8e7e2deaef2dd2bcb2bce97c077528db9470c9e Mon Sep 17 00:00:00 2001 From: louie5 Date: Mon, 9 Aug 2021 09:57:24 +0800 Subject: [PATCH] fix bugs caused by uncatched C++ exception, using exit() instead of throwing exception. --- mindspore/ccsrc/debug/common.cc | 45 +++++++++++++++++------------ mindspore/core/utils/log_adapter.cc | 4 ++- 2 files changed, 29 insertions(+), 20 deletions(-) diff --git a/mindspore/ccsrc/debug/common.cc b/mindspore/ccsrc/debug/common.cc index 14f389eec23..876eb32ecf9 100644 --- a/mindspore/ccsrc/debug/common.cc +++ b/mindspore/ccsrc/debug/common.cc @@ -28,7 +28,8 @@ namespace mindspore { std::optional Common::GetRealPath(const std::string &input_path) { if (input_path.length() >= PATH_MAX) { - MS_LOG(EXCEPTION) << "The length of path: " << input_path << " exceeds limit: " << PATH_MAX; + MS_LOG(ERROR) << "The length of path: " << input_path << " exceeds limit: " << PATH_MAX; + return std::nullopt; } auto path_split_pos = input_path.find_last_of('/'); if (path_split_pos == std::string::npos) { @@ -46,7 +47,8 @@ std::optional Common::GetRealPath(const std::string &input_path) { } #if defined(SYSTEM_ENV_POSIX) if (file_name.length() > NAME_MAX) { - MS_LOG(EXCEPTION) << "The length of file name : " << file_name.length() << " exceeds limit: " << NAME_MAX; + MS_LOG(ERROR) << "The length of file name : " << file_name.length() << " exceeds limit: " << NAME_MAX; + return std::nullopt; } if (realpath(common::SafeCStr(prefix_path), real_path) == nullptr) { MS_LOG(ERROR) << "The dir " << prefix_path << " does not exist."; @@ -63,7 +65,8 @@ std::optional Common::GetRealPath(const std::string &input_path) { // input_path is only file_name #if defined(SYSTEM_ENV_POSIX) if (input_path.length() > NAME_MAX) { - MS_LOG(EXCEPTION) << "The length of file name : " << input_path.length() << " exceeds limit: " << NAME_MAX; + MS_LOG(ERROR) << "The length of file name : " << input_path.length() << " exceeds limit: " << NAME_MAX; + return std::nullopt; } if (realpath(common::SafeCStr(input_path), real_path) == nullptr) { MS_LOG(INFO) << "The file " << input_path << " does not exist, it will be created."; @@ -145,8 +148,8 @@ std::optional Common::GetConfigFile(const std::string &env) { bool Common::IsStrLengthValid(const std::string &str, size_t length_limit, const std::string &error_message) { auto len_str = str.length(); if (len_str > length_limit) { - MS_LOG(WARNING) << error_message << "The length is " << str.length() << ", exceeding the limit of " << length_limit - << "."; + MS_LOG(ERROR) << error_message << "The length is " << str.length() << ", exceeding the limit of " << length_limit + << "."; return false; } return true; @@ -198,14 +201,16 @@ bool Common::IsPathValid(const std::string &path, size_t length_limit, const std return false; } - if (!std::all_of(path.begin(), path.end(), - [](char c) { return ::isalpha(c) || ::isdigit(c) || c == '-' || c == '_' || c == '/'; })) { - MS_LOG(WARNING) << err_msg << "The path only supports alphabets, digit or {'-', '_', '/'}, but got:" << path << "."; + if (!std::all_of(path.begin(), path.end(), [](char c) { + return ::isalpha(c) || ::isdigit(c) || c == '-' || c == '_' || c == '.' || c == '/'; + })) { + MS_LOG(ERROR) << err_msg << "The path only supports alphabets, digit or {'-', '_', '.', '/'}, but got:" << path + << "."; return false; } if (path[0] != '/') { - MS_LOG(WARNING) << err_msg << "The path only supports absolute path and should start with '/'."; + MS_LOG(ERROR) << err_msg << "The path only supports absolute path and should start with '/'."; return false; } @@ -229,11 +234,10 @@ bool Common::IsFilenameValid(const std::string &filename, size_t length_limit, c if (!IsStrLengthValid(filename, length_limit, err_msg)) { return false; } - - if (!std::all_of(filename.begin(), filename.end(), - [](char c) { return ::isalpha(c) || ::isdigit(c) || c == '-' || c == '_' || c == '.'; })) { - MS_LOG(WARNING) << err_msg << "The filename only supports alphabets, digit or {'-', '_', '.'}, but got:" << filename - << "."; + auto func = [](char c) { return ::isalpha(c) || ::isdigit(c) || c == '-' || c == '_' || c == '.'; }; + if (!std::all_of(filename.begin(), filename.end(), func)) { + MS_LOG(ERROR) << err_msg << "The filename only supports alphabets, digit or {'-', '_', '.'}, but got:" << filename + << "."; return false; } return true; @@ -301,16 +305,19 @@ struct GlogLogDirRegister { std::string log_dir_str = std::string(log_dir); auto real_log_dir_str = Common::GetRealPath(log_dir_str); - // While 'GLOG_logtostderr' = 0, logs output to files. - // 'GLOG_log_dir' must be specified as the path of log files. + // While 'GLOG_logtostderr' = 0, logs output to files. 'GLOG_log_dir' must be specified as the path of log files. + // Here can not throw exception and use python to catch, because the PYBIND11_MODULE is not yet been initialed. if (logtostderr_str == "0" && real_log_dir_str.has_value()) { if (!Common::IsPathValid(real_log_dir_str.value(), MAX_DIRECTORY_LENGTH, "")) { - MS_LOG(EXCEPTION) << "The path of log files, set by 'GLOG_log_dir', is invalid"; + MS_LOG(ERROR) << "The path of log files, which set by 'GLOG_log_dir', is invalid"; + exit(EXIT_FAILURE); } else if (!Common::CreateNotExistDirs(real_log_dir_str.value())) { - MS_LOG(EXCEPTION) << "Create the path of log files, set by 'GLOG_log_dir', failed."; + MS_LOG(ERROR) << "Create the path of log files, which set by 'GLOG_log_dir', failed."; + exit(EXIT_FAILURE); } } else if (logtostderr_str == "0") { - MS_LOG(EXCEPTION) << "The path of log files, set by 'GLOG_log_dir', is invalid."; + MS_LOG(ERROR) << "The path of log files, which set by 'GLOG_log_dir', is invalid."; + exit(EXIT_FAILURE); } } } diff --git a/mindspore/core/utils/log_adapter.cc b/mindspore/core/utils/log_adapter.cc index 7358cadbbe5..1bd1c7888fb 100644 --- a/mindspore/core/utils/log_adapter.cc +++ b/mindspore/core/utils/log_adapter.cc @@ -437,7 +437,9 @@ void common_log_init(void) { if (logtostderr.empty()) { FLAGS_logtostderr = true; } else if (logtostderr == "0" && mindspore::GetEnv("GLOG_log_dir").empty()) { - MS_LOG(EXCEPTION) << "`GLOG_log_dir` is empty, it must be set while 'logtostderr' equals to 0."; + MS_LOG(ERROR) << "`GLOG_log_dir` is empty, it must be set while 'logtostderr' equals to 0."; + // Here can not throw exception and use python to catch, because the PYBIND11_MODULE is not yet been initialed. + exit(EXIT_FAILURE); } // default GLOG_stderrthreshold level to WARNING