blob: 056ef0ab7404da598f8beaab9a40a1850eee192f [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;
Martijn Coenena8d509d2021-09-03 18:06:24 +020039std::unordered_map<int32_t, uint32_t> BpBinder::sTrackingMap;
40std::unordered_map<int32_t, uint32_t> BpBinder::sLastLimitCallbackMap;
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -070041int BpBinder::sNumTrackedUids = 0;
42std::atomic_bool BpBinder::sCountByUidEnabled(false);
43binder_proxy_limit_callback BpBinder::sLimitCallback;
44bool BpBinder::sBinderProxyThrottleCreate = false;
45
46// Arbitrarily high value that probably distinguishes a bad behaving app
47uint32_t BpBinder::sBinderProxyCountHighWatermark = 2500;
48// Another arbitrary value a binder count needs to drop below before another callback will be called
49uint32_t BpBinder::sBinderProxyCountLowWatermark = 2000;
50
Martijn Coenen1cad19c2021-10-04 09:19:01 +020051// Log any transactions for which the data exceeds this size
52#define LOG_TRANSACTIONS_OVER_SIZE (300 * 1024)
53
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -070054enum {
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -070055 LIMIT_REACHED_MASK = 0x80000000, // A flag denoting that the limit has been reached
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -070056 COUNTING_VALUE_MASK = 0x7FFFFFFF, // A mask of the remaining bits for the count value
57};
58
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059BpBinder::ObjectManager::ObjectManager()
60{
61}
62
63BpBinder::ObjectManager::~ObjectManager()
64{
65 kill();
66}
67
Steven Moreland63a2d512021-06-25 01:10:15 +000068void* BpBinder::ObjectManager::attach(const void* objectID, void* object, void* cleanupCookie,
69 IBinder::object_cleanup_func func) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080070 entry_t e;
71 e.object = object;
72 e.cleanupCookie = cleanupCookie;
73 e.func = func;
74
Steven Moreland63a2d512021-06-25 01:10:15 +000075 if (ssize_t idx = mObjects.indexOfKey(objectID); idx >= 0) {
76 ALOGI("Trying to attach object ID %p to binder ObjectManager %p with object %p, but object "
77 "ID already in use",
78 objectID, this, object);
79 return mObjects[idx].object;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080080 }
81
82 mObjects.add(objectID, e);
Steven Moreland63a2d512021-06-25 01:10:15 +000083 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080084}
85
86void* BpBinder::ObjectManager::find(const void* objectID) const
87{
88 const ssize_t i = mObjects.indexOfKey(objectID);
Yi Kongfdd8da92018-06-07 17:52:27 -070089 if (i < 0) return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080090 return mObjects.valueAt(i).object;
91}
92
Steven Moreland63a2d512021-06-25 01:10:15 +000093void* BpBinder::ObjectManager::detach(const void* objectID) {
94 ssize_t idx = mObjects.indexOfKey(objectID);
95 if (idx < 0) return nullptr;
96 void* value = mObjects[idx].object;
97 mObjects.removeItemsAt(idx, 1);
98 return value;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080099}
100
101void BpBinder::ObjectManager::kill()
102{
103 const size_t N = mObjects.size();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700104 ALOGV("Killing %zu objects in manager %p", N, this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800105 for (size_t i=0; i<N; i++) {
106 const entry_t& e = mObjects.valueAt(i);
Yi Kongfdd8da92018-06-07 17:52:27 -0700107 if (e.func != nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800108 e.func(mObjects.keyAt(i), e.object, e.cleanupCookie);
109 }
110 }
111
112 mObjects.clear();
113}
114
115// ---------------------------------------------------------------------------
116
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000117sp<BpBinder> BpBinder::create(int32_t handle) {
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700118 int32_t trackedUid = -1;
119 if (sCountByUidEnabled) {
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700120 trackedUid = IPCThreadState::self()->getCallingUid();
121 AutoMutex _l(sTrackingLock);
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -0700122 uint32_t trackedValue = sTrackingMap[trackedUid];
123 if (CC_UNLIKELY(trackedValue & LIMIT_REACHED_MASK)) {
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700124 if (sBinderProxyThrottleCreate) {
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -0700125 return nullptr;
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700126 }
Martijn Coenena8d509d2021-09-03 18:06:24 +0200127 trackedValue = trackedValue & COUNTING_VALUE_MASK;
128 uint32_t lastLimitCallbackAt = sLastLimitCallbackMap[trackedUid];
129
130 if (trackedValue > lastLimitCallbackAt &&
Martijn Coenen6711b6d2021-09-23 09:44:29 +0200131 (trackedValue - lastLimitCallbackAt > sBinderProxyCountHighWatermark)) {
Martijn Coenena8d509d2021-09-03 18:06:24 +0200132 ALOGE("Still too many binder proxy objects sent to uid %d from uid %d (%d proxies "
133 "held)",
134 getuid(), trackedUid, trackedValue);
135 if (sLimitCallback) sLimitCallback(trackedUid);
136 sLastLimitCallbackMap[trackedUid] = trackedValue;
137 }
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700138 } else {
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -0700139 if ((trackedValue & COUNTING_VALUE_MASK) >= sBinderProxyCountHighWatermark) {
140 ALOGE("Too many binder proxy objects sent to uid %d from uid %d (%d proxies held)",
141 getuid(), trackedUid, trackedValue);
142 sTrackingMap[trackedUid] |= LIMIT_REACHED_MASK;
143 if (sLimitCallback) sLimitCallback(trackedUid);
Martijn Coenena8d509d2021-09-03 18:06:24 +0200144 sLastLimitCallbackMap[trackedUid] = trackedValue & COUNTING_VALUE_MASK;
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -0700145 if (sBinderProxyThrottleCreate) {
146 ALOGI("Throttling binder proxy creates from uid %d in uid %d until binder proxy"
147 " count drops below %d",
148 trackedUid, getuid(), sBinderProxyCountLowWatermark);
149 return nullptr;
150 }
151 }
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700152 }
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -0700153 sTrackingMap[trackedUid]++;
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700154 }
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000155 return sp<BpBinder>::make(BinderHandle{handle}, trackedUid);
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700156}
157
Steven Moreland5623d1a2021-09-10 15:45:34 -0700158sp<BpBinder> BpBinder::create(const sp<RpcSession>& session, uint64_t address) {
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000159 LOG_ALWAYS_FATAL_IF(session == nullptr, "BpBinder::create null session");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800160
Steven Moreland5553ac42020-11-11 02:14:45 +0000161 // These are not currently tracked, since there is no UID or other
162 // identifier to track them with. However, if similar functionality is
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000163 // needed, session objects keep track of all BpBinder objects on a
164 // per-session basis.
Steven Moreland5553ac42020-11-11 02:14:45 +0000165
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000166 return sp<BpBinder>::make(RpcHandle{session, address});
Steven Moreland5553ac42020-11-11 02:14:45 +0000167}
168
169BpBinder::BpBinder(Handle&& handle)
170 : mStability(0),
171 mHandle(handle),
172 mAlive(true),
173 mObitsSent(false),
174 mObituaries(nullptr),
175 mTrackedUid(-1) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800176 extendObjectLifetime(OBJECT_LIFETIME_WEAK);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800177}
178
Steven Moreland5553ac42020-11-11 02:14:45 +0000179BpBinder::BpBinder(BinderHandle&& handle, int32_t trackedUid) : BpBinder(Handle(handle)) {
180 mTrackedUid = trackedUid;
181
182 ALOGV("Creating BpBinder %p handle %d\n", this, this->binderHandle());
183
184 IPCThreadState::self()->incWeakHandle(this->binderHandle(), this);
185}
186
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000187BpBinder::BpBinder(RpcHandle&& handle) : BpBinder(Handle(handle)) {
188 LOG_ALWAYS_FATAL_IF(rpcSession() == nullptr, "BpBinder created w/o session object");
Steven Moreland5553ac42020-11-11 02:14:45 +0000189}
190
191bool BpBinder::isRpcBinder() const {
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000192 return std::holds_alternative<RpcHandle>(mHandle);
Steven Moreland5553ac42020-11-11 02:14:45 +0000193}
194
Steven Moreland5623d1a2021-09-10 15:45:34 -0700195uint64_t BpBinder::rpcAddress() const {
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000196 return std::get<RpcHandle>(mHandle).address;
Steven Moreland5553ac42020-11-11 02:14:45 +0000197}
198
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000199const sp<RpcSession>& BpBinder::rpcSession() const {
200 return std::get<RpcHandle>(mHandle).session;
Steven Moreland5553ac42020-11-11 02:14:45 +0000201}
202
203int32_t BpBinder::binderHandle() const {
204 return std::get<BinderHandle>(mHandle).handle;
Steven Moreland85180c02019-07-16 14:24:20 -0700205}
206
Devin Mooref6f2e642021-08-05 19:03:47 +0000207std::optional<int32_t> BpBinder::getDebugBinderHandle() const {
208 if (!isRpcBinder()) {
209 return binderHandle();
210 } else {
211 return std::nullopt;
212 }
213}
214
Mathias Agopian83c04462009-05-22 19:00:22 -0700215bool BpBinder::isDescriptorCached() const {
216 Mutex::Autolock _l(mLock);
217 return mDescriptorCache.size() ? true : false;
218}
219
220const String16& BpBinder::getInterfaceDescriptor() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800221{
Mathias Agopian83c04462009-05-22 19:00:22 -0700222 if (isDescriptorCached() == false) {
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000223 sp<BpBinder> thiz = sp<BpBinder>::fromExisting(const_cast<BpBinder*>(this));
Steven Moreland4cf688f2021-03-31 01:48:58 +0000224
225 Parcel data;
226 data.markForBinder(thiz);
227 Parcel reply;
Mathias Agopian83c04462009-05-22 19:00:22 -0700228 // do the IPC without a lock held.
Steven Moreland4cf688f2021-03-31 01:48:58 +0000229 status_t err = thiz->transact(INTERFACE_TRANSACTION, data, &reply);
Mathias Agopian83c04462009-05-22 19:00:22 -0700230 if (err == NO_ERROR) {
231 String16 res(reply.readString16());
232 Mutex::Autolock _l(mLock);
233 // mDescriptorCache could have been assigned while the lock was
234 // released.
235 if (mDescriptorCache.size() == 0)
236 mDescriptorCache = res;
237 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800238 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700239
Mathias Agopian83c04462009-05-22 19:00:22 -0700240 // we're returning a reference to a non-static object here. Usually this
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700241 // is not something smart to do, however, with binder objects it is
Mathias Agopian83c04462009-05-22 19:00:22 -0700242 // (usually) safe because they are reference-counted.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700243
Mathias Agopian83c04462009-05-22 19:00:22 -0700244 return mDescriptorCache;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800245}
246
247bool BpBinder::isBinderAlive() const
248{
Steven Moreland06074d82020-03-05 23:16:51 +0000249 return mAlive != 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800250}
251
252status_t BpBinder::pingBinder()
253{
Steven Moreland5553ac42020-11-11 02:14:45 +0000254 Parcel data;
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000255 data.markForBinder(sp<BpBinder>::fromExisting(this));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800256 Parcel reply;
Steven Moreland5553ac42020-11-11 02:14:45 +0000257 return transact(PING_TRANSACTION, data, &reply);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800258}
259
260status_t BpBinder::dump(int fd, const Vector<String16>& args)
261{
262 Parcel send;
263 Parcel reply;
264 send.writeFileDescriptor(fd);
265 const size_t numArgs = args.size();
266 send.writeInt32(numArgs);
267 for (size_t i = 0; i < numArgs; i++) {
268 send.writeString16(args[i]);
269 }
270 status_t err = transact(DUMP_TRANSACTION, send, &reply);
271 return err;
272}
273
Jiyong Parkb86c8662018-10-29 23:01:57 +0900274// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800275status_t BpBinder::transact(
276 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
277{
278 // Once a binder has died, it will never come back to life.
279 if (mAlive) {
Steven Moreland46b5fea2019-10-15 11:22:18 -0700280 bool privateVendor = flags & FLAG_PRIVATE_VENDOR;
281 // don't send userspace flags to the kernel
282 flags = flags & ~FLAG_PRIVATE_VENDOR;
283
Steven Moreland6e5a7752019-08-05 20:30:14 -0700284 // user transactions require a given stability level
285 if (code >= FIRST_CALL_TRANSACTION && code <= LAST_CALL_TRANSACTION) {
286 using android::internal::Stability;
287
Steven Moreland16a41062021-07-23 13:35:25 -0700288 int16_t stability = Stability::getRepr(this);
Steven Moreland89ddfc52020-11-13 02:39:26 +0000289 Stability::Level required = privateVendor ? Stability::VENDOR
290 : Stability::getLocalLevel();
Steven Moreland6e5a7752019-08-05 20:30:14 -0700291
Steven Moreland16a41062021-07-23 13:35:25 -0700292 if (CC_UNLIKELY(!Stability::check(stability, required))) {
Steven Morelandb269b582021-02-10 17:09:11 +0000293 ALOGE("Cannot do a user transaction on a %s binder (%s) in a %s context.",
Steven Moreland16a41062021-07-23 13:35:25 -0700294 Stability::levelString(stability).c_str(),
295 String8(getInterfaceDescriptor()).c_str(),
296 Stability::levelString(required).c_str());
Steven Moreland6e5a7752019-08-05 20:30:14 -0700297 return BAD_TYPE;
298 }
299 }
300
Steven Moreland5553ac42020-11-11 02:14:45 +0000301 status_t status;
302 if (CC_UNLIKELY(isRpcBinder())) {
Steven Morelandf5174272021-05-25 00:39:28 +0000303 status = rpcSession()->transact(sp<IBinder>::fromExisting(this), code, data, reply,
304 flags);
Steven Moreland5553ac42020-11-11 02:14:45 +0000305 } else {
306 status = IPCThreadState::self()->transact(binderHandle(), code, data, reply, flags);
307 }
Martijn Coenen1cad19c2021-10-04 09:19:01 +0200308 if (data.dataSize() > LOG_TRANSACTIONS_OVER_SIZE) {
309 Mutex::Autolock _l(mLock);
310 ALOGW("Large outgoing transaction of %zu bytes, interface descriptor %s, code %d",
311 data.dataSize(),
312 mDescriptorCache.size() ? String8(mDescriptorCache).c_str()
313 : "<uncached descriptor>",
314 code);
315 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000316
Steven Moreland06074d82020-03-05 23:16:51 +0000317 if (status == DEAD_OBJECT) mAlive = 0;
Steven Morelanda86a3562019-08-01 23:28:34 +0000318
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800319 return status;
320 }
321
322 return DEAD_OBJECT;
323}
324
Jiyong Parkb86c8662018-10-29 23:01:57 +0900325// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800326status_t BpBinder::linkToDeath(
327 const sp<DeathRecipient>& recipient, void* cookie, uint32_t flags)
328{
Steven Moreland5553ac42020-11-11 02:14:45 +0000329 if (isRpcBinder()) return UNKNOWN_TRANSACTION;
330
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800331 Obituary ob;
332 ob.recipient = recipient;
333 ob.cookie = cookie;
334 ob.flags = flags;
335
Yi Kongfdd8da92018-06-07 17:52:27 -0700336 LOG_ALWAYS_FATAL_IF(recipient == nullptr,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800337 "linkToDeath(): recipient must be non-NULL");
338
339 {
340 AutoMutex _l(mLock);
341
342 if (!mObitsSent) {
343 if (!mObituaries) {
344 mObituaries = new Vector<Obituary>;
345 if (!mObituaries) {
346 return NO_MEMORY;
347 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000348 ALOGV("Requesting death notification: %p handle %d\n", this, binderHandle());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800349 getWeakRefs()->incWeak(this);
350 IPCThreadState* self = IPCThreadState::self();
Steven Moreland5553ac42020-11-11 02:14:45 +0000351 self->requestDeathNotification(binderHandle(), this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800352 self->flushCommands();
353 }
354 ssize_t res = mObituaries->add(ob);
355 return res >= (ssize_t)NO_ERROR ? (status_t)NO_ERROR : res;
356 }
357 }
358
359 return DEAD_OBJECT;
360}
361
Jiyong Parkb86c8662018-10-29 23:01:57 +0900362// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800363status_t BpBinder::unlinkToDeath(
364 const wp<DeathRecipient>& recipient, void* cookie, uint32_t flags,
365 wp<DeathRecipient>* outRecipient)
366{
Steven Moreland5553ac42020-11-11 02:14:45 +0000367 if (isRpcBinder()) return UNKNOWN_TRANSACTION;
368
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800369 AutoMutex _l(mLock);
370
371 if (mObitsSent) {
372 return DEAD_OBJECT;
373 }
374
375 const size_t N = mObituaries ? mObituaries->size() : 0;
376 for (size_t i=0; i<N; i++) {
377 const Obituary& obit = mObituaries->itemAt(i);
378 if ((obit.recipient == recipient
Yi Kongfdd8da92018-06-07 17:52:27 -0700379 || (recipient == nullptr && obit.cookie == cookie))
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800380 && obit.flags == flags) {
Yi Kongfdd8da92018-06-07 17:52:27 -0700381 if (outRecipient != nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800382 *outRecipient = mObituaries->itemAt(i).recipient;
383 }
384 mObituaries->removeAt(i);
385 if (mObituaries->size() == 0) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000386 ALOGV("Clearing death notification: %p handle %d\n", this, binderHandle());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800387 IPCThreadState* self = IPCThreadState::self();
Steven Moreland5553ac42020-11-11 02:14:45 +0000388 self->clearDeathNotification(binderHandle(), this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800389 self->flushCommands();
390 delete mObituaries;
Yi Kongfdd8da92018-06-07 17:52:27 -0700391 mObituaries = nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800392 }
393 return NO_ERROR;
394 }
395 }
396
397 return NAME_NOT_FOUND;
398}
399
400void BpBinder::sendObituary()
401{
Steven Moreland5553ac42020-11-11 02:14:45 +0000402 LOG_ALWAYS_FATAL_IF(isRpcBinder(), "Cannot send obituary for remote binder.");
403
404 ALOGV("Sending obituary for proxy %p handle %d, mObitsSent=%s\n", this, binderHandle(),
405 mObitsSent ? "true" : "false");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800406
Steven Moreland06074d82020-03-05 23:16:51 +0000407 mAlive = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800408 if (mObitsSent) return;
409
410 mLock.lock();
411 Vector<Obituary>* obits = mObituaries;
Yi Kongfdd8da92018-06-07 17:52:27 -0700412 if(obits != nullptr) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000413 ALOGV("Clearing sent death notification: %p handle %d\n", this, binderHandle());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800414 IPCThreadState* self = IPCThreadState::self();
Steven Moreland5553ac42020-11-11 02:14:45 +0000415 self->clearDeathNotification(binderHandle(), this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800416 self->flushCommands();
Yi Kongfdd8da92018-06-07 17:52:27 -0700417 mObituaries = nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800418 }
Steven Moreland06074d82020-03-05 23:16:51 +0000419 mObitsSent = 1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800420 mLock.unlock();
421
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700422 ALOGV("Reporting death of proxy %p for %zu recipients\n",
423 this, obits ? obits->size() : 0U);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800424
Yi Kongfdd8da92018-06-07 17:52:27 -0700425 if (obits != nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800426 const size_t N = obits->size();
427 for (size_t i=0; i<N; i++) {
428 reportOneDeath(obits->itemAt(i));
429 }
430
431 delete obits;
432 }
433}
434
435void BpBinder::reportOneDeath(const Obituary& obit)
436{
437 sp<DeathRecipient> recipient = obit.recipient.promote();
Steve Block6807e592011-10-20 11:56:00 +0100438 ALOGV("Reporting death to recipient: %p\n", recipient.get());
Yi Kongfdd8da92018-06-07 17:52:27 -0700439 if (recipient == nullptr) return;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800440
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000441 recipient->binderDied(wp<BpBinder>::fromExisting(this));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800442}
443
Steven Moreland63a2d512021-06-25 01:10:15 +0000444void* BpBinder::attachObject(const void* objectID, void* object, void* cleanupCookie,
445 object_cleanup_func func) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800446 AutoMutex _l(mLock);
Steve Block6807e592011-10-20 11:56:00 +0100447 ALOGV("Attaching object %p to binder %p (manager=%p)", object, this, &mObjects);
Steven Moreland63a2d512021-06-25 01:10:15 +0000448 return mObjects.attach(objectID, object, cleanupCookie, func);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800449}
450
451void* BpBinder::findObject(const void* objectID) const
452{
453 AutoMutex _l(mLock);
454 return mObjects.find(objectID);
455}
456
Steven Moreland63a2d512021-06-25 01:10:15 +0000457void* BpBinder::detachObject(const void* objectID) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800458 AutoMutex _l(mLock);
Steven Moreland63a2d512021-06-25 01:10:15 +0000459 return mObjects.detach(objectID);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800460}
461
Steven Moreland9e759e82021-06-25 21:30:23 +0000462void BpBinder::withLock(const std::function<void()>& doWithLock) {
463 AutoMutex _l(mLock);
464 doWithLock();
465}
466
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800467BpBinder* BpBinder::remoteBinder()
468{
469 return this;
470}
471
472BpBinder::~BpBinder()
473{
Steven Moreland5553ac42020-11-11 02:14:45 +0000474 ALOGV("Destroying BpBinder %p handle %d\n", this, binderHandle());
475
476 if (CC_UNLIKELY(isRpcBinder())) return;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800477
478 IPCThreadState* ipc = IPCThreadState::self();
479
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700480 if (mTrackedUid >= 0) {
481 AutoMutex _l(sTrackingLock);
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -0700482 uint32_t trackedValue = sTrackingMap[mTrackedUid];
483 if (CC_UNLIKELY((trackedValue & COUNTING_VALUE_MASK) == 0)) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000484 ALOGE("Unexpected Binder Proxy tracking decrement in %p handle %d\n", this,
485 binderHandle());
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700486 } else {
487 if (CC_UNLIKELY(
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -0700488 (trackedValue & LIMIT_REACHED_MASK) &&
489 ((trackedValue & COUNTING_VALUE_MASK) <= sBinderProxyCountLowWatermark)
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700490 )) {
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -0700491 ALOGI("Limit reached bit reset for uid %d (fewer than %d proxies from uid %d held)",
Martijn Coenena8d509d2021-09-03 18:06:24 +0200492 getuid(), sBinderProxyCountLowWatermark, mTrackedUid);
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -0700493 sTrackingMap[mTrackedUid] &= ~LIMIT_REACHED_MASK;
Martijn Coenena8d509d2021-09-03 18:06:24 +0200494 sLastLimitCallbackMap.erase(mTrackedUid);
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700495 }
496 if (--sTrackingMap[mTrackedUid] == 0) {
497 sTrackingMap.erase(mTrackedUid);
498 }
499 }
500 }
501
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800502 if (ipc) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000503 ipc->expungeHandle(binderHandle(), this);
504 ipc->decWeakHandle(binderHandle());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800505 }
506}
507
508void BpBinder::onFirstRef()
509{
Steven Moreland5553ac42020-11-11 02:14:45 +0000510 ALOGV("onFirstRef BpBinder %p handle %d\n", this, binderHandle());
511 if (CC_UNLIKELY(isRpcBinder())) return;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800512 IPCThreadState* ipc = IPCThreadState::self();
Steven Moreland5553ac42020-11-11 02:14:45 +0000513 if (ipc) ipc->incStrongHandle(binderHandle(), this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800514}
515
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800516void BpBinder::onLastStrongRef(const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800517{
Steven Moreland5553ac42020-11-11 02:14:45 +0000518 ALOGV("onLastStrongRef BpBinder %p handle %d\n", this, binderHandle());
519 if (CC_UNLIKELY(isRpcBinder())) {
Steven Moreland4f622fe2021-09-13 17:38:09 -0700520 (void)rpcSession()->sendDecStrong(this);
Steven Moreland5553ac42020-11-11 02:14:45 +0000521 return;
522 }
Steve Block6807e592011-10-20 11:56:00 +0100523 IF_ALOGV() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800524 printRefs();
525 }
526 IPCThreadState* ipc = IPCThreadState::self();
Steven Moreland5553ac42020-11-11 02:14:45 +0000527 if (ipc) ipc->decStrongHandle(binderHandle());
Steven Moreland80d23932019-06-07 12:43:27 -0700528
529 mLock.lock();
530 Vector<Obituary>* obits = mObituaries;
531 if(obits != nullptr) {
532 if (!obits->isEmpty()) {
Steven Moreland70fcb4e2019-12-13 17:28:58 -0800533 ALOGI("onLastStrongRef automatically unlinking death recipients: %s",
534 mDescriptorCache.size() ? String8(mDescriptorCache).c_str() : "<uncached descriptor>");
Steven Moreland80d23932019-06-07 12:43:27 -0700535 }
536
Steven Moreland5553ac42020-11-11 02:14:45 +0000537 if (ipc) ipc->clearDeathNotification(binderHandle(), this);
Steven Moreland80d23932019-06-07 12:43:27 -0700538 mObituaries = nullptr;
539 }
540 mLock.unlock();
541
542 if (obits != nullptr) {
543 // XXX Should we tell any remaining DeathRecipient
544 // objects that the last strong ref has gone away, so they
545 // are no longer linked?
546 delete obits;
547 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800548}
549
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800550bool BpBinder::onIncStrongAttempted(uint32_t /*flags*/, const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800551{
Steven Moreland5553ac42020-11-11 02:14:45 +0000552 // RPC binder doesn't currently support inc from weak binders
553 if (CC_UNLIKELY(isRpcBinder())) return false;
554
555 ALOGV("onIncStrongAttempted BpBinder %p handle %d\n", this, binderHandle());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800556 IPCThreadState* ipc = IPCThreadState::self();
Steven Moreland5553ac42020-11-11 02:14:45 +0000557 return ipc ? ipc->attemptIncStrongHandle(binderHandle()) == NO_ERROR : false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800558}
559
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700560uint32_t BpBinder::getBinderProxyCount(uint32_t uid)
561{
562 AutoMutex _l(sTrackingLock);
563 auto it = sTrackingMap.find(uid);
564 if (it != sTrackingMap.end()) {
565 return it->second & COUNTING_VALUE_MASK;
566 }
567 return 0;
568}
569
570void BpBinder::getCountByUid(Vector<uint32_t>& uids, Vector<uint32_t>& counts)
571{
572 AutoMutex _l(sTrackingLock);
573 uids.setCapacity(sTrackingMap.size());
574 counts.setCapacity(sTrackingMap.size());
575 for (const auto& it : sTrackingMap) {
576 uids.push_back(it.first);
577 counts.push_back(it.second & COUNTING_VALUE_MASK);
578 }
579}
580
581void BpBinder::enableCountByUid() { sCountByUidEnabled.store(true); }
582void BpBinder::disableCountByUid() { sCountByUidEnabled.store(false); }
583void BpBinder::setCountByUidEnabled(bool enable) { sCountByUidEnabled.store(enable); }
584
585void BpBinder::setLimitCallback(binder_proxy_limit_callback cb) {
586 AutoMutex _l(sTrackingLock);
587 sLimitCallback = cb;
588}
589
590void BpBinder::setBinderProxyCountWatermarks(int high, int low) {
591 AutoMutex _l(sTrackingLock);
592 sBinderProxyCountHighWatermark = high;
593 sBinderProxyCountLowWatermark = low;
594}
595
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800596// ---------------------------------------------------------------------------
597
Steven Moreland61ff8492019-09-26 16:05:45 -0700598} // namespace android