Moved validation to the specific classes.

It's hard to keep track of what's validated when the validations
are found in two files (NeuralNetworks.cpp + one of the other).
Move all the validations except for the initial nullptr check
to the specific file.

Moved some code from Memory.h into the .cpp file.

Adjusted some type size for setInput/Output/*

Adding new validations will be in the future CLs to make
review easier.

Bug: 63905942
Test: Ran local and VTS tests.
Change-Id: I2c448c9b0d3f2e878278f24eafce5720590403f3
diff --git a/common/Utils.cpp b/common/Utils.cpp
index 247d56f..417cad6 100644
--- a/common/Utils.cpp
+++ b/common/Utils.cpp
@@ -171,6 +171,48 @@
     return extra;
 }
 
+// Validates the type. The used dimensions can be underspecified.
+int validateOperandType(const ANeuralNetworksOperandType& type, const char* tag,
+                        bool allowPartial) {
+    if (!allowPartial) {
+        for (uint32_t i = 0; i < type.dimensionCount; i++) {
+            if (type.dimensions[i] == 0) {
+                LOG(ERROR) << tag << " OperandType invalid dimensions[" << i
+                           << "] = " << type.dimensions[i];
+                return ANEURALNETWORKS_BAD_DATA;
+            }
+        }
+    }
+    if (!validCode(kNumberOfDataTypes, kNumberOfDataTypesOEM, type.type)) {
+        LOG(ERROR) << tag << " OperandType invalid type " << type.type;
+        return ANEURALNETWORKS_BAD_DATA;
+    }
+    /* TODO validate the quantization info.
+    if (type.offset != 0.f && type.scale == 0.f) {
+        LOG(ERROR) << ("%s OperandType invalid offset %f and scale %f", tag, type.offset,
+    type.scale); return ANEURALNETWORKS_BAD_DATA;
+    }
+    if (type.scale != 0.f &&
+        (type.type != ANEURALNETWORKS_FLOAT32)) {
+            LOG(ERROR) << ("%s OperandType scale %f with float type %u", tag, type.scale,
+    type.type); return ANEURALNETWORKS_BAD_DATA;
+        }
+     */
+    return ANEURALNETWORKS_NO_ERROR;
+}
+
+int validateOperandList(uint32_t count, const uint32_t* list, uint32_t operandCount,
+                        const char* tag) {
+    for (uint32_t i = 0; i < count; i++) {
+        if (list[i] >= operandCount) {
+            LOG(ERROR) << tag << " invalid operand index at " << i << " = " << list[i]
+                       << ", operandCount " << operandCount;
+            return ANEURALNETWORKS_BAD_DATA;
+        }
+    }
+    return ANEURALNETWORKS_NO_ERROR;
+}
+
 static bool validOperandIndexes(const hidl_vec<uint32_t> indexes, size_t operandCount) {
     for (uint32_t i : indexes) {
         if (i >= operandCount) {
diff --git a/common/include/Utils.h b/common/include/Utils.h
index 5bf7975..2b2b362 100644
--- a/common/include/Utils.h
+++ b/common/include/Utils.h
@@ -105,6 +105,9 @@
     return (code < codeCount) || (code >= kOEMCodeBase && (code - kOEMCodeBase) < codeCountOEM);
 }
 
+int validateOperandType(const ANeuralNetworksOperandType& type, const char* tag, bool allowPartial);
+int validateOperandList(uint32_t count, const uint32_t* list, uint32_t operandCount,
+                        const char* tag);
 bool validateModel(const Model& model);
 bool validateRequest(const Request& request, const Model& model);
 
diff --git a/runtime/CompilationBuilder.cpp b/runtime/CompilationBuilder.cpp
index 71b5e03..bb2a63d 100644
--- a/runtime/CompilationBuilder.cpp
+++ b/runtime/CompilationBuilder.cpp
@@ -36,6 +36,7 @@
         LOG(ERROR) << "ANeuralNetworksCompilation_finish called more than once";
         return ANEURALNETWORKS_BAD_STATE;
     }
+    // TODO validate the rest
 
     mFinished = true;
 
@@ -55,6 +56,11 @@
                 "ANeuralNetworksCompilation_setPreference can't modify after compilation finished";
         return ANEURALNETWORKS_BAD_STATE;
     }
