!3155 Fix data dump security bug

Merge pull request !3155 from caifubi/data-dump-new
This commit is contained in:
mindspore-ci-bot 2020-07-18 14:24:44 +08:00 committed by Gitee
commit eb630f2501
5 changed files with 49 additions and 9 deletions

View File

@ -120,6 +120,10 @@ std::optional<std::string> Common::GetConfigFile(const std::string &env) {
MS_LOG(ERROR) << dump_config_file << " not exist."; MS_LOG(ERROR) << dump_config_file << " not exist.";
return {}; return {};
} }
auto suffix = dump_config_file.substr(dump_config_file.find_last_of('.') + 1);
if (suffix != "json") {
MS_LOG(EXCEPTION) << "[DataDump] dump config file suffix only support json! But got:." << suffix;
}
return dump_config_file; return dump_config_file;
} }
} // namespace mindspore } // namespace mindspore

View File

@ -29,7 +29,7 @@ void DataDumpParser::ResetParam() {
net_name_.clear(); net_name_.clear();
dump_mode_ = 0; dump_mode_ = 0;
dump_step_ = 0; dump_step_ = 0;
kernel_set_.clear(); kernel_map_.clear();
} }
bool DataDumpParser::DumpEnabled() const { bool DataDumpParser::DumpEnabled() const {
@ -60,9 +60,18 @@ std::optional<std::string> DataDumpParser::GetDumpPath() const {
return {}; return {};
} }
std::string dump_path_str(dump_path); std::string dump_path_str(dump_path);
if (!std::all_of(dump_path_str.begin(), dump_path_str.end(), ::isalpha)) {
MS_LOG(EXCEPTION) << "[DataDump] dump path only support alphas, but got:" << dump_path_str;
}
return dump_path_str; return dump_path_str;
} }
std::string GetIfstreamString(const std::ifstream &ifstream) {
std::stringstream buffer;
buffer << ifstream.rdbuf();
return buffer.str();
}
void DataDumpParser::ParseDumpConfig() { void DataDumpParser::ParseDumpConfig() {
std::lock_guard<std::mutex> guard(lock_); std::lock_guard<std::mutex> guard(lock_);
MS_LOG(INFO) << "[DataDump] parse start"; MS_LOG(INFO) << "[DataDump] parse start";
@ -84,7 +93,12 @@ void DataDumpParser::ParseDumpConfig() {
} }
nlohmann::json j; nlohmann::json j;
json_file >> j; try {
json_file >> j;
} catch (nlohmann::json::parse_error &e) {
MS_LOG(ERROR) << "[DataDump] json contents:" << GetIfstreamString(json_file);
MS_LOG(EXCEPTION) << "[DataDump] parse json failed, error:" << e.what();
}
if (j.find("DumpSettings") == j.end()) { if (j.find("DumpSettings") == j.end()) {
MS_LOG(EXCEPTION) << "[DataDump] DumpSettings is not exist."; MS_LOG(EXCEPTION) << "[DataDump] DumpSettings is not exist.";
} }
@ -111,8 +125,8 @@ bool DataDumpParser::NeedDump(const std::string &op_full_name) const {
if (dump_mode_ == 0) { if (dump_mode_ == 0) {
return true; return true;
} }
auto iter = kernel_set_.find(op_full_name); auto iter = kernel_map_.find(op_full_name);
return iter != kernel_set_.end(); return iter != kernel_map_.end();
} }
bool DataDumpParser::IsConfigExist(const nlohmann::json &dump_settings) const { bool DataDumpParser::IsConfigExist(const nlohmann::json &dump_settings) const {
@ -145,8 +159,25 @@ bool DataDumpParser::ParseDumpSetting(const nlohmann::json &dump_settings) {
auto kernel_str = kernel.dump(); auto kernel_str = kernel.dump();
kernel_str.erase(std::remove(kernel_str.begin(), kernel_str.end(), '\"'), kernel_str.end()); kernel_str.erase(std::remove(kernel_str.begin(), kernel_str.end(), '\"'), kernel_str.end());
MS_LOG(INFO) << "[DataDump] Need dump kernel:" << kernel_str; MS_LOG(INFO) << "[DataDump] Need dump kernel:" << kernel_str;
kernel_set_.insert(kernel_str); kernel_map_.insert({kernel_str, 0});
} }
return true; return true;
} }
void DataDumpParser::MatchKernel(const std::string &kernel_name) {
auto iter = kernel_map_.find(kernel_name);
if (iter == kernel_map_.end()) {
return;
}
iter->second = iter->second + 1;
MS_LOG(INFO) << "Match dump kernel:" << iter->first << " match times:" << iter->second;
}
void DataDumpParser::PrintUnusedKernel() {
for (const auto &iter : kernel_map_) {
if (iter.second == 0) {
MS_LOG(WARNING) << "[DataDump] Unused Kernel in json:" << iter.first;
}
}
}
} // namespace mindspore } // namespace mindspore

