blob: e6d4f46c161b29b032164a28b46dcbf25d9fd0d3 [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
Andrei Homescu38eafb72024-03-22 22:38:08 +000027#include "../BuildFlags.h"
Steven Moreland39fdec92022-08-27 00:27:37 +000028#include "ibinder_internal.h"
29#include "parcel_internal.h"
30#include "status_internal.h"
31
Steven Moreland901c7452018-09-04 16:17:28 -070032using DeathRecipient = ::android::IBinder::DeathRecipient;
33
Steven Moreland2e87adc2018-08-20 19:47:00 -070034using ::android::IBinder;
Ruchir Rastogicc7a7462020-01-31 14:29:15 -080035using ::android::IResultReceiver;
Steven Moreland2e87adc2018-08-20 19:47:00 -070036using ::android::Parcel;
37using ::android::sp;
Steven Moreland5d62e442018-09-13 15:01:02 -070038using ::android::status_t;
Steven Moreland009fc1a2022-06-10 17:24:25 +000039using ::android::statusToString;
Steven Moreland2e87adc2018-08-20 19:47:00 -070040using ::android::String16;
Steven Morelanda194c452019-03-04 16:47:07 -080041using ::android::String8;
Steven Moreland2e87adc2018-08-20 19:47:00 -070042using ::android::wp;
43
Steven Moreland71cddc32018-08-30 23:39:22 -070044namespace ABBinderTag {
45
46static const void* kId = "ABBinder";
47static void* kValue = static_cast<void*>(new bool{true});
Steven Moreland94968952018-09-05 14:42:59 -070048void clean(const void* /*id*/, void* /*obj*/, void* /*cookie*/){/* do nothing */};
Steven Moreland71cddc32018-08-30 23:39:22 -070049
50static void attach(const sp<IBinder>& binder) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -070051 auto alreadyAttached = binder->attachObject(kId, kValue, nullptr /*cookie*/, clean);
52 LOG_ALWAYS_FATAL_IF(alreadyAttached != nullptr, "can only attach once");
Steven Moreland71cddc32018-08-30 23:39:22 -070053}
54static bool has(const sp<IBinder>& binder) {
55 return binder != nullptr && binder->findObject(kId) == kValue;
56}
57
Steven Moreland6cf66ac2018-11-02 18:14:54 -070058} // namespace ABBinderTag
Steven Moreland71cddc32018-08-30 23:39:22 -070059
Steven Moreland94968952018-09-05 14:42:59 -070060namespace ABpBinderTag {
61
Steven Moreland94968952018-09-05 14:42:59 -070062static const void* kId = "ABpBinder";
63struct Value {
64 wp<ABpBinder> binder;
65};
66void clean(const void* id, void* obj, void* cookie) {
Steven Moreland3acd4902022-07-26 18:29:58 +000067 // be weary of leaks!
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -070068 // ALOGI("Deleting an ABpBinder");
Steven Moreland3acd4902022-07-26 18:29:58 +000069
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -070070 LOG_ALWAYS_FATAL_IF(id != kId, "%p %p %p", id, obj, cookie);
Steven Moreland94968952018-09-05 14:42:59 -070071
72 delete static_cast<Value*>(obj);
73};
74
Steven Moreland6cf66ac2018-11-02 18:14:54 -070075} // namespace ABpBinderTag
Steven Moreland94968952018-09-05 14:42:59 -070076
Steven Moreland2e87adc2018-08-20 19:47:00 -070077AIBinder::AIBinder(const AIBinder_Class* clazz) : mClazz(clazz) {}
78AIBinder::~AIBinder() {}
79
Steven Moreland94a76792022-12-21 01:20:06 +000080// b/175635923 libcxx causes "implicit-conversion" with a string with invalid char
81static std::string SanitizeString(const String16& str) {
82 std::string sanitized{String8(str)};
83 for (auto& c : sanitized) {
84 if (!isprint(c)) {
85 c = '?';
86 }
87 }
88 return sanitized;
89}
90
91bool AIBinder::associateClass(const AIBinder_Class* clazz) {
92 if (clazz == nullptr) return false;
93
94 // If mClazz is non-null, this must have been called and cached
95 // already. So, we can safely call this first. Due to the implementation
96 // of getInterfaceDescriptor (at time of writing), two simultaneous calls
97 // may lead to extra binder transactions, but this is expected to be
98 // exceedingly rare. Once we have a binder, when we get it again later,
99 // we won't make another binder transaction here.
100 const String16& descriptor = getBinder()->getInterfaceDescriptor();
101 const String16& newDescriptor = clazz->getInterfaceDescriptor();
102
Andrei Homescu9556bb12020-09-21 16:59:45 -0700103 std::lock_guard<std::mutex> lock(mClazzMutex);
Steven Moreland71cddc32018-08-30 23:39:22 -0700104 if (mClazz == clazz) return true;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700105
Steven Morelandfaf25a42022-12-21 02:21:36 +0000106 // If this is an ABpBinder, the first class object becomes the canonical one. The implication
107 // of this is that no API can require a proxy information to get information on how to behave.
108 // from the class itself - which should only store the interface descriptor. The functionality
109 // should be implemented by adding AIBinder_* APIs to set values on binders themselves, by
110 // setting things on AIBinder_Class which get transferred along with the binder, so that they
111 // can be read along with the BpBinder, or by modifying APIs directly (e.g. an option in
112 // onTransact).
113 //
114 // While this check is required to support linkernamespaces, one downside of it is that
115 // you may parcel code to communicate between things in the same process. However, comms
116 // between linkernamespaces like this already happen for cross-language calls like Java<->C++
117 // or Rust<->Java, and there are good stability guarantees here. This interacts with
118 // binder Stability checks exactly like any other in-process call. The stability is known
119 // to the IBinder object, so that it doesn't matter if a class object comes from
120 // a different stability level.
121 if (mClazz != nullptr && !asABpBinder()) {
Steven Moreland63872b52020-10-02 19:28:23 +0000122 const String16& currentDescriptor = mClazz->getInterfaceDescriptor();
Steven Moreland2e87adc2018-08-20 19:47:00 -0700123 if (newDescriptor == currentDescriptor) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700124 ALOGE("Class descriptors '%s' match during associateClass, but they are different class"
125 " objects (%p vs %p). Class descriptor collision?",
126 String8(currentDescriptor).c_str(), clazz, mClazz);
Steven Moreland71cddc32018-08-30 23:39:22 -0700127 } else {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700128 ALOGE("%s: Class cannot be associated on object which already has a class. "
129 "Trying to associate to '%s' but already set to '%s'.",
130 __func__, String8(newDescriptor).c_str(), String8(currentDescriptor).c_str());
Steven Moreland2e87adc2018-08-20 19:47:00 -0700131 }
132
Steven Moreland71cddc32018-08-30 23:39:22 -0700133 // always a failure because we know mClazz != clazz
134 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700135 }
136
Steven Moreland94a76792022-12-21 01:20:06 +0000137 // This will always be an O(n) comparison, but it's expected to be extremely rare.
138 // since it's an error condition. Do the comparison after we take the lock and
139 // check the pointer equality fast path. By always taking the lock, it's also
140 // more flake-proof. However, the check is not dependent on the lock.
Steven Moreland418914a2023-06-28 21:47:14 +0000141 if (descriptor != newDescriptor && !(asABpBinder() && asABpBinder()->isServiceFuzzing())) {
Steven Moreland640c4092020-07-08 00:19:58 +0000142 if (getBinder()->isBinderAlive()) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700143 ALOGE("%s: Expecting binder to have class '%s' but descriptor is actually '%s'.",
144 __func__, String8(newDescriptor).c_str(), SanitizeString(descriptor).c_str());
Steven Moreland640c4092020-07-08 00:19:58 +0000145 } else {
146 // b/155793159
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700147 ALOGE("%s: Cannot associate class '%s' to dead binder with cached descriptor '%s'.",
148 __func__, String8(newDescriptor).c_str(), SanitizeString(descriptor).c_str());
Steven Moreland640c4092020-07-08 00:19:58 +0000149 }
Steven Moreland71cddc32018-08-30 23:39:22 -0700150 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700151 }
152
Steven Morelandfaf25a42022-12-21 02:21:36 +0000153 // A local binder being set for the first time OR
154 // ignoring a proxy binder which is set multiple time, by considering the first
155 // associated class as the canonical one.
156 if (mClazz == nullptr) {
157 mClazz = clazz;
158 }
159
Steven Moreland94a76792022-12-21 01:20:06 +0000160 return true;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700161}
162
163ABBinder::ABBinder(const AIBinder_Class* clazz, void* userData)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700164 : AIBinder(clazz), BBinder(), mUserData(userData) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700165 LOG_ALWAYS_FATAL_IF(clazz == nullptr, "clazz == nullptr");
Steven Moreland2e87adc2018-08-20 19:47:00 -0700166}
167ABBinder::~ABBinder() {
168 getClass()->onDestroy(mUserData);
169}
170
171const String16& ABBinder::getInterfaceDescriptor() const {
172 return getClass()->getInterfaceDescriptor();
173}
174
Steven Morelanda194c452019-03-04 16:47:07 -0800175status_t ABBinder::dump(int fd, const ::android::Vector<String16>& args) {
176 AIBinder_onDump onDump = getClass()->onDump;
177
178 if (onDump == nullptr) {
179 return STATUS_OK;
180 }
181
182 // technically UINT32_MAX would be okay here, but INT32_MAX is expected since this may be
183 // null in Java
184 if (args.size() > INT32_MAX) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700185 ALOGE("ABBinder::dump received too many arguments: %zu", args.size());
Steven Morelanda194c452019-03-04 16:47:07 -0800186 return STATUS_BAD_VALUE;
187 }
188
189 std::vector<String8> utf8Args; // owns memory of utf8s
190 utf8Args.reserve(args.size());
191 std::vector<const char*> utf8Pointers; // what can be passed over NDK API
192 utf8Pointers.reserve(args.size());
193
194 for (size_t i = 0; i < args.size(); i++) {
195 utf8Args.push_back(String8(args[i]));
196 utf8Pointers.push_back(utf8Args[i].c_str());
197 }
198
199 return onDump(this, fd, utf8Pointers.data(), utf8Pointers.size());
200}
201
Steven Moreland5d62e442018-09-13 15:01:02 -0700202status_t ABBinder::onTransact(transaction_code_t code, const Parcel& data, Parcel* reply,
203 binder_flags_t flags) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700204 if (isUserCommand(code)) {
Steven Morelandf3ed8062021-08-27 14:31:14 -0700205 if (getClass()->writeHeader && !data.checkInterface(this)) {
Steven Moreland9d089af2018-09-18 08:58:09 -0700206 return STATUS_BAD_TYPE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700207 }
208
209 const AParcel in = AParcel::readOnly(this, &data);
210 AParcel out = AParcel(this, reply, false /*owns*/);
211
Steven Moreland5d62e442018-09-13 15:01:02 -0700212 binder_status_t status = getClass()->onTransact(this, code, &in, &out);
213 return PruneStatusT(status);
Steven Moreland88234072020-08-06 19:32:45 +0000214 } else if (code == SHELL_COMMAND_TRANSACTION && getClass()->handleShellCommand != nullptr) {
Andrei Homescu38eafb72024-03-22 22:38:08 +0000215 if constexpr (!android::kEnableKernelIpc) {
216 // Non-IPC builds do not have getCallingUid(),
217 // so we have no way of authenticating the caller
218 return STATUS_PERMISSION_DENIED;
219 }
220
Ruchir Rastogicc7a7462020-01-31 14:29:15 -0800221 int in = data.readFileDescriptor();
222 int out = data.readFileDescriptor();
223 int err = data.readFileDescriptor();
224
225 int argc = data.readInt32();
226 std::vector<String8> utf8Args; // owns memory of utf8s
227 std::vector<const char*> utf8Pointers; // what can be passed over NDK API
228 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
229 utf8Args.push_back(String8(data.readString16()));
230 utf8Pointers.push_back(utf8Args[i].c_str());
231 }
232
233 data.readStrongBinder(); // skip over the IShellCallback
234 sp<IResultReceiver> resultReceiver = IResultReceiver::asInterface(data.readStrongBinder());
235
236 // Shell commands should only be callable by ADB.
237 uid_t uid = AIBinder_getCallingUid();
Tomasz Wasilczykb8ebcca2023-10-09 19:54:20 +0000238 if (uid != 0 /* root */
239#ifdef AID_SHELL
240 && uid != AID_SHELL
241#endif
242 ) {
Ruchir Rastogicc7a7462020-01-31 14:29:15 -0800243 if (resultReceiver != nullptr) {
244 resultReceiver->send(-1);
245 }
246 return STATUS_PERMISSION_DENIED;
247 }
248
249 // Check that the file descriptors are valid.
250 if (in == STATUS_BAD_TYPE || out == STATUS_BAD_TYPE || err == STATUS_BAD_TYPE) {
251 if (resultReceiver != nullptr) {
252 resultReceiver->send(-1);
253 }
254 return STATUS_BAD_VALUE;
255 }
256
257 binder_status_t status = getClass()->handleShellCommand(
258 this, in, out, err, utf8Pointers.data(), utf8Pointers.size());
259 if (resultReceiver != nullptr) {
260 resultReceiver->send(status);
261 }
262 return status;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700263 } else {
264 return BBinder::onTransact(code, data, reply, flags);
265 }
266}
267
Steven Moreland71cddc32018-08-30 23:39:22 -0700268ABpBinder::ABpBinder(const ::android::sp<::android::IBinder>& binder)
Steven Moreland3acd4902022-07-26 18:29:58 +0000269 : AIBinder(nullptr /*clazz*/), mRemote(binder) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700270 LOG_ALWAYS_FATAL_IF(binder == nullptr, "binder == nullptr");
Steven Moreland2e87adc2018-08-20 19:47:00 -0700271}
272ABpBinder::~ABpBinder() {}
273
Steven Moreland94968952018-09-05 14:42:59 -0700274sp<AIBinder> ABpBinder::lookupOrCreateFromBinder(const ::android::sp<::android::IBinder>& binder) {
Steven Moreland71cddc32018-08-30 23:39:22 -0700275 if (binder == nullptr) {
276 return nullptr;
277 }
278 if (ABBinderTag::has(binder)) {
279 return static_cast<ABBinder*>(binder.get());
280 }
Steven Moreland94968952018-09-05 14:42:59 -0700281
Steven Moreland91a96b62021-06-30 23:07:13 +0000282 // The following code ensures that for a given binder object (remote or local), if it is not an
283 // ABBinder then at most one ABpBinder object exists in a given process representing it.
284
Steven Moreland1f1bed62021-06-25 21:50:50 +0000285 auto* value = static_cast<ABpBinderTag::Value*>(binder->findObject(ABpBinderTag::kId));
Steven Moreland94968952018-09-05 14:42:59 -0700286 if (value == nullptr) {
287 value = new ABpBinderTag::Value;
Steven Moreland1f1bed62021-06-25 21:50:50 +0000288 auto oldValue = static_cast<ABpBinderTag::Value*>(
289 binder->attachObject(ABpBinderTag::kId, static_cast<void*>(value),
290 nullptr /*cookie*/, ABpBinderTag::clean));
291
292 // allocated by another thread
293 if (oldValue) {
294 delete value;
295 value = oldValue;
296 }
Steven Moreland94968952018-09-05 14:42:59 -0700297 }
298
Steven Moreland1f1bed62021-06-25 21:50:50 +0000299 sp<ABpBinder> ret;
300 binder->withLock([&]() {
301 ret = value->binder.promote();
302 if (ret == nullptr) {
303 ret = sp<ABpBinder>::make(binder);
304 value->binder = ret;
305 }
306 });
Steven Moreland94968952018-09-05 14:42:59 -0700307
308 return ret;
Steven Moreland71cddc32018-08-30 23:39:22 -0700309}
310
Steven Moreland2e87adc2018-08-20 19:47:00 -0700311struct AIBinder_Weak {
312 wp<AIBinder> binder;
313};
314AIBinder_Weak* AIBinder_Weak_new(AIBinder* binder) {
Steven Moreland5ccb70f2018-09-04 16:30:21 -0700315 if (binder == nullptr) {
316 return nullptr;
317 }
318
Steven Moreland2e87adc2018-08-20 19:47:00 -0700319 return new AIBinder_Weak{wp<AIBinder>(binder)};
320}
Steven Moreland9b80e282018-09-19 13:57:23 -0700321void AIBinder_Weak_delete(AIBinder_Weak* weakBinder) {
322 delete weakBinder;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700323}
324AIBinder* AIBinder_Weak_promote(AIBinder_Weak* weakBinder) {
Steven Moreland5ccb70f2018-09-04 16:30:21 -0700325 if (weakBinder == nullptr) {
326 return nullptr;
327 }
328
Steven Moreland2e87adc2018-08-20 19:47:00 -0700329 sp<AIBinder> binder = weakBinder->binder.promote();
330 AIBinder_incStrong(binder.get());
331 return binder.get();
332}
333
Steven Morelandc70e0122021-01-08 02:44:53 +0000334AIBinder_Weak* AIBinder_Weak_clone(const AIBinder_Weak* weak) {
335 if (weak == nullptr) {
336 return nullptr;
337 }
338
339 return new AIBinder_Weak{weak->binder};
340}
341
342bool AIBinder_lt(const AIBinder* lhs, const AIBinder* rhs) {
343 if (lhs == nullptr || rhs == nullptr) return lhs < rhs;
344
345 return const_cast<AIBinder*>(lhs)->getBinder() < const_cast<AIBinder*>(rhs)->getBinder();
346}
347
348bool AIBinder_Weak_lt(const AIBinder_Weak* lhs, const AIBinder_Weak* rhs) {
349 if (lhs == nullptr || rhs == nullptr) return lhs < rhs;
350
351 return lhs->binder < rhs->binder;
352}
353
Steven Morelandfaf25a42022-12-21 02:21:36 +0000354// WARNING: When multiple classes exist with the same interface descriptor in different
355// linkernamespaces, the first one to be associated with mClazz becomes the canonical one
356// and the only requirement on this is that the interface descriptors match. If this
357// is an ABpBinder, no other state can be referenced from mClazz.
Steven Moreland2e87adc2018-08-20 19:47:00 -0700358AIBinder_Class::AIBinder_Class(const char* interfaceDescriptor, AIBinder_Class_onCreate onCreate,
359 AIBinder_Class_onDestroy onDestroy,
360 AIBinder_Class_onTransact onTransact)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700361 : onCreate(onCreate),
362 onDestroy(onDestroy),
363 onTransact(onTransact),
Stephen Crane8fde87f2020-11-17 15:06:11 -0800364 mInterfaceDescriptor(interfaceDescriptor),
365 mWideInterfaceDescriptor(interfaceDescriptor) {}
Steven Moreland2e87adc2018-08-20 19:47:00 -0700366
367AIBinder_Class* AIBinder_Class_define(const char* interfaceDescriptor,
368 AIBinder_Class_onCreate onCreate,
369 AIBinder_Class_onDestroy onDestroy,
370 AIBinder_Class_onTransact onTransact) {
371 if (interfaceDescriptor == nullptr || onCreate == nullptr || onDestroy == nullptr ||
372 onTransact == nullptr) {
373 return nullptr;
374 }
375
376 return new AIBinder_Class(interfaceDescriptor, onCreate, onDestroy, onTransact);
377}
378
Steven Morelanda194c452019-03-04 16:47:07 -0800379void AIBinder_Class_setOnDump(AIBinder_Class* clazz, AIBinder_onDump onDump) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700380 LOG_ALWAYS_FATAL_IF(clazz == nullptr, "setOnDump requires non-null clazz");
Steven Morelanda194c452019-03-04 16:47:07 -0800381
382 // this is required to be called before instances are instantiated
383 clazz->onDump = onDump;
384}
385
Steven Morelandf3ed8062021-08-27 14:31:14 -0700386void AIBinder_Class_disableInterfaceTokenHeader(AIBinder_Class* clazz) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700387 LOG_ALWAYS_FATAL_IF(clazz == nullptr, "disableInterfaceTokenHeader requires non-null clazz");
Steven Morelandf3ed8062021-08-27 14:31:14 -0700388
389 clazz->writeHeader = false;
390}
391
Ruchir Rastogicc7a7462020-01-31 14:29:15 -0800392void AIBinder_Class_setHandleShellCommand(AIBinder_Class* clazz,
393 AIBinder_handleShellCommand handleShellCommand) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700394 LOG_ALWAYS_FATAL_IF(clazz == nullptr, "setHandleShellCommand requires non-null clazz");
Ruchir Rastogicc7a7462020-01-31 14:29:15 -0800395
396 clazz->handleShellCommand = handleShellCommand;
397}
398
Stephen Crane8fde87f2020-11-17 15:06:11 -0800399const char* AIBinder_Class_getDescriptor(const AIBinder_Class* clazz) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700400 LOG_ALWAYS_FATAL_IF(clazz == nullptr, "getDescriptor requires non-null clazz");
Stephen Crane8fde87f2020-11-17 15:06:11 -0800401
402 return clazz->getInterfaceDescriptorUtf8();
403}
404
Alice Ryhlea9d9d22021-08-27 07:51:30 +0000405AIBinder_DeathRecipient::TransferDeathRecipient::~TransferDeathRecipient() {
406 if (mOnUnlinked != nullptr) {
407 mOnUnlinked(mCookie);
408 }
409}
410
Steven Moreland901c7452018-09-04 16:17:28 -0700411void AIBinder_DeathRecipient::TransferDeathRecipient::binderDied(const wp<IBinder>& who) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700412 LOG_ALWAYS_FATAL_IF(who != mWho, "%p (%p) vs %p (%p)", who.unsafe_get(), who.get_refs(),
413 mWho.unsafe_get(), mWho.get_refs());
Steven Moreland901c7452018-09-04 16:17:28 -0700414
415 mOnDied(mCookie);
Steven Moreland64127ca2019-03-13 09:25:44 -0700416
417 sp<AIBinder_DeathRecipient> recipient = mParentRecipient.promote();
418 sp<IBinder> strongWho = who.promote();
419
420 // otherwise this will be cleaned up later with pruneDeadTransferEntriesLocked
421 if (recipient != nullptr && strongWho != nullptr) {
422 status_t result = recipient->unlinkToDeath(strongWho, mCookie);
423 if (result != ::android::DEAD_OBJECT) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700424 ALOGW("Unlinking to dead binder resulted in: %d", result);
Steven Moreland64127ca2019-03-13 09:25:44 -0700425 }
426 }
427
Steven Moreland901c7452018-09-04 16:17:28 -0700428 mWho = nullptr;
429}
430
431AIBinder_DeathRecipient::AIBinder_DeathRecipient(AIBinder_DeathRecipient_onBinderDied onDied)
Alice Ryhlea9d9d22021-08-27 07:51:30 +0000432 : mOnDied(onDied), mOnUnlinked(nullptr) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700433 LOG_ALWAYS_FATAL_IF(onDied == nullptr, "onDied == nullptr");
Steven Moreland901c7452018-09-04 16:17:28 -0700434}
435
Steven Moreland64127ca2019-03-13 09:25:44 -0700436void AIBinder_DeathRecipient::pruneDeadTransferEntriesLocked() {
437 mDeathRecipients.erase(std::remove_if(mDeathRecipients.begin(), mDeathRecipients.end(),
438 [](const sp<TransferDeathRecipient>& tdr) {
439 return tdr->getWho() == nullptr;
440 }),
441 mDeathRecipients.end());
442}
443
Jiyong Park4f97a8c2020-11-16 18:11:14 +0900444binder_status_t AIBinder_DeathRecipient::linkToDeath(const sp<IBinder>& binder, void* cookie) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700445 LOG_ALWAYS_FATAL_IF(binder == nullptr, "binder == nullptr");
Steven Moreland901c7452018-09-04 16:17:28 -0700446
447 std::lock_guard<std::mutex> l(mDeathRecipientsMutex);
448
449 sp<TransferDeathRecipient> recipient =
Alice Ryhlea9d9d22021-08-27 07:51:30 +0000450 new TransferDeathRecipient(binder, cookie, this, mOnDied, mOnUnlinked);
Steven Moreland901c7452018-09-04 16:17:28 -0700451
Steven Moreland64127ca2019-03-13 09:25:44 -0700452 status_t status = binder->linkToDeath(recipient, cookie, 0 /*flags*/);
Steven Moreland5d62e442018-09-13 15:01:02 -0700453 if (status != STATUS_OK) {
Alice Ryhlea9d9d22021-08-27 07:51:30 +0000454 // When we failed to link, the destructor of TransferDeathRecipient runs here, which
455 // ensures that mOnUnlinked is called before we return with an error from this method.
Steven Moreland5d62e442018-09-13 15:01:02 -0700456 return PruneStatusT(status);
Steven Moreland901c7452018-09-04 16:17:28 -0700457 }
458
459 mDeathRecipients.push_back(recipient);
Steven Moreland64127ca2019-03-13 09:25:44 -0700460
461 pruneDeadTransferEntriesLocked();
Steven Moreland5d62e442018-09-13 15:01:02 -0700462 return STATUS_OK;
Steven Moreland901c7452018-09-04 16:17:28 -0700463}
464
Jiyong Park4f97a8c2020-11-16 18:11:14 +0900465binder_status_t AIBinder_DeathRecipient::unlinkToDeath(const sp<IBinder>& binder, void* cookie) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700466 LOG_ALWAYS_FATAL_IF(binder == nullptr, "binder == nullptr");
Steven Moreland901c7452018-09-04 16:17:28 -0700467
468 std::lock_guard<std::mutex> l(mDeathRecipientsMutex);
469
470 for (auto it = mDeathRecipients.rbegin(); it != mDeathRecipients.rend(); ++it) {
471 sp<TransferDeathRecipient> recipient = *it;
472
Steven Moreland64127ca2019-03-13 09:25:44 -0700473 if (recipient->getCookie() == cookie && recipient->getWho() == binder) {
Steven Moreland901c7452018-09-04 16:17:28 -0700474 mDeathRecipients.erase(it.base() - 1);
475
Steven Moreland64127ca2019-03-13 09:25:44 -0700476 status_t status = binder->unlinkToDeath(recipient, cookie, 0 /*flags*/);
Steven Moreland5d62e442018-09-13 15:01:02 -0700477 if (status != ::android::OK) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700478 ALOGE("%s: removed reference to death recipient but unlink failed: %s", __func__,
479 statusToString(status).c_str());
Steven Moreland901c7452018-09-04 16:17:28 -0700480 }
Steven Moreland5d62e442018-09-13 15:01:02 -0700481 return PruneStatusT(status);
Steven Moreland901c7452018-09-04 16:17:28 -0700482 }
483 }
484
Steven Moreland5d62e442018-09-13 15:01:02 -0700485 return STATUS_NAME_NOT_FOUND;
Steven Moreland901c7452018-09-04 16:17:28 -0700486}
487
Alice Ryhlea9d9d22021-08-27 07:51:30 +0000488void AIBinder_DeathRecipient::setOnUnlinked(AIBinder_DeathRecipient_onBinderUnlinked onUnlinked) {
489 mOnUnlinked = onUnlinked;
490}
491
Steven Moreland901c7452018-09-04 16:17:28 -0700492// start of C-API methods
493
Steven Moreland2e87adc2018-08-20 19:47:00 -0700494AIBinder* AIBinder_new(const AIBinder_Class* clazz, void* args) {
495 if (clazz == nullptr) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700496 ALOGE("%s: Must provide class to construct local binder.", __func__);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700497 return nullptr;
498 }
499
500 void* userData = clazz->onCreate(args);
501
Steven Moreland71cddc32018-08-30 23:39:22 -0700502 sp<AIBinder> ret = new ABBinder(clazz, userData);
503 ABBinderTag::attach(ret->getBinder());
504
505 AIBinder_incStrong(ret.get());
506 return ret.get();
Steven Moreland2e87adc2018-08-20 19:47:00 -0700507}
508
Steven Moreland5ea54da2018-09-04 13:29:55 -0700509bool AIBinder_isRemote(const AIBinder* binder) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700510 if (binder == nullptr) {
Steven Moreland56f752a2018-11-05 17:23:37 -0800511 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700512 }
513
514 return binder->isRemote();
515}
516
Steven Moreland65867d72018-09-04 14:22:26 -0700517bool AIBinder_isAlive(const AIBinder* binder) {
518 if (binder == nullptr) {
519 return false;
520 }
521
522 return const_cast<AIBinder*>(binder)->getBinder()->isBinderAlive();
523}
524
525binder_status_t AIBinder_ping(AIBinder* binder) {
526 if (binder == nullptr) {
Steven Moreland5d62e442018-09-13 15:01:02 -0700527 return STATUS_UNEXPECTED_NULL;
Steven Moreland65867d72018-09-04 14:22:26 -0700528 }
529
Steven Moreland5d62e442018-09-13 15:01:02 -0700530 return PruneStatusT(binder->getBinder()->pingBinder());
Steven Moreland65867d72018-09-04 14:22:26 -0700531}
532
Steven Morelanda194c452019-03-04 16:47:07 -0800533binder_status_t AIBinder_dump(AIBinder* binder, int fd, const char** args, uint32_t numArgs) {
534 if (binder == nullptr) {
535 return STATUS_UNEXPECTED_NULL;
536 }
537
538 ABBinder* bBinder = binder->asABBinder();
539 if (bBinder != nullptr) {
540 AIBinder_onDump onDump = binder->getClass()->onDump;
541 if (onDump == nullptr) {
542 return STATUS_OK;
543 }
544 return PruneStatusT(onDump(bBinder, fd, args, numArgs));
545 }
546
547 ::android::Vector<String16> utf16Args;
548 utf16Args.setCapacity(numArgs);
549 for (uint32_t i = 0; i < numArgs; i++) {
550 utf16Args.push(String16(String8(args[i])));
551 }
552
553 status_t status = binder->getBinder()->dump(fd, utf16Args);
554 return PruneStatusT(status);
555}
556
Steven Moreland901c7452018-09-04 16:17:28 -0700557binder_status_t AIBinder_linkToDeath(AIBinder* binder, AIBinder_DeathRecipient* recipient,
558 void* cookie) {
559 if (binder == nullptr || recipient == nullptr) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700560 ALOGE("%s: Must provide binder (%p) and recipient (%p)", __func__, binder, recipient);
Steven Moreland5d62e442018-09-13 15:01:02 -0700561 return STATUS_UNEXPECTED_NULL;
Steven Moreland901c7452018-09-04 16:17:28 -0700562 }
563
Steven Moreland5d62e442018-09-13 15:01:02 -0700564 // returns binder_status_t
Steven Moreland64127ca2019-03-13 09:25:44 -0700565 return recipient->linkToDeath(binder->getBinder(), cookie);
Steven Moreland901c7452018-09-04 16:17:28 -0700566}
567
568binder_status_t AIBinder_unlinkToDeath(AIBinder* binder, AIBinder_DeathRecipient* recipient,
569 void* cookie) {
570 if (binder == nullptr || recipient == nullptr) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700571 ALOGE("%s: Must provide binder (%p) and recipient (%p)", __func__, binder, recipient);
Steven Moreland5d62e442018-09-13 15:01:02 -0700572 return STATUS_UNEXPECTED_NULL;
Steven Moreland901c7452018-09-04 16:17:28 -0700573 }
574
Steven Moreland5d62e442018-09-13 15:01:02 -0700575 // returns binder_status_t
Steven Moreland64127ca2019-03-13 09:25:44 -0700576 return recipient->unlinkToDeath(binder->getBinder(), cookie);
Steven Moreland901c7452018-09-04 16:17:28 -0700577}
578
Steven Morelandf3034b02018-11-12 17:37:46 -0800579uid_t AIBinder_getCallingUid() {
580 return ::android::IPCThreadState::self()->getCallingUid();
581}
582
583pid_t AIBinder_getCallingPid() {
584 return ::android::IPCThreadState::self()->getCallingPid();
585}
586
Alice Ryhl59aeae82021-08-25 10:21:26 +0000587bool AIBinder_isHandlingTransaction() {
588 return ::android::IPCThreadState::self()->getServingStackPointer() != nullptr;
589}
590
Steven Moreland2e87adc2018-08-20 19:47:00 -0700591void AIBinder_incStrong(AIBinder* binder) {
592 if (binder == nullptr) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700593 return;
594 }
595
596 binder->incStrong(nullptr);
597}
598void AIBinder_decStrong(AIBinder* binder) {
599 if (binder == nullptr) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700600 ALOGE("%s: on null binder", __func__);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700601 return;
602 }
603
604 binder->decStrong(nullptr);
605}
606int32_t AIBinder_debugGetRefCount(AIBinder* binder) {
607 if (binder == nullptr) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700608 ALOGE("%s: on null binder", __func__);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700609 return -1;
610 }
611
612 return binder->getStrongCount();
613}
614
Steven Moreland71cddc32018-08-30 23:39:22 -0700615bool AIBinder_associateClass(AIBinder* binder, const AIBinder_Class* clazz) {
616 if (binder == nullptr) {
617 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700618 }
619
Steven Moreland71cddc32018-08-30 23:39:22 -0700620 return binder->associateClass(clazz);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700621}
622
623const AIBinder_Class* AIBinder_getClass(AIBinder* binder) {
624 if (binder == nullptr) {
625 return nullptr;
626 }
627
628 return binder->getClass();
629}
630
631void* AIBinder_getUserData(AIBinder* binder) {
632 if (binder == nullptr) {
633 return nullptr;
634 }
635
636 ABBinder* bBinder = binder->asABBinder();
637 if (bBinder == nullptr) {
638 return nullptr;
639 }
640
641 return bBinder->getUserData();
642}
643
644binder_status_t AIBinder_prepareTransaction(AIBinder* binder, AParcel** in) {
645 if (binder == nullptr || in == nullptr) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700646 ALOGE("%s: requires non-null parameters binder (%p) and in (%p).", __func__, binder, in);
Steven Moreland5d62e442018-09-13 15:01:02 -0700647 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700648 }
649 const AIBinder_Class* clazz = binder->getClass();
650 if (clazz == nullptr) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700651 ALOGE("%s: Class must be defined for a remote binder transaction. See "
652 "AIBinder_associateClass.",
653 __func__);
Steven Moreland5d62e442018-09-13 15:01:02 -0700654 return STATUS_INVALID_OPERATION;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700655 }
656
657 *in = new AParcel(binder);
Steven Moreland1fda67b2021-04-02 18:35:50 +0000658 (*in)->get()->markForBinder(binder->getBinder());
659
Steven Morelandf3ed8062021-08-27 14:31:14 -0700660 status_t status = android::OK;
Steven Morelandfaf25a42022-12-21 02:21:36 +0000661
662 // note - this is the only read of a value in clazz, and it comes with a warning
663 // on the API itself. Do not copy this design. Instead, attach data in a new
664 // version of the prepareTransaction function.
Steven Morelandf3ed8062021-08-27 14:31:14 -0700665 if (clazz->writeHeader) {
666 status = (*in)->get()->writeInterfaceToken(clazz->getInterfaceDescriptor());
667 }
Steven Moreland5d62e442018-09-13 15:01:02 -0700668 binder_status_t ret = PruneStatusT(status);
669
670 if (ret != STATUS_OK) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700671 delete *in;
672 *in = nullptr;
673 }
674
Steven Moreland5d62e442018-09-13 15:01:02 -0700675 return ret;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700676}
677
Steven Moreland9b80e282018-09-19 13:57:23 -0700678static void DestroyParcel(AParcel** parcel) {
679 delete *parcel;
680 *parcel = nullptr;
681}
682
Steven Moreland2e87adc2018-08-20 19:47:00 -0700683binder_status_t AIBinder_transact(AIBinder* binder, transaction_code_t code, AParcel** in,
684 AParcel** out, binder_flags_t flags) {
685 if (in == nullptr) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700686 ALOGE("%s: requires non-null in parameter", __func__);
Steven Moreland5d62e442018-09-13 15:01:02 -0700687 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700688 }
689
Steven Morelandcaa776c2018-09-04 13:48:11 -0700690 using AutoParcelDestroyer = std::unique_ptr<AParcel*, void (*)(AParcel**)>;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700691 // This object is the input to the transaction. This function takes ownership of it and deletes
692 // it.
Steven Moreland9b80e282018-09-19 13:57:23 -0700693 AutoParcelDestroyer forIn(in, DestroyParcel);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700694
695 if (!isUserCommand(code)) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700696 ALOGE("%s: Only user-defined transactions can be made from the NDK, but requested: %d",
697 __func__, code);
Steven Moreland5d62e442018-09-13 15:01:02 -0700698 return STATUS_UNKNOWN_TRANSACTION;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700699 }
700
Steven Morelandf183fdd2020-10-27 00:12:12 +0000701 constexpr binder_flags_t kAllFlags = FLAG_PRIVATE_VENDOR | FLAG_ONEWAY | FLAG_CLEAR_BUF;
Steven Moreland46b5fea2019-10-15 11:22:18 -0700702 if ((flags & ~kAllFlags) != 0) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700703 ALOGE("%s: Unrecognized flags sent: %d", __func__, flags);
Steven Moreland5d62e442018-09-13 15:01:02 -0700704 return STATUS_BAD_VALUE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700705 }
706
707 if (binder == nullptr || *in == nullptr || out == nullptr) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700708 ALOGE("%s: requires non-null parameters binder (%p), in (%p), and out (%p).", __func__,
709 binder, in, out);
Steven Moreland5d62e442018-09-13 15:01:02 -0700710 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700711 }
712
713 if ((*in)->getBinder() != binder) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700714 ALOGE("%s: parcel is associated with binder object %p but called with %p", __func__, binder,
715 (*in)->getBinder());
Steven Moreland5d62e442018-09-13 15:01:02 -0700716 return STATUS_BAD_VALUE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700717 }
718
719 *out = new AParcel(binder);
720
Steven Morelandf18615b2018-09-14 11:43:06 -0700721 status_t status = binder->getBinder()->transact(code, *(*in)->get(), (*out)->get(), flags);
Steven Moreland5d62e442018-09-13 15:01:02 -0700722 binder_status_t ret = PruneStatusT(status);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700723
Steven Moreland5d62e442018-09-13 15:01:02 -0700724 if (ret != STATUS_OK) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700725 delete *out;
726 *out = nullptr;
727 }
728
Steven Moreland5d62e442018-09-13 15:01:02 -0700729 return ret;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700730}
Steven Moreland901c7452018-09-04 16:17:28 -0700731
732AIBinder_DeathRecipient* AIBinder_DeathRecipient_new(
733 AIBinder_DeathRecipient_onBinderDied onBinderDied) {
734 if (onBinderDied == nullptr) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700735 ALOGE("%s: requires non-null onBinderDied parameter.", __func__);
Steven Moreland901c7452018-09-04 16:17:28 -0700736 return nullptr;
737 }
Steven Moreland64127ca2019-03-13 09:25:44 -0700738 auto ret = new AIBinder_DeathRecipient(onBinderDied);
739 ret->incStrong(nullptr);
740 return ret;
Steven Moreland901c7452018-09-04 16:17:28 -0700741}
742
Alice Ryhlea9d9d22021-08-27 07:51:30 +0000743void AIBinder_DeathRecipient_setOnUnlinked(AIBinder_DeathRecipient* recipient,
744 AIBinder_DeathRecipient_onBinderUnlinked onUnlinked) {
745 if (recipient == nullptr) {
746 return;
747 }
748
749 recipient->setOnUnlinked(onUnlinked);
750}
751
Steven Moreland9b80e282018-09-19 13:57:23 -0700752void AIBinder_DeathRecipient_delete(AIBinder_DeathRecipient* recipient) {
Steven Moreland64127ca2019-03-13 09:25:44 -0700753 if (recipient == nullptr) {
754 return;
755 }
756
757 recipient->decStrong(nullptr);
Steven Moreland901c7452018-09-04 16:17:28 -0700758}
Steven Morelandea14ef22019-08-20 10:50:08 -0700759
760binder_status_t AIBinder_getExtension(AIBinder* binder, AIBinder** outExt) {
761 if (binder == nullptr || outExt == nullptr) {
762 if (outExt != nullptr) {
763 *outExt = nullptr;
764 }
765 return STATUS_UNEXPECTED_NULL;
766 }
767
768 sp<IBinder> ext;
769 status_t res = binder->getBinder()->getExtension(&ext);
770
771 if (res != android::OK) {
772 *outExt = nullptr;
773 return PruneStatusT(res);
774 }
775
776 sp<AIBinder> ret = ABpBinder::lookupOrCreateFromBinder(ext);
777 if (ret != nullptr) ret->incStrong(binder);
778
779 *outExt = ret.get();
780 return STATUS_OK;
781}
782
783binder_status_t AIBinder_setExtension(AIBinder* binder, AIBinder* ext) {
784 if (binder == nullptr || ext == nullptr) {
785 return STATUS_UNEXPECTED_NULL;
786 }
787
788 ABBinder* rawBinder = binder->asABBinder();
789 if (rawBinder == nullptr) {
790 return STATUS_INVALID_OPERATION;
791 }
792
793 rawBinder->setExtension(ext->getBinder());
794 return STATUS_OK;
795}
Steven Moreland2f405f52020-07-08 22:24:29 +0000796
797// platform methods follow
798
799void AIBinder_setRequestingSid(AIBinder* binder, bool requestingSid) {
800 ABBinder* localBinder = binder->asABBinder();
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700801 LOG_ALWAYS_FATAL_IF(localBinder == nullptr,
802 "AIBinder_setRequestingSid must be called on a local binder");
Steven Moreland2f405f52020-07-08 22:24:29 +0000803
804 localBinder->setRequestingSid(requestingSid);
805}
806
807const char* AIBinder_getCallingSid() {
808 return ::android::IPCThreadState::self()->getCallingSid();
809}
Steven Morelandb2de9532020-05-29 21:44:41 +0000810
Ady Abraham96174ac2021-10-15 11:02:03 -0700811void AIBinder_setMinSchedulerPolicy(AIBinder* binder, int policy, int priority) {
812 binder->asABBinder()->setMinSchedulerPolicy(policy, priority);
813}
Steven Moreland454a26a2021-12-14 01:26:21 +0000814
815void AIBinder_setInheritRt(AIBinder* binder, bool inheritRt) {
816 ABBinder* localBinder = binder->asABBinder();
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700817 LOG_ALWAYS_FATAL_IF(localBinder == nullptr,
818 "AIBinder_setInheritRt must be called on a local binder");
Steven Moreland454a26a2021-12-14 01:26:21 +0000819
820 localBinder->setInheritRt(inheritRt);
821}