summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorXusong Wang <xusongw@google.com>2020-09-28 15:47:53 -0700
committerXusong Wang <xusongw@google.com>2020-10-06 10:29:24 -0700
commitf6c647be620f9fe04e3d5899b7358cfd6b92f770 (patch)
tree83d3992b6fcc6c13bfa8d9051f8788ec47a35ac1
parent3ab5ca5044de7f1f5929cd90147503025f2982e1 (diff)
downloadml-f6c647be620f9fe04e3d5899b7358cfd6b92f770.tar.gz
Fix AHWB handle leak in CpuExecutor when locking fails.
Fixes: 169166682 Test: NNT_static Change-Id: I78ff6f1d8ad454e69601bf69e00f4c7f4e865d2d Merged-In: I78ff6f1d8ad454e69601bf69e00f4c7f4e865d2d (cherry picked from commit 4f8699a89ba93512f835c71c0e503cffee6a2537)
-rw-r--r--nn/common/CpuExecutor.cpp1
-rw-r--r--nn/runtime/test/TestCompliance.cpp6
-rw-r--r--nn/runtime/test/TestMemory.cpp10
-rw-r--r--nn/runtime/test/TestTrivialModel.cpp10
4 files changed, 16 insertions, 11 deletions
diff --git a/nn/common/CpuExecutor.cpp b/nn/common/CpuExecutor.cpp
index 8d23c0a15..5dd41ad8e 100644
--- a/nn/common/CpuExecutor.cpp
+++ b/nn/common/CpuExecutor.cpp
@@ -407,6 +407,7 @@ std::optional<RunTimePoolInfo> RunTimePoolInfo::createFromHidlMemory(
void* gBuffer = nullptr;
status = AHardwareBuffer_lock(hardwareBuffer, usage, -1, nullptr, &gBuffer);
if (status != NO_ERROR) {
+ AHardwareBuffer_release(hardwareBuffer);
LOG(ERROR) << "RunTimePoolInfo Can't lock the AHardwareBuffer. Error: " << status;
return std::nullopt;
}
diff --git a/nn/runtime/test/TestCompliance.cpp b/nn/runtime/test/TestCompliance.cpp
index db5ab4d3e..39ae017fc 100644
--- a/nn/runtime/test/TestCompliance.cpp
+++ b/nn/runtime/test/TestCompliance.cpp
@@ -14,6 +14,7 @@
* limitations under the License.
*/
+#include <android-base/scopeguard.h>
#include <gtest/gtest.h>
#include "GeneratedTestUtils.h"
@@ -145,6 +146,9 @@ TEST_F(ComplianceTest, HardwareBufferModel) {
AHardwareBuffer* buffer = nullptr;
ASSERT_EQ(AHardwareBuffer_allocate(&desc, &buffer), 0);
+ auto allocateGuard =
+ android::base::make_scope_guard([buffer]() { AHardwareBuffer_release(buffer); });
+
test_wrapper::Memory memory(buffer);
ASSERT_TRUE(memory.isValid());
@@ -160,8 +164,6 @@ TEST_F(ComplianceTest, HardwareBufferModel) {
ASSERT_TRUE(model.isValid());
model.finish();
testAvailableSinceV1_2(model);
-
- AHardwareBuffer_release(buffer);
}
TEST_F(ComplianceTest, HardwareBufferRequest) {
diff --git a/nn/runtime/test/TestMemory.cpp b/nn/runtime/test/TestMemory.cpp
index 122bde2b6..b0e147882 100644
--- a/nn/runtime/test/TestMemory.cpp
+++ b/nn/runtime/test/TestMemory.cpp
@@ -16,13 +16,14 @@
#include "TestMemory.h"
-#include "TestNeuralNetworksWrapper.h"
-
+#include <android-base/scopeguard.h>
#include <gtest/gtest.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <unistd.h>
+#include "TestNeuralNetworksWrapper.h"
+
using WrapperCompilation = ::android::nn::test_wrapper::Compilation;
using WrapperExecution = ::android::nn::test_wrapper::Execution;
using WrapperMemory = ::android::nn::test_wrapper::Memory;
@@ -103,6 +104,8 @@ TEST_F(MemoryTest, TestAHardwareBuffer) {
};
AHardwareBuffer* buffer = nullptr;
ASSERT_EQ(AHardwareBuffer_allocate(&desc, &buffer), 0);
+ auto allocateGuard =
+ android::base::make_scope_guard([buffer]() { AHardwareBuffer_release(buffer); });
void* bufferPtr = nullptr;
ASSERT_EQ(AHardwareBuffer_lock(buffer, desc.usage, -1, NULL, &bufferPtr), 0);
@@ -143,8 +146,5 @@ TEST_F(MemoryTest, TestAHardwareBuffer) {
ASSERT_EQ(execution2.setOutput(0, actual, sizeof(Matrix3x4)), WrapperResult::NO_ERROR);
ASSERT_EQ(execution2.compute(), WrapperResult::NO_ERROR);
ASSERT_EQ(CompareMatrices(expected3, actual), 0);
-
- AHardwareBuffer_release(buffer);
- buffer = nullptr;
}
} // end namespace
diff --git a/nn/runtime/test/TestTrivialModel.cpp b/nn/runtime/test/TestTrivialModel.cpp
index 836e61d56..0ccbc57ba 100644
--- a/nn/runtime/test/TestTrivialModel.cpp
+++ b/nn/runtime/test/TestTrivialModel.cpp
@@ -14,11 +14,11 @@
* limitations under the License.
*/
-#include "TestNeuralNetworksWrapper.h"
-
-//#include <android-base/logging.h>
+#include <android-base/scopeguard.h>
#include <gtest/gtest.h>
+#include "TestNeuralNetworksWrapper.h"
+
using namespace android::nn::test_wrapper;
namespace {
@@ -135,6 +135,9 @@ TEST_F(TrivialTest, AddTwoWithHardwareBufferInput) {
};
AHardwareBuffer* matrix1Buffer = nullptr;
ASSERT_EQ(AHardwareBuffer_allocate(&desc, &matrix1Buffer), 0);
+ auto allocateGuard = android::base::make_scope_guard(
+ [matrix1Buffer]() { AHardwareBuffer_release(matrix1Buffer); });
+
Memory matrix1Memory(matrix1Buffer);
ASSERT_TRUE(matrix1Memory.isValid());
@@ -175,7 +178,6 @@ TEST_F(TrivialTest, AddTwoWithHardwareBufferInput) {
}
ASSERT_EQ(CompareMatrices(expected2, actual), 0);
- AHardwareBuffer_release(matrix1Buffer);
}
TEST_F(TrivialTest, AddThree) {