blob: 28d1f16d644c8c2458227c1a2b1c2646e3656a20 [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
Steven Moreland39fdec92022-08-27 00:27:37 +000017#include <android-base/logging.h>
Steven Moreland2e87adc2018-08-20 19:47:00 -070018#include <android/binder_ibinder.h>
Steven Moreland2f405f52020-07-08 22:24:29 +000019#include <android/binder_ibinder_platform.h>
Steven Moreland46b5fea2019-10-15 11:22:18 -070020#include <android/binder_stability.h>
Steven Moreland2e87adc2018-08-20 19:47:00 -070021#include <android/binder_status.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>
24#include <private/android_filesystem_config.h>
Steven Moreland2e87adc2018-08-20 19:47:00 -070025
Steven Moreland39fdec92022-08-27 00:27:37 +000026#include "ibinder_internal.h"
27#include "parcel_internal.h"
28#include "status_internal.h"
29
Steven Moreland901c7452018-09-04 16:17:28 -070030using DeathRecipient = ::android::IBinder::DeathRecipient;
31
Steven Moreland2e87adc2018-08-20 19:47:00 -070032using ::android::IBinder;
Ruchir Rastogicc7a7462020-01-31 14:29:15 -080033using ::android::IResultReceiver;
Steven Moreland2e87adc2018-08-20 19:47:00 -070034using ::android::Parcel;
35using ::android::sp;
Steven Moreland5d62e442018-09-13 15:01:02 -070036using ::android::status_t;
Steven Moreland009fc1a2022-06-10 17:24:25 +000037using ::android::statusToString;
Steven Moreland2e87adc2018-08-20 19:47:00 -070038using ::android::String16;
Steven Morelanda194c452019-03-04 16:47:07 -080039using ::android::String8;
Steven Moreland2e87adc2018-08-20 19:47:00 -070040using ::android::wp;
41
Steven Moreland71cddc32018-08-30 23:39:22 -070042namespace ABBinderTag {
43
44static const void* kId = "ABBinder";
45static void* kValue = static_cast<void*>(new bool{true});
Steven Moreland94968952018-09-05 14:42:59 -070046void clean(const void* /*id*/, void* /*obj*/, void* /*cookie*/){/* do nothing */};
Steven Moreland71cddc32018-08-30 23:39:22 -070047
48static void attach(const sp<IBinder>& binder) {
Steven Moreland9234c432021-06-29 23:01:10 +000049 // can only attach once
50 CHECK_EQ(nullptr, binder->attachObject(kId, kValue, nullptr /*cookie*/, clean));
Steven Moreland71cddc32018-08-30 23:39:22 -070051}
52static bool has(const sp<IBinder>& binder) {
53 return binder != nullptr && binder->findObject(kId) == kValue;
54}
55
Steven Moreland6cf66ac2018-11-02 18:14:54 -070056} // namespace ABBinderTag
Steven Moreland71cddc32018-08-30 23:39:22 -070057
Steven Moreland94968952018-09-05 14:42:59 -070058namespace ABpBinderTag {
59
Steven Moreland94968952018-09-05 14:42:59 -070060static const void* kId = "ABpBinder";
61struct Value {
62 wp<ABpBinder> binder;
63};
64void clean(const void* id, void* obj, void* cookie) {
Steven Moreland3acd4902022-07-26 18:29:58 +000065 // be weary of leaks!
66 // LOG(INFO) << "Deleting an ABpBinder";
67
Steven Moreland94968952018-09-05 14:42:59 -070068 CHECK(id == kId) << id << " " << obj << " " << cookie;
69
70 delete static_cast<Value*>(obj);
71};
72
Steven Moreland6cf66ac2018-11-02 18:14:54 -070073} // namespace ABpBinderTag
Steven Moreland94968952018-09-05 14:42:59 -070074
Steven Moreland2e87adc2018-08-20 19:47:00 -070075AIBinder::AIBinder(const AIBinder_Class* clazz) : mClazz(clazz) {}
76AIBinder::~AIBinder() {}
77
Andrei Homescu9556bb12020-09-21 16:59:45 -070078std::optional<bool> AIBinder::associateClassInternal(const AIBinder_Class* clazz,
Steven Moreland63872b52020-10-02 19:28:23 +000079 const String16& newDescriptor, bool set) {
Andrei Homescu9556bb12020-09-21 16:59:45 -070080 std::lock_guard<std::mutex> lock(mClazzMutex);
Steven Moreland71cddc32018-08-30 23:39:22 -070081 if (mClazz == clazz) return true;
Steven Moreland2e87adc2018-08-20 19:47:00 -070082
Steven Moreland2e87adc2018-08-20 19:47:00 -070083 if (mClazz != nullptr) {
Steven Moreland63872b52020-10-02 19:28:23 +000084 const String16& currentDescriptor = mClazz->getInterfaceDescriptor();
Steven Moreland2e87adc2018-08-20 19:47:00 -070085 if (newDescriptor == currentDescriptor) {
86 LOG(ERROR) << __func__ << ": Class descriptors '" << currentDescriptor
Steven Moreland4d196972021-08-12 15:49:44 -070087 << "' match during associateClass, but they are different class objects ("
88 << clazz << " vs " << mClazz << "). Class descriptor collision?";
Steven Moreland71cddc32018-08-30 23:39:22 -070089 } else {
90 LOG(ERROR) << __func__
91 << ": Class cannot be associated on object which already has a class. "
92 "Trying to associate to '"
Steven Moreland63872b52020-10-02 19:28:23 +000093 << newDescriptor << "' but already set to '" << currentDescriptor << "'.";
Steven Moreland2e87adc2018-08-20 19:47:00 -070094 }
95
Steven Moreland71cddc32018-08-30 23:39:22 -070096 // always a failure because we know mClazz != clazz
97 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -070098 }
99
Andrei Homescu9556bb12020-09-21 16:59:45 -0700100 if (set) {
101 // if this is a local object, it's not one known to libbinder_ndk
102 mClazz = clazz;
Steven Moreland9facfce2020-10-02 17:26:56 +0000103 return true;
Andrei Homescu9556bb12020-09-21 16:59:45 -0700104 }
105
106 return {};
107}
108
Jooyung Han1f9dcd32021-11-03 13:34:29 +0900109// b/175635923 libcxx causes "implicit-conversion" with a string with invalid char
110static std::string SanitizeString(const String16& str) {
111 std::string sanitized{String8(str)};
112 for (auto& c : sanitized) {
113 if (!isprint(c)) {
114 c = '?';
115 }
116 }
117 return sanitized;
118}
119
Andrei Homescu9556bb12020-09-21 16:59:45 -0700120bool AIBinder::associateClass(const AIBinder_Class* clazz) {
121 if (clazz == nullptr) return false;
122
Steven Moreland63872b52020-10-02 19:28:23 +0000123 const String16& newDescriptor = clazz->getInterfaceDescriptor();
Andrei Homescu9556bb12020-09-21 16:59:45 -0700124
125 auto result = associateClassInternal(clazz, newDescriptor, false);
126 if (result.has_value()) return *result;
127
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700128 CHECK(asABpBinder() != nullptr); // ABBinder always has a descriptor
Steven Moreland71cddc32018-08-30 23:39:22 -0700129
Steven Moreland63872b52020-10-02 19:28:23 +0000130 const String16& descriptor = getBinder()->getInterfaceDescriptor();
Steven Moreland2e87adc2018-08-20 19:47:00 -0700131 if (descriptor != newDescriptor) {
Steven Moreland640c4092020-07-08 00:19:58 +0000132 if (getBinder()->isBinderAlive()) {
Steven Moreland63872b52020-10-02 19:28:23 +0000133 LOG(ERROR) << __func__ << ": Expecting binder to have class '" << newDescriptor
Jooyung Han1f9dcd32021-11-03 13:34:29 +0900134 << "' but descriptor is actually '" << SanitizeString(descriptor) << "'.";
Steven Moreland640c4092020-07-08 00:19:58 +0000135 } else {
136 // b/155793159
Steven Moreland63872b52020-10-02 19:28:23 +0000137 LOG(ERROR) << __func__ << ": Cannot associate class '" << newDescriptor
Steven Moreland009fc1a2022-06-10 17:24:25 +0000138 << "' to dead binder with cached descriptor '" << SanitizeString(descriptor)
139 << "'.";
Steven Moreland640c4092020-07-08 00:19:58 +0000140 }
Steven Moreland71cddc32018-08-30 23:39:22 -0700141 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700142 }
143
Steven Moreland9facfce2020-10-02 17:26:56 +0000144 return associateClassInternal(clazz, newDescriptor, true).value();
Steven Moreland2e87adc2018-08-20 19:47:00 -0700145}
146
147ABBinder::ABBinder(const AIBinder_Class* clazz, void* userData)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700148 : AIBinder(clazz), BBinder(), mUserData(userData) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700149 CHECK(clazz != nullptr);
150}
151ABBinder::~ABBinder() {
152 getClass()->onDestroy(mUserData);
153}
154
155const String16& ABBinder::getInterfaceDescriptor() const {
156 return getClass()->getInterfaceDescriptor();
157}
158
Steven Morelanda194c452019-03-04 16:47:07 -0800159status_t ABBinder::dump(int fd, const ::android::Vector<String16>& args) {
160 AIBinder_onDump onDump = getClass()->onDump;
161
162 if (onDump == nullptr) {
163 return STATUS_OK;
164 }
165
166 // technically UINT32_MAX would be okay here, but INT32_MAX is expected since this may be
167 // null in Java
168 if (args.size() > INT32_MAX) {
169 LOG(ERROR) << "ABBinder::dump received too many arguments: " << args.size();
170 return STATUS_BAD_VALUE;
171 }
172
173 std::vector<String8> utf8Args; // owns memory of utf8s
174 utf8Args.reserve(args.size());
175 std::vector<const char*> utf8Pointers; // what can be passed over NDK API
176 utf8Pointers.reserve(args.size());
177
178 for (size_t i = 0; i < args.size(); i++) {
179 utf8Args.push_back(String8(args[i]));
180 utf8Pointers.push_back(utf8Args[i].c_str());
181 }
182
183 return onDump(this, fd, utf8Pointers.data(), utf8Pointers.size());
184}
185
Steven Moreland5d62e442018-09-13 15:01:02 -0700186status_t ABBinder::onTransact(transaction_code_t code, const Parcel& data, Parcel* reply,
187 binder_flags_t flags) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700188 if (isUserCommand(code)) {
Steven Morelandf3ed8062021-08-27 14:31:14 -0700189 if (getClass()->writeHeader && !data.checkInterface(this)) {
Steven Moreland9d089af2018-09-18 08:58:09 -0700190 return STATUS_BAD_TYPE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700191 }
192
193 const AParcel in = AParcel::readOnly(this, &data);
194 AParcel out = AParcel(this, reply, false /*owns*/);
195
Steven Moreland5d62e442018-09-13 15:01:02 -0700196 binder_status_t status = getClass()->onTransact(this, code, &in, &out);
197 return PruneStatusT(status);
Steven Moreland88234072020-08-06 19:32:45 +0000198 } else if (code == SHELL_COMMAND_TRANSACTION && getClass()->handleShellCommand != nullptr) {
Ruchir Rastogicc7a7462020-01-31 14:29:15 -0800199 int in = data.readFileDescriptor();
200 int out = data.readFileDescriptor();
201 int err = data.readFileDescriptor();
202
203 int argc = data.readInt32();
204 std::vector<String8> utf8Args; // owns memory of utf8s
205 std::vector<const char*> utf8Pointers; // what can be passed over NDK API
206 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
207 utf8Args.push_back(String8(data.readString16()));
208 utf8Pointers.push_back(utf8Args[i].c_str());
209 }
210
211 data.readStrongBinder(); // skip over the IShellCallback
212 sp<IResultReceiver> resultReceiver = IResultReceiver::asInterface(data.readStrongBinder());
213
214 // Shell commands should only be callable by ADB.
215 uid_t uid = AIBinder_getCallingUid();
216 if (uid != AID_ROOT && uid != AID_SHELL) {
217 if (resultReceiver != nullptr) {
218 resultReceiver->send(-1);
219 }
220 return STATUS_PERMISSION_DENIED;
221 }
222
223 // Check that the file descriptors are valid.
224 if (in == STATUS_BAD_TYPE || out == STATUS_BAD_TYPE || err == STATUS_BAD_TYPE) {
225 if (resultReceiver != nullptr) {
226 resultReceiver->send(-1);
227 }
228 return STATUS_BAD_VALUE;
229 }
230
231 binder_status_t status = getClass()->handleShellCommand(
232 this, in, out, err, utf8Pointers.data(), utf8Pointers.size());
233 if (resultReceiver != nullptr) {
234 resultReceiver->send(status);
235 }
236 return status;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700237 } else {
238 return BBinder::onTransact(code, data, reply, flags);
239 }
240}
241
Steven Moreland71cddc32018-08-30 23:39:22 -0700242ABpBinder::ABpBinder(const ::android::sp<::android::IBinder>& binder)
Steven Moreland3acd4902022-07-26 18:29:58 +0000243 : AIBinder(nullptr /*clazz*/), mRemote(binder) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700244 CHECK(binder != nullptr);
245}
246ABpBinder::~ABpBinder() {}
247
Steven Moreland94968952018-09-05 14:42:59 -0700248sp<AIBinder> ABpBinder::lookupOrCreateFromBinder(const ::android::sp<::android::IBinder>& binder) {
Steven Moreland71cddc32018-08-30 23:39:22 -0700249 if (binder == nullptr) {
250 return nullptr;
251 }
252 if (ABBinderTag::has(binder)) {
253 return static_cast<ABBinder*>(binder.get());
254 }
Steven Moreland94968952018-09-05 14:42:59 -0700255
Steven Moreland91a96b62021-06-30 23:07:13 +0000256 // The following code ensures that for a given binder object (remote or local), if it is not an
257 // ABBinder then at most one ABpBinder object exists in a given process representing it.
258
Steven Moreland1f1bed62021-06-25 21:50:50 +0000259 auto* value = static_cast<ABpBinderTag::Value*>(binder->findObject(ABpBinderTag::kId));
Steven Moreland94968952018-09-05 14:42:59 -0700260 if (value == nullptr) {
261 value = new ABpBinderTag::Value;
Steven Moreland1f1bed62021-06-25 21:50:50 +0000262 auto oldValue = static_cast<ABpBinderTag::Value*>(
263 binder->attachObject(ABpBinderTag::kId, static_cast<void*>(value),
264 nullptr /*cookie*/, ABpBinderTag::clean));
265
266 // allocated by another thread
267 if (oldValue) {
268 delete value;
269 value = oldValue;
270 }
Steven Moreland94968952018-09-05 14:42:59 -0700271 }
272
Steven Moreland1f1bed62021-06-25 21:50:50 +0000273 sp<ABpBinder> ret;
274 binder->withLock([&]() {
275 ret = value->binder.promote();
276 if (ret == nullptr) {
277 ret = sp<ABpBinder>::make(binder);
278 value->binder = ret;
279 }
280 });
Steven Moreland94968952018-09-05 14:42:59 -0700281
282 return ret;
Steven Moreland71cddc32018-08-30 23:39:22 -0700283}
284
Steven Moreland2e87adc2018-08-20 19:47:00 -0700285struct AIBinder_Weak {
286 wp<AIBinder> binder;
287};
288AIBinder_Weak* AIBinder_Weak_new(AIBinder* binder) {
Steven Moreland5ccb70f2018-09-04 16:30:21 -0700289 if (binder == nullptr) {
290 return nullptr;
291 }
292
Steven Moreland2e87adc2018-08-20 19:47:00 -0700293 return new AIBinder_Weak{wp<AIBinder>(binder)};
294}
Steven Moreland9b80e282018-09-19 13:57:23 -0700295void AIBinder_Weak_delete(AIBinder_Weak* weakBinder) {
296 delete weakBinder;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700297}
298AIBinder* AIBinder_Weak_promote(AIBinder_Weak* weakBinder) {
Steven Moreland5ccb70f2018-09-04 16:30:21 -0700299 if (weakBinder == nullptr) {
300 return nullptr;
301 }
302
Steven Moreland2e87adc2018-08-20 19:47:00 -0700303 sp<AIBinder> binder = weakBinder->binder.promote();
304 AIBinder_incStrong(binder.get());
305 return binder.get();
306}
307
Steven Morelandc70e0122021-01-08 02:44:53 +0000308AIBinder_Weak* AIBinder_Weak_clone(const AIBinder_Weak* weak) {
309 if (weak == nullptr) {
310 return nullptr;
311 }
312
313 return new AIBinder_Weak{weak->binder};
314}
315
316bool AIBinder_lt(const AIBinder* lhs, const AIBinder* rhs) {
317 if (lhs == nullptr || rhs == nullptr) return lhs < rhs;
318
319 return const_cast<AIBinder*>(lhs)->getBinder() < const_cast<AIBinder*>(rhs)->getBinder();
320}
321
322bool AIBinder_Weak_lt(const AIBinder_Weak* lhs, const AIBinder_Weak* rhs) {
323 if (lhs == nullptr || rhs == nullptr) return lhs < rhs;
324
325 return lhs->binder < rhs->binder;
326}
327
Steven Moreland2e87adc2018-08-20 19:47:00 -0700328AIBinder_Class::AIBinder_Class(const char* interfaceDescriptor, AIBinder_Class_onCreate onCreate,
329 AIBinder_Class_onDestroy onDestroy,
330 AIBinder_Class_onTransact onTransact)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700331 : onCreate(onCreate),
332 onDestroy(onDestroy),
333 onTransact(onTransact),
Stephen Crane8fde87f2020-11-17 15:06:11 -0800334 mInterfaceDescriptor(interfaceDescriptor),
335 mWideInterfaceDescriptor(interfaceDescriptor) {}
Steven Moreland2e87adc2018-08-20 19:47:00 -0700336
337AIBinder_Class* AIBinder_Class_define(const char* interfaceDescriptor,
338 AIBinder_Class_onCreate onCreate,
339 AIBinder_Class_onDestroy onDestroy,
340 AIBinder_Class_onTransact onTransact) {
341 if (interfaceDescriptor == nullptr || onCreate == nullptr || onDestroy == nullptr ||
342 onTransact == nullptr) {
343 return nullptr;
344 }
345
346 return new AIBinder_Class(interfaceDescriptor, onCreate, onDestroy, onTransact);
347}
348
Steven Morelanda194c452019-03-04 16:47:07 -0800349void AIBinder_Class_setOnDump(AIBinder_Class* clazz, AIBinder_onDump onDump) {
350 CHECK(clazz != nullptr) << "setOnDump requires non-null clazz";
351
352 // this is required to be called before instances are instantiated
353 clazz->onDump = onDump;
354}
355
Steven Morelandf3ed8062021-08-27 14:31:14 -0700356void AIBinder_Class_disableInterfaceTokenHeader(AIBinder_Class* clazz) {
357 CHECK(clazz != nullptr) << "disableInterfaceTokenHeader requires non-null clazz";
358
359 clazz->writeHeader = false;
360}
361
Ruchir Rastogicc7a7462020-01-31 14:29:15 -0800362void AIBinder_Class_setHandleShellCommand(AIBinder_Class* clazz,
363 AIBinder_handleShellCommand handleShellCommand) {
364 CHECK(clazz != nullptr) << "setHandleShellCommand requires non-null clazz";
365
366 clazz->handleShellCommand = handleShellCommand;
367}
368
Stephen Crane8fde87f2020-11-17 15:06:11 -0800369const char* AIBinder_Class_getDescriptor(const AIBinder_Class* clazz) {
370 CHECK(clazz != nullptr) << "getDescriptor requires non-null clazz";
371
372 return clazz->getInterfaceDescriptorUtf8();
373}
374
Alice Ryhlea9d9d22021-08-27 07:51:30 +0000375AIBinder_DeathRecipient::TransferDeathRecipient::~TransferDeathRecipient() {
376 if (mOnUnlinked != nullptr) {
377 mOnUnlinked(mCookie);
378 }
379}
380
Steven Moreland901c7452018-09-04 16:17:28 -0700381void AIBinder_DeathRecipient::TransferDeathRecipient::binderDied(const wp<IBinder>& who) {
Steven Moreland8bcb5032021-04-07 23:06:43 +0000382 CHECK(who == mWho) << who.unsafe_get() << "(" << who.get_refs() << ") vs " << mWho.unsafe_get()
383 << " (" << mWho.get_refs() << ")";
Steven Moreland901c7452018-09-04 16:17:28 -0700384
385 mOnDied(mCookie);
Steven Moreland64127ca2019-03-13 09:25:44 -0700386
387 sp<AIBinder_DeathRecipient> recipient = mParentRecipient.promote();
388 sp<IBinder> strongWho = who.promote();
389
390 // otherwise this will be cleaned up later with pruneDeadTransferEntriesLocked
391 if (recipient != nullptr && strongWho != nullptr) {
392 status_t result = recipient->unlinkToDeath(strongWho, mCookie);
393 if (result != ::android::DEAD_OBJECT) {
394 LOG(WARNING) << "Unlinking to dead binder resulted in: " << result;
395 }
396 }
397
Steven Moreland901c7452018-09-04 16:17:28 -0700398 mWho = nullptr;
399}
400
401AIBinder_DeathRecipient::AIBinder_DeathRecipient(AIBinder_DeathRecipient_onBinderDied onDied)
Alice Ryhlea9d9d22021-08-27 07:51:30 +0000402 : mOnDied(onDied), mOnUnlinked(nullptr) {
Steven Moreland901c7452018-09-04 16:17:28 -0700403 CHECK(onDied != nullptr);
404}
405
Steven Moreland64127ca2019-03-13 09:25:44 -0700406void AIBinder_DeathRecipient::pruneDeadTransferEntriesLocked() {
407 mDeathRecipients.erase(std::remove_if(mDeathRecipients.begin(), mDeathRecipients.end(),
408 [](const sp<TransferDeathRecipient>& tdr) {
409 return tdr->getWho() == nullptr;
410 }),
411 mDeathRecipients.end());
412}
413
Jiyong Park4f97a8c2020-11-16 18:11:14 +0900414binder_status_t AIBinder_DeathRecipient::linkToDeath(const sp<IBinder>& binder, void* cookie) {
Steven Moreland901c7452018-09-04 16:17:28 -0700415 CHECK(binder != nullptr);
416
417 std::lock_guard<std::mutex> l(mDeathRecipientsMutex);
418
419 sp<TransferDeathRecipient> recipient =
Alice Ryhlea9d9d22021-08-27 07:51:30 +0000420 new TransferDeathRecipient(binder, cookie, this, mOnDied, mOnUnlinked);
Steven Moreland901c7452018-09-04 16:17:28 -0700421
Steven Moreland64127ca2019-03-13 09:25:44 -0700422 status_t status = binder->linkToDeath(recipient, cookie, 0 /*flags*/);
Steven Moreland5d62e442018-09-13 15:01:02 -0700423 if (status != STATUS_OK) {
Alice Ryhlea9d9d22021-08-27 07:51:30 +0000424 // When we failed to link, the destructor of TransferDeathRecipient runs here, which
425 // ensures that mOnUnlinked is called before we return with an error from this method.
Steven Moreland5d62e442018-09-13 15:01:02 -0700426 return PruneStatusT(status);
Steven Moreland901c7452018-09-04 16:17:28 -0700427 }
428
429 mDeathRecipients.push_back(recipient);
Steven Moreland64127ca2019-03-13 09:25:44 -0700430
431 pruneDeadTransferEntriesLocked();
Steven Moreland5d62e442018-09-13 15:01:02 -0700432 return STATUS_OK;
Steven Moreland901c7452018-09-04 16:17:28 -0700433}
434
Jiyong Park4f97a8c2020-11-16 18:11:14 +0900435binder_status_t AIBinder_DeathRecipient::unlinkToDeath(const sp<IBinder>& binder, void* cookie) {
Steven Moreland901c7452018-09-04 16:17:28 -0700436 CHECK(binder != nullptr);
437
438 std::lock_guard<std::mutex> l(mDeathRecipientsMutex);
439
440 for (auto it = mDeathRecipients.rbegin(); it != mDeathRecipients.rend(); ++it) {
441 sp<TransferDeathRecipient> recipient = *it;
442
Steven Moreland64127ca2019-03-13 09:25:44 -0700443 if (recipient->getCookie() == cookie && recipient->getWho() == binder) {
Steven Moreland901c7452018-09-04 16:17:28 -0700444 mDeathRecipients.erase(it.base() - 1);
445
Steven Moreland64127ca2019-03-13 09:25:44 -0700446 status_t status = binder->unlinkToDeath(recipient, cookie, 0 /*flags*/);
Steven Moreland5d62e442018-09-13 15:01:02 -0700447 if (status != ::android::OK) {
Steven Moreland901c7452018-09-04 16:17:28 -0700448 LOG(ERROR) << __func__
Steven Moreland009fc1a2022-06-10 17:24:25 +0000449 << ": removed reference to death recipient but unlink failed: "
450 << statusToString(status);
Steven Moreland901c7452018-09-04 16:17:28 -0700451 }
Steven Moreland5d62e442018-09-13 15:01:02 -0700452 return PruneStatusT(status);
Steven Moreland901c7452018-09-04 16:17:28 -0700453 }
454 }
455
Steven Moreland5d62e442018-09-13 15:01:02 -0700456 return STATUS_NAME_NOT_FOUND;
Steven Moreland901c7452018-09-04 16:17:28 -0700457}
458
Alice Ryhlea9d9d22021-08-27 07:51:30 +0000459void AIBinder_DeathRecipient::setOnUnlinked(AIBinder_DeathRecipient_onBinderUnlinked onUnlinked) {
460 mOnUnlinked = onUnlinked;
461}
462
Steven Moreland901c7452018-09-04 16:17:28 -0700463// start of C-API methods
464
Steven Moreland2e87adc2018-08-20 19:47:00 -0700465AIBinder* AIBinder_new(const AIBinder_Class* clazz, void* args) {
466 if (clazz == nullptr) {
467 LOG(ERROR) << __func__ << ": Must provide class to construct local binder.";
468 return nullptr;
469 }
470
471 void* userData = clazz->onCreate(args);
472
Steven Moreland71cddc32018-08-30 23:39:22 -0700473 sp<AIBinder> ret = new ABBinder(clazz, userData);
474 ABBinderTag::attach(ret->getBinder());
475
476 AIBinder_incStrong(ret.get());
477 return ret.get();
Steven Moreland2e87adc2018-08-20 19:47:00 -0700478}
479
Steven Moreland5ea54da2018-09-04 13:29:55 -0700480bool AIBinder_isRemote(const AIBinder* binder) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700481 if (binder == nullptr) {
Steven Moreland56f752a2018-11-05 17:23:37 -0800482 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700483 }
484
485 return binder->isRemote();
486}
487
Steven Moreland65867d72018-09-04 14:22:26 -0700488bool AIBinder_isAlive(const AIBinder* binder) {
489 if (binder == nullptr) {
490 return false;
491 }
492
493 return const_cast<AIBinder*>(binder)->getBinder()->isBinderAlive();
494}
495
496binder_status_t AIBinder_ping(AIBinder* binder) {
497 if (binder == nullptr) {
Steven Moreland5d62e442018-09-13 15:01:02 -0700498 return STATUS_UNEXPECTED_NULL;
Steven Moreland65867d72018-09-04 14:22:26 -0700499 }
500
Steven Moreland5d62e442018-09-13 15:01:02 -0700501 return PruneStatusT(binder->getBinder()->pingBinder());
Steven Moreland65867d72018-09-04 14:22:26 -0700502}
503
Steven Morelanda194c452019-03-04 16:47:07 -0800504binder_status_t AIBinder_dump(AIBinder* binder, int fd, const char** args, uint32_t numArgs) {
505 if (binder == nullptr) {
506 return STATUS_UNEXPECTED_NULL;
507 }
508
509 ABBinder* bBinder = binder->asABBinder();
510 if (bBinder != nullptr) {
511 AIBinder_onDump onDump = binder->getClass()->onDump;
512 if (onDump == nullptr) {
513 return STATUS_OK;
514 }
515 return PruneStatusT(onDump(bBinder, fd, args, numArgs));
516 }
517
518 ::android::Vector<String16> utf16Args;
519 utf16Args.setCapacity(numArgs);
520 for (uint32_t i = 0; i < numArgs; i++) {
521 utf16Args.push(String16(String8(args[i])));
522 }
523
524 status_t status = binder->getBinder()->dump(fd, utf16Args);
525 return PruneStatusT(status);
526}
527
Steven Moreland901c7452018-09-04 16:17:28 -0700528binder_status_t AIBinder_linkToDeath(AIBinder* binder, AIBinder_DeathRecipient* recipient,
529 void* cookie) {
530 if (binder == nullptr || recipient == nullptr) {
Steven Moreland009fc1a2022-06-10 17:24:25 +0000531 LOG(ERROR) << __func__ << ": Must provide binder (" << binder << ") and recipient ("
532 << recipient << ")";
Steven Moreland5d62e442018-09-13 15:01:02 -0700533 return STATUS_UNEXPECTED_NULL;
Steven Moreland901c7452018-09-04 16:17:28 -0700534 }
535
Steven Moreland5d62e442018-09-13 15:01:02 -0700536 // returns binder_status_t
Steven Moreland64127ca2019-03-13 09:25:44 -0700537 return recipient->linkToDeath(binder->getBinder(), cookie);
Steven Moreland901c7452018-09-04 16:17:28 -0700538}
539
540binder_status_t AIBinder_unlinkToDeath(AIBinder* binder, AIBinder_DeathRecipient* recipient,
541 void* cookie) {
542 if (binder == nullptr || recipient == nullptr) {
Steven Moreland009fc1a2022-06-10 17:24:25 +0000543 LOG(ERROR) << __func__ << ": Must provide binder (" << binder << ") and recipient ("
544 << recipient << ")";
Steven Moreland5d62e442018-09-13 15:01:02 -0700545 return STATUS_UNEXPECTED_NULL;
Steven Moreland901c7452018-09-04 16:17:28 -0700546 }
547
Steven Moreland5d62e442018-09-13 15:01:02 -0700548 // returns binder_status_t
Steven Moreland64127ca2019-03-13 09:25:44 -0700549 return recipient->unlinkToDeath(binder->getBinder(), cookie);
Steven Moreland901c7452018-09-04 16:17:28 -0700550}
551
Steven Morelandf3034b02018-11-12 17:37:46 -0800552uid_t AIBinder_getCallingUid() {
553 return ::android::IPCThreadState::self()->getCallingUid();
554}
555
556pid_t AIBinder_getCallingPid() {
557 return ::android::IPCThreadState::self()->getCallingPid();
558}
559
Alice Ryhl59aeae82021-08-25 10:21:26 +0000560bool AIBinder_isHandlingTransaction() {
561 return ::android::IPCThreadState::self()->getServingStackPointer() != nullptr;
562}
563
Steven Moreland2e87adc2018-08-20 19:47:00 -0700564void AIBinder_incStrong(AIBinder* binder) {
565 if (binder == nullptr) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700566 return;
567 }
568
569 binder->incStrong(nullptr);
570}
571void AIBinder_decStrong(AIBinder* binder) {
572 if (binder == nullptr) {
573 LOG(ERROR) << __func__ << ": on null binder";
574 return;
575 }
576
577 binder->decStrong(nullptr);
578}
579int32_t AIBinder_debugGetRefCount(AIBinder* binder) {
580 if (binder == nullptr) {
581 LOG(ERROR) << __func__ << ": on null binder";
582 return -1;
583 }
584
585 return binder->getStrongCount();
586}
587
Steven Moreland71cddc32018-08-30 23:39:22 -0700588bool AIBinder_associateClass(AIBinder* binder, const AIBinder_Class* clazz) {
589 if (binder == nullptr) {
590 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700591 }
592
Steven Moreland71cddc32018-08-30 23:39:22 -0700593 return binder->associateClass(clazz);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700594}
595
596const AIBinder_Class* AIBinder_getClass(AIBinder* binder) {
597 if (binder == nullptr) {
598 return nullptr;
599 }
600
601 return binder->getClass();
602}
603
604void* AIBinder_getUserData(AIBinder* binder) {
605 if (binder == nullptr) {
606 return nullptr;
607 }
608
609 ABBinder* bBinder = binder->asABBinder();
610 if (bBinder == nullptr) {
611 return nullptr;
612 }
613
614 return bBinder->getUserData();
615}
616
617binder_status_t AIBinder_prepareTransaction(AIBinder* binder, AParcel** in) {
618 if (binder == nullptr || in == nullptr) {
Steven Moreland009fc1a2022-06-10 17:24:25 +0000619 LOG(ERROR) << __func__ << ": requires non-null parameters binder (" << binder
620 << ") and in (" << in << ").";
Steven Moreland5d62e442018-09-13 15:01:02 -0700621 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700622 }
623 const AIBinder_Class* clazz = binder->getClass();
624 if (clazz == nullptr) {
625 LOG(ERROR) << __func__
626 << ": Class must be defined for a remote binder transaction. See "
627 "AIBinder_associateClass.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700628 return STATUS_INVALID_OPERATION;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700629 }
630
631 *in = new AParcel(binder);
Steven Moreland1fda67b2021-04-02 18:35:50 +0000632 (*in)->get()->markForBinder(binder->getBinder());
633
Steven Morelandf3ed8062021-08-27 14:31:14 -0700634 status_t status = android::OK;
635 if (clazz->writeHeader) {
636 status = (*in)->get()->writeInterfaceToken(clazz->getInterfaceDescriptor());
637 }
Steven Moreland5d62e442018-09-13 15:01:02 -0700638 binder_status_t ret = PruneStatusT(status);
639
640 if (ret != STATUS_OK) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700641 delete *in;
642 *in = nullptr;
643 }
644
Steven Moreland5d62e442018-09-13 15:01:02 -0700645 return ret;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700646}
647
Steven Moreland9b80e282018-09-19 13:57:23 -0700648static void DestroyParcel(AParcel** parcel) {
649 delete *parcel;
650 *parcel = nullptr;
651}
652
Steven Moreland2e87adc2018-08-20 19:47:00 -0700653binder_status_t AIBinder_transact(AIBinder* binder, transaction_code_t code, AParcel** in,
654 AParcel** out, binder_flags_t flags) {
655 if (in == nullptr) {
656 LOG(ERROR) << __func__ << ": requires non-null in parameter";
Steven Moreland5d62e442018-09-13 15:01:02 -0700657 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700658 }
659
Steven Morelandcaa776c2018-09-04 13:48:11 -0700660 using AutoParcelDestroyer = std::unique_ptr<AParcel*, void (*)(AParcel**)>;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700661 // This object is the input to the transaction. This function takes ownership of it and deletes
662 // it.
Steven Moreland9b80e282018-09-19 13:57:23 -0700663 AutoParcelDestroyer forIn(in, DestroyParcel);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700664
665 if (!isUserCommand(code)) {
Steven Moreland009fc1a2022-06-10 17:24:25 +0000666 LOG(ERROR) << __func__
667 << ": Only user-defined transactions can be made from the NDK, but requested: "
668 << code;
Steven Moreland5d62e442018-09-13 15:01:02 -0700669 return STATUS_UNKNOWN_TRANSACTION;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700670 }
671
Steven Morelandf183fdd2020-10-27 00:12:12 +0000672 constexpr binder_flags_t kAllFlags = FLAG_PRIVATE_VENDOR | FLAG_ONEWAY | FLAG_CLEAR_BUF;
Steven Moreland46b5fea2019-10-15 11:22:18 -0700673 if ((flags & ~kAllFlags) != 0) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700674 LOG(ERROR) << __func__ << ": Unrecognized flags sent: " << flags;
Steven Moreland5d62e442018-09-13 15:01:02 -0700675 return STATUS_BAD_VALUE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700676 }
677
678 if (binder == nullptr || *in == nullptr || out == nullptr) {
Steven Moreland009fc1a2022-06-10 17:24:25 +0000679 LOG(ERROR) << __func__ << ": requires non-null parameters binder (" << binder << "), in ("
680 << in << "), and out (" << out << ").";
Steven Moreland5d62e442018-09-13 15:01:02 -0700681 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700682 }
683
684 if ((*in)->getBinder() != binder) {
685 LOG(ERROR) << __func__ << ": parcel is associated with binder object " << binder
686 << " but called with " << (*in)->getBinder();
Steven Moreland5d62e442018-09-13 15:01:02 -0700687 return STATUS_BAD_VALUE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700688 }
689
690 *out = new AParcel(binder);
691
Steven Morelandf18615b2018-09-14 11:43:06 -0700692 status_t status = binder->getBinder()->transact(code, *(*in)->get(), (*out)->get(), flags);
Steven Moreland5d62e442018-09-13 15:01:02 -0700693 binder_status_t ret = PruneStatusT(status);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700694
Steven Moreland5d62e442018-09-13 15:01:02 -0700695 if (ret != STATUS_OK) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700696 delete *out;
697 *out = nullptr;
698 }
699
Steven Moreland5d62e442018-09-13 15:01:02 -0700700 return ret;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700701}
Steven Moreland901c7452018-09-04 16:17:28 -0700702
703AIBinder_DeathRecipient* AIBinder_DeathRecipient_new(
704 AIBinder_DeathRecipient_onBinderDied onBinderDied) {
705 if (onBinderDied == nullptr) {
706 LOG(ERROR) << __func__ << ": requires non-null onBinderDied parameter.";
707 return nullptr;
708 }
Steven Moreland64127ca2019-03-13 09:25:44 -0700709 auto ret = new AIBinder_DeathRecipient(onBinderDied);
710 ret->incStrong(nullptr);
711 return ret;
Steven Moreland901c7452018-09-04 16:17:28 -0700712}
713
Alice Ryhlea9d9d22021-08-27 07:51:30 +0000714void AIBinder_DeathRecipient_setOnUnlinked(AIBinder_DeathRecipient* recipient,
715 AIBinder_DeathRecipient_onBinderUnlinked onUnlinked) {
716 if (recipient == nullptr) {
717 return;
718 }
719
720 recipient->setOnUnlinked(onUnlinked);
721}
722
Steven Moreland9b80e282018-09-19 13:57:23 -0700723void AIBinder_DeathRecipient_delete(AIBinder_DeathRecipient* recipient) {
Steven Moreland64127ca2019-03-13 09:25:44 -0700724 if (recipient == nullptr) {
725 return;
726 }
727
728 recipient->decStrong(nullptr);
Steven Moreland901c7452018-09-04 16:17:28 -0700729}
Steven Morelandea14ef22019-08-20 10:50:08 -0700730
731binder_status_t AIBinder_getExtension(AIBinder* binder, AIBinder** outExt) {
732 if (binder == nullptr || outExt == nullptr) {
733 if (outExt != nullptr) {
734 *outExt = nullptr;
735 }
736 return STATUS_UNEXPECTED_NULL;
737 }
738
739 sp<IBinder> ext;
740 status_t res = binder->getBinder()->getExtension(&ext);
741
742 if (res != android::OK) {
743 *outExt = nullptr;
744 return PruneStatusT(res);
745 }
746
747 sp<AIBinder> ret = ABpBinder::lookupOrCreateFromBinder(ext);
748 if (ret != nullptr) ret->incStrong(binder);
749
750 *outExt = ret.get();
751 return STATUS_OK;
752}
753
754binder_status_t AIBinder_setExtension(AIBinder* binder, AIBinder* ext) {
755 if (binder == nullptr || ext == nullptr) {
756 return STATUS_UNEXPECTED_NULL;
757 }
758
759 ABBinder* rawBinder = binder->asABBinder();
760 if (rawBinder == nullptr) {
761 return STATUS_INVALID_OPERATION;
762 }
763
764 rawBinder->setExtension(ext->getBinder());
765 return STATUS_OK;
766}
Steven Moreland2f405f52020-07-08 22:24:29 +0000767
768// platform methods follow
769
770void AIBinder_setRequestingSid(AIBinder* binder, bool requestingSid) {
771 ABBinder* localBinder = binder->asABBinder();
772 if (localBinder == nullptr) {
773 LOG(FATAL) << "AIBinder_setRequestingSid must be called on a local binder";
774 }
775
776 localBinder->setRequestingSid(requestingSid);
777}
778
779const char* AIBinder_getCallingSid() {
780 return ::android::IPCThreadState::self()->getCallingSid();
781}
Steven Morelandb2de9532020-05-29 21:44:41 +0000782
Ady Abraham96174ac2021-10-15 11:02:03 -0700783void AIBinder_setMinSchedulerPolicy(AIBinder* binder, int policy, int priority) {
784 binder->asABBinder()->setMinSchedulerPolicy(policy, priority);
785}
Steven Moreland454a26a2021-12-14 01:26:21 +0000786
787void AIBinder_setInheritRt(AIBinder* binder, bool inheritRt) {
788 ABBinder* localBinder = binder->asABBinder();
789 if (localBinder == nullptr) {
790 LOG(FATAL) << "AIBinder_setInheritRt must be called on a local binder";
791 }
792
793 localBinder->setInheritRt(inheritRt);
794}