Skip to content

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

[GPU] Fix out-of-bounds read in `Gather` (#37369)

Aleksandr Voron1803429b473c
diff --git a/src/plugins/intel_gpu/src/plugin/ops/gather.cpp b/src/plugins/intel_gpu/src/plugin/ops/gather.cpp
index 9c0f88ee2e4..2b990819a1e 100644
--- a/src/plugins/intel_gpu/src/plugin/ops/gather.cpp
+++ b/src/plugins/intel_gpu/src/plugin/ops/gather.cpp
@@ -104,7 +104,8 @@ void CreateGatherOpBase(ProgramBuilder& p, const std::shared_ptr<T>& op, const i
bool is_indices_constant = static_cast<bool>(indices_constant);
if (is_static && axis == 0 && input_rank > 1 && indices.get_partial_shape().rank().get_length() == 0 &&
- std::equal(input_shape.begin()+1, input_shape.end(), out_shape.begin()+1) && is_indices_constant) {
+ out_shape_original.size() + 1 == input_shape.size() &&
+ std::equal(input_shape.begin()+1, input_shape.end(), out_shape_original.begin()) && is_indices_constant) {
// Gather -> Crop
// this Gather simply divides an input tensor along Batch axis
auto get_crop_layer_name = [&](std::string name, size_t idx)->std::string {
diff --git a/src/plugins/intel_gpu/tests/functional/single_layer_tests/dynamic/gather.cpp b/src/plugins/intel_gpu/tests/functional/single_layer_tests/dynamic/gather.cpp
index 03a84374fe8..df1f812b905 100644
--- a/src/plugins/intel_gpu/tests/functional/single_layer_tests/dynamic/gather.cpp
+++ b/src/plugins/intel_gpu/tests/functional/single_layer_tests/dynamic/gather.cpp
@@ -114,6 +114,31 @@ protected:
ov::ResultVector results{std::make_shared<ov::op::v0::Result>(gatherNode)};
function = std::make_shared<ov::Model>(results, params, "Gather");
}
+
+ void generate_inputs(const std::vector<ov::Shape>& targetInputStaticShapes) override {
+ // constrain "indices" to a valid range while leaving every other input to the default generator
+ const auto axis = std::get<0>(this->GetParam()).axis;
+ const auto& funcInputs = function->inputs();
+ inputs.clear();
+ for (size_t i = 0; i < funcInputs.size(); ++i) {
+ const auto& funcInput = funcInputs[i];
+ ov::Tensor tensor;
+ if (funcInput.get_node()->get_friendly_name() == "indices") {
+ const auto dataRank = static_cast<int>(targetInputStaticShapes[0].size());
+ const auto axisNorm = axis < 0 ? axis + dataRank : axis;
+ ov::test::utils::InputGenerateData in_data;
+ in_data.start_from = 0;
+ in_data.range = static_cast<uint32_t>(targetInputStaticShapes[0][axisNorm]);
+ tensor = ov::test::utils::create_and_fill_tensor(funcInput.get_element_type(),
+ targetInputStaticShapes[i],
+ in_data);
+ } else {
+ tensor = ov::test::utils::create_and_fill_tensor(funcInput.get_element_type(),
+ targetInputStaticShapes[i]);
+ }
+ inputs.insert({funcInput.get_node_shared_ptr(), tensor});
+ }
+ }
};
TEST_P(GatherGPUTest, Inference) {
@@ -187,4 +212,20 @@ INSTANTIATE_TEST_SUITE_P(smoke_dynamic_input_shapes_const_target_shapes, GatherG
::testing::Values(true), // is const indices
::testing::Values(true)), // is const axis
GatherGPUTest::getTestCaseName);
+
+const std::vector<GatherShapeParams> dynamicAxisStaticOutputScalarIndices = {
+ {
+ ov::test::InputShape(ov::PartialShape({-1, 300}), {{271, 300}}),
+ ov::test::InputShape(ov::PartialShape({}), {{}}),
+ 0, 0
+ }
+};
+
+INSTANTIATE_TEST_SUITE_P(smoke_dynamic_axis0_scalar_indices_static_output, GatherGPUTest,
+ ::testing::Combine(
+ ::testing::ValuesIn(dynamicAxisStaticOutputScalarIndices), // input shapes
+ ::testing::Values(ov::element::f32), // network precision
+ ::testing::Values(false), // is const indices
+ ::testing::Values(true)), // is const axis
+ GatherGPUTest::getTestCaseName);
} // namespace