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

Commit 60caafc5 authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "bufferpool@2.0: Ensure uniqueness of connection id" into main

parents 5a5ab97b f478aa16
Loading
Loading
Loading
Loading
+23 −7
Original line number Original line Diff line number Diff line
@@ -17,6 +17,8 @@
#define LOG_TAG "BufferPoolAccessor2.0"
#define LOG_TAG "BufferPoolAccessor2.0"
//#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>
@@ -147,7 +149,25 @@ static constexpr uint32_t kSeqIdVndkBit = 0;
#endif
#endif


static constexpr uint32_t kSeqIdMax = 0x7fffffff;
static constexpr uint32_t kSeqIdMax = 0x7fffffff;
uint32_t Accessor::Impl::sSeqId = time(nullptr) & kSeqIdMax;

Accessor::Impl::ConnectionIdGenerator::ConnectionIdGenerator() {
    mSeqId = static_cast<uint32_t>(time(nullptr) & kSeqIdMax);
    mPid = static_cast<int32_t>(getpid());
}

ConnectionId Accessor::Impl::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;
}


Accessor::Impl::Impl(
Accessor::Impl::Impl(
        const std::shared_ptr<BufferPoolAllocator> &allocator)
        const std::shared_ptr<BufferPoolAllocator> &allocator)
@@ -163,13 +183,14 @@ ResultStatus Accessor::Impl::connect(
        uint32_t *pMsgId,
        uint32_t *pMsgId,
        const StatusDescriptor** statusDescPtr,
        const StatusDescriptor** statusDescPtr,
        const InvalidationDescriptor** invDescPtr) {
        const InvalidationDescriptor** invDescPtr) {
    static ::android::base::NoDestructor<ConnectionIdGenerator> sConIdGenerator;
    sp<Connection> newConnection = new Connection();
    sp<Connection> newConnection = new Connection();
    ResultStatus status = ResultStatus::CRITICAL_ERROR;
    ResultStatus 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(accessor, id);
                newConnection->initialize(accessor, id);
@@ -179,11 +200,6 @@ ResultStatus Accessor::Impl::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;
                }
            }
            }


        }
        }
+8 −1
Original line number Original line Diff line number Diff line
@@ -77,7 +77,14 @@ public:
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;


+3 −0
Original line number Original line Diff line number Diff line
@@ -21,6 +21,9 @@ cc_defaults {
    export_include_dirs: [
    export_include_dirs: [
        "include",
        "include",
    ],
    ],
    header_libs: [
        "libbase_headers",
    ],
    shared_libs: [
    shared_libs: [
        "libcutils",
        "libcutils",
        "libfmq",
        "libfmq",