Skip to content

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

[core, NPU] Fix wrong size calc for mem allocation by detect overflow (#31692)

Pawel Raasza70d48b03e89
diff --git a/src/common/util/include/openvino/util/common_util.hpp b/src/common/util/include/openvino/util/common_util.hpp
index 39d9dac06ed..df940e33c97 100644
--- a/src/common/util/include/openvino/util/common_util.hpp
+++ b/src/common/util/include/openvino/util/common_util.hpp
@@ -248,5 +248,40 @@ protected:
}
};
+/**
+ * @brief Multiplies two integral values
+ *
+ * The result value is not valid if overflow detected.
+ *
+ * @param T Type of values to multiply. Must be an integral type.
+ * @param x First value to multiply.
+ * @param y Second value to multiply.
+ * @param result Reference to store result value.
+ * @return True if overflow occurs, false otherwise
+ */
+template <class T>
+constexpr bool mul_overflow(T x, T y, T& result) {
+ static_assert(std::is_integral_v<T>, "T must be an integral type");
+#if defined(__GNUC__) || defined(__clang__)
+ return __builtin_mul_overflow(x, y, &result);
+#else
+ constexpr auto max = std::numeric_limits<T>::max();
+
+ if constexpr (std::is_unsigned_v<T>) {
+ if (y > 0 && x > max / y) {
+ return true;
+ }
+ } else {
+ constexpr auto min = std::numeric_limits<T>::lowest();
+ if ((x > 0 && y > 0 && x > max / y) || (x > 0 && y < 0 && y < min / x) || (x < 0 && y > 0 && x < min / y) ||
+ (x < 0 && y < 0 && x < max / y)) {
+ return true;
+ }
+ }
+ result = x * y;
+ return false;
+#endif
+}
+
} // namespace util
} // namespace ov
diff --git a/src/core/dev_api/openvino/core/memory_util.hpp b/src/core/dev_api/openvino/core/memory_util.hpp
new file mode 100644
index 00000000000..5ece113307d
--- /dev/null
+++ b/src/core/dev_api/openvino/core/memory_util.hpp
@@ -0,0 +1,31 @@
+// Copyright (C) 2018-2025 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#pragma once
+
+#include <optional>
+
+#include "openvino/core/shape.hpp"
+#include "openvino/core/type/element_type.hpp"
+
+namespace ov::util {
+
+/**
+ * @brief Gets size of memory in bytes for N elements of given precision if there is no overflow.
+ *
+ * @param type Element precision.
+ * @param n Number of elements.
+ * @return Memory size in bytes or std::nullopt if overflow occurs.
+ */
+OPENVINO_API std::optional<size_t> get_memory_size_safe(const element::Type& type, const size_t n);
+
+/**
+ * @brief Gets size of memory in bytes for shape of given precision if there is no overflow.
+ *
+ * @param type Element precision.
+ * @param shape Shape of elements.
+ * @return Memory size in bytes or std::nullopt if overflow occurs.
+ */
+OPENVINO_API std::optional<size_t> get_memory_size_safe(const element::Type& type, const ov::Shape& shape);
+} // namespace ov::util
diff --git a/src/core/dev_api/openvino/core/shape_util.hpp b/src/core/dev_api/openvino/core/shape_util.hpp
index 01e62dc7e17..4790b1469cf 100644
--- a/src/core/dev_api/openvino/core/shape_util.hpp
+++ b/src/core/dev_api/openvino/core/shape_util.hpp
@@ -4,8 +4,11 @@
#pragma once
+#include <optional>
+
#include "openvino/core/shape.hpp"
#include "openvino/op/util/attr_types.hpp"
+#include "openvino/util/common_util.hpp"
namespace ov {
namespace util {
@@ -71,5 +74,13 @@ Shape get_broadcast_shape(const Shape& first, const Shape& second, const ov::op:
* @return Normalized shape dimension index.
*/
OPENVINO_API std::ptrdiff_t normalize_shape_index(std::ptrdiff_t idx, size_t rank);
+
+/**
+ * @brief Calculates shape size if there is no overflow.
+ *
+ * @param shape Input shape for size calculation.
+ * @return Shape size if there is no overflow, otherwise nullopt.
+ */
+OPENVINO_API std::optional<size_t> shape_size_safe(const Shape& shape);
} // namespace util
} // namespace ov
diff --git a/src/core/src/memory_util.cpp b/src/core/src/memory_util.cpp
new file mode 100644
index 00000000000..cccee0023b8
--- /dev/null
+++ b/src/core/src/memory_util.cpp
@@ -0,0 +1,39 @@
+// Copyright (C) 2018-2025 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "openvino/core/memory_util.hpp"
+
+#include "openvino/core/shape_util.hpp"
+#include "openvino/core/type/element_iterator.hpp"
+#include "openvino/util/common_util.hpp"
+
+namespace ov::util {
+
+std::optional<size_t> get_memory_size_safe(const element::Type& type, const size_t n) {
+ auto byte_size = std::make_optional(n);
+
+ if (n != 0) {
+ if (element::is_split_bit_type(type)) {
+ constexpr size_t storage_unit_size = 24;
+ const size_t elements_per_storage_unit = storage_unit_size / type.bitwidth();
+ *byte_size /= elements_per_storage_unit;
+ *byte_size += static_cast<size_t>(*byte_size * elements_per_storage_unit != n);
+ *byte_size *= 3;
+ } else if (element::is_bit_type(type) || element::is_nibble_type(type)) {
+ constexpr size_t storage_unit_size = 8;
+ const auto elements_per_storage_unit = storage_unit_size / type.bitwidth();
+ *byte_size /= elements_per_storage_unit;
+ *byte_size += static_cast<size_t>((*byte_size * elements_per_storage_unit) != n);
+ } else if (mul_overflow<size_t>(type.bitwidth() / 8, *byte_size, *byte_size)) {
+ byte_size.reset();
+ }
+ }
+ return byte_size;
+}
+
+std::optional<size_t> get_memory_size_safe(const element::Type& type, const ov::Shape& shape) {
+ auto byte_size = shape_size_safe(shape);
+ return byte_size ? get_memory_size_safe(type, *byte_size) : byte_size;
+}
+} // namespace ov::util
diff --git a/src/core/src/op/constant.cpp b/src/core/src/op/constant.cpp
index c358c481737..48656a82465 100644
--- a/src/core/src/op/constant.cpp
+++ b/src/core/src/op/constant.cpp
@@ -12,6 +12,8 @@
#include "compare.hpp"
#include "element_visitor.hpp"
#include "itt.hpp"
+#include "openvino/core/memory_util.hpp"
+#include "openvino/core/shape_util.hpp"
#include "openvino/core/tensor_util.hpp"
#include "openvino/core/type/element_iterator.hpp"
#include "openvino/core/type/float16.hpp"
@@ -255,16 +257,17 @@ Constant::Constant(bool memset_allocation, const element::Type& type, const Shap
void Constant::allocate_buffer(bool memset_allocation) {
// memset_allocation flag is to switch on initialization of objects in memory for element::string type
// and set memory to zero for numeric element types
- const auto num_elements = shape_size(m_shape);
- const auto byte_size = element::get_memory_size(m_element_type, num_elements);
+ const auto byte_size = ov::util::get_memory_size_safe(m_element_type, m_shape);
+ OPENVINO_ASSERT(byte_size, "Cannot allocate memory for type: ", m_element_type, " and shape: ", m_shape);
if (m_element_type == ov::element::string) {
- m_data = std::make_shared<StringAlignedBuffer>(num_elements, byte_size, host_alignment(), memset_allocation);
+ const auto num_elements = shape_size(m_shape);
+ m_data = std::make_shared<StringAlignedBuffer>(num_elements, *byte_size, host_alignment(), memset_allocation);
} else {
constexpr uint8_t init_value = 0;
- m_data = std::make_shared<AlignedBuffer>(byte_size, host_alignment());
+ m_data = std::make_shared<AlignedBuffer>(*byte_size, host_alignment());
// AlignedBuffer allocates 1 byte for empty constants, and we set it to zero
- if (memset_allocation || byte_size == 0) {
+ if (memset_allocation || *byte_size == 0) {
std::memset(m_data->get_ptr(), init_value, m_data->size());
} else {
set_unused_bits(m_data->get_ptr());
diff --git a/src/core/src/shape_util.cpp b/src/core/src/shape_util.cpp
index 019979cb448..c20c92df414 100644
--- a/src/core/src/shape_util.cpp
+++ b/src/core/src/shape_util.cpp
@@ -67,5 +67,16 @@ std::ptrdiff_t normalize_shape_index(std::ptrdiff_t idx, size_t rank) {
return idx;
}
}
+
+std::optional<size_t> shape_size_safe(const Shape& shape) {
+ size_t size = 1;
+ for (auto first = shape.cbegin(), last = shape.cend(); first != last; ++first) {
+ if (mul_overflow(size, *first, size)) {
+ return std::nullopt;
+ }
+ }
+ return std::make_optional(size);
+}
+
} // namespace util
} // namespace ov
diff --git a/src/core/tests/memory_util.cpp b/src/core/tests/memory_util.cpp
new file mode 100644
index 00000000000..5150323d9f7
--- /dev/null
+++ b/src/core/tests/memory_util.cpp
@@ -0,0 +1,94 @@
+// Copyright (C) 2018-2025 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "openvino/core/memory_util.hpp"
+
+#include <gtest/gtest.h>
+
+#include "common_test_utils/common_utils.hpp"
+#include "common_test_utils/data_utils.hpp"
+#include "openvino/core/shape.hpp"
+#include "openvino/core/type/element_iterator.hpp"
+
+namespace ov::test {
+
+using MemsizeOverflowParam = std::tuple<element::Type, Shape, std::optional<size_t>>;
+
+class GetMemorySizeOverflowTest : public testing::TestWithParam<MemsizeOverflowParam> {
+public:
+ static std::string get_test_name(const testing::TestParamInfo<MemsizeOverflowParam>& obj) {
+ const auto& [type, shape, exp_size] = obj.param;
+ std::ostringstream result;
+ result << "type=" << type << "_shape=" << shape
+ << "_exp_size=" << (exp_size ? std::to_string(*exp_size) : "overflow");
+ return result.str();
+ }
+};
+
+constexpr auto max_dim = std::numeric_limits<size_t>::max();
+
+INSTANTIATE_TEST_SUITE_P(
+ bit_type_precision,
+ GetMemorySizeOverflowTest,
+ testing::Values(std::make_tuple(element::u1, Shape{}, std::optional<size_t>(1)),
+ std::make_tuple(element::u1, Shape{8}, std::optional<size_t>(1)),
+ std::make_tuple(element::u1, Shape{9}, std::optional<size_t>(2)),
+ std::make_tuple(element::u1, Shape{3, 3}, std::optional<size_t>(2)),
+ std::make_tuple(element::u1, Shape{max_dim}, std::optional<size_t>(max_dim / 8 + 1)),
+ std::make_tuple(element::u1, Shape{max_dim - 1}, std::optional<size_t>(max_dim / 8 + 1)),
+ std::make_tuple(element::u1, Shape{2, max_dim}, std::nullopt),
+ std::make_tuple(element::u2, Shape{}, std::optional<size_t>(1)),
+ std::make_tuple(element::u2, Shape{4}, std::optional<size_t>(1)),
+ std::make_tuple(element::u2, Shape{5}, std::optional<size_t>(2)),
+ std::make_tuple(element::u2, Shape{3, 3}, std::optional<size_t>(3)),
+ std::make_tuple(element::u2, Shape{max_dim}, std::optional<size_t>(max_dim / 4 + 1)),
+ std::make_tuple(element::u2, Shape{max_dim - 1}, std::optional<size_t>(max_dim / 4 + 1)),
+ std::make_tuple(element::u2, Shape{max_dim - 3}, std::optional<size_t>(max_dim / 4)),
+ std::make_tuple(element::u2, Shape{1, 2, max_dim}, std::nullopt)),
+ GetMemorySizeOverflowTest::get_test_name);
+
+INSTANTIATE_TEST_SUITE_P(
+ nibble_type_precision,
+ GetMemorySizeOverflowTest,
+ testing::Values(std::make_tuple(element::u4, Shape{}, std::optional<size_t>(1)),
+ std::make_tuple(element::u4, Shape{2}, std::optional<size_t>(1)),
+ std::make_tuple(element::u4, Shape{3}, std::optional<size_t>(2)),
+ std::make_tuple(element::u4, Shape{2, 2}, std::optional<size_t>(2)),
+ std::make_tuple(element::u4, Shape{3, 3, 2}, std::optional<size_t>(9)),
+ std::make_tuple(element::u4, Shape{max_dim}, std::optional<size_t>(max_dim / 2 + 1)),
+ std::make_tuple(element::u4, Shape{max_dim - 1}, std::optional<size_t>(max_dim / 2)),
+ std::make_tuple(element::u4, Shape{2, max_dim}, std::nullopt)),
+ GetMemorySizeOverflowTest::get_test_name);
+
+INSTANTIATE_TEST_SUITE_P(
+ byte_type_precision,
+ GetMemorySizeOverflowTest,
+ testing::Values(std::make_tuple(element::u8, Shape{}, std::optional<size_t>(1)),
+ std::make_tuple(element::u8, Shape{8}, std::optional<size_t>(8)),
+ std::make_tuple(element::u8, Shape{9}, std::optional<size_t>(9)),
+ std::make_tuple(element::u8, Shape{3, 3}, std::optional<size_t>(9)),
+ std::make_tuple(element::u8, Shape{max_dim, 3}, std::nullopt),
+ std::make_tuple(element::f64, Shape{}, std::optional<size_t>(8)),
+ std::make_tuple(element::f64, Shape{12}, std::optional<size_t>(96)),
+ std::make_tuple(element::f64, Shape{max_dim / 8}, std::optional<size_t>(max_dim - 7)),
+ std::make_tuple(element::f64, Shape{2, max_dim / 16}, std::optional<size_t>(max_dim - 15)),
+ std::make_tuple(element::f64, Shape{2, max_dim / 16 - 1}, std::optional<size_t>(max_dim - 31)),
+ std::make_tuple(element::f64, Shape{1, max_dim / 8 - 1, 3}, std::nullopt),
+ std::make_tuple(element::string, Shape{}, std::optional<size_t>(sizeof(std::string))),
+ std::make_tuple(element::string, Shape{12}, std::optional<size_t>(12 * sizeof(std::string))),
+ std::make_tuple(element::string, Shape{max_dim}, std::nullopt)),
+ GetMemorySizeOverflowTest::get_test_name);
+
+TEST_P(GetMemorySizeOverflowTest, calculate_from_shape) {
+ const auto& [type, shape, exp_size] = GetParam();
+
+ EXPECT_EQ(ov::util::get_memory_size_safe(type, shape), exp_size);
+}
+
+TEST(GetMemorySizeOverflowTest, zero_number_of_elements) {
+ EXPECT_EQ(ov::util::get_memory_size_safe(element::f4e2m1, 0), std::optional<size_t>(0));
+ EXPECT_EQ(ov::util::get_memory_size_safe(element::i16, 0), std::optional<size_t>(0));
+ EXPECT_EQ(ov::util::get_memory_size_safe(element::string, 0), std::optional<size_t>(0));
+}
+} // namespace ov::test
diff --git a/src/core/tests/ov_tensor_test.cpp b/src/core/tests/ov_tensor_test.cpp
index 4c0c6b6f966..2adce29761b 100644
--- a/src/core/tests/ov_tensor_test.cpp
+++ b/src/core/tests/ov_tensor_test.cpp
@@ -435,6 +435,13 @@ TEST_F(OVTensorTest, canSetShape) {
ASSERT_EQ(origShape, t.get_shape());
ASSERT_EQ(orig_data, t.data());
}
+
+ // set shape bigger than maximum allocation size
+ {
+ constexpr auto max_dim = std::numeric_limits<size_t>::max();
+ OV_EXPECT_THROW(t.set_shape(Shape({max_dim, 2})), ov::Exception, _);
+ OV_EXPECT_THROW(t.set_shape(Shape({3, max_dim / 8})), ov::Exception, _);
+ }
}
TEST_F(OVTensorTest, canSetShapeStringTensor) {
@@ -465,6 +472,13 @@ TEST_F(OVTensorTest, canSetShapeStringTensor) {
ASSERT_EQ(origShape, t.get_shape());
ASSERT_EQ(orig_data, t.data());
}
+
+ // set shape bigger than maximum allocation size
+ {
+ constexpr auto max_dim = std::numeric_limits<size_t>::max();
+ OV_EXPECT_THROW(t.set_shape(Shape({3, max_dim / 80})), ov::Exception, _);
+ OV_EXPECT_THROW(t.set_shape(Shape({1, max_dim / 80})), ov::Exception, _);
+ }
}
TEST_F(OVTensorTest, cannotSetShapeOfBiggerSizeOnPreallocatedMemory) {
@@ -931,6 +945,15 @@ TEST_F(OVTensorTest, createReadOnlyViewFromNullptr) {
OV_EXPECT_THROW(Tensor(ov::element::i32, ov::Shape{10}, static_cast<const void*>(nullptr)), ov::Exception, _);
}
+TEST_F(OVTensorTest, createAllocationOverflow) {
+ OV_EXPECT_THROW(Tensor(element::i32, Shape{std::numeric_limits<size_t>::max()}),
+ Exception,
+ HasSubstr("Cannot allocate memory"));
+ OV_EXPECT_THROW(Tensor(element::i32, Shape{std::numeric_limits<size_t>::max(), 2}),
+ Exception,
+ HasSubstr("Cannot allocate memory"));
+}
+
struct TestParams {
ov::Shape src_shape;
ov::Strides src_strides;
diff --git a/src/core/tests/shape.cpp b/src/core/tests/shape.cpp
index bd09d172698..ea9d2fc8585 100644
--- a/src/core/tests/shape.cpp
+++ b/src/core/tests/shape.cpp
@@ -8,6 +8,8 @@
#include <memory>
+#include "openvino/core/shape_util.hpp"
+
using namespace std;
using namespace ov;
@@ -59,3 +61,11 @@ TEST(shape, at_throw_exception) {
EXPECT_THROW(shape.at(-8), ov::Exception);
EXPECT_THROW(shape.at(-80000), ov::Exception);
}
+
+TEST(shape, shape_size_overflow) {
+ constexpr auto max = std::numeric_limits<size_t>::max();
+
+ EXPECT_EQ(ov::util::shape_size_safe({1, 2, 3, 4, 5, 6, 7}).value(), (2 * 3 * 4 * 5 * 6 * 7));
+ EXPECT_EQ(ov::util::shape_size_safe({3, max / 4, 2}), std::nullopt);
+ EXPECT_EQ(ov::util::shape_size_safe({3, max / 4}).value(), (3 * (max / 4)));
+}
diff --git a/src/inference/src/dev/make_tensor.cpp b/src/inference/src/dev/make_tensor.cpp
index 313d551d7d8..20432273c72 100644
--- a/src/inference/src/dev/make_tensor.cpp
+++ b/src/inference/src/dev/make_tensor.cpp
@@ -7,6 +7,7 @@
#include <memory>
#include <mutex>
+#include "openvino/core/memory_util.hpp"
#include "openvino/core/type/element_iterator.hpp"
#include "openvino/core/type/element_type_info.hpp"
#include "openvino/runtime/iremote_tensor.hpp"
@@ -286,10 +287,10 @@ public:
shape,
[&shape, &element_type, &allocator] {
OPENVINO_ASSERT(allocator, "Allocator was not initialized");
- const auto byte_size = element::get_memory_size(element_type, shape_size(shape));
- auto data = const_cast<Allocator&>(allocator).allocate(byte_size);
- OPENVINO_ASSERT(byte_size == 0 || data != nullptr, "Failed to allocate memory");
-
+ const auto byte_size = util::get_memory_size_safe(element_type, shape);
+ OPENVINO_ASSERT(byte_size, bad_alloc_error_msg(element_type, shape));
+ auto data = const_cast<Allocator&>(allocator).allocate(*byte_size);
+ OPENVINO_ASSERT(*byte_size == 0 || data != nullptr, "Failed to allocate memory");
initialize_elements(data, element_type, shape);
return data;
}()},
@@ -303,14 +304,15 @@ public:
if (m_shape == new_shape)
return;
+ const auto byte_size = util::get_memory_size_safe(m_element_type, new_shape);
+ OPENVINO_ASSERT(byte_size, bad_alloc_error_msg(m_element_type, new_shape));
m_shape = std::move(new_shape);
- if (get_size() > get_capacity()) {
+ if (*byte_size > get_bytes_capacity()) {
destroy_memory();
-
// allocate buffer and initialize objects from scratch
m_capacity = m_shape;
- m_ptr = m_allocator.allocate(get_bytes_capacity());
+ m_ptr = m_allocator.allocate(*byte_size);
initialize_elements(m_ptr, m_element_type, m_shape);
}
@@ -321,7 +323,7 @@ public:
private:
void destroy_elements(size_t begin_ind, size_t end_ind) {
// it removes elements from tail
- if (get_element_type() == element::Type_t::string) {
+ if (m_ptr != nullptr && get_element_type() == element::string) {
auto strings = static_cast<std::string*>(m_ptr);
for (size_t ind = begin_ind; ind < end_ind; ++ind) {
using std::string;
@@ -352,6 +354,10 @@ private:
return element::get_memory_size(get_element_type(), get_capacity());
}
+ static std::string bad_alloc_error_msg(const element::Type& element_type, const Shape& shape) {
+ return "Cannot allocate memory for type: " + element_type.to_string() + " and shape: " + shape.to_string();
+ }
+
Allocator m_allocator;
};
diff --git a/src/plugins/intel_npu/src/backend/src/zero_tensor.cpp b/src/plugins/intel_npu/src/backend/src/zero_tensor.cpp
index 82ac365334e..1db3d0e4256 100644
--- a/src/plugins/intel_npu/src/backend/src/zero_tensor.cpp
+++ b/src/plugins/intel_npu/src/backend/src/zero_tensor.cpp
@@ -5,6 +5,7 @@
#include "zero_tensor.hpp"
#include "intel_npu/config/options.hpp"
+#include "openvino/core/memory_util.hpp"
#include "openvino/core/type/element_iterator.hpp"
#include "openvino/runtime/properties.hpp"
#include "openvino/runtime/tensor.hpp"
@@ -38,9 +39,10 @@ ZeroTensor::ZeroTensor(const std::shared_ptr<ZeroInitStructsHolder>& init_struct
_allocator{allocator} {
OPENVINO_ASSERT(_element_type.is_static());
OPENVINO_ASSERT(allocator, "Allocator was not initialized");
- const auto byte_size = ov::element::get_memory_size(_element_type, shape_size(_shape));
- auto data = const_cast<ov::Allocator&>(_allocator).allocate(byte_size);
- OPENVINO_ASSERT(byte_size == 0 || data != nullptr, "Failed to allocate memory");
+ const auto byte_size = ov::util::get_memory_size_safe(element_type, _shape);
+ OPENVINO_ASSERT(byte_size, "Cannot allocate memory for type: ", element_type, " and shape: ", _shape);
+ auto data = const_cast<ov::Allocator&>(_allocator).allocate(*byte_size);
+ OPENVINO_ASSERT(*byte_size == 0 || data != nullptr, "Failed to allocate memory");
initialize_elements(data, element_type, _shape);
_ptr = data;
}
diff --git a/src/plugins/intel_npu/src/utils/src/zero/zero_remote_tensor.cpp b/src/plugins/intel_npu/src/utils/src/zero/zero_remote_tensor.cpp
index 5d8fba9d8e9..6d5e16d8380 100644
--- a/src/plugins/intel_npu/src/utils/src/zero/zero_remote_tensor.cpp
+++ b/src/plugins/intel_npu/src/utils/src/zero/zero_remote_tensor.cpp
@@ -12,7 +12,7 @@
#include "intel_npu/utils/utils.hpp"
#include "intel_npu/utils/zero/zero_api.hpp"
#include "intel_npu/utils/zero/zero_utils.hpp"
-#include "openvino/core/type/element_iterator.hpp"
+#include "openvino/core/memory_util.hpp"
#include "openvino/runtime/tensor.hpp"
using namespace ov::intel_npu;
@@ -51,7 +51,8 @@ ZeroRemoteTensor::ZeroRemoteTensor(const std::shared_ptr<ov::IRemoteContext>& co
OPENVINO_ASSERT(shape_size(_shape) != 0);
OPENVINO_ASSERT(_element_type.is_static());
- const auto byte_size = ov::element::get_memory_size(_element_type, shape_size(_shape));
+ const auto byte_size = ov::util::get_memory_size_safe(element_type, shape);
+ OPENVINO_ASSERT(byte_size, "Cannot allocate memory for type: ", element_type, " and shape: ", shape);
ze_device_external_memory_properties_t desc = {};
desc.stype = ZE_STRUCTURE_TYPE_DEVICE_EXTERNAL_MEMORY_PROPERTIES;
@@ -72,7 +73,7 @@ ZeroRemoteTensor::ZeroRemoteTensor(const std::shared_ptr<ov::IRemoteContext>& co
}
}
- allocate(byte_size);
+ allocate(*byte_size);
}
const ov::element::Type& ZeroRemoteTensor::get_element_type() const {
diff --git a/src/tests/test_utils/common_test_utils/tests/utils_tests.cpp b/src/tests/test_utils/common_test_utils/tests/utils_tests.cpp
index e333988442c..800582e6635 100644
--- a/src/tests/test_utils/common_test_utils/tests/utils_tests.cpp
+++ b/src/tests/test_utils/common_test_utils/tests/utils_tests.cpp
@@ -101,4 +101,115 @@ TEST(UtilsTests, split_by_delimiter_empty_and_comma) {
const std::unordered_set<std::string> expected_set = {"", ""};
ASSERT_EQ(split_set, expected_set);
-}
\ No newline at end of file
+}
+
+TEST(UtilsTests, mul_overflow_i8_detected) {
+ int8_t result;
+ constexpr auto max = std::numeric_limits<int8_t>::max();
+ constexpr auto min = std::numeric_limits<int8_t>::min();
+
+ EXPECT_TRUE(mul_overflow<int8_t>(2, max, result));
+ EXPECT_TRUE(mul_overflow<int8_t>(max, 3, result));
+ EXPECT_TRUE(mul_overflow<int8_t>(5, 50, result));
+ EXPECT_TRUE(mul_overflow<int8_t>(51, 4, result));
+
+ EXPECT_TRUE(mul_overflow<int8_t>(max, min, result));
+ EXPECT_TRUE(mul_overflow<int8_t>(max, -2, result));
+ EXPECT_TRUE(mul_overflow<int8_t>(31, -5, result));
+ EXPECT_TRUE(mul_overflow<int8_t>(2, min, result));
+
+ EXPECT_TRUE(mul_overflow<int8_t>(min, max, result));
+ EXPECT_TRUE(mul_overflow<int8_t>(min, 2, result));
+ EXPECT_TRUE(mul_overflow<int8_t>(-2, max, result));
+ EXPECT_TRUE(mul_overflow<int8_t>(-13, 12, result));
+
+ EXPECT_TRUE(mul_overflow<int8_t>(min, -1, result));
+ EXPECT_TRUE(mul_overflow<int8_t>(-1, min, result));
+ EXPECT_TRUE(mul_overflow<int8_t>(-2, -64, result));
+ EXPECT_TRUE(mul_overflow<int8_t>(-64, -2, result));
+ EXPECT_TRUE(mul_overflow<int8_t>(-43, -3, result));
+ EXPECT_TRUE(mul_overflow<int8_t>(-4, -33, result));
+}
+
+TEST(UtilsTests, mul_overflow_i8_zero_value) {
+ int8_t result;
+ constexpr auto max = std::numeric_limits<int8_t>::max();
+ constexpr auto min = std::numeric_limits<int8_t>::min();
+
+ ASSERT_FALSE(mul_overflow<int8_t>(0, 0, result));
+ EXPECT_EQ(result, 0);
+
+ ASSERT_FALSE(mul_overflow<int8_t>(0, max, result));
+ EXPECT_EQ(result, 0);
+
+ ASSERT_FALSE(mul_overflow<int8_t>(max, 0, result));
+ EXPECT_EQ(result, 0);
+
+ ASSERT_FALSE(mul_overflow<int8_t>(0, min, result));
+ EXPECT_EQ(result, 0);
+
+ ASSERT_FALSE(mul_overflow<int8_t>(min, 0, result));
+ EXPECT_EQ(result, 0);
+}
+
+TEST(UtilsTests, mul_overflow_i8_non_zero_value) {
+ int8_t result;
+ constexpr auto max = std::numeric_limits<int8_t>::max();
+ constexpr auto min = std::numeric_limits<int8_t>::min();
+
+ ASSERT_FALSE(mul_overflow<int8_t>(-2, 5, result));
+ EXPECT_EQ(result, -10);
+
+ ASSERT_FALSE(mul_overflow<int8_t>(-5, 3, result));
+ EXPECT_EQ(result, -15);
+
+ ASSERT_FALSE(mul_overflow<int8_t>(max, 1, result));
+ EXPECT_EQ(result, max);
+
+ ASSERT_FALSE(mul_overflow<int8_t>(max, -1, result));
+ EXPECT_EQ(result, -max);
+
+ ASSERT_FALSE(mul_overflow<int8_t>(1, min, result));
+ EXPECT_EQ(result, min);
+
+ ASSERT_FALSE(mul_overflow<int8_t>(-6, -10, result));
+ EXPECT_EQ(result, 60);
+}
+
+TEST(UtilsTests, mul_overflow_u8_detected) {
+ uint8_t result;
+ constexpr auto max = std::numeric_limits<uint8_t>::max();
+
+ EXPECT_TRUE(mul_overflow<uint8_t>(2, max, result));
+ EXPECT_TRUE(mul_overflow<uint8_t>(max, 3, result));
+ EXPECT_TRUE(mul_overflow<uint8_t>(5, 56, result));
+ EXPECT_TRUE(mul_overflow<uint8_t>(66, 4, result));
+}
+
+TEST(UtilsTests, mul_overflow_u8_zero_value) {
+ uint8_t result;
+ constexpr auto max = std::numeric_limits<uint8_t>::max();
+
+ ASSERT_FALSE(mul_overflow<uint8_t>(0, 0, result));
+ EXPECT_EQ(result, 0);
+
+ ASSERT_FALSE(mul_overflow<uint8_t>(0, max, result));
+ EXPECT_EQ(result, 0);
+
+ ASSERT_FALSE(mul_overflow<uint8_t>(max, 0, result));
+ EXPECT_EQ(result, 0);
+}
+
+TEST(UtilsTests, mul_overflow_u8_non_zero_value) {
+ uint8_t result;
+ constexpr auto max = std::numeric_limits<uint8_t>::max();
+
+ ASSERT_FALSE(mul_overflow<uint8_t>(2, 5, result));
+ EXPECT_EQ(result, 10);
+
+ ASSERT_FALSE(mul_overflow<uint8_t>(15, 3, result));
+ EXPECT_EQ(result, 45);
+
+ ASSERT_FALSE(mul_overflow<uint8_t>(max, 1, result));
+ EXPECT_EQ(result, max);
+}