1 /* 2 * Copyright (C) 2005 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #pragma once 18 19 #include <binder/IBinder.h> 20 #include <utils/Mutex.h> 21 22 #include <map> 23 #include <unordered_map> 24 #include <variant> 25 26 // --------------------------------------------------------------------------- 27 namespace android { 28 29 class RpcSession; 30 class RpcState; 31 namespace internal { 32 class Stability; 33 } 34 class ProcessState; 35 36 using binder_proxy_limit_callback = void(*)(int); 37 38 class BpBinder : public IBinder 39 { 40 public: 41 /** 42 * Return value: 43 * true - this is associated with a socket RpcSession 44 * false - (usual) binder over e.g. /dev/binder 45 */ 46 bool isRpcBinder() const; 47 48 virtual const String16& getInterfaceDescriptor() const; 49 virtual bool isBinderAlive() const; 50 virtual status_t pingBinder(); 51 virtual status_t dump(int fd, const Vector<String16>& args); 52 53 // NOLINTNEXTLINE(google-default-arguments) 54 virtual status_t transact( uint32_t code, 55 const Parcel& data, 56 Parcel* reply, 57 uint32_t flags = 0) final; 58 59 // NOLINTNEXTLINE(google-default-arguments) 60 virtual status_t linkToDeath(const sp<DeathRecipient>& recipient, 61 void* cookie = nullptr, 62 uint32_t flags = 0); 63 64 // NOLINTNEXTLINE(google-default-arguments) 65 virtual status_t unlinkToDeath( const wp<DeathRecipient>& recipient, 66 void* cookie = nullptr, 67 uint32_t flags = 0, 68 wp<DeathRecipient>* outRecipient = nullptr); 69 70 virtual void* attachObject(const void* objectID, void* object, void* cleanupCookie, 71 object_cleanup_func func) final; 72 virtual void* findObject(const void* objectID) const final; 73 virtual void* detachObject(const void* objectID) final; 74 void withLock(const std::function<void()>& doWithLock); 75 76 virtual BpBinder* remoteBinder(); 77 78 void sendObituary(); 79 80 static uint32_t getBinderProxyCount(uint32_t uid); 81 static void getCountByUid(Vector<uint32_t>& uids, Vector<uint32_t>& counts); 82 static void enableCountByUid(); 83 static void disableCountByUid(); 84 static void setCountByUidEnabled(bool enable); 85 static void setLimitCallback(binder_proxy_limit_callback cb); 86 static void setBinderProxyCountWatermarks(int high, int low); 87 88 std::optional<int32_t> getDebugBinderHandle() const; 89 90 class ObjectManager { 91 public: 92 ObjectManager(); 93 ~ObjectManager(); 94 95 void* attach(const void* objectID, void* object, void* cleanupCookie, 96 IBinder::object_cleanup_func func); 97 void* find(const void* objectID) const; 98 void* detach(const void* objectID); 99 100 void kill(); 101 102 private: 103 ObjectManager(const ObjectManager&); 104 ObjectManager& operator=(const ObjectManager&); 105 106 struct entry_t { 107 void* object; 108 void* cleanupCookie; 109 IBinder::object_cleanup_func func; 110 }; 111 112 std::map<const void*, entry_t> mObjects; 113 }; 114 115 class PrivateAccessor { 116 private: 117 friend class BpBinder; 118 friend class ::android::Parcel; 119 friend class ::android::ProcessState; 120 friend class ::android::RpcSession; 121 friend class ::android::RpcState; PrivateAccessor(const BpBinder * binder)122 explicit PrivateAccessor(const BpBinder* binder) : mBinder(binder) {} 123 create(int32_t handle)124 static sp<BpBinder> create(int32_t handle) { return BpBinder::create(handle); } create(const sp<RpcSession> & session,uint64_t address)125 static sp<BpBinder> create(const sp<RpcSession>& session, uint64_t address) { 126 return BpBinder::create(session, address); 127 } 128 129 // valid if !isRpcBinder binderHandle()130 int32_t binderHandle() const { return mBinder->binderHandle(); } 131 132 // valid if isRpcBinder rpcAddress()133 uint64_t rpcAddress() const { return mBinder->rpcAddress(); } rpcSession()134 const sp<RpcSession>& rpcSession() const { return mBinder->rpcSession(); } 135 136 const BpBinder* mBinder; 137 }; getPrivateAccessor()138 const PrivateAccessor getPrivateAccessor() const { return PrivateAccessor(this); } 139 140 private: 141 friend PrivateAccessor; 142 friend class sp<BpBinder>; 143 144 static sp<BpBinder> create(int32_t handle); 145 static sp<BpBinder> create(const sp<RpcSession>& session, uint64_t address); 146 147 struct BinderHandle { 148 int32_t handle; 149 }; 150 struct RpcHandle { 151 sp<RpcSession> session; 152 uint64_t address; 153 }; 154 using Handle = std::variant<BinderHandle, RpcHandle>; 155 156 int32_t binderHandle() const; 157 uint64_t rpcAddress() const; 158 const sp<RpcSession>& rpcSession() const; 159 160 explicit BpBinder(Handle&& handle); 161 BpBinder(BinderHandle&& handle, int32_t trackedUid); 162 explicit BpBinder(RpcHandle&& handle); 163 164 virtual ~BpBinder(); 165 virtual void onFirstRef(); 166 virtual void onLastStrongRef(const void* id); 167 virtual bool onIncStrongAttempted(uint32_t flags, const void* id); 168 169 friend ::android::internal::Stability; 170 171 int32_t mStability; 172 Handle mHandle; 173 174 struct Obituary { 175 wp<DeathRecipient> recipient; 176 void* cookie; 177 uint32_t flags; 178 }; 179 180 void reportOneDeath(const Obituary& obit); 181 bool isDescriptorCached() const; 182 183 mutable Mutex mLock; 184 volatile int32_t mAlive; 185 volatile int32_t mObitsSent; 186 Vector<Obituary>* mObituaries; 187 ObjectManager mObjects; 188 mutable String16 mDescriptorCache; 189 int32_t mTrackedUid; 190 191 static Mutex sTrackingLock; 192 static std::unordered_map<int32_t,uint32_t> sTrackingMap; 193 static int sNumTrackedUids; 194 static std::atomic_bool sCountByUidEnabled; 195 static binder_proxy_limit_callback sLimitCallback; 196 static uint32_t sBinderProxyCountHighWatermark; 197 static uint32_t sBinderProxyCountLowWatermark; 198 static bool sBinderProxyThrottleCreate; 199 static std::unordered_map<int32_t,uint32_t> sLastLimitCallbackMap; 200 }; 201 202 } // namespace android 203 204 // --------------------------------------------------------------------------- 205