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