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