+    if (preference >= kNumberOfPreferences) {
+        LOG(ERROR) << "ANeuralNetworksCompilation_setPreference invalid preference " << preference;
+        return ANEURALNETWORKS_BAD_DATA;
+    }
+
     mPreference = preference;
     return ANEURALNETWORKS_NO_ERROR;
 }
diff --git a/runtime/ExecutionBuilder.cpp b/runtime/ExecutionBuilder.cpp
index a11239f..bf5f4d9 100644
--- a/runtime/ExecutionBuilder.cpp
+++ b/runtime/ExecutionBuilder.cpp
@@ -89,18 +89,29 @@
 }
 
 int ExecutionBuilder::setInput(uint32_t index, const ANeuralNetworksOperandType* type,
-                               const void* buffer, uint32_t length) {
+                               const void* buffer, size_t length) {
     uint32_t count = static_cast<uint32_t>(mInputs.size());
     if (index >= count) {
         LOG(ERROR) << "ANeuralNetworksExecution_setInput bad index " << index << " " << count;
         return ANEURALNETWORKS_BAD_DATA;
     }
+    if (type != nullptr) {
+        int n = validateOperandType(*type, "ANeuralNetworksExecution_setInput", false);
+        if (n != ANEURALNETWORKS_NO_ERROR) {
+            return n;
+        }
+    }
+    if (length > 0xFFFFFFFF) {
+        LOG(ERROR) << "ANeuralNetworksExecution_setInput input exceeds max length " << length;
+        return ANEURALNETWORKS_BAD_DATA;
+    }
+    uint32_t l = static_cast<uint32_t>(length);
     return mInputs[index].setFromPointer(mModel->getInputOperand(index), type,
-                                         const_cast<void*>(buffer), length);
+                                         const_cast<void*>(buffer), l);
 }
 
 int ExecutionBuilder::setInputFromMemory(uint32_t index, const ANeuralNetworksOperandType* type,
-                                         const Memory* memory, uint32_t offset, uint32_t length) {
+                                         const Memory* memory, size_t offset, size_t length) {
     uint32_t count = static_cast<uint32_t>(mInputs.size());
     if (index >= count) {
         LOG(ERROR) << "ANeuralNetworksExecution_setInputFromMemory bad index " << index << " "
@@ -110,23 +121,35 @@
     if (!memory->validateSize(offset, length)) {
         return ANEURALNETWORKS_BAD_DATA;
     }
+    // TODO validate the rest
     uint32_t poolIndex = mMemories.add(memory);
     return mInputs[index].setFromMemory(mModel->getInputOperand(index), type, poolIndex, offset,
                                         length);
 }
 
 int ExecutionBuilder::setOutput(uint32_t index, const ANeuralNetworksOperandType* type, void* buffer,
-                                uint32_t length) {
+                                size_t length) {
     uint32_t count = static_cast<uint32_t>(mOutputs.size());
     if (index >= count) {
         LOG(ERROR) << "ANeuralNetworksExecution_setOutput bad index " << index << " " << count;
         return ANEURALNETWORKS_BAD_DATA;
     }
-    return mOutputs[index].setFromPointer(mModel->getOutputOperand(index), type, buffer, length);
+    if (type != nullptr) {
+        int n = validateOperandType(*type, "ANeuralNetworksExecution_setOutput", false);
+        if (n != ANEURALNETWORKS_NO_ERROR) {
+            return n;
+        }
+    }
+    if (length > 0xFFFFFFFF) {
+        LOG(ERROR) << "ANeuralNetworksExecution_setOutput input exceeds max length " << length;
+        return ANEURALNETWORKS_BAD_DATA;
+    }
+    uint32_t l = static_cast<uint32_t>(length);
+    return mOutputs[index].setFromPointer(mModel->getOutputOperand(index), type, buffer, l);
 }
 
 int ExecutionBuilder::setOutputFromMemory(uint32_t index, const ANeuralNetworksOperandType* type,
-                                          const Memory* memory, uint32_t offset, uint32_t length) {
+                                          const Memory* memory, size_t offset, size_t length) {
     uint32_t count = static_cast<uint32_t>(mOutputs.size());
     if (index >= count) {
         LOG(ERROR) << "ANeuralNetworksExecution_setOutputFromMemory bad index " << index << " "
@@ -136,6 +159,7 @@
     if (!memory->validateSize(offset, length)) {
         return ANEURALNETWORKS_BAD_DATA;
     }
+    // TODO validate the rest
     uint32_t poolIndex = mMemories.add(memory);
     return mOutputs[index].setFromMemory(mModel->getOutputOperand(index), type, poolIndex, offset,
                                          length);
diff --git a/runtime/ExecutionBuilder.h b/runtime/ExecutionBuilder.h
index b4f086a..9522798 100644
--- a/runtime/ExecutionBuilder.h
+++ b/runtime/ExecutionBuilder.h
@@ -61,13 +61,13 @@
     ExecutionBuilder(const CompilationBuilder* compilation);
 
     int setInput(uint32_t index, const ANeuralNetworksOperandType* type, const void* buffer,
-                 uint32_t length);
+                 size_t length);
     int setInputFromMemory(uint32_t index, const ANeuralNetworksOperandType* type,
-                           const Memory* memory, uint32_t offset, uint32_t length);
+                           const Memory* memory, size_t offset, size_t length);
     int setOutput(uint32_t index, const ANeuralNetworksOperandType* type, void* buffer,
-                  uint32_t length);
+                  size_t length);
     int setOutputFromMemory(uint32_t index, const ANeuralNetworksOperandType* type,
-                            const Memory* memory, uint32_t offset, uint32_t length);
+                            const Memory* memory, size_t offset, size_t length);
     int startCompute(sp<Event>* event);
 
 private:
