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

Commit 6dc67007 authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "Remove static list in libbinder for caching" into main

parents f9a71079 5e1b7e1b
Loading
Loading
Loading
Loading
+13 −8
Original line number Original line Diff line number Diff line
@@ -396,7 +396,7 @@ Status ServiceManager::getService(const std::string& name, sp<IBinder>* outBinde
    SM_PERFETTO_TRACE_FUNC(PERFETTO_TE_PROTO_FIELDS(
    SM_PERFETTO_TRACE_FUNC(PERFETTO_TE_PROTO_FIELDS(
            PERFETTO_TE_PROTO_FIELD_CSTR(kProtoServiceName, name.c_str())));
            PERFETTO_TE_PROTO_FIELD_CSTR(kProtoServiceName, name.c_str())));


    *outBinder = tryGetBinder(name, true);
    *outBinder = tryGetBinder(name, true).service;
    // returns ok regardless of result for legacy reasons
    // returns ok regardless of result for legacy reasons
    return Status::ok();
    return Status::ok();
}
}
@@ -430,13 +430,15 @@ os::Service ServiceManager::tryGetService(const std::string& name, bool startIfN
            return os::Service::make<os::Service::Tag::accessor>(nullptr);
            return os::Service::make<os::Service::Tag::accessor>(nullptr);
        }
        }
        return os::Service::make<os::Service::Tag::accessor>(
        return os::Service::make<os::Service::Tag::accessor>(
                tryGetBinder(*accessorName, startIfNotFound));
                tryGetBinder(*accessorName, startIfNotFound).service);
    } else {
    } else {
        return os::Service::make<os::Service::Tag::binder>(tryGetBinder(name, startIfNotFound));
        return os::Service::make<os::Service::Tag::serviceWithMetadata>(
                tryGetBinder(name, startIfNotFound));
    }
    }
}
}


sp<IBinder> ServiceManager::tryGetBinder(const std::string& name, bool startIfNotFound) {
os::ServiceWithMetadata ServiceManager::tryGetBinder(const std::string& name,
                                                     bool startIfNotFound) {
    SM_PERFETTO_TRACE_FUNC(PERFETTO_TE_PROTO_FIELDS(
    SM_PERFETTO_TRACE_FUNC(PERFETTO_TE_PROTO_FIELDS(
            PERFETTO_TE_PROTO_FIELD_CSTR(kProtoServiceName, name.c_str())));
            PERFETTO_TE_PROTO_FIELD_CSTR(kProtoServiceName, name.c_str())));


@@ -450,13 +452,13 @@ sp<IBinder> ServiceManager::tryGetBinder(const std::string& name, bool startIfNo
        if (!service->allowIsolated && is_multiuser_uid_isolated(ctx.uid)) {
        if (!service->allowIsolated && is_multiuser_uid_isolated(ctx.uid)) {
            LOG(WARNING) << "Isolated app with UID " << ctx.uid << " requested '" << name
            LOG(WARNING) << "Isolated app with UID " << ctx.uid << " requested '" << name
                         << "', but the service is not allowed for isolated apps.";
                         << "', but the service is not allowed for isolated apps.";
            return nullptr;
            return os::ServiceWithMetadata();
        }
        }
        out = service->binder;
        out = service->binder;
    }
    }


    if (!mAccess->canFind(ctx, name)) {
    if (!mAccess->canFind(ctx, name)) {
        return nullptr;
        return os::ServiceWithMetadata();
    }
    }


    if (!out && startIfNotFound) {
    if (!out && startIfNotFound) {
@@ -473,8 +475,11 @@ sp<IBinder> ServiceManager::tryGetBinder(const std::string& name, bool startIfNo
        CHECK(handleServiceClientCallback(2 /* sm + transaction */, name, false));
        CHECK(handleServiceClientCallback(2 /* sm + transaction */, name, false));
        service->guaranteeClient = true;
        service->guaranteeClient = true;
    }
    }

    os::ServiceWithMetadata serviceWithMetadata = os::ServiceWithMetadata();
    return out;
    serviceWithMetadata.service = out;
    serviceWithMetadata.isLazyService =
            service ? service->dumpPriority & FLAG_IS_LAZY_SERVICE : false;
    return serviceWithMetadata;
}
}


