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