Skip to content

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

[Core] Add signed division overflow guard for (INT_MIN, -1) in Divide reference kernel (#38215)

Xu Xinga0860980481d
diff --git a/src/core/reference/include/openvino/reference/divide.hpp b/src/core/reference/include/openvino/reference/divide.hpp
index 806307b267d..dcc48197dc7 100644
--- a/src/core/reference/include/openvino/reference/divide.hpp
+++ b/src/core/reference/include/openvino/reference/divide.hpp
@@ -5,9 +5,10 @@
#pragma once
#include <cstddef>
-#include <stdexcept>
+#include <limits>
#include <type_traits>
+#include "openvino/core/except.hpp"
#include "openvino/core/shape.hpp"
#include "openvino/core/type/bfloat16.hpp"
#include "openvino/core/type/float16.hpp"
@@ -23,21 +24,27 @@ constexpr T div(const T x, const T y) {
return x / y;
}
-// NOTE: Execution throws `std::domain_error` if either a non-integral value or an
-// out-of-bounds value is detected in the input tensor.
+// NOTE: Execution throws `ov::AssertFailure` on integer division by zero and, for
+// signed integral types, on the `std::numeric_limits<T>::min() / -1` overflow case.
+// This helper operates on values already typed as `T`; any non-integral or
+// out-of-range input validation, if required, must occur before calling it.
template <class T>
-T try_div(const T x, const T y) {
- if (y == 0) {
- throw std::domain_error("integer division by zero");
+void validate_div_inputs(const T x, const T y) {
+ OPENVINO_ASSERT(y != 0, "integer division by zero");
+ if constexpr (std::is_signed_v<T>) {
+ OPENVINO_ASSERT(x != std::numeric_limits<T>::min() || y != -1, "integer division overflow");
}
+}
+
+template <class T>
+T try_div(const T x, const T y) {
+ validate_div_inputs(x, y);
return div(x, y);
}
template <class T>
T try_python_div(const T x, const T y) {
- if (y == 0) {
- throw std::domain_error("integer division by zero");
- }
+ validate_div_inputs(x, y);
T quot = div(x, y);
if constexpr (std::is_unsigned_v<T>) {
diff --git a/src/plugins/template/tests/functional/op_reference/divide.cpp b/src/plugins/template/tests/functional/op_reference/divide.cpp
index 6c791dfdf1f..e2cdc6b078c 100644
--- a/src/plugins/template/tests/functional/op_reference/divide.cpp
+++ b/src/plugins/template/tests/functional/op_reference/divide.cpp
@@ -6,7 +6,10 @@
#include <gtest/gtest.h>
+#include <limits>
+
#include "base_reference_test.hpp"
+#include "common_test_utils/test_assertions.hpp"
using namespace ov;
using namespace reference_tests;
@@ -127,6 +130,62 @@ TEST_P(ReferenceDivideRoundingLayerTest, DivideWithHardcodedRefs) {
Exec();
}
+struct DivideOverflowParams {
+ template <class IT>
+ DivideOverflowParams(const element::Type& iType, IT x, IT y, bool pythondiv)
+ : inType(iType),
+ inputData1(CreateTensor(Shape{1}, iType, std::vector<IT>{x})),
+ inputData2(CreateTensor(Shape{1}, iType, std::vector<IT>{y})),
+ pythonDivision(pythondiv) {}
+
+ element::Type inType;
+ ov::Tensor inputData1;
+ ov::Tensor inputData2;
+ bool pythonDivision;
+};
+
+class ReferenceDivideOverflowTest : public testing::TestWithParam<DivideOverflowParams>, public CommonReferenceTest {
+public:
+ void SetUp() override {
+ const auto& params = GetParam();
+ function = CreateFunction(params.inType, params.pythonDivision);
+ inputData = {params.inputData1, params.inputData2};
+ }
+
+ static std::string getTestCaseName(const testing::TestParamInfo<DivideOverflowParams>& obj) {
+ const auto& param = obj.param;
+ std::ostringstream result;
+ result << "iType=" << param.inType << "_";
+ result << "pythonDivision=" << param.pythonDivision;
+ return result.str();
+ }
+
+private:
+ static std::shared_ptr<Model> CreateFunction(const element::Type& input_type, bool pythondiv) {
+ const auto in1 = std::make_shared<op::v0::Parameter>(input_type, Shape{1});
+ const auto in2 = std::make_shared<op::v0::Parameter>(input_type, Shape{1});
+ const auto divide = std::make_shared<op::v1::Divide>(in1, in2, pythondiv);
+ return std::make_shared<Model>(OutputVector{divide}, ParameterVector{in1, in2});
+ }
+};
+
+TEST_P(ReferenceDivideOverflowTest, ThrowsOnIntMinDividedByMinusOne) {
+ OV_EXPECT_THROW(Exec(), ov::Exception, testing::HasSubstr("integer division overflow"));
+}
+
+INSTANTIATE_TEST_SUITE_P(
+ smoke_Divide_Overflow,
+ ReferenceDivideOverflowTest,
+ ::testing::Values(DivideOverflowParams(element::i8, std::numeric_limits<int8_t>::min(), int8_t{-1}, true),
+ DivideOverflowParams(element::i8, std::numeric_limits<int8_t>::min(), int8_t{-1}, false),
+ DivideOverflowParams(element::i16, std::numeric_limits<int16_t>::min(), int16_t{-1}, true),
+ DivideOverflowParams(element::i16, std::numeric_limits<int16_t>::min(), int16_t{-1}, false),
+ DivideOverflowParams(element::i32, std::numeric_limits<int32_t>::min(), int32_t{-1}, true),
+ DivideOverflowParams(element::i32, std::numeric_limits<int32_t>::min(), int32_t{-1}, false),
+ DivideOverflowParams(element::i64, std::numeric_limits<int64_t>::min(), int64_t{-1}, true),
+ DivideOverflowParams(element::i64, std::numeric_limits<int64_t>::min(), int64_t{-1}, false)),
+ ReferenceDivideOverflowTest::getTestCaseName);
+
template <element::Type_t IN_ET>
std::vector<DivideParams> generateParamsForDivide() {
using T = typename element_type_traits<IN_ET>::value_type;