diff --git a/mindspore/ccsrc/backend/kernel_compiler/cpu/nnacl/base/concat_base.c b/mindspore/ccsrc/backend/kernel_compiler/cpu/nnacl/base/concat_base.c index bfef2732099..636afd8a3f7 100644 --- a/mindspore/ccsrc/backend/kernel_compiler/cpu/nnacl/base/concat_base.c +++ b/mindspore/ccsrc/backend/kernel_compiler/cpu/nnacl/base/concat_base.c @@ -36,6 +36,7 @@ void Concat(void **input, int input_num, int axis, int **inputs_output_shape, si continue; } int input_stride = after_axis_size * inputs_output_shape[i][axis]; + NNACL_CHECK_ZERO_RETURN(thread_num); int offset = UP_DIV(input_stride, thread_num); int count = input_stride - offset * task_id; if (count <= 0) { diff --git a/mindspore/ccsrc/backend/kernel_compiler/cpu/nnacl/fp16/pooling_fp16.c b/mindspore/ccsrc/backend/kernel_compiler/cpu/nnacl/fp16/pooling_fp16.c index d9b4921c5fb..0c4360727de 100644 --- a/mindspore/ccsrc/backend/kernel_compiler/cpu/nnacl/fp16/pooling_fp16.c +++ b/mindspore/ccsrc/backend/kernel_compiler/cpu/nnacl/fp16/pooling_fp16.c @@ -35,6 +35,7 @@ int AvgPoolingFp16(const float16_t *input_ptr, float16_t *output_ptr, const Pool MS_FLOAT16X8 max_value = MS_MOVQ_F16(max); #endif + NNACL_CHECK_ZERO_RETURN_ERR(output_w); for (int batch = 0; batch < pooling_param->output_batch_; batch++) { const float16_t *src_b_ptr = input_ptr + batch * in_h * in_w * channel; float16_t *dst_b_ptr = output_ptr + batch * output_h * output_w * channel; @@ -268,6 +269,7 @@ void MaxPoolingFp16(const float16_t *input_ptr, float16_t *output_ptr, const Poo int thread_num = pooling_param->thread_num_; // input channel is equal to output channel + NNACL_CHECK_ZERO_RETURN(output_w); for (int batch = 0; batch < output_batch; batch++) { int in_batch_offset = batch * in_h * in_w * channel; int out_batch_offset = batch * output_h * output_w * channel; diff --git a/mindspore/ccsrc/backend/kernel_compiler/cpu/nnacl/fp32/instance_norm_fp32.c b/mindspore/ccsrc/backend/kernel_compiler/cpu/nnacl/fp32/instance_norm_fp32.c index cf95e164a8f..9d3800812b5 100644 --- a/mindspore/ccsrc/backend/kernel_compiler/cpu/nnacl/fp32/instance_norm_fp32.c +++ b/mindspore/ccsrc/backend/kernel_compiler/cpu/nnacl/fp32/instance_norm_fp32.c @@ -23,7 +23,7 @@ int InstanceNorm(const float *src_data, float *dst_data, const float *gamma_data const InstanceNormParameter *param, size_t task_id) { NNACL_CHECK_NULL_RETURN_ERR(src_data); NNACL_CHECK_NULL_RETURN_ERR(dst_data); - NNACL_CHECK_ZERO_RETURN_ERR(param->op_parameter_.thread_num_) + NNACL_CHECK_ZERO_RETURN_ERR(param->op_parameter_.thread_num_); int channel_step = UP_DIV(param->channel_, param->op_parameter_.thread_num_); int channel_begin = (int)(task_id)*channel_step; int channel_end = MSMIN(channel_begin + channel_step, param->channel_); diff --git a/mindspore/ccsrc/backend/kernel_compiler/cpu/nnacl/fp32/pad_fp32.c b/mindspore/ccsrc/backend/kernel_compiler/cpu/nnacl/fp32/pad_fp32.c index 2daaed1bf27..817d9b315f2 100644 --- a/mindspore/ccsrc/backend/kernel_compiler/cpu/nnacl/fp32/pad_fp32.c +++ b/mindspore/ccsrc/backend/kernel_compiler/cpu/nnacl/fp32/pad_fp32.c @@ -65,7 +65,7 @@ int GetInputFlattenIndex(int out_flatten_index, const int *input_shape, const Pa int in_flatten_index = 0; for (int i = 0; i < DEFAULT_PAD_NDIMS; ++i) { int left_pad = pad_param->paddings_[i * 2]; - NNACL_CHECK_ZERO_RETURN_ERR(pad_param->out_strides[i]) + NNACL_CHECK_ZERO_RETURN_ERR(pad_param->out_strides[i]); int out_dim_index = out_flatten_index / pad_param->out_strides[i]; out_flatten_index %= pad_param->out_strides[i]; int in_dim_index = TransOut2InputDimIndex(out_dim_index, left_pad, input_shape[i], pad_param->mirror_offset_); diff --git a/mindspore/ccsrc/backend/kernel_compiler/cpu/nnacl/int8/pad_int8.c b/mindspore/ccsrc/backend/kernel_compiler/cpu/nnacl/int8/pad_int8.c index e7c0c0eaad6..b1aba2be52c 100644 --- a/mindspore/ccsrc/backend/kernel_compiler/cpu/nnacl/int8/pad_int8.c +++ b/mindspore/ccsrc/backend/kernel_compiler/cpu/nnacl/int8/pad_int8.c @@ -54,6 +54,7 @@ int GetInputFlattenIndexInt8(int out_flatten_index, const int *input_shape, cons int i; for (i = 0; i < COMM_SHAPE_SIZE; ++i) { int left_pad = pad_param->paddings_[i * 2]; + NNACL_CHECK_ZERO_RETURN_ERR(pad_param->out_strides[i]); int out_dim_index = out_flatten_index / pad_param->out_strides[i]; out_flatten_index %= pad_param->out_strides[i]; int in_dim_index = TransOut2InputDimIndexInt8(out_dim_index, left_pad, input_shape[i], pad_param->mirror_offset_); diff --git a/mindspore/ccsrc/backend/kernel_compiler/cpu/nnacl/int8/pooling_int8.c b/mindspore/ccsrc/backend/kernel_compiler/cpu/nnacl/int8/pooling_int8.c index 8621702f7ad..cbf25d93563 100644 --- a/mindspore/ccsrc/backend/kernel_compiler/cpu/nnacl/int8/pooling_int8.c +++ b/mindspore/ccsrc/backend/kernel_compiler/cpu/nnacl/int8/pooling_int8.c @@ -94,7 +94,7 @@ int AvgPoolingOptInt8(const int8_t *input_ptr, int8_t *output_ptr, PoolingParame double real_multiplier = pooling_param->quant_args_[0][0].scale_ / pooling_param->quant_args_[1][0].scale_; const int8_t out_min = INT8_MIN; const int8_t out_max = INT8_MAX; - + NNACL_CHECK_ZERO_RETURN_ERR(output_w); for (int batch = 0; batch < pooling_param->output_batch_; batch++) { int in_batch_offset = batch * pooling_param->input_h_ * in_w * channel; int out_batch_offset = batch * pooling_param->output_h_ * output_w * channel; @@ -341,6 +341,7 @@ void MaxPoolingWithQuantInt8(const int8_t *input_ptr, int8_t *output_ptr, Poolin int output_zp = pooling_param->quant_args_[1][0].zp_; double real_multiplier = input_scale / output_scale; + NNACL_CHECK_ZERO_RETURN(output_w); for (int batch = 0; batch < pooling_param->output_batch_; batch++) { int in_batch_offset = batch * in_h * in_w * channel; int out_batch_offset = batch * pooling_param->output_h_ * output_w * channel; @@ -428,6 +429,7 @@ void MaxPoolingOptInt8(const int8_t *input_ptr, int8_t *output_ptr, PoolingParam int thread_num = MSMIN(out_tile_count, pooling_param->thread_num_); int8_t out_array[MAX_MAXPOOL_SIZE]; + NNACL_CHECK_ZERO_RETURN(output_w); for (int batch = 0; batch < pooling_param->output_batch_; batch++) { int in_batch_offset = batch * pooling_param->input_h_ * in_w * channel; int out_batch_offset = batch * pooling_param->output_h_ * output_w * channel; diff --git a/mindspore/ccsrc/backend/kernel_compiler/cpu/nnacl/op_base.h b/mindspore/ccsrc/backend/kernel_compiler/cpu/nnacl/op_base.h index a60af72f1b3..6bf09e0205a 100644 --- a/mindspore/ccsrc/backend/kernel_compiler/cpu/nnacl/op_base.h +++ b/mindspore/ccsrc/backend/kernel_compiler/cpu/nnacl/op_base.h @@ -49,6 +49,7 @@ #define DOWN_ROUND(x, y) ((x) / (y) * (y)) #define MSVALID(left, x, right) (MSMIN((MSMAX(left, x)), right)) +#define SIZE_MUL_OVERFLOW(x, y) (((x) == 0) ? false : (SIZE_MAX / (x)) < (y)) #define INT_MUL_OVERFLOW(x, y) \ ((x == 0) ? false \ : ((x) > 0 ? ((y >= 0) ? (INT_MAX / (x)) < (y) : (INT_MAX / (x)) < (-1 * (y))) \ @@ -156,21 +157,21 @@ if ((val) == 0) { \ return NNACL_ERR; \ } \ - } while (0); + } while (0) #define NNACL_CHECK_ZERO_RETURN(val) \ do { \ if ((val) == 0) { \ return; \ } \ - } while (0); + } while (0) #define NNACL_CHECK_NULL_RETURN_ERR(ptr) \ do { \ if ((ptr) == NULL) { \ return NNACL_NULL_PTR; \ } \ - } while (0); + } while (0) #else diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/resize_fp32_coder.cc b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/resize_fp32_coder.cc index a76025b263a..c1e5dc6a1b0 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/resize_fp32_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/resize_fp32_coder.cc @@ -107,29 +107,29 @@ int ResizeFP32Coder::MallocTmpBuffer() { // malloc memory for x, y coordinates { coordinate_.x_lefts_ = reinterpret_cast(malloc(sizeof(int) * x_len_)); - CHECK_MALLOC_RES(coordinate_.x_lefts_, RET_NULL_PTR) + CHECK_MALLOC_RES(coordinate_.x_lefts_, RET_NULL_PTR); coordinate_.y_tops_ = reinterpret_cast(malloc(sizeof(int) * y_len_)); - CHECK_MALLOC_RES(coordinate_.y_tops_, RET_NULL_PTR) + CHECK_MALLOC_RES(coordinate_.y_tops_, RET_NULL_PTR); if (method_ == static_cast(schema::ResizeMethod_LINEAR)) { coordinate_.x_rights_ = reinterpret_cast(malloc(sizeof(int) * x_len_)); - CHECK_MALLOC_RES(coordinate_.x_rights_, RET_NULL_PTR) + CHECK_MALLOC_RES(coordinate_.x_rights_, RET_NULL_PTR); coordinate_.y_bottoms_ = reinterpret_cast(malloc(sizeof(int) * y_len_)); - CHECK_MALLOC_RES(coordinate_.y_bottoms_, RET_NULL_PTR) + CHECK_MALLOC_RES(coordinate_.y_bottoms_, RET_NULL_PTR); } } // malloc memory for weights of x, y axes { x_weights_ = reinterpret_cast(malloc(sizeof(float) * x_weight_len_)); - CHECK_MALLOC_RES(x_weights_, RET_NULL_PTR) + CHECK_MALLOC_RES(x_weights_, RET_NULL_PTR); y_weights_ = reinterpret_cast(malloc(sizeof(float) * y_weight_len_)); - CHECK_MALLOC_RES(y_weights_, RET_NULL_PTR) + CHECK_MALLOC_RES(y_weights_, RET_NULL_PTR); } { size_t line_buffer_size = sizeof(float) * x_len_ * input_tensor_->Channel() * 2 * kMaxThreadNumSupported; line_buffer_ = reinterpret_cast(allocator_->Malloc(kNumberTypeFloat32, line_buffer_size, kWorkspace)); - CHECK_MALLOC_RES(line_buffer_, RET_NULL_PTR) + CHECK_MALLOC_RES(line_buffer_, RET_NULL_PTR); } return RET_OK; } diff --git a/mindspore/lite/src/common/log_util.h b/mindspore/lite/src/common/log_util.h index e4190fba37f..43a7836d3c2 100644 --- a/mindspore/lite/src/common/log_util.h +++ b/mindspore/lite/src/common/log_util.h @@ -34,7 +34,7 @@ MS_LOG(ERROR) << "malloc data failed."; \ return errcode; \ } \ - } while (0); + } while (0) #ifndef ENABLE_HIGH_PERFORMANCE #define CHECK_NULL_RETURN(ptr) \ @@ -43,7 +43,7 @@ MS_LOG(ERROR) << #ptr << " must not be null!"; \ return mindspore::lite::RET_NULL_PTR; \ } \ - } while (0); + } while (0) #define CHECK_LESS_RETURN(size1, size2) \ do { \ @@ -51,7 +51,8 @@ MS_LOG(ERROR) << #size1 << " must not less than " << #size2; \ return mindspore::lite::RET_ERROR; \ } \ - } while (0); + } while (0) + #else #define CHECK_NULL_RETURN(ptr) #define CHECK_LESS_RETURN(size1, size2) diff --git a/mindspore/lite/src/ops/populate/concat_populate.cc b/mindspore/lite/src/ops/populate/concat_populate.cc index c4a5bf48e75..ed0d5111ceb 100644 --- a/mindspore/lite/src/ops/populate/concat_populate.cc +++ b/mindspore/lite/src/ops/populate/concat_populate.cc @@ -20,8 +20,8 @@ using mindspore::schema::PrimitiveType_Concat; namespace mindspore { namespace lite { OpParameter *PopulateConcatParameter(const void *prim) { + MS_CHECK_TRUE_RET(prim != nullptr, nullptr); auto *primitive = static_cast(prim); - MS_ASSERT(primitive != nullptr); auto value = primitive->value_as_Concat(); if (value == nullptr) { MS_LOG(ERROR) << "param is nullptr"; diff --git a/mindspore/lite/src/ops/populate/non_max_suppression_populate.cc b/mindspore/lite/src/ops/populate/non_max_suppression_populate.cc index ddc8a423dde..c329e9d9a71 100644 --- a/mindspore/lite/src/ops/populate/non_max_suppression_populate.cc +++ b/mindspore/lite/src/ops/populate/non_max_suppression_populate.cc @@ -20,8 +20,8 @@ using mindspore::schema::PrimitiveType_NonMaxSuppression; namespace mindspore { namespace lite { OpParameter *PopulateNonMaxSuppressionParameter(const void *prim) { + MS_CHECK_TRUE_RET(prim != nullptr, nullptr); auto primitive = static_cast(prim); - MS_ASSERT(primitive != nullptr); auto value = primitive->value_as_NonMaxSuppression(); if (value == nullptr) { MS_LOG(ERROR) << "value is nullptr"; diff --git a/mindspore/lite/src/ops/populate/pad_populate.cc b/mindspore/lite/src/ops/populate/pad_populate.cc index f6a20588bb4..2c5535006aa 100644 --- a/mindspore/lite/src/ops/populate/pad_populate.cc +++ b/mindspore/lite/src/ops/populate/pad_populate.cc @@ -20,8 +20,8 @@ using mindspore::schema::PrimitiveType_PadFusion; namespace mindspore { namespace lite { OpParameter *PopulatePadParameter(const void *prim) { + MS_CHECK_TRUE_RET(prim != nullptr, nullptr); auto primitive = static_cast(prim); - MS_ASSERT(primitive != nullptr); auto value = primitive->value_as_PadFusion(); if (value == nullptr) { MS_LOG(ERROR) << "value is nullptr"; diff --git a/mindspore/lite/src/ops/populate/pooling_populate.cc b/mindspore/lite/src/ops/populate/pooling_populate.cc index 8b2933aa85b..794f5faeefc 100644 --- a/mindspore/lite/src/ops/populate/pooling_populate.cc +++ b/mindspore/lite/src/ops/populate/pooling_populate.cc @@ -21,8 +21,8 @@ using mindspore::schema::PrimitiveType_MaxPoolFusion; namespace mindspore { namespace lite { OpParameter *PopulateAvgPoolParameter(const void *primitive) { + MS_CHECK_TRUE_RET(primitive != nullptr, nullptr); auto pooling_prim = static_cast(primitive); - MS_ASSERT(pooling_prim != nullptr); auto value = pooling_prim->value_as_AvgPoolFusion(); if (value == nullptr) { MS_LOG(ERROR) << "value is nullptr"; diff --git a/mindspore/lite/src/ops/populate/reverse_sequence_populate.cc b/mindspore/lite/src/ops/populate/reverse_sequence_populate.cc index f40c22342be..5c3bb80434d 100644 --- a/mindspore/lite/src/ops/populate/reverse_sequence_populate.cc +++ b/mindspore/lite/src/ops/populate/reverse_sequence_populate.cc @@ -20,8 +20,8 @@ using mindspore::schema::PrimitiveType_ReverseSequence; namespace mindspore { namespace lite { OpParameter *PopulateReverseSequenceParameter(const void *prim) { + MS_CHECK_TRUE_RET(prim != nullptr, nullptr); auto primitive = static_cast(prim); - MS_ASSERT(primitive != nullptr); auto value = primitive->value_as_ReverseSequence(); if (value == nullptr) { MS_LOG(ERROR) << "value is nullptr"; diff --git a/mindspore/lite/src/ops/populate/roi_pooling_populate.cc b/mindspore/lite/src/ops/populate/roi_pooling_populate.cc index 90a5b7a0d78..faf8e654f56 100644 --- a/mindspore/lite/src/ops/populate/roi_pooling_populate.cc +++ b/mindspore/lite/src/ops/populate/roi_pooling_populate.cc @@ -20,8 +20,8 @@ using mindspore::schema::PrimitiveType_ROIPooling; namespace mindspore { namespace lite { OpParameter *PopulateROIPoolingParameter(const void *prim) { + MS_CHECK_TRUE_RET(prim != nullptr, nullptr); auto primitive = static_cast(prim); - MS_ASSERT(primitive != nullptr); auto value = primitive->value_as_ROIPooling(); if (value == nullptr) { MS_LOG(ERROR) << "value is nullptr"; diff --git a/mindspore/lite/src/ops/populate/shape_populate.cc b/mindspore/lite/src/ops/populate/shape_populate.cc index b3b316168cf..3e9c69b87dd 100644 --- a/mindspore/lite/src/ops/populate/shape_populate.cc +++ b/mindspore/lite/src/ops/populate/shape_populate.cc @@ -21,8 +21,8 @@ using mindspore::schema::PrimitiveType_Shape; namespace mindspore { namespace lite { OpParameter *PopulateShapeParameter(const void *prim) { + MS_CHECK_TRUE_RET(prim != nullptr, nullptr); auto primitive = static_cast(prim); - MS_ASSERT(primitive != nullptr); auto *param = reinterpret_cast(malloc(sizeof(OpParameter))); if (param == nullptr) { diff --git a/mindspore/lite/src/ops/populate/sparse_to_dense_populate.cc b/mindspore/lite/src/ops/populate/sparse_to_dense_populate.cc index 50755feb49b..121387d6fe7 100644 --- a/mindspore/lite/src/ops/populate/sparse_to_dense_populate.cc +++ b/mindspore/lite/src/ops/populate/sparse_to_dense_populate.cc @@ -20,8 +20,8 @@ using mindspore::schema::PrimitiveType_SparseToDense; namespace mindspore { namespace lite { OpParameter *PopulateSparseToDenseParameter(const void *prim) { + MS_CHECK_TRUE_RET(prim != nullptr, nullptr); auto primitive = static_cast(prim); - MS_ASSERT(primitive != nullptr); auto *param = reinterpret_cast(malloc(sizeof(SparseToDenseParameter))); if (param == nullptr) { diff --git a/mindspore/lite/src/ops/populate/unstack_populate.cc b/mindspore/lite/src/ops/populate/unstack_populate.cc index 56419db4dcc..851197a0839 100644 --- a/mindspore/lite/src/ops/populate/unstack_populate.cc +++ b/mindspore/lite/src/ops/populate/unstack_populate.cc @@ -20,8 +20,8 @@ using mindspore::schema::PrimitiveType_Unstack; namespace mindspore { namespace lite { OpParameter *PopulateUnstackParameter(const void *prim) { + MS_CHECK_TRUE_RET(prim != nullptr, nullptr); auto primitive = static_cast(prim); - MS_ASSERT(primitive != nullptr); auto value = primitive->value_as_Unstack(); if (value == nullptr) { MS_LOG(ERROR) << "value is nullptr"; diff --git a/mindspore/lite/src/ops/populate/v0/concat_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/concat_populate_v0.cc index 13203e8c245..ba96dede399 100644 --- a/mindspore/lite/src/ops/populate/v0/concat_populate_v0.cc +++ b/mindspore/lite/src/ops/populate/v0/concat_populate_v0.cc @@ -22,8 +22,8 @@ namespace mindspore { namespace lite { namespace { OpParameter *PopulateConcatParameter(const void *prim) { + MS_CHECK_TRUE_RET(prim != nullptr, nullptr); auto *primitive = static_cast(prim); - MS_ASSERT(primitive != nullptr); auto concat_prim = primitive->value_as_Concat(); if (concat_prim == nullptr) { MS_LOG(ERROR) << "concat_prim is nullptr"; diff --git a/mindspore/lite/src/ops/populate/v0/non_max_suppression_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/non_max_suppression_populate_v0.cc index d14f3720b09..b0dc6970df7 100644 --- a/mindspore/lite/src/ops/populate/v0/non_max_suppression_populate_v0.cc +++ b/mindspore/lite/src/ops/populate/v0/non_max_suppression_populate_v0.cc @@ -22,8 +22,8 @@ namespace mindspore { namespace lite { namespace { OpParameter *PopulateNonMaxSuppressionParameter(const void *prim) { + MS_CHECK_TRUE_RET(prim != nullptr, nullptr); auto *primitive = static_cast(prim); - MS_ASSERT(primitive != nullptr); auto non_max_suppression_prim = primitive->value_as_NonMaxSuppression(); if (non_max_suppression_prim == nullptr) { MS_LOG(ERROR) << "non_max_suppression_prim is nullptr"; diff --git a/mindspore/lite/src/ops/populate/v0/pad_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/pad_populate_v0.cc index e7eba86b1d9..51bddf903e1 100644 --- a/mindspore/lite/src/ops/populate/v0/pad_populate_v0.cc +++ b/mindspore/lite/src/ops/populate/v0/pad_populate_v0.cc @@ -22,8 +22,9 @@ namespace mindspore { namespace lite { namespace { OpParameter *PopulatePadParameter(const void *prim) { + MS_CHECK_TRUE_RET(prim != nullptr, nullptr); + auto *primitive = static_cast(prim); - MS_ASSERT(primitive != nullptr); auto pad_prim = primitive->value_as_Pad(); if (pad_prim == nullptr) { MS_LOG(ERROR) << "pad_prim is nullptr"; diff --git a/mindspore/lite/src/ops/populate/v0/pooling_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/pooling_populate_v0.cc index 8b5c46b1fd2..153ab440dd8 100644 --- a/mindspore/lite/src/ops/populate/v0/pooling_populate_v0.cc +++ b/mindspore/lite/src/ops/populate/v0/pooling_populate_v0.cc @@ -22,8 +22,8 @@ namespace mindspore { namespace lite { namespace { OpParameter *PopulatePoolingParameter(const void *prim) { + MS_CHECK_TRUE_RET(prim != nullptr, nullptr); auto *primitive = static_cast(prim); - MS_ASSERT(primitive != nullptr); auto pooling_prim = primitive->value_as_Pooling(); if (pooling_prim == nullptr) { MS_LOG(ERROR) << "pooling_prim is nullptr"; diff --git a/mindspore/lite/src/ops/populate/v0/reverse_sequence_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/reverse_sequence_populate_v0.cc index 305be9de031..d5e8511419c 100644 --- a/mindspore/lite/src/ops/populate/v0/reverse_sequence_populate_v0.cc +++ b/mindspore/lite/src/ops/populate/v0/reverse_sequence_populate_v0.cc @@ -22,8 +22,8 @@ namespace mindspore { namespace lite { namespace { OpParameter *PopulateReverseSequenceParameter(const void *prim) { + MS_CHECK_TRUE_RET(prim != nullptr, nullptr); auto *primitive = static_cast(prim); - MS_ASSERT(primitive != nullptr); auto reverse_sequence_prim = primitive->value_as_ReverseSequence(); if (reverse_sequence_prim == nullptr) { MS_LOG(ERROR) << "reverse_sequence_prim is nullptr"; diff --git a/mindspore/lite/src/ops/populate/v0/roi_pooling_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/roi_pooling_populate_v0.cc index ae49c335c7d..fafabd04af1 100644 --- a/mindspore/lite/src/ops/populate/v0/roi_pooling_populate_v0.cc +++ b/mindspore/lite/src/ops/populate/v0/roi_pooling_populate_v0.cc @@ -22,8 +22,8 @@ namespace mindspore { namespace lite { namespace { OpParameter *PopulateROIPoolingParameter(const void *prim) { + MS_CHECK_TRUE_RET(prim != nullptr, nullptr); auto *primitive = static_cast(prim); - MS_ASSERT(primitive != nullptr); auto roi_pooling_prim = primitive->value_as_ROIPooling(); if (roi_pooling_prim == nullptr) { MS_LOG(ERROR) << "roi_pooling_prim is nullptr"; diff --git a/mindspore/lite/src/ops/populate/v0/unstack_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/unstack_populate_v0.cc index 5959c11b93e..e6d08ea637e 100644 --- a/mindspore/lite/src/ops/populate/v0/unstack_populate_v0.cc +++ b/mindspore/lite/src/ops/populate/v0/unstack_populate_v0.cc @@ -22,8 +22,8 @@ namespace mindspore { namespace lite { namespace { OpParameter *PopulateUnstackParameter(const void *prim) { + MS_CHECK_TRUE_RET(prim != nullptr, nullptr); auto *primitive = static_cast(prim); - MS_ASSERT(primitive != nullptr); auto unstack_prim = primitive->value_as_Unstack(); if (unstack_prim == nullptr) { MS_LOG(ERROR) << "unstack_prim is nullptr"; diff --git a/mindspore/lite/src/ops/populate/where_populate.cc b/mindspore/lite/src/ops/populate/where_populate.cc index 01401e462f0..31af1ce556c 100644 --- a/mindspore/lite/src/ops/populate/where_populate.cc +++ b/mindspore/lite/src/ops/populate/where_populate.cc @@ -20,8 +20,8 @@ using mindspore::schema::PrimitiveType_Where; namespace mindspore { namespace lite { OpParameter *PopulateWhereParameter(const void *prim) { + MS_CHECK_TRUE_RET(prim != nullptr, nullptr); auto primitive = static_cast(prim); - MS_ASSERT(primitive != nullptr); auto *param = reinterpret_cast(malloc(sizeof(WhereParameter))); if (param == nullptr) { diff --git a/mindspore/lite/src/runtime/kernel/arm/base/pooling_base.cc b/mindspore/lite/src/runtime/kernel/arm/base/pooling_base.cc index a579f473fd4..94540e82542 100644 --- a/mindspore/lite/src/runtime/kernel/arm/base/pooling_base.cc +++ b/mindspore/lite/src/runtime/kernel/arm/base/pooling_base.cc @@ -83,10 +83,10 @@ void PoolingBaseCPUKernel::FreeQuantParam() { } int PoolingBaseCPUKernel::Init() { - MS_ASSERT(in_tensors_.size() == 1); - MS_ASSERT(out_tensors_.size() == 1); + CHECK_LESS_RETURN(in_tensors_.size(), 1); + CHECK_LESS_RETURN(out_tensors_.size(), 1); pooling_param_->thread_num_ = thread_count_; - MS_ASSERT(this->op_parameter_ != nullptr); + CHECK_NULL_RETURN(op_parameter_); return RET_OK; } diff --git a/mindspore/lite/src/runtime/kernel/arm/fp16/concat_fp16.cc b/mindspore/lite/src/runtime/kernel/arm/fp16/concat_fp16.cc index 355ad85f5d6..f2556fdefd0 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp16/concat_fp16.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp16/concat_fp16.cc @@ -101,21 +101,27 @@ int ConcatFp16CPUKernel::Run() { if (in_tensor->data_type() == kNumberTypeFloat || in_tensor->data_type() == kNumberTypeFloat32) { auto in_tensor_data = reinterpret_cast(in_tensor->data_c()); MS_ASSERT(in_tensor_data != nullptr); + CHECK_NULL_RETURN(in_tensor_data); Float32ToFloat16(in_tensor_data, fp16_inputs_[i], in_tensor->ElementsNum()); } else { fp16_inputs_[i] = reinterpret_cast(in_tensor->data_c()); MS_ASSERT(fp16_inputs_[i] != nullptr); + CHECK_NULL_RETURN(fp16_inputs_[i]); } shapes.push_back(in_tensors_[i]->shape()); + MS_CHECK_LT(concat_param_->axis_, static_cast(in_tensors_[i]->shape().size()), RET_ERROR); inputs_output_shape[i] = shapes[i].data(); } auto output_shape = out_tensors_.at(0)->shape(); + MS_CHECK_LT(concat_param_->axis_, static_cast(output_shape.size()), RET_ERROR); inputs_output_shape[input_num] = output_shape.data(); auto output_addr = out_tensors_.at(0)->MutableData(); + CHECK_NULL_RETURN(output_addr); if (out_tensors_.at(0)->data_type() == kNumberTypeFloat16) { fp16_output_ = reinterpret_cast(out_tensors_.at(0)->data_c()); MS_ASSERT(fp16_output_ != nullptr); + CHECK_NULL_RETURN(fp16_output_); } int dtype_len = in_tensors_.at(0)->data_type() == kNumberTypeInt32 ? sizeof(int32_t) : sizeof(float16_t); diff --git a/mindspore/lite/src/runtime/kernel/arm/fp16/pad_fp16.cc b/mindspore/lite/src/runtime/kernel/arm/fp16/pad_fp16.cc index 20c8a4f784a..f272cf8b15e 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp16/pad_fp16.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp16/pad_fp16.cc @@ -35,9 +35,13 @@ int PadFp16CPUKernel::RunImpl(int task_id) { int PadFp16CPUKernel::RunMirrorPadImpl(int task_id) { auto input = in_tensors_.at(0); + CHECK_NULL_RETURN(input); auto output = out_tensors_.at(0); + CHECK_NULL_RETURN(output); auto input_data = reinterpret_cast(input->data_c()); + CHECK_NULL_RETURN(input_data); auto output_data = reinterpret_cast(output->data_c()); + CHECK_NULL_RETURN(output_data); /* Fast Mirror pad */ if (mirror_pad_block_.size() != 0) { @@ -67,7 +71,7 @@ int PadFp16CPUKernel::RunMirrorPadImpl(int task_id) { } return RET_OK; } - + MS_CHECK_FALSE(op_parameter_->thread_num_ == 0, RET_ERROR); int unit = UP_DIV(out_tensors_.at(0)->ElementsNum(), op_parameter_->thread_num_); int begin = unit * task_id; int end = MSMIN(begin + unit, out_tensors_.at(0)->ElementsNum()); @@ -83,6 +87,7 @@ int PadFp16CPUKernel::Run() { MS_LOG(ERROR) << "The number of padding value should be only one, but got " << value_num; return RET_ERROR; } + CHECK_NULL_RETURN(pad_value->data_c()); pad_param_->constant_value_ = *(reinterpret_cast(pad_value->data_c())); } diff --git a/mindspore/lite/src/runtime/kernel/arm/fp16/pooling_fp16.cc b/mindspore/lite/src/runtime/kernel/arm/fp16/pooling_fp16.cc index 0ffff245ca8..f723dd7dbff 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp16/pooling_fp16.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp16/pooling_fp16.cc @@ -60,6 +60,9 @@ int PoolingFp16CPUKernel::RunImpl(int task_id) { minf = 0.f; maxf = 6.f; } + CHECK_NULL_RETURN(fp16_input_); + CHECK_NULL_RETURN(fp16_output_); + CHECK_NULL_RETURN(pooling_param_); if (pooling_param_->pool_mode_ == PoolMode_MaxPool) { MaxPoolingFp16(fp16_input_, fp16_output_, pooling_param_, task_id, minf, maxf); } else { diff --git a/mindspore/lite/src/runtime/kernel/arm/fp32/broadcast_to_fp32.cc b/mindspore/lite/src/runtime/kernel/arm/fp32/broadcast_to_fp32.cc index 9fef2ebb778..3e711faeecb 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp32/broadcast_to_fp32.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp32/broadcast_to_fp32.cc @@ -51,8 +51,8 @@ int BroadcastToCPUKernel::ReSize() { } int BroadcastToCPUKernel::Init() { - CHECK_LESS_RETURN(in_tensors_.size(), 1) - CHECK_LESS_RETURN(out_tensors_.size(), 1) + CHECK_LESS_RETURN(in_tensors_.size(), 1); + CHECK_LESS_RETURN(out_tensors_.size(), 1); shape_info_ = reinterpret_cast(malloc(sizeof(BroadcastShapeInfo))); if (shape_info_ == nullptr) { MS_LOG(ERROR) << "Malloc BroadcastShapeInfo failed!"; diff --git a/mindspore/lite/src/runtime/kernel/arm/fp32/concat_fp32.cc b/mindspore/lite/src/runtime/kernel/arm/fp32/concat_fp32.cc index 401ba4f74c9..c57df3b722e 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp32/concat_fp32.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp32/concat_fp32.cc @@ -47,12 +47,16 @@ int ConcatCPUKernel::DoConcat(int task_id) { std::vector> shapes; for (size_t i = 0; i < input_num; ++i) { inputs_addr[i] = in_tensors_[i]->data_c(); + CHECK_NULL_RETURN(inputs_addr[i]); shapes.push_back(in_tensors_[i]->shape()); + MS_CHECK_LT(concat_param_->axis_, static_cast(in_tensors_[i]->shape().size()), RET_ERROR); inputs_output_shape[i] = shapes[i].data(); } auto output_shape = out_tensors_.at(0)->shape(); + MS_CHECK_LT(concat_param_->axis_, static_cast(output_shape.size()), RET_ERROR); inputs_output_shape[input_num] = output_shape.data(); auto output_addr = out_tensors_.at(0)->data_c(); + CHECK_NULL_RETURN(output_addr); Concat(inputs_addr.data(), input_num, concat_param_->axis_, inputs_output_shape.data(), output_shape.size(), output_addr, task_id, op_parameter_->thread_num_, sizeof(float)); diff --git a/mindspore/lite/src/runtime/kernel/arm/fp32/non_max_suppression_fp32.cc b/mindspore/lite/src/runtime/kernel/arm/fp32/non_max_suppression_fp32.cc index 32c7795d5bc..27f908c7c1c 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp32/non_max_suppression_fp32.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp32/non_max_suppression_fp32.cc @@ -39,6 +39,8 @@ constexpr size_t kScoreTensorIndex = 1; constexpr size_t kMaxOutputNumTensorIndex = 2; constexpr size_t kIoUThresholdTensorIndex = 3; constexpr size_t kScoreThresholdTensorIndex = 4; +constexpr size_t kScoreDimsBoxNumIndex = 2; +constexpr size_t kBoxDimsBoxNumIndex = 1; constexpr size_t kYIndexA = 0; constexpr size_t kYIndexB = 2; constexpr size_t kXIndexA = 1; @@ -47,8 +49,6 @@ constexpr int kBoxPointNum = 4; } // namespace int NonMaxSuppressionCPUKernel::Init() { - CHECK_LESS_RETURN(in_tensors_.size(), C2NUM); - CHECK_LESS_RETURN(out_tensors_.size(), 1); // boxes, scores, max_output_boxes, iou_threshold, score_threshold if (in_tensors_.size() < kMinInputsSize || in_tensors_.size() > kMaxInputsSize || out_tensors_.size() != kOutputNum) { MS_LOG(ERROR) << "NonMaxSuppression input size should be in [" << kMinInputsSize << ", " << kMaxInputsSize << "]" @@ -166,7 +166,7 @@ int NonMaxSuppressionCPUKernel::Run_Selecte(bool simple_out, int box_num, int ba if (!simple_out) { const int output_last_dim = 3; output->set_shape({selected_num, output_last_dim}); - MS_ASSERT(output_last_dim * sizeof(int32_t) == sizeof(NMSIndex)); + MS_CHECK_TRUE_RET(output_last_dim * sizeof(int32_t) == sizeof(NMSIndex), RET_ERROR); auto *out_data = reinterpret_cast(output->ReallocData()); if (out_data == nullptr) { MS_LOG(ERROR) << "out_data is nullptr."; @@ -220,8 +220,7 @@ int NonMaxSuppressionCPUKernel::Run() { MS_LOG(ERROR) << "Boxes tensor batch num should be equal to scores tensor's batch num."; return RET_ERROR; } - constexpr size_t kScoreDimsBoxNumIndex = 2; - constexpr size_t kBoxDimsBoxNumIndex = 1; + if (score_dims.at(kScoreDimsBoxNumIndex) != box_dims.at(kBoxDimsBoxNumIndex)) { MS_LOG(ERROR) << "Boxes tensor spatial dimension should be equal to scores tensor's spatial dimension."; return RET_ERROR; diff --git a/mindspore/lite/src/runtime/kernel/arm/fp32/pad_fp32.cc b/mindspore/lite/src/runtime/kernel/arm/fp32/pad_fp32.cc index 5c8d9460dac..006f2184cc2 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp32/pad_fp32.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp32/pad_fp32.cc @@ -31,7 +31,7 @@ constexpr size_t kMirrorPadInputSize = 2; constexpr size_t kPadCommonInputSize = 2; } // namespace int PadCPUKernel::Init() { - CHECK_LESS_RETURN(in_tensors_.size(), 1); + CHECK_LESS_RETURN(in_tensors_.size(), kPadCommonInputSize); CHECK_LESS_RETURN(out_tensors_.size(), 1); if (!InferShapeDone()) { return RET_OK; diff --git a/mindspore/lite/src/runtime/kernel/arm/fp32/resize_fp32.cc b/mindspore/lite/src/runtime/kernel/arm/fp32/resize_fp32.cc index f2a79f05881..cb747d77d8f 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp32/resize_fp32.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp32/resize_fp32.cc @@ -118,30 +118,30 @@ int ResizeCPUKernel::MallocTmpBuffer() { // malloc memory for x, y coordinates { coordinate_.x_lefts_ = reinterpret_cast(malloc(static_cast(sizeof(int)) * x_len)); - CHECK_MALLOC_RES(coordinate_.x_lefts_, RET_NULL_PTR) + CHECK_MALLOC_RES(coordinate_.x_lefts_, RET_NULL_PTR); coordinate_.y_tops_ = reinterpret_cast(malloc(static_cast(sizeof(int)) * y_len)); - CHECK_MALLOC_RES(coordinate_.y_tops_, RET_NULL_PTR) + CHECK_MALLOC_RES(coordinate_.y_tops_, RET_NULL_PTR); if (method_ == static_cast(schema::ResizeMethod_LINEAR)) { coordinate_.x_rights_ = reinterpret_cast(malloc(static_cast(sizeof(int)) * x_len)); - CHECK_MALLOC_RES(coordinate_.x_rights_, RET_NULL_PTR) + CHECK_MALLOC_RES(coordinate_.x_rights_, RET_NULL_PTR); coordinate_.y_bottoms_ = reinterpret_cast(malloc(static_cast(sizeof(int)) * y_len)); - CHECK_MALLOC_RES(coordinate_.y_bottoms_, RET_NULL_PTR) + CHECK_MALLOC_RES(coordinate_.y_bottoms_, RET_NULL_PTR); } } // malloc memory for weights of x, y axes { x_weights_ = reinterpret_cast(malloc(static_cast(sizeof(float)) * x_weight_len)); - CHECK_MALLOC_RES(x_weights_, RET_NULL_PTR) + CHECK_MALLOC_RES(x_weights_, RET_NULL_PTR); y_weights_ = reinterpret_cast(malloc(static_cast(sizeof(float)) * y_weight_len)); - CHECK_MALLOC_RES(y_weights_, RET_NULL_PTR) + CHECK_MALLOC_RES(y_weights_, RET_NULL_PTR); } { line_buffer_ = reinterpret_cast(malloc(static_cast(sizeof(float)) * x_len * in_tensors_.at(0)->Channel() * kResizeSizeDouble * op_parameter_->thread_num_)); - CHECK_MALLOC_RES(line_buffer_, RET_NULL_PTR) + CHECK_MALLOC_RES(line_buffer_, RET_NULL_PTR); } return RET_OK; } diff --git a/mindspore/lite/src/runtime/kernel/arm/fp32/reverse_sequence_fp32.cc b/mindspore/lite/src/runtime/kernel/arm/fp32/reverse_sequence_fp32.cc index f7954b965ab..9c9bc66c8c7 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp32/reverse_sequence_fp32.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp32/reverse_sequence_fp32.cc @@ -25,7 +25,7 @@ using mindspore::schema::PrimitiveType_ReverseSequence; namespace mindspore::kernel { int ReverseSequenceCPUKernel::Init() { - CHECK_LESS_RETURN(in_tensors_.size(), C2NUM); + CHECK_LESS_RETURN(in_tensors_.size(), kInputSize1); CHECK_LESS_RETURN(out_tensors_.size(), 1); if (!InferShapeDone()) { return RET_OK; @@ -94,11 +94,11 @@ int ReverseSequenceCPUKernel::Run() { void *input1 = in_tensors_.at(1)->MutableData(); float *output = reinterpret_cast(out_tensors_.at(0)->MutableData()); ReverseSequenceParameter *param = reinterpret_cast(op_parameter_); - MS_ASSERT(param); + CHECK_NULL_RETURN(param); param->is_seq_length_int32_ = in_tensors_.at(1)->data_type() == kNumberTypeInt32; - MS_ASSERT(input0); - MS_ASSERT(input1); - MS_ASSERT(output); + CHECK_NULL_RETURN(input0); + CHECK_NULL_RETURN(input1); + CHECK_NULL_RETURN(output); ReverseSequence(input0, input1, output, param); return RET_OK; } diff --git a/mindspore/lite/src/runtime/kernel/arm/fp32/roi_pooling_fp32.cc b/mindspore/lite/src/runtime/kernel/arm/fp32/roi_pooling_fp32.cc index 4744e78952c..23412a2d70a 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp32/roi_pooling_fp32.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp32/roi_pooling_fp32.cc @@ -29,7 +29,7 @@ using mindspore::schema::PrimitiveType_ROIPooling; namespace mindspore::kernel { int ROIPoolingCPUKernel::Init() { - CHECK_LESS_RETURN(in_tensors_.size(), C2NUM); + CHECK_LESS_RETURN(in_tensors_.size(), kInputSize1); CHECK_LESS_RETURN(out_tensors_.size(), 1); if (!InferShapeDone()) { return RET_OK; @@ -69,6 +69,10 @@ int ROIPoolingCPUKernel::ReSize() { param_->out_strides_[i] = out_shape.at(i + 1) * param_->out_strides_[i + 1]; } param_->thread_num_ = MSMIN(param_->op_parameter_.thread_num_, out_shape.at(0)); + if (INT_MUL_OVERFLOW(param_->input_c_, static_cast(sizeof(float)))) { + MS_LOG(ERROR) << "int mul overflow"; + return RET_ERROR; + } max_c_ = reinterpret_cast(malloc(param_->input_c_ * static_cast(sizeof(float)))); if (max_c_ == nullptr) { MS_LOG(ERROR) << "malloc max_c failed."; @@ -78,11 +82,11 @@ int ROIPoolingCPUKernel::ReSize() { } int ROIPoolingCPUKernel::DoExecute(int task_id) { - MS_ASSERT(in_ptr_); - MS_ASSERT(out_ptr_); - MS_ASSERT(roi_ptr_); - MS_ASSERT(max_c_); - MS_ASSERT(param_); + CHECK_NULL_RETURN(in_ptr_); + CHECK_NULL_RETURN(out_ptr_); + CHECK_NULL_RETURN(roi_ptr_); + CHECK_NULL_RETURN(max_c_); + CHECK_NULL_RETURN(param_); auto ret = ROIPooling(in_ptr_, out_ptr_, roi_ptr_, max_c_, task_id, param_); if (ret != RET_OK) { MS_LOG(ERROR) << "ROIPooling Execute error task_id[" << task_id << "] error_code[" << ret << "]"; diff --git a/mindspore/lite/src/runtime/kernel/arm/fp32/scatter_nd_fp32.cc b/mindspore/lite/src/runtime/kernel/arm/fp32/scatter_nd_fp32.cc index f6ee6fa2bb6..f9afe0a5427 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp32/scatter_nd_fp32.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp32/scatter_nd_fp32.cc @@ -34,8 +34,8 @@ constexpr int kScatterIndicesIndex = 1; constexpr int kScatterUpdateIndex = 2; } // namespace int ScatterNDCPUKernel::Init() { - CHECK_LESS_RETURN(in_tensors_.size(), DIMENSION_3D) - CHECK_LESS_RETURN(out_tensors_.size(), 1) + CHECK_LESS_RETURN(in_tensors_.size(), DIMENSION_3D); + CHECK_LESS_RETURN(out_tensors_.size(), 1); if (!InferShapeDone()) { return RET_OK; } diff --git a/mindspore/lite/src/runtime/kernel/arm/fp32/scatter_nd_update_fp32.cc b/mindspore/lite/src/runtime/kernel/arm/fp32/scatter_nd_update_fp32.cc index 1e04578785e..97a2d18b771 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp32/scatter_nd_update_fp32.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp32/scatter_nd_update_fp32.cc @@ -35,8 +35,8 @@ constexpr int kScatterUpdateIndex = 2; constexpr size_t kScatterIndicesDims = 2; } // namespace int ScatterNdUpdateCPUKernel::Init() { - CHECK_LESS_RETURN(in_tensors_.size(), DIMENSION_3D) - CHECK_LESS_RETURN(out_tensors_.size(), 1) + CHECK_LESS_RETURN(in_tensors_.size(), DIMENSION_3D); + CHECK_LESS_RETURN(out_tensors_.size(), 1); if (!InferShapeDone()) { return RET_OK; } diff --git a/mindspore/lite/src/runtime/kernel/arm/fp32/sparse_to_dense_fp32.cc b/mindspore/lite/src/runtime/kernel/arm/fp32/sparse_to_dense_fp32.cc index 2f4db92acc9..93961b89ca3 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp32/sparse_to_dense_fp32.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp32/sparse_to_dense_fp32.cc @@ -37,6 +37,8 @@ int SparseToDenseCPUKernel::Init() { auto input3 = in_tensors_.at(3); MS_ASSERT(input3); sparse_values = reinterpret_cast(input2->MutableData()); + CHECK_NULL_RETURN(sparse_values); + CHECK_NULL_RETURN(input3->MutableData()); default_value = reinterpret_cast(input3->MutableData())[0]; if (input2->ElementsNum() == 1) { isScalar = true; @@ -74,10 +76,10 @@ int SparseToDenseCPUKernel::DoExcute(int task_id) { return RET_ERROR; } int out_width = output_num / index_num; - MS_ASSERT(sparse_indices_vect); - MS_ASSERT(output_shape); - MS_ASSERT(sparse_values); - MS_ASSERT(output_data); + CHECK_NULL_RETURN(sparse_indices_vect); + CHECK_NULL_RETURN(output_shape); + CHECK_NULL_RETURN(sparse_values); + CHECK_NULL_RETURN(output_data); SparseToDense(sparse_indices_vect, output_shape, sparse_values, default_value, output_data, isScalar, index_start, index_end, out_width); return RET_OK; @@ -108,6 +110,7 @@ int SparseToDenseCPUKernel::GenerateIndices() { } index_dim = static_cast(input0->shape().size()); int *sparse_indices = reinterpret_cast(input0->MutableData()); + CHECK_NULL_RETURN(sparse_indices); switch (index_dim) { case 0: case 1: { @@ -180,7 +183,7 @@ int SparseToDenseCPUKernel::Run() { } } output_data = reinterpret_cast(out_tensors_.at(0)->MutableData()); - MS_ASSERT(output_data != nullptr); + CHECK_NULL_RETURN(output_data); count_unit_ = thread_count_ > 1 ? UP_DIV(index_num, thread_count_) : index_num; ret = ParallelLaunch(this->ms_context_, SparseToDenseRun, this, s2d_param->thread_num_); if (ret != RET_OK) { @@ -191,7 +194,7 @@ int SparseToDenseCPUKernel::Run() { if (sparse_indices_vect != nullptr) { for (int i = 0; i < index_num; i++) { if (sparse_indices_vect[i] != nullptr) { - delete sparse_indices_vect[i]; + delete[] sparse_indices_vect[i]; } } ctx_->allocator->Free(sparse_indices_vect); diff --git a/mindspore/lite/src/runtime/kernel/arm/fp32/unstack_fp32.cc b/mindspore/lite/src/runtime/kernel/arm/fp32/unstack_fp32.cc index 97deeee1cb9..c89a38027df 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp32/unstack_fp32.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp32/unstack_fp32.cc @@ -27,6 +27,7 @@ namespace mindspore::kernel { int UnstackCPUKernel::Init() { CHECK_LESS_RETURN(in_tensors_.size(), 1); CHECK_LESS_RETURN(out_tensors_.size(), 1); + CHECK_NULL_RETURN(op_parameter_); if (!InferShapeDone()) { return RET_OK; } @@ -59,6 +60,10 @@ int UnstackCPUKernel::ReSize() { free(output_addr_array_); output_addr_array_ = nullptr; } + if (SIZE_MUL_OVERFLOW(sizeof(void *), out_tensors_.size())) { + MS_LOG(ERROR) << "mul overflow"; + return RET_ERROR; + } output_addr_array_ = reinterpret_cast(malloc(sizeof(void *) * out_tensors_.size())); if (output_addr_array_ == nullptr) { MS_LOG(ERROR) << "Failed to malloc memory"; @@ -69,12 +74,12 @@ int UnstackCPUKernel::ReSize() { int UnstackCPUKernel::Run() { float *input = reinterpret_cast(in_tensors_.at(0)->MutableData()); - MS_ASSERT(input); + CHECK_NULL_RETURN(input); size_t out_num = out_tensors_.size(); for (size_t i = 0; i < out_num; i++) { output_addr_array_[i] = out_tensors_.at(i)->data_c(); + CHECK_NULL_RETURN(output_addr_array_[i]); } - MS_ASSERT(output_addr_array_); auto para = reinterpret_cast(op_parameter_); para->num_ = static_cast(out_num); Unstack(input, output_addr_array_, para, sizeof(float)); diff --git a/mindspore/lite/src/runtime/kernel/arm/fp32/where_fp32.cc b/mindspore/lite/src/runtime/kernel/arm/fp32/where_fp32.cc index 3e7b5653e26..27aaa2a9d97 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp32/where_fp32.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp32/where_fp32.cc @@ -15,7 +15,6 @@ */ #include "src/runtime/kernel/arm/fp32/where_fp32.h" #include -#include #include "schema/model_generated.h" #include "nnacl/fp32/where_fp32.h" #include "src/kernel_registry.h" @@ -47,11 +46,11 @@ int WhereCPUKernel::PreProcess() { } int WhereCPUKernel::DoExcute(int task_id) { - MS_ASSERT(condition_); - MS_ASSERT(x_); - MS_ASSERT(y_); - MS_ASSERT(output_data_); - MS_ASSERT(where_param_); + CHECK_NULL_RETURN(condition_); + CHECK_NULL_RETURN(x_); + CHECK_NULL_RETURN(y_); + CHECK_NULL_RETURN(output_data_); + CHECK_NULL_RETURN(where_param_); WhereWithTripleInputs(condition_, x_, y_, output_data_, where_param_, task_id); return RET_OK; } @@ -70,15 +69,15 @@ int WhereCPUKernel::RunWithSingleInput() { auto input = in_tensors_.at(0); MS_ASSERT(input); condition_ = reinterpret_cast(input->data_c()); + CHECK_NULL_RETURN(condition_); where_param_->condition_num_ = input->ElementsNum(); where_param_->rank_ = static_cast(input->shape().size()); int strides[8]; ComputeStrides(in_tensors_.at(0)->shape().data(), strides, where_param_->rank_); - auto data = ms_context_->allocator->Malloc(where_param_->condition_num_ * where_param_->rank_ * static_cast(sizeof(int32_t))); if (data == nullptr) { - MS_LOG(ERROR) << "macllov data is error!"; + MS_LOG(ERROR) << "malloc data is error!"; return RET_ERROR; } int *result = reinterpret_cast(data); @@ -107,6 +106,7 @@ int WhereCPUKernel::RunWithSingleInput() { MS_LOG(ERROR) << "malloc out tensor failed."; return RET_ERROR; } + MS_CHECK_GE(where_param_->condition_num_, true_num, RET_ERROR); memcpy(out_data, result, true_num * where_param_->rank_ * static_cast(sizeof(int32_t))); ms_context_->allocator->Free(data); return RET_OK; diff --git a/mindspore/lite/src/runtime/kernel/arm/int8/concat_int8.cc b/mindspore/lite/src/runtime/kernel/arm/int8/concat_int8.cc index e185ff3f9e5..67bbfefef5c 100644 --- a/mindspore/lite/src/runtime/kernel/arm/int8/concat_int8.cc +++ b/mindspore/lite/src/runtime/kernel/arm/int8/concat_int8.cc @@ -28,11 +28,21 @@ namespace mindspore::kernel { int ConcatInt8CPUKernel::Init() { concat_param_->input_shapes_ = nullptr; auto input_num = in_tensors_.size(); + MS_CHECK_FALSE(input_num == 0, RET_ERROR); + if (SIZE_MUL_OVERFLOW(sizeof(int8_t *), input_num)) { + MS_LOG(ERROR) << "mul overflow"; + return RET_ERROR; + } input_data_ = reinterpret_cast(malloc(sizeof(int8_t *) * input_num)); if (input_data_ == nullptr) { MS_LOG(ERROR) << "Null pointer reference: inputs_array."; return RET_ERROR; } + + if (SIZE_MUL_OVERFLOW(sizeof(QuantArg), input_num)) { + MS_LOG(ERROR) << "mul overflow"; + return RET_ERROR; + } concat_param_->quant_arg_.in_args_ = reinterpret_cast(malloc(sizeof(QuantArg) * input_num)); if (concat_param_->quant_arg_.in_args_ == nullptr) { MS_LOG(ERROR) << "Null pointer reference: quant_concat_parm_->in_quant_args_."; @@ -65,6 +75,10 @@ int ConcatInt8CPUKernel::ReSize() { auto input_num = in_tensors_.size(); concat_param_->input_num_ = static_cast(input_num); + if (SIZE_MUL_OVERFLOW(sizeof(int *), input_num)) { + MS_LOG(ERROR) << "mul overflow"; + return RET_ERROR; + } concat_param_->input_shapes_ = reinterpret_cast(malloc(sizeof(int *) * input_num)); if (concat_param_->input_shapes_ == nullptr) { MS_LOG(ERROR) << "malloc concat_param_->input_shapes_ failed."; @@ -72,6 +86,10 @@ int ConcatInt8CPUKernel::ReSize() { } for (size_t i = 0; i < input_num; i++) { auto in_shape = in_tensors_.at(i)->shape(); + if (SIZE_MUL_OVERFLOW(in_shape.size(), sizeof(int))) { + MS_LOG(ERROR) << "mul overflow"; + return RET_ERROR; + } concat_param_->input_shapes_[i] = reinterpret_cast(malloc(in_shape.size() * sizeof(int))); if (concat_param_->input_shapes_[i] == nullptr) { MS_LOG(ERROR) << "malloc concat_param_->input_shapes_[" << i << "]" @@ -90,6 +108,10 @@ int ConcatInt8CPUKernel::ReSize() { auto output_tensor = out_tensors_.at(kOutputIndex); auto out_shape = output_tensor->shape(); size_t output_dim = out_shape.size(); + if (SIZE_MUL_OVERFLOW(output_dim, sizeof(int))) { + MS_LOG(ERROR) << "mul overflow"; + return RET_ERROR; + } concat_param_->output_shapes_ = reinterpret_cast(malloc(output_dim * sizeof(int))); if (concat_param_->output_shapes_ == nullptr) { MS_LOG(ERROR) << "malloc concat_param_->output_shapes_ failed."; @@ -107,15 +129,17 @@ int ConcatInt8CPUKernel::ReSize() { int ConcatInt8CPUKernel::Run() { auto input_num = concat_param_->input_num_; + MS_CHECK_FALSE(op_parameter_->thread_num_ == 0, RET_ERROR); count_unit_ = op_parameter_->thread_num_ > 1 ? UP_DIV(before_axis_size, op_parameter_->thread_num_) : before_axis_size; concat_param_->count_unit_ = count_unit_; for (int i = 0; i < input_num; i++) { input_data_[i] = static_cast(in_tensors_.at(i)->MutableData()); + CHECK_NULL_RETURN(input_data_[i]); } output_data_ = reinterpret_cast(out_tensors_.at(0)->MutableData()); - + CHECK_NULL_RETURN(output_data_); auto ret = ParallelLaunch(this->ms_context_, ConcatInt8Run, this, op_parameter_->thread_num_); return ret; diff --git a/mindspore/lite/src/runtime/kernel/arm/int8/pad_int8.cc b/mindspore/lite/src/runtime/kernel/arm/int8/pad_int8.cc index d45afdee830..e1649e3fa89 100644 --- a/mindspore/lite/src/runtime/kernel/arm/int8/pad_int8.cc +++ b/mindspore/lite/src/runtime/kernel/arm/int8/pad_int8.cc @@ -18,6 +18,7 @@ #include #include #include "src/kernel_registry.h" +#include "nnacl/op_base.h" using mindspore::lite::RET_ERROR; using mindspore::lite::RET_MEMORY_FAILED; @@ -112,6 +113,9 @@ int PadInt8CPUKernel::ReSize() { int PadInt8CPUKernel::Init() { MS_ASSERT(pad_param_); + CHECK_LESS_RETURN(in_tensors_.size(), kInputSize1); + CHECK_LESS_RETURN(out_tensors_.size(), 1); + CHECK_NULL_RETURN(op_parameter_); auto error_code = SetQuantParam(); if (error_code != RET_OK) { MS_LOG(ERROR) << "SetQuantParam failed. errorcode: " << error_code; @@ -124,10 +128,10 @@ int PadInt8CPUKernel::Init() { } int PadInt8CPUKernel::RunImpl(int task_id) { - MS_ASSERT(in_data_); - MS_ASSERT(out_data_); - MS_ASSERT(in_dims_); - MS_ASSERT(out_dims_); + CHECK_NULL_RETURN(in_data_); + CHECK_NULL_RETURN(out_data_); + CHECK_NULL_RETURN(in_dims_); + CHECK_NULL_RETURN(out_dims_); return PadConstant4D(in_data_, out_data_, in_dims_, out_dims_, pad_param_->paddings_, task_id, op_parameter_->thread_num_); } @@ -159,6 +163,9 @@ int PadInt8CPUKernel::HandleMirrorPad() { void PadInt8CPUKernel::CalculateStrides() { pad_param_->in_strides[COMM_SHAPE_SIZE - 1] = 1; for (auto i = COMM_SHAPE_SIZE - 2; i >= 0; --i) { + if (INT_MUL_OVERFLOW(in_dims_[i + 1], pad_param_->in_strides[i + 1])) { + return; + } pad_param_->in_strides[i] = in_dims_[i + 1] * pad_param_->in_strides[i + 1]; } for (auto i = 0; i < COMM_SHAPE_SIZE; ++i) { @@ -166,6 +173,9 @@ void PadInt8CPUKernel::CalculateStrides() { } pad_param_->out_strides[COMM_SHAPE_SIZE - 1] = 1; for (auto i = COMM_SHAPE_SIZE - 2; i >= 0; --i) { + if (INT_MUL_OVERFLOW(out_dims_[i + 1], pad_param_->out_strides[i + 1])) { + return; + } pad_param_->out_strides[i] = out_dims_[i + 1] * pad_param_->out_strides[i + 1]; } } @@ -189,10 +199,10 @@ int PadInt8CPUKernel::RunMirrorPadImpl(int task_id) { auto output = out_tensors_.at(0); MS_ASSERT(output); auto input_data = reinterpret_cast(input->MutableData()); - MS_ASSERT(input_data); + CHECK_NULL_RETURN(input_data); auto output_data = reinterpret_cast(output->MutableData()); - MS_ASSERT(output_data); - + CHECK_NULL_RETURN(output_data); + MS_CHECK_FALSE(op_parameter_->thread_num_ == 0, RET_ERROR); int unit = UP_DIV(output->ElementsNum(), op_parameter_->thread_num_); int begin = unit * task_id; int end = MSMIN(begin + unit, output->ElementsNum()); @@ -263,8 +273,9 @@ int PadInt8CPUKernel::CopyPaddingFromInput() { int PadInt8CPUKernel::Run() { in_data_ = reinterpret_cast(in_tensors_.at(0)->MutableData()); + CHECK_NULL_RETURN(in_data_); out_data_ = reinterpret_cast(out_tensors_.at(0)->MutableData()); - + CHECK_NULL_RETURN(out_data_); int error_code; if (pad_param_->pad_mode_ == static_cast(schema::PaddingMode_CONSTANT)) { memset(out_data_, pad_param_->pad_quant_arg_.constant_value_[0], diff --git a/mindspore/lite/src/runtime/kernel/arm/int8/pooling_int8.cc b/mindspore/lite/src/runtime/kernel/arm/int8/pooling_int8.cc index 798d0275950..ee7f79290a3 100644 --- a/mindspore/lite/src/runtime/kernel/arm/int8/pooling_int8.cc +++ b/mindspore/lite/src/runtime/kernel/arm/int8/pooling_int8.cc @@ -64,10 +64,10 @@ int PoolingInt8CPUKernel::ReSize() { int PoolingInt8CPUKernel::RunImpl(int task_id) { auto input_data = reinterpret_cast(in_tensors_.at(kInputIndex)->MutableData()); - MS_ASSERT(input_data); + CHECK_NULL_RETURN(input_data); auto output_data = reinterpret_cast(out_tensors_.at(kOutputIndex)->MutableData()); - MS_ASSERT(output_data); - MS_ASSERT(pooling_param_); + CHECK_NULL_RETURN(output_data); + CHECK_NULL_RETURN(pooling_param_); if (pooling_param_->pool_mode_ == PoolMode_MaxPool) { if (pooling_param_->quantize_) { MaxPoolingWithQuantInt8(input_data, output_data, pooling_param_, task_id); diff --git a/mindspore/lite/src/runtime/kernel/opencl/kernel/concat.cc b/mindspore/lite/src/runtime/kernel/opencl/kernel/concat.cc index 92bcac2ce63..fb50e74c69b 100644 --- a/mindspore/lite/src/runtime/kernel/opencl/kernel/concat.cc +++ b/mindspore/lite/src/runtime/kernel/opencl/kernel/concat.cc @@ -59,8 +59,14 @@ void ConcatGetWorkGroup(const std::vector &global, std::vector * const int max_divider = 8; const int max_x = 2, max_y = 8; int x = std::min(GetMaxDivisorStrategy1(global[0], max_divider), max_x); + if (x == 0) { + return; + } int yz = max_size / x; int y = std::min(std::min(GetMaxDivisorStrategy1(global[1], max_divider), yz), max_y); + if (y == 0) { + return; + } int z = std::min(yz / y, static_cast(UP_DIV(global[2], 2))); local->clear(); diff --git a/mindspore/lite/src/runtime/kernel/opencl/kernel/sparse_to_dense.cc b/mindspore/lite/src/runtime/kernel/opencl/kernel/sparse_to_dense.cc index ccb3b4bc566..95884d19e14 100644 --- a/mindspore/lite/src/runtime/kernel/opencl/kernel/sparse_to_dense.cc +++ b/mindspore/lite/src/runtime/kernel/opencl/kernel/sparse_to_dense.cc @@ -55,6 +55,7 @@ int SparseToDenseOpenCLKernel::InitOutputToDefault() { int SparseToDenseOpenCLKernel::InitWeights() { auto allocator = ocl_runtime_->GetAllocator(); + MS_CHECK_GE(in_tensors_.size(), 3, RET_ERROR); auto weight_tensor = in_tensors_[2]; size_t size = 1; for (int i = 0; i < weight_tensor->shape().size(); ++i) { @@ -177,6 +178,7 @@ void SparseToDenseOpenCLKernel::SetGlobalLocal() { int SparseToDenseOpenCLKernel::Prepare() { enable_fp16_ = ocl_runtime_->GetFp16Enable(); input_dim_ = in_tensors_[0]->shape().size(); + MS_CHECK_GE(input_dim_, 2, RET_ERROR); inshapeindex1_dim = in_tensors_[0]->shape()[1]; weight_scalar_ = in_tensors_[2]->IsScalar(); const std::string kernel_name = "SparseToDense" + std::string(weight_scalar_ ? "Scalar" : "Vector");