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
Add shape size check to avoid overflow (#35277)
Mang Guo812a00ba4723
diff --git a/src/common/util/include/openvino/util/common_util.hpp b/src/common/util/include/openvino/util/common_util.hppindex 38c002ed43e..ac5baaca71d 100644--- a/src/common/util/include/openvino/util/common_util.hpp+++ b/src/common/util/include/openvino/util/common_util.hpp@@ -273,6 +273,40 @@ protected:}};+/**+ * @brief Adds two integral values+ *+ * The result value is not valid if overflow detected.+ *+ * @param T Type of values to add. Must be an integral type.+ * @param x First value to add.+ * @param y Second value to add.+ * @param result Reference to store result value.+ * @return True if overflow occurs, false otherwise+ */+template <class T>+constexpr bool add_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_add_overflow(x, y, &result);+#else+ constexpr auto max = std::numeric_limits<T>::max();++ if constexpr (std::is_unsigned_v<T>) {+ if (x > max - y) {+ return true;+ }+ } else {+ constexpr auto min = std::numeric_limits<T>::lowest();+ if ((y > 0 && x > max - y) || (y < 0 && x < min - y)) {+ return true;+ }+ }+ result = x + y;+ return false;+#endif+}+/*** @brief Multiplies two integral values*diff --git a/src/plugins/intel_cpu/src/memory_desc/cpu_blocked_memory_desc.cpp b/src/plugins/intel_cpu/src/memory_desc/cpu_blocked_memory_desc.cppindex 94885ceac91..3c2927ecb5b 100644--- a/src/plugins/intel_cpu/src/memory_desc/cpu_blocked_memory_desc.cpp+++ b/src/plugins/intel_cpu/src/memory_desc/cpu_blocked_memory_desc.cpp@@ -7,6 +7,7 @@#include <algorithm>#include <cstddef>#include <functional>+#include <limits>#include <memory>#include <numeric>@@ -16,7 +17,10 @@#include "memory_desc/blocked_memory_desc.h"#include "memory_desc/cpu_memory_desc.h"#include "openvino/core/except.hpp"+#include "openvino/core/memory_util.hpp"+#include "openvino/core/shape_util.hpp"#include "openvino/core/type/element_type.hpp"+#include "openvino/util/common_util.hpp"#include "utils/general_utils.h"namespace ov::intel_cpu {@@ -151,11 +155,16 @@ bool CpuBlockedMemoryDesc::canComputeMemSizeZeroDims() const {}size_t CpuBlockedMemoryDesc::getCurrentMemSizeImp() const {- auto e_size = getOffsetPadding(); // size in bytes (from begin of data to last element)+ size_t e_size = getOffsetPadding(); // elements from begin of data to the last addressed elementif (!getShape().hasZeroDims()) {- e_size += 1;+ OPENVINO_ASSERT(!ov::util::add_overflow(e_size, static_cast<size_t>(1), e_size),+ "CpuBlockedMemoryDesc::getCurrentMemSizeImp overflow while adding tail element");for (size_t j = 0; j < getBlockDims().size(); j++) {- e_size += (getBlockDims()[j] - 1) * getStrides()[j];+ const auto dim_stride = ov::util::shape_size_safe(ov::Shape{getBlockDims()[j] - 1, getStrides()[j]});+ OPENVINO_ASSERT(dim_stride.has_value(),+ "CpuBlockedMemoryDesc::getCurrentMemSizeImp overflow while multiplying dim and stride");+ OPENVINO_ASSERT(!ov::util::add_overflow(e_size, *dim_stride, e_size),+ "CpuBlockedMemoryDesc::getCurrentMemSizeImp overflow while accumulating element count");}}@@ -165,20 +174,14 @@ size_t CpuBlockedMemoryDesc::getCurrentMemSizeImp() const {return e_size;}- auto byte_size = e_size * prc.bitwidth();-- if (any_of(prc, ov::element::u3, ov::element::u6)) {- constexpr size_t storage_unit_size = 24;- byte_size += storage_unit_size - 1;- byte_size /= storage_unit_size;- byte_size *= 3;- } else {- constexpr size_t storage_unit_size = 8;- byte_size += storage_unit_size - 1;- byte_size /= storage_unit_size;- }-- return byte_size;+ const auto byte_size = ov::util::get_memory_size_safe(prc, e_size);+ OPENVINO_ASSERT(byte_size.has_value(),+ "CpuBlockedMemoryDesc::getCurrentMemSizeImp overflow while converting elements to byte size");+ OPENVINO_ASSERT(*byte_size <= static_cast<size_t>(std::numeric_limits<ptrdiff_t>::max()),+ "CpuBlockedMemoryDesc::getCurrentMemSizeImp requested allocation size { ",+ *byte_size,+ " } exceeds PTRDIFF_MAX");+ return *byte_size;}size_t CpuBlockedMemoryDesc::getMaxMemSize() const {