| 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 { | 
| Michael Wachenschwanz | 74d967a | 2018-05-15 15:03:57 -0700 | [diff] [blame] | 49 | LIMIT_REACHED_MASK = 0x80000000,        // A flag denoting that the limit has been reached | 
| Michael Wachenschwanz | d296d0c | 2017-08-15 00:57:14 -0700 | [diff] [blame] | 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); | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 83 | if (i < 0) return nullptr; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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); | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 98 | if (e.func != nullptr) { | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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) { | 
| Michael Wachenschwanz | d296d0c | 2017-08-15 00:57:14 -0700 | [diff] [blame] | 112 | trackedUid = IPCThreadState::self()->getCallingUid(); | 
|  | 113 | AutoMutex _l(sTrackingLock); | 
| Michael Wachenschwanz | 74d967a | 2018-05-15 15:03:57 -0700 | [diff] [blame] | 114 | uint32_t trackedValue = sTrackingMap[trackedUid]; | 
|  | 115 | if (CC_UNLIKELY(trackedValue & LIMIT_REACHED_MASK)) { | 
| Michael Wachenschwanz | d296d0c | 2017-08-15 00:57:14 -0700 | [diff] [blame] | 116 | if (sBinderProxyThrottleCreate) { | 
| Michael Wachenschwanz | 74d967a | 2018-05-15 15:03:57 -0700 | [diff] [blame] | 117 | return nullptr; | 
| Michael Wachenschwanz | d296d0c | 2017-08-15 00:57:14 -0700 | [diff] [blame] | 118 | } | 
|  | 119 | } else { | 
| Michael Wachenschwanz | 74d967a | 2018-05-15 15:03:57 -0700 | [diff] [blame] | 120 | if ((trackedValue & COUNTING_VALUE_MASK) >= sBinderProxyCountHighWatermark) { | 
|  | 121 | ALOGE("Too many binder proxy objects sent to uid %d from uid %d (%d proxies held)", | 
|  | 122 | getuid(), trackedUid, trackedValue); | 
|  | 123 | sTrackingMap[trackedUid] |= LIMIT_REACHED_MASK; | 
|  | 124 | if (sLimitCallback) sLimitCallback(trackedUid); | 
|  | 125 | if (sBinderProxyThrottleCreate) { | 
|  | 126 | ALOGI("Throttling binder proxy creates from uid %d in uid %d until binder proxy" | 
|  | 127 | " count drops below %d", | 
|  | 128 | trackedUid, getuid(), sBinderProxyCountLowWatermark); | 
|  | 129 | return nullptr; | 
|  | 130 | } | 
|  | 131 | } | 
| Michael Wachenschwanz | d296d0c | 2017-08-15 00:57:14 -0700 | [diff] [blame] | 132 | } | 
| Michael Wachenschwanz | 74d967a | 2018-05-15 15:03:57 -0700 | [diff] [blame] | 133 | sTrackingMap[trackedUid]++; | 
| Michael Wachenschwanz | d296d0c | 2017-08-15 00:57:14 -0700 | [diff] [blame] | 134 | } | 
| Michael Wachenschwanz | 74d967a | 2018-05-15 15:03:57 -0700 | [diff] [blame] | 135 | return new BpBinder(handle, trackedUid); | 
| Michael Wachenschwanz | d296d0c | 2017-08-15 00:57:14 -0700 | [diff] [blame] | 136 | } | 
|  | 137 |  | 
|  | 138 | BpBinder::BpBinder(int32_t handle, int32_t trackedUid) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 139 | : mHandle(handle) | 
|  | 140 | , mAlive(1) | 
|  | 141 | , mObitsSent(0) | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 142 | , mObituaries(nullptr) | 
| Michael Wachenschwanz | d296d0c | 2017-08-15 00:57:14 -0700 | [diff] [blame] | 143 | , mTrackedUid(trackedUid) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 144 | { | 
| Steve Block | 6807e59 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 145 | ALOGV("Creating BpBinder %p handle %d\n", this, mHandle); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 146 |  | 
|  | 147 | extendObjectLifetime(OBJECT_LIFETIME_WEAK); | 
| Martijn Coenen | 7c170bb | 2018-05-04 17:28:55 -0700 | [diff] [blame] | 148 | IPCThreadState::self()->incWeakHandle(handle, this); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 149 | } | 
|  | 150 |  | 
| Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 151 | bool BpBinder::isDescriptorCached() const { | 
|  | 152 | Mutex::Autolock _l(mLock); | 
|  | 153 | return mDescriptorCache.size() ? true : false; | 
|  | 154 | } | 
|  | 155 |  | 
|  | 156 | const String16& BpBinder::getInterfaceDescriptor() const | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 157 | { | 
| Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 158 | if (isDescriptorCached() == false) { | 
|  | 159 | Parcel send, reply; | 
|  | 160 | // do the IPC without a lock held. | 
|  | 161 | status_t err = const_cast<BpBinder*>(this)->transact( | 
|  | 162 | INTERFACE_TRANSACTION, send, &reply); | 
|  | 163 | if (err == NO_ERROR) { | 
|  | 164 | String16 res(reply.readString16()); | 
|  | 165 | Mutex::Autolock _l(mLock); | 
|  | 166 | // mDescriptorCache could have been assigned while the lock was | 
|  | 167 | // released. | 
|  | 168 | if (mDescriptorCache.size() == 0) | 
|  | 169 | mDescriptorCache = res; | 
|  | 170 | } | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 171 | } | 
| Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 172 |  | 
| Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 173 | // 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] | 174 | // is not something smart to do, however, with binder objects it is | 
| Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 175 | // (usually) safe because they are reference-counted. | 
| Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 176 |  | 
| Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 177 | return mDescriptorCache; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 178 | } | 
|  | 179 |  | 
|  | 180 | bool BpBinder::isBinderAlive() const | 
|  | 181 | { | 
|  | 182 | return mAlive != 0; | 
|  | 183 | } | 
|  | 184 |  | 
|  | 185 | status_t BpBinder::pingBinder() | 
|  | 186 | { | 
|  | 187 | Parcel send; | 
|  | 188 | Parcel reply; | 
|  | 189 | status_t err = transact(PING_TRANSACTION, send, &reply); | 
|  | 190 | if (err != NO_ERROR) return err; | 
|  | 191 | if (reply.dataSize() < sizeof(status_t)) return NOT_ENOUGH_DATA; | 
|  | 192 | return (status_t)reply.readInt32(); | 
|  | 193 | } | 
|  | 194 |  | 
|  | 195 | status_t BpBinder::dump(int fd, const Vector<String16>& args) | 
|  | 196 | { | 
|  | 197 | Parcel send; | 
|  | 198 | Parcel reply; | 
|  | 199 | send.writeFileDescriptor(fd); | 
|  | 200 | const size_t numArgs = args.size(); | 
|  | 201 | send.writeInt32(numArgs); | 
|  | 202 | for (size_t i = 0; i < numArgs; i++) { | 
|  | 203 | send.writeString16(args[i]); | 
|  | 204 | } | 
|  | 205 | status_t err = transact(DUMP_TRANSACTION, send, &reply); | 
|  | 206 | return err; | 
|  | 207 | } | 
|  | 208 |  | 
| Jiyong Park | b86c866 | 2018-10-29 23:01:57 +0900 | [diff] [blame] | 209 | // NOLINTNEXTLINE(google-default-arguments) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 210 | status_t BpBinder::transact( | 
|  | 211 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) | 
|  | 212 | { | 
|  | 213 | // Once a binder has died, it will never come back to life. | 
|  | 214 | if (mAlive) { | 
|  | 215 | status_t status = IPCThreadState::self()->transact( | 
|  | 216 | mHandle, code, data, reply, flags); | 
|  | 217 | if (status == DEAD_OBJECT) mAlive = 0; | 
|  | 218 | return status; | 
|  | 219 | } | 
|  | 220 |  | 
|  | 221 | return DEAD_OBJECT; | 
|  | 222 | } | 
|  | 223 |  | 
| Jiyong Park | b86c866 | 2018-10-29 23:01:57 +0900 | [diff] [blame] | 224 | // NOLINTNEXTLINE(google-default-arguments) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 225 | status_t BpBinder::linkToDeath( | 
|  | 226 | const sp<DeathRecipient>& recipient, void* cookie, uint32_t flags) | 
|  | 227 | { | 
|  | 228 | Obituary ob; | 
|  | 229 | ob.recipient = recipient; | 
|  | 230 | ob.cookie = cookie; | 
|  | 231 | ob.flags = flags; | 
|  | 232 |  | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 233 | LOG_ALWAYS_FATAL_IF(recipient == nullptr, | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 234 | "linkToDeath(): recipient must be non-NULL"); | 
|  | 235 |  | 
|  | 236 | { | 
|  | 237 | AutoMutex _l(mLock); | 
|  | 238 |  | 
|  | 239 | if (!mObitsSent) { | 
|  | 240 | if (!mObituaries) { | 
|  | 241 | mObituaries = new Vector<Obituary>; | 
|  | 242 | if (!mObituaries) { | 
|  | 243 | return NO_MEMORY; | 
|  | 244 | } | 
| Steve Block | 6807e59 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 245 | 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] | 246 | getWeakRefs()->incWeak(this); | 
|  | 247 | IPCThreadState* self = IPCThreadState::self(); | 
|  | 248 | self->requestDeathNotification(mHandle, this); | 
|  | 249 | self->flushCommands(); | 
|  | 250 | } | 
|  | 251 | ssize_t res = mObituaries->add(ob); | 
|  | 252 | return res >= (ssize_t)NO_ERROR ? (status_t)NO_ERROR : res; | 
|  | 253 | } | 
|  | 254 | } | 
|  | 255 |  | 
|  | 256 | return DEAD_OBJECT; | 
|  | 257 | } | 
|  | 258 |  | 
| Jiyong Park | b86c866 | 2018-10-29 23:01:57 +0900 | [diff] [blame] | 259 | // NOLINTNEXTLINE(google-default-arguments) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 260 | status_t BpBinder::unlinkToDeath( | 
|  | 261 | const wp<DeathRecipient>& recipient, void* cookie, uint32_t flags, | 
|  | 262 | wp<DeathRecipient>* outRecipient) | 
|  | 263 | { | 
|  | 264 | AutoMutex _l(mLock); | 
|  | 265 |  | 
|  | 266 | if (mObitsSent) { | 
|  | 267 | return DEAD_OBJECT; | 
|  | 268 | } | 
|  | 269 |  | 
|  | 270 | const size_t N = mObituaries ? mObituaries->size() : 0; | 
|  | 271 | for (size_t i=0; i<N; i++) { | 
|  | 272 | const Obituary& obit = mObituaries->itemAt(i); | 
|  | 273 | if ((obit.recipient == recipient | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 274 | || (recipient == nullptr && obit.cookie == cookie)) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 275 | && obit.flags == flags) { | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 276 | if (outRecipient != nullptr) { | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 277 | *outRecipient = mObituaries->itemAt(i).recipient; | 
|  | 278 | } | 
|  | 279 | mObituaries->removeAt(i); | 
|  | 280 | if (mObituaries->size() == 0) { | 
| Steve Block | 6807e59 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 281 | 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] | 282 | IPCThreadState* self = IPCThreadState::self(); | 
|  | 283 | self->clearDeathNotification(mHandle, this); | 
|  | 284 | self->flushCommands(); | 
|  | 285 | delete mObituaries; | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 286 | mObituaries = nullptr; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 287 | } | 
|  | 288 | return NO_ERROR; | 
|  | 289 | } | 
|  | 290 | } | 
|  | 291 |  | 
|  | 292 | return NAME_NOT_FOUND; | 
|  | 293 | } | 
|  | 294 |  | 
|  | 295 | void BpBinder::sendObituary() | 
|  | 296 | { | 
| Steve Block | 6807e59 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 297 | 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] | 298 | this, mHandle, mObitsSent ? "true" : "false"); | 
|  | 299 |  | 
|  | 300 | mAlive = 0; | 
|  | 301 | if (mObitsSent) return; | 
|  | 302 |  | 
|  | 303 | mLock.lock(); | 
|  | 304 | Vector<Obituary>* obits = mObituaries; | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 305 | if(obits != nullptr) { | 
| Steve Block | 6807e59 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 306 | 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] | 307 | IPCThreadState* self = IPCThreadState::self(); | 
|  | 308 | self->clearDeathNotification(mHandle, this); | 
|  | 309 | self->flushCommands(); | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 310 | mObituaries = nullptr; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 311 | } | 
|  | 312 | mObitsSent = 1; | 
|  | 313 | mLock.unlock(); | 
|  | 314 |  | 
| Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 315 | ALOGV("Reporting death of proxy %p for %zu recipients\n", | 
|  | 316 | this, obits ? obits->size() : 0U); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 317 |  | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 318 | if (obits != nullptr) { | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 319 | const size_t N = obits->size(); | 
|  | 320 | for (size_t i=0; i<N; i++) { | 
|  | 321 | reportOneDeath(obits->itemAt(i)); | 
|  | 322 | } | 
|  | 323 |  | 
|  | 324 | delete obits; | 
|  | 325 | } | 
|  | 326 | } | 
|  | 327 |  | 
|  | 328 | void BpBinder::reportOneDeath(const Obituary& obit) | 
|  | 329 | { | 
|  | 330 | sp<DeathRecipient> recipient = obit.recipient.promote(); | 
| Steve Block | 6807e59 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 331 | ALOGV("Reporting death to recipient: %p\n", recipient.get()); | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 332 | if (recipient == nullptr) return; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 333 |  | 
|  | 334 | recipient->binderDied(this); | 
|  | 335 | } | 
|  | 336 |  | 
|  | 337 |  | 
|  | 338 | void BpBinder::attachObject( | 
|  | 339 | const void* objectID, void* object, void* cleanupCookie, | 
|  | 340 | object_cleanup_func func) | 
|  | 341 | { | 
|  | 342 | AutoMutex _l(mLock); | 
| Steve Block | 6807e59 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 343 | 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] | 344 | mObjects.attach(objectID, object, cleanupCookie, func); | 
|  | 345 | } | 
|  | 346 |  | 
|  | 347 | void* BpBinder::findObject(const void* objectID) const | 
|  | 348 | { | 
|  | 349 | AutoMutex _l(mLock); | 
|  | 350 | return mObjects.find(objectID); | 
|  | 351 | } | 
|  | 352 |  | 
|  | 353 | void BpBinder::detachObject(const void* objectID) | 
|  | 354 | { | 
|  | 355 | AutoMutex _l(mLock); | 
|  | 356 | mObjects.detach(objectID); | 
|  | 357 | } | 
|  | 358 |  | 
|  | 359 | BpBinder* BpBinder::remoteBinder() | 
|  | 360 | { | 
|  | 361 | return this; | 
|  | 362 | } | 
|  | 363 |  | 
|  | 364 | BpBinder::~BpBinder() | 
|  | 365 | { | 
| Steve Block | 6807e59 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 366 | ALOGV("Destroying BpBinder %p handle %d\n", this, mHandle); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 367 |  | 
|  | 368 | IPCThreadState* ipc = IPCThreadState::self(); | 
|  | 369 |  | 
| Michael Wachenschwanz | d296d0c | 2017-08-15 00:57:14 -0700 | [diff] [blame] | 370 | if (mTrackedUid >= 0) { | 
|  | 371 | AutoMutex _l(sTrackingLock); | 
| Michael Wachenschwanz | 74d967a | 2018-05-15 15:03:57 -0700 | [diff] [blame] | 372 | uint32_t trackedValue = sTrackingMap[mTrackedUid]; | 
|  | 373 | if (CC_UNLIKELY((trackedValue & COUNTING_VALUE_MASK) == 0)) { | 
| Michael Wachenschwanz | d296d0c | 2017-08-15 00:57:14 -0700 | [diff] [blame] | 374 | ALOGE("Unexpected Binder Proxy tracking decrement in %p handle %d\n", this, mHandle); | 
|  | 375 | } else { | 
|  | 376 | if (CC_UNLIKELY( | 
| Michael Wachenschwanz | 74d967a | 2018-05-15 15:03:57 -0700 | [diff] [blame] | 377 | (trackedValue & LIMIT_REACHED_MASK) && | 
|  | 378 | ((trackedValue & COUNTING_VALUE_MASK) <= sBinderProxyCountLowWatermark) | 
| Michael Wachenschwanz | d296d0c | 2017-08-15 00:57:14 -0700 | [diff] [blame] | 379 | )) { | 
| Michael Wachenschwanz | 74d967a | 2018-05-15 15:03:57 -0700 | [diff] [blame] | 380 | ALOGI("Limit reached bit reset for uid %d (fewer than %d proxies from uid %d held)", | 
|  | 381 | getuid(), mTrackedUid, sBinderProxyCountLowWatermark); | 
|  | 382 | sTrackingMap[mTrackedUid] &= ~LIMIT_REACHED_MASK; | 
| Michael Wachenschwanz | d296d0c | 2017-08-15 00:57:14 -0700 | [diff] [blame] | 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 | if (ipc) { | 
|  | 391 | ipc->expungeHandle(mHandle, this); | 
|  | 392 | ipc->decWeakHandle(mHandle); | 
|  | 393 | } | 
|  | 394 | } | 
|  | 395 |  | 
|  | 396 | void BpBinder::onFirstRef() | 
|  | 397 | { | 
| Steve Block | 6807e59 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 398 | ALOGV("onFirstRef BpBinder %p handle %d\n", this, mHandle); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 399 | IPCThreadState* ipc = IPCThreadState::self(); | 
| Martijn Coenen | 7c170bb | 2018-05-04 17:28:55 -0700 | [diff] [blame] | 400 | if (ipc) ipc->incStrongHandle(mHandle, this); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 401 | } | 
|  | 402 |  | 
| Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 403 | void BpBinder::onLastStrongRef(const void* /*id*/) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 404 | { | 
| Steve Block | 6807e59 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 405 | ALOGV("onLastStrongRef BpBinder %p handle %d\n", this, mHandle); | 
|  | 406 | IF_ALOGV() { | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 407 | printRefs(); | 
|  | 408 | } | 
|  | 409 | IPCThreadState* ipc = IPCThreadState::self(); | 
|  | 410 | if (ipc) ipc->decStrongHandle(mHandle); | 
| Steven Moreland | 80d2393 | 2019-06-07 12:43:27 -0700 | [diff] [blame] | 411 |  | 
|  | 412 | mLock.lock(); | 
|  | 413 | Vector<Obituary>* obits = mObituaries; | 
|  | 414 | if(obits != nullptr) { | 
|  | 415 | if (!obits->isEmpty()) { | 
|  | 416 | ALOGI("onLastStrongRef automatically unlinking death recipients"); | 
|  | 417 | } | 
|  | 418 |  | 
|  | 419 | if (ipc) ipc->clearDeathNotification(mHandle, this); | 
|  | 420 | mObituaries = nullptr; | 
|  | 421 | } | 
|  | 422 | mLock.unlock(); | 
|  | 423 |  | 
|  | 424 | if (obits != nullptr) { | 
|  | 425 | // XXX Should we tell any remaining DeathRecipient | 
|  | 426 | // objects that the last strong ref has gone away, so they | 
|  | 427 | // are no longer linked? | 
|  | 428 | delete obits; | 
|  | 429 | } | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 430 | } | 
|  | 431 |  | 
| Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 432 | bool BpBinder::onIncStrongAttempted(uint32_t /*flags*/, const void* /*id*/) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 433 | { | 
| Steve Block | 6807e59 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 434 | ALOGV("onIncStrongAttempted BpBinder %p handle %d\n", this, mHandle); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 435 | IPCThreadState* ipc = IPCThreadState::self(); | 
|  | 436 | return ipc ? ipc->attemptIncStrongHandle(mHandle) == NO_ERROR : false; | 
|  | 437 | } | 
|  | 438 |  | 
| Michael Wachenschwanz | d296d0c | 2017-08-15 00:57:14 -0700 | [diff] [blame] | 439 | uint32_t BpBinder::getBinderProxyCount(uint32_t uid) | 
|  | 440 | { | 
|  | 441 | AutoMutex _l(sTrackingLock); | 
|  | 442 | auto it = sTrackingMap.find(uid); | 
|  | 443 | if (it != sTrackingMap.end()) { | 
|  | 444 | return it->second & COUNTING_VALUE_MASK; | 
|  | 445 | } | 
|  | 446 | return 0; | 
|  | 447 | } | 
|  | 448 |  | 
|  | 449 | void BpBinder::getCountByUid(Vector<uint32_t>& uids, Vector<uint32_t>& counts) | 
|  | 450 | { | 
|  | 451 | AutoMutex _l(sTrackingLock); | 
|  | 452 | uids.setCapacity(sTrackingMap.size()); | 
|  | 453 | counts.setCapacity(sTrackingMap.size()); | 
|  | 454 | for (const auto& it : sTrackingMap) { | 
|  | 455 | uids.push_back(it.first); | 
|  | 456 | counts.push_back(it.second & COUNTING_VALUE_MASK); | 
|  | 457 | } | 
|  | 458 | } | 
|  | 459 |  | 
|  | 460 | void BpBinder::enableCountByUid() { sCountByUidEnabled.store(true); } | 
|  | 461 | void BpBinder::disableCountByUid() { sCountByUidEnabled.store(false); } | 
|  | 462 | void BpBinder::setCountByUidEnabled(bool enable) { sCountByUidEnabled.store(enable); } | 
|  | 463 |  | 
|  | 464 | void BpBinder::setLimitCallback(binder_proxy_limit_callback cb) { | 
|  | 465 | AutoMutex _l(sTrackingLock); | 
|  | 466 | sLimitCallback = cb; | 
|  | 467 | } | 
|  | 468 |  | 
|  | 469 | void BpBinder::setBinderProxyCountWatermarks(int high, int low) { | 
|  | 470 | AutoMutex _l(sTrackingLock); | 
|  | 471 | sBinderProxyCountHighWatermark = high; | 
|  | 472 | sBinderProxyCountLowWatermark = low; | 
|  | 473 | } | 
|  | 474 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 475 | // --------------------------------------------------------------------------- | 
|  | 476 |  | 
|  | 477 | }; // namespace android |