blob: fdcf94acfaa19efd2df3a9e1dcc7e40825d20cd1 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
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#define LOG_TAG "BpBinder"
18//#define LOG_NDEBUG 0
19
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070020#include <binder/BpBinder.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080021
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070022#include <binder/IPCThreadState.h>
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070023#include <binder/IResultReceiver.h>
Steven Moreland5553ac42020-11-11 02:14:45 +000024#include <binder/RpcConnection.h>
Steven Moreland6e5a7752019-08-05 20:30:14 -070025#include <binder/Stability.h>
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -070026#include <cutils/compiler.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027#include <utils/Log.h>
28
29#include <stdio.h>
30
Steve Block6807e592011-10-20 11:56:00 +010031//#undef ALOGV
32//#define ALOGV(...) fprintf(stderr, __VA_ARGS__)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080033
34namespace android {
35
36// ---------------------------------------------------------------------------
37
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -070038Mutex BpBinder::sTrackingLock;
39std::unordered_map<int32_t,uint32_t> BpBinder::sTrackingMap;
40int BpBinder::sNumTrackedUids = 0;
41std::atomic_bool BpBinder::sCountByUidEnabled(false);
42binder_proxy_limit_callback BpBinder::sLimitCallback;
43bool BpBinder::sBinderProxyThrottleCreate = false;
44
45// Arbitrarily high value that probably distinguishes a bad behaving app
46uint32_t BpBinder::sBinderProxyCountHighWatermark = 2500;
47// Another arbitrary value a binder count needs to drop below before another callback will be called
48uint32_t BpBinder::sBinderProxyCountLowWatermark = 2000;
49
50enum {
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -070051 LIMIT_REACHED_MASK = 0x80000000, // A flag denoting that the limit has been reached
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -070052 COUNTING_VALUE_MASK = 0x7FFFFFFF, // A mask of the remaining bits for the count value
53};
54
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080055BpBinder::ObjectManager::ObjectManager()
56{
57}
58
59BpBinder::ObjectManager::~ObjectManager()
60{
61 kill();
62}
63
64void BpBinder::ObjectManager::attach(
65 const void* objectID, void* object, void* cleanupCookie,
66 IBinder::object_cleanup_func func)
67{
68 entry_t e;
69 e.object = object;
70 e.cleanupCookie = cleanupCookie;
71 e.func = func;
72
73 if (mObjects.indexOfKey(objectID) >= 0) {
Steve Blocke6f43dd2012-01-06 19:20:56 +000074 ALOGE("Trying to attach object ID %p to binder ObjectManager %p with object %p, but object ID already in use",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080075 objectID, this, object);
76 return;
77 }
78
79 mObjects.add(objectID, e);
80}
81
82void* BpBinder::ObjectManager::find(const void* objectID) const
83{
84 const ssize_t i = mObjects.indexOfKey(objectID);
Yi Kongfdd8da92018-06-07 17:52:27 -070085 if (i < 0) return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080086 return mObjects.valueAt(i).object;
87}
88
89void BpBinder::ObjectManager::detach(const void* objectID)
90{
91 mObjects.removeItem(objectID);
92}
93
94void BpBinder::ObjectManager::kill()
95{
96 const size_t N = mObjects.size();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -070097 ALOGV("Killing %zu objects in manager %p", N, this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080098 for (size_t i=0; i<N; i++) {
99 const entry_t& e = mObjects.valueAt(i);
Yi Kongfdd8da92018-06-07 17:52:27 -0700100 if (e.func != nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800101 e.func(mObjects.keyAt(i), e.object, e.cleanupCookie);
102 }
103 }
104
105 mObjects.clear();
106}
107
108// ---------------------------------------------------------------------------
109
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000110sp<BpBinder> BpBinder::create(int32_t handle) {
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700111 int32_t trackedUid = -1;
112 if (sCountByUidEnabled) {
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700113 trackedUid = IPCThreadState::self()->getCallingUid();
114 AutoMutex _l(sTrackingLock);
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -0700115 uint32_t trackedValue = sTrackingMap[trackedUid];
116 if (CC_UNLIKELY(trackedValue & LIMIT_REACHED_MASK)) {
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700117 if (sBinderProxyThrottleCreate) {
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -0700118 return nullptr;
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700119 }
120 } else {
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -0700121 if ((trackedValue & COUNTING_VALUE_MASK) >= sBinderProxyCountHighWatermark) {
122 ALOGE("Too many binder proxy objects sent to uid %d from uid %d (%d proxies held)",
123 getuid(), trackedUid, trackedValue);
124 sTrackingMap[trackedUid] |= LIMIT_REACHED_MASK;
125 if (sLimitCallback) sLimitCallback(trackedUid);
126 if (sBinderProxyThrottleCreate) {
127 ALOGI("Throttling binder proxy creates from uid %d in uid %d until binder proxy"
128 " count drops below %d",
129 trackedUid, getuid(), sBinderProxyCountLowWatermark);
130 return nullptr;
131 }
132 }
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700133 }
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -0700134 sTrackingMap[trackedUid]++;
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700135 }
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000136 return sp<BpBinder>::make(BinderHandle{handle}, trackedUid);
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700137}
138
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000139sp<BpBinder> BpBinder::create(const sp<RpcConnection>& connection, const RpcAddress& address) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000140 LOG_ALWAYS_FATAL_IF(connection == nullptr, "BpBinder::create null connection");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800141
Steven Moreland5553ac42020-11-11 02:14:45 +0000142 // These are not currently tracked, since there is no UID or other
143 // identifier to track them with. However, if similar functionality is
144 // needed, connection objects keep track of all BpBinder objects on a
145 // per-connection basis.
146
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000147 return sp<BpBinder>::make(SocketHandle{connection, address});
Steven Moreland5553ac42020-11-11 02:14:45 +0000148}
149
150BpBinder::BpBinder(Handle&& handle)
151 : mStability(0),
152 mHandle(handle),
153 mAlive(true),
154 mObitsSent(false),
155 mObituaries(nullptr),
156 mTrackedUid(-1) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800157 extendObjectLifetime(OBJECT_LIFETIME_WEAK);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800158}
159
Steven Moreland5553ac42020-11-11 02:14:45 +0000160BpBinder::BpBinder(BinderHandle&& handle, int32_t trackedUid) : BpBinder(Handle(handle)) {
161 mTrackedUid = trackedUid;
162
163 ALOGV("Creating BpBinder %p handle %d\n", this, this->binderHandle());
164
165 IPCThreadState::self()->incWeakHandle(this->binderHandle(), this);
166}
167
168BpBinder::BpBinder(SocketHandle&& handle) : BpBinder(Handle(handle)) {
169 LOG_ALWAYS_FATAL_IF(rpcConnection() == nullptr, "BpBinder created w/o connection object");
170}
171
172bool BpBinder::isRpcBinder() const {
173 return std::holds_alternative<SocketHandle>(mHandle);
174}
175
176const RpcAddress& BpBinder::rpcAddress() const {
177 return std::get<SocketHandle>(mHandle).address;
178}
179
180const sp<RpcConnection>& BpBinder::rpcConnection() const {
181 return std::get<SocketHandle>(mHandle).connection;
182}
183
184int32_t BpBinder::binderHandle() const {
185 return std::get<BinderHandle>(mHandle).handle;
Steven Moreland85180c02019-07-16 14:24:20 -0700186}
187
Mathias Agopian83c04462009-05-22 19:00:22 -0700188bool BpBinder::isDescriptorCached() const {
189 Mutex::Autolock _l(mLock);
190 return mDescriptorCache.size() ? true : false;
191}
192
193const String16& BpBinder::getInterfaceDescriptor() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800194{
Mathias Agopian83c04462009-05-22 19:00:22 -0700195 if (isDescriptorCached() == false) {
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000196 sp<BpBinder> thiz = sp<BpBinder>::fromExisting(const_cast<BpBinder*>(this));
Steven Moreland4cf688f2021-03-31 01:48:58 +0000197
198 Parcel data;
199 data.markForBinder(thiz);
200 Parcel reply;
Mathias Agopian83c04462009-05-22 19:00:22 -0700201 // do the IPC without a lock held.
Steven Moreland4cf688f2021-03-31 01:48:58 +0000202 status_t err = thiz->transact(INTERFACE_TRANSACTION, data, &reply);
Mathias Agopian83c04462009-05-22 19:00:22 -0700203 if (err == NO_ERROR) {
204 String16 res(reply.readString16());
205 Mutex::Autolock _l(mLock);
206 // mDescriptorCache could have been assigned while the lock was
207 // released.
208 if (mDescriptorCache.size() == 0)
209 mDescriptorCache = res;
210 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800211 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700212
Mathias Agopian83c04462009-05-22 19:00:22 -0700213 // we're returning a reference to a non-static object here. Usually this
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700214 // is not something smart to do, however, with binder objects it is
Mathias Agopian83c04462009-05-22 19:00:22 -0700215 // (usually) safe because they are reference-counted.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700216
Mathias Agopian83c04462009-05-22 19:00:22 -0700217 return mDescriptorCache;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800218}
219
220bool BpBinder::isBinderAlive() const
221{
Steven Moreland06074d82020-03-05 23:16:51 +0000222 return mAlive != 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800223}
224
225status_t BpBinder::pingBinder()
226{
Steven Moreland5553ac42020-11-11 02:14:45 +0000227 Parcel data;
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000228 data.markForBinder(sp<BpBinder>::fromExisting(this));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800229 Parcel reply;
Steven Moreland5553ac42020-11-11 02:14:45 +0000230 return transact(PING_TRANSACTION, data, &reply);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800231}
232
233status_t BpBinder::dump(int fd, const Vector<String16>& args)
234{
235 Parcel send;
236 Parcel reply;
237 send.writeFileDescriptor(fd);
238 const size_t numArgs = args.size();
239 send.writeInt32(numArgs);
240 for (size_t i = 0; i < numArgs; i++) {
241 send.writeString16(args[i]);
242 }
243 status_t err = transact(DUMP_TRANSACTION, send, &reply);
244 return err;
245}
246
Jiyong Parkb86c8662018-10-29 23:01:57 +0900247// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800248status_t BpBinder::transact(
249 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
250{
251 // Once a binder has died, it will never come back to life.
252 if (mAlive) {
Steven Moreland46b5fea2019-10-15 11:22:18 -0700253 bool privateVendor = flags & FLAG_PRIVATE_VENDOR;
254 // don't send userspace flags to the kernel
255 flags = flags & ~FLAG_PRIVATE_VENDOR;
256
Steven Moreland6e5a7752019-08-05 20:30:14 -0700257 // user transactions require a given stability level
258 if (code >= FIRST_CALL_TRANSACTION && code <= LAST_CALL_TRANSACTION) {
259 using android::internal::Stability;
260
Steven Moreland89ddfc52020-11-13 02:39:26 +0000261 auto category = Stability::getCategory(this);
262 Stability::Level required = privateVendor ? Stability::VENDOR
263 : Stability::getLocalLevel();
Steven Moreland6e5a7752019-08-05 20:30:14 -0700264
Steven Moreland89ddfc52020-11-13 02:39:26 +0000265 if (CC_UNLIKELY(!Stability::check(category, required))) {
Steven Morelandb269b582021-02-10 17:09:11 +0000266 ALOGE("Cannot do a user transaction on a %s binder (%s) in a %s context.",
Steven Moreland89ddfc52020-11-13 02:39:26 +0000267 category.debugString().c_str(),
Steven Morelandb269b582021-02-10 17:09:11 +0000268 String8(getInterfaceDescriptor()).c_str(),
Steven Moreland89ddfc52020-11-13 02:39:26 +0000269 Stability::levelString(required).c_str());
Steven Moreland6e5a7752019-08-05 20:30:14 -0700270 return BAD_TYPE;
271 }
272 }
273
Steven Moreland5553ac42020-11-11 02:14:45 +0000274 status_t status;
275 if (CC_UNLIKELY(isRpcBinder())) {
276 status = rpcConnection()->transact(rpcAddress(), code, data, reply, flags);
277 } else {
278 status = IPCThreadState::self()->transact(binderHandle(), code, data, reply, flags);
279 }
280
Steven Moreland06074d82020-03-05 23:16:51 +0000281 if (status == DEAD_OBJECT) mAlive = 0;
Steven Morelanda86a3562019-08-01 23:28:34 +0000282
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800283 return status;
284 }
285
286 return DEAD_OBJECT;
287}
288
Jiyong Parkb86c8662018-10-29 23:01:57 +0900289// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800290status_t BpBinder::linkToDeath(
291 const sp<DeathRecipient>& recipient, void* cookie, uint32_t flags)
292{
Steven Moreland5553ac42020-11-11 02:14:45 +0000293 if (isRpcBinder()) return UNKNOWN_TRANSACTION;
294
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800295 Obituary ob;
296 ob.recipient = recipient;
297 ob.cookie = cookie;
298 ob.flags = flags;
299
Yi Kongfdd8da92018-06-07 17:52:27 -0700300 LOG_ALWAYS_FATAL_IF(recipient == nullptr,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800301 "linkToDeath(): recipient must be non-NULL");
302
303 {
304 AutoMutex _l(mLock);
305
306 if (!mObitsSent) {
307 if (!mObituaries) {
308 mObituaries = new Vector<Obituary>;
309 if (!mObituaries) {
310 return NO_MEMORY;
311 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000312 ALOGV("Requesting death notification: %p handle %d\n", this, binderHandle());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800313 getWeakRefs()->incWeak(this);
314 IPCThreadState* self = IPCThreadState::self();
Steven Moreland5553ac42020-11-11 02:14:45 +0000315 self->requestDeathNotification(binderHandle(), this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800316 self->flushCommands();
317 }
318 ssize_t res = mObituaries->add(ob);
319 return res >= (ssize_t)NO_ERROR ? (status_t)NO_ERROR : res;
320 }
321 }
322
323 return DEAD_OBJECT;
324}
325
Jiyong Parkb86c8662018-10-29 23:01:57 +0900326// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800327status_t BpBinder::unlinkToDeath(
328 const wp<DeathRecipient>& recipient, void* cookie, uint32_t flags,
329 wp<DeathRecipient>* outRecipient)
330{
Steven Moreland5553ac42020-11-11 02:14:45 +0000331 if (isRpcBinder()) return UNKNOWN_TRANSACTION;
332
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800333 AutoMutex _l(mLock);
334
335 if (mObitsSent) {
336 return DEAD_OBJECT;
337 }
338
339 const size_t N = mObituaries ? mObituaries->size() : 0;
340 for (size_t i=0; i<N; i++) {
341 const Obituary& obit = mObituaries->itemAt(i);
342 if ((obit.recipient == recipient
Yi Kongfdd8da92018-06-07 17:52:27 -0700343 || (recipient == nullptr && obit.cookie == cookie))
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800344 && obit.flags == flags) {
Yi Kongfdd8da92018-06-07 17:52:27 -0700345 if (outRecipient != nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800346 *outRecipient = mObituaries->itemAt(i).recipient;
347 }
348 mObituaries->removeAt(i);
349 if (mObituaries->size() == 0) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000350 ALOGV("Clearing death notification: %p handle %d\n", this, binderHandle());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800351 IPCThreadState* self = IPCThreadState::self();
Steven Moreland5553ac42020-11-11 02:14:45 +0000352 self->clearDeathNotification(binderHandle(), this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800353 self->flushCommands();
354 delete mObituaries;
Yi Kongfdd8da92018-06-07 17:52:27 -0700355 mObituaries = nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800356 }
357 return NO_ERROR;
358 }
359 }
360
361 return NAME_NOT_FOUND;
362}
363
364void BpBinder::sendObituary()
365{
Steven Moreland5553ac42020-11-11 02:14:45 +0000366 LOG_ALWAYS_FATAL_IF(isRpcBinder(), "Cannot send obituary for remote binder.");
367
368 ALOGV("Sending obituary for proxy %p handle %d, mObitsSent=%s\n", this, binderHandle(),
369 mObitsSent ? "true" : "false");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800370
Steven Moreland06074d82020-03-05 23:16:51 +0000371 mAlive = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800372 if (mObitsSent) return;
373
374 mLock.lock();
375 Vector<Obituary>* obits = mObituaries;
Yi Kongfdd8da92018-06-07 17:52:27 -0700376 if(obits != nullptr) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000377 ALOGV("Clearing sent death notification: %p handle %d\n", this, binderHandle());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800378 IPCThreadState* self = IPCThreadState::self();
Steven Moreland5553ac42020-11-11 02:14:45 +0000379 self->clearDeathNotification(binderHandle(), this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800380 self->flushCommands();
Yi Kongfdd8da92018-06-07 17:52:27 -0700381 mObituaries = nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800382 }
Steven Moreland06074d82020-03-05 23:16:51 +0000383 mObitsSent = 1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800384 mLock.unlock();
385
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700386 ALOGV("Reporting death of proxy %p for %zu recipients\n",
387 this, obits ? obits->size() : 0U);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800388
Yi Kongfdd8da92018-06-07 17:52:27 -0700389 if (obits != nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800390 const size_t N = obits->size();
391 for (size_t i=0; i<N; i++) {
392 reportOneDeath(obits->itemAt(i));
393 }
394
395 delete obits;
396 }
397}
398
399void BpBinder::reportOneDeath(const Obituary& obit)
400{
401 sp<DeathRecipient> recipient = obit.recipient.promote();
Steve Block6807e592011-10-20 11:56:00 +0100402 ALOGV("Reporting death to recipient: %p\n", recipient.get());
Yi Kongfdd8da92018-06-07 17:52:27 -0700403 if (recipient == nullptr) return;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800404
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000405 recipient->binderDied(wp<BpBinder>::fromExisting(this));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800406}
407
408
409void BpBinder::attachObject(
410 const void* objectID, void* object, void* cleanupCookie,
411 object_cleanup_func func)
412{
413 AutoMutex _l(mLock);
Steve Block6807e592011-10-20 11:56:00 +0100414 ALOGV("Attaching object %p to binder %p (manager=%p)", object, this, &mObjects);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800415 mObjects.attach(objectID, object, cleanupCookie, func);
416}
417
418void* BpBinder::findObject(const void* objectID) const
419{
420 AutoMutex _l(mLock);
421 return mObjects.find(objectID);
422}
423
424void BpBinder::detachObject(const void* objectID)
425{
426 AutoMutex _l(mLock);
427 mObjects.detach(objectID);
428}
429
430BpBinder* BpBinder::remoteBinder()
431{
432 return this;
433}
434
435BpBinder::~BpBinder()
436{
Steven Moreland5553ac42020-11-11 02:14:45 +0000437 ALOGV("Destroying BpBinder %p handle %d\n", this, binderHandle());
438
439 if (CC_UNLIKELY(isRpcBinder())) return;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800440
441 IPCThreadState* ipc = IPCThreadState::self();
442
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700443 if (mTrackedUid >= 0) {
444 AutoMutex _l(sTrackingLock);
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -0700445 uint32_t trackedValue = sTrackingMap[mTrackedUid];
446 if (CC_UNLIKELY((trackedValue & COUNTING_VALUE_MASK) == 0)) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000447 ALOGE("Unexpected Binder Proxy tracking decrement in %p handle %d\n", this,
448 binderHandle());
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700449 } else {
450 if (CC_UNLIKELY(
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -0700451 (trackedValue & LIMIT_REACHED_MASK) &&
452 ((trackedValue & COUNTING_VALUE_MASK) <= sBinderProxyCountLowWatermark)
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700453 )) {
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -0700454 ALOGI("Limit reached bit reset for uid %d (fewer than %d proxies from uid %d held)",
455 getuid(), mTrackedUid, sBinderProxyCountLowWatermark);
456 sTrackingMap[mTrackedUid] &= ~LIMIT_REACHED_MASK;
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700457 }
458 if (--sTrackingMap[mTrackedUid] == 0) {
459 sTrackingMap.erase(mTrackedUid);
460 }
461 }
462 }
463
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800464 if (ipc) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000465 ipc->expungeHandle(binderHandle(), this);
466 ipc->decWeakHandle(binderHandle());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800467 }
468}
469
470void BpBinder::onFirstRef()
471{
Steven Moreland5553ac42020-11-11 02:14:45 +0000472 ALOGV("onFirstRef BpBinder %p handle %d\n", this, binderHandle());
473 if (CC_UNLIKELY(isRpcBinder())) return;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800474 IPCThreadState* ipc = IPCThreadState::self();
Steven Moreland5553ac42020-11-11 02:14:45 +0000475 if (ipc) ipc->incStrongHandle(binderHandle(), this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800476}
477
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800478void BpBinder::onLastStrongRef(const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800479{
Steven Moreland5553ac42020-11-11 02:14:45 +0000480 ALOGV("onLastStrongRef BpBinder %p handle %d\n", this, binderHandle());
481 if (CC_UNLIKELY(isRpcBinder())) {
482 (void)rpcConnection()->sendDecStrong(rpcAddress());
483 return;
484 }
Steve Block6807e592011-10-20 11:56:00 +0100485 IF_ALOGV() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800486 printRefs();
487 }
488 IPCThreadState* ipc = IPCThreadState::self();
Steven Moreland5553ac42020-11-11 02:14:45 +0000489 if (ipc) ipc->decStrongHandle(binderHandle());
Steven Moreland80d23932019-06-07 12:43:27 -0700490
491 mLock.lock();
492 Vector<Obituary>* obits = mObituaries;
493 if(obits != nullptr) {
494 if (!obits->isEmpty()) {
Steven Moreland70fcb4e2019-12-13 17:28:58 -0800495 ALOGI("onLastStrongRef automatically unlinking death recipients: %s",
496 mDescriptorCache.size() ? String8(mDescriptorCache).c_str() : "<uncached descriptor>");
Steven Moreland80d23932019-06-07 12:43:27 -0700497 }
498
Steven Moreland5553ac42020-11-11 02:14:45 +0000499 if (ipc) ipc->clearDeathNotification(binderHandle(), this);
Steven Moreland80d23932019-06-07 12:43:27 -0700500 mObituaries = nullptr;
501 }
502 mLock.unlock();
503
504 if (obits != nullptr) {
505 // XXX Should we tell any remaining DeathRecipient
506 // objects that the last strong ref has gone away, so they
507 // are no longer linked?
508 delete obits;
509 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800510}
511
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800512bool BpBinder::onIncStrongAttempted(uint32_t /*flags*/, const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800513{
Steven Moreland5553ac42020-11-11 02:14:45 +0000514 // RPC binder doesn't currently support inc from weak binders
515 if (CC_UNLIKELY(isRpcBinder())) return false;
516
517 ALOGV("onIncStrongAttempted BpBinder %p handle %d\n", this, binderHandle());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800518 IPCThreadState* ipc = IPCThreadState::self();
Steven Moreland5553ac42020-11-11 02:14:45 +0000519 return ipc ? ipc->attemptIncStrongHandle(binderHandle()) == NO_ERROR : false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800520}
521
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700522uint32_t BpBinder::getBinderProxyCount(uint32_t uid)
523{
524 AutoMutex _l(sTrackingLock);
525 auto it = sTrackingMap.find(uid);
526 if (it != sTrackingMap.end()) {
527 return it->second & COUNTING_VALUE_MASK;
528 }
529 return 0;
530}
531
532void BpBinder::getCountByUid(Vector<uint32_t>& uids, Vector<uint32_t>& counts)
533{
534 AutoMutex _l(sTrackingLock);
535 uids.setCapacity(sTrackingMap.size());
536 counts.setCapacity(sTrackingMap.size());
537 for (const auto& it : sTrackingMap) {
538 uids.push_back(it.first);
539 counts.push_back(it.second & COUNTING_VALUE_MASK);
540 }
541}
542
543void BpBinder::enableCountByUid() { sCountByUidEnabled.store(true); }
544void BpBinder::disableCountByUid() { sCountByUidEnabled.store(false); }
545void BpBinder::setCountByUidEnabled(bool enable) { sCountByUidEnabled.store(enable); }
546
547void BpBinder::setLimitCallback(binder_proxy_limit_callback cb) {
548 AutoMutex _l(sTrackingLock);
549 sLimitCallback = cb;
550}
551
552void BpBinder::setBinderProxyCountWatermarks(int high, int low) {
553 AutoMutex _l(sTrackingLock);
554 sBinderProxyCountHighWatermark = high;
555 sBinderProxyCountLowWatermark = low;
556}
557
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800558// ---------------------------------------------------------------------------
559
Steven Moreland61ff8492019-09-26 16:05:45 -0700560} // namespace android