Skip to content

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

[cpu] Fix out-of-bounds access in GatherND and ScatterUpdate (#38297)

Pawel Raasz2f44767919c2
diff --git a/src/plugins/intel_cpu/src/nodes/gather_nd.cpp b/src/plugins/intel_cpu/src/nodes/gather_nd.cpp
index 7f74f36c8ab..6b03aa13581 100644
--- a/src/plugins/intel_cpu/src/nodes/gather_nd.cpp
+++ b/src/plugins/intel_cpu/src/nodes/gather_nd.cpp
@@ -191,7 +191,9 @@ void GatherND::GatherNDExecutor::gatherBlocks(const MemoryPtr& srcMemPtr,
const auto* indices = idxMemPtr->getDataAs<const int32_t>();
auto* dstData = dstMemPtr->getDataAs<uint8_t>();
- parallel_nt(0, [&](const int ithr, const int nthr) {
+ const auto nthr = parallel_get_max_threads();
+ std::vector<uint8_t> out_of_range_flags(nthr, 0);
+ parallel_nt(nthr, [&](const int ithr, const int nthr) {
size_t start(0LU);
size_t end(0LU);
splitter(workAmount, nthr, ithr, start, end);
@@ -206,17 +208,19 @@ void GatherND::GatherNDExecutor::gatherBlocks(const MemoryPtr& srcMemPtr,
const int32_t* shiftedIndices = indices + bStart * idxBatchStride + cStart * sliceRank;
uint8_t* shiftedDstData = dstData + bStart * dstBatchStride + cStart * dataLength;
+ auto bad = false;
for (size_t b = bStart; b < batchSize; b++) {
for (size_t j = cStart; j < cycles; j++) {
size_t dataIdx = 0LU;
for (size_t i = 0; i < sliceRank; i++) {
- const int32_t index = HandleNegativeIndices(shiftedIndices, i);
+ const int32_t index = HandleNegativeIndices(shiftedIndices, i, bad);
dataIdx += srcShifts[i] * index;
}
cpu_memcpy(shiftedDstData, &(shiftedSrcData[dataIdx]), dataLength);
shiftedDstData += dataLength;
shiftedIndices += sliceRank;
if (++workCounter == end) {
+ out_of_range_flags[ithr] |= static_cast<uint8_t>(bad);
return;
}
}
@@ -224,6 +228,7 @@ void GatherND::GatherNDExecutor::gatherBlocks(const MemoryPtr& srcMemPtr,
shiftedSrcData += srcBatchStride;
}
});
+ OPENVINO_ASSERT(all_of_values(out_of_range_flags, uint8_t{0}), "GatherND indices value is out of bounds.");
}
template <typename dataType>
@@ -234,7 +239,9 @@ void GatherND::GatherNDExecutor::gatherElementwise(const MemoryPtr& srcMemPtr,
const auto* indices = idxMemPtr->getDataAs<const int32_t>();
auto* dstData = dstMemPtr->getDataAs<dataType>();
- parallel_nt(0, [&](const int ithr, const int nthr) {
+ const auto nthr = parallel_get_max_threads();
+ std::vector<uint8_t> out_of_range_flags(nthr, 0);
+ parallel_nt(nthr, [&](const int ithr, const int nthr) {
size_t start(0LU);
size_t end(0LU);
splitter(workAmount, nthr, ithr, start, end);
@@ -249,17 +256,19 @@ void GatherND::GatherNDExecutor::gatherElementwise(const MemoryPtr& srcMemPtr,
const int32_t* shiftedIndices = indices + bStart * idxBatchStride + cStart * sliceRank;
dataType* shiftedDstData = dstData + bStart * dstBatchStride + cStart * dataLength;
+ auto bad = false;
for (size_t b = bStart; b < batchSize; b++) {
for (size_t j = cStart; j < cycles; j++) {
size_t dataIdx = 0LU;
for (size_t i = 0LU; i < sliceRank; i++) {
- const int32_t index = HandleNegativeIndices(shiftedIndices, i);
+ const int32_t index = HandleNegativeIndices(shiftedIndices, i, bad);
dataIdx += srcShifts[i] * index;
}
shiftedDstData[0] = shiftedSrcData[dataIdx];
shiftedDstData++;
shiftedIndices += sliceRank;
if (++workCounter == end) {
+ out_of_range_flags[ithr] |= static_cast<uint8_t>(bad);
return;
}
}
@@ -267,14 +276,20 @@ void GatherND::GatherNDExecutor::gatherElementwise(const MemoryPtr& srcMemPtr,
shiftedSrcData += srcBatchStride;
}
});
+ OPENVINO_ASSERT(all_of_values(out_of_range_flags, uint8_t{0}), "GatherND indices value is out of bounds.");
}
-int32_t GatherND::GatherNDExecutor::HandleNegativeIndices(const int32_t* indices, size_t idx) const {
+int32_t GatherND::GatherNDExecutor::HandleNegativeIndices(const int32_t* indices,
+ size_t idx,
+ bool& out_of_range) const {
int32_t index = indices[idx];
+ const auto dim_size = static_cast<int32_t>(srcDims[idx + batchDims]);
if (index < 0) {
- index += static_cast<int32_t>(srcDims[idx + batchDims]);
+ index += dim_size;
}
- return index;
+ const auto bad = static_cast<uint32_t>(index) >= static_cast<uint32_t>(dim_size);
+ out_of_range |= bad;
+ return bad ? 0 : index;
}
void GatherND::executeDynamicImpl(const dnnl::stream& strm) {
diff --git a/src/plugins/intel_cpu/src/nodes/gather_nd.h b/src/plugins/intel_cpu/src/nodes/gather_nd.h
index 9935835a546..644fca827dd 100644
--- a/src/plugins/intel_cpu/src/nodes/gather_nd.h
+++ b/src/plugins/intel_cpu/src/nodes/gather_nd.h
@@ -5,9 +5,11 @@
#pragma once
#include <cstddef>
+#include <cstdint>
#include <memory>
#include <oneapi/dnnl/dnnl_common.hpp>
#include <string>
+#include <vector>
#include "cpu_memory.h"
#include "cpu_types.h"
@@ -52,7 +54,9 @@ private:
template <typename dataType>
void gatherElementwise(const MemoryPtr& srcMemPtr, const MemoryPtr& idxMemPtr, const MemoryPtr& dstMemPtr);
void gatherBlocks(const MemoryPtr& srcMemPtr, const MemoryPtr& idxMemPtr, const MemoryPtr& dstMemPtr);
- int32_t HandleNegativeIndices(const int32_t* indices, size_t idx) const;
+ // Sets out_of_range instead of throwing: an exception escaping a parallel_nt worker is not
+ // reliably propagated on the OpenMP threading backend.
+ int32_t HandleNegativeIndices(const int32_t* indices, size_t idx, bool& out_of_range) const;
size_t batchSize = 1LU;
size_t dataSize = 1LU;
diff --git a/src/plugins/intel_cpu/src/nodes/scatter_update.cpp b/src/plugins/intel_cpu/src/nodes/scatter_update.cpp
index db139798ce1..bac4bdd26a4 100644
--- a/src/plugins/intel_cpu/src/nodes/scatter_update.cpp
+++ b/src/plugins/intel_cpu/src/nodes/scatter_update.cpp
@@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
//
-#include "scatter_update.h"
+#include "openvino/op/scatter_update.hpp"
#include <array>
#include <cassert>
@@ -35,7 +35,7 @@
#include "openvino/core/type/float16.hpp"
#include "openvino/op/scatter_elements_update.hpp"
#include "openvino/op/scatter_nd_update.hpp"
-#include "openvino/op/scatter_update.hpp"
+#include "scatter_update.h"
#include "selective_build.h"
#include "shape_inference/shape_inference_cpu.hpp"
#include "utils/debug_capabilities.h"
@@ -880,6 +880,8 @@ void ScatterUpdate::execute([[maybe_unused]] const dnnl::stream& strm) {
auto* pindices = reinterpret_cast<int32_t*>(indicesPtr);
auto* pupdate = reinterpret_cast<int32_t*>(updatePtr);
for (size_t i = 0; i < updateCnt; i++) {
+ CPU_NODE_ASSERT(pindices[i] >= 0 && static_cast<size_t>(pindices[i]) < srcLength,
+ "has indices value that points to non-existing output tensor element");
pdst[pindices[i]] = pupdate[i];
}
return;
@@ -904,17 +906,29 @@ void ScatterUpdate::execute([[maybe_unused]] const dnnl::stream& strm) {
size_t srcDimAxis = srcDataDim[axis];
std::vector<size_t> indicesBlockND = getBlockND(indicesDim);
- parallel_nt(0, [&](const int ithr, const int nthr) {
+ // fully validate (possibly negative) indices here so the per-element write loops stay branch-free.
+ // Violations are flagged, not thrown, inside the loop: an exception escaping a parallel_nt worker
+ // is not reliably propagated on the OpenMP threading backend.
+ const bool allow_negative_indices = scatterUpdateMode == ScatterUpdateMode::ScatterElementsUpdate;
+ const auto nthr = parallel_get_max_threads();
+ std::vector<uint8_t> out_of_range_flags(nthr, 0);
+ parallel_nt(nthr, [&](const int ithr, const int nthr) {
size_t start = 0;
size_t end = 0;
splitter(indicesBlockND[0], nthr, ithr, start, end);
+ bool local_out_of_range = false;
for (size_t i = start; i < end; i++) {
int64_t idxValue = getIndicesValue(indicesPtr, i);
- CPU_NODE_ASSERT(idxValue < static_cast<int64_t>(srcDimAxis) &&
- (idxValue >= 0 || scatterUpdateMode == ScatterUpdateMode::ScatterElementsUpdate),
- "have indices value that points to non-existing output tensor element");
+ if (allow_negative_indices && idxValue < 0) {
+ idxValue += static_cast<int64_t>(srcDimAxis);
+ }
+ local_out_of_range =
+ local_out_of_range || static_cast<uint64_t>(idxValue) >= static_cast<uint64_t>(srcDimAxis);
}
+ out_of_range_flags[ithr] = static_cast<uint8_t>(local_out_of_range);
});
+ CPU_NODE_ASSERT(all_of_values(out_of_range_flags, uint8_t{0}),
+ "have indices value that points to non-existing output tensor element");
if (scatterUpdateMode == ScatterUpdateMode::ScatterUpdate) {
VectorDims indicesDim = getParentEdgeAt(INDICES_ID)->getMemory().getStaticDims();
diff --git a/src/plugins/intel_cpu/tests/functional/custom/single_layer_tests/gather_nd.cpp b/src/plugins/intel_cpu/tests/functional/custom/single_layer_tests/gather_nd.cpp
index 97d85b99f53..f14b86e4cf1 100644
--- a/src/plugins/intel_cpu/tests/functional/custom/single_layer_tests/gather_nd.cpp
+++ b/src/plugins/intel_cpu/tests/functional/custom/single_layer_tests/gather_nd.cpp
@@ -2,8 +2,10 @@
// SPDX-License-Identifier: Apache-2.0
//
-#include "shared_test_classes/base/ov_subgraph.hpp"
+#include "common_test_utils/test_assertions.hpp"
+#include "functional_test_utils/skip_tests_config.hpp"
#include "openvino/op/gather_nd.hpp"
+#include "shared_test_classes/base/ov_subgraph.hpp"
namespace ov {
namespace test {
@@ -179,6 +181,36 @@ INSTANTIATE_TEST_SUITE_P(smoke_GatherND8DynamicBD_2,
subset_BD2,
GatherNDLayerCPUTest::getTestCaseName);
+class GatherND8LayerCPUErrorConditionTest : public GatherND8LayerCPUTest {};
+
+TEST_P(GatherND8LayerCPUErrorConditionTest, ThrowsOnOutOfRangeIndices) {
+ SKIP_IF_CURRENT_TEST_IS_DISABLED();
+ compile_model();
+ // Both CPU and TEMPLATE plugins should throw; a mismatch is a bug.
+ for (const auto& targetStaticShapeVec : targetStaticShapes) {
+ generate_inputs(targetStaticShapeVec);
+ OV_EXPECT_THROW(get_plugin_outputs(), ov::Exception, ::testing::_);
+ OV_EXPECT_THROW(calculate_refs(), ov::Exception, ::testing::_);
+ }
+}
+
+const std::vector<InputShape> inputShapesOutOfRange = {
+ {{4, 4}, {{4, 4}}},
+};
+
+const std::vector<std::pair<Shape, std::vector<int>>> indexesShapesOutOfRange = {
+ std::pair<Shape, std::vector<int>>{{1, 2}, {40, 0}},
+};
+
+INSTANTIATE_TEST_SUITE_P(smoke_GatherND8OutOfRangeIndices,
+ GatherND8LayerCPUErrorConditionTest,
+ ::testing::Combine(::testing::ValuesIn(inputShapesOutOfRange),
+ ::testing::ValuesIn(indexesShapesOutOfRange),
+ ::testing::ValuesIn(inputPrecisions),
+ ::testing::ValuesIn(indexesPrecisions),
+ ::testing::Values(0)),
+ GatherND8LayerCPUTest::getTestCaseName);
+
} // namespace
} // namespace test
} // namespace ov
diff --git a/src/plugins/intel_cpu/tests/functional/custom/single_layer_tests/scatter_elements_update.cpp b/src/plugins/intel_cpu/tests/functional/custom/single_layer_tests/scatter_elements_update.cpp
index b70050b9b68..2c653ec5500 100644
--- a/src/plugins/intel_cpu/tests/functional/custom/single_layer_tests/scatter_elements_update.cpp
+++ b/src/plugins/intel_cpu/tests/functional/custom/single_layer_tests/scatter_elements_update.cpp
@@ -3,9 +3,11 @@
//
#include "common_test_utils/ov_tensor_utils.hpp"
+#include "common_test_utils/test_assertions.hpp"
+#include "functional_test_utils/skip_tests_config.hpp"
+#include "openvino/op/scatter_elements_update.hpp"
#include "shared_test_classes/base/ov_subgraph.hpp"
#include "utils/cpu_test_utils.hpp"
-#include "openvino/op/scatter_elements_update.hpp"
using namespace CPUTestUtils;
namespace ov {
@@ -77,10 +79,10 @@ protected:
}
} else {
if (inputPrecision.is_real()) {
- ov::test::utils::InputGenerateData in_data;
- in_data.start_from = 0;
- in_data.range = 10;
- in_data.resolution = 1000;
+ ov::test::utils::InputGenerateData in_data;
+ in_data.start_from = 0;
+ in_data.range = 10;
+ in_data.resolution = 1000;
tensor = ov::test::utils::create_and_fill_tensor(inputPrecision, targetShape, in_data);
} else {
tensor = ov::test::utils::create_and_fill_tensor(inputPrecision, targetShape);
@@ -169,5 +171,33 @@ INSTANTIATE_TEST_SUITE_P(smoke_CompareWithRefs,
::testing::ValuesIn(inputPrecisions),
::testing::ValuesIn(constantPrecisions)),
ScatterElementsUpdateLayerCPUTest::getTestCaseName);
+
+class ScatterElementsUpdateLayerCPUErrorConditionTest : public ScatterElementsUpdateLayerCPUTest {};
+
+TEST_P(ScatterElementsUpdateLayerCPUErrorConditionTest, ThrowsOnOutOfRangeIndices) {
+ SKIP_IF_CURRENT_TEST_IS_DISABLED();
+ compile_model();
+ // Both CPU and TEMPLATE plugins should throw; a mismatch is a bug.
+ for (const auto& targetStaticShapeVec : targetStaticShapes) {
+ generate_inputs(targetStaticShapeVec);
+ OV_EXPECT_THROW(get_plugin_outputs(), ov::Exception, ::testing::_);
+ OV_EXPECT_THROW(calculate_refs(), ov::Exception, ::testing::_);
+ }
+}
+
+const std::vector<ScatterElementsUpdateLayerParams> scatterOutOfRangeParams = {
+ ScatterElementsUpdateLayerParams{
+ ScatterElementsUpdateShapes{{{-1, -1}, {{2, 2}}}, {{-1, -1}, {{2, 2}}}, {{-1, -1}, {{2, 2}}}},
+ IndicesValues{-1000, 1, 0, 0},
+ },
+};
+
+INSTANTIATE_TEST_SUITE_P(smoke_ScatterElementsUpdateOutOfRangeIndices,
+ ScatterElementsUpdateLayerCPUErrorConditionTest,
+ ::testing::Combine(::testing::ValuesIn(scatterOutOfRangeParams),
+ ::testing::Values(std::int64_t{0}),
+ ::testing::ValuesIn(inputPrecisions),
+ ::testing::ValuesIn(constantPrecisions)),
+ ScatterElementsUpdateLayerCPUTest::getTestCaseName);
} // namespace test
} // namespace ov
diff --git a/src/plugins/intel_cpu/tests/functional/custom/single_layer_tests/scatter_update.cpp b/src/plugins/intel_cpu/tests/functional/custom/single_layer_tests/scatter_update.cpp
index c18784fcf17..bcf75dd6766 100644
--- a/src/plugins/intel_cpu/tests/functional/custom/single_layer_tests/scatter_update.cpp
+++ b/src/plugins/intel_cpu/tests/functional/custom/single_layer_tests/scatter_update.cpp
@@ -2,9 +2,11 @@
// SPDX-License-Identifier: Apache-2.0
//
+#include "common_test_utils/test_assertions.hpp"
+#include "functional_test_utils/skip_tests_config.hpp"
+#include "openvino/op/scatter_update.hpp"
#include "shared_test_classes/base/ov_subgraph.hpp"
#include "utils/cpu_test_utils.hpp"
-#include "openvino/op/scatter_update.hpp"
using namespace CPUTestUtils;
@@ -131,5 +133,44 @@ INSTANTIATE_TEST_SUITE_P(smoke_CompareWithRefs,
::testing::ValuesIn(inputPrecisions),
::testing::ValuesIn(constantPrecisions)),
ScatterUpdateLayerCPUTest::getTestCaseName);
+
+class ScatterUpdateLayerCPUErrorConditionTest : public ScatterUpdateLayerCPUTest {};
+
+TEST_P(ScatterUpdateLayerCPUErrorConditionTest, ThrowsOnOutOfRangeIndices) {
+ SKIP_IF_CURRENT_TEST_IS_DISABLED();
+ compile_model();
+ // Both CPU and TEMPLATE plugins should throw; a mismatch is a bug.
+ for (const auto& targetStaticShapeVec : targetStaticShapes) {
+ generate_inputs(targetStaticShapeVec);
+ OV_EXPECT_THROW(get_plugin_outputs(), ov::Exception, ::testing::_);
+ OV_EXPECT_THROW(calculate_refs(), ov::Exception, ::testing::_);
+ }
+}
+
+const std::vector<ScatterUpdateLayerParams> scatterUpdateOutOfRangeParams = {
+ ScatterUpdateLayerParams{ScatterUpdateShapes{{{8}, {{8}}}, {{1}, {{1}}}},
+ IndicesDescription{{1}, {100000}},
+ Axis{0}},
+};
+
+INSTANTIATE_TEST_SUITE_P(smoke_ScatterUpdate1DFastPathOutOfRangeIndices,
+ ScatterUpdateLayerCPUErrorConditionTest,
+ ::testing::Combine(::testing::ValuesIn(scatterUpdateOutOfRangeParams),
+ ::testing::Values(ElementType::i32),
+ ::testing::Values(ElementType::i32)),
+ ScatterUpdateLayerCPUTest::getTestCaseName);
+
+const std::vector<ScatterUpdateLayerParams> scatterUpdateGeneralPathOutOfRangeParams = {
+ ScatterUpdateLayerParams{ScatterUpdateShapes{{{8, 4}, {{8, 4}}}, {{2, 4}, {{2, 4}}}},
+ IndicesDescription{{2}, {0, 100000}},
+ Axis{0}},
+};
+
+INSTANTIATE_TEST_SUITE_P(smoke_ScatterUpdateGeneralPathOutOfRangeIndices,
+ ScatterUpdateLayerCPUErrorConditionTest,
+ ::testing::Combine(::testing::ValuesIn(scatterUpdateGeneralPathOutOfRangeParams),
+ ::testing::ValuesIn(inputPrecisions),
+ ::testing::ValuesIn(constantPrecisions)),
+ ScatterUpdateLayerCPUTest::getTestCaseName);
} // namespace test
} // namespace ov