OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[Core] Fix OOB read in ConstantWriter constant deduplication (#37465)
mingmingtasd102ef366ef96
diff --git a/src/core/dev_api/openvino/xml_util/constant_writer.hpp b/src/core/dev_api/openvino/xml_util/constant_writer.hppindex 210dad45073..c5c9ba13825 100644--- a/src/core/dev_api/openvino/xml_util/constant_writer.hpp+++ b/src/core/dev_api/openvino/xml_util/constant_writer.hpp@@ -19,7 +19,12 @@ class OPENVINO_API ConstantWriter {public:using FilePosition = int64_t;using HashValue = size_t;- using ConstWritePositions = std::multimap<HashValue, std::pair<FilePosition, const void*>>;+ struct ConstWritePosition {+ FilePosition offset;+ const void* ptr;+ size_t size; // length in bytes of the buffer pointed to by 'ptr'+ };+ using ConstWritePositions = std::multimap<HashValue, ConstWritePosition>;ConstantWriter(std::ostream& bin_data, bool enable_compression = true);virtual ~ConstantWriter();diff --git a/src/core/src/xml_util/constant_writer.cpp b/src/core/src/xml_util/constant_writer.cppindex 818c41ace3d..49c93cb4b8e 100644--- a/src/core/src/xml_util/constant_writer.cpp+++ b/src/core/src/xml_util/constant_writer.cpp@@ -43,14 +43,14 @@ ConstantWriter::FilePosition ConstantWriter::write(const char* ptr,const auto found = m_hash_to_file_positions.equal_range(hash);// iterate over all matches of the key in the multimapfor (auto it = found.first; it != found.second; ++it) {- if (memcmp(ptr, it->second.second, size) == 0) {- return it->second.first;+ if (it->second.size == size && memcmp(ptr, it->second.ptr, size) == 0) {+ return it->second.offset;}}if (!ptr_is_temporary) {// Since fp16_compressed data will be disposed at exit point and since we cannot reread it from the// ostream, we store pointer to the original uncompressed blob.- m_hash_to_file_positions.insert({hash, {offset, static_cast<const void*>(ptr)}});+ m_hash_to_file_positions.insert({hash, {offset, static_cast<const void*>(ptr), size}});}m_data_hash = util::u64_hash_combine(m_data_hash, hash);} else {@@ -77,8 +77,8 @@ ConstantWriter::FilePosition ConstantWriter::write(const std::vector<std::stringconst HashValue hash = ov::runtime::compute_hash(tmp.data(), new_size);const auto found = m_hash_to_file_positions.equal_range(hash);for (auto it = found.first; it != found.second; ++it) {- if (memcmp(tmp.data(), it->second.second, new_size) == 0) {- return it->second.first;+ if (it->second.size == new_size && memcmp(tmp.data(), it->second.ptr, new_size) == 0) {+ return it->second.offset;}}@@ -87,7 +87,7 @@ ConstantWriter::FilePosition ConstantWriter::write(const std::vector<std::stringconst char* stable_ptr = m_packed_string_data.back().data();const FilePosition write_pos = m_binary_output.get().tellp();const FilePosition offset = write_pos - m_blob_offset;- m_hash_to_file_positions.insert({hash, {offset, static_cast<const void*>(stable_ptr)}});+ m_hash_to_file_positions.insert({hash, {offset, static_cast<const void*>(stable_ptr), new_size}});m_data_hash = util::u64_hash_combine(m_data_hash, hash);m_binary_output.get().write(stable_ptr, new_size);return offset;diff --git a/src/core/tests/xml_util/constant_writer_test.cpp b/src/core/tests/xml_util/constant_writer_test.cppnew file mode 100644index 00000000000..111c3380e6f--- /dev/null+++ b/src/core/tests/xml_util/constant_writer_test.cpp@@ -0,0 +1,92 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "openvino/xml_util/constant_writer.hpp"++#include <gtest/gtest.h>++#include <array>+#include <cstdint>+#include <sstream>+#include <vector>++#include "openvino/core/visibility.hpp"++namespace ov::test {++// Tests for ConstantWriter's deduplication size guard against hash collisions between different-sized buffers.++// Precomputed collision: k_large starts with k_small and both hash to the same value. Valid only for+// the x86-64 CRC-64 hash, so the tests are x86-64 only and assert the collision to catch hash changes.+#if defined(OPENVINO_ARCH_X86_64)+namespace {+constexpr std::array<uint8_t, 16> k_small =+ {0x07, 0x18, 0x29, 0x3a, 0x4b, 0x5c, 0x6d, 0x7e, 0x8f, 0xa0, 0xb1, 0xc2, 0xd3, 0xe4, 0xf5, 0x06};+constexpr std::array<uint8_t, 24> k_large = {0x07, 0x18, 0x29, 0x3a, 0x4b, 0x5c, 0x6d, 0x7e, 0x8f, 0xa0, 0xb1, 0xc2,+ 0xd3, 0xe4, 0xf5, 0x06, 0x2b, 0xa4, 0x34, 0x82, 0x0b, 0x04, 0xb1, 0x2a};++// Writes compile-time byte data through ConstantWriter, reinterpreting it as the char buffer the API expects.+template <size_t N>+ov::util::ConstantWriter::FilePosition write_bytes(ov::util::ConstantWriter& writer,+ const std::array<uint8_t, N>& data,+ size_t& new_size) {+ return writer.write(reinterpret_cast<const char*>(data.data()), data.size(), new_size);+}++// Surfaces the internal per-buffer hash through the public get_data_hash(): a fresh writer combines it+// as u64_hash_combine(0, hash), which is one-to-one, so equal results imply equal hashes.+template <size_t N>+uint64_t const_write_hash(const std::array<uint8_t, N>& data) {+ std::stringstream bin;+ ov::util::ConstantWriter writer(bin, /*enable_compression=*/true);+ size_t new_size = 0;+ write_bytes(writer, data, new_size);+ return writer.get_data_hash();+}+} // namespace++TEST(ConstantWriterTest, size_mismatch_is_not_deduplicated) {+ ASSERT_EQ(const_write_hash(k_small), const_write_hash(k_large))+ << "hardcoded buffers no longer collide; regenerate them for the current hash";++ std::stringstream bin;+ ov::util::ConstantWriter writer(bin, /*enable_compression=*/true);+ size_t new_size = 0;+ const auto off_large = write_bytes(writer, k_large, new_size);+ const auto off_small = write_bytes(writer, k_small, new_size);++ EXPECT_NE(off_small, off_large) << "a shorter constant must not be deduplicated onto a longer colliding one";+ EXPECT_EQ(static_cast<size_t>(bin.tellp()), k_large.size() + k_small.size()) << "both constants must be written";+}++TEST(ConstantWriterTest, hash_collision_with_larger_current_buffer_no_oob) {+ ASSERT_EQ(const_write_hash(k_small), const_write_hash(k_large))+ << "hardcoded buffers no longer collide; regenerate them for the current hash";++ std::stringstream bin;+ ov::util::ConstantWriter writer(bin, /*enable_compression=*/true);+ size_t new_size = 0;+ const auto off_small = write_bytes(writer, k_small, new_size);+ const auto off_large = write_bytes(writer, k_large, new_size);++ EXPECT_NE(off_large, off_small) << "a longer constant must not be deduplicated onto a shorter colliding one";+ EXPECT_EQ(static_cast<size_t>(bin.tellp()), k_small.size() + k_large.size()) << "both constants must be written";+}+#endif // OPENVINO_ARCH_X86_64++TEST(ConstantWriterTest, identical_constants_are_deduplicated) {+ const std::vector<char> a(128, char{0x3C});+ const std::vector<char> b(128, char{0x3C});++ std::stringstream bin;+ ov::util::ConstantWriter writer(bin, /*enable_compression=*/true);+ size_t new_size = 0;+ const auto off_a = writer.write(a.data(), a.size(), new_size);+ const auto off_b = writer.write(b.data(), b.size(), new_size);++ EXPECT_EQ(off_a, off_b) << "identical constants must still be deduplicated";+ EXPECT_EQ(static_cast<size_t>(bin.tellp()), a.size()) << "duplicate constant must not be re-written";+}++} // namespace ov::testdiff --git a/src/core/tests/xml_util/sources.cmake b/src/core/tests/xml_util/sources.cmakeindex f8ffa13ad3a..6b1c95fde22 100644--- a/src/core/tests/xml_util/sources.cmake+++ b/src/core/tests/xml_util/sources.cmake@@ -3,6 +3,7 @@#set(OV_CORE_TESTS_XML_UTIL_SRCS+ ${CMAKE_CURRENT_LIST_DIR}/constant_writer_test.cpp${CMAKE_CURRENT_LIST_DIR}/custom_ir.cpp${CMAKE_CURRENT_LIST_DIR}/xml_parse_utils_test.cpp)