Merge "media.bufferpool2: Ensure uniqueness of connection id" into main am: e540e3385d

Original change: https://android-review.googlesource.com/c/platform/hardware/interfaces/+/3118258

Change-Id: I15540b657902daa97eb6a2027d85f7921f3991f8
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
Treehugger Robot 2024-06-11 18:37:30 +00:00 committed by Automerger Merge Worker
commit 4e10251c1d
3 changed files with 34 additions and 8 deletions

View file

@ -16,6 +16,8 @@
#define LOG_TAG "AidlBufferPoolAcc" #define LOG_TAG "AidlBufferPoolAcc"
//#define LOG_NDEBUG 0 //#define LOG_NDEBUG 0
#include <android-base/no_destructor.h>
#include <sys/types.h> #include <sys/types.h>
#include <stdint.h> #include <stdint.h>
#include <time.h> #include <time.h>
@ -41,7 +43,25 @@ static constexpr uint32_t kSeqIdVndkBit = 0;
#endif #endif
static constexpr uint32_t kSeqIdMax = 0x7fffffff; static constexpr uint32_t kSeqIdMax = 0x7fffffff;
uint32_t Accessor::sSeqId = time(nullptr) & kSeqIdMax;
Accessor::ConnectionIdGenerator::ConnectionIdGenerator() {
mSeqId = static_cast<uint32_t>(time(nullptr) & kSeqIdMax);
mPid = static_cast<int32_t>(getpid());
}
ConnectionId Accessor::ConnectionIdGenerator::getConnectionId() {
uint32_t seq;
{
std::lock_guard<std::mutex> l(mLock);
seq = mSeqId;
if (mSeqId == kSeqIdMax) {
mSeqId = 0;
} else {
++mSeqId;
}
}
return (int64_t)mPid << 32 | seq | kSeqIdVndkBit;
}
namespace { namespace {
// anonymous namespace // anonymous namespace
@ -239,13 +259,14 @@ BufferPoolStatus Accessor::connect(
uint32_t *pMsgId, uint32_t *pMsgId,
StatusDescriptor* statusDescPtr, StatusDescriptor* statusDescPtr,
InvalidationDescriptor* invDescPtr) { InvalidationDescriptor* invDescPtr) {
static ::android::base::NoDestructor<ConnectionIdGenerator> sConIdGenerator;
std::shared_ptr<Connection> newConnection = ::ndk::SharedRefBase::make<Connection>(); std::shared_ptr<Connection> newConnection = ::ndk::SharedRefBase::make<Connection>();
BufferPoolStatus status = ResultStatus::CRITICAL_ERROR; BufferPoolStatus status = ResultStatus::CRITICAL_ERROR;
{ {
std::lock_guard<std::mutex> lock(mBufferPool.mMutex); std::lock_guard<std::mutex> lock(mBufferPool.mMutex);
if (newConnection) { if (newConnection) {
int32_t pid = getpid(); int32_t pid = getpid();
ConnectionId id = (int64_t)pid << 32 | sSeqId | kSeqIdVndkBit; ConnectionId id = sConIdGenerator->getConnectionId();
status = mBufferPool.mObserver.open(id, statusDescPtr); status = mBufferPool.mObserver.open(id, statusDescPtr);
if (status == ResultStatus::OK) { if (status == ResultStatus::OK) {
newConnection->initialize(ref<Accessor>(), id); newConnection->initialize(ref<Accessor>(), id);
@ -255,11 +276,6 @@ BufferPoolStatus Accessor::connect(
mBufferPool.mConnectionIds.insert(id); mBufferPool.mConnectionIds.insert(id);
mBufferPool.mInvalidationChannel.getDesc(invDescPtr); mBufferPool.mInvalidationChannel.getDesc(invDescPtr);
mBufferPool.mInvalidation.onConnect(id, observer); mBufferPool.mInvalidation.onConnect(id, observer);
if (sSeqId == kSeqIdMax) {
sSeqId = 0;
} else {
++sSeqId;
}
} }
} }

View file

@ -189,7 +189,14 @@ struct Accessor : public BnAccessor {
private: private:
// ConnectionId = pid : (timestamp_created + seqId) // ConnectionId = pid : (timestamp_created + seqId)
// in order to guarantee uniqueness for each connection // in order to guarantee uniqueness for each connection
static uint32_t sSeqId; struct ConnectionIdGenerator {
int32_t mPid;
uint32_t mSeqId;
std::mutex mLock;
ConnectionIdGenerator();
ConnectionId getConnectionId();
};
const std::shared_ptr<BufferPoolAllocator> mAllocator; const std::shared_ptr<BufferPoolAllocator> mAllocator;
nsecs_t mScheduleEvictTs; nsecs_t mScheduleEvictTs;

View file

@ -29,6 +29,9 @@ cc_library {
export_include_dirs: [ export_include_dirs: [
"include", "include",
], ],
header_libs: [
"libbase_headers",
],
shared_libs: [ shared_libs: [
"libbinder_ndk", "libbinder_ndk",
"libcutils", "libcutils",