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