diff --git a/runtime/Memory.cpp b/runtime/Memory.cpp
index f1fb124..9b05dbf 100644
--- a/runtime/Memory.cpp
+++ b/runtime/Memory.cpp
@@ -34,6 +34,80 @@
     return ANEURALNETWORKS_NO_ERROR;
 }
 
+bool Memory::validateSize(uint32_t offset, uint32_t length) const {
+    if (offset + length > mHidlMemory.size()) {
+        LOG(ERROR) << "Request size larger than the memory size.";
+        return false;
+    } else {
+        return true;
+    }
+}
+
+MemoryFd::~MemoryFd() {
+    // Delete the native_handle.
+    if (mHandle) {
+        int fd = mHandle->data[0];
+        if (fd != -1) {
+            close(fd);
+        }
+        native_handle_delete(mHandle);
+    }
+}
+
+int MemoryFd::set(size_t size, int prot, int fd, size_t offset) {
+    if (fd < 0) {
+        LOG(ERROR) << "ANeuralNetworksMemory_createFromFd invalid fd " << fd;
+        return ANEURALNETWORKS_UNEXPECTED_NULL;
+    }
+    if (size == 0 || fd < 0) {
+        LOG(ERROR) << "Invalid size or fd";
+        return ANEURALNETWORKS_BAD_DATA;
+    }
+    int dupfd = dup(fd);
+    if (dupfd == -1) {
+        LOG(ERROR) << "Failed to dup the fd";
+        return ANEURALNETWORKS_UNEXPECTED_NULL;
+    }
+
+    if (mHandle) {
+        native_handle_delete(mHandle);
+    }
+    mHandle = native_handle_create(1, 3);
+    if (mHandle == nullptr) {
+        LOG(ERROR) << "Failed to create native_handle";
+        return ANEURALNETWORKS_UNEXPECTED_NULL;
+    }
+    mHandle->data[0] = dupfd;
+    mHandle->data[1] = prot;
+    mHandle->data[2] = (int32_t)(uint32_t)(offset & 0xffffffff);
+#if defined(__LP64__)
+    mHandle->data[3] = (int32_t)(uint32_t)(offset >> 32);
+#else
+    mHandle->data[3] = 0;
+#endif
+    mHidlMemory = hidl_memory("mmap_fd", mHandle, size);
+    return ANEURALNETWORKS_NO_ERROR;
+}
+
+int MemoryFd::getPointer(uint8_t** buffer) const {
+    if (mHandle == nullptr) {
+        LOG(ERROR) << "Memory not initialized";
+        return ANEURALNETWORKS_UNEXPECTED_NULL;
+    }
+
+    int fd = mHandle->data[0];
+    int prot = mHandle->data[1];
+    size_t offset = getSizeFromInts(mHandle->data[2], mHandle->data[3]);
+    void* data = mmap(nullptr, mHidlMemory.size(), prot, MAP_SHARED, fd, offset);
+    if (data == MAP_FAILED) {
+        LOG(ERROR) << "Can't mmap the file descriptor.";
+        return ANEURALNETWORKS_UNMAPPABLE;
+    } else {
+        *buffer = static_cast<uint8_t*>(data);
+        return ANEURALNETWORKS_NO_ERROR;
+    }
+}
+
 uint32_t MemoryTracker::add(const Memory* memory) {
     // See if we already have this memory. If so,
     // return its index.
diff --git a/runtime/Memory.h b/runtime/Memory.h
index fd72df9..77e81ce 100644
--- a/runtime/Memory.h
+++ b/runtime/Memory.h
@@ -52,14 +52,7 @@
         return ANEURALNETWORKS_NO_ERROR;
     }
 
