blob: af280d38a30576bb6dbb7df963afb645ee541ea8 [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});
Andrei Homescu40dbf252024-03-28 21:19:42 +000048void clean(const void* /*id*/, void* /*obj*/, void* /*cookie*/) {
49 /* do nothing */
50}
Steven Moreland71cddc32018-08-30 23:39:22 -070051
52static void attach(const sp<IBinder>& binder) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -070053 auto alreadyAttached = binder->attachObject(kId, kValue, nullptr /*cookie*/, clean);
54 LOG_ALWAYS_FATAL_IF(alreadyAttached != nullptr, "can only attach once");
Steven Moreland71cddc32018-08-30 23:39:22 -070055}
56static bool has(const sp<IBinder>& binder) {
57 return binder != nullptr && binder->findObject(kId) == kValue;
58}
59
Steven Moreland6cf66ac2018-11-02 18:14:54 -070060} // namespace ABBinderTag
Steven Moreland71cddc32018-08-30 23:39:22 -070061
Steven Moreland94968952018-09-05 14:42:59 -070062namespace ABpBinderTag {
63
Steven Moreland94968952018-09-05 14:42:59 -070064static const void* kId = "ABpBinder";
65struct Value {
66 wp<ABpBinder> binder;
67};
68void clean(const void* id, void* obj, void* cookie) {
Steven Moreland3acd4902022-07-26 18:29:58 +000069 // be weary of leaks!
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -070070 // ALOGI("Deleting an ABpBinder");
Steven Moreland3acd4902022-07-26 18:29:58 +000071
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -070072 LOG_ALWAYS_FATAL_IF(id != kId, "%p %p %p", id, obj, cookie);
Steven Moreland94968952018-09-05 14:42:59 -070073
74 delete static_cast<Value*>(obj);
Andrei Homescu40dbf252024-03-28 21:19:42 +000075}
Steven Moreland94968952018-09-05 14:42:59 -070076
Steven Moreland6cf66ac2018-11-02 18:14:54 -070077} // namespace ABpBinderTag
Steven Moreland94968952018-09-05 14:42:59 -070078
Steven Moreland2e87adc2018-08-20 19:47:00 -070079AIBinder::AIBinder(const AIBinder_Class* clazz) : mClazz(clazz) {}
80AIBinder::~AIBinder() {}
81
Steven Moreland94a76792022-12-21 01:20:06 +000082// b/175635923 libcxx causes "implicit-conversion" with a string with invalid char
83static std::string SanitizeString(const String16& str) {
84 std::string sanitized{String8(str)};
85 for (auto& c : sanitized) {
86 if (!isprint(c)) {
87 c = '?';
88 }
89 }
90 return sanitized;
91}
92
93bool AIBinder::associateClass(const AIBinder_Class* clazz) {
94 if (clazz == nullptr) return false;
95
96 // If mClazz is non-null, this must have been called and cached
97 // already. So, we can safely call this first. Due to the implementation
98 // of getInterfaceDescriptor (at time of writing), two simultaneous calls
99 // may lead to extra binder transactions, but this is expected to be
100 // exceedingly rare. Once we have a binder, when we get it again later,
101 // we won't make another binder transaction here.
102 const String16& descriptor = getBinder()->getInterfaceDescriptor();
103 const String16& newDescriptor = clazz->getInterfaceDescriptor();
104
Andrei Homescu9556bb12020-09-21 16:59:45 -0700105 std::lock_guard<std::mutex> lock(mClazzMutex);
Steven Moreland71cddc32018-08-30 23:39:22 -0700106 if (mClazz == clazz) return true;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700107
Steven Morelandfaf25a42022-12-21 02:21:36 +0000108 // If this is an ABpBinder, the first class object becomes the canonical one. The implication
109 // of this is that no API can require a proxy information to get information on how to behave.
110 // from the class itself - which should only store the interface descriptor. The functionality
111 // should be implemented by adding AIBinder_* APIs to set values on binders themselves, by
112 // setting things on AIBinder_Class which get transferred along with the binder, so that they
113 // can be read along with the BpBinder, or by modifying APIs directly (e.g. an option in
114 // onTransact).
115 //
116 // While this check is required to support linkernamespaces, one downside of it is that
117 // you may parcel code to communicate between things in the same process. However, comms
118 // between linkernamespaces like this already happen for cross-language calls like Java<->C++
119 // or Rust<->Java, and there are good stability guarantees here. This interacts with
120 // binder Stability checks exactly like any other in-process call. The stability is known
121 // to the IBinder object, so that it doesn't matter if a class object comes from
122 // a different stability level.
123 if (mClazz != nullptr && !asABpBinder()) {
Steven Moreland63872b52020-10-02 19:28:23 +0000124 const String16& currentDescriptor = mClazz->getInterfaceDescriptor();
Steven Moreland2e87adc2018-08-20 19:47:00 -0700125 if (newDescriptor == currentDescriptor) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700126 ALOGE("Class descriptors '%s' match during associateClass, but they are different class"
127 " objects (%p vs %p). Class descriptor collision?",
128 String8(currentDescriptor).c_str(), clazz, mClazz);
Steven Moreland71cddc32018-08-30 23:39:22 -0700129 } else {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700130 ALOGE("%s: Class cannot be associated on object which already has a class. "
131 "Trying to associate to '%s' but already set to '%s'.",
132 __func__, String8(newDescriptor).c_str(), String8(currentDescriptor).c_str());
Steven Moreland2e87adc2018-08-20 19:47:00 -0700133 }
134
Steven Moreland71cddc32018-08-30 23:39:22 -0700135 // always a failure because we know mClazz != clazz
136 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700137 }
138
Steven Moreland94a76792022-12-21 01:20:06 +0000139 // This will always be an O(n) comparison, but it's expected to be extremely rare.
140 // since it's an error condition. Do the comparison after we take the lock and
141 // check the pointer equality fast path. By always taking the lock, it's also
142 // more flake-proof. However, the check is not dependent on the lock.
Steven Moreland418914a2023-06-28 21:47:14 +0000143 if (descriptor != newDescriptor && !(asABpBinder() && asABpBinder()->isServiceFuzzing())) {
Steven Moreland640c4092020-07-08 00:19:58 +0000144 if (getBinder()->isBinderAlive()) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700145 ALOGE("%s: Expecting binder to have class '%s' but descriptor is actually '%s'.",
146 __func__, String8(newDescriptor).c_str(), SanitizeString(descriptor).c_str());
Steven Moreland640c4092020-07-08 00:19:58 +0000147 } else {
148 // b/155793159
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700149 ALOGE("%s: Cannot associate class '%s' to dead binder with cached descriptor '%s'.",
150 __func__, String8(newDescriptor).c_str(), SanitizeString(descriptor).c_str());
Steven Moreland640c4092020-07-08 00:19:58 +0000151 }
Steven Moreland71cddc32018-08-30 23:39:22 -0700152 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700153 }
154
Steven Morelandfaf25a42022-12-21 02:21:36 +0000155 // A local binder being set for the first time OR
156 // ignoring a proxy binder which is set multiple time, by considering the first
157 // associated class as the canonical one.
158 if (mClazz == nullptr) {
159 mClazz = clazz;
160 }
161
Steven Moreland94a76792022-12-21 01:20:06 +0000162 return true;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700163}
164
165ABBinder::ABBinder(const AIBinder_Class* clazz, void* userData)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700166 : AIBinder(clazz), BBinder(), mUserData(userData) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700167 LOG_ALWAYS_FATAL_IF(clazz == nullptr, "clazz == nullptr");
Steven Moreland2e87adc2018-08-20 19:47:00 -0700168}
169ABBinder::~ABBinder() {
170 getClass()->onDestroy(mUserData);
171}
172
173const String16& ABBinder::getInterfaceDescriptor() const {
174 return getClass()->getInterfaceDescriptor();
175}
176
Steven Morelanda194c452019-03-04 16:47:07 -0800177status_t ABBinder::dump(int fd, const ::android::Vector<String16>& args) {
178 AIBinder_onDump onDump = getClass()->onDump;
179
180 if (onDump == nullptr) {
181 return STATUS_OK;
182 }
183
184 // technically UINT32_MAX would be okay here, but INT32_MAX is expected since this may be
185 // null in Java
186 if (args.size() > INT32_MAX) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700187 ALOGE("ABBinder::dump received too many arguments: %zu", args.size());
Steven Morelanda194c452019-03-04 16:47:07 -0800188 return STATUS_BAD_VALUE;
189 }
190
191 std::vector<String8> utf8Args; // owns memory of utf8s
192 utf8Args.reserve(args.size());
193 std::vector<const char*> utf8Pointers; // what can be passed over NDK API
194 utf8Pointers.reserve(args.size());
195
196 for (size_t i = 0; i < args.size(); i++) {
197 utf8Args.push_back(String8(args[i]));
198 utf8Pointers.push_back(utf8Args[i].c_str());
199 }
200
201 return onDump(this, fd, utf8Pointers.data(), utf8Pointers.size());
202}
203
Steven Moreland5d62e442018-09-13 15:01:02 -0700204status_t ABBinder::onTransact(transaction_code_t code, const Parcel& data, Parcel* reply,
205 binder_flags_t flags) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700206 if (isUserCommand(code)) {
Steven Morelandf3ed8062021-08-27 14:31:14 -0700207 if (getClass()->writeHeader && !data.checkInterface(this)) {
Steven Moreland9d089af2018-09-18 08:58:09 -0700208 return STATUS_BAD_TYPE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700209 }
210
211 const AParcel in = AParcel::readOnly(this, &data);
212 AParcel out = AParcel(this, reply, false /*owns*/);
213
Steven Moreland5d62e442018-09-13 15:01:02 -0700214 binder_status_t status = getClass()->onTransact(this, code, &in, &out);
215 return PruneStatusT(status);
Steven Moreland88234072020-08-06 19:32:45 +0000216 } else if (code == SHELL_COMMAND_TRANSACTION && getClass()->handleShellCommand != nullptr) {
Andrei Homescu38eafb72024-03-22 22:38:08 +0000217 if constexpr (!android::kEnableKernelIpc) {
218 // Non-IPC builds do not have getCallingUid(),
219 // so we have no way of authenticating the caller
220 return STATUS_PERMISSION_DENIED;
221 }
222
Ruchir Rastogicc7a7462020-01-31 14:29:15 -0800223 int in = data.readFileDescriptor();
224 int out = data.readFileDescriptor();
225 int err = data.readFileDescriptor();
226
227 int argc = data.readInt32();
228 std::vector<String8> utf8Args; // owns memory of utf8s
229 std::vector<const char*> utf8Pointers; // what can be passed over NDK API
230 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
231 utf8Args.push_back(String8(data.readString16()));
232 utf8Pointers.push_back(utf8Args[i].c_str());
233 }
234
235 data.readStrongBinder(); // skip over the IShellCallback
236 sp<IResultReceiver> resultReceiver = IResultReceiver::asInterface(data.readStrongBinder());
237
238 // Shell commands should only be callable by ADB.
239 uid_t uid = AIBinder_getCallingUid();
Tomasz Wasilczykb8ebcca2023-10-09 19:54:20 +0000240 if (uid != 0 /* root */
241#ifdef AID_SHELL
242 && uid != AID_SHELL
243#endif
244 ) {
Ruchir Rastogicc7a7462020-01-31 14:29:15 -0800245 if (resultReceiver != nullptr) {
246 resultReceiver->send(-1);
247 }
248 return STATUS_PERMISSION_DENIED;
249 }
250
251 // Check that the file descriptors are valid.
252 if (in == STATUS_BAD_TYPE || out == STATUS_BAD_TYPE || err == STATUS_BAD_TYPE) {
253 if (resultReceiver != nullptr) {
254 resultReceiver->send(-1);
255 }
256 return STATUS_BAD_VALUE;
257 }
258
259 binder_status_t status = getClass()->handleShellCommand(
260 this, in, out, err, utf8Pointers.data(), utf8Pointers.size());
261 if (resultReceiver != nullptr) {
262 resultReceiver->send(status);
263 }
264 return status;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700265 } else {
266 return BBinder::onTransact(code, data, reply, flags);
267 }
268}
269
Devin Moore9f9d0e12024-02-29 22:07:12 +0000270void ABBinder::addDeathRecipient(const ::android::sp<AIBinder_DeathRecipient>& /* recipient */,
271 void* /* cookie */) {
272 LOG_ALWAYS_FATAL("Should not reach this. Can't linkToDeath local binders.");
273}
274
Steven Moreland71cddc32018-08-30 23:39:22 -0700275ABpBinder::ABpBinder(const ::android::sp<::android::IBinder>& binder)
Steven Moreland3acd4902022-07-26 18:29:58 +0000276 : AIBinder(nullptr /*clazz*/), mRemote(binder) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700277 LOG_ALWAYS_FATAL_IF(binder == nullptr, "binder == nullptr");
Steven Moreland2e87adc2018-08-20 19:47:00 -0700278}
Devin Moore9f9d0e12024-02-29 22:07:12 +0000279
280ABpBinder::~ABpBinder() {
281 for (auto& recip : mDeathRecipients) {
282 sp<AIBinder_DeathRecipient> strongRecip = recip.recipient.promote();
283 if (strongRecip) {
284 strongRecip->pruneThisTransferEntry(getBinder(), recip.cookie);
285 }
286 }
287}
Steven Moreland2e87adc2018-08-20 19:47:00 -0700288
Steven Moreland94968952018-09-05 14:42:59 -0700289sp<AIBinder> ABpBinder::lookupOrCreateFromBinder(const ::android::sp<::android::IBinder>& binder) {
Steven Moreland71cddc32018-08-30 23:39:22 -0700290 if (binder == nullptr) {
291 return nullptr;
292 }
293 if (ABBinderTag::has(binder)) {
294 return static_cast<ABBinder*>(binder.get());
295 }
Steven Moreland94968952018-09-05 14:42:59 -0700296
Steven Moreland91a96b62021-06-30 23:07:13 +0000297 // The following code ensures that for a given binder object (remote or local), if it is not an
298 // ABBinder then at most one ABpBinder object exists in a given process representing it.
299
Steven Moreland1f1bed62021-06-25 21:50:50 +0000300 auto* value = static_cast<ABpBinderTag::Value*>(binder->findObject(ABpBinderTag::kId));
Steven Moreland94968952018-09-05 14:42:59 -0700301 if (value == nullptr) {
302 value = new ABpBinderTag::Value;
Steven Moreland1f1bed62021-06-25 21:50:50 +0000303 auto oldValue = static_cast<ABpBinderTag::Value*>(
304 binder->attachObject(ABpBinderTag::kId, static_cast<void*>(value),
305 nullptr /*cookie*/, ABpBinderTag::clean));
306
307 // allocated by another thread
308 if (oldValue) {
309 delete value;
310 value = oldValue;
311 }
Steven Moreland94968952018-09-05 14:42:59 -0700312 }
313
Steven Moreland1f1bed62021-06-25 21:50:50 +0000314 sp<ABpBinder> ret;
315 binder->withLock([&]() {
316 ret = value->binder.promote();
317 if (ret == nullptr) {
318 ret = sp<ABpBinder>::make(binder);
319 value->binder = ret;
320 }
321 });
Steven Moreland94968952018-09-05 14:42:59 -0700322
323 return ret;
Steven Moreland71cddc32018-08-30 23:39:22 -0700324}
325
Devin Moore9f9d0e12024-02-29 22:07:12 +0000326void ABpBinder::addDeathRecipient(const ::android::sp<AIBinder_DeathRecipient>& recipient,
327 void* cookie) {
328 std::lock_guard<std::mutex> l(mDeathRecipientsMutex);
329 mDeathRecipients.emplace_back(recipient, cookie);
330}
331
Steven Moreland2e87adc2018-08-20 19:47:00 -0700332struct AIBinder_Weak {
333 wp<AIBinder> binder;
334};
335AIBinder_Weak* AIBinder_Weak_new(AIBinder* binder) {
Steven Moreland5ccb70f2018-09-04 16:30:21 -0700336 if (binder == nullptr) {
337 return nullptr;
338 }
339
Steven Moreland2e87adc2018-08-20 19:47:00 -0700340 return new AIBinder_Weak{wp<AIBinder>(binder)};
341}
Steven Moreland9b80e282018-09-19 13:57:23 -0700342void AIBinder_Weak_delete(AIBinder_Weak* weakBinder) {
343 delete weakBinder;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700344}
345AIBinder* AIBinder_Weak_promote(AIBinder_Weak* weakBinder) {
Steven Moreland5ccb70f2018-09-04 16:30:21 -0700346 if (weakBinder == nullptr) {
347 return nullptr;
348 }
349
Steven Moreland2e87adc2018-08-20 19:47:00 -0700350 sp<AIBinder> binder = weakBinder->binder.promote();
351 AIBinder_incStrong(binder.get());
352 return binder.get();
353}
354
Steven Morelandc70e0122021-01-08 02:44:53 +0000355AIBinder_Weak* AIBinder_Weak_clone(const AIBinder_Weak* weak) {
356 if (weak == nullptr) {
357 return nullptr;
358 }
359
360 return new AIBinder_Weak{weak->binder};
361}
362
363bool AIBinder_lt(const AIBinder* lhs, const AIBinder* rhs) {
364 if (lhs == nullptr || rhs == nullptr) return lhs < rhs;
365
366 return const_cast<AIBinder*>(lhs)->getBinder() < const_cast<AIBinder*>(rhs)->getBinder();
367}
368
369bool AIBinder_Weak_lt(const AIBinder_Weak* lhs, const AIBinder_Weak* rhs) {
370 if (lhs == nullptr || rhs == nullptr) return lhs < rhs;
371
372 return lhs->binder < rhs->binder;
373}
374
Steven Morelandfaf25a42022-12-21 02:21:36 +0000375// WARNING: When multiple classes exist with the same interface descriptor in different
376// linkernamespaces, the first one to be associated with mClazz becomes the canonical one
377// and the only requirement on this is that the interface descriptors match. If this
378// is an ABpBinder, no other state can be referenced from mClazz.
Steven Moreland2e87adc2018-08-20 19:47:00 -0700379AIBinder_Class::AIBinder_Class(const char* interfaceDescriptor, AIBinder_Class_onCreate onCreate,
380 AIBinder_Class_onDestroy onDestroy,
381 AIBinder_Class_onTransact onTransact)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700382 : onCreate(onCreate),
383 onDestroy(onDestroy),
384 onTransact(onTransact),
Stephen Crane8fde87f2020-11-17 15:06:11 -0800385 mInterfaceDescriptor(interfaceDescriptor),
386 mWideInterfaceDescriptor(interfaceDescriptor) {}
Steven Moreland2e87adc2018-08-20 19:47:00 -0700387
388AIBinder_Class* AIBinder_Class_define(const char* interfaceDescriptor,
389 AIBinder_Class_onCreate onCreate,
390 AIBinder_Class_onDestroy onDestroy,
391 AIBinder_Class_onTransact onTransact) {
392 if (interfaceDescriptor == nullptr || onCreate == nullptr || onDestroy == nullptr ||
393 onTransact == nullptr) {
394 return nullptr;
395 }
396
397 return new AIBinder_Class(interfaceDescriptor, onCreate, onDestroy, onTransact);
398}
399
Steven Morelanda194c452019-03-04 16:47:07 -0800400void AIBinder_Class_setOnDump(AIBinder_Class* clazz, AIBinder_onDump onDump) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700401 LOG_ALWAYS_FATAL_IF(clazz == nullptr, "setOnDump requires non-null clazz");
Steven Morelanda194c452019-03-04 16:47:07 -0800402
403 // this is required to be called before instances are instantiated
404 clazz->onDump = onDump;
405}
406
Steven Morelandf3ed8062021-08-27 14:31:14 -0700407void AIBinder_Class_disableInterfaceTokenHeader(AIBinder_Class* clazz) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700408 LOG_ALWAYS_FATAL_IF(clazz == nullptr, "disableInterfaceTokenHeader requires non-null clazz");
Steven Morelandf3ed8062021-08-27 14:31:14 -0700409
410 clazz->writeHeader = false;
411}
412
Ruchir Rastogicc7a7462020-01-31 14:29:15 -0800413void AIBinder_Class_setHandleShellCommand(AIBinder_Class* clazz,
414 AIBinder_handleShellCommand handleShellCommand) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700415 LOG_ALWAYS_FATAL_IF(clazz == nullptr, "setHandleShellCommand requires non-null clazz");
Ruchir Rastogicc7a7462020-01-31 14:29:15 -0800416
417 clazz->handleShellCommand = handleShellCommand;
418}
419
Stephen Crane8fde87f2020-11-17 15:06:11 -0800420const char* AIBinder_Class_getDescriptor(const AIBinder_Class* clazz) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700421 LOG_ALWAYS_FATAL_IF(clazz == nullptr, "getDescriptor requires non-null clazz");
Stephen Crane8fde87f2020-11-17 15:06:11 -0800422
423 return clazz->getInterfaceDescriptorUtf8();
424}
425
Alice Ryhlea9d9d22021-08-27 07:51:30 +0000426AIBinder_DeathRecipient::TransferDeathRecipient::~TransferDeathRecipient() {
427 if (mOnUnlinked != nullptr) {
428 mOnUnlinked(mCookie);
429 }
430}
431
Steven Moreland901c7452018-09-04 16:17:28 -0700432void AIBinder_DeathRecipient::TransferDeathRecipient::binderDied(const wp<IBinder>& who) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700433 LOG_ALWAYS_FATAL_IF(who != mWho, "%p (%p) vs %p (%p)", who.unsafe_get(), who.get_refs(),
434 mWho.unsafe_get(), mWho.get_refs());
Steven Moreland901c7452018-09-04 16:17:28 -0700435
436 mOnDied(mCookie);
Steven Moreland64127ca2019-03-13 09:25:44 -0700437
438 sp<AIBinder_DeathRecipient> recipient = mParentRecipient.promote();
439 sp<IBinder> strongWho = who.promote();
440
441 // otherwise this will be cleaned up later with pruneDeadTransferEntriesLocked
442 if (recipient != nullptr && strongWho != nullptr) {
443 status_t result = recipient->unlinkToDeath(strongWho, mCookie);
444 if (result != ::android::DEAD_OBJECT) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700445 ALOGW("Unlinking to dead binder resulted in: %d", result);
Steven Moreland64127ca2019-03-13 09:25:44 -0700446 }
447 }
448
Steven Moreland901c7452018-09-04 16:17:28 -0700449 mWho = nullptr;
450}
451
452AIBinder_DeathRecipient::AIBinder_DeathRecipient(AIBinder_DeathRecipient_onBinderDied onDied)
Alice Ryhlea9d9d22021-08-27 07:51:30 +0000453 : mOnDied(onDied), mOnUnlinked(nullptr) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700454 LOG_ALWAYS_FATAL_IF(onDied == nullptr, "onDied == nullptr");
Steven Moreland901c7452018-09-04 16:17:28 -0700455}
456
Devin Moore9f9d0e12024-02-29 22:07:12 +0000457void AIBinder_DeathRecipient::pruneThisTransferEntry(const sp<IBinder>& who, void* cookie) {
458 std::lock_guard<std::mutex> l(mDeathRecipientsMutex);
459 mDeathRecipients.erase(std::remove_if(mDeathRecipients.begin(), mDeathRecipients.end(),
460 [&](const sp<TransferDeathRecipient>& tdr) {
461 auto tdrWho = tdr->getWho();
462 return tdrWho != nullptr && tdrWho.promote() == who &&
463 cookie == tdr->getCookie();
464 }),
465 mDeathRecipients.end());
466}
467
Steven Moreland64127ca2019-03-13 09:25:44 -0700468void AIBinder_DeathRecipient::pruneDeadTransferEntriesLocked() {
469 mDeathRecipients.erase(std::remove_if(mDeathRecipients.begin(), mDeathRecipients.end(),
470 [](const sp<TransferDeathRecipient>& tdr) {
471 return tdr->getWho() == nullptr;
472 }),
473 mDeathRecipients.end());
474}
475
Jiyong Park4f97a8c2020-11-16 18:11:14 +0900476binder_status_t AIBinder_DeathRecipient::linkToDeath(const sp<IBinder>& binder, void* cookie) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700477 LOG_ALWAYS_FATAL_IF(binder == nullptr, "binder == nullptr");
Steven Moreland901c7452018-09-04 16:17:28 -0700478
479 std::lock_guard<std::mutex> l(mDeathRecipientsMutex);
480
Devin Mooref93b3102024-04-12 21:48:35 +0000481 if (mOnUnlinked && cookie &&
482 std::find_if(mDeathRecipients.begin(), mDeathRecipients.end(),
483 [&cookie](android::sp<TransferDeathRecipient> recipient) {
484 return recipient->getCookie() == cookie;
485 }) != mDeathRecipients.end()) {
486 ALOGE("Attempting to AIBinder_linkToDeath with the same cookie with an onUnlink callback. "
487 "This will cause the onUnlinked callback to be called multiple times with the same "
488 "cookie, which is usually not intended.");
489 }
Devin Moore50c3d702024-04-16 23:52:13 +0000490 if (!mOnUnlinked && cookie) {
491 ALOGW("AIBinder_linkToDeath is being called with a non-null cookie and no onUnlink "
492 "callback set. This might not be intended. AIBinder_DeathRecipient_setOnUnlinked "
493 "should be called first.");
494 }
Devin Mooref93b3102024-04-12 21:48:35 +0000495
Steven Moreland901c7452018-09-04 16:17:28 -0700496 sp<TransferDeathRecipient> recipient =
Alice Ryhlea9d9d22021-08-27 07:51:30 +0000497 new TransferDeathRecipient(binder, cookie, this, mOnDied, mOnUnlinked);
Steven Moreland901c7452018-09-04 16:17:28 -0700498
Steven Moreland64127ca2019-03-13 09:25:44 -0700499 status_t status = binder->linkToDeath(recipient, cookie, 0 /*flags*/);
Steven Moreland5d62e442018-09-13 15:01:02 -0700500 if (status != STATUS_OK) {
Alice Ryhlea9d9d22021-08-27 07:51:30 +0000501 // When we failed to link, the destructor of TransferDeathRecipient runs here, which
502 // ensures that mOnUnlinked is called before we return with an error from this method.
Steven Moreland5d62e442018-09-13 15:01:02 -0700503 return PruneStatusT(status);
Steven Moreland901c7452018-09-04 16:17:28 -0700504 }
505
506 mDeathRecipients.push_back(recipient);
Steven Moreland64127ca2019-03-13 09:25:44 -0700507
508 pruneDeadTransferEntriesLocked();
Steven Moreland5d62e442018-09-13 15:01:02 -0700509 return STATUS_OK;
Steven Moreland901c7452018-09-04 16:17:28 -0700510}
511
Jiyong Park4f97a8c2020-11-16 18:11:14 +0900512binder_status_t AIBinder_DeathRecipient::unlinkToDeath(const sp<IBinder>& binder, void* cookie) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700513 LOG_ALWAYS_FATAL_IF(binder == nullptr, "binder == nullptr");
Steven Moreland901c7452018-09-04 16:17:28 -0700514
515 std::lock_guard<std::mutex> l(mDeathRecipientsMutex);
516
517 for (auto it = mDeathRecipients.rbegin(); it != mDeathRecipients.rend(); ++it) {
518 sp<TransferDeathRecipient> recipient = *it;
519
Steven Moreland64127ca2019-03-13 09:25:44 -0700520 if (recipient->getCookie() == cookie && recipient->getWho() == binder) {
Steven Moreland901c7452018-09-04 16:17:28 -0700521 mDeathRecipients.erase(it.base() - 1);
522
Steven Moreland64127ca2019-03-13 09:25:44 -0700523 status_t status = binder->unlinkToDeath(recipient, cookie, 0 /*flags*/);
Steven Moreland5d62e442018-09-13 15:01:02 -0700524 if (status != ::android::OK) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700525 ALOGE("%s: removed reference to death recipient but unlink failed: %s", __func__,
526 statusToString(status).c_str());
Steven Moreland901c7452018-09-04 16:17:28 -0700527 }
Steven Moreland5d62e442018-09-13 15:01:02 -0700528 return PruneStatusT(status);
Steven Moreland901c7452018-09-04 16:17:28 -0700529 }
530 }
531
Steven Moreland5d62e442018-09-13 15:01:02 -0700532 return STATUS_NAME_NOT_FOUND;
Steven Moreland901c7452018-09-04 16:17:28 -0700533}
534
Alice Ryhlea9d9d22021-08-27 07:51:30 +0000535void AIBinder_DeathRecipient::setOnUnlinked(AIBinder_DeathRecipient_onBinderUnlinked onUnlinked) {
536 mOnUnlinked = onUnlinked;
537}
538
Steven Moreland901c7452018-09-04 16:17:28 -0700539// start of C-API methods
540
Steven Moreland2e87adc2018-08-20 19:47:00 -0700541AIBinder* AIBinder_new(const AIBinder_Class* clazz, void* args) {
542 if (clazz == nullptr) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700543 ALOGE("%s: Must provide class to construct local binder.", __func__);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700544 return nullptr;
545 }
546
547 void* userData = clazz->onCreate(args);
548
Steven Moreland71cddc32018-08-30 23:39:22 -0700549 sp<AIBinder> ret = new ABBinder(clazz, userData);
550 ABBinderTag::attach(ret->getBinder());
551
552 AIBinder_incStrong(ret.get());
553 return ret.get();
Steven Moreland2e87adc2018-08-20 19:47:00 -0700554}
555
Steven Moreland5ea54da2018-09-04 13:29:55 -0700556bool AIBinder_isRemote(const AIBinder* binder) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700557 if (binder == nullptr) {
Steven Moreland56f752a2018-11-05 17:23:37 -0800558 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700559 }
560
561 return binder->isRemote();
562}
563
Steven Moreland65867d72018-09-04 14:22:26 -0700564bool AIBinder_isAlive(const AIBinder* binder) {
565 if (binder == nullptr) {
566 return false;
567 }
568
569 return const_cast<AIBinder*>(binder)->getBinder()->isBinderAlive();
570}
571
572binder_status_t AIBinder_ping(AIBinder* binder) {
573 if (binder == nullptr) {
Steven Moreland5d62e442018-09-13 15:01:02 -0700574 return STATUS_UNEXPECTED_NULL;
Steven Moreland65867d72018-09-04 14:22:26 -0700575 }
576
Steven Moreland5d62e442018-09-13 15:01:02 -0700577 return PruneStatusT(binder->getBinder()->pingBinder());
Steven Moreland65867d72018-09-04 14:22:26 -0700578}
579
Steven Morelanda194c452019-03-04 16:47:07 -0800580binder_status_t AIBinder_dump(AIBinder* binder, int fd, const char** args, uint32_t numArgs) {
581 if (binder == nullptr) {
582 return STATUS_UNEXPECTED_NULL;
583 }
584
585 ABBinder* bBinder = binder->asABBinder();
586 if (bBinder != nullptr) {
587 AIBinder_onDump onDump = binder->getClass()->onDump;
588 if (onDump == nullptr) {
589 return STATUS_OK;
590 }
591 return PruneStatusT(onDump(bBinder, fd, args, numArgs));
592 }
593
594 ::android::Vector<String16> utf16Args;
595 utf16Args.setCapacity(numArgs);
596 for (uint32_t i = 0; i < numArgs; i++) {
597 utf16Args.push(String16(String8(args[i])));
598 }
599
600 status_t status = binder->getBinder()->dump(fd, utf16Args);
601 return PruneStatusT(status);
602}
603
Steven Moreland901c7452018-09-04 16:17:28 -0700604binder_status_t AIBinder_linkToDeath(AIBinder* binder, AIBinder_DeathRecipient* recipient,
605 void* cookie) {
606 if (binder == nullptr || recipient == nullptr) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700607 ALOGE("%s: Must provide binder (%p) and recipient (%p)", __func__, binder, recipient);
Steven Moreland5d62e442018-09-13 15:01:02 -0700608 return STATUS_UNEXPECTED_NULL;
Steven Moreland901c7452018-09-04 16:17:28 -0700609 }
610
Devin Moore9f9d0e12024-02-29 22:07:12 +0000611 binder_status_t ret = recipient->linkToDeath(binder->getBinder(), cookie);
612 if (ret == STATUS_OK) {
613 binder->addDeathRecipient(recipient, cookie);
614 }
615 return ret;
Steven Moreland901c7452018-09-04 16:17:28 -0700616}
617
618binder_status_t AIBinder_unlinkToDeath(AIBinder* binder, AIBinder_DeathRecipient* recipient,
619 void* cookie) {
620 if (binder == nullptr || recipient == nullptr) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700621 ALOGE("%s: Must provide binder (%p) and recipient (%p)", __func__, binder, recipient);
Steven Moreland5d62e442018-09-13 15:01:02 -0700622 return STATUS_UNEXPECTED_NULL;
Steven Moreland901c7452018-09-04 16:17:28 -0700623 }
624
Steven Moreland5d62e442018-09-13 15:01:02 -0700625 // returns binder_status_t
Steven Moreland64127ca2019-03-13 09:25:44 -0700626 return recipient->unlinkToDeath(binder->getBinder(), cookie);
Steven Moreland901c7452018-09-04 16:17:28 -0700627}
628
Andrei Homescu40dbf252024-03-28 21:19:42 +0000629#ifdef BINDER_WITH_KERNEL_IPC
Steven Morelandf3034b02018-11-12 17:37:46 -0800630uid_t AIBinder_getCallingUid() {
631 return ::android::IPCThreadState::self()->getCallingUid();
632}
633
634pid_t AIBinder_getCallingPid() {
635 return ::android::IPCThreadState::self()->getCallingPid();
636}
637
Alice Ryhl59aeae82021-08-25 10:21:26 +0000638bool AIBinder_isHandlingTransaction() {
639 return ::android::IPCThreadState::self()->getServingStackPointer() != nullptr;
640}
Andrei Homescu40dbf252024-03-28 21:19:42 +0000641#endif
Alice Ryhl59aeae82021-08-25 10:21:26 +0000642
Steven Moreland2e87adc2018-08-20 19:47:00 -0700643void AIBinder_incStrong(AIBinder* binder) {
644 if (binder == nullptr) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700645 return;
646 }
647
648 binder->incStrong(nullptr);
649}
650void AIBinder_decStrong(AIBinder* binder) {
651 if (binder == nullptr) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700652 ALOGE("%s: on null binder", __func__);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700653 return;
654 }
655
656 binder->decStrong(nullptr);
657}
658int32_t AIBinder_debugGetRefCount(AIBinder* binder) {
659 if (binder == nullptr) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700660 ALOGE("%s: on null binder", __func__);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700661 return -1;
662 }
663
664 return binder->getStrongCount();
665}
666
Steven Moreland71cddc32018-08-30 23:39:22 -0700667bool AIBinder_associateClass(AIBinder* binder, const AIBinder_Class* clazz) {
668 if (binder == nullptr) {
669 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700670 }
671
Steven Moreland71cddc32018-08-30 23:39:22 -0700672 return binder->associateClass(clazz);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700673}
674
675const AIBinder_Class* AIBinder_getClass(AIBinder* binder) {
676 if (binder == nullptr) {
677 return nullptr;
678 }
679
680 return binder->getClass();
681}
682
683void* AIBinder_getUserData(AIBinder* binder) {
684 if (binder == nullptr) {
685 return nullptr;
686 }
687
688 ABBinder* bBinder = binder->asABBinder();
689 if (bBinder == nullptr) {
690 return nullptr;
691 }
692
693 return bBinder->getUserData();
694}
695
696binder_status_t AIBinder_prepareTransaction(AIBinder* binder, AParcel** in) {
697 if (binder == nullptr || in == nullptr) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700698 ALOGE("%s: requires non-null parameters binder (%p) and in (%p).", __func__, binder, in);
Steven Moreland5d62e442018-09-13 15:01:02 -0700699 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700700 }
701 const AIBinder_Class* clazz = binder->getClass();
702 if (clazz == nullptr) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700703 ALOGE("%s: Class must be defined for a remote binder transaction. See "
704 "AIBinder_associateClass.",
705 __func__);
Steven Moreland5d62e442018-09-13 15:01:02 -0700706 return STATUS_INVALID_OPERATION;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700707 }
708
709 *in = new AParcel(binder);
Steven Moreland1fda67b2021-04-02 18:35:50 +0000710 (*in)->get()->markForBinder(binder->getBinder());
711
Steven Morelandf3ed8062021-08-27 14:31:14 -0700712 status_t status = android::OK;
Steven Morelandfaf25a42022-12-21 02:21:36 +0000713
714 // note - this is the only read of a value in clazz, and it comes with a warning
715 // on the API itself. Do not copy this design. Instead, attach data in a new
716 // version of the prepareTransaction function.
Steven Morelandf3ed8062021-08-27 14:31:14 -0700717 if (clazz->writeHeader) {
718 status = (*in)->get()->writeInterfaceToken(clazz->getInterfaceDescriptor());
719 }
Steven Moreland5d62e442018-09-13 15:01:02 -0700720 binder_status_t ret = PruneStatusT(status);
721
722 if (ret != STATUS_OK) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700723 delete *in;
724 *in = nullptr;
725 }
726
Steven Moreland5d62e442018-09-13 15:01:02 -0700727 return ret;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700728}
729
Steven Moreland9b80e282018-09-19 13:57:23 -0700730static void DestroyParcel(AParcel** parcel) {
731 delete *parcel;
732 *parcel = nullptr;
733}
734
Steven Moreland2e87adc2018-08-20 19:47:00 -0700735binder_status_t AIBinder_transact(AIBinder* binder, transaction_code_t code, AParcel** in,
736 AParcel** out, binder_flags_t flags) {
737 if (in == nullptr) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700738 ALOGE("%s: requires non-null in parameter", __func__);
Steven Moreland5d62e442018-09-13 15:01:02 -0700739 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700740 }
741
Steven Morelandcaa776c2018-09-04 13:48:11 -0700742 using AutoParcelDestroyer = std::unique_ptr<AParcel*, void (*)(AParcel**)>;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700743 // This object is the input to the transaction. This function takes ownership of it and deletes
744 // it.
Steven Moreland9b80e282018-09-19 13:57:23 -0700745 AutoParcelDestroyer forIn(in, DestroyParcel);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700746
747 if (!isUserCommand(code)) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700748 ALOGE("%s: Only user-defined transactions can be made from the NDK, but requested: %d",
749 __func__, code);
Steven Moreland5d62e442018-09-13 15:01:02 -0700750 return STATUS_UNKNOWN_TRANSACTION;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700751 }
752
Steven Morelandf183fdd2020-10-27 00:12:12 +0000753 constexpr binder_flags_t kAllFlags = FLAG_PRIVATE_VENDOR | FLAG_ONEWAY | FLAG_CLEAR_BUF;
Steven Moreland46b5fea2019-10-15 11:22:18 -0700754 if ((flags & ~kAllFlags) != 0) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700755 ALOGE("%s: Unrecognized flags sent: %d", __func__, flags);
Steven Moreland5d62e442018-09-13 15:01:02 -0700756 return STATUS_BAD_VALUE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700757 }
758
759 if (binder == nullptr || *in == nullptr || out == nullptr) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700760 ALOGE("%s: requires non-null parameters binder (%p), in (%p), and out (%p).", __func__,
761 binder, in, out);
Steven Moreland5d62e442018-09-13 15:01:02 -0700762 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700763 }
764
765 if ((*in)->getBinder() != binder) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700766 ALOGE("%s: parcel is associated with binder object %p but called with %p", __func__, binder,
767 (*in)->getBinder());
Steven Moreland5d62e442018-09-13 15:01:02 -0700768 return STATUS_BAD_VALUE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700769 }
770
771 *out = new AParcel(binder);
772
Steven Morelandf18615b2018-09-14 11:43:06 -0700773 status_t status = binder->getBinder()->transact(code, *(*in)->get(), (*out)->get(), flags);
Steven Moreland5d62e442018-09-13 15:01:02 -0700774 binder_status_t ret = PruneStatusT(status);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700775
Steven Moreland5d62e442018-09-13 15:01:02 -0700776 if (ret != STATUS_OK) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700777 delete *out;
778 *out = nullptr;
779 }
780
Steven Moreland5d62e442018-09-13 15:01:02 -0700781 return ret;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700782}
Steven Moreland901c7452018-09-04 16:17:28 -0700783
784AIBinder_DeathRecipient* AIBinder_DeathRecipient_new(
785 AIBinder_DeathRecipient_onBinderDied onBinderDied) {
786 if (onBinderDied == nullptr) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700787 ALOGE("%s: requires non-null onBinderDied parameter.", __func__);
Steven Moreland901c7452018-09-04 16:17:28 -0700788 return nullptr;
789 }
Steven Moreland64127ca2019-03-13 09:25:44 -0700790 auto ret = new AIBinder_DeathRecipient(onBinderDied);
791 ret->incStrong(nullptr);
792 return ret;
Steven Moreland901c7452018-09-04 16:17:28 -0700793}
794
Alice Ryhlea9d9d22021-08-27 07:51:30 +0000795void AIBinder_DeathRecipient_setOnUnlinked(AIBinder_DeathRecipient* recipient,
796 AIBinder_DeathRecipient_onBinderUnlinked onUnlinked) {
797 if (recipient == nullptr) {
798 return;
799 }
800
801 recipient->setOnUnlinked(onUnlinked);
802}
803
Steven Moreland9b80e282018-09-19 13:57:23 -0700804void AIBinder_DeathRecipient_delete(AIBinder_DeathRecipient* recipient) {
Steven Moreland64127ca2019-03-13 09:25:44 -0700805 if (recipient == nullptr) {
806 return;
807 }
808
809 recipient->decStrong(nullptr);
Steven Moreland901c7452018-09-04 16:17:28 -0700810}
Steven Morelandea14ef22019-08-20 10:50:08 -0700811
812binder_status_t AIBinder_getExtension(AIBinder* binder, AIBinder** outExt) {
813 if (binder == nullptr || outExt == nullptr) {
814 if (outExt != nullptr) {
815 *outExt = nullptr;
816 }
817 return STATUS_UNEXPECTED_NULL;
818 }
819
820 sp<IBinder> ext;
821 status_t res = binder->getBinder()->getExtension(&ext);
822
823 if (res != android::OK) {
824 *outExt = nullptr;
825 return PruneStatusT(res);
826 }
827
828 sp<AIBinder> ret = ABpBinder::lookupOrCreateFromBinder(ext);
829 if (ret != nullptr) ret->incStrong(binder);
830
831 *outExt = ret.get();
832 return STATUS_OK;
833}
834
835binder_status_t AIBinder_setExtension(AIBinder* binder, AIBinder* ext) {
836 if (binder == nullptr || ext == nullptr) {
837 return STATUS_UNEXPECTED_NULL;
838 }
839
840 ABBinder* rawBinder = binder->asABBinder();
841 if (rawBinder == nullptr) {
842 return STATUS_INVALID_OPERATION;
843 }
844
845 rawBinder->setExtension(ext->getBinder());
846 return STATUS_OK;
847}
Steven Moreland2f405f52020-07-08 22:24:29 +0000848
849// platform methods follow
850
851void AIBinder_setRequestingSid(AIBinder* binder, bool requestingSid) {
852 ABBinder* localBinder = binder->asABBinder();
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700853 LOG_ALWAYS_FATAL_IF(localBinder == nullptr,
854 "AIBinder_setRequestingSid must be called on a local binder");
Steven Moreland2f405f52020-07-08 22:24:29 +0000855
856 localBinder->setRequestingSid(requestingSid);
857}
858
Andrei Homescu40dbf252024-03-28 21:19:42 +0000859#ifdef BINDER_WITH_KERNEL_IPC
Steven Moreland2f405f52020-07-08 22:24:29 +0000860const char* AIBinder_getCallingSid() {
861 return ::android::IPCThreadState::self()->getCallingSid();
862}
Andrei Homescu40dbf252024-03-28 21:19:42 +0000863#endif
Steven Morelandb2de9532020-05-29 21:44:41 +0000864
Ady Abraham96174ac2021-10-15 11:02:03 -0700865void AIBinder_setMinSchedulerPolicy(AIBinder* binder, int policy, int priority) {
866 binder->asABBinder()->setMinSchedulerPolicy(policy, priority);
867}
Steven Moreland454a26a2021-12-14 01:26:21 +0000868
869void AIBinder_setInheritRt(AIBinder* binder, bool inheritRt) {
870 ABBinder* localBinder = binder->asABBinder();
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700871 LOG_ALWAYS_FATAL_IF(localBinder == nullptr,
872 "AIBinder_setInheritRt must be called on a local binder");
Steven Moreland454a26a2021-12-14 01:26:21 +0000873
874 localBinder->setInheritRt(inheritRt);
875}