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