-    virtual bool validateSize(uint32_t offset, uint32_t length) const {
-        if (offset + length > mHidlMemory.size()) {
-            LOG(ERROR) << "Request size larger than the memory size.";
-            return false;
-        } else {
-            return true;
-        }
-    }
+    virtual bool validateSize(uint32_t offset, uint32_t length) const;
 protected:
     // The hidl_memory handle for this shared memory.  We will pass this value when
     // communicating with the drivers.
@@ -70,16 +63,7 @@
 class MemoryFd : public Memory {
 public:
     MemoryFd() {}
-    ~MemoryFd() {
-        // Delete the native_handle.
-        if (mHandle) {
-            int fd = mHandle->data[0];
-            if (fd != -1) {
-                close(fd);
-            }
-            native_handle_delete(mHandle);
-        }
-    }
+    ~MemoryFd();
 
     // Disallow copy semantics to ensure the runtime object can only be freed
     // once. Copy semantics could be enabled if some sort of reference counting
@@ -90,55 +74,9 @@
     // Create the native_handle based on input size, prot, and fd.
     // Existing native_handle will be deleted, and mHidlMemory will wrap
     // the newly created native_handle.
-    int set(size_t size, int prot, int fd, size_t offset) {
-        if (size == 0 || fd < 0) {
-            LOG(ERROR) << "Invalid size or fd";
-            return ANEURALNETWORKS_BAD_DATA;
-        }
-        int dupfd = dup(fd);
-        if (dupfd == -1) {
-            LOG(ERROR) << "Failed to dup the fd";
-            return ANEURALNETWORKS_UNEXPECTED_NULL;
-        }
+    int set(size_t size, int prot, int fd, size_t offset);
 
-        if (mHandle) {
-            native_handle_delete(mHandle);
-        }
-        mHandle = native_handle_create(1, 3);
-        if (mHandle == nullptr) {
-            LOG(ERROR) << "Failed to create native_handle";
-            return ANEURALNETWORKS_UNEXPECTED_NULL;
-        }
-        mHandle->data[0] = dupfd;
-        mHandle->data[1] = prot;
-        mHandle->data[2] = (int32_t)(uint32_t)(offset & 0xffffffff);
-#if defined(__LP64__)
-        mHandle->data[3] = (int32_t)(uint32_t)(offset >> 32);
-#else
-        mHandle->data[3] = 0;
-#endif
-        mHidlMemory = hidl_memory("mmap_fd", mHandle, size);
-        return ANEURALNETWORKS_NO_ERROR;
-    }
-
-    int getPointer(uint8_t** buffer) const override {
-        if (mHandle == nullptr) {
-            LOG(ERROR) << "Memory not initialized";
-            return ANEURALNETWORKS_UNEXPECTED_NULL;
-        }
-
-        int fd = mHandle->data[0];
-        int prot = mHandle->data[1];
-        size_t offset = getSizeFromInts(mHandle->data[2], mHandle->data[3]);
-        void* data = mmap(nullptr, mHidlMemory.size(), prot, MAP_SHARED, fd, offset);
-        if (data == MAP_FAILED) {
-            LOG(ERROR) << "Can't mmap the file descriptor.";
-            return ANEURALNETWORKS_UNMAPPABLE;
-        } else {
-            *buffer = static_cast<uint8_t*>(data);
-            return ANEURALNETWORKS_NO_ERROR;
-        }
-    }
+    int getPointer(uint8_t** buffer) const override;
 
 private:
     native_handle_t* mHandle = nullptr;
diff --git a/runtime/ModelBuilder.cpp b/runtime/ModelBuilder.cpp
index db83904..27e2ee1 100644
--- a/runtime/ModelBuilder.cpp
+++ b/runtime/ModelBuilder.cpp
@@ -36,6 +36,10 @@
         LOG(ERROR) << "ANeuralNetworksModel_addOperand can't modify after model finished";
         return ANEURALNETWORKS_BAD_DATA;
     }
