diff options
author | 2021-04-02 02:52:46 +0000 | |
---|---|---|
committer | 2021-04-09 20:16:46 +0000 | |
commit | 1a3a8effca802995ece193b7d40cd80abba55798 (patch) | |
tree | 6d4d56881d48e2067459e26eb8d6f61a11ac7197 | |
parent | 64eff6c18215bf4c70ce8b56d2c1304157260bec (diff) |
libbinder use stronger refbase semantics
As a larger example of how to use
ANDROID_UTILS_REF_BASE_DISABLE_IMPLICIT_CONSTRUCTION.
Bug: 184190315
Test: boot (relevant tests in TEST_MAPPING)
Change-Id: I88283543e1f63fd1010f9cd3f63baefec84e6a84
-rw-r--r-- | libs/binder/Android.bp | 1 | ||||
-rw-r--r-- | libs/binder/AppOpsManager.cpp | 2 | ||||
-rw-r--r-- | libs/binder/BpBinder.cpp | 15 | ||||
-rw-r--r-- | libs/binder/BufferedTextOutput.cpp | 2 | ||||
-rw-r--r-- | libs/binder/IInterface.cpp | 4 | ||||
-rw-r--r-- | libs/binder/IMemory.cpp | 4 | ||||
-rw-r--r-- | libs/binder/IServiceManager.cpp | 4 | ||||
-rw-r--r-- | libs/binder/LazyServiceRegistrar.cpp | 4 | ||||
-rw-r--r-- | libs/binder/MemoryDealer.cpp | 8 | ||||
-rw-r--r-- | libs/binder/Parcel.cpp | 3 | ||||
-rw-r--r-- | libs/binder/ProcessState.cpp | 8 | ||||
-rw-r--r-- | libs/binder/RpcConnection.cpp | 22 | ||||
-rw-r--r-- | libs/binder/RpcServer.cpp | 4 | ||||
-rw-r--r-- | libs/binder/include/binder/Binder.h | 4 | ||||
-rw-r--r-- | libs/binder/include/binder/BpBinder.h | 5 | ||||
-rw-r--r-- | libs/binder/include/binder/IInterface.h | 7 | ||||
-rw-r--r-- | libs/binder/include/binder/ProcessState.h | 3 | ||||
-rw-r--r-- | libs/binder/include/binder/RpcServer.h | 1 |
18 files changed, 53 insertions, 48 deletions
diff --git a/libs/binder/Android.bp b/libs/binder/Android.bp index f85c4dce79..79fa6bae30 100644 --- a/libs/binder/Android.bp +++ b/libs/binder/Android.bp @@ -163,6 +163,7 @@ cc_library { "-Werror", "-Wzero-as-null-pointer-constant", "-DANDROID_BASE_UNIQUE_FD_DISABLE_IMPLICIT_CONVERSION", + "-DANDROID_UTILS_REF_BASE_DISABLE_IMPLICIT_CONSTRUCTION", ], product_variables: { binder32bit: { diff --git a/libs/binder/AppOpsManager.cpp b/libs/binder/AppOpsManager.cpp index 1c6b49135d..f3ea1a71d0 100644 --- a/libs/binder/AppOpsManager.cpp +++ b/libs/binder/AppOpsManager.cpp @@ -36,7 +36,7 @@ static const sp<IBinder>& getClientId() { pthread_mutex_lock(&gClientIdMutex); if (gClientId == nullptr) { - gClientId = new BBinder(); + gClientId = sp<BBinder>::make(); } pthread_mutex_unlock(&gClientIdMutex); return gClientId; diff --git a/libs/binder/BpBinder.cpp b/libs/binder/BpBinder.cpp index 53b36fffdc..fdcf94acfa 100644 --- a/libs/binder/BpBinder.cpp +++ b/libs/binder/BpBinder.cpp @@ -107,8 +107,7 @@ void BpBinder::ObjectManager::kill() // --------------------------------------------------------------------------- - -BpBinder* BpBinder::create(int32_t handle) { +sp<BpBinder> BpBinder::create(int32_t handle) { int32_t trackedUid = -1; if (sCountByUidEnabled) { trackedUid = IPCThreadState::self()->getCallingUid(); @@ -134,10 +133,10 @@ BpBinder* BpBinder::create(int32_t handle) { } sTrackingMap[trackedUid]++; } - return new BpBinder(BinderHandle{handle}, trackedUid); + return sp<BpBinder>::make(BinderHandle{handle}, trackedUid); } -BpBinder* BpBinder::create(const sp<RpcConnection>& connection, const RpcAddress& address) { +sp<BpBinder> BpBinder::create(const sp<RpcConnection>& connection, const RpcAddress& address) { LOG_ALWAYS_FATAL_IF(connection == nullptr, "BpBinder::create null connection"); // These are not currently tracked, since there is no UID or other @@ -145,7 +144,7 @@ BpBinder* BpBinder::create(const sp<RpcConnection>& connection, const RpcAddress // needed, connection objects keep track of all BpBinder objects on a // per-connection basis. - return new BpBinder(SocketHandle{connection, address}); + return sp<BpBinder>::make(SocketHandle{connection, address}); } BpBinder::BpBinder(Handle&& handle) @@ -194,7 +193,7 @@ bool BpBinder::isDescriptorCached() const { const String16& BpBinder::getInterfaceDescriptor() const { if (isDescriptorCached() == false) { - sp<BpBinder> thiz = const_cast<BpBinder*>(this); + sp<BpBinder> thiz = sp<BpBinder>::fromExisting(const_cast<BpBinder*>(this)); Parcel data; data.markForBinder(thiz); @@ -226,7 +225,7 @@ bool BpBinder::isBinderAlive() const status_t BpBinder::pingBinder() { Parcel data; - data.markForBinder(this); + data.markForBinder(sp<BpBinder>::fromExisting(this)); Parcel reply; return transact(PING_TRANSACTION, data, &reply); } @@ -403,7 +402,7 @@ void BpBinder::reportOneDeath(const Obituary& obit) ALOGV("Reporting death to recipient: %p\n", recipient.get()); if (recipient == nullptr) return; - recipient->binderDied(this); + recipient->binderDied(wp<BpBinder>::fromExisting(this)); } diff --git a/libs/binder/BufferedTextOutput.cpp b/libs/binder/BufferedTextOutput.cpp index 349658ecfa..a90bfd29ba 100644 --- a/libs/binder/BufferedTextOutput.cpp +++ b/libs/binder/BufferedTextOutput.cpp @@ -254,7 +254,7 @@ BufferedTextOutput::BufferState* BufferedTextOutput::getBuffer() const BufferState* bs = ts.states[mIndex].get(); if (bs != nullptr && bs->seq == mSeq) return bs; - ts.states.editItemAt(mIndex) = new BufferState(mIndex); + ts.states.editItemAt(mIndex) = sp<BufferState>::make(mIndex); bs = ts.states[mIndex].get(); if (bs != nullptr) return bs; } diff --git a/libs/binder/IInterface.cpp b/libs/binder/IInterface.cpp index b19004d454..2780bd4cd9 100644 --- a/libs/binder/IInterface.cpp +++ b/libs/binder/IInterface.cpp @@ -33,14 +33,14 @@ IInterface::~IInterface() { sp<IBinder> IInterface::asBinder(const IInterface* iface) { if (iface == nullptr) return nullptr; - return const_cast<IInterface*>(iface)->onAsBinder(); + return sp<IBinder>::fromExisting(const_cast<IInterface*>(iface)->onAsBinder()); } // static sp<IBinder> IInterface::asBinder(const sp<IInterface>& iface) { if (iface == nullptr) return nullptr; - return iface->onAsBinder(); + return sp<IBinder>::fromExisting(iface->onAsBinder()); } diff --git a/libs/binder/IMemory.cpp b/libs/binder/IMemory.cpp index cca8f81e45..bd974b02b1 100644 --- a/libs/binder/IMemory.cpp +++ b/libs/binder/IMemory.cpp @@ -68,7 +68,7 @@ private: // TODO: Reimplemement based on standard C++ container? }; -static sp<HeapCache> gHeapCache = new HeapCache(); +static sp<HeapCache> gHeapCache = sp<HeapCache>::make(); /******************************************************************************/ @@ -288,7 +288,7 @@ void BpMemoryHeap::assertMapped() const int32_t heapId = mHeapId.load(memory_order_acquire); if (heapId == -1) { sp<IBinder> binder(IInterface::asBinder(const_cast<BpMemoryHeap*>(this))); - sp<BpMemoryHeap> heap(static_cast<BpMemoryHeap*>(find_heap(binder).get())); + sp<BpMemoryHeap> heap = sp<BpMemoryHeap>::cast(find_heap(binder)); heap->assertReallyMapped(); if (heap->mBase != MAP_FAILED) { Mutex::Autolock _l(mLock); diff --git a/libs/binder/IServiceManager.cpp b/libs/binder/IServiceManager.cpp index ca067e2d7f..61f4581df3 100644 --- a/libs/binder/IServiceManager.cpp +++ b/libs/binder/IServiceManager.cpp @@ -102,7 +102,7 @@ sp<IServiceManager> defaultServiceManager() } } - gDefaultServiceManager = new ServiceManagerShim(sm); + gDefaultServiceManager = sp<ServiceManagerShim>::make(sm); }); return gDefaultServiceManager; @@ -324,7 +324,7 @@ sp<IBinder> ServiceManagerShim::waitForService(const String16& name16) } if (out != nullptr) return out; - sp<Waiter> waiter = new Waiter; + sp<Waiter> waiter = sp<Waiter>::make(); if (!mTheRealServiceManager->registerForNotifications( name, waiter).isOk()) { return nullptr; diff --git a/libs/binder/LazyServiceRegistrar.cpp b/libs/binder/LazyServiceRegistrar.cpp index f96b6bb4eb..b503bebc5e 100644 --- a/libs/binder/LazyServiceRegistrar.cpp +++ b/libs/binder/LazyServiceRegistrar.cpp @@ -129,7 +129,9 @@ bool ClientCounterCallbackImpl::registerService(const sp<IBinder>& service, cons } if (!reRegister) { - if(!manager->registerClientCallback(name, service, this).isOk()) { + if (!manager->registerClientCallback(name, service, + sp<android::os::IClientCallback>::fromExisting(this)) + .isOk()) { ALOGE("Failed to add client callback for service %s", name.c_str()); return false; } diff --git a/libs/binder/MemoryDealer.cpp b/libs/binder/MemoryDealer.cpp index b46b3e88fc..c4475c7780 100644 --- a/libs/binder/MemoryDealer.cpp +++ b/libs/binder/MemoryDealer.cpp @@ -228,10 +228,8 @@ Allocation::~Allocation() // ---------------------------------------------------------------------------- MemoryDealer::MemoryDealer(size_t size, const char* name, uint32_t flags) - : mHeap(new MemoryHeapBase(size, flags, name)), - mAllocator(new SimpleBestFitAllocator(size)) -{ -} + : mHeap(sp<MemoryHeapBase>::make(size, flags, name)), + mAllocator(new SimpleBestFitAllocator(size)) {} MemoryDealer::~MemoryDealer() { @@ -243,7 +241,7 @@ sp<IMemory> MemoryDealer::allocate(size_t size) sp<IMemory> memory; const ssize_t offset = allocator()->allocate(size); if (offset >= 0) { - memory = new Allocation(this, heap(), offset, size); + memory = sp<Allocation>::make(sp<MemoryDealer>::fromExisting(this), heap(), offset, size); } return memory; } diff --git a/libs/binder/Parcel.cpp b/libs/binder/Parcel.cpp index b1dddd14ec..98ca829031 100644 --- a/libs/binder/Parcel.cpp +++ b/libs/binder/Parcel.cpp @@ -293,7 +293,8 @@ status_t Parcel::unflattenBinder(sp<IBinder>* out) const if (flat) { switch (flat->hdr.type) { case BINDER_TYPE_BINDER: { - sp<IBinder> binder = reinterpret_cast<IBinder*>(flat->cookie); + sp<IBinder> binder = + sp<IBinder>::fromExisting(reinterpret_cast<IBinder*>(flat->cookie)); return finishUnflattenBinder(binder, out); } case BINDER_TYPE_HANDLE: { diff --git a/libs/binder/ProcessState.cpp b/libs/binder/ProcessState.cpp index 82f6faf7a1..a8b2fb2ff8 100644 --- a/libs/binder/ProcessState.cpp +++ b/libs/binder/ProcessState.cpp @@ -105,7 +105,7 @@ sp<ProcessState> ProcessState::init(const char *driver, bool requireDefault) } std::lock_guard<std::mutex> l(gProcessMutex); - gProcess = new ProcessState(driver); + gProcess = sp<ProcessState>::make(driver); }); if (requireDefault) { @@ -299,8 +299,8 @@ sp<IBinder> ProcessState::getStrongProxyForHandle(int32_t handle) return nullptr; } - b = BpBinder::create(handle); - e->binder = b; + sp<BpBinder> b = BpBinder::create(handle); + e->binder = b.get(); if (b) e->refs = b->getWeakRefs(); result = b; } else { @@ -340,7 +340,7 @@ void ProcessState::spawnPooledThread(bool isMain) if (mThreadPoolStarted) { String8 name = makeBinderThreadName(); ALOGV("Spawning new pooled thread, name=%s\n", name.string()); - sp<Thread> t = new PoolThread(isMain); + sp<Thread> t = sp<PoolThread>::make(isMain); t->run(name.string()); } } diff --git a/libs/binder/RpcConnection.cpp b/libs/binder/RpcConnection.cpp index dab3246fc5..1bf3d889a5 100644 --- a/libs/binder/RpcConnection.cpp +++ b/libs/binder/RpcConnection.cpp @@ -54,7 +54,7 @@ RpcConnection::~RpcConnection() { } sp<RpcConnection> RpcConnection::make() { - return new RpcConnection; + return sp<RpcConnection>::make(); } class UnixSocketAddress : public RpcConnection::SocketAddress { @@ -120,20 +120,21 @@ bool RpcConnection::addVsockClient(unsigned int cid, unsigned int port) { #endif // __BIONIC__ sp<IBinder> RpcConnection::getRootObject() { - ExclusiveSocket socket(this, SocketUse::CLIENT); - return state()->getRootObject(socket.fd(), this); + ExclusiveSocket socket(sp<RpcConnection>::fromExisting(this), SocketUse::CLIENT); + return state()->getRootObject(socket.fd(), sp<RpcConnection>::fromExisting(this)); } status_t RpcConnection::transact(const RpcAddress& address, uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) { - ExclusiveSocket socket(this, + ExclusiveSocket socket(sp<RpcConnection>::fromExisting(this), (flags & IBinder::FLAG_ONEWAY) ? SocketUse::CLIENT_ASYNC : SocketUse::CLIENT); - return state()->transact(socket.fd(), address, code, data, this, reply, flags); + return state()->transact(socket.fd(), address, code, data, + sp<RpcConnection>::fromExisting(this), reply, flags); } status_t RpcConnection::sendDecStrong(const RpcAddress& address) { - ExclusiveSocket socket(this, SocketUse::CLIENT_REFCOUNT); + ExclusiveSocket socket(sp<RpcConnection>::fromExisting(this), SocketUse::CLIENT_REFCOUNT); return state()->sendDecStrong(socket.fd(), address); } @@ -157,10 +158,11 @@ void RpcConnection::join() { // We may not use the connection we just established (two threads might // establish connections for each other), but for now, just use one // server/socket connection. - ExclusiveSocket socket(this, SocketUse::SERVER); + ExclusiveSocket socket(sp<RpcConnection>::fromExisting(this), SocketUse::SERVER); while (true) { - status_t error = state()->getAndExecuteCommand(socket.fd(), this); + status_t error = + state()->getAndExecuteCommand(socket.fd(), sp<RpcConnection>::fromExisting(this)); if (error != OK) { ALOGI("Binder socket thread closing w/ status %s", statusToString(error).c_str()); @@ -221,7 +223,7 @@ bool RpcConnection::addClient(const SocketAddress& addr) { LOG_RPC_DETAIL("Socket at %s client with fd %d", addr.toString().c_str(), serverFd.get()); std::lock_guard<std::mutex> _l(mSocketMutex); - sp<ConnectionSocket> connection = new ConnectionSocket(); + sp<ConnectionSocket> connection = sp<ConnectionSocket>::make(); connection->fd = std::move(serverFd); mClients.push_back(connection); return true; @@ -229,7 +231,7 @@ bool RpcConnection::addClient(const SocketAddress& addr) { void RpcConnection::assignServerToThisThread(base::unique_fd&& fd) { std::lock_guard<std::mutex> _l(mSocketMutex); - sp<ConnectionSocket> connection = new ConnectionSocket(); + sp<ConnectionSocket> connection = sp<ConnectionSocket>::make(); connection->fd = std::move(fd); mServers.push_back(connection); } diff --git a/libs/binder/RpcServer.cpp b/libs/binder/RpcServer.cpp index df07916f7a..1fa37bacb3 100644 --- a/libs/binder/RpcServer.cpp +++ b/libs/binder/RpcServer.cpp @@ -36,7 +36,7 @@ RpcServer::RpcServer() {} RpcServer::~RpcServer() {} sp<RpcServer> RpcServer::make() { - return new RpcServer; + return sp<RpcServer>::make(); } void RpcServer::iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction() { @@ -47,7 +47,7 @@ sp<RpcConnection> RpcServer::addClientConnection() { LOG_ALWAYS_FATAL_IF(!mAgreedExperimental, "no!"); auto connection = RpcConnection::make(); - connection->setForServer(this); + connection->setForServer(sp<RpcServer>::fromExisting(this)); mConnections.push_back(connection); return connection; } diff --git a/libs/binder/include/binder/Binder.h b/libs/binder/include/binder/Binder.h index 7079544080..7e9be417e1 100644 --- a/libs/binder/include/binder/Binder.h +++ b/libs/binder/include/binder/Binder.h @@ -131,8 +131,8 @@ protected: virtual void onLastStrongRef(const void* id); virtual bool onIncStrongAttempted(uint32_t flags, const void* id); - inline IBinder* remote() { return mRemote; } - inline IBinder* remote() const { return mRemote; } + inline IBinder* remote() const { return mRemote; } + inline sp<IBinder> remoteStrong() const { return sp<IBinder>::fromExisting(mRemote); } private: BpRefBase(const BpRefBase& o); diff --git a/libs/binder/include/binder/BpBinder.h b/libs/binder/include/binder/BpBinder.h index 8ab789318d..ad618f9de4 100644 --- a/libs/binder/include/binder/BpBinder.h +++ b/libs/binder/include/binder/BpBinder.h @@ -40,8 +40,8 @@ using binder_proxy_limit_callback = void(*)(int); class BpBinder : public IBinder { public: - static BpBinder* create(int32_t handle); - static BpBinder* create(const sp<RpcConnection>& connection, const RpcAddress& address); + static sp<BpBinder> create(int32_t handle); + static sp<BpBinder> create(const sp<RpcConnection>& connection, const RpcAddress& address); /** * Return value: @@ -143,6 +143,7 @@ public: private: friend PrivateAccessorForId; + friend class sp<BpBinder>; struct BinderHandle { int32_t handle; diff --git a/libs/binder/include/binder/IInterface.h b/libs/binder/include/binder/IInterface.h index ea917539b7..f35e2db951 100644 --- a/libs/binder/include/binder/IInterface.h +++ b/libs/binder/include/binder/IInterface.h @@ -143,11 +143,10 @@ public: \ { \ ::android::sp<I##INTERFACE> intr; \ if (obj != nullptr) { \ - intr = static_cast<I##INTERFACE*>( \ - obj->queryLocalInterface( \ - I##INTERFACE::descriptor).get()); \ + intr = ::android::sp<I##INTERFACE>::cast( \ + obj->queryLocalInterface(I##INTERFACE::descriptor)); \ if (intr == nullptr) { \ - intr = new Bp##INTERFACE(obj); \ + intr = ::android::sp<Bp##INTERFACE>::make(obj); \ } \ } \ return intr; \ diff --git a/libs/binder/include/binder/ProcessState.h b/libs/binder/include/binder/ProcessState.h index ca29440e7c..0919648541 100644 --- a/libs/binder/include/binder/ProcessState.h +++ b/libs/binder/include/binder/ProcessState.h @@ -88,7 +88,8 @@ private: static sp<ProcessState> init(const char *defaultDriver, bool requireDefault); friend class IPCThreadState; - + friend class sp<ProcessState>; + explicit ProcessState(const char* driver); ~ProcessState(); diff --git a/libs/binder/include/binder/RpcServer.h b/libs/binder/include/binder/RpcServer.h index a2c2aee7b8..d29b651f0b 100644 --- a/libs/binder/include/binder/RpcServer.h +++ b/libs/binder/include/binder/RpcServer.h @@ -72,6 +72,7 @@ public: ~RpcServer(); private: + friend sp<RpcServer>; RpcServer(); bool mAgreedExperimental = false; |