blob: b21a7e9584c5b2989533f830f4019e905624e85a [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) {
67 CHECK(id == kId) << id << " " << obj << " " << cookie;
68
69 delete static_cast<Value*>(obj);
70};
71
Steven Moreland6cf66ac2018-11-02 18:14:54 -070072} // namespace ABpBinderTag
Steven Moreland94968952018-09-05 14:42:59 -070073
Steven Moreland2e87adc2018-08-20 19:47:00 -070074AIBinder::AIBinder(const AIBinder_Class* clazz) : mClazz(clazz) {}
75AIBinder::~AIBinder() {}
76
Andrei Homescu9556bb12020-09-21 16:59:45 -070077std::optional<bool> AIBinder::associateClassInternal(const AIBinder_Class* clazz,
Steven Moreland63872b52020-10-02 19:28:23 +000078 const String16& newDescriptor, bool set) {
Andrei Homescu9556bb12020-09-21 16:59:45 -070079 std::lock_guard<std::mutex> lock(mClazzMutex);
Steven Moreland71cddc32018-08-30 23:39:22 -070080 if (mClazz == clazz) return true;
Steven Moreland2e87adc2018-08-20 19:47:00 -070081
Steven Moreland2e87adc2018-08-20 19:47:00 -070082 if (mClazz != nullptr) {
Steven Moreland63872b52020-10-02 19:28:23 +000083 const String16& currentDescriptor = mClazz->getInterfaceDescriptor();
Steven Moreland2e87adc2018-08-20 19:47:00 -070084 if (newDescriptor == currentDescriptor) {
85 LOG(ERROR) << __func__ << ": Class descriptors '" << currentDescriptor
Steven Moreland4d196972021-08-12 15:49:44 -070086 << "' match during associateClass, but they are different class objects ("
87 << clazz << " vs " << mClazz << "). Class descriptor collision?";
Steven Moreland71cddc32018-08-30 23:39:22 -070088 } else {
89 LOG(ERROR) << __func__
90 << ": Class cannot be associated on object which already has a class. "
91 "Trying to associate to '"
Steven Moreland63872b52020-10-02 19:28:23 +000092 << newDescriptor << "' but already set to '" << currentDescriptor << "'.";
Steven Moreland2e87adc2018-08-20 19:47:00 -070093 }
94
Steven Moreland71cddc32018-08-30 23:39:22 -070095 // always a failure because we know mClazz != clazz
96 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -070097 }
98
Andrei Homescu9556bb12020-09-21 16:59:45 -070099 if (set) {
100 // if this is a local object, it's not one known to libbinder_ndk
101 mClazz = clazz;
Steven Moreland9facfce2020-10-02 17:26:56 +0000102 return true;
Andrei Homescu9556bb12020-09-21 16:59:45 -0700103 }
104
105 return {};
106}
107
Jooyung Han1f9dcd32021-11-03 13:34:29 +0900108// b/175635923 libcxx causes "implicit-conversion" with a string with invalid char
109static std::string SanitizeString(const String16& str) {
110 std::string sanitized{String8(str)};
111 for (auto& c : sanitized) {
112 if (!isprint(c)) {
113 c = '?';
114 }
115 }
116 return sanitized;
117}
118
Andrei Homescu9556bb12020-09-21 16:59:45 -0700119bool AIBinder::associateClass(const AIBinder_Class* clazz) {
120 if (clazz == nullptr) return false;
121
Steven Moreland63872b52020-10-02 19:28:23 +0000122 const String16& newDescriptor = clazz->getInterfaceDescriptor();
Andrei Homescu9556bb12020-09-21 16:59:45 -0700123
124 auto result = associateClassInternal(clazz, newDescriptor, false);
125 if (result.has_value()) return *result;
126
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700127 CHECK(asABpBinder() != nullptr); // ABBinder always has a descriptor
Steven Moreland71cddc32018-08-30 23:39:22 -0700128
Steven Moreland63872b52020-10-02 19:28:23 +0000129 const String16& descriptor = getBinder()->getInterfaceDescriptor();
Steven Moreland2e87adc2018-08-20 19:47:00 -0700130 if (descriptor != newDescriptor) {
Steven Moreland640c4092020-07-08 00:19:58 +0000131 if (getBinder()->isBinderAlive()) {
Steven Moreland63872b52020-10-02 19:28:23 +0000132 LOG(ERROR) << __func__ << ": Expecting binder to have class '" << newDescriptor
Jooyung Han1f9dcd32021-11-03 13:34:29 +0900133 << "' but descriptor is actually '" << SanitizeString(descriptor) << "'.";
Steven Moreland640c4092020-07-08 00:19:58 +0000134 } else {
135 // b/155793159
Steven Moreland63872b52020-10-02 19:28:23 +0000136 LOG(ERROR) << __func__ << ": Cannot associate class '" << newDescriptor
Steven Moreland009fc1a2022-06-10 17:24:25 +0000137 << "' to dead binder with cached descriptor '" << SanitizeString(descriptor)
138 << "'.";
Steven Moreland640c4092020-07-08 00:19:58 +0000139 }
Steven Moreland71cddc32018-08-30 23:39:22 -0700140 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700141 }
142
Steven Moreland9facfce2020-10-02 17:26:56 +0000143 return associateClassInternal(clazz, newDescriptor, true).value();
Steven Moreland2e87adc2018-08-20 19:47:00 -0700144}
145
146ABBinder::ABBinder(const AIBinder_Class* clazz, void* userData)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700147 : AIBinder(clazz), BBinder(), mUserData(userData) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700148 CHECK(clazz != nullptr);
149}
150ABBinder::~ABBinder() {
151 getClass()->onDestroy(mUserData);
152}
153
154const String16& ABBinder::getInterfaceDescriptor() const {
155 return getClass()->getInterfaceDescriptor();
156}
157
Steven Morelanda194c452019-03-04 16:47:07 -0800158status_t ABBinder::dump(int fd, const ::android::Vector<String16>& args) {
159 AIBinder_onDump onDump = getClass()->onDump;
160
161 if (onDump == nullptr) {
162 return STATUS_OK;
163 }
164
165 // technically UINT32_MAX would be okay here, but INT32_MAX is expected since this may be
166 // null in Java
167 if (args.size() > INT32_MAX) {
168 LOG(ERROR) << "ABBinder::dump received too many arguments: " << args.size();
169 return STATUS_BAD_VALUE;
170 }
171
172 std::vector<String8> utf8Args; // owns memory of utf8s
173 utf8Args.reserve(args.size());
174 std::vector<const char*> utf8Pointers; // what can be passed over NDK API
175 utf8Pointers.reserve(args.size());
176
177 for (size_t i = 0; i < args.size(); i++) {
178 utf8Args.push_back(String8(args[i]));
179 utf8Pointers.push_back(utf8Args[i].c_str());
180 }
181
182 return onDump(this, fd, utf8Pointers.data(), utf8Pointers.size());
183}
184
Steven Moreland5d62e442018-09-13 15:01:02 -0700185status_t ABBinder::onTransact(transaction_code_t code, const Parcel& data, Parcel* reply,
186 binder_flags_t flags) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700187 if (isUserCommand(code)) {
Steven Morelandf3ed8062021-08-27 14:31:14 -0700188 if (getClass()->writeHeader && !data.checkInterface(this)) {
Steven Moreland9d089af2018-09-18 08:58:09 -0700189 return STATUS_BAD_TYPE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700190 }
191
192 const AParcel in = AParcel::readOnly(this, &data);
193 AParcel out = AParcel(this, reply, false /*owns*/);
194
Steven Moreland5d62e442018-09-13 15:01:02 -0700195 binder_status_t status = getClass()->onTransact(this, code, &in, &out);
196 return PruneStatusT(status);
Steven Moreland88234072020-08-06 19:32:45 +0000197 } else if (code == SHELL_COMMAND_TRANSACTION && getClass()->handleShellCommand != nullptr) {
Ruchir Rastogicc7a7462020-01-31 14:29:15 -0800198 int in = data.readFileDescriptor();
199 int out = data.readFileDescriptor();
200 int err = data.readFileDescriptor();
201
202 int argc = data.readInt32();
203 std::vector<String8> utf8Args; // owns memory of utf8s
204 std::vector<const char*> utf8Pointers; // what can be passed over NDK API
205 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
206 utf8Args.push_back(String8(data.readString16()));
207 utf8Pointers.push_back(utf8Args[i].c_str());
208 }
209
210 data.readStrongBinder(); // skip over the IShellCallback
211 sp<IResultReceiver> resultReceiver = IResultReceiver::asInterface(data.readStrongBinder());
212
213 // Shell commands should only be callable by ADB.
214 uid_t uid = AIBinder_getCallingUid();
215 if (uid != AID_ROOT && uid != AID_SHELL) {
216 if (resultReceiver != nullptr) {
217 resultReceiver->send(-1);
218 }
219 return STATUS_PERMISSION_DENIED;
220 }
221
222 // Check that the file descriptors are valid.
223 if (in == STATUS_BAD_TYPE || out == STATUS_BAD_TYPE || err == STATUS_BAD_TYPE) {
224 if (resultReceiver != nullptr) {
225 resultReceiver->send(-1);
226 }
227 return STATUS_BAD_VALUE;
228 }
229
230 binder_status_t status = getClass()->handleShellCommand(
231 this, in, out, err, utf8Pointers.data(), utf8Pointers.size());
232 if (resultReceiver != nullptr) {
233 resultReceiver->send(status);
234 }
235 return status;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700236 } else {
237 return BBinder::onTransact(code, data, reply, flags);
238 }
239}
240
Steven Moreland71cddc32018-08-30 23:39:22 -0700241ABpBinder::ABpBinder(const ::android::sp<::android::IBinder>& binder)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700242 : AIBinder(nullptr /*clazz*/), BpRefBase(binder) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700243 CHECK(binder != nullptr);
244}
245ABpBinder::~ABpBinder() {}
246
Steven Moreland94968952018-09-05 14:42:59 -0700247void ABpBinder::onLastStrongRef(const void* id) {
Steven Moreland2505ec42021-06-25 02:08:49 +0000248 // Since ABpBinder is OBJECT_LIFETIME_WEAK, we must remove this weak reference in order for
Steven Moreland91a96b62021-06-30 23:07:13 +0000249 // the ABpBinder to be deleted. Even though we have no more references on the ABpBinder
250 // (BpRefBase), the remote object may still exist (for instance, if we
251 // receive it from another process, before the ABpBinder is attached).
Steven Moreland94968952018-09-05 14:42:59 -0700252
Steven Moreland2505ec42021-06-25 02:08:49 +0000253 ABpBinderTag::Value* value =
Steven Moreland91a96b62021-06-30 23:07:13 +0000254 static_cast<ABpBinderTag::Value*>(remote()->findObject(ABpBinderTag::kId));
255 CHECK_NE(nullptr, value) << "ABpBinder must always be attached";
256
257 remote()->withLock([&]() { value->binder = nullptr; });
Steven Moreland94968952018-09-05 14:42:59 -0700258
259 BpRefBase::onLastStrongRef(id);
260}
261
262sp<AIBinder> ABpBinder::lookupOrCreateFromBinder(const ::android::sp<::android::IBinder>& binder) {
Steven Moreland71cddc32018-08-30 23:39:22 -0700263 if (binder == nullptr) {
264 return nullptr;
265 }
266 if (ABBinderTag::has(binder)) {
267 return static_cast<ABBinder*>(binder.get());
268 }
Steven Moreland94968952018-09-05 14:42:59 -0700269
Steven Moreland91a96b62021-06-30 23:07:13 +0000270 // The following code ensures that for a given binder object (remote or local), if it is not an
271 // ABBinder then at most one ABpBinder object exists in a given process representing it.
272
Steven Moreland1f1bed62021-06-25 21:50:50 +0000273 auto* value = static_cast<ABpBinderTag::Value*>(binder->findObject(ABpBinderTag::kId));
Steven Moreland94968952018-09-05 14:42:59 -0700274 if (value == nullptr) {
275 value = new ABpBinderTag::Value;
Steven Moreland1f1bed62021-06-25 21:50:50 +0000276 auto oldValue = static_cast<ABpBinderTag::Value*>(
277 binder->attachObject(ABpBinderTag::kId, static_cast<void*>(value),
278 nullptr /*cookie*/, ABpBinderTag::clean));
279
280 // allocated by another thread
281 if (oldValue) {
282 delete value;
283 value = oldValue;
284 }
Steven Moreland94968952018-09-05 14:42:59 -0700285 }
286
Steven Moreland1f1bed62021-06-25 21:50:50 +0000287 sp<ABpBinder> ret;
288 binder->withLock([&]() {
289 ret = value->binder.promote();
290 if (ret == nullptr) {
291 ret = sp<ABpBinder>::make(binder);
292 value->binder = ret;
293 }
294 });
Steven Moreland94968952018-09-05 14:42:59 -0700295
296 return ret;
Steven Moreland71cddc32018-08-30 23:39:22 -0700297}
298
Steven Moreland2e87adc2018-08-20 19:47:00 -0700299struct AIBinder_Weak {
300 wp<AIBinder> binder;
301};
302AIBinder_Weak* AIBinder_Weak_new(AIBinder* binder) {
Steven Moreland5ccb70f2018-09-04 16:30:21 -0700303 if (binder == nullptr) {
304 return nullptr;
305 }
306
Steven Moreland2e87adc2018-08-20 19:47:00 -0700307 return new AIBinder_Weak{wp<AIBinder>(binder)};
308}
Steven Moreland9b80e282018-09-19 13:57:23 -0700309void AIBinder_Weak_delete(AIBinder_Weak* weakBinder) {
310 delete weakBinder;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700311}
312AIBinder* AIBinder_Weak_promote(AIBinder_Weak* weakBinder) {
Steven Moreland5ccb70f2018-09-04 16:30:21 -0700313 if (weakBinder == nullptr) {
314 return nullptr;
315 }
316
Steven Moreland2e87adc2018-08-20 19:47:00 -0700317 sp<AIBinder> binder = weakBinder->binder.promote();
318 AIBinder_incStrong(binder.get());
319 return binder.get();
320}
321
Steven Morelandc70e0122021-01-08 02:44:53 +0000322AIBinder_Weak* AIBinder_Weak_clone(const AIBinder_Weak* weak) {
323 if (weak == nullptr) {
324 return nullptr;
325 }
326
327 return new AIBinder_Weak{weak->binder};
328}
329
330bool AIBinder_lt(const AIBinder* lhs, const AIBinder* rhs) {
331 if (lhs == nullptr || rhs == nullptr) return lhs < rhs;
332
333 return const_cast<AIBinder*>(lhs)->getBinder() < const_cast<AIBinder*>(rhs)->getBinder();
334}
335
336bool AIBinder_Weak_lt(const AIBinder_Weak* lhs, const AIBinder_Weak* rhs) {
337 if (lhs == nullptr || rhs == nullptr) return lhs < rhs;
338
339 return lhs->binder < rhs->binder;
340}
341
Steven Moreland2e87adc2018-08-20 19:47:00 -0700342AIBinder_Class::AIBinder_Class(const char* interfaceDescriptor, AIBinder_Class_onCreate onCreate,
343 AIBinder_Class_onDestroy onDestroy,
344 AIBinder_Class_onTransact onTransact)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700345 : onCreate(onCreate),
346 onDestroy(onDestroy),
347 onTransact(onTransact),
Stephen Crane8fde87f2020-11-17 15:06:11 -0800348 mInterfaceDescriptor(interfaceDescriptor),
349 mWideInterfaceDescriptor(interfaceDescriptor) {}
Steven Moreland2e87adc2018-08-20 19:47:00 -0700350
351AIBinder_Class* AIBinder_Class_define(const char* interfaceDescriptor,
352 AIBinder_Class_onCreate onCreate,
353 AIBinder_Class_onDestroy onDestroy,
354 AIBinder_Class_onTransact onTransact) {
355 if (interfaceDescriptor == nullptr || onCreate == nullptr || onDestroy == nullptr ||
356 onTransact == nullptr) {
357 return nullptr;
358 }
359
360 return new AIBinder_Class(interfaceDescriptor, onCreate, onDestroy, onTransact);
361}
362
Steven Morelanda194c452019-03-04 16:47:07 -0800363void AIBinder_Class_setOnDump(AIBinder_Class* clazz, AIBinder_onDump onDump) {
364 CHECK(clazz != nullptr) << "setOnDump requires non-null clazz";
365
366 // this is required to be called before instances are instantiated
367 clazz->onDump = onDump;
368}
369
Steven Morelandf3ed8062021-08-27 14:31:14 -0700370void AIBinder_Class_disableInterfaceTokenHeader(AIBinder_Class* clazz) {
371 CHECK(clazz != nullptr) << "disableInterfaceTokenHeader requires non-null clazz";
372
373 clazz->writeHeader = false;
374}
375
Ruchir Rastogicc7a7462020-01-31 14:29:15 -0800376void AIBinder_Class_setHandleShellCommand(AIBinder_Class* clazz,
377 AIBinder_handleShellCommand handleShellCommand) {
378 CHECK(clazz != nullptr) << "setHandleShellCommand requires non-null clazz";
379
380 clazz->handleShellCommand = handleShellCommand;
381}
382
Stephen Crane8fde87f2020-11-17 15:06:11 -0800383const char* AIBinder_Class_getDescriptor(const AIBinder_Class* clazz) {
384 CHECK(clazz != nullptr) << "getDescriptor requires non-null clazz";
385
386 return clazz->getInterfaceDescriptorUtf8();
387}
388
Alice Ryhlea9d9d22021-08-27 07:51:30 +0000389AIBinder_DeathRecipient::TransferDeathRecipient::~TransferDeathRecipient() {
390 if (mOnUnlinked != nullptr) {
391 mOnUnlinked(mCookie);
392 }
393}
394
Steven Moreland901c7452018-09-04 16:17:28 -0700395void AIBinder_DeathRecipient::TransferDeathRecipient::binderDied(const wp<IBinder>& who) {
Steven Moreland8bcb5032021-04-07 23:06:43 +0000396 CHECK(who == mWho) << who.unsafe_get() << "(" << who.get_refs() << ") vs " << mWho.unsafe_get()
397 << " (" << mWho.get_refs() << ")";
Steven Moreland901c7452018-09-04 16:17:28 -0700398
399 mOnDied(mCookie);
Steven Moreland64127ca2019-03-13 09:25:44 -0700400
401 sp<AIBinder_DeathRecipient> recipient = mParentRecipient.promote();
402 sp<IBinder> strongWho = who.promote();
403
404 // otherwise this will be cleaned up later with pruneDeadTransferEntriesLocked
405 if (recipient != nullptr && strongWho != nullptr) {
406 status_t result = recipient->unlinkToDeath(strongWho, mCookie);
407 if (result != ::android::DEAD_OBJECT) {
408 LOG(WARNING) << "Unlinking to dead binder resulted in: " << result;
409 }
410 }
411
Steven Moreland901c7452018-09-04 16:17:28 -0700412 mWho = nullptr;
413}
414
415AIBinder_DeathRecipient::AIBinder_DeathRecipient(AIBinder_DeathRecipient_onBinderDied onDied)
Alice Ryhlea9d9d22021-08-27 07:51:30 +0000416 : mOnDied(onDied), mOnUnlinked(nullptr) {
Steven Moreland901c7452018-09-04 16:17:28 -0700417 CHECK(onDied != nullptr);
418}
419
Steven Moreland64127ca2019-03-13 09:25:44 -0700420void AIBinder_DeathRecipient::pruneDeadTransferEntriesLocked() {
421 mDeathRecipients.erase(std::remove_if(mDeathRecipients.begin(), mDeathRecipients.end(),
422 [](const sp<TransferDeathRecipient>& tdr) {
423 return tdr->getWho() == nullptr;
424 }),
425 mDeathRecipients.end());
426}
427
Jiyong Park4f97a8c2020-11-16 18:11:14 +0900428binder_status_t AIBinder_DeathRecipient::linkToDeath(const sp<IBinder>& binder, void* cookie) {
Steven Moreland901c7452018-09-04 16:17:28 -0700429 CHECK(binder != nullptr);
430
431 std::lock_guard<std::mutex> l(mDeathRecipientsMutex);
432
433 sp<TransferDeathRecipient> recipient =
Alice Ryhlea9d9d22021-08-27 07:51:30 +0000434 new TransferDeathRecipient(binder, cookie, this, mOnDied, mOnUnlinked);
Steven Moreland901c7452018-09-04 16:17:28 -0700435
Steven Moreland64127ca2019-03-13 09:25:44 -0700436 status_t status = binder->linkToDeath(recipient, cookie, 0 /*flags*/);
Steven Moreland5d62e442018-09-13 15:01:02 -0700437 if (status != STATUS_OK) {
Alice Ryhlea9d9d22021-08-27 07:51:30 +0000438 // When we failed to link, the destructor of TransferDeathRecipient runs here, which
439 // ensures that mOnUnlinked is called before we return with an error from this method.
Steven Moreland5d62e442018-09-13 15:01:02 -0700440 return PruneStatusT(status);
Steven Moreland901c7452018-09-04 16:17:28 -0700441 }
442
443 mDeathRecipients.push_back(recipient);
Steven Moreland64127ca2019-03-13 09:25:44 -0700444
445 pruneDeadTransferEntriesLocked();
Steven Moreland5d62e442018-09-13 15:01:02 -0700446 return STATUS_OK;
Steven Moreland901c7452018-09-04 16:17:28 -0700447}
448
Jiyong Park4f97a8c2020-11-16 18:11:14 +0900449binder_status_t AIBinder_DeathRecipient::unlinkToDeath(const sp<IBinder>& binder, void* cookie) {
Steven Moreland901c7452018-09-04 16:17:28 -0700450 CHECK(binder != nullptr);
451
452 std::lock_guard<std::mutex> l(mDeathRecipientsMutex);
453
454 for (auto it = mDeathRecipients.rbegin(); it != mDeathRecipients.rend(); ++it) {
455 sp<TransferDeathRecipient> recipient = *it;
456
Steven Moreland64127ca2019-03-13 09:25:44 -0700457 if (recipient->getCookie() == cookie && recipient->getWho() == binder) {
Steven Moreland901c7452018-09-04 16:17:28 -0700458 mDeathRecipients.erase(it.base() - 1);
459
Steven Moreland64127ca2019-03-13 09:25:44 -0700460 status_t status = binder->unlinkToDeath(recipient, cookie, 0 /*flags*/);
Steven Moreland5d62e442018-09-13 15:01:02 -0700461 if (status != ::android::OK) {
Steven Moreland901c7452018-09-04 16:17:28 -0700462 LOG(ERROR) << __func__
Steven Moreland009fc1a2022-06-10 17:24:25 +0000463 << ": removed reference to death recipient but unlink failed: "
464 << statusToString(status);
Steven Moreland901c7452018-09-04 16:17:28 -0700465 }
Steven Moreland5d62e442018-09-13 15:01:02 -0700466 return PruneStatusT(status);
Steven Moreland901c7452018-09-04 16:17:28 -0700467 }
468 }
469
Steven Moreland5d62e442018-09-13 15:01:02 -0700470 return STATUS_NAME_NOT_FOUND;
Steven Moreland901c7452018-09-04 16:17:28 -0700471}
472
Alice Ryhlea9d9d22021-08-27 07:51:30 +0000473void AIBinder_DeathRecipient::setOnUnlinked(AIBinder_DeathRecipient_onBinderUnlinked onUnlinked) {
474 mOnUnlinked = onUnlinked;
475}
476
Steven Moreland901c7452018-09-04 16:17:28 -0700477// start of C-API methods
478
Steven Moreland2e87adc2018-08-20 19:47:00 -0700479AIBinder* AIBinder_new(const AIBinder_Class* clazz, void* args) {
480 if (clazz == nullptr) {
481 LOG(ERROR) << __func__ << ": Must provide class to construct local binder.";
482 return nullptr;
483 }
484
485 void* userData = clazz->onCreate(args);
486
Steven Moreland71cddc32018-08-30 23:39:22 -0700487 sp<AIBinder> ret = new ABBinder(clazz, userData);
488 ABBinderTag::attach(ret->getBinder());
489
490 AIBinder_incStrong(ret.get());
491 return ret.get();
Steven Moreland2e87adc2018-08-20 19:47:00 -0700492}
493
Steven Moreland5ea54da2018-09-04 13:29:55 -0700494bool AIBinder_isRemote(const AIBinder* binder) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700495 if (binder == nullptr) {
Steven Moreland56f752a2018-11-05 17:23:37 -0800496 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700497 }
498
499 return binder->isRemote();
500}
501
Steven Moreland65867d72018-09-04 14:22:26 -0700502bool AIBinder_isAlive(const AIBinder* binder) {
503 if (binder == nullptr) {
504 return false;
505 }
506
507 return const_cast<AIBinder*>(binder)->getBinder()->isBinderAlive();
508}
509
510binder_status_t AIBinder_ping(AIBinder* binder) {
511 if (binder == nullptr) {
Steven Moreland5d62e442018-09-13 15:01:02 -0700512 return STATUS_UNEXPECTED_NULL;
Steven Moreland65867d72018-09-04 14:22:26 -0700513 }
514
Steven Moreland5d62e442018-09-13 15:01:02 -0700515 return PruneStatusT(binder->getBinder()->pingBinder());
Steven Moreland65867d72018-09-04 14:22:26 -0700516}
517
Steven Morelanda194c452019-03-04 16:47:07 -0800518binder_status_t AIBinder_dump(AIBinder* binder, int fd, const char** args, uint32_t numArgs) {
519 if (binder == nullptr) {
520 return STATUS_UNEXPECTED_NULL;
521 }
522
523 ABBinder* bBinder = binder->asABBinder();
524 if (bBinder != nullptr) {
525 AIBinder_onDump onDump = binder->getClass()->onDump;
526 if (onDump == nullptr) {
527 return STATUS_OK;
528 }
529 return PruneStatusT(onDump(bBinder, fd, args, numArgs));
530 }
531
532 ::android::Vector<String16> utf16Args;
533 utf16Args.setCapacity(numArgs);
534 for (uint32_t i = 0; i < numArgs; i++) {
535 utf16Args.push(String16(String8(args[i])));
536 }
537
538 status_t status = binder->getBinder()->dump(fd, utf16Args);
539 return PruneStatusT(status);
540}
541
Steven Moreland901c7452018-09-04 16:17:28 -0700542binder_status_t AIBinder_linkToDeath(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->linkToDeath(binder->getBinder(), cookie);
Steven Moreland901c7452018-09-04 16:17:28 -0700552}
553
554binder_status_t AIBinder_unlinkToDeath(AIBinder* binder, AIBinder_DeathRecipient* recipient,
555 void* cookie) {
556 if (binder == nullptr || recipient == nullptr) {
Steven Moreland009fc1a2022-06-10 17:24:25 +0000557 LOG(ERROR) << __func__ << ": Must provide binder (" << binder << ") and recipient ("
558 << recipient << ")";
Steven Moreland5d62e442018-09-13 15:01:02 -0700559 return STATUS_UNEXPECTED_NULL;
Steven Moreland901c7452018-09-04 16:17:28 -0700560 }
561
Steven Moreland5d62e442018-09-13 15:01:02 -0700562 // returns binder_status_t
Steven Moreland64127ca2019-03-13 09:25:44 -0700563 return recipient->unlinkToDeath(binder->getBinder(), cookie);
Steven Moreland901c7452018-09-04 16:17:28 -0700564}
565
Steven Morelandf3034b02018-11-12 17:37:46 -0800566uid_t AIBinder_getCallingUid() {
567 return ::android::IPCThreadState::self()->getCallingUid();
568}
569
570pid_t AIBinder_getCallingPid() {
571 return ::android::IPCThreadState::self()->getCallingPid();
572}
573
Alice Ryhl59aeae82021-08-25 10:21:26 +0000574bool AIBinder_isHandlingTransaction() {
575 return ::android::IPCThreadState::self()->getServingStackPointer() != nullptr;
576}
577
Steven Moreland2e87adc2018-08-20 19:47:00 -0700578void AIBinder_incStrong(AIBinder* binder) {
579 if (binder == nullptr) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700580 return;
581 }
582
583 binder->incStrong(nullptr);
584}
585void AIBinder_decStrong(AIBinder* binder) {
586 if (binder == nullptr) {
587 LOG(ERROR) << __func__ << ": on null binder";
588 return;
589 }
590
591 binder->decStrong(nullptr);
592}
593int32_t AIBinder_debugGetRefCount(AIBinder* binder) {
594 if (binder == nullptr) {
595 LOG(ERROR) << __func__ << ": on null binder";
596 return -1;
597 }
598
599 return binder->getStrongCount();
600}
601
Steven Moreland71cddc32018-08-30 23:39:22 -0700602bool AIBinder_associateClass(AIBinder* binder, const AIBinder_Class* clazz) {
603 if (binder == nullptr) {
604 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700605 }
606
Steven Moreland71cddc32018-08-30 23:39:22 -0700607 return binder->associateClass(clazz);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700608}
609
610const AIBinder_Class* AIBinder_getClass(AIBinder* binder) {
611 if (binder == nullptr) {
612 return nullptr;
613 }
614
615 return binder->getClass();
616}
617
618void* AIBinder_getUserData(AIBinder* binder) {
619 if (binder == nullptr) {
620 return nullptr;
621 }
622
623 ABBinder* bBinder = binder->asABBinder();
624 if (bBinder == nullptr) {
625 return nullptr;
626 }
627
628 return bBinder->getUserData();
629}
630
631binder_status_t AIBinder_prepareTransaction(AIBinder* binder, AParcel** in) {
632 if (binder == nullptr || in == nullptr) {
Steven Moreland009fc1a2022-06-10 17:24:25 +0000633 LOG(ERROR) << __func__ << ": requires non-null parameters binder (" << binder
634 << ") and in (" << in << ").";
Steven Moreland5d62e442018-09-13 15:01:02 -0700635 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700636 }
637 const AIBinder_Class* clazz = binder->getClass();
638 if (clazz == nullptr) {
639 LOG(ERROR) << __func__
640 << ": Class must be defined for a remote binder transaction. See "
641 "AIBinder_associateClass.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700642 return STATUS_INVALID_OPERATION;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700643 }
644
645 *in = new AParcel(binder);
Steven Moreland1fda67b2021-04-02 18:35:50 +0000646 (*in)->get()->markForBinder(binder->getBinder());
647
Steven Morelandf3ed8062021-08-27 14:31:14 -0700648 status_t status = android::OK;
649 if (clazz->writeHeader) {
650 status = (*in)->get()->writeInterfaceToken(clazz->getInterfaceDescriptor());
651 }
Steven Moreland5d62e442018-09-13 15:01:02 -0700652 binder_status_t ret = PruneStatusT(status);
653
654 if (ret != STATUS_OK) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700655 delete *in;
656 *in = nullptr;
657 }
658
Steven Moreland5d62e442018-09-13 15:01:02 -0700659 return ret;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700660}
661
Steven Moreland9b80e282018-09-19 13:57:23 -0700662static void DestroyParcel(AParcel** parcel) {
663 delete *parcel;
664 *parcel = nullptr;
665}
666
Steven Moreland2e87adc2018-08-20 19:47:00 -0700667binder_status_t AIBinder_transact(AIBinder* binder, transaction_code_t code, AParcel** in,
668 AParcel** out, binder_flags_t flags) {
669 if (in == nullptr) {
670 LOG(ERROR) << __func__ << ": requires non-null in parameter";
Steven Moreland5d62e442018-09-13 15:01:02 -0700671 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700672 }
673
Steven Morelandcaa776c2018-09-04 13:48:11 -0700674 using AutoParcelDestroyer = std::unique_ptr<AParcel*, void (*)(AParcel**)>;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700675 // This object is the input to the transaction. This function takes ownership of it and deletes
676 // it.
Steven Moreland9b80e282018-09-19 13:57:23 -0700677 AutoParcelDestroyer forIn(in, DestroyParcel);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700678
679 if (!isUserCommand(code)) {
Steven Moreland009fc1a2022-06-10 17:24:25 +0000680 LOG(ERROR) << __func__
681 << ": Only user-defined transactions can be made from the NDK, but requested: "
682 << code;
Steven Moreland5d62e442018-09-13 15:01:02 -0700683 return STATUS_UNKNOWN_TRANSACTION;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700684 }
685
Steven Morelandf183fdd2020-10-27 00:12:12 +0000686 constexpr binder_flags_t kAllFlags = FLAG_PRIVATE_VENDOR | FLAG_ONEWAY | FLAG_CLEAR_BUF;
Steven Moreland46b5fea2019-10-15 11:22:18 -0700687 if ((flags & ~kAllFlags) != 0) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700688 LOG(ERROR) << __func__ << ": Unrecognized flags sent: " << flags;
Steven Moreland5d62e442018-09-13 15:01:02 -0700689 return STATUS_BAD_VALUE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700690 }
691
692 if (binder == nullptr || *in == nullptr || out == nullptr) {
Steven Moreland009fc1a2022-06-10 17:24:25 +0000693 LOG(ERROR) << __func__ << ": requires non-null parameters binder (" << binder << "), in ("
694 << in << "), and out (" << out << ").";
Steven Moreland5d62e442018-09-13 15:01:02 -0700695 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700696 }
697
698 if ((*in)->getBinder() != binder) {
699 LOG(ERROR) << __func__ << ": parcel is associated with binder object " << binder
700 << " but called with " << (*in)->getBinder();
Steven Moreland5d62e442018-09-13 15:01:02 -0700701 return STATUS_BAD_VALUE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700702 }
703
704 *out = new AParcel(binder);
705
Steven Morelandf18615b2018-09-14 11:43:06 -0700706 status_t status = binder->getBinder()->transact(code, *(*in)->get(), (*out)->get(), flags);
Steven Moreland5d62e442018-09-13 15:01:02 -0700707 binder_status_t ret = PruneStatusT(status);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700708
Steven Moreland5d62e442018-09-13 15:01:02 -0700709 if (ret != STATUS_OK) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700710 delete *out;
711 *out = nullptr;
712 }
713
Steven Moreland5d62e442018-09-13 15:01:02 -0700714 return ret;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700715}
Steven Moreland901c7452018-09-04 16:17:28 -0700716
717AIBinder_DeathRecipient* AIBinder_DeathRecipient_new(
718 AIBinder_DeathRecipient_onBinderDied onBinderDied) {
719 if (onBinderDied == nullptr) {
720 LOG(ERROR) << __func__ << ": requires non-null onBinderDied parameter.";
721 return nullptr;
722 }
Steven Moreland64127ca2019-03-13 09:25:44 -0700723 auto ret = new AIBinder_DeathRecipient(onBinderDied);
724 ret->incStrong(nullptr);
725 return ret;
Steven Moreland901c7452018-09-04 16:17:28 -0700726}
727
Alice Ryhlea9d9d22021-08-27 07:51:30 +0000728void AIBinder_DeathRecipient_setOnUnlinked(AIBinder_DeathRecipient* recipient,
729 AIBinder_DeathRecipient_onBinderUnlinked onUnlinked) {
730 if (recipient == nullptr) {
731 return;
732 }
733
734 recipient->setOnUnlinked(onUnlinked);
735}
736
Steven Moreland9b80e282018-09-19 13:57:23 -0700737void AIBinder_DeathRecipient_delete(AIBinder_DeathRecipient* recipient) {
Steven Moreland64127ca2019-03-13 09:25:44 -0700738 if (recipient == nullptr) {
739 return;
740 }
741
742 recipient->decStrong(nullptr);
Steven Moreland901c7452018-09-04 16:17:28 -0700743}
Steven Morelandea14ef22019-08-20 10:50:08 -0700744
745binder_status_t AIBinder_getExtension(AIBinder* binder, AIBinder** outExt) {
746 if (binder == nullptr || outExt == nullptr) {
747 if (outExt != nullptr) {
748 *outExt = nullptr;
749 }
750 return STATUS_UNEXPECTED_NULL;
751 }
752
753 sp<IBinder> ext;
754 status_t res = binder->getBinder()->getExtension(&ext);
755
756 if (res != android::OK) {
757 *outExt = nullptr;
758 return PruneStatusT(res);
759 }
760
761 sp<AIBinder> ret = ABpBinder::lookupOrCreateFromBinder(ext);
762 if (ret != nullptr) ret->incStrong(binder);
763
764 *outExt = ret.get();
765 return STATUS_OK;
766}
767
768binder_status_t AIBinder_setExtension(AIBinder* binder, AIBinder* ext) {
769 if (binder == nullptr || ext == nullptr) {
770 return STATUS_UNEXPECTED_NULL;
771 }
772
773 ABBinder* rawBinder = binder->asABBinder();
774 if (rawBinder == nullptr) {
775 return STATUS_INVALID_OPERATION;
776 }
777
778 rawBinder->setExtension(ext->getBinder());
779 return STATUS_OK;
780}
Steven Moreland2f405f52020-07-08 22:24:29 +0000781
782// platform methods follow
783
784void AIBinder_setRequestingSid(AIBinder* binder, bool requestingSid) {
785 ABBinder* localBinder = binder->asABBinder();
786 if (localBinder == nullptr) {
787 LOG(FATAL) << "AIBinder_setRequestingSid must be called on a local binder";
788 }
789
790 localBinder->setRequestingSid(requestingSid);
791}
792
793const char* AIBinder_getCallingSid() {
794 return ::android::IPCThreadState::self()->getCallingSid();
795}
Steven Morelandb2de9532020-05-29 21:44:41 +0000796
797android::sp<android::IBinder> AIBinder_toPlatformBinder(AIBinder* binder) {
798 if (binder == nullptr) return nullptr;
799 return binder->getBinder();
800}
801
802AIBinder* AIBinder_fromPlatformBinder(const android::sp<android::IBinder>& binder) {
803 sp<AIBinder> ndkBinder = ABpBinder::lookupOrCreateFromBinder(binder);
804 AIBinder_incStrong(ndkBinder.get());
805 return ndkBinder.get();
806}
Ady Abraham96174ac2021-10-15 11:02:03 -0700807
808void AIBinder_setMinSchedulerPolicy(AIBinder* binder, int policy, int priority) {
809 binder->asABBinder()->setMinSchedulerPolicy(policy, priority);
810}
Steven Moreland454a26a2021-12-14 01:26:21 +0000811
812void AIBinder_setInheritRt(AIBinder* binder, bool inheritRt) {
813 ABBinder* localBinder = binder->asABBinder();
814 if (localBinder == nullptr) {
815 LOG(FATAL) << "AIBinder_setInheritRt must be called on a local binder";
816 }
817
818 localBinder->setInheritRt(inheritRt);
819}