+    int n = validateOperandType(type, "ANeuralNetworksModel_addOperand", true);
+    if (n != ANEURALNETWORKS_NO_ERROR) {
+        return n;
+    }
     size_t idx = mOperands.size();
     if (idx >= MAX_NUMBER_OF_OPERANDS) {
         LOG(ERROR) << "ANeuralNetworksModel_addOperand exceed max operands";
@@ -104,6 +108,21 @@
         LOG(ERROR) << "ANeuralNetworksModel_addOperation can't modify after model finished";
         return ANEURALNETWORKS_BAD_DATA;
     }
+    if (!validCode(kNumberOfOperationTypes, kNumberOfOperationTypesOEM, type)) {
+        LOG(ERROR) << "ANeuralNetworksModel_addOperation invalid operations type " << type;
+        return ANEURALNETWORKS_BAD_DATA;
+    }
+    int n = validateOperandList(inputCount, inputs, operandCount(),
+                                "ANeuralNetworksModel_addOperation inputs");
+    if (n != ANEURALNETWORKS_NO_ERROR) {
+        return n;
+    }
+    n = validateOperandList(outputCount, outputs, operandCount(),
+                            "ANeuralNetworksModel_addOperation outputs");
+    if (n != ANEURALNETWORKS_NO_ERROR) {
+        return n;
+    }
+
     uint32_t operationIndex = operationCount();
     if (operationIndex >= MAX_NUMBER_OF_OPERATIONS) {
         LOG(ERROR) << "ANeuralNetworksModel_addOperation exceed max operations";
@@ -128,6 +147,16 @@
         LOG(ERROR) << "ANeuralNetworksModel_setInputsAndOutputs can't modify after model finished";
         return ANEURALNETWORKS_BAD_DATA;
     }
+    int n = validateOperandList(inputCount, inputs, operandCount(),
+                                "ANeuralNetworksModel_setInputsAndOutputs inputs");
+    if (n != ANEURALNETWORKS_NO_ERROR) {
+        return n;
+    }
+    n = validateOperandList(outputCount, outputs, operandCount(),
+                            "ANeuralNetworksModel_setInputsAndOutputs outputs");
+    if (n != ANEURALNETWORKS_NO_ERROR) {
+        return n;
+    }
 
     // Makes a copy of the index list, validates the arguments, and changes
     // the lifetime info of the corresponding operand.
diff --git a/runtime/NeuralNetworks.cpp b/runtime/NeuralNetworks.cpp
index d68dec4..1ef9cf1 100644
--- a/runtime/NeuralNetworks.cpp
+++ b/runtime/NeuralNetworks.cpp
@@ -213,54 +213,8 @@
 using android::sp;
 using namespace android::nn;
 
