aboutsummaryrefslogtreecommitdiff
path: root/src/armnnUtils
diff options
context:
space:
mode:
authorDavid Monahan <david.monahan@arm.com>2023-08-29 09:10:50 +0100
committerDavid Monahan <david.monahan@arm.com>2023-09-08 10:55:19 +0000
commit6a1d506cb0619c6bcf8685ada56ddf4507c2f2d4 (patch)
treebe43f5514cf38e06b6dae01886023915dd302eee /src/armnnUtils
parent564c13dc098eb9353ac15e2609712ab8db9bf350 (diff)
downloadarmnn-6a1d506cb0619c6bcf8685ada56ddf4507c2f2d4.tar.gz
IVGCVSW-7901 Fix unsafe Usages of Memcpy in Armnn
* Updated usages of Memcpy to use proper checks for null instead of asserts * Added error checking in places where none existed Signed-off-by: David Monahan <david.monahan@arm.com> Change-Id: I9529acd966466ba281f88918be2ec372a756e183
Diffstat (limited to 'src/armnnUtils')
-rw-r--r--src/armnnUtils/Permute.cpp59
-rw-r--r--src/armnnUtils/Transpose.cpp59
2 files changed, 92 insertions, 26 deletions
diff --git a/src/armnnUtils/Permute.cpp b/src/armnnUtils/Permute.cpp
index 7d15f3ca5d..19b465ba5d 100644
--- a/src/armnnUtils/Permute.cpp
+++ b/src/armnnUtils/Permute.cpp
@@ -9,7 +9,6 @@
#include "Half.hpp"
-#include <cassert>
#include <cstring>
namespace
@@ -23,7 +22,13 @@ public:
PermuteLoop(const armnn::TensorShape& dstShape, const armnn::PermutationVector& mappings)
: m_DstShape(dstShape)
{
- assert(dstShape.GetNumDimensions() == mappings.GetSize());
+ if (dstShape.GetNumDimensions() != mappings.GetSize())
+ {
+ std::stringstream msg;
+ msg << "Permute: Number of shape dimensions (" << dstShape.GetNumDimensions() <<
+ ") does not match the size of the mappings (" << mappings.GetSize() << ")";
+ throw armnn::InvalidArgumentException(msg.str());
+ }
const size_type numDims = dstShape.GetNumDimensions();
@@ -42,9 +47,18 @@ public:
void Unroll(const void* srcData, void* dstData, size_t dataTypeSize)
{
- assert(srcData);
- assert(dstData);
- assert(dataTypeSize > 0);
+ if (srcData == nullptr)
+ {
+ throw armnn::InvalidArgumentException("Permute: Source Data pointer is null");
+ }
+ if (dstData == nullptr)
+ {
+ throw armnn::InvalidArgumentException("Permute: Destination Data pointer is null");
+ }
+ if (dataTypeSize == 0)
+ {
+ throw armnn::InvalidArgumentException("Permute: dataTypeSize is zero");
+ }
const unsigned char* srcDataPtr = reinterpret_cast<const unsigned char*>(srcData);
unsigned char* dstDataPtr = reinterpret_cast<unsigned char*>(dstData);
@@ -61,13 +75,26 @@ private:
const unsigned char* srcEnd, unsigned char* dstEnd,
size_t dataTypeSize)
{
- assert(srcData);
- assert(dstData);
- assert(srcEnd);
- assert(dstEnd);
- assert(srcData < srcEnd);
- assert(dstData < dstEnd);
- assert(dataTypeSize > 0);
+ if (srcData == nullptr)
+ {
+ throw armnn::InvalidArgumentException("Permute: Source Data pointer is null");
+ }
+ if (dstData == nullptr)
+ {
+ throw armnn::InvalidArgumentException("Permute: Destination Data pointer is null");
+ }
+ if (srcEnd == nullptr)
+ {
+ throw armnn::InvalidArgumentException("Permute: Source End pointer is null");
+ }
+ if (dstEnd == nullptr)
+ {
+ throw armnn::InvalidArgumentException("Permute: Destination End pointer is null");
+ }
+ if (dataTypeSize == 0)
+ {
+ throw armnn::Exception("Permute: dataTypeSize is zero");
+ }
if (dimension >= m_DstShape.GetNumDimensions())
{
@@ -98,7 +125,13 @@ namespace armnnUtils
armnn::TensorShape Permuted(const armnn::TensorShape& srcShape,
const armnn::PermutationVector& mappings)
{
- assert(srcShape.GetNumDimensions() == mappings.GetSize());
+ if (srcShape.GetNumDimensions() != mappings.GetSize())
+ {
+ std::stringstream msg;
+ msg << "Permute: Number of shape dimensions (" << srcShape.GetNumDimensions() <<
+ ") does not match the size of the mappings (" << mappings.GetSize() << ")";
+ throw armnn::InvalidArgumentException(msg.str());
+ }
const unsigned int numDims = mappings.GetSize();
unsigned int outDims[armnn::MaxNumOfTensorDimensions];
diff --git a/src/armnnUtils/Transpose.cpp b/src/armnnUtils/Transpose.cpp
index 3457cacd65..a8e4a1cb81 100644
--- a/src/armnnUtils/Transpose.cpp
+++ b/src/armnnUtils/Transpose.cpp
@@ -9,7 +9,6 @@
#include "Half.hpp"
-#include <cassert>
#include <cstring>
namespace
@@ -23,7 +22,13 @@ public:
TransposeLoop(const armnn::TensorShape& srcShape, const armnn::PermutationVector& mappings)
: m_SrcShape(srcShape)
{
- assert(srcShape.GetNumDimensions() == mappings.GetSize());
+ if (srcShape.GetNumDimensions() != mappings.GetSize())
+ {
+ std::stringstream msg;
+ msg << "Transpose: Number of shape dimensions (" << srcShape.GetNumDimensions() <<
+ ") does not match the size of the mappings (" << mappings.GetSize() << ")";
+ throw armnn::InvalidArgumentException(msg.str());
+ }
const size_type numDims = srcShape.GetNumDimensions();
@@ -42,9 +47,18 @@ public:
void Unroll(const void* srcData, void* dstData, size_t dataTypeSize)
{
- assert(srcData);
- assert(dstData);
- assert(dataTypeSize > 0);
+ if (srcData == nullptr)
+ {
+ throw armnn::Exception("Transpose: Source Data pointer is null");
+ }
+ if (dstData == nullptr)
+ {
+ throw armnn::Exception("Transpose: Destination Data pointer is null");
+ }
+ if (dataTypeSize == 0)
+ {
+ throw armnn::Exception("Transpose: dataTypeSize is zero");
+ }
const unsigned char* srcDataPtr = reinterpret_cast<const unsigned char*>(srcData);
unsigned char* dstDataPtr = reinterpret_cast<unsigned char*>(dstData);
@@ -61,13 +75,26 @@ private:
const unsigned char* srcEnd, unsigned char* dstEnd,
size_t dataTypeSize)
{
- assert(srcData);
- assert(dstData);
- assert(srcEnd);
- assert(dstEnd);
- assert(srcData < srcEnd);
- assert(dstData < dstEnd);
- assert(dataTypeSize > 0);
+ if (srcData == nullptr)
+ {
+ throw armnn::Exception("Transpose: Source Data pointer is null");
+ }
+ if (dstData == nullptr)
+ {
+ throw armnn::Exception("Transpose: Destination Data pointer is null");
+ }
+ if (srcEnd == nullptr)
+ {
+ throw armnn::Exception("Transpose: Source End pointer is null");
+ }
+ if (dstEnd == nullptr)
+ {
+ throw armnn::Exception("Transpose: Destination End is zero");
+ }
+ if (dataTypeSize == 0)
+ {
+ throw armnn::Exception("Transpose: dataTypeSize is invalid");
+ }
if (dimension >= m_SrcShape.GetNumDimensions())
{
@@ -97,7 +124,13 @@ namespace armnnUtils
armnn::TensorShape TransposeTensorShape(const armnn::TensorShape& srcShape, const armnn::PermutationVector& mappings)
{
- assert(srcShape.GetNumDimensions() == mappings.GetSize());
+ if (srcShape.GetNumDimensions() != mappings.GetSize())
+ {
+ std::stringstream msg;
+ msg << "Transpose: Number of shape dimensions (" << srcShape.GetNumDimensions() <<
+ ") does not match the size of the mappings (" << mappings.GetSize() << ")";
+ throw armnn::InvalidArgumentException(msg.str());
+ }
const unsigned int numDims = mappings.GetSize();
unsigned int outDims[armnn::MaxNumOfTensorDimensions];