From d97b4ea8d3f871fa6478cdef1e91a6744a001eba Mon Sep 17 00:00:00 2001 From: chunseoklee Date: Wed, 3 Jul 2024 12:59:31 +0900 Subject: [PATCH] Fix several svace defects (#13320) * [onert-micro] fix NO_EFFECT(142733,142734) - fix NO_EFFECT (wid 142733, 142734) ONE-DCO-1.0-Signed-off-by: Chunseok Lee * [onert-micro] Fix UNINIT.CTOR (wid 142747) - Fix UNINIT.CTOR (wid 142747) ONE-DCO-1.0-Signed-off-by: Chunseok Lee * [onert-micro] fix INTEGER_OVERFLOW (cov 1780901) - fix INTEGER_OVERFLOW (cov 1780901) ONE-DCO-1.0-Signed-off-by: Chunseok Lee * [onert-micro] fix UNREACHABLE_CODE(wid 143590) - w/o this, axis_data is always positive(uint_8) ONE-DCO-1.0-Signed-off-by: Chunseok Lee * [onert-micro] fix UNREACHABLE_CODE.RET(wid:142758) - fix UNREACHABLE_CODE.RET ( wid:142758 ) ONE-DCO-1.0-Signed-off-by: Chunseok Lee * [onert-micro] fix UNREACHABLE_CODE.RET(142752) - Fix UNREACHABLE_CODE.RET for wid 142752 ONE-DCO-1.0-Signed-off-by: Chunseok Lee * [onert-micro] fix UNREACHABLE_CODE.RET(142753) - Fix UNREACHABLE_CODE.RET in wid 142753 ONE-DCO-1.0-Signed-off-by: Chunseok Lee * [onert-micro] fix UNREACHABLE_CODE.RET(142755) - fix UNREACHABLE_CODE.RET(142755) ONE-DCO-1.0-Signed-off-by: Chunseok Lee * [onert-micro] fix NO_RETURN_VALUE(142738, 142736) - fix NO_RETURN_VALUE in wid 142738, 142736 ONE-DCO-1.0-Signed-off-by: Chunseok Lee * [onert-micro] fix UNREACHABLE_CODE.RET(142756) - fix UNREACHABLE_CODE.RET in wid 142756 ONE-DCO-1.0-Signed-off-by: Chunseok Lee * [onert-micro] fix FALL_THROUGH(143586) - Add missing breaks ONE-DCO-1.0-Signed-off-by: Chunseok Lee * [onert-micro] fix COMPARE_RESULT_OF_NEW(142728) - Rmove meaningless null check ONE-DCO-1.0-Signed-off-by: Chunseok Lee * [onert-micro] fix UNREACHABLE_CODE.RET(142754) - fix UNREACHABLE_CODE.RET(142754) ONE-DCO-1.0-Signed-off-by: Chunseok Lee * [onert-micro] fix 3 SIGN_EXTENSION defects - fix wid 143587, 143588, 143589 ONE-DCO-1.0-Signed-off-by: Chunseok Lee * [onert-micro] fix 6 SIGNED_TO_BIGGER_UNSIGNED - SIGNED_TO_BIGGER_UNSIGNED (143468, 69, 70, 71, 73, 74) ONE-DCO-1.0-Signed-off-by: Chunseok Lee * [onert-micro] fix UNREACHABLE_CODE.RET(142757) - Fix wid 142757 - it also fixes cov 1786029 ONE-DCO-1.0-Signed-off-by: Chunseok Lee * [onert-micro] fix DIVISION_BY_ZERO(142730) - fix DIVISION_BY_ZERO for wid 142730 ONE-DCO-1.0-Signed-off-by: Chunseok Lee --------- Co-authored-by: chunseoklee --- .../core/reader/OMWeightOnlyFormatReader.h | 2 +- .../include/execute/OMKernelExecutionBuilder.h | 6 +----- .../include/import/OMKernelConfigureBuilder.h | 6 +----- .../include/pal/common/PALL2Pool2DCommon.h | 6 ++++++ .../onert-micro/include/pal/common/PALSplit.h | 4 ++-- .../onert-micro/include/pal/common/PALUtils.h | 13 +++++++++++++ onert-micro/onert-micro/include/pal/mcu/PALAddN.h | 2 ++ .../onert-micro/src/core/OMRuntimeModule.cpp | 6 ++---- .../src/core/memory/OMMemoryManager.cpp | 3 --- .../src/core/memory/OMRuntimeAllocator.cpp | 15 ++++++++------- .../src/core/train/OMTrainingHandler.cpp | 4 ---- .../onert-micro/src/execute/OMRuntimeKernel.cpp | 3 --- .../onert-micro/src/execute/kernels/Split.cpp | 2 +- .../src/optimize/pass/FindInplaceOpPass.cpp | 8 +++++--- 14 files changed, 42 insertions(+), 38 deletions(-) diff --git a/onert-micro/onert-micro/include/core/reader/OMWeightOnlyFormatReader.h b/onert-micro/onert-micro/include/core/reader/OMWeightOnlyFormatReader.h index 761cca6667a..eee99defdbb 100644 --- a/onert-micro/onert-micro/include/core/reader/OMWeightOnlyFormatReader.h +++ b/onert-micro/onert-micro/include/core/reader/OMWeightOnlyFormatReader.h @@ -66,7 +66,7 @@ class OMWeightOnlyFormatReader char *buffer(const uint32_t tensor_index); private: - char *_wof_ptr; + char *_wof_ptr = nullptr; }; } // namespace reader diff --git a/onert-micro/onert-micro/include/execute/OMKernelExecutionBuilder.h b/onert-micro/onert-micro/include/execute/OMKernelExecutionBuilder.h index 2cf7cea5183..1281b658c80 100644 --- a/onert-micro/onert-micro/include/execute/OMKernelExecutionBuilder.h +++ b/onert-micro/onert-micro/include/execute/OMKernelExecutionBuilder.h @@ -103,11 +103,7 @@ class KernelCustomExecuteRegistry } const auto builder_id_offset = size_t(core::OMBuilderID::BuiltinOperatorsSize); builder_id_opcode -= builder_id_offset - 1; - if (builder_id_opcode < 0) - { - *execute_func = nullptr; - return UnknownError; - } + *execute_func = _operator_execute[builder_id_opcode]; return Ok; } diff --git a/onert-micro/onert-micro/include/import/OMKernelConfigureBuilder.h b/onert-micro/onert-micro/include/import/OMKernelConfigureBuilder.h index 718e98542f8..2348911bef0 100644 --- a/onert-micro/onert-micro/include/import/OMKernelConfigureBuilder.h +++ b/onert-micro/onert-micro/include/import/OMKernelConfigureBuilder.h @@ -107,11 +107,7 @@ class KernelCustomConfigureRegistry } const auto builder_id_offset = size_t(core::OMBuilderID::BuiltinOperatorsSize); builder_id_opcode -= builder_id_offset - 1; - if (builder_id_opcode < 0) - { - *configure_func = nullptr; - return UnknownError; - } + *configure_func = _operator_configure[builder_id_opcode]; return Ok; } diff --git a/onert-micro/onert-micro/include/pal/common/PALL2Pool2DCommon.h b/onert-micro/onert-micro/include/pal/common/PALL2Pool2DCommon.h index d8d3ce3caf4..917522c4883 100644 --- a/onert-micro/onert-micro/include/pal/common/PALL2Pool2DCommon.h +++ b/onert-micro/onert-micro/include/pal/common/PALL2Pool2DCommon.h @@ -23,6 +23,7 @@ #include "core/OMKernelData.h" #include "core/OMRuntimeShape.h" #include +#include namespace onert_micro { namespace execute @@ -73,6 +74,11 @@ OMStatus L2Pool(const core::Pool2DParams ¶ms, const core::OMRuntimeShape &in } } assert(filter_count != 0); + if (filter_count == 0) + { + std::cerr << "filter_count is zero" << std::endl; + return FailedCheckCondition; + } const float l2pool_result = std::sqrt(sum_squares / filter_count); output_data[offset(output_shape.dimsData(), batch, out_y, out_x, channel)] = activationFunctionWithMinMax(l2pool_result, params.activation_min, diff --git a/onert-micro/onert-micro/include/pal/common/PALSplit.h b/onert-micro/onert-micro/include/pal/common/PALSplit.h index d9978b946a9..3265f9d3f20 100644 --- a/onert-micro/onert-micro/include/pal/common/PALSplit.h +++ b/onert-micro/onert-micro/include/pal/common/PALSplit.h @@ -56,7 +56,7 @@ OMStatus Split(const core::SplitParams ¶ms, const core::OMRuntimeShape &inpu } assert(input_data != nullptr); - for (uint32_t k = 0; k < outer_size; ++k) + for (int64_t k = 0; k < outer_size; ++k) { for (uint32_t i = 0; i < output_count; ++i) { @@ -65,7 +65,7 @@ OMStatus Split(const core::SplitParams ¶ms, const core::OMRuntimeShape &inpu const auto copy_size = output_shape.dims(axis_value) * base_inner_size; T *output_ptr = output_data + k * copy_size; assert(output_ptr != nullptr); - for (uint32_t j = 0; j < copy_size; ++j) + for (int64_t j = 0; j < copy_size; ++j) output_ptr[j] = input_data[j]; input_data += copy_size; } diff --git a/onert-micro/onert-micro/include/pal/common/PALUtils.h b/onert-micro/onert-micro/include/pal/common/PALUtils.h index 50866949cb7..d2f02203475 100644 --- a/onert-micro/onert-micro/include/pal/common/PALUtils.h +++ b/onert-micro/onert-micro/include/pal/common/PALUtils.h @@ -230,6 +230,11 @@ bool ReduceDimensionsForBroadcast(const core::OMRuntimeShape &input1_shape, bool broadcast_input1 = false; bool broadcast_input2 = false; bool first_nonunit = true; + + if (input1_shape.dimensionsCount() < 0 || input2_shape.dimensionsCount() < 0) + { + return false; + } const size_t num_input1_dims = input1_shape.dimensionsCount(); const size_t num_input2_dims = input2_shape.dimensionsCount(); const int32_t *input1_dims = input1_shape.dimsData(); @@ -237,6 +242,10 @@ bool ReduceDimensionsForBroadcast(const core::OMRuntimeShape &input1_shape, const size_t num_common_dims = std::min(num_input1_dims, num_input2_dims); for (size_t i = 1; i <= num_common_dims; i++) { + if (input1_dims[num_input1_dims - i] < 0 || input2_dims[num_input2_dims - i] < 0) + { + return false; + } const size_t input1_dim = input1_dims[num_input1_dims - i]; const size_t input2_dim = input2_dims[num_input2_dims - i]; if (input1_dim == 0 || input2_dim == 0) @@ -294,6 +303,8 @@ bool ReduceDimensionsForBroadcast(const core::OMRuntimeShape &input1_shape, } for (size_t i = 0; i < num_input1_dims - num_input2_dims; i++) { + if (input1_dims[i] < 0) + return false; const size_t input1_dim = input1_dims[i]; if (input1_dim == 0) { @@ -311,6 +322,8 @@ bool ReduceDimensionsForBroadcast(const core::OMRuntimeShape &input1_shape, } for (size_t i = 0; i < num_input2_dims - num_input1_dims; i++) { + if (input2_dims[i] < 0) + return false; const size_t input2_dim = input2_dims[i]; if (input2_dim == 0) { diff --git a/onert-micro/onert-micro/include/pal/mcu/PALAddN.h b/onert-micro/onert-micro/include/pal/mcu/PALAddN.h index 4076a489e42..35dd84eac64 100644 --- a/onert-micro/onert-micro/include/pal/mcu/PALAddN.h +++ b/onert-micro/onert-micro/include/pal/mcu/PALAddN.h @@ -31,6 +31,7 @@ OMStatus AddN(const size_t flat_size, const size_t num_inputs, const int8_t *const *input_data, int8_t *output_data) { assert(false && "Not IMPL yet"); + return UnsupportedOp; } template <> @@ -38,6 +39,7 @@ OMStatus AddN(const size_t flat_size, const size_t num_inputs, const int16_t *const *input_data, int16_t *output_data) { assert(false && "Not IMPL yet"); + return UnsupportedOp; } } // namespace pal diff --git a/onert-micro/onert-micro/src/core/OMRuntimeModule.cpp b/onert-micro/onert-micro/src/core/OMRuntimeModule.cpp index 00555f59567..166d2e89ca0 100644 --- a/onert-micro/onert-micro/src/core/OMRuntimeModule.cpp +++ b/onert-micro/onert-micro/src/core/OMRuntimeModule.cpp @@ -100,10 +100,8 @@ OMStatus OMRuntimeModule::importModel(const char *model_ptr, const OMConfig &con return status; // 4 - AllocDeallocPlan creation - status = import::OMExecutionPlanCreator::createExecutionPlan(runtime_storage, runtime_context, - runtime_allocator, config); - if (status != Ok) - return status; + import::OMExecutionPlanCreator::createExecutionPlan(runtime_storage, runtime_context, + runtime_allocator, config); } for (uint32_t i = 0; i < num_subgraph; ++i) { diff --git a/onert-micro/onert-micro/src/core/memory/OMMemoryManager.cpp b/onert-micro/onert-micro/src/core/memory/OMMemoryManager.cpp index 210562d1e77..7ecbae1f4b0 100644 --- a/onert-micro/onert-micro/src/core/memory/OMMemoryManager.cpp +++ b/onert-micro/onert-micro/src/core/memory/OMMemoryManager.cpp @@ -27,9 +27,6 @@ OMStatus OMMemoryManager::allocateMemory(uint32_t size, uint8_t **data) *data = data_tmp; - if (*data == nullptr) - return UnknownError; - return Ok; } diff --git a/onert-micro/onert-micro/src/core/memory/OMRuntimeAllocator.cpp b/onert-micro/onert-micro/src/core/memory/OMRuntimeAllocator.cpp index acbbc11b63a..fbb89a1d9aa 100644 --- a/onert-micro/onert-micro/src/core/memory/OMRuntimeAllocator.cpp +++ b/onert-micro/onert-micro/src/core/memory/OMRuntimeAllocator.cpp @@ -18,6 +18,7 @@ #include "core/memory/OMMemoryManager.h" #include "core/OMDataType.h" +#include using namespace onert_micro::core::memory; using namespace onert_micro; @@ -66,7 +67,10 @@ OMStatus OMRuntimeAllocator::allocate(size_t kernel_index, OMRuntimeContext *con const auto casted_num_elements = static_cast(num_elements); const auto type_size = static_cast(getOMDataTypeSize(onertMicroDatatype(tensor->type()))); - + if (casted_num_elements > std::numeric_limits::max() / type_size) + { + return FailedCheckCondition; + } // allocate data uint8_t *allocated_data = nullptr; assert(storage->getDataByTensorIndex(&allocated_data, tensor_index) == Ok && @@ -94,15 +98,14 @@ OMStatus OMRuntimeAllocator::deallocate(size_t kernel_index, OMRuntimeStorage *s { uint8_t *allocated_data = nullptr; OMStatus status = storage->getDataByTensorIndex(&allocated_data, tensor_index); + assert(status == Ok); // note that status always 0 + // To continue deallocate due to current tensor is not saved in storage if (allocated_data == nullptr) continue; - if (status != Ok) - return status; status = OMMemoryManager::deallocateMemory(allocated_data); - if (status != Ok) - return status; + assert(status == Ok); // note that status always 0 status = storage->removeTensorFromTensorIndexToData(tensor_index); if (status != Ok) @@ -135,8 +138,6 @@ OMStatus OMRuntimeAllocator::allocateGraphInputs(OMRuntimeContext *context, uint8_t *allocated_data = nullptr; // First clear if already allocated status = storage->getDataByTensorIndex(&allocated_data, tensor_index); - if (status != Ok) - return status; OMMemoryManager::deallocateMemory(allocated_data); diff --git a/onert-micro/onert-micro/src/core/train/OMTrainingHandler.cpp b/onert-micro/onert-micro/src/core/train/OMTrainingHandler.cpp index d636f16b7b6..24535625a02 100644 --- a/onert-micro/onert-micro/src/core/train/OMTrainingHandler.cpp +++ b/onert-micro/onert-micro/src/core/train/OMTrainingHandler.cpp @@ -54,8 +54,6 @@ OMStatus OMTrainingHandler::handleError(const OMConfig &config, OMRuntimeStorage // Get calculated data uint8_t *calculated_data = nullptr; OMStatus status = forward_storage.getDataByTensorIndex(&calculated_data, forward_output_index); - if (status != Ok) - return status; assert(calculated_data != nullptr); // Get target data @@ -221,8 +219,6 @@ OMStatus OMTrainingHandler::evaluateMetric(OMMetrics metric, void *metric_val, // Get calculated data uint8_t *calculated_data = nullptr; OMStatus status = storage.getDataByTensorIndex(&calculated_data, forward_output_index); - if (status != Ok) - return status; assert(calculated_data != nullptr); // Get target data diff --git a/onert-micro/onert-micro/src/execute/OMRuntimeKernel.cpp b/onert-micro/onert-micro/src/execute/OMRuntimeKernel.cpp index 11b3e0437a7..afa3d64e5ce 100644 --- a/onert-micro/onert-micro/src/execute/OMRuntimeKernel.cpp +++ b/onert-micro/onert-micro/src/execute/OMRuntimeKernel.cpp @@ -87,9 +87,6 @@ OMStatus onert_micro::execute::OMRuntimeKernel::getDataFromStorage(uint16_t op_i continue; status = storage.getDataByTensorIndex(&outputs_data[i], outputs_index[i]); - if (status != Ok) - return status; - if (storage.getKernelType(op_index) == core::Inplace) { outputs_data[i] = inputs_data[i]; diff --git a/onert-micro/onert-micro/src/execute/kernels/Split.cpp b/onert-micro/onert-micro/src/execute/kernels/Split.cpp index ea0089e1b5d..982566b7026 100644 --- a/onert-micro/onert-micro/src/execute/kernels/Split.cpp +++ b/onert-micro/onert-micro/src/execute/kernels/Split.cpp @@ -90,7 +90,7 @@ OMStatus onert_micro::execute::execute_kernel_CircleSplit(const OMExecuteArgs &e OMRuntimeShape input_shape(input); OMRuntimeShape output_shape(output); - int32_t axis_value = axis_data[0]; + int32_t axis_value = utils::castInputData(axis_data)[0]; if (axis_value < 0) { axis_value += input_shape.dimensionsCount() + 1; diff --git a/onert-micro/onert-micro/src/optimize/pass/FindInplaceOpPass.cpp b/onert-micro/onert-micro/src/optimize/pass/FindInplaceOpPass.cpp index 08d2ee7dcae..06233d00561 100644 --- a/onert-micro/onert-micro/src/optimize/pass/FindInplaceOpPass.cpp +++ b/onert-micro/onert-micro/src/optimize/pass/FindInplaceOpPass.cpp @@ -61,6 +61,7 @@ OMStatus isInplaceOperation(const circle::Operator *op, core::OMRuntimeContext & is_inplace = true; break; } +#if 0 // FIXME: Enable after custom operation is introduced case circle::BuiltinOperator_CUSTOM: { core::OMBuilderCustomID custom_id; @@ -83,10 +84,13 @@ OMStatus isInplaceOperation(const circle::Operator *op, core::OMRuntimeContext & break; default: is_inplace = false; + break; } } +#endif default: is_inplace = false; + break; } return status; } @@ -212,9 +216,7 @@ OMStatus findInplaceOp(core::OMRuntimeStorage &storage, core::OMRuntimeContext & auto cur_op = operators->operator[](i); bool is_inplace = false; - status = isInplaceOperation(cur_op, context, is_inplace); - if (status != Ok) - return status; + isInplaceOperation(cur_op, context, is_inplace); if (is_inplace == false) continue;