Skip to content

Commit 4d3f15d

Browse files
authored
[ONNX] Fix editor tests to check for nullptr before dereferencing (#32668)
### Details: - *Fix segmentation faults when running editor tests with onnx delegate* ### Tickets: - *ticket-id* --------- Signed-off-by: Maxim Vafin <[email protected]>
1 parent f6faa90 commit 4d3f15d

File tree

1 file changed

+95
-18
lines changed

1 file changed

+95
-18
lines changed

src/frontends/onnx/tests/onnx_editor.cpp

Lines changed: 95 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -454,9 +454,12 @@ OPENVINO_TEST(onnx_editor, subgraph__twice_input_edge_from_tensor_with_single_co
454454
FrontEnd::Ptr front_end;
455455
auto input_model = load_model("model_editor/add_ab.onnx", &front_end);
456456

457-
input_model->extract_subgraph(
458-
{input_model->get_place_by_tensor_name("X")->get_consuming_operations()[0]->get_input_port(1)},
459-
{});
457+
auto x_place = input_model->get_place_by_tensor_name("X");
458+
ASSERT_TRUE(x_place != nullptr) << "get_place_by_tensor_name(\"X\") returned nullptr";
459+
auto x_consumers = x_place->get_consuming_operations();
460+
ASSERT_FALSE(x_consumers.empty()) << "get_consuming_operations() returned empty container for \"X\"";
461+
462+
input_model->extract_subgraph({x_consumers[0]->get_input_port(1)}, {});
460463

461464
auto model = front_end->convert(input_model);
462465
auto model_ref =
@@ -474,8 +477,13 @@ OPENVINO_TEST(onnx_editor, subgraph__input_edge_from_tensor_with_multiple_consum
474477
auto input_model = load_model("model_editor/subgraph_extraction_tests.onnx", &front_end);
475478

476479
auto relu_node = input_model->get_place_by_operation_name("relu1_name");
480+
ASSERT_TRUE(relu_node != nullptr) << "get_place_by_operation_name(\"relu1_name\") returned nullptr";
477481
auto relu_consumers = relu_node->get_consuming_operations();
478482

483+
ASSERT_GE(relu_consumers.size(), 3) << "relu_consumers vector does not have enough elements";
484+
ASSERT_TRUE(relu_consumers[0] != nullptr) << "relu_consumers[0] is nullptr";
485+
ASSERT_TRUE(relu_consumers[2] != nullptr) << "relu_consumers[2] is nullptr";
486+
479487
input_model->extract_subgraph(
480488
{relu_consumers[0]->get_input_port(0), relu_consumers[2]->get_input_port(0)},
481489
{input_model->get_place_by_tensor_name("mul1"), input_model->get_place_by_tensor_name("mul2")});
@@ -496,7 +504,10 @@ OPENVINO_TEST(onnx_editor, subgraph__input_edge_from_tensor_with_multiple_consum
496504
auto input_model = load_model("model_editor/subgraph_extraction_tests.onnx", &front_end);
497505

498506
auto relu_node = input_model->get_place_by_operation_name("relu1_name");
507+
ASSERT_TRUE(relu_node != nullptr) << "get_place_by_operation_name(\"relu1_name\") returned nullptr";
499508
auto relu_consumers = relu_node->get_consuming_operations();
509+
ASSERT_GE(relu_consumers.size(), 2) << "relu_consumers vector does not have enough elements";
510+
ASSERT_TRUE(relu_consumers[1] != nullptr) << "relu_consumers[1] is nullptr";
500511

501512
input_model->extract_subgraph(
502513
{relu_consumers[1]->get_input_port(0), relu_consumers[1]->get_input_port(1)},
@@ -518,7 +529,11 @@ OPENVINO_TEST(onnx_editor, subgraph__input_edge_from_tensor_with_multiple_consum
518529
auto input_model = load_model("model_editor/subgraph_extraction_tests.onnx", &front_end);
519530

520531
auto relu_node = input_model->get_place_by_operation_name("relu1_name");
532+
ASSERT_TRUE(relu_node != nullptr) << "get_place_by_operation_name(\"relu1_name\") returned nullptr";
521533
auto relu_consumers = relu_node->get_consuming_operations();
534+
ASSERT_GE(relu_consumers.size(), 3) << "relu_consumers vector does not have enough elements";
535+
ASSERT_TRUE(relu_consumers[1] != nullptr) << "relu_consumers[1] is nullptr";
536+
ASSERT_TRUE(relu_consumers[2] != nullptr) << "relu_consumers[2] is nullptr";
522537

523538
input_model->extract_subgraph(
524539
{relu_consumers[1]->get_input_port(0), relu_consumers[2]->get_input_port(0)},
@@ -540,7 +555,11 @@ OPENVINO_TEST(onnx_editor, subgraph__input_edge_from_tensor_with_multiple_consum
540555
auto input_model = load_model("model_editor/subgraph_extraction_tests.onnx", &front_end);
541556

542557
auto relu_node = input_model->get_place_by_operation_name("relu1_name");
558+
ASSERT_TRUE(relu_node != nullptr) << "get_place_by_operation_name(\"relu1_name\") returned nullptr";
543559
auto relu_consumers = relu_node->get_consuming_operations();
560+
ASSERT_GE(relu_consumers.size(), 2) << "relu_consumers vector does not have enough elements";
561+
ASSERT_TRUE(relu_consumers[0] != nullptr) << "relu_consumers[0] is nullptr";
562+
ASSERT_TRUE(relu_consumers[1] != nullptr) << "relu_consumers[1] is nullptr";
544563

545564
input_model->extract_subgraph({relu_consumers[0]->get_input_port(0), relu_consumers[1]->get_input_port(0)}, {});
546565
auto model = front_end->convert(input_model);
@@ -560,7 +579,10 @@ OPENVINO_TEST(onnx_editor, subgraph__input_edge_from_tensor_with_multiple_consum
560579
auto input_model = load_model("model_editor/subgraph_extraction_tests.onnx", &front_end);
561580

562581
auto relu_node = input_model->get_place_by_operation_name("relu1_name");
582+
ASSERT_TRUE(relu_node != nullptr) << "get_place_by_operation_name(\"relu1_name\") returned nullptr";
563583
auto relu_consumers = relu_node->get_consuming_operations();
584+
ASSERT_GE(relu_consumers.size(), 2) << "relu_consumers vector does not have enough elements";
585+
ASSERT_TRUE(relu_consumers[1] != nullptr) << "relu_consumers[1] is nullptr";
564586

565587
input_model->extract_subgraph(
566588
{relu_consumers[1]->get_input_port(0)},
@@ -582,8 +604,15 @@ OPENVINO_TEST(onnx_editor, subgraph__input_edge_from_tensor_with_multiple_consum
582604
auto input_model = load_model("model_editor/subgraph_extraction_tests.onnx", &front_end);
583605

584606
auto relu_node = input_model->get_place_by_operation_name("relu1_name");
607+
ASSERT_TRUE(relu_node != nullptr) << "get_place_by_operation_name(\"relu1_name\") returned nullptr";
585608
auto relu_consumers = relu_node->get_consuming_operations();
586609

610+
ASSERT_GE(relu_consumers.size(), 3) << "relu_consumers vector does not have enough elements";
611+
ASSERT_TRUE(relu_consumers[0] != nullptr) << "relu_consumers[0] is nullptr";
612+
ASSERT_TRUE(relu_consumers[2] != nullptr) << "relu_consumers[2] is nullptr";
613+
ASSERT_TRUE(relu_consumers[0]->get_input_port(0) != nullptr) << "relu_consumers[0]->get_input_port(0) is nullptr";
614+
ASSERT_TRUE(relu_consumers[2]->get_input_port(0) != nullptr) << "relu_consumers[2]->get_input_port(0) is nullptr";
615+
587616
input_model->cut_and_add_new_input(relu_consumers[0]->get_input_port(0), "new_name_1");
588617
input_model->cut_and_add_new_input(relu_consumers[2]->get_input_port(0), "new_name_2");
589618

@@ -660,10 +689,16 @@ OPENVINO_TEST(onnx_editor, subgraph__multiple_consumers_of_graph_initializer_rel
660689
FrontEnd::Ptr front_end;
661690
auto input_model = load_model("model_editor/subgraph_extraction_tests_2.onnx", &front_end);
662691

663-
input_model->extract_subgraph(
664-
{input_model->get_place_by_tensor_name("in2"),
665-
input_model->get_place_by_tensor_name("relu3")->get_consuming_operations()[0]->get_input_port(0)},
666-
{});
692+
auto in2_place = input_model->get_place_by_tensor_name("in2");
693+
ASSERT_TRUE(in2_place != nullptr) << "get_place_by_tensor_name(\"in2\") returned nullptr";
694+
auto relu3_place = input_model->get_place_by_tensor_name("relu3");
695+
ASSERT_TRUE(relu3_place != nullptr) << "get_place_by_tensor_name(\"relu3\") returned nullptr";
696+
auto relu3_consumers = relu3_place->get_consuming_operations();
697+
ASSERT_FALSE(relu3_consumers.empty()) << "get_consuming_operations() returned empty container for \"relu3\"";
698+
auto relu3_input_port = relu3_consumers[0]->get_input_port(0);
699+
ASSERT_TRUE(relu3_input_port != nullptr) << "get_input_port(0) returned nullptr for relu3's first consumer";
700+
701+
input_model->extract_subgraph({in2_place, relu3_input_port}, {});
667702

668703
auto model = front_end->convert(input_model);
669704

@@ -870,15 +905,25 @@ OPENVINO_TEST(onnx_editor, cut_operator_with_no_schema) {
870905
OPENVINO_TEST(onnx_editor, is_model_input) {
871906
auto input_model = load_model("model_editor/subgraph_extraction_tests.onnx");
872907

873-
EXPECT_TRUE(input_model->get_place_by_tensor_name("in2")->is_input());
874-
EXPECT_FALSE(input_model->get_place_by_tensor_name("conv1")->is_input());
908+
auto in2_place = input_model->get_place_by_tensor_name("in2");
909+
ASSERT_TRUE(in2_place != nullptr) << "get_place_by_tensor_name(\"in2\") returned nullptr";
910+
EXPECT_TRUE(in2_place->is_input());
911+
912+
auto conv1_place = input_model->get_place_by_tensor_name("conv1");
913+
ASSERT_TRUE(conv1_place != nullptr) << "get_place_by_tensor_name(\"conv1\") returned nullptr";
914+
EXPECT_FALSE(conv1_place->is_input());
875915
}
876916

877917
OPENVINO_TEST(onnx_editor, is_model_output) {
878918
auto input_model = load_model("model_editor/subgraph_extraction_tests.onnx");
879919

880-
EXPECT_TRUE(input_model->get_place_by_tensor_name("split2")->is_output());
881-
EXPECT_FALSE(input_model->get_place_by_tensor_name("add2")->is_output());
920+
auto split2_place = input_model->get_place_by_tensor_name("split2");
921+
ASSERT_TRUE(split2_place != nullptr) << "get_place_by_tensor_name(\"split2\") returned nullptr";
922+
EXPECT_TRUE(split2_place->is_output());
923+
924+
auto add2_place = input_model->get_place_by_tensor_name("add2");
925+
ASSERT_TRUE(add2_place != nullptr) << "get_place_by_tensor_name(\"add2\") returned nullptr";
926+
EXPECT_FALSE(add2_place->is_output());
882927
}
883928

884929
OPENVINO_TEST(onnx_editor, model_inputs) {
@@ -965,12 +1010,17 @@ OPENVINO_TEST(onnx_editor, is_correct_tensor_name) {
9651010
OPENVINO_TEST(onnx_editor, get_input_ports) {
9661011
auto input_model = load_model("model_editor/subgraph_extraction_tests.onnx");
9671012
const auto ports_1 = input_model->get_place_by_operation_name("relu1_name");
1013+
ASSERT_TRUE(ports_1 != nullptr) << "get_place_by_operation_name(\"relu1_name\") returned nullptr";
9681014
EXPECT_EQ(ports_1->get_input_port()->get_source_tensor()->get_names()[0], "in1");
9691015
EXPECT_FALSE(ports_1->get_input_port(1));
9701016
const auto ports_2 = input_model->get_place_by_operation_name("split_name");
1017+
ASSERT_TRUE(ports_2 != nullptr) << "get_place_by_operation_name(\"split_name\") returned nullptr";
9711018
EXPECT_EQ(ports_2->get_input_port(0)->get_source_tensor()->get_names()[0], "add2");
9721019
EXPECT_FALSE(ports_2->get_input_port(1));
973-
const auto ports_3 = input_model->get_place_by_tensor_name("add2")->get_producing_operation();
1020+
const auto add2_tensor = input_model->get_place_by_tensor_name("add2");
1021+
ASSERT_TRUE(add2_tensor != nullptr) << "get_place_by_tensor_name(\"add2\") returned nullptr";
1022+
const auto ports_3 = add2_tensor->get_producing_operation();
1023+
ASSERT_TRUE(ports_3 != nullptr) << "get_producing_operation() for \"add2\" returned nullptr";
9741024
EXPECT_EQ(ports_3->get_input_port(0)->get_source_tensor()->get_names()[0], "relu1");
9751025
EXPECT_EQ(ports_3->get_input_port(1)->get_source_tensor()->get_names()[0], "add1");
9761026
EXPECT_FALSE(ports_3->get_input_port(2));
@@ -979,21 +1029,48 @@ OPENVINO_TEST(onnx_editor, get_input_ports) {
9791029
OPENVINO_TEST(onnx_editor, get_output_ports) {
9801030
auto input_model = load_model("model_editor/subgraph_extraction_tests.onnx");
9811031
const auto ports_1 = input_model->get_place_by_operation_name("relu1_name");
982-
EXPECT_EQ(ports_1->get_output_port(0)->get_target_tensor()->get_names()[0], "relu1");
1032+
ASSERT_TRUE(ports_1 != nullptr) << "get_place_by_operation_name(\"relu1_name\") returned nullptr";
1033+
const auto relu1_out_port_0 = ports_1->get_output_port(0);
1034+
ASSERT_TRUE(relu1_out_port_0 != nullptr) << "get_output_port(0) for relu1_name returned nullptr";
1035+
const auto relu1_target_tensor = relu1_out_port_0->get_target_tensor();
1036+
ASSERT_TRUE(relu1_target_tensor != nullptr) << "get_target_tensor() for relu1_name output port 0 returned nullptr";
1037+
EXPECT_EQ(relu1_target_tensor->get_names()[0], "relu1");
9831038
EXPECT_FALSE(ports_1->get_output_port(1));
1039+
9841040
const auto ports_2 = input_model->get_place_by_operation_name("split_name");
985-
EXPECT_EQ(ports_2->get_output_port(0)->get_target_tensor()->get_names()[0], "split1");
986-
EXPECT_EQ(ports_2->get_output_port(1)->get_target_tensor()->get_names()[0], "split2");
1041+
EXPECT_TRUE(ports_2 != nullptr) << "get_place_by_operation_name(\"split_name\") returned nullptr";
1042+
const auto split_out_port_0 = ports_2->get_output_port(0);
1043+
ASSERT_TRUE(split_out_port_0 != nullptr) << "get_output_port(0) for split_name returned nullptr";
1044+
const auto split_target_tensor_0 = split_out_port_0->get_target_tensor();
1045+
ASSERT_TRUE(split_target_tensor_0 != nullptr)
1046+
<< "get_target_tensor() for split_name output port 0 returned nullptr";
1047+
EXPECT_EQ(split_target_tensor_0->get_names()[0], "split1");
1048+
const auto split_out_port_1 = ports_2->get_output_port(1);
1049+
ASSERT_TRUE(split_out_port_1 != nullptr) << "get_output_port(1) for split_name returned nullptr";
1050+
const auto split_target_tensor_1 = split_out_port_1->get_target_tensor();
1051+
ASSERT_TRUE(split_target_tensor_1 != nullptr)
1052+
<< "get_target_tensor() for split_name output port 1 returned nullptr";
1053+
EXPECT_EQ(split_target_tensor_1->get_names()[0], "split2");
9871054
EXPECT_FALSE(ports_2->get_output_port(2));
988-
const auto ports_3 = input_model->get_place_by_tensor_name("add2")->get_producing_operation();
989-
EXPECT_EQ(ports_3->get_output_port()->get_target_tensor()->get_names()[0], "add2");
1055+
1056+
const auto add2_tensor = input_model->get_place_by_tensor_name("add2");
1057+
EXPECT_TRUE(add2_tensor != nullptr) << "get_place_by_tensor_name(\"add2\") returned nullptr";
1058+
const auto ports_3 = add2_tensor->get_producing_operation();
1059+
EXPECT_TRUE(ports_3 != nullptr) << "get_producing_operation() for \"add2\" returned nullptr";
1060+
const auto add2_out_port_0 = ports_3->get_output_port();
1061+
ASSERT_TRUE(add2_out_port_0 != nullptr) << "get_output_port() for add2 producing operation returned nullptr";
1062+
const auto add2_target_tensor = add2_out_port_0->get_target_tensor();
1063+
ASSERT_TRUE(add2_target_tensor != nullptr) << "get_target_tensor() for add2 output port returned nullptr";
1064+
EXPECT_EQ(add2_target_tensor->get_names()[0], "add2");
9901065
EXPECT_FALSE(ports_3->get_output_port(1));
9911066
}
9921067

9931068
OPENVINO_TEST(onnx_editor, add_output) {
9941069
auto input_model = load_model("model_editor/add_abc.onnx");
9951070

996-
input_model->add_output(input_model->get_place_by_operation_name("add_node1")->get_target_tensor());
1071+
auto add_node1_place = input_model->get_place_by_operation_name("add_node1");
1072+
ASSERT_TRUE(add_node1_place != nullptr) << "get_place_by_operation_name(\"add_node1\") returned nullptr";
1073+
input_model->add_output(add_node1_place->get_target_tensor());
9971074

9981075
EXPECT_EQ(input_model->get_outputs().size(), 2);
9991076

0 commit comments

Comments
 (0)