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