aboutsummaryrefslogtreecommitdiff
path: root/Utils.cpp
diff options
context:
space:
mode:
authorMike Kelly <mike.kelly@arm.com>2023-03-08 10:08:20 +0000
committerMike Kelly <mike.kelly@arm.com>2023-03-08 12:13:33 +0000
commitde547168f108ec1494f18b3ab1ea50bd09f370c1 (patch)
treedaecce0d03ee65c674f7b03672deaefb813239c2 /Utils.cpp
parent4f0d5d18dc7e0ac49a7e105564905d48a88161b1 (diff)
downloadandroid-nn-driver-de547168f108ec1494f18b3ab1ea50bd09f370c1.tar.gz
IVGCVSW-7404 Out of bounds detection
* Added test to ensure that all inputs and outputs do not go out of bounds. Signed-off-by: Mike Kelly <mike.kelly@arm.com> Change-Id: Ia97e85f71e46cd2203306243e4dcbc23e0f29ec1
Diffstat (limited to 'Utils.cpp')
-rw-r--r--Utils.cpp65
1 files changed, 64 insertions, 1 deletions
diff --git a/Utils.cpp b/Utils.cpp
index 884bed00..13eb84d5 100644
--- a/Utils.cpp
+++ b/Utils.cpp
@@ -1,5 +1,5 @@
//
-// Copyright © 2017 Arm Ltd and Contributors. All rights reserved.
+// Copyright © 2017-2021,2023 Arm Ltd and Contributors. All rights reserved.
// SPDX-License-Identifier: MIT
//
@@ -767,4 +767,67 @@ void CommitPools(std::vector<::android::nn::RunTimePoolInfo>& memPools)
#endif
}
}
+
+size_t GetSize(const V1_0::Request& request, const V1_0::RequestArgument& requestArgument)
+{
+ return request.pools[requestArgument.location.poolIndex].size();
+}
+
+#ifdef ARMNN_ANDROID_NN_V1_3
+size_t GetSize(const V1_3::Request& request, const V1_0::RequestArgument& requestArgument)
+{
+ if (request.pools[requestArgument.location.poolIndex].getDiscriminator() ==
+ V1_3::Request::MemoryPool::hidl_discriminator::hidlMemory)
+ {
+ return request.pools[requestArgument.location.poolIndex].hidlMemory().size();
+ }
+ else
+ {
+ return 0;
+ }
+}
+#endif
+
+template <typename ErrorStatus, typename Request>
+ErrorStatus ValidateRequestArgument(const Request& request,
+ const armnn::TensorInfo& tensorInfo,
+ const V1_0::RequestArgument& requestArgument,
+ std::string descString)
+{
+ if (requestArgument.location.poolIndex >= request.pools.size())
+ {
+ std::string err = fmt::format("Invalid {} pool at index {} the pool index is greater than the number "
+ "of available pools {}",
+ descString, requestArgument.location.poolIndex, request.pools.size());
+ ALOGE(err.c_str());
+ return ErrorStatus::GENERAL_FAILURE;
+ }
+ const size_t size = GetSize(request, requestArgument);
+ size_t totalLength = tensorInfo.GetNumBytes();
+
+ if (static_cast<size_t>(requestArgument.location.offset) + totalLength > size)
+ {
+ std::string err = fmt::format("Invalid {} pool at index {} the offset {} and length {} are greater "
+ "than the pool size {}", descString, requestArgument.location.poolIndex,
+ requestArgument.location.offset, totalLength, size);
+ ALOGE(err.c_str());
+ return ErrorStatus::GENERAL_FAILURE;
+ }
+ return ErrorStatus::NONE;
+}
+
+template V1_0::ErrorStatus ValidateRequestArgument<V1_0::ErrorStatus, V1_0::Request>(
+ const V1_0::Request& request,
+ const armnn::TensorInfo& tensorInfo,
+ const V1_0::RequestArgument& requestArgument,
+ std::string descString);
+
+#ifdef ARMNN_ANDROID_NN_V1_3
+template V1_3::ErrorStatus ValidateRequestArgument<V1_3::ErrorStatus, V1_3::Request>(
+ const V1_3::Request& request,
+ const armnn::TensorInfo& tensorInfo,
+ const V1_0::RequestArgument& requestArgument,
+ std::string descString);
+#endif
+
} // namespace armnn_driver