From e7e82b9eb7e35e4345a9f5ecc6493eb9a4e7cf63 Mon Sep 17 00:00:00 2001 From: Nikolay Shchegolev Date: Wed, 16 Sep 2020 12:32:20 +0300 Subject: [PATCH] Statically analyzed issues. (#2261) --- .../legacy_api/src/convert_function_to_cnn_network.cpp | 6 ++++++ inference-engine/src/legacy_api/src/net_pass.cpp | 4 +++- .../src/readers/ir_reader_v7/ie_layer_validators.cpp | 2 +- .../convert_opset1_to_legacy/fc_bias_fusion.cpp | 2 ++ .../src/transformations/reduce_l1_decomposition.cpp | 2 +- .../src/transformations/reduce_l2_decomposition.cpp | 2 +- .../dynamic_to_static_shape_reshape.cpp | 3 +++ .../dynamic_to_static_shape_squeeze.cpp | 3 +++ .../dynamic_to_static_shape_transpose.cpp | 3 +++ .../dynamic_to_static_shape_unsqueeze.cpp | 3 +++ .../src/frontend/ShaveElfMetadataParser.cpp | 10 ++++++---- .../graph_transformer/src/frontend/custom_layer.cpp | 2 ++ 12 files changed, 34 insertions(+), 8 deletions(-) diff --git a/inference-engine/src/legacy_api/src/convert_function_to_cnn_network.cpp b/inference-engine/src/legacy_api/src/convert_function_to_cnn_network.cpp index f0401fe8992089..fd6c344bc462f1 100644 --- a/inference-engine/src/legacy_api/src/convert_function_to_cnn_network.cpp +++ b/inference-engine/src/legacy_api/src/convert_function_to_cnn_network.cpp @@ -669,6 +669,8 @@ InferenceEngine::details::CNNLayerCreator::CNNLayerCreator(const std::shared_ptr [](const std::shared_ptr<::ngraph::Node>& node, const std::map& params) -> CNNLayerPtr { LayerParams attrs = {node->get_friendly_name(), node->description(), details::convertPrecision(node->get_output_element_type(0))}; auto reduce_node = std::dynamic_pointer_cast(node); + if (reduce_node == nullptr) + THROW_IE_EXCEPTION << "Node '" << node->get_name() << "' is not an instance of ArithmeticReductionKeepDims."; auto res = std::make_shared(attrs); res->params = params; res->params["keep_dims"] = reduce_node->get_keep_dims() ? "True" : "False"; @@ -678,6 +680,8 @@ InferenceEngine::details::CNNLayerCreator::CNNLayerCreator(const std::shared_ptr addSpecificCreator({"ReduceLogicalAnd"}, [](const std::shared_ptr<::ngraph::Node>& node, const std::map& params) -> CNNLayerPtr { LayerParams attrs = {node->get_friendly_name(), "ReduceAnd", details::convertPrecision(node->get_output_element_type(0))}; auto reduce_node = std::dynamic_pointer_cast(node); + if (reduce_node == nullptr) + THROW_IE_EXCEPTION << "Node '" << node->get_name() << "' is not an instance of LogicalReductionKeepDims."; auto res = std::make_shared(attrs); res->params = params; res->params["keep_dims"] = reduce_node->get_keep_dims() ? "True" : "False"; @@ -687,6 +691,8 @@ InferenceEngine::details::CNNLayerCreator::CNNLayerCreator(const std::shared_ptr addSpecificCreator({"ReduceLogicalOr"}, [](const std::shared_ptr<::ngraph::Node>& node, const std::map& params) -> CNNLayerPtr { LayerParams attrs = {node->get_friendly_name(), "ReduceOr", details::convertPrecision(node->get_output_element_type(0))}; auto reduce_node = std::dynamic_pointer_cast(node); + if (reduce_node == nullptr) + THROW_IE_EXCEPTION << "Node '" << node->get_name() << "' is not an instance of LogicalReductionKeepDims."; auto res = std::make_shared(attrs); res->params = params; res->params["keep_dims"] = reduce_node->get_keep_dims() ? "True" : "False"; diff --git a/inference-engine/src/legacy_api/src/net_pass.cpp b/inference-engine/src/legacy_api/src/net_pass.cpp index 2efd8585210464..d568c525039c10 100644 --- a/inference-engine/src/legacy_api/src/net_pass.cpp +++ b/inference-engine/src/legacy_api/src/net_pass.cpp @@ -398,8 +398,10 @@ bool convertToRNNSeq(CNNLayerPtr cur, const N& net) { IE_ASSERT(cell->insData.size() == NS + 1); // {data, state1, [state2]} IE_ASSERT(cell->outData.size() == NS); // {state1, [state2]} + auto outData0InputsTo = getInputTo(cell->outData[0]); if (getCreatorLayer(cell->insData[0].lock()).lock() != rsp1 || - getInputTo(cell->outData[0]).begin()->second != rsp2) + outData0InputsTo.empty() || + outData0InputsTo.begin()->second != rsp2) return false; // Check port mapping diff --git a/inference-engine/src/readers/ir_reader_v7/ie_layer_validators.cpp b/inference-engine/src/readers/ir_reader_v7/ie_layer_validators.cpp index cae6bd12ad9ec9..7cd900a669e9ad 100644 --- a/inference-engine/src/readers/ir_reader_v7/ie_layer_validators.cpp +++ b/inference-engine/src/readers/ir_reader_v7/ie_layer_validators.cpp @@ -269,7 +269,7 @@ void CropValidator::checkShapes(const CNNLayer* layer, const vector& } } else if (!casted->dim.empty()) { int dim = casted->dim[i]; - if (firstShape[axis] < static_cast(offset + dim)) { + if (firstShape[axis] < (static_cast(offset) + dim)) { THROW_IE_EXCEPTION << "Incorrect crop data! Offset(" << offset << ") + result size of output(" << dim << ") should be less then input size(" << firstShape[axis] << ") for axis(" << axis << ")"; diff --git a/inference-engine/src/transformations/src/transformations/convert_opset1_to_legacy/fc_bias_fusion.cpp b/inference-engine/src/transformations/src/transformations/convert_opset1_to_legacy/fc_bias_fusion.cpp index 01511dedf5bfec..38e591b97bc2e5 100644 --- a/inference-engine/src/transformations/src/transformations/convert_opset1_to_legacy/fc_bias_fusion.cpp +++ b/inference-engine/src/transformations/src/transformations/convert_opset1_to_legacy/fc_bias_fusion.cpp @@ -26,6 +26,8 @@ ngraph::pass::FullyConnectedBiasFusion::FullyConnectedBiasFusion() { if (m_fc == nullptr) { m_fc = std::dynamic_pointer_cast(add_input_1); + if (m_fc == nullptr) + return false; m_bias = add_input_0; } diff --git a/inference-engine/src/transformations/src/transformations/reduce_l1_decomposition.cpp b/inference-engine/src/transformations/src/transformations/reduce_l1_decomposition.cpp index c543b96ee6c15b..816d7ae2e4912f 100644 --- a/inference-engine/src/transformations/src/transformations/reduce_l1_decomposition.cpp +++ b/inference-engine/src/transformations/src/transformations/reduce_l1_decomposition.cpp @@ -18,7 +18,7 @@ ngraph::pass::ReduceL1Decomposition::ReduceL1Decomposition() { auto &pattern_to_output = m.get_pattern_value_map(); auto reduce_l1_node = std::dynamic_pointer_cast(pattern_to_output.at(reduce_l1).get_node_shared_ptr()); - if (m_transformation_callback(reduce_l1_node)) { + if (reduce_l1_node == nullptr || m_transformation_callback(reduce_l1_node)) { return false; } diff --git a/inference-engine/src/transformations/src/transformations/reduce_l2_decomposition.cpp b/inference-engine/src/transformations/src/transformations/reduce_l2_decomposition.cpp index d3887f549e20c5..2d4abdeab64a9e 100644 --- a/inference-engine/src/transformations/src/transformations/reduce_l2_decomposition.cpp +++ b/inference-engine/src/transformations/src/transformations/reduce_l2_decomposition.cpp @@ -18,7 +18,7 @@ ngraph::pass::ReduceL2Decomposition::ReduceL2Decomposition() { auto &pattern_to_output = m.get_pattern_value_map(); auto reduce_l2_node = std::dynamic_pointer_cast(pattern_to_output.at(reduce_l2).get_node_shared_ptr()); - if (m_transformation_callback(reduce_l2_node)) { + if (reduce_l2_node == nullptr || m_transformation_callback(reduce_l2_node)) { return false; } diff --git a/inference-engine/src/vpu/common/src/ngraph/transformations/dynamic_to_static_shape_reshape.cpp b/inference-engine/src/vpu/common/src/ngraph/transformations/dynamic_to_static_shape_reshape.cpp index 5c0fe8accb31c2..3c0d86dd8dce4d 100644 --- a/inference-engine/src/vpu/common/src/ngraph/transformations/dynamic_to_static_shape_reshape.cpp +++ b/inference-engine/src/vpu/common/src/ngraph/transformations/dynamic_to_static_shape_reshape.cpp @@ -23,6 +23,9 @@ void dynamicToStaticShapeReshape(std::shared_ptr target) { target->get_friendly_name(), target->get_type_info(), ngraph::vpu::op::DynamicShapeResolver::type_info, 0); const auto reshape = std::dynamic_pointer_cast(target); + VPU_THROW_UNLESS(reshape != nullptr, + "DynamicToStaticShape transformation for '{}' expects Reshape node", + target->get_friendly_name()); const auto outShapeDescriptor = reshape->input_value(1).get_node_shared_ptr(); const auto replacement = ngraph::as_type_ptr(outShapeDescriptor) diff --git a/inference-engine/src/vpu/common/src/ngraph/transformations/dynamic_to_static_shape_squeeze.cpp b/inference-engine/src/vpu/common/src/ngraph/transformations/dynamic_to_static_shape_squeeze.cpp index 46f60cc42afe4c..b28820018ece72 100644 --- a/inference-engine/src/vpu/common/src/ngraph/transformations/dynamic_to_static_shape_squeeze.cpp +++ b/inference-engine/src/vpu/common/src/ngraph/transformations/dynamic_to_static_shape_squeeze.cpp @@ -28,6 +28,9 @@ void dynamicToStaticShapeSqueeze(std::shared_ptr target) { target->get_friendly_name(), target->get_type_info(), ngraph::op::Constant::type_info, 1); const auto squeeze = std::dynamic_pointer_cast(target); + VPU_THROW_UNLESS(squeeze != nullptr, + "DynamicToStaticShape transformation for '{}' expects Squeeze node", + target->get_friendly_name()); const auto copied = squeeze->clone_with_new_inputs(target->input_values()); const auto shape = dsr->input(1).get_source_output(); diff --git a/inference-engine/src/vpu/common/src/ngraph/transformations/dynamic_to_static_shape_transpose.cpp b/inference-engine/src/vpu/common/src/ngraph/transformations/dynamic_to_static_shape_transpose.cpp index f1b9266e4d11e5..d758c108b30534 100644 --- a/inference-engine/src/vpu/common/src/ngraph/transformations/dynamic_to_static_shape_transpose.cpp +++ b/inference-engine/src/vpu/common/src/ngraph/transformations/dynamic_to_static_shape_transpose.cpp @@ -26,6 +26,9 @@ void dynamicToStaticShapeTranspose(std::shared_ptr target) { target->get_friendly_name(), target->get_type_info(), ngraph::opset3::Constant::type_info, 1); const auto transpose = std::dynamic_pointer_cast(target); + VPU_THROW_UNLESS(transpose != nullptr, + "DynamicToStaticShape transformation for '{}' expects Transpose node", + target->get_friendly_name()); const auto copied = transpose->clone_with_new_inputs(target->input_values()); const auto shape = dsr->input(1).get_source_output(); diff --git a/inference-engine/src/vpu/common/src/ngraph/transformations/dynamic_to_static_shape_unsqueeze.cpp b/inference-engine/src/vpu/common/src/ngraph/transformations/dynamic_to_static_shape_unsqueeze.cpp index 81c053b2fa4f52..65a3715db4af0c 100644 --- a/inference-engine/src/vpu/common/src/ngraph/transformations/dynamic_to_static_shape_unsqueeze.cpp +++ b/inference-engine/src/vpu/common/src/ngraph/transformations/dynamic_to_static_shape_unsqueeze.cpp @@ -29,6 +29,9 @@ void dynamicToStaticShapeUnsqueeze(std::shared_ptr target) { target->get_friendly_name(), target->get_type_info(), ngraph::op::Constant::type_info, 1); const auto unsqueeze = std::dynamic_pointer_cast(target); + VPU_THROW_UNLESS(unsqueeze != nullptr, + "DynamicToStaticShape transformation for '{}' expects Unsqueeze node", + target->get_friendly_name()); const auto copied = unsqueeze->clone_with_new_inputs(target->input_values()); const auto shape = dsr->input(1).get_source_output(); diff --git a/inference-engine/src/vpu/graph_transformer/src/frontend/ShaveElfMetadataParser.cpp b/inference-engine/src/vpu/graph_transformer/src/frontend/ShaveElfMetadataParser.cpp index d8c14661f06d66..53a1be77a955a5 100644 --- a/inference-engine/src/vpu/graph_transformer/src/frontend/ShaveElfMetadataParser.cpp +++ b/inference-engine/src/vpu/graph_transformer/src/frontend/ShaveElfMetadataParser.cpp @@ -65,10 +65,12 @@ uint32_t md_parser_t::evaluate_expr(const md_expr_t *expression, case md_type_op_xor: case md_type_op_shl: case md_type_op_lshr: - uint32_t rhs = values.rbegin()[0]; - uint32_t lhs = values.rbegin()[1]; - values.pop_back(); - values.back() = md_eval_expression_type_op_2(v.type, lhs, rhs); + if (!values.empty()) { + uint32_t rhs = values.rbegin()[0]; + uint32_t lhs = values.rbegin()[1]; + values.pop_back(); + values.back() = md_eval_expression_type_op_2(v.type, lhs, rhs); + } } break; case md_type_global_size: diff --git a/inference-engine/src/vpu/graph_transformer/src/frontend/custom_layer.cpp b/inference-engine/src/vpu/graph_transformer/src/frontend/custom_layer.cpp index 4a9a209a0e62e7..ceb7e33f8a0bd3 100644 --- a/inference-engine/src/vpu/graph_transformer/src/frontend/custom_layer.cpp +++ b/inference-engine/src/vpu/graph_transformer/src/frontend/custom_layer.cpp @@ -184,6 +184,8 @@ CustomLayer::CustomLayer(std::string configDir, const pugi::xml_node& customLaye stageOrder.emplace(stageNum, CustomKernel{kernel, _configDir}); } + VPU_THROW_UNLESS(!stageOrder.empty(), + "Error while binding %s custom layer: No stages.", _layerName); VPU_THROW_UNLESS(stageOrder.begin()->first == 0, "Error while binding %s custom layer: Stage 0 is not found.", _layerName); VPU_THROW_UNLESS(stageOrder.rbegin()->first == stageOrder.size() - 1,