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