blob: bf7a0ba5f07d79811369f93fbd3ef909666aa089 [file] [log] [blame]
Steven Moreland2e87adc2018-08-20 19:47:00 -07001/*
2 * Copyright (C) 2018 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#include <android/binder_ibinder.h>
Steven Moreland2f405f52020-07-08 22:24:29 +000018#include <android/binder_ibinder_platform.h>
Steven Moreland46b5fea2019-10-15 11:22:18 -070019#include <android/binder_stability.h>
Steven Moreland2e87adc2018-08-20 19:47:00 -070020#include <android/binder_status.h>
Steven Morelandf3034b02018-11-12 17:37:46 -080021#include <binder/IPCThreadState.h>
Ruchir Rastogicc7a7462020-01-31 14:29:15 -080022#include <binder/IResultReceiver.h>
Tomasz Wasilczykb8ebcca2023-10-09 19:54:20 +000023#if __has_include(<private/android_filesystem_config.h>)
Ruchir Rastogicc7a7462020-01-31 14:29:15 -080024#include <private/android_filesystem_config.h>
Tomasz Wasilczykb8ebcca2023-10-09 19:54:20 +000025#endif
Steven Moreland2e87adc2018-08-20 19:47:00 -070026
Steven Moreland39fdec92022-08-27 00:27:37 +000027#include "ibinder_internal.h"
28#include "parcel_internal.h"
29#include "status_internal.h"
30
Steven Moreland901c7452018-09-04 16:17:28 -070031using DeathRecipient = ::android::IBinder::DeathRecipient;
32
Steven Moreland2e87adc2018-08-20 19:47:00 -070033using ::android::IBinder;
Ruchir Rastogicc7a7462020-01-31 14:29:15 -080034using ::android::IResultReceiver;
Steven Moreland2e87adc2018-08-20 19:47:00 -070035using ::android::Parcel;
36using ::android::sp;
Steven Moreland5d62e442018-09-13 15:01:02 -070037using ::android::status_t;
Steven Moreland009fc1a2022-06-10 17:24:25 +000038using ::android::statusToString;
Steven Moreland2e87adc2018-08-20 19:47:00 -070039using ::android::String16;
Steven Morelanda194c452019-03-04 16:47:07 -080040using ::android::String8;
Steven Moreland2e87adc2018-08-20 19:47:00 -070041using ::android::wp;
42
Steven Moreland71cddc32018-08-30 23:39:22 -070043namespace ABBinderTag {
44
45static const void* kId = "ABBinder";
46static void* kValue = static_cast<void*>(new bool{true});
Steven Moreland94968952018-09-05 14:42:59 -070047void clean(const void* /*id*/, void* /*obj*/, void* /*cookie*/){/* do nothing */};
Steven Moreland71cddc32018-08-30 23:39:22 -070048
49static void attach(const sp<IBinder>& binder) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -070050 auto alreadyAttached = binder->attachObject(kId, kValue, nullptr /*cookie*/, clean);
51 LOG_ALWAYS_FATAL_IF(alreadyAttached != nullptr, "can only attach once");
Steven Moreland71cddc32018-08-30 23:39:22 -070052}
53static bool has(const sp<IBinder>& binder) {
54 return binder != nullptr && binder->findObject(kId) == kValue;
55}
56
Steven Moreland6cf66ac2018-11-02 18:14:54 -070057} // namespace ABBinderTag
Steven Moreland71cddc32018-08-30 23:39:22 -070058
Steven Moreland94968952018-09-05 14:42:59 -070059namespace ABpBinderTag {
60
Steven Moreland94968952018-09-05 14:42:59 -070061static const void* kId = "ABpBinder";
62struct Value {
63 wp<ABpBinder> binder;
64};
65void clean(const void* id, void* obj, void* cookie) {
Steven Moreland3acd4902022-07-26 18:29:58 +000066 // be weary of leaks!
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -070067 // ALOGI("Deleting an ABpBinder");
Steven Moreland3acd4902022-07-26 18:29:58 +000068
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -070069 LOG_ALWAYS_FATAL_IF(id != kId, "%p %p %p", id, obj, cookie);
Steven Moreland94968952018-09-05 14:42:59 -070070
71 delete static_cast<Value*>(obj);
72};
73
Steven Moreland6cf66ac2018-11-02 18:14:54 -070074} // namespace ABpBinderTag
Steven Moreland94968952018-09-05 14:42:59 -070075
Steven Moreland2e87adc2018-08-20 19:47:00 -070076AIBinder::AIBinder(const AIBinder_Class* clazz) : mClazz(clazz) {}
77AIBinder::~AIBinder() {}
78
Steven Moreland94a76792022-12-21 01:20:06 +000079// b/175635923 libcxx causes "implicit-conversion" with a string with invalid char
80static std::string SanitizeString(const String16& str) {
81 std::string sanitized{String8(str)};
82 for (auto& c : sanitized) {
83 if (!isprint(c)) {
84 c = '?';
85 }
86 }
87 return sanitized;
88}
89
90bool AIBinder::associateClass(const AIBinder_Class* clazz) {
91 if (clazz == nullptr) return false;
92
93 // If mClazz is non-null, this must have been called and cached
94 // already. So, we can safely call this first. Due to the implementation
95 // of getInterfaceDescriptor (at time of writing), two simultaneous calls
96 // may lead to extra binder transactions, but this is expected to be
97 // exceedingly rare. Once we have a binder, when we get it again later,
98 // we won't make another binder transaction here.
99 const String16& descriptor = getBinder()->getInterfaceDescriptor();
100 const String16& newDescriptor = clazz->getInterfaceDescriptor();
101
Andrei Homescu9556bb12020-09-21 16:59:45 -0700102 std::lock_guard<std::mutex> lock(mClazzMutex);
Steven Moreland71cddc32018-08-30 23:39:22 -0700103 if (mClazz == clazz) return true;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700104
Steven Morelandfaf25a42022-12-21 02:21:36 +0000105 // If this is an ABpBinder, the first class object becomes the canonical one. The implication
106 // of this is that no API can require a proxy information to get information on how to behave.
107 // from the class itself - which should only store the interface descriptor. The functionality
108 // should be implemented by adding AIBinder_* APIs to set values on binders themselves, by
109 // setting things on AIBinder_Class which get transferred along with the binder, so that they
110 // can be read along with the BpBinder, or by modifying APIs directly (e.g. an option in
111 // onTransact).
112 //
113 // While this check is required to support linkernamespaces, one downside of it is that
114 // you may parcel code to communicate between things in the same process. However, comms
115 // between linkernamespaces like this already happen for cross-language calls like Java<->C++
116 // or Rust<->Java, and there are good stability guarantees here. This interacts with
117 // binder Stability checks exactly like any other in-process call. The stability is known
118 // to the IBinder object, so that it doesn't matter if a class object comes from
119 // a different stability level.
120 if (mClazz != nullptr && !asABpBinder()) {
Steven Moreland63872b52020-10-02 19:28:23 +0000121 const String16& currentDescriptor = mClazz->getInterfaceDescriptor();
Steven Moreland2e87adc2018-08-20 19:47:00 -0700122 if (newDescriptor == currentDescriptor) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700123 ALOGE("Class descriptors '%s' match during associateClass, but they are different class"
124 " objects (%p vs %p). Class descriptor collision?",
125 String8(currentDescriptor).c_str(), clazz, mClazz);
Steven Moreland71cddc32018-08-30 23:39:22 -0700126 } else {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700127 ALOGE("%s: Class cannot be associated on object which already has a class. "
128 "Trying to associate to '%s' but already set to '%s'.",
129 __func__, String8(newDescriptor).c_str(), String8(currentDescriptor).c_str());
Steven Moreland2e87adc2018-08-20 19:47:00 -0700130 }
131
Steven Moreland71cddc32018-08-30 23:39:22 -0700132 // always a failure because we know mClazz != clazz
133 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700134 }
135
Steven Moreland94a76792022-12-21 01:20:06 +0000136 // This will always be an O(n) comparison, but it's expected to be extremely rare.
137 // since it's an error condition. Do the comparison after we take the lock and
138 // check the pointer equality fast path. By always taking the lock, it's also
139 // more flake-proof. However, the check is not dependent on the lock.
Steven Moreland418914a2023-06-28 21:47:14 +0000140 if (descriptor != newDescriptor && !(asABpBinder() && asABpBinder()->isServiceFuzzing())) {
Steven Moreland640c4092020-07-08 00:19:58 +0000141 if (getBinder()->isBinderAlive()) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700142 ALOGE("%s: Expecting binder to have class '%s' but descriptor is actually '%s'.",
143 __func__, String8(newDescriptor).c_str(), SanitizeString(descriptor).c_str());
Steven Moreland640c4092020-07-08 00:19:58 +0000144 } else {
145 // b/155793159
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700146 ALOGE("%s: Cannot associate class '%s' to dead binder with cached descriptor '%s'.",
147 __func__, String8(newDescriptor).c_str(), SanitizeString(descriptor).c_str());
Steven Moreland640c4092020-07-08 00:19:58 +0000148 }
Steven Moreland71cddc32018-08-30 23:39:22 -0700149 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700150 }
151
Steven Morelandfaf25a42022-12-21 02:21:36 +0000152 // A local binder being set for the first time OR
153 // ignoring a proxy binder which is set multiple time, by considering the first
154 // associated class as the canonical one.
155 if (mClazz == nullptr) {
156 mClazz = clazz;
157 }
158
Steven Moreland94a76792022-12-21 01:20:06 +0000159 return true;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700160}
161
162ABBinder::ABBinder(const AIBinder_Class* clazz, void* userData)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700163 : AIBinder(clazz), BBinder(), mUserData(userData) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700164 LOG_ALWAYS_FATAL_IF(clazz == nullptr, "clazz == nullptr");
Steven Moreland2e87adc2018-08-20 19:47:00 -0700165}
166ABBinder::~ABBinder() {
167 getClass()->onDestroy(mUserData);
168}
169
170const String16& ABBinder::getInterfaceDescriptor() const {
171 return getClass()->getInterfaceDescriptor();
172}
173
Steven Morelanda194c452019-03-04 16:47:07 -0800174status_t ABBinder::dump(int fd, const ::android::Vector<String16>& args) {
175 AIBinder_onDump onDump = getClass()->onDump;
176
177 if (onDump == nullptr) {
178 return STATUS_OK;
179 }
180
181 // technically UINT32_MAX would be okay here, but INT32_MAX is expected since this may be
182 // null in Java
183 if (args.size() > INT32_MAX) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700184 ALOGE("ABBinder::dump received too many arguments: %zu", args.size());
Steven Morelanda194c452019-03-04 16:47:07 -0800185 return STATUS_BAD_VALUE;
186 }
187
188 std::vector<String8> utf8Args; // owns memory of utf8s
189 utf8Args.reserve(args.size());
190 std::vector<const char*> utf8Pointers; // what can be passed over NDK API
191 utf8Pointers.reserve(args.size());
192
193 for (size_t i = 0; i < args.size(); i++) {
194 utf8Args.push_back(String8(args[i]));
195 utf8Pointers.push_back(utf8Args[i].c_str());
196 }
197
198 return onDump(this, fd, utf8Pointers.data(), utf8Pointers.size());
199}
200
Steven Moreland5d62e442018-09-13 15:01:02 -0700201status_t ABBinder::onTransact(transaction_code_t code, const Parcel& data, Parcel* reply,
202 binder_flags_t flags) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700203 if (isUserCommand(code)) {
Steven Morelandf3ed8062021-08-27 14:31:14 -0700204 if (getClass()->writeHeader && !data.checkInterface(this)) {
Steven Moreland9d089af2018-09-18 08:58:09 -0700205 return STATUS_BAD_TYPE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700206 }
207
208 const AParcel in = AParcel::readOnly(this, &data);
209 AParcel out = AParcel(this, reply, false /*owns*/);
210
Steven Moreland5d62e442018-09-13 15:01:02 -0700211 binder_status_t status = getClass()->onTransact(this, code, &in, &out);
212 return PruneStatusT(status);
Steven Moreland88234072020-08-06 19:32:45 +0000213 } else if (code == SHELL_COMMAND_TRANSACTION && getClass()->handleShellCommand != nullptr) {
Ruchir Rastogicc7a7462020-01-31 14:29:15 -0800214 int in = data.readFileDescriptor();
215 int out = data.readFileDescriptor();
216 int err = data.readFileDescriptor();
217
218 int argc = data.readInt32();
219 std::vector<String8> utf8Args; // owns memory of utf8s
220 std::vector<const char*> utf8Pointers; // what can be passed over NDK API
221 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
222 utf8Args.push_back(String8(data.readString16()));
223 utf8Pointers.push_back(utf8Args[i].c_str());
224 }
225
226 data.readStrongBinder(); // skip over the IShellCallback
227 sp<IResultReceiver> resultReceiver = IResultReceiver::asInterface(data.readStrongBinder());
228
229 // Shell commands should only be callable by ADB.
230 uid_t uid = AIBinder_getCallingUid();
Tomasz Wasilczykb8ebcca2023-10-09 19:54:20 +0000231 if (uid != 0 /* root */
232#ifdef AID_SHELL
233 && uid != AID_SHELL
234#endif
235 ) {
Ruchir Rastogicc7a7462020-01-31 14:29:15 -0800236 if (resultReceiver != nullptr) {
237 resultReceiver->send(-1);
238 }
239 return STATUS_PERMISSION_DENIED;
240 }
241
242 // Check that the file descriptors are valid.
243 if (in == STATUS_BAD_TYPE || out == STATUS_BAD_TYPE || err == STATUS_BAD_TYPE) {
244 if (resultReceiver != nullptr) {
245 resultReceiver->send(-1);
246 }
247 return STATUS_BAD_VALUE;
248 }
249
250 binder_status_t status = getClass()->handleShellCommand(
251 this, in, out, err, utf8Pointers.data(), utf8Pointers.size());
252 if (resultReceiver != nullptr) {
253 resultReceiver->send(status);
254 }
255 return status;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700256 } else {
257 return BBinder::onTransact(code, data, reply, flags);
258 }
259}
260
Steven Moreland71cddc32018-08-30 23:39:22 -0700261ABpBinder::ABpBinder(const ::android::sp<::android::IBinder>& binder)
Steven Moreland3acd4902022-07-26 18:29:58 +0000262 : AIBinder(nullptr /*clazz*/), mRemote(binder) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700263 LOG_ALWAYS_FATAL_IF(binder == nullptr, "binder == nullptr");
Steven Moreland2e87adc2018-08-20 19:47:00 -0700264}
265ABpBinder::~ABpBinder() {}
266
Steven Moreland94968952018-09-05 14:42:59 -0700267sp<AIBinder> ABpBinder::lookupOrCreateFromBinder(const ::android::sp<::android::IBinder>& binder) {
Steven Moreland71cddc32018-08-30 23:39:22 -0700268 if (binder == nullptr) {
269 return nullptr;
270 }
271 if (ABBinderTag::has(binder)) {
272 return static_cast<ABBinder*>(binder.get());
273 }
Steven Moreland94968952018-09-05 14:42:59 -0700274
Steven Moreland91a96b62021-06-30 23:07:13 +0000275 // The following code ensures that for a given binder object (remote or local), if it is not an
276 // ABBinder then at most one ABpBinder object exists in a given process representing it.
277
Steven Moreland1f1bed62021-06-25 21:50:50 +0000278 auto* value = static_cast<ABpBinderTag::Value*>(binder->findObject(ABpBinderTag::kId));
Steven Moreland94968952018-09-05 14:42:59 -0700279 if (value == nullptr) {
280 value = new ABpBinderTag::Value;
Steven Moreland1f1bed62021-06-25 21:50:50 +0000281 auto oldValue = static_cast<ABpBinderTag::Value*>(
282 binder->attachObject(ABpBinderTag::kId, static_cast<void*>(value),
283 nullptr /*cookie*/, ABpBinderTag::clean));
284
285 // allocated by another thread
286 if (oldValue) {
287 delete value;
288 value = oldValue;
289 }
Steven Moreland94968952018-09-05 14:42:59 -0700290 }
291
Steven Moreland1f1bed62021-06-25 21:50:50 +0000292 sp<ABpBinder> ret;
293 binder->withLock([&]() {
294 ret = value->binder.promote();
295 if (ret == nullptr) {
296 ret = sp<ABpBinder>::make(binder);
297 value->binder = ret;
298 }
299 });
Steven Moreland94968952018-09-05 14:42:59 -0700300
301 return ret;
Steven Moreland71cddc32018-08-30 23:39:22 -0700302}
303
Steven Moreland2e87adc2018-08-20 19:47:00 -0700304struct AIBinder_Weak {
305 wp<AIBinder> binder;
306};
307AIBinder_Weak* AIBinder_Weak_new(AIBinder* binder) {
Steven Moreland5ccb70f2018-09-04 16:30:21 -0700308 if (binder == nullptr) {
309 return nullptr;
310 }
311
Steven Moreland2e87adc2018-08-20 19:47:00 -0700312 return new AIBinder_Weak{wp<AIBinder>(binder)};
313}
Steven Moreland9b80e282018-09-19 13:57:23 -0700314void AIBinder_Weak_delete(AIBinder_Weak* weakBinder) {
315 delete weakBinder;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700316}
317AIBinder* AIBinder_Weak_promote(AIBinder_Weak* weakBinder) {
Steven Moreland5ccb70f2018-09-04 16:30:21 -0700318 if (weakBinder == nullptr) {
319 return nullptr;
320 }
321
Steven Moreland2e87adc2018-08-20 19:47:00 -0700322 sp<AIBinder> binder = weakBinder->binder.promote();
323 AIBinder_incStrong(binder.get());
324 return binder.get();
325}
326
Steven Morelandc70e0122021-01-08 02:44:53 +0000327AIBinder_Weak* AIBinder_Weak_clone(const AIBinder_Weak* weak) {
328 if (weak == nullptr) {
329 return nullptr;
330 }
331
332 return new AIBinder_Weak{weak->binder};
333}
334
335bool AIBinder_lt(const AIBinder* lhs, const AIBinder* rhs) {
336 if (lhs == nullptr || rhs == nullptr) return lhs < rhs;
337
338 return const_cast<AIBinder*>(lhs)->getBinder() < const_cast<AIBinder*>(rhs)->getBinder();
339}
340
341bool AIBinder_Weak_lt(const AIBinder_Weak* lhs, const AIBinder_Weak* rhs) {
342 if (lhs == nullptr || rhs == nullptr) return lhs < rhs;
343
344 return lhs->binder < rhs->binder;
345}
346
Steven Morelandfaf25a42022-12-21 02:21:36 +0000347// WARNING: When multiple classes exist with the same interface descriptor in different
348// linkernamespaces, the first one to be associated with mClazz becomes the canonical one
349// and the only requirement on this is that the interface descriptors match. If this
350// is an ABpBinder, no other state can be referenced from mClazz.
Steven Moreland2e87adc2018-08-20 19:47:00 -0700351AIBinder_Class::AIBinder_Class(const char* interfaceDescriptor, AIBinder_Class_onCreate onCreate,
352 AIBinder_Class_onDestroy onDestroy,
353 AIBinder_Class_onTransact onTransact)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700354 : onCreate(onCreate),
355 onDestroy(onDestroy),
356 onTransact(onTransact),
Stephen Crane8fde87f2020-11-17 15:06:11 -0800357 mInterfaceDescriptor(interfaceDescriptor),
358 mWideInterfaceDescriptor(interfaceDescriptor) {}
Steven Moreland2e87adc2018-08-20 19:47:00 -0700359
360AIBinder_Class* AIBinder_Class_define(const char* interfaceDescriptor,
361 AIBinder_Class_onCreate onCreate,
362 AIBinder_Class_onDestroy onDestroy,
363 AIBinder_Class_onTransact onTransact) {
364 if (interfaceDescriptor == nullptr || onCreate == nullptr || onDestroy == nullptr ||
365 onTransact == nullptr) {
366 return nullptr;
367 }
368
369 return new AIBinder_Class(interfaceDescriptor, onCreate, onDestroy, onTransact);
370}
371
Steven Morelanda194c452019-03-04 16:47:07 -0800372void AIBinder_Class_setOnDump(AIBinder_Class* clazz, AIBinder_onDump onDump) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700373 LOG_ALWAYS_FATAL_IF(clazz == nullptr, "setOnDump requires non-null clazz");
Steven Morelanda194c452019-03-04 16:47:07 -0800374
375 // this is required to be called before instances are instantiated
376 clazz->onDump = onDump;
377}
378
Steven Morelandf3ed8062021-08-27 14:31:14 -0700379void AIBinder_Class_disableInterfaceTokenHeader(AIBinder_Class* clazz) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700380 LOG_ALWAYS_FATAL_IF(clazz == nullptr, "disableInterfaceTokenHeader requires non-null clazz");
Steven Morelandf3ed8062021-08-27 14:31:14 -0700381
382 clazz->writeHeader = false;
383}
384
Ruchir Rastogicc7a7462020-01-31 14:29:15 -0800385void AIBinder_Class_setHandleShellCommand(AIBinder_Class* clazz,
386 AIBinder_handleShellCommand handleShellCommand) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700387 LOG_ALWAYS_FATAL_IF(clazz == nullptr, "setHandleShellCommand requires non-null clazz");
Ruchir Rastogicc7a7462020-01-31 14:29:15 -0800388
389 clazz->handleShellCommand = handleShellCommand;
390}
391
Stephen Crane8fde87f2020-11-17 15:06:11 -0800392const char* AIBinder_Class_getDescriptor(const AIBinder_Class* clazz) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700393 LOG_ALWAYS_FATAL_IF(clazz == nullptr, "getDescriptor requires non-null clazz");
Stephen Crane8fde87f2020-11-17 15:06:11 -0800394
395 return clazz->getInterfaceDescriptorUtf8();
396}
397
Alice Ryhlea9d9d22021-08-27 07:51:30 +0000398AIBinder_DeathRecipient::TransferDeathRecipient::~TransferDeathRecipient() {
399 if (mOnUnlinked != nullptr) {
400 mOnUnlinked(mCookie);
401 }
402}
403
Steven Moreland901c7452018-09-04 16:17:28 -0700404void AIBinder_DeathRecipient::TransferDeathRecipient::binderDied(const wp<IBinder>& who) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700405 LOG_ALWAYS_FATAL_IF(who != mWho, "%p (%p) vs %p (%p)", who.unsafe_get(), who.get_refs(),
406 mWho.unsafe_get(), mWho.get_refs());
Steven Moreland901c7452018-09-04 16:17:28 -0700407
408 mOnDied(mCookie);
Steven Moreland64127ca2019-03-13 09:25:44 -0700409
410 sp<AIBinder_DeathRecipient> recipient = mParentRecipient.promote();
411 sp<IBinder> strongWho = who.promote();
412
413 // otherwise this will be cleaned up later with pruneDeadTransferEntriesLocked
414 if (recipient != nullptr && strongWho != nullptr) {
415 status_t result = recipient->unlinkToDeath(strongWho, mCookie);
416 if (result != ::android::DEAD_OBJECT) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700417 ALOGW("Unlinking to dead binder resulted in: %d", result);
Steven Moreland64127ca2019-03-13 09:25:44 -0700418 }
419 }
420
Steven Moreland901c7452018-09-04 16:17:28 -0700421 mWho = nullptr;
422}
423
424AIBinder_DeathRecipient::AIBinder_DeathRecipient(AIBinder_DeathRecipient_onBinderDied onDied)
Alice Ryhlea9d9d22021-08-27 07:51:30 +0000425 : mOnDied(onDied), mOnUnlinked(nullptr) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700426 LOG_ALWAYS_FATAL_IF(onDied == nullptr, "onDied == nullptr");
Steven Moreland901c7452018-09-04 16:17:28 -0700427}
428
Steven Moreland64127ca2019-03-13 09:25:44 -0700429void AIBinder_DeathRecipient::pruneDeadTransferEntriesLocked() {
430 mDeathRecipients.erase(std::remove_if(mDeathRecipients.begin(), mDeathRecipients.end(),
431 [](const sp<TransferDeathRecipient>& tdr) {
432 return tdr->getWho() == nullptr;
433 }),
434 mDeathRecipients.end());
435}
436
Jiyong Park4f97a8c2020-11-16 18:11:14 +0900437binder_status_t AIBinder_DeathRecipient::linkToDeath(const sp<IBinder>& binder, void* cookie) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700438 LOG_ALWAYS_FATAL_IF(binder == nullptr, "binder == nullptr");
Steven Moreland901c7452018-09-04 16:17:28 -0700439
440 std::lock_guard<std::mutex> l(mDeathRecipientsMutex);
441
442 sp<TransferDeathRecipient> recipient =
Alice Ryhlea9d9d22021-08-27 07:51:30 +0000443 new TransferDeathRecipient(binder, cookie, this, mOnDied, mOnUnlinked);
Steven Moreland901c7452018-09-04 16:17:28 -0700444
Steven Moreland64127ca2019-03-13 09:25:44 -0700445 status_t status = binder->linkToDeath(recipient, cookie, 0 /*flags*/);
Steven Moreland5d62e442018-09-13 15:01:02 -0700446 if (status != STATUS_OK) {
Alice Ryhlea9d9d22021-08-27 07:51:30 +0000447 // When we failed to link, the destructor of TransferDeathRecipient runs here, which
448 // ensures that mOnUnlinked is called before we return with an error from this method.
Steven Moreland5d62e442018-09-13 15:01:02 -0700449 return PruneStatusT(status);
Steven Moreland901c7452018-09-04 16:17:28 -0700450 }
451
452 mDeathRecipients.push_back(recipient);
Steven Moreland64127ca2019-03-13 09:25:44 -0700453
454 pruneDeadTransferEntriesLocked();
Steven Moreland5d62e442018-09-13 15:01:02 -0700455 return STATUS_OK;
Steven Moreland901c7452018-09-04 16:17:28 -0700456}
457
Jiyong Park4f97a8c2020-11-16 18:11:14 +0900458binder_status_t AIBinder_DeathRecipient::unlinkToDeath(const sp<IBinder>& binder, void* cookie) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700459 LOG_ALWAYS_FATAL_IF(binder == nullptr, "binder == nullptr");
Steven Moreland901c7452018-09-04 16:17:28 -0700460
461 std::lock_guard<std::mutex> l(mDeathRecipientsMutex);
462
463 for (auto it = mDeathRecipients.rbegin(); it != mDeathRecipients.rend(); ++it) {
464 sp<TransferDeathRecipient> recipient = *it;
465
Steven Moreland64127ca2019-03-13 09:25:44 -0700466 if (recipient->getCookie() == cookie && recipient->getWho() == binder) {
Steven Moreland901c7452018-09-04 16:17:28 -0700467 mDeathRecipients.erase(it.base() - 1);
468
Steven Moreland64127ca2019-03-13 09:25:44 -0700469 status_t status = binder->unlinkToDeath(recipient, cookie, 0 /*flags*/);
Steven Moreland5d62e442018-09-13 15:01:02 -0700470 if (status != ::android::OK) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700471 ALOGE("%s: removed reference to death recipient but unlink failed: %s", __func__,
472 statusToString(status).c_str());
Steven Moreland901c7452018-09-04 16:17:28 -0700473 }
Steven Moreland5d62e442018-09-13 15:01:02 -0700474 return PruneStatusT(status);
Steven Moreland901c7452018-09-04 16:17:28 -0700475 }
476 }
477
Steven Moreland5d62e442018-09-13 15:01:02 -0700478 return STATUS_NAME_NOT_FOUND;
Steven Moreland901c7452018-09-04 16:17:28 -0700479}
480
Alice Ryhlea9d9d22021-08-27 07:51:30 +0000481void AIBinder_DeathRecipient::setOnUnlinked(AIBinder_DeathRecipient_onBinderUnlinked onUnlinked) {
482 mOnUnlinked = onUnlinked;
483}
484
Steven Moreland901c7452018-09-04 16:17:28 -0700485// start of C-API methods
486
Steven Moreland2e87adc2018-08-20 19:47:00 -0700487AIBinder* AIBinder_new(const AIBinder_Class* clazz, void* args) {
488 if (clazz == nullptr) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700489 ALOGE("%s: Must provide class to construct local binder.", __func__);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700490 return nullptr;
491 }
492
493 void* userData = clazz->onCreate(args);
494
Steven Moreland71cddc32018-08-30 23:39:22 -0700495 sp<AIBinder> ret = new ABBinder(clazz, userData);
496 ABBinderTag::attach(ret->getBinder());
497
498 AIBinder_incStrong(ret.get());
499 return ret.get();
Steven Moreland2e87adc2018-08-20 19:47:00 -0700500}
501
Steven Moreland5ea54da2018-09-04 13:29:55 -0700502bool AIBinder_isRemote(const AIBinder* binder) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700503 if (binder == nullptr) {
Steven Moreland56f752a2018-11-05 17:23:37 -0800504 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700505 }
506
507 return binder->isRemote();
508}
509
Steven Moreland65867d72018-09-04 14:22:26 -0700510bool AIBinder_isAlive(const AIBinder* binder) {
511 if (binder == nullptr) {
512 return false;
513 }
514
515 return const_cast<AIBinder*>(binder)->getBinder()->isBinderAlive();
516}
517
518binder_status_t AIBinder_ping(AIBinder* binder) {
519 if (binder == nullptr) {
Steven Moreland5d62e442018-09-13 15:01:02 -0700520 return STATUS_UNEXPECTED_NULL;
Steven Moreland65867d72018-09-04 14:22:26 -0700521 }
522
Steven Moreland5d62e442018-09-13 15:01:02 -0700523 return PruneStatusT(binder->getBinder()->pingBinder());
Steven Moreland65867d72018-09-04 14:22:26 -0700524}
525
Steven Morelanda194c452019-03-04 16:47:07 -0800526binder_status_t AIBinder_dump(AIBinder* binder, int fd, const char** args, uint32_t numArgs) {
527 if (binder == nullptr) {
528 return STATUS_UNEXPECTED_NULL;
529 }
530
531 ABBinder* bBinder = binder->asABBinder();
532 if (bBinder != nullptr) {
533 AIBinder_onDump onDump = binder->getClass()->onDump;
534 if (onDump == nullptr) {
535 return STATUS_OK;
536 }
537 return PruneStatusT(onDump(bBinder, fd, args, numArgs));
538 }
539
540 ::android::Vector<String16> utf16Args;
541 utf16Args.setCapacity(numArgs);
542 for (uint32_t i = 0; i < numArgs; i++) {
543 utf16Args.push(String16(String8(args[i])));
544 }
545
546 status_t status = binder->getBinder()->dump(fd, utf16Args);
547 return PruneStatusT(status);
548}
549
Steven Moreland901c7452018-09-04 16:17:28 -0700550binder_status_t AIBinder_linkToDeath(AIBinder* binder, AIBinder_DeathRecipient* recipient,
551 void* cookie) {
552 if (binder == nullptr || recipient == nullptr) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700553 ALOGE("%s: Must provide binder (%p) and recipient (%p)", __func__, binder, recipient);
Steven Moreland5d62e442018-09-13 15:01:02 -0700554 return STATUS_UNEXPECTED_NULL;
Steven Moreland901c7452018-09-04 16:17:28 -0700555 }
556
Steven Moreland5d62e442018-09-13 15:01:02 -0700557 // returns binder_status_t
Steven Moreland64127ca2019-03-13 09:25:44 -0700558 return recipient->linkToDeath(binder->getBinder(), cookie);
Steven Moreland901c7452018-09-04 16:17:28 -0700559}
560
561binder_status_t AIBinder_unlinkToDeath(AIBinder* binder, AIBinder_DeathRecipient* recipient,
562 void* cookie) {
563 if (binder == nullptr || recipient == nullptr) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700564 ALOGE("%s: Must provide binder (%p) and recipient (%p)", __func__, binder, recipient);
Steven Moreland5d62e442018-09-13 15:01:02 -0700565 return STATUS_UNEXPECTED_NULL;
Steven Moreland901c7452018-09-04 16:17:28 -0700566 }
567
Steven Moreland5d62e442018-09-13 15:01:02 -0700568 // returns binder_status_t
Steven Moreland64127ca2019-03-13 09:25:44 -0700569 return recipient->unlinkToDeath(binder->getBinder(), cookie);
Steven Moreland901c7452018-09-04 16:17:28 -0700570}
571
Steven Morelandf3034b02018-11-12 17:37:46 -0800572uid_t AIBinder_getCallingUid() {
573 return ::android::IPCThreadState::self()->getCallingUid();
574}
575
576pid_t AIBinder_getCallingPid() {
577 return ::android::IPCThreadState::self()->getCallingPid();
578}
579
Alice Ryhl59aeae82021-08-25 10:21:26 +0000580bool AIBinder_isHandlingTransaction() {
581 return ::android::IPCThreadState::self()->getServingStackPointer() != nullptr;
582}
583
Steven Moreland2e87adc2018-08-20 19:47:00 -0700584void AIBinder_incStrong(AIBinder* binder) {
585 if (binder == nullptr) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700586 return;
587 }
588
589 binder->incStrong(nullptr);
590}
591void AIBinder_decStrong(AIBinder* binder) {
592 if (binder == nullptr) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700593 ALOGE("%s: on null binder", __func__);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700594 return;
595 }
596
597 binder->decStrong(nullptr);
598}
599int32_t AIBinder_debugGetRefCount(AIBinder* binder) {
600 if (binder == nullptr) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700601 ALOGE("%s: on null binder", __func__);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700602 return -1;
603 }
604
605 return binder->getStrongCount();
606}
607
Steven Moreland71cddc32018-08-30 23:39:22 -0700608bool AIBinder_associateClass(AIBinder* binder, const AIBinder_Class* clazz) {
609 if (binder == nullptr) {
610 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700611 }
612
Steven Moreland71cddc32018-08-30 23:39:22 -0700613 return binder->associateClass(clazz);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700614}
615
616const AIBinder_Class* AIBinder_getClass(AIBinder* binder) {
617 if (binder == nullptr) {
618 return nullptr;
619 }
620
621 return binder->getClass();
622}
623
624void* AIBinder_getUserData(AIBinder* binder) {
625 if (binder == nullptr) {
626 return nullptr;
627 }
628
629 ABBinder* bBinder = binder->asABBinder();
630 if (bBinder == nullptr) {
631 return nullptr;
632 }
633
634 return bBinder->getUserData();
635}
636
637binder_status_t AIBinder_prepareTransaction(AIBinder* binder, AParcel** in) {
638 if (binder == nullptr || in == nullptr) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700639 ALOGE("%s: requires non-null parameters binder (%p) and in (%p).", __func__, binder, in);
Steven Moreland5d62e442018-09-13 15:01:02 -0700640 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700641 }
642 const AIBinder_Class* clazz = binder->getClass();
643 if (clazz == nullptr) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700644 ALOGE("%s: Class must be defined for a remote binder transaction. See "
645 "AIBinder_associateClass.",
646 __func__);
Steven Moreland5d62e442018-09-13 15:01:02 -0700647 return STATUS_INVALID_OPERATION;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700648 }
649
650 *in = new AParcel(binder);
Steven Moreland1fda67b2021-04-02 18:35:50 +0000651 (*in)->get()->markForBinder(binder->getBinder());
652
Steven Morelandf3ed8062021-08-27 14:31:14 -0700653 status_t status = android::OK;
Steven Morelandfaf25a42022-12-21 02:21:36 +0000654
655 // note - this is the only read of a value in clazz, and it comes with a warning
656 // on the API itself. Do not copy this design. Instead, attach data in a new
657 // version of the prepareTransaction function.
Steven Morelandf3ed8062021-08-27 14:31:14 -0700658 if (clazz->writeHeader) {
659 status = (*in)->get()->writeInterfaceToken(clazz->getInterfaceDescriptor());
660 }
Steven Moreland5d62e442018-09-13 15:01:02 -0700661 binder_status_t ret = PruneStatusT(status);
662
663 if (ret != STATUS_OK) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700664 delete *in;
665 *in = nullptr;
666 }
667
Steven Moreland5d62e442018-09-13 15:01:02 -0700668 return ret;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700669}
670
Steven Moreland9b80e282018-09-19 13:57:23 -0700671static void DestroyParcel(AParcel** parcel) {
672 delete *parcel;
673 *parcel = nullptr;
674}
675
Steven Moreland2e87adc2018-08-20 19:47:00 -0700676binder_status_t AIBinder_transact(AIBinder* binder, transaction_code_t code, AParcel** in,
677 AParcel** out, binder_flags_t flags) {
678 if (in == nullptr) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700679 ALOGE("%s: requires non-null in parameter", __func__);
Steven Moreland5d62e442018-09-13 15:01:02 -0700680 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700681 }
682
Steven Morelandcaa776c2018-09-04 13:48:11 -0700683 using AutoParcelDestroyer = std::unique_ptr<AParcel*, void (*)(AParcel**)>;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700684 // This object is the input to the transaction. This function takes ownership of it and deletes
685 // it.
Steven Moreland9b80e282018-09-19 13:57:23 -0700686 AutoParcelDestroyer forIn(in, DestroyParcel);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700687
688 if (!isUserCommand(code)) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700689 ALOGE("%s: Only user-defined transactions can be made from the NDK, but requested: %d",
690 __func__, code);
Steven Moreland5d62e442018-09-13 15:01:02 -0700691 return STATUS_UNKNOWN_TRANSACTION;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700692 }
693
Steven Morelandf183fdd2020-10-27 00:12:12 +0000694 constexpr binder_flags_t kAllFlags = FLAG_PRIVATE_VENDOR | FLAG_ONEWAY | FLAG_CLEAR_BUF;
Steven Moreland46b5fea2019-10-15 11:22:18 -0700695 if ((flags & ~kAllFlags) != 0) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700696 ALOGE("%s: Unrecognized flags sent: %d", __func__, flags);
Steven Moreland5d62e442018-09-13 15:01:02 -0700697 return STATUS_BAD_VALUE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700698 }
699
700 if (binder == nullptr || *in == nullptr || out == nullptr) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700701 ALOGE("%s: requires non-null parameters binder (%p), in (%p), and out (%p).", __func__,
702 binder, in, out);
Steven Moreland5d62e442018-09-13 15:01:02 -0700703 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700704 }
705
706 if ((*in)->getBinder() != binder) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700707 ALOGE("%s: parcel is associated with binder object %p but called with %p", __func__, binder,
708 (*in)->getBinder());
Steven Moreland5d62e442018-09-13 15:01:02 -0700709 return STATUS_BAD_VALUE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700710 }
711
712 *out = new AParcel(binder);
713
Steven Morelandf18615b2018-09-14 11:43:06 -0700714 status_t status = binder->getBinder()->transact(code, *(*in)->get(), (*out)->get(), flags);
Steven Moreland5d62e442018-09-13 15:01:02 -0700715 binder_status_t ret = PruneStatusT(status);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700716
Steven Moreland5d62e442018-09-13 15:01:02 -0700717 if (ret != STATUS_OK) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700718 delete *out;
719 *out = nullptr;
720 }
721
Steven Moreland5d62e442018-09-13 15:01:02 -0700722 return ret;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700723}
Steven Moreland901c7452018-09-04 16:17:28 -0700724
725AIBinder_DeathRecipient* AIBinder_DeathRecipient_new(
726 AIBinder_DeathRecipient_onBinderDied onBinderDied) {
727 if (onBinderDied == nullptr) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700728 ALOGE("%s: requires non-null onBinderDied parameter.", __func__);
Steven Moreland901c7452018-09-04 16:17:28 -0700729 return nullptr;
730 }
Steven Moreland64127ca2019-03-13 09:25:44 -0700731 auto ret = new AIBinder_DeathRecipient(onBinderDied);
732 ret->incStrong(nullptr);
733 return ret;
Steven Moreland901c7452018-09-04 16:17:28 -0700734}
735
Alice Ryhlea9d9d22021-08-27 07:51:30 +0000736void AIBinder_DeathRecipient_setOnUnlinked(AIBinder_DeathRecipient* recipient,
737 AIBinder_DeathRecipient_onBinderUnlinked onUnlinked) {
738 if (recipient == nullptr) {
739 return;
740 }
741
742 recipient->setOnUnlinked(onUnlinked);
743}
744
Steven Moreland9b80e282018-09-19 13:57:23 -0700745void AIBinder_DeathRecipient_delete(AIBinder_DeathRecipient* recipient) {
Steven Moreland64127ca2019-03-13 09:25:44 -0700746 if (recipient == nullptr) {
747 return;
748 }
749
750 recipient->decStrong(nullptr);
Steven Moreland901c7452018-09-04 16:17:28 -0700751}
Steven Morelandea14ef22019-08-20 10:50:08 -0700752
753binder_status_t AIBinder_getExtension(AIBinder* binder, AIBinder** outExt) {
754 if (binder == nullptr || outExt == nullptr) {
755 if (outExt != nullptr) {
756 *outExt = nullptr;
757 }
758 return STATUS_UNEXPECTED_NULL;
759 }
760
761 sp<IBinder> ext;
762 status_t res = binder->getBinder()->getExtension(&ext);
763
764 if (res != android::OK) {
765 *outExt = nullptr;
766 return PruneStatusT(res);
767 }
768
769 sp<AIBinder> ret = ABpBinder::lookupOrCreateFromBinder(ext);
770 if (ret != nullptr) ret->incStrong(binder);
771
772 *outExt = ret.get();
773 return STATUS_OK;
774}
775
776binder_status_t AIBinder_setExtension(AIBinder* binder, AIBinder* ext) {
777 if (binder == nullptr || ext == nullptr) {
778 return STATUS_UNEXPECTED_NULL;
779 }
780
781 ABBinder* rawBinder = binder->asABBinder();
782 if (rawBinder == nullptr) {
783 return STATUS_INVALID_OPERATION;
784 }
785
786 rawBinder->setExtension(ext->getBinder());
787 return STATUS_OK;
788}
Steven Moreland2f405f52020-07-08 22:24:29 +0000789
790// platform methods follow
791
792void AIBinder_setRequestingSid(AIBinder* binder, bool requestingSid) {
793 ABBinder* localBinder = binder->asABBinder();
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700794 LOG_ALWAYS_FATAL_IF(localBinder == nullptr,
795 "AIBinder_setRequestingSid must be called on a local binder");
Steven Moreland2f405f52020-07-08 22:24:29 +0000796
797 localBinder->setRequestingSid(requestingSid);
798}
799
800const char* AIBinder_getCallingSid() {
801 return ::android::IPCThreadState::self()->getCallingSid();
802}
Steven Morelandb2de9532020-05-29 21:44:41 +0000803
Ady Abraham96174ac2021-10-15 11:02:03 -0700804void AIBinder_setMinSchedulerPolicy(AIBinder* binder, int policy, int priority) {
805 binder->asABBinder()->setMinSchedulerPolicy(policy, priority);
806}
Steven Moreland454a26a2021-12-14 01:26:21 +0000807
808void AIBinder_setInheritRt(AIBinder* binder, bool inheritRt) {
809 ABBinder* localBinder = binder->asABBinder();
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700810 LOG_ALWAYS_FATAL_IF(localBinder == nullptr,
811 "AIBinder_setInheritRt must be called on a local binder");
Steven Moreland454a26a2021-12-14 01:26:21 +0000812
813 localBinder->setInheritRt(inheritRt);
814}