The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 Agopian | c5b2c0b | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 20 | #include <binder/BpBinder.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | |
Mathias Agopian | c5b2c0b | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 22 | #include <binder/IPCThreadState.h> |
Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 23 | #include <binder/IResultReceiver.h> |
Michael Wachenschwanz | d296d0c | 2017-08-15 00:57:14 -0700 | [diff] [blame] | 24 | #include <cutils/compiler.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | #include <utils/Log.h> |
| 26 | |
| 27 | #include <stdio.h> |
| 28 | |
Steve Block | 6807e59 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 29 | //#undef ALOGV |
| 30 | //#define ALOGV(...) fprintf(stderr, __VA_ARGS__) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | |
| 32 | namespace android { |
| 33 | |
| 34 | // --------------------------------------------------------------------------- |
| 35 | |
Michael Wachenschwanz | d296d0c | 2017-08-15 00:57:14 -0700 | [diff] [blame] | 36 | Mutex BpBinder::sTrackingLock; |
| 37 | std::unordered_map<int32_t,uint32_t> BpBinder::sTrackingMap; |
| 38 | int BpBinder::sNumTrackedUids = 0; |
| 39 | std::atomic_bool BpBinder::sCountByUidEnabled(false); |
| 40 | binder_proxy_limit_callback BpBinder::sLimitCallback; |
| 41 | bool BpBinder::sBinderProxyThrottleCreate = false; |
| 42 | |
| 43 | // Arbitrarily high value that probably distinguishes a bad behaving app |
| 44 | uint32_t BpBinder::sBinderProxyCountHighWatermark = 2500; |
| 45 | // Another arbitrary value a binder count needs to drop below before another callback will be called |
| 46 | uint32_t BpBinder::sBinderProxyCountLowWatermark = 2000; |
| 47 | |
| 48 | enum { |
| 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 Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | BpBinder::ObjectManager::ObjectManager() |
| 54 | { |
| 55 | } |
| 56 | |
| 57 | BpBinder::ObjectManager::~ObjectManager() |
| 58 | { |
| 59 | kill(); |
| 60 | } |
| 61 | |
| 62 | void 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 Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 72 | ALOGE("Trying to attach object ID %p to binder ObjectManager %p with object %p, but object ID already in use", |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | objectID, this, object); |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | mObjects.add(objectID, e); |
| 78 | } |
| 79 | |
| 80 | void* 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 | |
| 87 | void BpBinder::ObjectManager::detach(const void* objectID) |
| 88 | { |
| 89 | mObjects.removeItem(objectID); |
| 90 | } |
| 91 | |
| 92 | void BpBinder::ObjectManager::kill() |
| 93 | { |
| 94 | const size_t N = mObjects.size(); |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 95 | ALOGV("Killing %zu objects in manager %p", N, this); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | 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 Wachenschwanz | d296d0c | 2017-08-15 00:57:14 -0700 | [diff] [blame] | 108 | |
| 109 | BpBinder* 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 | |
| 143 | BpBinder::BpBinder(int32_t handle, int32_t trackedUid) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 144 | : mHandle(handle) |
| 145 | , mAlive(1) |
| 146 | , mObitsSent(0) |
| 147 | , mObituaries(NULL) |
Michael Wachenschwanz | d296d0c | 2017-08-15 00:57:14 -0700 | [diff] [blame] | 148 | , mTrackedUid(trackedUid) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 149 | { |
Steve Block | 6807e59 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 150 | ALOGV("Creating BpBinder %p handle %d\n", this, mHandle); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 151 | |
| 152 | extendObjectLifetime(OBJECT_LIFETIME_WEAK); |
Martijn Coenen | 7c170bb | 2018-05-04 17:28:55 -0700 | [diff] [blame^] | 153 | IPCThreadState::self()->incWeakHandle(handle, this); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 154 | } |
| 155 | |
Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 156 | bool BpBinder::isDescriptorCached() const { |
| 157 | Mutex::Autolock _l(mLock); |
| 158 | return mDescriptorCache.size() ? true : false; |
| 159 | } |
| 160 | |
| 161 | const String16& BpBinder::getInterfaceDescriptor() const |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 162 | { |
Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 163 | 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 Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 176 | } |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 177 | |
Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 178 | // we're returning a reference to a non-static object here. Usually this |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 179 | // is not something smart to do, however, with binder objects it is |
Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 180 | // (usually) safe because they are reference-counted. |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 181 | |
Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 182 | return mDescriptorCache; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | bool BpBinder::isBinderAlive() const |
| 186 | { |
| 187 | return mAlive != 0; |
| 188 | } |
| 189 | |
| 190 | status_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 | |
| 200 | status_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 | |
| 214 | status_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 | |
| 228 | status_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 Block | 6807e59 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 248 | ALOGV("Requesting death notification: %p handle %d\n", this, mHandle); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 249 | 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 | |
| 262 | status_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 Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 278 | if (outRecipient != NULL) { |
| 279 | *outRecipient = mObituaries->itemAt(i).recipient; |
| 280 | } |
| 281 | mObituaries->removeAt(i); |
| 282 | if (mObituaries->size() == 0) { |
Steve Block | 6807e59 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 283 | ALOGV("Clearing death notification: %p handle %d\n", this, mHandle); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 284 | 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 | |
| 297 | void BpBinder::sendObituary() |
| 298 | { |
Steve Block | 6807e59 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 299 | ALOGV("Sending obituary for proxy %p handle %d, mObitsSent=%s\n", |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 300 | 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 Block | 6807e59 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 308 | ALOGV("Clearing sent death notification: %p handle %d\n", this, mHandle); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 309 | IPCThreadState* self = IPCThreadState::self(); |
| 310 | self->clearDeathNotification(mHandle, this); |
| 311 | self->flushCommands(); |
| 312 | mObituaries = NULL; |
| 313 | } |
| 314 | mObitsSent = 1; |
| 315 | mLock.unlock(); |
| 316 | |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 317 | ALOGV("Reporting death of proxy %p for %zu recipients\n", |
| 318 | this, obits ? obits->size() : 0U); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 319 | |
| 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 | |
| 330 | void BpBinder::reportOneDeath(const Obituary& obit) |
| 331 | { |
| 332 | sp<DeathRecipient> recipient = obit.recipient.promote(); |
Steve Block | 6807e59 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 333 | ALOGV("Reporting death to recipient: %p\n", recipient.get()); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 334 | if (recipient == NULL) return; |
| 335 | |
| 336 | recipient->binderDied(this); |
| 337 | } |
| 338 | |
| 339 | |
| 340 | void BpBinder::attachObject( |
| 341 | const void* objectID, void* object, void* cleanupCookie, |
| 342 | object_cleanup_func func) |
| 343 | { |
| 344 | AutoMutex _l(mLock); |
Steve Block | 6807e59 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 345 | ALOGV("Attaching object %p to binder %p (manager=%p)", object, this, &mObjects); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 346 | mObjects.attach(objectID, object, cleanupCookie, func); |
| 347 | } |
| 348 | |
| 349 | void* BpBinder::findObject(const void* objectID) const |
| 350 | { |
| 351 | AutoMutex _l(mLock); |
| 352 | return mObjects.find(objectID); |
| 353 | } |
| 354 | |
| 355 | void BpBinder::detachObject(const void* objectID) |
| 356 | { |
| 357 | AutoMutex _l(mLock); |
| 358 | mObjects.detach(objectID); |
| 359 | } |
| 360 | |
| 361 | BpBinder* BpBinder::remoteBinder() |
| 362 | { |
| 363 | return this; |
| 364 | } |
| 365 | |
| 366 | BpBinder::~BpBinder() |
| 367 | { |
Steve Block | 6807e59 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 368 | ALOGV("Destroying BpBinder %p handle %d\n", this, mHandle); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 369 | |
| 370 | IPCThreadState* ipc = IPCThreadState::self(); |
| 371 | |
Michael Wachenschwanz | d296d0c | 2017-08-15 00:57:14 -0700 | [diff] [blame] | 372 | 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 Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 390 | 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 | |
| 411 | void BpBinder::onFirstRef() |
| 412 | { |
Steve Block | 6807e59 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 413 | ALOGV("onFirstRef BpBinder %p handle %d\n", this, mHandle); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 414 | IPCThreadState* ipc = IPCThreadState::self(); |
Martijn Coenen | 7c170bb | 2018-05-04 17:28:55 -0700 | [diff] [blame^] | 415 | if (ipc) ipc->incStrongHandle(mHandle, this); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 416 | } |
| 417 | |
Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 418 | void BpBinder::onLastStrongRef(const void* /*id*/) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 419 | { |
Steve Block | 6807e59 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 420 | ALOGV("onLastStrongRef BpBinder %p handle %d\n", this, mHandle); |
| 421 | IF_ALOGV() { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 422 | printRefs(); |
| 423 | } |
| 424 | IPCThreadState* ipc = IPCThreadState::self(); |
| 425 | if (ipc) ipc->decStrongHandle(mHandle); |
| 426 | } |
| 427 | |
Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 428 | bool BpBinder::onIncStrongAttempted(uint32_t /*flags*/, const void* /*id*/) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 429 | { |
Steve Block | 6807e59 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 430 | ALOGV("onIncStrongAttempted BpBinder %p handle %d\n", this, mHandle); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 431 | IPCThreadState* ipc = IPCThreadState::self(); |
| 432 | return ipc ? ipc->attemptIncStrongHandle(mHandle) == NO_ERROR : false; |
| 433 | } |
| 434 | |
Michael Wachenschwanz | d296d0c | 2017-08-15 00:57:14 -0700 | [diff] [blame] | 435 | uint32_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 | |
| 445 | void 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 | |
| 456 | void BpBinder::enableCountByUid() { sCountByUidEnabled.store(true); } |
| 457 | void BpBinder::disableCountByUid() { sCountByUidEnabled.store(false); } |
| 458 | void BpBinder::setCountByUidEnabled(bool enable) { sCountByUidEnabled.store(enable); } |
| 459 | |
| 460 | void BpBinder::setLimitCallback(binder_proxy_limit_callback cb) { |
| 461 | AutoMutex _l(sTrackingLock); |
| 462 | sLimitCallback = cb; |
| 463 | } |
| 464 | |
| 465 | void BpBinder::setBinderProxyCountWatermarks(int high, int low) { |
| 466 | AutoMutex _l(sTrackingLock); |
| 467 | sBinderProxyCountHighWatermark = high; |
| 468 | sBinderProxyCountLowWatermark = low; |
| 469 | } |
| 470 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 471 | // --------------------------------------------------------------------------- |
| 472 | |
| 473 | }; // namespace android |