blob: ff31dd0193f75dfaa52c96ab5fc1e7b954cbc7f3 [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>
Pawan Wagh0d6e60f2024-09-26 00:08:10 +000021#include <binder/Functional.h>
Steven Morelandf3034b02018-11-12 17:37:46 -080022#include <binder/IPCThreadState.h>
Ruchir Rastogicc7a7462020-01-31 14:29:15 -080023#include <binder/IResultReceiver.h>
Pawan Wagh0d6e60f2024-09-26 00:08:10 +000024#include <binder/Trace.h>
Tomasz Wasilczykb8ebcca2023-10-09 19:54:20 +000025#if __has_include(<private/android_filesystem_config.h>)
Ruchir Rastogicc7a7462020-01-31 14:29:15 -080026#include <private/android_filesystem_config.h>
Tomasz Wasilczykb8ebcca2023-10-09 19:54:20 +000027#endif
Steven Moreland2e87adc2018-08-20 19:47:00 -070028
Andrei Homescu38eafb72024-03-22 22:38:08 +000029#include "../BuildFlags.h"
Steven Moreland39fdec92022-08-27 00:27:37 +000030#include "ibinder_internal.h"
31#include "parcel_internal.h"
32#include "status_internal.h"
33
Steven Moreland901c7452018-09-04 16:17:28 -070034using DeathRecipient = ::android::IBinder::DeathRecipient;
35
Steven Moreland2e87adc2018-08-20 19:47:00 -070036using ::android::IBinder;
Ruchir Rastogicc7a7462020-01-31 14:29:15 -080037using ::android::IResultReceiver;
Steven Moreland2e87adc2018-08-20 19:47:00 -070038using ::android::Parcel;
39using ::android::sp;
Steven Moreland5d62e442018-09-13 15:01:02 -070040using ::android::status_t;
Steven Moreland009fc1a2022-06-10 17:24:25 +000041using ::android::statusToString;
Steven Moreland2e87adc2018-08-20 19:47:00 -070042using ::android::String16;
Steven Morelanda194c452019-03-04 16:47:07 -080043using ::android::String8;
Steven Moreland2e87adc2018-08-20 19:47:00 -070044using ::android::wp;
Pawan Wagh0d6e60f2024-09-26 00:08:10 +000045using ::android::binder::impl::make_scope_guard;
46using ::android::binder::impl::scope_guard;
47using ::android::binder::os::get_trace_enabled_tags;
48using ::android::binder::os::trace_begin;
49using ::android::binder::os::trace_end;
50
51// transaction codes for getInterfaceHash and getInterfaceVersion are defined
52// in file : system/tools/aidl/aidl.cpp
53static constexpr int kGetInterfaceVersionId = 0x00fffffe;
54static const char* kInterfaceVersion = "getInterfaceVersion";
55static constexpr int kGetInterfaceHashId = 0x00fffffd;
56static const char* kInterfaceHash = "getInterfaceHash";
57static const char* kNdkTrace = "AIDL::ndk::";
58static const char* kServerTrace = "::server";
59static const char* kClientTrace = "::client";
60static const char* kSeparator = "::";
61static const char* kUnknownCode = "Unknown_Transaction_Code:";
Steven Moreland2e87adc2018-08-20 19:47:00 -070062
Steven Moreland71cddc32018-08-30 23:39:22 -070063namespace ABBinderTag {
64
65static const void* kId = "ABBinder";
66static void* kValue = static_cast<void*>(new bool{true});
Andrei Homescu40dbf252024-03-28 21:19:42 +000067void clean(const void* /*id*/, void* /*obj*/, void* /*cookie*/) {
68 /* do nothing */
69}
Steven Moreland71cddc32018-08-30 23:39:22 -070070
71static void attach(const sp<IBinder>& binder) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -070072 auto alreadyAttached = binder->attachObject(kId, kValue, nullptr /*cookie*/, clean);
73 LOG_ALWAYS_FATAL_IF(alreadyAttached != nullptr, "can only attach once");
Steven Moreland71cddc32018-08-30 23:39:22 -070074}
75static bool has(const sp<IBinder>& binder) {
76 return binder != nullptr && binder->findObject(kId) == kValue;
77}
78
Steven Moreland6cf66ac2018-11-02 18:14:54 -070079} // namespace ABBinderTag
Steven Moreland71cddc32018-08-30 23:39:22 -070080
Steven Moreland94968952018-09-05 14:42:59 -070081namespace ABpBinderTag {
82
Steven Moreland94968952018-09-05 14:42:59 -070083static const void* kId = "ABpBinder";
84struct Value {
85 wp<ABpBinder> binder;
86};
87void clean(const void* id, void* obj, void* cookie) {
Steven Moreland3acd4902022-07-26 18:29:58 +000088 // be weary of leaks!
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -070089 // ALOGI("Deleting an ABpBinder");
Steven Moreland3acd4902022-07-26 18:29:58 +000090
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -070091 LOG_ALWAYS_FATAL_IF(id != kId, "%p %p %p", id, obj, cookie);
Steven Moreland94968952018-09-05 14:42:59 -070092
93 delete static_cast<Value*>(obj);
Andrei Homescu40dbf252024-03-28 21:19:42 +000094}
Steven Moreland94968952018-09-05 14:42:59 -070095
Steven Moreland6cf66ac2018-11-02 18:14:54 -070096} // namespace ABpBinderTag
Steven Moreland94968952018-09-05 14:42:59 -070097
Steven Moreland2e87adc2018-08-20 19:47:00 -070098AIBinder::AIBinder(const AIBinder_Class* clazz) : mClazz(clazz) {}
99AIBinder::~AIBinder() {}
100
Steven Moreland94a76792022-12-21 01:20:06 +0000101// b/175635923 libcxx causes "implicit-conversion" with a string with invalid char
102static std::string SanitizeString(const String16& str) {
103 std::string sanitized{String8(str)};
104 for (auto& c : sanitized) {
105 if (!isprint(c)) {
106 c = '?';
107 }
108 }
109 return sanitized;
110}
111
Pawan Wagh0d6e60f2024-09-26 00:08:10 +0000112const std::string getMethodName(const AIBinder_Class* clazz, transaction_code_t code) {
113 // TODO(b/150155678) - Move getInterfaceHash and getInterfaceVersion to libbinder and remove
114 // hardcoded cases.
115 if (code <= clazz->getTransactionCodeToFunctionLength() && code >= FIRST_CALL_TRANSACTION) {
116 // Codes have FIRST_CALL_TRANSACTION as added offset. Subtract to access function name
117 return clazz->getFunctionName(code);
118 } else if (code == kGetInterfaceVersionId) {
119 return kInterfaceVersion;
120 } else if (code == kGetInterfaceHashId) {
121 return kInterfaceHash;
122 }
123 return kUnknownCode + std::to_string(code);
124}
125
126const std::string getTraceSectionName(const AIBinder_Class* clazz, transaction_code_t code,
127 bool isServer) {
128 if (clazz == nullptr) {
129 ALOGE("class associated with binder is null. Class is needed to add trace with interface "
130 "name and function name");
131 return kNdkTrace;
132 }
133
134 const std::string descriptor = clazz->getInterfaceDescriptorUtf8();
135 const std::string methodName = getMethodName(clazz, code);
136
137 size_t traceSize =
138 strlen(kNdkTrace) + descriptor.size() + strlen(kSeparator) + methodName.size();
139 traceSize += isServer ? strlen(kServerTrace) : strlen(kClientTrace);
140
141 std::string trace;
142 // reserve to avoid repeated allocations
143 trace.reserve(traceSize);
144
145 trace += kNdkTrace;
146 trace += clazz->getInterfaceDescriptorUtf8();
147 trace += kSeparator;
148 trace += methodName;
149 trace += isServer ? kServerTrace : kClientTrace;
150
151 LOG_ALWAYS_FATAL_IF(trace.size() != traceSize, "Trace size mismatch. Expected %zu, got %zu",
152 traceSize, trace.size());
153
154 return trace;
155}
156
Steven Moreland94a76792022-12-21 01:20:06 +0000157bool AIBinder::associateClass(const AIBinder_Class* clazz) {
158 if (clazz == nullptr) return false;
159
160 // If mClazz is non-null, this must have been called and cached
161 // already. So, we can safely call this first. Due to the implementation
162 // of getInterfaceDescriptor (at time of writing), two simultaneous calls
163 // may lead to extra binder transactions, but this is expected to be
164 // exceedingly rare. Once we have a binder, when we get it again later,
165 // we won't make another binder transaction here.
166 const String16& descriptor = getBinder()->getInterfaceDescriptor();
167 const String16& newDescriptor = clazz->getInterfaceDescriptor();
168
Andrei Homescu9556bb12020-09-21 16:59:45 -0700169 std::lock_guard<std::mutex> lock(mClazzMutex);
Steven Moreland71cddc32018-08-30 23:39:22 -0700170 if (mClazz == clazz) return true;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700171
Steven Morelandfaf25a42022-12-21 02:21:36 +0000172 // If this is an ABpBinder, the first class object becomes the canonical one. The implication
173 // of this is that no API can require a proxy information to get information on how to behave.
174 // from the class itself - which should only store the interface descriptor. The functionality
175 // should be implemented by adding AIBinder_* APIs to set values on binders themselves, by
176 // setting things on AIBinder_Class which get transferred along with the binder, so that they
177 // can be read along with the BpBinder, or by modifying APIs directly (e.g. an option in
178 // onTransact).
179 //
180 // While this check is required to support linkernamespaces, one downside of it is that
181 // you may parcel code to communicate between things in the same process. However, comms
182 // between linkernamespaces like this already happen for cross-language calls like Java<->C++
183 // or Rust<->Java, and there are good stability guarantees here. This interacts with
184 // binder Stability checks exactly like any other in-process call. The stability is known
185 // to the IBinder object, so that it doesn't matter if a class object comes from
186 // a different stability level.
187 if (mClazz != nullptr && !asABpBinder()) {
Steven Moreland63872b52020-10-02 19:28:23 +0000188 const String16& currentDescriptor = mClazz->getInterfaceDescriptor();
Steven Moreland2e87adc2018-08-20 19:47:00 -0700189 if (newDescriptor == currentDescriptor) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700190 ALOGE("Class descriptors '%s' match during associateClass, but they are different class"
191 " objects (%p vs %p). Class descriptor collision?",
192 String8(currentDescriptor).c_str(), clazz, mClazz);
Steven Moreland71cddc32018-08-30 23:39:22 -0700193 } else {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700194 ALOGE("%s: Class cannot be associated on object which already has a class. "
195 "Trying to associate to '%s' but already set to '%s'.",
196 __func__, String8(newDescriptor).c_str(), String8(currentDescriptor).c_str());
Steven Moreland2e87adc2018-08-20 19:47:00 -0700197 }
198
Steven Moreland71cddc32018-08-30 23:39:22 -0700199 // always a failure because we know mClazz != clazz
200 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700201 }
202
Steven Moreland94a76792022-12-21 01:20:06 +0000203 // This will always be an O(n) comparison, but it's expected to be extremely rare.
204 // since it's an error condition. Do the comparison after we take the lock and
205 // check the pointer equality fast path. By always taking the lock, it's also
206 // more flake-proof. However, the check is not dependent on the lock.
Steven Moreland418914a2023-06-28 21:47:14 +0000207 if (descriptor != newDescriptor && !(asABpBinder() && asABpBinder()->isServiceFuzzing())) {
Steven Moreland640c4092020-07-08 00:19:58 +0000208 if (getBinder()->isBinderAlive()) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700209 ALOGE("%s: Expecting binder to have class '%s' but descriptor is actually '%s'.",
210 __func__, String8(newDescriptor).c_str(), SanitizeString(descriptor).c_str());
Steven Moreland640c4092020-07-08 00:19:58 +0000211 } else {
212 // b/155793159
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700213 ALOGE("%s: Cannot associate class '%s' to dead binder with cached descriptor '%s'.",
214 __func__, String8(newDescriptor).c_str(), SanitizeString(descriptor).c_str());
Steven Moreland640c4092020-07-08 00:19:58 +0000215 }
Steven Moreland71cddc32018-08-30 23:39:22 -0700216 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700217 }
218
Steven Morelandfaf25a42022-12-21 02:21:36 +0000219 // A local binder being set for the first time OR
220 // ignoring a proxy binder which is set multiple time, by considering the first
221 // associated class as the canonical one.
222 if (mClazz == nullptr) {
223 mClazz = clazz;
224 }
225
Steven Moreland94a76792022-12-21 01:20:06 +0000226 return true;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700227}
228
229ABBinder::ABBinder(const AIBinder_Class* clazz, void* userData)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700230 : AIBinder(clazz), BBinder(), mUserData(userData) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700231 LOG_ALWAYS_FATAL_IF(clazz == nullptr, "clazz == nullptr");
Steven Moreland2e87adc2018-08-20 19:47:00 -0700232}
233ABBinder::~ABBinder() {
234 getClass()->onDestroy(mUserData);
235}
236
237const String16& ABBinder::getInterfaceDescriptor() const {
238 return getClass()->getInterfaceDescriptor();
239}
240
Steven Morelanda194c452019-03-04 16:47:07 -0800241status_t ABBinder::dump(int fd, const ::android::Vector<String16>& args) {
242 AIBinder_onDump onDump = getClass()->onDump;
243
244 if (onDump == nullptr) {
245 return STATUS_OK;
246 }
247
248 // technically UINT32_MAX would be okay here, but INT32_MAX is expected since this may be
249 // null in Java
250 if (args.size() > INT32_MAX) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700251 ALOGE("ABBinder::dump received too many arguments: %zu", args.size());
Steven Morelanda194c452019-03-04 16:47:07 -0800252 return STATUS_BAD_VALUE;
253 }
254
255 std::vector<String8> utf8Args; // owns memory of utf8s
256 utf8Args.reserve(args.size());
257 std::vector<const char*> utf8Pointers; // what can be passed over NDK API
258 utf8Pointers.reserve(args.size());
259
260 for (size_t i = 0; i < args.size(); i++) {
261 utf8Args.push_back(String8(args[i]));
262 utf8Pointers.push_back(utf8Args[i].c_str());
263 }
264
265 return onDump(this, fd, utf8Pointers.data(), utf8Pointers.size());
266}
267
Steven Moreland5d62e442018-09-13 15:01:02 -0700268status_t ABBinder::onTransact(transaction_code_t code, const Parcel& data, Parcel* reply,
269 binder_flags_t flags) {
Pawan Wagh0d6e60f2024-09-26 00:08:10 +0000270 std::string sectionName;
271 bool tracingEnabled = get_trace_enabled_tags() & ATRACE_TAG_AIDL;
272 if (tracingEnabled) {
273 sectionName = getTraceSectionName(getClass(), code, true /*isServer*/);
274 trace_begin(ATRACE_TAG_AIDL, sectionName.c_str());
275 }
276
277 scope_guard guard = make_scope_guard([&]() {
278 if (tracingEnabled) trace_end(ATRACE_TAG_AIDL);
279 });
280
Steven Moreland2e87adc2018-08-20 19:47:00 -0700281 if (isUserCommand(code)) {
Steven Morelandf3ed8062021-08-27 14:31:14 -0700282 if (getClass()->writeHeader && !data.checkInterface(this)) {
Steven Moreland9d089af2018-09-18 08:58:09 -0700283 return STATUS_BAD_TYPE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700284 }
285
286 const AParcel in = AParcel::readOnly(this, &data);
287 AParcel out = AParcel(this, reply, false /*owns*/);
288
Steven Moreland5d62e442018-09-13 15:01:02 -0700289 binder_status_t status = getClass()->onTransact(this, code, &in, &out);
290 return PruneStatusT(status);
Steven Moreland88234072020-08-06 19:32:45 +0000291 } else if (code == SHELL_COMMAND_TRANSACTION && getClass()->handleShellCommand != nullptr) {
Andrei Homescu38eafb72024-03-22 22:38:08 +0000292 if constexpr (!android::kEnableKernelIpc) {
293 // Non-IPC builds do not have getCallingUid(),
294 // so we have no way of authenticating the caller
295 return STATUS_PERMISSION_DENIED;
296 }
297
Ruchir Rastogicc7a7462020-01-31 14:29:15 -0800298 int in = data.readFileDescriptor();
299 int out = data.readFileDescriptor();
300 int err = data.readFileDescriptor();
301
302 int argc = data.readInt32();
303 std::vector<String8> utf8Args; // owns memory of utf8s
304 std::vector<const char*> utf8Pointers; // what can be passed over NDK API
305 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
306 utf8Args.push_back(String8(data.readString16()));
307 utf8Pointers.push_back(utf8Args[i].c_str());
308 }
309
310 data.readStrongBinder(); // skip over the IShellCallback
311 sp<IResultReceiver> resultReceiver = IResultReceiver::asInterface(data.readStrongBinder());
312
313 // Shell commands should only be callable by ADB.
314 uid_t uid = AIBinder_getCallingUid();
Tomasz Wasilczykb8ebcca2023-10-09 19:54:20 +0000315 if (uid != 0 /* root */
316#ifdef AID_SHELL
317 && uid != AID_SHELL
318#endif
319 ) {
Ruchir Rastogicc7a7462020-01-31 14:29:15 -0800320 if (resultReceiver != nullptr) {
321 resultReceiver->send(-1);
322 }
323 return STATUS_PERMISSION_DENIED;
324 }
325
326 // Check that the file descriptors are valid.
327 if (in == STATUS_BAD_TYPE || out == STATUS_BAD_TYPE || err == STATUS_BAD_TYPE) {
328 if (resultReceiver != nullptr) {
329 resultReceiver->send(-1);
330 }
331 return STATUS_BAD_VALUE;
332 }
333
334 binder_status_t status = getClass()->handleShellCommand(
335 this, in, out, err, utf8Pointers.data(), utf8Pointers.size());
336 if (resultReceiver != nullptr) {
337 resultReceiver->send(status);
338 }
339 return status;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700340 } else {
341 return BBinder::onTransact(code, data, reply, flags);
342 }
343}
344
Devin Moore9f9d0e12024-02-29 22:07:12 +0000345void ABBinder::addDeathRecipient(const ::android::sp<AIBinder_DeathRecipient>& /* recipient */,
346 void* /* cookie */) {
347 LOG_ALWAYS_FATAL("Should not reach this. Can't linkToDeath local binders.");
348}
349
Steven Moreland71cddc32018-08-30 23:39:22 -0700350ABpBinder::ABpBinder(const ::android::sp<::android::IBinder>& binder)
Steven Moreland3acd4902022-07-26 18:29:58 +0000351 : AIBinder(nullptr /*clazz*/), mRemote(binder) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700352 LOG_ALWAYS_FATAL_IF(binder == nullptr, "binder == nullptr");
Steven Moreland2e87adc2018-08-20 19:47:00 -0700353}
Devin Moore9f9d0e12024-02-29 22:07:12 +0000354
355ABpBinder::~ABpBinder() {
356 for (auto& recip : mDeathRecipients) {
357 sp<AIBinder_DeathRecipient> strongRecip = recip.recipient.promote();
358 if (strongRecip) {
359 strongRecip->pruneThisTransferEntry(getBinder(), recip.cookie);
360 }
361 }
362}
Steven Moreland2e87adc2018-08-20 19:47:00 -0700363
Steven Moreland94968952018-09-05 14:42:59 -0700364sp<AIBinder> ABpBinder::lookupOrCreateFromBinder(const ::android::sp<::android::IBinder>& binder) {
Steven Moreland71cddc32018-08-30 23:39:22 -0700365 if (binder == nullptr) {
366 return nullptr;
367 }
368 if (ABBinderTag::has(binder)) {
369 return static_cast<ABBinder*>(binder.get());
370 }
Steven Moreland94968952018-09-05 14:42:59 -0700371
Steven Moreland91a96b62021-06-30 23:07:13 +0000372 // The following code ensures that for a given binder object (remote or local), if it is not an
373 // ABBinder then at most one ABpBinder object exists in a given process representing it.
374
Steven Moreland1f1bed62021-06-25 21:50:50 +0000375 auto* value = static_cast<ABpBinderTag::Value*>(binder->findObject(ABpBinderTag::kId));
Steven Moreland94968952018-09-05 14:42:59 -0700376 if (value == nullptr) {
377 value = new ABpBinderTag::Value;
Steven Moreland1f1bed62021-06-25 21:50:50 +0000378 auto oldValue = static_cast<ABpBinderTag::Value*>(
379 binder->attachObject(ABpBinderTag::kId, static_cast<void*>(value),
380 nullptr /*cookie*/, ABpBinderTag::clean));
381
382 // allocated by another thread
383 if (oldValue) {
384 delete value;
385 value = oldValue;
386 }
Steven Moreland94968952018-09-05 14:42:59 -0700387 }
388
Steven Moreland1f1bed62021-06-25 21:50:50 +0000389 sp<ABpBinder> ret;
390 binder->withLock([&]() {
391 ret = value->binder.promote();
392 if (ret == nullptr) {
393 ret = sp<ABpBinder>::make(binder);
394 value->binder = ret;
395 }
396 });
Steven Moreland94968952018-09-05 14:42:59 -0700397
398 return ret;
Steven Moreland71cddc32018-08-30 23:39:22 -0700399}
400
Devin Moore9f9d0e12024-02-29 22:07:12 +0000401void ABpBinder::addDeathRecipient(const ::android::sp<AIBinder_DeathRecipient>& recipient,
402 void* cookie) {
403 std::lock_guard<std::mutex> l(mDeathRecipientsMutex);
404 mDeathRecipients.emplace_back(recipient, cookie);
405}
406
Steven Moreland2e87adc2018-08-20 19:47:00 -0700407struct AIBinder_Weak {
408 wp<AIBinder> binder;
409};
410AIBinder_Weak* AIBinder_Weak_new(AIBinder* binder) {
Steven Moreland5ccb70f2018-09-04 16:30:21 -0700411 if (binder == nullptr) {
412 return nullptr;
413 }
414
Steven Moreland2e87adc2018-08-20 19:47:00 -0700415 return new AIBinder_Weak{wp<AIBinder>(binder)};
416}
Steven Moreland9b80e282018-09-19 13:57:23 -0700417void AIBinder_Weak_delete(AIBinder_Weak* weakBinder) {
418 delete weakBinder;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700419}
420AIBinder* AIBinder_Weak_promote(AIBinder_Weak* weakBinder) {
Steven Moreland5ccb70f2018-09-04 16:30:21 -0700421 if (weakBinder == nullptr) {
422 return nullptr;
423 }
424
Steven Moreland2e87adc2018-08-20 19:47:00 -0700425 sp<AIBinder> binder = weakBinder->binder.promote();
426 AIBinder_incStrong(binder.get());
427 return binder.get();
428}
429
Steven Morelandc70e0122021-01-08 02:44:53 +0000430AIBinder_Weak* AIBinder_Weak_clone(const AIBinder_Weak* weak) {
431 if (weak == nullptr) {
432 return nullptr;
433 }
434
435 return new AIBinder_Weak{weak->binder};
436}
437
438bool AIBinder_lt(const AIBinder* lhs, const AIBinder* rhs) {
439 if (lhs == nullptr || rhs == nullptr) return lhs < rhs;
440
441 return const_cast<AIBinder*>(lhs)->getBinder() < const_cast<AIBinder*>(rhs)->getBinder();
442}
443
444bool AIBinder_Weak_lt(const AIBinder_Weak* lhs, const AIBinder_Weak* rhs) {
445 if (lhs == nullptr || rhs == nullptr) return lhs < rhs;
446
447 return lhs->binder < rhs->binder;
448}
449
Steven Morelandfaf25a42022-12-21 02:21:36 +0000450// WARNING: When multiple classes exist with the same interface descriptor in different
451// linkernamespaces, the first one to be associated with mClazz becomes the canonical one
452// and the only requirement on this is that the interface descriptors match. If this
453// is an ABpBinder, no other state can be referenced from mClazz.
Steven Moreland2e87adc2018-08-20 19:47:00 -0700454AIBinder_Class::AIBinder_Class(const char* interfaceDescriptor, AIBinder_Class_onCreate onCreate,
455 AIBinder_Class_onDestroy onDestroy,
456 AIBinder_Class_onTransact onTransact)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700457 : onCreate(onCreate),
458 onDestroy(onDestroy),
459 onTransact(onTransact),
Stephen Crane8fde87f2020-11-17 15:06:11 -0800460 mInterfaceDescriptor(interfaceDescriptor),
461 mWideInterfaceDescriptor(interfaceDescriptor) {}
Steven Moreland2e87adc2018-08-20 19:47:00 -0700462
Pawan Wagh0d6e60f2024-09-26 00:08:10 +0000463bool AIBinder_Class::setTransactionCodeMap(const char** transactionCodeMap, size_t length) {
464 if (mTransactionCodeToFunction != nullptr) {
465 ALOGE("mTransactionCodeToFunction is already set!");
466 return false;
467 }
468 mTransactionCodeToFunction = transactionCodeMap;
469 mTransactionCodeToFunctionLength = length;
470 return true;
471}
472
473const char* AIBinder_Class::getFunctionName(transaction_code_t code) const {
474 if (mTransactionCodeToFunction == nullptr) {
475 ALOGE("mTransactionCodeToFunction is not set!");
476 return nullptr;
477 }
478
479 if (code < FIRST_CALL_TRANSACTION ||
480 code - FIRST_CALL_TRANSACTION >= mTransactionCodeToFunctionLength) {
481 ALOGE("Function name for requested code not found!");
482 return nullptr;
483 }
484
485 return mTransactionCodeToFunction[code - FIRST_CALL_TRANSACTION];
486}
487
Steven Moreland2e87adc2018-08-20 19:47:00 -0700488AIBinder_Class* AIBinder_Class_define(const char* interfaceDescriptor,
489 AIBinder_Class_onCreate onCreate,
490 AIBinder_Class_onDestroy onDestroy,
491 AIBinder_Class_onTransact onTransact) {
492 if (interfaceDescriptor == nullptr || onCreate == nullptr || onDestroy == nullptr ||
493 onTransact == nullptr) {
494 return nullptr;
495 }
496
497 return new AIBinder_Class(interfaceDescriptor, onCreate, onDestroy, onTransact);
498}
499
Steven Morelanda194c452019-03-04 16:47:07 -0800500void AIBinder_Class_setOnDump(AIBinder_Class* clazz, AIBinder_onDump onDump) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700501 LOG_ALWAYS_FATAL_IF(clazz == nullptr, "setOnDump requires non-null clazz");
Steven Morelanda194c452019-03-04 16:47:07 -0800502
503 // this is required to be called before instances are instantiated
504 clazz->onDump = onDump;
505}
506
Pawan Wagh0d6e60f2024-09-26 00:08:10 +0000507void AIBinder_Class_setTransactionCodeToFunctionNameMap(AIBinder_Class* clazz,
508 const char** transactionCodeToFunction,
509 size_t length) {
510 LOG_ALWAYS_FATAL_IF(clazz == nullptr || transactionCodeToFunction == nullptr,
511 "Valid clazz and transactionCodeToFunction are needed to set code to "
512 "function mapping.");
513 LOG_ALWAYS_FATAL_IF(!clazz->setTransactionCodeMap(transactionCodeToFunction, length),
514 "Failed to set transactionCodeToFunction to clazz! Is "
515 "transactionCodeToFunction already set?");
516}
517
518const char* AIBinder_Class_getFunctionName(AIBinder_Class* clazz, transaction_code_t code) {
519 LOG_ALWAYS_FATAL_IF(
520 clazz == nullptr,
521 "Valid clazz is needed to get function name for requested transaction code");
522 return clazz->getFunctionName(code);
523}
524
Steven Morelandf3ed8062021-08-27 14:31:14 -0700525void AIBinder_Class_disableInterfaceTokenHeader(AIBinder_Class* clazz) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700526 LOG_ALWAYS_FATAL_IF(clazz == nullptr, "disableInterfaceTokenHeader requires non-null clazz");
Steven Morelandf3ed8062021-08-27 14:31:14 -0700527
528 clazz->writeHeader = false;
529}
530
Ruchir Rastogicc7a7462020-01-31 14:29:15 -0800531void AIBinder_Class_setHandleShellCommand(AIBinder_Class* clazz,
532 AIBinder_handleShellCommand handleShellCommand) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700533 LOG_ALWAYS_FATAL_IF(clazz == nullptr, "setHandleShellCommand requires non-null clazz");
Ruchir Rastogicc7a7462020-01-31 14:29:15 -0800534
535 clazz->handleShellCommand = handleShellCommand;
536}
537
Stephen Crane8fde87f2020-11-17 15:06:11 -0800538const char* AIBinder_Class_getDescriptor(const AIBinder_Class* clazz) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700539 LOG_ALWAYS_FATAL_IF(clazz == nullptr, "getDescriptor requires non-null clazz");
Stephen Crane8fde87f2020-11-17 15:06:11 -0800540
541 return clazz->getInterfaceDescriptorUtf8();
542}
543
Alice Ryhlea9d9d22021-08-27 07:51:30 +0000544AIBinder_DeathRecipient::TransferDeathRecipient::~TransferDeathRecipient() {
545 if (mOnUnlinked != nullptr) {
546 mOnUnlinked(mCookie);
547 }
548}
549
Steven Moreland901c7452018-09-04 16:17:28 -0700550void AIBinder_DeathRecipient::TransferDeathRecipient::binderDied(const wp<IBinder>& who) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700551 LOG_ALWAYS_FATAL_IF(who != mWho, "%p (%p) vs %p (%p)", who.unsafe_get(), who.get_refs(),
552 mWho.unsafe_get(), mWho.get_refs());
Steven Moreland901c7452018-09-04 16:17:28 -0700553
554 mOnDied(mCookie);
Steven Moreland64127ca2019-03-13 09:25:44 -0700555
556 sp<AIBinder_DeathRecipient> recipient = mParentRecipient.promote();
557 sp<IBinder> strongWho = who.promote();
558
559 // otherwise this will be cleaned up later with pruneDeadTransferEntriesLocked
560 if (recipient != nullptr && strongWho != nullptr) {
561 status_t result = recipient->unlinkToDeath(strongWho, mCookie);
562 if (result != ::android::DEAD_OBJECT) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700563 ALOGW("Unlinking to dead binder resulted in: %d", result);
Steven Moreland64127ca2019-03-13 09:25:44 -0700564 }
565 }
566
Steven Moreland901c7452018-09-04 16:17:28 -0700567 mWho = nullptr;
568}
569
570AIBinder_DeathRecipient::AIBinder_DeathRecipient(AIBinder_DeathRecipient_onBinderDied onDied)
Alice Ryhlea9d9d22021-08-27 07:51:30 +0000571 : mOnDied(onDied), mOnUnlinked(nullptr) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700572 LOG_ALWAYS_FATAL_IF(onDied == nullptr, "onDied == nullptr");
Steven Moreland901c7452018-09-04 16:17:28 -0700573}
574
Devin Moore9f9d0e12024-02-29 22:07:12 +0000575void AIBinder_DeathRecipient::pruneThisTransferEntry(const sp<IBinder>& who, void* cookie) {
576 std::lock_guard<std::mutex> l(mDeathRecipientsMutex);
577 mDeathRecipients.erase(std::remove_if(mDeathRecipients.begin(), mDeathRecipients.end(),
578 [&](const sp<TransferDeathRecipient>& tdr) {
579 auto tdrWho = tdr->getWho();
580 return tdrWho != nullptr && tdrWho.promote() == who &&
581 cookie == tdr->getCookie();
582 }),
583 mDeathRecipients.end());
584}
585
Steven Moreland64127ca2019-03-13 09:25:44 -0700586void AIBinder_DeathRecipient::pruneDeadTransferEntriesLocked() {
587 mDeathRecipients.erase(std::remove_if(mDeathRecipients.begin(), mDeathRecipients.end(),
588 [](const sp<TransferDeathRecipient>& tdr) {
589 return tdr->getWho() == nullptr;
590 }),
591 mDeathRecipients.end());
592}
593
Jiyong Park4f97a8c2020-11-16 18:11:14 +0900594binder_status_t AIBinder_DeathRecipient::linkToDeath(const sp<IBinder>& binder, void* cookie) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700595 LOG_ALWAYS_FATAL_IF(binder == nullptr, "binder == nullptr");
Steven Moreland901c7452018-09-04 16:17:28 -0700596
597 std::lock_guard<std::mutex> l(mDeathRecipientsMutex);
598
Devin Mooref93b3102024-04-12 21:48:35 +0000599 if (mOnUnlinked && cookie &&
600 std::find_if(mDeathRecipients.begin(), mDeathRecipients.end(),
601 [&cookie](android::sp<TransferDeathRecipient> recipient) {
602 return recipient->getCookie() == cookie;
603 }) != mDeathRecipients.end()) {
604 ALOGE("Attempting to AIBinder_linkToDeath with the same cookie with an onUnlink callback. "
605 "This will cause the onUnlinked callback to be called multiple times with the same "
606 "cookie, which is usually not intended.");
607 }
Devin Moore50c3d702024-04-16 23:52:13 +0000608 if (!mOnUnlinked && cookie) {
609 ALOGW("AIBinder_linkToDeath is being called with a non-null cookie and no onUnlink "
610 "callback set. This might not be intended. AIBinder_DeathRecipient_setOnUnlinked "
611 "should be called first.");
612 }
Devin Mooref93b3102024-04-12 21:48:35 +0000613
Steven Moreland901c7452018-09-04 16:17:28 -0700614 sp<TransferDeathRecipient> recipient =
Alice Ryhlea9d9d22021-08-27 07:51:30 +0000615 new TransferDeathRecipient(binder, cookie, this, mOnDied, mOnUnlinked);
Steven Moreland901c7452018-09-04 16:17:28 -0700616
Steven Moreland64127ca2019-03-13 09:25:44 -0700617 status_t status = binder->linkToDeath(recipient, cookie, 0 /*flags*/);
Steven Moreland5d62e442018-09-13 15:01:02 -0700618 if (status != STATUS_OK) {
Alice Ryhlea9d9d22021-08-27 07:51:30 +0000619 // When we failed to link, the destructor of TransferDeathRecipient runs here, which
620 // ensures that mOnUnlinked is called before we return with an error from this method.
Steven Moreland5d62e442018-09-13 15:01:02 -0700621 return PruneStatusT(status);
Steven Moreland901c7452018-09-04 16:17:28 -0700622 }
623
624 mDeathRecipients.push_back(recipient);
Steven Moreland64127ca2019-03-13 09:25:44 -0700625
626 pruneDeadTransferEntriesLocked();
Steven Moreland5d62e442018-09-13 15:01:02 -0700627 return STATUS_OK;
Steven Moreland901c7452018-09-04 16:17:28 -0700628}
629
Jiyong Park4f97a8c2020-11-16 18:11:14 +0900630binder_status_t AIBinder_DeathRecipient::unlinkToDeath(const sp<IBinder>& binder, void* cookie) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700631 LOG_ALWAYS_FATAL_IF(binder == nullptr, "binder == nullptr");
Steven Moreland901c7452018-09-04 16:17:28 -0700632
633 std::lock_guard<std::mutex> l(mDeathRecipientsMutex);
634
635 for (auto it = mDeathRecipients.rbegin(); it != mDeathRecipients.rend(); ++it) {
636 sp<TransferDeathRecipient> recipient = *it;
637
Steven Moreland64127ca2019-03-13 09:25:44 -0700638 if (recipient->getCookie() == cookie && recipient->getWho() == binder) {
Steven Moreland901c7452018-09-04 16:17:28 -0700639 mDeathRecipients.erase(it.base() - 1);
640
Steven Moreland64127ca2019-03-13 09:25:44 -0700641 status_t status = binder->unlinkToDeath(recipient, cookie, 0 /*flags*/);
Steven Moreland5d62e442018-09-13 15:01:02 -0700642 if (status != ::android::OK) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700643 ALOGE("%s: removed reference to death recipient but unlink failed: %s", __func__,
644 statusToString(status).c_str());
Steven Moreland901c7452018-09-04 16:17:28 -0700645 }
Steven Moreland5d62e442018-09-13 15:01:02 -0700646 return PruneStatusT(status);
Steven Moreland901c7452018-09-04 16:17:28 -0700647 }
648 }
649
Steven Moreland5d62e442018-09-13 15:01:02 -0700650 return STATUS_NAME_NOT_FOUND;
Steven Moreland901c7452018-09-04 16:17:28 -0700651}
652
Alice Ryhlea9d9d22021-08-27 07:51:30 +0000653void AIBinder_DeathRecipient::setOnUnlinked(AIBinder_DeathRecipient_onBinderUnlinked onUnlinked) {
654 mOnUnlinked = onUnlinked;
655}
656
Steven Moreland901c7452018-09-04 16:17:28 -0700657// start of C-API methods
658
Steven Moreland2e87adc2018-08-20 19:47:00 -0700659AIBinder* AIBinder_new(const AIBinder_Class* clazz, void* args) {
660 if (clazz == nullptr) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700661 ALOGE("%s: Must provide class to construct local binder.", __func__);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700662 return nullptr;
663 }
664
665 void* userData = clazz->onCreate(args);
666
Steven Moreland71cddc32018-08-30 23:39:22 -0700667 sp<AIBinder> ret = new ABBinder(clazz, userData);
668 ABBinderTag::attach(ret->getBinder());
669
670 AIBinder_incStrong(ret.get());
671 return ret.get();
Steven Moreland2e87adc2018-08-20 19:47:00 -0700672}
673
Steven Moreland5ea54da2018-09-04 13:29:55 -0700674bool AIBinder_isRemote(const AIBinder* binder) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700675 if (binder == nullptr) {
Steven Moreland56f752a2018-11-05 17:23:37 -0800676 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700677 }
678
679 return binder->isRemote();
680}
681
Steven Moreland65867d72018-09-04 14:22:26 -0700682bool AIBinder_isAlive(const AIBinder* binder) {
683 if (binder == nullptr) {
684 return false;
685 }
686
687 return const_cast<AIBinder*>(binder)->getBinder()->isBinderAlive();
688}
689
690binder_status_t AIBinder_ping(AIBinder* binder) {
691 if (binder == nullptr) {
Steven Moreland5d62e442018-09-13 15:01:02 -0700692 return STATUS_UNEXPECTED_NULL;
Steven Moreland65867d72018-09-04 14:22:26 -0700693 }
694
Steven Moreland5d62e442018-09-13 15:01:02 -0700695 return PruneStatusT(binder->getBinder()->pingBinder());
Steven Moreland65867d72018-09-04 14:22:26 -0700696}
697
Steven Morelanda194c452019-03-04 16:47:07 -0800698binder_status_t AIBinder_dump(AIBinder* binder, int fd, const char** args, uint32_t numArgs) {
699 if (binder == nullptr) {
700 return STATUS_UNEXPECTED_NULL;
701 }
702
703 ABBinder* bBinder = binder->asABBinder();
704 if (bBinder != nullptr) {
705 AIBinder_onDump onDump = binder->getClass()->onDump;
706 if (onDump == nullptr) {
707 return STATUS_OK;
708 }
709 return PruneStatusT(onDump(bBinder, fd, args, numArgs));
710 }
711
712 ::android::Vector<String16> utf16Args;
713 utf16Args.setCapacity(numArgs);
714 for (uint32_t i = 0; i < numArgs; i++) {
715 utf16Args.push(String16(String8(args[i])));
716 }
717
718 status_t status = binder->getBinder()->dump(fd, utf16Args);
719 return PruneStatusT(status);
720}
721
Steven Moreland901c7452018-09-04 16:17:28 -0700722binder_status_t AIBinder_linkToDeath(AIBinder* binder, AIBinder_DeathRecipient* recipient,
723 void* cookie) {
724 if (binder == nullptr || recipient == nullptr) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700725 ALOGE("%s: Must provide binder (%p) and recipient (%p)", __func__, binder, recipient);
Steven Moreland5d62e442018-09-13 15:01:02 -0700726 return STATUS_UNEXPECTED_NULL;
Steven Moreland901c7452018-09-04 16:17:28 -0700727 }
728
Devin Moore9f9d0e12024-02-29 22:07:12 +0000729 binder_status_t ret = recipient->linkToDeath(binder->getBinder(), cookie);
730 if (ret == STATUS_OK) {
731 binder->addDeathRecipient(recipient, cookie);
732 }
733 return ret;
Steven Moreland901c7452018-09-04 16:17:28 -0700734}
735
736binder_status_t AIBinder_unlinkToDeath(AIBinder* binder, AIBinder_DeathRecipient* recipient,
737 void* cookie) {
738 if (binder == nullptr || recipient == nullptr) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700739 ALOGE("%s: Must provide binder (%p) and recipient (%p)", __func__, binder, recipient);
Steven Moreland5d62e442018-09-13 15:01:02 -0700740 return STATUS_UNEXPECTED_NULL;
Steven Moreland901c7452018-09-04 16:17:28 -0700741 }
742
Steven Moreland5d62e442018-09-13 15:01:02 -0700743 // returns binder_status_t
Steven Moreland64127ca2019-03-13 09:25:44 -0700744 return recipient->unlinkToDeath(binder->getBinder(), cookie);
Steven Moreland901c7452018-09-04 16:17:28 -0700745}
746
Andrei Homescu40dbf252024-03-28 21:19:42 +0000747#ifdef BINDER_WITH_KERNEL_IPC
Steven Morelandf3034b02018-11-12 17:37:46 -0800748uid_t AIBinder_getCallingUid() {
749 return ::android::IPCThreadState::self()->getCallingUid();
750}
751
752pid_t AIBinder_getCallingPid() {
753 return ::android::IPCThreadState::self()->getCallingPid();
754}
755
Alice Ryhl59aeae82021-08-25 10:21:26 +0000756bool AIBinder_isHandlingTransaction() {
757 return ::android::IPCThreadState::self()->getServingStackPointer() != nullptr;
758}
Andrei Homescu40dbf252024-03-28 21:19:42 +0000759#endif
Alice Ryhl59aeae82021-08-25 10:21:26 +0000760
Steven Moreland2e87adc2018-08-20 19:47:00 -0700761void AIBinder_incStrong(AIBinder* binder) {
762 if (binder == nullptr) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700763 return;
764 }
765
766 binder->incStrong(nullptr);
767}
768void AIBinder_decStrong(AIBinder* binder) {
769 if (binder == nullptr) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700770 ALOGE("%s: on null binder", __func__);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700771 return;
772 }
773
774 binder->decStrong(nullptr);
775}
776int32_t AIBinder_debugGetRefCount(AIBinder* binder) {
777 if (binder == nullptr) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700778 ALOGE("%s: on null binder", __func__);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700779 return -1;
780 }
781
782 return binder->getStrongCount();
783}
784
Steven Moreland71cddc32018-08-30 23:39:22 -0700785bool AIBinder_associateClass(AIBinder* binder, const AIBinder_Class* clazz) {
786 if (binder == nullptr) {
787 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700788 }
789
Steven Moreland71cddc32018-08-30 23:39:22 -0700790 return binder->associateClass(clazz);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700791}
792
793const AIBinder_Class* AIBinder_getClass(AIBinder* binder) {
794 if (binder == nullptr) {
795 return nullptr;
796 }
797
798 return binder->getClass();
799}
800
801void* AIBinder_getUserData(AIBinder* binder) {
802 if (binder == nullptr) {
803 return nullptr;
804 }
805
806 ABBinder* bBinder = binder->asABBinder();
807 if (bBinder == nullptr) {
808 return nullptr;
809 }
810
811 return bBinder->getUserData();
812}
813
814binder_status_t AIBinder_prepareTransaction(AIBinder* binder, AParcel** in) {
815 if (binder == nullptr || in == nullptr) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700816 ALOGE("%s: requires non-null parameters binder (%p) and in (%p).", __func__, binder, in);
Steven Moreland5d62e442018-09-13 15:01:02 -0700817 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700818 }
819 const AIBinder_Class* clazz = binder->getClass();
820 if (clazz == nullptr) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700821 ALOGE("%s: Class must be defined for a remote binder transaction. See "
822 "AIBinder_associateClass.",
823 __func__);
Steven Moreland5d62e442018-09-13 15:01:02 -0700824 return STATUS_INVALID_OPERATION;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700825 }
826
827 *in = new AParcel(binder);
Steven Moreland1fda67b2021-04-02 18:35:50 +0000828 (*in)->get()->markForBinder(binder->getBinder());
829
Steven Morelandf3ed8062021-08-27 14:31:14 -0700830 status_t status = android::OK;
Steven Morelandfaf25a42022-12-21 02:21:36 +0000831
832 // note - this is the only read of a value in clazz, and it comes with a warning
833 // on the API itself. Do not copy this design. Instead, attach data in a new
834 // version of the prepareTransaction function.
Steven Morelandf3ed8062021-08-27 14:31:14 -0700835 if (clazz->writeHeader) {
836 status = (*in)->get()->writeInterfaceToken(clazz->getInterfaceDescriptor());
837 }
Steven Moreland5d62e442018-09-13 15:01:02 -0700838 binder_status_t ret = PruneStatusT(status);
839
840 if (ret != STATUS_OK) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700841 delete *in;
842 *in = nullptr;
843 }
844
Steven Moreland5d62e442018-09-13 15:01:02 -0700845 return ret;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700846}
847
Steven Moreland9b80e282018-09-19 13:57:23 -0700848static void DestroyParcel(AParcel** parcel) {
849 delete *parcel;
850 *parcel = nullptr;
851}
852
Steven Moreland2e87adc2018-08-20 19:47:00 -0700853binder_status_t AIBinder_transact(AIBinder* binder, transaction_code_t code, AParcel** in,
854 AParcel** out, binder_flags_t flags) {
Pawan Wagh0d6e60f2024-09-26 00:08:10 +0000855 const AIBinder_Class* clazz = binder ? binder->getClass() : nullptr;
856
857 std::string sectionName;
858 bool tracingEnabled = get_trace_enabled_tags() & ATRACE_TAG_AIDL;
859 if (tracingEnabled) {
860 sectionName = getTraceSectionName(clazz, code, false /*isServer*/);
861 trace_begin(ATRACE_TAG_AIDL, sectionName.c_str());
862 }
863
864 scope_guard guard = make_scope_guard([&]() {
865 if (tracingEnabled) trace_end(ATRACE_TAG_AIDL);
866 });
867
Steven Moreland2e87adc2018-08-20 19:47:00 -0700868 if (in == nullptr) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700869 ALOGE("%s: requires non-null in parameter", __func__);
Steven Moreland5d62e442018-09-13 15:01:02 -0700870 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700871 }
872
Steven Morelandcaa776c2018-09-04 13:48:11 -0700873 using AutoParcelDestroyer = std::unique_ptr<AParcel*, void (*)(AParcel**)>;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700874 // This object is the input to the transaction. This function takes ownership of it and deletes
875 // it.
Steven Moreland9b80e282018-09-19 13:57:23 -0700876 AutoParcelDestroyer forIn(in, DestroyParcel);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700877
878 if (!isUserCommand(code)) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700879 ALOGE("%s: Only user-defined transactions can be made from the NDK, but requested: %d",
880 __func__, code);
Steven Moreland5d62e442018-09-13 15:01:02 -0700881 return STATUS_UNKNOWN_TRANSACTION;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700882 }
883
Steven Morelandf183fdd2020-10-27 00:12:12 +0000884 constexpr binder_flags_t kAllFlags = FLAG_PRIVATE_VENDOR | FLAG_ONEWAY | FLAG_CLEAR_BUF;
Steven Moreland46b5fea2019-10-15 11:22:18 -0700885 if ((flags & ~kAllFlags) != 0) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700886 ALOGE("%s: Unrecognized flags sent: %d", __func__, flags);
Steven Moreland5d62e442018-09-13 15:01:02 -0700887 return STATUS_BAD_VALUE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700888 }
889
890 if (binder == nullptr || *in == nullptr || out == nullptr) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700891 ALOGE("%s: requires non-null parameters binder (%p), in (%p), and out (%p).", __func__,
892 binder, in, out);
Steven Moreland5d62e442018-09-13 15:01:02 -0700893 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700894 }
895
896 if ((*in)->getBinder() != binder) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700897 ALOGE("%s: parcel is associated with binder object %p but called with %p", __func__, binder,
898 (*in)->getBinder());
Steven Moreland5d62e442018-09-13 15:01:02 -0700899 return STATUS_BAD_VALUE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700900 }
901
902 *out = new AParcel(binder);
903
Steven Morelandf18615b2018-09-14 11:43:06 -0700904 status_t status = binder->getBinder()->transact(code, *(*in)->get(), (*out)->get(), flags);
Steven Moreland5d62e442018-09-13 15:01:02 -0700905 binder_status_t ret = PruneStatusT(status);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700906
Steven Moreland5d62e442018-09-13 15:01:02 -0700907 if (ret != STATUS_OK) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700908 delete *out;
909 *out = nullptr;
910 }
911
Steven Moreland5d62e442018-09-13 15:01:02 -0700912 return ret;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700913}
Steven Moreland901c7452018-09-04 16:17:28 -0700914
915AIBinder_DeathRecipient* AIBinder_DeathRecipient_new(
916 AIBinder_DeathRecipient_onBinderDied onBinderDied) {
917 if (onBinderDied == nullptr) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700918 ALOGE("%s: requires non-null onBinderDied parameter.", __func__);
Steven Moreland901c7452018-09-04 16:17:28 -0700919 return nullptr;
920 }
Steven Moreland64127ca2019-03-13 09:25:44 -0700921 auto ret = new AIBinder_DeathRecipient(onBinderDied);
922 ret->incStrong(nullptr);
923 return ret;
Steven Moreland901c7452018-09-04 16:17:28 -0700924}
925
Alice Ryhlea9d9d22021-08-27 07:51:30 +0000926void AIBinder_DeathRecipient_setOnUnlinked(AIBinder_DeathRecipient* recipient,
927 AIBinder_DeathRecipient_onBinderUnlinked onUnlinked) {
928 if (recipient == nullptr) {
929 return;
930 }
931
932 recipient->setOnUnlinked(onUnlinked);
933}
934
Steven Moreland9b80e282018-09-19 13:57:23 -0700935void AIBinder_DeathRecipient_delete(AIBinder_DeathRecipient* recipient) {
Steven Moreland64127ca2019-03-13 09:25:44 -0700936 if (recipient == nullptr) {
937 return;
938 }
939
940 recipient->decStrong(nullptr);
Steven Moreland901c7452018-09-04 16:17:28 -0700941}
Steven Morelandea14ef22019-08-20 10:50:08 -0700942
943binder_status_t AIBinder_getExtension(AIBinder* binder, AIBinder** outExt) {
944 if (binder == nullptr || outExt == nullptr) {
945 if (outExt != nullptr) {
946 *outExt = nullptr;
947 }
948 return STATUS_UNEXPECTED_NULL;
949 }
950
951 sp<IBinder> ext;
952 status_t res = binder->getBinder()->getExtension(&ext);
953
954 if (res != android::OK) {
955 *outExt = nullptr;
956 return PruneStatusT(res);
957 }
958
959 sp<AIBinder> ret = ABpBinder::lookupOrCreateFromBinder(ext);
960 if (ret != nullptr) ret->incStrong(binder);
961
962 *outExt = ret.get();
963 return STATUS_OK;
964}
965
966binder_status_t AIBinder_setExtension(AIBinder* binder, AIBinder* ext) {
967 if (binder == nullptr || ext == nullptr) {
968 return STATUS_UNEXPECTED_NULL;
969 }
970
971 ABBinder* rawBinder = binder->asABBinder();
972 if (rawBinder == nullptr) {
973 return STATUS_INVALID_OPERATION;
974 }
975
976 rawBinder->setExtension(ext->getBinder());
977 return STATUS_OK;
978}
Steven Moreland2f405f52020-07-08 22:24:29 +0000979
980// platform methods follow
981
982void AIBinder_setRequestingSid(AIBinder* binder, bool requestingSid) {
983 ABBinder* localBinder = binder->asABBinder();
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700984 LOG_ALWAYS_FATAL_IF(localBinder == nullptr,
985 "AIBinder_setRequestingSid must be called on a local binder");
Steven Moreland2f405f52020-07-08 22:24:29 +0000986
987 localBinder->setRequestingSid(requestingSid);
988}
989
Andrei Homescu40dbf252024-03-28 21:19:42 +0000990#ifdef BINDER_WITH_KERNEL_IPC
Steven Moreland2f405f52020-07-08 22:24:29 +0000991const char* AIBinder_getCallingSid() {
992 return ::android::IPCThreadState::self()->getCallingSid();
993}
Andrei Homescu40dbf252024-03-28 21:19:42 +0000994#endif
Steven Morelandb2de9532020-05-29 21:44:41 +0000995
Ady Abraham96174ac2021-10-15 11:02:03 -0700996void AIBinder_setMinSchedulerPolicy(AIBinder* binder, int policy, int priority) {
997 binder->asABBinder()->setMinSchedulerPolicy(policy, priority);
998}
Steven Moreland454a26a2021-12-14 01:26:21 +0000999
1000void AIBinder_setInheritRt(AIBinder* binder, bool inheritRt) {
1001 ABBinder* localBinder = binder->asABBinder();
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -07001002 LOG_ALWAYS_FATAL_IF(localBinder == nullptr,
1003 "AIBinder_setInheritRt must be called on a local binder");
Steven Moreland454a26a2021-12-14 01:26:21 +00001004
1005 localBinder->setInheritRt(inheritRt);
Pawan Wagh0d6e60f2024-09-26 00:08:10 +00001006}