blob: b29f22e41a8966160b387fb753a02bb874e92a24 [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
Steven Moreland94a76792022-12-21 01:20:06 +000078// b/175635923 libcxx causes "implicit-conversion" with a string with invalid char
79static std::string SanitizeString(const String16& str) {
80 std::string sanitized{String8(str)};
81 for (auto& c : sanitized) {
82 if (!isprint(c)) {
83 c = '?';
84 }
85 }
86 return sanitized;
87}
88
89bool AIBinder::associateClass(const AIBinder_Class* clazz) {
90 if (clazz == nullptr) return false;
91
92 // If mClazz is non-null, this must have been called and cached
93 // already. So, we can safely call this first. Due to the implementation
94 // of getInterfaceDescriptor (at time of writing), two simultaneous calls
95 // may lead to extra binder transactions, but this is expected to be
96 // exceedingly rare. Once we have a binder, when we get it again later,
97 // we won't make another binder transaction here.
98 const String16& descriptor = getBinder()->getInterfaceDescriptor();
99 const String16& newDescriptor = clazz->getInterfaceDescriptor();
100
Andrei Homescu9556bb12020-09-21 16:59:45 -0700101 std::lock_guard<std::mutex> lock(mClazzMutex);
Steven Moreland71cddc32018-08-30 23:39:22 -0700102 if (mClazz == clazz) return true;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700103
Steven Moreland2e87adc2018-08-20 19:47:00 -0700104 if (mClazz != nullptr) {
Steven Moreland63872b52020-10-02 19:28:23 +0000105 const String16& currentDescriptor = mClazz->getInterfaceDescriptor();
Steven Moreland2e87adc2018-08-20 19:47:00 -0700106 if (newDescriptor == currentDescriptor) {
107 LOG(ERROR) << __func__ << ": Class descriptors '" << currentDescriptor
Steven Moreland4d196972021-08-12 15:49:44 -0700108 << "' match during associateClass, but they are different class objects ("
109 << clazz << " vs " << mClazz << "). Class descriptor collision?";
Steven Moreland71cddc32018-08-30 23:39:22 -0700110 } else {
111 LOG(ERROR) << __func__
112 << ": Class cannot be associated on object which already has a class. "
113 "Trying to associate to '"
Steven Moreland63872b52020-10-02 19:28:23 +0000114 << newDescriptor << "' but already set to '" << currentDescriptor << "'.";
Steven Moreland2e87adc2018-08-20 19:47:00 -0700115 }
116
Steven Moreland71cddc32018-08-30 23:39:22 -0700117 // always a failure because we know mClazz != clazz
Steven Moreland94a76792022-12-21 01:20:06 +0000118 // TODO(b/262463798): support multiple ABpBinder in different namespaces
Steven Moreland71cddc32018-08-30 23:39:22 -0700119 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700120 }
121
Steven Moreland94a76792022-12-21 01:20:06 +0000122 // This will always be an O(n) comparison, but it's expected to be extremely rare.
123 // since it's an error condition. Do the comparison after we take the lock and
124 // check the pointer equality fast path. By always taking the lock, it's also
125 // more flake-proof. However, the check is not dependent on the lock.
Steven Moreland2e87adc2018-08-20 19:47:00 -0700126 if (descriptor != newDescriptor) {
Steven Moreland640c4092020-07-08 00:19:58 +0000127 if (getBinder()->isBinderAlive()) {
Steven Moreland63872b52020-10-02 19:28:23 +0000128 LOG(ERROR) << __func__ << ": Expecting binder to have class '" << newDescriptor
Jooyung Han1f9dcd32021-11-03 13:34:29 +0900129 << "' but descriptor is actually '" << SanitizeString(descriptor) << "'.";
Steven Moreland640c4092020-07-08 00:19:58 +0000130 } else {
131 // b/155793159
Steven Moreland63872b52020-10-02 19:28:23 +0000132 LOG(ERROR) << __func__ << ": Cannot associate class '" << newDescriptor
Steven Moreland009fc1a2022-06-10 17:24:25 +0000133 << "' to dead binder with cached descriptor '" << SanitizeString(descriptor)
134 << "'.";
Steven Moreland640c4092020-07-08 00:19:58 +0000135 }
Steven Moreland71cddc32018-08-30 23:39:22 -0700136 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700137 }
138
Steven Moreland94a76792022-12-21 01:20:06 +0000139 // if this is a local object, it's not one known to libbinder_ndk
140 mClazz = clazz;
141 return true;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700142}
143
144ABBinder::ABBinder(const AIBinder_Class* clazz, void* userData)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700145 : AIBinder(clazz), BBinder(), mUserData(userData) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700146 CHECK(clazz != nullptr);
147}
148ABBinder::~ABBinder() {
149 getClass()->onDestroy(mUserData);
150}
151
152const String16& ABBinder::getInterfaceDescriptor() const {
153 return getClass()->getInterfaceDescriptor();
154}
155
Steven Morelanda194c452019-03-04 16:47:07 -0800156status_t ABBinder::dump(int fd, const ::android::Vector<String16>& args) {
157 AIBinder_onDump onDump = getClass()->onDump;
158
159 if (onDump == nullptr) {
160 return STATUS_OK;
161 }
162
163 // technically UINT32_MAX would be okay here, but INT32_MAX is expected since this may be
164 // null in Java
165 if (args.size() > INT32_MAX) {
166 LOG(ERROR) << "ABBinder::dump received too many arguments: " << args.size();
167 return STATUS_BAD_VALUE;
168 }
169
170 std::vector<String8> utf8Args; // owns memory of utf8s
171 utf8Args.reserve(args.size());
172 std::vector<const char*> utf8Pointers; // what can be passed over NDK API
173 utf8Pointers.reserve(args.size());
174
175 for (size_t i = 0; i < args.size(); i++) {
176 utf8Args.push_back(String8(args[i]));
177 utf8Pointers.push_back(utf8Args[i].c_str());
178 }
179
180 return onDump(this, fd, utf8Pointers.data(), utf8Pointers.size());
181}
182
Steven Moreland5d62e442018-09-13 15:01:02 -0700183status_t ABBinder::onTransact(transaction_code_t code, const Parcel& data, Parcel* reply,
184 binder_flags_t flags) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700185 if (isUserCommand(code)) {
Steven Morelandf3ed8062021-08-27 14:31:14 -0700186 if (getClass()->writeHeader && !data.checkInterface(this)) {
Steven Moreland9d089af2018-09-18 08:58:09 -0700187 return STATUS_BAD_TYPE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700188 }
189
190 const AParcel in = AParcel::readOnly(this, &data);
191 AParcel out = AParcel(this, reply, false /*owns*/);
192
Steven Moreland5d62e442018-09-13 15:01:02 -0700193 binder_status_t status = getClass()->onTransact(this, code, &in, &out);
194 return PruneStatusT(status);
Steven Moreland88234072020-08-06 19:32:45 +0000195 } else if (code == SHELL_COMMAND_TRANSACTION && getClass()->handleShellCommand != nullptr) {
Ruchir Rastogicc7a7462020-01-31 14:29:15 -0800196 int in = data.readFileDescriptor();
197 int out = data.readFileDescriptor();
198 int err = data.readFileDescriptor();
199
200 int argc = data.readInt32();
201 std::vector<String8> utf8Args; // owns memory of utf8s
202 std::vector<const char*> utf8Pointers; // what can be passed over NDK API
203 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
204 utf8Args.push_back(String8(data.readString16()));
205 utf8Pointers.push_back(utf8Args[i].c_str());
206 }
207
208 data.readStrongBinder(); // skip over the IShellCallback
209 sp<IResultReceiver> resultReceiver = IResultReceiver::asInterface(data.readStrongBinder());
210
211 // Shell commands should only be callable by ADB.
212 uid_t uid = AIBinder_getCallingUid();
213 if (uid != AID_ROOT && uid != AID_SHELL) {
214 if (resultReceiver != nullptr) {
215 resultReceiver->send(-1);
216 }
217 return STATUS_PERMISSION_DENIED;
218 }
219
220 // Check that the file descriptors are valid.
221 if (in == STATUS_BAD_TYPE || out == STATUS_BAD_TYPE || err == STATUS_BAD_TYPE) {
222 if (resultReceiver != nullptr) {
223 resultReceiver->send(-1);
224 }
225 return STATUS_BAD_VALUE;
226 }
227
228 binder_status_t status = getClass()->handleShellCommand(
229 this, in, out, err, utf8Pointers.data(), utf8Pointers.size());
230 if (resultReceiver != nullptr) {
231 resultReceiver->send(status);
232 }
233 return status;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700234 } else {
235 return BBinder::onTransact(code, data, reply, flags);
236 }
237}
238
Steven Moreland71cddc32018-08-30 23:39:22 -0700239ABpBinder::ABpBinder(const ::android::sp<::android::IBinder>& binder)
Steven Moreland3acd4902022-07-26 18:29:58 +0000240 : AIBinder(nullptr /*clazz*/), mRemote(binder) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700241 CHECK(binder != nullptr);
242}
243ABpBinder::~ABpBinder() {}
244
Steven Moreland94968952018-09-05 14:42:59 -0700245sp<AIBinder> ABpBinder::lookupOrCreateFromBinder(const ::android::sp<::android::IBinder>& binder) {
Steven Moreland71cddc32018-08-30 23:39:22 -0700246 if (binder == nullptr) {
247 return nullptr;
248 }
249 if (ABBinderTag::has(binder)) {
250 return static_cast<ABBinder*>(binder.get());
251 }
Steven Moreland94968952018-09-05 14:42:59 -0700252
Steven Moreland91a96b62021-06-30 23:07:13 +0000253 // The following code ensures that for a given binder object (remote or local), if it is not an
254 // ABBinder then at most one ABpBinder object exists in a given process representing it.
255
Steven Moreland1f1bed62021-06-25 21:50:50 +0000256 auto* value = static_cast<ABpBinderTag::Value*>(binder->findObject(ABpBinderTag::kId));
Steven Moreland94968952018-09-05 14:42:59 -0700257 if (value == nullptr) {
258 value = new ABpBinderTag::Value;
Steven Moreland1f1bed62021-06-25 21:50:50 +0000259 auto oldValue = static_cast<ABpBinderTag::Value*>(
260 binder->attachObject(ABpBinderTag::kId, static_cast<void*>(value),
261 nullptr /*cookie*/, ABpBinderTag::clean));
262
263 // allocated by another thread
264 if (oldValue) {
265 delete value;
266 value = oldValue;
267 }
Steven Moreland94968952018-09-05 14:42:59 -0700268 }
269
Steven Moreland1f1bed62021-06-25 21:50:50 +0000270 sp<ABpBinder> ret;
271 binder->withLock([&]() {
272 ret = value->binder.promote();
273 if (ret == nullptr) {
274 ret = sp<ABpBinder>::make(binder);
275 value->binder = ret;
276 }
277 });
Steven Moreland94968952018-09-05 14:42:59 -0700278
279 return ret;
Steven Moreland71cddc32018-08-30 23:39:22 -0700280}
281
Steven Moreland2e87adc2018-08-20 19:47:00 -0700282struct AIBinder_Weak {
283 wp<AIBinder> binder;
284};
285AIBinder_Weak* AIBinder_Weak_new(AIBinder* binder) {
Steven Moreland5ccb70f2018-09-04 16:30:21 -0700286 if (binder == nullptr) {
287 return nullptr;
288 }
289
Steven Moreland2e87adc2018-08-20 19:47:00 -0700290 return new AIBinder_Weak{wp<AIBinder>(binder)};
291}
Steven Moreland9b80e282018-09-19 13:57:23 -0700292void AIBinder_Weak_delete(AIBinder_Weak* weakBinder) {
293 delete weakBinder;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700294}
295AIBinder* AIBinder_Weak_promote(AIBinder_Weak* weakBinder) {
Steven Moreland5ccb70f2018-09-04 16:30:21 -0700296 if (weakBinder == nullptr) {
297 return nullptr;
298 }
299
Steven Moreland2e87adc2018-08-20 19:47:00 -0700300 sp<AIBinder> binder = weakBinder->binder.promote();
301 AIBinder_incStrong(binder.get());
302 return binder.get();
303}
304
Steven Morelandc70e0122021-01-08 02:44:53 +0000305AIBinder_Weak* AIBinder_Weak_clone(const AIBinder_Weak* weak) {
306 if (weak == nullptr) {
307 return nullptr;
308 }
309
310 return new AIBinder_Weak{weak->binder};
311}
312
313bool AIBinder_lt(const AIBinder* lhs, const AIBinder* rhs) {
314 if (lhs == nullptr || rhs == nullptr) return lhs < rhs;
315
316 return const_cast<AIBinder*>(lhs)->getBinder() < const_cast<AIBinder*>(rhs)->getBinder();
317}
318
319bool AIBinder_Weak_lt(const AIBinder_Weak* lhs, const AIBinder_Weak* rhs) {
320 if (lhs == nullptr || rhs == nullptr) return lhs < rhs;
321
322 return lhs->binder < rhs->binder;
323}
324
Steven Moreland2e87adc2018-08-20 19:47:00 -0700325AIBinder_Class::AIBinder_Class(const char* interfaceDescriptor, AIBinder_Class_onCreate onCreate,
326 AIBinder_Class_onDestroy onDestroy,
327 AIBinder_Class_onTransact onTransact)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700328 : onCreate(onCreate),
329 onDestroy(onDestroy),
330 onTransact(onTransact),
Stephen Crane8fde87f2020-11-17 15:06:11 -0800331 mInterfaceDescriptor(interfaceDescriptor),
332 mWideInterfaceDescriptor(interfaceDescriptor) {}
Steven Moreland2e87adc2018-08-20 19:47:00 -0700333
334AIBinder_Class* AIBinder_Class_define(const char* interfaceDescriptor,
335 AIBinder_Class_onCreate onCreate,
336 AIBinder_Class_onDestroy onDestroy,
337 AIBinder_Class_onTransact onTransact) {
338 if (interfaceDescriptor == nullptr || onCreate == nullptr || onDestroy == nullptr ||
339 onTransact == nullptr) {
340 return nullptr;
341 }
342
343 return new AIBinder_Class(interfaceDescriptor, onCreate, onDestroy, onTransact);
344}
345
Steven Morelanda194c452019-03-04 16:47:07 -0800346void AIBinder_Class_setOnDump(AIBinder_Class* clazz, AIBinder_onDump onDump) {
347 CHECK(clazz != nullptr) << "setOnDump requires non-null clazz";
348
349 // this is required to be called before instances are instantiated
350 clazz->onDump = onDump;
351}
352
Steven Morelandf3ed8062021-08-27 14:31:14 -0700353void AIBinder_Class_disableInterfaceTokenHeader(AIBinder_Class* clazz) {
354 CHECK(clazz != nullptr) << "disableInterfaceTokenHeader requires non-null clazz";
355
356 clazz->writeHeader = false;
357}
358
Ruchir Rastogicc7a7462020-01-31 14:29:15 -0800359void AIBinder_Class_setHandleShellCommand(AIBinder_Class* clazz,
360 AIBinder_handleShellCommand handleShellCommand) {
361 CHECK(clazz != nullptr) << "setHandleShellCommand requires non-null clazz";
362
363 clazz->handleShellCommand = handleShellCommand;
364}
365
Stephen Crane8fde87f2020-11-17 15:06:11 -0800366const char* AIBinder_Class_getDescriptor(const AIBinder_Class* clazz) {
367 CHECK(clazz != nullptr) << "getDescriptor requires non-null clazz";
368
369 return clazz->getInterfaceDescriptorUtf8();
370}
371
Alice Ryhlea9d9d22021-08-27 07:51:30 +0000372AIBinder_DeathRecipient::TransferDeathRecipient::~TransferDeathRecipient() {
373 if (mOnUnlinked != nullptr) {
374 mOnUnlinked(mCookie);
375 }
376}
377
Steven Moreland901c7452018-09-04 16:17:28 -0700378void AIBinder_DeathRecipient::TransferDeathRecipient::binderDied(const wp<IBinder>& who) {
Steven Moreland8bcb5032021-04-07 23:06:43 +0000379 CHECK(who == mWho) << who.unsafe_get() << "(" << who.get_refs() << ") vs " << mWho.unsafe_get()
380 << " (" << mWho.get_refs() << ")";
Steven Moreland901c7452018-09-04 16:17:28 -0700381
382 mOnDied(mCookie);
Steven Moreland64127ca2019-03-13 09:25:44 -0700383
384 sp<AIBinder_DeathRecipient> recipient = mParentRecipient.promote();
385 sp<IBinder> strongWho = who.promote();
386
387 // otherwise this will be cleaned up later with pruneDeadTransferEntriesLocked
388 if (recipient != nullptr && strongWho != nullptr) {
389 status_t result = recipient->unlinkToDeath(strongWho, mCookie);
390 if (result != ::android::DEAD_OBJECT) {
391 LOG(WARNING) << "Unlinking to dead binder resulted in: " << result;
392 }
393 }
394
Steven Moreland901c7452018-09-04 16:17:28 -0700395 mWho = nullptr;
396}
397
398AIBinder_DeathRecipient::AIBinder_DeathRecipient(AIBinder_DeathRecipient_onBinderDied onDied)
Alice Ryhlea9d9d22021-08-27 07:51:30 +0000399 : mOnDied(onDied), mOnUnlinked(nullptr) {
Steven Moreland901c7452018-09-04 16:17:28 -0700400 CHECK(onDied != nullptr);
401}
402
Steven Moreland64127ca2019-03-13 09:25:44 -0700403void AIBinder_DeathRecipient::pruneDeadTransferEntriesLocked() {
404 mDeathRecipients.erase(std::remove_if(mDeathRecipients.begin(), mDeathRecipients.end(),
405 [](const sp<TransferDeathRecipient>& tdr) {
406 return tdr->getWho() == nullptr;
407 }),
408 mDeathRecipients.end());
409}
410
Jiyong Park4f97a8c2020-11-16 18:11:14 +0900411binder_status_t AIBinder_DeathRecipient::linkToDeath(const sp<IBinder>& binder, void* cookie) {
Steven Moreland901c7452018-09-04 16:17:28 -0700412 CHECK(binder != nullptr);
413
414 std::lock_guard<std::mutex> l(mDeathRecipientsMutex);
415
416 sp<TransferDeathRecipient> recipient =
Alice Ryhlea9d9d22021-08-27 07:51:30 +0000417 new TransferDeathRecipient(binder, cookie, this, mOnDied, mOnUnlinked);
Steven Moreland901c7452018-09-04 16:17:28 -0700418
Steven Moreland64127ca2019-03-13 09:25:44 -0700419 status_t status = binder->linkToDeath(recipient, cookie, 0 /*flags*/);
Steven Moreland5d62e442018-09-13 15:01:02 -0700420 if (status != STATUS_OK) {
Alice Ryhlea9d9d22021-08-27 07:51:30 +0000421 // When we failed to link, the destructor of TransferDeathRecipient runs here, which
422 // ensures that mOnUnlinked is called before we return with an error from this method.
Steven Moreland5d62e442018-09-13 15:01:02 -0700423 return PruneStatusT(status);
Steven Moreland901c7452018-09-04 16:17:28 -0700424 }
425
426 mDeathRecipients.push_back(recipient);
Steven Moreland64127ca2019-03-13 09:25:44 -0700427
428 pruneDeadTransferEntriesLocked();
Steven Moreland5d62e442018-09-13 15:01:02 -0700429 return STATUS_OK;
Steven Moreland901c7452018-09-04 16:17:28 -0700430}
431
Jiyong Park4f97a8c2020-11-16 18:11:14 +0900432binder_status_t AIBinder_DeathRecipient::unlinkToDeath(const sp<IBinder>& binder, void* cookie) {
Steven Moreland901c7452018-09-04 16:17:28 -0700433 CHECK(binder != nullptr);
434
435 std::lock_guard<std::mutex> l(mDeathRecipientsMutex);
436
437 for (auto it = mDeathRecipients.rbegin(); it != mDeathRecipients.rend(); ++it) {
438 sp<TransferDeathRecipient> recipient = *it;
439
Steven Moreland64127ca2019-03-13 09:25:44 -0700440 if (recipient->getCookie() == cookie && recipient->getWho() == binder) {
Steven Moreland901c7452018-09-04 16:17:28 -0700441 mDeathRecipients.erase(it.base() - 1);
442
Steven Moreland64127ca2019-03-13 09:25:44 -0700443 status_t status = binder->unlinkToDeath(recipient, cookie, 0 /*flags*/);
Steven Moreland5d62e442018-09-13 15:01:02 -0700444 if (status != ::android::OK) {
Steven Moreland901c7452018-09-04 16:17:28 -0700445 LOG(ERROR) << __func__
Steven Moreland009fc1a2022-06-10 17:24:25 +0000446 << ": removed reference to death recipient but unlink failed: "
447 << statusToString(status);
Steven Moreland901c7452018-09-04 16:17:28 -0700448 }
Steven Moreland5d62e442018-09-13 15:01:02 -0700449 return PruneStatusT(status);
Steven Moreland901c7452018-09-04 16:17:28 -0700450 }
451 }
452
Steven Moreland5d62e442018-09-13 15:01:02 -0700453 return STATUS_NAME_NOT_FOUND;
Steven Moreland901c7452018-09-04 16:17:28 -0700454}
455
Alice Ryhlea9d9d22021-08-27 07:51:30 +0000456void AIBinder_DeathRecipient::setOnUnlinked(AIBinder_DeathRecipient_onBinderUnlinked onUnlinked) {
457 mOnUnlinked = onUnlinked;
458}
459
Steven Moreland901c7452018-09-04 16:17:28 -0700460// start of C-API methods
461
Steven Moreland2e87adc2018-08-20 19:47:00 -0700462AIBinder* AIBinder_new(const AIBinder_Class* clazz, void* args) {
463 if (clazz == nullptr) {
464 LOG(ERROR) << __func__ << ": Must provide class to construct local binder.";
465 return nullptr;
466 }
467
468 void* userData = clazz->onCreate(args);
469
Steven Moreland71cddc32018-08-30 23:39:22 -0700470 sp<AIBinder> ret = new ABBinder(clazz, userData);
471 ABBinderTag::attach(ret->getBinder());
472
473 AIBinder_incStrong(ret.get());
474 return ret.get();
Steven Moreland2e87adc2018-08-20 19:47:00 -0700475}
476
Steven Moreland5ea54da2018-09-04 13:29:55 -0700477bool AIBinder_isRemote(const AIBinder* binder) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700478 if (binder == nullptr) {
Steven Moreland56f752a2018-11-05 17:23:37 -0800479 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700480 }
481
482 return binder->isRemote();
483}
484
Steven Moreland65867d72018-09-04 14:22:26 -0700485bool AIBinder_isAlive(const AIBinder* binder) {
486 if (binder == nullptr) {
487 return false;
488 }
489
490 return const_cast<AIBinder*>(binder)->getBinder()->isBinderAlive();
491}
492
493binder_status_t AIBinder_ping(AIBinder* binder) {
494 if (binder == nullptr) {
Steven Moreland5d62e442018-09-13 15:01:02 -0700495 return STATUS_UNEXPECTED_NULL;
Steven Moreland65867d72018-09-04 14:22:26 -0700496 }
497
Steven Moreland5d62e442018-09-13 15:01:02 -0700498 return PruneStatusT(binder->getBinder()->pingBinder());
Steven Moreland65867d72018-09-04 14:22:26 -0700499}
500
Steven Morelanda194c452019-03-04 16:47:07 -0800501binder_status_t AIBinder_dump(AIBinder* binder, int fd, const char** args, uint32_t numArgs) {
502 if (binder == nullptr) {
503 return STATUS_UNEXPECTED_NULL;
504 }
505
506 ABBinder* bBinder = binder->asABBinder();
507 if (bBinder != nullptr) {
508 AIBinder_onDump onDump = binder->getClass()->onDump;
509 if (onDump == nullptr) {
510 return STATUS_OK;
511 }
512 return PruneStatusT(onDump(bBinder, fd, args, numArgs));
513 }
514
515 ::android::Vector<String16> utf16Args;
516 utf16Args.setCapacity(numArgs);
517 for (uint32_t i = 0; i < numArgs; i++) {
518 utf16Args.push(String16(String8(args[i])));
519 }
520
521 status_t status = binder->getBinder()->dump(fd, utf16Args);
522 return PruneStatusT(status);
523}
524
Steven Moreland901c7452018-09-04 16:17:28 -0700525binder_status_t AIBinder_linkToDeath(AIBinder* binder, AIBinder_DeathRecipient* recipient,
526 void* cookie) {
527 if (binder == nullptr || recipient == nullptr) {
Steven Moreland009fc1a2022-06-10 17:24:25 +0000528 LOG(ERROR) << __func__ << ": Must provide binder (" << binder << ") and recipient ("
529 << recipient << ")";
Steven Moreland5d62e442018-09-13 15:01:02 -0700530 return STATUS_UNEXPECTED_NULL;
Steven Moreland901c7452018-09-04 16:17:28 -0700531 }
532
Steven Moreland5d62e442018-09-13 15:01:02 -0700533 // returns binder_status_t
Steven Moreland64127ca2019-03-13 09:25:44 -0700534 return recipient->linkToDeath(binder->getBinder(), cookie);
Steven Moreland901c7452018-09-04 16:17:28 -0700535}
536
537binder_status_t AIBinder_unlinkToDeath(AIBinder* binder, AIBinder_DeathRecipient* recipient,
538 void* cookie) {
539 if (binder == nullptr || recipient == nullptr) {
Steven Moreland009fc1a2022-06-10 17:24:25 +0000540 LOG(ERROR) << __func__ << ": Must provide binder (" << binder << ") and recipient ("
541 << recipient << ")";
Steven Moreland5d62e442018-09-13 15:01:02 -0700542 return STATUS_UNEXPECTED_NULL;
Steven Moreland901c7452018-09-04 16:17:28 -0700543 }
544
Steven Moreland5d62e442018-09-13 15:01:02 -0700545 // returns binder_status_t
Steven Moreland64127ca2019-03-13 09:25:44 -0700546 return recipient->unlinkToDeath(binder->getBinder(), cookie);
Steven Moreland901c7452018-09-04 16:17:28 -0700547}
548
Steven Morelandf3034b02018-11-12 17:37:46 -0800549uid_t AIBinder_getCallingUid() {
550 return ::android::IPCThreadState::self()->getCallingUid();
551}
552
553pid_t AIBinder_getCallingPid() {
554 return ::android::IPCThreadState::self()->getCallingPid();
555}
556
Alice Ryhl59aeae82021-08-25 10:21:26 +0000557bool AIBinder_isHandlingTransaction() {
558 return ::android::IPCThreadState::self()->getServingStackPointer() != nullptr;
559}
560
Steven Moreland2e87adc2018-08-20 19:47:00 -0700561void AIBinder_incStrong(AIBinder* binder) {
562 if (binder == nullptr) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700563 return;
564 }
565
566 binder->incStrong(nullptr);
567}
568void AIBinder_decStrong(AIBinder* binder) {
569 if (binder == nullptr) {
570 LOG(ERROR) << __func__ << ": on null binder";
571 return;
572 }
573
574 binder->decStrong(nullptr);
575}
576int32_t AIBinder_debugGetRefCount(AIBinder* binder) {
577 if (binder == nullptr) {
578 LOG(ERROR) << __func__ << ": on null binder";
579 return -1;
580 }
581
582 return binder->getStrongCount();
583}
584
Steven Moreland71cddc32018-08-30 23:39:22 -0700585bool AIBinder_associateClass(AIBinder* binder, const AIBinder_Class* clazz) {
586 if (binder == nullptr) {
587 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700588 }
589
Steven Moreland71cddc32018-08-30 23:39:22 -0700590 return binder->associateClass(clazz);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700591}
592
593const AIBinder_Class* AIBinder_getClass(AIBinder* binder) {
594 if (binder == nullptr) {
595 return nullptr;
596 }
597
598 return binder->getClass();
599}
600
601void* AIBinder_getUserData(AIBinder* binder) {
602 if (binder == nullptr) {
603 return nullptr;
604 }
605
606 ABBinder* bBinder = binder->asABBinder();
607 if (bBinder == nullptr) {
608 return nullptr;
609 }
610
611 return bBinder->getUserData();
612}
613
614binder_status_t AIBinder_prepareTransaction(AIBinder* binder, AParcel** in) {
615 if (binder == nullptr || in == nullptr) {
Steven Moreland009fc1a2022-06-10 17:24:25 +0000616 LOG(ERROR) << __func__ << ": requires non-null parameters binder (" << binder
617 << ") and in (" << in << ").";
Steven Moreland5d62e442018-09-13 15:01:02 -0700618 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700619 }
620 const AIBinder_Class* clazz = binder->getClass();
621 if (clazz == nullptr) {
622 LOG(ERROR) << __func__
623 << ": Class must be defined for a remote binder transaction. See "
624 "AIBinder_associateClass.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700625 return STATUS_INVALID_OPERATION;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700626 }
627
628 *in = new AParcel(binder);
Steven Moreland1fda67b2021-04-02 18:35:50 +0000629 (*in)->get()->markForBinder(binder->getBinder());
630
Steven Morelandf3ed8062021-08-27 14:31:14 -0700631 status_t status = android::OK;
632 if (clazz->writeHeader) {
633 status = (*in)->get()->writeInterfaceToken(clazz->getInterfaceDescriptor());
634 }
Steven Moreland5d62e442018-09-13 15:01:02 -0700635 binder_status_t ret = PruneStatusT(status);
636
637 if (ret != STATUS_OK) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700638 delete *in;
639 *in = nullptr;
640 }
641
Steven Moreland5d62e442018-09-13 15:01:02 -0700642 return ret;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700643}
644
Steven Moreland9b80e282018-09-19 13:57:23 -0700645static void DestroyParcel(AParcel** parcel) {
646 delete *parcel;
647 *parcel = nullptr;
648}
649
Steven Moreland2e87adc2018-08-20 19:47:00 -0700650binder_status_t AIBinder_transact(AIBinder* binder, transaction_code_t code, AParcel** in,
651 AParcel** out, binder_flags_t flags) {
652 if (in == nullptr) {
653 LOG(ERROR) << __func__ << ": requires non-null in parameter";
Steven Moreland5d62e442018-09-13 15:01:02 -0700654 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700655 }
656
Steven Morelandcaa776c2018-09-04 13:48:11 -0700657 using AutoParcelDestroyer = std::unique_ptr<AParcel*, void (*)(AParcel**)>;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700658 // This object is the input to the transaction. This function takes ownership of it and deletes
659 // it.
Steven Moreland9b80e282018-09-19 13:57:23 -0700660 AutoParcelDestroyer forIn(in, DestroyParcel);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700661
662 if (!isUserCommand(code)) {
Steven Moreland009fc1a2022-06-10 17:24:25 +0000663 LOG(ERROR) << __func__
664 << ": Only user-defined transactions can be made from the NDK, but requested: "
665 << code;
Steven Moreland5d62e442018-09-13 15:01:02 -0700666 return STATUS_UNKNOWN_TRANSACTION;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700667 }
668
Steven Morelandf183fdd2020-10-27 00:12:12 +0000669 constexpr binder_flags_t kAllFlags = FLAG_PRIVATE_VENDOR | FLAG_ONEWAY | FLAG_CLEAR_BUF;
Steven Moreland46b5fea2019-10-15 11:22:18 -0700670 if ((flags & ~kAllFlags) != 0) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700671 LOG(ERROR) << __func__ << ": Unrecognized flags sent: " << flags;
Steven Moreland5d62e442018-09-13 15:01:02 -0700672 return STATUS_BAD_VALUE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700673 }
674
675 if (binder == nullptr || *in == nullptr || out == nullptr) {
Steven Moreland009fc1a2022-06-10 17:24:25 +0000676 LOG(ERROR) << __func__ << ": requires non-null parameters binder (" << binder << "), in ("
677 << in << "), and out (" << out << ").";
Steven Moreland5d62e442018-09-13 15:01:02 -0700678 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700679 }
680
681 if ((*in)->getBinder() != binder) {
682 LOG(ERROR) << __func__ << ": parcel is associated with binder object " << binder
683 << " but called with " << (*in)->getBinder();
Steven Moreland5d62e442018-09-13 15:01:02 -0700684 return STATUS_BAD_VALUE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700685 }
686
687 *out = new AParcel(binder);
688
Steven Morelandf18615b2018-09-14 11:43:06 -0700689 status_t status = binder->getBinder()->transact(code, *(*in)->get(), (*out)->get(), flags);
Steven Moreland5d62e442018-09-13 15:01:02 -0700690 binder_status_t ret = PruneStatusT(status);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700691
Steven Moreland5d62e442018-09-13 15:01:02 -0700692 if (ret != STATUS_OK) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700693 delete *out;
694 *out = nullptr;
695 }
696
Steven Moreland5d62e442018-09-13 15:01:02 -0700697 return ret;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700698}
Steven Moreland901c7452018-09-04 16:17:28 -0700699
700AIBinder_DeathRecipient* AIBinder_DeathRecipient_new(
701 AIBinder_DeathRecipient_onBinderDied onBinderDied) {
702 if (onBinderDied == nullptr) {
703 LOG(ERROR) << __func__ << ": requires non-null onBinderDied parameter.";
704 return nullptr;
705 }
Steven Moreland64127ca2019-03-13 09:25:44 -0700706 auto ret = new AIBinder_DeathRecipient(onBinderDied);
707 ret->incStrong(nullptr);
708 return ret;
Steven Moreland901c7452018-09-04 16:17:28 -0700709}
710
Alice Ryhlea9d9d22021-08-27 07:51:30 +0000711void AIBinder_DeathRecipient_setOnUnlinked(AIBinder_DeathRecipient* recipient,
712 AIBinder_DeathRecipient_onBinderUnlinked onUnlinked) {
713 if (recipient == nullptr) {
714 return;
715 }
716
717 recipient->setOnUnlinked(onUnlinked);
718}
719
Steven Moreland9b80e282018-09-19 13:57:23 -0700720void AIBinder_DeathRecipient_delete(AIBinder_DeathRecipient* recipient) {
Steven Moreland64127ca2019-03-13 09:25:44 -0700721 if (recipient == nullptr) {
722 return;
723 }
724
725 recipient->decStrong(nullptr);
Steven Moreland901c7452018-09-04 16:17:28 -0700726}
Steven Morelandea14ef22019-08-20 10:50:08 -0700727
728binder_status_t AIBinder_getExtension(AIBinder* binder, AIBinder** outExt) {
729 if (binder == nullptr || outExt == nullptr) {
730 if (outExt != nullptr) {
731 *outExt = nullptr;
732 }
733 return STATUS_UNEXPECTED_NULL;
734 }
735
736 sp<IBinder> ext;
737 status_t res = binder->getBinder()->getExtension(&ext);
738
739 if (res != android::OK) {
740 *outExt = nullptr;
741 return PruneStatusT(res);
742 }
743
744 sp<AIBinder> ret = ABpBinder::lookupOrCreateFromBinder(ext);
745 if (ret != nullptr) ret->incStrong(binder);
746
747 *outExt = ret.get();
748 return STATUS_OK;
749}
750
751binder_status_t AIBinder_setExtension(AIBinder* binder, AIBinder* ext) {
752 if (binder == nullptr || ext == nullptr) {
753 return STATUS_UNEXPECTED_NULL;
754 }
755
756 ABBinder* rawBinder = binder->asABBinder();
757 if (rawBinder == nullptr) {
758 return STATUS_INVALID_OPERATION;
759 }
760
761 rawBinder->setExtension(ext->getBinder());
762 return STATUS_OK;
763}
Steven Moreland2f405f52020-07-08 22:24:29 +0000764
765// platform methods follow
766
767void AIBinder_setRequestingSid(AIBinder* binder, bool requestingSid) {
768 ABBinder* localBinder = binder->asABBinder();
769 if (localBinder == nullptr) {
770 LOG(FATAL) << "AIBinder_setRequestingSid must be called on a local binder";
771 }
772
773 localBinder->setRequestingSid(requestingSid);
774}
775
776const char* AIBinder_getCallingSid() {
777 return ::android::IPCThreadState::self()->getCallingSid();
778}
Steven Morelandb2de9532020-05-29 21:44:41 +0000779
Ady Abraham96174ac2021-10-15 11:02:03 -0700780void AIBinder_setMinSchedulerPolicy(AIBinder* binder, int policy, int priority) {
781 binder->asABBinder()->setMinSchedulerPolicy(policy, priority);
782}
Steven Moreland454a26a2021-12-14 01:26:21 +0000783
784void AIBinder_setInheritRt(AIBinder* binder, bool inheritRt) {
785 ABBinder* localBinder = binder->asABBinder();
786 if (localBinder == nullptr) {
787 LOG(FATAL) << "AIBinder_setInheritRt must be called on a local binder";
788 }
789
790 localBinder->setInheritRt(inheritRt);
791}