blob: 54d24457d49fb51206efc9928ed3f13586e5450d [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
Sahil Somani2522b072022-08-08 14:07:47 -070033#include <android-base/file.h>
34
Steve Block6807e592011-10-20 11:56:00 +010035//#undef ALOGV
36//#define ALOGV(...) fprintf(stderr, __VA_ARGS__)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080037
38namespace android {
39
40// ---------------------------------------------------------------------------
41
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -070042Mutex BpBinder::sTrackingLock;
Martijn Coenena8d509d2021-09-03 18:06:24 +020043std::unordered_map<int32_t, uint32_t> BpBinder::sTrackingMap;
44std::unordered_map<int32_t, uint32_t> BpBinder::sLastLimitCallbackMap;
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -070045int BpBinder::sNumTrackedUids = 0;
46std::atomic_bool BpBinder::sCountByUidEnabled(false);
47binder_proxy_limit_callback BpBinder::sLimitCallback;
48bool BpBinder::sBinderProxyThrottleCreate = false;
49
50// Arbitrarily high value that probably distinguishes a bad behaving app
51uint32_t BpBinder::sBinderProxyCountHighWatermark = 2500;
52// Another arbitrary value a binder count needs to drop below before another callback will be called
53uint32_t BpBinder::sBinderProxyCountLowWatermark = 2000;
54
Martijn Coenen1cad19c2021-10-04 09:19:01 +020055// Log any transactions for which the data exceeds this size
56#define LOG_TRANSACTIONS_OVER_SIZE (300 * 1024)
57
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -070058enum {
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -070059 LIMIT_REACHED_MASK = 0x80000000, // A flag denoting that the limit has been reached
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -070060 COUNTING_VALUE_MASK = 0x7FFFFFFF, // A mask of the remaining bits for the count value
61};
62
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063BpBinder::ObjectManager::ObjectManager()
64{
65}
66
67BpBinder::ObjectManager::~ObjectManager()
68{
69 kill();
70}
71
Steven Moreland63a2d512021-06-25 01:10:15 +000072void* BpBinder::ObjectManager::attach(const void* objectID, void* object, void* cleanupCookie,
73 IBinder::object_cleanup_func func) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080074 entry_t e;
75 e.object = object;
76 e.cleanupCookie = cleanupCookie;
77 e.func = func;
78
Jiyong Park5970d0a2022-03-08 16:56:13 +090079 if (mObjects.find(objectID) != mObjects.end()) {
Steven Moreland63a2d512021-06-25 01:10:15 +000080 ALOGI("Trying to attach object ID %p to binder ObjectManager %p with object %p, but object "
81 "ID already in use",
82 objectID, this, object);
Jiyong Park5970d0a2022-03-08 16:56:13 +090083 return mObjects[objectID].object;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080084 }
85
Jiyong Park5970d0a2022-03-08 16:56:13 +090086 mObjects.insert({objectID, e});
Steven Moreland63a2d512021-06-25 01:10:15 +000087 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088}
89
90void* BpBinder::ObjectManager::find(const void* objectID) const
91{
Jiyong Park5970d0a2022-03-08 16:56:13 +090092 auto i = mObjects.find(objectID);
93 if (i == mObjects.end()) return nullptr;
94 return i->second.object;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080095}
96
Steven Moreland63a2d512021-06-25 01:10:15 +000097void* BpBinder::ObjectManager::detach(const void* objectID) {
Jiyong Park5970d0a2022-03-08 16:56:13 +090098 auto i = mObjects.find(objectID);
99 if (i == mObjects.end()) return nullptr;
100 void* value = i->second.object;
101 mObjects.erase(i);
Steven Moreland63a2d512021-06-25 01:10:15 +0000102 return value;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800103}
104
Devin Moore3faaa002022-07-27 15:54:06 +0000105namespace {
106struct Tag {
107 wp<IBinder> binder;
108};
109} // namespace
110
111static void cleanWeak(const void* /* id */, void* obj, void* /* cookie */) {
112 delete static_cast<Tag*>(obj);
113}
114
115sp<IBinder> BpBinder::ObjectManager::lookupOrCreateWeak(const void* objectID, object_make_func make,
116 const void* makeArgs) {
117 entry_t& e = mObjects[objectID];
118 if (e.object != nullptr) {
119 if (auto attached = static_cast<Tag*>(e.object)->binder.promote()) {
120 return attached;
121 }
122 } else {
123 e.object = new Tag;
124 LOG_ALWAYS_FATAL_IF(!e.object, "no more memory");
125 }
126 sp<IBinder> newObj = make(makeArgs);
127
128 static_cast<Tag*>(e.object)->binder = newObj;
129 e.cleanupCookie = nullptr;
130 e.func = cleanWeak;
131
132 return newObj;
133}
134
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800135void BpBinder::ObjectManager::kill()
136{
137 const size_t N = mObjects.size();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700138 ALOGV("Killing %zu objects in manager %p", N, this);
Jiyong Park5970d0a2022-03-08 16:56:13 +0900139 for (auto i : mObjects) {
140 const entry_t& e = i.second;
Yi Kongfdd8da92018-06-07 17:52:27 -0700141 if (e.func != nullptr) {
Jiyong Park5970d0a2022-03-08 16:56:13 +0900142 e.func(i.first, e.object, e.cleanupCookie);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800143 }
144 }
145
146 mObjects.clear();
147}
148
149// ---------------------------------------------------------------------------
150
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000151sp<BpBinder> BpBinder::create(int32_t handle) {
Steven Moreland32150282021-11-12 22:54:53 +0000152 if constexpr (!kEnableKernelIpc) {
153 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
154 return nullptr;
155 }
156
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700157 int32_t trackedUid = -1;
158 if (sCountByUidEnabled) {
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700159 trackedUid = IPCThreadState::self()->getCallingUid();
160 AutoMutex _l(sTrackingLock);
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -0700161 uint32_t trackedValue = sTrackingMap[trackedUid];
162 if (CC_UNLIKELY(trackedValue & LIMIT_REACHED_MASK)) {
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700163 if (sBinderProxyThrottleCreate) {
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -0700164 return nullptr;
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700165 }
Martijn Coenena8d509d2021-09-03 18:06:24 +0200166 trackedValue = trackedValue & COUNTING_VALUE_MASK;
167 uint32_t lastLimitCallbackAt = sLastLimitCallbackMap[trackedUid];
168
169 if (trackedValue > lastLimitCallbackAt &&
Martijn Coenen6711b6d2021-09-23 09:44:29 +0200170 (trackedValue - lastLimitCallbackAt > sBinderProxyCountHighWatermark)) {
Martijn Coenena8d509d2021-09-03 18:06:24 +0200171 ALOGE("Still too many binder proxy objects sent to uid %d from uid %d (%d proxies "
172 "held)",
173 getuid(), trackedUid, trackedValue);
174 if (sLimitCallback) sLimitCallback(trackedUid);
175 sLastLimitCallbackMap[trackedUid] = trackedValue;
176 }
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700177 } else {
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -0700178 if ((trackedValue & COUNTING_VALUE_MASK) >= sBinderProxyCountHighWatermark) {
179 ALOGE("Too many binder proxy objects sent to uid %d from uid %d (%d proxies held)",
180 getuid(), trackedUid, trackedValue);
181 sTrackingMap[trackedUid] |= LIMIT_REACHED_MASK;
182 if (sLimitCallback) sLimitCallback(trackedUid);
Martijn Coenena8d509d2021-09-03 18:06:24 +0200183 sLastLimitCallbackMap[trackedUid] = trackedValue & COUNTING_VALUE_MASK;
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -0700184 if (sBinderProxyThrottleCreate) {
185 ALOGI("Throttling binder proxy creates from uid %d in uid %d until binder proxy"
186 " count drops below %d",
187 trackedUid, getuid(), sBinderProxyCountLowWatermark);
188 return nullptr;
189 }
190 }
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700191 }
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -0700192 sTrackingMap[trackedUid]++;
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700193 }
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000194 return sp<BpBinder>::make(BinderHandle{handle}, trackedUid);
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700195}
196
Steven Moreland5623d1a2021-09-10 15:45:34 -0700197sp<BpBinder> BpBinder::create(const sp<RpcSession>& session, uint64_t address) {
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000198 LOG_ALWAYS_FATAL_IF(session == nullptr, "BpBinder::create null session");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800199
Steven Moreland5553ac42020-11-11 02:14:45 +0000200 // These are not currently tracked, since there is no UID or other
201 // identifier to track them with. However, if similar functionality is
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000202 // needed, session objects keep track of all BpBinder objects on a
203 // per-session basis.
Steven Moreland5553ac42020-11-11 02:14:45 +0000204
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000205 return sp<BpBinder>::make(RpcHandle{session, address});
Steven Moreland5553ac42020-11-11 02:14:45 +0000206}
207
208BpBinder::BpBinder(Handle&& handle)
209 : mStability(0),
210 mHandle(handle),
211 mAlive(true),
212 mObitsSent(false),
213 mObituaries(nullptr),
214 mTrackedUid(-1) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800215 extendObjectLifetime(OBJECT_LIFETIME_WEAK);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800216}
217
Steven Moreland5553ac42020-11-11 02:14:45 +0000218BpBinder::BpBinder(BinderHandle&& handle, int32_t trackedUid) : BpBinder(Handle(handle)) {
Steven Moreland32150282021-11-12 22:54:53 +0000219 if constexpr (!kEnableKernelIpc) {
220 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
221 return;
222 }
223
Steven Moreland5553ac42020-11-11 02:14:45 +0000224 mTrackedUid = trackedUid;
225
226 ALOGV("Creating BpBinder %p handle %d\n", this, this->binderHandle());
227
228 IPCThreadState::self()->incWeakHandle(this->binderHandle(), this);
229}
230
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000231BpBinder::BpBinder(RpcHandle&& handle) : BpBinder(Handle(handle)) {
232 LOG_ALWAYS_FATAL_IF(rpcSession() == nullptr, "BpBinder created w/o session object");
Steven Moreland5553ac42020-11-11 02:14:45 +0000233}
234
235bool BpBinder::isRpcBinder() const {
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000236 return std::holds_alternative<RpcHandle>(mHandle);
Steven Moreland5553ac42020-11-11 02:14:45 +0000237}
238
Steven Moreland5623d1a2021-09-10 15:45:34 -0700239uint64_t BpBinder::rpcAddress() const {
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000240 return std::get<RpcHandle>(mHandle).address;
Steven Moreland5553ac42020-11-11 02:14:45 +0000241}
242
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000243const sp<RpcSession>& BpBinder::rpcSession() const {
244 return std::get<RpcHandle>(mHandle).session;
Steven Moreland5553ac42020-11-11 02:14:45 +0000245}
246
247int32_t BpBinder::binderHandle() const {
248 return std::get<BinderHandle>(mHandle).handle;
Steven Moreland85180c02019-07-16 14:24:20 -0700249}
250
Devin Mooref6f2e642021-08-05 19:03:47 +0000251std::optional<int32_t> BpBinder::getDebugBinderHandle() const {
252 if (!isRpcBinder()) {
253 return binderHandle();
254 } else {
255 return std::nullopt;
256 }
257}
258
Mathias Agopian83c04462009-05-22 19:00:22 -0700259bool BpBinder::isDescriptorCached() const {
260 Mutex::Autolock _l(mLock);
261 return mDescriptorCache.size() ? true : false;
262}
263
264const String16& BpBinder::getInterfaceDescriptor() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800265{
Mathias Agopian83c04462009-05-22 19:00:22 -0700266 if (isDescriptorCached() == false) {
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000267 sp<BpBinder> thiz = sp<BpBinder>::fromExisting(const_cast<BpBinder*>(this));
Steven Moreland4cf688f2021-03-31 01:48:58 +0000268
269 Parcel data;
270 data.markForBinder(thiz);
271 Parcel reply;
Mathias Agopian83c04462009-05-22 19:00:22 -0700272 // do the IPC without a lock held.
Steven Moreland4cf688f2021-03-31 01:48:58 +0000273 status_t err = thiz->transact(INTERFACE_TRANSACTION, data, &reply);
Mathias Agopian83c04462009-05-22 19:00:22 -0700274 if (err == NO_ERROR) {
275 String16 res(reply.readString16());
276 Mutex::Autolock _l(mLock);
277 // mDescriptorCache could have been assigned while the lock was
278 // released.
279 if (mDescriptorCache.size() == 0)
280 mDescriptorCache = res;
281 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800282 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700283
Mathias Agopian83c04462009-05-22 19:00:22 -0700284 // we're returning a reference to a non-static object here. Usually this
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700285 // is not something smart to do, however, with binder objects it is
Mathias Agopian83c04462009-05-22 19:00:22 -0700286 // (usually) safe because they are reference-counted.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700287
Mathias Agopian83c04462009-05-22 19:00:22 -0700288 return mDescriptorCache;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800289}
290
291bool BpBinder::isBinderAlive() const
292{
Steven Moreland06074d82020-03-05 23:16:51 +0000293 return mAlive != 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800294}
295
296status_t BpBinder::pingBinder()
297{
Steven Moreland5553ac42020-11-11 02:14:45 +0000298 Parcel data;
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000299 data.markForBinder(sp<BpBinder>::fromExisting(this));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800300 Parcel reply;
Steven Moreland5553ac42020-11-11 02:14:45 +0000301 return transact(PING_TRANSACTION, data, &reply);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800302}
303
Sahil Somani2522b072022-08-08 14:07:47 -0700304status_t BpBinder::startRecordingBinder(const android::base::unique_fd& fd) {
305 Parcel send, reply;
306 send.writeUniqueFileDescriptor(fd);
307 return transact(START_RECORDING_TRANSACTION, send, &reply);
308}
309
310status_t BpBinder::stopRecordingBinder() {
311 Parcel data, reply;
312 data.markForBinder(sp<BpBinder>::fromExisting(this));
313 return transact(STOP_RECORDING_TRANSACTION, data, &reply);
314}
315
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800316status_t BpBinder::dump(int fd, const Vector<String16>& args)
317{
318 Parcel send;
319 Parcel reply;
320 send.writeFileDescriptor(fd);
321 const size_t numArgs = args.size();
322 send.writeInt32(numArgs);
323 for (size_t i = 0; i < numArgs; i++) {
324 send.writeString16(args[i]);
325 }
326 status_t err = transact(DUMP_TRANSACTION, send, &reply);
327 return err;
328}
329
Jiyong Parkb86c8662018-10-29 23:01:57 +0900330// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800331status_t BpBinder::transact(
332 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
333{
334 // Once a binder has died, it will never come back to life.
335 if (mAlive) {
Steven Moreland46b5fea2019-10-15 11:22:18 -0700336 bool privateVendor = flags & FLAG_PRIVATE_VENDOR;
337 // don't send userspace flags to the kernel
Andrei Homescu7cdf5c22022-04-30 03:41:16 +0000338 flags = flags & ~static_cast<uint32_t>(FLAG_PRIVATE_VENDOR);
Steven Moreland46b5fea2019-10-15 11:22:18 -0700339
Steven Moreland6e5a7752019-08-05 20:30:14 -0700340 // user transactions require a given stability level
341 if (code >= FIRST_CALL_TRANSACTION && code <= LAST_CALL_TRANSACTION) {
342 using android::internal::Stability;
343
Steven Moreland16a41062021-07-23 13:35:25 -0700344 int16_t stability = Stability::getRepr(this);
Steven Moreland89ddfc52020-11-13 02:39:26 +0000345 Stability::Level required = privateVendor ? Stability::VENDOR
346 : Stability::getLocalLevel();
Steven Moreland6e5a7752019-08-05 20:30:14 -0700347
Steven Moreland16a41062021-07-23 13:35:25 -0700348 if (CC_UNLIKELY(!Stability::check(stability, required))) {
Steven Morelandb269b582021-02-10 17:09:11 +0000349 ALOGE("Cannot do a user transaction on a %s binder (%s) in a %s context.",
Steven Moreland16a41062021-07-23 13:35:25 -0700350 Stability::levelString(stability).c_str(),
351 String8(getInterfaceDescriptor()).c_str(),
352 Stability::levelString(required).c_str());
Steven Moreland6e5a7752019-08-05 20:30:14 -0700353 return BAD_TYPE;
354 }
355 }
356
Steven Moreland5553ac42020-11-11 02:14:45 +0000357 status_t status;
358 if (CC_UNLIKELY(isRpcBinder())) {
Steven Morelandf5174272021-05-25 00:39:28 +0000359 status = rpcSession()->transact(sp<IBinder>::fromExisting(this), code, data, reply,
360 flags);
Steven Moreland5553ac42020-11-11 02:14:45 +0000361 } else {
Steven Moreland32150282021-11-12 22:54:53 +0000362 if constexpr (!kEnableKernelIpc) {
363 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
364 return INVALID_OPERATION;
365 }
366
Steven Moreland5553ac42020-11-11 02:14:45 +0000367 status = IPCThreadState::self()->transact(binderHandle(), code, data, reply, flags);
368 }
Martijn Coenen1cad19c2021-10-04 09:19:01 +0200369 if (data.dataSize() > LOG_TRANSACTIONS_OVER_SIZE) {
370 Mutex::Autolock _l(mLock);
371 ALOGW("Large outgoing transaction of %zu bytes, interface descriptor %s, code %d",
372 data.dataSize(),
373 mDescriptorCache.size() ? String8(mDescriptorCache).c_str()
374 : "<uncached descriptor>",
375 code);
376 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000377
Steven Moreland06074d82020-03-05 23:16:51 +0000378 if (status == DEAD_OBJECT) mAlive = 0;
Steven Morelanda86a3562019-08-01 23:28:34 +0000379
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800380 return status;
381 }
382
383 return DEAD_OBJECT;
384}
385
Jiyong Parkb86c8662018-10-29 23:01:57 +0900386// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800387status_t BpBinder::linkToDeath(
388 const sp<DeathRecipient>& recipient, void* cookie, uint32_t flags)
389{
Devin Moore66d5b7a2022-07-07 21:42:10 +0000390 if (isRpcBinder()) {
391 if (rpcSession()->getMaxIncomingThreads() < 1) {
392 LOG_ALWAYS_FATAL("Cannot register a DeathRecipient without any incoming connections.");
393 return INVALID_OPERATION;
394 }
395 } else if constexpr (!kEnableKernelIpc) {
Steven Moreland32150282021-11-12 22:54:53 +0000396 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
397 return INVALID_OPERATION;
Devin Moore66d5b7a2022-07-07 21:42:10 +0000398 } else {
399 if (ProcessState::self()->getThreadPoolMaxTotalThreadCount() == 0) {
400 ALOGW("Linking to death on %s but there are no threads (yet?) listening to incoming "
401 "transactions. See ProcessState::startThreadPool and "
402 "ProcessState::setThreadPoolMaxThreadCount. Generally you should setup the "
403 "binder "
404 "threadpool before other initialization steps.",
405 String8(getInterfaceDescriptor()).c_str());
406 }
Steven Moreland32150282021-11-12 22:54:53 +0000407 }
408
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800409 Obituary ob;
410 ob.recipient = recipient;
411 ob.cookie = cookie;
412 ob.flags = flags;
413
Yi Kongfdd8da92018-06-07 17:52:27 -0700414 LOG_ALWAYS_FATAL_IF(recipient == nullptr,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800415 "linkToDeath(): recipient must be non-NULL");
416
417 {
418 AutoMutex _l(mLock);
419
420 if (!mObitsSent) {
421 if (!mObituaries) {
422 mObituaries = new Vector<Obituary>;
423 if (!mObituaries) {
424 return NO_MEMORY;
425 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000426 ALOGV("Requesting death notification: %p handle %d\n", this, binderHandle());
Devin Moore66d5b7a2022-07-07 21:42:10 +0000427 if (!isRpcBinder()) {
428 if constexpr (kEnableKernelIpc) {
429 getWeakRefs()->incWeak(this);
430 IPCThreadState* self = IPCThreadState::self();
431 self->requestDeathNotification(binderHandle(), this);
432 self->flushCommands();
433 }
434 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800435 }
436 ssize_t res = mObituaries->add(ob);
437 return res >= (ssize_t)NO_ERROR ? (status_t)NO_ERROR : res;
438 }
439 }
440
441 return DEAD_OBJECT;
442}
443
Jiyong Parkb86c8662018-10-29 23:01:57 +0900444// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800445status_t BpBinder::unlinkToDeath(
446 const wp<DeathRecipient>& recipient, void* cookie, uint32_t flags,
447 wp<DeathRecipient>* outRecipient)
448{
Devin Moore66d5b7a2022-07-07 21:42:10 +0000449 if (!kEnableKernelIpc && !isRpcBinder()) {
Steven Moreland32150282021-11-12 22:54:53 +0000450 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
451 return INVALID_OPERATION;
452 }
453
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800454 AutoMutex _l(mLock);
455
456 if (mObitsSent) {
457 return DEAD_OBJECT;
458 }
459
460 const size_t N = mObituaries ? mObituaries->size() : 0;
461 for (size_t i=0; i<N; i++) {
462 const Obituary& obit = mObituaries->itemAt(i);
463 if ((obit.recipient == recipient
Yi Kongfdd8da92018-06-07 17:52:27 -0700464 || (recipient == nullptr && obit.cookie == cookie))
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800465 && obit.flags == flags) {
Yi Kongfdd8da92018-06-07 17:52:27 -0700466 if (outRecipient != nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800467 *outRecipient = mObituaries->itemAt(i).recipient;
468 }
469 mObituaries->removeAt(i);
470 if (mObituaries->size() == 0) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000471 ALOGV("Clearing death notification: %p handle %d\n", this, binderHandle());
Devin Moore66d5b7a2022-07-07 21:42:10 +0000472 if (!isRpcBinder()) {
473 if constexpr (kEnableKernelIpc) {
474 IPCThreadState* self = IPCThreadState::self();
475 self->clearDeathNotification(binderHandle(), this);
476 self->flushCommands();
477 }
478 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800479 delete mObituaries;
Yi Kongfdd8da92018-06-07 17:52:27 -0700480 mObituaries = nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800481 }
482 return NO_ERROR;
483 }
484 }
485
486 return NAME_NOT_FOUND;
487}
488
489void BpBinder::sendObituary()
490{
Devin Moore66d5b7a2022-07-07 21:42:10 +0000491 if (!kEnableKernelIpc && !isRpcBinder()) {
Steven Moreland32150282021-11-12 22:54:53 +0000492 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
493 return;
494 }
495
Steven Moreland5553ac42020-11-11 02:14:45 +0000496 ALOGV("Sending obituary for proxy %p handle %d, mObitsSent=%s\n", this, binderHandle(),
497 mObitsSent ? "true" : "false");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800498
Steven Moreland06074d82020-03-05 23:16:51 +0000499 mAlive = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800500 if (mObitsSent) return;
501
502 mLock.lock();
503 Vector<Obituary>* obits = mObituaries;
Yi Kongfdd8da92018-06-07 17:52:27 -0700504 if(obits != nullptr) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000505 ALOGV("Clearing sent death notification: %p handle %d\n", this, binderHandle());
Devin Moore66d5b7a2022-07-07 21:42:10 +0000506 if (!isRpcBinder()) {
507 if constexpr (kEnableKernelIpc) {
508 IPCThreadState* self = IPCThreadState::self();
509 self->clearDeathNotification(binderHandle(), this);
510 self->flushCommands();
511 }
512 }
Yi Kongfdd8da92018-06-07 17:52:27 -0700513 mObituaries = nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800514 }
Steven Moreland06074d82020-03-05 23:16:51 +0000515 mObitsSent = 1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800516 mLock.unlock();
517
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700518 ALOGV("Reporting death of proxy %p for %zu recipients\n",
519 this, obits ? obits->size() : 0U);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800520
Yi Kongfdd8da92018-06-07 17:52:27 -0700521 if (obits != nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800522 const size_t N = obits->size();
523 for (size_t i=0; i<N; i++) {
524 reportOneDeath(obits->itemAt(i));
525 }
526
527 delete obits;
528 }
529}
530
531void BpBinder::reportOneDeath(const Obituary& obit)
532{
533 sp<DeathRecipient> recipient = obit.recipient.promote();
Steve Block6807e592011-10-20 11:56:00 +0100534 ALOGV("Reporting death to recipient: %p\n", recipient.get());
Yi Kongfdd8da92018-06-07 17:52:27 -0700535 if (recipient == nullptr) return;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800536
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000537 recipient->binderDied(wp<BpBinder>::fromExisting(this));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800538}
539
Steven Moreland63a2d512021-06-25 01:10:15 +0000540void* BpBinder::attachObject(const void* objectID, void* object, void* cleanupCookie,
541 object_cleanup_func func) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800542 AutoMutex _l(mLock);
Steve Block6807e592011-10-20 11:56:00 +0100543 ALOGV("Attaching object %p to binder %p (manager=%p)", object, this, &mObjects);
Steven Moreland63a2d512021-06-25 01:10:15 +0000544 return mObjects.attach(objectID, object, cleanupCookie, func);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800545}
546
547void* BpBinder::findObject(const void* objectID) const
548{
549 AutoMutex _l(mLock);
550 return mObjects.find(objectID);
551}
552
Steven Moreland63a2d512021-06-25 01:10:15 +0000553void* BpBinder::detachObject(const void* objectID) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800554 AutoMutex _l(mLock);
Steven Moreland63a2d512021-06-25 01:10:15 +0000555 return mObjects.detach(objectID);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800556}
557
Steven Moreland9e759e82021-06-25 21:30:23 +0000558void BpBinder::withLock(const std::function<void()>& doWithLock) {
559 AutoMutex _l(mLock);
560 doWithLock();
561}
562
Devin Moore3faaa002022-07-27 15:54:06 +0000563sp<IBinder> BpBinder::lookupOrCreateWeak(const void* objectID, object_make_func make,
564 const void* makeArgs) {
565 AutoMutex _l(mLock);
566 return mObjects.lookupOrCreateWeak(objectID, make, makeArgs);
567}
568
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800569BpBinder* BpBinder::remoteBinder()
570{
571 return this;
572}
573
Steven Moreland32150282021-11-12 22:54:53 +0000574BpBinder::~BpBinder() {
Steven Moreland5553ac42020-11-11 02:14:45 +0000575 if (CC_UNLIKELY(isRpcBinder())) return;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800576
Steven Moreland32150282021-11-12 22:54:53 +0000577 if constexpr (!kEnableKernelIpc) {
578 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
579 return;
580 }
581
582 ALOGV("Destroying BpBinder %p handle %d\n", this, binderHandle());
583
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800584 IPCThreadState* ipc = IPCThreadState::self();
585
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700586 if (mTrackedUid >= 0) {
587 AutoMutex _l(sTrackingLock);
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -0700588 uint32_t trackedValue = sTrackingMap[mTrackedUid];
589 if (CC_UNLIKELY((trackedValue & COUNTING_VALUE_MASK) == 0)) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000590 ALOGE("Unexpected Binder Proxy tracking decrement in %p handle %d\n", this,
591 binderHandle());
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700592 } else {
593 if (CC_UNLIKELY(
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -0700594 (trackedValue & LIMIT_REACHED_MASK) &&
595 ((trackedValue & COUNTING_VALUE_MASK) <= sBinderProxyCountLowWatermark)
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700596 )) {
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -0700597 ALOGI("Limit reached bit reset for uid %d (fewer than %d proxies from uid %d held)",
Martijn Coenena8d509d2021-09-03 18:06:24 +0200598 getuid(), sBinderProxyCountLowWatermark, mTrackedUid);
Michael Wachenschwanz74d967a2018-05-15 15:03:57 -0700599 sTrackingMap[mTrackedUid] &= ~LIMIT_REACHED_MASK;
Martijn Coenena8d509d2021-09-03 18:06:24 +0200600 sLastLimitCallbackMap.erase(mTrackedUid);
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700601 }
602 if (--sTrackingMap[mTrackedUid] == 0) {
603 sTrackingMap.erase(mTrackedUid);
604 }
605 }
606 }
607
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800608 if (ipc) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000609 ipc->expungeHandle(binderHandle(), this);
610 ipc->decWeakHandle(binderHandle());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800611 }
612}
613
Steven Moreland32150282021-11-12 22:54:53 +0000614void BpBinder::onFirstRef() {
Steven Moreland5553ac42020-11-11 02:14:45 +0000615 if (CC_UNLIKELY(isRpcBinder())) return;
Steven Moreland32150282021-11-12 22:54:53 +0000616
617 if constexpr (!kEnableKernelIpc) {
618 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
619 return;
620 }
621
622 ALOGV("onFirstRef BpBinder %p handle %d\n", this, binderHandle());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800623 IPCThreadState* ipc = IPCThreadState::self();
Steven Moreland5553ac42020-11-11 02:14:45 +0000624 if (ipc) ipc->incStrongHandle(binderHandle(), this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800625}
626
Steven Moreland32150282021-11-12 22:54:53 +0000627void BpBinder::onLastStrongRef(const void* /*id*/) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000628 if (CC_UNLIKELY(isRpcBinder())) {
Steven Moreland4f622fe2021-09-13 17:38:09 -0700629 (void)rpcSession()->sendDecStrong(this);
Steven Moreland5553ac42020-11-11 02:14:45 +0000630 return;
631 }
Steven Moreland32150282021-11-12 22:54:53 +0000632
633 if constexpr (!kEnableKernelIpc) {
634 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
635 return;
636 }
637
638 ALOGV("onLastStrongRef BpBinder %p handle %d\n", this, binderHandle());
Steve Block6807e592011-10-20 11:56:00 +0100639 IF_ALOGV() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800640 printRefs();
641 }
642 IPCThreadState* ipc = IPCThreadState::self();
Steven Moreland5553ac42020-11-11 02:14:45 +0000643 if (ipc) ipc->decStrongHandle(binderHandle());
Steven Moreland80d23932019-06-07 12:43:27 -0700644
645 mLock.lock();
646 Vector<Obituary>* obits = mObituaries;
647 if(obits != nullptr) {
648 if (!obits->isEmpty()) {
Steven Moreland70fcb4e2019-12-13 17:28:58 -0800649 ALOGI("onLastStrongRef automatically unlinking death recipients: %s",
650 mDescriptorCache.size() ? String8(mDescriptorCache).c_str() : "<uncached descriptor>");
Steven Moreland80d23932019-06-07 12:43:27 -0700651 }
652
Steven Moreland5553ac42020-11-11 02:14:45 +0000653 if (ipc) ipc->clearDeathNotification(binderHandle(), this);
Steven Moreland80d23932019-06-07 12:43:27 -0700654 mObituaries = nullptr;
655 }
656 mLock.unlock();
657
658 if (obits != nullptr) {
659 // XXX Should we tell any remaining DeathRecipient
660 // objects that the last strong ref has gone away, so they
661 // are no longer linked?
662 delete obits;
663 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800664}
665
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800666bool BpBinder::onIncStrongAttempted(uint32_t /*flags*/, const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800667{
Steven Moreland5553ac42020-11-11 02:14:45 +0000668 // RPC binder doesn't currently support inc from weak binders
669 if (CC_UNLIKELY(isRpcBinder())) return false;
670
Steven Moreland32150282021-11-12 22:54:53 +0000671 if constexpr (!kEnableKernelIpc) {
672 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
673 return false;
674 }
675
Steven Moreland5553ac42020-11-11 02:14:45 +0000676 ALOGV("onIncStrongAttempted BpBinder %p handle %d\n", this, binderHandle());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800677 IPCThreadState* ipc = IPCThreadState::self();
Steven Moreland5553ac42020-11-11 02:14:45 +0000678 return ipc ? ipc->attemptIncStrongHandle(binderHandle()) == NO_ERROR : false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800679}
680
Michael Wachenschwanzd296d0c2017-08-15 00:57:14 -0700681uint32_t BpBinder::getBinderProxyCount(uint32_t uid)
682{
683 AutoMutex _l(sTrackingLock);
684 auto it = sTrackingMap.find(uid);
685 if (it != sTrackingMap.end()) {
686 return it->second & COUNTING_VALUE_MASK;
687 }
688 return 0;
689}
690
691void BpBinder::getCountByUid(Vector<uint32_t>& uids, Vector<uint32_t>& counts)
692{
693 AutoMutex _l(sTrackingLock);
694 uids.setCapacity(sTrackingMap.size());
695 counts.setCapacity(sTrackingMap.size());
696 for (const auto& it : sTrackingMap) {
697 uids.push_back(it.first);
698 counts.push_back(it.second & COUNTING_VALUE_MASK);
699 }
700}
701
702void BpBinder::enableCountByUid() { sCountByUidEnabled.store(true); }
703void BpBinder::disableCountByUid() { sCountByUidEnabled.store(false); }
704void BpBinder::setCountByUidEnabled(bool enable) { sCountByUidEnabled.store(enable); }
705
706void BpBinder::setLimitCallback(binder_proxy_limit_callback cb) {
707 AutoMutex _l(sTrackingLock);
708 sLimitCallback = cb;
709}
710
711void BpBinder::setBinderProxyCountWatermarks(int high, int low) {
712 AutoMutex _l(sTrackingLock);
713 sBinderProxyCountHighWatermark = high;
714 sBinderProxyCountLowWatermark = low;
715}
716
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800717// ---------------------------------------------------------------------------
718
Steven Moreland61ff8492019-09-26 16:05:45 -0700719} // namespace android