| 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> | 
| Steven Moreland | 6e5a775 | 2019-08-05 20:30:14 -0700 | [diff] [blame] | 24 | #include <binder/Stability.h> | 
| Michael Wachenschwanz | d296d0c | 2017-08-15 00:57:14 -0700 | [diff] [blame] | 25 | #include <cutils/compiler.h> | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | #include <utils/Log.h> | 
 | 27 |  | 
 | 28 | #include <stdio.h> | 
 | 29 |  | 
| Steve Block | 6807e59 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 30 | //#undef ALOGV | 
 | 31 | //#define ALOGV(...) fprintf(stderr, __VA_ARGS__) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 |  | 
 | 33 | namespace android { | 
 | 34 |  | 
 | 35 | // --------------------------------------------------------------------------- | 
 | 36 |  | 
| Michael Wachenschwanz | d296d0c | 2017-08-15 00:57:14 -0700 | [diff] [blame] | 37 | Mutex BpBinder::sTrackingLock; | 
 | 38 | std::unordered_map<int32_t,uint32_t> BpBinder::sTrackingMap; | 
 | 39 | int BpBinder::sNumTrackedUids = 0; | 
 | 40 | std::atomic_bool BpBinder::sCountByUidEnabled(false); | 
 | 41 | binder_proxy_limit_callback BpBinder::sLimitCallback; | 
 | 42 | bool BpBinder::sBinderProxyThrottleCreate = false; | 
 | 43 |  | 
 | 44 | // Arbitrarily high value that probably distinguishes a bad behaving app | 
 | 45 | uint32_t BpBinder::sBinderProxyCountHighWatermark = 2500; | 
 | 46 | // Another arbitrary value a binder count needs to drop below before another callback will be called | 
 | 47 | uint32_t BpBinder::sBinderProxyCountLowWatermark = 2000; | 
 | 48 |  | 
 | 49 | enum { | 
| Michael Wachenschwanz | 74d967a | 2018-05-15 15:03:57 -0700 | [diff] [blame] | 50 |     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] | 51 |     COUNTING_VALUE_MASK = 0x7FFFFFFF,       // A mask of the remaining bits for the count value | 
 | 52 | }; | 
 | 53 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | BpBinder::ObjectManager::ObjectManager() | 
 | 55 | { | 
 | 56 | } | 
 | 57 |  | 
 | 58 | BpBinder::ObjectManager::~ObjectManager() | 
 | 59 | { | 
 | 60 |     kill(); | 
 | 61 | } | 
 | 62 |  | 
 | 63 | void BpBinder::ObjectManager::attach( | 
 | 64 |     const void* objectID, void* object, void* cleanupCookie, | 
 | 65 |     IBinder::object_cleanup_func func) | 
 | 66 | { | 
 | 67 |     entry_t e; | 
 | 68 |     e.object = object; | 
 | 69 |     e.cleanupCookie = cleanupCookie; | 
 | 70 |     e.func = func; | 
 | 71 |  | 
 | 72 |     if (mObjects.indexOfKey(objectID) >= 0) { | 
| Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 73 |         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] | 74 |                 objectID, this,  object); | 
 | 75 |         return; | 
 | 76 |     } | 
 | 77 |  | 
 | 78 |     mObjects.add(objectID, e); | 
 | 79 | } | 
 | 80 |  | 
 | 81 | void* BpBinder::ObjectManager::find(const void* objectID) const | 
 | 82 | { | 
 | 83 |     const ssize_t i = mObjects.indexOfKey(objectID); | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 84 |     if (i < 0) return nullptr; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 |     return mObjects.valueAt(i).object; | 
 | 86 | } | 
 | 87 |  | 
 | 88 | void BpBinder::ObjectManager::detach(const void* objectID) | 
 | 89 | { | 
 | 90 |     mObjects.removeItem(objectID); | 
 | 91 | } | 
 | 92 |  | 
 | 93 | void BpBinder::ObjectManager::kill() | 
 | 94 | { | 
 | 95 |     const size_t N = mObjects.size(); | 
| Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 96 |     ALOGV("Killing %zu objects in manager %p", N, this); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 97 |     for (size_t i=0; i<N; i++) { | 
 | 98 |         const entry_t& e = mObjects.valueAt(i); | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 99 |         if (e.func != nullptr) { | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 |             e.func(mObjects.keyAt(i), e.object, e.cleanupCookie); | 
 | 101 |         } | 
 | 102 |     } | 
 | 103 |  | 
 | 104 |     mObjects.clear(); | 
 | 105 | } | 
 | 106 |  | 
 | 107 | // --------------------------------------------------------------------------- | 
 | 108 |  | 
