From 5e104e41add57e65f329de38f82b91cc5764ed3f Mon Sep 17 00:00:00 2001 From: Sungtak Lee Date: Fri, 7 Jun 2024 04:46:23 +0000 Subject: [PATCH] media.bufferpool2: Ensure uniqueness of connection id Currently same connection id can be given to different connections if they don't belong to the same bufferpool. Ensure uniqueness of connection id for each connection. Bug: 323793249 Change-Id: I350872e6d60736ea4525d473944c92b5fe3f5f84 --- media/bufferpool/aidl/default/Accessor.cpp | 30 +++++++++++++++++----- media/bufferpool/aidl/default/Accessor.h | 9 ++++++- media/bufferpool/aidl/default/Android.bp | 3 +++ 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/media/bufferpool/aidl/default/Accessor.cpp b/media/bufferpool/aidl/default/Accessor.cpp index 3d206ac78b..423fd84391 100644 --- a/media/bufferpool/aidl/default/Accessor.cpp +++ b/media/bufferpool/aidl/default/Accessor.cpp @@ -16,6 +16,8 @@ #define LOG_TAG "AidlBufferPoolAcc" //#define LOG_NDEBUG 0 +#include + #include #include #include @@ -41,7 +43,25 @@ static constexpr uint32_t kSeqIdVndkBit = 0; #endif static constexpr uint32_t kSeqIdMax = 0x7fffffff; -uint32_t Accessor::sSeqId = time(nullptr) & kSeqIdMax; + +Accessor::ConnectionIdGenerator::ConnectionIdGenerator() { + mSeqId = static_cast(time(nullptr) & kSeqIdMax); + mPid = static_cast(getpid()); +} + +ConnectionId Accessor::ConnectionIdGenerator::getConnectionId() { + uint32_t seq; + { + std::lock_guard l(mLock); + seq = mSeqId; + if (mSeqId == kSeqIdMax) { + mSeqId = 0; + } else { + ++mSeqId; + } + } + return (int64_t)mPid << 32 | seq | kSeqIdVndkBit; +} namespace { // anonymous namespace @@ -239,13 +259,14 @@ BufferPoolStatus Accessor::connect( uint32_t *pMsgId, StatusDescriptor* statusDescPtr, InvalidationDescriptor* invDescPtr) { + static ::android::base::NoDestructor sConIdGenerator; std::shared_ptr newConnection = ::ndk::SharedRefBase::make(); BufferPoolStatus status = ResultStatus::CRITICAL_ERROR; { std::lock_guard lock(mBufferPool.mMutex); if (newConnection) { int32_t pid = getpid(); - ConnectionId id = (int64_t)pid << 32 | sSeqId | kSeqIdVndkBit; + ConnectionId id = sConIdGenerator->getConnectionId(); status = mBufferPool.mObserver.open(id, statusDescPtr); if (status == ResultStatus::OK) { newConnection->initialize(ref(), id); @@ -255,11 +276,6 @@ BufferPoolStatus Accessor::connect( mBufferPool.mConnectionIds.insert(id); mBufferPool.mInvalidationChannel.getDesc(invDescPtr); mBufferPool.mInvalidation.onConnect(id, observer); - if (sSeqId == kSeqIdMax) { - sSeqId = 0; - } else { - ++sSeqId; - } } } diff --git a/media/bufferpool/aidl/default/Accessor.h b/media/bufferpool/aidl/default/Accessor.h index 85e2fa77f5..0ebe347e08 100644 --- a/media/bufferpool/aidl/default/Accessor.h +++ b/media/bufferpool/aidl/default/Accessor.h @@ -189,7 +189,14 @@ struct Accessor : public BnAccessor { private: // ConnectionId = pid : (timestamp_created + seqId) // 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 mAllocator; nsecs_t mScheduleEvictTs; diff --git a/media/bufferpool/aidl/default/Android.bp b/media/bufferpool/aidl/default/Android.bp index 72cd9031e9..3c31446c91 100644 --- a/media/bufferpool/aidl/default/Android.bp +++ b/media/bufferpool/aidl/default/Android.bp @@ -29,6 +29,9 @@ cc_library { export_include_dirs: [ "include", ], + header_libs: [ + "libbase_headers", + ], shared_libs: [ "libbinder_ndk", "libcutils",