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] Scatter elements update ref throws if cannot normalize index (#37804)
Pawel Raaszf24b8c63a784
diff --git a/src/core/reference/include/openvino/reference/scatter_elements_update.hpp b/src/core/reference/include/openvino/reference/scatter_elements_update.hppindex 44a3947e4e4..5b2e9e4de87 100644--- a/src/core/reference/include/openvino/reference/scatter_elements_update.hpp+++ b/src/core/reference/include/openvino/reference/scatter_elements_update.hpp@@ -11,6 +11,7 @@#include "openvino/core/except.hpp"#include "openvino/core/shape.hpp"+#include "openvino/core/shape_util.hpp"#include "openvino/op/scatter_elements_update.hpp"#include "openvino/reference/rounding_guard.hpp"#include "openvino/reference/utils/coordinate_index.hpp"@@ -20,23 +21,7 @@ namespace ov {namespace reference {template <typename T>size_t normalize_index(const T idx, const size_t dim_value) {- // Per the operator specification the index must lie within [-dim_value, dim_value - 1].- if (idx < 0) {- const int64_t normalized = static_cast<int64_t>(idx) + static_cast<int64_t>(dim_value);- OPENVINO_ASSERT(normalized >= 0,- "ScatterElementsUpdate index ",- static_cast<int64_t>(idx),- " is out of bounds for the axis of size ",- dim_value);- return static_cast<size_t>(normalized);- } else {- OPENVINO_ASSERT(static_cast<size_t>(idx) < dim_value,- "ScatterElementsUpdate index ",- static_cast<int64_t>(idx),- " is out of bounds for the axis of size ",- dim_value);- return static_cast<size_t>(idx);- }+ return static_cast<size_t>(ov::util::normalize_shape_index(static_cast<std::ptrdiff_t>(idx), dim_value));}namespace {diff --git a/src/core/tests/eval.cpp b/src/core/tests/eval.cppindex 0eeb8b0bc85..e1ec07a12cb 100644--- a/src/core/tests/eval.cpp+++ b/src/core/tests/eval.cpp@@ -1537,8 +1537,8 @@ TYPED_TEST_P(ScatterElementsUpdateEvalTest, evaluate_scatter_elements_update_outmake_tensor<element::Type_t::f32>(indices_shape, {1.0f, 1.1f, 1.2f, 2.0f, 2.1f, 2.2f}),make_tensor<element::Type_t::i64>({}, {0})};OV_EXPECT_THROW(model->evaluate(out_vector, in_vector),- ov::AssertFailure,- HasSubstr("is out of bounds for the axis of size"));+ ov::Exception,+ HasSubstr("Accessing out-of-range dimension"));}TYPED_TEST_P(ScatterElementsUpdateEvalTest, evaluate_scatter_elements_update_out_of_bounds_negative_index) {@@ -1561,8 +1561,8 @@ TYPED_TEST_P(ScatterElementsUpdateEvalTest, evaluate_scatter_elements_update_outmake_tensor<element::Type_t::f32>(indices_shape, {1.0f, 1.1f, 1.2f, 2.0f, 2.1f, 2.2f}),make_tensor<element::Type_t::i64>({}, {0})};OV_EXPECT_THROW(model->evaluate(out_vector, in_vector),- ov::AssertFailure,- HasSubstr("is out of bounds for the axis of size"));+ ov::Exception,+ HasSubstr("Accessing out-of-range dimension"));}REGISTER_TYPED_TEST_SUITE_P(ScatterElementsUpdateEvalTest,diff --git a/src/plugins/template/tests/functional/op_reference/scatter_elements_update.cpp b/src/plugins/template/tests/functional/op_reference/scatter_elements_update.cppindex a17395e3cb1..6488e39069b 100644--- a/src/plugins/template/tests/functional/op_reference/scatter_elements_update.cpp+++ b/src/plugins/template/tests/functional/op_reference/scatter_elements_update.cpp@@ -7,6 +7,7 @@#include <gtest/gtest.h>#include "base_reference_test.hpp"+#include "common_test_utils/test_assertions.hpp"using namespace reference_tests;using namespace ov;@@ -362,4 +363,27 @@ INSTANTIATE_TEST_SUITE_P(smoke_ScatterElementsUpdate,ReferenceScatterElementsUpdateV12LayerTest,::testing::ValuesIn(collect_scatter_eu_v12_params()),ReferenceScatterElementsUpdateV12LayerTest::getTestCaseName);++// Regression test for CVS-193801: out-of-range indices used to cause a heap-buffer-overflow+// write in the reference kernel instead of a graceful validation failure.+class ReferenceScatterElementsUpdateV12LayerTestNegative : public ReferenceScatterElementsUpdateV12LayerTest {};++TEST_P(ReferenceScatterElementsUpdateV12LayerTestNegative, IndicesOutOfRangeThrows) {+ OV_EXPECT_THROW_HAS_SUBSTRING(Exec(), ov::Exception, "Accessing out-of-range dimension");+}++std::vector<ScatterElementsUpdateParams> generateScatterOutOfRangeIndicesParams() {+ return {+ ScatterElementsUpdateParams(reference_tests::Tensor({2, 2}, element::f32, std::vector<float>{1, 2, 3, 4}),+ reference_tests::Tensor({2, 2}, element::i64, std::vector<int64_t>{5, 1, 0, 0}),+ reference_tests::Tensor({2, 2}, element::f32, std::vector<float>{10, 20, 30, 40}),+ reference_tests::Tensor({1}, element::i64, std::vector<int64_t>{0}),+ reference_tests::Tensor({2, 2}, element::f32, std::vector<float>{0, 0, 0, 0})),+ };+}++INSTANTIATE_TEST_SUITE_P(smoke_ScatterElementsUpdateNegative,+ ReferenceScatterElementsUpdateV12LayerTestNegative,+ ::testing::ValuesIn(generateScatterOutOfRangeIndicesParams()),+ ReferenceScatterElementsUpdateV12LayerTestNegative::getTestCaseName);} // namespace