From 5434dac309040725b227e7d57723d7eea0a8dece Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EA=B9=80=EC=88=98=EC=A7=84/=EB=8F=99=EC=9E=91=EC=A0=9C?= =?utf8?q?=EC=96=B4Lab=28SR=29/Engineer/=EC=82=BC=EC=84=B1=EC=A0=84?= =?utf8?q?=EC=9E=90?= Date: Thu, 26 Apr 2018 16:05:17 +0900 Subject: [PATCH] Add ANEURALNETWORKS_BAD_DATA check in validateOperandType/setFromPointer (#916) * add additional ANEURALNETWORKS_BAD_DATA check in validateOperandType/setFromPointer This commit adds additional ANEURALNETWORKS_BAD_DATA check in below - validateOperandType - setFromPointer These codes have copied from android NN master branch(af63a36) Fix : #805 Signed-off-by: sjsujinkim * Add detailed comment to TODO comment --- src/runtime/ref/nn/common/Utils.cpp | 12 ++++++++++++ src/runtime/ref/nn/runtime/ExecutionBuilder.cpp | 21 ++++++++++++++------- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/runtime/ref/nn/common/Utils.cpp b/src/runtime/ref/nn/common/Utils.cpp index 98321f4..a06f944 100644 --- a/src/runtime/ref/nn/common/Utils.cpp +++ b/src/runtime/ref/nn/common/Utils.cpp @@ -218,6 +218,18 @@ int validateOperandType(const ANeuralNetworksOperandType& type, const char* tag, return ANEURALNETWORKS_BAD_DATA; } } + + // TODO-NNRT : add 'type.type == ANEURALNETWORKS_OEM_SCALAR' later. + // OEM operaters are not supported now. + if (type.type == ANEURALNETWORKS_FLOAT32 || + type.type == ANEURALNETWORKS_INT32 || + type.type == ANEURALNETWORKS_UINT32) { + if (type.dimensionCount != 0 || type.dimensions != nullptr) { + LOG(ERROR) << tag << " Invalid dimensions for scalar type"; + return ANEURALNETWORKS_BAD_DATA; + } + } + return ANEURALNETWORKS_NO_ERROR; } diff --git a/src/runtime/ref/nn/runtime/ExecutionBuilder.cpp b/src/runtime/ref/nn/runtime/ExecutionBuilder.cpp index 333836e..b89dc2c 100644 --- a/src/runtime/ref/nn/runtime/ExecutionBuilder.cpp +++ b/src/runtime/ref/nn/runtime/ExecutionBuilder.cpp @@ -28,17 +28,24 @@ namespace nn { int ModelArgumentInfo::setFromPointer(const Operand& operand, const ANeuralNetworksOperandType* type, void* data, uint32_t length) { - int n = updateDimensionInfo(operand, type); - if (n != ANEURALNETWORKS_NO_ERROR) { - return n; + if ((data == nullptr) != (length == 0)) { + LOG(ERROR) << "Data pointer must be nullptr if and only if length is zero (data = " + << data << ", length = " << length << ")"; + return ANEURALNETWORKS_BAD_DATA; } if (data == nullptr) { - if (length) { - LOG(ERROR) << "Setting argument as having no value but non-zero length passed."; - return ANEURALNETWORKS_BAD_DATA; - } state = ModelArgumentInfo::HAS_NO_VALUE; } else { + int n = updateDimensionInfo(operand, type); + if (n != ANEURALNETWORKS_NO_ERROR) { + return n; + } + uint32_t neededLength = sizeOfData(operand.type, dimensions); + if (operand.type != OperandType::OEM && neededLength != length) { + LOG(ERROR) << "Setting argument with invalid length: " << length + << ", expected length: " << neededLength; + return ANEURALNETWORKS_BAD_DATA; + } state = ModelArgumentInfo::POINTER; } buffer = data; -- 2.7.4