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