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