blob: 06542f064ded2724d01ec9aaf8ef0551b4d66aa1 [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
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
Steven Moreland63a2d512021-06-25 01:10:15 +000065void* BpBinder::ObjectManager::attach(const void* objectID, void* object, void* cleanupCookie,
66 IBinder::object_cleanup_func func) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080067 entry_t e;
68 e.object = object;
69 e.cleanupCookie = cleanupCookie;
70 e.func = func;
71
Steven Moreland63a2d512021-06-25 01:10:15 +000072 if (ssize_t idx = mObjects.indexOfKey(objectID); idx >= 0) {
73 ALOGI("Trying to attach object ID %p to binder ObjectManager %p with object %p, but object "
74 "ID already in use",
75 objectID, this, object);
76 return mObjects[idx].object;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080077 }
78
79 mObjects.add(objectID, e);
Steven Moreland63a2d512021-06-25 01:10:15 +000080 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080081}
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
Steven Moreland63a2d512021-06-25 01:10:15 +000090void* BpBinder::ObjectManager::detach(const void* objectID) {
91 ssize_t idx = mObjects.indexOfKey(objectID);
92 if (idx < 0) return nullptr;
93 void* value = mObjects[idx].object;
94 mObjects.removeItemsAt(idx, 1);
95 return value;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080096}
97
98void BpBinder::ObjectManager::kill()
99{
100 const size_t N = mObjects.size();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700101 ALOGV("Killing %zu objects in manager %p", N, this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800102 for (size_t i=0; i<N; i++) {
103 const entry_t& e = mObjects.valueAt(i);
Yi Kongfdd8da92018-06-07 17:52:27 -0700104 if (e.func != nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800105 e.func(mObjects.keyAt(i), e.object, e.cleanupCookie);
106 }
107 }
108
109 mObjects.clear();
110}
111
112// ---------------------------------------------------------------------------
113
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000114sp<BpBinder> BpBinder::create(int32_t handle) {
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700115 int32_t trackedUid = -1;
116 if (sCountByUidEnabled) {
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700117 trackedUid = IPCThreadState::self()->getCallingUid();
118 AutoMutex _l(sTrackingLock);
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -0700119 uint32_t trackedValue = sTrackingMap[trackedUid];
120 if (CC_UNLIKELY(trackedValue & LIMIT_REACHED_MASK)) {
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700121 if (sBinderProxyThrottleCreate) {
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -0700122 return nullptr;
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700123 }
Martijn Coenena8d509d2021-09-03 18:06:24 +0200124 trackedValue = trackedValue & COUNTING_VALUE_MASK;
125 uint32_t lastLimitCallbackAt = sLastLimitCallbackMap[trackedUid];
126
127 if (trackedValue > lastLimitCallbackAt &&
Martijn Coenen6711b6d2021-09-23 09:44:29 +0200128 (trackedValue - lastLimitCallbackAt > sBinderProxyCountHighWatermark)) {
Martijn Coenena8d509d2021-09-03 18:06:24 +0200129 ALOGE("Still too many binder proxy objects sent to uid %d from uid %d (%d proxies "
130 "held)",
131 getuid(), trackedUid, trackedValue);
132 if (sLimitCallback) sLimitCallback(trackedUid);
133 sLastLimitCallbackMap[trackedUid] = trackedValue;
134 }
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700135 } else {
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -0700136 if ((trackedValue & COUNTING_VALUE_MASK) >= sBinderProxyCountHighWatermark) {
137 ALOGE("Too many binder proxy objects sent to uid %d from uid %d (%d proxies held)",
138 getuid(), trackedUid, trackedValue);
139 sTrackingMap[trackedUid] |= LIMIT_REACHED_MASK;
140 if (sLimitCallback) sLimitCallback(trackedUid);
Martijn Coenena8d509d2021-09-03 18:06:24 +0200141 sLastLimitCallbackMap[trackedUid] = trackedValue & COUNTING_VALUE_MASK;
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -0700142 if (sBinderProxyThrottleCreate) {
143 ALOGI("Throttling binder proxy creates from uid %d in uid %d until binder proxy"
144 " count drops below %d",
145 trackedUid, getuid(), sBinderProxyCountLowWatermark);
146 return nullptr;
147 }
148 }
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700149 }
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -0700150 sTrackingMap[trackedUid]++;
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700151 }
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000152 return sp<BpBinder>::make(BinderHandle{handle}, trackedUid);
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700153}
154
Steven Moreland5623d1a2021-09-10 15:45:34 -0700155sp<BpBinder> BpBinder::create(const sp<RpcSession>& session, uint64_t address) {
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000156 LOG_ALWAYS_FATAL_IF(session == nullptr, "BpBinder::create null session");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800157
Steven Moreland5553ac42020-11-11 02:14:45 +0000158 // These are not currently tracked, since there is no UID or other
159 // identifier to track them with. However, if similar functionality is
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000160 // needed, session objects keep track of all BpBinder objects on a
161 // per-session basis.
Steven Moreland5553ac42020-11-11 02:14:45 +0000162
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000163 return sp<BpBinder>::make(RpcHandle{session, address});
Steven Moreland5553ac42020-11-11 02:14:45 +0000164}
165
166BpBinder::BpBinder(Handle&& handle)
167 : mStability(0),
168 mHandle(handle),
169 mAlive(true),
170 mObitsSent(false),
171 mObituaries(nullptr),
172 mTrackedUid(-1) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800173 extendObjectLifetime(OBJECT_LIFETIME_WEAK);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800174}
175
Steven Moreland5553ac42020-11-11 02:14:45 +0000176BpBinder::BpBinder(BinderHandle&& handle, int32_t trackedUid) : BpBinder(Handle(handle)) {
177 mTrackedUid = trackedUid;
178
179 ALOGV("Creating BpBinder %p handle %d\n", this, this->binderHandle());
180
181 IPCThreadState::self()->incWeakHandle(this->binderHandle(), this);
182}
183
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000184BpBinder::BpBinder(RpcHandle&& handle) : BpBinder(Handle(handle)) {
185 LOG_ALWAYS_FATAL_IF(rpcSession() == nullptr, "BpBinder created w/o session object");
Steven Moreland5553ac42020-11-11 02:14:45 +0000186}
187
188bool BpBinder::isRpcBinder() const {
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000189 return std::holds_alternative<RpcHandle>(mHandle);
Steven Moreland5553ac42020-11-11 02:14:45 +0000190}
191
Steven Moreland5623d1a2021-09-10 15:45:34 -0700192uint64_t BpBinder::rpcAddress() const {
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000193 return std::get<RpcHandle>(mHandle).address;
Steven Moreland5553ac42020-11-11 02:14:45 +0000194}
195
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000196const sp<RpcSession>& BpBinder::rpcSession() const {
197 return std::get<RpcHandle>(mHandle).session;
Steven Moreland5553ac42020-11-11 02:14:45 +0000198}
199
200int32_t BpBinder::binderHandle() const {
201 return std::get<BinderHandle>(mHandle).handle;
Steven Moreland85180c02019-07-16 14:24:20 -0700202}
203
Devin Mooref6f2e642021-08-05 19:03:47 +0000204std::optional<int32_t> BpBinder::getDebugBinderHandle() const {
205 if (!isRpcBinder()) {
206 return binderHandle();
207 } else {
208 return std::nullopt;
209 }
210}
211
Mathias Agopian83c04462009-05-22 19:00:22 -0700212bool BpBinder::isDescriptorCached() const {
213 Mutex::Autolock _l(mLock);
214 return mDescriptorCache.size() ? true : false;
215}
216
217const String16& BpBinder::getInterfaceDescriptor() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800218{
Mathias Agopian83c04462009-05-22 19:00:22 -0700219 if (isDescriptorCached() == false) {
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000220 sp<BpBinder> thiz = sp<BpBinder>::fromExisting(const_cast<BpBinder*>(this));
Steven Moreland4cf688f2021-03-31 01:48:58 +0000221
222 Parcel data;
223 data.markForBinder(thiz);
224 Parcel reply;
Mathias Agopian83c04462009-05-22 19:00:22 -0700225 // do the IPC without a lock held.
Steven Moreland4cf688f2021-03-31 01:48:58 +0000226 status_t err = thiz->transact(INTERFACE_TRANSACTION, data, &reply);
Mathias Agopian83c04462009-05-22 19:00:22 -0700227 if (err == NO_ERROR) {
228 String16 res(reply.readString16());
229 Mutex::Autolock _l(mLock);
230 // mDescriptorCache could have been assigned while the lock was
231 // released.
232 if (mDescriptorCache.size() == 0)
233 mDescriptorCache = res;
234 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800235 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700236
Mathias Agopian83c04462009-05-22 19:00:22 -0700237 // we're returning a reference to a non-static object here. Usually this
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700238 // is not something smart to do, however, with binder objects it is
Mathias Agopian83c04462009-05-22 19:00:22 -0700239 // (usually) safe because they are reference-counted.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700240
Mathias Agopian83c04462009-05-22 19:00:22 -0700241 return mDescriptorCache;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800242}
243
244bool BpBinder::isBinderAlive() const
245{
Steven Moreland06074d82020-03-05 23:16:51 +0000246 return mAlive != 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800247}
248
249status_t BpBinder::pingBinder()
250{
Steven Moreland5553ac42020-11-11 02:14:45 +0000251 Parcel data;
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000252 data.markForBinder(sp<BpBinder>::fromExisting(this));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800253 Parcel reply;
Steven Moreland5553ac42020-11-11 02:14:45 +0000254 return transact(PING_TRANSACTION, data, &reply);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800255}
256
257status_t BpBinder::dump(int fd, const Vector<String16>& args)
258{
259 Parcel send;
260 Parcel reply;
261 send.writeFileDescriptor(fd);
262 const size_t numArgs = args.size();
263 send.writeInt32(numArgs);
264 for (size_t i = 0; i < numArgs; i++) {
265 send.writeString16(args[i]);
266 }
267 status_t err = transact(DUMP_TRANSACTION, send, &reply);
268 return err;
269}
270
Jiyong Parkb86c8662018-10-29 23:01:57 +0900271// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800272status_t BpBinder::transact(
273 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
274{
275 // Once a binder has died, it will never come back to life.
276 if (mAlive) {
Steven Moreland46b5fea2019-10-15 11:22:18 -0700277 bool privateVendor = flags & FLAG_PRIVATE_VENDOR;
278 // don't send userspace flags to the kernel
279 flags = flags & ~FLAG_PRIVATE_VENDOR;
280
Steven Moreland6e5a7752019-08-05 20:30:14 -0700281 // user transactions require a given stability level
282 if (code >= FIRST_CALL_TRANSACTION && code <= LAST_CALL_TRANSACTION) {
283 using android::internal::Stability;
284
Steven Moreland16a41062021-07-23 13:35:25 -0700285 int16_t stability = Stability::getRepr(this);
Steven Moreland89ddfc52020-11-13 02:39:26 +0000286 Stability::Level required = privateVendor ? Stability::VENDOR
287 : Stability::getLocalLevel();
Steven Moreland6e5a7752019-08-05 20:30:14 -0700288
Steven Moreland16a41062021-07-23 13:35:25 -0700289 if (CC_UNLIKELY(!Stability::check(stability, required))) {
Steven Morelandb269b582021-02-10 17:09:11 +0000290 ALOGE("Cannot do a user transaction on a %s binder (%s) in a %s context.",
Steven Moreland16a41062021-07-23 13:35:25 -0700291 Stability::levelString(stability).c_str(),
292 String8(getInterfaceDescriptor()).c_str(),
293 Stability::levelString(required).c_str());
Steven Moreland6e5a7752019-08-05 20:30:14 -0700294 return BAD_TYPE;
295 }
296 }
297
Steven Moreland5553ac42020-11-11 02:14:45 +0000298 status_t status;
299 if (CC_UNLIKELY(isRpcBinder())) {
Steven Morelandf5174272021-05-25 00:39:28 +0000300 status = rpcSession()->transact(sp<IBinder>::fromExisting(this), code, data, reply,
301 flags);
Steven Moreland5553ac42020-11-11 02:14:45 +0000302 } else {
303 status = IPCThreadState::self()->transact(binderHandle(), code, data, reply, flags);
304 }
305
Steven Moreland06074d82020-03-05 23:16:51 +0000306 if (status == DEAD_OBJECT) mAlive = 0;
Steven Morelanda86a3562019-08-01 23:28:34 +0000307
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800308 return status;
309 }
310
311 return DEAD_OBJECT;
312}
313
Jiyong Parkb86c8662018-10-29 23:01:57 +0900314// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800315status_t BpBinder::linkToDeath(
316 const sp<DeathRecipient>& recipient, void* cookie, uint32_t flags)
317{
Steven Moreland5553ac42020-11-11 02:14:45 +0000318 if (isRpcBinder()) return UNKNOWN_TRANSACTION;
319
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800320 Obituary ob;
321 ob.recipient = recipient;
322 ob.cookie = cookie;
323 ob.flags = flags;
324
Yi Kongfdd8da92018-06-07 17:52:27 -0700325 LOG_ALWAYS_FATAL_IF(recipient == nullptr,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800326 "linkToDeath(): recipient must be non-NULL");
327
328 {
329 AutoMutex _l(mLock);
330
331 if (!mObitsSent) {
332 if (!mObituaries) {
333 mObituaries = new Vector<Obituary>;
334 if (!mObituaries) {
335 return NO_MEMORY;
336 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000337 ALOGV("Requesting death notification: %p handle %d\n", this, binderHandle());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800338 getWeakRefs()->incWeak(this);
339 IPCThreadState* self = IPCThreadState::self();
Steven Moreland5553ac42020-11-11 02:14:45 +0000340 self->requestDeathNotification(binderHandle(), this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800341 self->flushCommands();
342 }
343 ssize_t res = mObituaries->add(ob);
344 return res >= (ssize_t)NO_ERROR ? (status_t)NO_ERROR : res;
345 }
346 }
347
348 return DEAD_OBJECT;
349}
350
Jiyong Parkb86c8662018-10-29 23:01:57 +0900351// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800352status_t BpBinder::unlinkToDeath(
353 const wp<DeathRecipient>& recipient, void* cookie, uint32_t flags,
354 wp<DeathRecipient>* outRecipient)
355{
Steven Moreland5553ac42020-11-11 02:14:45 +0000356 if (isRpcBinder()) return UNKNOWN_TRANSACTION;
357
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800358 AutoMutex _l(mLock);
359
360 if (mObitsSent) {
361 return DEAD_OBJECT;
362 }
363
364 const size_t N = mObituaries ? mObituaries->size() : 0;
365 for (size_t i=0; i<N; i++) {
366 const Obituary& obit = mObituaries->itemAt(i);
367 if ((obit.recipient == recipient
Yi Kongfdd8da92018-06-07 17:52:27 -0700368 || (recipient == nullptr && obit.cookie == cookie))
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800369 && obit.flags == flags) {
Yi Kongfdd8da92018-06-07 17:52:27 -0700370 if (outRecipient != nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800371 *outRecipient = mObituaries->itemAt(i).recipient;
372 }
373 mObituaries->removeAt(i);
374 if (mObituaries->size() == 0) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000375 ALOGV("Clearing death notification: %p handle %d\n", this, binderHandle());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800376 IPCThreadState* self = IPCThreadState::self();
Steven Moreland5553ac42020-11-11 02:14:45 +0000377 self->clearDeathNotification(binderHandle(), this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800378 self->flushCommands();
379 delete mObituaries;
Yi Kongfdd8da92018-06-07 17:52:27 -0700380 mObituaries = nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800381 }
382 return NO_ERROR;
383 }
384 }
385
386 return NAME_NOT_FOUND;
387}
388
389void BpBinder::sendObituary()
390{
Steven Moreland5553ac42020-11-11 02:14:45 +0000391 LOG_ALWAYS_FATAL_IF(isRpcBinder(), "Cannot send obituary for remote binder.");
392
393 ALOGV("Sending obituary for proxy %p handle %d, mObitsSent=%s\n", this, binderHandle(),
394 mObitsSent ? "true" : "false");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800395
Steven Moreland06074d82020-03-05 23:16:51 +0000396 mAlive = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800397 if (mObitsSent) return;
398
399 mLock.lock();
400 Vector<Obituary>* obits = mObituaries;
Yi Kongfdd8da92018-06-07 17:52:27 -0700401 if(obits != nullptr) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000402 ALOGV("Clearing sent death notification: %p handle %d\n", this, binderHandle());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800403 IPCThreadState* self = IPCThreadState::self();
Steven Moreland5553ac42020-11-11 02:14:45 +0000404 self->clearDeathNotification(binderHandle(), this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800405 self->flushCommands();
Yi Kongfdd8da92018-06-07 17:52:27 -0700406 mObituaries = nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800407 }
Steven Moreland06074d82020-03-05 23:16:51 +0000408 mObitsSent = 1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800409 mLock.unlock();
410
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700411 ALOGV("Reporting death of proxy %p for %zu recipients\n",
412 this, obits ? obits->size() : 0U);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800413
Yi Kongfdd8da92018-06-07 17:52:27 -0700414 if (obits != nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800415 const size_t N = obits->size();
416 for (size_t i=0; i<N; i++) {
417 reportOneDeath(obits->itemAt(i));
418 }
419
420 delete obits;
421 }
422}
423
424void BpBinder::reportOneDeath(const Obituary& obit)
425{
426 sp<DeathRecipient> recipient = obit.recipient.promote();
Steve Block6807e592011-10-20 11:56:00 +0100427 ALOGV("Reporting death to recipient: %p\n", recipient.get());
Yi Kongfdd8da92018-06-07 17:52:27 -0700428 if (recipient == nullptr) return;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800429
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000430 recipient->binderDied(wp<BpBinder>::fromExisting(this));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800431}
432
Steven Moreland63a2d512021-06-25 01:10:15 +0000433void* BpBinder::attachObject(const void* objectID, void* object, void* cleanupCookie,
434 object_cleanup_func func) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800435 AutoMutex _l(mLock);
Steve Block6807e592011-10-20 11:56:00 +0100436 ALOGV("Attaching object %p to binder %p (manager=%p)", object, this, &mObjects);
Steven Moreland63a2d512021-06-25 01:10:15 +0000437 return mObjects.attach(objectID, object, cleanupCookie, func);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800438}
439
440void* BpBinder::findObject(const void* objectID) const
441{
442 AutoMutex _l(mLock);
443 return mObjects.find(objectID);
444}
445
Steven Moreland63a2d512021-06-25 01:10:15 +0000446void* BpBinder::detachObject(const void* objectID) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800447 AutoMutex _l(mLock);
Steven Moreland63a2d512021-06-25 01:10:15 +0000448 return mObjects.detach(objectID);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800449}
450
Steven Moreland9e759e82021-06-25 21:30:23 +0000451void BpBinder::withLock(const std::function<void()>& doWithLock) {
452 AutoMutex _l(mLock);
453 doWithLock();
454}
455
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800456BpBinder* BpBinder::remoteBinder()
457{
458 return this;
459}
460
461BpBinder::~BpBinder()
462{
Steven Moreland5553ac42020-11-11 02:14:45 +0000463 ALOGV("Destroying BpBinder %p handle %d\n", this, binderHandle());
464
465 if (CC_UNLIKELY(isRpcBinder())) return;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800466
467 IPCThreadState* ipc = IPCThreadState::self();
468
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700469 if (mTrackedUid >= 0) {
470 AutoMutex _l(sTrackingLock);
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -0700471 uint32_t trackedValue = sTrackingMap[mTrackedUid];
472 if (CC_UNLIKELY((trackedValue & COUNTING_VALUE_MASK) == 0)) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000473 ALOGE("Unexpected Binder Proxy tracking decrement in %p handle %d\n", this,
474 binderHandle());
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700475 } else {
476 if (CC_UNLIKELY(
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -0700477 (trackedValue & LIMIT_REACHED_MASK) &&
478 ((trackedValue & COUNTING_VALUE_MASK) <= sBinderProxyCountLowWatermark)
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700479 )) {
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -0700480 ALOGI("Limit reached bit reset for uid %d (fewer than %d proxies from uid %d held)",
Martijn Coenena8d509d2021-09-03 18:06:24 +0200481 getuid(), sBinderProxyCountLowWatermark, mTrackedUid);
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -0700482 sTrackingMap[mTrackedUid] &= ~LIMIT_REACHED_MASK;
Martijn Coenena8d509d2021-09-03 18:06:24 +0200483 sLastLimitCallbackMap.erase(mTrackedUid);
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700484 }
485 if (--sTrackingMap[mTrackedUid] == 0) {
486 sTrackingMap.erase(mTrackedUid);
487 }
488 }
489 }
490
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800491 if (ipc) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000492 ipc->expungeHandle(binderHandle(), this);
493 ipc->decWeakHandle(binderHandle());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800494 }
495}
496
497void BpBinder::onFirstRef()
498{
Steven Moreland5553ac42020-11-11 02:14:45 +0000499 ALOGV("onFirstRef BpBinder %p handle %d\n", this, binderHandle());
500 if (CC_UNLIKELY(isRpcBinder())) return;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800501 IPCThreadState* ipc = IPCThreadState::self();
Steven Moreland5553ac42020-11-11 02:14:45 +0000502 if (ipc) ipc->incStrongHandle(binderHandle(), this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800503}
504
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800505void BpBinder::onLastStrongRef(const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800506{
Steven Moreland5553ac42020-11-11 02:14:45 +0000507 ALOGV("onLastStrongRef BpBinder %p handle %d\n", this, binderHandle());
508 if (CC_UNLIKELY(isRpcBinder())) {
Steven Moreland4f622fe2021-09-13 17:38:09 -0700509 (void)rpcSession()->sendDecStrong(this);
Steven Moreland5553ac42020-11-11 02:14:45 +0000510 return;
511 }
Steve Block6807e592011-10-20 11:56:00 +0100512 IF_ALOGV() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800513 printRefs();
514 }
515 IPCThreadState* ipc = IPCThreadState::self();
Steven Moreland5553ac42020-11-11 02:14:45 +0000516 if (ipc) ipc->decStrongHandle(binderHandle());
Steven Moreland80d23932019-06-07 12:43:27 -0700517
518 mLock.lock();
519 Vector<Obituary>* obits = mObituaries;
520 if(obits != nullptr) {
521 if (!obits->isEmpty()) {
Steven Moreland70fcb4e2019-12-13 17:28:58 -0800522 ALOGI("onLastStrongRef automatically unlinking death recipients: %s",
523 mDescriptorCache.size() ? String8(mDescriptorCache).c_str() : "<uncached descriptor>");
Steven Moreland80d23932019-06-07 12:43:27 -0700524 }
525
Steven Moreland5553ac42020-11-11 02:14:45 +0000526 if (ipc) ipc->clearDeathNotification(binderHandle(), this);
Steven Moreland80d23932019-06-07 12:43:27 -0700527 mObituaries = nullptr;
528 }
529 mLock.unlock();
530
531 if (obits != nullptr) {
532 // XXX Should we tell any remaining DeathRecipient
533 // objects that the last strong ref has gone away, so they
534 // are no longer linked?
535 delete obits;
536 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800537}
538
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800539bool BpBinder::onIncStrongAttempted(uint32_t /*flags*/, const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800540{
Steven Moreland5553ac42020-11-11 02:14:45 +0000541 // RPC binder doesn't currently support inc from weak binders
542 if (CC_UNLIKELY(isRpcBinder())) return false;
543
544 ALOGV("onIncStrongAttempted BpBinder %p handle %d\n", this, binderHandle());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800545 IPCThreadState* ipc = IPCThreadState::self();
Steven Moreland5553ac42020-11-11 02:14:45 +0000546 return ipc ? ipc->attemptIncStrongHandle(binderHandle()) == NO_ERROR : false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800547}
548
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700549uint32_t BpBinder::getBinderProxyCount(uint32_t uid)
550{
551 AutoMutex _l(sTrackingLock);
552 auto it = sTrackingMap.find(uid);
553 if (it != sTrackingMap.end()) {
554 return it->second & COUNTING_VALUE_MASK;
555 }
556 return 0;
557}
558
559void BpBinder::getCountByUid(Vector<uint32_t>& uids, Vector<uint32_t>& counts)
560{
561 AutoMutex _l(sTrackingLock);
562 uids.setCapacity(sTrackingMap.size());
563 counts.setCapacity(sTrackingMap.size());
564 for (const auto& it : sTrackingMap) {
565 uids.push_back(it.first);
566 counts.push_back(it.second & COUNTING_VALUE_MASK);
567 }
568}
569
570void BpBinder::enableCountByUid() { sCountByUidEnabled.store(true); }
571void BpBinder::disableCountByUid() { sCountByUidEnabled.store(false); }
572void BpBinder::setCountByUidEnabled(bool enable) { sCountByUidEnabled.store(enable); }
573
574void BpBinder::setLimitCallback(binder_proxy_limit_callback cb) {
575 AutoMutex _l(sTrackingLock);
576 sLimitCallback = cb;
577}
578
579void BpBinder::setBinderProxyCountWatermarks(int high, int low) {
580 AutoMutex _l(sTrackingLock);
581 sBinderProxyCountHighWatermark = high;
582 sBinderProxyCountLowWatermark = low;
583}
584
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800585// ---------------------------------------------------------------------------
586
Steven Moreland61ff8492019-09-26 16:05:45 -0700587} // namespace android