Merge changes If8ce9142,Icdbc4fd9
* changes:
Fix / add various ANeuralNetworksModel_* validations
Fix the validation for ANeuralNetworksExecution_setInput & setOutput
diff --git a/nn/common/Utils.cpp b/nn/common/Utils.cpp
index ab08bd4..6a79e5e 100644
--- a/nn/common/Utils.cpp
+++ b/nn/common/Utils.cpp
@@ -18,6 +18,7 @@
#include "Utils.h"
#include "NeuralNetworks.h"
+#include "NeuralNetworksOEM.h"
#include <android-base/logging.h>
#include <android-base/properties.h>
@@ -306,6 +307,16 @@
return ANEURALNETWORKS_BAD_DATA;
}
}
+ if (type.type == ANEURALNETWORKS_FLOAT32 ||
+ type.type == ANEURALNETWORKS_INT32 ||
+ type.type == ANEURALNETWORKS_UINT32 ||
+ type.type == ANEURALNETWORKS_OEM_SCALAR) {
+ 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/nn/runtime/ModelBuilder.cpp b/nn/runtime/ModelBuilder.cpp
index 413259e..3b6434f 100644
--- a/nn/runtime/ModelBuilder.cpp
+++ b/nn/runtime/ModelBuilder.cpp
@@ -34,7 +34,7 @@
int ModelBuilder::addOperand(const ANeuralNetworksOperandType& type) {
if (mCompletedModel) {
LOG(ERROR) << "ANeuralNetworksModel_addOperand can't modify after model finished";
- return ANEURALNETWORKS_BAD_DATA;
+ return ANEURALNETWORKS_BAD_STATE;
}
int n = validateOperandType(type, "ANeuralNetworksModel_addOperand", true);
if (n != ANEURALNETWORKS_NO_ERROR) {
@@ -59,6 +59,11 @@
int ModelBuilder::setOperandValue(uint32_t index, const void* buffer, size_t length) {
VLOG(MODEL) << __func__ << " for operand " << index << " size " << length;
+ if (mCompletedModel) {
+ LOG(ERROR) << "ANeuralNetworksModel_setOperandValue can't modify after model finished";
+ return ANEURALNETWORKS_BAD_STATE;
+ }
+
if (index >= operandCount()) {
LOG(ERROR) << "ANeuralNetworksModel_setOperandValue setting operand " << index << " of "
<< operandCount();
@@ -152,6 +157,11 @@
int ModelBuilder::setOperandValueFromMemory(uint32_t index, const Memory* memory, uint32_t offset,
size_t length) {
VLOG(MODEL) << __func__ << " for operand " << index << " offset " << offset << " size " << length;
+ if (mCompletedModel) {
+ LOG(ERROR) << "ANeuralNetworksModel_setOperandValueFromMemory can't modify after model finished";
+ return ANEURALNETWORKS_BAD_STATE;
+ }
+
if (index >= operandCount()) {
LOG(ERROR) << "ANeuralNetworksModel_setOperandValueFromMemory setting operand " << index
<< " of " << operandCount();
@@ -164,7 +174,9 @@
<< " bytes when needing " << neededLength;
return ANEURALNETWORKS_BAD_DATA;
}
- // TODO validate does not exceed length of memory
+ if (!memory->validateSize(offset, length)) {
+ return ANEURALNETWORKS_BAD_DATA;
+ }
operand.lifetime = OperandLifeTime::CONSTANT_REFERENCE;
operand.location = {
.poolIndex = mMemories.add(memory), .offset = offset, .length = neededLength};
@@ -176,7 +188,7 @@
const uint32_t* outputs) {
if (mCompletedModel) {
LOG(ERROR) << "ANeuralNetworksModel_addOperation can't modify after model finished";
- return ANEURALNETWORKS_BAD_DATA;
+ return ANEURALNETWORKS_BAD_STATE;
}
if (!validCode(kNumberOfOperationTypes, kNumberOfOperationTypesOEM, type)) {
LOG(ERROR) << "ANeuralNetworksModel_addOperation invalid operations type " << type;
@@ -215,7 +227,7 @@
uint32_t outputCount, const uint32_t* outputs) {
if (mCompletedModel) {
LOG(ERROR) << "ANeuralNetworksModel_identifyInputsAndOutputs can't modify after model finished";
- return ANEURALNETWORKS_BAD_DATA;
+ return ANEURALNETWORKS_BAD_STATE;
}
int n = validateOperandList(inputCount, inputs, operandCount(),
"ANeuralNetworksModel_identifyInputsAndOutputs inputs");
diff --git a/nn/runtime/NeuralNetworks.cpp b/nn/runtime/NeuralNetworks.cpp
index 6ee8659..9de91bd 100644
--- a/nn/runtime/NeuralNetworks.cpp
+++ b/nn/runtime/NeuralNetworks.cpp
@@ -428,8 +428,7 @@
int ANeuralNetworksExecution_setInput(ANeuralNetworksExecution* execution, int32_t index,
const ANeuralNetworksOperandType* type, const void* buffer,
size_t length) {
- // TODO: For a non-optional input, also verify that buffer is not null.
- if (!execution) {
+ if (!execution || (!buffer && length != 0)) {
LOG(ERROR) << "ANeuralNetworksExecution_setInput passed a nullptr";
return ANEURALNETWORKS_UNEXPECTED_NULL;
}
@@ -454,7 +453,7 @@
int ANeuralNetworksExecution_setOutput(ANeuralNetworksExecution* execution, int32_t index,
const ANeuralNetworksOperandType* type, void* buffer,
size_t length) {
- if (!execution || !buffer) {
+ if (!execution || (!buffer && length != 0)) {
LOG(ERROR) << "ANeuralNetworksExecution_setOutput passed a nullptr";
return ANEURALNETWORKS_UNEXPECTED_NULL;
}