Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[core] Enhance HSM reader to handle exceptions in stream reads (#39575)

Pawel Raasz9041181274a6
diff --git a/src/inference/dev_api/openvino/runtime/hsm_reader.hpp b/src/inference/dev_api/openvino/runtime/hsm_reader.hpp
index 18bd6b09caa..a1632c53f35 100644
--- a/src/inference/dev_api/openvino/runtime/hsm_reader.hpp
+++ b/src/inference/dev_api/openvino/runtime/hsm_reader.hpp
@@ -67,14 +67,15 @@ public:
* ever makes. Always works, regardless of source; prefer #view() first to avoid the copy when possible.
* @param destination Must point to at least @p length writable bytes, kept alive by the caller - `void*`
* so any buffer type (`ov::Tensor::data()`, `vector<uint8_t>::data()`, ...) can be passed without a cast.
- * @return false if `[offset, offset + length)` exceeds this section's size, or the underlying read failed.
+ * @return false if `[offset, offset + length)` exceeds this section's size, or the underlying read failed,
+ * including a stream that reports failure by throwing.
*/
- bool read(size_t offset, size_t length, void* destination) const;
+ bool read(size_t offset, size_t length, void* destination) const noexcept;
/**
* @overload Reads the whole section; @p destination must point to at least #size() writable bytes.
*/
- bool read(void* destination) const {
+ bool read(void* destination) const noexcept {
return read(0, size(), destination);
}
diff --git a/src/inference/src/dev/hsm_model_id_codec.cpp b/src/inference/src/dev/hsm_model_id_codec.cpp
index 54839b992c8..508d54d9e50 100644
--- a/src/inference/src/dev/hsm_model_id_codec.cpp
+++ b/src/inference/src/dev/hsm_model_id_codec.cpp
@@ -4,20 +4,12 @@
#include "openvino/runtime/hsm_model_id_codec.hpp"
-#include <cassert>
-
-#include "openvino/core/except.hpp"
-
namespace ov::runtime::hsm {
inline namespace v1 {
std::optional<uint64_t> decode_model_id(const Section& section) noexcept {
uint64_t id{};
- try {
- return section.read(0, sizeof(id), &id) ? std::optional{id} : std::nullopt;
- } catch (...) {
- return std::nullopt;
- }
+ return section.read(0, sizeof(id), &id) ? std::optional{id} : std::nullopt;
}
} // namespace v1
diff --git a/src/inference/src/dev/hsm_reader.cpp b/src/inference/src/dev/hsm_reader.cpp
index a4867518712..8cebd66539d 100644
--- a/src/inference/src/dev/hsm_reader.cpp
+++ b/src/inference/src/dev/hsm_reader.cpp
@@ -31,7 +31,7 @@ ov::util::MemoryView Section::view() const noexcept {
}
}
-bool Section::read(size_t offset, size_t length, void* destination) const {
+bool Section::read(size_t offset, size_t length, void* destination) const noexcept {
if (const auto total = size(); destination == nullptr || offset > total || length > total - offset) {
return false;
}
@@ -50,9 +50,13 @@ bool Section::read(size_t offset, size_t length, void* destination) const {
},
[&](const StreamSource& stream_source) {
auto& [stream, payload_start] = stream_source;
- stream->seekg(payload_start + static_cast<std::streamoff>(offset));
- return static_cast<bool>(stream->read(reinterpret_cast<char*>(destination),
- static_cast<std::streamsize>(length)));
+ try {
+ stream->seekg(payload_start + static_cast<std::streamoff>(offset));
+ return static_cast<bool>(stream->read(reinterpret_cast<char*>(destination),
+ static_cast<std::streamsize>(length)));
+ } catch (...) {
+ return false;
+ }
},
},
m_source);
diff --git a/src/inference/tests/unit/hsm_model_id_codec.cpp b/src/inference/tests/unit/hsm_model_id_codec.cpp
index 961e102acb5..b3c95db78a2 100644
--- a/src/inference/tests/unit/hsm_model_id_codec.cpp
+++ b/src/inference/tests/unit/hsm_model_id_codec.cpp
@@ -12,6 +12,8 @@
#include <cstdint>
#include <cstring>
#include <limits>
+#include <optional>
+#include <sstream>
#include <type_traits>
#include "openvino/runtime/hsm_format.hpp"
@@ -47,4 +49,17 @@ TEST(HsmModelIdSectionTest, decode_reads_the_little_endian_wire_bytes) {
EXPECT_EQ(hsm::decode_model_id(inline_section(WireId{})), 0u);
}
+TEST(HsmModelIdSectionTest, decode_reports_a_throwing_stream_as_a_failed_read) {
+ hsm::ManifestEntry entry{};
+ entry.tag = hsm::SectionTag::make(hsm::model_id_tag.id(), /*is_inline=*/false);
+ entry.size = sizeof(uint64_t);
+ std::stringstream stream("abc"); // shorter than the declared section
+ stream.exceptions(std::ios::failbit | std::ios::badbit);
+ const hsm::Section section(entry, stream, std::streampos(0));
+
+ std::optional<uint64_t> decoded = sample_id;
+ EXPECT_NO_THROW(decoded = hsm::decode_model_id(section));
+ EXPECT_FALSE(decoded.has_value());
+}
+
} // namespace ov::test
diff --git a/src/inference/tests/unit/hsm_reader.cpp b/src/inference/tests/unit/hsm_reader.cpp
index b0a2a048ff5..561aaf7b240 100644
--- a/src/inference/tests/unit/hsm_reader.cpp
+++ b/src/inference/tests/unit/hsm_reader.cpp
@@ -8,9 +8,11 @@
#include <algorithm>
#include <cstring>
+#include <ios>
#include <limits>
#include <optional>
#include <sstream>
+#include <stdexcept>
#include <string>
#include <vector>
@@ -1045,6 +1047,100 @@ TEST(HsmReaderStreamTest, read_sections_dispatches_same_handler_type_as_memory_b
EXPECT_EQ(extension.last_payload, "device-specific-payload");
}
+TEST(HsmReaderStreamTest, section_read_reports_a_throwing_stream_as_a_failed_read) {
+ static_assert(noexcept(std::declval<const hsm::Section&>().read(nullptr)), "Section::read must not throw");
+
+ const auto tag = hsm::SectionTag::make_device_tag(1, false);
+ const auto blob = make_container({{make_pointer_entry(fake_device_id, tag), "unreadable-payload"}});
+ ::testing::NiceMock<MockStreamBuf> buf(blob);
+ {
+ ::testing::InSequence seq;
+ EXPECT_CALL(buf, xsgetn).Times(2); // open()'s header + manifest reads
+ EXPECT_CALL(buf, xsgetn).WillOnce(::testing::Return(0)); // the payload read
+ EXPECT_CALL(buf, xsgetn).Times(::testing::AnyNumber());
+ }
+ std::istream stream(&buf);
+ const auto reader = hsm::Reader::open(stream);
+ ASSERT_TRUE(reader.has_value());
+ stream.exceptions(std::ios::failbit | std::ios::badbit);
+
+ const auto section = reader->section(fake_device_id, tag.id());
+ ASSERT_TRUE(section.has_value());
+ std::vector<std::byte> destination(section->size());
+ bool read_ok = true;
+ EXPECT_NO_THROW(read_ok = section->read(destination.data()));
+ EXPECT_FALSE(read_ok);
+}
+
+// Throws from the streambuf itself, not through the stream state - an istream swallows it unless badbit is in the
+// exception mask.
+class ThrowingStreamBuf : public std::stringbuf {
+public:
+ bool armed = false;
+ bool throw_non_std_type = false;
+
+protected:
+ std::streamsize xsgetn(char* s, std::streamsize n) override {
+ if (armed) {
+ if (throw_non_std_type) {
+ throw 42;
+ }
+ throw std::runtime_error("streambuf failure");
+ }
+ return std::stringbuf::xsgetn(s, n);
+ }
+};
+
+class HsmReaderThrowingStreamBufTest : public ::testing::TestWithParam<std::ios::iostate> {};
+
+TEST_P(HsmReaderThrowingStreamBufTest, section_read_reports_a_throwing_streambuf_as_a_failed_read) {
+ const auto tag = hsm::SectionTag::make_device_tag(1, false);
+ const auto blob = make_container({{make_pointer_entry(fake_device_id, tag), "unreadable-payload"}});
+ ThrowingStreamBuf buf;
+ buf.str(std::string(blob.begin(), blob.end()));
+ std::istream stream(&buf);
+ const auto reader = hsm::Reader::open(stream);
+ ASSERT_TRUE(reader.has_value());
+ buf.armed = true;
+ stream.exceptions(GetParam());
+
+ const auto section = reader->section(fake_device_id, tag.id());
+ ASSERT_TRUE(section.has_value());
+ std::vector<std::byte> destination(section->size());
+ bool read_ok = true;
+ EXPECT_NO_THROW(read_ok = section->read(destination.data()));
+ EXPECT_FALSE(read_ok);
+}
+
+INSTANTIATE_TEST_SUITE_P(exception_mask,
+ HsmReaderThrowingStreamBufTest,
+ ::testing::Values(std::ios::goodbit, std::ios::badbit | std::ios::failbit),
+ [](const ::testing::TestParamInfo<std::ios::iostate>& info) {
+ return info.param == std::ios::goodbit ? std::string("default_mask")
+ : std::string("badbit_mask");
+ });
+
+TEST(HsmReaderStreamTest, section_read_reports_a_streambuf_throwing_a_non_std_type_as_a_failed_read) {
+ const auto tag = hsm::SectionTag::make_device_tag(1, false);
+ const auto blob = make_container({{make_pointer_entry(fake_device_id, tag), "unreadable-payload"}});
+ ThrowingStreamBuf buf;
+ buf.str(std::string(blob.begin(), blob.end()));
+ std::istream stream(&buf);
+ const auto reader = hsm::Reader::open(stream);
+ ASSERT_TRUE(reader.has_value());
+ buf.armed = true;
+ buf.throw_non_std_type = true;
+ // With badbit in the mask the stream rethrows the original exception.
+ stream.exceptions(std::ios::badbit);
+
+ const auto section = reader->section(fake_device_id, tag.id());
+ ASSERT_TRUE(section.has_value());
+ std::vector<std::byte> destination(section->size());
+ bool read_ok = true;
+ EXPECT_NO_THROW(read_ok = section->read(destination.data()));
+ EXPECT_FALSE(read_ok);
+}
+
TEST(HsmReaderStreamTest, read_sections_skips_an_entry_whose_bounds_validate_but_whose_read_fails) {
const auto blob = make_container({
{make_pointer_entry(fake_device_id, hsm::SectionTag::make_device_tag(1, false)), "unreadable-payload"},