| Michael Wachenschwanz | d296d0c | 2017-08-15 00:57:14 -0700 | [diff] [blame] | 109 |  | 
 | 110 | BpBinder* BpBinder::create(int32_t handle) { | 
 | 111 |     int32_t trackedUid = -1; | 
 | 112 |     if (sCountByUidEnabled) { | 
| Michael Wachenschwanz | d296d0c | 2017-08-15 00:57:14 -0700 | [diff] [blame] | 113 |         trackedUid = IPCThreadState::self()->getCallingUid(); | 
 | 114 |         AutoMutex _l(sTrackingLock); | 
| Michael Wachenschwanz | 74d967a | 2018-05-15 15:03:57 -0700 | [diff] [blame] | 115 |         uint32_t trackedValue = sTrackingMap[trackedUid]; | 
 | 116 |         if (CC_UNLIKELY(trackedValue & LIMIT_REACHED_MASK)) { | 
| Michael Wachenschwanz | d296d0c | 2017-08-15 00:57:14 -0700 | [diff] [blame] | 117 |             if (sBinderProxyThrottleCreate) { | 
| Michael Wachenschwanz | 74d967a | 2018-05-15 15:03:57 -0700 | [diff] [blame] | 118 |                 return nullptr; | 
| Michael Wachenschwanz | d296d0c | 2017-08-15 00:57:14 -0700 | [diff] [blame] | 119 |             } | 
 | 120 |         } else { | 
| Michael Wachenschwanz | 74d967a | 2018-05-15 15:03:57 -0700 | [diff] [blame] | 121 |             if ((trackedValue & COUNTING_VALUE_MASK) >= sBinderProxyCountHighWatermark) { | 
 | 122 |                 ALOGE("Too many binder proxy objects sent to uid %d from uid %d (%d proxies held)", | 
 | 123 |                       getuid(), trackedUid, trackedValue); | 
 | 124 |                 sTrackingMap[trackedUid] |= LIMIT_REACHED_MASK; | 
 | 125 |                 if (sLimitCallback) sLimitCallback(trackedUid); | 
 | 126 |                 if (sBinderProxyThrottleCreate) { | 
 | 127 |                     ALOGI("Throttling binder proxy creates from uid %d in uid %d until binder proxy" | 
 | 128 |                           " count drops below %d", | 
 | 129 |                           trackedUid, getuid(), sBinderProxyCountLowWatermark); | 
 | 130 |                     return nullptr; | 
 | 131 |                 } | 
 | 132 |             } | 
| Michael Wachenschwanz | d296d0c | 2017-08-15 00:57:14 -0700 | [diff] [blame] | 133 |         } | 
| Michael Wachenschwanz | 74d967a | 2018-05-15 15:03:57 -0700 | [diff] [blame] | 134 |         sTrackingMap[trackedUid]++; | 
| Michael Wachenschwanz | d296d0c | 2017-08-15 00:57:14 -0700 | [diff] [blame] | 135 |     } | 
| Michael Wachenschwanz | 74d967a | 2018-05-15 15:03:57 -0700 | [diff] [blame] | 136 |     return new BpBinder(handle, trackedUid); | 
| Michael Wachenschwanz | d296d0c | 2017-08-15 00:57:14 -0700 | [diff] [blame] | 137 | } | 
 | 138 |  | 
 | 139 | BpBinder::BpBinder(int32_t handle, int32_t trackedUid) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 140 |     : mHandle(handle) | 
| Steven Moreland | a7fb018 | 2020-02-26 16:02:08 -0800 | [diff] [blame] | 141 |     , mStability(0) | 
| Steven Moreland | 06074d8 | 2020-03-05 23:16:51 +0000 | [diff] [blame] | 142 |     , mAlive(1) | 
 | 143 |     , mObitsSent(0) | 
