blob: 6471aa6d6bf4f05de4fa4c10798ab9ae06f11cd0 [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 Morelandc9939062021-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 Coenen3b2091f2021-09-03 18:06:24 +020039std::unordered_map<int32_t, uint32_t> BpBinder::sTrackingMap;
40std::unordered_map<int32_t, uint32_t> 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
51enum {
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -070052 LIMIT_REACHED_MASK = 0x80000000, // A flag denoting that the limit has been reached
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -070053 COUNTING_VALUE_MASK = 0x7FFFFFFF, // A mask of the remaining bits for the count value
54};
55
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056BpBinder::ObjectManager::ObjectManager()
57{
58}
59
60BpBinder::ObjectManager::~ObjectManager()
61{
62 kill();
63}
64
65void BpBinder::ObjectManager::attach(
66 const void* objectID, void* object, void* cleanupCookie,
67 IBinder::object_cleanup_func func)
68{
69 entry_t e;
70 e.object = object;
71 e.cleanupCookie = cleanupCookie;
72 e.func = func;
73
74 if (mObjects.indexOfKey(objectID) >= 0) {
Steve Blocke6f43dd2012-01-06 19:20:56 +000075 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 -080076 objectID, this, object);
77 return;
78 }
79
80 mObjects.add(objectID, e);
81}
82
83void* BpBinder::ObjectManager::find(const void* objectID) const
84{
85 const ssize_t i = mObjects.indexOfKey(objectID);
Yi Kongfdd8da92018-06-07 17:52:27 -070086 if (i < 0) return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087 return mObjects.valueAt(i).object;
88}
89
90void BpBinder::ObjectManager::detach(const void* objectID)
91{
92 mObjects.removeItem(objectID);
93}
94
95void BpBinder::ObjectManager::kill()
96{
97 const size_t N = mObjects.size();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -070098 ALOGV("Killing %zu objects in manager %p", N, this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080099 for (size_t i=0; i<N; i++) {
100 const entry_t& e = mObjects.valueAt(i);
Yi Kongfdd8da92018-06-07 17:52:27 -0700101 if (e.func != nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800102 e.func(mObjects.keyAt(i), e.object, e.cleanupCookie);
103 }
104 }
105
106 mObjects.clear();
107}
108
109// ---------------------------------------------------------------------------
110
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000111sp<BpBinder> BpBinder::create(int32_t handle) {
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700112 int32_t trackedUid = -1;
113 if (sCountByUidEnabled) {
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700114 trackedUid = IPCThreadState::self()->getCallingUid();
115 AutoMutex _l(sTrackingLock);
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -0700116 uint32_t trackedValue = sTrackingMap[trackedUid];
117 if (CC_UNLIKELY(trackedValue & LIMIT_REACHED_MASK)) {
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700118 if (sBinderProxyThrottleCreate) {
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -0700119 return nullptr;
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700120 }
Martijn Coenen3b2091f2021-09-03 18:06:24 +0200121 trackedValue = trackedValue & COUNTING_VALUE_MASK;
122 uint32_t lastLimitCallbackAt = sLastLimitCallbackMap[trackedUid];
123
124 if (trackedValue > lastLimitCallbackAt &&
125 (trackedValue - lastLimitCallbackAt > sBinderProxyCountHighWatermark)) {
126 ALOGE("Still too many binder proxy objects sent to uid %d from uid %d (%d proxies "
127 "held)",
128 getuid(), trackedUid, trackedValue);
129 if (sLimitCallback) sLimitCallback(trackedUid);
130 sLastLimitCallbackMap[trackedUid] = trackedValue;
131 }
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700132 } else {
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -0700133 if ((trackedValue & COUNTING_VALUE_MASK) >= sBinderProxyCountHighWatermark) {
134 ALOGE("Too many binder proxy objects sent to uid %d from uid %d (%d proxies held)",
135 getuid(), trackedUid, trackedValue);
136 sTrackingMap[trackedUid] |= LIMIT_REACHED_MASK;
137 if (sLimitCallback) sLimitCallback(trackedUid);
Martijn Coenen3b2091f2021-09-03 18:06:24 +0200138 sLastLimitCallbackMap[trackedUid] = trackedValue & COUNTING_VALUE_MASK;
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -0700139 if (sBinderProxyThrottleCreate) {
140 ALOGI("Throttling binder proxy creates from uid %d in uid %d until binder proxy"
141 " count drops below %d",
142 trackedUid, getuid(), sBinderProxyCountLowWatermark);
143 return nullptr;
144 }
145 }
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700146 }
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -0700147 sTrackingMap[trackedUid]++;
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700148 }
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000149 return sp<BpBinder>::make(BinderHandle{handle}, trackedUid);
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700150}
151
Steven Morelandc9939062021-05-05 17:57:41 +0000152sp<BpBinder> BpBinder::create(const sp<RpcSession>& session, const RpcAddress& address) {
153 LOG_ALWAYS_FATAL_IF(session == nullptr, "BpBinder::create null session");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800154
Steven Moreland5553ac42020-11-11 02:14:45 +0000155 // These are not currently tracked, since there is no UID or other
156 // identifier to track them with. However, if similar functionality is
Steven Morelandc9939062021-05-05 17:57:41 +0000157 // needed, session objects keep track of all BpBinder objects on a
158 // per-session basis.
Steven Moreland5553ac42020-11-11 02:14:45 +0000159
Steven Morelandc9939062021-05-05 17:57:41 +0000160 return sp<BpBinder>::make(RpcHandle{session, address});
Steven Moreland5553ac42020-11-11 02:14:45 +0000161}
162
163BpBinder::BpBinder(Handle&& handle)
164 : mStability(0),
165 mHandle(handle),
166 mAlive(true),
167 mObitsSent(false),
168 mObituaries(nullptr),
169 mTrackedUid(-1) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800170 extendObjectLifetime(OBJECT_LIFETIME_WEAK);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800171}
172
Steven Moreland5553ac42020-11-11 02:14:45 +0000173BpBinder::BpBinder(BinderHandle&& handle, int32_t trackedUid) : BpBinder(Handle(handle)) {
174 mTrackedUid = trackedUid;
175
176 ALOGV("Creating BpBinder %p handle %d\n", this, this->binderHandle());
177
178 IPCThreadState::self()->incWeakHandle(this->binderHandle(), this);
179}
180
Steven Morelandc9939062021-05-05 17:57:41 +0000181BpBinder::BpBinder(RpcHandle&& handle) : BpBinder(Handle(handle)) {
182 LOG_ALWAYS_FATAL_IF(rpcSession() == nullptr, "BpBinder created w/o session object");
Steven Moreland5553ac42020-11-11 02:14:45 +0000183}
184
185bool BpBinder::isRpcBinder() const {
Steven Morelandc9939062021-05-05 17:57:41 +0000186 return std::holds_alternative<RpcHandle>(mHandle);
Steven Moreland5553ac42020-11-11 02:14:45 +0000187}
188
189const RpcAddress& BpBinder::rpcAddress() const {
Steven Morelandc9939062021-05-05 17:57:41 +0000190 return std::get<RpcHandle>(mHandle).address;
Steven Moreland5553ac42020-11-11 02:14:45 +0000191}
192
Steven Morelandc9939062021-05-05 17:57:41 +0000193const sp<RpcSession>& BpBinder::rpcSession() const {
194 return std::get<RpcHandle>(mHandle).session;
Steven Moreland5553ac42020-11-11 02:14:45 +0000195}
196
197int32_t BpBinder::binderHandle() const {
198 return std::get<BinderHandle>(mHandle).handle;
Steven Moreland85180c02019-07-16 14:24:20 -0700199}
200
Mathias Agopian83c04462009-05-22 19:00:22 -0700201bool BpBinder::isDescriptorCached() const {
202 Mutex::Autolock _l(mLock);
203 return mDescriptorCache.size() ? true : false;
204}
205
206const String16& BpBinder::getInterfaceDescriptor() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800207{
Mathias Agopian83c04462009-05-22 19:00:22 -0700208 if (isDescriptorCached() == false) {
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000209 sp<BpBinder> thiz = sp<BpBinder>::fromExisting(const_cast<BpBinder*>(this));
Steven Moreland4cf688f2021-03-31 01:48:58 +0000210
211 Parcel data;
212 data.markForBinder(thiz);
213 Parcel reply;
Mathias Agopian83c04462009-05-22 19:00:22 -0700214 // do the IPC without a lock held.
Steven Moreland4cf688f2021-03-31 01:48:58 +0000215 status_t err = thiz->transact(INTERFACE_TRANSACTION, data, &reply);
Mathias Agopian83c04462009-05-22 19:00:22 -0700216 if (err == NO_ERROR) {
217 String16 res(reply.readString16());
218 Mutex::Autolock _l(mLock);
219 // mDescriptorCache could have been assigned while the lock was
220 // released.
221 if (mDescriptorCache.size() == 0)
222 mDescriptorCache = res;
223 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800224 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700225
Mathias Agopian83c04462009-05-22 19:00:22 -0700226 // we're returning a reference to a non-static object here. Usually this
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700227 // is not something smart to do, however, with binder objects it is
Mathias Agopian83c04462009-05-22 19:00:22 -0700228 // (usually) safe because they are reference-counted.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700229
Mathias Agopian83c04462009-05-22 19:00:22 -0700230 return mDescriptorCache;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800231}
232
233bool BpBinder::isBinderAlive() const
234{
Steven Moreland06074d82020-03-05 23:16:51 +0000235 return mAlive != 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800236}
237
238status_t BpBinder::pingBinder()
239{
Steven Moreland5553ac42020-11-11 02:14:45 +0000240 Parcel data;
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000241 data.markForBinder(sp<BpBinder>::fromExisting(this));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800242 Parcel reply;
Steven Moreland5553ac42020-11-11 02:14:45 +0000243 return transact(PING_TRANSACTION, data, &reply);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800244}
245
246status_t BpBinder::dump(int fd, const Vector<String16>& args)
247{
248 Parcel send;
249 Parcel reply;
250 send.writeFileDescriptor(fd);
251 const size_t numArgs = args.size();
252 send.writeInt32(numArgs);
253 for (size_t i = 0; i < numArgs; i++) {
254 send.writeString16(args[i]);
255 }
256 status_t err = transact(DUMP_TRANSACTION, send, &reply);
257 return err;
258}
259
Jiyong Parkb86c8662018-10-29 23:01:57 +0900260// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800261status_t BpBinder::transact(
262 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
263{
264 // Once a binder has died, it will never come back to life.
265 if (mAlive) {
Steven Moreland46b5fea2019-10-15 11:22:18 -0700266 bool privateVendor = flags & FLAG_PRIVATE_VENDOR;
267 // don't send userspace flags to the kernel
268 flags = flags & ~FLAG_PRIVATE_VENDOR;
269
Steven Moreland6e5a7752019-08-05 20:30:14 -0700270 // user transactions require a given stability level
271 if (code >= FIRST_CALL_TRANSACTION && code <= LAST_CALL_TRANSACTION) {
272 using android::internal::Stability;
273
Steven Moreland89ddfc52020-11-13 02:39:26 +0000274 auto category = Stability::getCategory(this);
275 Stability::Level required = privateVendor ? Stability::VENDOR
276 : Stability::getLocalLevel();
Steven Moreland6e5a7752019-08-05 20:30:14 -0700277
Steven Moreland89ddfc52020-11-13 02:39:26 +0000278 if (CC_UNLIKELY(!Stability::check(category, required))) {
Steven Morelandb269b582021-02-10 17:09:11 +0000279 ALOGE("Cannot do a user transaction on a %s binder (%s) in a %s context.",
Steven Moreland89ddfc52020-11-13 02:39:26 +0000280 category.debugString().c_str(),
Steven Morelandb269b582021-02-10 17:09:11 +0000281 String8(getInterfaceDescriptor()).c_str(),
Steven Moreland89ddfc52020-11-13 02:39:26 +0000282 Stability::levelString(required).c_str());
Steven Moreland6e5a7752019-08-05 20:30:14 -0700283 return BAD_TYPE;
284 }
285 }
286
Steven Moreland5553ac42020-11-11 02:14:45 +0000287 status_t status;
288 if (CC_UNLIKELY(isRpcBinder())) {
Steven Morelandc9939062021-05-05 17:57:41 +0000289 status = rpcSession()->transact(rpcAddress(), code, data, reply, flags);
Steven Moreland5553ac42020-11-11 02:14:45 +0000290 } else {
291 status = IPCThreadState::self()->transact(binderHandle(), code, data, reply, flags);
292 }
293
Steven Moreland06074d82020-03-05 23:16:51 +0000294 if (status == DEAD_OBJECT) mAlive = 0;
Steven Morelanda86a3562019-08-01 23:28:34 +0000295
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800296 return status;
297 }
298
299 return DEAD_OBJECT;
300}
301
Jiyong Parkb86c8662018-10-29 23:01:57 +0900302// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800303status_t BpBinder::linkToDeath(
304 const sp<DeathRecipient>& recipient, void* cookie, uint32_t flags)
305{
Steven Moreland5553ac42020-11-11 02:14:45 +0000306 if (isRpcBinder()) return UNKNOWN_TRANSACTION;
307
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800308 Obituary ob;
309 ob.recipient = recipient;
310 ob.cookie = cookie;
311 ob.flags = flags;
312
Yi Kongfdd8da92018-06-07 17:52:27 -0700313 LOG_ALWAYS_FATAL_IF(recipient == nullptr,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800314 "linkToDeath(): recipient must be non-NULL");
315
316 {
317 AutoMutex _l(mLock);
318
319 if (!mObitsSent) {
320 if (!mObituaries) {
321 mObituaries = new Vector<Obituary>;
322 if (!mObituaries) {
323 return NO_MEMORY;
324 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000325 ALOGV("Requesting death notification: %p handle %d\n", this, binderHandle());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800326 getWeakRefs()->incWeak(this);
327 IPCThreadState* self = IPCThreadState::self();
Steven Moreland5553ac42020-11-11 02:14:45 +0000328 self->requestDeathNotification(binderHandle(), this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800329 self->flushCommands();
330 }
331 ssize_t res = mObituaries->add(ob);
332 return res >= (ssize_t)NO_ERROR ? (status_t)NO_ERROR : res;
333 }
334 }
335
336 return DEAD_OBJECT;
337}
338
Jiyong Parkb86c8662018-10-29 23:01:57 +0900339// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800340status_t BpBinder::unlinkToDeath(
341 const wp<DeathRecipient>& recipient, void* cookie, uint32_t flags,
342 wp<DeathRecipient>* outRecipient)
343{
Steven Moreland5553ac42020-11-11 02:14:45 +0000344 if (isRpcBinder()) return UNKNOWN_TRANSACTION;
345
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800346 AutoMutex _l(mLock);
347
348 if (mObitsSent) {
349 return DEAD_OBJECT;
350 }
351
352 const size_t N = mObituaries ? mObituaries->size() : 0;
353 for (size_t i=0; i<N; i++) {
354 const Obituary& obit = mObituaries->itemAt(i);
355 if ((obit.recipient == recipient
Yi Kongfdd8da92018-06-07 17:52:27 -0700356 || (recipient == nullptr && obit.cookie == cookie))
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800357 && obit.flags == flags) {
Yi Kongfdd8da92018-06-07 17:52:27 -0700358 if (outRecipient != nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800359 *outRecipient = mObituaries->itemAt(i).recipient;
360 }
361 mObituaries->removeAt(i);
362 if (mObituaries->size() == 0) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000363 ALOGV("Clearing death notification: %p handle %d\n", this, binderHandle());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800364 IPCThreadState* self = IPCThreadState::self();
Steven Moreland5553ac42020-11-11 02:14:45 +0000365 self->clearDeathNotification(binderHandle(), this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800366 self->flushCommands();
367 delete mObituaries;
Yi Kongfdd8da92018-06-07 17:52:27 -0700368 mObituaries = nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800369 }
370 return NO_ERROR;
371 }
372 }
373
374 return NAME_NOT_FOUND;
375}
376
377void BpBinder::sendObituary()
378{
Steven Moreland5553ac42020-11-11 02:14:45 +0000379 LOG_ALWAYS_FATAL_IF(isRpcBinder(), "Cannot send obituary for remote binder.");
380
381 ALOGV("Sending obituary for proxy %p handle %d, mObitsSent=%s\n", this, binderHandle(),
382 mObitsSent ? "true" : "false");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800383
Steven Moreland06074d82020-03-05 23:16:51 +0000384 mAlive = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800385 if (mObitsSent) return;
386
387 mLock.lock();
388 Vector<Obituary>* obits = mObituaries;
Yi Kongfdd8da92018-06-07 17:52:27 -0700389 if(obits != nullptr) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000390 ALOGV("Clearing sent death notification: %p handle %d\n", this, binderHandle());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800391 IPCThreadState* self = IPCThreadState::self();
Steven Moreland5553ac42020-11-11 02:14:45 +0000392 self->clearDeathNotification(binderHandle(), this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800393 self->flushCommands();
Yi Kongfdd8da92018-06-07 17:52:27 -0700394 mObituaries = nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800395 }
Steven Moreland06074d82020-03-05 23:16:51 +0000396 mObitsSent = 1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800397 mLock.unlock();
398
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700399 ALOGV("Reporting death of proxy %p for %zu recipients\n",
400 this, obits ? obits->size() : 0U);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800401
Yi Kongfdd8da92018-06-07 17:52:27 -0700402 if (obits != nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800403 const size_t N = obits->size();
404 for (size_t i=0; i<N; i++) {
405 reportOneDeath(obits->itemAt(i));
406 }
407
408 delete obits;
409 }
410}
411
412void BpBinder::reportOneDeath(const Obituary& obit)
413{
414 sp<DeathRecipient> recipient = obit.recipient.promote();
Steve Block6807e592011-10-20 11:56:00 +0100415 ALOGV("Reporting death to recipient: %p\n", recipient.get());
Yi Kongfdd8da92018-06-07 17:52:27 -0700416 if (recipient == nullptr) return;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800417
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000418 recipient->binderDied(wp<BpBinder>::fromExisting(this));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800419}
420
421
422void BpBinder::attachObject(
423 const void* objectID, void* object, void* cleanupCookie,
424 object_cleanup_func func)
425{
426 AutoMutex _l(mLock);
Steve Block6807e592011-10-20 11:56:00 +0100427 ALOGV("Attaching object %p to binder %p (manager=%p)", object, this, &mObjects);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800428 mObjects.attach(objectID, object, cleanupCookie, func);
429}
430
431void* BpBinder::findObject(const void* objectID) const
432{
433 AutoMutex _l(mLock);
434 return mObjects.find(objectID);
435}
436
437void BpBinder::detachObject(const void* objectID)
438{
439 AutoMutex _l(mLock);
440 mObjects.detach(objectID);
441}
442
443BpBinder* BpBinder::remoteBinder()
444{
445 return this;
446}
447
448BpBinder::~BpBinder()
449{
Steven Moreland5553ac42020-11-11 02:14:45 +0000450 ALOGV("Destroying BpBinder %p handle %d\n", this, binderHandle());
451
452 if (CC_UNLIKELY(isRpcBinder())) return;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800453
454 IPCThreadState* ipc = IPCThreadState::self();
455
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700456 if (mTrackedUid >= 0) {
457 AutoMutex _l(sTrackingLock);
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -0700458 uint32_t trackedValue = sTrackingMap[mTrackedUid];
459 if (CC_UNLIKELY((trackedValue & COUNTING_VALUE_MASK) == 0)) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000460 ALOGE("Unexpected Binder Proxy tracking decrement in %p handle %d\n", this,
461 binderHandle());
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700462 } else {
463 if (CC_UNLIKELY(
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -0700464 (trackedValue & LIMIT_REACHED_MASK) &&
465 ((trackedValue & COUNTING_VALUE_MASK) <= sBinderProxyCountLowWatermark)
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700466 )) {
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -0700467 ALOGI("Limit reached bit reset for uid %d (fewer than %d proxies from uid %d held)",
Martijn Coenen3b2091f2021-09-03 18:06:24 +0200468 getuid(), sBinderProxyCountLowWatermark, mTrackedUid);
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -0700469 sTrackingMap[mTrackedUid] &= ~LIMIT_REACHED_MASK;
Martijn Coenen3b2091f2021-09-03 18:06:24 +0200470 sLastLimitCallbackMap.erase(mTrackedUid);
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700471 }
472 if (--sTrackingMap[mTrackedUid] == 0) {
473 sTrackingMap.erase(mTrackedUid);
474 }
475 }
476 }
477
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800478 if (ipc) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000479 ipc->expungeHandle(binderHandle(), this);
480 ipc->decWeakHandle(binderHandle());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800481 }
482}
483
484void BpBinder::onFirstRef()
485{
Steven Moreland5553ac42020-11-11 02:14:45 +0000486 ALOGV("onFirstRef BpBinder %p handle %d\n", this, binderHandle());
487 if (CC_UNLIKELY(isRpcBinder())) return;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800488 IPCThreadState* ipc = IPCThreadState::self();
Steven Moreland5553ac42020-11-11 02:14:45 +0000489 if (ipc) ipc->incStrongHandle(binderHandle(), this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800490}
491
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800492void BpBinder::onLastStrongRef(const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800493{
Steven Moreland5553ac42020-11-11 02:14:45 +0000494 ALOGV("onLastStrongRef BpBinder %p handle %d\n", this, binderHandle());
495 if (CC_UNLIKELY(isRpcBinder())) {
Steven Morelandc9939062021-05-05 17:57:41 +0000496 (void)rpcSession()->sendDecStrong(rpcAddress());
Steven Moreland5553ac42020-11-11 02:14:45 +0000497 return;
498 }
Steve Block6807e592011-10-20 11:56:00 +0100499 IF_ALOGV() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800500 printRefs();
501 }
502 IPCThreadState* ipc = IPCThreadState::self();
Steven Moreland5553ac42020-11-11 02:14:45 +0000503 if (ipc) ipc->decStrongHandle(binderHandle());
Steven Moreland80d23932019-06-07 12:43:27 -0700504
505 mLock.lock();
506 Vector<Obituary>* obits = mObituaries;
507 if(obits != nullptr) {
508 if (!obits->isEmpty()) {
Steven Moreland70fcb4e2019-12-13 17:28:58 -0800509 ALOGI("onLastStrongRef automatically unlinking death recipients: %s",
510 mDescriptorCache.size() ? String8(mDescriptorCache).c_str() : "<uncached descriptor>");
Steven Moreland80d23932019-06-07 12:43:27 -0700511 }
512
Steven Moreland5553ac42020-11-11 02:14:45 +0000513 if (ipc) ipc->clearDeathNotification(binderHandle(), this);
Steven Moreland80d23932019-06-07 12:43:27 -0700514 mObituaries = nullptr;
515 }
516 mLock.unlock();
517
518 if (obits != nullptr) {
519 // XXX Should we tell any remaining DeathRecipient
520 // objects that the last strong ref has gone away, so they
521 // are no longer linked?
522 delete obits;
523 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800524}
525
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800526bool BpBinder::onIncStrongAttempted(uint32_t /*flags*/, const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800527{
Steven Moreland5553ac42020-11-11 02:14:45 +0000528 // RPC binder doesn't currently support inc from weak binders
529 if (CC_UNLIKELY(isRpcBinder())) return false;
530
531 ALOGV("onIncStrongAttempted BpBinder %p handle %d\n", this, binderHandle());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800532 IPCThreadState* ipc = IPCThreadState::self();
Steven Moreland5553ac42020-11-11 02:14:45 +0000533 return ipc ? ipc->attemptIncStrongHandle(binderHandle()) == NO_ERROR : false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800534}
535
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700536uint32_t BpBinder::getBinderProxyCount(uint32_t uid)
537{
538 AutoMutex _l(sTrackingLock);
539 auto it = sTrackingMap.find(uid);
540 if (it != sTrackingMap.end()) {
541 return it->second & COUNTING_VALUE_MASK;
542 }
543 return 0;
544}
545
546void BpBinder::getCountByUid(Vector<uint32_t>& uids, Vector<uint32_t>& counts)
547{
548 AutoMutex _l(sTrackingLock);
549 uids.setCapacity(sTrackingMap.size());
550 counts.setCapacity(sTrackingMap.size());
551 for (const auto& it : sTrackingMap) {
552 uids.push_back(it.first);
553 counts.push_back(it.second & COUNTING_VALUE_MASK);
554 }
555}
556
557void BpBinder::enableCountByUid() { sCountByUidEnabled.store(true); }
558void BpBinder::disableCountByUid() { sCountByUidEnabled.store(false); }
559void BpBinder::setCountByUidEnabled(bool enable) { sCountByUidEnabled.store(enable); }
560
561void BpBinder::setLimitCallback(binder_proxy_limit_callback cb) {
562 AutoMutex _l(sTrackingLock);
563 sLimitCallback = cb;
564}
565
566void BpBinder::setBinderProxyCountWatermarks(int high, int low) {
567 AutoMutex _l(sTrackingLock);
568 sBinderProxyCountHighWatermark = high;
569 sBinderProxyCountLowWatermark = low;
570}
571
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800572// ---------------------------------------------------------------------------
573
Steven Moreland61ff8492019-09-26 16:05:45 -0700574} // namespace android