-// Validates the type. The used dimensions can be underspecified.
-static int ValidateOperandType(const ANeuralNetworksOperandType& type, const char* tag,
-                               bool allowPartial) {
-    if (!allowPartial) {
-        for (uint32_t i = 0; i < type.dimensionCount; i++) {
-            if (type.dimensions[i] == 0) {
-                LOG(ERROR) << tag << " OperandType invalid dimensions[" << i
-                           << "] = " << type.dimensions[i];
-                return ANEURALNETWORKS_BAD_DATA;
-            }
-        }
-    }
-    if (!validCode(kNumberOfDataTypes, kNumberOfDataTypesOEM, type.type)) {
-        LOG(ERROR) << tag << " OperandType invalid type " << type.type;
-        return ANEURALNETWORKS_BAD_DATA;
-    }
-    /* TODO validate the quantization info.
-    if (type.offset != 0.f && type.scale == 0.f) {
-        LOG(ERROR) << ("%s OperandType invalid offset %f and scale %f", tag, type.offset,
-    type.scale); return ANEURALNETWORKS_BAD_DATA;
-    }
-    if (type.scale != 0.f &&
-        (type.type != ANEURALNETWORKS_FLOAT32)) {
-            LOG(ERROR) << ("%s OperandType scale %f with float type %u", tag, type.scale,
-    type.type); return ANEURALNETWORKS_BAD_DATA;
-        }
-     */
-    return ANEURALNETWORKS_NO_ERROR;
-}
-
-static int ValidateOperandList(uint32_t count, const uint32_t* list, uint32_t operandCount,
-                               const char* tag) {
-    for (uint32_t i = 0; i < count; i++) {
-        if (list[i] >= operandCount) {
-            LOG(ERROR) << tag << " invalid operand index at " << i << " = " << list[i]
-                       << ", operandCount " << operandCount;
-            return ANEURALNETWORKS_BAD_DATA;
-        }
-    }
-    return ANEURALNETWORKS_NO_ERROR;
-}
-
 int ANeuralNetworksMemory_createFromFd(size_t size, int prot, int fd, size_t offset,
                                        ANeuralNetworksMemory** memory) {
-    if (fd < 0) {
-        LOG(ERROR) << "ANeuralNetworksMemory_createFromFd invalid fd " << fd;
-        return ANEURALNETWORKS_UNEXPECTED_NULL;
-    }
     *memory = nullptr;
     std::unique_ptr<MemoryFd> m = std::make_unique<MemoryFd>();
     if (m == nullptr) {
@@ -316,10 +270,6 @@
         return ANEURALNETWORKS_UNEXPECTED_NULL;
     }
     ModelBuilder* m = reinterpret_cast<ModelBuilder*>(model);
-    int n = ValidateOperandType(*type, "ANeuralNetworksModel_addOperand", true);
-    if (n != ANEURALNETWORKS_NO_ERROR) {
-        return n;
-    }
     return m->addOperand(*type);
 }
 
@@ -354,21 +304,6 @@
         return ANEURALNETWORKS_UNEXPECTED_NULL;
     }
     ModelBuilder* m = reinterpret_cast<ModelBuilder*>(model);
-    if (!validCode(kNumberOfOperationTypes, kNumberOfOperationTypesOEM, type)) {
-        LOG(ERROR) << "ANeuralNetworksModel_addOperation invalid operations type " << type;
-        return ANEURALNETWORKS_BAD_DATA;
-    }
-    int n = ValidateOperandList(inputCount, inputs, m->operandCount(),
-                                "ANeuralNetworksModel_addOperation inputs");
-    if (n != ANEURALNETWORKS_NO_ERROR) {
-        return n;
-    }
-    n = ValidateOperandList(outputCount, outputs, m->operandCount(),
-                            "ANeuralNetworksModel_addOperation outputs");
-    if (n != ANEURALNETWORKS_NO_ERROR) {
-        return n;
-    }
-
     return m->addOperation(type, inputCount, inputs, outputCount, outputs);
 }
 
@@ -380,17 +315,6 @@
         return ANEURALNETWORKS_UNEXPECTED_NULL;
     }
     ModelBuilder* m = reinterpret_cast<ModelBuilder*>(model);
-    int n = ValidateOperandList(inputCount, inputs, m->operandCount(),
-                                "ANeuralNetworksModel_setInputsAndOutputs inputs");
-    if (n != ANEURALNETWORKS_NO_ERROR) {
-        return n;
-    }
-    n = ValidateOperandList(outputCount, outputs, m->operandCount(),
-                            "ANeuralNetworksModel_setInputsAndOutputs outputs");
-    if (n != ANEURALNETWORKS_NO_ERROR) {
-        return n;
-    }
-
     return m->setInputsAndOutputs(inputCount, inputs, outputCount, outputs);
 }
 