bool isValidServiceName(const std::string& name) {
bool isValidServiceName(const std::string& name) {
+1 −1
Original line number Original line Diff line number Diff line
@@ -114,7 +114,7 @@ private:
    void removeClientCallback(const wp<IBinder>& who, ClientCallbackMap::iterator* it);
    void removeClientCallback(const wp<IBinder>& who, ClientCallbackMap::iterator* it);


    os::Service tryGetService(const std::string& name, bool startIfNotFound);
    os::Service tryGetService(const std::string& name, bool startIfNotFound);
    sp<IBinder> tryGetBinder(const std::string& name, bool startIfNotFound);
    os::ServiceWithMetadata tryGetBinder(const std::string& name, bool startIfNotFound);
    binder::Status canAddService(const Access::CallingContext& ctx, const std::string& name,
    binder::Status canAddService(const Access::CallingContext& ctx, const std::string& name,
                                 std::optional<std::string>* accessor);
                                 std::optional<std::string>* accessor);
    binder::Status canFindService(const Access::CallingContext& ctx, const std::string& name,
    binder::Status canFindService(const Access::CallingContext& ctx, const std::string& name,
+12 −7
Original line number Original line Diff line number Diff line
@@ -92,6 +92,11 @@ TEST(AddService, HappyHappy) {
    auto sm = getPermissiveServiceManager();
    auto sm = getPermissiveServiceManager();
    EXPECT_TRUE(sm->addService("foo", getBinder(), false /*allowIsolated*/,
    EXPECT_TRUE(sm->addService("foo", getBinder(), false /*allowIsolated*/,
        IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
        IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());

    EXPECT_TRUE(sm->addService("lazyfoo", getBinder(), false /*allowIsolated*/,
                               IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT |
                                       IServiceManager::FLAG_IS_LAZY_SERVICE)
                        .isOk());
}
}


TEST(AddService, EmptyNameDisallowed) {
TEST(AddService, EmptyNameDisallowed) {
@@ -156,7 +161,7 @@ TEST(AddService, OverwriteExistingService) {


    Service outA;
    Service outA;
    EXPECT_TRUE(sm->getService2("foo", &outA).isOk());
    EXPECT_TRUE(sm->getService2("foo", &outA).isOk());
    EXPECT_EQ(serviceA, outA.get<Service::Tag::binder>());
    EXPECT_EQ(serviceA, outA.get<Service::Tag::serviceWithMetadata>().service);
    sp<IBinder> outBinderA;
    sp<IBinder> outBinderA;
    EXPECT_TRUE(sm->getService("foo", &outBinderA).isOk());
    EXPECT_TRUE(sm->getService("foo", &outBinderA).isOk());
    EXPECT_EQ(serviceA, outBinderA);
    EXPECT_EQ(serviceA, outBinderA);
@@ -168,7 +173,7 @@ TEST(AddService, OverwriteExistingService) {


    Service outB;
    Service outB;
    EXPECT_TRUE(sm->getService2("foo", &outB).isOk());
    EXPECT_TRUE(sm->getService2("foo", &outB).isOk());
    EXPECT_EQ(serviceB, outB.get<Service::Tag::binder>());
    EXPECT_EQ(serviceB, outB.get<Service::Tag::serviceWithMetadata>().service);
    sp<IBinder> outBinderB;
    sp<IBinder> outBinderB;
    EXPECT_TRUE(sm->getService("foo", &outBinderB).isOk());
    EXPECT_TRUE(sm->getService("foo", &outBinderB).isOk());
    EXPECT_EQ(serviceB, outBinderB);
    EXPECT_EQ(serviceB, outBinderB);
@@ -195,7 +200,7 @@ TEST(GetService, HappyHappy) {


    Service out;
    Service out;
    EXPECT_TRUE(sm->getService2("foo", &out).isOk());
    EXPECT_TRUE(sm->getService2("foo", &out).isOk());
    EXPECT_EQ(service, out.get<Service::Tag::binder>());
    EXPECT_EQ(service, out.get<Service::Tag::serviceWithMetadata>().service);
    sp<IBinder> outBinder;
    sp<IBinder> outBinder;
    EXPECT_TRUE(sm->getService("foo", &outBinder).isOk());
    EXPECT_TRUE(sm->getService("foo", &outBinder).isOk());
    EXPECT_EQ(service, outBinder);
    EXPECT_EQ(service, outBinder);
@@ -206,7 +211,7 @@ TEST(GetService, NonExistant) {


    Service out;
    Service out;
    EXPECT_TRUE(sm->getService2("foo", &out).isOk());
    EXPECT_TRUE(sm->getService2("foo", &out).isOk());
    EXPECT_EQ(nullptr, out.get<Service::Tag::binder>());
    EXPECT_EQ(nullptr, out.get<Service::Tag::serviceWithMetadata>().service);
    sp<IBinder> outBinder;
    sp<IBinder> outBinder;
    EXPECT_TRUE(sm->getService("foo", &outBinder).isOk());
    EXPECT_TRUE(sm->getService("foo", &outBinder).isOk());
    EXPECT_EQ(nullptr, outBinder);
    EXPECT_EQ(nullptr, outBinder);
@@ -227,7 +232,7 @@ TEST(GetService, NoPermissionsForGettingService) {
    Service out;
    Service out;
    // returns nullptr but has OK status for legacy compatibility
    // returns nullptr but has OK status for legacy compatibility
    EXPECT_TRUE(sm->getService2("foo", &out).isOk());
    EXPECT_TRUE(sm->getService2("foo", &out).isOk());
    EXPECT_EQ(nullptr, out.get<Service::Tag::binder>());
    EXPECT_EQ(nullptr, out.get<Service::Tag::serviceWithMetadata>().service);
    sp<IBinder> outBinder;
    sp<IBinder> outBinder;
    EXPECT_TRUE(sm->getService("foo", &outBinder).isOk());
    EXPECT_TRUE(sm->getService("foo", &outBinder).isOk());
    EXPECT_EQ(nullptr, outBinder);
    EXPECT_EQ(nullptr, outBinder);
@@ -257,7 +262,7 @@ TEST(GetService, AllowedFromIsolated) {


    Service out;
    Service out;
    EXPECT_TRUE(sm->getService2("foo", &out).isOk());
    EXPECT_TRUE(sm->getService2("foo", &out).isOk());
    EXPECT_EQ(service, out.get<Service::Tag::binder>());
    EXPECT_EQ(service, out.get<Service::Tag::serviceWithMetadata>().service);
    sp<IBinder> outBinder;
    sp<IBinder> outBinder;
    EXPECT_TRUE(sm->getService("foo", &outBinder).isOk());
    EXPECT_TRUE(sm->getService("foo", &outBinder).isOk());
    EXPECT_EQ(service, outBinder);
    EXPECT_EQ(service, outBinder);
@@ -289,7 +294,7 @@ TEST(GetService, NotAllowedFromIsolated) {
    Service out;
    Service out;
    // returns nullptr but has OK status for legacy compatibility
    // returns nullptr but has OK status for legacy compatibility
    EXPECT_TRUE(sm->getService2("foo", &out).isOk());
    EXPECT_TRUE(sm->getService2("foo", &out).isOk());
    EXPECT_EQ(nullptr, out.get<Service::Tag::binder>());
    EXPECT_EQ(nullptr, out.get<Service::Tag::serviceWithMetadata>().service);
    sp<IBinder> outBinder;
    sp<IBinder> outBinder;
    EXPECT_TRUE(sm->getService("foo", &outBinder).isOk());
    EXPECT_TRUE(sm->getService("foo", &outBinder).isOk());
    EXPECT_EQ(nullptr, outBinder);
    EXPECT_EQ(nullptr, outBinder);
+24 −0
Original line number Original line Diff line number Diff line
@@ -455,6 +455,28 @@ cc_library_shared {
    ],
    ],
}
}


soong_config_module_type {
    name: "libbinder_remove_cache_static_list_config",
    module_type: "cc_defaults",
    config_namespace: "libbinder",
    bool_variables: ["release_libbinder_remove_cache_static_list"],
    properties: [
        "cflags",
    ],
}

libbinder_remove_cache_static_list_config {
    name: "libbinder_remove_cache_static_list_flag",
    soong_config_variables: {
        release_libbinder_remove_cache_static_list: {
            cflags: ["-DLIBBINDER_REMOVE_CACHE_STATIC_LIST"],
            conditions_default: {
                cflags: ["-DNO_LIBBINDER_REMOVE_CACHE_STATIC_LIST"],
            },
        },
    },
}

soong_config_module_type {
soong_config_module_type {
    name: "libbinder_client_cache_config",
    name: "libbinder_client_cache_config",
    module_type: "cc_defaults",
    module_type: "cc_defaults",
@@ -504,6 +526,7 @@ cc_defaults {
    defaults: [
    defaults: [
        "libbinder_client_cache_flag",
        "libbinder_client_cache_flag",
        "libbinder_addservice_cache_flag",
        "libbinder_addservice_cache_flag",
        "libbinder_remove_cache_static_list_flag",
    ],
    ],
    srcs: [
    srcs: [
        "BufferedTextOutput.cpp",
        "BufferedTextOutput.cpp",
@@ -826,6 +849,7 @@ filegroup {
        "aidl/android/os/IServiceCallback.aidl",
        "aidl/android/os/IServiceCallback.aidl",
        "aidl/android/os/IServiceManager.aidl",
        "aidl/android/os/IServiceManager.aidl",
        "aidl/android/os/Service.aidl",
        "aidl/android/os/Service.aidl",
        "aidl/android/os/ServiceWithMetadata.aidl",
        "aidl/android/os/ServiceDebugInfo.aidl",
        "aidl/android/os/ServiceDebugInfo.aidl",
    ],
    ],
    path: "aidl",
    path: "aidl",
+39 −12
Original line number Original line Diff line number Diff line
@@ -16,6 +16,7 @@
#include "BackendUnifiedServiceManager.h"
#include "BackendUnifiedServiceManager.h"


#include <android/os/IAccessor.h>
#include <android/os/IAccessor.h>
#include <android/os/IServiceManager.h>
#include <binder/RpcSession.h>
#include <binder/RpcSession.h>


#if defined(__BIONIC__) && !defined(__ANDROID_VNDK__)
#if defined(__BIONIC__) && !defined(__ANDROID_VNDK__)
@@ -36,6 +37,12 @@ constexpr bool kUseCacheInAddService = true;
constexpr bool kUseCacheInAddService = false;
constexpr bool kUseCacheInAddService = false;
#endif
#endif


#ifdef LIBBINDER_REMOVE_CACHE_STATIC_LIST
constexpr bool kRemoveStaticList = true;
#else
constexpr bool kRemoveStaticList = false;
#endif

using AidlServiceManager = android::os::IServiceManager;
using AidlServiceManager = android::os::IServiceManager;
using android::os::IAccessor;
using android::os::IAccessor;
using binder::Status;
using binder::Status;
@@ -110,6 +117,13 @@ static const char* kStaticCachableList[] = {
        // go/keep-sorted end
        // go/keep-sorted end
};
};


os::ServiceWithMetadata createServiceWithMetadata(const sp<IBinder>& service, bool isLazyService) {
    os::ServiceWithMetadata out = os::ServiceWithMetadata();
    out.service = service;
    out.isLazyService = isLazyService;
    return out;
}

bool BinderCacheWithInvalidation::isClientSideCachingEnabled(const std::string& serviceName) {
bool BinderCacheWithInvalidation::isClientSideCachingEnabled(const std::string& serviceName) {
    sp<ProcessState> self = ProcessState::selfOrNull();
    sp<ProcessState> self = ProcessState::selfOrNull();
    if (!self || self->getThreadPoolMaxTotalThreadCount() <= 0) {
    if (!self || self->getThreadPoolMaxTotalThreadCount() <= 0) {
@@ -132,15 +146,21 @@ Status BackendUnifiedServiceManager::updateCache(const std::string& serviceName,
        return Status::ok();
        return Status::ok();
    }
    }


    if (service.getTag() == os::Service::Tag::binder) {
    if (service.getTag() == os::Service::Tag::serviceWithMetadata) {
        return updateCache(serviceName, service.get<os::Service::Tag::binder>());
        auto serviceWithMetadata = service.get<os::Service::Tag::serviceWithMetadata>();
        return updateCache(serviceName, serviceWithMetadata.service,
                           serviceWithMetadata.isLazyService);
    }
    }
    return Status::ok();
    return Status::ok();
}
}


Status BackendUnifiedServiceManager::updateCache(const std::string& serviceName,
Status BackendUnifiedServiceManager::updateCache(const std::string& serviceName,
                                                 const sp<IBinder>& binder) {
                                                 const sp<IBinder>& binder, bool isServiceLazy) {
    std::string traceStr;
    std::string traceStr;
    // Don't cache if service is lazy
    if (kRemoveStaticList && isServiceLazy) {
        return Status::ok();
    }
    if (atrace_is_tag_enabled(ATRACE_TAG_AIDL)) {
    if (atrace_is_tag_enabled(ATRACE_TAG_AIDL)) {
        traceStr = "BinderCacheWithInvalidation::updateCache : " + serviceName;
        traceStr = "BinderCacheWithInvalidation::updateCache : " + serviceName;
    }
    }
@@ -153,7 +173,9 @@ Status BackendUnifiedServiceManager::updateCache(const std::string& serviceName,
        binder::ScopedTrace aidlTrace(ATRACE_TAG_AIDL,
        binder::ScopedTrace aidlTrace(ATRACE_TAG_AIDL,
                                      "BinderCacheWithInvalidation::updateCache failed: "
                                      "BinderCacheWithInvalidation::updateCache failed: "
                                      "isBinderAlive_false");
                                      "isBinderAlive_false");
    } else if (mCacheForGetService->isClientSideCachingEnabled(serviceName)) {
    }
    // If we reach here with kRemoveStaticList=true then we know service isn't lazy
    else if (kRemoveStaticList || mCacheForGetService->isClientSideCachingEnabled(serviceName)) {
        binder::ScopedTrace aidlTrace(ATRACE_TAG_AIDL,
        binder::ScopedTrace aidlTrace(ATRACE_TAG_AIDL,
                                      "BinderCacheWithInvalidation::updateCache successful");
                                      "BinderCacheWithInvalidation::updateCache successful");
        return mCacheForGetService->setItem(serviceName, binder);
        return mCacheForGetService->setItem(serviceName, binder);
@@ -173,7 +195,7 @@ bool BackendUnifiedServiceManager::returnIfCached(const std::string& serviceName
    sp<IBinder> item = mCacheForGetService->getItem(serviceName);
    sp<IBinder> item = mCacheForGetService->getItem(serviceName);
    // TODO(b/363177618): Enable caching for binders which are always null.
    // TODO(b/363177618): Enable caching for binders which are always null.
    if (item != nullptr && item->isBinderAlive()) {
    if (item != nullptr && item->isBinderAlive()) {
        *_out = os::Service::make<os::Service::Tag::binder>(item);
        *_out = createServiceWithMetadata(item, false);
        return true;
        return true;
    }
    }
    return false;
    return false;
@@ -188,7 +210,7 @@ Status BackendUnifiedServiceManager::getService(const ::std::string& name,
                                                sp<IBinder>* _aidl_return) {
                                                sp<IBinder>* _aidl_return) {
    os::Service service;
    os::Service service;
    Status status = getService2(name, &service);
    Status status = getService2(name, &service);
    *_aidl_return = service.get<os::Service::Tag::binder>();
    *_aidl_return = service.get<os::Service::Tag::serviceWithMetadata>().service;
    return status;
    return status;
}
}


@@ -227,14 +249,16 @@ Status BackendUnifiedServiceManager::checkService(const ::std::string& name, os:
Status BackendUnifiedServiceManager::toBinderService(const ::std::string& name,
Status BackendUnifiedServiceManager::toBinderService(const ::std::string& name,
                                                     const os::Service& in, os::Service* _out) {
                                                     const os::Service& in, os::Service* _out) {
    switch (in.getTag()) {
    switch (in.getTag()) {
        case os::Service::Tag::binder: {
        case os::Service::Tag::serviceWithMetadata: {
            if (in.get<os::Service::Tag::binder>() == nullptr) {
            auto serviceWithMetadata = in.get<os::Service::Tag::serviceWithMetadata>();
            if (serviceWithMetadata.service == nullptr) {
                // failed to find a service. Check to see if we have any local
                // failed to find a service. Check to see if we have any local
                // injected Accessors for this service.
                // injected Accessors for this service.
                os::Service accessor;
                os::Service accessor;
                Status status = getInjectedAccessor(name, &accessor);
                Status status = getInjectedAccessor(name, &accessor);
                if (!status.isOk()) {
                if (!status.isOk()) {
                    *_out = os::Service::make<os::Service::Tag::binder>(nullptr);
                    *_out = os::Service::make<os::Service::Tag::serviceWithMetadata>(
                            createServiceWithMetadata(nullptr, false));
                    return status;
                    return status;
                }
                }
                if (accessor.getTag() == os::Service::Tag::accessor &&
                if (accessor.getTag() == os::Service::Tag::accessor &&
@@ -255,7 +279,8 @@ Status BackendUnifiedServiceManager::toBinderService(const ::std::string& name,
            sp<IAccessor> accessor = interface_cast<IAccessor>(accessorBinder);
            sp<IAccessor> accessor = interface_cast<IAccessor>(accessorBinder);
            if (accessor == nullptr) {
            if (accessor == nullptr) {
                ALOGE("Service#accessor doesn't have accessor. VM is maybe starting...");
                ALOGE("Service#accessor doesn't have accessor. VM is maybe starting...");
                *_out = os::Service::make<os::Service::Tag::binder>(nullptr);
                *_out = os::Service::make<os::Service::Tag::serviceWithMetadata>(
                        createServiceWithMetadata(nullptr, false));
                return Status::ok();
                return Status::ok();
            }
            }
            auto request = [=] {
            auto request = [=] {
@@ -276,7 +301,8 @@ Status BackendUnifiedServiceManager::toBinderService(const ::std::string& name,
                return Status::fromStatusT(status);
                return Status::fromStatusT(status);
            }
            }
            session->setSessionSpecificRoot(accessorBinder);
            session->setSessionSpecificRoot(accessorBinder);
            *_out = os::Service::make<os::Service::Tag::binder>(session->getRootObject());
            *_out = os::Service::make<os::Service::Tag::serviceWithMetadata>(
                    createServiceWithMetadata(session->getRootObject(), false));
            return Status::ok();
            return Status::ok();
        }
        }
        default: {
        default: {
@@ -291,7 +317,8 @@ Status BackendUnifiedServiceManager::addService(const ::std::string& name,
    Status status = mTheRealServiceManager->addService(name, service, allowIsolated, dumpPriority);
    Status status = mTheRealServiceManager->addService(name, service, allowIsolated, dumpPriority);
    // mEnableAddServiceCache is true by default.
    // mEnableAddServiceCache is true by default.
    if (kUseCacheInAddService && mEnableAddServiceCache && status.isOk()) {
    if (kUseCacheInAddService && mEnableAddServiceCache && status.isOk()) {
        return updateCache(name, service);
        return updateCache(name, service,
                           dumpPriority & android::os::IServiceManager::FLAG_IS_LAZY_SERVICE);
    }
    }
    return status;
    return status;
}
}
Loading