diff options
author | 2024-11-14 11:49:08 +0000 | |
---|---|---|
committer | 2024-11-29 17:13:56 +0000 | |
commit | dc207544897a30fe412ffcc0deab07dd974358d1 (patch) | |
tree | cfc962fbcfb3c604aabc6b26fa23ce0b45300b77 /libs/binder/BackendUnifiedServiceManager.cpp | |
parent | bd28005fead6cea70a6213d37071ee7313f1839f (diff) |
Add binder to libbinder cache after addService
This will prevent system_server and other applications
from calling servicemanager for local binders.
Flag: RELEASE_LIBBINDER_CLIENT_CACHE
Bug: 333854840
Test: atest binderCacheUnitTest
Change-Id: I2693f21a3f5b7a5770481e5ac79719444284524d
Diffstat (limited to 'libs/binder/BackendUnifiedServiceManager.cpp')
-rw-r--r-- | libs/binder/BackendUnifiedServiceManager.cpp | 58 |
1 files changed, 37 insertions, 21 deletions
diff --git a/libs/binder/BackendUnifiedServiceManager.cpp b/libs/binder/BackendUnifiedServiceManager.cpp index d32eecdc60..123dfd2c05 100644 --- a/libs/binder/BackendUnifiedServiceManager.cpp +++ b/libs/binder/BackendUnifiedServiceManager.cpp @@ -30,6 +30,12 @@ constexpr bool kUseCache = true; constexpr bool kUseCache = false; #endif +#ifdef LIBBINDER_ADDSERVICE_CACHE +constexpr bool kUseCacheInAddService = true; +#else +constexpr bool kUseCacheInAddService = false; +#endif + using AidlServiceManager = android::os::IServiceManager; using android::os::IAccessor; using binder::Status; @@ -125,31 +131,36 @@ Status BackendUnifiedServiceManager::updateCache(const std::string& serviceName, if (!kUseCache) { return Status::ok(); } + + if (service.getTag() == os::Service::Tag::binder) { + return updateCache(serviceName, service.get<os::Service::Tag::binder>()); + } + return Status::ok(); +} + +Status BackendUnifiedServiceManager::updateCache(const std::string& serviceName, + const sp<IBinder>& binder) { std::string traceStr; if (atrace_is_tag_enabled(ATRACE_TAG_AIDL)) { traceStr = "BinderCacheWithInvalidation::updateCache : " + serviceName; } binder::ScopedTrace aidlTrace(ATRACE_TAG_AIDL, traceStr.c_str()); - - if (service.getTag() == os::Service::Tag::binder) { - sp<IBinder> binder = service.get<os::Service::Tag::binder>(); - if (!binder) { - binder::ScopedTrace - aidlTrace(ATRACE_TAG_AIDL, - "BinderCacheWithInvalidation::updateCache failed: binder_null"); - } else if (!binder->isBinderAlive()) { - binder::ScopedTrace aidlTrace(ATRACE_TAG_AIDL, - "BinderCacheWithInvalidation::updateCache failed: " - "isBinderAlive_false"); - } else if (mCacheForGetService->isClientSideCachingEnabled(serviceName)) { - binder::ScopedTrace aidlTrace(ATRACE_TAG_AIDL, - "BinderCacheWithInvalidation::updateCache successful"); - return mCacheForGetService->setItem(serviceName, binder); - } else { - binder::ScopedTrace aidlTrace(ATRACE_TAG_AIDL, - "BinderCacheWithInvalidation::updateCache failed: " - "caching_not_enabled"); - } + if (!binder) { + binder::ScopedTrace + aidlTrace(ATRACE_TAG_AIDL, + "BinderCacheWithInvalidation::updateCache failed: binder_null"); + } else if (!binder->isBinderAlive()) { + binder::ScopedTrace aidlTrace(ATRACE_TAG_AIDL, + "BinderCacheWithInvalidation::updateCache failed: " + "isBinderAlive_false"); + } else if (mCacheForGetService->isClientSideCachingEnabled(serviceName)) { + binder::ScopedTrace aidlTrace(ATRACE_TAG_AIDL, + "BinderCacheWithInvalidation::updateCache successful"); + return mCacheForGetService->setItem(serviceName, binder); + } else { + binder::ScopedTrace aidlTrace(ATRACE_TAG_AIDL, + "BinderCacheWithInvalidation::updateCache failed: " + "caching_not_enabled"); } return Status::ok(); } @@ -277,7 +288,12 @@ Status BackendUnifiedServiceManager::toBinderService(const ::std::string& name, Status BackendUnifiedServiceManager::addService(const ::std::string& name, const sp<IBinder>& service, bool allowIsolated, int32_t dumpPriority) { - return mTheRealServiceManager->addService(name, service, allowIsolated, dumpPriority); + Status status = mTheRealServiceManager->addService(name, service, allowIsolated, dumpPriority); + // mEnableAddServiceCache is true by default. + if (kUseCacheInAddService && mEnableAddServiceCache && status.isOk()) { + return updateCache(name, service); + } + return status; } Status BackendUnifiedServiceManager::listServices(int32_t dumpPriority, ::std::vector<::std::string>* _aidl_return) { |