blob: 765e21cb381e095daf240dfefd8382d3b4d151cf [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 Morelandbdb53ab2021-05-05 17:57:41 +000024#include <binder/RpcSession.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
Steven Moreland63a2d512021-06-25 01:10:15 +000064void* BpBinder::ObjectManager::attach(const void* objectID, void* object, void* cleanupCookie,
65 IBinder::object_cleanup_func func) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080066 entry_t e;
67 e.object = object;
68 e.cleanupCookie = cleanupCookie;
69 e.func = func;
70
Steven Moreland63a2d512021-06-25 01:10:15 +000071 if (ssize_t idx = mObjects.indexOfKey(objectID); idx >= 0) {
72 ALOGI("Trying to attach object ID %p to binder ObjectManager %p with object %p, but object "
73 "ID already in use",
74 objectID, this, object);
75 return mObjects[idx].object;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080076 }
77
78 mObjects.add(objectID, e);
Steven Moreland63a2d512021-06-25 01:10:15 +000079 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080080}
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
Steven Moreland63a2d512021-06-25 01:10:15 +000089void* BpBinder::ObjectManager::detach(const void* objectID) {
90 ssize_t idx = mObjects.indexOfKey(objectID);
91 if (idx < 0) return nullptr;
92 void* value = mObjects[idx].object;
93 mObjects.removeItemsAt(idx, 1);
94 return value;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080095}
96
97void BpBinder::ObjectManager::kill()
98{
99 const size_t N = mObjects.size();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700100 ALOGV("Killing %zu objects in manager %p", N, this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800101 for (size_t i=0; i<N; i++) {
102 const entry_t& e = mObjects.valueAt(i);
Yi Kongfdd8da92018-06-07 17:52:27 -0700103 if (e.func != nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800104 e.func(mObjects.keyAt(i), e.object, e.cleanupCookie);
105 }
106 }
107
108 mObjects.clear();
109}
110
111// ---------------------------------------------------------------------------
112
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000113sp<BpBinder> BpBinder::create(int32_t handle) {
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700114 int32_t trackedUid = -1;
115 if (sCountByUidEnabled) {
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700116 trackedUid = IPCThreadState::self()->getCallingUid();
117 AutoMutex _l(sTrackingLock);
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -0700118 uint32_t trackedValue = sTrackingMap[trackedUid];
119 if (CC_UNLIKELY(trackedValue & LIMIT_REACHED_MASK)) {
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700120 if (sBinderProxyThrottleCreate) {
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -0700121 return nullptr;
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700122 }
123 } else {
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -0700124 if ((trackedValue & COUNTING_VALUE_MASK) >= sBinderProxyCountHighWatermark) {
125 ALOGE("Too many binder proxy objects sent to uid %d from uid %d (%d proxies held)",
126 getuid(), trackedUid, trackedValue);
127 sTrackingMap[trackedUid] |= LIMIT_REACHED_MASK;
128 if (sLimitCallback) sLimitCallback(trackedUid);
129 if (sBinderProxyThrottleCreate) {
130 ALOGI("Throttling binder proxy creates from uid %d in uid %d until binder proxy"
131 " count drops below %d",
132 trackedUid, getuid(), sBinderProxyCountLowWatermark);
133 return nullptr;
134 }
135 }
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700136 }
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -0700137 sTrackingMap[trackedUid]++;
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700138 }
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000139 return sp<BpBinder>::make(BinderHandle{handle}, trackedUid);
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700140}
141
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000142sp<BpBinder> BpBinder::create(const sp<RpcSession>& session, const RpcAddress& address) {
143 LOG_ALWAYS_FATAL_IF(session == nullptr, "BpBinder::create null session");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800144
Steven Moreland5553ac42020-11-11 02:14:45 +0000145 // These are not currently tracked, since there is no UID or other
146 // identifier to track them with. However, if similar functionality is
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000147 // needed, session objects keep track of all BpBinder objects on a
148 // per-session basis.
Steven Moreland5553ac42020-11-11 02:14:45 +0000149
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000150 return sp<BpBinder>::make(RpcHandle{session, address});
Steven Moreland5553ac42020-11-11 02:14:45 +0000151}
152
153BpBinder::BpBinder(Handle&& handle)
154 : mStability(0),
155 mHandle(handle),
156 mAlive(true),
157 mObitsSent(false),
158 mObituaries(nullptr),
159 mTrackedUid(-1) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800160 extendObjectLifetime(OBJECT_LIFETIME_WEAK);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800161}
162
Steven Moreland5553ac42020-11-11 02:14:45 +0000163BpBinder::BpBinder(BinderHandle&& handle, int32_t trackedUid) : BpBinder(Handle(handle)) {
164 mTrackedUid = trackedUid;
165
166 ALOGV("Creating BpBinder %p handle %d\n", this, this->binderHandle());
167
168 IPCThreadState::self()->incWeakHandle(this->binderHandle(), this);
169}
170
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000171BpBinder::BpBinder(RpcHandle&& handle) : BpBinder(Handle(handle)) {
172 LOG_ALWAYS_FATAL_IF(rpcSession() == nullptr, "BpBinder created w/o session object");
Steven Moreland5553ac42020-11-11 02:14:45 +0000173}
174
175bool BpBinder::isRpcBinder() const {
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000176 return std::holds_alternative<RpcHandle>(mHandle);
Steven Moreland5553ac42020-11-11 02:14:45 +0000177}
178
179const RpcAddress& BpBinder::rpcAddress() const {
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000180 return std::get<RpcHandle>(mHandle).address;
Steven Moreland5553ac42020-11-11 02:14:45 +0000181}
182
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000183const sp<RpcSession>& BpBinder::rpcSession() const {
184 return std::get<RpcHandle>(mHandle).session;
Steven Moreland5553ac42020-11-11 02:14:45 +0000185}
186
187int32_t BpBinder::binderHandle() const {
188 return std::get<BinderHandle>(mHandle).handle;
Steven Moreland85180c02019-07-16 14:24:20 -0700189}
190
Mathias Agopian83c04462009-05-22 19:00:22 -0700191bool BpBinder::isDescriptorCached() const {
192 Mutex::Autolock _l(mLock);
193 return mDescriptorCache.size() ? true : false;
194}
195
196const String16& BpBinder::getInterfaceDescriptor() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800197{
Mathias Agopian83c04462009-05-22 19:00:22 -0700198 if (isDescriptorCached() == false) {
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000199 sp<BpBinder> thiz = sp<BpBinder>::fromExisting(const_cast<BpBinder*>(this));
Steven Moreland4cf688f2021-03-31 01:48:58 +0000200
201 Parcel data;
202 data.markForBinder(thiz);
203 Parcel reply;
Mathias Agopian83c04462009-05-22 19:00:22 -0700204 // do the IPC without a lock held.
Steven Moreland4cf688f2021-03-31 01:48:58 +0000205 status_t err = thiz->transact(INTERFACE_TRANSACTION, data, &reply);
Mathias Agopian83c04462009-05-22 19:00:22 -0700206 if (err == NO_ERROR) {
207 String16 res(reply.readString16());
208 Mutex::Autolock _l(mLock);
209 // mDescriptorCache could have been assigned while the lock was
210 // released.
211 if (mDescriptorCache.size() == 0)
212 mDescriptorCache = res;
213 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800214 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700215
Mathias Agopian83c04462009-05-22 19:00:22 -0700216 // we're returning a reference to a non-static object here. Usually this
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700217 // is not something smart to do, however, with binder objects it is
Mathias Agopian83c04462009-05-22 19:00:22 -0700218 // (usually) safe because they are reference-counted.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700219
Mathias Agopian83c04462009-05-22 19:00:22 -0700220 return mDescriptorCache;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800221}
222
223bool BpBinder::isBinderAlive() const
224{
Steven Moreland06074d82020-03-05 23:16:51 +0000225 return mAlive != 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800226}
227
228status_t BpBinder::pingBinder()
229{
Steven Moreland5553ac42020-11-11 02:14:45 +0000230 Parcel data;
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000231 data.markForBinder(sp<BpBinder>::fromExisting(this));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800232 Parcel reply;
Steven Moreland5553ac42020-11-11 02:14:45 +0000233 return transact(PING_TRANSACTION, data, &reply);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800234}
235
236status_t BpBinder::dump(int fd, const Vector<String16>& args)
237{
238 Parcel send;
239 Parcel reply;
240 send.writeFileDescriptor(fd);
241 const size_t numArgs = args.size();
242 send.writeInt32(numArgs);
243 for (size_t i = 0; i < numArgs; i++) {
244 send.writeString16(args[i]);
245 }
246 status_t err = transact(DUMP_TRANSACTION, send, &reply);
247 return err;
248}
249
Jiyong Parkb86c8662018-10-29 23:01:57 +0900250// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800251status_t BpBinder::transact(
252 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
253{
254 // Once a binder has died, it will never come back to life.
255 if (mAlive) {
Steven Moreland46b5fea2019-10-15 11:22:18 -0700256 bool privateVendor = flags & FLAG_PRIVATE_VENDOR;
257 // don't send userspace flags to the kernel
258 flags = flags & ~FLAG_PRIVATE_VENDOR;
259
Steven Moreland6e5a7752019-08-05 20:30:14 -0700260 // user transactions require a given stability level
261 if (code >= FIRST_CALL_TRANSACTION && code <= LAST_CALL_TRANSACTION) {
262 using android::internal::Stability;
263
Steven Moreland16a41062021-07-23 13:35:25 -0700264 int16_t stability = Stability::getRepr(this);
Steven Moreland89ddfc52020-11-13 02:39:26 +0000265 Stability::Level required = privateVendor ? Stability::VENDOR
266 : Stability::getLocalLevel();
Steven Moreland6e5a7752019-08-05 20:30:14 -0700267
Steven Moreland16a41062021-07-23 13:35:25 -0700268 if (CC_UNLIKELY(!Stability::check(stability, required))) {
Steven Morelandb269b582021-02-10 17:09:11 +0000269 ALOGE("Cannot do a user transaction on a %s binder (%s) in a %s context.",
Steven Moreland16a41062021-07-23 13:35:25 -0700270 Stability::levelString(stability).c_str(),
271 String8(getInterfaceDescriptor()).c_str(),
272 Stability::levelString(required).c_str());
Steven Moreland6e5a7752019-08-05 20:30:14 -0700273 return BAD_TYPE;
274 }
275 }
276
Steven Moreland5553ac42020-11-11 02:14:45 +0000277 status_t status;
278 if (CC_UNLIKELY(isRpcBinder())) {
Steven Morelandf5174272021-05-25 00:39:28 +0000279 status = rpcSession()->transact(sp<IBinder>::fromExisting(this), code, data, reply,
280 flags);
Steven Moreland5553ac42020-11-11 02:14:45 +0000281 } else {
282 status = IPCThreadState::self()->transact(binderHandle(), code, data, reply, flags);
283 }
284
Steven Moreland06074d82020-03-05 23:16:51 +0000285 if (status == DEAD_OBJECT) mAlive = 0;
Steven Morelanda86a3562019-08-01 23:28:34 +0000286
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800287 return status;
288 }
289
290 return DEAD_OBJECT;
291}
292
Jiyong Parkb86c8662018-10-29 23:01:57 +0900293// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800294status_t BpBinder::linkToDeath(
295 const sp<DeathRecipient>& recipient, void* cookie, uint32_t flags)
296{
Steven Moreland5553ac42020-11-11 02:14:45 +0000297 if (isRpcBinder()) return UNKNOWN_TRANSACTION;
298
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800299 Obituary ob;
300 ob.recipient = recipient;
301 ob.cookie = cookie;
302 ob.flags = flags;
303
Yi Kongfdd8da92018-06-07 17:52:27 -0700304 LOG_ALWAYS_FATAL_IF(recipient == nullptr,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800305 "linkToDeath(): recipient must be non-NULL");
306
307 {
308 AutoMutex _l(mLock);
309
310 if (!mObitsSent) {
311 if (!mObituaries) {
312 mObituaries = new Vector<Obituary>;
313 if (!mObituaries) {
314 return NO_MEMORY;
315 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000316 ALOGV("Requesting death notification: %p handle %d\n", this, binderHandle());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800317 getWeakRefs()->incWeak(this);
318 IPCThreadState* self = IPCThreadState::self();
Steven Moreland5553ac42020-11-11 02:14:45 +0000319 self->requestDeathNotification(binderHandle(), this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800320 self->flushCommands();
321 }
322 ssize_t res = mObituaries->add(ob);
323 return res >= (ssize_t)NO_ERROR ? (status_t)NO_ERROR : res;
324 }
325 }
326
327 return DEAD_OBJECT;
328}
329
Jiyong Parkb86c8662018-10-29 23:01:57 +0900330// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800331status_t BpBinder::unlinkToDeath(
332 const wp<DeathRecipient>& recipient, void* cookie, uint32_t flags,
333 wp<DeathRecipient>* outRecipient)
334{
Steven Moreland5553ac42020-11-11 02:14:45 +0000335 if (isRpcBinder()) return UNKNOWN_TRANSACTION;
336
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800337 AutoMutex _l(mLock);
338
339 if (mObitsSent) {
340 return DEAD_OBJECT;
341 }
342
343 const size_t N = mObituaries ? mObituaries->size() : 0;
344 for (size_t i=0; i<N; i++) {
345 const Obituary& obit = mObituaries->itemAt(i);
346 if ((obit.recipient == recipient
Yi Kongfdd8da92018-06-07 17:52:27 -0700347 || (recipient == nullptr && obit.cookie == cookie))
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800348 && obit.flags == flags) {
Yi Kongfdd8da92018-06-07 17:52:27 -0700349 if (outRecipient != nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800350 *outRecipient = mObituaries->itemAt(i).recipient;
351 }
352 mObituaries->removeAt(i);
353 if (mObituaries->size() == 0) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000354 ALOGV("Clearing death notification: %p handle %d\n", this, binderHandle());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800355 IPCThreadState* self = IPCThreadState::self();
Steven Moreland5553ac42020-11-11 02:14:45 +0000356 self->clearDeathNotification(binderHandle(), this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800357 self->flushCommands();
358 delete mObituaries;
Yi Kongfdd8da92018-06-07 17:52:27 -0700359 mObituaries = nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800360 }
361 return NO_ERROR;
362 }
363 }
364
365 return NAME_NOT_FOUND;
366}
367
368void BpBinder::sendObituary()
369{
Steven Moreland5553ac42020-11-11 02:14:45 +0000370 LOG_ALWAYS_FATAL_IF(isRpcBinder(), "Cannot send obituary for remote binder.");
371
372 ALOGV("Sending obituary for proxy %p handle %d, mObitsSent=%s\n", this, binderHandle(),
373 mObitsSent ? "true" : "false");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800374
Steven Moreland06074d82020-03-05 23:16:51 +0000375 mAlive = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800376 if (mObitsSent) return;
377
378 mLock.lock();
379 Vector<Obituary>* obits = mObituaries;
Yi Kongfdd8da92018-06-07 17:52:27 -0700380 if(obits != nullptr) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000381 ALOGV("Clearing sent death notification: %p handle %d\n", this, binderHandle());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800382 IPCThreadState* self = IPCThreadState::self();
Steven Moreland5553ac42020-11-11 02:14:45 +0000383 self->clearDeathNotification(binderHandle(), this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800384 self->flushCommands();
Yi Kongfdd8da92018-06-07 17:52:27 -0700385 mObituaries = nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800386 }
Steven Moreland06074d82020-03-05 23:16:51 +0000387 mObitsSent = 1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800388 mLock.unlock();
389
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700390 ALOGV("Reporting death of proxy %p for %zu recipients\n",
391 this, obits ? obits->size() : 0U);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800392
Yi Kongfdd8da92018-06-07 17:52:27 -0700393 if (obits != nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800394 const size_t N = obits->size();
395 for (size_t i=0; i<N; i++) {
396 reportOneDeath(obits->itemAt(i));
397 }
398
399 delete obits;
400 }
401}
402
403void BpBinder::reportOneDeath(const Obituary& obit)
404{
405 sp<DeathRecipient> recipient = obit.recipient.promote();
Steve Block6807e592011-10-20 11:56:00 +0100406 ALOGV("Reporting death to recipient: %p\n", recipient.get());
Yi Kongfdd8da92018-06-07 17:52:27 -0700407 if (recipient == nullptr) return;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800408
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000409 recipient->binderDied(wp<BpBinder>::fromExisting(this));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800410}
411
Steven Moreland63a2d512021-06-25 01:10:15 +0000412void* BpBinder::attachObject(const void* objectID, void* object, void* cleanupCookie,
413 object_cleanup_func func) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800414 AutoMutex _l(mLock);
Steve Block6807e592011-10-20 11:56:00 +0100415 ALOGV("Attaching object %p to binder %p (manager=%p)", object, this, &mObjects);
Steven Moreland63a2d512021-06-25 01:10:15 +0000416 return mObjects.attach(objectID, object, cleanupCookie, func);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800417}
418
419void* BpBinder::findObject(const void* objectID) const
420{
421 AutoMutex _l(mLock);
422 return mObjects.find(objectID);
423}
424
Steven Moreland63a2d512021-06-25 01:10:15 +0000425void* BpBinder::detachObject(const void* objectID) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800426 AutoMutex _l(mLock);
Steven Moreland63a2d512021-06-25 01:10:15 +0000427 return mObjects.detach(objectID);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800428}
429
Steven Moreland9e759e82021-06-25 21:30:23 +0000430void BpBinder::withLock(const std::function<void()>& doWithLock) {
431 AutoMutex _l(mLock);
432 doWithLock();
433}
434
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800435BpBinder* BpBinder::remoteBinder()
436{
437 return this;
438}
439
440BpBinder::~BpBinder()
441{
Steven Moreland5553ac42020-11-11 02:14:45 +0000442 ALOGV("Destroying BpBinder %p handle %d\n", this, binderHandle());
443
444 if (CC_UNLIKELY(isRpcBinder())) return;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800445
446 IPCThreadState* ipc = IPCThreadState::self();
447
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700448 if (mTrackedUid >= 0) {
449 AutoMutex _l(sTrackingLock);
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -0700450 uint32_t trackedValue = sTrackingMap[mTrackedUid];
451 if (CC_UNLIKELY((trackedValue & COUNTING_VALUE_MASK) == 0)) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000452 ALOGE("Unexpected Binder Proxy tracking decrement in %p handle %d\n", this,
453 binderHandle());
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700454 } else {
455 if (CC_UNLIKELY(
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -0700456 (trackedValue & LIMIT_REACHED_MASK) &&
457 ((trackedValue & COUNTING_VALUE_MASK) <= sBinderProxyCountLowWatermark)
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700458 )) {
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -0700459 ALOGI("Limit reached bit reset for uid %d (fewer than %d proxies from uid %d held)",
460 getuid(), mTrackedUid, sBinderProxyCountLowWatermark);
461 sTrackingMap[mTrackedUid] &= ~LIMIT_REACHED_MASK;
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700462 }
463 if (--sTrackingMap[mTrackedUid] == 0) {
464 sTrackingMap.erase(mTrackedUid);
465 }
466 }
467 }
468
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800469 if (ipc) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000470 ipc->expungeHandle(binderHandle(), this);
471 ipc->decWeakHandle(binderHandle());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800472 }
473}
474
475void BpBinder::onFirstRef()
476{
Steven Moreland5553ac42020-11-11 02:14:45 +0000477 ALOGV("onFirstRef BpBinder %p handle %d\n", this, binderHandle());
478 if (CC_UNLIKELY(isRpcBinder())) return;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800479 IPCThreadState* ipc = IPCThreadState::self();
Steven Moreland5553ac42020-11-11 02:14:45 +0000480 if (ipc) ipc->incStrongHandle(binderHandle(), this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800481}
482
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800483void BpBinder::onLastStrongRef(const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800484{
Steven Moreland5553ac42020-11-11 02:14:45 +0000485 ALOGV("onLastStrongRef BpBinder %p handle %d\n", this, binderHandle());
486 if (CC_UNLIKELY(isRpcBinder())) {
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000487 (void)rpcSession()->sendDecStrong(rpcAddress());
Steven Moreland5553ac42020-11-11 02:14:45 +0000488 return;
489 }
Steve Block6807e592011-10-20 11:56:00 +0100490 IF_ALOGV() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800491 printRefs();
492 }
493 IPCThreadState* ipc = IPCThreadState::self();
Steven Moreland5553ac42020-11-11 02:14:45 +0000494 if (ipc) ipc->decStrongHandle(binderHandle());
Steven Moreland80d23932019-06-07 12:43:27 -0700495
496 mLock.lock();
497 Vector<Obituary>* obits = mObituaries;
498 if(obits != nullptr) {
499 if (!obits->isEmpty()) {
Steven Moreland70fcb4e2019-12-13 17:28:58 -0800500 ALOGI("onLastStrongRef automatically unlinking death recipients: %s",
501 mDescriptorCache.size() ? String8(mDescriptorCache).c_str() : "<uncached descriptor>");
Steven Moreland80d23932019-06-07 12:43:27 -0700502 }
503
Steven Moreland5553ac42020-11-11 02:14:45 +0000504 if (ipc) ipc->clearDeathNotification(binderHandle(), this);
Steven Moreland80d23932019-06-07 12:43:27 -0700505 mObituaries = nullptr;
506 }
507 mLock.unlock();
508
509 if (obits != nullptr) {
510 // XXX Should we tell any remaining DeathRecipient
511 // objects that the last strong ref has gone away, so they
512 // are no longer linked?
513 delete obits;
514 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800515}
516
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800517bool BpBinder::onIncStrongAttempted(uint32_t /*flags*/, const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800518{
Steven Moreland5553ac42020-11-11 02:14:45 +0000519 // RPC binder doesn't currently support inc from weak binders
520 if (CC_UNLIKELY(isRpcBinder())) return false;
521
522 ALOGV("onIncStrongAttempted BpBinder %p handle %d\n", this, binderHandle());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800523 IPCThreadState* ipc = IPCThreadState::self();
Steven Moreland5553ac42020-11-11 02:14:45 +0000524 return ipc ? ipc->attemptIncStrongHandle(binderHandle()) == NO_ERROR : false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800525}
526
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700527uint32_t BpBinder::getBinderProxyCount(uint32_t uid)
528{
529 AutoMutex _l(sTrackingLock);
530 auto it = sTrackingMap.find(uid);
531 if (it != sTrackingMap.end()) {
532 return it->second & COUNTING_VALUE_MASK;
533 }
534 return 0;
535}
536
537void BpBinder::getCountByUid(Vector<uint32_t>& uids, Vector<uint32_t>& counts)
538{
539 AutoMutex _l(sTrackingLock);
540 uids.setCapacity(sTrackingMap.size());
541 counts.setCapacity(sTrackingMap.size());
542 for (const auto& it : sTrackingMap) {
543 uids.push_back(it.first);
544 counts.push_back(it.second & COUNTING_VALUE_MASK);
545 }
546}
547
548void BpBinder::enableCountByUid() { sCountByUidEnabled.store(true); }
549void BpBinder::disableCountByUid() { sCountByUidEnabled.store(false); }
550void BpBinder::setCountByUidEnabled(bool enable) { sCountByUidEnabled.store(enable); }
551
552void BpBinder::setLimitCallback(binder_proxy_limit_callback cb) {
553 AutoMutex _l(sTrackingLock);
554 sLimitCallback = cb;
555}
556
557void BpBinder::setBinderProxyCountWatermarks(int high, int low) {
558 AutoMutex _l(sTrackingLock);
559 sBinderProxyCountHighWatermark = high;
560 sBinderProxyCountLowWatermark = low;
561}
562
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800563// ---------------------------------------------------------------------------
564
Steven Moreland61ff8492019-09-26 16:05:45 -0700565} // namespace android