Skip to content

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

[NPUW] Validate GQA, Flux2, and LLM serialization version length (#38258)

Alexis Menard8a3d4fce43a4
diff --git a/src/plugins/intel_npu/src/plugin/npuw/orc.cpp b/src/plugins/intel_npu/src/plugin/npuw/orc.cpp
index 3b7aa71db1d..f2351e919ed 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/orc.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/orc.cpp
@@ -38,10 +38,14 @@ std::streamsize checked_stream_size(const std::size_t size) {
return static_cast<std::streamsize>(size);
}
-std::size_t checked_size(const std::uint64_t size) {
+std::size_t checked_size(const std::uint64_t size,
+ const std::size_t max_size = std::numeric_limits<std::size_t>::max()) {
if (size > static_cast<std::uint64_t>(std::numeric_limits<std::size_t>::max())) {
OPENVINO_THROW("ORC section size exceeds std::size_t range");
}
+ if (size > max_size) {
+ OPENVINO_THROW("ORC size ", size, " exceeds remaining payload ", max_size);
+ }
return static_cast<std::size_t>(size);
}
@@ -199,10 +203,22 @@ bool ov::npuw::orc::Stream::memory() const {
}
std::size_t ov::npuw::orc::Stream::remaining() const {
- if (!memory()) {
- OPENVINO_THROW("ORC remaining() is only available for memory-backed input streams");
+ if (memory()) {
+ return m_memory_size - m_memory_offset;
+ }
+ if (m_input == nullptr) {
+ OPENVINO_THROW("ORC remaining() is only available for input streams");
+ }
+
+ const auto pos = checked_tellg(*m_input, "remaining payload");
+ m_input->seekg(0, std::ios::end);
+ const auto end = m_input->tellg();
+ m_input->clear();
+ m_input->seekg(pos);
+ if (end == std::streampos(-1) || end < pos || !m_input->good()) {
+ OPENVINO_THROW("Failed to determine the remaining ORC input stream size");
}
- return m_memory_size - m_memory_offset;
+ return ::checked_size(static_cast<std::uint64_t>(end - pos));
}
void ov::npuw::orc::Stream::bytes(void* data, const std::size_t size) {
@@ -243,7 +259,7 @@ void ov::npuw::orc::serialize(Stream& stream, std::string& value) {
std::size_t size = 0u;
stream & size;
- value.resize(size);
+ value.resize(::checked_size(size, stream.remaining()));
if (size != 0u) {
stream.bytes(value.data(), value.size());
}
diff --git a/src/plugins/intel_npu/src/plugin/npuw/serialization.cpp b/src/plugins/intel_npu/src/plugin/npuw/serialization.cpp
index 5af52589150..56accb163f5 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/serialization.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/serialization.cpp
@@ -461,7 +461,21 @@ void ov::npuw::s11n::read_and_check_header(std::istream& stream,
read(stream, vmajor);
read(stream, vminor);
read(stream, vpatch);
- read(stream, s11n_version);
+
+ // Generous cap so any past/future version string still reaches the mismatch report below.
+ constexpr std::size_t MAX_S11N_VERSION_SIZE = 64u;
+ auto stream_io = Stream::reader(stream);
+ std::size_t s11n_version_size = 0u;
+ stream_io & s11n_version_size;
+ OPENVINO_ASSERT(s11n_version_size <= MAX_S11N_VERSION_SIZE,
+ "Corrupted NPUW blob: serialization version string is ",
+ s11n_version_size,
+ " bytes, the maximum is ",
+ MAX_S11N_VERSION_SIZE);
+ s11n_version.resize(s11n_version_size);
+ if (s11n_version_size != 0u) {
+ stream_io.bytes(s11n_version.data(), s11n_version_size);
+ }
if (vmajor != OPENVINO_VERSION_MAJOR || vminor != OPENVINO_VERSION_MINOR || vpatch != OPENVINO_VERSION_PATCH ||
s11n_version != std::string(NPUW_SERIALIZATION_VERSION)) {
diff --git a/src/plugins/intel_npu/tests/unit/npuw/flux2_compiled_model_test.cpp b/src/plugins/intel_npu/tests/unit/npuw/flux2_compiled_model_test.cpp
index 7c901e5ac2b..48e727ea02c 100644
--- a/src/plugins/intel_npu/tests/unit/npuw/flux2_compiled_model_test.cpp
+++ b/src/plugins/intel_npu/tests/unit/npuw/flux2_compiled_model_test.cpp
@@ -6,12 +6,14 @@
#include <gtest/gtest.h>
+#include <limits>
#include <memory>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
+#include "common_test_utils/test_assertions.hpp"
#include "llm_test_helpers.hpp"
#include "openvino/core/version.hpp"
#include "openvino/op/parameter.hpp"
@@ -129,6 +131,18 @@ std::string make_flux2_header(const ov::npuw::s11n::IndicatorType& serialization
return stream.str();
}
+std::string make_flux2_header(const std::size_t claimed_version_size, const std::string& version) {
+ std::ostringstream stream;
+ ov::npuw::s11n::write(stream, NPUW_SERIALIZATION_INDICATOR);
+ ov::npuw::s11n::write(stream, NPUW_FLUX2_COMPILED_MODEL_INDICATOR);
+ ov::npuw::s11n::write(stream, OPENVINO_VERSION_MAJOR);
+ ov::npuw::s11n::write(stream, OPENVINO_VERSION_MINOR);
+ ov::npuw::s11n::write(stream, OPENVINO_VERSION_PATCH);
+ ov::npuw::s11n::write(stream, claimed_version_size);
+ stream.write(version.data(), static_cast<std::streamsize>(version.size()));
+ return stream.str();
+}
+
class Flux2CompiledModelTest : public ::testing::Test {
protected:
void SetUp() override {
@@ -387,6 +401,24 @@ TEST_F(Flux2CompiledModelTest, ImportRejectsVersionMismatch) {
EXPECT_THROW(ov::npuw::Flux2CompiledModel::import_model(stream, m_plugin, {}), ov::Exception);
}
+TEST_F(Flux2CompiledModelTest, ImportRejectsExcessiveSerializationVersionLengthBeforeAllocation) {
+ const auto header = make_flux2_header(std::numeric_limits<std::size_t>::max(), NPUW_SERIALIZATION_VERSION);
+ std::istringstream stream(header);
+
+ OV_EXPECT_THROW_HAS_SUBSTRING(ov::npuw::Flux2CompiledModel::import_model(stream, m_plugin, {}),
+ ov::Exception,
+ "the maximum is 64");
+}
+
+TEST_F(Flux2CompiledModelTest, ImportReportsVersionMismatchForDifferentVersionLength) {
+ const auto header = make_flux2_header(3u, "0.3");
+ std::istringstream stream(header);
+
+ OV_EXPECT_THROW_HAS_SUBSTRING(ov::npuw::Flux2CompiledModel::import_model(stream, m_plugin, {}),
+ ov::Exception,
+ "NPUW serialized by version 0.3");
+}
+
// --- Architecture-aware NPU tile configuration ------------------------------
TEST_F(Flux2CompiledModelTest, Arch5000SeriesSetsNpuTilesForTransformer) {
diff --git a/src/plugins/intel_npu/tests/unit/npuw/gqa_compiled_model_test.cpp b/src/plugins/intel_npu/tests/unit/npuw/gqa_compiled_model_test.cpp
index 5b408345992..821bba7c0a8 100644
--- a/src/plugins/intel_npu/tests/unit/npuw/gqa_compiled_model_test.cpp
+++ b/src/plugins/intel_npu/tests/unit/npuw/gqa_compiled_model_test.cpp
@@ -7,11 +7,13 @@
#include <gtest/gtest.h>
#include <algorithm>
+#include <limits>
#include <memory>
#include <sstream>
#include <string>
#include <vector>
+#include "common_test_utils/test_assertions.hpp"
#include "gqa_compiled_model.hpp"
#include "intel_npu/config/npuw.hpp"
#include "llm_test_helpers.hpp"
@@ -41,6 +43,18 @@ using ov::test::npuw::build_llm_test_model;
using ov::test::npuw::MockSubCompiledModel;
using ov::test::npuw::NullPlugin;
+std::string make_gqa_header(const std::size_t claimed_version_size, const std::string& version) {
+ std::ostringstream stream;
+ ov::npuw::s11n::write(stream, NPUW_SERIALIZATION_INDICATOR);
+ ov::npuw::s11n::write(stream, NPUW_GQA_COMPILED_MODEL_INDICATOR);
+ ov::npuw::s11n::write(stream, OPENVINO_VERSION_MAJOR);
+ ov::npuw::s11n::write(stream, OPENVINO_VERSION_MINOR);
+ ov::npuw::s11n::write(stream, OPENVINO_VERSION_PATCH);
+ ov::npuw::s11n::write(stream, claimed_version_size);
+ stream.write(version.data(), static_cast<std::streamsize>(version.size()));
+ return stream.str();
+}
+
template <class Op>
std::size_t count_ops(const std::shared_ptr<ov::Model>& model) {
const auto ops = model->get_ops();
@@ -2259,4 +2273,42 @@ TEST(GQACompiledModelPresentToPastParamNamesTest, MapsDotStyleNamesNotMatchingSu
EXPECT_EQ(map.at("present.0.value"), "past_key_values.0.value");
}
+TEST_F(GQACompiledModelTest, ImportRejectsExcessiveSerializationVersionLengthBeforeAllocation) {
+ const auto header = make_gqa_header(std::numeric_limits<std::size_t>::max(), NPUW_SERIALIZATION_VERSION);
+ std::istringstream stream(header);
+
+ OV_EXPECT_THROW_HAS_SUBSTRING(ov::npuw::GQACompiledModel::import_model(stream, m_plugin, {}),
+ ov::Exception,
+ "the maximum is 64");
+}
+
+TEST_F(GQACompiledModelTest, ImportRejectsSerializationVersionLongerThanCap) {
+ const std::string version(65u, 'x');
+ const auto header = make_gqa_header(version.size(), version);
+ std::istringstream stream(header);
+
+ OV_EXPECT_THROW_HAS_SUBSTRING(ov::npuw::GQACompiledModel::import_model(stream, m_plugin, {}),
+ ov::Exception,
+ "serialization version string is 65 bytes, the maximum is 64");
+}
+
+TEST_F(GQACompiledModelTest, ImportReportsVersionMismatchForVersionAtCap) {
+ const std::string version(64u, 'x');
+ const auto header = make_gqa_header(version.size(), version);
+ std::istringstream stream(header);
+
+ OV_EXPECT_THROW_HAS_SUBSTRING(ov::npuw::GQACompiledModel::import_model(stream, m_plugin, {}),
+ ov::Exception,
+ "NPUW serialized by version " + version);
+}
+
+TEST_F(GQACompiledModelTest, ImportReportsVersionMismatchForDifferentVersionLength) {
+ const auto header = make_gqa_header(3u, "0.3");
+ std::istringstream stream(header);
+
+ OV_EXPECT_THROW_HAS_SUBSTRING(ov::npuw::GQACompiledModel::import_model(stream, m_plugin, {}),
+ ov::Exception,
+ "NPUW serialized by version 0.3");
+}
+
} // namespace
diff --git a/src/plugins/intel_npu/tests/unit/npuw/llm_compiled_model_factory_options_test.cpp b/src/plugins/intel_npu/tests/unit/npuw/llm_compiled_model_factory_options_test.cpp
index ff5170d67ba..934c6807410 100644
--- a/src/plugins/intel_npu/tests/unit/npuw/llm_compiled_model_factory_options_test.cpp
+++ b/src/plugins/intel_npu/tests/unit/npuw/llm_compiled_model_factory_options_test.cpp
@@ -6,16 +6,20 @@
#include <algorithm>
#include <array>
+#include <limits>
#include <memory>
+#include <sstream>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
+#include "common_test_utils/test_assertions.hpp"
#include "intel_npu/config/npuw.hpp"
#include "llm_compiled_model_utils.hpp"
#include "llm_test_helpers.hpp"
#include "moe_transformations/moe_topology.hpp"
+#include "openvino/core/version.hpp"
#include "openvino/op/constant.hpp"
#include "openvino/op/convert.hpp"
#include "openvino/op/gather.hpp"
@@ -27,6 +31,7 @@
#include "openvino/op/subtract.hpp"
#include "openvino/pass/stateful_to_stateless.hpp"
#include "openvino/runtime/intel_npu/properties.hpp"
+#include "serialization.hpp"
#include "unit_test_utils/mocks/openvino/runtime/mock_icore.hpp"
#include "whisper/prepare_whisper_model.hpp"
@@ -36,6 +41,18 @@ using ov::test::npuw::CompileCall;
using ov::test::npuw::NullPlugin;
using ov::test::npuw::RecordingFactory;
+std::string make_llm_header(const std::size_t claimed_version_size, const std::string& version) {
+ std::ostringstream stream;
+ ov::npuw::s11n::write(stream, NPUW_SERIALIZATION_INDICATOR);
+ ov::npuw::s11n::write(stream, NPUW_LLM_COMPILED_MODEL_INDICATOR);
+ ov::npuw::s11n::write(stream, OPENVINO_VERSION_MAJOR);
+ ov::npuw::s11n::write(stream, OPENVINO_VERSION_MINOR);
+ ov::npuw::s11n::write(stream, OPENVINO_VERSION_PATCH);
+ ov::npuw::s11n::write(stream, claimed_version_size);
+ stream.write(version.data(), static_cast<std::streamsize>(version.size()));
+ return stream.str();
+}
+
bool has_transposed_value_matmul(const std::shared_ptr<ov::Model>& model, std::string_view attention_kind) {
for (const auto& op : model->get_ops()) {
const auto matmul = ov::as_type_ptr<ov::op::v0::MatMul>(op);
@@ -1422,4 +1439,22 @@ TEST_F(LLMCompiledModelFactoryOptionsTest, TextEmbedEncoderClampsPromptLenToMaxP
EXPECT_EQ(compiled->get_property("NPUW_LLM_MAX_PROMPT_LEN").as<uint32_t>(), 512u);
}
+TEST_F(LLMCompiledModelFactoryOptionsTest, ImportRejectsExcessiveSerializationVersionLengthBeforeAllocation) {
+ const auto header = make_llm_header(std::numeric_limits<std::size_t>::max(), NPUW_SERIALIZATION_VERSION);
+ std::istringstream stream(header);
+
+ OV_EXPECT_THROW_HAS_SUBSTRING(ov::npuw::LLMCompiledModel::import_model(stream, m_plugin, {}),
+ ov::Exception,
+ "the maximum is 64");
+}
+
+TEST_F(LLMCompiledModelFactoryOptionsTest, ImportReportsVersionMismatchForDifferentVersionLength) {
+ const auto header = make_llm_header(3u, "0.3");
+ std::istringstream stream(header);
+
+ OV_EXPECT_THROW_HAS_SUBSTRING(ov::npuw::LLMCompiledModel::import_model(stream, m_plugin, {}),
+ ov::Exception,
+ "NPUW serialized by version 0.3");
+}
+
} // namespace
diff --git a/src/plugins/intel_npu/tests/unit/npuw/orc.cpp b/src/plugins/intel_npu/tests/unit/npuw/orc.cpp
index c17a3dafa02..cfd791c3fcd 100644
--- a/src/plugins/intel_npu/tests/unit/npuw/orc.cpp
+++ b/src/plugins/intel_npu/tests/unit/npuw/orc.cpp
@@ -6,10 +6,14 @@
#include <gtest/gtest.h>
+#include <cstring>
+#include <limits>
#include <sstream>
#include <string>
#include <vector>
+#include "common_test_utils/test_assertions.hpp"
+
namespace {
using namespace ov::npuw::orc;
@@ -131,6 +135,46 @@ TEST(OrcTest, RejectsTruncatedFile) {
EXPECT_THROW(read_file(truncated), ov::Exception);
}
+TEST(OrcTest, StringRejectsSizeExceedingRemainingMemoryPayloadBeforeAllocation) {
+ const auto claimed_size = std::numeric_limits<std::size_t>::max();
+ std::vector<std::byte> payload(sizeof(claimed_size) + 1u);
+ std::memcpy(payload.data(), &claimed_size, sizeof(claimed_size));
+
+ auto stream = Stream::memory_reader(payload.data(), payload.size());
+ std::string value;
+
+ OV_EXPECT_THROW_HAS_SUBSTRING(stream & value, ov::Exception, "exceeds remaining payload 1");
+}
+
+TEST(OrcTest, StringRejectsSizeExceedingInputStreamPayloadBeforeAllocation) {
+ const auto claimed_size = std::numeric_limits<std::size_t>::max();
+ std::string payload(sizeof(claimed_size) + 1u, 'x');
+ std::memcpy(payload.data(), &claimed_size, sizeof(claimed_size));
+
+ std::istringstream input(payload, std::ios::in | std::ios::binary);
+ auto stream = Stream::reader(input);
+ std::string value;
+
+ OV_EXPECT_THROW_HAS_SUBSTRING(stream & value, ov::Exception, "exceeds remaining payload 1");
+}
+
+TEST(OrcTest, StringRoundTripsThroughInputStream) {
+ std::string original(200u * 1024u + 7u, '\0');
+ for (std::size_t idx = 0; idx < original.size(); ++idx) {
+ original[idx] = static_cast<char>(idx % 251u);
+ }
+
+ std::stringstream buffer(std::ios::in | std::ios::out | std::ios::binary);
+ auto writer = Stream::writer(buffer);
+ writer & original;
+
+ auto reader = Stream::reader(buffer);
+ std::string restored;
+ reader & restored;
+
+ EXPECT_EQ(restored, original);
+}
+
TEST(OrcTest, ScopedSectionsRoundTripMetadataBeforeChildren) {
std::stringstream buffer(std::ios::in | std::ios::out | std::ios::binary);
write_file_header(buffer, TEST_UUID);