store unreadable xml in model cache (#24202)

### Details:
 - *store unreadable xml in model cache*

### Tickets:
 - *CVS-138573*

---------

Co-authored-by: Chen Peter <peter.chen@intel.com>
This commit is contained in:
Fang Xu 2024-04-30 19:14:55 +08:00 committed by GitHub
parent 51b3f77e71
commit f8311f33b4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 48 additions and 3 deletions

View File

@ -0,0 +1,13 @@
// Copyright (C) 2018-2024 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#pragma once
#include <string>
namespace ov {
namespace util {
std::string codec_xor(const std::string& source_str);
} // namespace util
} // namespace ov

View File

@ -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;
}

View File

@ -67,11 +67,13 @@ public:
StreamSerialize(std::ostream& stream,
const std::function<void(std::ostream&)>& custom_data_serializer = {},
const std::function<std::string(const std::string&)>& cache_encrypt = {},
Serialize::Version version = Serialize::Version::UNSPECIFIED);
private:
std::ostream& m_stream;
std::function<void(std::ostream&)> m_custom_data_serializer;
std::function<std::string(const std::string&)> m_cache_encrypt;
const Serialize::Version m_version;
};
} // namespace pass

View File

@ -1247,9 +1247,11 @@ pass::Serialize::Serialize(const std::string& xmlPath, const std::string& binPat
pass::StreamSerialize::StreamSerialize(std::ostream& stream,
const std::function<void(std::ostream&)>& custom_data_serializer,
const std::function<std::string(const std::string&)>& 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<ov::Model>& 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();

View File

@ -6,6 +6,7 @@
#include <pugixml.hpp>
#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<ov::Model>& model) {
}
}
ModelSerializer::ModelSerializer(std::ostream& ostream) : _ostream(ostream) {}
ModelSerializer::ModelSerializer(std::ostream& ostream)
: _ostream(ostream) {}
void ModelSerializer::operator<<(const std::shared_ptr<ov::Model>& model) {
auto serializeInfo = [&](std::ostream& stream) {
@ -40,7 +42,7 @@ void ModelSerializer::operator<<(const std::shared_ptr<ov::Model>& 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<ov::Model>(model->clone()));
}
@ -98,6 +100,7 @@ void ModelDeserializer::operator>>(std::shared_ptr<ov::Model>& model) {
_istream.seekg(hdr.model_offset);
xmlString.resize(hdr.model_size);
_istream.read(const_cast<char*>(xmlString.c_str()), hdr.model_size);
xmlString = ov::util::codec_xor(xmlString);
model = _model_builder(xmlString, std::move(dataBlob));