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