blob: 1100d72a0c23def10f657a6401b928b4e2a2bc22 [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 Coenena8d509d2021-09-03 18:06:24 +020051// Once the limit has been exceeded, keep calling the limit callback for every this many new proxies
52// created over the limit.
53constexpr uint32_t REPEAT_LIMIT_CALLBACK_INTERVAL = 1000;
54
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -070055enum {
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -070056 LIMIT_REACHED_MASK = 0x80000000, // A flag denoting that the limit has been reached
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -070057 COUNTING_VALUE_MASK = 0x7FFFFFFF, // A mask of the remaining bits for the count value
58};
59
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060BpBinder::ObjectManager::ObjectManager()
61{
62}
63
64BpBinder::ObjectManager::~ObjectManager()
65{
66 kill();
67}
68
Steven Moreland63a2d512021-06-25 01:10:15 +000069void* BpBinder::ObjectManager::attach(const void* objectID, void* object, void* cleanupCookie,
70 IBinder::object_cleanup_func func) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080071 entry_t e;
72 e.object = object;
73 e.cleanupCookie = cleanupCookie;
74 e.func = func;
75
Steven Moreland63a2d512021-06-25 01:10:15 +000076 if (ssize_t idx = mObjects.indexOfKey(objectID); idx >= 0) {
77 ALOGI("Trying to attach object ID %p to binder ObjectManager %p with object %p, but object "
78 "ID already in use",
79 objectID, this, object);
80 return mObjects[idx].object;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080081 }
82
83 mObjects.add(objectID, e);
Steven Moreland63a2d512021-06-25 01:10:15 +000084 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080085}
86
87void* BpBinder::ObjectManager::find(const void* objectID) const
88{
89 const ssize_t i = mObjects.indexOfKey(objectID);
Yi Kongfdd8da92018-06-07 17:52:27 -070090 if (i < 0) return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080091 return mObjects.valueAt(i).object;
92}
93
Steven Moreland63a2d512021-06-25 01:10:15 +000094void* BpBinder::ObjectManager::detach(const void* objectID) {
95 ssize_t idx = mObjects.indexOfKey(objectID);
96 if (idx < 0) return nullptr;
97 void* value = mObjects[idx].object;
98 mObjects.removeItemsAt(idx, 1);
99 return value;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800100}
101
102void BpBinder::ObjectManager::kill()
103{
104 const size_t N = mObjects.size();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700105 ALOGV("Killing %zu objects in manager %p", N, this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800106 for (size_t i=0; i<N; i++) {
107 const entry_t& e = mObjects.valueAt(i);
Yi Kongfdd8da92018-06-07 17:52:27 -0700108 if (e.func != nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800109 e.func(mObjects.keyAt(i), e.object, e.cleanupCookie);
110 }
111 }
112
113 mObjects.clear();
114}
115
116// ---------------------------------------------------------------------------
117
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000118sp<BpBinder> BpBinder::create(int32_t handle) {
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700119 int32_t trackedUid = -1;
120 if (sCountByUidEnabled) {
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700121 trackedUid = IPCThreadState::self()->getCallingUid();
122 AutoMutex _l(sTrackingLock);
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -0700123 uint32_t trackedValue = sTrackingMap[trackedUid];
124 if (CC_UNLIKELY(trackedValue & LIMIT_REACHED_MASK)) {
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700125 if (sBinderProxyThrottleCreate) {
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -0700126 return nullptr;
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700127 }
Martijn Coenena8d509d2021-09-03 18:06:24 +0200128 trackedValue = trackedValue & COUNTING_VALUE_MASK;
129 uint32_t lastLimitCallbackAt = sLastLimitCallbackMap[trackedUid];
130
131 if (trackedValue > lastLimitCallbackAt &&
132 (trackedValue - lastLimitCallbackAt > REPEAT_LIMIT_CALLBACK_INTERVAL)) {
133 ALOGE("Still too many binder proxy objects sent to uid %d from uid %d (%d proxies "
134 "held)",
135 getuid(), trackedUid, trackedValue);
136 if (sLimitCallback) sLimitCallback(trackedUid);
137 sLastLimitCallbackMap[trackedUid] = trackedValue;
138 }
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700139 } else {
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -0700140 if ((trackedValue & COUNTING_VALUE_MASK) >= sBinderProxyCountHighWatermark) {
141 ALOGE("Too many binder proxy objects sent to uid %d from uid %d (%d proxies held)",
142 getuid(), trackedUid, trackedValue);
143 sTrackingMap[trackedUid] |= LIMIT_REACHED_MASK;
144 if (sLimitCallback) sLimitCallback(trackedUid);
Martijn Coenena8d509d2021-09-03 18:06:24 +0200145 sLastLimitCallbackMap[trackedUid] = trackedValue & COUNTING_VALUE_MASK;
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -0700146 if (sBinderProxyThrottleCreate) {
147 ALOGI("Throttling binder proxy creates from uid %d in uid %d until binder proxy"
148 " count drops below %d",
149 trackedUid, getuid(), sBinderProxyCountLowWatermark);
150 return nullptr;
151 }
152 }
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700153 }
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -0700154 sTrackingMap[trackedUid]++;
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700155 }
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000156 return sp<BpBinder>::make(BinderHandle{handle}, trackedUid);
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700157}
158
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000159sp<BpBinder> BpBinder::create(const sp<RpcSession>& session, const RpcAddress& address) {
160 LOG_ALWAYS_FATAL_IF(session == nullptr, "BpBinder::create null session");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800161
Steven Moreland5553ac42020-11-11 02:14:45 +0000162 // These are not currently tracked, since there is no UID or other
163 // identifier to track them with. However, if similar functionality is
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000164 // needed, session objects keep track of all BpBinder objects on a
165 // per-session basis.
Steven Moreland5553ac42020-11-11 02:14:45 +0000166
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000167 return sp<BpBinder>::make(RpcHandle{session, address});
Steven Moreland5553ac42020-11-11 02:14:45 +0000168}
169
170BpBinder::BpBinder(Handle&& handle)
171 : mStability(0),
172 mHandle(handle),
173 mAlive(true),
174 mObitsSent(false),
175 mObituaries(nullptr),
176 mTrackedUid(-1) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800177 extendObjectLifetime(OBJECT_LIFETIME_WEAK);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800178}
179
Steven Moreland5553ac42020-11-11 02:14:45 +0000180BpBinder::BpBinder(BinderHandle&& handle, int32_t trackedUid) : BpBinder(Handle(handle)) {
181 mTrackedUid = trackedUid;
182
183 ALOGV("Creating BpBinder %p handle %d\n", this, this->binderHandle());
184
185 IPCThreadState::self()->incWeakHandle(this->binderHandle(), this);
186}
187
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000188BpBinder::BpBinder(RpcHandle&& handle) : BpBinder(Handle(handle)) {
189 LOG_ALWAYS_FATAL_IF(rpcSession() == nullptr, "BpBinder created w/o session object");
Steven Moreland5553ac42020-11-11 02:14:45 +0000190}
191
192bool BpBinder::isRpcBinder() const {
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000193 return std::holds_alternative<RpcHandle>(mHandle);
Steven Moreland5553ac42020-11-11 02:14:45 +0000194}
195
196const RpcAddress& BpBinder::rpcAddress() const {
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000197 return std::get<RpcHandle>(mHandle).address;
Steven Moreland5553ac42020-11-11 02:14:45 +0000198}
199
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000200const sp<RpcSession>& BpBinder::rpcSession() const {
201 return std::get<RpcHandle>(mHandle).session;
Steven Moreland5553ac42020-11-11 02:14:45 +0000202}
203
204int32_t BpBinder::binderHandle() const {
205 return std::get<BinderHandle>(mHandle).handle;
Steven Moreland85180c02019-07-16 14:24:20 -0700206}
207
Devin Mooref6f2e642021-08-05 19:03:47 +0000208std::optional<int32_t> BpBinder::getDebugBinderHandle() const {
209 if (!isRpcBinder()) {
210 return binderHandle();
211 } else {
212 return std::nullopt;
213 }
214}
215
Mathias Agopian83c04462009-05-22 19:00:22 -0700216bool BpBinder::isDescriptorCached() const {
217 Mutex::Autolock _l(mLock);
218 return mDescriptorCache.size() ? true : false;
219}
220
221const String16& BpBinder::getInterfaceDescriptor() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800222{
Mathias Agopian83c04462009-05-22 19:00:22 -0700223 if (isDescriptorCached() == false) {
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000224 sp<BpBinder> thiz = sp<BpBinder>::fromExisting(const_cast<BpBinder*>(this));
Steven Moreland4cf688f2021-03-31 01:48:58 +0000225
226 Parcel data;
227 data.markForBinder(thiz);
228 Parcel reply;
Mathias Agopian83c04462009-05-22 19:00:22 -0700229 // do the IPC without a lock held.
Steven Moreland4cf688f2021-03-31 01:48:58 +0000230 status_t err = thiz->transact(INTERFACE_TRANSACTION, data, &reply);
Mathias Agopian83c04462009-05-22 19:00:22 -0700231 if (err == NO_ERROR) {
232 String16 res(reply.readString16());
233 Mutex::Autolock _l(mLock);
234 // mDescriptorCache could have been assigned while the lock was
235 // released.
236 if (mDescriptorCache.size() == 0)
237 mDescriptorCache = res;
238 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800239 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700240
Mathias Agopian83c04462009-05-22 19:00:22 -0700241 // we're returning a reference to a non-static object here. Usually this
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700242 // is not something smart to do, however, with binder objects it is
Mathias Agopian83c04462009-05-22 19:00:22 -0700243 // (usually) safe because they are reference-counted.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700244
Mathias Agopian83c04462009-05-22 19:00:22 -0700245 return mDescriptorCache;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800246}
247
248bool BpBinder::isBinderAlive() const
249{
Steven Moreland06074d82020-03-05 23:16:51 +0000250 return mAlive != 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800251}
252
253status_t BpBinder::pingBinder()
254{
Steven Moreland5553ac42020-11-11 02:14:45 +0000255 Parcel data;
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000256 data.markForBinder(sp<BpBinder>::fromExisting(this));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800257 Parcel reply;
Steven Moreland5553ac42020-11-11 02:14:45 +0000258 return transact(PING_TRANSACTION, data, &reply);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800259}
260
261status_t BpBinder::dump(int fd, const Vector<String16>& args)
262{
263 Parcel send;
264 Parcel reply;
265 send.writeFileDescriptor(fd);
266 const size_t numArgs = args.size();
267 send.writeInt32(numArgs);
268 for (size_t i = 0; i < numArgs; i++) {
269 send.writeString16(args[i]);
270 }
271 status_t err = transact(DUMP_TRANSACTION, send, &reply);
272 return err;
273}
274
Jiyong Parkb86c8662018-10-29 23:01:57 +0900275// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800276status_t BpBinder::transact(
277 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
278{
279 // Once a binder has died, it will never come back to life.
280 if (mAlive) {
Steven Moreland46b5fea2019-10-15 11:22:18 -0700281 bool privateVendor = flags & FLAG_PRIVATE_VENDOR;
282 // don't send userspace flags to the kernel
283 flags = flags & ~FLAG_PRIVATE_VENDOR;
284
Steven Moreland6e5a7752019-08-05 20:30:14 -0700285 // user transactions require a given stability level
286 if (code >= FIRST_CALL_TRANSACTION && code <= LAST_CALL_TRANSACTION) {
287 using android::internal::Stability;
288
Steven Moreland16a41062021-07-23 13:35:25 -0700289 int16_t stability = Stability::getRepr(this);
Steven Moreland89ddfc52020-11-13 02:39:26 +0000290 Stability::Level required = privateVendor ? Stability::VENDOR
291 : Stability::getLocalLevel();
Steven Moreland6e5a7752019-08-05 20:30:14 -0700292
Steven Moreland16a41062021-07-23 13:35:25 -0700293 if (CC_UNLIKELY(!Stability::check(stability, required))) {
Steven Morelandb269b582021-02-10 17:09:11 +0000294 ALOGE("Cannot do a user transaction on a %s binder (%s) in a %s context.",
Steven Moreland16a41062021-07-23 13:35:25 -0700295 Stability::levelString(stability).c_str(),
296 String8(getInterfaceDescriptor()).c_str(),
297 Stability::levelString(required).c_str());
Steven Moreland6e5a7752019-08-05 20:30:14 -0700298 return BAD_TYPE;
299 }
300 }
301
Steven Moreland5553ac42020-11-11 02:14:45 +0000302 status_t status;
303 if (CC_UNLIKELY(isRpcBinder())) {
Steven Morelandf5174272021-05-25 00:39:28 +0000304 status = rpcSession()->transact(sp<IBinder>::fromExisting(this), code, data, reply,
305 flags);
Steven Moreland5553ac42020-11-11 02:14:45 +0000306 } else {
307 status = IPCThreadState::self()->transact(binderHandle(), code, data, reply, flags);
308 }
309
Steven Moreland06074d82020-03-05 23:16:51 +0000310 if (status == DEAD_OBJECT) mAlive = 0;
Steven Morelanda86a3562019-08-01 23:28:34 +0000311
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800312 return status;
313 }
314
315 return DEAD_OBJECT;
316}
317
Jiyong Parkb86c8662018-10-29 23:01:57 +0900318// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800319status_t BpBinder::linkToDeath(
320 const sp<DeathRecipient>& recipient, void* cookie, uint32_t flags)
321{
Steven Moreland5553ac42020-11-11 02:14:45 +0000322 if (isRpcBinder()) return UNKNOWN_TRANSACTION;
323
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800324 Obituary ob;
325 ob.recipient = recipient;
326 ob.cookie = cookie;
327 ob.flags = flags;
328
Yi Kongfdd8da92018-06-07 17:52:27 -0700329 LOG_ALWAYS_FATAL_IF(recipient == nullptr,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800330 "linkToDeath(): recipient must be non-NULL");
331
332 {
333 AutoMutex _l(mLock);
334
335 if (!mObitsSent) {
336 if (!mObituaries) {
337 mObituaries = new Vector<Obituary>;
338 if (!mObituaries) {
339 return NO_MEMORY;
340 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000341 ALOGV("Requesting death notification: %p handle %d\n", this, binderHandle());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800342 getWeakRefs()->incWeak(this);
343 IPCThreadState* self = IPCThreadState::self();
Steven Moreland5553ac42020-11-11 02:14:45 +0000344 self->requestDeathNotification(binderHandle(), this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800345 self->flushCommands();
346 }
347 ssize_t res = mObituaries->add(ob);
348 return res >= (ssize_t)NO_ERROR ? (status_t)NO_ERROR : res;
349 }
350 }
351
352 return DEAD_OBJECT;
353}
354
Jiyong Parkb86c8662018-10-29 23:01:57 +0900355// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800356status_t BpBinder::unlinkToDeath(
357 const wp<DeathRecipient>& recipient, void* cookie, uint32_t flags,
358 wp<DeathRecipient>* outRecipient)
359{
Steven Moreland5553ac42020-11-11 02:14:45 +0000360 if (isRpcBinder()) return UNKNOWN_TRANSACTION;
361
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800362 AutoMutex _l(mLock);
363
364 if (mObitsSent) {
365 return DEAD_OBJECT;
366 }
367
368 const size_t N = mObituaries ? mObituaries->size() : 0;
369 for (size_t i=0; i<N; i++) {
370 const Obituary& obit = mObituaries->itemAt(i);
371 if ((obit.recipient == recipient
Yi Kongfdd8da92018-06-07 17:52:27 -0700372 || (recipient == nullptr && obit.cookie == cookie))
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800373 && obit.flags == flags) {
Yi Kongfdd8da92018-06-07 17:52:27 -0700374 if (outRecipient != nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800375 *outRecipient = mObituaries->itemAt(i).recipient;
376 }
377 mObituaries->removeAt(i);
378 if (mObituaries->size() == 0) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000379 ALOGV("Clearing death notification: %p handle %d\n", this, binderHandle());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800380 IPCThreadState* self = IPCThreadState::self();
Steven Moreland5553ac42020-11-11 02:14:45 +0000381 self->clearDeathNotification(binderHandle(), this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800382 self->flushCommands();
383 delete mObituaries;
Yi Kongfdd8da92018-06-07 17:52:27 -0700384 mObituaries = nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800385 }
386 return NO_ERROR;
387 }
388 }
389
390 return NAME_NOT_FOUND;
391}
392
393void BpBinder::sendObituary()
394{
Steven Moreland5553ac42020-11-11 02:14:45 +0000395 LOG_ALWAYS_FATAL_IF(isRpcBinder(), "Cannot send obituary for remote binder.");
396
397 ALOGV("Sending obituary for proxy %p handle %d, mObitsSent=%s\n", this, binderHandle(),
398 mObitsSent ? "true" : "false");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800399
Steven Moreland06074d82020-03-05 23:16:51 +0000400 mAlive = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800401 if (mObitsSent) return;
402
403 mLock.lock();
404 Vector<Obituary>* obits = mObituaries;
Yi Kongfdd8da92018-06-07 17:52:27 -0700405 if(obits != nullptr) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000406 ALOGV("Clearing sent death notification: %p handle %d\n", this, binderHandle());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800407 IPCThreadState* self = IPCThreadState::self();
Steven Moreland5553ac42020-11-11 02:14:45 +0000408 self->clearDeathNotification(binderHandle(), this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800409 self->flushCommands();
Yi Kongfdd8da92018-06-07 17:52:27 -0700410 mObituaries = nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800411 }
Steven Moreland06074d82020-03-05 23:16:51 +0000412 mObitsSent = 1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800413 mLock.unlock();
414
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700415 ALOGV("Reporting death of proxy %p for %zu recipients\n",
416 this, obits ? obits->size() : 0U);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800417
Yi Kongfdd8da92018-06-07 17:52:27 -0700418 if (obits != nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800419 const size_t N = obits->size();
420 for (size_t i=0; i<N; i++) {
421 reportOneDeath(obits->itemAt(i));
422 }
423
424 delete obits;
425 }
426}
427
428void BpBinder::reportOneDeath(const Obituary& obit)
429{
430 sp<DeathRecipient> recipient = obit.recipient.promote();
Steve Block6807e592011-10-20 11:56:00 +0100431 ALOGV("Reporting death to recipient: %p\n", recipient.get());
Yi Kongfdd8da92018-06-07 17:52:27 -0700432 if (recipient == nullptr) return;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800433
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000434 recipient->binderDied(wp<BpBinder>::fromExisting(this));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800435}
436
Steven Moreland63a2d512021-06-25 01:10:15 +0000437void* BpBinder::attachObject(const void* objectID, void* object, void* cleanupCookie,
438 object_cleanup_func func) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800439 AutoMutex _l(mLock);
Steve Block6807e592011-10-20 11:56:00 +0100440 ALOGV("Attaching object %p to binder %p (manager=%p)", object, this, &mObjects);
Steven Moreland63a2d512021-06-25 01:10:15 +0000441 return mObjects.attach(objectID, object, cleanupCookie, func);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800442}
443
444void* BpBinder::findObject(const void* objectID) const
445{
446 AutoMutex _l(mLock);
447 return mObjects.find(objectID);
448}
449
Steven Moreland63a2d512021-06-25 01:10:15 +0000450void* BpBinder::detachObject(const void* objectID) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800451 AutoMutex _l(mLock);
Steven Moreland63a2d512021-06-25 01:10:15 +0000452 return mObjects.detach(objectID);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800453}
454
Steven Moreland9e759e82021-06-25 21:30:23 +0000455void BpBinder::withLock(const std::function<void()>& doWithLock) {
456 AutoMutex _l(mLock);
457 doWithLock();
458}
459
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800460BpBinder* BpBinder::remoteBinder()
461{
462 return this;
463}
464
465BpBinder::~BpBinder()
466{
Steven Moreland5553ac42020-11-11 02:14:45 +0000467 ALOGV("Destroying BpBinder %p handle %d\n", this, binderHandle());
468
469 if (CC_UNLIKELY(isRpcBinder())) return;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800470
471 IPCThreadState* ipc = IPCThreadState::self();
472
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700473 if (mTrackedUid >= 0) {
474 AutoMutex _l(sTrackingLock);
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -0700475 uint32_t trackedValue = sTrackingMap[mTrackedUid];
476 if (CC_UNLIKELY((trackedValue & COUNTING_VALUE_MASK) == 0)) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000477 ALOGE("Unexpected Binder Proxy tracking decrement in %p handle %d\n", this,
478 binderHandle());
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700479 } else {
480 if (CC_UNLIKELY(
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -0700481 (trackedValue & LIMIT_REACHED_MASK) &&
482 ((trackedValue & COUNTING_VALUE_MASK) <= sBinderProxyCountLowWatermark)
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700483 )) {
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -0700484 ALOGI("Limit reached bit reset for uid %d (fewer than %d proxies from uid %d held)",
Martijn Coenena8d509d2021-09-03 18:06:24 +0200485 getuid(), sBinderProxyCountLowWatermark, mTrackedUid);
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -0700486 sTrackingMap[mTrackedUid] &= ~LIMIT_REACHED_MASK;
Martijn Coenena8d509d2021-09-03 18:06:24 +0200487 sLastLimitCallbackMap.erase(mTrackedUid);
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700488 }
489 if (--sTrackingMap[mTrackedUid] == 0) {
490 sTrackingMap.erase(mTrackedUid);
491 }
492 }
493 }
494
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800495 if (ipc) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000496 ipc->expungeHandle(binderHandle(), this);
497 ipc->decWeakHandle(binderHandle());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800498 }
499}
500
501void BpBinder::onFirstRef()
502{
Steven Moreland5553ac42020-11-11 02:14:45 +0000503 ALOGV("onFirstRef BpBinder %p handle %d\n", this, binderHandle());
504 if (CC_UNLIKELY(isRpcBinder())) return;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800505 IPCThreadState* ipc = IPCThreadState::self();
Steven Moreland5553ac42020-11-11 02:14:45 +0000506 if (ipc) ipc->incStrongHandle(binderHandle(), this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800507}
508
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800509void BpBinder::onLastStrongRef(const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800510{
Steven Moreland5553ac42020-11-11 02:14:45 +0000511 ALOGV("onLastStrongRef BpBinder %p handle %d\n", this, binderHandle());
512 if (CC_UNLIKELY(isRpcBinder())) {
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000513 (void)rpcSession()->sendDecStrong(rpcAddress());
Steven Moreland5553ac42020-11-11 02:14:45 +0000514 return;
515 }
Steve Block6807e592011-10-20 11:56:00 +0100516 IF_ALOGV() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800517 printRefs();
518 }
519 IPCThreadState* ipc = IPCThreadState::self();
Steven Moreland5553ac42020-11-11 02:14:45 +0000520 if (ipc) ipc->decStrongHandle(binderHandle());
Steven Moreland80d23932019-06-07 12:43:27 -0700521
522 mLock.lock();
523 Vector<Obituary>* obits = mObituaries;
524 if(obits != nullptr) {
525 if (!obits->isEmpty()) {
Steven Moreland70fcb4e2019-12-13 17:28:58 -0800526 ALOGI("onLastStrongRef automatically unlinking death recipients: %s",
527 mDescriptorCache.size() ? String8(mDescriptorCache).c_str() : "<uncached descriptor>");
Steven Moreland80d23932019-06-07 12:43:27 -0700528 }
529
Steven Moreland5553ac42020-11-11 02:14:45 +0000530 if (ipc) ipc->clearDeathNotification(binderHandle(), this);
Steven Moreland80d23932019-06-07 12:43:27 -0700531 mObituaries = nullptr;
532 }
533 mLock.unlock();
534
535 if (obits != nullptr) {
536 // XXX Should we tell any remaining DeathRecipient
537 // objects that the last strong ref has gone away, so they
538 // are no longer linked?
539 delete obits;
540 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800541}
542
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800543bool BpBinder::onIncStrongAttempted(uint32_t /*flags*/, const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800544{
Steven Moreland5553ac42020-11-11 02:14:45 +0000545 // RPC binder doesn't currently support inc from weak binders
546 if (CC_UNLIKELY(isRpcBinder())) return false;
547
548 ALOGV("onIncStrongAttempted BpBinder %p handle %d\n", this, binderHandle());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800549 IPCThreadState* ipc = IPCThreadState::self();
Steven Moreland5553ac42020-11-11 02:14:45 +0000550 return ipc ? ipc->attemptIncStrongHandle(binderHandle()) == NO_ERROR : false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800551}
552
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700553uint32_t BpBinder::getBinderProxyCount(uint32_t uid)
554{
555 AutoMutex _l(sTrackingLock);
556 auto it = sTrackingMap.find(uid);
557 if (it != sTrackingMap.end()) {
558 return it->second & COUNTING_VALUE_MASK;
559 }
560 return 0;
561}
562
563void BpBinder::getCountByUid(Vector<uint32_t>& uids, Vector<uint32_t>& counts)
564{
565 AutoMutex _l(sTrackingLock);
566 uids.setCapacity(sTrackingMap.size());
567 counts.setCapacity(sTrackingMap.size());
568 for (const auto& it : sTrackingMap) {
569 uids.push_back(it.first);
570 counts.push_back(it.second & COUNTING_VALUE_MASK);
571 }
572}
573
574void BpBinder::enableCountByUid() { sCountByUidEnabled.store(true); }
575void BpBinder::disableCountByUid() { sCountByUidEnabled.store(false); }
576void BpBinder::setCountByUidEnabled(bool enable) { sCountByUidEnabled.store(enable); }
577
578void BpBinder::setLimitCallback(binder_proxy_limit_callback cb) {
579 AutoMutex _l(sTrackingLock);
580 sLimitCallback = cb;
581}
582
583void BpBinder::setBinderProxyCountWatermarks(int high, int low) {
584 AutoMutex _l(sTrackingLock);
585 sBinderProxyCountHighWatermark = high;
586 sBinderProxyCountLowWatermark = low;
587}
588
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800589// ---------------------------------------------------------------------------
590
Steven Moreland61ff8492019-09-26 16:05:45 -0700591} // namespace android