| Steven Moreland | 76c0aa4 | 2020-02-26 16:22:58 -0800 | [diff] [blame] | 144 |     , mObituaries(nullptr) | 
| Steven Moreland | 06074d8 | 2020-03-05 23:16:51 +0000 | [diff] [blame] | 145 |     , mTrackedUid(trackedUid) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 146 | { | 
| Steve Block | 6807e59 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 147 |     ALOGV("Creating BpBinder %p handle %d\n", this, mHandle); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 148 |  | 
 | 149 |     extendObjectLifetime(OBJECT_LIFETIME_WEAK); | 
| Martijn Coenen | 7c170bb | 2018-05-04 17:28:55 -0700 | [diff] [blame] | 150 |     IPCThreadState::self()->incWeakHandle(handle, this); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 151 | } | 
 | 152 |  | 
| Steven Moreland | 85180c0 | 2019-07-16 14:24:20 -0700 | [diff] [blame] | 153 | int32_t BpBinder::handle() const { | 
 | 154 |     return mHandle; | 
 | 155 | } | 
 | 156 |  | 
| Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 157 | bool BpBinder::isDescriptorCached() const { | 
 | 158 |     Mutex::Autolock _l(mLock); | 
 | 159 |     return mDescriptorCache.size() ? true : false; | 
 | 160 | } | 
 | 161 |  | 
 | 162 | const String16& BpBinder::getInterfaceDescriptor() const | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 163 | { | 
| Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 164 |     if (isDescriptorCached() == false) { | 
 | 165 |         Parcel send, reply; | 
 | 166 |         // do the IPC without a lock held. | 
 | 167 |         status_t err = const_cast<BpBinder*>(this)->transact( | 
 | 168 |                 INTERFACE_TRANSACTION, send, &reply); | 
 | 169 |         if (err == NO_ERROR) { | 
 | 170 |             String16 res(reply.readString16()); | 
 | 171 |             Mutex::Autolock _l(mLock); | 
 | 172 |             // mDescriptorCache could have been assigned while the lock was | 
 | 173 |             // released. | 
 | 174 |             if (mDescriptorCache.size() == 0) | 
 | 175 |                 mDescriptorCache = res; | 
 | 176 |         } | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 177 |     } | 
| Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 178 |  | 
| Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 179 |     // 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] | 180 |     // is not something smart to do, however, with binder objects it is | 
| Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 181 |     // (usually) safe because they are reference-counted. | 
| Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 182 |  | 
| Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 183 |     return mDescriptorCache; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 184 | } | 
 | 185 |  | 
 | 186 | bool BpBinder::isBinderAlive() const | 
 | 187 | { | 
| Steven Moreland | 06074d8 | 2020-03-05 23:16:51 +0000 | [diff] [blame] | 188 |     return mAlive != 0; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 189 | } | 
 | 190 |  | 
 | 191 | status_t BpBinder::pingBinder() | 
 | 192 | { | 
 | 193 |     Parcel send; | 
 | 194 |     Parcel reply; | 
| Steven Moreland | 6e69d65 | 2019-07-10 14:17:55 -0700 | [diff] [blame] | 195 |     return transact(PING_TRANSACTION, send, &reply); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 196 | } | 
 | 197 |  | 
 | 198 | status_t BpBinder::dump(int fd, const Vector<String16>& args) | 
 | 199 | { | 
 | 200 |     Parcel send; | 
 | 201 |     Parcel reply; | 
 | 202 |     send.writeFileDescriptor(fd); | 
 | 203 |     const size_t numArgs = args.size(); | 
 | 204 |     send.writeInt32(numArgs); | 
 | 205 |     for (size_t i = 0; i < numArgs; i++) { | 
 | 206 |         send.writeString16(args[i]); | 
 | 207 |     } | 
 | 208 |     status_t err = transact(DUMP_TRANSACTION, send, &reply); | 
 | 209 |     return err; | 
 | 210 | } | 
 | 211 |  | 