View File

@ -18,7 +18,7 @@
#define MINDSPORE_MINDSPORE_CCSRC_DEBUG_ASYNC_DUMP_JSON_PARE_H_ #define MINDSPORE_MINDSPORE_CCSRC_DEBUG_ASYNC_DUMP_JSON_PARE_H_
#include <string> #include <string>
#include <set> #include <map>
#include <mutex> #include <mutex>
#include <optional> #include <optional>
#include "nlohmann/json.hpp" #include "nlohmann/json.hpp"
@ -39,7 +39,8 @@ class DataDumpParser {
const std::string &net_name() const { return net_name_; } const std::string &net_name() const { return net_name_; }
uint32_t dump_mode() const { return dump_mode_; } uint32_t dump_mode() const { return dump_mode_; }
uint32_t dump_step() const { return dump_step_; } uint32_t dump_step() const { return dump_step_; }
const std::set<std::string> &kernel_set() const { return kernel_set_; } void MatchKernel(const std::string &kernel_name);
void PrintUnusedKernel();
private: private:
DataDumpParser() = default; DataDumpParser() = default;
@ -55,7 +56,7 @@ class DataDumpParser {
std::string net_name_; std::string net_name_;
uint32_t dump_mode_{0}; uint32_t dump_mode_{0};
uint32_t dump_step_{0}; uint32_t dump_step_{0};
std::set<std::string> kernel_set_; std::map<std::string, uint32_t> kernel_map_;
}; };
} // namespace mindspore } // namespace mindspore
#endif // MINDSPORE_MINDSPORE_CCSRC_DEBUG_ASYNC_DUMP_JSON_PARE_H_ #endif // MINDSPORE_MINDSPORE_CCSRC_DEBUG_ASYNC_DUMP_JSON_PARE_H_

View File

@ -100,6 +100,9 @@ void AscendKernelRuntime::ClearGraphModelMap() {
iter.second->UnloadDumpInfo(); iter.second->UnloadDumpInfo();
} }
graph_data_dumper_.clear(); graph_data_dumper_.clear();
// tell users which dump kernel name not used
DataDumpParser::GetInstance().PrintUnusedKernel();
for (auto &iter : graph_model_map_) { for (auto &iter : graph_model_map_) {
MS_LOG(INFO) << "Ge UnloadModel " << iter.first; MS_LOG(INFO) << "Ge UnloadModel " << iter.first;
auto ret = ModelRunner::Instance().UnloadModel(iter.first); auto ret = ModelRunner::Instance().UnloadModel(iter.first);

View File

@ -62,6 +62,7 @@ void DataDumper::LoadDumpInfo() {
} }
MS_LOG(INFO) << "[DataDump] LoadDumpInfo kernel:" << kernel->fullname_with_scope(); MS_LOG(INFO) << "[DataDump] LoadDumpInfo kernel:" << kernel->fullname_with_scope();
dump_kernel_names_.emplace_back(kernel->fullname_with_scope()); dump_kernel_names_.emplace_back(kernel->fullname_with_scope());
DataDumpParser::GetInstance().MatchKernel(kernel->fullname_with_scope());
aicpu::dump::Task task; aicpu::dump::Task task;
ConstructDumpTask(NOT_NULL(kernel), NOT_NULL(&task)); ConstructDumpTask(NOT_NULL(kernel), NOT_NULL(&task));
@ -84,7 +85,7 @@ void DataDumper::SetOpMappingInfo(NotNull<aicpu::dump::OpMappingInfo *> dump_inf
MS_LOG(EXCEPTION) << "Dump path invalid"; MS_LOG(EXCEPTION) << "Dump path invalid";
} }
auto device_id = context_ptr->device_id(); auto device_id = context_ptr->device_id();
dump_info->set_dump_path(dump_path.value() + "_" + std::to_string(device_id) + "/"); dump_info->set_dump_path("/" + dump_path.value() + "_" + std::to_string(device_id) + "/");
MS_LOG(INFO) << "[DataDump] dump_path:" << dump_path.value(); MS_LOG(INFO) << "[DataDump] dump_path:" << dump_path.value();
dump_info->set_model_name(DataDumpParser::GetInstance().net_name() + "_" + std::to_string(kernel_graph_->graph_id())); dump_info->set_model_name(DataDumpParser::GetInstance().net_name() + "_" + std::to_string(kernel_graph_->graph_id()));