blob: b6d35ef3ef41d7ffa5aac9d4d69d13afaf922abd [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
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 Agopianc5b2c0b2009-05-19 19:08:10 -070020#include <binder/BpBinder.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080021
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070022#include <binder/IPCThreadState.h>
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070023#include <binder/IResultReceiver.h>
Steven Morelandbdb53ab2021-05-05 17:57:41 +000024#include <binder/RpcSession.h>
Steven Moreland6e5a7752019-08-05 20:30:14 -070025#include <binder/Stability.h>
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -070026#include <cutils/compiler.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027#include <utils/Log.h>
28
29#include <stdio.h>
30
Steven Moreland32150282021-11-12 22:54:53 +000031#include "BuildFlags.h"
32
Steve Block6807e592011-10-20 11:56:00 +010033//#undef ALOGV
34//#define ALOGV(...) fprintf(stderr, __VA_ARGS__)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080035
36namespace android {
37
38// ---------------------------------------------------------------------------
39
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -070040Mutex BpBinder::sTrackingLock;
Martijn Coenena8d509d2021-09-03 18:06:24 +020041std::unordered_map<int32_t, uint32_t> BpBinder::sTrackingMap;
42std::unordered_map<int32_t, uint32_t> BpBinder::sLastLimitCallbackMap;
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -070043int BpBinder::sNumTrackedUids = 0;
44std::atomic_bool BpBinder::sCountByUidEnabled(false);
45binder_proxy_limit_callback BpBinder::sLimitCallback;
46bool BpBinder::sBinderProxyThrottleCreate = false;
47
48// Arbitrarily high value that probably distinguishes a bad behaving app
49uint32_t BpBinder::sBinderProxyCountHighWatermark = 2500;
50// Another arbitrary value a binder count needs to drop below before another callback will be called
51uint32_t BpBinder::sBinderProxyCountLowWatermark = 2000;
52
Martijn Coenen1cad19c2021-10-04 09:19:01 +020053// Log any transactions for which the data exceeds this size
54#define LOG_TRANSACTIONS_OVER_SIZE (300 * 1024)
55
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -070056enum {
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -070057 LIMIT_REACHED_MASK = 0x80000000, // A flag denoting that the limit has been reached
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -070058 COUNTING_VALUE_MASK = 0x7FFFFFFF, // A mask of the remaining bits for the count value
59};
60
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061BpBinder::ObjectManager::ObjectManager()
62{
63}
64
65BpBinder::ObjectManager::~ObjectManager()
66{
67 kill();
68}
69
Steven Moreland63a2d512021-06-25 01:10:15 +000070void* BpBinder::ObjectManager::attach(const void* objectID, void* object, void* cleanupCookie,
71 IBinder::object_cleanup_func func) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080072 entry_t e;
73 e.object = object;
74 e.cleanupCookie = cleanupCookie;
75 e.func = func;
76
Jiyong Park5970d0a2022-03-08 16:56:13 +090077 if (mObjects.find(objectID) != mObjects.end()) {
Steven Moreland63a2d512021-06-25 01:10:15 +000078 ALOGI("Trying to attach object ID %p to binder ObjectManager %p with object %p, but object "
79 "ID already in use",
80 objectID, this, object);
Jiyong Park5970d0a2022-03-08 16:56:13 +090081 return mObjects[objectID].object;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080082 }
83
Jiyong Park5970d0a2022-03-08 16:56:13 +090084 mObjects.insert({objectID, e});
Steven Moreland63a2d512021-06-25 01:10:15 +000085 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080086}
87
88void* BpBinder::ObjectManager::find(const void* objectID) const
89{
Jiyong Park5970d0a2022-03-08 16:56:13 +090090 auto i = mObjects.find(objectID);
91 if (i == mObjects.end()) return nullptr;
92 return i->second.object;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080093}
94
Steven Moreland63a2d512021-06-25 01:10:15 +000095void* BpBinder::ObjectManager::detach(const void* objectID) {
Jiyong Park5970d0a2022-03-08 16:56:13 +090096 auto i = mObjects.find(objectID);
97 if (i == mObjects.end()) return nullptr;
98 void* value = i->second.object;
99 mObjects.erase(i);
Steven Moreland63a2d512021-06-25 01:10:15 +0000100 return value;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800101}
102
103void BpBinder::ObjectManager::kill()
104{
105 const size_t N = mObjects.size();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700106 ALOGV("Killing %zu objects in manager %p", N, this);
Jiyong Park5970d0a2022-03-08 16:56:13 +0900107 for (auto i : mObjects) {
108 const entry_t& e = i.second;
Yi Kongfdd8da92018-06-07 17:52:27 -0700109 if (e.func != nullptr) {
Jiyong Park5970d0a2022-03-08 16:56:13 +0900110 e.func(i.first, e.object, e.cleanupCookie);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800111 }
112 }
113
114 mObjects.clear();
115}
116
117// ---------------------------------------------------------------------------
118
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000119sp<BpBinder> BpBinder::create(int32_t handle) {
Steven Moreland32150282021-11-12 22:54:53 +0000120 if constexpr (!kEnableKernelIpc) {
121 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
122 return nullptr;
123 }
124
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700125 int32_t trackedUid = -1;
126 if (sCountByUidEnabled) {
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700127 trackedUid = IPCThreadState::self()->getCallingUid();
128 AutoMutex _l(sTrackingLock);
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -0700129 uint32_t trackedValue = sTrackingMap[trackedUid];
130 if (CC_UNLIKELY(trackedValue & LIMIT_REACHED_MASK)) {
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700131 if (sBinderProxyThrottleCreate) {
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -0700132 return nullptr;
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700133 }
Martijn Coenena8d509d2021-09-03 18:06:24 +0200134 trackedValue = trackedValue & COUNTING_VALUE_MASK;
135 uint32_t lastLimitCallbackAt = sLastLimitCallbackMap[trackedUid];
136
137 if (trackedValue > lastLimitCallbackAt &&
Martijn Coenen6711b6d2021-09-23 09:44:29 +0200138 (trackedValue - lastLimitCallbackAt > sBinderProxyCountHighWatermark)) {
Martijn Coenena8d509d2021-09-03 18:06:24 +0200139 ALOGE("Still too many binder proxy objects sent to uid %d from uid %d (%d proxies "
140 "held)",
141 getuid(), trackedUid, trackedValue);
142 if (sLimitCallback) sLimitCallback(trackedUid);
143 sLastLimitCallbackMap[trackedUid] = trackedValue;
144 }
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700145 } else {
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -0700146 if ((trackedValue & COUNTING_VALUE_MASK) >= sBinderProxyCountHighWatermark) {
147 ALOGE("Too many binder proxy objects sent to uid %d from uid %d (%d proxies held)",
148 getuid(), trackedUid, trackedValue);
149 sTrackingMap[trackedUid] |= LIMIT_REACHED_MASK;
150 if (sLimitCallback) sLimitCallback(trackedUid);
Martijn Coenena8d509d2021-09-03 18:06:24 +0200151 sLastLimitCallbackMap[trackedUid] = trackedValue & COUNTING_VALUE_MASK;
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -0700152 if (sBinderProxyThrottleCreate) {
153 ALOGI("Throttling binder proxy creates from uid %d in uid %d until binder proxy"
154 " count drops below %d",
155 trackedUid, getuid(), sBinderProxyCountLowWatermark);
156 return nullptr;
157 }
158 }
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700159 }
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -0700160 sTrackingMap[trackedUid]++;
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700161 }
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000162 return sp<BpBinder>::make(BinderHandle{handle}, trackedUid);
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700163}
164
Steven Moreland5623d1a2021-09-10 15:45:34 -0700165sp<BpBinder> BpBinder::create(const sp<RpcSession>& session, uint64_t address) {
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000166 LOG_ALWAYS_FATAL_IF(session == nullptr, "BpBinder::create null session");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800167
Steven Moreland5553ac42020-11-11 02:14:45 +0000168 // These are not currently tracked, since there is no UID or other
169 // identifier to track them with. However, if similar functionality is
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000170 // needed, session objects keep track of all BpBinder objects on a
171 // per-session basis.
Steven Moreland5553ac42020-11-11 02:14:45 +0000172
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000173 return sp<BpBinder>::make(RpcHandle{session, address});
Steven Moreland5553ac42020-11-11 02:14:45 +0000174}
175
176BpBinder::BpBinder(Handle&& handle)
177 : mStability(0),
178 mHandle(handle),
179 mAlive(true),
180 mObitsSent(false),
181 mObituaries(nullptr),
182 mTrackedUid(-1) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800183 extendObjectLifetime(OBJECT_LIFETIME_WEAK);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800184}
185
Steven Moreland5553ac42020-11-11 02:14:45 +0000186BpBinder::BpBinder(BinderHandle&& handle, int32_t trackedUid) : BpBinder(Handle(handle)) {
Steven Moreland32150282021-11-12 22:54:53 +0000187 if constexpr (!kEnableKernelIpc) {
188 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
189 return;
190 }
191
Steven Moreland5553ac42020-11-11 02:14:45 +0000192 mTrackedUid = trackedUid;
193
194 ALOGV("Creating BpBinder %p handle %d\n", this, this->binderHandle());
195
196 IPCThreadState::self()->incWeakHandle(this->binderHandle(), this);
197}
198
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000199BpBinder::BpBinder(RpcHandle&& handle) : BpBinder(Handle(handle)) {
200 LOG_ALWAYS_FATAL_IF(rpcSession() == nullptr, "BpBinder created w/o session object");
Steven Moreland5553ac42020-11-11 02:14:45 +0000201}
202
203bool BpBinder::isRpcBinder() const {
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000204 return std::holds_alternative<RpcHandle>(mHandle);
Steven Moreland5553ac42020-11-11 02:14:45 +0000205}
206
Steven Moreland5623d1a2021-09-10 15:45:34 -0700207uint64_t BpBinder::rpcAddress() const {
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000208 return std::get<RpcHandle>(mHandle).address;
Steven Moreland5553ac42020-11-11 02:14:45 +0000209}
210
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000211const sp<RpcSession>& BpBinder::rpcSession() const {
212 return std::get<RpcHandle>(mHandle).session;
Steven Moreland5553ac42020-11-11 02:14:45 +0000213}
214
215int32_t BpBinder::binderHandle() const {
216 return std::get<BinderHandle>(mHandle).handle;
Steven Moreland85180c02019-07-16 14:24:20 -0700217}
218
Devin Mooref6f2e642021-08-05 19:03:47 +0000219std::optional<int32_t> BpBinder::getDebugBinderHandle() const {
220 if (!isRpcBinder()) {
221 return binderHandle();
222 } else {
223 return std::nullopt;
224 }
225}
226
Mathias Agopian83c04462009-05-22 19:00:22 -0700227bool BpBinder::isDescriptorCached() const {
228 Mutex::Autolock _l(mLock);
229 return mDescriptorCache.size() ? true : false;
230}
231
232const String16& BpBinder::getInterfaceDescriptor() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800233{
Mathias Agopian83c04462009-05-22 19:00:22 -0700234 if (isDescriptorCached() == false) {
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000235 sp<BpBinder> thiz = sp<BpBinder>::fromExisting(const_cast<BpBinder*>(this));
Steven Moreland4cf688f2021-03-31 01:48:58 +0000236
237 Parcel data;
238 data.markForBinder(thiz);
239 Parcel reply;
Mathias Agopian83c04462009-05-22 19:00:22 -0700240 // do the IPC without a lock held.
Steven Moreland4cf688f2021-03-31 01:48:58 +0000241 status_t err = thiz->transact(INTERFACE_TRANSACTION, data, &reply);
Mathias Agopian83c04462009-05-22 19:00:22 -0700242 if (err == NO_ERROR) {
243 String16 res(reply.readString16());
244 Mutex::Autolock _l(mLock);
245 // mDescriptorCache could have been assigned while the lock was
246 // released.
247 if (mDescriptorCache.size() == 0)
248 mDescriptorCache = res;
249 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800250 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700251
Mathias Agopian83c04462009-05-22 19:00:22 -0700252 // we're returning a reference to a non-static object here. Usually this
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700253 // is not something smart to do, however, with binder objects it is
Mathias Agopian83c04462009-05-22 19:00:22 -0700254 // (usually) safe because they are reference-counted.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700255
Mathias Agopian83c04462009-05-22 19:00:22 -0700256 return mDescriptorCache;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800257}
258
259bool BpBinder::isBinderAlive() const
260{
Steven Moreland06074d82020-03-05 23:16:51 +0000261 return mAlive != 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800262}
263
264status_t BpBinder::pingBinder()
265{
Steven Moreland5553ac42020-11-11 02:14:45 +0000266 Parcel data;
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000267 data.markForBinder(sp<BpBinder>::fromExisting(this));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800268 Parcel reply;
Steven Moreland5553ac42020-11-11 02:14:45 +0000269 return transact(PING_TRANSACTION, data, &reply);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800270}
271
272status_t BpBinder::dump(int fd, const Vector<String16>& args)
273{
274 Parcel send;
275 Parcel reply;
276 send.writeFileDescriptor(fd);
277 const size_t numArgs = args.size();
278 send.writeInt32(numArgs);
279 for (size_t i = 0; i < numArgs; i++) {
280 send.writeString16(args[i]);
281 }
282 status_t err = transact(DUMP_TRANSACTION, send, &reply);
283 return err;
284}
285
Jiyong Parkb86c8662018-10-29 23:01:57 +0900286// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800287status_t BpBinder::transact(
288 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
289{
290 // Once a binder has died, it will never come back to life.
291 if (mAlive) {
Steven Moreland46b5fea2019-10-15 11:22:18 -0700292 bool privateVendor = flags & FLAG_PRIVATE_VENDOR;
293 // don't send userspace flags to the kernel
Andrei Homescu7cdf5c22022-04-30 03:41:16 +0000294 flags = flags & ~static_cast<uint32_t>(FLAG_PRIVATE_VENDOR);
Steven Moreland46b5fea2019-10-15 11:22:18 -0700295
Steven Moreland6e5a7752019-08-05 20:30:14 -0700296 // user transactions require a given stability level
297 if (code >= FIRST_CALL_TRANSACTION && code <= LAST_CALL_TRANSACTION) {
298 using android::internal::Stability;
299
Steven Moreland16a41062021-07-23 13:35:25 -0700300 int16_t stability = Stability::getRepr(this);
Steven Moreland89ddfc52020-11-13 02:39:26 +0000301 Stability::Level required = privateVendor ? Stability::VENDOR
302 : Stability::getLocalLevel();
Steven Moreland6e5a7752019-08-05 20:30:14 -0700303
Steven Moreland16a41062021-07-23 13:35:25 -0700304 if (CC_UNLIKELY(!Stability::check(stability, required))) {
Steven Morelandb269b582021-02-10 17:09:11 +0000305 ALOGE("Cannot do a user transaction on a %s binder (%s) in a %s context.",
Steven Moreland16a41062021-07-23 13:35:25 -0700306 Stability::levelString(stability).c_str(),
307 String8(getInterfaceDescriptor()).c_str(),
308 Stability::levelString(required).c_str());
Steven Moreland6e5a7752019-08-05 20:30:14 -0700309 return BAD_TYPE;
310 }
311 }
312
Steven Moreland5553ac42020-11-11 02:14:45 +0000313 status_t status;
314 if (CC_UNLIKELY(isRpcBinder())) {
Steven Morelandf5174272021-05-25 00:39:28 +0000315 status = rpcSession()->transact(sp<IBinder>::fromExisting(this), code, data, reply,
316 flags);
Steven Moreland5553ac42020-11-11 02:14:45 +0000317 } else {
Steven Moreland32150282021-11-12 22:54:53 +0000318 if constexpr (!kEnableKernelIpc) {
319 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
320 return INVALID_OPERATION;
321 }
322
Steven Moreland5553ac42020-11-11 02:14:45 +0000323 status = IPCThreadState::self()->transact(binderHandle(), code, data, reply, flags);
324 }
Martijn Coenen1cad19c2021-10-04 09:19:01 +0200325 if (data.dataSize() > LOG_TRANSACTIONS_OVER_SIZE) {
326 Mutex::Autolock _l(mLock);
327 ALOGW("Large outgoing transaction of %zu bytes, interface descriptor %s, code %d",
328 data.dataSize(),
329 mDescriptorCache.size() ? String8(mDescriptorCache).c_str()
330 : "<uncached descriptor>",
331 code);
332 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000333
Steven Moreland06074d82020-03-05 23:16:51 +0000334 if (status == DEAD_OBJECT) mAlive = 0;
Steven Morelanda86a3562019-08-01 23:28:34 +0000335
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800336 return status;
337 }
338
339 return DEAD_OBJECT;
340}
341
Jiyong Parkb86c8662018-10-29 23:01:57 +0900342// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800343status_t BpBinder::linkToDeath(
344 const sp<DeathRecipient>& recipient, void* cookie, uint32_t flags)
345{
Devin Moore66d5b7a2022-07-07 21:42:10 +0000346 if (isRpcBinder()) {
347 if (rpcSession()->getMaxIncomingThreads() < 1) {
348 LOG_ALWAYS_FATAL("Cannot register a DeathRecipient without any incoming connections.");
349 return INVALID_OPERATION;
350 }
351 } else if constexpr (!kEnableKernelIpc) {
Steven Moreland32150282021-11-12 22:54:53 +0000352 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
353 return INVALID_OPERATION;
Devin Moore66d5b7a2022-07-07 21:42:10 +0000354 } else {
355 if (ProcessState::self()->getThreadPoolMaxTotalThreadCount() == 0) {
356 ALOGW("Linking to death on %s but there are no threads (yet?) listening to incoming "
357 "transactions. See ProcessState::startThreadPool and "
358 "ProcessState::setThreadPoolMaxThreadCount. Generally you should setup the "
359 "binder "
360 "threadpool before other initialization steps.",
361 String8(getInterfaceDescriptor()).c_str());
362 }
Steven Moreland32150282021-11-12 22:54:53 +0000363 }
364
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800365 Obituary ob;
366 ob.recipient = recipient;
367 ob.cookie = cookie;
368 ob.flags = flags;
369
Yi Kongfdd8da92018-06-07 17:52:27 -0700370 LOG_ALWAYS_FATAL_IF(recipient == nullptr,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800371 "linkToDeath(): recipient must be non-NULL");
372
373 {
374 AutoMutex _l(mLock);
375
376 if (!mObitsSent) {
377 if (!mObituaries) {
378 mObituaries = new Vector<Obituary>;
379 if (!mObituaries) {
380 return NO_MEMORY;
381 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000382 ALOGV("Requesting death notification: %p handle %d\n", this, binderHandle());
Devin Moore66d5b7a2022-07-07 21:42:10 +0000383 if (!isRpcBinder()) {
384 if constexpr (kEnableKernelIpc) {
385 getWeakRefs()->incWeak(this);
386 IPCThreadState* self = IPCThreadState::self();
387 self->requestDeathNotification(binderHandle(), this);
388 self->flushCommands();
389 }
390 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800391 }
392 ssize_t res = mObituaries->add(ob);
393 return res >= (ssize_t)NO_ERROR ? (status_t)NO_ERROR : res;
394 }
395 }
396
397 return DEAD_OBJECT;
398}
399
Jiyong Parkb86c8662018-10-29 23:01:57 +0900400// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800401status_t BpBinder::unlinkToDeath(
402 const wp<DeathRecipient>& recipient, void* cookie, uint32_t flags,
403 wp<DeathRecipient>* outRecipient)
404{
Devin Moore66d5b7a2022-07-07 21:42:10 +0000405 if (!kEnableKernelIpc && !isRpcBinder()) {
Steven Moreland32150282021-11-12 22:54:53 +0000406 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
407 return INVALID_OPERATION;
408 }
409
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800410 AutoMutex _l(mLock);
411
412 if (mObitsSent) {
413 return DEAD_OBJECT;
414 }
415
416 const size_t N = mObituaries ? mObituaries->size() : 0;
417 for (size_t i=0; i<N; i++) {
418 const Obituary& obit = mObituaries->itemAt(i);
419 if ((obit.recipient == recipient
Yi Kongfdd8da92018-06-07 17:52:27 -0700420 || (recipient == nullptr && obit.cookie == cookie))
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800421 && obit.flags == flags) {
Yi Kongfdd8da92018-06-07 17:52:27 -0700422 if (outRecipient != nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800423 *outRecipient = mObituaries->itemAt(i).recipient;
424 }
425 mObituaries->removeAt(i);
426 if (mObituaries->size() == 0) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000427 ALOGV("Clearing death notification: %p handle %d\n", this, binderHandle());
Devin Moore66d5b7a2022-07-07 21:42:10 +0000428 if (!isRpcBinder()) {
429 if constexpr (kEnableKernelIpc) {
430 IPCThreadState* self = IPCThreadState::self();
431 self->clearDeathNotification(binderHandle(), this);
432 self->flushCommands();
433 }
434 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800435 delete mObituaries;
Yi Kongfdd8da92018-06-07 17:52:27 -0700436 mObituaries = nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800437 }
438 return NO_ERROR;
439 }
440 }
441
442 return NAME_NOT_FOUND;
443}
444
445void BpBinder::sendObituary()
446{
Devin Moore66d5b7a2022-07-07 21:42:10 +0000447 if (!kEnableKernelIpc && !isRpcBinder()) {
Steven Moreland32150282021-11-12 22:54:53 +0000448 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
449 return;
450 }
451
Steven Moreland5553ac42020-11-11 02:14:45 +0000452 ALOGV("Sending obituary for proxy %p handle %d, mObitsSent=%s\n", this, binderHandle(),
453 mObitsSent ? "true" : "false");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800454
Steven Moreland06074d82020-03-05 23:16:51 +0000455 mAlive = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800456 if (mObitsSent) return;
457
458 mLock.lock();
459 Vector<Obituary>* obits = mObituaries;
Yi Kongfdd8da92018-06-07 17:52:27 -0700460 if(obits != nullptr) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000461 ALOGV("Clearing sent death notification: %p handle %d\n", this, binderHandle());
Devin Moore66d5b7a2022-07-07 21:42:10 +0000462 if (!isRpcBinder()) {
463 if constexpr (kEnableKernelIpc) {
464 IPCThreadState* self = IPCThreadState::self();
465 self->clearDeathNotification(binderHandle(), this);
466 self->flushCommands();
467 }
468 }
Yi Kongfdd8da92018-06-07 17:52:27 -0700469 mObituaries = nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800470 }
Steven Moreland06074d82020-03-05 23:16:51 +0000471 mObitsSent = 1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800472 mLock.unlock();
473
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700474 ALOGV("Reporting death of proxy %p for %zu recipients\n",
475 this, obits ? obits->size() : 0U);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800476
Yi Kongfdd8da92018-06-07 17:52:27 -0700477 if (obits != nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800478 const size_t N = obits->size();
479 for (size_t i=0; i<N; i++) {
480 reportOneDeath(obits->itemAt(i));
481 }
482
483 delete obits;
484 }
485}
486
487void BpBinder::reportOneDeath(const Obituary& obit)
488{
489 sp<DeathRecipient> recipient = obit.recipient.promote();
Steve Block6807e592011-10-20 11:56:00 +0100490 ALOGV("Reporting death to recipient: %p\n", recipient.get());
Yi Kongfdd8da92018-06-07 17:52:27 -0700491 if (recipient == nullptr) return;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800492
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000493 recipient->binderDied(wp<BpBinder>::fromExisting(this));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800494}
495
Steven Moreland63a2d512021-06-25 01:10:15 +0000496void* BpBinder::attachObject(const void* objectID, void* object, void* cleanupCookie,
497 object_cleanup_func func) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800498 AutoMutex _l(mLock);
Steve Block6807e592011-10-20 11:56:00 +0100499 ALOGV("Attaching object %p to binder %p (manager=%p)", object, this, &mObjects);
Steven Moreland63a2d512021-06-25 01:10:15 +0000500 return mObjects.attach(objectID, object, cleanupCookie, func);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800501}
502
503void* BpBinder::findObject(const void* objectID) const
504{
505 AutoMutex _l(mLock);
506 return mObjects.find(objectID);
507}
508
Steven Moreland63a2d512021-06-25 01:10:15 +0000509void* BpBinder::detachObject(const void* objectID) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800510 AutoMutex _l(mLock);
Steven Moreland63a2d512021-06-25 01:10:15 +0000511 return mObjects.detach(objectID);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800512}
513
Steven Moreland9e759e82021-06-25 21:30:23 +0000514void BpBinder::withLock(const std::function<void()>& doWithLock) {
515 AutoMutex _l(mLock);
516 doWithLock();
517}
518
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800519BpBinder* BpBinder::remoteBinder()
520{
521 return this;
522}
523
Steven Moreland32150282021-11-12 22:54:53 +0000524BpBinder::~BpBinder() {
Steven Moreland5553ac42020-11-11 02:14:45 +0000525 if (CC_UNLIKELY(isRpcBinder())) return;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800526
Steven Moreland32150282021-11-12 22:54:53 +0000527 if constexpr (!kEnableKernelIpc) {
528 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
529 return;
530 }
531
532 ALOGV("Destroying BpBinder %p handle %d\n", this, binderHandle());
533
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800534 IPCThreadState* ipc = IPCThreadState::self();
535
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700536 if (mTrackedUid >= 0) {
537 AutoMutex _l(sTrackingLock);
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -0700538 uint32_t trackedValue = sTrackingMap[mTrackedUid];
539 if (CC_UNLIKELY((trackedValue & COUNTING_VALUE_MASK) == 0)) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000540 ALOGE("Unexpected Binder Proxy tracking decrement in %p handle %d\n", this,
541 binderHandle());
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700542 } else {
543 if (CC_UNLIKELY(
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -0700544 (trackedValue & LIMIT_REACHED_MASK) &&
545 ((trackedValue & COUNTING_VALUE_MASK) <= sBinderProxyCountLowWatermark)
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700546 )) {
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -0700547 ALOGI("Limit reached bit reset for uid %d (fewer than %d proxies from uid %d held)",
Martijn Coenena8d509d2021-09-03 18:06:24 +0200548 getuid(), sBinderProxyCountLowWatermark, mTrackedUid);
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -0700549 sTrackingMap[mTrackedUid] &= ~LIMIT_REACHED_MASK;
Martijn Coenena8d509d2021-09-03 18:06:24 +0200550 sLastLimitCallbackMap.erase(mTrackedUid);
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700551 }
552 if (--sTrackingMap[mTrackedUid] == 0) {
553 sTrackingMap.erase(mTrackedUid);
554 }
555 }
556 }
557
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800558 if (ipc) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000559 ipc->expungeHandle(binderHandle(), this);
560 ipc->decWeakHandle(binderHandle());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800561 }
562}
563
Steven Moreland32150282021-11-12 22:54:53 +0000564void BpBinder::onFirstRef() {
Steven Moreland5553ac42020-11-11 02:14:45 +0000565 if (CC_UNLIKELY(isRpcBinder())) return;
Steven Moreland32150282021-11-12 22:54:53 +0000566
567 if constexpr (!kEnableKernelIpc) {
568 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
569 return;
570 }
571
572 ALOGV("onFirstRef BpBinder %p handle %d\n", this, binderHandle());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800573 IPCThreadState* ipc = IPCThreadState::self();
Steven Moreland5553ac42020-11-11 02:14:45 +0000574 if (ipc) ipc->incStrongHandle(binderHandle(), this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800575}
576
Steven Moreland32150282021-11-12 22:54:53 +0000577void BpBinder::onLastStrongRef(const void* /*id*/) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000578 if (CC_UNLIKELY(isRpcBinder())) {
Steven Moreland4f622fe2021-09-13 17:38:09 -0700579 (void)rpcSession()->sendDecStrong(this);
Steven Moreland5553ac42020-11-11 02:14:45 +0000580 return;
581 }
Steven Moreland32150282021-11-12 22:54:53 +0000582
583 if constexpr (!kEnableKernelIpc) {
584 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
585 return;
586 }
587
588 ALOGV("onLastStrongRef BpBinder %p handle %d\n", this, binderHandle());
Steve Block6807e592011-10-20 11:56:00 +0100589 IF_ALOGV() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800590 printRefs();
591 }
592 IPCThreadState* ipc = IPCThreadState::self();
Steven Moreland5553ac42020-11-11 02:14:45 +0000593 if (ipc) ipc->decStrongHandle(binderHandle());
Steven Moreland80d23932019-06-07 12:43:27 -0700594
595 mLock.lock();
596 Vector<Obituary>* obits = mObituaries;
597 if(obits != nullptr) {
598 if (!obits->isEmpty()) {
Steven Moreland70fcb4e2019-12-13 17:28:58 -0800599 ALOGI("onLastStrongRef automatically unlinking death recipients: %s",
600 mDescriptorCache.size() ? String8(mDescriptorCache).c_str() : "<uncached descriptor>");
Steven Moreland80d23932019-06-07 12:43:27 -0700601 }
602
Steven Moreland5553ac42020-11-11 02:14:45 +0000603 if (ipc) ipc->clearDeathNotification(binderHandle(), this);
Steven Moreland80d23932019-06-07 12:43:27 -0700604 mObituaries = nullptr;
605 }
606 mLock.unlock();
607
608 if (obits != nullptr) {
609 // XXX Should we tell any remaining DeathRecipient
610 // objects that the last strong ref has gone away, so they
611 // are no longer linked?
612 delete obits;
613 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800614}
615
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800616bool BpBinder::onIncStrongAttempted(uint32_t /*flags*/, const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800617{
Steven Moreland5553ac42020-11-11 02:14:45 +0000618 // RPC binder doesn't currently support inc from weak binders
619 if (CC_UNLIKELY(isRpcBinder())) return false;
620
Steven Moreland32150282021-11-12 22:54:53 +0000621 if constexpr (!kEnableKernelIpc) {
622 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
623 return false;
624 }
625
Steven Moreland5553ac42020-11-11 02:14:45 +0000626 ALOGV("onIncStrongAttempted BpBinder %p handle %d\n", this, binderHandle());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800627 IPCThreadState* ipc = IPCThreadState::self();
Steven Moreland5553ac42020-11-11 02:14:45 +0000628 return ipc ? ipc->attemptIncStrongHandle(binderHandle()) == NO_ERROR : false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800629}
630
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700631uint32_t BpBinder::getBinderProxyCount(uint32_t uid)
632{
633 AutoMutex _l(sTrackingLock);
634 auto it = sTrackingMap.find(uid);
635 if (it != sTrackingMap.end()) {
636 return it->second & COUNTING_VALUE_MASK;
637 }
638 return 0;
639}
640
641void BpBinder::getCountByUid(Vector<uint32_t>& uids, Vector<uint32_t>& counts)
642{
643 AutoMutex _l(sTrackingLock);
644 uids.setCapacity(sTrackingMap.size());
645 counts.setCapacity(sTrackingMap.size());
646 for (const auto& it : sTrackingMap) {
647 uids.push_back(it.first);
648 counts.push_back(it.second & COUNTING_VALUE_MASK);
649 }
650}
651
652void BpBinder::enableCountByUid() { sCountByUidEnabled.store(true); }
653void BpBinder::disableCountByUid() { sCountByUidEnabled.store(false); }
654void BpBinder::setCountByUidEnabled(bool enable) { sCountByUidEnabled.store(enable); }
655
656void BpBinder::setLimitCallback(binder_proxy_limit_callback cb) {
657 AutoMutex _l(sTrackingLock);
658 sLimitCallback = cb;
659}
660
661void BpBinder::setBinderProxyCountWatermarks(int high, int low) {
662 AutoMutex _l(sTrackingLock);
663 sBinderProxyCountHighWatermark = high;
664 sBinderProxyCountLowWatermark = low;
665}
666
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800667// ---------------------------------------------------------------------------
668
Steven Moreland61ff8492019-09-26 16:05:45 -0700669} // namespace android