Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 67d55088 authored by Kevin Rocard's avatar Kevin Rocard
Browse files

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: default avatarKevin Rocard <krocard@google.com>
parent fba442a6
Loading
Loading
Loading
Loading
+13 −2
Original line number Diff line number Diff line
@@ -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 @@ class ReadThread : public Thread {
              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 @@ Return<void> StreamIn::prepareForReading(
    }

    // 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 @@ Return<void> StreamIn::prepareForReading(
    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,
+15 −3
Original line number Diff line number Diff line
@@ -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 @@ class WriteThread : public Thread {
              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 @@ Return<void> StreamOut::prepareForWriting(
    }

    // 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 @@ Return<void> StreamOut::prepareForWriting(
    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,