| Jiyong Park | b86c866 | 2018-10-29 23:01:57 +0900 | [diff] [blame] | 212 | // NOLINTNEXTLINE(google-default-arguments) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 213 | status_t BpBinder::transact( | 
 | 214 |     uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) | 
 | 215 | { | 
 | 216 |     // Once a binder has died, it will never come back to life. | 
 | 217 |     if (mAlive) { | 
| Steven Moreland | 46b5fea | 2019-10-15 11:22:18 -0700 | [diff] [blame] | 218 |         bool privateVendor = flags & FLAG_PRIVATE_VENDOR; | 
 | 219 |         // don't send userspace flags to the kernel | 
 | 220 |         flags = flags & ~FLAG_PRIVATE_VENDOR; | 
 | 221 |  | 
| Steven Moreland | 6e5a775 | 2019-08-05 20:30:14 -0700 | [diff] [blame] | 222 |         // user transactions require a given stability level | 
 | 223 |         if (code >= FIRST_CALL_TRANSACTION && code <= LAST_CALL_TRANSACTION) { | 
 | 224 |             using android::internal::Stability; | 
 | 225 |  | 
| Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 226 |             auto category = Stability::getCategory(this); | 
 | 227 |             Stability::Level required = privateVendor ? Stability::VENDOR | 
 | 228 |                 : Stability::getLocalLevel(); | 
| Steven Moreland | 6e5a775 | 2019-08-05 20:30:14 -0700 | [diff] [blame] | 229 |  | 
| Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 230 |             if (CC_UNLIKELY(!Stability::check(category, required))) { | 
| Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 231 |                 ALOGE("Cannot do a user transaction on a %s binder in a %s context.", | 
| Steven Moreland | 89ddfc5 | 2020-11-13 02:39:26 +0000 | [diff] [blame] | 232 |                     category.debugString().c_str(), | 
 | 233 |                     Stability::levelString(required).c_str()); | 
| Steven Moreland | 6e5a775 | 2019-08-05 20:30:14 -0700 | [diff] [blame] | 234 |                 return BAD_TYPE; | 
 | 235 |             } | 
 | 236 |         } | 
 | 237 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 238 |         status_t status = IPCThreadState::self()->transact( | 
 | 239 |             mHandle, code, data, reply, flags); | 
| Steven Moreland | 06074d8 | 2020-03-05 23:16:51 +0000 | [diff] [blame] | 240 |         if (status == DEAD_OBJECT) mAlive = 0; | 
| Steven Moreland | a86a356 | 2019-08-01 23:28:34 +0000 | [diff] [blame] | 241 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 242 |         return status; | 
 | 243 |     } | 
 | 244 |  | 
 | 245 |     return DEAD_OBJECT; | 
 | 246 | } | 
 | 247 |  | 
| Jiyong Park | b86c866 | 2018-10-29 23:01:57 +0900 | [diff] [blame] | 248 | // NOLINTNEXTLINE(google-default-arguments) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 249 | status_t BpBinder::linkToDeath( | 
 | 250 |     const sp<DeathRecipient>& recipient, void* cookie, uint32_t flags) | 
 | 251 | { | 
 | 252 |     Obituary ob; | 
 | 253 |     ob.recipient = recipient; | 
 | 254 |     ob.cookie = cookie; | 
 | 255 |     ob.flags = flags; | 
 | 256 |  | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 257 |     LOG_ALWAYS_FATAL_IF(recipient == nullptr, | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 258 |                         "linkToDeath(): recipient must be non-NULL"); | 
 | 259 |  | 
 | 260 |     { | 
 | 261 |         AutoMutex _l(mLock); | 
 | 262 |  | 
 | 263 |         if (!mObitsSent) { | 
 | 264 |             if (!mObituaries) { | 
 | 265 |                 mObituaries = new Vector<Obituary>; | 
 | 266 |                 if (!mObituaries) { | 
 | 267 |                     return NO_MEMORY; | 
 | 268 |                 } | 
| Steve Block | 6807e59 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 269 |                 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] | 270 |                 getWeakRefs()->incWeak(this); | 
 | 271 |                 IPCThreadState* self = IPCThreadState::self(); | 
 | 272 |                 self->requestDeathNotification(mHandle, this); | 
 | 273 |                 self->flushCommands(); | 
 | 274 |             } | 
 | 275 |             ssize_t res = mObituaries->add(ob); | 
 | 276 |             return res >= (ssize_t)NO_ERROR ? (status_t)NO_ERROR : res; | 
 | 277 |         } | 
 | 278 |     } | 
 | 279 |  | 
 | 280 |     return DEAD_OBJECT; | 
 | 281 | } | 
 | 282 |  | 
