Skip to content

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

[Core] Add non-zero guards for Col2Im shape_infer divisors (CWE-369) (#37329)

Xu Xing9b1d5c9494e8
diff --git a/src/core/shape_inference/include/col2im_shape_inference.hpp b/src/core/shape_inference/include/col2im_shape_inference.hpp
index 0ec27aa1fac..65a093dc7da 100644
--- a/src/core/shape_inference/include/col2im_shape_inference.hpp
+++ b/src/core/shape_inference/include/col2im_shape_inference.hpp
@@ -6,7 +6,9 @@
#include <cmath>
+#include "dimension_util.hpp"
#include "openvino/op/col2im.hpp"
+#include "openvino/util/math_util.hpp"
#include "utils.hpp"
namespace ov {
@@ -65,16 +67,24 @@ std::vector<TRShape> shape_infer(const Col2Im* op,
// ^
const size_t C_idx = is_batched ? 1 : 0;
const auto kernel_val = ov::op::get_input_const_data_as<TRShape, int64_t>(op, 2, tensor_accessor);
- if (kernel_val && data_shape.rank().is_static() && data_shape[C_idx].is_static()) {
- const auto& dividend = data_shape[C_idx].get_length();
- const auto divisor = ((*kernel_val)[0] * (*kernel_val)[1]);
- output_shape[idx] = dividend / divisor;
+ if (kernel_val && data_shape[C_idx].is_static()) {
+ int64_t divisor;
+ NODE_SHAPE_INFER_CHECK(op,
+ input_shapes,
+ !ov::util::mul_overflow<int64_t>((*kernel_val)[0], (*kernel_val)[1], divisor),
+ "kernel_size product overflows int64.");
+ NODE_SHAPE_INFER_CHECK(op,
+ input_shapes,
+ divisor > 0,
+ "kernel_size elements must be positive non-zero values.");
+ // Dimension division yields an empty dimension when it is not evenly divisible.
+ output_shape[idx] = data_shape[C_idx] / static_cast<typename TShape::value_type::value_type>(divisor);
NODE_SHAPE_INFER_CHECK(op,
input_shapes,
- dividend % divisor == 0,
+ !ov::util::dim::is_empty(output_shape[idx]),
"First non-batch dimension is not evenly divisible by Product(kernel_shape). Got: ",
- data_shape[C_idx].get_length());
+ data_shape[C_idx]);
}
// output_shape: (N, C, H, W)
@@ -98,6 +108,7 @@ std::vector<TRShape> shape_infer(const Col2Im* op,
using TVal = typename TShape::value_type::value_type;
TVal L_calculated = 1;
for (size_t d = 0; d < spatial_dims; ++d) {
+ NODE_SHAPE_INFER_CHECK(op, input_shapes, strides[d] != 0, "strides must be non-zero.");
L_calculated *= (((*output_size_val)[d].get_length() + pads_begin[d] + pads_end[d] -
dilations[d] * ((*kernel_val)[d] - 1) - 1) /
strides[d]) +
diff --git a/src/core/tests/type_prop/col2im.cpp b/src/core/tests/type_prop/col2im.cpp
index 0afc0e82e23..33b53ed85b5 100644
--- a/src/core/tests/type_prop/col2im.cpp
+++ b/src/core/tests/type_prop/col2im.cpp
@@ -6,6 +6,8 @@
#include <gtest/gtest.h>
+#include <limits>
+
#include "common_test_utils/test_assertions.hpp"
#include "common_test_utils/type_prop.hpp"
#include "openvino/op/constant.hpp"
@@ -402,5 +404,75 @@ TEST_F(TypePropCol2ImTest, interval_inputs_from_shapeof) {
testing::ElementsAre(data_symbols[0], nullptr, output_size_symbols[0], output_size_symbols[1]));
}
+TEST_F(TypePropCol2ImTest, kernel_size_zero_both_dims) {
+ const auto data = std::make_shared<Parameter>(element::i64, PartialShape{3, 12, 81});
+ const auto output_size = std::make_shared<Constant>(element::i64, Shape{2}, std::vector<int64_t>{16, 16});
+ const auto kernel_size = std::make_shared<Constant>(element::i64, Shape{2}, std::vector<int64_t>{0, 0});
+
+ OV_EXPECT_THROW(
+ std::ignore = make_op(data, output_size, kernel_size, Strides{2, 2}, Strides{2, 2}, Shape{2, 2}, Shape{2, 2}),
+ ov::NodeValidationFailure,
+ HasSubstr("kernel"));
+}
+
+TEST_F(TypePropCol2ImTest, kernel_size_zero_one_dim) {
+ const auto data = std::make_shared<Parameter>(element::i64, PartialShape{3, 12, 81});
+ const auto output_size = std::make_shared<Constant>(element::i64, Shape{2}, std::vector<int64_t>{16, 16});
+ const auto kernel_size = std::make_shared<Constant>(element::i64, Shape{2}, std::vector<int64_t>{0, 3});
+
+ OV_EXPECT_THROW(
+ std::ignore = make_op(data, output_size, kernel_size, Strides{2, 2}, Strides{2, 2}, Shape{2, 2}, Shape{2, 2}),
+ ov::NodeValidationFailure,
+ HasSubstr("kernel"));
+}
+
+TEST_F(TypePropCol2ImTest, zero_strides_both_dims) {
+ const auto data = std::make_shared<Parameter>(element::i64, Shape{12, 324});
+ const auto output_size = std::make_shared<Constant>(element::i64, Shape{2}, std::vector<int32_t>{32, 32});
+ const auto kernel_size = std::make_shared<Constant>(element::i64, Shape{2}, std::vector<int32_t>{2, 2});
+
+ OV_EXPECT_THROW(
+ std::ignore = make_op(data, output_size, kernel_size, Strides{0, 0}, Strides{1, 1}, Shape{0, 0}, Shape{0, 0}),
+ ov::NodeValidationFailure,
+ HasSubstr("strides"));
+}
+
+TEST_F(TypePropCol2ImTest, zero_strides_one_dim) {
+ const auto data = std::make_shared<Parameter>(element::i64, Shape{12, 324});
+ const auto output_size = std::make_shared<Constant>(element::i64, Shape{2}, std::vector<int32_t>{32, 32});
+ const auto kernel_size = std::make_shared<Constant>(element::i64, Shape{2}, std::vector<int32_t>{2, 2});
+
+ OV_EXPECT_THROW(
+ std::ignore = make_op(data, output_size, kernel_size, Strides{1, 0}, Strides{1, 1}, Shape{0, 0}, Shape{0, 0}),
+ ov::NodeValidationFailure,
+ HasSubstr("strides"));
+}
+
+TEST_F(TypePropCol2ImTest, kernel_size_overflow_product) {
+ const auto data = std::make_shared<Parameter>(element::i64, PartialShape{1, 12, 81});
+ const auto output_size = std::make_shared<Constant>(element::i64, Shape{2}, std::vector<int64_t>{16, 16});
+ const int64_t large = std::numeric_limits<int64_t>::max() / 2 + 1;
+ const auto kernel_size = std::make_shared<Constant>(element::i64, Shape{2}, std::vector<int64_t>{large, 2});
+
+ OV_EXPECT_THROW(
+ std::ignore = make_op(data, output_size, kernel_size, Strides{1, 1}, Strides{1, 1}, Shape{0, 0}, Shape{0, 0}),
+ ov::NodeValidationFailure,
+ HasSubstr("kernel_size product overflows"));
+}
+
+TEST_F(TypePropCol2ImTest, kernel_size_product_wraps_to_zero) {
+ const auto data = std::make_shared<Parameter>(element::i64, PartialShape{1, 12, 81});
+ const auto output_size = std::make_shared<Constant>(element::i64, Shape{2}, std::vector<int64_t>{16, 16});
+ // Both elements are positive, but their product wraps around to exactly 0 without the overflow guard.
+ constexpr int64_t two_pow_32 = int64_t{1} << 32;
+ const auto kernel_size =
+ std::make_shared<Constant>(element::i64, Shape{2}, std::vector<int64_t>{two_pow_32, two_pow_32});
+
+ OV_EXPECT_THROW(
+ std::ignore = make_op(data, output_size, kernel_size, Strides{1, 1}, Strides{1, 1}, Shape{0, 0}, Shape{0, 0}),
+ ov::NodeValidationFailure,
+ HasSubstr("kernel_size product overflows"));
+}
+
} // namespace test
} // namespace ov