From 50d2573a1ca7095c8c6c6a708f35fcfce94e4972 Mon Sep 17 00:00:00 2001 From: Pawin Vongmasa Date: Fri, 24 Apr 2020 18:59:54 -0700 Subject: [PATCH] BufferPool: Guarantee double-loadability When a library is double loadable, there may be 2 singleton objects in the same process. Using the pid as an identifier is not sufficient. This CL adds a bit to the identifier based on which side the library is loaded. Test: atest CtsMediaTestCases -- \ --module-arg CtsMediaTestCases:size:small Bug: 147147992 Change-Id: If3b52e4491ced58ee71bb55f0fc4c3c17569c0f3 --- media/bufferpool/2.0/AccessorImpl.cpp | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/media/bufferpool/2.0/AccessorImpl.cpp b/media/bufferpool/2.0/AccessorImpl.cpp index 194765637c..cb55b0781d 100644 --- a/media/bufferpool/2.0/AccessorImpl.cpp +++ b/media/bufferpool/2.0/AccessorImpl.cpp @@ -135,7 +135,14 @@ bool contains(std::map> *mapOfSet, T key, U value) { return false; } -uint32_t Accessor::Impl::sSeqId = time(nullptr); +#ifdef __ANDROID_VNDK__ +static constexpr uint32_t kSeqIdVndkBit = 1 << 31; +#else +static constexpr uint32_t kSeqIdVndkBit = 0; +#endif + +static constexpr uint32_t kSeqIdMax = 0x7fffffff; +uint32_t Accessor::Impl::sSeqId = time(nullptr) & kSeqIdMax; Accessor::Impl::Impl( const std::shared_ptr &allocator) @@ -157,7 +164,7 @@ ResultStatus Accessor::Impl::connect( std::lock_guard lock(mBufferPool.mMutex); if (newConnection) { int32_t pid = getpid(); - ConnectionId id = (int64_t)pid << 32 | sSeqId; + ConnectionId id = (int64_t)pid << 32 | sSeqId | kSeqIdVndkBit; status = mBufferPool.mObserver.open(id, statusDescPtr); if (status == ResultStatus::OK) { newConnection->initialize(accessor, id); @@ -167,7 +174,7 @@ ResultStatus Accessor::Impl::connect( mBufferPool.mConnectionIds.insert(id); mBufferPool.mInvalidationChannel.getDesc(invDescPtr); mBufferPool.mInvalidation.onConnect(id, observer); - if (sSeqId == UINT32_MAX) { + if (sSeqId == kSeqIdMax) { sSeqId = 0; } else { ++sSeqId;