blob: e2ede3f52a94f6a9c6a0af67f90a0c8b69029320 [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
Devin Moore9f9d0e12024-02-29 22:07:12 +0000261void ABBinder::addDeathRecipient(const ::android::sp<AIBinder_DeathRecipient>& /* recipient */,
262 void* /* cookie */) {
263 LOG_ALWAYS_FATAL("Should not reach this. Can't linkToDeath local binders.");
264}
265
Steven Moreland71cddc32018-08-30 23:39:22 -0700266ABpBinder::ABpBinder(const ::android::sp<::android::IBinder>& binder)
Steven Moreland3acd4902022-07-26 18:29:58 +0000267 : AIBinder(nullptr /*clazz*/), mRemote(binder) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700268 LOG_ALWAYS_FATAL_IF(binder == nullptr, "binder == nullptr");
Steven Moreland2e87adc2018-08-20 19:47:00 -0700269}
Devin Moore9f9d0e12024-02-29 22:07:12 +0000270
271ABpBinder::~ABpBinder() {
272 for (auto& recip : mDeathRecipients) {
273 sp<AIBinder_DeathRecipient> strongRecip = recip.recipient.promote();
274 if (strongRecip) {
275 strongRecip->pruneThisTransferEntry(getBinder(), recip.cookie);
276 }
277 }
278}
Steven Moreland2e87adc2018-08-20 19:47:00 -0700279
Steven Moreland94968952018-09-05 14:42:59 -0700280sp<AIBinder> ABpBinder::lookupOrCreateFromBinder(const ::android::sp<::android::IBinder>& binder) {
Steven Moreland71cddc32018-08-30 23:39:22 -0700281 if (binder == nullptr) {
282 return nullptr;
283 }
284 if (ABBinderTag::has(binder)) {
285 return static_cast<ABBinder*>(binder.get());
286 }
Steven Moreland94968952018-09-05 14:42:59 -0700287
Steven Moreland91a96b62021-06-30 23:07:13 +0000288 // The following code ensures that for a given binder object (remote or local), if it is not an
289 // ABBinder then at most one ABpBinder object exists in a given process representing it.
290
Steven Moreland1f1bed62021-06-25 21:50:50 +0000291 auto* value = static_cast<ABpBinderTag::Value*>(binder->findObject(ABpBinderTag::kId));
Steven Moreland94968952018-09-05 14:42:59 -0700292 if (value == nullptr) {
293 value = new ABpBinderTag::Value;
Steven Moreland1f1bed62021-06-25 21:50:50 +0000294 auto oldValue = static_cast<ABpBinderTag::Value*>(
295 binder->attachObject(ABpBinderTag::kId, static_cast<void*>(value),
296 nullptr /*cookie*/, ABpBinderTag::clean));
297
298 // allocated by another thread
299 if (oldValue) {
300 delete value;
301 value = oldValue;
302 }
Steven Moreland94968952018-09-05 14:42:59 -0700303 }
304
Steven Moreland1f1bed62021-06-25 21:50:50 +0000305 sp<ABpBinder> ret;
306 binder->withLock([&]() {
307 ret = value->binder.promote();
308 if (ret == nullptr) {
309 ret = sp<ABpBinder>::make(binder);
310 value->binder = ret;
311 }
312 });
Steven Moreland94968952018-09-05 14:42:59 -0700313
314 return ret;
Steven Moreland71cddc32018-08-30 23:39:22 -0700315}
316
Devin Moore9f9d0e12024-02-29 22:07:12 +0000317void ABpBinder::addDeathRecipient(const ::android::sp<AIBinder_DeathRecipient>& recipient,
318 void* cookie) {
319 std::lock_guard<std::mutex> l(mDeathRecipientsMutex);
320 mDeathRecipients.emplace_back(recipient, cookie);
321}
322
Steven Moreland2e87adc2018-08-20 19:47:00 -0700323struct AIBinder_Weak {
324 wp<AIBinder> binder;
325};
326AIBinder_Weak* AIBinder_Weak_new(AIBinder* binder) {
Steven Moreland5ccb70f2018-09-04 16:30:21 -0700327 if (binder == nullptr) {
328 return nullptr;
329 }
330
Steven Moreland2e87adc2018-08-20 19:47:00 -0700331 return new AIBinder_Weak{wp<AIBinder>(binder)};
332}
Steven Moreland9b80e282018-09-19 13:57:23 -0700333void AIBinder_Weak_delete(AIBinder_Weak* weakBinder) {
334 delete weakBinder;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700335}
336AIBinder* AIBinder_Weak_promote(AIBinder_Weak* weakBinder) {
Steven Moreland5ccb70f2018-09-04 16:30:21 -0700337 if (weakBinder == nullptr) {
338 return nullptr;
339 }
340
Steven Moreland2e87adc2018-08-20 19:47:00 -0700341 sp<AIBinder> binder = weakBinder->binder.promote();
342 AIBinder_incStrong(binder.get());
343 return binder.get();
344}
345
Steven Morelandc70e0122021-01-08 02:44:53 +0000346AIBinder_Weak* AIBinder_Weak_clone(const AIBinder_Weak* weak) {
347 if (weak == nullptr) {
348 return nullptr;
349 }
350
351 return new AIBinder_Weak{weak->binder};
352}
353
354bool AIBinder_lt(const AIBinder* lhs, const AIBinder* rhs) {
355 if (lhs == nullptr || rhs == nullptr) return lhs < rhs;
356
357 return const_cast<AIBinder*>(lhs)->getBinder() < const_cast<AIBinder*>(rhs)->getBinder();
358}
359
360bool AIBinder_Weak_lt(const AIBinder_Weak* lhs, const AIBinder_Weak* rhs) {
361 if (lhs == nullptr || rhs == nullptr) return lhs < rhs;
362
363 return lhs->binder < rhs->binder;
364}
365
Steven Morelandfaf25a42022-12-21 02:21:36 +0000366// WARNING: When multiple classes exist with the same interface descriptor in different
367// linkernamespaces, the first one to be associated with mClazz becomes the canonical one
368// and the only requirement on this is that the interface descriptors match. If this
369// is an ABpBinder, no other state can be referenced from mClazz.
Steven Moreland2e87adc2018-08-20 19:47:00 -0700370AIBinder_Class::AIBinder_Class(const char* interfaceDescriptor, AIBinder_Class_onCreate onCreate,
371 AIBinder_Class_onDestroy onDestroy,
372 AIBinder_Class_onTransact onTransact)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700373 : onCreate(onCreate),
374 onDestroy(onDestroy),
375 onTransact(onTransact),
Stephen Crane8fde87f2020-11-17 15:06:11 -0800376 mInterfaceDescriptor(interfaceDescriptor),
377 mWideInterfaceDescriptor(interfaceDescriptor) {}
Steven Moreland2e87adc2018-08-20 19:47:00 -0700378
379AIBinder_Class* AIBinder_Class_define(const char* interfaceDescriptor,
380 AIBinder_Class_onCreate onCreate,
381 AIBinder_Class_onDestroy onDestroy,
382 AIBinder_Class_onTransact onTransact) {
383 if (interfaceDescriptor == nullptr || onCreate == nullptr || onDestroy == nullptr ||
384 onTransact == nullptr) {
385 return nullptr;
386 }
387
388 return new AIBinder_Class(interfaceDescriptor, onCreate, onDestroy, onTransact);
389}
390
Steven Morelanda194c452019-03-04 16:47:07 -0800391void AIBinder_Class_setOnDump(AIBinder_Class* clazz, AIBinder_onDump onDump) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700392 LOG_ALWAYS_FATAL_IF(clazz == nullptr, "setOnDump requires non-null clazz");
Steven Morelanda194c452019-03-04 16:47:07 -0800393
394 // this is required to be called before instances are instantiated
395 clazz->onDump = onDump;
396}
397
Steven Morelandf3ed8062021-08-27 14:31:14 -0700398void AIBinder_Class_disableInterfaceTokenHeader(AIBinder_Class* clazz) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700399 LOG_ALWAYS_FATAL_IF(clazz == nullptr, "disableInterfaceTokenHeader requires non-null clazz");
Steven Morelandf3ed8062021-08-27 14:31:14 -0700400
401 clazz->writeHeader = false;
402}
403
Ruchir Rastogicc7a7462020-01-31 14:29:15 -0800404void AIBinder_Class_setHandleShellCommand(AIBinder_Class* clazz,
405 AIBinder_handleShellCommand handleShellCommand) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700406 LOG_ALWAYS_FATAL_IF(clazz == nullptr, "setHandleShellCommand requires non-null clazz");
Ruchir Rastogicc7a7462020-01-31 14:29:15 -0800407
408 clazz->handleShellCommand = handleShellCommand;
409}
410
Stephen Crane8fde87f2020-11-17 15:06:11 -0800411const char* AIBinder_Class_getDescriptor(const AIBinder_Class* clazz) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700412 LOG_ALWAYS_FATAL_IF(clazz == nullptr, "getDescriptor requires non-null clazz");
Stephen Crane8fde87f2020-11-17 15:06:11 -0800413
414 return clazz->getInterfaceDescriptorUtf8();
415}
416
Alice Ryhlea9d9d22021-08-27 07:51:30 +0000417AIBinder_DeathRecipient::TransferDeathRecipient::~TransferDeathRecipient() {
418 if (mOnUnlinked != nullptr) {
419 mOnUnlinked(mCookie);
420 }
421}
422
Steven Moreland901c7452018-09-04 16:17:28 -0700423void AIBinder_DeathRecipient::TransferDeathRecipient::binderDied(const wp<IBinder>& who) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700424 LOG_ALWAYS_FATAL_IF(who != mWho, "%p (%p) vs %p (%p)", who.unsafe_get(), who.get_refs(),
425 mWho.unsafe_get(), mWho.get_refs());
Steven Moreland901c7452018-09-04 16:17:28 -0700426
427 mOnDied(mCookie);
Steven Moreland64127ca2019-03-13 09:25:44 -0700428
429 sp<AIBinder_DeathRecipient> recipient = mParentRecipient.promote();
430 sp<IBinder> strongWho = who.promote();
431
432 // otherwise this will be cleaned up later with pruneDeadTransferEntriesLocked
433 if (recipient != nullptr && strongWho != nullptr) {
434 status_t result = recipient->unlinkToDeath(strongWho, mCookie);
435 if (result != ::android::DEAD_OBJECT) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700436 ALOGW("Unlinking to dead binder resulted in: %d", result);
Steven Moreland64127ca2019-03-13 09:25:44 -0700437 }
438 }
439
Steven Moreland901c7452018-09-04 16:17:28 -0700440 mWho = nullptr;
441}
442
443AIBinder_DeathRecipient::AIBinder_DeathRecipient(AIBinder_DeathRecipient_onBinderDied onDied)
Alice Ryhlea9d9d22021-08-27 07:51:30 +0000444 : mOnDied(onDied), mOnUnlinked(nullptr) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700445 LOG_ALWAYS_FATAL_IF(onDied == nullptr, "onDied == nullptr");
Steven Moreland901c7452018-09-04 16:17:28 -0700446}
447
Devin Moore9f9d0e12024-02-29 22:07:12 +0000448void AIBinder_DeathRecipient::pruneThisTransferEntry(const sp<IBinder>& who, void* cookie) {
449 std::lock_guard<std::mutex> l(mDeathRecipientsMutex);
450 mDeathRecipients.erase(std::remove_if(mDeathRecipients.begin(), mDeathRecipients.end(),
451 [&](const sp<TransferDeathRecipient>& tdr) {
452 auto tdrWho = tdr->getWho();
453 return tdrWho != nullptr && tdrWho.promote() == who &&
454 cookie == tdr->getCookie();
455 }),
456 mDeathRecipients.end());
457}
458
Steven Moreland64127ca2019-03-13 09:25:44 -0700459void AIBinder_DeathRecipient::pruneDeadTransferEntriesLocked() {
460 mDeathRecipients.erase(std::remove_if(mDeathRecipients.begin(), mDeathRecipients.end(),
461 [](const sp<TransferDeathRecipient>& tdr) {
462 return tdr->getWho() == nullptr;
463 }),
464 mDeathRecipients.end());
465}
466
Jiyong Park4f97a8c2020-11-16 18:11:14 +0900467binder_status_t AIBinder_DeathRecipient::linkToDeath(const sp<IBinder>& binder, void* cookie) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700468 LOG_ALWAYS_FATAL_IF(binder == nullptr, "binder == nullptr");
Steven Moreland901c7452018-09-04 16:17:28 -0700469
470 std::lock_guard<std::mutex> l(mDeathRecipientsMutex);
471
472 sp<TransferDeathRecipient> recipient =
Alice Ryhlea9d9d22021-08-27 07:51:30 +0000473 new TransferDeathRecipient(binder, cookie, this, mOnDied, mOnUnlinked);
Steven Moreland901c7452018-09-04 16:17:28 -0700474
Steven Moreland64127ca2019-03-13 09:25:44 -0700475 status_t status = binder->linkToDeath(recipient, cookie, 0 /*flags*/);
Steven Moreland5d62e442018-09-13 15:01:02 -0700476 if (status != STATUS_OK) {
Alice Ryhlea9d9d22021-08-27 07:51:30 +0000477 // When we failed to link, the destructor of TransferDeathRecipient runs here, which
478 // ensures that mOnUnlinked is called before we return with an error from this method.
Steven Moreland5d62e442018-09-13 15:01:02 -0700479 return PruneStatusT(status);
Steven Moreland901c7452018-09-04 16:17:28 -0700480 }
481
482 mDeathRecipients.push_back(recipient);
Steven Moreland64127ca2019-03-13 09:25:44 -0700483
484 pruneDeadTransferEntriesLocked();
Steven Moreland5d62e442018-09-13 15:01:02 -0700485 return STATUS_OK;
Steven Moreland901c7452018-09-04 16:17:28 -0700486}
487
Jiyong Park4f97a8c2020-11-16 18:11:14 +0900488binder_status_t AIBinder_DeathRecipient::unlinkToDeath(const sp<IBinder>& binder, void* cookie) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700489 LOG_ALWAYS_FATAL_IF(binder == nullptr, "binder == nullptr");
Steven Moreland901c7452018-09-04 16:17:28 -0700490
491 std::lock_guard<std::mutex> l(mDeathRecipientsMutex);
492
493 for (auto it = mDeathRecipients.rbegin(); it != mDeathRecipients.rend(); ++it) {
494 sp<TransferDeathRecipient> recipient = *it;
495
Steven Moreland64127ca2019-03-13 09:25:44 -0700496 if (recipient->getCookie() == cookie && recipient->getWho() == binder) {
Steven Moreland901c7452018-09-04 16:17:28 -0700497 mDeathRecipients.erase(it.base() - 1);
498
Steven Moreland64127ca2019-03-13 09:25:44 -0700499 status_t status = binder->unlinkToDeath(recipient, cookie, 0 /*flags*/);
Steven Moreland5d62e442018-09-13 15:01:02 -0700500 if (status != ::android::OK) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700501 ALOGE("%s: removed reference to death recipient but unlink failed: %s", __func__,
502 statusToString(status).c_str());
Steven Moreland901c7452018-09-04 16:17:28 -0700503 }
Steven Moreland5d62e442018-09-13 15:01:02 -0700504 return PruneStatusT(status);
Steven Moreland901c7452018-09-04 16:17:28 -0700505 }
506 }
507
Steven Moreland5d62e442018-09-13 15:01:02 -0700508 return STATUS_NAME_NOT_FOUND;
Steven Moreland901c7452018-09-04 16:17:28 -0700509}
510
Alice Ryhlea9d9d22021-08-27 07:51:30 +0000511void AIBinder_DeathRecipient::setOnUnlinked(AIBinder_DeathRecipient_onBinderUnlinked onUnlinked) {
512 mOnUnlinked = onUnlinked;
513}
514
Steven Moreland901c7452018-09-04 16:17:28 -0700515// start of C-API methods
516
Steven Moreland2e87adc2018-08-20 19:47:00 -0700517AIBinder* AIBinder_new(const AIBinder_Class* clazz, void* args) {
518 if (clazz == nullptr) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700519 ALOGE("%s: Must provide class to construct local binder.", __func__);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700520 return nullptr;
521 }
522
523 void* userData = clazz->onCreate(args);
524
Steven Moreland71cddc32018-08-30 23:39:22 -0700525 sp<AIBinder> ret = new ABBinder(clazz, userData);
526 ABBinderTag::attach(ret->getBinder());
527
528 AIBinder_incStrong(ret.get());
529 return ret.get();
Steven Moreland2e87adc2018-08-20 19:47:00 -0700530}
531
Steven Moreland5ea54da2018-09-04 13:29:55 -0700532bool AIBinder_isRemote(const AIBinder* binder) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700533 if (binder == nullptr) {
Steven Moreland56f752a2018-11-05 17:23:37 -0800534 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700535 }
536
537 return binder->isRemote();
538}
539
Steven Moreland65867d72018-09-04 14:22:26 -0700540bool AIBinder_isAlive(const AIBinder* binder) {
541 if (binder == nullptr) {
542 return false;
543 }
544
545 return const_cast<AIBinder*>(binder)->getBinder()->isBinderAlive();
546}
547
548binder_status_t AIBinder_ping(AIBinder* binder) {
549 if (binder == nullptr) {
Steven Moreland5d62e442018-09-13 15:01:02 -0700550 return STATUS_UNEXPECTED_NULL;
Steven Moreland65867d72018-09-04 14:22:26 -0700551 }
552
Steven Moreland5d62e442018-09-13 15:01:02 -0700553 return PruneStatusT(binder->getBinder()->pingBinder());
Steven Moreland65867d72018-09-04 14:22:26 -0700554}
555
Steven Morelanda194c452019-03-04 16:47:07 -0800556binder_status_t AIBinder_dump(AIBinder* binder, int fd, const char** args, uint32_t numArgs) {
557 if (binder == nullptr) {
558 return STATUS_UNEXPECTED_NULL;
559 }
560
561 ABBinder* bBinder = binder->asABBinder();
562 if (bBinder != nullptr) {
563 AIBinder_onDump onDump = binder->getClass()->onDump;
564 if (onDump == nullptr) {
565 return STATUS_OK;
566 }
567 return PruneStatusT(onDump(bBinder, fd, args, numArgs));
568 }
569
570 ::android::Vector<String16> utf16Args;
571 utf16Args.setCapacity(numArgs);
572 for (uint32_t i = 0; i < numArgs; i++) {
573 utf16Args.push(String16(String8(args[i])));
574 }
575
576 status_t status = binder->getBinder()->dump(fd, utf16Args);
577 return PruneStatusT(status);
578}
579
Steven Moreland901c7452018-09-04 16:17:28 -0700580binder_status_t AIBinder_linkToDeath(AIBinder* binder, AIBinder_DeathRecipient* recipient,
581 void* cookie) {
582 if (binder == nullptr || recipient == nullptr) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700583 ALOGE("%s: Must provide binder (%p) and recipient (%p)", __func__, binder, recipient);
Steven Moreland5d62e442018-09-13 15:01:02 -0700584 return STATUS_UNEXPECTED_NULL;
Steven Moreland901c7452018-09-04 16:17:28 -0700585 }
586
Devin Moore9f9d0e12024-02-29 22:07:12 +0000587 binder_status_t ret = recipient->linkToDeath(binder->getBinder(), cookie);
588 if (ret == STATUS_OK) {
589 binder->addDeathRecipient(recipient, cookie);
590 }
591 return ret;
Steven Moreland901c7452018-09-04 16:17:28 -0700592}
593
594binder_status_t AIBinder_unlinkToDeath(AIBinder* binder, AIBinder_DeathRecipient* recipient,
595 void* cookie) {
596 if (binder == nullptr || recipient == nullptr) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700597 ALOGE("%s: Must provide binder (%p) and recipient (%p)", __func__, binder, recipient);
Steven Moreland5d62e442018-09-13 15:01:02 -0700598 return STATUS_UNEXPECTED_NULL;
Steven Moreland901c7452018-09-04 16:17:28 -0700599 }
600
Steven Moreland5d62e442018-09-13 15:01:02 -0700601 // returns binder_status_t
Steven Moreland64127ca2019-03-13 09:25:44 -0700602 return recipient->unlinkToDeath(binder->getBinder(), cookie);
Steven Moreland901c7452018-09-04 16:17:28 -0700603}
604
Steven Morelandf3034b02018-11-12 17:37:46 -0800605uid_t AIBinder_getCallingUid() {
606 return ::android::IPCThreadState::self()->getCallingUid();
607}
608
609pid_t AIBinder_getCallingPid() {
610 return ::android::IPCThreadState::self()->getCallingPid();
611}
612
Alice Ryhl59aeae82021-08-25 10:21:26 +0000613bool AIBinder_isHandlingTransaction() {
614 return ::android::IPCThreadState::self()->getServingStackPointer() != nullptr;
615}
616
Steven Moreland2e87adc2018-08-20 19:47:00 -0700617void AIBinder_incStrong(AIBinder* binder) {
618 if (binder == nullptr) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700619 return;
620 }
621
622 binder->incStrong(nullptr);
623}
624void AIBinder_decStrong(AIBinder* binder) {
625 if (binder == nullptr) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700626 ALOGE("%s: on null binder", __func__);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700627 return;
628 }
629
630 binder->decStrong(nullptr);
631}
632int32_t AIBinder_debugGetRefCount(AIBinder* binder) {
633 if (binder == nullptr) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700634 ALOGE("%s: on null binder", __func__);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700635 return -1;
636 }
637
638 return binder->getStrongCount();
639}
640
Steven Moreland71cddc32018-08-30 23:39:22 -0700641bool AIBinder_associateClass(AIBinder* binder, const AIBinder_Class* clazz) {
642 if (binder == nullptr) {
643 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700644 }
645
Steven Moreland71cddc32018-08-30 23:39:22 -0700646 return binder->associateClass(clazz);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700647}
648
649const AIBinder_Class* AIBinder_getClass(AIBinder* binder) {
650 if (binder == nullptr) {
651 return nullptr;
652 }
653
654 return binder->getClass();
655}
656
657void* AIBinder_getUserData(AIBinder* binder) {
658 if (binder == nullptr) {
659 return nullptr;
660 }
661
662 ABBinder* bBinder = binder->asABBinder();
663 if (bBinder == nullptr) {
664 return nullptr;
665 }
666
667 return bBinder->getUserData();
668}
669
670binder_status_t AIBinder_prepareTransaction(AIBinder* binder, AParcel** in) {
671 if (binder == nullptr || in == nullptr) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700672 ALOGE("%s: requires non-null parameters binder (%p) and in (%p).", __func__, binder, in);
Steven Moreland5d62e442018-09-13 15:01:02 -0700673 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700674 }
675 const AIBinder_Class* clazz = binder->getClass();
676 if (clazz == nullptr) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700677 ALOGE("%s: Class must be defined for a remote binder transaction. See "
678 "AIBinder_associateClass.",
679 __func__);
Steven Moreland5d62e442018-09-13 15:01:02 -0700680 return STATUS_INVALID_OPERATION;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700681 }
682
683 *in = new AParcel(binder);
Steven Moreland1fda67b2021-04-02 18:35:50 +0000684 (*in)->get()->markForBinder(binder->getBinder());
685
Steven Morelandf3ed8062021-08-27 14:31:14 -0700686 status_t status = android::OK;
Steven Morelandfaf25a42022-12-21 02:21:36 +0000687
688 // note - this is the only read of a value in clazz, and it comes with a warning
689 // on the API itself. Do not copy this design. Instead, attach data in a new
690 // version of the prepareTransaction function.
Steven Morelandf3ed8062021-08-27 14:31:14 -0700691 if (clazz->writeHeader) {
692 status = (*in)->get()->writeInterfaceToken(clazz->getInterfaceDescriptor());
693 }
Steven Moreland5d62e442018-09-13 15:01:02 -0700694 binder_status_t ret = PruneStatusT(status);
695
696 if (ret != STATUS_OK) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700697 delete *in;
698 *in = nullptr;
699 }
700
Steven Moreland5d62e442018-09-13 15:01:02 -0700701 return ret;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700702}
703
Steven Moreland9b80e282018-09-19 13:57:23 -0700704static void DestroyParcel(AParcel** parcel) {
705 delete *parcel;
706 *parcel = nullptr;
707}
708
Steven Moreland2e87adc2018-08-20 19:47:00 -0700709binder_status_t AIBinder_transact(AIBinder* binder, transaction_code_t code, AParcel** in,
710 AParcel** out, binder_flags_t flags) {
711 if (in == nullptr) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700712 ALOGE("%s: requires non-null in parameter", __func__);
Steven Moreland5d62e442018-09-13 15:01:02 -0700713 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700714 }
715
Steven Morelandcaa776c2018-09-04 13:48:11 -0700716 using AutoParcelDestroyer = std::unique_ptr<AParcel*, void (*)(AParcel**)>;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700717 // This object is the input to the transaction. This function takes ownership of it and deletes
718 // it.
Steven Moreland9b80e282018-09-19 13:57:23 -0700719 AutoParcelDestroyer forIn(in, DestroyParcel);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700720
721 if (!isUserCommand(code)) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700722 ALOGE("%s: Only user-defined transactions can be made from the NDK, but requested: %d",
723 __func__, code);
Steven Moreland5d62e442018-09-13 15:01:02 -0700724 return STATUS_UNKNOWN_TRANSACTION;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700725 }
726
Steven Morelandf183fdd2020-10-27 00:12:12 +0000727 constexpr binder_flags_t kAllFlags = FLAG_PRIVATE_VENDOR | FLAG_ONEWAY | FLAG_CLEAR_BUF;
Steven Moreland46b5fea2019-10-15 11:22:18 -0700728 if ((flags & ~kAllFlags) != 0) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700729 ALOGE("%s: Unrecognized flags sent: %d", __func__, flags);
Steven Moreland5d62e442018-09-13 15:01:02 -0700730 return STATUS_BAD_VALUE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700731 }
732
733 if (binder == nullptr || *in == nullptr || out == nullptr) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700734 ALOGE("%s: requires non-null parameters binder (%p), in (%p), and out (%p).", __func__,
735 binder, in, out);
Steven Moreland5d62e442018-09-13 15:01:02 -0700736 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700737 }
738
739 if ((*in)->getBinder() != binder) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700740 ALOGE("%s: parcel is associated with binder object %p but called with %p", __func__, binder,
741 (*in)->getBinder());
Steven Moreland5d62e442018-09-13 15:01:02 -0700742 return STATUS_BAD_VALUE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700743 }
744
745 *out = new AParcel(binder);
746
Steven Morelandf18615b2018-09-14 11:43:06 -0700747 status_t status = binder->getBinder()->transact(code, *(*in)->get(), (*out)->get(), flags);
Steven Moreland5d62e442018-09-13 15:01:02 -0700748 binder_status_t ret = PruneStatusT(status);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700749
Steven Moreland5d62e442018-09-13 15:01:02 -0700750 if (ret != STATUS_OK) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700751 delete *out;
752 *out = nullptr;
753 }
754
Steven Moreland5d62e442018-09-13 15:01:02 -0700755 return ret;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700756}
Steven Moreland901c7452018-09-04 16:17:28 -0700757
758AIBinder_DeathRecipient* AIBinder_DeathRecipient_new(
759 AIBinder_DeathRecipient_onBinderDied onBinderDied) {
760 if (onBinderDied == nullptr) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700761 ALOGE("%s: requires non-null onBinderDied parameter.", __func__);
Steven Moreland901c7452018-09-04 16:17:28 -0700762 return nullptr;
763 }
Steven Moreland64127ca2019-03-13 09:25:44 -0700764 auto ret = new AIBinder_DeathRecipient(onBinderDied);
765 ret->incStrong(nullptr);
766 return ret;
Steven Moreland901c7452018-09-04 16:17:28 -0700767}
768
Alice Ryhlea9d9d22021-08-27 07:51:30 +0000769void AIBinder_DeathRecipient_setOnUnlinked(AIBinder_DeathRecipient* recipient,
770 AIBinder_DeathRecipient_onBinderUnlinked onUnlinked) {
771 if (recipient == nullptr) {
772 return;
773 }
774
775 recipient->setOnUnlinked(onUnlinked);
776}
777
Steven Moreland9b80e282018-09-19 13:57:23 -0700778void AIBinder_DeathRecipient_delete(AIBinder_DeathRecipient* recipient) {
Steven Moreland64127ca2019-03-13 09:25:44 -0700779 if (recipient == nullptr) {
780 return;
781 }
782
783 recipient->decStrong(nullptr);
Steven Moreland901c7452018-09-04 16:17:28 -0700784}
Steven Morelandea14ef22019-08-20 10:50:08 -0700785
786binder_status_t AIBinder_getExtension(AIBinder* binder, AIBinder** outExt) {
787 if (binder == nullptr || outExt == nullptr) {
788 if (outExt != nullptr) {
789 *outExt = nullptr;
790 }
791 return STATUS_UNEXPECTED_NULL;
792 }
793
794 sp<IBinder> ext;
795 status_t res = binder->getBinder()->getExtension(&ext);
796
797 if (res != android::OK) {
798 *outExt = nullptr;
799 return PruneStatusT(res);
800 }
801
802 sp<AIBinder> ret = ABpBinder::lookupOrCreateFromBinder(ext);
803 if (ret != nullptr) ret->incStrong(binder);
804
805 *outExt = ret.get();
806 return STATUS_OK;
807}
808
809binder_status_t AIBinder_setExtension(AIBinder* binder, AIBinder* ext) {
810 if (binder == nullptr || ext == nullptr) {
811 return STATUS_UNEXPECTED_NULL;
812 }
813
814 ABBinder* rawBinder = binder->asABBinder();
815 if (rawBinder == nullptr) {
816 return STATUS_INVALID_OPERATION;
817 }
818
819 rawBinder->setExtension(ext->getBinder());
820 return STATUS_OK;
821}
Steven Moreland2f405f52020-07-08 22:24:29 +0000822
823// platform methods follow
824
825void AIBinder_setRequestingSid(AIBinder* binder, bool requestingSid) {
826 ABBinder* localBinder = binder->asABBinder();
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700827 LOG_ALWAYS_FATAL_IF(localBinder == nullptr,
828 "AIBinder_setRequestingSid must be called on a local binder");
Steven Moreland2f405f52020-07-08 22:24:29 +0000829
830 localBinder->setRequestingSid(requestingSid);
831}
832
833const char* AIBinder_getCallingSid() {
834 return ::android::IPCThreadState::self()->getCallingSid();
835}
Steven Morelandb2de9532020-05-29 21:44:41 +0000836
Ady Abraham96174ac2021-10-15 11:02:03 -0700837void AIBinder_setMinSchedulerPolicy(AIBinder* binder, int policy, int priority) {
838 binder->asABBinder()->setMinSchedulerPolicy(policy, priority);
839}
Steven Moreland454a26a2021-12-14 01:26:21 +0000840
841void AIBinder_setInheritRt(AIBinder* binder, bool inheritRt) {
842 ABBinder* localBinder = binder->asABBinder();
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700843 LOG_ALWAYS_FATAL_IF(localBinder == nullptr,
844 "AIBinder_setInheritRt must be called on a local binder");
Steven Moreland454a26a2021-12-14 01:26:21 +0000845
846 localBinder->setInheritRt(inheritRt);
847}