blob: c8de373af2d814609057d23bf794cd2af768023a [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>
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -070024#include <cutils/compiler.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080025#include <utils/Log.h>
26
27#include <stdio.h>
28
Steve Block6807e592011-10-20 11:56:00 +010029//#undef ALOGV
30//#define ALOGV(...) fprintf(stderr, __VA_ARGS__)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080031
32namespace android {
33
34// ---------------------------------------------------------------------------
35
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -070036Mutex BpBinder::sTrackingLock;
37std::unordered_map<int32_t,uint32_t> BpBinder::sTrackingMap;
38int BpBinder::sNumTrackedUids = 0;
39std::atomic_bool BpBinder::sCountByUidEnabled(false);
40binder_proxy_limit_callback BpBinder::sLimitCallback;
41bool BpBinder::sBinderProxyThrottleCreate = false;
42
43// Arbitrarily high value that probably distinguishes a bad behaving app
44uint32_t BpBinder::sBinderProxyCountHighWatermark = 2500;
45// Another arbitrary value a binder count needs to drop below before another callback will be called
46uint32_t BpBinder::sBinderProxyCountLowWatermark = 2000;
47
48enum {
49 CALLBACK_TRIGGERED_MASK = 0x80000000, // A flag denoting that the callback has been called
50 COUNTING_VALUE_MASK = 0x7FFFFFFF, // A mask of the remaining bits for the count value
51};
52
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053BpBinder::ObjectManager::ObjectManager()
54{
55}
56
57BpBinder::ObjectManager::~ObjectManager()
58{
59 kill();
60}
61
62void BpBinder::ObjectManager::attach(
63 const void* objectID, void* object, void* cleanupCookie,
64 IBinder::object_cleanup_func func)
65{
66 entry_t e;
67 e.object = object;
68 e.cleanupCookie = cleanupCookie;
69 e.func = func;
70
71 if (mObjects.indexOfKey(objectID) >= 0) {
Steve Blocke6f43dd2012-01-06 19:20:56 +000072 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 -080073 objectID, this, object);
74 return;
75 }
76
77 mObjects.add(objectID, e);
78}
79
80void* BpBinder::ObjectManager::find(const void* objectID) const
81{
82 const ssize_t i = mObjects.indexOfKey(objectID);
83 if (i < 0) return NULL;
84 return mObjects.valueAt(i).object;
85}
86
87void BpBinder::ObjectManager::detach(const void* objectID)
88{
89 mObjects.removeItem(objectID);
90}
91
92void BpBinder::ObjectManager::kill()
93{
94 const size_t N = mObjects.size();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -070095 ALOGV("Killing %zu objects in manager %p", N, this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080096 for (size_t i=0; i<N; i++) {
97 const entry_t& e = mObjects.valueAt(i);
98 if (e.func != NULL) {
99 e.func(mObjects.keyAt(i), e.object, e.cleanupCookie);
100 }
101 }
102
103 mObjects.clear();
104}
105
106// ---------------------------------------------------------------------------
107
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700108
109BpBinder* BpBinder::create(int32_t handle) {
110 int32_t trackedUid = -1;
111 if (sCountByUidEnabled) {
112 BpBinder* out;
113 trackedUid = IPCThreadState::self()->getCallingUid();
114 AutoMutex _l(sTrackingLock);
115 if ((sTrackingMap[trackedUid] & COUNTING_VALUE_MASK) >= sBinderProxyCountHighWatermark) {
116 ALOGE("Too many binder proxy objects sent to uid %d from uid %d (over %d proxies held)",
117 getuid(), trackedUid, sBinderProxyCountHighWatermark);
118
119 if (sBinderProxyThrottleCreate) {
120 ALOGE("Returning Null Binder Proxy Object to uid %d", trackedUid);
121 out = nullptr;
122 } else {
123 // increment and construct here in case callback has an async kill causing a race
124 sTrackingMap[trackedUid]++;
125 out = new BpBinder(handle, trackedUid);
126 }
127
128 if (sLimitCallback && !(sTrackingMap[trackedUid] & CALLBACK_TRIGGERED_MASK)) {
129 sTrackingMap[trackedUid] |= CALLBACK_TRIGGERED_MASK;
130 sLimitCallback(trackedUid);
131 }
132 } else {
133 sTrackingMap[trackedUid]++;
134 out = new BpBinder(handle, trackedUid);
135 }
136
137 return out;
138 } else {
139 return new BpBinder(handle, trackedUid);
140 }
141}
142
143BpBinder::BpBinder(int32_t handle, int32_t trackedUid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800144 : mHandle(handle)
145 , mAlive(1)
146 , mObitsSent(0)
147 , mObituaries(NULL)
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700148 , mTrackedUid(trackedUid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800149{
Steve Block6807e592011-10-20 11:56:00 +0100150 ALOGV("Creating BpBinder %p handle %d\n", this, mHandle);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800151
152 extendObjectLifetime(OBJECT_LIFETIME_WEAK);
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700153 IPCThreadState::self()->incWeakHandle(handle, this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800154}
155
Mathias Agopian83c04462009-05-22 19:00:22 -0700156bool BpBinder::isDescriptorCached() const {
157 Mutex::Autolock _l(mLock);
158 return mDescriptorCache.size() ? true : false;
159}
160
161const String16& BpBinder::getInterfaceDescriptor() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800162{
Mathias Agopian83c04462009-05-22 19:00:22 -0700163 if (isDescriptorCached() == false) {
164 Parcel send, reply;
165 // do the IPC without a lock held.
166 status_t err = const_cast<BpBinder*>(this)->transact(
167 INTERFACE_TRANSACTION, send, &reply);
168 if (err == NO_ERROR) {
169 String16 res(reply.readString16());
170 Mutex::Autolock _l(mLock);
171 // mDescriptorCache could have been assigned while the lock was
172 // released.
173 if (mDescriptorCache.size() == 0)
174 mDescriptorCache = res;
175 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800176 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700177
Mathias Agopian83c04462009-05-22 19:00:22 -0700178 // we're returning a reference to a non-static object here. Usually this
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700179 // is not something smart to do, however, with binder objects it is
Mathias Agopian83c04462009-05-22 19:00:22 -0700180 // (usually) safe because they are reference-counted.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700181
Mathias Agopian83c04462009-05-22 19:00:22 -0700182 return mDescriptorCache;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800183}
184
185bool BpBinder::isBinderAlive() const
186{
187 return mAlive != 0;
188}
189
190status_t BpBinder::pingBinder()
191{
192 Parcel send;
193 Parcel reply;
194 status_t err = transact(PING_TRANSACTION, send, &reply);
195 if (err != NO_ERROR) return err;
196 if (reply.dataSize() < sizeof(status_t)) return NOT_ENOUGH_DATA;
197 return (status_t)reply.readInt32();
198}
199
200status_t BpBinder::dump(int fd, const Vector<String16>& args)
201{
202 Parcel send;
203 Parcel reply;
204 send.writeFileDescriptor(fd);
205 const size_t numArgs = args.size();
206 send.writeInt32(numArgs);
207 for (size_t i = 0; i < numArgs; i++) {
208 send.writeString16(args[i]);
209 }
210 status_t err = transact(DUMP_TRANSACTION, send, &reply);
211 return err;
212}
213
214status_t BpBinder::transact(
215 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
216{
217 // Once a binder has died, it will never come back to life.
218 if (mAlive) {
219 status_t status = IPCThreadState::self()->transact(
220 mHandle, code, data, reply, flags);
221 if (status == DEAD_OBJECT) mAlive = 0;
222 return status;
223 }
224
225 return DEAD_OBJECT;
226}
227
228status_t BpBinder::linkToDeath(
229 const sp<DeathRecipient>& recipient, void* cookie, uint32_t flags)
230{
231 Obituary ob;
232 ob.recipient = recipient;
233 ob.cookie = cookie;
234 ob.flags = flags;
235
236 LOG_ALWAYS_FATAL_IF(recipient == NULL,
237 "linkToDeath(): recipient must be non-NULL");
238
239 {
240 AutoMutex _l(mLock);
241
242 if (!mObitsSent) {
243 if (!mObituaries) {
244 mObituaries = new Vector<Obituary>;
245 if (!mObituaries) {
246 return NO_MEMORY;
247 }
Steve Block6807e592011-10-20 11:56:00 +0100248 ALOGV("Requesting death notification: %p handle %d\n", this, mHandle);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800249 getWeakRefs()->incWeak(this);
250 IPCThreadState* self = IPCThreadState::self();
251 self->requestDeathNotification(mHandle, this);
252 self->flushCommands();
253 }
254 ssize_t res = mObituaries->add(ob);
255 return res >= (ssize_t)NO_ERROR ? (status_t)NO_ERROR : res;
256 }
257 }
258
259 return DEAD_OBJECT;
260}
261
262status_t BpBinder::unlinkToDeath(
263 const wp<DeathRecipient>& recipient, void* cookie, uint32_t flags,
264 wp<DeathRecipient>* outRecipient)
265{
266 AutoMutex _l(mLock);
267
268 if (mObitsSent) {
269 return DEAD_OBJECT;
270 }
271
272 const size_t N = mObituaries ? mObituaries->size() : 0;
273 for (size_t i=0; i<N; i++) {
274 const Obituary& obit = mObituaries->itemAt(i);
275 if ((obit.recipient == recipient
276 || (recipient == NULL && obit.cookie == cookie))
277 && obit.flags == flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800278 if (outRecipient != NULL) {
279 *outRecipient = mObituaries->itemAt(i).recipient;
280 }
281 mObituaries->removeAt(i);
282 if (mObituaries->size() == 0) {
Steve Block6807e592011-10-20 11:56:00 +0100283 ALOGV("Clearing death notification: %p handle %d\n", this, mHandle);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800284 IPCThreadState* self = IPCThreadState::self();
285 self->clearDeathNotification(mHandle, this);
286 self->flushCommands();
287 delete mObituaries;
288 mObituaries = NULL;
289 }
290 return NO_ERROR;
291 }
292 }
293
294 return NAME_NOT_FOUND;
295}
296
297void BpBinder::sendObituary()
298{
Steve Block6807e592011-10-20 11:56:00 +0100299 ALOGV("Sending obituary for proxy %p handle %d, mObitsSent=%s\n",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800300 this, mHandle, mObitsSent ? "true" : "false");
301
302 mAlive = 0;
303 if (mObitsSent) return;
304
305 mLock.lock();
306 Vector<Obituary>* obits = mObituaries;
307 if(obits != NULL) {
Steve Block6807e592011-10-20 11:56:00 +0100308 ALOGV("Clearing sent death notification: %p handle %d\n", this, mHandle);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800309 IPCThreadState* self = IPCThreadState::self();
310 self->clearDeathNotification(mHandle, this);
311 self->flushCommands();
312 mObituaries = NULL;
313 }
314 mObitsSent = 1;
315 mLock.unlock();
316
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700317 ALOGV("Reporting death of proxy %p for %zu recipients\n",
318 this, obits ? obits->size() : 0U);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800319
320 if (obits != NULL) {
321 const size_t N = obits->size();
322 for (size_t i=0; i<N; i++) {
323 reportOneDeath(obits->itemAt(i));
324 }
325
326 delete obits;
327 }
328}
329
330void BpBinder::reportOneDeath(const Obituary& obit)
331{
332 sp<DeathRecipient> recipient = obit.recipient.promote();
Steve Block6807e592011-10-20 11:56:00 +0100333 ALOGV("Reporting death to recipient: %p\n", recipient.get());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800334 if (recipient == NULL) return;
335
336 recipient->binderDied(this);
337}
338
339
340void BpBinder::attachObject(
341 const void* objectID, void* object, void* cleanupCookie,
342 object_cleanup_func func)
343{
344 AutoMutex _l(mLock);
Steve Block6807e592011-10-20 11:56:00 +0100345 ALOGV("Attaching object %p to binder %p (manager=%p)", object, this, &mObjects);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800346 mObjects.attach(objectID, object, cleanupCookie, func);
347}
348
349void* BpBinder::findObject(const void* objectID) const
350{
351 AutoMutex _l(mLock);
352 return mObjects.find(objectID);
353}
354
355void BpBinder::detachObject(const void* objectID)
356{
357 AutoMutex _l(mLock);
358 mObjects.detach(objectID);
359}
360
361BpBinder* BpBinder::remoteBinder()
362{
363 return this;
364}
365
366BpBinder::~BpBinder()
367{
Steve Block6807e592011-10-20 11:56:00 +0100368 ALOGV("Destroying BpBinder %p handle %d\n", this, mHandle);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800369
370 IPCThreadState* ipc = IPCThreadState::self();
371
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700372 if (mTrackedUid >= 0) {
373 AutoMutex _l(sTrackingLock);
374 if (CC_UNLIKELY(sTrackingMap[mTrackedUid] == 0)) {
375 ALOGE("Unexpected Binder Proxy tracking decrement in %p handle %d\n", this, mHandle);
376 } else {
377 if (CC_UNLIKELY(
378 (sTrackingMap[mTrackedUid] & CALLBACK_TRIGGERED_MASK) &&
379 ((sTrackingMap[mTrackedUid] & COUNTING_VALUE_MASK) <= sBinderProxyCountLowWatermark)
380 )) {
381 // Clear the Callback Triggered bit when crossing below the low watermark
382 sTrackingMap[mTrackedUid] &= ~CALLBACK_TRIGGERED_MASK;
383 }
384 if (--sTrackingMap[mTrackedUid] == 0) {
385 sTrackingMap.erase(mTrackedUid);
386 }
387 }
388 }
389
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800390 mLock.lock();
391 Vector<Obituary>* obits = mObituaries;
392 if(obits != NULL) {
393 if (ipc) ipc->clearDeathNotification(mHandle, this);
394 mObituaries = NULL;
395 }
396 mLock.unlock();
397
398 if (obits != NULL) {
399 // XXX Should we tell any remaining DeathRecipient
400 // objects that the last strong ref has gone away, so they
401 // are no longer linked?
402 delete obits;
403 }
404
405 if (ipc) {
406 ipc->expungeHandle(mHandle, this);
407 ipc->decWeakHandle(mHandle);
408 }
409}
410
411void BpBinder::onFirstRef()
412{
Steve Block6807e592011-10-20 11:56:00 +0100413 ALOGV("onFirstRef BpBinder %p handle %d\n", this, mHandle);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800414 IPCThreadState* ipc = IPCThreadState::self();
Martijn Coenen7c170bb2018-05-04 17:28:55 -0700415 if (ipc) ipc->incStrongHandle(mHandle, this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800416}
417
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800418void BpBinder::onLastStrongRef(const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800419{
Steve Block6807e592011-10-20 11:56:00 +0100420 ALOGV("onLastStrongRef BpBinder %p handle %d\n", this, mHandle);
421 IF_ALOGV() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800422 printRefs();
423 }
424 IPCThreadState* ipc = IPCThreadState::self();
425 if (ipc) ipc->decStrongHandle(mHandle);
426}
427
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800428bool BpBinder::onIncStrongAttempted(uint32_t /*flags*/, const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800429{
Steve Block6807e592011-10-20 11:56:00 +0100430 ALOGV("onIncStrongAttempted BpBinder %p handle %d\n", this, mHandle);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800431 IPCThreadState* ipc = IPCThreadState::self();
432 return ipc ? ipc->attemptIncStrongHandle(mHandle) == NO_ERROR : false;
433}
434
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700435uint32_t BpBinder::getBinderProxyCount(uint32_t uid)
436{
437 AutoMutex _l(sTrackingLock);
438 auto it = sTrackingMap.find(uid);
439 if (it != sTrackingMap.end()) {
440 return it->second & COUNTING_VALUE_MASK;
441 }
442 return 0;
443}
444
445void BpBinder::getCountByUid(Vector<uint32_t>& uids, Vector<uint32_t>& counts)
446{
447 AutoMutex _l(sTrackingLock);
448 uids.setCapacity(sTrackingMap.size());
449 counts.setCapacity(sTrackingMap.size());
450 for (const auto& it : sTrackingMap) {
451 uids.push_back(it.first);
452 counts.push_back(it.second & COUNTING_VALUE_MASK);
453 }
454}
455
456void BpBinder::enableCountByUid() { sCountByUidEnabled.store(true); }
457void BpBinder::disableCountByUid() { sCountByUidEnabled.store(false); }
458void BpBinder::setCountByUidEnabled(bool enable) { sCountByUidEnabled.store(enable); }
459
460void BpBinder::setLimitCallback(binder_proxy_limit_callback cb) {
461 AutoMutex _l(sTrackingLock);
462 sLimitCallback = cb;
463}
464
465void BpBinder::setBinderProxyCountWatermarks(int high, int low) {
466 AutoMutex _l(sTrackingLock);
467 sBinderProxyCountHighWatermark = high;
468 sBinderProxyCountLowWatermark = low;
469}
470
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800471// ---------------------------------------------------------------------------
472
473}; // namespace android