| Jiyong Park | b86c866 | 2018-10-29 23:01:57 +0900 | [diff] [blame] | 283 | // NOLINTNEXTLINE(google-default-arguments) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 284 | status_t BpBinder::unlinkToDeath( | 
 | 285 |     const wp<DeathRecipient>& recipient, void* cookie, uint32_t flags, | 
 | 286 |     wp<DeathRecipient>* outRecipient) | 
 | 287 | { | 
 | 288 |     AutoMutex _l(mLock); | 
 | 289 |  | 
 | 290 |     if (mObitsSent) { | 
 | 291 |         return DEAD_OBJECT; | 
 | 292 |     } | 
 | 293 |  | 
 | 294 |     const size_t N = mObituaries ? mObituaries->size() : 0; | 
 | 295 |     for (size_t i=0; i<N; i++) { | 
 | 296 |         const Obituary& obit = mObituaries->itemAt(i); | 
 | 297 |         if ((obit.recipient == recipient | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 298 |                     || (recipient == nullptr && obit.cookie == cookie)) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 299 |                 && obit.flags == flags) { | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 300 |             if (outRecipient != nullptr) { | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 301 |                 *outRecipient = mObituaries->itemAt(i).recipient; | 
 | 302 |             } | 
 | 303 |             mObituaries->removeAt(i); | 
 | 304 |             if (mObituaries->size() == 0) { | 
| Steve Block | 6807e59 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 305 |                 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] | 306 |                 IPCThreadState* self = IPCThreadState::self(); | 
 | 307 |                 self->clearDeathNotification(mHandle, this); | 
 | 308 |                 self->flushCommands(); | 
 | 309 |                 delete mObituaries; | 
| 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 |             return NO_ERROR; | 
 | 313 |         } | 
 | 314 |     } | 
 | 315 |  | 
 | 316 |     return NAME_NOT_FOUND; | 
 | 317 | } | 
 | 318 |  | 
 | 319 | void BpBinder::sendObituary() | 
 | 320 | { | 
| Steve Block | 6807e59 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 321 |     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] | 322 |         this, mHandle, mObitsSent ? "true" : "false"); | 
 | 323 |  | 
| Steven Moreland | 06074d8 | 2020-03-05 23:16:51 +0000 | [diff] [blame] | 324 |     mAlive = 0; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 325 |     if (mObitsSent) return; | 
 | 326 |  | 
 | 327 |     mLock.lock(); | 
 | 328 |     Vector<Obituary>* obits = mObituaries; | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 329 |     if(obits != nullptr) { | 
| Steve Block | 6807e59 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 330 |         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] | 331 |         IPCThreadState* self = IPCThreadState::self(); | 
 | 332 |         self->clearDeathNotification(mHandle, this); | 
 | 333 |         self->flushCommands(); | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 334 |         mObituaries = nullptr; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 335 |     } | 
| Steven Moreland | 06074d8 | 2020-03-05 23:16:51 +0000 | [diff] [blame] | 336 |     mObitsSent = 1; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 337 |     mLock.unlock(); | 
 | 338 |  | 
| Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 339 |     ALOGV("Reporting death of proxy %p for %zu recipients\n", | 
 | 340 |         this, obits ? obits->size() : 0U); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 341 |  | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 342 |     if (obits != nullptr) { | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 343 |         const size_t N = obits->size(); | 
 | 344 |         for (size_t i=0; i<N; i++) { | 
 | 345 |             reportOneDeath(obits->itemAt(i)); | 
 | 346 |         } | 
 | 347 |  | 
 | 348 |         delete obits; | 
 | 349 |     } | 
 | 350 | } | 
 | 351 |  | 
 | 352 | void BpBinder::reportOneDeath(const Obituary& obit) | 
 | 353 | { | 
 | 354 |     sp<DeathRecipient> recipient = obit.recipient.promote(); | 
| Steve Block | 6807e59 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 355 |     ALOGV("Reporting death to recipient: %p\n", recipient.get()); | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 356 |     if (recipient == nullptr) return; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 357 |  | 
 | 358 |     recipient->binderDied(this); | 
 | 359 | } | 
 | 360 |  | 
 | 361 |  | 
 | 362 | void BpBinder::attachObject( | 
 | 363 |     const void* objectID, void* object, void* cleanupCookie, | 
 | 364 |     object_cleanup_func func) | 
 | 365 | { | 
 | 366 |     AutoMutex _l(mLock); | 
| Steve Block | 6807e59 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 367 |     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] | 368 |     mObjects.attach(objectID, object, cleanupCookie, func); | 
 | 369 | } | 
 | 370 |  | 
 | 371 | void* BpBinder::findObject(const void* objectID) const | 
 | 372 | { | 
 | 373 |     AutoMutex _l(mLock); | 
 | 374 |     return mObjects.find(objectID); | 
 | 375 | } | 
 | 376 |  | 
 | 377 | void BpBinder::detachObject(const void* objectID) | 
 | 378 | { | 
 | 379 |     AutoMutex _l(mLock); | 
 | 380 |     mObjects.detach(objectID); | 
 | 381 | } | 
 | 382 |  | 
 | 383 | BpBinder* BpBinder::remoteBinder() | 
 | 384 | { | 
 | 385 |     return this; | 
 | 386 | } | 
 | 387 |  | 
 | 388 | BpBinder::~BpBinder() | 
 | 389 | { | 
| Steve Block | 6807e59 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 390 |     ALOGV("Destroying BpBinder %p handle %d\n", this, mHandle); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 391 |  | 
 | 392 |     IPCThreadState* ipc = IPCThreadState::self(); | 
 | 393 |  | 
| Michael Wachenschwanz | d296d0c | 2017-08-15 00:57:14 -0700 | [diff] [blame] | 394 |     if (mTrackedUid >= 0) { | 
 | 395 |         AutoMutex _l(sTrackingLock); | 
| Michael Wachenschwanz | 74d967a | 2018-05-15 15:03:57 -0700 | [diff] [blame] | 396 |         uint32_t trackedValue = sTrackingMap[mTrackedUid]; | 
 | 397 |         if (CC_UNLIKELY((trackedValue & COUNTING_VALUE_MASK) == 0)) { | 
| Michael Wachenschwanz | d296d0c | 2017-08-15 00:57:14 -0700 | [diff] [blame] | 398 |             ALOGE("Unexpected Binder Proxy tracking decrement in %p handle %d\n", this, mHandle); | 
 | 399 |         } else { | 
 | 400 |             if (CC_UNLIKELY( | 
| Michael Wachenschwanz | 74d967a | 2018-05-15 15:03:57 -0700 | [diff] [blame] | 401 |                 (trackedValue & LIMIT_REACHED_MASK) && | 
 | 402 |                 ((trackedValue & COUNTING_VALUE_MASK) <= sBinderProxyCountLowWatermark) | 
| Michael Wachenschwanz | d296d0c | 2017-08-15 00:57:14 -0700 | [diff] [blame] | 403 |                 )) { | 
| Michael Wachenschwanz | 74d967a | 2018-05-15 15:03:57 -0700 | [diff] [blame] | 404 |                 ALOGI("Limit reached bit reset for uid %d (fewer than %d proxies from uid %d held)", | 
 | 405 |                                    getuid(), mTrackedUid, sBinderProxyCountLowWatermark); | 
 | 406 |                 sTrackingMap[mTrackedUid] &= ~LIMIT_REACHED_MASK; | 
| Michael Wachenschwanz | d296d0c | 2017-08-15 00:57:14 -0700 | [diff] [blame] | 407 |             } | 
 | 408 |             if (--sTrackingMap[mTrackedUid] == 0) { | 
 | 409 |                 sTrackingMap.erase(mTrackedUid); | 
 | 410 |             } | 
 | 411 |         } | 
 | 412 |     } | 
 | 413 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 414 |     if (ipc) { | 
 | 415 |         ipc->expungeHandle(mHandle, this); | 
 | 416 |         ipc->decWeakHandle(mHandle); | 
 | 417 |     } | 
 | 418 | } | 
 | 419 |  | 
 | 420 | void BpBinder::onFirstRef() | 
 | 421 | { | 
| Steve Block | 6807e59 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 422 |     ALOGV("onFirstRef BpBinder %p handle %d\n", this, mHandle); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 423 |     IPCThreadState* ipc = IPCThreadState::self(); | 
| Martijn Coenen | 7c170bb | 2018-05-04 17:28:55 -0700 | [diff] [blame] | 424 |     if (ipc) ipc->incStrongHandle(mHandle, this); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 425 | } | 
 | 426 |  | 
| Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 427 | void BpBinder::onLastStrongRef(const void* /*id*/) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 428 | { | 
| Steve Block | 6807e59 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 429 |     ALOGV("onLastStrongRef BpBinder %p handle %d\n", this, mHandle); | 
 | 430 |     IF_ALOGV() { | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 431 |         printRefs(); | 
 | 432 |     } | 
 | 433 |     IPCThreadState* ipc = IPCThreadState::self(); | 
 | 434 |     if (ipc) ipc->decStrongHandle(mHandle); | 
| Steven Moreland | 80d2393 | 2019-06-07 12:43:27 -0700 | [diff] [blame] | 435 |  | 
 | 436 |     mLock.lock(); | 
 | 437 |     Vector<Obituary>* obits = mObituaries; | 
 | 438 |     if(obits != nullptr) { | 
 | 439 |         if (!obits->isEmpty()) { | 
| Steven Moreland | 70fcb4e | 2019-12-13 17:28:58 -0800 | [diff] [blame] | 440 |             ALOGI("onLastStrongRef automatically unlinking death recipients: %s", | 
 | 441 |                   mDescriptorCache.size() ? String8(mDescriptorCache).c_str() : "<uncached descriptor>"); | 
| Steven Moreland | 80d2393 | 2019-06-07 12:43:27 -0700 | [diff] [blame] | 442 |         } | 
 | 443 |  | 
 | 444 |         if (ipc) ipc->clearDeathNotification(mHandle, this); | 
 | 445 |         mObituaries = nullptr; | 
 | 446 |     } | 
 | 447 |     mLock.unlock(); | 
 | 448 |  | 
 | 449 |     if (obits != nullptr) { | 
 | 450 |         // XXX Should we tell any remaining DeathRecipient | 
 | 451 |         // objects that the last strong ref has gone away, so they | 
 | 452 |         // are no longer linked? | 
 | 453 |         delete obits; | 
 | 454 |     } | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 455 | } | 
 | 456 |  | 
| Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 457 | bool BpBinder::onIncStrongAttempted(uint32_t /*flags*/, const void* /*id*/) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 458 | { | 
| Steve Block | 6807e59 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 459 |     ALOGV("onIncStrongAttempted BpBinder %p handle %d\n", this, mHandle); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 460 |     IPCThreadState* ipc = IPCThreadState::self(); | 
 | 461 |     return ipc ? ipc->attemptIncStrongHandle(mHandle) == NO_ERROR : false; | 
 | 462 | } | 
 | 463 |  | 
| Michael Wachenschwanz | d296d0c | 2017-08-15 00:57:14 -0700 | [diff] [blame] | 464 | uint32_t BpBinder::getBinderProxyCount(uint32_t uid) | 
 | 465 | { | 
 | 466 |     AutoMutex _l(sTrackingLock); | 
 | 467 |     auto it = sTrackingMap.find(uid); | 
 | 468 |     if (it != sTrackingMap.end()) { | 
 | 469 |         return it->second & COUNTING_VALUE_MASK; | 
 | 470 |     } | 
 | 471 |     return 0; | 
 | 472 | } | 
 | 473 |  | 
 | 474 | void BpBinder::getCountByUid(Vector<uint32_t>& uids, Vector<uint32_t>& counts) | 
 | 475 | { | 
 | 476 |     AutoMutex _l(sTrackingLock); | 
 | 477 |     uids.setCapacity(sTrackingMap.size()); | 
 | 478 |     counts.setCapacity(sTrackingMap.size()); | 
 | 479 |     for (const auto& it : sTrackingMap) { | 
 | 480 |         uids.push_back(it.first); | 
 | 481 |         counts.push_back(it.second & COUNTING_VALUE_MASK); | 
 | 482 |     } | 
 | 483 | } | 
 | 484 |  | 
 | 485 | void BpBinder::enableCountByUid() { sCountByUidEnabled.store(true); } | 
 | 486 | void BpBinder::disableCountByUid() { sCountByUidEnabled.store(false); } | 
 | 487 | void BpBinder::setCountByUidEnabled(bool enable) { sCountByUidEnabled.store(enable); } | 
 | 488 |  | 
 | 489 | void BpBinder::setLimitCallback(binder_proxy_limit_callback cb) { | 
 | 490 |     AutoMutex _l(sTrackingLock); | 
 | 491 |     sLimitCallback = cb; | 
 | 492 | } | 
 | 493 |  | 
 | 494 | void BpBinder::setBinderProxyCountWatermarks(int high, int low) { | 
 | 495 |     AutoMutex _l(sTrackingLock); | 
 | 496 |     sBinderProxyCountHighWatermark = high; | 
 | 497 |     sBinderProxyCountLowWatermark = low; | 
 | 498 | } | 
 | 499 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 500 | // --------------------------------------------------------------------------- | 
 | 501 |  | 
| Steven Moreland | 61ff849 | 2019-09-26 16:05:45 -0700 | [diff] [blame] | 502 | } // namespace android |