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

Commit 2405ce88 authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "resourcemanager: fix a leak with death notifier" into main

parents d48f3506 087e06c6
Loading
Loading
Loading
Loading
+50 −26
Original line number Diff line number Diff line
@@ -53,23 +53,20 @@ class DeathNotifier : public std::enable_shared_from_this<DeathNotifier> {
        std::weak_ptr<DeathNotifier> mDeathNotifier;
    };
public:
    DeathNotifier(const std::shared_ptr<IResourceManagerClient>& client,
    static std::shared_ptr<DeathNotifier> Create(
        const std::shared_ptr<IResourceManagerClient>& client,
        const std::shared_ptr<ResourceManagerService>& service,
        const ClientInfoParcel& clientInfo,
                  AIBinder_DeathRecipient* recipient);
        bool overrideProcessInfo = false);

    DeathNotifier(const std::shared_ptr<IResourceManagerClient>& client,
                  const std::shared_ptr<ResourceManagerService>& service,
                  const ClientInfoParcel& clientInfo);

    virtual ~DeathNotifier() {
        unlink();
    }

    void unlink() {
        if (mClient != nullptr) {
            // Register for the callbacks by linking to death notification.
            AIBinder_unlinkToDeath(mClient->asBinder().get(), mRecipient, mCookie);
            mClient = nullptr;
        }
    }

    // Implement death recipient
    static void BinderDiedCallback(void* cookie);
    static void BinderUnlinkedCallback(void* cookie);
@@ -81,24 +78,34 @@ private:
        // The context gets deleted at BinderUnlinkedCallback.
        mCookie = new BinderDiedContext{.mDeathNotifier = weak_from_this()};
        // Register for the callbacks by linking to death notification.
        AIBinder_linkToDeath(mClient->asBinder().get(), mRecipient, mCookie);
        AIBinder_linkToDeath(mClient->asBinder().get(), mDeathRecipient.get(), mCookie);
    }

    void unlink() {
        if (mClient != nullptr) {
            // Unlink from the death notification.
            AIBinder_unlinkToDeath(mClient->asBinder().get(), mDeathRecipient.get(), mCookie);
            mClient = nullptr;
        }
    }

protected:
    std::shared_ptr<IResourceManagerClient> mClient;
    std::weak_ptr<ResourceManagerService> mService;
    const ClientInfoParcel mClientInfo;
    AIBinder_DeathRecipient* mRecipient;
    BinderDiedContext* mCookie;
    ::ndk::ScopedAIBinder_DeathRecipient mDeathRecipient;
};

DeathNotifier::DeathNotifier(const std::shared_ptr<IResourceManagerClient>& client,
                             const std::shared_ptr<ResourceManagerService>& service,
                             const ClientInfoParcel& clientInfo,
                             AIBinder_DeathRecipient* recipient)
                             const ClientInfoParcel& clientInfo)
    : mClient(client), mService(service), mClientInfo(clientInfo),
      mRecipient(recipient), mCookie(nullptr) {
    link();
      mCookie(nullptr),
      mDeathRecipient(::ndk::ScopedAIBinder_DeathRecipient(
                      AIBinder_DeathRecipient_new(BinderDiedCallback))) {
    // Setting callback notification when DeathRecipient gets deleted.
    AIBinder_DeathRecipient_setOnUnlinked(mDeathRecipient.get(), BinderUnlinkedCallback);
}

//static
@@ -140,9 +147,8 @@ class OverrideProcessInfoDeathNotifier : public DeathNotifier {
public:
    OverrideProcessInfoDeathNotifier(const std::shared_ptr<IResourceManagerClient>& client,
                                     const std::shared_ptr<ResourceManagerService>& service,
                                     const ClientInfoParcel& clientInfo,
                                     AIBinder_DeathRecipient* recipient)
            : DeathNotifier(client, service, clientInfo, recipient) {}
                                     const ClientInfoParcel& clientInfo)
            : DeathNotifier(client, service, clientInfo) {}

    virtual ~OverrideProcessInfoDeathNotifier() {}

@@ -160,6 +166,26 @@ void OverrideProcessInfoDeathNotifier::binderDied() {
    service->removeProcessInfoOverride(mClientInfo.pid);
}

std::shared_ptr<DeathNotifier> DeathNotifier::Create(
    const std::shared_ptr<IResourceManagerClient>& client,
    const std::shared_ptr<ResourceManagerService>& service,
    const ClientInfoParcel& clientInfo,
    bool overrideProcessInfo) {
    std::shared_ptr<DeathNotifier> deathNotifier = nullptr;
    if (overrideProcessInfo) {
        deathNotifier = std::make_shared<OverrideProcessInfoDeathNotifier>(
            client, service, clientInfo);
    } else {
        deathNotifier = std::make_shared<DeathNotifier>(client, service, clientInfo);
    }

    if (deathNotifier) {
        deathNotifier->link();
    }

    return deathNotifier;
}

template <typename T>
static String8 getString(const std::vector<T>& items) {
    String8 itemsStr;
@@ -377,9 +403,7 @@ ResourceManagerService::ResourceManagerService(const sp<ProcessInfoInterface> &p
      mServiceLog(new ServiceLog()),
      mSupportsMultipleSecureCodecs(true),
      mSupportsSecureWithNonSecureCodec(true),
      mCpuBoostCount(0),
      mDeathRecipient(::ndk::ScopedAIBinder_DeathRecipient(
                      AIBinder_DeathRecipient_new(DeathNotifier::BinderDiedCallback))) {
      mCpuBoostCount(0) {
    mSystemCB->noteResetVideo();
    // Create ResourceManagerMetrics that handles all the metrics.
    mResourceManagerMetrics = std::make_unique<ResourceManagerMetrics>(mProcessInfo);
@@ -535,8 +559,8 @@ Status ResourceManagerService::addResource(const ClientInfoParcel& clientInfo,
        }
    }
    if (info.deathNotifier == nullptr && client != nullptr) {
        info.deathNotifier = std::make_shared<DeathNotifier>(
            client, ref<ResourceManagerService>(), clientInfo, mDeathRecipient.get());
        info.deathNotifier = DeathNotifier::Create(
            client, ref<ResourceManagerService>(), clientInfo);
    }
    if (mObserverService != nullptr && !resourceAdded.empty()) {
        mObserverService->onResourceAdded(uid, pid, resourceAdded);
@@ -905,8 +929,8 @@ Status ResourceManagerService::overrideProcessInfo(
                                .uid = 0,
                                .id = 0,
                                .name = "<unknown client>"};
    auto deathNotifier = std::make_shared<OverrideProcessInfoDeathNotifier>(
            client, ref<ResourceManagerService>(), clientInfo, mDeathRecipient.get());
    auto deathNotifier = DeathNotifier::Create(
        client, ref<ResourceManagerService>(), clientInfo, true);

    mProcessInfoOverrideMap.emplace(pid, ProcessInfoOverride{deathNotifier, client});

+0 −1
Original line number Diff line number Diff line
@@ -204,7 +204,6 @@ private:
    bool mSupportsMultipleSecureCodecs;
    bool mSupportsSecureWithNonSecureCodec;
    int32_t mCpuBoostCount;
    ::ndk::ScopedAIBinder_DeathRecipient mDeathRecipient;
    struct ProcessInfoOverride {
        std::shared_ptr<DeathNotifier> deathNotifier = nullptr;
        std::shared_ptr<IResourceManagerClient> client;