Skip to content

Commit

Permalink
store unreadable xml in model cache (#24202)
Browse files Browse the repository at this point in the history
### Details:
 - *store unreadable xml in model cache*

### Tickets:
 - *CVS-138573*

---------

Co-authored-by: Chen Peter <[email protected]>
  • Loading branch information
xufang-lisa and peterchen-intel authored Apr 30, 2024
1 parent 51b3f77 commit f8311f3
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 3 deletions.
13 changes: 13 additions & 0 deletions src/common/util/include/openvino/util/codec_xor.hpp
Original file line number Diff line number Diff line change
@@ -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
18 changes: 18 additions & 0 deletions src/common/util/src/codec_xor.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
2 changes: 2 additions & 0 deletions src/core/include/openvino/pass/serialize.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,13 @@ class OPENVINO_API StreamSerialize : public ov::pass::ModelPass {

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
Expand Down
11 changes: 10 additions & 1 deletion src/core/src/pass/serialize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
Expand Down
7 changes: 5 additions & 2 deletions src/plugins/intel_cpu/src/serialize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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) {
Expand All @@ -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()));
}

Expand Down Expand Up @@ -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));

Expand Down

0 comments on commit f8311f3

Please sign in to comment.