Skip to content

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

[core] Add HSM format header for common blob (#38125)

Pawel Raasz15845bd402a5
diff --git a/src/common/util/include/openvino/util/memory.hpp b/src/common/util/include/openvino/util/memory.hpp
index 7b8fa80ac53..5fff01fc9c7 100644
--- a/src/common/util/include/openvino/util/memory.hpp
+++ b/src/common/util/include/openvino/util/memory.hpp
@@ -143,6 +143,7 @@ struct MemoryProperties {
/** @brief Read-only, non-owning view (pointer + size) of a buffer's contents. */
class MemoryView {
public:
+ constexpr MemoryView() noexcept = default;
constexpr MemoryView(const std::byte* data, size_t size) noexcept : m_data{data}, m_size{size} {}
constexpr const std::byte* data() const noexcept {
diff --git a/src/inference/dev_api/openvino/runtime/hsm_format.hpp b/src/inference/dev_api/openvino/runtime/hsm_format.hpp
new file mode 100644
index 00000000000..86551f02b16
--- /dev/null
+++ b/src/inference/dev_api/openvino/runtime/hsm_format.hpp
@@ -0,0 +1,464 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+/**
+ * @brief HSM (Header-Sections-Manifest) blob container format contract.
+ * @file openvino/runtime/hsm_format.hpp
+ *
+ * @verbatim
+ +---------------------------+---------------------------+---------------------------+
+ | Header | Section payloads | Manifest |
+ | 32 bytes | variable size, 0+ | ManifestEntry[], 32B each |
+ | offset 0 | offset 32 | offset = manifest_offset |
+ +---------------------------+---------------------------+---------------------------+
+ @endverbatim
+ */
+
+#pragma once
+
+#include <array>
+#include <cstddef>
+#include <cstdint>
+#include <optional>
+
+#include "openvino/core/except.hpp"
+#include "openvino/runtime/common.hpp"
+#include "openvino/util/container_util.hpp"
+#include "openvino/util/memory.hpp"
+
+namespace ov::runtime::hsm {
+
+using MagicType = std::array<uint8_t, 5>; //!< 5 raw ASCII bytes: container magic at offset 0.
+using SizeType = uint64_t; //!< Container/section size fields.
+using OffsetType = uint64_t; //!< Byte offsets within the container.
+
+/// Container magic (5 raw bytes).
+struct BlobMagic {
+ MagicType value{}; //!< Raw bytes. Zero-initialized: an unset BlobMagic never matches a valid magic.
+
+ /**
+ * @brief Byte-wise compare; hand-written loop (not `value == other.value`) since
+ * `std::array::operator==` isn't `constexpr` until C++20, and this must stay usable in a constant expression.
+ */
+ constexpr bool operator==(const BlobMagic& other) const noexcept {
+ for (size_t i = 0; i < value.size(); ++i) {
+ if (value[i] != other.value[i]) {
+ return false;
+ }
+ }
+ return true;
+ }
+ constexpr bool operator!=(const BlobMagic& other) const noexcept {
+ return !(*this == other);
+ }
+
+ static const BlobMagic single; //!< "OVBLS": container holds exactly one model blob.
+ static const BlobMagic multi; //!< "OVWSH": shared-context container - see #MultiBlobView.
+};
+
+inline constexpr BlobMagic BlobMagic::single{ov::util::make_array<uint8_t>('O', 'V', 'B', 'L', 'S')};
+inline constexpr BlobMagic BlobMagic::multi{ov::util::make_array<uint8_t>('O', 'V', 'W', 'S', 'H')};
+
+/**
+ * @brief The currently-active HSM v1.x format, as an `inline namespace` nested under #ov::runtime::hsm:
+ * `Header`, `FormatVersion`, `SectionTag`, `Tags`, `ManifestEntry` and friends all resolve as
+ * `ov::runtime::hsm::Header` etc. A future major version is a plain `namespace v2` alongside this one,
+ * explicit until promoted by moving `inline` here. Only #BlobMagic and the basic type aliases above stay
+ * shared across every version.
+ */
+inline namespace v1 {
+
+/**
+ * @brief HSM format version written by this codebase.
+ * @note Compatibility rule: a reader requires `header.version_major == major` exactly. #minor is additive-only
+ * (new optional tags/fields never gate compatibility) - a reader must accept any #minor value once #major matches,
+ * and must never fail solely on a #minor mismatch.
+ */
+struct FormatVersion {
+ static constexpr uint16_t major = 1; //!< Major version written by this codebase.
+ static constexpr uint8_t minor = 0; //!< Minor version written by this codebase.
+};
+
+/**
+ * @brief Fixed header at offset 0 of every HSM v1.x container. Compatibility lives in sections/manifest
+ * entries, not in this header's layout.
+ *
+ * @verbatim
+ +--------+------+-----------------+
+ | Offset | Size | Field |
+ +--------+------+-----------------+
+ | 0 | 5 | magic |
+ | 5 | 2 | version_major |
+ | 7 | 1 | version_minor |
+ | 8 | 8 | container_size |
+ | 16 | 8 | manifest_offset |
+ | 24 | 8 | manifest_size |
+ +--------+------+-----------------+
+ @endverbatim
+ *
+ * @note Native byte order (safe: all supported targets are little-endian); `#pragma pack(1)` keeps fields at
+ * their documented offsets with no implicit padding.
+ */
+#pragma pack(push, 1)
+struct Header {
+ BlobMagic magic; //!< Container magic - see #BlobMagic.
+ uint16_t version_major; //!< Format major version this container was written with - see #FormatVersion.
+ uint8_t version_minor; //!< Format minor version this container was written with - see #FormatVersion.
+
+ SizeType container_size; //!< Whole container size, in bytes.
+ OffsetType manifest_offset; //!< Byte offset of the first ManifestEntry.
+ SizeType manifest_size; //!< Manifest size in bytes; entry count = manifest_size / sizeof(ManifestEntry).
+
+ /**
+ * @brief Non-owning view of the header at the start of an HSM container buffer - reinterprets the bytes
+ * in place, no copy.
+ * @warning No bounds checking: caller must ensure `data` points to at least `sizeof(Header)` readable bytes.
+ */
+ static const Header& view(const uint8_t* data) noexcept {
+ return *reinterpret_cast<const Header*>(data);
+ }
+};
+#pragma pack(pop)
+static_assert(sizeof(Header) == 32,
+ "Header layout changed - bump FormatVersion::major/document the change before touching "
+ "this struct; readers and writers must be updated together.");
+
+/**
+ * @brief Device/plugin that owns a ManifestEntry's section. Device catalog and collision rules follow later.
+ * @note Paired with #SectionTag: a reader always compares `(device, tag)` together, never `tag` alone, so two
+ * devices may reuse the same tag id for unrelated content.
+ */
+using DeviceId = uint8_t;
+
+/** @brief Reserved #DeviceId for sections not tied to one specific device. */
+inline constexpr DeviceId any_device_id = 0;
+
+/**
+ * @brief Boundary in the 23-bit #SectionTag id space: Core ids are `< core_tag_id_range_end`;
+ * #SectionTag::make_device_tag() only produces ids at/above it, so a device can never collide with a Core tag.
+ */
+inline constexpr uint32_t core_tag_id_range_end = 0x1000;
+
+/** @brief Maximum tag id representable by the 23-bit #SectionTag id space. */
+inline constexpr uint32_t max_tag_id = 0x7FFFFF;
+
+/**
+ * @brief Semantic content of a ManifestEntry section (or inline payload); a raw 3-byte value split into a
+ * 23-bit tag id (Core-owned range vs. device-specific range) plus a pointer/inline mode flag in bit 7 of
+ * `value[0]` - see #is_inline().
+ * @note Unknown-tag rule: a reader must skip any `(device, tag)` it doesn't recognize, never fail import.
+ */
+struct SectionTag {
+ std::array<uint8_t, 3> value{}; //!< Raw bytes; bit 7 of `value[0]` is the pointer/inline flag.
+
+ /**
+ * @brief Packs a 23-bit tag id and the pointer/inline mode flag into a wire SectionTag.
+ * @param id Tag id (Core-owned or device-specific range - see #SectionTag); only the low 23 bits are kept.
+ * @param is_inline true for inline-mode, false for pointer-mode.
+ */
+ static constexpr SectionTag make(uint32_t id, bool is_inline) noexcept {
+ return {{static_cast<uint8_t>(((id >> 16) & 0x7F) | (is_inline ? 0x80 : 0x00)),
+ static_cast<uint8_t>((id >> 8) & 0xFF),
+ static_cast<uint8_t>(id & 0xFF)}};
+ }
+
+ /**
+ * @brief Builds a device-specific wire SectionTag from a device-local id; always lands at/above
+ * #core_tag_id_range_end, so it can never collide with a Core tag.
+ * @param local_id Device-local id, starting at 0; must be `<= max_tag_id - core_tag_id_range_end`.
+ * @param is_inline true for inline-mode, false for pointer-mode.
+ */
+ static constexpr SectionTag make_device_tag(uint32_t local_id, bool is_inline) noexcept {
+ OPENVINO_DEBUG_ASSERT(local_id <= max_tag_id - core_tag_id_range_end);
+ return make(core_tag_id_range_end + local_id, is_inline);
+ }
+
+ /// The 23-bit tag id (mode bit masked out).
+ constexpr uint32_t id() const noexcept {
+ return (static_cast<uint32_t>(value[0] & 0x7F) << 16) | (static_cast<uint32_t>(value[1]) << 8) |
+ static_cast<uint32_t>(value[2]);
+ }
+
+ /// True if the entry carrying this tag stores its payload directly (bit 7 of `value[0]` set).
+ constexpr bool is_inline() const noexcept {
+ return (value[0] & 0x80) != 0;
+ }
+
+ /// True if the entry carrying this tag points at a section stored elsewhere.
+ constexpr bool is_pointer() const noexcept {
+ return !is_inline();
+ }
+
+ /// Inverse of #make_device_tag(): recovers the device-local id (`id() - core_tag_id_range_end`).
+ constexpr uint32_t device_local_id() const noexcept {
+ return id() - core_tag_id_range_end;
+ }
+};
+
+/**
+ * @brief Core-owned HSM tag identifiers - explicit wire values so reordering is safe; never change or reuse a
+ * value once shipped. #sentinel_count auto-tracks the count and must stay last.
+ */
+enum class Tags : uint32_t {
+ invalid = 0, //!< Reserved: never a real tag id.
+ model_id = 1, //!< See #model_id.
+ model = 2, //!< See #model.
+ runtime_requirements = 3, //!< See #runtime_requirements_tag.
+ // Add new Core tags above this line only, each with the next explicit value - never change or reuse an
+ // existing tag's value.
+ sentinel_count, // Not a real tag id - always exactly one past the last real entry above.
+};
+static_assert(static_cast<uint32_t>(Tags::sentinel_count) <= core_tag_id_range_end,
+ "Too many Core tags defined for core_tag_id_range_end - widen the boundary.");
+
+inline constexpr uint32_t model_id = static_cast<uint32_t>(Tags::model_id); //!< Model identifier (e.g. a hash).
+inline constexpr uint32_t model = static_cast<uint32_t>(Tags::model); //!< The serialized compiled model itself.
+inline constexpr uint32_t runtime_requirements = static_cast<uint32_t>(Tags::runtime_requirements);
+
+/// Wire tag for #model_id - always inline-mode.
+inline constexpr SectionTag model_id_tag = SectionTag::make(model_id, /*is_inline=*/true);
+
+/// Wire tag for #model - always pointer-mode.
+inline constexpr SectionTag model_tag = SectionTag::make(model, /*is_inline=*/false);
+
+/**
+ * @brief Wire tag for #runtime_requirements - always pointer-mode. Payload is opaque to the common
+ * reader/format: this contract only reserves the tag and its bounds (like any pointer-mode section) -
+ * interpreting and enforcing the encoded requirements is entirely the emitting device/plugin's
+ * responsibility, typically via #ISectionExtension. No expression scheme is defined at this layer
+ * (out of scope here; a richer format, if any, belongs to the tag registry).
+ */
+inline constexpr SectionTag runtime_requirements_tag = SectionTag::make(runtime_requirements, /*is_inline=*/false);
+
+/**
+ * @brief One fixed-size, 32-byte record of the manifest table (see #Header::manifest_offset).
+ *
+ * @verbatim
+ +--------+------+-------------------------+
+ | Offset | Size | Field |
+ +--------+------+-------------------------+
+ | 0 | 1 | device |
+ | 1 | 3 | tag |
+ | 4 | 4 | tag_reserved |
+ | 8 | 8 | offset |
+ | 16 | 8 | size |
+ | 24 | 8 | pointer_reserved |
+ +--------+------+-------------------------+
+ Pointer-mode: offset/size/pointer_reserved are three named 8-byte fields (pointer_reserved must be 0).
+ Inline-mode: the whole 24-byte region (offset 8-31) is reinterpreted as `inline_bytes` - up to 24 bytes
+ embedded directly in the entry, no separate section payload. Which mode applies is read from
+ #SectionTag::is_inline() - there is no separate mode byte.
+ @endverbatim
+ *
+ * @note `#pragma pack(1)` is required: without it, the 3-byte `SectionTag` followed by the 8-byte-aligned
+ * `offset` field would force padding, cascading misalignment through the rest of the struct.
+ * @note `tag_reserved`/`pointer_reserved` are zero unless the specific `(device, tag)` pair they belong to
+ * redefines their meaning - treat them as opaque otherwise.
+ */
+#pragma pack(push, 1)
+struct ManifestEntry {
+ DeviceId device; //!< Owning device.
+ SectionTag tag; //!< Section content;
+ std::array<uint8_t, 4> tag_reserved; //!< Zero, unless the specific (#device, #tag) pair redefines this.
+
+ union {
+ struct {
+ OffsetType offset; //!< Section payload offset (pointer-mode).
+ SizeType size; //!< Section payload size (pointer-mode).
+ std::array<uint8_t, 8> pointer_reserved; //!< Zero, unless (#device, #tag) redefines this.
+ };
+ std::array<uint8_t, 24> inline_bytes; //!< Inline payload, up to 24 bytes (inline-mode).
+ };
+};
+#pragma pack(pop)
+static_assert(sizeof(ManifestEntry) == 32, "ManifestEntry layout changed.");
+
+/**
+ * @brief Checks if the HSM header has a recognized magic number and version.
+ *
+ * @param header The HSM header to check.
+ * @return true if the header has a recognized magic number and version, false otherwise.
+ */
+constexpr bool is_recognized_header(const Header& header) noexcept {
+ return (header.magic == BlobMagic::single || header.magic == BlobMagic::multi) &&
+ header.version_major == FormatVersion::major && header.container_size >= sizeof(Header);
+}
+
+/**
+ * @brief Checks that the header fields satisfy basic consistency rules.
+ *
+ * Ensures that the magic number is valid, the version matches the expected format, the total size is at least
+ * as large as the header, the manifest size is a multiple of the manifest entry size, and the manifest offset
+ * and size fit within the total size.
+ * @param header The HSM header to validate.
+ * @return true if the header fields satisfy the basic consistency rules, false otherwise.
+ */
+constexpr bool is_valid_header_fields(const Header& header) noexcept {
+ return is_recognized_header(header) && header.manifest_size % sizeof(ManifestEntry) == 0 &&
+ header.manifest_offset >= sizeof(Header) && header.manifest_offset <= header.container_size &&
+ header.container_size - header.manifest_offset >= header.manifest_size;
+}
+
+/**
+ * @brief Checks that a manifest entry's section bounds are valid within the container.
+ *
+ * Returns true for an inline-mode entry (nothing to bounds-check), or a pointer-mode one whose offset/size stay
+ * within `header.manifest_offset` (payload always precedes the manifest - see #Header::manifest_offset).
+ * @param entry The manifest entry to validate.
+ * @param header The HSM header providing context for bounds checking.
+ * @return true if the section bounds are valid, false otherwise.
+ */
+constexpr bool is_valid_section_bounds(const ManifestEntry& entry, const Header& header) noexcept {
+ return entry.tag.is_inline() || (entry.offset >= sizeof(Header) && entry.offset <= header.manifest_offset &&
+ header.manifest_offset - entry.offset >= entry.size);
+}
+
+/**
+ * @brief Reader-side plugin hook: interprets one manifest entry's section content. A concrete extension
+ * self-dispatches by checking `(entry.device, entry.tag)` and returning whether it recognized it - per the
+ * unknown-tag rule on #SectionTag, the reader must skip any entry no extension recognizes, never fail
+ * import.
+ * @note Forward-looking contract only - not yet wired to a real reader.
+ */
+class ISectionExtension {
+public:
+ virtual ~ISectionExtension() = default;
+
+ /**
+ * @brief Attempts to interpret @p entry's section content.
+ * @param entry Manifest entry being considered - not necessarily one this extension owns.
+ * @param section Bounds-checked view of the payload - #ContainerView::section() for a pointer-mode
+ * entry, or `entry.inline_bytes` for an inline-mode one; never a raw, unchecked pointer.
+ * @return true if `(entry.device, entry.tag)` was recognized and handled, false otherwise.
+ */
+ virtual bool read_section(const ManifestEntry& entry, ov::util::MemoryView section) = 0;
+};
+
+/**
+ * @brief Read-only, zero-copy view of an entire in-memory HSM container: header, manifest and pointer-mode
+ */
+class OPENVINO_RUNTIME_API ContainerView {
+public:
+ /// Empty (zero-size, null-data) view - #validate() is false for it.
+ constexpr ContainerView() noexcept = default;
+ explicit constexpr ContainerView(const std::byte* data, size_t size) noexcept : m_view{data, size} {}
+ explicit ContainerView(const uint8_t* data, size_t size) noexcept
+ : ContainerView{reinterpret_cast<const std::byte*>(data), size} {}
+
+ constexpr size_t size() const noexcept {
+ return m_view.size();
+ }
+
+ /**
+ * @brief Returns the header at the start of the buffer.
+ * @return Reference to the header at the start of the buffer.
+ */
+ const Header& header() const noexcept;
+
+ /**
+ * @brief Returns the first manifest entry at `header().manifest_offset`.
+ * @return The first manifest entry at `header().manifest_offset`.
+ */
+ const ManifestEntry& manifest() const noexcept;
+
+ /// Number of entries at #manifest().
+ size_t manifest_count() const noexcept {
+ return static_cast<size_t>(header().manifest_size / sizeof(ManifestEntry));
+ }
+
+ /**
+ * @brief Bounds-checked payload bytes of a pointer-mode manifest entry; empty view for an invalid or inline entry.
+ */
+ constexpr ov::util::MemoryView section(const ManifestEntry& entry) const noexcept {
+ if (entry.tag.is_inline() || entry.offset > size() || entry.size > size() - entry.offset) {
+ return {};
+ } else {
+ return {begin() + static_cast<size_t>(entry.offset), static_cast<size_t>(entry.size)};
+ }
+ }
+
+ /**
+ * @brief Basic structural integrity check: magic, and that the header/manifest/pointer-mode section
+ * bounds all stay within #size() with no overflow. Doesn't interpret tag-specific (device, tag) content.
+ */
+ bool validate() const noexcept;
+
+private:
+ constexpr const std::byte* begin() const noexcept {
+ return m_view.begin();
+ }
+ constexpr const std::byte* end() const noexcept {
+ return m_view.end();
+ }
+
+ ov::util::MemoryView m_view{};
+};
+
+/**
+ * @brief View over a multi-blob HSM file, providing access to individual blob containers.
+ *
+ * This class allows iterating over and accessing the #BlobMagic::single containers within a multi-blob HSM file,
+ * skipping over shared-context containers.
+
+ */
+class OPENVINO_RUNTIME_API MultiBlobView {
+public:
+ // CVS-191965: finalize this class (multi-blob/shared-context implementation).
+ /// Constructs a view over `[data, data + size)` - no copy, no validation of its contents.
+ explicit constexpr MultiBlobView(const std::byte* data, size_t size) noexcept : m_view{data, size} {}
+
+ /// @overload uint8_t variant of MultiBlobView(const std::byte*, size_t).
+ explicit MultiBlobView(const uint8_t* data, size_t size) noexcept
+ : MultiBlobView{reinterpret_cast<const std::byte*>(data), size} {}
+
+ constexpr size_t size() const noexcept {
+ return m_view.size();
+ }
+
+ /**
+ * @brief Returns the number of #BlobMagic::single containers in the multi-blob HSM file.
+ * @return The number of #BlobMagic::single containers in the multi-blob HSM file.
+ */
+ size_t blob_count() const noexcept;
+
+ /**
+ * @brief The `index`-th #BlobMagic::single container (shared-context containers don't count towards `index`).
+ * @return An empty (zero-size) view if `index >= blob_count()`.
+ */
+ ContainerView blob_at(size_t index) const noexcept;
+
+private:
+ /**
+ * @brief Describes the result of advancing past one container in a memory view.
+ *
+ * This struct contains the remaining view after the container and a flag indicating whether the container was a
+ * blob.
+ */
+ struct NextContainer {
+ // Definitions in hsm_format.cpp: only used there, so out-of-line costs no inlining (same TU).
+ NextContainer(ov::util::MemoryView remaining, size_t container_size, bool is_blob) noexcept;
+
+ const ov::util::MemoryView& remaining() const noexcept;
+ size_t container_size() const noexcept;
+ bool is_blob() const noexcept;
+
+ private:
+ ov::util::MemoryView m_remaining; //!< The remaining view after the container.
+ size_t m_container_size; //!< Size of the container just advanced past.
+ bool m_is_blob; //!< True if the container was a blob.
+ };
+
+ /**
+ * @brief Advances past one container in the given view.
+ * @param view The memory view starting at the container header.
+ * @return A NextContainer describing the remaining view and whether it was a blob, or `std::nullopt` if the header
+ * is invalid.
+ */
+ static std::optional<NextContainer> advance_container(const ov::util::MemoryView& view) noexcept;
+
+ ov::util::MemoryView m_view;
+};
+
+} // namespace v1
+} // namespace ov::runtime::hsm
diff --git a/src/inference/sources.cmake b/src/inference/sources.cmake
index 872ed968db9..cecc244a12c 100644
--- a/src/inference/sources.cmake
+++ b/src/inference/sources.cmake
@@ -20,6 +20,7 @@ set(OV_RUNTIME_SRCS
${CMAKE_CURRENT_LIST_DIR}/src/dev/core_impl.cpp
${CMAKE_CURRENT_LIST_DIR}/src/dev/device_id_parser.cpp
${CMAKE_CURRENT_LIST_DIR}/src/dev/exec_model_info.cpp
+ ${CMAKE_CURRENT_LIST_DIR}/src/dev/hsm_format.cpp
${CMAKE_CURRENT_LIST_DIR}/src/dev/iasync_infer_request.cpp
${CMAKE_CURRENT_LIST_DIR}/src/dev/icompiled_model.cpp
${CMAKE_CURRENT_LIST_DIR}/src/dev/iplugin.cpp
diff --git a/src/inference/src/dev/hsm_format.cpp b/src/inference/src/dev/hsm_format.cpp
new file mode 100644
index 00000000000..871a7ffa039
--- /dev/null
+++ b/src/inference/src/dev/hsm_format.cpp
@@ -0,0 +1,102 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "openvino/runtime/hsm_format.hpp"
+
+#include <algorithm>
+
+namespace ov::runtime::hsm {
+
+MultiBlobView::NextContainer::NextContainer(ov::util::MemoryView remaining,
+ size_t container_size,
+ bool is_blob) noexcept
+ : m_remaining(remaining),
+ m_container_size(container_size),
+ m_is_blob(is_blob) {}
+
+const ov::util::MemoryView& MultiBlobView::NextContainer::remaining() const noexcept {
+ return m_remaining;
+}
+
+size_t MultiBlobView::NextContainer::container_size() const noexcept {
+ return m_container_size;
+}
+
+bool MultiBlobView::NextContainer::is_blob() const noexcept {
+ return m_is_blob;
+}
+
+const Header& ContainerView::header() const noexcept {
+ return Header::view(reinterpret_cast<const uint8_t*>(begin()));
+}
+
+const ManifestEntry& ContainerView::manifest() const noexcept {
+ return *reinterpret_cast<const ManifestEntry*>(begin() + header().manifest_offset);
+}
+
+bool ContainerView::validate() const noexcept {
+ if (static_cast<size_t>(end() - begin()) < sizeof(Header)) {
+ return false;
+ }
+ const auto& hdr = header();
+ if (!is_valid_header_fields(hdr) || hdr.container_size > size()) {
+ return false;
+ }
+
+ if (hdr.manifest_size == 0) {
+ return true;
+ }
+
+ const auto* entries = &manifest();
+ return std::all_of(entries, entries + manifest_count(), [&hdr](const auto& entry) {
+ return is_valid_section_bounds(entry, hdr);
+ });
+}
+
+size_t MultiBlobView::blob_count() const noexcept {
+ auto view = m_view;
+ size_t count = 0;
+ while (view.size() >= sizeof(Header)) {
+ const auto next = advance_container(view);
+ if (!next) {
+ break;
+ }
+ count += next->is_blob() ? 1 : 0;
+ view = next->remaining();
+ }
+ return count;
+}
+
+ContainerView MultiBlobView::blob_at(size_t index) const noexcept {
+ auto view = m_view;
+ while (view.size() >= sizeof(Header)) {
+ const auto next = advance_container(view);
+ if (!next) {
+ break;
+ }
+ if (next->is_blob()) {
+ if (index == 0) {
+ return ContainerView{view.data(), next->container_size()};
+ }
+ --index;
+ }
+ view = next->remaining();
+ }
+ return {};
+}
+
+std::optional<MultiBlobView::NextContainer> MultiBlobView::advance_container(
+ const ov::util::MemoryView& view) noexcept {
+ const auto& hdr = Header::view(reinterpret_cast<const uint8_t*>(view.data()));
+ if (!is_recognized_header(hdr) || hdr.container_size > view.size()) {
+ return std::nullopt;
+ } else {
+ const auto container_size = static_cast<size_t>(hdr.container_size);
+ return NextContainer{ov::util::MemoryView{view.data() + container_size, view.size() - container_size},
+ container_size,
+ hdr.magic == BlobMagic::single};
+ }
+}
+
+} // namespace ov::runtime::hsm
diff --git a/src/inference/tests/unit/CMakeLists.txt b/src/inference/tests/unit/CMakeLists.txt
index 2f8a9a92c64..466706148c2 100644
--- a/src/inference/tests/unit/CMakeLists.txt
+++ b/src/inference/tests/unit/CMakeLists.txt
@@ -26,6 +26,7 @@ ov_add_test_target(
${CMAKE_CURRENT_SOURCE_DIR}/cpu_stream_info_test.cpp
${CMAKE_CURRENT_SOURCE_DIR}/executor_config_test.cpp
${CMAKE_CURRENT_SOURCE_DIR}/executor_manager_tests.cpp
+ ${CMAKE_CURRENT_SOURCE_DIR}/hsm_format.cpp
${CMAKE_CURRENT_SOURCE_DIR}/infer_request_test.cpp
${CMAKE_CURRENT_SOURCE_DIR}/iplugin_test.cpp
${CMAKE_CURRENT_SOURCE_DIR}/make_default_multi_threaded_test.cpp
@@ -42,6 +43,7 @@ ov_add_test_target(
LINK_LIBRARIES
unit_test_utils
ADD_CLANG_FORMAT
+ TESTS_PER_SOURCE
LABELS
OV UNIT RUNTIME
CHECK_SOURCES_LISTED
diff --git a/src/inference/tests/unit/hsm_format.cpp b/src/inference/tests/unit/hsm_format.cpp
new file mode 100644
index 00000000000..e2f36931bb3
--- /dev/null
+++ b/src/inference/tests/unit/hsm_format.cpp
@@ -0,0 +1,550 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "openvino/runtime/hsm_format.hpp"
+
+#include <gtest/gtest.h>
+
+#include <cstddef>
+#include <cstring>
+#include <limits>
+#include <string>
+#include <vector>
+
+namespace ov::test {
+namespace hsm = ov::runtime::hsm;
+namespace {
+
+// Byte size of one manifest entry; used for raw placeholder manifest bytes below.
+constexpr size_t k_manifest_entry_size = 32;
+
+// Raw bytes of an HSM container: header, then section_payload, then manifest (already-serialized).
+std::vector<uint8_t> make_container(const hsm::BlobMagic& magic,
+ const std::vector<uint8_t>& section_payload,
+ const std::vector<uint8_t>& manifest) {
+ hsm::Header header{};
+ header.magic = magic;
+ header.version_major = hsm::FormatVersion::major;
+ header.version_minor = hsm::FormatVersion::minor;
+ header.manifest_offset = sizeof(hsm::Header) + section_payload.size();
+ header.manifest_size = manifest.size();
+ header.container_size = header.manifest_offset + header.manifest_size;
+
+ std::vector<uint8_t> buffer;
+ buffer.reserve(header.container_size);
+ const auto* header_bytes = reinterpret_cast<const uint8_t*>(&header);
+ buffer.insert(buffer.end(), header_bytes, header_bytes + sizeof(header));
+ buffer.insert(buffer.end(), section_payload.begin(), section_payload.end());
+ buffer.insert(buffer.end(), manifest.begin(), manifest.end());
+ return buffer;
+}
+
+// Single compiled model container.
+std::vector<uint8_t> make_single_blob_container(const std::vector<uint8_t>& section_payload,
+ const std::vector<uint8_t>& manifest) {
+ return make_container(hsm::BlobMagic::single, section_payload, manifest);
+}
+
+// One container tagged as part of a multi-blob file.
+std::vector<uint8_t> make_multi_container(const std::vector<uint8_t>& section_payload,
+ const std::vector<uint8_t>& manifest) {
+ return make_container(hsm::BlobMagic::multi, section_payload, manifest);
+}
+
+// blob_count MULTI-magic containers concatenated back-to-back
+std::vector<uint8_t> make_multi_blob_container(size_t blob_count) {
+ std::vector<uint8_t> buffer;
+ for (size_t i = 0; i < blob_count; ++i) {
+ const auto blob = make_multi_container({}, {});
+ buffer.insert(buffer.end(), blob.begin(), blob.end());
+ }
+ return buffer;
+}
+
+// Mandatory shared-context container (MULTI) followed by blob_count model containers (SINGLE).
+std::vector<uint8_t> make_multi_blob_file(size_t blob_count) {
+ std::vector<uint8_t> buffer = make_multi_container({}, {});
+ for (size_t i = 0; i < blob_count; ++i) {
+ const auto blob = make_single_blob_container({}, {});
+ buffer.insert(buffer.end(), blob.begin(), blob.end());
+ }
+ return buffer;
+}
+
+// Sample container for HsmHeaderTest: 2-byte "OV" section payload, 2 dummy manifest entries.
+std::vector<uint8_t> make_sample_single_blob_container() {
+ const std::vector<uint8_t> manifest(2 * k_manifest_entry_size, 0xCD);
+ return make_single_blob_container({'O', 'V'}, manifest);
+}
+
+// Sample container with real entries: inline model_id tag + pointer-mode model tag ("OV" payload).
+std::vector<uint8_t> make_sample_container_with_entries() {
+ hsm::ManifestEntry id_entry{};
+ id_entry.tag = hsm::model_id_tag;
+ id_entry.inline_bytes = {0xAA, 0xBB, 0xCC, 0xDD};
+
+ hsm::ManifestEntry model_entry{};
+ model_entry.tag = hsm::model_tag;
+ model_entry.offset = sizeof(hsm::Header);
+ model_entry.size = 2;
+
+ std::vector<uint8_t> manifest(2 * sizeof(hsm::ManifestEntry));
+ std::memcpy(manifest.data(), &id_entry, sizeof(id_entry));
+ std::memcpy(manifest.data() + sizeof(id_entry), &model_entry, sizeof(model_entry));
+
+ return make_single_blob_container({'O', 'V'}, manifest);
+}
+
+// Minimal ISectionExtension: recognizes one (device, tag id) pair, records the section size it saw.
+class RecordingExtension : public hsm::ISectionExtension {
+public:
+ static constexpr hsm::DeviceId owned_device = 7;
+ static constexpr uint32_t owned_tag_id = 42;
+
+ bool read_section(const hsm::ManifestEntry& entry, ov::util::MemoryView section) override {
+ if (entry.device != owned_device || entry.tag.id() != owned_tag_id) {
+ return false;
+ }
+ last_section_size = section.size();
+ return true;
+ }
+
+ size_t last_section_size = 0;
+};
+
+} // namespace
+
+// --- HSM wire-format layout/version compatibility --------------------------------------------------------
+// Pinned to FormatVersion::major == 1. A failure in this fixture means the on-disk byte layout changed:
+static_assert(hsm::FormatVersion::major == 1,
+ "FormatVersion::major changed - review HsmFormatLayoutCompatibilityTest, update the pinned "
+ "layout checks below to match the new format, then update this assert.");
+
+class HsmFormatLayoutCompatibilityTest : public ::testing::Test {};
+
+TEST_F(HsmFormatLayoutCompatibilityTest, hsm_header_layout) {
+ static_assert(sizeof(hsm::MagicType) == 5, "MagicType width changed");
+ static_assert(sizeof(hsm::Header) == 32, "Header total size changed");
+ static_assert(offsetof(hsm::Header, magic) == 0, "Header::magic offset changed");
+ static_assert(offsetof(hsm::Header, version_major) == 5, "Header::version_major offset changed");
+ static_assert(offsetof(hsm::Header, version_minor) == 7, "Header::version_minor offset changed");
+ static_assert(offsetof(hsm::Header, container_size) == 8, "Header::container_size offset changed");
+ static_assert(offsetof(hsm::Header, manifest_offset) == 16, "Header::manifest_offset offset changed");
+ static_assert(offsetof(hsm::Header, manifest_size) == 24, "Header::manifest_size offset changed");
+
+ // Runtime echo, for visibility in test reports (the static_asserts above already block the build on a break).
+ EXPECT_EQ(sizeof(hsm::Header), 32u);
+ EXPECT_EQ(offsetof(hsm::Header, manifest_size), 24u);
+}
+
+TEST_F(HsmFormatLayoutCompatibilityTest, manifest_entry_layout) {
+ static_assert(sizeof(hsm::ManifestEntry) == 32, "ManifestEntry total size changed");
+ static_assert(offsetof(hsm::ManifestEntry, device) == 0, "ManifestEntry::device offset changed");
+ static_assert(offsetof(hsm::ManifestEntry, tag) == 1, "ManifestEntry::tag offset changed");
+ static_assert(sizeof(hsm::SectionTag) == 3, "SectionTag width changed");
+ static_assert(offsetof(hsm::ManifestEntry, tag_reserved) == 4, "ManifestEntry::tag_reserved offset changed");
+ static_assert(offsetof(hsm::ManifestEntry, offset) == 8, "ManifestEntry::offset offset changed");
+ static_assert(offsetof(hsm::ManifestEntry, size) == 16, "ManifestEntry::size offset changed");
+ static_assert(offsetof(hsm::ManifestEntry, pointer_reserved) == 24,
+ "ManifestEntry::pointer_reserved offset changed");
+ static_assert(offsetof(hsm::ManifestEntry, inline_bytes) == 8, "ManifestEntry::inline_bytes offset changed");
+ static_assert(sizeof(hsm::ManifestEntry{}.inline_bytes) == 24, "ManifestEntry::inline_bytes width changed");
+
+ EXPECT_EQ(sizeof(hsm::ManifestEntry), 32u);
+}
+
+TEST_F(HsmFormatLayoutCompatibilityTest, blob_magic_is_compile_time_comparable) {
+ // BlobMagic::operator==/!= must be usable in a constant expression, not just nominally marked constexpr.
+ static_assert(hsm::BlobMagic::single == hsm::BlobMagic::single, "BlobMagic::operator== must be constexpr-usable");
+ static_assert(hsm::BlobMagic::single != hsm::BlobMagic::multi, "BlobMagic::operator!= must be constexpr-usable");
+ SUCCEED();
+}
+
+TEST_F(HsmFormatLayoutCompatibilityTest, section_tag_packs_id_and_mode_at_compile_time) {
+ static_assert(hsm::SectionTag::make(1, true).id() == 1u, "tag id round-trip (inline)");
+ static_assert(hsm::SectionTag::make(1, false).id() == 1u, "tag id round-trip (pointer)");
+ static_assert(hsm::SectionTag::make(0x7FFFFF, true).id() == 0x7FFFFFu, "tag id round-trip (max 23-bit id)");
+
+ static_assert(hsm::SectionTag::make(1, true).is_inline(), "make(id, true) must produce an inline-mode tag");
+ static_assert(!hsm::SectionTag::make(1, true).is_pointer(), "inline-mode tag must not also read as pointer-mode");
+ static_assert(hsm::SectionTag::make(2, false).is_pointer(), "make(id, false) must produce a pointer-mode tag");
+ static_assert(!hsm::SectionTag::make(2, false).is_inline(), "pointer-mode tag must not also read as inline-mode");
+
+ SUCCEED();
+}
+
+TEST_F(HsmFormatLayoutCompatibilityTest, core_tags_have_fixed_mode) {
+ static_assert(hsm::model_id_tag.id() == hsm::model_id, "model_id_tag id");
+ static_assert(hsm::model_id_tag.is_inline(), "model_id_tag must always be inline-mode");
+
+ static_assert(hsm::model_tag.id() == hsm::model, "model_tag id");
+ static_assert(hsm::model_tag.is_pointer(), "model_tag must always be pointer-mode");
+
+ static_assert(hsm::runtime_requirements_tag.id() == hsm::runtime_requirements, "runtime_requirements_tag id");
+ static_assert(hsm::runtime_requirements_tag.is_pointer(), "runtime_requirements_tag must always be pointer-mode");
+
+ SUCCEED();
+}
+
+TEST_F(HsmFormatLayoutCompatibilityTest, device_tags_cannot_collide_with_core_tags) {
+ // A device tag built from local id 0 must never land in the Core-owned range, no matter how small the
+ // local id is - this is the whole point of SectionTag::make_device_tag() vs. picking a raw absolute id by hand.
+ static_assert(hsm::SectionTag::make_device_tag(0, true).device_local_id() == 0,
+ "make_device_tag()/device_local_id() must round-trip the local id");
+ static_assert(hsm::SectionTag::make_device_tag(0, true).id() == hsm::core_tag_id_range_end,
+ "make_device_tag(0, ...) must land exactly at the range boundary");
+ static_assert(hsm::SectionTag::make_device_tag(0, true).id() >= hsm::core_tag_id_range_end,
+ "device tag ids must never fall below core_tag_id_range_end");
+ static_assert(hsm::model_id < hsm::core_tag_id_range_end, "model_id must stay below core_tag_id_range_end");
+ static_assert(hsm::model < hsm::core_tag_id_range_end, "model must stay below core_tag_id_range_end");
+
+ SUCCEED();
+}
+
+TEST_F(HsmFormatLayoutCompatibilityTest, core_tag_values_are_pinned) {
+ static_assert(static_cast<uint32_t>(hsm::Tags::invalid) == 0, "Tags::invalid value changed");
+ static_assert(static_cast<uint32_t>(hsm::Tags::model_id) == 1, "Tags::model_id value changed");
+ static_assert(static_cast<uint32_t>(hsm::Tags::model) == 2, "Tags::model value changed");
+ static_assert(static_cast<uint32_t>(hsm::Tags::runtime_requirements) == 3,
+ "Tags::runtime_requirements value changed");
+
+ SUCCEED();
+}
+// --- HSM wire-format layout/version compatibility end ----------------------------------------------------
+
+TEST(HsmHeaderTest, check_single_blob_header) {
+ const auto blob = make_sample_single_blob_container();
+ ASSERT_GE(blob.size(), sizeof(hsm::Header));
+
+ const auto header = hsm::Header::view(blob.data());
+ EXPECT_EQ(header.magic, hsm::BlobMagic::single);
+ EXPECT_EQ(header.version_major, hsm::FormatVersion::major);
+ EXPECT_EQ(header.version_minor, hsm::FormatVersion::minor);
+ EXPECT_EQ(header.container_size, blob.size());
+ EXPECT_EQ(header.manifest_offset, sizeof(hsm::Header) + 2u); // 2 bytes of section payload
+ EXPECT_EQ(header.manifest_size, 2 * k_manifest_entry_size); // 2 entries in the manifest
+};
+
+TEST(HsmHeaderTest, check_multi_blob_single_blob) {
+ const auto blob = make_multi_blob_container(1);
+ ASSERT_GE(blob.size(), sizeof(hsm::Header));
+
+ const auto header = hsm::Header::view(blob.data());
+ EXPECT_EQ(header.magic, hsm::BlobMagic::multi);
+ EXPECT_EQ(header.version_major, hsm::FormatVersion::major);
+ EXPECT_EQ(header.version_minor, hsm::FormatVersion::minor);
+ EXPECT_EQ(header.container_size, blob.size());
+ EXPECT_EQ(header.manifest_offset, sizeof(hsm::Header));
+ EXPECT_EQ(header.manifest_size, 0u); // no entries in the manifest
+};
+
+TEST(HsmHeaderTest, check_multi_blob_two_blobs) {
+ const auto blob = make_multi_blob_container(2);
+ ASSERT_GE(blob.size(), 2 * sizeof(hsm::Header));
+
+ const auto header1 = hsm::Header::view(blob.data());
+ EXPECT_EQ(header1.magic, hsm::BlobMagic::multi);
+ EXPECT_EQ(header1.version_major, hsm::FormatVersion::major);
+ EXPECT_EQ(header1.version_minor, hsm::FormatVersion::minor);
+ EXPECT_EQ(header1.container_size, sizeof(hsm::Header));
+ EXPECT_EQ(header1.manifest_offset, sizeof(hsm::Header));
+ EXPECT_EQ(header1.manifest_size, 0u); // no entries in the manifest
+
+ const auto header2 = hsm::Header::view(blob.data() + header1.container_size);
+ EXPECT_EQ(header2.magic, hsm::BlobMagic::multi);
+ EXPECT_EQ(header2.version_major, hsm::FormatVersion::major);
+ EXPECT_EQ(header2.version_minor, hsm::FormatVersion::minor);
+ EXPECT_EQ(header2.container_size, sizeof(hsm::Header));
+ EXPECT_EQ(header2.manifest_offset, sizeof(hsm::Header));
+ EXPECT_EQ(header2.manifest_size, 0u); // no entries in the manifest
+}
+
+TEST(HsmContainerViewTest, reads_header) {
+ const auto blob = make_sample_container_with_entries();
+ const hsm::ContainerView view(blob.data(), blob.size());
+ EXPECT_EQ(view.size(), blob.size());
+ EXPECT_EQ(view.header().magic, hsm::BlobMagic::single);
+}
+
+TEST(HsmContainerViewTest, reads_manifest_entries) {
+ const auto blob = make_sample_container_with_entries();
+ const hsm::ContainerView view(blob.data(), blob.size());
+
+ ASSERT_EQ(view.manifest_count(), 2u);
+ const auto* manifest = &view.manifest();
+
+ const auto& id_entry = manifest[0];
+ EXPECT_EQ(id_entry.tag.id(), hsm::model_id);
+ EXPECT_TRUE(id_entry.tag.is_inline());
+ EXPECT_EQ(id_entry.inline_bytes[0], 0xAA);
+
+ const auto& model_entry = manifest[1];
+ EXPECT_EQ(model_entry.tag.id(), hsm::model);
+ EXPECT_TRUE(model_entry.tag.is_pointer());
+ EXPECT_EQ(model_entry.size, 2u);
+}
+
+TEST(HsmContainerViewTest, reads_pointer_section) {
+ const auto blob = make_sample_container_with_entries();
+ const hsm::ContainerView view(blob.data(), blob.size());
+ const auto& model_entry = (&view.manifest())[1];
+ const auto section = view.section(model_entry);
+ ASSERT_EQ(section.size(), 2u);
+ EXPECT_EQ(std::string(reinterpret_cast<const char*>(section.data()), section.size()), "OV");
+}
+
+TEST(HsmContainerViewTest, section_rejects_inline_mode_entry) {
+ const auto blob = make_sample_container_with_entries();
+ const hsm::ContainerView view(blob.data(), blob.size());
+
+ hsm::ManifestEntry entry{};
+ entry.tag = hsm::model_id_tag; // inline-mode: offset/size below don't refer to a real section
+ entry.offset = sizeof(hsm::Header);
+ entry.size = 2;
+
+ EXPECT_EQ(view.section(entry).size(), 0u);
+}
+
+TEST(HsmContainerViewTest, section_rejects_out_of_bounds_offset) {
+ const auto blob = make_sample_container_with_entries();
+ const hsm::ContainerView view(blob.data(), blob.size());
+
+ hsm::ManifestEntry entry{};
+ entry.tag = hsm::model_tag;
+ entry.offset = view.size() + 1;
+ entry.size = 1;
+
+ EXPECT_EQ(view.section(entry).size(), 0u);
+}
+
+TEST(HsmContainerViewTest, section_rejects_out_of_bounds_size) {
+ const auto blob = make_sample_container_with_entries();
+ const hsm::ContainerView view(blob.data(), blob.size());
+
+ hsm::ManifestEntry entry{};
+ entry.tag = hsm::model_tag;
+ entry.offset = 0;
+ entry.size = view.size() + 1; // fits at offset 0 alone, but overruns the buffer
+
+ EXPECT_EQ(view.section(entry).size(), 0u);
+}
+
+TEST(HsmContainerViewValidateTest, accepts_well_formed_container) {
+ const auto blob = make_sample_container_with_entries();
+ const hsm::ContainerView view(blob.data(), blob.size());
+ EXPECT_TRUE(view.validate());
+}
+
+TEST(HsmContainerViewValidateTest, accepts_well_formed_multi_container) {
+ const auto blob = make_multi_container({}, {});
+ const hsm::ContainerView view(blob.data(), blob.size());
+ EXPECT_TRUE(view.validate());
+}
+
+TEST(HsmContainerViewValidateTest, rejects_bad_magic) {
+ auto blob = make_sample_container_with_entries();
+ blob[0] = 'X'; // corrupt Header::magic
+ const hsm::ContainerView view(blob.data(), blob.size());
+ EXPECT_FALSE(view.validate());
+}
+
+TEST(HsmContainerViewValidateTest, rejects_mismatched_major_version) {
+ auto blob = make_sample_container_with_entries();
+ auto header = hsm::Header::view(blob.data());
+ header.version_major = hsm::FormatVersion::major + 1;
+ std::memcpy(blob.data(), &header, sizeof(header));
+
+ const hsm::ContainerView view(blob.data(), blob.size());
+ EXPECT_FALSE(view.validate());
+}
+
+TEST(HsmContainerViewValidateTest, rejects_buffer_smaller_than_total_size) {
+ const auto blob = make_sample_container_with_entries();
+ // View sees fewer bytes than Header::container_size claims.
+ const hsm::ContainerView view(blob.data(), blob.size() - 1);
+ EXPECT_FALSE(view.validate());
+}
+
+TEST(HsmContainerViewValidateTest, rejects_out_of_bounds_manifest_offset) {
+ auto blob = make_sample_container_with_entries();
+ auto header = hsm::Header::view(blob.data());
+ header.manifest_offset = std::numeric_limits<hsm::OffsetType>::max();
+ std::memcpy(blob.data(), &header, sizeof(header));
+
+ const hsm::ContainerView view(blob.data(), blob.size());
+ EXPECT_FALSE(view.validate());
+}
+
+TEST(HsmContainerViewValidateTest, rejects_manifest_offset_inside_header) {
+ auto blob = make_sample_container_with_entries();
+ auto header = hsm::Header::view(blob.data());
+ header.manifest_offset = sizeof(hsm::Header) - 1; // would start reading manifest inside the header
+ std::memcpy(blob.data(), &header, sizeof(header));
+
+ const hsm::ContainerView view(blob.data(), blob.size());
+ EXPECT_FALSE(view.validate());
+}
+
+TEST(HsmContainerViewValidateTest, rejects_manifest_size_not_multiple_of_entry_size) {
+ auto blob = make_sample_container_with_entries();
+ auto header = hsm::Header::view(blob.data());
+ header.manifest_size -= 1; // no longer a multiple of sizeof(ManifestEntry)
+ std::memcpy(blob.data(), &header, sizeof(header));
+
+ const hsm::ContainerView view(blob.data(), blob.size());
+ EXPECT_FALSE(view.validate());
+}
+
+TEST(HsmContainerViewValidateTest, accepts_container_with_empty_manifest) {
+ const auto blob = make_single_blob_container({}, {}); // no section payload, no manifest entries
+ const hsm::ContainerView view(blob.data(), blob.size());
+ ASSERT_EQ(view.manifest_count(), 0u);
+ EXPECT_TRUE(view.validate());
+}
+
+TEST(HsmContainerViewValidateTest, rejects_out_of_bounds_section_offset) {
+ auto blob = make_sample_container_with_entries();
+ const auto header = hsm::Header::view(blob.data());
+
+ // Second manifest entry is the pointer-mode "model" tag.
+ const auto entry_offset = header.manifest_offset + sizeof(hsm::ManifestEntry);
+ hsm::ManifestEntry entry{};
+ std::memcpy(&entry, blob.data() + entry_offset, sizeof(entry));
+ entry.offset = std::numeric_limits<hsm::OffsetType>::max();
+ std::memcpy(blob.data() + entry_offset, &entry, sizeof(entry));
+
+ const hsm::ContainerView view(blob.data(), blob.size());
+ EXPECT_FALSE(view.validate());
+}
+
+TEST(HsmContainerViewValidateTest, rejects_section_overlapping_header) {
+ auto blob = make_sample_container_with_entries();
+ const auto header = hsm::Header::view(blob.data());
+
+ // Second manifest entry is the pointer-mode "model" tag; point it at the header itself.
+ const auto entry_offset = header.manifest_offset + sizeof(hsm::ManifestEntry);
+ hsm::ManifestEntry entry{};
+ std::memcpy(&entry, blob.data() + entry_offset, sizeof(entry));
+ entry.offset = 0;
+ entry.size = 4;
+ std::memcpy(blob.data() + entry_offset, &entry, sizeof(entry));
+
+ const hsm::ContainerView view(blob.data(), blob.size());
+ EXPECT_FALSE(view.validate());
+}
+
+TEST(HsmMultiBlobViewTest, empty_buffer_has_no_blobs) {
+ const hsm::MultiBlobView view(static_cast<const uint8_t*>(nullptr), 0);
+ EXPECT_EQ(view.blob_count(), 0u);
+}
+
+TEST(HsmMultiBlobViewTest, reads_single_blob) {
+ const auto blob = make_multi_blob_file(1);
+ const hsm::MultiBlobView view(blob.data(), blob.size());
+
+ ASSERT_EQ(view.blob_count(), 1u);
+ EXPECT_EQ(view.blob_at(0).header().magic, hsm::BlobMagic::single);
+}
+
+TEST(HsmMultiBlobViewTest, reads_multiple_blobs) {
+ const auto blob = make_multi_blob_file(3);
+ const hsm::MultiBlobView view(blob.data(), blob.size());
+
+ ASSERT_EQ(view.blob_count(), 3u);
+ for (size_t i = 0; i < view.blob_count(); ++i) {
+ EXPECT_EQ(view.blob_at(i).header().magic, hsm::BlobMagic::single);
+ }
+}
+
+TEST(HsmMultiBlobViewTest, skips_optional_shared_context_between_blobs) {
+ auto buffer = make_multi_blob_file(1); // mandatory shared context + 1 blob
+ const auto extra_context = make_multi_container({}, {}); // optional shared-context update
+ buffer.insert(buffer.end(), extra_context.begin(), extra_context.end());
+ const auto blob1 = make_single_blob_container({}, {});
+ buffer.insert(buffer.end(), blob1.begin(), blob1.end());
+
+ const hsm::MultiBlobView view(buffer.data(), buffer.size());
+ ASSERT_EQ(view.blob_count(), 2u); // the extra shared-context container doesn't count as a blob
+ EXPECT_EQ(view.blob_at(0).header().magic, hsm::BlobMagic::single);
+ EXPECT_TRUE(view.blob_at(0).validate());
+ EXPECT_EQ(view.blob_at(1).header().magic, hsm::BlobMagic::single);
+}
+
+TEST(HsmMultiBlobViewTest, stops_on_oversized_total_size) {
+ auto blob = make_multi_blob_file(1);
+ auto header = hsm::Header::view(blob.data()); // corrupt the mandatory shared context's header
+ header.container_size = std::numeric_limits<hsm::SizeType>::max();
+ std::memcpy(blob.data(), &header, sizeof(header));
+
+ const hsm::MultiBlobView view(blob.data(), blob.size());
+ EXPECT_EQ(view.blob_count(), 0u); // can't even get past the corrupt first container
+ EXPECT_EQ(view.blob_at(0).size(), 0u); // out-of-range -> empty view, not a crash
+}
+
+TEST(HsmMultiBlobViewTest, stops_on_invalid_magic) {
+ auto blob = make_multi_blob_file(1);
+ const auto second_container_offset = sizeof(hsm::Header);
+ blob[second_container_offset] = 'X'; // corrupt the blob's magic to neither single nor multi
+
+ const hsm::MultiBlobView view(blob.data(), blob.size());
+ EXPECT_EQ(view.blob_count(), 0u); // shared context is skipped fine, but the blob itself is unreadable
+}
+
+TEST(HsmMultiBlobViewTest, blob_view_excludes_following_containers) {
+ auto buffer = make_multi_container({}, {}); // mandatory shared context
+ const auto blob0 = make_single_blob_container({}, {});
+ const auto blob1 = make_single_blob_container({'O', 'V'}, {}); // different size than blob0
+ buffer.insert(buffer.end(), blob0.begin(), blob0.end());
+ buffer.insert(buffer.end(), blob1.begin(), blob1.end());
+
+ const hsm::MultiBlobView view(buffer.data(), buffer.size());
+ ASSERT_EQ(view.blob_count(), 2u);
+
+ const auto view0 = view.blob_at(0);
+ EXPECT_EQ(view0.size(), blob0.size()); // must not leak into blob1's bytes
+ EXPECT_TRUE(view0.validate());
+}
+
+TEST(HsmMultiBlobViewTest, stops_on_mismatched_major_version) {
+ auto buffer = make_multi_container({}, {}); // mandatory shared context
+ auto header = hsm::Header::view(buffer.data());
+ header.version_major = hsm::FormatVersion::major + 1;
+ std::memcpy(buffer.data(), &header, sizeof(header));
+ const auto blob0 = make_single_blob_container({}, {});
+ buffer.insert(buffer.end(), blob0.begin(), blob0.end());
+
+ const hsm::MultiBlobView view(buffer.data(), buffer.size());
+ EXPECT_EQ(view.blob_count(), 0u); // can't trust framing past an unsupported major version
+}
+
+TEST(IHsmSectionExtensionTest, recognizes_own_device_and_tag) {
+ hsm::ManifestEntry entry{};
+ entry.device = RecordingExtension::owned_device;
+ entry.tag = hsm::SectionTag::make(RecordingExtension::owned_tag_id, /*is_inline=*/false);
+
+ const std::byte payload[4] = {};
+ RecordingExtension extension;
+ EXPECT_TRUE(extension.read_section(entry, ov::util::MemoryView{payload, 4}));
+ EXPECT_EQ(extension.last_section_size, 4u);
+}
+
+TEST(IHsmSectionExtensionTest, skips_entry_it_does_not_own) {
+ hsm::ManifestEntry entry{};
+ entry.device = RecordingExtension::owned_device + 1; // different device
+ entry.tag = hsm::SectionTag::make(RecordingExtension::owned_tag_id, /*is_inline=*/false);
+
+ RecordingExtension extension;
+ EXPECT_FALSE(extension.read_section(entry, ov::util::MemoryView{}));
+ EXPECT_EQ(extension.last_section_size, 0u); // never called
+}
+
+// OPENVINO_DEBUG_ASSERT compiles out entirely under NDEBUG (Release builds), so this only runs in debug builds.
+#ifndef NDEBUG
+TEST(MakeDeviceTagTest, debug_asserts_on_id_overflow) {
+ const auto out_of_range_id = hsm::max_tag_id - hsm::core_tag_id_range_end + 1;
+ EXPECT_DEATH(hsm::SectionTag::make_device_tag(out_of_range_id, false), "");
+}
+#endif
+
+} // namespace ov::test