blob: 9778ec0a76367b88af85d0710153b3305e2e110a [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 Moreland8afad292020-11-06 22:26:51 +000019#include <android/binder_libbinder.h>
Steven Moreland4d5ad492018-09-13 12:49:16 -070020#include "ibinder_internal.h"
Steven Moreland2e87adc2018-08-20 19:47:00 -070021
Steven Moreland46b5fea2019-10-15 11:22:18 -070022#include <android/binder_stability.h>
Steven Moreland2e87adc2018-08-20 19:47:00 -070023#include <android/binder_status.h>
Steven Moreland4d5ad492018-09-13 12:49:16 -070024#include "parcel_internal.h"
Steven Moreland5d62e442018-09-13 15:01:02 -070025#include "status_internal.h"
Steven Moreland2e87adc2018-08-20 19:47:00 -070026
27#include <android-base/logging.h>
Steven Morelandf3034b02018-11-12 17:37:46 -080028#include <binder/IPCThreadState.h>
Ruchir Rastogicc7a7462020-01-31 14:29:15 -080029#include <binder/IResultReceiver.h>
30#include <private/android_filesystem_config.h>
Steven Moreland2e87adc2018-08-20 19:47:00 -070031
Steven Moreland901c7452018-09-04 16:17:28 -070032using DeathRecipient = ::android::IBinder::DeathRecipient;
33
Steven Moreland2e87adc2018-08-20 19:47:00 -070034using ::android::IBinder;
Ruchir Rastogicc7a7462020-01-31 14:29:15 -080035using ::android::IResultReceiver;
Steven Moreland2e87adc2018-08-20 19:47:00 -070036using ::android::Parcel;
37using ::android::sp;
Steven Moreland5d62e442018-09-13 15:01:02 -070038using ::android::status_t;
Steven Moreland009fc1a2022-06-10 17:24:25 +000039using ::android::statusToString;
Steven Moreland2e87adc2018-08-20 19:47:00 -070040using ::android::String16;
Steven Morelanda194c452019-03-04 16:47:07 -080041using ::android::String8;
Steven Moreland2e87adc2018-08-20 19:47:00 -070042using ::android::wp;
43
Steven Moreland71cddc32018-08-30 23:39:22 -070044namespace ABBinderTag {
45
46static const void* kId = "ABBinder";
47static void* kValue = static_cast<void*>(new bool{true});
Steven Moreland94968952018-09-05 14:42:59 -070048void clean(const void* /*id*/, void* /*obj*/, void* /*cookie*/){/* do nothing */};
Steven Moreland71cddc32018-08-30 23:39:22 -070049
50static void attach(const sp<IBinder>& binder) {
Steven Moreland9234c432021-06-29 23:01:10 +000051 // can only attach once
52 CHECK_EQ(nullptr, binder->attachObject(kId, kValue, nullptr /*cookie*/, clean));
Steven Moreland71cddc32018-08-30 23:39:22 -070053}
54static bool has(const sp<IBinder>& binder) {
55 return binder != nullptr && binder->findObject(kId) == kValue;
56}
57
Steven Moreland6cf66ac2018-11-02 18:14:54 -070058} // namespace ABBinderTag
Steven Moreland71cddc32018-08-30 23:39:22 -070059
Steven Moreland94968952018-09-05 14:42:59 -070060namespace ABpBinderTag {
61
Steven Moreland94968952018-09-05 14:42:59 -070062static const void* kId = "ABpBinder";
63struct Value {
64 wp<ABpBinder> binder;
65};
66void clean(const void* id, void* obj, void* cookie) {
Steven Moreland3acd4902022-07-26 18:29:58 +000067 // be weary of leaks!
68 // LOG(INFO) << "Deleting an ABpBinder";
69
Steven Moreland94968952018-09-05 14:42:59 -070070 CHECK(id == kId) << id << " " << obj << " " << cookie;
71
72 delete static_cast<Value*>(obj);
73};
74
Steven Moreland6cf66ac2018-11-02 18:14:54 -070075} // namespace ABpBinderTag
Steven Moreland94968952018-09-05 14:42:59 -070076
Steven Moreland2e87adc2018-08-20 19:47:00 -070077AIBinder::AIBinder(const AIBinder_Class* clazz) : mClazz(clazz) {}
78AIBinder::~AIBinder() {}
79
Andrei Homescu9556bb12020-09-21 16:59:45 -070080std::optional<bool> AIBinder::associateClassInternal(const AIBinder_Class* clazz,
Steven Moreland63872b52020-10-02 19:28:23 +000081 const String16& newDescriptor, bool set) {
Andrei Homescu9556bb12020-09-21 16:59:45 -070082 std::lock_guard<std::mutex> lock(mClazzMutex);
Steven Moreland71cddc32018-08-30 23:39:22 -070083 if (mClazz == clazz) return true;
Steven Moreland2e87adc2018-08-20 19:47:00 -070084
Steven Moreland2e87adc2018-08-20 19:47:00 -070085 if (mClazz != nullptr) {
Steven Moreland63872b52020-10-02 19:28:23 +000086 const String16& currentDescriptor = mClazz->getInterfaceDescriptor();
Steven Moreland2e87adc2018-08-20 19:47:00 -070087 if (newDescriptor == currentDescriptor) {
88 LOG(ERROR) << __func__ << ": Class descriptors '" << currentDescriptor
Steven Moreland4d196972021-08-12 15:49:44 -070089 << "' match during associateClass, but they are different class objects ("
90 << clazz << " vs " << mClazz << "). Class descriptor collision?";
Steven Moreland71cddc32018-08-30 23:39:22 -070091 } else {
92 LOG(ERROR) << __func__
93 << ": Class cannot be associated on object which already has a class. "
94 "Trying to associate to '"
Steven Moreland63872b52020-10-02 19:28:23 +000095 << newDescriptor << "' but already set to '" << currentDescriptor << "'.";
Steven Moreland2e87adc2018-08-20 19:47:00 -070096 }
97
Steven Moreland71cddc32018-08-30 23:39:22 -070098 // always a failure because we know mClazz != clazz
99 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700100 }
101
Andrei Homescu9556bb12020-09-21 16:59:45 -0700102 if (set) {
103 // if this is a local object, it's not one known to libbinder_ndk
104 mClazz = clazz;
Steven Moreland9facfce2020-10-02 17:26:56 +0000105 return true;
Andrei Homescu9556bb12020-09-21 16:59:45 -0700106 }
107
108 return {};
109}
110
Jooyung Han1f9dcd32021-11-03 13:34:29 +0900111// b/175635923 libcxx causes "implicit-conversion" with a string with invalid char
112static std::string SanitizeString(const String16& str) {
113 std::string sanitized{String8(str)};
114 for (auto& c : sanitized) {
115 if (!isprint(c)) {
116 c = '?';
117 }
118 }
119 return sanitized;
120}
121
Andrei Homescu9556bb12020-09-21 16:59:45 -0700122bool AIBinder::associateClass(const AIBinder_Class* clazz) {
123 if (clazz == nullptr) return false;
124
Steven Moreland63872b52020-10-02 19:28:23 +0000125 const String16& newDescriptor = clazz->getInterfaceDescriptor();
Andrei Homescu9556bb12020-09-21 16:59:45 -0700126
127 auto result = associateClassInternal(clazz, newDescriptor, false);
128 if (result.has_value()) return *result;
129
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700130 CHECK(asABpBinder() != nullptr); // ABBinder always has a descriptor
Steven Moreland71cddc32018-08-30 23:39:22 -0700131
Steven Moreland63872b52020-10-02 19:28:23 +0000132 const String16& descriptor = getBinder()->getInterfaceDescriptor();
Steven Moreland2e87adc2018-08-20 19:47:00 -0700133 if (descriptor != newDescriptor) {
Steven Moreland640c4092020-07-08 00:19:58 +0000134 if (getBinder()->isBinderAlive()) {
Steven Moreland63872b52020-10-02 19:28:23 +0000135 LOG(ERROR) << __func__ << ": Expecting binder to have class '" << newDescriptor
Jooyung Han1f9dcd32021-11-03 13:34:29 +0900136 << "' but descriptor is actually '" << SanitizeString(descriptor) << "'.";
Steven Moreland640c4092020-07-08 00:19:58 +0000137 } else {
138 // b/155793159
Steven Moreland63872b52020-10-02 19:28:23 +0000139 LOG(ERROR) << __func__ << ": Cannot associate class '" << newDescriptor
Steven Moreland009fc1a2022-06-10 17:24:25 +0000140 << "' to dead binder with cached descriptor '" << SanitizeString(descriptor)
141 << "'.";
Steven Moreland640c4092020-07-08 00:19:58 +0000142 }
Steven Moreland71cddc32018-08-30 23:39:22 -0700143 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700144 }
145
Steven Moreland9facfce2020-10-02 17:26:56 +0000146 return associateClassInternal(clazz, newDescriptor, true).value();
Steven Moreland2e87adc2018-08-20 19:47:00 -0700147}
148
149ABBinder::ABBinder(const AIBinder_Class* clazz, void* userData)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700150 : AIBinder(clazz), BBinder(), mUserData(userData) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700151 CHECK(clazz != nullptr);
152}
153ABBinder::~ABBinder() {
154 getClass()->onDestroy(mUserData);
155}
156
157const String16& ABBinder::getInterfaceDescriptor() const {
158 return getClass()->getInterfaceDescriptor();
159}
160
Steven Morelanda194c452019-03-04 16:47:07 -0800161status_t ABBinder::dump(int fd, const ::android::Vector<String16>& args) {
162 AIBinder_onDump onDump = getClass()->onDump;
163
164 if (onDump == nullptr) {
165 return STATUS_OK;
166 }
167
168 // technically UINT32_MAX would be okay here, but INT32_MAX is expected since this may be
169 // null in Java
170 if (args.size() > INT32_MAX) {
171 LOG(ERROR) << "ABBinder::dump received too many arguments: " << args.size();
172 return STATUS_BAD_VALUE;
173 }
174
175 std::vector<String8> utf8Args; // owns memory of utf8s
176 utf8Args.reserve(args.size());
177 std::vector<const char*> utf8Pointers; // what can be passed over NDK API
178 utf8Pointers.reserve(args.size());
179
180 for (size_t i = 0; i < args.size(); i++) {
181 utf8Args.push_back(String8(args[i]));
182 utf8Pointers.push_back(utf8Args[i].c_str());
183 }
184
185 return onDump(this, fd, utf8Pointers.data(), utf8Pointers.size());
186}
187
Steven Moreland5d62e442018-09-13 15:01:02 -0700188status_t ABBinder::onTransact(transaction_code_t code, const Parcel& data, Parcel* reply,
189 binder_flags_t flags) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700190 if (isUserCommand(code)) {
Steven Morelandf3ed8062021-08-27 14:31:14 -0700191 if (getClass()->writeHeader && !data.checkInterface(this)) {
Steven Moreland9d089af2018-09-18 08:58:09 -0700192 return STATUS_BAD_TYPE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700193 }
194
195 const AParcel in = AParcel::readOnly(this, &data);
196 AParcel out = AParcel(this, reply, false /*owns*/);
197
Steven Moreland5d62e442018-09-13 15:01:02 -0700198 binder_status_t status = getClass()->onTransact(this, code, &in, &out);
199 return PruneStatusT(status);
Steven Moreland88234072020-08-06 19:32:45 +0000200 } else if (code == SHELL_COMMAND_TRANSACTION && getClass()->handleShellCommand != nullptr) {
Ruchir Rastogicc7a7462020-01-31 14:29:15 -0800201 int in = data.readFileDescriptor();
202 int out = data.readFileDescriptor();
203 int err = data.readFileDescriptor();
204
205 int argc = data.readInt32();
206 std::vector<String8> utf8Args; // owns memory of utf8s
207 std::vector<const char*> utf8Pointers; // what can be passed over NDK API
208 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
209 utf8Args.push_back(String8(data.readString16()));
210 utf8Pointers.push_back(utf8Args[i].c_str());
211 }
212
213 data.readStrongBinder(); // skip over the IShellCallback
214 sp<IResultReceiver> resultReceiver = IResultReceiver::asInterface(data.readStrongBinder());
215
216 // Shell commands should only be callable by ADB.
217 uid_t uid = AIBinder_getCallingUid();
218 if (uid != AID_ROOT && uid != AID_SHELL) {
219 if (resultReceiver != nullptr) {
220 resultReceiver->send(-1);
221 }
222 return STATUS_PERMISSION_DENIED;
223 }
224
225 // Check that the file descriptors are valid.
226 if (in == STATUS_BAD_TYPE || out == STATUS_BAD_TYPE || err == STATUS_BAD_TYPE) {
227 if (resultReceiver != nullptr) {
228 resultReceiver->send(-1);
229 }
230 return STATUS_BAD_VALUE;
231 }
232
233 binder_status_t status = getClass()->handleShellCommand(
234 this, in, out, err, utf8Pointers.data(), utf8Pointers.size());
235 if (resultReceiver != nullptr) {
236 resultReceiver->send(status);
237 }
238 return status;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700239 } else {
240 return BBinder::onTransact(code, data, reply, flags);
241 }
242}
243
Steven Moreland71cddc32018-08-30 23:39:22 -0700244ABpBinder::ABpBinder(const ::android::sp<::android::IBinder>& binder)
Steven Moreland3acd4902022-07-26 18:29:58 +0000245 : AIBinder(nullptr /*clazz*/), mRemote(binder) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700246 CHECK(binder != nullptr);
247}
248ABpBinder::~ABpBinder() {}
249
Steven Moreland94968952018-09-05 14:42:59 -0700250sp<AIBinder> ABpBinder::lookupOrCreateFromBinder(const ::android::sp<::android::IBinder>& binder) {
Steven Moreland71cddc32018-08-30 23:39:22 -0700251 if (binder == nullptr) {
252 return nullptr;
253 }
254 if (ABBinderTag::has(binder)) {
255 return static_cast<ABBinder*>(binder.get());
256 }
Steven Moreland94968952018-09-05 14:42:59 -0700257
Steven Moreland91a96b62021-06-30 23:07:13 +0000258 // The following code ensures that for a given binder object (remote or local), if it is not an
259 // ABBinder then at most one ABpBinder object exists in a given process representing it.
260
Steven Moreland1f1bed62021-06-25 21:50:50 +0000261 auto* value = static_cast<ABpBinderTag::Value*>(binder->findObject(ABpBinderTag::kId));
Steven Moreland94968952018-09-05 14:42:59 -0700262 if (value == nullptr) {
263 value = new ABpBinderTag::Value;
Steven Moreland1f1bed62021-06-25 21:50:50 +0000264 auto oldValue = static_cast<ABpBinderTag::Value*>(
265 binder->attachObject(ABpBinderTag::kId, static_cast<void*>(value),
266 nullptr /*cookie*/, ABpBinderTag::clean));
267
268 // allocated by another thread
269 if (oldValue) {
270 delete value;
271 value = oldValue;
272 }
Steven Moreland94968952018-09-05 14:42:59 -0700273 }
274
Steven Moreland1f1bed62021-06-25 21:50:50 +0000275 sp<ABpBinder> ret;
276 binder->withLock([&]() {
277 ret = value->binder.promote();
278 if (ret == nullptr) {
279 ret = sp<ABpBinder>::make(binder);
280 value->binder = ret;
281 }
282 });
Steven Moreland94968952018-09-05 14:42:59 -0700283
284 return ret;
Steven Moreland71cddc32018-08-30 23:39:22 -0700285}
286
Steven Moreland2e87adc2018-08-20 19:47:00 -0700287struct AIBinder_Weak {
288 wp<AIBinder> binder;
289};
290AIBinder_Weak* AIBinder_Weak_new(AIBinder* binder) {
Steven Moreland5ccb70f2018-09-04 16:30:21 -0700291 if (binder == nullptr) {
292 return nullptr;
293 }
294
Steven Moreland2e87adc2018-08-20 19:47:00 -0700295 return new AIBinder_Weak{wp<AIBinder>(binder)};
296}
Steven Moreland9b80e282018-09-19 13:57:23 -0700297void AIBinder_Weak_delete(AIBinder_Weak* weakBinder) {
298 delete weakBinder;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700299}
300AIBinder* AIBinder_Weak_promote(AIBinder_Weak* weakBinder) {
Steven Moreland5ccb70f2018-09-04 16:30:21 -0700301 if (weakBinder == nullptr) {
302 return nullptr;
303 }
304
Steven Moreland2e87adc2018-08-20 19:47:00 -0700305 sp<AIBinder> binder = weakBinder->binder.promote();
306 AIBinder_incStrong(binder.get());
307 return binder.get();
308}
309
Steven Morelandc70e0122021-01-08 02:44:53 +0000310AIBinder_Weak* AIBinder_Weak_clone(const AIBinder_Weak* weak) {
311 if (weak == nullptr) {
312 return nullptr;
313 }
314
315 return new AIBinder_Weak{weak->binder};
316}
317
318bool AIBinder_lt(const AIBinder* lhs, const AIBinder* rhs) {
319 if (lhs == nullptr || rhs == nullptr) return lhs < rhs;
320
321 return const_cast<AIBinder*>(lhs)->getBinder() < const_cast<AIBinder*>(rhs)->getBinder();
322}
323
324bool AIBinder_Weak_lt(const AIBinder_Weak* lhs, const AIBinder_Weak* rhs) {
325 if (lhs == nullptr || rhs == nullptr) return lhs < rhs;
326
327 return lhs->binder < rhs->binder;
328}
329
Steven Moreland2e87adc2018-08-20 19:47:00 -0700330AIBinder_Class::AIBinder_Class(const char* interfaceDescriptor, AIBinder_Class_onCreate onCreate,
331 AIBinder_Class_onDestroy onDestroy,
332 AIBinder_Class_onTransact onTransact)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700333 : onCreate(onCreate),
334 onDestroy(onDestroy),
335 onTransact(onTransact),
Stephen Crane8fde87f2020-11-17 15:06:11 -0800336 mInterfaceDescriptor(interfaceDescriptor),
337 mWideInterfaceDescriptor(interfaceDescriptor) {}
Steven Moreland2e87adc2018-08-20 19:47:00 -0700338
339AIBinder_Class* AIBinder_Class_define(const char* interfaceDescriptor,
340 AIBinder_Class_onCreate onCreate,
341 AIBinder_Class_onDestroy onDestroy,
342 AIBinder_Class_onTransact onTransact) {
343 if (interfaceDescriptor == nullptr || onCreate == nullptr || onDestroy == nullptr ||
344 onTransact == nullptr) {
345 return nullptr;
346 }
347
348 return new AIBinder_Class(interfaceDescriptor, onCreate, onDestroy, onTransact);
349}
350
Steven Morelanda194c452019-03-04 16:47:07 -0800351void AIBinder_Class_setOnDump(AIBinder_Class* clazz, AIBinder_onDump onDump) {
352 CHECK(clazz != nullptr) << "setOnDump requires non-null clazz";
353
354 // this is required to be called before instances are instantiated
355 clazz->onDump = onDump;
356}
357
Steven Morelandf3ed8062021-08-27 14:31:14 -0700358void AIBinder_Class_disableInterfaceTokenHeader(AIBinder_Class* clazz) {
359 CHECK(clazz != nullptr) << "disableInterfaceTokenHeader requires non-null clazz";
360
361 clazz->writeHeader = false;
362}
363
Ruchir Rastogicc7a7462020-01-31 14:29:15 -0800364void AIBinder_Class_setHandleShellCommand(AIBinder_Class* clazz,
365 AIBinder_handleShellCommand handleShellCommand) {
366 CHECK(clazz != nullptr) << "setHandleShellCommand requires non-null clazz";
367
368 clazz->handleShellCommand = handleShellCommand;
369}
370
Stephen Crane8fde87f2020-11-17 15:06:11 -0800371const char* AIBinder_Class_getDescriptor(const AIBinder_Class* clazz) {
372 CHECK(clazz != nullptr) << "getDescriptor requires non-null clazz";
373
374 return clazz->getInterfaceDescriptorUtf8();
375}
376
Alice Ryhlea9d9d22021-08-27 07:51:30 +0000377AIBinder_DeathRecipient::TransferDeathRecipient::~TransferDeathRecipient() {
378 if (mOnUnlinked != nullptr) {
379 mOnUnlinked(mCookie);
380 }
381}
382
Steven Moreland901c7452018-09-04 16:17:28 -0700383void AIBinder_DeathRecipient::TransferDeathRecipient::binderDied(const wp<IBinder>& who) {
Steven Moreland8bcb5032021-04-07 23:06:43 +0000384 CHECK(who == mWho) << who.unsafe_get() << "(" << who.get_refs() << ") vs " << mWho.unsafe_get()
385 << " (" << mWho.get_refs() << ")";
Steven Moreland901c7452018-09-04 16:17:28 -0700386
387 mOnDied(mCookie);
Steven Moreland64127ca2019-03-13 09:25:44 -0700388
389 sp<AIBinder_DeathRecipient> recipient = mParentRecipient.promote();
390 sp<IBinder> strongWho = who.promote();
391
392 // otherwise this will be cleaned up later with pruneDeadTransferEntriesLocked
393 if (recipient != nullptr && strongWho != nullptr) {
394 status_t result = recipient->unlinkToDeath(strongWho, mCookie);
395 if (result != ::android::DEAD_OBJECT) {
396 LOG(WARNING) << "Unlinking to dead binder resulted in: " << result;
397 }
398 }
399
Steven Moreland901c7452018-09-04 16:17:28 -0700400 mWho = nullptr;
401}
402
403AIBinder_DeathRecipient::AIBinder_DeathRecipient(AIBinder_DeathRecipient_onBinderDied onDied)
Alice Ryhlea9d9d22021-08-27 07:51:30 +0000404 : mOnDied(onDied), mOnUnlinked(nullptr) {
Steven Moreland901c7452018-09-04 16:17:28 -0700405 CHECK(onDied != nullptr);
406}
407
Steven Moreland64127ca2019-03-13 09:25:44 -0700408void AIBinder_DeathRecipient::pruneDeadTransferEntriesLocked() {
409 mDeathRecipients.erase(std::remove_if(mDeathRecipients.begin(), mDeathRecipients.end(),
410 [](const sp<TransferDeathRecipient>& tdr) {
411 return tdr->getWho() == nullptr;
412 }),
413 mDeathRecipients.end());
414}
415
Jiyong Park4f97a8c2020-11-16 18:11:14 +0900416binder_status_t AIBinder_DeathRecipient::linkToDeath(const sp<IBinder>& binder, void* cookie) {
Steven Moreland901c7452018-09-04 16:17:28 -0700417 CHECK(binder != nullptr);
418
419 std::lock_guard<std::mutex> l(mDeathRecipientsMutex);
420
421 sp<TransferDeathRecipient> recipient =
Alice Ryhlea9d9d22021-08-27 07:51:30 +0000422 new TransferDeathRecipient(binder, cookie, this, mOnDied, mOnUnlinked);
Steven Moreland901c7452018-09-04 16:17:28 -0700423
Steven Moreland64127ca2019-03-13 09:25:44 -0700424 status_t status = binder->linkToDeath(recipient, cookie, 0 /*flags*/);
Steven Moreland5d62e442018-09-13 15:01:02 -0700425 if (status != STATUS_OK) {
Alice Ryhlea9d9d22021-08-27 07:51:30 +0000426 // When we failed to link, the destructor of TransferDeathRecipient runs here, which
427 // ensures that mOnUnlinked is called before we return with an error from this method.
Steven Moreland5d62e442018-09-13 15:01:02 -0700428 return PruneStatusT(status);
Steven Moreland901c7452018-09-04 16:17:28 -0700429 }
430
431 mDeathRecipients.push_back(recipient);
Steven Moreland64127ca2019-03-13 09:25:44 -0700432
433 pruneDeadTransferEntriesLocked();
Steven Moreland5d62e442018-09-13 15:01:02 -0700434 return STATUS_OK;
Steven Moreland901c7452018-09-04 16:17:28 -0700435}
436
Jiyong Park4f97a8c2020-11-16 18:11:14 +0900437binder_status_t AIBinder_DeathRecipient::unlinkToDeath(const sp<IBinder>& binder, void* cookie) {
Steven Moreland901c7452018-09-04 16:17:28 -0700438 CHECK(binder != nullptr);
439
440 std::lock_guard<std::mutex> l(mDeathRecipientsMutex);
441
442 for (auto it = mDeathRecipients.rbegin(); it != mDeathRecipients.rend(); ++it) {
443 sp<TransferDeathRecipient> recipient = *it;
444
Steven Moreland64127ca2019-03-13 09:25:44 -0700445 if (recipient->getCookie() == cookie && recipient->getWho() == binder) {
Steven Moreland901c7452018-09-04 16:17:28 -0700446 mDeathRecipients.erase(it.base() - 1);
447
Steven Moreland64127ca2019-03-13 09:25:44 -0700448 status_t status = binder->unlinkToDeath(recipient, cookie, 0 /*flags*/);
Steven Moreland5d62e442018-09-13 15:01:02 -0700449 if (status != ::android::OK) {
Steven Moreland901c7452018-09-04 16:17:28 -0700450 LOG(ERROR) << __func__
Steven Moreland009fc1a2022-06-10 17:24:25 +0000451 << ": removed reference to death recipient but unlink failed: "
452 << statusToString(status);
Steven Moreland901c7452018-09-04 16:17:28 -0700453 }
Steven Moreland5d62e442018-09-13 15:01:02 -0700454 return PruneStatusT(status);
Steven Moreland901c7452018-09-04 16:17:28 -0700455 }
456 }
457
Steven Moreland5d62e442018-09-13 15:01:02 -0700458 return STATUS_NAME_NOT_FOUND;
Steven Moreland901c7452018-09-04 16:17:28 -0700459}
460
Alice Ryhlea9d9d22021-08-27 07:51:30 +0000461void AIBinder_DeathRecipient::setOnUnlinked(AIBinder_DeathRecipient_onBinderUnlinked onUnlinked) {
462 mOnUnlinked = onUnlinked;
463}
464
Steven Moreland901c7452018-09-04 16:17:28 -0700465// start of C-API methods
466
Steven Moreland2e87adc2018-08-20 19:47:00 -0700467AIBinder* AIBinder_new(const AIBinder_Class* clazz, void* args) {
468 if (clazz == nullptr) {
469 LOG(ERROR) << __func__ << ": Must provide class to construct local binder.";
470 return nullptr;
471 }
472
473 void* userData = clazz->onCreate(args);
474
Steven Moreland71cddc32018-08-30 23:39:22 -0700475 sp<AIBinder> ret = new ABBinder(clazz, userData);
476 ABBinderTag::attach(ret->getBinder());
477
478 AIBinder_incStrong(ret.get());
479 return ret.get();
Steven Moreland2e87adc2018-08-20 19:47:00 -0700480}
481
Steven Moreland5ea54da2018-09-04 13:29:55 -0700482bool AIBinder_isRemote(const AIBinder* binder) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700483 if (binder == nullptr) {
Steven Moreland56f752a2018-11-05 17:23:37 -0800484 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700485 }
486
487 return binder->isRemote();
488}
489
Steven Moreland65867d72018-09-04 14:22:26 -0700490bool AIBinder_isAlive(const AIBinder* binder) {
491 if (binder == nullptr) {
492 return false;
493 }
494
495 return const_cast<AIBinder*>(binder)->getBinder()->isBinderAlive();
496}
497
498binder_status_t AIBinder_ping(AIBinder* binder) {
499 if (binder == nullptr) {
Steven Moreland5d62e442018-09-13 15:01:02 -0700500 return STATUS_UNEXPECTED_NULL;
Steven Moreland65867d72018-09-04 14:22:26 -0700501 }
502
Steven Moreland5d62e442018-09-13 15:01:02 -0700503 return PruneStatusT(binder->getBinder()->pingBinder());
Steven Moreland65867d72018-09-04 14:22:26 -0700504}
505
Steven Morelanda194c452019-03-04 16:47:07 -0800506binder_status_t AIBinder_dump(AIBinder* binder, int fd, const char** args, uint32_t numArgs) {
507 if (binder == nullptr) {
508 return STATUS_UNEXPECTED_NULL;
509 }
510
511 ABBinder* bBinder = binder->asABBinder();
512 if (bBinder != nullptr) {
513 AIBinder_onDump onDump = binder->getClass()->onDump;
514 if (onDump == nullptr) {
515 return STATUS_OK;
516 }
517 return PruneStatusT(onDump(bBinder, fd, args, numArgs));
518 }
519
520 ::android::Vector<String16> utf16Args;
521 utf16Args.setCapacity(numArgs);
522 for (uint32_t i = 0; i < numArgs; i++) {
523 utf16Args.push(String16(String8(args[i])));
524 }
525
526 status_t status = binder->getBinder()->dump(fd, utf16Args);
527 return PruneStatusT(status);
528}
529
Steven Moreland901c7452018-09-04 16:17:28 -0700530binder_status_t AIBinder_linkToDeath(AIBinder* binder, AIBinder_DeathRecipient* recipient,
531 void* cookie) {
532 if (binder == nullptr || recipient == nullptr) {
Steven Moreland009fc1a2022-06-10 17:24:25 +0000533 LOG(ERROR) << __func__ << ": Must provide binder (" << binder << ") and recipient ("
534 << recipient << ")";
Steven Moreland5d62e442018-09-13 15:01:02 -0700535 return STATUS_UNEXPECTED_NULL;
Steven Moreland901c7452018-09-04 16:17:28 -0700536 }
537
Steven Moreland5d62e442018-09-13 15:01:02 -0700538 // returns binder_status_t
Steven Moreland64127ca2019-03-13 09:25:44 -0700539 return recipient->linkToDeath(binder->getBinder(), cookie);
Steven Moreland901c7452018-09-04 16:17:28 -0700540}
541
542binder_status_t AIBinder_unlinkToDeath(AIBinder* binder, AIBinder_DeathRecipient* recipient,
543 void* cookie) {
544 if (binder == nullptr || recipient == nullptr) {
Steven Moreland009fc1a2022-06-10 17:24:25 +0000545 LOG(ERROR) << __func__ << ": Must provide binder (" << binder << ") and recipient ("
546 << recipient << ")";
Steven Moreland5d62e442018-09-13 15:01:02 -0700547 return STATUS_UNEXPECTED_NULL;
Steven Moreland901c7452018-09-04 16:17:28 -0700548 }
549
Steven Moreland5d62e442018-09-13 15:01:02 -0700550 // returns binder_status_t
Steven Moreland64127ca2019-03-13 09:25:44 -0700551 return recipient->unlinkToDeath(binder->getBinder(), cookie);
Steven Moreland901c7452018-09-04 16:17:28 -0700552}
553
Steven Morelandf3034b02018-11-12 17:37:46 -0800554uid_t AIBinder_getCallingUid() {
555 return ::android::IPCThreadState::self()->getCallingUid();
556}
557
558pid_t AIBinder_getCallingPid() {
559 return ::android::IPCThreadState::self()->getCallingPid();
560}
561
Alice Ryhl59aeae82021-08-25 10:21:26 +0000562bool AIBinder_isHandlingTransaction() {
563 return ::android::IPCThreadState::self()->getServingStackPointer() != nullptr;
564}
565
Steven Moreland2e87adc2018-08-20 19:47:00 -0700566void AIBinder_incStrong(AIBinder* binder) {
567 if (binder == nullptr) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700568 return;
569 }
570
571 binder->incStrong(nullptr);
572}
573void AIBinder_decStrong(AIBinder* binder) {
574 if (binder == nullptr) {
575 LOG(ERROR) << __func__ << ": on null binder";
576 return;
577 }
578
579 binder->decStrong(nullptr);
580}
581int32_t AIBinder_debugGetRefCount(AIBinder* binder) {
582 if (binder == nullptr) {
583 LOG(ERROR) << __func__ << ": on null binder";
584 return -1;
585 }
586
587 return binder->getStrongCount();
588}
589
Steven Moreland71cddc32018-08-30 23:39:22 -0700590bool AIBinder_associateClass(AIBinder* binder, const AIBinder_Class* clazz) {
591 if (binder == nullptr) {
592 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700593 }
594
Steven Moreland71cddc32018-08-30 23:39:22 -0700595 return binder->associateClass(clazz);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700596}
597
598const AIBinder_Class* AIBinder_getClass(AIBinder* binder) {
599 if (binder == nullptr) {
600 return nullptr;
601 }
602
603 return binder->getClass();
604}
605
606void* AIBinder_getUserData(AIBinder* binder) {
607 if (binder == nullptr) {
608 return nullptr;
609 }
610
611 ABBinder* bBinder = binder->asABBinder();
612 if (bBinder == nullptr) {
613 return nullptr;
614 }
615
616 return bBinder->getUserData();
617}
618
619binder_status_t AIBinder_prepareTransaction(AIBinder* binder, AParcel** in) {
620 if (binder == nullptr || in == nullptr) {
Steven Moreland009fc1a2022-06-10 17:24:25 +0000621 LOG(ERROR) << __func__ << ": requires non-null parameters binder (" << binder
622 << ") and in (" << in << ").";
Steven Moreland5d62e442018-09-13 15:01:02 -0700623 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700624 }
625 const AIBinder_Class* clazz = binder->getClass();
626 if (clazz == nullptr) {
627 LOG(ERROR) << __func__
628 << ": Class must be defined for a remote binder transaction. See "
629 "AIBinder_associateClass.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700630 return STATUS_INVALID_OPERATION;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700631 }
632
633 *in = new AParcel(binder);
Steven Moreland1fda67b2021-04-02 18:35:50 +0000634 (*in)->get()->markForBinder(binder->getBinder());
635
Steven Morelandf3ed8062021-08-27 14:31:14 -0700636 status_t status = android::OK;
637 if (clazz->writeHeader) {
638 status = (*in)->get()->writeInterfaceToken(clazz->getInterfaceDescriptor());
639 }
Steven Moreland5d62e442018-09-13 15:01:02 -0700640 binder_status_t ret = PruneStatusT(status);
641
642 if (ret != STATUS_OK) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700643 delete *in;
644 *in = nullptr;
645 }
646
Steven Moreland5d62e442018-09-13 15:01:02 -0700647 return ret;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700648}
649
Steven Moreland9b80e282018-09-19 13:57:23 -0700650static void DestroyParcel(AParcel** parcel) {
651 delete *parcel;
652 *parcel = nullptr;
653}
654
Steven Moreland2e87adc2018-08-20 19:47:00 -0700655binder_status_t AIBinder_transact(AIBinder* binder, transaction_code_t code, AParcel** in,
656 AParcel** out, binder_flags_t flags) {
657 if (in == nullptr) {
658 LOG(ERROR) << __func__ << ": requires non-null in parameter";
Steven Moreland5d62e442018-09-13 15:01:02 -0700659 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700660 }
661
Steven Morelandcaa776c2018-09-04 13:48:11 -0700662 using AutoParcelDestroyer = std::unique_ptr<AParcel*, void (*)(AParcel**)>;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700663 // This object is the input to the transaction. This function takes ownership of it and deletes
664 // it.
Steven Moreland9b80e282018-09-19 13:57:23 -0700665 AutoParcelDestroyer forIn(in, DestroyParcel);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700666
667 if (!isUserCommand(code)) {
Steven Moreland009fc1a2022-06-10 17:24:25 +0000668 LOG(ERROR) << __func__
669 << ": Only user-defined transactions can be made from the NDK, but requested: "
670 << code;
Steven Moreland5d62e442018-09-13 15:01:02 -0700671 return STATUS_UNKNOWN_TRANSACTION;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700672 }
673
Steven Morelandf183fdd2020-10-27 00:12:12 +0000674 constexpr binder_flags_t kAllFlags = FLAG_PRIVATE_VENDOR | FLAG_ONEWAY | FLAG_CLEAR_BUF;
Steven Moreland46b5fea2019-10-15 11:22:18 -0700675 if ((flags & ~kAllFlags) != 0) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700676 LOG(ERROR) << __func__ << ": Unrecognized flags sent: " << flags;
Steven Moreland5d62e442018-09-13 15:01:02 -0700677 return STATUS_BAD_VALUE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700678 }
679
680 if (binder == nullptr || *in == nullptr || out == nullptr) {
Steven Moreland009fc1a2022-06-10 17:24:25 +0000681 LOG(ERROR) << __func__ << ": requires non-null parameters binder (" << binder << "), in ("
682 << in << "), and out (" << out << ").";
Steven Moreland5d62e442018-09-13 15:01:02 -0700683 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700684 }
685
686 if ((*in)->getBinder() != binder) {
687 LOG(ERROR) << __func__ << ": parcel is associated with binder object " << binder
688 << " but called with " << (*in)->getBinder();
Steven Moreland5d62e442018-09-13 15:01:02 -0700689 return STATUS_BAD_VALUE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700690 }
691
692 *out = new AParcel(binder);
693
Steven Morelandf18615b2018-09-14 11:43:06 -0700694 status_t status = binder->getBinder()->transact(code, *(*in)->get(), (*out)->get(), flags);
Steven Moreland5d62e442018-09-13 15:01:02 -0700695 binder_status_t ret = PruneStatusT(status);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700696
Steven Moreland5d62e442018-09-13 15:01:02 -0700697 if (ret != STATUS_OK) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700698 delete *out;
699 *out = nullptr;
700 }
701
Steven Moreland5d62e442018-09-13 15:01:02 -0700702 return ret;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700703}
Steven Moreland901c7452018-09-04 16:17:28 -0700704
705AIBinder_DeathRecipient* AIBinder_DeathRecipient_new(
706 AIBinder_DeathRecipient_onBinderDied onBinderDied) {
707 if (onBinderDied == nullptr) {
708 LOG(ERROR) << __func__ << ": requires non-null onBinderDied parameter.";
709 return nullptr;
710 }
Steven Moreland64127ca2019-03-13 09:25:44 -0700711 auto ret = new AIBinder_DeathRecipient(onBinderDied);
712 ret->incStrong(nullptr);
713 return ret;
Steven Moreland901c7452018-09-04 16:17:28 -0700714}
715
Alice Ryhlea9d9d22021-08-27 07:51:30 +0000716void AIBinder_DeathRecipient_setOnUnlinked(AIBinder_DeathRecipient* recipient,
717 AIBinder_DeathRecipient_onBinderUnlinked onUnlinked) {
718 if (recipient == nullptr) {
719 return;
720 }
721
722 recipient->setOnUnlinked(onUnlinked);
723}
724
Steven Moreland9b80e282018-09-19 13:57:23 -0700725void AIBinder_DeathRecipient_delete(AIBinder_DeathRecipient* recipient) {
Steven Moreland64127ca2019-03-13 09:25:44 -0700726 if (recipient == nullptr) {
727 return;
728 }
729
730 recipient->decStrong(nullptr);
Steven Moreland901c7452018-09-04 16:17:28 -0700731}
Steven Morelandea14ef22019-08-20 10:50:08 -0700732
733binder_status_t AIBinder_getExtension(AIBinder* binder, AIBinder** outExt) {
734 if (binder == nullptr || outExt == nullptr) {
735 if (outExt != nullptr) {
736 *outExt = nullptr;
737 }
738 return STATUS_UNEXPECTED_NULL;
739 }
740
741 sp<IBinder> ext;
742 status_t res = binder->getBinder()->getExtension(&ext);
743
744 if (res != android::OK) {
745 *outExt = nullptr;
746 return PruneStatusT(res);
747 }
748
749 sp<AIBinder> ret = ABpBinder::lookupOrCreateFromBinder(ext);
750 if (ret != nullptr) ret->incStrong(binder);
751
752 *outExt = ret.get();
753 return STATUS_OK;
754}
755
756binder_status_t AIBinder_setExtension(AIBinder* binder, AIBinder* ext) {
757 if (binder == nullptr || ext == nullptr) {
758 return STATUS_UNEXPECTED_NULL;
759 }
760
761 ABBinder* rawBinder = binder->asABBinder();
762 if (rawBinder == nullptr) {
763 return STATUS_INVALID_OPERATION;
764 }
765
766 rawBinder->setExtension(ext->getBinder());
767 return STATUS_OK;
768}
Steven Moreland2f405f52020-07-08 22:24:29 +0000769
770// platform methods follow
771
772void AIBinder_setRequestingSid(AIBinder* binder, bool requestingSid) {
773 ABBinder* localBinder = binder->asABBinder();
774 if (localBinder == nullptr) {
775 LOG(FATAL) << "AIBinder_setRequestingSid must be called on a local binder";
776 }
777
778 localBinder->setRequestingSid(requestingSid);
779}
780
781const char* AIBinder_getCallingSid() {
782 return ::android::IPCThreadState::self()->getCallingSid();
783}
Steven Morelandb2de9532020-05-29 21:44:41 +0000784
785android::sp<android::IBinder> AIBinder_toPlatformBinder(AIBinder* binder) {
786 if (binder == nullptr) return nullptr;
787 return binder->getBinder();
788}
789
790AIBinder* AIBinder_fromPlatformBinder(const android::sp<android::IBinder>& binder) {
791 sp<AIBinder> ndkBinder = ABpBinder::lookupOrCreateFromBinder(binder);
792 AIBinder_incStrong(ndkBinder.get());
793 return ndkBinder.get();
794}
Ady Abraham96174ac2021-10-15 11:02:03 -0700795
796void AIBinder_setMinSchedulerPolicy(AIBinder* binder, int policy, int priority) {
797 binder->asABBinder()->setMinSchedulerPolicy(policy, priority);
798}
Steven Moreland454a26a2021-12-14 01:26:21 +0000799
800void AIBinder_setInheritRt(AIBinder* binder, bool inheritRt) {
801 ABBinder* localBinder = binder->asABBinder();
802 if (localBinder == nullptr) {
803 LOG(FATAL) << "AIBinder_setInheritRt must be called on a local binder";
804 }
805
806 localBinder->setInheritRt(inheritRt);
807}