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