Audio HAL: Detect buffer memory allocation failure
If the requested buffer was too big, memory allocation would fail,
resulting if a audio hal crash (uncatch exception thrown by new).
Properly hadle the failure by retuning INVALID_PARAMETERS in such case.
Bug: 36311550
Test: Run test on target
Change-Id: Ib4170eb6a9f88f9352d0912083b43d600771bb8e
Signed-off-by: Kevin Rocard <krocard@google.com>
diff --git a/audio/2.0/default/StreamIn.cpp b/audio/2.0/default/StreamIn.cpp
index 2745607..97f8307 100644
--- a/audio/2.0/default/StreamIn.cpp
+++ b/audio/2.0/default/StreamIn.cpp
@@ -20,6 +20,7 @@
#include <android/log.h>
#include <hardware/audio.h>
+#include <memory>
#include <utils/Trace.h>
#include "StreamIn.h"
@@ -52,7 +53,11 @@
mDataMQ(dataMQ),
mStatusMQ(statusMQ),
mEfGroup(efGroup),
- mBuffer(new uint8_t[dataMQ->getQuantumCount()]) {
+ mBuffer(nullptr) {
+ }
+ bool init() {
+ mBuffer.reset(new(std::nothrow) uint8_t[mDataMQ->getQuantumCount()]);
+ return mBuffer != nullptr;
}
virtual ~ReadThread() {}
@@ -328,13 +333,18 @@
}
// Create and launch the thread.
- mReadThread = new ReadThread(
+ auto tempReadThread = std::make_unique<ReadThread>(
&mStopReadThread,
mStream,
tempCommandMQ.get(),
tempDataMQ.get(),
tempStatusMQ.get(),
mEfGroup);
+ if (!tempReadThread->init()) {
+ _hidl_cb(Result::INVALID_ARGUMENTS,
+ CommandMQ::Descriptor(), DataMQ::Descriptor(), StatusMQ::Descriptor(), threadInfo);
+ return Void();
+ }
status = mReadThread->run("reader", PRIORITY_URGENT_AUDIO);
if (status != OK) {
ALOGW("failed to start reader thread: %s", strerror(-status));
@@ -346,6 +356,7 @@
mCommandMQ = std::move(tempCommandMQ);
mDataMQ = std::move(tempDataMQ);
mStatusMQ = std::move(tempStatusMQ);
+ mReadThread = tempReadThread.release();
threadInfo.pid = getpid();
threadInfo.tid = mReadThread->getTid();
_hidl_cb(Result::OK,
diff --git a/audio/2.0/default/StreamOut.cpp b/audio/2.0/default/StreamOut.cpp
index 88045a0..cc16a82 100644
--- a/audio/2.0/default/StreamOut.cpp
+++ b/audio/2.0/default/StreamOut.cpp
@@ -18,6 +18,8 @@
//#define LOG_NDEBUG 0
#define ATRACE_TAG ATRACE_TAG_AUDIO
+#include <memory>
+
#include <android/log.h>
#include <hardware/audio.h>
#include <utils/Trace.h>
@@ -50,7 +52,11 @@
mDataMQ(dataMQ),
mStatusMQ(statusMQ),
mEfGroup(efGroup),
- mBuffer(new uint8_t[dataMQ->getQuantumCount()]) {
+ mBuffer(nullptr) {
+ }
+ bool init() {
+ mBuffer.reset(new(std::nothrow) uint8_t[mDataMQ->getQuantumCount()]);
+ return mBuffer != nullptr;
}
virtual ~WriteThread() {}
@@ -311,14 +317,19 @@
}
// Create and launch the thread.
- mWriteThread = new WriteThread(
+ auto tempWriteThread = std::make_unique<WriteThread>(
&mStopWriteThread,
mStream,
tempCommandMQ.get(),
tempDataMQ.get(),
tempStatusMQ.get(),
mEfGroup);
- status = mWriteThread->run("writer", PRIORITY_URGENT_AUDIO);
+ if (!tempWriteThread->init()) {
+ _hidl_cb(Result::INVALID_ARGUMENTS,
+ CommandMQ::Descriptor(), DataMQ::Descriptor(), StatusMQ::Descriptor(), threadInfo);
+ return Void();
+ }
+ status = tempWriteThread->run("writer", PRIORITY_URGENT_AUDIO);
if (status != OK) {
ALOGW("failed to start writer thread: %s", strerror(-status));
_hidl_cb(Result::INVALID_ARGUMENTS,
@@ -329,6 +340,7 @@
mCommandMQ = std::move(tempCommandMQ);
mDataMQ = std::move(tempDataMQ);
mStatusMQ = std::move(tempStatusMQ);
+ mWriteThread = tempWriteThread.release();
threadInfo.pid = getpid();
threadInfo.tid = mWriteThread->getTid();
_hidl_cb(Result::OK,