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