Add ANEURALNETWORKS_BAD_DATA check in validateOperandType/setFromPointer (#916)
author김수진/동작제어Lab(SR)/Engineer/삼성전자 <sjsujin.kim@samsung.com>
Thu, 26 Apr 2018 07:05:17 +0000 (16:05 +0900)
committer최형규/동작제어Lab(SR)/Senior Engineer/삼성전자 <hk0110.choi@samsung.com>
Thu, 26 Apr 2018 07:05:17 +0000 (16:05 +0900)
* 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 <sjsujin.kim@samsung.com>
* Add detailed comment to TODO comment

src/runtime/ref/nn/common/Utils.cpp
src/runtime/ref/nn/runtime/ExecutionBuilder.cpp

index 98321f4..a06f944 100644 (file)
@@ -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;
 }
 
index 333836e..b89dc2c 100644 (file)
@@ -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;