From 19e9d1a8802443203df895733ff5fd7a47d1d45f Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EC=98=A4=ED=98=95=EC=84=9D/=EB=8F=99=EC=9E=91=EC=A0=9C?= =?utf8?q?=EC=96=B4Lab=28SR=29/Staff=20Engineer/=EC=82=BC=EC=84=B1?= =?utf8?q?=EC=A0=84=EC=9E=90?= Date: Fri, 3 Aug 2018 13:37:45 +0900 Subject: [PATCH] [neurun] NNAPI frontend exception handling by nullptr (#2157) Handle exception by nullptr parameter in NNAPI frontend implementation Signed-off-by: Hyeongseok Oh --- runtimes/neurun/src/frontend/compilation.cc | 22 +++++++++++- runtimes/neurun/src/frontend/event.cc | 10 +++++- runtimes/neurun/src/frontend/execution.cc | 54 ++++++++++++++++++++++++++-- runtimes/neurun/src/frontend/memory.cc | 12 ++++++- runtimes/neurun/src/frontend/model.cc | 56 +++++++++++++++++++++++++++-- 5 files changed, 147 insertions(+), 7 deletions(-) diff --git a/runtimes/neurun/src/frontend/compilation.cc b/runtimes/neurun/src/frontend/compilation.cc index 794fbed..f8b9a57 100644 --- a/runtimes/neurun/src/frontend/compilation.cc +++ b/runtimes/neurun/src/frontend/compilation.cc @@ -9,17 +9,32 @@ int ANeuralNetworksCompilation_create(ANeuralNetworksModel *model, ANeuralNetworksCompilation **compilation) { + if ((model == nullptr) || (compilation == nullptr)) + { + return ANEURALNETWORKS_UNEXPECTED_NULL; + } + std::shared_ptr internal; model->release(internal); - *compilation = new ANeuralNetworksCompilation(internal); + ANeuralNetworksCompilation *compilation_ptr = new ANeuralNetworksCompilation(internal); + if (compilation_ptr == nullptr) + { + return ANEURALNETWORKS_OUT_OF_MEMORY; + } + *compilation = compilation_ptr; return ANEURALNETWORKS_NO_ERROR; } int ANeuralNetworksCompilation_finish(ANeuralNetworksCompilation *compilation) { + if (compilation == nullptr) + { + return ANEURALNETWORKS_UNEXPECTED_NULL; + } + return compilation->finish(); } @@ -31,6 +46,11 @@ void ANeuralNetworksCompilation_free(ANeuralNetworksCompilation *compilation) int ANeuralNetworksCompilation_setPreference(ANeuralNetworksCompilation *compilation, int32_t preference) { + if (compilation == nullptr) + { + return ANEURALNETWORKS_UNEXPECTED_NULL; + } + // NYi return ANEURALNETWORKS_NO_ERROR; } diff --git a/runtimes/neurun/src/frontend/event.cc b/runtimes/neurun/src/frontend/event.cc index 9a406e1..47d77ca 100644 --- a/runtimes/neurun/src/frontend/event.cc +++ b/runtimes/neurun/src/frontend/event.cc @@ -2,6 +2,14 @@ #include "event.h" -int ANeuralNetworksEvent_wait(ANeuralNetworksEvent *event) { return ANEURALNETWORKS_NO_ERROR; } +int ANeuralNetworksEvent_wait(ANeuralNetworksEvent *event) +{ + if (event == nullptr) + { + return ANEURALNETWORKS_UNEXPECTED_NULL; + } + + return ANEURALNETWORKS_NO_ERROR; +} void ANeuralNetworksEvent_free(ANeuralNetworksEvent *event) { delete event; } diff --git a/runtimes/neurun/src/frontend/execution.cc b/runtimes/neurun/src/frontend/execution.cc index 4862938..04a7210 100644 --- a/runtimes/neurun/src/frontend/execution.cc +++ b/runtimes/neurun/src/frontend/execution.cc @@ -2,6 +2,7 @@ #include "compilation.h" #include "execution.h" +#include "event.h" #include "internal/Source.h" @@ -11,11 +12,21 @@ int ANeuralNetworksExecution_create(ANeuralNetworksCompilation *compilation, ANeuralNetworksExecution **execution) { + if ((compilation == nullptr) || (execution == nullptr)) + { + return ANEURALNETWORKS_UNEXPECTED_NULL; + } + std::shared_ptr plan; compilation->publish(plan); - *execution = new ANeuralNetworksExecution{plan}; + ANeuralNetworksExecution *execution_ptr = new ANeuralNetworksExecution{plan}; + if (execution_ptr == nullptr) + { + return ANEURALNETWORKS_OUT_OF_MEMORY; + } + *execution = execution_ptr; return ANEURALNETWORKS_NO_ERROR; } @@ -24,6 +35,15 @@ int ANeuralNetworksExecution_setInput(ANeuralNetworksExecution *execution, int32 const ANeuralNetworksOperandType *type, const void *buffer, size_t length) { + // Don't check type + // Comment about ANeuralNetworksOperandType in NeuralNetworks.h: + // If the input or output is optional and omitted then it need not have a fully specified tensor + // operand type + if ((execution == nullptr) || ((buffer == nullptr) && (length != 0))) + { + return ANEURALNETWORKS_UNEXPECTED_NULL; + } + const auto &operands = execution->plan().model().operands(); // TODO Check type conflicts @@ -59,6 +79,15 @@ int ANeuralNetworksExecution_setOutput(ANeuralNetworksExecution *execution, int3 const ANeuralNetworksOperandType *type, void *buffer, size_t length) { + // Don't check type + // Comment about ANeuralNetworksOperandType in NeuralNetworks.h: + // If the input or output is optional and omitted then it need not have a fully specified tensor + // operand type + if ((execution == nullptr) || ((buffer == nullptr) && (length != 0))) + { + return ANEURALNETWORKS_UNEXPECTED_NULL; + } + const auto &operands = execution->plan().model().operands(); // TODO Check type conflicts @@ -92,7 +121,18 @@ int ANeuralNetworksExecution_setOutput(ANeuralNetworksExecution *execution, int3 int ANeuralNetworksExecution_startCompute(ANeuralNetworksExecution *execution, ANeuralNetworksEvent **event) { - assert(execution != nullptr); + if ((execution == nullptr) || (event == nullptr)) + { + return ANEURALNETWORKS_UNEXPECTED_NULL; + } + + // TODO: Handle event + ANeuralNetworksEvent *event_ptr = new ANeuralNetworksEvent{}; + if (event_ptr == nullptr) + { + return ANEURALNETWORKS_OUT_OF_MEMORY; + } + *event = event_ptr; const auto &plan = execution->plan(); const auto &model = plan.model(); @@ -130,6 +170,11 @@ int ANeuralNetworksExecution_setInputFromMemory(ANeuralNetworksExecution *execut const ANeuralNetworksMemory *memory, size_t offset, size_t length) { + if ((execution == nullptr) || (memory == nullptr)) + { + return ANEURALNETWORKS_UNEXPECTED_NULL; + } + // NYI return ANEURALNETWORKS_NO_ERROR; } @@ -139,6 +184,11 @@ int ANeuralNetworksExecution_setOutputFromMemory(ANeuralNetworksExecution *execu const ANeuralNetworksMemory *memory, size_t offset, size_t length) { + if ((execution == nullptr) || (memory == nullptr)) + { + return ANEURALNETWORKS_UNEXPECTED_NULL; + } + // NYI return ANEURALNETWORKS_NO_ERROR; } diff --git a/runtimes/neurun/src/frontend/memory.cc b/runtimes/neurun/src/frontend/memory.cc index 9e3dea9..cf2177d 100644 --- a/runtimes/neurun/src/frontend/memory.cc +++ b/runtimes/neurun/src/frontend/memory.cc @@ -6,7 +6,17 @@ int ANeuralNetworksMemory_createFromFd(size_t size, int protect, int fd, size_t offset, ANeuralNetworksMemory **memory) { - *memory = new ANeuralNetworksMemory{size, protect, fd, offset}; + if (memory == nullptr) + { + return ANEURALNETWORKS_UNEXPECTED_NULL; + } + + ANeuralNetworksMemory *memory_ptr = new ANeuralNetworksMemory{size, protect, fd, offset}; + if (memory_ptr == nullptr) + { + return ANEURALNETWORKS_OUT_OF_MEMORY; + } + *memory = memory_ptr; return ANEURALNETWORKS_NO_ERROR; } diff --git a/runtimes/neurun/src/frontend/model.cc b/runtimes/neurun/src/frontend/model.cc index 8135e06..8debb4c 100644 --- a/runtimes/neurun/src/frontend/model.cc +++ b/runtimes/neurun/src/frontend/model.cc @@ -9,7 +9,19 @@ int ANeuralNetworksModel_create(ANeuralNetworksModel **model) { - *model = new ANeuralNetworksModel{}; + if (model == nullptr) + { + return ANEURALNETWORKS_UNEXPECTED_NULL; + } + + ANeuralNetworksModel *model_ptr = new ANeuralNetworksModel{}; + + if (model_ptr == nullptr) + { + return ANEURALNETWORKS_OUT_OF_MEMORY; + } + + *model = model_ptr; return ANEURALNETWORKS_NO_ERROR; } @@ -19,6 +31,11 @@ void ANeuralNetworksModel_free(ANeuralNetworksModel *model) { delete model; } int ANeuralNetworksModel_addOperand(ANeuralNetworksModel *model, const ANeuralNetworksOperandType *type) { + if ((model == nullptr) || (type == nullptr)) + { + return ANEURALNETWORKS_UNEXPECTED_NULL; + } + // ASSUME A tensor operand always consists of fp32 values // NOTE We do not care about scala operands. assert(!(type->dimensionCount > 1) || (type->type == 3 /* ANEURALNETWORKS_TENSOR_FLOAT32 */)); @@ -44,6 +61,11 @@ int ANeuralNetworksModel_addOperand(ANeuralNetworksModel *model, int ANeuralNetworksModel_setOperandValue(ANeuralNetworksModel *model, int32_t index, const void *buffer, size_t length) { + if ((model == nullptr) || ((buffer == nullptr) && (length != 0))) + { + return ANEURALNETWORKS_UNEXPECTED_NULL; + } + const internal::tflite::operand::Index ind{index}; auto &obj = model->deref().operands().at(ind); @@ -58,6 +80,11 @@ int ANeuralNetworksModel_setOperandValueFromMemory(ANeuralNetworksModel *model, const ANeuralNetworksMemory *memory, size_t offset, size_t length) { + if ((model == nullptr) || (memory == nullptr)) + { + return ANEURALNETWORKS_UNEXPECTED_NULL; + } + const internal::tflite::operand::Index ind{index}; auto &obj = model->deref().operands().at(ind); @@ -73,6 +100,12 @@ int ANeuralNetworksModel_addOperation(ANeuralNetworksModel *model, const uint32_t *inputs, uint32_t outputCount, const uint32_t *outputs) { + if ((model == nullptr) || ((inputs == nullptr) && (inputCount != 0)) || + ((outputs == nullptr) && (outputCount != 0))) + { + return ANEURALNETWORKS_UNEXPECTED_NULL; + } + switch (type) { case ANEURALNETWORKS_CONV_2D: @@ -210,6 +243,12 @@ int ANeuralNetworksModel_addOperationEx(ANeuralNetworksModel *model, const uint32_t *inputs, uint32_t outputCount, const uint32_t *outputs) { + if ((model == nullptr) || ((inputs == nullptr) && (inputCount != 0)) || + ((outputs == nullptr) && (outputCount != 0))) + { + return ANEURALNETWORKS_UNEXPECTED_NULL; + } + switch (type) { default: @@ -221,6 +260,11 @@ int ANeuralNetworksModel_identifyInputsAndOutputs(ANeuralNetworksModel *model, u const uint32_t *inputs, uint32_t outputCount, const uint32_t *outputs) { + if ((model == nullptr) || (inputs == nullptr) || (outputs == nullptr)) + { + return ANEURALNETWORKS_UNEXPECTED_NULL; + } + // NOTE ::internal::tflite::operand::Index uses int as its underlying type as various NNAPI // functions such as ANeuralNetworksModel_setOperandValue use int to represent operand index // @@ -243,4 +287,12 @@ int ANeuralNetworksModel_identifyInputsAndOutputs(ANeuralNetworksModel *model, u return ANEURALNETWORKS_NO_ERROR; } -int ANeuralNetworksModel_finish(ANeuralNetworksModel *model) { return ANEURALNETWORKS_NO_ERROR; } +int ANeuralNetworksModel_finish(ANeuralNetworksModel *model) +{ + if (model == nullptr) + { + return ANEURALNETWORKS_UNEXPECTED_NULL; + } + + return ANEURALNETWORKS_NO_ERROR; +} -- 2.7.4