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/Common.h> 20 #include <binder/IBinder.h> 21 #include <binder/RpcThreads.h> 22 #include <binder/unique_fd.h> 23 24 #include <map> 25 #include <optional> 26 #include <unordered_map> 27 #include <variant> 28 29 // --------------------------------------------------------------------------- 30 namespace android { 31 32 class IPCThreadState; 33 class RpcSession; 34 class RpcState; 35 namespace internal { 36 class Stability; 37 } 38 class ProcessState; 39 40 using binder_proxy_limit_callback = std::function<void(int)>; 41 using binder_proxy_warning_callback = std::function<void(int)>; 42 43 class BpBinder : public IBinder { 44 public: 45 /** 46 * Return value: 47 * true - this is associated with a socket RpcSession 48 * false - (usual) binder over e.g. /dev/binder 49 */ 50 LIBBINDER_EXPORTED bool isRpcBinder() const; 51 52 LIBBINDER_EXPORTED virtual const String16& getInterfaceDescriptor() const; 53 LIBBINDER_EXPORTED virtual bool isBinderAlive() const; 54 LIBBINDER_EXPORTED virtual status_t pingBinder(); 55 LIBBINDER_EXPORTED virtual status_t dump(int fd, const Vector<String16>& args); 56 57 // NOLINTNEXTLINE(google-default-arguments) 58 LIBBINDER_EXPORTED virtual status_t transact(uint32_t code, const Parcel& data, Parcel* reply, 59 uint32_t flags = 0) final; 60 61 // NOLINTNEXTLINE(google-default-arguments) 62 LIBBINDER_EXPORTED virtual status_t linkToDeath(const sp<DeathRecipient>& recipient, 63 void* cookie = nullptr, uint32_t flags = 0); 64 65 // NOLINTNEXTLINE(google-default-arguments) 66 LIBBINDER_EXPORTED virtual status_t unlinkToDeath(const wp<DeathRecipient>& recipient, 67 void* cookie = nullptr, uint32_t flags = 0, 68 wp<DeathRecipient>* outRecipient = nullptr); 69 70 [[nodiscard]] status_t addFrozenStateChangeCallback( 71 const wp<FrozenStateChangeCallback>& recipient); 72 73 [[nodiscard]] status_t removeFrozenStateChangeCallback( 74 const wp<FrozenStateChangeCallback>& recipient); 75 76 LIBBINDER_EXPORTED virtual void* attachObject(const void* objectID, void* object, 77 void* cleanupCookie, 78 object_cleanup_func func) final; 79 LIBBINDER_EXPORTED virtual void* findObject(const void* objectID) const final; 80 LIBBINDER_EXPORTED virtual void* detachObject(const void* objectID) final; 81 LIBBINDER_EXPORTED void withLock(const std::function<void()>& doWithLock); 82 LIBBINDER_EXPORTED sp<IBinder> lookupOrCreateWeak(const void* objectID, 83 IBinder::object_make_func make, 84 const void* makeArgs); 85 LIBBINDER_EXPORTED virtual BpBinder* remoteBinder(); 86 87 LIBBINDER_EXPORTED void sendObituary(); 88 89 LIBBINDER_EXPORTED static uint32_t getBinderProxyCount(uint32_t uid); 90 LIBBINDER_EXPORTED static void getCountByUid(Vector<uint32_t>& uids, Vector<uint32_t>& counts); 91 LIBBINDER_EXPORTED static void enableCountByUid(); 92 LIBBINDER_EXPORTED static void disableCountByUid(); 93 LIBBINDER_EXPORTED static void setCountByUidEnabled(bool enable); 94 LIBBINDER_EXPORTED static void setBinderProxyCountEventCallback( 95 binder_proxy_limit_callback cbl, binder_proxy_warning_callback cbw); 96 LIBBINDER_EXPORTED static void setBinderProxyCountWatermarks(int high, int low, int warning); 97 LIBBINDER_EXPORTED static uint32_t getBinderProxyCount(); 98 99 LIBBINDER_EXPORTED std::optional<int32_t> getDebugBinderHandle() const; 100 101 // Start recording transactions to the unique_fd. 102 // See RecordedTransaction.h for more details. 103 LIBBINDER_EXPORTED status_t startRecordingBinder(const binder::unique_fd& fd); 104 // Stop the current recording. 105 LIBBINDER_EXPORTED status_t stopRecordingBinder(); 106 107 class ObjectManager { 108 public: 109 ObjectManager(); 110 ~ObjectManager(); 111 112 void* attach(const void* objectID, void* object, void* cleanupCookie, 113 IBinder::object_cleanup_func func); 114 void* find(const void* objectID) const; 115 void* detach(const void* objectID); 116 sp<IBinder> lookupOrCreateWeak(const void* objectID, IBinder::object_make_func make, 117 const void* makeArgs); 118 119 void kill(); 120 121 private: 122 ObjectManager(const ObjectManager&); 123 ObjectManager& operator=(const ObjectManager&); 124 125 struct entry_t { 126 void* object = nullptr; 127 void* cleanupCookie = nullptr; 128 IBinder::object_cleanup_func func = nullptr; 129 }; 130 131 std::map<const void*, entry_t> mObjects; 132 }; 133 134 class PrivateAccessor { 135 private: 136 friend class BpBinder; 137 friend class ::android::Parcel; 138 friend class ::android::ProcessState; 139 friend class ::android::RpcSession; 140 friend class ::android::RpcState; 141 friend class ::android::IPCThreadState; PrivateAccessor(const BpBinder * binder)142 explicit PrivateAccessor(const BpBinder* binder) 143 : mBinder(binder), mMutableBinder(nullptr) {} PrivateAccessor(BpBinder * binder)144 explicit PrivateAccessor(BpBinder* binder) : mBinder(binder), mMutableBinder(binder) {} 145 create(int32_t handle,std::function<void ()> * postTask)146 static sp<BpBinder> create(int32_t handle, std::function<void()>* postTask) { 147 return BpBinder::create(handle, postTask); 148 } create(const sp<RpcSession> & session,uint64_t address)149 static sp<BpBinder> create(const sp<RpcSession>& session, uint64_t address) { 150 return BpBinder::create(session, address); 151 } 152 153 // valid if !isRpcBinder binderHandle()154 int32_t binderHandle() const { return mBinder->binderHandle(); } 155 156 // valid if isRpcBinder rpcAddress()157 uint64_t rpcAddress() const { return mBinder->rpcAddress(); } rpcSession()158 const sp<RpcSession>& rpcSession() const { return mBinder->rpcSession(); } 159 onFrozenStateChanged(bool isFrozen)160 void onFrozenStateChanged(bool isFrozen) { mMutableBinder->onFrozenStateChanged(isFrozen); } 161 const BpBinder* mBinder; 162 BpBinder* mMutableBinder; 163 }; 164 getPrivateAccessor()165 LIBBINDER_EXPORTED const PrivateAccessor getPrivateAccessor() const { 166 return PrivateAccessor(this); 167 } 168 getPrivateAccessor()169 PrivateAccessor getPrivateAccessor() { return PrivateAccessor(this); } 170 171 private: 172 friend PrivateAccessor; 173 friend class sp<BpBinder>; 174 175 static sp<BpBinder> create(int32_t handle, std::function<void()>* postTask); 176 static sp<BpBinder> create(const sp<RpcSession>& session, uint64_t address); 177 178 struct BinderHandle { 179 int32_t handle; 180 }; 181 struct RpcHandle { 182 sp<RpcSession> session; 183 uint64_t address; 184 }; 185 using Handle = std::variant<BinderHandle, RpcHandle>; 186 187 int32_t binderHandle() const; 188 uint64_t rpcAddress() const; 189 const sp<RpcSession>& rpcSession() const; 190 191 explicit BpBinder(Handle&& handle); 192 BpBinder(BinderHandle&& handle, int32_t trackedUid); 193 explicit BpBinder(RpcHandle&& handle); 194 195 virtual ~BpBinder(); 196 virtual void onFirstRef(); 197 virtual void onLastStrongRef(const void* id); 198 virtual bool onIncStrongAttempted(uint32_t flags, const void* id); 199 200 friend ::android::internal::Stability; 201 202 int32_t mStability; 203 Handle mHandle; 204 205 struct Obituary { 206 wp<DeathRecipient> recipient; 207 void* cookie; 208 uint32_t flags; 209 }; 210 211 void onFrozenStateChanged(bool isFrozen); 212 213 struct FrozenStateChange { 214 bool isFrozen = false; 215 Vector<wp<FrozenStateChangeCallback>> callbacks; 216 bool initialStateReceived = false; 217 }; 218 219 void reportOneDeath(const Obituary& obit); 220 bool isDescriptorCached() const; 221 222 mutable RpcMutex mLock; 223 volatile int32_t mAlive; 224 volatile int32_t mObitsSent; 225 Vector<Obituary>* mObituaries; 226 std::unique_ptr<FrozenStateChange> mFrozen; 227 ObjectManager mObjects; 228 mutable String16 mDescriptorCache; 229 int32_t mTrackedUid; 230 231 static RpcMutex sTrackingLock; 232 static std::unordered_map<int32_t, uint32_t> sTrackingMap; 233 static int sNumTrackedUids; 234 static std::atomic_bool sCountByUidEnabled; 235 static binder_proxy_limit_callback sLimitCallback; 236 static uint32_t sBinderProxyCountHighWatermark; 237 static uint32_t sBinderProxyCountLowWatermark; 238 static bool sBinderProxyThrottleCreate; 239 static std::unordered_map<int32_t, uint32_t> sLastLimitCallbackMap; 240 static std::atomic<uint32_t> sBinderProxyCount; 241 static std::atomic<uint32_t> sBinderProxyCountWarned; 242 static binder_proxy_warning_callback sWarningCallback; 243 static uint32_t sBinderProxyCountWarningWatermark; 244 }; 245 246 } // namespace android 247 248 // --------------------------------------------------------------------------- 249