• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     // Note: This class is not thread safe so protect uses of it when necessary
108     class ObjectManager {
109     public:
110         ObjectManager();
111         ~ObjectManager();
112 
113         void* attach(const void* objectID, void* object, void* cleanupCookie,
114                      IBinder::object_cleanup_func func);
115         void* find(const void* objectID) const;
116         void* detach(const void* objectID);
117         sp<IBinder> lookupOrCreateWeak(const void* objectID, IBinder::object_make_func make,
118                                        const void* makeArgs);
119 
120     private:
121         ObjectManager(const ObjectManager&);
122         ObjectManager& operator=(const ObjectManager&);
123 
124         struct entry_t {
125             void* object = nullptr;
126             void* cleanupCookie = nullptr;
127             IBinder::object_cleanup_func func = nullptr;
128         };
129 
130         std::map<const void*, entry_t> mObjects;
131     };
132 
133     class PrivateAccessor {
134     private:
135         friend class BpBinder;
136         friend class ::android::Parcel;
137         friend class ::android::ProcessState;
138         friend class ::android::RpcSession;
139         friend class ::android::RpcState;
140         friend class ::android::IPCThreadState;
PrivateAccessor(const BpBinder * binder)141         explicit PrivateAccessor(const BpBinder* binder)
142               : mBinder(binder), mMutableBinder(nullptr) {}
PrivateAccessor(BpBinder * binder)143         explicit PrivateAccessor(BpBinder* binder) : mBinder(binder), mMutableBinder(binder) {}
144 
create(int32_t handle,std::function<void ()> * postTask)145         static sp<BpBinder> create(int32_t handle, std::function<void()>* postTask) {
146             return BpBinder::create(handle, postTask);
147         }
create(const sp<RpcSession> & session,uint64_t address)148         static sp<BpBinder> create(const sp<RpcSession>& session, uint64_t address) {
149             return BpBinder::create(session, address);
150         }
151 
152         // valid if !isRpcBinder
binderHandle()153         int32_t binderHandle() const { return mBinder->binderHandle(); }
154 
155         // valid if isRpcBinder
rpcAddress()156         uint64_t rpcAddress() const { return mBinder->rpcAddress(); }
rpcSession()157         const sp<RpcSession>& rpcSession() const { return mBinder->rpcSession(); }
158 
onFrozenStateChanged(bool isFrozen)159         void onFrozenStateChanged(bool isFrozen) { mMutableBinder->onFrozenStateChanged(isFrozen); }
160         const BpBinder* mBinder;
161         BpBinder* mMutableBinder;
162     };
163 
getPrivateAccessor()164     LIBBINDER_EXPORTED const PrivateAccessor getPrivateAccessor() const {
165         return PrivateAccessor(this);
166     }
167 
getPrivateAccessor()168     PrivateAccessor getPrivateAccessor() { return PrivateAccessor(this); }
169 
170 private:
171     friend PrivateAccessor;
172     friend class sp<BpBinder>;
173 
174     static sp<BpBinder> create(int32_t handle, std::function<void()>* postTask);
175     static sp<BpBinder> create(const sp<RpcSession>& session, uint64_t address);
176 
177     struct BinderHandle {
178         int32_t handle;
179     };
180     struct RpcHandle {
181         sp<RpcSession> session;
182         uint64_t address;
183     };
184     using Handle = std::variant<BinderHandle, RpcHandle>;
185 
186     int32_t binderHandle() const;
187     uint64_t rpcAddress() const;
188     const sp<RpcSession>& rpcSession() const;
189 
190     explicit BpBinder(Handle&& handle);
191     BpBinder(BinderHandle&& handle, int32_t trackedUid);
192     explicit BpBinder(RpcHandle&& handle);
193 
194     virtual ~BpBinder();
195     virtual void onFirstRef();
196     virtual void onLastStrongRef(const void* id);
197     virtual bool onIncStrongAttempted(uint32_t flags, const void* id);
198 
199     friend ::android::internal::Stability;
200 
201     int32_t mStability;
202     Handle mHandle;
203 
204     struct Obituary {
205         wp<DeathRecipient> recipient;
206         void* cookie;
207         uint32_t flags;
208     };
209 
210     void onFrozenStateChanged(bool isFrozen);
211 
212     struct FrozenStateChange {
213         bool isFrozen = false;
214         Vector<wp<FrozenStateChangeCallback>> callbacks;
215         bool initialStateReceived = false;
216     };
217 
218     void reportOneDeath(const Obituary& obit);
219     bool isDescriptorCached() const;
220 
221     mutable RpcMutex mLock;
222     volatile int32_t mAlive;
223     volatile int32_t mObitsSent;
224     Vector<Obituary>* mObituaries;
225     std::unique_ptr<FrozenStateChange> mFrozen;
226     ObjectManager mObjectMgr;
227     mutable String16 mDescriptorCache;
228     int32_t mTrackedUid;
229 
230     static RpcMutex sTrackingLock;
231     static std::unordered_map<int32_t, uint32_t> sTrackingMap;
232     static int sNumTrackedUids;
233     static std::atomic_bool sCountByUidEnabled;
234     static binder_proxy_limit_callback sLimitCallback;
235     static uint32_t sBinderProxyCountHighWatermark;
236     static uint32_t sBinderProxyCountLowWatermark;
237     static bool sBinderProxyThrottleCreate;
238     static std::unordered_map<int32_t, uint32_t> sLastLimitCallbackMap;
239     static std::atomic<uint32_t> sBinderProxyCount;
240     static std::atomic<uint32_t> sBinderProxyCountWarned;
241     static binder_proxy_warning_callback sWarningCallback;
242     static uint32_t sBinderProxyCountWarningWatermark;
243 };
244 
245 } // namespace android
246 
247 // ---------------------------------------------------------------------------
248