@@ -421,11 +345,6 @@
         LOG(ERROR) << "ANeuralNetworksCompilation_setPreference passed a nullptr";
         return ANEURALNETWORKS_UNEXPECTED_NULL;
     }
-    if (preference >= kNumberOfPreferences) {
-        LOG(ERROR) << "ANeuralNetworksCompilation_setPreference invalid preference " << preference;
-        return ANEURALNETWORKS_BAD_DATA;
-    }
-
     CompilationBuilder* c = reinterpret_cast<CompilationBuilder*>(compilation);
     return c->setPreference(preference);
 }
@@ -435,8 +354,6 @@
         LOG(ERROR) << "ANeuralNetworksCompilation_finish passed a nullptr";
         return ANEURALNETWORKS_UNEXPECTED_NULL;
     }
-    // TODO validate the rest
-
     CompilationBuilder* c = reinterpret_cast<CompilationBuilder*>(compilation);
     return c->finish();
 }
@@ -470,19 +387,8 @@
         LOG(ERROR) << "ANeuralNetworksExecution_setInput passed a nullptr";
         return ANEURALNETWORKS_UNEXPECTED_NULL;
     }
-    if (type != nullptr) {
-        int n = ValidateOperandType(*type, "ANeuralNetworksExecution_setInput", false);
-        if (n != ANEURALNETWORKS_NO_ERROR) {
-            return n;
-        }
-    }
-    if (length > 0xFFFFFFFF) {
-        LOG(ERROR) << "ANeuralNetworksExecution_setInput input exceeds max length " << length;
-        return ANEURALNETWORKS_BAD_DATA;
-    }
-    uint32_t l = static_cast<uint32_t>(length);
     ExecutionBuilder* r = reinterpret_cast<ExecutionBuilder*>(execution);
-    return r->setInput(index, type, buffer, l);
+    return r->setInput(index, type, buffer, length);
 }
 
 int ANeuralNetworksExecution_setInputFromMemory(ANeuralNetworksExecution* execution, int32_t index,
@@ -493,7 +399,6 @@
         LOG(ERROR) << "ANeuralNetworksExecution_setInputFromMemory passed a nullptr";
         return ANEURALNETWORKS_UNEXPECTED_NULL;
     }
-    // TODO validate the rest
 
     const Memory* m = reinterpret_cast<const Memory*>(memory);
     ExecutionBuilder* r = reinterpret_cast<ExecutionBuilder*>(execution);
@@ -507,20 +412,8 @@
         LOG(ERROR) << "ANeuralNetworksExecution_setOutput passed a nullptr";
         return ANEURALNETWORKS_UNEXPECTED_NULL;
     }
-    if (type != nullptr) {
-        int n = ValidateOperandType(*type, "ANeuralNetworksExecution_setOutput", false);
-        if (n != ANEURALNETWORKS_NO_ERROR) {
-            return n;
-        }
-    }
-    if (length > 0xFFFFFFFF) {
-        LOG(ERROR) << "ANeuralNetworksExecution_setOutput input exceeds max length " << length;
-        return ANEURALNETWORKS_BAD_DATA;
-    }
-    uint32_t l = static_cast<uint32_t>(length);
-
     ExecutionBuilder* r = reinterpret_cast<ExecutionBuilder*>(execution);
-    return r->setOutput(index, type, buffer, l);
+    return r->setOutput(index, type, buffer, length);
 }
 
 int ANeuralNetworksExecution_setOutputFromMemory(ANeuralNetworksExecution* execution, int32_t index,
@@ -531,7 +424,6 @@
         LOG(ERROR) << "ANeuralNetworksExecution_setOutputFromMemory passed a nullptr";
         return ANEURALNETWORKS_UNEXPECTED_NULL;
     }
-    // TODO validate the rest
 
     ExecutionBuilder* r = reinterpret_cast<ExecutionBuilder*>(execution);
     const Memory* m = reinterpret_cast<const Memory*>(memory);