OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[Core] Sanitize subgraph friendly name and dump path in VisualizeTree (#38086)
Xu Xing0b55347bfbb4
diff --git a/src/core/src/pass/visualize_tree.cpp b/src/core/src/pass/visualize_tree.cppindex 9d9d8fdf92e..64aba958ce3 100644--- a/src/core/src/pass/visualize_tree.cpp+++ b/src/core/src/pass/visualize_tree.cpp@@ -4,6 +4,7 @@#include "openvino/pass/visualize_tree.hpp"+#include <cctype>#include <cmath>#include <fstream>@@ -167,18 +168,61 @@ static std::string get_attribute_values(const std::map<std::string, ov::Any>& atreturn ss.str();}+static char hex_digit(unsigned char value) {+ OPENVINO_ASSERT(value < 16, "Hexadecimal digit value must be less than 16");+ return static_cast<char>(value < 10 ? '0' + value : 'a' + (value - 10));+}++// std::isalnum() is locale-dependent above 7-bit ASCII; this bound keeps is_safe() locale-independent.+constexpr unsigned char ascii_limit = 0x80;++// Allowlist, not a denylist of path-sensitive characters, since those are platform-dependent+// (e.g. ':' only matters on NTFS).+static bool is_safe(unsigned char c) {+ return (c < ascii_limit && std::isalnum(c)) || c == '_' || c == '.' || c == '-';+}+static std::filesystem::path name_of_subgraph_file(const std::shared_ptr<ov::Node> op,const std::filesystem::path& current_file_name,const size_t i) {// friendly is never empty it is either friendly (set by user) or unique (auto-generated) name- auto node_name = op->get_friendly_name();- std::replace(node_name.begin(), node_name.end(), '/', '-');+ const auto& node_name = op->get_friendly_name();++ std::string sanitized_name;+ sanitized_name.reserve(node_name.size());+ for (unsigned char c : node_name) {+ if (is_safe(c)) {+ sanitized_name += static_cast<char>(c);+ } else {+ // '~' is not in is_safe()'s allowlist, so it only ever appears here as an escape's+ // lead byte, keeping this "~xy" encoding unambiguous.+ sanitized_name += '~';+ sanitized_name += hex_digit(c >> 4);+ sanitized_name += hex_digit(c & 0xF);+ }+ }auto file_name = current_file_name;- file_name.replace_extension("._node_" + node_name + "_subgraph_#" + std::to_string(i));+ file_name.replace_extension("._node_" + sanitized_name + "_subgraph_#" + std::to_string(i));return file_name;}+#if defined(ENABLE_OPENVINO_DEBUG) && !defined(_WIN32)+static std::string quote_shell_argument(const std::filesystem::path& path) {+ const auto path_string = ov::util::path_to_string(path);+ std::string quoted_path = "'";+ for (const auto character : path_string) {+ if (character == '\'') {+ quoted_path += "'\\''";+ } else {+ quoted_path += character;+ }+ }+ quoted_path += "'";+ return quoted_path;+}+#endif+static void collect_symbol_print_values(const std::shared_ptr<ov::Model>& m,std::unordered_map<std::shared_ptr<ov::Symbol>, size_t>& symbol_to_number) {size_t n = symbol_to_number.size() + 1;@@ -683,6 +727,8 @@ void ov::pass::VisualizeTree::render() const {dot_file += dot_ext;}+ // Keep the dump under its parent as sanitize_path() does (file_util.cpp:102).+ dot_file = ov::util::sanitize_path(m_name.parent_path(), dot_file.filename());if (std::ofstream out(dot_file); out) {out << "digraph \n{\n";out << m_ss.str();@@ -695,8 +741,8 @@ void ov::pass::VisualizeTree::render() const {if (system("command -v dot > /dev/null 2>&1") != 0) {OPENVINO_THROW("Graphviz 'dot' command not found in PATH");}- ss << "dot -T" << ext.string().substr(1) << " " << ov::util::path_to_string(dot_file) << " -o"- << ov::util::path_to_string(m_name);+ ss << "dot -T" << ext.string().substr(1) << " " << quote_shell_argument(dot_file) << " -o"+ << quote_shell_argument(m_name);auto cmd = ss.str();auto stream = popen(cmd.c_str(), "r");if (stream) {diff --git a/src/core/tests/pass/visualize_tree.cpp b/src/core/tests/pass/visualize_tree.cppindex ef4a758946a..091c6d9e38d 100644--- a/src/core/tests/pass/visualize_tree.cpp+++ b/src/core/tests/pass/visualize_tree.cpp@@ -9,6 +9,7 @@#include "openvino/core/model.hpp"#include "openvino/op/add.hpp"#include "openvino/op/constant.hpp"+#include "openvino/op/if.hpp"namespace ov::test {@@ -16,6 +17,7 @@ using ov::op::v0::Constant;using ov::op::v0::Parameter;using ov::op::v0::Result;using ov::op::v1::Add;+using ov::op::v8::If;class VisualizeTreeTest : public testing::Test {protected:@@ -37,6 +39,66 @@ protected:return std::make_shared<Model>(ResultVector{output}, ParameterVector{input});}+ // Model with a MultiSubGraphOp (If), so its friendly name drives a subgraph dump file name.+ static std::shared_ptr<Model> make_dummy_if_model(const std::string& if_friendly_name) {+ const auto x = std::make_shared<Parameter>(element::f32, Shape{1});+ const auto y = std::make_shared<Parameter>(element::f32, Shape{1});+ const auto cond = std::make_shared<Constant>(element::boolean, Shape{}, true);++ const auto xt = std::make_shared<Parameter>(element::f32, PartialShape::dynamic());+ const auto yt = std::make_shared<Parameter>(element::f32, PartialShape::dynamic());+ const auto then_res = std::make_shared<Result>(std::make_shared<Add>(xt, yt));+ const auto then_body = std::make_shared<Model>(OutputVector{then_res}, ParameterVector{xt, yt});++ const auto xe = std::make_shared<Parameter>(element::f32, PartialShape::dynamic());+ const auto ye = std::make_shared<Parameter>(element::f32, PartialShape::dynamic());+ const auto else_res = std::make_shared<Result>(std::make_shared<Add>(xe, ye));+ const auto else_body = std::make_shared<Model>(OutputVector{else_res}, ParameterVector{xe, ye});++ const auto if_op = std::make_shared<If>(cond);+ if_op->set_friendly_name(if_friendly_name);+ if_op->set_then_body(then_body);+ if_op->set_else_body(else_body);+ if_op->set_input(x, xt, xe);+ if_op->set_input(y, yt, ye);+ const auto out = if_op->set_output(then_res, else_res);+ const auto output = std::make_shared<Result>(out);+ return std::make_shared<Model>(ResultVector{output}, ParameterVector{x, y});+ }++ // Two independent If nodes (so two independent "subgraph_#0" dump paths under the same+ // parent), used to prove distinct friendly names never collide onto the same file name.+ static std::shared_ptr<Model> make_dummy_two_if_model(const std::string& first_if_name,+ const std::string& second_if_name) {+ const auto x = std::make_shared<Parameter>(element::f32, Shape{1});+ const auto y = std::make_shared<Parameter>(element::f32, Shape{1});+ const auto cond = std::make_shared<Constant>(element::boolean, Shape{}, true);++ auto make_if = [&](const std::string& name) {+ const auto xt = std::make_shared<Parameter>(element::f32, PartialShape::dynamic());+ const auto yt = std::make_shared<Parameter>(element::f32, PartialShape::dynamic());+ const auto then_res = std::make_shared<Result>(std::make_shared<Add>(xt, yt));+ const auto then_body = std::make_shared<Model>(OutputVector{then_res}, ParameterVector{xt, yt});++ const auto xe = std::make_shared<Parameter>(element::f32, PartialShape::dynamic());+ const auto ye = std::make_shared<Parameter>(element::f32, PartialShape::dynamic());+ const auto else_res = std::make_shared<Result>(std::make_shared<Add>(xe, ye));+ const auto else_body = std::make_shared<Model>(OutputVector{else_res}, ParameterVector{xe, ye});++ const auto if_op = std::make_shared<If>(cond);+ if_op->set_friendly_name(name);+ if_op->set_then_body(then_body);+ if_op->set_else_body(else_body);+ if_op->set_input(x, xt, xe);+ if_op->set_input(y, yt, ye);+ return if_op->set_output(then_res, else_res);+ };++ const auto output1 = std::make_shared<Result>(make_if(first_if_name));+ const auto output2 = std::make_shared<Result>(make_if(second_if_name));+ return std::make_shared<Model>(ResultVector{output1, output2}, ParameterVector{x, y});+ }+const std::filesystem::path vt_svg_file_path =ov::util::make_path(utils::getExecutableDirectory()) / (utils::generateTestFilePrefix() + "_tree.svg");const std::filesystem::path dot_file_path = vt_svg_file_path.string() + ".dot";@@ -61,4 +123,97 @@ TEST_F(VisualizeTreeTest, model_has_constant_with_no_inf) {OV_ASSERT_NO_THROW(vt.run_on_model(model));ASSERT_TRUE(util::file_exists(dot_file_path)) << dot_file_path;}++struct VisualizeTreeSanitizeParam {+ std::string friendly_name;+ std::string sanitized_name;+};++class VisualizeTreeSanitizeTest : public VisualizeTreeTest,+ public testing::WithParamInterface<VisualizeTreeSanitizeParam> {};++TEST_P(VisualizeTreeSanitizeTest, subgraph_friendly_name_with_disallowed_characters_is_sanitized) {+ const auto& param = GetParam();+ const auto model = make_dummy_if_model(param.friendly_name);++ pass::VisualizeTree vt(vt_svg_file_path);++ OV_ASSERT_NO_THROW(vt.run_on_model(model));+ ASSERT_TRUE(util::file_exists(dot_file_path)) << dot_file_path;++ // If has both a then_body and an else_body, so it dumps subgraph #0 and #1.+ for (const auto subgraph_index : {0, 1}) {+ auto subgraph_file_path = vt_svg_file_path;+ subgraph_file_path.replace_extension("._node_" + param.sanitized_name + "_subgraph_#" ++ std::to_string(subgraph_index));+ subgraph_file_path += ".dot";+ ASSERT_TRUE(util::file_exists(subgraph_file_path)) << subgraph_file_path;+ std::filesystem::remove(subgraph_file_path);+ }+}++INSTANTIATE_TEST_SUITE_P(DisallowedCharacters,+ VisualizeTreeSanitizeTest,+ testing::Values(VisualizeTreeSanitizeParam{"x\\y", "x~5cy"},+ VisualizeTreeSanitizeParam{"evil:stream", "evil~3astream"}));++TEST_F(VisualizeTreeTest, subgraph_friendly_names_with_different_disallowed_characters_do_not_collide) {+ const auto model = make_dummy_two_if_model("a:b", "a?b");++ pass::VisualizeTree vt(vt_svg_file_path);++ OV_ASSERT_NO_THROW(vt.run_on_model(model));+ ASSERT_TRUE(util::file_exists(dot_file_path)) << dot_file_path;++ // Each If has both a then_body and an else_body, so each dumps subgraph #0 and #1.+ for (const auto subgraph_index : {0, 1}) {+ auto first_subgraph_file_path = vt_svg_file_path;+ first_subgraph_file_path.replace_extension("._node_a~3ab_subgraph_#" + std::to_string(subgraph_index));+ first_subgraph_file_path += ".dot";+ auto second_subgraph_file_path = vt_svg_file_path;+ second_subgraph_file_path.replace_extension("._node_a~3fb_subgraph_#" + std::to_string(subgraph_index));+ second_subgraph_file_path += ".dot";++ ASSERT_TRUE(util::file_exists(first_subgraph_file_path)) << first_subgraph_file_path;+ ASSERT_TRUE(util::file_exists(second_subgraph_file_path)) << second_subgraph_file_path;+ std::filesystem::remove(first_subgraph_file_path);+ std::filesystem::remove(second_subgraph_file_path);+ }+}++TEST_F(VisualizeTreeTest, subgraph_friendly_name_with_safe_characters_is_dumped) {+ const auto model = make_dummy_if_model("safe_name-1.2");++ pass::VisualizeTree vt(vt_svg_file_path);++ OV_ASSERT_NO_THROW(vt.run_on_model(model));+ ASSERT_TRUE(util::file_exists(dot_file_path)) << dot_file_path;++ // If has both a then_body and an else_body, so it dumps subgraph #0 and #1.+ for (const auto subgraph_index : {0, 1}) {+ auto subgraph_file_path = vt_svg_file_path;+ subgraph_file_path.replace_extension("._node_safe_name-1.2_subgraph_#" + std::to_string(subgraph_index));+ subgraph_file_path += ".dot";+ ASSERT_TRUE(util::file_exists(subgraph_file_path)) << subgraph_file_path;+ std::filesystem::remove(subgraph_file_path);+ }+}++TEST_F(VisualizeTreeTest, output_path_with_spaces_is_supported) {+ const auto directory_path =+ vt_svg_file_path.parent_path() / (utils::generateTestFilePrefix() + " directory with spaces");+ ASSERT_TRUE(std::filesystem::create_directories(directory_path));+ const auto file_path = directory_path / "tree.svg";+ const auto dot_path = file_path.string() + ".dot";+ const auto model = make_dummy_add_model(element::f32);++ pass::VisualizeTree vt(file_path);++ OV_ASSERT_NO_THROW(vt.run_on_model(model));+ ASSERT_TRUE(util::file_exists(dot_path)) << dot_path;+#if defined(ENABLE_OPENVINO_DEBUG) && !defined(_WIN32)+ ASSERT_TRUE(util::file_exists(file_path)) << file_path;+#endif+ std::filesystem::remove_all(directory_path);+}} // namespace ov::test