From f8311f33b46ec3e50bd3f37a828aa02fd47359f6 Mon Sep 17 00:00:00 2001 From: Fang Xu Date: Tue, 30 Apr 2024 19:14:55 +0800 Subject: [PATCH] store unreadable xml in model cache (#24202) ### Details: - *store unreadable xml in model cache* ### Tickets: - *CVS-138573* --------- Co-authored-by: Chen Peter --- .../util/include/openvino/util/codec_xor.hpp | 13 +++++++++++++ src/common/util/src/codec_xor.cpp | 18 ++++++++++++++++++ src/core/include/openvino/pass/serialize.hpp | 2 ++ src/core/src/pass/serialize.cpp | 11 ++++++++++- src/plugins/intel_cpu/src/serialize.cpp | 7 +++++-- 5 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 src/common/util/include/openvino/util/codec_xor.hpp create mode 100644 src/common/util/src/codec_xor.cpp diff --git a/src/common/util/include/openvino/util/codec_xor.hpp b/src/common/util/include/openvino/util/codec_xor.hpp new file mode 100644 index 00000000000000..d5de2aa9aa5661 --- /dev/null +++ b/src/common/util/include/openvino/util/codec_xor.hpp @@ -0,0 +1,13 @@ +// Copyright (C) 2018-2024 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// +#pragma once + +#include + +namespace ov { +namespace util { + +std::string codec_xor(const std::string& source_str); +} // namespace util +} // namespace ov diff --git a/src/common/util/src/codec_xor.cpp b/src/common/util/src/codec_xor.cpp new file mode 100644 index 00000000000000..a1743e13e07ac3 --- /dev/null +++ b/src/common/util/src/codec_xor.cpp @@ -0,0 +1,18 @@ +// Copyright (C) 2018-2024 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#include "openvino/util/codec_xor.hpp" + +static const char codec_key[] = {0x30, 0x60, 0x70, 0x02, 0x04, 0x08, 0x3F, 0x6F, 0x72, 0x74, 0x78, 0x7F}; + +std::string ov::util::codec_xor(const std::string& source_str) { + auto key_size = sizeof(codec_key); + int key_idx = 0; + std::string dst_str = source_str; + for (char& c : dst_str) { + c ^= codec_key[key_idx % key_size]; + key_idx++; + } + return dst_str; +} \ No newline at end of file diff --git a/src/core/include/openvino/pass/serialize.hpp b/src/core/include/openvino/pass/serialize.hpp index c85a85381b29de..fc3e743d4005dc 100644 --- a/src/core/include/openvino/pass/serialize.hpp +++ b/src/core/include/openvino/pass/serialize.hpp @@ -67,11 +67,13 @@ class OPENVINO_API StreamSerialize : public ov::pass::ModelPass { StreamSerialize(std::ostream& stream, const std::function& custom_data_serializer = {}, + const std::function& cache_encrypt = {}, Serialize::Version version = Serialize::Version::UNSPECIFIED); private: std::ostream& m_stream; std::function m_custom_data_serializer; + std::function m_cache_encrypt; const Serialize::Version m_version; }; } // namespace pass diff --git a/src/core/src/pass/serialize.cpp b/src/core/src/pass/serialize.cpp index 59732bd6ebf7c8..c82c5a806578f9 100644 --- a/src/core/src/pass/serialize.cpp +++ b/src/core/src/pass/serialize.cpp @@ -1247,9 +1247,11 @@ pass::Serialize::Serialize(const std::string& xmlPath, const std::string& binPat pass::StreamSerialize::StreamSerialize(std::ostream& stream, const std::function& custom_data_serializer, + const std::function& cache_encrypt, Serialize::Version version) : m_stream(stream), m_custom_data_serializer(custom_data_serializer), + m_cache_encrypt(cache_encrypt), m_version(version) { if (version != Serialize::Version::UNSPECIFIED && version != Serialize::Version::IR_V10 && version != Serialize::Version::IR_V11) { @@ -1306,7 +1308,14 @@ bool pass::StreamSerialize::run_on_model(const std::shared_ptr& model // IR hdr.model_offset = m_stream.tellp(); - xml_doc.save(m_stream); + if (m_cache_encrypt) { + std::stringstream ss; + xml_doc.save(ss); + auto str_encode = m_cache_encrypt(ss.str()); + m_stream.write((char*)str_encode.c_str(), str_encode.length()); + } else { + xml_doc.save(m_stream); + } m_stream.flush(); const size_t file_size = m_stream.tellp(); diff --git a/src/plugins/intel_cpu/src/serialize.cpp b/src/plugins/intel_cpu/src/serialize.cpp index 08f2fefcfd9010..266a89aa0b0cf3 100644 --- a/src/plugins/intel_cpu/src/serialize.cpp +++ b/src/plugins/intel_cpu/src/serialize.cpp @@ -6,6 +6,7 @@ #include #include "openvino/pass/serialize.hpp" +#include "openvino/util/codec_xor.hpp" #include "transformations/utils/utils.hpp" namespace ov { @@ -24,7 +25,8 @@ static void setInfo(pugi::xml_node& root, std::shared_ptr& model) { } } -ModelSerializer::ModelSerializer(std::ostream& ostream) : _ostream(ostream) {} +ModelSerializer::ModelSerializer(std::ostream& ostream) + : _ostream(ostream) {} void ModelSerializer::operator<<(const std::shared_ptr& model) { auto serializeInfo = [&](std::ostream& stream) { @@ -40,7 +42,7 @@ void ModelSerializer::operator<<(const std::shared_ptr& model) { xml_doc.save(stream); }; - ov::pass::StreamSerialize serializer(_ostream, serializeInfo); + ov::pass::StreamSerialize serializer(_ostream, serializeInfo, ov::util::codec_xor); serializer.run_on_model(std::const_pointer_cast(model->clone())); } @@ -98,6 +100,7 @@ void ModelDeserializer::operator>>(std::shared_ptr& model) { _istream.seekg(hdr.model_offset); xmlString.resize(hdr.model_size); _istream.read(const_cast(xmlString.c_str()), hdr.model_size); + xmlString = ov::util::codec_xor(xmlString); model = _model_builder(xmlString, std::move(dataBlob));